mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 11 | #ifndef WEBRTC_TEST_FAKE_NETWORK_PIPE_H_ |
| 12 | #define WEBRTC_TEST_FAKE_NETWORK_PIPE_H_ |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 13 | |
| 14 | #include <queue> |
| 15 | |
mcasas@webrtc.org | 2fa7f79 | 2014-05-21 11:07:29 +0000 | [diff] [blame] | 16 | #include "webrtc/system_wrappers/interface/constructor_magic.h" |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 17 | #include "webrtc/system_wrappers/interface/event_wrapper.h" |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 18 | #include "webrtc/system_wrappers/interface/scoped_ptr.h" |
| 19 | #include "webrtc/typedefs.h" |
| 20 | |
| 21 | namespace webrtc { |
| 22 | |
| 23 | class CriticalSectionWrapper; |
| 24 | class NetworkPacket; |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 25 | class PacketReceiver; |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 26 | |
| 27 | // Class faking a network link. This is a simple and naive solution just faking |
| 28 | // capacity and adding an extra transport delay in addition to the capacity |
| 29 | // introduced delay. |
| 30 | |
| 31 | // TODO(mflodman) Add random and bursty packet loss. |
| 32 | class FakeNetworkPipe { |
| 33 | public: |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 34 | struct Config { |
| 35 | Config() |
| 36 | : queue_length(0), |
mflodman@webrtc.org | 7acb65a | 2012-12-13 15:53:11 +0000 | [diff] [blame] | 37 | queue_delay_ms(0), |
| 38 | delay_standard_deviation_ms(0), |
| 39 | link_capacity_kbps(0), |
| 40 | loss_percent(0) { |
| 41 | } |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 42 | // Queue length in number of packets. |
mflodman@webrtc.org | 7acb65a | 2012-12-13 15:53:11 +0000 | [diff] [blame] | 43 | size_t queue_length; |
| 44 | // Delay in addition to capacity induced delay. |
| 45 | int queue_delay_ms; |
| 46 | // Standard deviation of the extra delay. |
| 47 | int delay_standard_deviation_ms; |
| 48 | // Link capacity in kbps. |
| 49 | int link_capacity_kbps; |
| 50 | // Random packet loss. Not implemented. |
| 51 | int loss_percent; |
| 52 | }; |
| 53 | |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 54 | explicit FakeNetworkPipe(const FakeNetworkPipe::Config& config); |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 55 | ~FakeNetworkPipe(); |
| 56 | |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 57 | // Must not be called in parallel with SendPacket or Process. |
| 58 | void SetReceiver(PacketReceiver* receiver); |
| 59 | |
henrik.lundin@webrtc.org | c0e9aeb | 2014-02-26 13:34:52 +0000 | [diff] [blame] | 60 | // Sets a new configuration. This won't affect packets already in the pipe. |
| 61 | void SetConfig(const FakeNetworkPipe::Config& config); |
| 62 | |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 63 | // Sends a new packet to the link. |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 64 | void SendPacket(const uint8_t* packet, size_t packet_length); |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 65 | |
| 66 | // Processes the network queues and trigger PacketReceiver::IncomingPacket for |
| 67 | // packets ready to be delivered. |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 68 | void Process(); |
| 69 | int TimeUntilNextProcess() const; |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 70 | |
| 71 | // Get statistics. |
| 72 | float PercentageLoss(); |
| 73 | int AverageDelay(); |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 74 | size_t dropped_packets() { return dropped_packets_; } |
| 75 | size_t sent_packets() { return sent_packets_; } |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 76 | |
| 77 | private: |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 78 | scoped_ptr<CriticalSectionWrapper> lock_; |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 79 | PacketReceiver* packet_receiver_; |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 80 | std::queue<NetworkPacket*> capacity_link_; |
| 81 | std::queue<NetworkPacket*> delay_link_; |
| 82 | |
| 83 | // Link configuration. |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 84 | Config config_; |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 85 | |
| 86 | // Statistics. |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 87 | size_t dropped_packets_; |
| 88 | size_t sent_packets_; |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 89 | int total_packet_delay_; |
| 90 | |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 91 | int64_t next_process_time_; |
| 92 | |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 93 | DISALLOW_COPY_AND_ASSIGN(FakeNetworkPipe); |
| 94 | }; |
| 95 | |
| 96 | } // namespace webrtc |
| 97 | |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 98 | #endif // WEBRTC_TEST_FAKE_NETWORK_PIPE_H_ |