blob: 4cb5bcdb9081e7c30587e932912139b7b333629d [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 Jansson4124dab2019-04-01 14:33:53 +020025#include "rtc_base/task_queue.h"
Sebastian Jansson98b07e92018-09-27 13:47:01 +020026#include "test/scenario/column_printer.h"
Artem Titov40f51152019-01-04 15:45:01 +010027#include "test/scenario/network/network_emulation.h"
Sebastian Jansson98b07e92018-09-27 13:47:01 +020028#include "test/scenario/scenario_config.h"
29
30namespace webrtc {
31namespace test {
32
Artem Titov40f51152019-01-04 15:45:01 +010033class NullReceiver : public EmulatedNetworkReceiverInterface {
Sebastian Jansson98b07e92018-09-27 13:47:01 +020034 public:
Artem Titov40f51152019-01-04 15:45:01 +010035 void OnPacketReceived(EmulatedIpPacket packet) override;
Sebastian Jansson98b07e92018-09-27 13:47:01 +020036};
Artem Titov40f51152019-01-04 15:45:01 +010037class ActionReceiver : public EmulatedNetworkReceiverInterface {
Sebastian Jansson98b07e92018-09-27 13:47:01 +020038 public:
39 explicit ActionReceiver(std::function<void()> action);
40 virtual ~ActionReceiver() = default;
Artem Titov40f51152019-01-04 15:45:01 +010041
42 void OnPacketReceived(EmulatedIpPacket packet) override;
Sebastian Jansson98b07e92018-09-27 13:47:01 +020043
44 private:
45 std::function<void()> action_;
46};
47
Artem Titov37d18482019-01-08 15:41:45 +010048// SimulationNode is a EmulatedNetworkNode that expose an interface for changing
49// run time behavior of the underlying simulation.
50class SimulationNode : public EmulatedNetworkNode {
Sebastian Jansson98b07e92018-09-27 13:47:01 +020051 public:
52 void UpdateConfig(std::function<void(NetworkNodeConfig*)> modifier);
53 void PauseTransmissionUntil(Timestamp until);
54 ColumnPrinter ConfigPrinter() const;
55
56 private:
57 friend class Scenario;
58
Sebastian Jansson4124dab2019-04-01 14:33:53 +020059 SimulationNode(Clock* clock,
60 rtc::TaskQueue* task_queue,
61 NetworkNodeConfig config,
Artem Titov8ea1e9d2018-10-04 14:46:31 +020062 std::unique_ptr<NetworkBehaviorInterface> behavior,
Sebastian Jansson98b07e92018-09-27 13:47:01 +020063 SimulatedNetwork* simulation);
Sebastian Jansson4124dab2019-04-01 14:33:53 +020064 static std::unique_ptr<SimulationNode> Create(Clock* clock,
65 rtc::TaskQueue* task_queue,
66 NetworkNodeConfig config);
Artem Titov37d18482019-01-08 15:41:45 +010067
Sebastian Jansson98b07e92018-09-27 13:47:01 +020068 SimulatedNetwork* const simulated_network_;
69 NetworkNodeConfig config_;
70};
71
72class NetworkNodeTransport : public Transport {
73 public:
Sebastian Janssonaa01f272019-01-30 11:28:59 +010074 NetworkNodeTransport(Clock* sender_clock, Call* sender_call);
Sebastian Jansson98b07e92018-09-27 13:47:01 +020075 ~NetworkNodeTransport() override;
76
77 bool SendRtp(const uint8_t* packet,
78 size_t length,
79 const PacketOptions& options) override;
80 bool SendRtcp(const uint8_t* packet, size_t length) override;
Sebastian Jansson800e1212018-10-22 11:49:03 +020081
Artem Titov37d18482019-01-08 15:41:45 +010082 void Connect(EmulatedNetworkNode* send_node,
Artem Titov4cd433e2019-04-01 11:01:16 +020083 rtc::IPAddress receiver_ip,
Sebastian Jansson800e1212018-10-22 11:49:03 +020084 DataSize packet_overhead);
Sebastian Jansson4124dab2019-04-01 14:33:53 +020085 void Disconnect();
Sebastian Jansson800e1212018-10-22 11:49:03 +020086
87 DataSize packet_overhead() {
88 rtc::CritScope crit(&crit_sect_);
89 return packet_overhead_;
90 }
Sebastian Jansson98b07e92018-09-27 13:47:01 +020091
92 private:
Sebastian Jansson800e1212018-10-22 11:49:03 +020093 rtc::CriticalSection crit_sect_;
Sebastian Janssonaa01f272019-01-30 11:28:59 +010094 Clock* const sender_clock_;
Sebastian Jansson800e1212018-10-22 11:49:03 +020095 Call* const sender_call_;
Artem Titov4cd433e2019-04-01 11:01:16 +020096 // Store local address here for consistency with receiver address.
97 const rtc::SocketAddress local_address_;
Artem Titov37d18482019-01-08 15:41:45 +010098 EmulatedNetworkNode* send_net_ RTC_GUARDED_BY(crit_sect_) = nullptr;
Artem Titov4cd433e2019-04-01 11:01:16 +020099 rtc::SocketAddress receiver_address_ RTC_GUARDED_BY(crit_sect_);
Sebastian Jansson800e1212018-10-22 11:49:03 +0200100 DataSize packet_overhead_ RTC_GUARDED_BY(crit_sect_) = DataSize::Zero();
Sebastian Jansson4124dab2019-04-01 14:33:53 +0200101 rtc::NetworkRoute current_network_route_ RTC_GUARDED_BY(crit_sect_);
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200102};
103
104// CrossTrafficSource is created by a Scenario and generates cross traffic. It
105// provides methods to access and print internal state.
106class CrossTrafficSource {
107 public:
108 DataRate TrafficRate() const;
109 ColumnPrinter StatsPrinter();
110 ~CrossTrafficSource();
111
112 private:
113 friend class Scenario;
Artem Titov40f51152019-01-04 15:45:01 +0100114 CrossTrafficSource(EmulatedNetworkReceiverInterface* target,
Artem Titov4cd433e2019-04-01 11:01:16 +0200115 rtc::IPAddress receiver_ip,
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200116 CrossTrafficConfig config);
117 void Process(Timestamp at_time, TimeDelta delta);
118
Artem Titov40f51152019-01-04 15:45:01 +0100119 EmulatedNetworkReceiverInterface* const target_;
Artem Titov4cd433e2019-04-01 11:01:16 +0200120 const rtc::SocketAddress receiver_address_;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200121 CrossTrafficConfig config_;
122 webrtc::Random random_;
123
124 TimeDelta time_since_update_ = TimeDelta::Zero();
125 double intensity_ = 0;
126 DataSize pending_size_ = DataSize::Zero();
127};
128} // namespace test
129} // namespace webrtc
130#endif // TEST_SCENARIO_NETWORK_NODE_H_