| 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 // MSVC++ requires this to be set before any other includes to get M_PI. | 5 // MSVC++ requires this to be set before any other includes to get M_PI. |
| 6 #define _USE_MATH_DEFINES | 6 #define _USE_MATH_DEFINES |
| 7 | 7 |
| 8 #include "remoting/test/fake_socket_factory.h" | 8 #include "remoting/test/fake_socket_factory.h" |
| 9 | 9 |
| 10 #include <algorithm> |
| 10 #include <cmath> | 11 #include <cmath> |
| 11 #include <cstddef> | 12 #include <cstddef> |
| 12 #include <cstdlib> | 13 #include <cstdlib> |
| 13 #include <string> | 14 #include <string> |
| 14 | 15 |
| 15 #include "base/bind.h" | 16 #include "base/bind.h" |
| 16 #include "base/callback.h" | 17 #include "base/callback.h" |
| 17 #include "base/location.h" | 18 #include "base/location.h" |
| 18 #include "base/macros.h" | 19 #include "base/macros.h" |
| 19 #include "base/single_thread_task_runner.h" | 20 #include "base/single_thread_task_runner.h" |
| 20 #include "base/threading/thread_task_runner_handle.h" | 21 #include "base/threading/thread_task_runner_handle.h" |
| 21 #include "base/time/time.h" | 22 #include "base/time/time.h" |
| 22 #include "net/base/io_buffer.h" | 23 #include "net/base/io_buffer.h" |
| 23 #include "remoting/test/leaky_bucket.h" | 24 #include "remoting/base/leaky_bucket.h" |
| 24 #include "third_party/webrtc/base/asyncpacketsocket.h" | 25 #include "third_party/webrtc/base/asyncpacketsocket.h" |
| 25 #include "third_party/webrtc/base/socket.h" | 26 #include "third_party/webrtc/base/socket.h" |
| 26 #include "third_party/webrtc/media/base/rtputils.h" | 27 #include "third_party/webrtc/media/base/rtputils.h" |
| 27 | 28 |
| 28 namespace remoting { | 29 namespace remoting { |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 const int kPortRangeStart = 1024; | 33 const int kPortRangeStart = 1024; |
| 33 const int kPortRangeEnd = 65535; | 34 const int kPortRangeEnd = 65535; |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 const rtc::PacketOptions& options) { | 117 const rtc::PacketOptions& options) { |
| 117 NOTREACHED(); | 118 NOTREACHED(); |
| 118 return EINVAL; | 119 return EINVAL; |
| 119 } | 120 } |
| 120 | 121 |
| 121 int FakeUdpSocket::SendTo(const void* data, size_t data_size, | 122 int FakeUdpSocket::SendTo(const void* data, size_t data_size, |
| 122 const rtc::SocketAddress& address, | 123 const rtc::SocketAddress& address, |
| 123 const rtc::PacketOptions& options) { | 124 const rtc::PacketOptions& options) { |
| 124 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(data_size); | 125 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(data_size); |
| 125 memcpy(buffer->data(), data, data_size); | 126 memcpy(buffer->data(), data, data_size); |
| 126 cricket::ApplyPacketOptions( | 127 base::TimeTicks now = base::TimeTicks::Now(); |
| 127 reinterpret_cast<uint8_t*>(buffer->data()), data_size, | 128 cricket::ApplyPacketOptions(reinterpret_cast<uint8_t*>(buffer->data()), |
| 128 options.packet_time_params, | 129 data_size, options.packet_time_params, |
| 129 (base::TimeTicks::Now() - base::TimeTicks()).InMicroseconds()); | 130 (now - base::TimeTicks()).InMicroseconds()); |
| 130 SignalSentPacket( | 131 SignalSentPacket( |
| 131 this, rtc::SentPacket(options.packet_id, (base::TimeTicks::Now() - | 132 this, |
| 132 base::TimeTicks::UnixEpoch()) | 133 rtc::SentPacket(options.packet_id, |
| 133 .InMilliseconds())); | 134 (now - base::TimeTicks::UnixEpoch()).InMilliseconds())); |
| 134 dispatcher_->DeliverPacket(local_address_, address, buffer, data_size); | 135 dispatcher_->DeliverPacket(local_address_, address, buffer, data_size); |
| 135 return data_size; | 136 return data_size; |
| 136 } | 137 } |
| 137 | 138 |
| 138 int FakeUdpSocket::Close() { | 139 int FakeUdpSocket::Close() { |
| 139 state_ = STATE_CLOSED; | 140 state_ = STATE_CLOSED; |
| 140 return 0; | 141 return 0; |
| 141 } | 142 } |
| 142 | 143 |
| 143 rtc::AsyncPacketSocket::State FakeUdpSocket::GetState() const { | 144 rtc::AsyncPacketSocket::State FakeUdpSocket::GetState() const { |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 const rtc::SocketAddress& from, | 290 const rtc::SocketAddress& from, |
| 290 const rtc::SocketAddress& to, | 291 const rtc::SocketAddress& to, |
| 291 const scoped_refptr<net::IOBuffer>& data, | 292 const scoped_refptr<net::IOBuffer>& data, |
| 292 int data_size) { | 293 int data_size) { |
| 293 DCHECK(task_runner_->BelongsToCurrentThread()); | 294 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 294 DCHECK(to.ipaddr() == address_); | 295 DCHECK(to.ipaddr() == address_); |
| 295 | 296 |
| 296 base::TimeDelta delay; | 297 base::TimeDelta delay; |
| 297 | 298 |
| 298 if (leaky_bucket_) { | 299 if (leaky_bucket_) { |
| 299 delay = leaky_bucket_->AddPacket(data_size); | 300 base::TimeTicks now = base::TimeTicks::Now(); |
| 300 if (delay.is_max()) { | 301 if (!leaky_bucket_->RefillOrSpill(data_size, now)) { |
| 301 ++total_packets_dropped_; | 302 ++total_packets_dropped_; |
| 302 // Drop the packet. | 303 // Drop the packet. |
| 303 return; | 304 return; |
| 304 } | 305 } |
| 306 delay = std::max(base::TimeDelta(), leaky_bucket_->GetEmptyTime() - now); |
| 305 } | 307 } |
| 306 | 308 |
| 307 total_buffer_delay_ += delay; | 309 total_buffer_delay_ += delay; |
| 308 if (delay > max_buffer_delay_) | 310 if (delay > max_buffer_delay_) |
| 309 max_buffer_delay_ = delay; | 311 max_buffer_delay_ = delay; |
| 310 ++total_packets_received_; | 312 ++total_packets_received_; |
| 311 | 313 |
| 312 if (latency_average_ > base::TimeDelta()) { | 314 if (latency_average_ > base::TimeDelta()) { |
| 313 delay += base::TimeDelta::FromMillisecondsD( | 315 delay += base::TimeDelta::FromMillisecondsD( |
| 314 GetNormalRandom(latency_average_.InMillisecondsF(), | 316 GetNormalRandom(latency_average_.InMillisecondsF(), |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 } | 358 } |
| 357 | 359 |
| 358 void FakePacketSocketFactory::ResetStats() { | 360 void FakePacketSocketFactory::ResetStats() { |
| 359 total_packets_dropped_ = 0; | 361 total_packets_dropped_ = 0; |
| 360 total_packets_received_ = 0; | 362 total_packets_received_ = 0; |
| 361 total_buffer_delay_ = base::TimeDelta(); | 363 total_buffer_delay_ = base::TimeDelta(); |
| 362 max_buffer_delay_ = base::TimeDelta(); | 364 max_buffer_delay_ = base::TimeDelta(); |
| 363 } | 365 } |
| 364 | 366 |
| 365 } // namespace remoting | 367 } // namespace remoting |
| OLD | NEW |