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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 Schedule(); | 65 Schedule(); |
66 } | 66 } |
67 } | 67 } |
68 } | 68 } |
69 | 69 |
70 private: | 70 private: |
71 void Schedule() { | 71 void Schedule() { |
72 last_schedule_ = clock_->NowTicks(); | 72 last_schedule_ = clock_->NowTicks(); |
73 double megabits = buffer_.front()->size() * 8 / 1000000.0; | 73 double megabits = buffer_.front()->size() * 8 / 1000000.0; |
74 double seconds = megabits / max_megabits_per_second_; | 74 double seconds = megabits / max_megabits_per_second_; |
75 int64 microseconds = static_cast<int64>(seconds * 1E6); | 75 int64_t microseconds = static_cast<int64_t>(seconds * 1E6); |
76 task_runner_->PostDelayedTask( | 76 task_runner_->PostDelayedTask( |
77 FROM_HERE, | 77 FROM_HERE, |
78 base::Bind(&Buffer::ProcessBuffer, weak_factory_.GetWeakPtr()), | 78 base::Bind(&Buffer::ProcessBuffer, weak_factory_.GetWeakPtr()), |
79 base::TimeDelta::FromMicroseconds(microseconds)); | 79 base::TimeDelta::FromMicroseconds(microseconds)); |
80 } | 80 } |
81 | 81 |
82 void ProcessBuffer() { | 82 void ProcessBuffer() { |
83 int64 bytes_to_send = static_cast<int64>( | 83 int64_t bytes_to_send = static_cast<int64_t>( |
84 (clock_->NowTicks() - last_schedule_).InSecondsF() * | 84 (clock_->NowTicks() - last_schedule_).InSecondsF() * |
85 max_megabits_per_second_ * 1E6 / 8); | 85 max_megabits_per_second_ * 1E6 / 8); |
86 if (bytes_to_send < static_cast<int64>(buffer_.front()->size())) { | 86 if (bytes_to_send < static_cast<int64_t>(buffer_.front()->size())) { |
87 bytes_to_send = buffer_.front()->size(); | 87 bytes_to_send = buffer_.front()->size(); |
88 } | 88 } |
89 while (!buffer_.empty() && | 89 while (!buffer_.empty() && |
90 static_cast<int64>(buffer_.front()->size()) <= bytes_to_send) { | 90 static_cast<int64_t>(buffer_.front()->size()) <= bytes_to_send) { |
91 CHECK(!buffer_.empty()); | 91 CHECK(!buffer_.empty()); |
92 scoped_ptr<Packet> packet(buffer_.front().release()); | 92 scoped_ptr<Packet> packet(buffer_.front().release()); |
93 bytes_to_send -= packet->size(); | 93 bytes_to_send -= packet->size(); |
94 buffer_size_ -= packet->size(); | 94 buffer_size_ -= packet->size(); |
95 buffer_.pop_front(); | 95 buffer_.pop_front(); |
96 pipe_->Send(packet.Pass()); | 96 pipe_->Send(packet.Pass()); |
97 } | 97 } |
98 if (!buffer_.empty()) { | 98 if (!buffer_.empty()) { |
99 Schedule(); | 99 Schedule(); |
100 } | 100 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 | 133 |
134 class SimpleDelayBase : public PacketPipe { | 134 class SimpleDelayBase : public PacketPipe { |
135 public: | 135 public: |
136 SimpleDelayBase() : weak_factory_(this) {} | 136 SimpleDelayBase() : weak_factory_(this) {} |
137 ~SimpleDelayBase() override {} | 137 ~SimpleDelayBase() override {} |
138 | 138 |
139 void Send(scoped_ptr<Packet> packet) override { | 139 void Send(scoped_ptr<Packet> packet) override { |
140 double seconds = GetDelay(); | 140 double seconds = GetDelay(); |
141 task_runner_->PostDelayedTask( | 141 task_runner_->PostDelayedTask( |
142 FROM_HERE, | 142 FROM_HERE, |
143 base::Bind(&SimpleDelayBase::SendInternal, | 143 base::Bind(&SimpleDelayBase::SendInternal, weak_factory_.GetWeakPtr(), |
144 weak_factory_.GetWeakPtr(), | |
145 base::Passed(&packet)), | 144 base::Passed(&packet)), |
146 base::TimeDelta::FromMicroseconds(static_cast<int64>(seconds * 1E6))); | 145 base::TimeDelta::FromMicroseconds(static_cast<int64_t>(seconds * 1E6))); |
147 } | 146 } |
148 protected: | 147 protected: |
149 virtual double GetDelay() = 0; | 148 virtual double GetDelay() = 0; |
150 | 149 |
151 private: | 150 private: |
152 virtual void SendInternal(scoped_ptr<Packet> packet) { | 151 virtual void SendInternal(scoped_ptr<Packet> packet) { |
153 pipe_->Send(packet.Pass()); | 152 pipe_->Send(packet.Pass()); |
154 } | 153 } |
155 | 154 |
156 base::WeakPtrFactory<SimpleDelayBase> weak_factory_; | 155 base::WeakPtrFactory<SimpleDelayBase> weak_factory_; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 base::TickClock* clock) final { | 231 base::TickClock* clock) final { |
233 PacketPipe::InitOnIOThread(task_runner, clock); | 232 PacketPipe::InitOnIOThread(task_runner, clock); |
234 // As we start the stream, assume that we are in a random | 233 // As we start the stream, assume that we are in a random |
235 // place between two extra delays, thus multiplier = 1.0; | 234 // place between two extra delays, thus multiplier = 1.0; |
236 ScheduleExtraDelay(1.0); | 235 ScheduleExtraDelay(1.0); |
237 } | 236 } |
238 | 237 |
239 private: | 238 private: |
240 void ScheduleExtraDelay(double mult) { | 239 void ScheduleExtraDelay(double mult) { |
241 double seconds = seconds_between_extra_delay_ * mult * base::RandDouble(); | 240 double seconds = seconds_between_extra_delay_ * mult * base::RandDouble(); |
242 int64 microseconds = static_cast<int64>(seconds * 1E6); | 241 int64_t microseconds = static_cast<int64_t>(seconds * 1E6); |
243 task_runner_->PostDelayedTask( | 242 task_runner_->PostDelayedTask( |
244 FROM_HERE, | 243 FROM_HERE, |
245 base::Bind(&RandomSortedDelay::CauseExtraDelay, | 244 base::Bind(&RandomSortedDelay::CauseExtraDelay, |
246 weak_factory_.GetWeakPtr()), | 245 weak_factory_.GetWeakPtr()), |
247 base::TimeDelta::FromMicroseconds(microseconds)); | 246 base::TimeDelta::FromMicroseconds(microseconds)); |
248 } | 247 } |
249 | 248 |
250 void CauseExtraDelay() { | 249 void CauseExtraDelay() { |
251 next_send_ = std::max<base::TimeTicks>( | 250 next_send_ = std::max<base::TimeTicks>( |
252 clock_->NowTicks() + | 251 clock_->NowTicks() + base::TimeDelta::FromMicroseconds( |
253 base::TimeDelta::FromMicroseconds( | 252 static_cast<int64_t>(extra_delay_ * 1E6)), |
254 static_cast<int64>(extra_delay_ * 1E6)), | |
255 next_send_); | 253 next_send_); |
256 // An extra delay just happened, wait up to seconds_between_extra_delay_*2 | 254 // An extra delay just happened, wait up to seconds_between_extra_delay_*2 |
257 // before scheduling another one to make the average equal to | 255 // before scheduling another one to make the average equal to |
258 // seconds_between_extra_delay_. | 256 // seconds_between_extra_delay_. |
259 ScheduleExtraDelay(2.0); | 257 ScheduleExtraDelay(2.0); |
260 } | 258 } |
261 | 259 |
262 void ProcessBuffer() { | 260 void ProcessBuffer() { |
263 base::TimeTicks now = clock_->NowTicks(); | 261 base::TimeTicks now = clock_->NowTicks(); |
264 while (!buffer_.empty() && next_send_ <= now) { | 262 while (!buffer_.empty() && next_send_ <= now) { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 if (works_) { | 315 if (works_) { |
318 pipe_->Send(packet.Pass()); | 316 pipe_->Send(packet.Pass()); |
319 } | 317 } |
320 } | 318 } |
321 | 319 |
322 private: | 320 private: |
323 void Flip() { | 321 void Flip() { |
324 works_ = !works_; | 322 works_ = !works_; |
325 double seconds = base::RandDouble() * | 323 double seconds = base::RandDouble() * |
326 (works_ ? max_work_time_ : max_outage_time_); | 324 (works_ ? max_work_time_ : max_outage_time_); |
327 int64 microseconds = static_cast<int64>(seconds * 1E6); | 325 int64_t microseconds = static_cast<int64_t>(seconds * 1E6); |
328 task_runner_->PostDelayedTask( | 326 task_runner_->PostDelayedTask( |
329 FROM_HERE, | 327 FROM_HERE, |
330 base::Bind(&NetworkGlitchPipe::Flip, weak_factory_.GetWeakPtr()), | 328 base::Bind(&NetworkGlitchPipe::Flip, weak_factory_.GetWeakPtr()), |
331 base::TimeDelta::FromMicroseconds(microseconds)); | 329 base::TimeDelta::FromMicroseconds(microseconds)); |
332 } | 330 } |
333 | 331 |
334 bool works_; | 332 bool works_; |
335 double max_work_time_; | 333 double max_work_time_; |
336 double max_outage_time_; | 334 double max_outage_time_; |
337 base::WeakPtrFactory<NetworkGlitchPipe> weak_factory_; | 335 base::WeakPtrFactory<NetworkGlitchPipe> weak_factory_; |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 base::TickClock* clock_; | 406 base::TickClock* clock_; |
409 base::WeakPtrFactory<InternalBuffer> weak_factory_; | 407 base::WeakPtrFactory<InternalBuffer> weak_factory_; |
410 | 408 |
411 DISALLOW_COPY_AND_ASSIGN(InternalBuffer); | 409 DISALLOW_COPY_AND_ASSIGN(InternalBuffer); |
412 }; | 410 }; |
413 | 411 |
414 InterruptedPoissonProcess::InterruptedPoissonProcess( | 412 InterruptedPoissonProcess::InterruptedPoissonProcess( |
415 const std::vector<double>& average_rates, | 413 const std::vector<double>& average_rates, |
416 double coef_burstiness, | 414 double coef_burstiness, |
417 double coef_variance, | 415 double coef_variance, |
418 uint32 rand_seed) | 416 uint32_t rand_seed) |
419 : clock_(NULL), | 417 : clock_(NULL), |
420 average_rates_(average_rates), | 418 average_rates_(average_rates), |
421 coef_burstiness_(coef_burstiness), | 419 coef_burstiness_(coef_burstiness), |
422 coef_variance_(coef_variance), | 420 coef_variance_(coef_variance), |
423 rate_index_(0), | 421 rate_index_(0), |
424 on_state_(true), | 422 on_state_(true), |
425 weak_factory_(this) { | 423 weak_factory_(this) { |
426 mt_rand_.init_genrand(rand_seed); | 424 mt_rand_.init_genrand(rand_seed); |
427 DCHECK(!average_rates.empty()); | 425 DCHECK(!average_rates.empty()); |
428 ComputeRates(); | 426 ComputeRates(); |
(...skipping 26 matching lines...) Expand all Loading... |
455 // Rate is per milliseconds. | 453 // Rate is per milliseconds. |
456 // The time until next event is exponentially distributed to the | 454 // The time until next event is exponentially distributed to the |
457 // inverse of |rate|. | 455 // inverse of |rate|. |
458 return base::TimeDelta::FromMillisecondsD( | 456 return base::TimeDelta::FromMillisecondsD( |
459 fabs(-log(1.0 - RandDouble()) / rate)); | 457 fabs(-log(1.0 - RandDouble()) / rate)); |
460 } | 458 } |
461 | 459 |
462 double InterruptedPoissonProcess::RandDouble() { | 460 double InterruptedPoissonProcess::RandDouble() { |
463 // Generate a 64-bits random number from MT19937 and then convert | 461 // Generate a 64-bits random number from MT19937 and then convert |
464 // it to double. | 462 // it to double. |
465 uint64 rand = mt_rand_.genrand_int32(); | 463 uint64_t rand = mt_rand_.genrand_int32(); |
466 rand <<= 32; | 464 rand <<= 32; |
467 rand |= mt_rand_.genrand_int32(); | 465 rand |= mt_rand_.genrand_int32(); |
468 return base::BitsToOpenEndedUnitInterval(rand); | 466 return base::BitsToOpenEndedUnitInterval(rand); |
469 } | 467 } |
470 | 468 |
471 void InterruptedPoissonProcess::ComputeRates() { | 469 void InterruptedPoissonProcess::ComputeRates() { |
472 double avg_rate = average_rates_[rate_index_]; | 470 double avg_rate = average_rates_[rate_index_]; |
473 | 471 |
474 send_rate_ = avg_rate / coef_burstiness_; | 472 send_rate_ = avg_rate / coef_burstiness_; |
475 switch_off_rate_ = | 473 switch_off_rate_ = |
(...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
856 destination, | 854 destination, |
857 to_dest_pipe.Pass(), | 855 to_dest_pipe.Pass(), |
858 from_dest_pipe.Pass(), | 856 from_dest_pipe.Pass(), |
859 net_log)); | 857 net_log)); |
860 return ret.Pass(); | 858 return ret.Pass(); |
861 } | 859 } |
862 | 860 |
863 } // namespace test | 861 } // namespace test |
864 } // namespace cast | 862 } // namespace cast |
865 } // namespace media | 863 } // namespace media |
OLD | NEW |