1eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org/*
2eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org *
4eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org *  Use of this source code is governed by a BSD-style license
5eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org *  that can be found in the LICENSE file in the root of the source
6eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org *  tree. An additional intellectual property rights grant can be found
7eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org *  in the file PATENTS.  All contributing project authors may
8eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
9eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org */
10eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org
11faada6e604e04e0765f93829cd29782667a1f235stefan@webrtc.org#ifndef WEBRTC_TEST_FAKE_NETWORK_PIPE_H_
12faada6e604e04e0765f93829cd29782667a1f235stefan@webrtc.org#define WEBRTC_TEST_FAKE_NETWORK_PIPE_H_
13eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org
14eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org#include <queue>
15eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org
1688fbb2d86b33a3886bba1af4d098efa2c19eb1e7henrike@webrtc.org#include "webrtc/base/constructormagic.h"
17f2f828374c3ee1e1834c72bb27eaae88ef67bb40Peter Boström#include "webrtc/base/criticalsection.h"
1800b8f6b3643332cce1ee711715f7fbb824d793cakwiberg@webrtc.org#include "webrtc/base/scoped_ptr.h"
19eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org#include "webrtc/typedefs.h"
20eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org
21eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.orgnamespace webrtc {
22eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org
23d3c944755ec546f46d5bdd84bff359fe6c4639e9Peter Boströmclass Clock;
24eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.orgclass CriticalSectionWrapper;
25eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.orgclass NetworkPacket;
26faada6e604e04e0765f93829cd29782667a1f235stefan@webrtc.orgclass PacketReceiver;
27eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org
28eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org// Class faking a network link. This is a simple and naive solution just faking
29eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org// capacity and adding an extra transport delay in addition to the capacity
30eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org// introduced delay.
31eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org
32eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org// TODO(mflodman) Add random and bursty packet loss.
33eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.orgclass FakeNetworkPipe {
34eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org public:
35faada6e604e04e0765f93829cd29782667a1f235stefan@webrtc.org  struct Config {
36d3c944755ec546f46d5bdd84bff359fe6c4639e9Peter Boström    Config() {}
37faada6e604e04e0765f93829cd29782667a1f235stefan@webrtc.org    // Queue length in number of packets.
38d3c944755ec546f46d5bdd84bff359fe6c4639e9Peter Boström    size_t queue_length_packets = 0;
397acb65a870d706892a00961d54f48d2a8dd5fe01mflodman@webrtc.org    // Delay in addition to capacity induced delay.
40d3c944755ec546f46d5bdd84bff359fe6c4639e9Peter Boström    int queue_delay_ms = 0;
417acb65a870d706892a00961d54f48d2a8dd5fe01mflodman@webrtc.org    // Standard deviation of the extra delay.
42d3c944755ec546f46d5bdd84bff359fe6c4639e9Peter Boström    int delay_standard_deviation_ms = 0;
437acb65a870d706892a00961d54f48d2a8dd5fe01mflodman@webrtc.org    // Link capacity in kbps.
44d3c944755ec546f46d5bdd84bff359fe6c4639e9Peter Boström    int link_capacity_kbps = 0;
45bfe6e081952540bd74b62627a3b910208e33c490stefan@webrtc.org    // Random packet loss.
46d3c944755ec546f46d5bdd84bff359fe6c4639e9Peter Boström    int loss_percent = 0;
477acb65a870d706892a00961d54f48d2a8dd5fe01mflodman@webrtc.org  };
487acb65a870d706892a00961d54f48d2a8dd5fe01mflodman@webrtc.org
49d3c944755ec546f46d5bdd84bff359fe6c4639e9Peter Boström  FakeNetworkPipe(Clock* clock, const FakeNetworkPipe::Config& config);
50eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org  ~FakeNetworkPipe();
51eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org
52faada6e604e04e0765f93829cd29782667a1f235stefan@webrtc.org  // Must not be called in parallel with SendPacket or Process.
53faada6e604e04e0765f93829cd29782667a1f235stefan@webrtc.org  void SetReceiver(PacketReceiver* receiver);
54faada6e604e04e0765f93829cd29782667a1f235stefan@webrtc.org
55c0e9aebe8f11e8622dc146406d8263f4bb436008henrik.lundin@webrtc.org  // Sets a new configuration. This won't affect packets already in the pipe.
56c0e9aebe8f11e8622dc146406d8263f4bb436008henrik.lundin@webrtc.org  void SetConfig(const FakeNetworkPipe::Config& config);
57c0e9aebe8f11e8622dc146406d8263f4bb436008henrik.lundin@webrtc.org
58eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org  // Sends a new packet to the link.
59faada6e604e04e0765f93829cd29782667a1f235stefan@webrtc.org  void SendPacket(const uint8_t* packet, size_t packet_length);
60eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org
61eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org  // Processes the network queues and trigger PacketReceiver::IncomingPacket for
62eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org  // packets ready to be delivered.
63faada6e604e04e0765f93829cd29782667a1f235stefan@webrtc.org  void Process();
640b1534c52eab79372557a6d81aaf4dd9407f55d3pkasting@chromium.org  int64_t TimeUntilNextProcess() const;
65eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org
66eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org  // Get statistics.
67eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org  float PercentageLoss();
68eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org  int AverageDelay();
69faada6e604e04e0765f93829cd29782667a1f235stefan@webrtc.org  size_t dropped_packets() { return dropped_packets_; }
70faada6e604e04e0765f93829cd29782667a1f235stefan@webrtc.org  size_t sent_packets() { return sent_packets_; }
71eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org
72eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org private:
73d3c944755ec546f46d5bdd84bff359fe6c4639e9Peter Boström  Clock* const clock_;
74f2f828374c3ee1e1834c72bb27eaae88ef67bb40Peter Boström  mutable rtc::CriticalSection lock_;
75eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org  PacketReceiver* packet_receiver_;
76eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org  std::queue<NetworkPacket*> capacity_link_;
77eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org  std::queue<NetworkPacket*> delay_link_;
78eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org
79eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org  // Link configuration.
80faada6e604e04e0765f93829cd29782667a1f235stefan@webrtc.org  Config config_;
81eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org
82eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org  // Statistics.
83faada6e604e04e0765f93829cd29782667a1f235stefan@webrtc.org  size_t dropped_packets_;
84faada6e604e04e0765f93829cd29782667a1f235stefan@webrtc.org  size_t sent_packets_;
85ff2a6351e0ad81ef8123c368fc17eeab40e66c71Stefan Holmer  int64_t total_packet_delay_;
86eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org
87faada6e604e04e0765f93829cd29782667a1f235stefan@webrtc.org  int64_t next_process_time_;
88faada6e604e04e0765f93829cd29782667a1f235stefan@webrtc.org
893c089d751ede283e21e186885eaf705c3257ccd2henrikg  RTC_DISALLOW_COPY_AND_ASSIGN(FakeNetworkPipe);
90eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org};
91eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org
92eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org}  // namespace webrtc
93eaf7cf26fe85b97f64c8668722d1390a6eddfe0emflodman@webrtc.org
94faada6e604e04e0765f93829cd29782667a1f235stefan@webrtc.org#endif  // WEBRTC_TEST_FAKE_NETWORK_PIPE_H_
95