OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_TEST_FAKE_NETWORK_PIPE_H_ | 11 #ifndef WEBRTC_TEST_FAKE_NETWORK_PIPE_H_ |
12 #define WEBRTC_TEST_FAKE_NETWORK_PIPE_H_ | 12 #define WEBRTC_TEST_FAKE_NETWORK_PIPE_H_ |
13 | 13 |
14 #include <set> | |
15 #include <string.h> | |
14 #include <queue> | 16 #include <queue> |
15 | 17 |
16 #include "webrtc/base/constructormagic.h" | 18 #include "webrtc/base/constructormagic.h" |
17 #include "webrtc/base/criticalsection.h" | 19 #include "webrtc/base/criticalsection.h" |
18 #include "webrtc/base/scoped_ptr.h" | 20 #include "webrtc/base/scoped_ptr.h" |
19 #include "webrtc/typedefs.h" | 21 #include "webrtc/typedefs.h" |
20 | 22 |
21 namespace webrtc { | 23 namespace webrtc { |
22 | 24 |
23 class Clock; | 25 class Clock; |
24 class CriticalSectionWrapper; | 26 class CriticalSectionWrapper; |
25 class NetworkPacket; | |
26 class PacketReceiver; | 27 class PacketReceiver; |
27 | 28 |
29 class NetworkPacket { | |
30 public: | |
31 NetworkPacket(const uint8_t* data, size_t length, int64_t send_time, | |
32 int64_t arrival_time) | |
33 : data_(NULL), | |
34 data_length_(length), | |
35 send_time_(send_time), | |
36 arrival_time_(arrival_time) { | |
37 data_ = new uint8_t[length]; | |
38 memcpy(data_, data, length); | |
39 } | |
40 ~NetworkPacket() { | |
41 delete [] data_; | |
42 } | |
43 | |
44 uint8_t* data() const { return data_; } | |
45 size_t data_length() const { return data_length_; } | |
46 int64_t send_time() const { return send_time_; } | |
47 int64_t arrival_time() const { return arrival_time_; } | |
48 void IncrementArrivalTime(int64_t extra_delay) { | |
49 arrival_time_+= extra_delay; | |
50 } | |
51 | |
52 private: | |
53 // The packet data. | |
54 uint8_t* data_; | |
stefan-webrtc
2016/01/20 10:57:33
Please change this to a scoped_ptr<uint8_t[]>, or
philipel
2016/01/20 14:22:57
Done.
| |
55 // Length of data_. | |
56 size_t data_length_; | |
57 // The time the packet was sent out on the network. | |
58 const int64_t send_time_; | |
59 // The time the packet should arrive at the reciver. | |
60 int64_t arrival_time_; | |
61 }; | |
62 | |
28 // Class faking a network link. This is a simple and naive solution just faking | 63 // Class faking a network link. This is a simple and naive solution just faking |
29 // capacity and adding an extra transport delay in addition to the capacity | 64 // capacity and adding an extra transport delay in addition to the capacity |
30 // introduced delay. | 65 // introduced delay. |
31 | 66 |
32 // TODO(mflodman) Add random and bursty packet loss. | 67 // TODO(mflodman) Add random and bursty packet loss. |
33 class FakeNetworkPipe { | 68 class FakeNetworkPipe { |
34 public: | 69 public: |
35 struct Config { | 70 struct Config { |
36 Config() {} | 71 Config() {} |
37 // Queue length in number of packets. | 72 // Queue length in number of packets. |
38 size_t queue_length_packets = 0; | 73 size_t queue_length_packets = 0; |
39 // Delay in addition to capacity induced delay. | 74 // Delay in addition to capacity induced delay. |
40 int queue_delay_ms = 0; | 75 int queue_delay_ms = 0; |
41 // Standard deviation of the extra delay. | 76 // Standard deviation of the extra delay. |
42 int delay_standard_deviation_ms = 0; | 77 int delay_standard_deviation_ms = 0; |
43 // Link capacity in kbps. | 78 // Link capacity in kbps. |
44 int link_capacity_kbps = 0; | 79 int link_capacity_kbps = 0; |
45 // Random packet loss. | 80 // Random packet loss. |
46 int loss_percent = 0; | 81 int loss_percent = 0; |
82 // If packets are allowed to be reordered. | |
83 bool allow_reordering = false; | |
47 }; | 84 }; |
48 | 85 |
49 FakeNetworkPipe(Clock* clock, const FakeNetworkPipe::Config& config); | 86 FakeNetworkPipe(Clock* clock, const FakeNetworkPipe::Config& config); |
50 ~FakeNetworkPipe(); | 87 ~FakeNetworkPipe(); |
51 | 88 |
52 // Must not be called in parallel with SendPacket or Process. | 89 // Must not be called in parallel with SendPacket or Process. |
53 void SetReceiver(PacketReceiver* receiver); | 90 void SetReceiver(PacketReceiver* receiver); |
54 | 91 |
55 // Sets a new configuration. This won't affect packets already in the pipe. | 92 // Sets a new configuration. This won't affect packets already in the pipe. |
56 void SetConfig(const FakeNetworkPipe::Config& config); | 93 void SetConfig(const FakeNetworkPipe::Config& config); |
(...skipping 10 matching lines...) Expand all Loading... | |
67 float PercentageLoss(); | 104 float PercentageLoss(); |
68 int AverageDelay(); | 105 int AverageDelay(); |
69 size_t dropped_packets() { return dropped_packets_; } | 106 size_t dropped_packets() { return dropped_packets_; } |
70 size_t sent_packets() { return sent_packets_; } | 107 size_t sent_packets() { return sent_packets_; } |
71 | 108 |
72 private: | 109 private: |
73 Clock* const clock_; | 110 Clock* const clock_; |
74 mutable rtc::CriticalSection lock_; | 111 mutable rtc::CriticalSection lock_; |
75 PacketReceiver* packet_receiver_; | 112 PacketReceiver* packet_receiver_; |
76 std::queue<NetworkPacket*> capacity_link_; | 113 std::queue<NetworkPacket*> capacity_link_; |
77 std::queue<NetworkPacket*> delay_link_; | 114 |
115 struct PacketArrivalTimeComperator { | |
stefan-webrtc
2016/01/20 10:57:33
Comparator
philipel
2016/01/20 14:22:57
Done.
| |
116 bool operator()(const NetworkPacket* p1, const NetworkPacket* p2) { | |
117 return p1->arrival_time() < p2->arrival_time(); | |
118 } | |
119 }; | |
120 std::multiset<NetworkPacket*, PacketArrivalTimeComperator> delay_link_; | |
stefan-webrtc
2016/01/20 10:57:33
Maybe add a comment on why this needs to be sorted
philipel
2016/01/20 14:22:57
Done.
| |
78 | 121 |
79 // Link configuration. | 122 // Link configuration. |
80 Config config_; | 123 Config config_; |
81 | 124 |
82 // Statistics. | 125 // Statistics. |
83 size_t dropped_packets_; | 126 size_t dropped_packets_; |
84 size_t sent_packets_; | 127 size_t sent_packets_; |
85 int total_packet_delay_; | 128 int total_packet_delay_; |
86 | 129 |
87 int64_t next_process_time_; | 130 int64_t next_process_time_; |
88 | 131 |
89 RTC_DISALLOW_COPY_AND_ASSIGN(FakeNetworkPipe); | 132 RTC_DISALLOW_COPY_AND_ASSIGN(FakeNetworkPipe); |
90 }; | 133 }; |
91 | 134 |
92 } // namespace webrtc | 135 } // namespace webrtc |
93 | 136 |
94 #endif // WEBRTC_TEST_FAKE_NETWORK_PIPE_H_ | 137 #endif // WEBRTC_TEST_FAKE_NETWORK_PIPE_H_ |
OLD | NEW |