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 <math.h> | 10 #include <cmath> |
11 #include <stddef.h> | 11 #include <cstddef> |
12 | 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/rand_util.h" | |
20 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
21 #include "base/threading/thread_task_runner_handle.h" | 20 #include "base/threading/thread_task_runner_handle.h" |
22 #include "base/time/time.h" | 21 #include "base/time/time.h" |
23 #include "net/base/io_buffer.h" | 22 #include "net/base/io_buffer.h" |
24 #include "remoting/test/leaky_bucket.h" | 23 #include "remoting/test/leaky_bucket.h" |
25 #include "third_party/webrtc/base/asyncpacketsocket.h" | 24 #include "third_party/webrtc/base/asyncpacketsocket.h" |
26 #include "third_party/webrtc/base/socket.h" | 25 #include "third_party/webrtc/base/socket.h" |
27 #include "third_party/webrtc/media/base/rtputils.h" | 26 #include "third_party/webrtc/media/base/rtputils.h" |
28 | 27 |
29 namespace remoting { | 28 namespace remoting { |
30 | 29 |
31 namespace { | 30 namespace { |
32 | 31 |
33 const int kPortRangeStart = 1024; | 32 const int kPortRangeStart = 1024; |
34 const int kPortRangeEnd = 65535; | 33 const int kPortRangeEnd = 65535; |
35 | 34 |
| 35 double RandDouble() { |
| 36 return static_cast<double>(std::rand()) / RAND_MAX; |
| 37 } |
| 38 |
36 double GetNormalRandom(double average, double stddev) { | 39 double GetNormalRandom(double average, double stddev) { |
37 // Based on Box-Muller transform, see | 40 // Based on Box-Muller transform, see |
38 // http://en.wikipedia.org/wiki/Box_Muller_transform . | 41 // http://en.wikipedia.org/wiki/Box_Muller_transform . |
39 return average + | 42 return average + |
40 stddev * sqrt(-2.0 * log(1.0 - base::RandDouble())) * | 43 stddev * sqrt(-2.0 * log(1.0 - RandDouble())) * |
41 cos(base::RandDouble() * 2.0 * M_PI); | 44 cos(RandDouble() * 2.0 * M_PI); |
42 } | 45 } |
43 | 46 |
44 class FakeUdpSocket : public rtc::AsyncPacketSocket { | 47 class FakeUdpSocket : public rtc::AsyncPacketSocket { |
45 public: | 48 public: |
46 FakeUdpSocket(FakePacketSocketFactory* factory, | 49 FakeUdpSocket(FakePacketSocketFactory* factory, |
47 scoped_refptr<FakeNetworkDispatcher> dispatcher, | 50 scoped_refptr<FakeNetworkDispatcher> dispatcher, |
48 const rtc::SocketAddress& local_address); | 51 const rtc::SocketAddress& local_address); |
49 ~FakeUdpSocket() override; | 52 ~FakeUdpSocket() override; |
50 | 53 |
51 void ReceivePacket(const rtc::SocketAddress& from, | 54 void ReceivePacket(const rtc::SocketAddress& from, |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 FROM_HERE, | 324 FROM_HERE, |
322 base::Bind(&FakePacketSocketFactory::DoReceivePacket, | 325 base::Bind(&FakePacketSocketFactory::DoReceivePacket, |
323 weak_factory_.GetWeakPtr()), | 326 weak_factory_.GetWeakPtr()), |
324 delay); | 327 delay); |
325 } | 328 } |
326 | 329 |
327 void FakePacketSocketFactory::DoReceivePacket() { | 330 void FakePacketSocketFactory::DoReceivePacket() { |
328 DCHECK(task_runner_->BelongsToCurrentThread()); | 331 DCHECK(task_runner_->BelongsToCurrentThread()); |
329 | 332 |
330 PendingPacket packet; | 333 PendingPacket packet; |
331 if (pending_packets_.size() > 1 && base::RandDouble() < out_of_order_rate_) { | 334 if (pending_packets_.size() > 1 && RandDouble() < out_of_order_rate_) { |
332 std::list<PendingPacket>::iterator it = pending_packets_.begin(); | 335 std::list<PendingPacket>::iterator it = pending_packets_.begin(); |
333 ++it; | 336 ++it; |
334 packet = *it; | 337 packet = *it; |
335 pending_packets_.erase(it); | 338 pending_packets_.erase(it); |
336 } else { | 339 } else { |
337 packet = pending_packets_.front(); | 340 packet = pending_packets_.front(); |
338 pending_packets_.pop_front(); | 341 pending_packets_.pop_front(); |
339 } | 342 } |
340 | 343 |
341 UdpSocketsMap::iterator iter = udp_sockets_.find(packet.to.port()); | 344 UdpSocketsMap::iterator iter = udp_sockets_.find(packet.to.port()); |
342 if (iter == udp_sockets_.end()) { | 345 if (iter == udp_sockets_.end()) { |
343 // Invalid port number. | 346 // Invalid port number. |
344 return; | 347 return; |
345 } | 348 } |
346 | 349 |
347 iter->second.Run(packet.from, packet.to, packet.data, packet.data_size); | 350 iter->second.Run(packet.from, packet.to, packet.data, packet.data_size); |
348 } | 351 } |
349 | 352 |
350 } // namespace remoting | 353 } // namespace remoting |
OLD | NEW |