blob: e47d435e8b4a2992abb0819bf0cf6d95d481aec0 [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"
Sebastian Jansson800e1212018-10-22 11:49:03 +020021#include "call/call.h"
Sebastian Jansson98b07e92018-09-27 13:47:01 +020022#include "call/simulated_network.h"
23#include "rtc_base/constructormagic.h"
24#include "rtc_base/copyonwritebuffer.h"
Sebastian Jansson98b07e92018-09-27 13:47:01 +020025#include "test/scenario/column_printer.h"
26#include "test/scenario/scenario_config.h"
27
28namespace webrtc {
29namespace test {
30
31class NetworkReceiverInterface {
32 public:
Sebastian Janssonf65309c2018-12-20 10:26:00 +010033 virtual void DeliverPacket(rtc::CopyOnWriteBuffer packet,
34 uint64_t receiver,
35 Timestamp at_time) = 0;
Sebastian Jansson98b07e92018-09-27 13:47:01 +020036 virtual ~NetworkReceiverInterface() = default;
37};
38class NullReceiver : public NetworkReceiverInterface {
39 public:
Sebastian Janssonf65309c2018-12-20 10:26:00 +010040 void DeliverPacket(rtc::CopyOnWriteBuffer packet,
41 uint64_t receiver,
42 Timestamp at_time) override;
Sebastian Jansson98b07e92018-09-27 13:47:01 +020043};
44class ActionReceiver : public NetworkReceiverInterface {
45 public:
46 explicit ActionReceiver(std::function<void()> action);
47 virtual ~ActionReceiver() = default;
Sebastian Janssonf65309c2018-12-20 10:26:00 +010048 void DeliverPacket(rtc::CopyOnWriteBuffer packet,
49 uint64_t receiver,
50 Timestamp at_time) override;
Sebastian Jansson98b07e92018-09-27 13:47:01 +020051
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
Sebastian Janssonf65309c2018-12-20 10:26:00 +010063 void DeliverPacket(rtc::CopyOnWriteBuffer packet,
64 uint64_t receiver,
65 Timestamp at_time) override;
Sebastian Jansson98b07e92018-09-27 13:47:01 +020066 // 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,
Artem Titov8ea1e9d2018-10-04 14:46:31 +020078 std::unique_ptr<NetworkBehaviorInterface> simulation);
Sebastian Jansson98b07e92018-09-27 13:47:01 +020079 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_);
Artem Titov8ea1e9d2018-10-04 14:46:31 +020093 const std::unique_ptr<NetworkBehaviorInterface> behavior_
Sebastian Jansson98b07e92018-09-27 13:47:01 +020094 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,
Artem Titov8ea1e9d2018-10-04 14:46:31 +0200113 std::unique_ptr<NetworkBehaviorInterface> behavior,
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200114 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:
Sebastian Jansson800e1212018-10-22 11:49:03 +0200122 NetworkNodeTransport(const Clock* sender_clock, Call* sender_call);
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200123 ~NetworkNodeTransport() override;
124
125 bool SendRtp(const uint8_t* packet,
126 size_t length,
127 const PacketOptions& options) override;
128 bool SendRtcp(const uint8_t* packet, size_t length) override;
Sebastian Jansson800e1212018-10-22 11:49:03 +0200129
130 void Connect(NetworkNode* send_node,
131 uint64_t receiver_id,
132 DataSize packet_overhead);
133
134 DataSize packet_overhead() {
135 rtc::CritScope crit(&crit_sect_);
136 return packet_overhead_;
137 }
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200138
139 private:
Sebastian Jansson800e1212018-10-22 11:49:03 +0200140 rtc::CriticalSection crit_sect_;
141 const Clock* const sender_clock_;
142 Call* const sender_call_;
143 NetworkNode* send_net_ RTC_GUARDED_BY(crit_sect_) = nullptr;
144 uint64_t receiver_id_ RTC_GUARDED_BY(crit_sect_) = 0;
145 DataSize packet_overhead_ RTC_GUARDED_BY(crit_sect_) = DataSize::Zero();
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200146};
147
148// CrossTrafficSource is created by a Scenario and generates cross traffic. It
149// provides methods to access and print internal state.
150class CrossTrafficSource {
151 public:
152 DataRate TrafficRate() const;
153 ColumnPrinter StatsPrinter();
154 ~CrossTrafficSource();
155
156 private:
157 friend class Scenario;
158 CrossTrafficSource(NetworkReceiverInterface* target,
159 uint64_t receiver_id,
160 CrossTrafficConfig config);
161 void Process(Timestamp at_time, TimeDelta delta);
162
163 NetworkReceiverInterface* const target_;
164 const uint64_t receiver_id_;
165 CrossTrafficConfig config_;
166 webrtc::Random random_;
167
168 TimeDelta time_since_update_ = TimeDelta::Zero();
169 double intensity_ = 0;
170 DataSize pending_size_ = DataSize::Zero();
171};
172} // namespace test
173} // namespace webrtc
174#endif // TEST_SCENARIO_NETWORK_NODE_H_