• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 #ifndef WEBRTC_TEST_FAKE_NETWORK_PIPE_H_
12 #define WEBRTC_TEST_FAKE_NETWORK_PIPE_H_
13 
14 #include <queue>
15 
16 #include "webrtc/base/constructormagic.h"
17 #include "webrtc/system_wrappers/interface/event_wrapper.h"
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;
25 class PacketReceiver;
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:
34   struct Config {
ConfigConfig35     Config()
36         : queue_length_packets(0),
37           queue_delay_ms(0),
38           delay_standard_deviation_ms(0),
39           link_capacity_kbps(0),
40           loss_percent(0) {
41     }
42     // Queue length in number of packets.
43     size_t queue_length_packets;
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.
51     int loss_percent;
52   };
53 
54   explicit FakeNetworkPipe(const FakeNetworkPipe::Config& config);
55   ~FakeNetworkPipe();
56 
57   // Must not be called in parallel with SendPacket or Process.
58   void SetReceiver(PacketReceiver* receiver);
59 
60   // Sets a new configuration. This won't affect packets already in the pipe.
61   void SetConfig(const FakeNetworkPipe::Config& config);
62 
63   // Sends a new packet to the link.
64   void SendPacket(const uint8_t* packet, size_t packet_length);
65 
66   // Processes the network queues and trigger PacketReceiver::IncomingPacket for
67   // packets ready to be delivered.
68   void Process();
69   int TimeUntilNextProcess() const;
70 
71   // Get statistics.
72   float PercentageLoss();
73   int AverageDelay();
dropped_packets()74   size_t dropped_packets() { return dropped_packets_; }
sent_packets()75   size_t sent_packets() { return sent_packets_; }
76 
77  private:
78   scoped_ptr<CriticalSectionWrapper> lock_;
79   PacketReceiver* packet_receiver_;
80   std::queue<NetworkPacket*> capacity_link_;
81   std::queue<NetworkPacket*> delay_link_;
82 
83   // Link configuration.
84   Config config_;
85 
86   // Statistics.
87   size_t dropped_packets_;
88   size_t sent_packets_;
89   int total_packet_delay_;
90 
91   int64_t next_process_time_;
92 
93   DISALLOW_COPY_AND_ASSIGN(FakeNetworkPipe);
94 };
95 
96 }  // namespace webrtc
97 
98 #endif  // WEBRTC_TEST_FAKE_NETWORK_PIPE_H_
99