Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 1 | /* |
| 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 Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 21 | #include "call/call.h" |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 22 | #include "call/simulated_network.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 23 | #include "rtc_base/constructor_magic.h" |
| 24 | #include "rtc_base/copy_on_write_buffer.h" |
Sebastian Jansson | 4124dab | 2019-04-01 14:33:53 +0200 | [diff] [blame] | 25 | #include "rtc_base/task_queue.h" |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 26 | #include "test/scenario/column_printer.h" |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame] | 27 | #include "test/scenario/network/network_emulation.h" |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 28 | #include "test/scenario/scenario_config.h" |
| 29 | |
| 30 | namespace webrtc { |
| 31 | namespace test { |
| 32 | |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame] | 33 | class NullReceiver : public EmulatedNetworkReceiverInterface { |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 34 | public: |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame] | 35 | void OnPacketReceived(EmulatedIpPacket packet) override; |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 36 | }; |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame] | 37 | class ActionReceiver : public EmulatedNetworkReceiverInterface { |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 38 | public: |
| 39 | explicit ActionReceiver(std::function<void()> action); |
| 40 | virtual ~ActionReceiver() = default; |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame] | 41 | |
| 42 | void OnPacketReceived(EmulatedIpPacket packet) override; |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 43 | |
| 44 | private: |
| 45 | std::function<void()> action_; |
| 46 | }; |
| 47 | |
Artem Titov | 37d1848 | 2019-01-08 15:41:45 +0100 | [diff] [blame] | 48 | class SimulationNode : public EmulatedNetworkNode { |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 49 | public: |
Sebastian Jansson | ef86d14 | 2019-04-15 14:42:42 +0200 | [diff] [blame] | 50 | void UpdateConfig(std::function<void(NetworkSimulationConfig*)> modifier); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 51 | void PauseTransmissionUntil(Timestamp until); |
| 52 | ColumnPrinter ConfigPrinter() const; |
Sebastian Jansson | ef86d14 | 2019-04-15 14:42:42 +0200 | [diff] [blame] | 53 | EmulatedNetworkNode* node() { return this; } |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 54 | |
| 55 | private: |
| 56 | friend class Scenario; |
| 57 | |
Sebastian Jansson | 4124dab | 2019-04-01 14:33:53 +0200 | [diff] [blame] | 58 | SimulationNode(Clock* clock, |
| 59 | rtc::TaskQueue* task_queue, |
Sebastian Jansson | ef86d14 | 2019-04-15 14:42:42 +0200 | [diff] [blame] | 60 | NetworkSimulationConfig config, |
Artem Titov | 8ea1e9d | 2018-10-04 14:46:31 +0200 | [diff] [blame] | 61 | std::unique_ptr<NetworkBehaviorInterface> behavior, |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 62 | SimulatedNetwork* simulation); |
Sebastian Jansson | 4124dab | 2019-04-01 14:33:53 +0200 | [diff] [blame] | 63 | static std::unique_ptr<SimulationNode> Create(Clock* clock, |
| 64 | rtc::TaskQueue* task_queue, |
Sebastian Jansson | ef86d14 | 2019-04-15 14:42:42 +0200 | [diff] [blame] | 65 | NetworkSimulationConfig config); |
Artem Titov | 37d1848 | 2019-01-08 15:41:45 +0100 | [diff] [blame] | 66 | |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 67 | SimulatedNetwork* const simulated_network_; |
Sebastian Jansson | ef86d14 | 2019-04-15 14:42:42 +0200 | [diff] [blame] | 68 | NetworkSimulationConfig config_; |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | class NetworkNodeTransport : public Transport { |
| 72 | public: |
Sebastian Jansson | aa01f27 | 2019-01-30 11:28:59 +0100 | [diff] [blame] | 73 | NetworkNodeTransport(Clock* sender_clock, Call* sender_call); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 74 | ~NetworkNodeTransport() override; |
| 75 | |
| 76 | bool SendRtp(const uint8_t* packet, |
| 77 | size_t length, |
| 78 | const PacketOptions& options) override; |
| 79 | bool SendRtcp(const uint8_t* packet, size_t length) override; |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 80 | |
Artem Titov | 37d1848 | 2019-01-08 15:41:45 +0100 | [diff] [blame] | 81 | void Connect(EmulatedNetworkNode* send_node, |
Artem Titov | 4cd433e | 2019-04-01 11:01:16 +0200 | [diff] [blame] | 82 | rtc::IPAddress receiver_ip, |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 83 | DataSize packet_overhead); |
Sebastian Jansson | 4124dab | 2019-04-01 14:33:53 +0200 | [diff] [blame] | 84 | void Disconnect(); |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 85 | |
| 86 | DataSize packet_overhead() { |
| 87 | rtc::CritScope crit(&crit_sect_); |
| 88 | return packet_overhead_; |
| 89 | } |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 90 | |
| 91 | private: |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 92 | rtc::CriticalSection crit_sect_; |
Sebastian Jansson | aa01f27 | 2019-01-30 11:28:59 +0100 | [diff] [blame] | 93 | Clock* const sender_clock_; |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 94 | Call* const sender_call_; |
Artem Titov | 4cd433e | 2019-04-01 11:01:16 +0200 | [diff] [blame] | 95 | // Store local address here for consistency with receiver address. |
| 96 | const rtc::SocketAddress local_address_; |
Artem Titov | 37d1848 | 2019-01-08 15:41:45 +0100 | [diff] [blame] | 97 | EmulatedNetworkNode* send_net_ RTC_GUARDED_BY(crit_sect_) = nullptr; |
Artem Titov | 4cd433e | 2019-04-01 11:01:16 +0200 | [diff] [blame] | 98 | rtc::SocketAddress receiver_address_ RTC_GUARDED_BY(crit_sect_); |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 99 | DataSize packet_overhead_ RTC_GUARDED_BY(crit_sect_) = DataSize::Zero(); |
Sebastian Jansson | 4124dab | 2019-04-01 14:33:53 +0200 | [diff] [blame] | 100 | rtc::NetworkRoute current_network_route_ RTC_GUARDED_BY(crit_sect_); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | // CrossTrafficSource is created by a Scenario and generates cross traffic. It |
| 104 | // provides methods to access and print internal state. |
| 105 | class CrossTrafficSource { |
| 106 | public: |
| 107 | DataRate TrafficRate() const; |
| 108 | ColumnPrinter StatsPrinter(); |
| 109 | ~CrossTrafficSource(); |
| 110 | |
| 111 | private: |
| 112 | friend class Scenario; |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame] | 113 | CrossTrafficSource(EmulatedNetworkReceiverInterface* target, |
Artem Titov | 4cd433e | 2019-04-01 11:01:16 +0200 | [diff] [blame] | 114 | rtc::IPAddress receiver_ip, |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 115 | CrossTrafficConfig config); |
| 116 | void Process(Timestamp at_time, TimeDelta delta); |
| 117 | |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame] | 118 | EmulatedNetworkReceiverInterface* const target_; |
Artem Titov | 4cd433e | 2019-04-01 11:01:16 +0200 | [diff] [blame] | 119 | const rtc::SocketAddress receiver_address_; |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 120 | CrossTrafficConfig config_; |
| 121 | webrtc::Random random_; |
| 122 | |
| 123 | TimeDelta time_since_update_ = TimeDelta::Zero(); |
| 124 | double intensity_ = 0; |
| 125 | DataSize pending_size_ = DataSize::Zero(); |
| 126 | }; |
| 127 | } // namespace test |
| 128 | } // namespace webrtc |
| 129 | #endif // TEST_SCENARIO_NETWORK_NODE_H_ |