blob: a94df1b34b3ac549cd292231a65a051a56ea7ba4 [file] [log] [blame]
Sebastian Jansson98b07e92018-09-27 13:47:01 +02001/*
2 * Copyright 2018 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#ifndef TEST_SCENARIO_NETWORK_NODE_H_
11#define TEST_SCENARIO_NETWORK_NODE_H_
12
13#include <deque>
14#include <map>
15#include <memory>
16#include <utility>
17#include <vector>
18
19#include "api/call/transport.h"
20#include "api/units/timestamp.h"
21#include "call/simulated_network.h"
22#include "rtc_base/constructormagic.h"
23#include "rtc_base/copyonwritebuffer.h"
24#include "test/scenario/call_client.h"
25#include "test/scenario/column_printer.h"
26#include "test/scenario/scenario_config.h"
27
28namespace webrtc {
29namespace test {
30
31class NetworkReceiverInterface {
32 public:
33 virtual bool TryDeliverPacket(rtc::CopyOnWriteBuffer packet,
34 uint64_t receiver,
35 Timestamp at_time) = 0;
36 virtual ~NetworkReceiverInterface() = default;
37};
38class NullReceiver : public NetworkReceiverInterface {
39 public:
40 bool TryDeliverPacket(rtc::CopyOnWriteBuffer packet,
41 uint64_t receiver,
42 Timestamp at_time) override;
43};
44class ActionReceiver : public NetworkReceiverInterface {
45 public:
46 explicit ActionReceiver(std::function<void()> action);
47 virtual ~ActionReceiver() = default;
48 bool TryDeliverPacket(rtc::CopyOnWriteBuffer packet,
49 uint64_t receiver,
50 Timestamp at_time) override;
51
52 private:
53 std::function<void()> action_;
54};
55
56// NetworkNode represents one link in a simulated network. It is created by a
57// scenario and can be used when setting up audio and video stream sessions.
58class NetworkNode : public NetworkReceiverInterface {
59 public:
60 ~NetworkNode() override;
61 RTC_DISALLOW_COPY_AND_ASSIGN(NetworkNode);
62
63 bool TryDeliverPacket(rtc::CopyOnWriteBuffer packet,
64 uint64_t receiver,
65 Timestamp at_time) override;
66 // Creates a route for the given receiver_id over all the given nodes to the
67 // given receiver.
68 static void Route(int64_t receiver_id,
69 std::vector<NetworkNode*> nodes,
70 NetworkReceiverInterface* receiver);
71
72 protected:
73 friend class Scenario;
74 friend class AudioStreamPair;
75 friend class VideoStreamPair;
76
77 NetworkNode(NetworkNodeConfig config,
78 std::unique_ptr<NetworkSimulationInterface> simulation);
79 static void ClearRoute(int64_t receiver_id, std::vector<NetworkNode*> nodes);
80 void Process(Timestamp at_time);
81
82 private:
83 struct StoredPacket {
84 rtc::CopyOnWriteBuffer packet_data;
85 uint64_t receiver_id;
86 uint64_t id;
87 bool removed;
88 };
89 void SetRoute(uint64_t receiver, NetworkReceiverInterface* node);
90 void ClearRoute(uint64_t receiver_id);
91 rtc::CriticalSection crit_sect_;
92 size_t packet_overhead_ RTC_GUARDED_BY(crit_sect_);
93 const std::unique_ptr<NetworkSimulationInterface> simulation_
94 RTC_GUARDED_BY(crit_sect_);
95 std::map<uint64_t, NetworkReceiverInterface*> routing_
96 RTC_GUARDED_BY(crit_sect_);
97 std::deque<StoredPacket> packets_ RTC_GUARDED_BY(crit_sect_);
98
99 uint64_t next_packet_id_ RTC_GUARDED_BY(crit_sect_) = 1;
100};
101// SimulationNode is a NetworkNode that expose an interface for changing run
102// time behavior of the underlying simulation.
103class SimulationNode : public NetworkNode {
104 public:
105 void UpdateConfig(std::function<void(NetworkNodeConfig*)> modifier);
106 void PauseTransmissionUntil(Timestamp until);
107 ColumnPrinter ConfigPrinter() const;
108
109 private:
110 friend class Scenario;
111
112 SimulationNode(NetworkNodeConfig config,
113 std::unique_ptr<NetworkSimulationInterface> behavior,
114 SimulatedNetwork* simulation);
115 static std::unique_ptr<SimulationNode> Create(NetworkNodeConfig config);
116 SimulatedNetwork* const simulated_network_;
117 NetworkNodeConfig config_;
118};
119
120class NetworkNodeTransport : public Transport {
121 public:
122 NetworkNodeTransport(CallClient* sender,
123 NetworkNode* send_net,
124 uint64_t receiver,
125 DataSize packet_overhead);
126 ~NetworkNodeTransport() override;
127
128 bool SendRtp(const uint8_t* packet,
129 size_t length,
130 const PacketOptions& options) override;
131 bool SendRtcp(const uint8_t* packet, size_t length) override;
132 uint64_t ReceiverId() const;
133
134 private:
135 CallClient* const sender_;
136 NetworkNode* const send_net_;
137 const uint64_t receiver_id_;
138 const DataSize packet_overhead_;
139};
140
141// CrossTrafficSource is created by a Scenario and generates cross traffic. It
142// provides methods to access and print internal state.
143class CrossTrafficSource {
144 public:
145 DataRate TrafficRate() const;
146 ColumnPrinter StatsPrinter();
147 ~CrossTrafficSource();
148
149 private:
150 friend class Scenario;
151 CrossTrafficSource(NetworkReceiverInterface* target,
152 uint64_t receiver_id,
153 CrossTrafficConfig config);
154 void Process(Timestamp at_time, TimeDelta delta);
155
156 NetworkReceiverInterface* const target_;
157 const uint64_t receiver_id_;
158 CrossTrafficConfig config_;
159 webrtc::Random random_;
160
161 TimeDelta time_since_update_ = TimeDelta::Zero();
162 double intensity_ = 0;
163 DataSize pending_size_ = DataSize::Zero();
164};
165} // namespace test
166} // namespace webrtc
167#endif // TEST_SCENARIO_NETWORK_NODE_H_