blob: 71b7de97f8ff0aa67f6ba6ee167017815e7c65c8 [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"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/constructor_magic.h"
24#include "rtc_base/copy_on_write_buffer.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
Artem Titov37d18482019-01-08 15:41:45 +010047// SimulationNode is a EmulatedNetworkNode that expose an interface for changing
48// run time behavior of the underlying simulation.
49class SimulationNode : public EmulatedNetworkNode {
Sebastian Jansson98b07e92018-09-27 13:47:01 +020050 public:
51 void UpdateConfig(std::function<void(NetworkNodeConfig*)> modifier);
52 void PauseTransmissionUntil(Timestamp until);
53 ColumnPrinter ConfigPrinter() const;
54
55 private:
56 friend class Scenario;
57
58 SimulationNode(NetworkNodeConfig config,
Artem Titov8ea1e9d2018-10-04 14:46:31 +020059 std::unique_ptr<NetworkBehaviorInterface> behavior,
Sebastian Jansson98b07e92018-09-27 13:47:01 +020060 SimulatedNetwork* simulation);
61 static std::unique_ptr<SimulationNode> Create(NetworkNodeConfig config);
Artem Titov37d18482019-01-08 15:41:45 +010062
Sebastian Jansson98b07e92018-09-27 13:47:01 +020063 SimulatedNetwork* const simulated_network_;
64 NetworkNodeConfig config_;
65};
66
67class NetworkNodeTransport : public Transport {
68 public:
Sebastian Janssonaa01f272019-01-30 11:28:59 +010069 NetworkNodeTransport(Clock* sender_clock, Call* sender_call);
Sebastian Jansson98b07e92018-09-27 13:47:01 +020070 ~NetworkNodeTransport() override;
71
72 bool SendRtp(const uint8_t* packet,
73 size_t length,
74 const PacketOptions& options) override;
75 bool SendRtcp(const uint8_t* packet, size_t length) override;
Sebastian Jansson800e1212018-10-22 11:49:03 +020076
Artem Titov37d18482019-01-08 15:41:45 +010077 void Connect(EmulatedNetworkNode* send_node,
Artem Titov4cd433e2019-04-01 11:01:16 +020078 rtc::IPAddress receiver_ip,
Sebastian Jansson800e1212018-10-22 11:49:03 +020079 DataSize packet_overhead);
80
81 DataSize packet_overhead() {
82 rtc::CritScope crit(&crit_sect_);
83 return packet_overhead_;
84 }
Sebastian Jansson98b07e92018-09-27 13:47:01 +020085
86 private:
Sebastian Jansson800e1212018-10-22 11:49:03 +020087 rtc::CriticalSection crit_sect_;
Sebastian Janssonaa01f272019-01-30 11:28:59 +010088 Clock* const sender_clock_;
Sebastian Jansson800e1212018-10-22 11:49:03 +020089 Call* const sender_call_;
Artem Titov4cd433e2019-04-01 11:01:16 +020090 // Store local address here for consistency with receiver address.
91 const rtc::SocketAddress local_address_;
Artem Titov37d18482019-01-08 15:41:45 +010092 EmulatedNetworkNode* send_net_ RTC_GUARDED_BY(crit_sect_) = nullptr;
Artem Titov4cd433e2019-04-01 11:01:16 +020093 rtc::SocketAddress receiver_address_ RTC_GUARDED_BY(crit_sect_);
Sebastian Jansson800e1212018-10-22 11:49:03 +020094 DataSize packet_overhead_ RTC_GUARDED_BY(crit_sect_) = DataSize::Zero();
Sebastian Jansson98b07e92018-09-27 13:47:01 +020095};
96
97// CrossTrafficSource is created by a Scenario and generates cross traffic. It
98// provides methods to access and print internal state.
99class CrossTrafficSource {
100 public:
101 DataRate TrafficRate() const;
102 ColumnPrinter StatsPrinter();
103 ~CrossTrafficSource();
104
105 private:
106 friend class Scenario;
Artem Titov40f51152019-01-04 15:45:01 +0100107 CrossTrafficSource(EmulatedNetworkReceiverInterface* target,
Artem Titov4cd433e2019-04-01 11:01:16 +0200108 rtc::IPAddress receiver_ip,
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200109 CrossTrafficConfig config);
110 void Process(Timestamp at_time, TimeDelta delta);
111
Artem Titov40f51152019-01-04 15:45:01 +0100112 EmulatedNetworkReceiverInterface* const target_;
Artem Titov4cd433e2019-04-01 11:01:16 +0200113 const rtc::SocketAddress receiver_address_;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200114 CrossTrafficConfig config_;
115 webrtc::Random random_;
116
117 TimeDelta time_since_update_ = TimeDelta::Zero();
118 double intensity_ = 0;
119 DataSize pending_size_ = DataSize::Zero();
120};
121} // namespace test
122} // namespace webrtc
123#endif // TEST_SCENARIO_NETWORK_NODE_H_