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 <stdlib.h> |
| 6 |
5 #include "media/cast/test/utility/udp_proxy.h" | 7 #include "media/cast/test/utility/udp_proxy.h" |
6 | 8 |
7 #include "base/logging.h" | 9 #include "base/logging.h" |
8 #include "base/memory/linked_ptr.h" | 10 #include "base/memory/linked_ptr.h" |
9 #include "base/rand_util.h" | 11 #include "base/rand_util.h" |
10 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
11 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
12 #include "base/time/default_tick_clock.h" | 14 #include "base/time/default_tick_clock.h" |
13 #include "net/base/io_buffer.h" | 15 #include "net/base/io_buffer.h" |
14 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
(...skipping 26 matching lines...) Expand all Loading... |
41 | 43 |
42 // Roughly emulates a buffer inside a device. | 44 // Roughly emulates a buffer inside a device. |
43 // If the buffer is full, packets are dropped. | 45 // If the buffer is full, packets are dropped. |
44 // Packets are output at a maximum bandwidth. | 46 // Packets are output at a maximum bandwidth. |
45 class Buffer : public PacketPipe { | 47 class Buffer : public PacketPipe { |
46 public: | 48 public: |
47 Buffer(size_t buffer_size, double max_megabits_per_second) | 49 Buffer(size_t buffer_size, double max_megabits_per_second) |
48 : buffer_size_(0), | 50 : buffer_size_(0), |
49 max_buffer_size_(buffer_size), | 51 max_buffer_size_(buffer_size), |
50 max_megabits_per_second_(max_megabits_per_second), | 52 max_megabits_per_second_(max_megabits_per_second), |
51 weak_factory_(this) {} | 53 weak_factory_(this) { |
| 54 CHECK_GT(max_buffer_size_, 0UL); |
| 55 CHECK_GT(max_megabits_per_second, 0); |
| 56 } |
52 | 57 |
53 virtual void Send(scoped_ptr<transport::Packet> packet) OVERRIDE { | 58 virtual void Send(scoped_ptr<transport::Packet> packet) OVERRIDE { |
54 if (packet->size() + buffer_size_ <= max_buffer_size_) { | 59 if (packet->size() + buffer_size_ <= max_buffer_size_) { |
55 buffer_size_ += packet->size(); | 60 buffer_size_ += packet->size(); |
56 buffer_.push_back(linked_ptr<transport::Packet>(packet.release())); | 61 buffer_.push_back(linked_ptr<transport::Packet>(packet.release())); |
57 if (buffer_.size() == 1) { | 62 if (buffer_.size() == 1) { |
58 Schedule(); | 63 Schedule(); |
59 } | 64 } |
60 } | 65 } |
61 } | 66 } |
(...skipping 26 matching lines...) Expand all Loading... |
88 double max_megabits_per_second_; // megabits per second | 93 double max_megabits_per_second_; // megabits per second |
89 base::WeakPtrFactory<Buffer> weak_factory_; | 94 base::WeakPtrFactory<Buffer> weak_factory_; |
90 }; | 95 }; |
91 | 96 |
92 scoped_ptr<PacketPipe> NewBuffer(size_t buffer_size, double bandwidth) { | 97 scoped_ptr<PacketPipe> NewBuffer(size_t buffer_size, double bandwidth) { |
93 return scoped_ptr<PacketPipe>(new Buffer(buffer_size, bandwidth)).Pass(); | 98 return scoped_ptr<PacketPipe>(new Buffer(buffer_size, bandwidth)).Pass(); |
94 } | 99 } |
95 | 100 |
96 class RandomDrop : public PacketPipe { | 101 class RandomDrop : public PacketPipe { |
97 public: | 102 public: |
98 RandomDrop(double drop_fraction) : drop_fraction_(drop_fraction) { | 103 RandomDrop(double drop_fraction) |
99 } | 104 : drop_fraction_(static_cast<int>(drop_fraction * RAND_MAX)) {} |
100 | 105 |
101 virtual void Send(scoped_ptr<transport::Packet> packet) OVERRIDE { | 106 virtual void Send(scoped_ptr<transport::Packet> packet) OVERRIDE { |
102 if (base::RandDouble() >= drop_fraction_) { | 107 if (rand() > drop_fraction_) { |
103 pipe_->Send(packet.Pass()); | 108 pipe_->Send(packet.Pass()); |
104 } | 109 } |
105 } | 110 } |
106 | 111 |
107 private: | 112 private: |
108 double drop_fraction_; | 113 int drop_fraction_; |
109 }; | 114 }; |
110 | 115 |
111 scoped_ptr<PacketPipe> NewRandomDrop(double drop_fraction) { | 116 scoped_ptr<PacketPipe> NewRandomDrop(double drop_fraction) { |
112 return scoped_ptr<PacketPipe>(new RandomDrop(drop_fraction)).Pass(); | 117 return scoped_ptr<PacketPipe>(new RandomDrop(drop_fraction)).Pass(); |
113 } | 118 } |
114 | 119 |
115 class SimpleDelayBase : public PacketPipe { | 120 class SimpleDelayBase : public PacketPipe { |
116 public: | 121 public: |
117 SimpleDelayBase() : weak_factory_(this) {} | 122 SimpleDelayBase() : weak_factory_(this) {} |
118 virtual ~SimpleDelayBase() {} | 123 virtual ~SimpleDelayBase() {} |
(...skipping 460 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
579 destination, | 584 destination, |
580 to_dest_pipe.Pass(), | 585 to_dest_pipe.Pass(), |
581 from_dest_pipe.Pass(), | 586 from_dest_pipe.Pass(), |
582 net_log)); | 587 net_log)); |
583 return ret.Pass(); | 588 return ret.Pass(); |
584 } | 589 } |
585 | 590 |
586 } // namespace test | 591 } // namespace test |
587 } // namespace cast | 592 } // namespace cast |
588 } // namespace media | 593 } // namespace media |
OLD | NEW |