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 | |
Artem Titov | 37d1848 | 2019-01-08 15:41:45 +0100 | [diff] [blame^] | 47 | // SimulationNode is a EmulatedNetworkNode that expose an interface for changing |
| 48 | // run time behavior of the underlying simulation. |
| 49 | class SimulationNode : public EmulatedNetworkNode { |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 50 | 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 Titov | 8ea1e9d | 2018-10-04 14:46:31 +0200 | [diff] [blame] | 59 | std::unique_ptr<NetworkBehaviorInterface> behavior, |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 60 | SimulatedNetwork* simulation); |
| 61 | static std::unique_ptr<SimulationNode> Create(NetworkNodeConfig config); |
Artem Titov | 37d1848 | 2019-01-08 15:41:45 +0100 | [diff] [blame^] | 62 | |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 63 | SimulatedNetwork* const simulated_network_; |
| 64 | NetworkNodeConfig config_; |
| 65 | }; |
| 66 | |
| 67 | class NetworkNodeTransport : public Transport { |
| 68 | public: |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 69 | NetworkNodeTransport(const Clock* sender_clock, Call* sender_call); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 70 | ~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 Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 76 | |
Artem Titov | 37d1848 | 2019-01-08 15:41:45 +0100 | [diff] [blame^] | 77 | void Connect(EmulatedNetworkNode* send_node, |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 78 | uint64_t receiver_id, |
| 79 | DataSize packet_overhead); |
| 80 | |
| 81 | DataSize packet_overhead() { |
| 82 | rtc::CritScope crit(&crit_sect_); |
| 83 | return packet_overhead_; |
| 84 | } |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 85 | |
| 86 | private: |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 87 | rtc::CriticalSection crit_sect_; |
| 88 | const Clock* const sender_clock_; |
| 89 | Call* const sender_call_; |
Artem Titov | 37d1848 | 2019-01-08 15:41:45 +0100 | [diff] [blame^] | 90 | EmulatedNetworkNode* send_net_ RTC_GUARDED_BY(crit_sect_) = nullptr; |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 91 | uint64_t receiver_id_ RTC_GUARDED_BY(crit_sect_) = 0; |
| 92 | DataSize packet_overhead_ RTC_GUARDED_BY(crit_sect_) = DataSize::Zero(); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | // CrossTrafficSource is created by a Scenario and generates cross traffic. It |
| 96 | // provides methods to access and print internal state. |
| 97 | class CrossTrafficSource { |
| 98 | public: |
| 99 | DataRate TrafficRate() const; |
| 100 | ColumnPrinter StatsPrinter(); |
| 101 | ~CrossTrafficSource(); |
| 102 | |
| 103 | private: |
| 104 | friend class Scenario; |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame] | 105 | CrossTrafficSource(EmulatedNetworkReceiverInterface* target, |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 106 | uint64_t receiver_id, |
| 107 | CrossTrafficConfig config); |
| 108 | void Process(Timestamp at_time, TimeDelta delta); |
| 109 | |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame] | 110 | EmulatedNetworkReceiverInterface* const target_; |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 111 | const uint64_t receiver_id_; |
| 112 | CrossTrafficConfig config_; |
| 113 | webrtc::Random random_; |
| 114 | |
| 115 | TimeDelta time_since_update_ = TimeDelta::Zero(); |
| 116 | double intensity_ = 0; |
| 117 | DataSize pending_size_ = DataSize::Zero(); |
| 118 | }; |
| 119 | } // namespace test |
| 120 | } // namespace webrtc |
| 121 | #endif // TEST_SCENARIO_NETWORK_NODE_H_ |