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