blob: 99c5c1366bc3038e7aafe9d276fc14c69195d0f3 [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"
Peter Boströmf2f82832015-05-01 13:00:41 +020017#include "webrtc/base/criticalsection.h"
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000018#include "webrtc/base/scoped_ptr.h"
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000019#include "webrtc/typedefs.h"
20
21namespace webrtc {
22
Peter Boströmd3c94472015-12-09 11:20:58 +010023class Clock;
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000024class CriticalSectionWrapper;
25class NetworkPacket;
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000026class PacketReceiver;
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000027
28// Class faking a network link. This is a simple and naive solution just faking
29// capacity and adding an extra transport delay in addition to the capacity
30// introduced delay.
31
32// TODO(mflodman) Add random and bursty packet loss.
33class FakeNetworkPipe {
34 public:
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000035 struct Config {
Peter Boströmd3c94472015-12-09 11:20:58 +010036 Config() {}
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000037 // Queue length in number of packets.
Peter Boströmd3c94472015-12-09 11:20:58 +010038 size_t queue_length_packets = 0;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11 +000039 // Delay in addition to capacity induced delay.
Peter Boströmd3c94472015-12-09 11:20:58 +010040 int queue_delay_ms = 0;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11 +000041 // Standard deviation of the extra delay.
Peter Boströmd3c94472015-12-09 11:20:58 +010042 int delay_standard_deviation_ms = 0;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11 +000043 // Link capacity in kbps.
Peter Boströmd3c94472015-12-09 11:20:58 +010044 int link_capacity_kbps = 0;
stefan@webrtc.orgbfe6e082014-07-31 12:30:18 +000045 // Random packet loss.
Peter Boströmd3c94472015-12-09 11:20:58 +010046 int loss_percent = 0;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11 +000047 };
48
Peter Boströmd3c94472015-12-09 11:20:58 +010049 FakeNetworkPipe(Clock* clock, const FakeNetworkPipe::Config& config);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000050 ~FakeNetworkPipe();
51
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000052 // Must not be called in parallel with SendPacket or Process.
53 void SetReceiver(PacketReceiver* receiver);
54
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +000055 // Sets a new configuration. This won't affect packets already in the pipe.
56 void SetConfig(const FakeNetworkPipe::Config& config);
57
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000058 // Sends a new packet to the link.
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000059 void SendPacket(const uint8_t* packet, size_t packet_length);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000060
61 // Processes the network queues and trigger PacketReceiver::IncomingPacket for
62 // packets ready to be delivered.
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000063 void Process();
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000064 int64_t TimeUntilNextProcess() const;
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000065
66 // Get statistics.
67 float PercentageLoss();
68 int AverageDelay();
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000069 size_t dropped_packets() { return dropped_packets_; }
70 size_t sent_packets() { return sent_packets_; }
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000071
72 private:
Peter Boströmd3c94472015-12-09 11:20:58 +010073 Clock* const clock_;
pbos5ad935c2016-01-25 03:52:44 -080074 rtc::CriticalSection lock_;
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000075 PacketReceiver* packet_receiver_;
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000076 std::queue<NetworkPacket*> capacity_link_;
77 std::queue<NetworkPacket*> delay_link_;
78
79 // Link configuration.
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000080 Config config_;
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000081
82 // Statistics.
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000083 size_t dropped_packets_;
84 size_t sent_packets_;
Stefan Holmerff2a6352016-01-14 10:00:21 +010085 int64_t total_packet_delay_;
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000086
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000087 int64_t next_process_time_;
88
henrikg3c089d72015-09-16 05:37:44 -070089 RTC_DISALLOW_COPY_AND_ASSIGN(FakeNetworkPipe);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000090};
91
92} // namespace webrtc
93
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000094#endif // WEBRTC_TEST_FAKE_NETWORK_PIPE_H_