blob: b9690e79d059c1476cad354812d1ea4731d34d79 [file] [log] [blame]
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +00001/*
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.orgfaada6e2013-12-18 20:28:25 +000011#ifndef WEBRTC_TEST_FAKE_NETWORK_PIPE_H_
12#define WEBRTC_TEST_FAKE_NETWORK_PIPE_H_
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000013
14#include <queue>
15
henrike@webrtc.org88fbb2d2014-05-21 21:18:46 +000016#include "webrtc/base/constructormagic.h"
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000017#include "webrtc/system_wrappers/interface/event_wrapper.h"
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000018#include "webrtc/system_wrappers/interface/scoped_ptr.h"
19#include "webrtc/typedefs.h"
20
21namespace webrtc {
22
23class CriticalSectionWrapper;
24class NetworkPacket;
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000025class PacketReceiver;
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000026
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.
32class FakeNetworkPipe {
33 public:
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000034 struct Config {
35 Config()
36 : queue_length(0),
mflodman@webrtc.org7acb65a2012-12-13 15:53:11 +000037 queue_delay_ms(0),
38 delay_standard_deviation_ms(0),
39 link_capacity_kbps(0),
40 loss_percent(0) {
41 }
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000042 // Queue length in number of packets.
mflodman@webrtc.org7acb65a2012-12-13 15:53:11 +000043 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.orgfaada6e2013-12-18 20:28:25 +000054 explicit FakeNetworkPipe(const FakeNetworkPipe::Config& config);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000055 ~FakeNetworkPipe();
56
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000057 // Must not be called in parallel with SendPacket or Process.
58 void SetReceiver(PacketReceiver* receiver);
59
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +000060 // Sets a new configuration. This won't affect packets already in the pipe.
61 void SetConfig(const FakeNetworkPipe::Config& config);
62
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000063 // Sends a new packet to the link.
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000064 void SendPacket(const uint8_t* packet, size_t packet_length);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000065
66 // Processes the network queues and trigger PacketReceiver::IncomingPacket for
67 // packets ready to be delivered.
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000068 void Process();
69 int TimeUntilNextProcess() const;
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000070
71 // Get statistics.
72 float PercentageLoss();
73 int AverageDelay();
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000074 size_t dropped_packets() { return dropped_packets_; }
75 size_t sent_packets() { return sent_packets_; }
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000076
77 private:
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000078 scoped_ptr<CriticalSectionWrapper> lock_;
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000079 PacketReceiver* packet_receiver_;
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000080 std::queue<NetworkPacket*> capacity_link_;
81 std::queue<NetworkPacket*> delay_link_;
82
83 // Link configuration.
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000084 Config config_;
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000085
86 // Statistics.
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000087 size_t dropped_packets_;
88 size_t sent_packets_;
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000089 int total_packet_delay_;
90
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000091 int64_t next_process_time_;
92
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000093 DISALLOW_COPY_AND_ASSIGN(FakeNetworkPipe);
94};
95
96} // namespace webrtc
97
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000098#endif // WEBRTC_TEST_FAKE_NETWORK_PIPE_H_