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" |
21 #include "webrtc/base/random.h" | |
stefan-webrtc
2016/01/26 11:50:47
Alphabetic order
philipel
2016/01/26 12:21:38
Done.
| |
19 #include "webrtc/typedefs.h" | 22 #include "webrtc/typedefs.h" |
20 | 23 |
21 namespace webrtc { | 24 namespace webrtc { |
22 | 25 |
23 class Clock; | 26 class Clock; |
24 class CriticalSectionWrapper; | 27 class CriticalSectionWrapper; |
25 class NetworkPacket; | |
26 class PacketReceiver; | 28 class PacketReceiver; |
27 | 29 |
30 class NetworkPacket { | |
31 public: | |
32 NetworkPacket(const uint8_t* data, size_t length, int64_t send_time, | |
33 int64_t arrival_time) | |
34 : data_(new uint8_t[length]), | |
35 data_length_(length), | |
36 send_time_(send_time), | |
37 arrival_time_(arrival_time) { | |
38 memcpy(data_.get(), data, length); | |
39 } | |
40 | |
41 uint8_t* data() const { return data_.get(); } | |
42 size_t data_length() const { return data_length_; } | |
43 int64_t send_time() const { return send_time_; } | |
44 int64_t arrival_time() const { return arrival_time_; } | |
45 void IncrementArrivalTime(int64_t extra_delay) { | |
46 arrival_time_+= extra_delay; | |
47 } | |
48 | |
49 private: | |
50 // The packet data. | |
51 rtc::scoped_ptr<uint8_t> data_; | |
52 // Length of data_. | |
53 size_t data_length_; | |
54 // The time the packet was sent out on the network. | |
55 const int64_t send_time_; | |
56 // The time the packet should arrive at the receiver. | |
57 int64_t arrival_time_; | |
58 }; | |
59 | |
28 // Class faking a network link. This is a simple and naive solution just faking | 60 // 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 | 61 // capacity and adding an extra transport delay in addition to the capacity |
30 // introduced delay. | 62 // introduced delay. |
31 | 63 |
32 // TODO(mflodman) Add random and bursty packet loss. | 64 // TODO(mflodman) Add random and bursty packet loss. |
33 class FakeNetworkPipe { | 65 class FakeNetworkPipe { |
34 public: | 66 public: |
35 struct Config { | 67 struct Config { |
36 Config() {} | 68 Config() {} |
37 // Queue length in number of packets. | 69 // Queue length in number of packets. |
38 size_t queue_length_packets = 0; | 70 size_t queue_length_packets = 0; |
39 // Delay in addition to capacity induced delay. | 71 // Delay in addition to capacity induced delay. |
40 int queue_delay_ms = 0; | 72 int queue_delay_ms = 0; |
41 // Standard deviation of the extra delay. | 73 // Standard deviation of the extra delay. |
42 int delay_standard_deviation_ms = 0; | 74 int delay_standard_deviation_ms = 0; |
43 // Link capacity in kbps. | 75 // Link capacity in kbps. |
44 int link_capacity_kbps = 0; | 76 int link_capacity_kbps = 0; |
45 // Random packet loss. | 77 // Random packet loss. |
46 int loss_percent = 0; | 78 int loss_percent = 0; |
79 // If packets are allowed to be reordered. | |
80 bool allow_reordering = false; | |
47 }; | 81 }; |
48 | 82 |
49 FakeNetworkPipe(Clock* clock, const FakeNetworkPipe::Config& config); | 83 FakeNetworkPipe(Clock* clock, |
84 const FakeNetworkPipe::Config& config, | |
85 uint64_t seed = 1); | |
stefan-webrtc
2016/01/26 11:50:47
We generally try to avoid default values. Maybe yo
philipel
2016/01/26 12:21:38
Done.
| |
50 ~FakeNetworkPipe(); | 86 ~FakeNetworkPipe(); |
51 | 87 |
52 // Must not be called in parallel with SendPacket or Process. | 88 // Must not be called in parallel with SendPacket or Process. |
53 void SetReceiver(PacketReceiver* receiver); | 89 void SetReceiver(PacketReceiver* receiver); |
54 | 90 |
55 // Sets a new configuration. This won't affect packets already in the pipe. | 91 // Sets a new configuration. This won't affect packets already in the pipe. |
56 void SetConfig(const FakeNetworkPipe::Config& config); | 92 void SetConfig(const FakeNetworkPipe::Config& config); |
57 | 93 |
58 // Sends a new packet to the link. | 94 // Sends a new packet to the link. |
59 void SendPacket(const uint8_t* packet, size_t packet_length); | 95 void SendPacket(const uint8_t* packet, size_t packet_length); |
60 | 96 |
61 // Processes the network queues and trigger PacketReceiver::IncomingPacket for | 97 // Processes the network queues and trigger PacketReceiver::IncomingPacket for |
62 // packets ready to be delivered. | 98 // packets ready to be delivered. |
63 void Process(); | 99 void Process(); |
64 int64_t TimeUntilNextProcess() const; | 100 int64_t TimeUntilNextProcess() const; |
65 | 101 |
66 // Get statistics. | 102 // Get statistics. |
67 float PercentageLoss(); | 103 float PercentageLoss(); |
68 int AverageDelay(); | 104 int AverageDelay(); |
69 size_t dropped_packets() { return dropped_packets_; } | 105 size_t dropped_packets() { return dropped_packets_; } |
70 size_t sent_packets() { return sent_packets_; } | 106 size_t sent_packets() { return sent_packets_; } |
71 | 107 |
72 private: | 108 private: |
73 Clock* const clock_; | 109 Clock* const clock_; |
74 mutable rtc::CriticalSection lock_; | 110 mutable rtc::CriticalSection lock_; |
75 PacketReceiver* packet_receiver_; | 111 PacketReceiver* packet_receiver_; |
76 std::queue<NetworkPacket*> capacity_link_; | 112 std::queue<NetworkPacket*> capacity_link_; |
77 std::queue<NetworkPacket*> delay_link_; | 113 Random random_; |
114 | |
115 // Since we need to access both the packet with the earliest and latest | |
116 // arrival time we need to use a multiset to keep all packets sorted, | |
117 // hence, we cannot use a priority queue. | |
118 struct PacketArrivalTimeComparator { | |
119 bool operator()(const NetworkPacket* p1, const NetworkPacket* p2) { | |
120 return p1->arrival_time() < p2->arrival_time(); | |
121 } | |
122 }; | |
123 std::multiset<NetworkPacket*, PacketArrivalTimeComparator> delay_link_; | |
78 | 124 |
79 // Link configuration. | 125 // Link configuration. |
80 Config config_; | 126 Config config_; |
81 | 127 |
82 // Statistics. | 128 // Statistics. |
83 size_t dropped_packets_; | 129 size_t dropped_packets_; |
84 size_t sent_packets_; | 130 size_t sent_packets_; |
85 int total_packet_delay_; | 131 int total_packet_delay_; |
86 | 132 |
87 int64_t next_process_time_; | 133 int64_t next_process_time_; |
88 | 134 |
89 RTC_DISALLOW_COPY_AND_ASSIGN(FakeNetworkPipe); | 135 RTC_DISALLOW_COPY_AND_ASSIGN(FakeNetworkPipe); |
90 }; | 136 }; |
91 | 137 |
92 } // namespace webrtc | 138 } // namespace webrtc |
93 | 139 |
94 #endif // WEBRTC_TEST_FAKE_NETWORK_PIPE_H_ | 140 #endif // WEBRTC_TEST_FAKE_NETWORK_PIPE_H_ |
OLD | NEW |