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" |
| 23 | #include "rtc_base/constructormagic.h" |
| 24 | #include "rtc_base/copyonwritebuffer.h" |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 25 | #include "test/scenario/column_printer.h" |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame^] | 26 | #include "test/scenario/network/network_emulation.h" |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 27 | #include "test/scenario/scenario_config.h" |
| 28 | |
| 29 | namespace webrtc { |
| 30 | namespace test { |
| 31 | |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame^] | 32 | class NullReceiver : public EmulatedNetworkReceiverInterface { |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 33 | public: |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame^] | 34 | void OnPacketReceived(EmulatedIpPacket packet) override; |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 35 | }; |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame^] | 36 | class ActionReceiver : public EmulatedNetworkReceiverInterface { |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 37 | public: |
| 38 | explicit ActionReceiver(std::function<void()> action); |
| 39 | virtual ~ActionReceiver() = default; |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame^] | 40 | |
| 41 | void OnPacketReceived(EmulatedIpPacket packet) override; |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 42 | |
| 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 Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame^] | 49 | class NetworkNode : public EmulatedNetworkReceiverInterface { |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 50 | public: |
| 51 | ~NetworkNode() override; |
| 52 | RTC_DISALLOW_COPY_AND_ASSIGN(NetworkNode); |
| 53 | |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame^] | 54 | void OnPacketReceived(EmulatedIpPacket packet) override; |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 55 | // Creates a route for the given receiver_id over all the given nodes to the |
| 56 | // given receiver. |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame^] | 57 | static void Route(uint64_t receiver_id, |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 58 | std::vector<NetworkNode*> nodes, |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame^] | 59 | EmulatedNetworkReceiverInterface* receiver); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 60 | |
| 61 | protected: |
| 62 | friend class Scenario; |
| 63 | friend class AudioStreamPair; |
| 64 | friend class VideoStreamPair; |
| 65 | |
| 66 | NetworkNode(NetworkNodeConfig config, |
Artem Titov | 8ea1e9d | 2018-10-04 14:46:31 +0200 | [diff] [blame] | 67 | std::unique_ptr<NetworkBehaviorInterface> simulation); |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame^] | 68 | static void ClearRoute(uint64_t receiver_id, std::vector<NetworkNode*> nodes); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 69 | void Process(Timestamp at_time); |
| 70 | |
| 71 | private: |
| 72 | struct StoredPacket { |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame^] | 73 | EmulatedIpPacket packet; |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 74 | uint64_t id; |
| 75 | bool removed; |
| 76 | }; |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame^] | 77 | void SetRoute(uint64_t receiver, EmulatedNetworkReceiverInterface* node); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 78 | void ClearRoute(uint64_t receiver_id); |
| 79 | rtc::CriticalSection crit_sect_; |
| 80 | size_t packet_overhead_ RTC_GUARDED_BY(crit_sect_); |
Artem Titov | 8ea1e9d | 2018-10-04 14:46:31 +0200 | [diff] [blame] | 81 | const std::unique_ptr<NetworkBehaviorInterface> behavior_ |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 82 | RTC_GUARDED_BY(crit_sect_); |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame^] | 83 | std::map<uint64_t, EmulatedNetworkReceiverInterface*> routing_ |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 84 | 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. |
| 91 | class 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 Titov | 8ea1e9d | 2018-10-04 14:46:31 +0200 | [diff] [blame] | 101 | std::unique_ptr<NetworkBehaviorInterface> behavior, |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 102 | SimulatedNetwork* simulation); |
| 103 | static std::unique_ptr<SimulationNode> Create(NetworkNodeConfig config); |
| 104 | SimulatedNetwork* const simulated_network_; |
| 105 | NetworkNodeConfig config_; |
| 106 | }; |
| 107 | |
| 108 | class NetworkNodeTransport : public Transport { |
| 109 | public: |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 110 | NetworkNodeTransport(const Clock* sender_clock, Call* sender_call); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 111 | ~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 Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 117 | |
| 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 Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 126 | |
| 127 | private: |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 128 | 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 Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | // CrossTrafficSource is created by a Scenario and generates cross traffic. It |
| 137 | // provides methods to access and print internal state. |
| 138 | class CrossTrafficSource { |
| 139 | public: |
| 140 | DataRate TrafficRate() const; |
| 141 | ColumnPrinter StatsPrinter(); |
| 142 | ~CrossTrafficSource(); |
| 143 | |
| 144 | private: |
| 145 | friend class Scenario; |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame^] | 146 | CrossTrafficSource(EmulatedNetworkReceiverInterface* target, |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 147 | uint64_t receiver_id, |
| 148 | CrossTrafficConfig config); |
| 149 | void Process(Timestamp at_time, TimeDelta delta); |
| 150 | |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame^] | 151 | EmulatedNetworkReceiverInterface* const target_; |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 152 | 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_ |