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_SCENARIO_H_ |
| 11 | #define TEST_SCENARIO_SCENARIO_H_ |
| 12 | #include <memory> |
| 13 | #include <string> |
| 14 | #include <utility> |
| 15 | #include <vector> |
| 16 | |
| 17 | #include "rtc_base/constructormagic.h" |
| 18 | #include "rtc_base/fakeclock.h" |
| 19 | #include "test/scenario/audio_stream.h" |
| 20 | #include "test/scenario/call_client.h" |
| 21 | #include "test/scenario/column_printer.h" |
| 22 | #include "test/scenario/network_node.h" |
| 23 | #include "test/scenario/scenario_config.h" |
Sebastian Jansson | 71a091e | 2018-09-27 19:08:21 +0200 | [diff] [blame] | 24 | #include "test/scenario/simulated_time.h" |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 25 | #include "test/scenario/video_stream.h" |
| 26 | |
| 27 | namespace webrtc { |
| 28 | namespace test { |
| 29 | // RepeatedActivity is created by the Scenario class and can be used to stop a |
| 30 | // running activity at runtime. |
| 31 | class RepeatedActivity { |
| 32 | public: |
| 33 | void Stop(); |
| 34 | |
| 35 | private: |
| 36 | friend class Scenario; |
| 37 | RepeatedActivity(TimeDelta interval, std::function<void(TimeDelta)> function); |
| 38 | |
| 39 | void Poll(Timestamp time); |
| 40 | void SetStartTime(Timestamp time); |
| 41 | Timestamp NextTime(); |
| 42 | |
| 43 | TimeDelta interval_; |
| 44 | std::function<void(TimeDelta)> function_; |
| 45 | Timestamp last_update_ = Timestamp::MinusInfinity(); |
| 46 | }; |
| 47 | |
| 48 | struct PendingActivity { |
| 49 | TimeDelta after_duration; |
| 50 | std::function<void()> function; |
| 51 | }; |
| 52 | |
| 53 | // Scenario is a class owning everything for a test scenario. It creates and |
| 54 | // holds network nodes, call clients and media streams. It also provides methods |
| 55 | // for changing behavior at runtime. Since it always keeps ownership of the |
| 56 | // created components, it generally returns non-owning pointers. It maintains |
| 57 | // the life of its objects until it is destroyed. |
| 58 | // For methods accepting configuration structs, a modifier function interface is |
| 59 | // generally provided. This allows simple partial overriding of the default |
| 60 | // configuration. |
| 61 | class Scenario { |
| 62 | public: |
| 63 | Scenario(); |
| 64 | explicit Scenario(std::string file_name); |
| 65 | Scenario(std::string file_name, bool real_time); |
| 66 | RTC_DISALLOW_COPY_AND_ASSIGN(Scenario); |
| 67 | ~Scenario(); |
| 68 | |
| 69 | SimulationNode* CreateSimulationNode(NetworkNodeConfig config); |
| 70 | SimulationNode* CreateSimulationNode( |
| 71 | std::function<void(NetworkNodeConfig*)> config_modifier); |
| 72 | NetworkNode* CreateNetworkNode( |
| 73 | NetworkNodeConfig config, |
Artem Titov | 8ea1e9d | 2018-10-04 14:46:31 +0200 | [diff] [blame] | 74 | std::unique_ptr<NetworkBehaviorInterface> behavior); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 75 | |
| 76 | CallClient* CreateClient(std::string name, CallClientConfig config); |
| 77 | CallClient* CreateClient( |
| 78 | std::string name, |
| 79 | std::function<void(CallClientConfig*)> config_modifier); |
| 80 | |
Sebastian Jansson | 71a091e | 2018-09-27 19:08:21 +0200 | [diff] [blame] | 81 | SimulatedTimeClient* CreateSimulatedTimeClient( |
| 82 | std::string name, |
| 83 | SimulatedTimeClientConfig config, |
| 84 | std::vector<PacketStreamConfig> stream_configs, |
| 85 | std::vector<NetworkNode*> send_link, |
| 86 | std::vector<NetworkNode*> return_link); |
| 87 | |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 88 | VideoStreamPair* CreateVideoStream( |
| 89 | CallClient* sender, |
| 90 | std::vector<NetworkNode*> send_link, |
| 91 | CallClient* receiver, |
| 92 | std::vector<NetworkNode*> return_link, |
| 93 | std::function<void(VideoStreamConfig*)> config_modifier); |
| 94 | VideoStreamPair* CreateVideoStream(CallClient* sender, |
| 95 | std::vector<NetworkNode*> send_link, |
| 96 | CallClient* receiver, |
| 97 | std::vector<NetworkNode*> return_link, |
| 98 | VideoStreamConfig config); |
| 99 | |
| 100 | AudioStreamPair* CreateAudioStream( |
| 101 | CallClient* sender, |
| 102 | std::vector<NetworkNode*> send_link, |
| 103 | CallClient* receiver, |
| 104 | std::vector<NetworkNode*> return_link, |
| 105 | std::function<void(AudioStreamConfig*)> config_modifier); |
| 106 | AudioStreamPair* CreateAudioStream(CallClient* sender, |
| 107 | std::vector<NetworkNode*> send_link, |
| 108 | CallClient* receiver, |
| 109 | std::vector<NetworkNode*> return_link, |
| 110 | AudioStreamConfig config); |
| 111 | |
| 112 | CrossTrafficSource* CreateCrossTraffic( |
| 113 | std::vector<NetworkNode*> over_nodes, |
| 114 | std::function<void(CrossTrafficConfig*)> config_modifier); |
| 115 | CrossTrafficSource* CreateCrossTraffic(std::vector<NetworkNode*> over_nodes, |
| 116 | CrossTrafficConfig config); |
| 117 | |
| 118 | // Runs the provided function with a fixed interval. |
| 119 | RepeatedActivity* Every(TimeDelta interval, |
| 120 | std::function<void(TimeDelta)> function); |
| 121 | RepeatedActivity* Every(TimeDelta interval, std::function<void()> function); |
| 122 | |
| 123 | // Runs the provided function after given duration has passed in a session. |
| 124 | void At(TimeDelta offset, std::function<void()> function); |
| 125 | |
| 126 | // Sends a packet over the nodes and runs |action| when it has been delivered. |
| 127 | void NetworkDelayedAction(std::vector<NetworkNode*> over_nodes, |
| 128 | size_t packet_size, |
| 129 | std::function<void()> action); |
| 130 | |
| 131 | // Runs the scenario for the given time or until the exit function returns |
| 132 | // true. |
| 133 | void RunFor(TimeDelta duration); |
| 134 | void RunUntil(TimeDelta max_duration, |
| 135 | TimeDelta probe_interval, |
| 136 | std::function<bool()> exit_function); |
| 137 | |
| 138 | // Triggers sending of dummy packets over the given nodes. |
| 139 | void TriggerPacketBurst(std::vector<NetworkNode*> over_nodes, |
| 140 | size_t num_packets, |
| 141 | size_t packet_size); |
| 142 | |
| 143 | ColumnPrinter TimePrinter(); |
| 144 | StatesPrinter* CreatePrinter(std::string name, |
| 145 | TimeDelta interval, |
| 146 | std::vector<ColumnPrinter> printers); |
| 147 | |
| 148 | // Returns the current time. |
| 149 | Timestamp Now(); |
| 150 | // Return the duration of the current session so far. |
| 151 | TimeDelta Duration(); |
| 152 | |
| 153 | std::string GetFullPathOrEmpty(std::string name) const { |
| 154 | if (base_filename_.empty() || name.empty()) |
| 155 | return std::string(); |
| 156 | return base_filename_ + "." + name; |
| 157 | } |
| 158 | |
| 159 | private: |
| 160 | NullReceiver null_receiver_; |
| 161 | std::string base_filename_; |
| 162 | const bool real_time_mode_; |
| 163 | SimulatedClock sim_clock_; |
| 164 | Clock* clock_; |
| 165 | // Event logs use a global clock instance, this is used to override that |
| 166 | // instance when not running in real time. |
| 167 | rtc::FakeClock event_log_fake_clock_; |
| 168 | |
| 169 | std::vector<std::unique_ptr<CallClient>> clients_; |
| 170 | std::vector<std::unique_ptr<NetworkNode>> network_nodes_; |
| 171 | std::vector<std::unique_ptr<CrossTrafficSource>> cross_traffic_sources_; |
| 172 | std::vector<std::unique_ptr<VideoStreamPair>> video_streams_; |
| 173 | std::vector<std::unique_ptr<AudioStreamPair>> audio_streams_; |
| 174 | |
Sebastian Jansson | 71a091e | 2018-09-27 19:08:21 +0200 | [diff] [blame] | 175 | std::vector<std::unique_ptr<SimulatedTimeClient>> simulated_time_clients_; |
| 176 | |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 177 | std::vector<std::unique_ptr<RepeatedActivity>> repeated_activities_; |
| 178 | std::vector<std::unique_ptr<ActionReceiver>> action_receivers_; |
| 179 | std::vector<std::unique_ptr<PendingActivity>> pending_activities_; |
| 180 | std::vector<std::unique_ptr<StatesPrinter>> printers_; |
| 181 | |
| 182 | int64_t next_receiver_id_ = 40000; |
| 183 | rtc::scoped_refptr<AudioDecoderFactory> audio_decoder_factory_; |
| 184 | rtc::scoped_refptr<AudioEncoderFactory> audio_encoder_factory_; |
| 185 | |
| 186 | Timestamp start_time_ = Timestamp::PlusInfinity(); |
| 187 | }; |
| 188 | } // namespace test |
| 189 | } // namespace webrtc |
| 190 | |
| 191 | #endif // TEST_SCENARIO_SCENARIO_H_ |