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" |
| 26 | #include "test/scenario/scenario_config.h" |
| 27 | |
| 28 | namespace webrtc { |
| 29 | namespace test { |
| 30 | |
| 31 | class NetworkReceiverInterface { |
| 32 | public: |
Sebastian Jansson | f65309c | 2018-12-20 10:26:00 +0100 | [diff] [blame] | 33 | virtual void DeliverPacket(rtc::CopyOnWriteBuffer packet, |
| 34 | uint64_t receiver, |
| 35 | Timestamp at_time) = 0; |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 36 | virtual ~NetworkReceiverInterface() = default; |
| 37 | }; |
| 38 | class NullReceiver : public NetworkReceiverInterface { |
| 39 | public: |
Sebastian Jansson | f65309c | 2018-12-20 10:26:00 +0100 | [diff] [blame] | 40 | void DeliverPacket(rtc::CopyOnWriteBuffer packet, |
| 41 | uint64_t receiver, |
| 42 | Timestamp at_time) override; |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 43 | }; |
| 44 | class ActionReceiver : public NetworkReceiverInterface { |
| 45 | public: |
| 46 | explicit ActionReceiver(std::function<void()> action); |
| 47 | virtual ~ActionReceiver() = default; |
Sebastian Jansson | f65309c | 2018-12-20 10:26:00 +0100 | [diff] [blame] | 48 | void DeliverPacket(rtc::CopyOnWriteBuffer packet, |
| 49 | uint64_t receiver, |
| 50 | Timestamp at_time) override; |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 51 | |
| 52 | private: |
| 53 | std::function<void()> action_; |
| 54 | }; |
| 55 | |
| 56 | // NetworkNode represents one link in a simulated network. It is created by a |
| 57 | // scenario and can be used when setting up audio and video stream sessions. |
| 58 | class NetworkNode : public NetworkReceiverInterface { |
| 59 | public: |
| 60 | ~NetworkNode() override; |
| 61 | RTC_DISALLOW_COPY_AND_ASSIGN(NetworkNode); |
| 62 | |
Sebastian Jansson | f65309c | 2018-12-20 10:26:00 +0100 | [diff] [blame] | 63 | void DeliverPacket(rtc::CopyOnWriteBuffer packet, |
| 64 | uint64_t receiver, |
| 65 | Timestamp at_time) override; |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 66 | // Creates a route for the given receiver_id over all the given nodes to the |
| 67 | // given receiver. |
| 68 | static void Route(int64_t receiver_id, |
| 69 | std::vector<NetworkNode*> nodes, |
| 70 | NetworkReceiverInterface* receiver); |
| 71 | |
| 72 | protected: |
| 73 | friend class Scenario; |
| 74 | friend class AudioStreamPair; |
| 75 | friend class VideoStreamPair; |
| 76 | |
| 77 | NetworkNode(NetworkNodeConfig config, |
Artem Titov | 8ea1e9d | 2018-10-04 14:46:31 +0200 | [diff] [blame] | 78 | std::unique_ptr<NetworkBehaviorInterface> simulation); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 79 | static void ClearRoute(int64_t receiver_id, std::vector<NetworkNode*> nodes); |
| 80 | void Process(Timestamp at_time); |
| 81 | |
| 82 | private: |
| 83 | struct StoredPacket { |
| 84 | rtc::CopyOnWriteBuffer packet_data; |
| 85 | uint64_t receiver_id; |
| 86 | uint64_t id; |
| 87 | bool removed; |
| 88 | }; |
| 89 | void SetRoute(uint64_t receiver, NetworkReceiverInterface* node); |
| 90 | void ClearRoute(uint64_t receiver_id); |
| 91 | rtc::CriticalSection crit_sect_; |
| 92 | size_t packet_overhead_ RTC_GUARDED_BY(crit_sect_); |
Artem Titov | 8ea1e9d | 2018-10-04 14:46:31 +0200 | [diff] [blame] | 93 | const std::unique_ptr<NetworkBehaviorInterface> behavior_ |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 94 | RTC_GUARDED_BY(crit_sect_); |
| 95 | std::map<uint64_t, NetworkReceiverInterface*> routing_ |
| 96 | RTC_GUARDED_BY(crit_sect_); |
| 97 | std::deque<StoredPacket> packets_ RTC_GUARDED_BY(crit_sect_); |
| 98 | |
| 99 | uint64_t next_packet_id_ RTC_GUARDED_BY(crit_sect_) = 1; |
| 100 | }; |
| 101 | // SimulationNode is a NetworkNode that expose an interface for changing run |
| 102 | // time behavior of the underlying simulation. |
| 103 | class SimulationNode : public NetworkNode { |
| 104 | public: |
| 105 | void UpdateConfig(std::function<void(NetworkNodeConfig*)> modifier); |
| 106 | void PauseTransmissionUntil(Timestamp until); |
| 107 | ColumnPrinter ConfigPrinter() const; |
| 108 | |
| 109 | private: |
| 110 | friend class Scenario; |
| 111 | |
| 112 | SimulationNode(NetworkNodeConfig config, |
Artem Titov | 8ea1e9d | 2018-10-04 14:46:31 +0200 | [diff] [blame] | 113 | std::unique_ptr<NetworkBehaviorInterface> behavior, |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 114 | SimulatedNetwork* simulation); |
| 115 | static std::unique_ptr<SimulationNode> Create(NetworkNodeConfig config); |
| 116 | SimulatedNetwork* const simulated_network_; |
| 117 | NetworkNodeConfig config_; |
| 118 | }; |
| 119 | |
| 120 | class NetworkNodeTransport : public Transport { |
| 121 | public: |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 122 | NetworkNodeTransport(const Clock* sender_clock, Call* sender_call); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 123 | ~NetworkNodeTransport() override; |
| 124 | |
| 125 | bool SendRtp(const uint8_t* packet, |
| 126 | size_t length, |
| 127 | const PacketOptions& options) override; |
| 128 | bool SendRtcp(const uint8_t* packet, size_t length) override; |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 129 | |
| 130 | void Connect(NetworkNode* send_node, |
| 131 | uint64_t receiver_id, |
| 132 | DataSize packet_overhead); |
| 133 | |
| 134 | DataSize packet_overhead() { |
| 135 | rtc::CritScope crit(&crit_sect_); |
| 136 | return packet_overhead_; |
| 137 | } |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 138 | |
| 139 | private: |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 140 | rtc::CriticalSection crit_sect_; |
| 141 | const Clock* const sender_clock_; |
| 142 | Call* const sender_call_; |
| 143 | NetworkNode* send_net_ RTC_GUARDED_BY(crit_sect_) = nullptr; |
| 144 | uint64_t receiver_id_ RTC_GUARDED_BY(crit_sect_) = 0; |
| 145 | DataSize packet_overhead_ RTC_GUARDED_BY(crit_sect_) = DataSize::Zero(); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | // CrossTrafficSource is created by a Scenario and generates cross traffic. It |
| 149 | // provides methods to access and print internal state. |
| 150 | class CrossTrafficSource { |
| 151 | public: |
| 152 | DataRate TrafficRate() const; |
| 153 | ColumnPrinter StatsPrinter(); |
| 154 | ~CrossTrafficSource(); |
| 155 | |
| 156 | private: |
| 157 | friend class Scenario; |
| 158 | CrossTrafficSource(NetworkReceiverInterface* target, |
| 159 | uint64_t receiver_id, |
| 160 | CrossTrafficConfig config); |
| 161 | void Process(Timestamp at_time, TimeDelta delta); |
| 162 | |
| 163 | NetworkReceiverInterface* const target_; |
| 164 | const uint64_t receiver_id_; |
| 165 | CrossTrafficConfig config_; |
| 166 | webrtc::Random random_; |
| 167 | |
| 168 | TimeDelta time_since_update_ = TimeDelta::Zero(); |
| 169 | double intensity_ = 0; |
| 170 | DataSize pending_size_ = DataSize::Zero(); |
| 171 | }; |
| 172 | } // namespace test |
| 173 | } // namespace webrtc |
| 174 | #endif // TEST_SCENARIO_NETWORK_NODE_H_ |