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 <string.h> |
| 15 #include <map> |
14 #include <memory> | 16 #include <memory> |
| 17 #include <queue> |
15 #include <set> | 18 #include <set> |
16 #include <string.h> | |
17 #include <queue> | |
18 | 19 |
19 #include "webrtc/base/constructormagic.h" | 20 #include "webrtc/base/constructormagic.h" |
20 #include "webrtc/base/criticalsection.h" | 21 #include "webrtc/base/criticalsection.h" |
21 #include "webrtc/base/random.h" | 22 #include "webrtc/base/random.h" |
| 23 #include "webrtc/common_types.h" |
22 #include "webrtc/typedefs.h" | 24 #include "webrtc/typedefs.h" |
23 | 25 |
24 namespace webrtc { | 26 namespace webrtc { |
25 | 27 |
26 class Clock; | 28 class Clock; |
27 class PacketReceiver; | 29 class PacketReceiver; |
28 enum class MediaType; | 30 enum class MediaType; |
29 | 31 |
30 class NetworkPacket { | 32 class NetworkPacket { |
31 public: | 33 public: |
(...skipping 20 matching lines...) Expand all Loading... |
52 // The packet data. | 54 // The packet data. |
53 std::unique_ptr<uint8_t[]> data_; | 55 std::unique_ptr<uint8_t[]> data_; |
54 // Length of data_. | 56 // Length of data_. |
55 size_t data_length_; | 57 size_t data_length_; |
56 // The time the packet was sent out on the network. | 58 // The time the packet was sent out on the network. |
57 const int64_t send_time_; | 59 const int64_t send_time_; |
58 // The time the packet should arrive at the receiver. | 60 // The time the packet should arrive at the receiver. |
59 int64_t arrival_time_; | 61 int64_t arrival_time_; |
60 }; | 62 }; |
61 | 63 |
| 64 class Demuxer { |
| 65 public: |
| 66 virtual ~Demuxer() = default; |
| 67 virtual void SetReceiver(PacketReceiver* receiver) = 0; |
| 68 virtual void DeliverPacket(const NetworkPacket* packet, |
| 69 const PacketTime& packet_time) = 0; |
| 70 }; |
| 71 |
| 72 class DemuxerImpl final : public Demuxer { |
| 73 public: |
| 74 explicit DemuxerImpl(const std::map<uint8_t, MediaType>& payload_type_map); |
| 75 |
| 76 void SetReceiver(PacketReceiver* receiver) override; |
| 77 void DeliverPacket(const NetworkPacket* packet, |
| 78 const PacketTime& packet_time) override; |
| 79 |
| 80 private: |
| 81 PacketReceiver* packet_receiver_; |
| 82 const std::map<uint8_t, MediaType> payload_type_map_; |
| 83 RTC_DISALLOW_COPY_AND_ASSIGN(DemuxerImpl); |
| 84 }; |
| 85 |
62 // Class faking a network link. This is a simple and naive solution just faking | 86 // Class faking a network link. This is a simple and naive solution just faking |
63 // capacity and adding an extra transport delay in addition to the capacity | 87 // capacity and adding an extra transport delay in addition to the capacity |
64 // introduced delay. | 88 // introduced delay. |
65 | 89 |
66 class FakeNetworkPipe { | 90 class FakeNetworkPipe { |
67 public: | 91 public: |
68 struct Config { | 92 struct Config { |
69 Config() {} | 93 Config() {} |
70 // Queue length in number of packets. | 94 // Queue length in number of packets. |
71 size_t queue_length_packets = 0; | 95 size_t queue_length_packets = 0; |
72 // Delay in addition to capacity induced delay. | 96 // Delay in addition to capacity induced delay. |
73 int queue_delay_ms = 0; | 97 int queue_delay_ms = 0; |
74 // Standard deviation of the extra delay. | 98 // Standard deviation of the extra delay. |
75 int delay_standard_deviation_ms = 0; | 99 int delay_standard_deviation_ms = 0; |
76 // Link capacity in kbps. | 100 // Link capacity in kbps. |
77 int link_capacity_kbps = 0; | 101 int link_capacity_kbps = 0; |
78 // Random packet loss. | 102 // Random packet loss. |
79 int loss_percent = 0; | 103 int loss_percent = 0; |
80 // If packets are allowed to be reordered. | 104 // If packets are allowed to be reordered. |
81 bool allow_reordering = false; | 105 bool allow_reordering = false; |
82 // The average length of a burst of lost packets. | 106 // The average length of a burst of lost packets. |
83 int avg_burst_loss_length = -1; | 107 int avg_burst_loss_length = -1; |
84 }; | 108 }; |
85 | 109 |
86 FakeNetworkPipe(Clock* clock, const FakeNetworkPipe::Config& config, | |
87 MediaType media_type); | |
88 FakeNetworkPipe(Clock* clock, | 110 FakeNetworkPipe(Clock* clock, |
89 const FakeNetworkPipe::Config& config, MediaType media_type, | 111 const FakeNetworkPipe::Config& config, |
| 112 std::unique_ptr<Demuxer> demuxer); |
| 113 FakeNetworkPipe(Clock* clock, |
| 114 const FakeNetworkPipe::Config& config, |
| 115 std::unique_ptr<Demuxer> demuxer, |
90 uint64_t seed); | 116 uint64_t seed); |
91 ~FakeNetworkPipe(); | 117 ~FakeNetworkPipe(); |
92 | 118 |
93 // Must not be called in parallel with SendPacket or Process. | |
94 void SetReceiver(PacketReceiver* receiver); | |
95 | 119 |
96 // Sets a new configuration. This won't affect packets already in the pipe. | 120 // Sets a new configuration. This won't affect packets already in the pipe. |
97 void SetConfig(const FakeNetworkPipe::Config& config); | 121 void SetConfig(const FakeNetworkPipe::Config& config); |
98 | 122 |
99 // Sends a new packet to the link. | 123 // Sends a new packet to the link. |
100 void SendPacket(const uint8_t* packet, size_t packet_length); | 124 void SendPacket(const uint8_t* packet, size_t packet_length); |
101 | 125 |
| 126 // Must not be called in parallel with SendPacket or Process. |
| 127 void SetReceiver(PacketReceiver* receiver); |
| 128 |
102 // Processes the network queues and trigger PacketReceiver::IncomingPacket for | 129 // Processes the network queues and trigger PacketReceiver::IncomingPacket for |
103 // packets ready to be delivered. | 130 // packets ready to be delivered. |
104 void Process(); | 131 void Process(); |
105 int64_t TimeUntilNextProcess() const; | 132 int64_t TimeUntilNextProcess() const; |
106 | 133 |
107 // Get statistics. | 134 // Get statistics. |
108 float PercentageLoss(); | 135 float PercentageLoss(); |
109 int AverageDelay(); | 136 int AverageDelay(); |
110 size_t dropped_packets() { return dropped_packets_; } | 137 size_t dropped_packets() { return dropped_packets_; } |
111 size_t sent_packets() { return sent_packets_; } | 138 size_t sent_packets() { return sent_packets_; } |
112 | 139 |
113 private: | 140 private: |
114 Clock* const clock_; | 141 Clock* const clock_; |
115 const MediaType media_type_; | |
116 rtc::CriticalSection lock_; | 142 rtc::CriticalSection lock_; |
117 PacketReceiver* packet_receiver_; | 143 const std::unique_ptr<Demuxer> demuxer_; |
118 std::queue<NetworkPacket*> capacity_link_; | 144 std::queue<NetworkPacket*> capacity_link_; |
119 Random random_; | 145 Random random_; |
120 | 146 |
121 // Since we need to access both the packet with the earliest and latest | 147 // Since we need to access both the packet with the earliest and latest |
122 // arrival time we need to use a multiset to keep all packets sorted, | 148 // arrival time we need to use a multiset to keep all packets sorted, |
123 // hence, we cannot use a priority queue. | 149 // hence, we cannot use a priority queue. |
124 struct PacketArrivalTimeComparator { | 150 struct PacketArrivalTimeComparator { |
125 bool operator()(const NetworkPacket* p1, const NetworkPacket* p2) { | 151 bool operator()(const NetworkPacket* p1, const NetworkPacket* p2) { |
126 return p1->arrival_time() < p2->arrival_time(); | 152 return p1->arrival_time() < p2->arrival_time(); |
127 } | 153 } |
(...skipping 21 matching lines...) Expand all Loading... |
149 int64_t next_process_time_; | 175 int64_t next_process_time_; |
150 | 176 |
151 int64_t last_log_time_; | 177 int64_t last_log_time_; |
152 | 178 |
153 RTC_DISALLOW_COPY_AND_ASSIGN(FakeNetworkPipe); | 179 RTC_DISALLOW_COPY_AND_ASSIGN(FakeNetworkPipe); |
154 }; | 180 }; |
155 | 181 |
156 } // namespace webrtc | 182 } // namespace webrtc |
157 | 183 |
158 #endif // WEBRTC_TEST_FAKE_NETWORK_PIPE_H_ | 184 #endif // WEBRTC_TEST_FAKE_NETWORK_PIPE_H_ |
OLD | NEW |