| 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<transport::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<transport::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: |
| 70 void Schedule() { | 70 void Schedule() { |
| 71 double megabits = buffer_.front()->size() * 8 / 1000000.0; | 71 double megabits = buffer_.front()->size() * 8 / 1000000.0; |
| 72 double seconds = megabits / max_megabits_per_second_; | 72 double seconds = megabits / max_megabits_per_second_; |
| 73 int64 microseconds = static_cast<int64>(seconds * 1E6); | 73 int64 microseconds = static_cast<int64>(seconds * 1E6); |
| 74 task_runner_->PostDelayedTask( | 74 task_runner_->PostDelayedTask( |
| 75 FROM_HERE, | 75 FROM_HERE, |
| 76 base::Bind(&Buffer::ProcessBuffer, weak_factory_.GetWeakPtr()), | 76 base::Bind(&Buffer::ProcessBuffer, weak_factory_.GetWeakPtr()), |
| 77 base::TimeDelta::FromMicroseconds(microseconds)); | 77 base::TimeDelta::FromMicroseconds(microseconds)); |
| 78 } | 78 } |
| 79 | 79 |
| 80 void ProcessBuffer() { | 80 void ProcessBuffer() { |
| 81 CHECK(!buffer_.empty()); | 81 CHECK(!buffer_.empty()); |
| 82 scoped_ptr<transport::Packet> packet(buffer_.front().release()); | 82 scoped_ptr<Packet> packet(buffer_.front().release()); |
| 83 buffer_size_ -= packet->size(); | 83 buffer_size_ -= packet->size(); |
| 84 buffer_.pop_front(); | 84 buffer_.pop_front(); |
| 85 pipe_->Send(packet.Pass()); | 85 pipe_->Send(packet.Pass()); |
| 86 if (!buffer_.empty()) { | 86 if (!buffer_.empty()) { |
| 87 Schedule(); | 87 Schedule(); |
| 88 } | 88 } |
| 89 } | 89 } |
| 90 | 90 |
| 91 std::deque<linked_ptr<transport::Packet> > buffer_; | 91 std::deque<linked_ptr<Packet> > buffer_; |
| 92 size_t buffer_size_; | 92 size_t buffer_size_; |
| 93 size_t max_buffer_size_; | 93 size_t max_buffer_size_; |
| 94 double max_megabits_per_second_; // megabits per second | 94 double max_megabits_per_second_; // megabits per second |
| 95 base::WeakPtrFactory<Buffer> weak_factory_; | 95 base::WeakPtrFactory<Buffer> weak_factory_; |
| 96 }; | 96 }; |
| 97 | 97 |
| 98 scoped_ptr<PacketPipe> NewBuffer(size_t buffer_size, double bandwidth) { | 98 scoped_ptr<PacketPipe> NewBuffer(size_t buffer_size, double bandwidth) { |
| 99 return scoped_ptr<PacketPipe>(new Buffer(buffer_size, bandwidth)).Pass(); | 99 return scoped_ptr<PacketPipe>(new Buffer(buffer_size, bandwidth)).Pass(); |
| 100 } | 100 } |
| 101 | 101 |
| 102 class RandomDrop : public PacketPipe { | 102 class RandomDrop : public PacketPipe { |
| 103 public: | 103 public: |
| 104 RandomDrop(double drop_fraction) | 104 RandomDrop(double drop_fraction) |
| 105 : drop_fraction_(static_cast<int>(drop_fraction * RAND_MAX)) {} | 105 : drop_fraction_(static_cast<int>(drop_fraction * RAND_MAX)) {} |
| 106 | 106 |
| 107 virtual void Send(scoped_ptr<transport::Packet> packet) OVERRIDE { | 107 virtual void Send(scoped_ptr<Packet> packet) OVERRIDE { |
| 108 if (rand() > drop_fraction_) { | 108 if (rand() > drop_fraction_) { |
| 109 pipe_->Send(packet.Pass()); | 109 pipe_->Send(packet.Pass()); |
| 110 } | 110 } |
| 111 } | 111 } |
| 112 | 112 |
| 113 private: | 113 private: |
| 114 int drop_fraction_; | 114 int drop_fraction_; |
| 115 }; | 115 }; |
| 116 | 116 |
| 117 scoped_ptr<PacketPipe> NewRandomDrop(double drop_fraction) { | 117 scoped_ptr<PacketPipe> NewRandomDrop(double drop_fraction) { |
| 118 return scoped_ptr<PacketPipe>(new RandomDrop(drop_fraction)).Pass(); | 118 return scoped_ptr<PacketPipe>(new RandomDrop(drop_fraction)).Pass(); |
| 119 } | 119 } |
| 120 | 120 |
| 121 class SimpleDelayBase : public PacketPipe { | 121 class SimpleDelayBase : public PacketPipe { |
| 122 public: | 122 public: |
| 123 SimpleDelayBase() : weak_factory_(this) {} | 123 SimpleDelayBase() : weak_factory_(this) {} |
| 124 virtual ~SimpleDelayBase() {} | 124 virtual ~SimpleDelayBase() {} |
| 125 | 125 |
| 126 virtual void Send(scoped_ptr<transport::Packet> packet) OVERRIDE { | 126 virtual void Send(scoped_ptr<Packet> packet) OVERRIDE { |
| 127 double seconds = GetDelay(); | 127 double seconds = GetDelay(); |
| 128 task_runner_->PostDelayedTask( | 128 task_runner_->PostDelayedTask( |
| 129 FROM_HERE, | 129 FROM_HERE, |
| 130 base::Bind(&SimpleDelayBase::SendInternal, | 130 base::Bind(&SimpleDelayBase::SendInternal, |
| 131 weak_factory_.GetWeakPtr(), | 131 weak_factory_.GetWeakPtr(), |
| 132 base::Passed(&packet)), | 132 base::Passed(&packet)), |
| 133 base::TimeDelta::FromMicroseconds(static_cast<int64>(seconds * 1E6))); | 133 base::TimeDelta::FromMicroseconds(static_cast<int64>(seconds * 1E6))); |
| 134 } | 134 } |
| 135 protected: | 135 protected: |
| 136 virtual double GetDelay() = 0; | 136 virtual double GetDelay() = 0; |
| 137 | 137 |
| 138 private: | 138 private: |
| 139 virtual void SendInternal(scoped_ptr<transport::Packet> packet) { | 139 virtual void SendInternal(scoped_ptr<Packet> packet) { |
| 140 pipe_->Send(packet.Pass()); | 140 pipe_->Send(packet.Pass()); |
| 141 } | 141 } |
| 142 | 142 |
| 143 base::WeakPtrFactory<SimpleDelayBase> weak_factory_; | 143 base::WeakPtrFactory<SimpleDelayBase> weak_factory_; |
| 144 }; | 144 }; |
| 145 | 145 |
| 146 class ConstantDelay : public SimpleDelayBase { | 146 class ConstantDelay : public SimpleDelayBase { |
| 147 public: | 147 public: |
| 148 ConstantDelay(double delay_seconds) : delay_seconds_(delay_seconds) {} | 148 ConstantDelay(double delay_seconds) : delay_seconds_(delay_seconds) {} |
| 149 virtual double GetDelay() OVERRIDE { | 149 virtual double GetDelay() OVERRIDE { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 178 class RandomSortedDelay : public PacketPipe { | 178 class RandomSortedDelay : public PacketPipe { |
| 179 public: | 179 public: |
| 180 RandomSortedDelay(double random_delay, | 180 RandomSortedDelay(double random_delay, |
| 181 double extra_delay, | 181 double extra_delay, |
| 182 double seconds_between_extra_delay) | 182 double seconds_between_extra_delay) |
| 183 : random_delay_(random_delay), | 183 : random_delay_(random_delay), |
| 184 extra_delay_(extra_delay), | 184 extra_delay_(extra_delay), |
| 185 seconds_between_extra_delay_(seconds_between_extra_delay), | 185 seconds_between_extra_delay_(seconds_between_extra_delay), |
| 186 weak_factory_(this) {} | 186 weak_factory_(this) {} |
| 187 | 187 |
| 188 virtual void Send(scoped_ptr<transport::Packet> packet) OVERRIDE { | 188 virtual void Send(scoped_ptr<Packet> packet) OVERRIDE { |
| 189 buffer_.push_back(linked_ptr<transport::Packet>(packet.release())); | 189 buffer_.push_back(linked_ptr<Packet>(packet.release())); |
| 190 if (buffer_.size() == 1) { | 190 if (buffer_.size() == 1) { |
| 191 Schedule(); | 191 Schedule(); |
| 192 } | 192 } |
| 193 } | 193 } |
| 194 virtual void InitOnIOThread( | 194 virtual void InitOnIOThread( |
| 195 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 195 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 196 base::TickClock* clock) OVERRIDE { | 196 base::TickClock* clock) OVERRIDE { |
| 197 PacketPipe::InitOnIOThread(task_runner, clock); | 197 PacketPipe::InitOnIOThread(task_runner, clock); |
| 198 // As we start the stream, assume that we are in a random | 198 // As we start the stream, assume that we are in a random |
| 199 // place between two extra delays, thus multiplier = 1.0; | 199 // place between two extra delays, thus multiplier = 1.0; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 } | 232 } |
| 233 | 233 |
| 234 task_runner_->PostDelayedTask(FROM_HERE, | 234 task_runner_->PostDelayedTask(FROM_HERE, |
| 235 base::Bind(&RandomSortedDelay::ProcessBuffer, | 235 base::Bind(&RandomSortedDelay::ProcessBuffer, |
| 236 weak_factory_.GetWeakPtr()), | 236 weak_factory_.GetWeakPtr()), |
| 237 delay_time); | 237 delay_time); |
| 238 } | 238 } |
| 239 | 239 |
| 240 void ProcessBuffer() { | 240 void ProcessBuffer() { |
| 241 CHECK(!buffer_.empty()); | 241 CHECK(!buffer_.empty()); |
| 242 scoped_ptr<transport::Packet> packet(buffer_.front().release()); | 242 scoped_ptr<Packet> packet(buffer_.front().release()); |
| 243 pipe_->Send(packet.Pass()); | 243 pipe_->Send(packet.Pass()); |
| 244 buffer_.pop_front(); | 244 buffer_.pop_front(); |
| 245 if (!buffer_.empty()) { | 245 if (!buffer_.empty()) { |
| 246 Schedule(); | 246 Schedule(); |
| 247 } | 247 } |
| 248 } | 248 } |
| 249 | 249 |
| 250 base::TimeTicks block_until_; | 250 base::TimeTicks block_until_; |
| 251 std::deque<linked_ptr<transport::Packet> > buffer_; | 251 std::deque<linked_ptr<Packet> > buffer_; |
| 252 double random_delay_; | 252 double random_delay_; |
| 253 double extra_delay_; | 253 double extra_delay_; |
| 254 double seconds_between_extra_delay_; | 254 double seconds_between_extra_delay_; |
| 255 base::WeakPtrFactory<RandomSortedDelay> weak_factory_; | 255 base::WeakPtrFactory<RandomSortedDelay> weak_factory_; |
| 256 }; | 256 }; |
| 257 | 257 |
| 258 scoped_ptr<PacketPipe> NewRandomSortedDelay( | 258 scoped_ptr<PacketPipe> NewRandomSortedDelay( |
| 259 double random_delay, | 259 double random_delay, |
| 260 double extra_delay, | 260 double extra_delay, |
| 261 double seconds_between_extra_delay) { | 261 double seconds_between_extra_delay) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 273 max_outage_time_(average_outage_time * 2), | 273 max_outage_time_(average_outage_time * 2), |
| 274 weak_factory_(this) {} | 274 weak_factory_(this) {} |
| 275 | 275 |
| 276 virtual void InitOnIOThread( | 276 virtual void InitOnIOThread( |
| 277 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 277 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 278 base::TickClock* clock) OVERRIDE { | 278 base::TickClock* clock) OVERRIDE { |
| 279 PacketPipe::InitOnIOThread(task_runner, clock); | 279 PacketPipe::InitOnIOThread(task_runner, clock); |
| 280 Flip(); | 280 Flip(); |
| 281 } | 281 } |
| 282 | 282 |
| 283 virtual void Send(scoped_ptr<transport::Packet> packet) OVERRIDE { | 283 virtual void Send(scoped_ptr<Packet> packet) OVERRIDE { |
| 284 if (works_) { | 284 if (works_) { |
| 285 pipe_->Send(packet.Pass()); | 285 pipe_->Send(packet.Pass()); |
| 286 } | 286 } |
| 287 } | 287 } |
| 288 | 288 |
| 289 private: | 289 private: |
| 290 void Flip() { | 290 void Flip() { |
| 291 works_ = !works_; | 291 works_ = !works_; |
| 292 double seconds = base::RandDouble() * | 292 double seconds = base::RandDouble() * |
| 293 (works_ ? max_work_time_ : max_outage_time_); | 293 (works_ ? max_work_time_ : max_outage_time_); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 317 public: | 317 public: |
| 318 InternalBuffer(base::WeakPtr<InterruptedPoissonProcess> ipp, | 318 InternalBuffer(base::WeakPtr<InterruptedPoissonProcess> ipp, |
| 319 size_t size) | 319 size_t size) |
| 320 : ipp_(ipp), | 320 : ipp_(ipp), |
| 321 stored_size_(0), | 321 stored_size_(0), |
| 322 stored_limit_(size), | 322 stored_limit_(size), |
| 323 clock_(NULL), | 323 clock_(NULL), |
| 324 weak_factory_(this) { | 324 weak_factory_(this) { |
| 325 } | 325 } |
| 326 | 326 |
| 327 virtual void Send(scoped_ptr<transport::Packet> packet) OVERRIDE { | 327 virtual void Send(scoped_ptr<Packet> packet) OVERRIDE { |
| 328 // Drop if buffer is full. | 328 // Drop if buffer is full. |
| 329 if (stored_size_ >= stored_limit_) | 329 if (stored_size_ >= stored_limit_) |
| 330 return; | 330 return; |
| 331 stored_size_ += packet->size(); | 331 stored_size_ += packet->size(); |
| 332 buffer_.push_back(linked_ptr<transport::Packet>(packet.release())); | 332 buffer_.push_back(linked_ptr<Packet>(packet.release())); |
| 333 buffer_time_.push_back(clock_->NowTicks()); | 333 buffer_time_.push_back(clock_->NowTicks()); |
| 334 DCHECK(buffer_.size() == buffer_time_.size()); | 334 DCHECK(buffer_.size() == buffer_time_.size()); |
| 335 } | 335 } |
| 336 | 336 |
| 337 virtual void InitOnIOThread( | 337 virtual void InitOnIOThread( |
| 338 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 338 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 339 base::TickClock* clock) OVERRIDE { | 339 base::TickClock* clock) OVERRIDE { |
| 340 clock_ = clock; | 340 clock_ = clock; |
| 341 if (ipp_) | 341 if (ipp_) |
| 342 ipp_->InitOnIOThread(task_runner, clock); | 342 ipp_->InitOnIOThread(task_runner, clock); |
| 343 PacketPipe::InitOnIOThread(task_runner, clock); | 343 PacketPipe::InitOnIOThread(task_runner, clock); |
| 344 } | 344 } |
| 345 | 345 |
| 346 void SendOnePacket() { | 346 void SendOnePacket() { |
| 347 scoped_ptr<transport::Packet> packet(buffer_.front().release()); | 347 scoped_ptr<Packet> packet(buffer_.front().release()); |
| 348 stored_size_ -= packet->size(); | 348 stored_size_ -= packet->size(); |
| 349 buffer_.pop_front(); | 349 buffer_.pop_front(); |
| 350 buffer_time_.pop_front(); | 350 buffer_time_.pop_front(); |
| 351 pipe_->Send(packet.Pass()); | 351 pipe_->Send(packet.Pass()); |
| 352 DCHECK(buffer_.size() == buffer_time_.size()); | 352 DCHECK(buffer_.size() == buffer_time_.size()); |
| 353 } | 353 } |
| 354 | 354 |
| 355 bool Empty() const { | 355 bool Empty() const { |
| 356 return buffer_.empty(); | 356 return buffer_.empty(); |
| 357 } | 357 } |
| 358 | 358 |
| 359 base::TimeTicks FirstPacketTime() const { | 359 base::TimeTicks FirstPacketTime() const { |
| 360 DCHECK(!buffer_time_.empty()); | 360 DCHECK(!buffer_time_.empty()); |
| 361 return buffer_time_.front(); | 361 return buffer_time_.front(); |
| 362 } | 362 } |
| 363 | 363 |
| 364 base::WeakPtr<InternalBuffer> GetWeakPtr() { | 364 base::WeakPtr<InternalBuffer> GetWeakPtr() { |
| 365 return weak_factory_.GetWeakPtr(); | 365 return weak_factory_.GetWeakPtr(); |
| 366 | 366 |
| 367 } | 367 } |
| 368 | 368 |
| 369 private: | 369 private: |
| 370 const base::WeakPtr<InterruptedPoissonProcess> ipp_; | 370 const base::WeakPtr<InterruptedPoissonProcess> ipp_; |
| 371 size_t stored_size_; | 371 size_t stored_size_; |
| 372 const size_t stored_limit_; | 372 const size_t stored_limit_; |
| 373 std::deque<linked_ptr<transport::Packet> > buffer_; | 373 std::deque<linked_ptr<Packet> > buffer_; |
| 374 std::deque<base::TimeTicks> buffer_time_; | 374 std::deque<base::TimeTicks> buffer_time_; |
| 375 base::TickClock* clock_; | 375 base::TickClock* clock_; |
| 376 base::WeakPtrFactory<InternalBuffer> weak_factory_; | 376 base::WeakPtrFactory<InternalBuffer> weak_factory_; |
| 377 | 377 |
| 378 DISALLOW_COPY_AND_ASSIGN(InternalBuffer); | 378 DISALLOW_COPY_AND_ASSIGN(InternalBuffer); |
| 379 }; | 379 }; |
| 380 | 380 |
| 381 InterruptedPoissonProcess::InterruptedPoissonProcess( | 381 InterruptedPoissonProcess::InterruptedPoissonProcess( |
| 382 const std::vector<double>& average_rates, | 382 const std::vector<double>& average_rates, |
| 383 double coef_burstiness, | 383 double coef_burstiness, |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 509 break; | 509 break; |
| 510 } | 510 } |
| 511 } | 511 } |
| 512 | 512 |
| 513 class UDPProxyImpl; | 513 class UDPProxyImpl; |
| 514 | 514 |
| 515 class PacketSender : public PacketPipe { | 515 class PacketSender : public PacketPipe { |
| 516 public: | 516 public: |
| 517 PacketSender(UDPProxyImpl* udp_proxy, const net::IPEndPoint* destination) | 517 PacketSender(UDPProxyImpl* udp_proxy, const net::IPEndPoint* destination) |
| 518 : udp_proxy_(udp_proxy), destination_(destination) {} | 518 : udp_proxy_(udp_proxy), destination_(destination) {} |
| 519 virtual void Send(scoped_ptr<transport::Packet> packet) OVERRIDE; | 519 virtual void Send(scoped_ptr<Packet> packet) OVERRIDE; |
| 520 virtual void AppendToPipe(scoped_ptr<PacketPipe> pipe) OVERRIDE { | 520 virtual void AppendToPipe(scoped_ptr<PacketPipe> pipe) OVERRIDE { |
| 521 NOTREACHED(); | 521 NOTREACHED(); |
| 522 } | 522 } |
| 523 | 523 |
| 524 private: | 524 private: |
| 525 UDPProxyImpl* udp_proxy_; | 525 UDPProxyImpl* udp_proxy_; |
| 526 const net::IPEndPoint* destination_; // not owned | 526 const net::IPEndPoint* destination_; // not owned |
| 527 }; | 527 }; |
| 528 | 528 |
| 529 namespace { | 529 namespace { |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 619 base::WaitableEvent stop_event(false, false); | 619 base::WaitableEvent stop_event(false, false); |
| 620 proxy_thread_.message_loop_proxy()->PostTask( | 620 proxy_thread_.message_loop_proxy()->PostTask( |
| 621 FROM_HERE, | 621 FROM_HERE, |
| 622 base::Bind(&UDPProxyImpl::Stop, | 622 base::Bind(&UDPProxyImpl::Stop, |
| 623 base::Unretained(this), | 623 base::Unretained(this), |
| 624 base::Unretained(&stop_event))); | 624 base::Unretained(&stop_event))); |
| 625 stop_event.Wait(); | 625 stop_event.Wait(); |
| 626 proxy_thread_.Stop(); | 626 proxy_thread_.Stop(); |
| 627 } | 627 } |
| 628 | 628 |
| 629 void Send(scoped_ptr<transport::Packet> packet, | 629 void Send(scoped_ptr<Packet> packet, |
| 630 const net::IPEndPoint& destination) { | 630 const net::IPEndPoint& destination) { |
| 631 if (blocked_) { | 631 if (blocked_) { |
| 632 LOG(ERROR) << "Cannot write packet right now: blocked"; | 632 LOG(ERROR) << "Cannot write packet right now: blocked"; |
| 633 return; | 633 return; |
| 634 } | 634 } |
| 635 | 635 |
| 636 VLOG(1) << "Sending packet, len = " << packet->size(); | 636 VLOG(1) << "Sending packet, len = " << packet->size(); |
| 637 // We ignore all problems, callbacks and errors. | 637 // We ignore all problems, callbacks and errors. |
| 638 // If it didn't work we just drop the packet at and call it a day. | 638 // If it didn't work we just drop the packet at and call it a day. |
| 639 scoped_refptr<net::IOBuffer> buf = | 639 scoped_refptr<net::IOBuffer> buf = |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 715 } | 715 } |
| 716 } | 716 } |
| 717 | 717 |
| 718 void ReadCallback(scoped_refptr<net::IOBuffer> recv_buf, int len) { | 718 void ReadCallback(scoped_refptr<net::IOBuffer> recv_buf, int len) { |
| 719 ProcessPacket(recv_buf, len); | 719 ProcessPacket(recv_buf, len); |
| 720 PollRead(); | 720 PollRead(); |
| 721 } | 721 } |
| 722 | 722 |
| 723 void PollRead() { | 723 void PollRead() { |
| 724 while (true) { | 724 while (true) { |
| 725 packet_.reset(new transport::Packet(kMaxPacketSize)); | 725 packet_.reset(new Packet(kMaxPacketSize)); |
| 726 scoped_refptr<net::IOBuffer> recv_buf = | 726 scoped_refptr<net::IOBuffer> recv_buf = |
| 727 new net::WrappedIOBuffer(reinterpret_cast<char*>(&packet_->front())); | 727 new net::WrappedIOBuffer(reinterpret_cast<char*>(&packet_->front())); |
| 728 int len = socket_->RecvFrom( | 728 int len = socket_->RecvFrom( |
| 729 recv_buf, | 729 recv_buf, |
| 730 kMaxPacketSize, | 730 kMaxPacketSize, |
| 731 &recv_address_, | 731 &recv_address_, |
| 732 base::Bind(&UDPProxyImpl::ReadCallback, | 732 base::Bind(&UDPProxyImpl::ReadCallback, |
| 733 base::Unretained(this), | 733 base::Unretained(this), |
| 734 recv_buf)); | 734 recv_buf)); |
| 735 if (len == net::ERR_IO_PENDING) | 735 if (len == net::ERR_IO_PENDING) |
| 736 break; | 736 break; |
| 737 ProcessPacket(recv_buf, len); | 737 ProcessPacket(recv_buf, len); |
| 738 } | 738 } |
| 739 } | 739 } |
| 740 | 740 |
| 741 void AllowWrite(scoped_refptr<net::IOBuffer> buf, | 741 void AllowWrite(scoped_refptr<net::IOBuffer> buf, |
| 742 scoped_ptr<transport::Packet> packet, | 742 scoped_ptr<Packet> packet, |
| 743 int unused_len) { | 743 int unused_len) { |
| 744 DCHECK(blocked_); | 744 DCHECK(blocked_); |
| 745 blocked_ = false; | 745 blocked_ = false; |
| 746 } | 746 } |
| 747 | 747 |
| 748 // Input | 748 // Input |
| 749 net::IPEndPoint local_port_; | 749 net::IPEndPoint local_port_; |
| 750 | 750 |
| 751 net::IPEndPoint destination_; | 751 net::IPEndPoint destination_; |
| 752 bool destination_is_mutable_; | 752 bool destination_is_mutable_; |
| 753 | 753 |
| 754 net::IPEndPoint return_address_; | 754 net::IPEndPoint return_address_; |
| 755 bool set_destination_next_; | 755 bool set_destination_next_; |
| 756 | 756 |
| 757 base::DefaultTickClock tick_clock_; | 757 base::DefaultTickClock tick_clock_; |
| 758 base::Thread proxy_thread_; | 758 base::Thread proxy_thread_; |
| 759 scoped_ptr<net::UDPSocket> socket_; | 759 scoped_ptr<net::UDPSocket> socket_; |
| 760 scoped_ptr<PacketPipe> to_dest_pipe_; | 760 scoped_ptr<PacketPipe> to_dest_pipe_; |
| 761 scoped_ptr<PacketPipe> from_dest_pipe_; | 761 scoped_ptr<PacketPipe> from_dest_pipe_; |
| 762 | 762 |
| 763 // For receiving. | 763 // For receiving. |
| 764 net::IPEndPoint recv_address_; | 764 net::IPEndPoint recv_address_; |
| 765 scoped_ptr<transport::Packet> packet_; | 765 scoped_ptr<Packet> packet_; |
| 766 | 766 |
| 767 // For sending. | 767 // For sending. |
| 768 bool blocked_; | 768 bool blocked_; |
| 769 | 769 |
| 770 base::WeakPtrFactory<UDPProxyImpl> weak_factory_; | 770 base::WeakPtrFactory<UDPProxyImpl> weak_factory_; |
| 771 }; | 771 }; |
| 772 | 772 |
| 773 void PacketSender::Send(scoped_ptr<transport::Packet> packet) { | 773 void PacketSender::Send(scoped_ptr<Packet> packet) { |
| 774 udp_proxy_->Send(packet.Pass(), *destination_); | 774 udp_proxy_->Send(packet.Pass(), *destination_); |
| 775 } | 775 } |
| 776 | 776 |
| 777 scoped_ptr<UDPProxy> UDPProxy::Create( | 777 scoped_ptr<UDPProxy> UDPProxy::Create( |
| 778 const net::IPEndPoint& local_port, | 778 const net::IPEndPoint& local_port, |
| 779 const net::IPEndPoint& destination, | 779 const net::IPEndPoint& destination, |
| 780 scoped_ptr<PacketPipe> to_dest_pipe, | 780 scoped_ptr<PacketPipe> to_dest_pipe, |
| 781 scoped_ptr<PacketPipe> from_dest_pipe, | 781 scoped_ptr<PacketPipe> from_dest_pipe, |
| 782 net::NetLog* net_log) { | 782 net::NetLog* net_log) { |
| 783 scoped_ptr<UDPProxy> ret(new UDPProxyImpl(local_port, | 783 scoped_ptr<UDPProxy> ret(new UDPProxyImpl(local_port, |
| 784 destination, | 784 destination, |
| 785 to_dest_pipe.Pass(), | 785 to_dest_pipe.Pass(), |
| 786 from_dest_pipe.Pass(), | 786 from_dest_pipe.Pass(), |
| 787 net_log)); | 787 net_log)); |
| 788 return ret.Pass(); | 788 return ret.Pass(); |
| 789 } | 789 } |
| 790 | 790 |
| 791 } // namespace test | 791 } // namespace test |
| 792 } // namespace cast | 792 } // namespace cast |
| 793 } // namespace media | 793 } // namespace media |
| OLD | NEW |