| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MEDIA_CAST_TEST_UTILITY_UDP_PROXY_H_ | 5 #ifndef MEDIA_CAST_TEST_UTILITY_UDP_PROXY_H_ |
| 6 #define MEDIA_CAST_TEST_UTILITY_UDP_PROXY_H_ | 6 #define MEDIA_CAST_TEST_UTILITY_UDP_PROXY_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <memory> |
| 11 #include <vector> | 12 #include <vector> |
| 12 | 13 |
| 13 #include "base/macros.h" | 14 #include "base/macros.h" |
| 14 #include "base/memory/linked_ptr.h" | 15 #include "base/memory/linked_ptr.h" |
| 15 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/scoped_ptr.h" | |
| 17 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
| 18 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
| 19 #include "media/cast/net/cast_transport_config.h" | 19 #include "media/cast/net/cast_transport_config.h" |
| 20 #include "net/base/ip_endpoint.h" | 20 #include "net/base/ip_endpoint.h" |
| 21 #include "third_party/mt19937ar/mt19937ar.h" | 21 #include "third_party/mt19937ar/mt19937ar.h" |
| 22 | 22 |
| 23 namespace net { | 23 namespace net { |
| 24 class NetLog; | 24 class NetLog; |
| 25 }; | 25 }; |
| 26 | 26 |
| 27 namespace base { | 27 namespace base { |
| 28 class TickClock; | 28 class TickClock; |
| 29 }; | 29 }; |
| 30 | 30 |
| 31 namespace media { | 31 namespace media { |
| 32 namespace cast { | 32 namespace cast { |
| 33 namespace test { | 33 namespace test { |
| 34 | 34 |
| 35 class PacketPipe { | 35 class PacketPipe { |
| 36 public: | 36 public: |
| 37 PacketPipe(); | 37 PacketPipe(); |
| 38 virtual ~PacketPipe(); | 38 virtual ~PacketPipe(); |
| 39 virtual void Send(scoped_ptr<Packet> packet) = 0; | 39 virtual void Send(std::unique_ptr<Packet> packet) = 0; |
| 40 // Allows injection of fake test runner for testing. | 40 // Allows injection of fake test runner for testing. |
| 41 virtual void InitOnIOThread( | 41 virtual void InitOnIOThread( |
| 42 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 42 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 43 base::TickClock* clock); | 43 base::TickClock* clock); |
| 44 virtual void AppendToPipe(scoped_ptr<PacketPipe> pipe); | 44 virtual void AppendToPipe(std::unique_ptr<PacketPipe> pipe); |
| 45 |
| 45 protected: | 46 protected: |
| 46 scoped_ptr<PacketPipe> pipe_; | 47 std::unique_ptr<PacketPipe> pipe_; |
| 47 // Allows injection of fake task runner for testing. | 48 // Allows injection of fake task runner for testing. |
| 48 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 49 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 49 base::TickClock* clock_; | 50 base::TickClock* clock_; |
| 50 }; | 51 }; |
| 51 | 52 |
| 52 // Implements a Interrupted Poisson Process for packet delivery. | 53 // Implements a Interrupted Poisson Process for packet delivery. |
| 53 // The process has 2 states: ON and OFF, the rate of switching between | 54 // The process has 2 states: ON and OFF, the rate of switching between |
| 54 // these two states are defined. | 55 // these two states are defined. |
| 55 // When in ON state packets are sent according to a defined rate. | 56 // When in ON state packets are sent according to a defined rate. |
| 56 // When in OFF state packets are not sent. | 57 // When in OFF state packets are not sent. |
| 57 // The rate above is the average rate of a poisson distribution. | 58 // The rate above is the average rate of a poisson distribution. |
| 58 class InterruptedPoissonProcess { | 59 class InterruptedPoissonProcess { |
| 59 public: | 60 public: |
| 60 InterruptedPoissonProcess(const std::vector<double>& average_rates, | 61 InterruptedPoissonProcess(const std::vector<double>& average_rates, |
| 61 double coef_burstiness, | 62 double coef_burstiness, |
| 62 double coef_variance, | 63 double coef_variance, |
| 63 uint32_t rand_seed); | 64 uint32_t rand_seed); |
| 64 ~InterruptedPoissonProcess(); | 65 ~InterruptedPoissonProcess(); |
| 65 | 66 |
| 66 scoped_ptr<PacketPipe> NewBuffer(size_t size); | 67 std::unique_ptr<PacketPipe> NewBuffer(size_t size); |
| 67 | 68 |
| 68 private: | 69 private: |
| 69 class InternalBuffer; | 70 class InternalBuffer; |
| 70 | 71 |
| 71 // |task_runner| is the executor of the IO thread. | 72 // |task_runner| is the executor of the IO thread. |
| 72 // |clock| is the system clock. | 73 // |clock| is the system clock. |
| 73 void InitOnIOThread( | 74 void InitOnIOThread( |
| 74 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 75 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 75 base::TickClock* clock); | 76 base::TickClock* clock); |
| 76 | 77 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 // Packets send to that port will be forwarded to |destination|. | 110 // Packets send to that port will be forwarded to |destination|. |
| 110 // Packets send from |destination| to |local_port| will be returned | 111 // Packets send from |destination| to |local_port| will be returned |
| 111 // to whoever sent a packet to |local_port| last. (Not counting packets | 112 // to whoever sent a packet to |local_port| last. (Not counting packets |
| 112 // from |destination|.) The UDPProxy will run a separate thread to | 113 // from |destination|.) The UDPProxy will run a separate thread to |
| 113 // do the forwarding of packets, and will keep doing so until destroyed. | 114 // do the forwarding of packets, and will keep doing so until destroyed. |
| 114 // You can insert delays and packet drops by supplying a PacketPipe. | 115 // You can insert delays and packet drops by supplying a PacketPipe. |
| 115 // The PacketPipes may also be NULL if you just want to forward packets. | 116 // The PacketPipes may also be NULL if you just want to forward packets. |
| 116 class UDPProxy { | 117 class UDPProxy { |
| 117 public: | 118 public: |
| 118 virtual ~UDPProxy() {} | 119 virtual ~UDPProxy() {} |
| 119 static scoped_ptr<UDPProxy> Create(const net::IPEndPoint& local_port, | 120 static std::unique_ptr<UDPProxy> Create( |
| 120 const net::IPEndPoint& destination, | 121 const net::IPEndPoint& local_port, |
| 121 scoped_ptr<PacketPipe> to_dest_pipe, | 122 const net::IPEndPoint& destination, |
| 122 scoped_ptr<PacketPipe> from_dest_pipe, | 123 std::unique_ptr<PacketPipe> to_dest_pipe, |
| 123 net::NetLog* net_log); | 124 std::unique_ptr<PacketPipe> from_dest_pipe, |
| 125 net::NetLog* net_log); |
| 124 }; | 126 }; |
| 125 | 127 |
| 126 // The following functions create PacketPipes which can be linked | 128 // The following functions create PacketPipes which can be linked |
| 127 // together (with AppendToPipe) and passed into UdpProxy::Create below. | 129 // together (with AppendToPipe) and passed into UdpProxy::Create below. |
| 128 | 130 |
| 129 // This PacketPipe emulates a buffer of a given size. Limits our output | 131 // This PacketPipe emulates a buffer of a given size. Limits our output |
| 130 // from the buffer at a rate given by |bandwidth| (in megabits per second). | 132 // from the buffer at a rate given by |bandwidth| (in megabits per second). |
| 131 // Packets entering the buffer will be dropped if there is not enough | 133 // Packets entering the buffer will be dropped if there is not enough |
| 132 // room for them. | 134 // room for them. |
| 133 scoped_ptr<PacketPipe> NewBuffer(size_t buffer_size, double bandwidth); | 135 std::unique_ptr<PacketPipe> NewBuffer(size_t buffer_size, double bandwidth); |
| 134 | 136 |
| 135 // Randomly drops |drop_fraction|*100% of packets. | 137 // Randomly drops |drop_fraction|*100% of packets. |
| 136 scoped_ptr<PacketPipe> NewRandomDrop(double drop_fraction); | 138 std::unique_ptr<PacketPipe> NewRandomDrop(double drop_fraction); |
| 137 | 139 |
| 138 // Delays each packet by |delay_seconds|. | 140 // Delays each packet by |delay_seconds|. |
| 139 scoped_ptr<PacketPipe> NewConstantDelay(double delay_seconds); | 141 std::unique_ptr<PacketPipe> NewConstantDelay(double delay_seconds); |
| 140 | 142 |
| 141 // Delays packets by a random amount between zero and |delay|. | 143 // Delays packets by a random amount between zero and |delay|. |
| 142 // This PacketPipe can reorder packets. | 144 // This PacketPipe can reorder packets. |
| 143 scoped_ptr<PacketPipe> NewRandomUnsortedDelay(double delay); | 145 std::unique_ptr<PacketPipe> NewRandomUnsortedDelay(double delay); |
| 144 | 146 |
| 145 // Duplicates every packet, one is transmitted immediately, | 147 // Duplicates every packet, one is transmitted immediately, |
| 146 // one is transmitted after a random delay between |delay_min| | 148 // one is transmitted after a random delay between |delay_min| |
| 147 // and |delay_min + random_delay|. | 149 // and |delay_min + random_delay|. |
| 148 // This PacketPipe will reorder packets. | 150 // This PacketPipe will reorder packets. |
| 149 scoped_ptr<PacketPipe> NewDuplicateAndDelay(double delay_min, | 151 std::unique_ptr<PacketPipe> NewDuplicateAndDelay(double delay_min, |
| 150 double random_delay); | 152 double random_delay); |
| 151 | 153 |
| 152 // This PacketPipe inserts a random delay between each packet. | 154 // This PacketPipe inserts a random delay between each packet. |
| 153 // This PacketPipe cannot re-order packets. The delay between each | 155 // This PacketPipe cannot re-order packets. The delay between each |
| 154 // packet is asically |min_delay| + random( |random_delay| ) | 156 // packet is asically |min_delay| + random( |random_delay| ) |
| 155 // However, every now and then a delay of |big_delay| will be | 157 // However, every now and then a delay of |big_delay| will be |
| 156 // inserted (roughly every |seconds_between_big_delay| seconds). | 158 // inserted (roughly every |seconds_between_big_delay| seconds). |
| 157 scoped_ptr<PacketPipe> NewRandomSortedDelay(double random_delay, | 159 std::unique_ptr<PacketPipe> NewRandomSortedDelay( |
| 158 double big_delay, | 160 double random_delay, |
| 159 double seconds_between_big_delay); | 161 double big_delay, |
| 162 double seconds_between_big_delay); |
| 160 | 163 |
| 161 // This PacketPipe emulates network outages. It basically waits | 164 // This PacketPipe emulates network outages. It basically waits |
| 162 // for 0-2*|average_work_time| seconds, then kills the network for | 165 // for 0-2*|average_work_time| seconds, then kills the network for |
| 163 // 0-|2*average_outage_time| seconds. Then it starts over again. | 166 // 0-|2*average_outage_time| seconds. Then it starts over again. |
| 164 scoped_ptr<PacketPipe> NewNetworkGlitchPipe(double average_work_time, | 167 std::unique_ptr<PacketPipe> NewNetworkGlitchPipe(double average_work_time, |
| 165 double average_outage_time); | 168 double average_outage_time); |
| 166 | 169 |
| 167 // This method builds a stack of PacketPipes to emulate a reasonably | 170 // This method builds a stack of PacketPipes to emulate a reasonably |
| 168 // good network. ~50mbit, ~3ms latency, no packet loss unless saturated. | 171 // good network. ~50mbit, ~3ms latency, no packet loss unless saturated. |
| 169 scoped_ptr<PacketPipe> GoodNetwork(); | 172 std::unique_ptr<PacketPipe> GoodNetwork(); |
| 170 | 173 |
| 171 // This method builds a stack of PacketPipes to emulate a reasonably | 174 // This method builds a stack of PacketPipes to emulate a reasonably |
| 172 // good wifi network. ~20mbit, 1% packet loss, ~3ms latency. | 175 // good wifi network. ~20mbit, 1% packet loss, ~3ms latency. |
| 173 scoped_ptr<PacketPipe> WifiNetwork(); | 176 std::unique_ptr<PacketPipe> WifiNetwork(); |
| 174 | 177 |
| 175 // This method builds a stack of PacketPipes to emulate a | 178 // This method builds a stack of PacketPipes to emulate a |
| 176 // bad wifi network. ~5mbit, 5% packet loss, ~7ms latency | 179 // bad wifi network. ~5mbit, 5% packet loss, ~7ms latency |
| 177 // 40ms dropouts every ~2 seconds. Can reorder packets. | 180 // 40ms dropouts every ~2 seconds. Can reorder packets. |
| 178 scoped_ptr<PacketPipe> BadNetwork(); | 181 std::unique_ptr<PacketPipe> BadNetwork(); |
| 179 | 182 |
| 180 // This method builds a stack of PacketPipes to emulate a crappy wifi network. | 183 // This method builds a stack of PacketPipes to emulate a crappy wifi network. |
| 181 // ~2mbit, 20% packet loss, ~40ms latency and packets can get reordered. | 184 // ~2mbit, 20% packet loss, ~40ms latency and packets can get reordered. |
| 182 // 300ms drouputs every ~2 seconds. | 185 // 300ms drouputs every ~2 seconds. |
| 183 scoped_ptr<PacketPipe> EvilNetwork(); | 186 std::unique_ptr<PacketPipe> EvilNetwork(); |
| 184 | 187 |
| 185 // Builds an Interrupted Poisson Process network simulator with default | 188 // Builds an Interrupted Poisson Process network simulator with default |
| 186 // settings. It simulates a challenging interference-heavy WiFi environment | 189 // settings. It simulates a challenging interference-heavy WiFi environment |
| 187 // of roughly 2mbits/s. | 190 // of roughly 2mbits/s. |
| 188 scoped_ptr<InterruptedPoissonProcess> DefaultInterruptedPoissonProcess(); | 191 std::unique_ptr<InterruptedPoissonProcess> DefaultInterruptedPoissonProcess(); |
| 189 | 192 |
| 190 } // namespace test | 193 } // namespace test |
| 191 } // namespace cast | 194 } // namespace cast |
| 192 } // namespace media | 195 } // namespace media |
| 193 | 196 |
| 194 #endif // MEDIA_CAST_TEST_UTILITY_UDP_PROXY_H_ | 197 #endif // MEDIA_CAST_TEST_UTILITY_UDP_PROXY_H_ |
| OLD | NEW |