blob: dd28d7fa8843b8dfc7a6edac3b633abc85415ddb [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"
Artem Titov40f51152019-01-04 15:45:01 +010026#include "test/scenario/network/network_emulation.h"
Sebastian Jansson98b07e92018-09-27 13:47:01 +020027#include "test/scenario/scenario_config.h"
28
29namespace webrtc {
30namespace test {
31
Artem Titov40f51152019-01-04 15:45:01 +010032class NullReceiver : public EmulatedNetworkReceiverInterface {
Sebastian Jansson98b07e92018-09-27 13:47:01 +020033 public:
Artem Titov40f51152019-01-04 15:45:01 +010034 void OnPacketReceived(EmulatedIpPacket packet) override;
Sebastian Jansson98b07e92018-09-27 13:47:01 +020035};
Artem Titov40f51152019-01-04 15:45:01 +010036class ActionReceiver : public EmulatedNetworkReceiverInterface {
Sebastian Jansson98b07e92018-09-27 13:47:01 +020037 public:
38 explicit ActionReceiver(std::function<void()> action);
39 virtual ~ActionReceiver() = default;
Artem Titov40f51152019-01-04 15:45:01 +010040
41 void OnPacketReceived(EmulatedIpPacket packet) override;
Sebastian Jansson98b07e92018-09-27 13:47:01 +020042
43 private:
44 std::function<void()> action_;
45};
46
47// NetworkNode represents one link in a simulated network. It is created by a
48// scenario and can be used when setting up audio and video stream sessions.
Artem Titov40f51152019-01-04 15:45:01 +010049class NetworkNode : public EmulatedNetworkReceiverInterface {
Sebastian Jansson98b07e92018-09-27 13:47:01 +020050 public:
51 ~NetworkNode() override;
52 RTC_DISALLOW_COPY_AND_ASSIGN(NetworkNode);
53
Artem Titov40f51152019-01-04 15:45:01 +010054 void OnPacketReceived(EmulatedIpPacket packet) override;
Sebastian Jansson98b07e92018-09-27 13:47:01 +020055 // Creates a route for the given receiver_id over all the given nodes to the
56 // given receiver.
Artem Titov40f51152019-01-04 15:45:01 +010057 static void Route(uint64_t receiver_id,
Sebastian Jansson98b07e92018-09-27 13:47:01 +020058 std::vector<NetworkNode*> nodes,
Artem Titov40f51152019-01-04 15:45:01 +010059 EmulatedNetworkReceiverInterface* receiver);
Sebastian Jansson98b07e92018-09-27 13:47:01 +020060
61 protected:
62 friend class Scenario;
63 friend class AudioStreamPair;
64 friend class VideoStreamPair;
65
66 NetworkNode(NetworkNodeConfig config,
Artem Titov8ea1e9d2018-10-04 14:46:31 +020067 std::unique_ptr<NetworkBehaviorInterface> simulation);
Artem Titov40f51152019-01-04 15:45:01 +010068 static void ClearRoute(uint64_t receiver_id, std::vector<NetworkNode*> nodes);
Sebastian Jansson98b07e92018-09-27 13:47:01 +020069 void Process(Timestamp at_time);
70
71 private:
72 struct StoredPacket {
Artem Titov40f51152019-01-04 15:45:01 +010073 EmulatedIpPacket packet;
Sebastian Jansson98b07e92018-09-27 13:47:01 +020074 uint64_t id;
75 bool removed;
76 };
Artem Titov40f51152019-01-04 15:45:01 +010077 void SetRoute(uint64_t receiver, EmulatedNetworkReceiverInterface* node);
Sebastian Jansson98b07e92018-09-27 13:47:01 +020078 void ClearRoute(uint64_t receiver_id);
79 rtc::CriticalSection crit_sect_;
80 size_t packet_overhead_ RTC_GUARDED_BY(crit_sect_);
Artem Titov8ea1e9d2018-10-04 14:46:31 +020081 const std::unique_ptr<NetworkBehaviorInterface> behavior_
Sebastian Jansson98b07e92018-09-27 13:47:01 +020082 RTC_GUARDED_BY(crit_sect_);
Artem Titov40f51152019-01-04 15:45:01 +010083 std::map<uint64_t, EmulatedNetworkReceiverInterface*> routing_
Sebastian Jansson98b07e92018-09-27 13:47:01 +020084 RTC_GUARDED_BY(crit_sect_);
85 std::deque<StoredPacket> packets_ RTC_GUARDED_BY(crit_sect_);
86
87 uint64_t next_packet_id_ RTC_GUARDED_BY(crit_sect_) = 1;
88};
89// SimulationNode is a NetworkNode that expose an interface for changing run
90// time behavior of the underlying simulation.
91class SimulationNode : public NetworkNode {
92 public:
93 void UpdateConfig(std::function<void(NetworkNodeConfig*)> modifier);
94 void PauseTransmissionUntil(Timestamp until);
95 ColumnPrinter ConfigPrinter() const;
96
97 private:
98 friend class Scenario;
99
100 SimulationNode(NetworkNodeConfig config,
Artem Titov8ea1e9d2018-10-04 14:46:31 +0200101 std::unique_ptr<NetworkBehaviorInterface> behavior,
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200102 SimulatedNetwork* simulation);
103 static std::unique_ptr<SimulationNode> Create(NetworkNodeConfig config);
104 SimulatedNetwork* const simulated_network_;
105 NetworkNodeConfig config_;
106};
107
108class NetworkNodeTransport : public Transport {
109 public:
Sebastian Jansson800e1212018-10-22 11:49:03 +0200110 NetworkNodeTransport(const Clock* sender_clock, Call* sender_call);
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200111 ~NetworkNodeTransport() override;
112
113 bool SendRtp(const uint8_t* packet,
114 size_t length,
115 const PacketOptions& options) override;
116 bool SendRtcp(const uint8_t* packet, size_t length) override;
Sebastian Jansson800e1212018-10-22 11:49:03 +0200117
118 void Connect(NetworkNode* send_node,
119 uint64_t receiver_id,
120 DataSize packet_overhead);
121
122 DataSize packet_overhead() {
123 rtc::CritScope crit(&crit_sect_);
124 return packet_overhead_;
125 }
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200126
127 private:
Sebastian Jansson800e1212018-10-22 11:49:03 +0200128 rtc::CriticalSection crit_sect_;
129 const Clock* const sender_clock_;
130 Call* const sender_call_;
131 NetworkNode* send_net_ RTC_GUARDED_BY(crit_sect_) = nullptr;
132 uint64_t receiver_id_ RTC_GUARDED_BY(crit_sect_) = 0;
133 DataSize packet_overhead_ RTC_GUARDED_BY(crit_sect_) = DataSize::Zero();
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200134};
135
136// CrossTrafficSource is created by a Scenario and generates cross traffic. It
137// provides methods to access and print internal state.
138class CrossTrafficSource {
139 public:
140 DataRate TrafficRate() const;
141 ColumnPrinter StatsPrinter();
142 ~CrossTrafficSource();
143
144 private:
145 friend class Scenario;
Artem Titov40f51152019-01-04 15:45:01 +0100146 CrossTrafficSource(EmulatedNetworkReceiverInterface* target,
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200147 uint64_t receiver_id,
148 CrossTrafficConfig config);
149 void Process(Timestamp at_time, TimeDelta delta);
150
Artem Titov40f51152019-01-04 15:45:01 +0100151 EmulatedNetworkReceiverInterface* const target_;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200152 const uint64_t receiver_id_;
153 CrossTrafficConfig config_;
154 webrtc::Random random_;
155
156 TimeDelta time_since_update_ = TimeDelta::Zero();
157 double intensity_ = 0;
158 DataSize pending_size_ = DataSize::Zero();
159};
160} // namespace test
161} // namespace webrtc
162#endif // TEST_SCENARIO_NETWORK_NODE_H_