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 #include <math.h> | 5 #include <math.h> |
6 #include <stdlib.h> | 6 #include <stdlib.h> |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "media/cast/test/utility/udp_proxy.h" | 9 #include "media/cast/test/utility/udp_proxy.h" |
10 | 10 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 public: | 49 public: |
50 Buffer(size_t buffer_size, double max_megabits_per_second) | 50 Buffer(size_t buffer_size, double max_megabits_per_second) |
51 : buffer_size_(0), | 51 : buffer_size_(0), |
52 max_buffer_size_(buffer_size), | 52 max_buffer_size_(buffer_size), |
53 max_megabits_per_second_(max_megabits_per_second), | 53 max_megabits_per_second_(max_megabits_per_second), |
54 weak_factory_(this) { | 54 weak_factory_(this) { |
55 CHECK_GT(max_buffer_size_, 0UL); | 55 CHECK_GT(max_buffer_size_, 0UL); |
56 CHECK_GT(max_megabits_per_second, 0); | 56 CHECK_GT(max_megabits_per_second, 0); |
57 } | 57 } |
58 | 58 |
59 virtual void Send(scoped_ptr<Packet> packet) OVERRIDE { | 59 virtual void Send(scoped_ptr<Packet> packet) override { |
60 if (packet->size() + buffer_size_ <= max_buffer_size_) { | 60 if (packet->size() + buffer_size_ <= max_buffer_size_) { |
61 buffer_size_ += packet->size(); | 61 buffer_size_ += packet->size(); |
62 buffer_.push_back(linked_ptr<Packet>(packet.release())); | 62 buffer_.push_back(linked_ptr<Packet>(packet.release())); |
63 if (buffer_.size() == 1) { | 63 if (buffer_.size() == 1) { |
64 Schedule(); | 64 Schedule(); |
65 } | 65 } |
66 } | 66 } |
67 } | 67 } |
68 | 68 |
69 private: | 69 private: |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 | 109 |
110 scoped_ptr<PacketPipe> NewBuffer(size_t buffer_size, double bandwidth) { | 110 scoped_ptr<PacketPipe> NewBuffer(size_t buffer_size, double bandwidth) { |
111 return scoped_ptr<PacketPipe>(new Buffer(buffer_size, bandwidth)).Pass(); | 111 return scoped_ptr<PacketPipe>(new Buffer(buffer_size, bandwidth)).Pass(); |
112 } | 112 } |
113 | 113 |
114 class RandomDrop : public PacketPipe { | 114 class RandomDrop : public PacketPipe { |
115 public: | 115 public: |
116 RandomDrop(double drop_fraction) | 116 RandomDrop(double drop_fraction) |
117 : drop_fraction_(static_cast<int>(drop_fraction * RAND_MAX)) {} | 117 : drop_fraction_(static_cast<int>(drop_fraction * RAND_MAX)) {} |
118 | 118 |
119 virtual void Send(scoped_ptr<Packet> packet) OVERRIDE { | 119 virtual void Send(scoped_ptr<Packet> packet) override { |
120 if (rand() > drop_fraction_) { | 120 if (rand() > drop_fraction_) { |
121 pipe_->Send(packet.Pass()); | 121 pipe_->Send(packet.Pass()); |
122 } | 122 } |
123 } | 123 } |
124 | 124 |
125 private: | 125 private: |
126 int drop_fraction_; | 126 int drop_fraction_; |
127 }; | 127 }; |
128 | 128 |
129 scoped_ptr<PacketPipe> NewRandomDrop(double drop_fraction) { | 129 scoped_ptr<PacketPipe> NewRandomDrop(double drop_fraction) { |
130 return scoped_ptr<PacketPipe>(new RandomDrop(drop_fraction)).Pass(); | 130 return scoped_ptr<PacketPipe>(new RandomDrop(drop_fraction)).Pass(); |
131 } | 131 } |
132 | 132 |
133 class SimpleDelayBase : public PacketPipe { | 133 class SimpleDelayBase : public PacketPipe { |
134 public: | 134 public: |
135 SimpleDelayBase() : weak_factory_(this) {} | 135 SimpleDelayBase() : weak_factory_(this) {} |
136 virtual ~SimpleDelayBase() {} | 136 virtual ~SimpleDelayBase() {} |
137 | 137 |
138 virtual void Send(scoped_ptr<Packet> packet) OVERRIDE { | 138 virtual void Send(scoped_ptr<Packet> packet) override { |
139 double seconds = GetDelay(); | 139 double seconds = GetDelay(); |
140 task_runner_->PostDelayedTask( | 140 task_runner_->PostDelayedTask( |
141 FROM_HERE, | 141 FROM_HERE, |
142 base::Bind(&SimpleDelayBase::SendInternal, | 142 base::Bind(&SimpleDelayBase::SendInternal, |
143 weak_factory_.GetWeakPtr(), | 143 weak_factory_.GetWeakPtr(), |
144 base::Passed(&packet)), | 144 base::Passed(&packet)), |
145 base::TimeDelta::FromMicroseconds(static_cast<int64>(seconds * 1E6))); | 145 base::TimeDelta::FromMicroseconds(static_cast<int64>(seconds * 1E6))); |
146 } | 146 } |
147 protected: | 147 protected: |
148 virtual double GetDelay() = 0; | 148 virtual double GetDelay() = 0; |
149 | 149 |
150 private: | 150 private: |
151 virtual void SendInternal(scoped_ptr<Packet> packet) { | 151 virtual void SendInternal(scoped_ptr<Packet> packet) { |
152 pipe_->Send(packet.Pass()); | 152 pipe_->Send(packet.Pass()); |
153 } | 153 } |
154 | 154 |
155 base::WeakPtrFactory<SimpleDelayBase> weak_factory_; | 155 base::WeakPtrFactory<SimpleDelayBase> weak_factory_; |
156 }; | 156 }; |
157 | 157 |
158 class ConstantDelay : public SimpleDelayBase { | 158 class ConstantDelay : public SimpleDelayBase { |
159 public: | 159 public: |
160 ConstantDelay(double delay_seconds) : delay_seconds_(delay_seconds) {} | 160 ConstantDelay(double delay_seconds) : delay_seconds_(delay_seconds) {} |
161 virtual double GetDelay() OVERRIDE { | 161 virtual double GetDelay() override { |
162 return delay_seconds_; | 162 return delay_seconds_; |
163 } | 163 } |
164 | 164 |
165 private: | 165 private: |
166 double delay_seconds_; | 166 double delay_seconds_; |
167 }; | 167 }; |
168 | 168 |
169 scoped_ptr<PacketPipe> NewConstantDelay(double delay_seconds) { | 169 scoped_ptr<PacketPipe> NewConstantDelay(double delay_seconds) { |
170 return scoped_ptr<PacketPipe>(new ConstantDelay(delay_seconds)).Pass(); | 170 return scoped_ptr<PacketPipe>(new ConstantDelay(delay_seconds)).Pass(); |
171 } | 171 } |
172 | 172 |
173 class RandomUnsortedDelay : public SimpleDelayBase { | 173 class RandomUnsortedDelay : public SimpleDelayBase { |
174 public: | 174 public: |
175 RandomUnsortedDelay(double random_delay) : random_delay_(random_delay) {} | 175 RandomUnsortedDelay(double random_delay) : random_delay_(random_delay) {} |
176 | 176 |
177 virtual double GetDelay() OVERRIDE { | 177 virtual double GetDelay() override { |
178 return random_delay_ * base::RandDouble(); | 178 return random_delay_ * base::RandDouble(); |
179 } | 179 } |
180 | 180 |
181 private: | 181 private: |
182 double random_delay_; | 182 double random_delay_; |
183 }; | 183 }; |
184 | 184 |
185 scoped_ptr<PacketPipe> NewRandomUnsortedDelay(double random_delay) { | 185 scoped_ptr<PacketPipe> NewRandomUnsortedDelay(double random_delay) { |
186 return scoped_ptr<PacketPipe>(new RandomUnsortedDelay(random_delay)).Pass(); | 186 return scoped_ptr<PacketPipe>(new RandomUnsortedDelay(random_delay)).Pass(); |
187 } | 187 } |
188 | 188 |
189 class DuplicateAndDelay : public RandomUnsortedDelay { | 189 class DuplicateAndDelay : public RandomUnsortedDelay { |
190 public: | 190 public: |
191 DuplicateAndDelay(double delay_min, | 191 DuplicateAndDelay(double delay_min, |
192 double random_delay) : | 192 double random_delay) : |
193 RandomUnsortedDelay(random_delay), | 193 RandomUnsortedDelay(random_delay), |
194 delay_min_(delay_min) { | 194 delay_min_(delay_min) { |
195 } | 195 } |
196 virtual void Send(scoped_ptr<Packet> packet) OVERRIDE { | 196 virtual void Send(scoped_ptr<Packet> packet) override { |
197 pipe_->Send(scoped_ptr<Packet>(new Packet(*packet.get()))); | 197 pipe_->Send(scoped_ptr<Packet>(new Packet(*packet.get()))); |
198 RandomUnsortedDelay::Send(packet.Pass()); | 198 RandomUnsortedDelay::Send(packet.Pass()); |
199 } | 199 } |
200 virtual double GetDelay() OVERRIDE { | 200 virtual double GetDelay() override { |
201 return RandomUnsortedDelay::GetDelay() + delay_min_; | 201 return RandomUnsortedDelay::GetDelay() + delay_min_; |
202 } | 202 } |
203 private: | 203 private: |
204 double delay_min_; | 204 double delay_min_; |
205 }; | 205 }; |
206 | 206 |
207 scoped_ptr<PacketPipe> NewDuplicateAndDelay(double delay_min, | 207 scoped_ptr<PacketPipe> NewDuplicateAndDelay(double delay_min, |
208 double random_delay) { | 208 double random_delay) { |
209 return scoped_ptr<PacketPipe>( | 209 return scoped_ptr<PacketPipe>( |
210 new DuplicateAndDelay(delay_min, random_delay)).Pass(); | 210 new DuplicateAndDelay(delay_min, random_delay)).Pass(); |
211 } | 211 } |
212 | 212 |
213 class RandomSortedDelay : public PacketPipe { | 213 class RandomSortedDelay : public PacketPipe { |
214 public: | 214 public: |
215 RandomSortedDelay(double random_delay, | 215 RandomSortedDelay(double random_delay, |
216 double extra_delay, | 216 double extra_delay, |
217 double seconds_between_extra_delay) | 217 double seconds_between_extra_delay) |
218 : random_delay_(random_delay), | 218 : random_delay_(random_delay), |
219 extra_delay_(extra_delay), | 219 extra_delay_(extra_delay), |
220 seconds_between_extra_delay_(seconds_between_extra_delay), | 220 seconds_between_extra_delay_(seconds_between_extra_delay), |
221 weak_factory_(this) {} | 221 weak_factory_(this) {} |
222 | 222 |
223 virtual void Send(scoped_ptr<Packet> packet) OVERRIDE { | 223 virtual void Send(scoped_ptr<Packet> packet) override { |
224 buffer_.push_back(linked_ptr<Packet>(packet.release())); | 224 buffer_.push_back(linked_ptr<Packet>(packet.release())); |
225 if (buffer_.size() == 1) { | 225 if (buffer_.size() == 1) { |
226 next_send_ = std::max( | 226 next_send_ = std::max( |
227 clock_->NowTicks() + | 227 clock_->NowTicks() + |
228 base::TimeDelta::FromSecondsD(base::RandDouble() * random_delay_), | 228 base::TimeDelta::FromSecondsD(base::RandDouble() * random_delay_), |
229 next_send_); | 229 next_send_); |
230 ProcessBuffer(); | 230 ProcessBuffer(); |
231 } | 231 } |
232 } | 232 } |
233 virtual void InitOnIOThread( | 233 virtual void InitOnIOThread( |
234 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 234 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
235 base::TickClock* clock) OVERRIDE { | 235 base::TickClock* clock) override { |
236 PacketPipe::InitOnIOThread(task_runner, clock); | 236 PacketPipe::InitOnIOThread(task_runner, clock); |
237 // As we start the stream, assume that we are in a random | 237 // As we start the stream, assume that we are in a random |
238 // place between two extra delays, thus multiplier = 1.0; | 238 // place between two extra delays, thus multiplier = 1.0; |
239 ScheduleExtraDelay(1.0); | 239 ScheduleExtraDelay(1.0); |
240 } | 240 } |
241 | 241 |
242 private: | 242 private: |
243 void ScheduleExtraDelay(double mult) { | 243 void ScheduleExtraDelay(double mult) { |
244 double seconds = seconds_between_extra_delay_ * mult * base::RandDouble(); | 244 double seconds = seconds_between_extra_delay_ * mult * base::RandDouble(); |
245 int64 microseconds = static_cast<int64>(seconds * 1E6); | 245 int64 microseconds = static_cast<int64>(seconds * 1E6); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 class NetworkGlitchPipe : public PacketPipe { | 304 class NetworkGlitchPipe : public PacketPipe { |
305 public: | 305 public: |
306 NetworkGlitchPipe(double average_work_time, double average_outage_time) | 306 NetworkGlitchPipe(double average_work_time, double average_outage_time) |
307 : works_(false), | 307 : works_(false), |
308 max_work_time_(average_work_time * 2), | 308 max_work_time_(average_work_time * 2), |
309 max_outage_time_(average_outage_time * 2), | 309 max_outage_time_(average_outage_time * 2), |
310 weak_factory_(this) {} | 310 weak_factory_(this) {} |
311 | 311 |
312 virtual void InitOnIOThread( | 312 virtual void InitOnIOThread( |
313 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 313 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
314 base::TickClock* clock) OVERRIDE { | 314 base::TickClock* clock) override { |
315 PacketPipe::InitOnIOThread(task_runner, clock); | 315 PacketPipe::InitOnIOThread(task_runner, clock); |
316 Flip(); | 316 Flip(); |
317 } | 317 } |
318 | 318 |
319 virtual void Send(scoped_ptr<Packet> packet) OVERRIDE { | 319 virtual void Send(scoped_ptr<Packet> packet) override { |
320 if (works_) { | 320 if (works_) { |
321 pipe_->Send(packet.Pass()); | 321 pipe_->Send(packet.Pass()); |
322 } | 322 } |
323 } | 323 } |
324 | 324 |
325 private: | 325 private: |
326 void Flip() { | 326 void Flip() { |
327 works_ = !works_; | 327 works_ = !works_; |
328 double seconds = base::RandDouble() * | 328 double seconds = base::RandDouble() * |
329 (works_ ? max_work_time_ : max_outage_time_); | 329 (works_ ? max_work_time_ : max_outage_time_); |
(...skipping 23 matching lines...) Expand all Loading... |
353 public: | 353 public: |
354 InternalBuffer(base::WeakPtr<InterruptedPoissonProcess> ipp, | 354 InternalBuffer(base::WeakPtr<InterruptedPoissonProcess> ipp, |
355 size_t size) | 355 size_t size) |
356 : ipp_(ipp), | 356 : ipp_(ipp), |
357 stored_size_(0), | 357 stored_size_(0), |
358 stored_limit_(size), | 358 stored_limit_(size), |
359 clock_(NULL), | 359 clock_(NULL), |
360 weak_factory_(this) { | 360 weak_factory_(this) { |
361 } | 361 } |
362 | 362 |
363 virtual void Send(scoped_ptr<Packet> packet) OVERRIDE { | 363 virtual void Send(scoped_ptr<Packet> packet) override { |
364 // Drop if buffer is full. | 364 // Drop if buffer is full. |
365 if (stored_size_ >= stored_limit_) | 365 if (stored_size_ >= stored_limit_) |
366 return; | 366 return; |
367 stored_size_ += packet->size(); | 367 stored_size_ += packet->size(); |
368 buffer_.push_back(linked_ptr<Packet>(packet.release())); | 368 buffer_.push_back(linked_ptr<Packet>(packet.release())); |
369 buffer_time_.push_back(clock_->NowTicks()); | 369 buffer_time_.push_back(clock_->NowTicks()); |
370 DCHECK(buffer_.size() == buffer_time_.size()); | 370 DCHECK(buffer_.size() == buffer_time_.size()); |
371 } | 371 } |
372 | 372 |
373 virtual void InitOnIOThread( | 373 virtual void InitOnIOThread( |
374 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 374 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
375 base::TickClock* clock) OVERRIDE { | 375 base::TickClock* clock) override { |
376 clock_ = clock; | 376 clock_ = clock; |
377 if (ipp_) | 377 if (ipp_) |
378 ipp_->InitOnIOThread(task_runner, clock); | 378 ipp_->InitOnIOThread(task_runner, clock); |
379 PacketPipe::InitOnIOThread(task_runner, clock); | 379 PacketPipe::InitOnIOThread(task_runner, clock); |
380 } | 380 } |
381 | 381 |
382 void SendOnePacket() { | 382 void SendOnePacket() { |
383 scoped_ptr<Packet> packet(buffer_.front().release()); | 383 scoped_ptr<Packet> packet(buffer_.front().release()); |
384 stored_size_ -= packet->size(); | 384 stored_size_ -= packet->size(); |
385 buffer_.pop_front(); | 385 buffer_.pop_front(); |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
545 break; | 545 break; |
546 } | 546 } |
547 } | 547 } |
548 | 548 |
549 class UDPProxyImpl; | 549 class UDPProxyImpl; |
550 | 550 |
551 class PacketSender : public PacketPipe { | 551 class PacketSender : public PacketPipe { |
552 public: | 552 public: |
553 PacketSender(UDPProxyImpl* udp_proxy, const net::IPEndPoint* destination) | 553 PacketSender(UDPProxyImpl* udp_proxy, const net::IPEndPoint* destination) |
554 : udp_proxy_(udp_proxy), destination_(destination) {} | 554 : udp_proxy_(udp_proxy), destination_(destination) {} |
555 virtual void Send(scoped_ptr<Packet> packet) OVERRIDE; | 555 virtual void Send(scoped_ptr<Packet> packet) override; |
556 virtual void AppendToPipe(scoped_ptr<PacketPipe> pipe) OVERRIDE { | 556 virtual void AppendToPipe(scoped_ptr<PacketPipe> pipe) override { |
557 NOTREACHED(); | 557 NOTREACHED(); |
558 } | 558 } |
559 | 559 |
560 private: | 560 private: |
561 UDPProxyImpl* udp_proxy_; | 561 UDPProxyImpl* udp_proxy_; |
562 const net::IPEndPoint* destination_; // not owned | 562 const net::IPEndPoint* destination_; // not owned |
563 }; | 563 }; |
564 | 564 |
565 namespace { | 565 namespace { |
566 void BuildPipe(scoped_ptr<PacketPipe>* pipe, PacketPipe* next) { | 566 void BuildPipe(scoped_ptr<PacketPipe>* pipe, PacketPipe* next) { |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
830 destination, | 830 destination, |
831 to_dest_pipe.Pass(), | 831 to_dest_pipe.Pass(), |
832 from_dest_pipe.Pass(), | 832 from_dest_pipe.Pass(), |
833 net_log)); | 833 net_log)); |
834 return ret.Pass(); | 834 return ret.Pass(); |
835 } | 835 } |
836 | 836 |
837 } // namespace test | 837 } // namespace test |
838 } // namespace cast | 838 } // namespace cast |
839 } // namespace media | 839 } // namespace media |
OLD | NEW |