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 | |
Bjorn A Mellem | c4f8654 | 2019-11-21 10:37:18 -0800 | [diff] [blame] | 17 | #include "api/test/time_controller.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 18 | #include "rtc_base/fake_clock.h" |
Sebastian Jansson | 105a10a | 2019-04-01 09:18:14 +0200 | [diff] [blame] | 19 | #include "rtc_base/task_queue.h" |
| 20 | #include "rtc_base/task_utils/repeating_task.h" |
Sebastian Jansson | 3d4d94a | 2020-01-14 14:25:41 +0100 | [diff] [blame] | 21 | #include "test/gtest.h" |
Sebastian Jansson | 52de8b0 | 2019-01-16 17:25:44 +0100 | [diff] [blame] | 22 | #include "test/logging/log_writer.h" |
Artem Titov | 386802e | 2019-07-05 10:48:17 +0200 | [diff] [blame] | 23 | #include "test/network/network_emulation_manager.h" |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 24 | #include "test/scenario/audio_stream.h" |
| 25 | #include "test/scenario/call_client.h" |
| 26 | #include "test/scenario/column_printer.h" |
| 27 | #include "test/scenario/network_node.h" |
| 28 | #include "test/scenario/scenario_config.h" |
| 29 | #include "test/scenario/video_stream.h" |
| 30 | |
| 31 | namespace webrtc { |
| 32 | namespace test { |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 33 | // Scenario is a class owning everything for a test scenario. It creates and |
| 34 | // holds network nodes, call clients and media streams. It also provides methods |
| 35 | // for changing behavior at runtime. Since it always keeps ownership of the |
| 36 | // created components, it generally returns non-owning pointers. It maintains |
| 37 | // the life of its objects until it is destroyed. |
| 38 | // For methods accepting configuration structs, a modifier function interface is |
| 39 | // generally provided. This allows simple partial overriding of the default |
| 40 | // configuration. |
| 41 | class Scenario { |
| 42 | public: |
| 43 | Scenario(); |
Sebastian Jansson | 3d4d94a | 2020-01-14 14:25:41 +0100 | [diff] [blame] | 44 | explicit Scenario(const testing::TestInfo* test_info); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 45 | explicit Scenario(std::string file_name); |
| 46 | Scenario(std::string file_name, bool real_time); |
Sebastian Jansson | 52de8b0 | 2019-01-16 17:25:44 +0100 | [diff] [blame] | 47 | Scenario(std::unique_ptr<LogWriterFactoryInterface> log_writer_manager, |
| 48 | bool real_time); |
Artem Titov | 6cae2d5 | 2022-01-26 15:01:10 +0000 | [diff] [blame] | 49 | |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 50 | ~Scenario(); |
Artem Titov | 6cae2d5 | 2022-01-26 15:01:10 +0000 | [diff] [blame] | 51 | |
| 52 | Scenario(const Scenario&) = delete; |
| 53 | Scenario& operator=(const Scenario&) = delete; |
| 54 | |
Sebastian Jansson | a4c22b9 | 2019-04-15 21:10:00 +0200 | [diff] [blame] | 55 | NetworkEmulationManagerImpl* net() { return &network_manager_; } |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 56 | |
Sebastian Jansson | ef86d14 | 2019-04-15 14:42:42 +0200 | [diff] [blame] | 57 | EmulatedNetworkNode* CreateSimulationNode(NetworkSimulationConfig config); |
| 58 | EmulatedNetworkNode* CreateSimulationNode( |
| 59 | std::function<void(NetworkSimulationConfig*)> config_modifier); |
| 60 | |
| 61 | SimulationNode* CreateMutableSimulationNode(NetworkSimulationConfig config); |
| 62 | SimulationNode* CreateMutableSimulationNode( |
| 63 | std::function<void(NetworkSimulationConfig*)> config_modifier); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 64 | |
| 65 | CallClient* CreateClient(std::string name, CallClientConfig config); |
| 66 | CallClient* CreateClient( |
| 67 | std::string name, |
| 68 | std::function<void(CallClientConfig*)> config_modifier); |
| 69 | |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 70 | CallClientPair* CreateRoutes(CallClient* first, |
Artem Titov | 37d1848 | 2019-01-08 15:41:45 +0100 | [diff] [blame] | 71 | std::vector<EmulatedNetworkNode*> send_link, |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 72 | CallClient* second, |
Artem Titov | 37d1848 | 2019-01-08 15:41:45 +0100 | [diff] [blame] | 73 | std::vector<EmulatedNetworkNode*> return_link); |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 74 | |
| 75 | CallClientPair* CreateRoutes(CallClient* first, |
Artem Titov | 37d1848 | 2019-01-08 15:41:45 +0100 | [diff] [blame] | 76 | std::vector<EmulatedNetworkNode*> send_link, |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 77 | DataSize first_overhead, |
| 78 | CallClient* second, |
Artem Titov | 37d1848 | 2019-01-08 15:41:45 +0100 | [diff] [blame] | 79 | std::vector<EmulatedNetworkNode*> return_link, |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 80 | DataSize second_overhead); |
| 81 | |
| 82 | void ChangeRoute(std::pair<CallClient*, CallClient*> clients, |
Artem Titov | 37d1848 | 2019-01-08 15:41:45 +0100 | [diff] [blame] | 83 | std::vector<EmulatedNetworkNode*> over_nodes); |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 84 | |
| 85 | void ChangeRoute(std::pair<CallClient*, CallClient*> clients, |
Artem Titov | 37d1848 | 2019-01-08 15:41:45 +0100 | [diff] [blame] | 86 | std::vector<EmulatedNetworkNode*> over_nodes, |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 87 | DataSize overhead); |
| 88 | |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 89 | VideoStreamPair* CreateVideoStream( |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 90 | std::pair<CallClient*, CallClient*> clients, |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 91 | std::function<void(VideoStreamConfig*)> config_modifier); |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 92 | VideoStreamPair* CreateVideoStream( |
| 93 | std::pair<CallClient*, CallClient*> clients, |
| 94 | VideoStreamConfig config); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 95 | |
| 96 | AudioStreamPair* CreateAudioStream( |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 97 | std::pair<CallClient*, CallClient*> clients, |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 98 | std::function<void(AudioStreamConfig*)> config_modifier); |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 99 | AudioStreamPair* CreateAudioStream( |
| 100 | std::pair<CallClient*, CallClient*> clients, |
| 101 | AudioStreamConfig config); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 102 | |
Sebastian Jansson | 105a10a | 2019-04-01 09:18:14 +0200 | [diff] [blame] | 103 | // Runs the provided function with a fixed interval. For real time tests, |
Artem Titov | 1ee563d | 2021-07-27 12:46:29 +0200 | [diff] [blame] | 104 | // `function` starts being called after `interval` from the call to Every(). |
Sebastian Jansson | 123f345 | 2019-03-13 11:22:52 +0100 | [diff] [blame] | 105 | void Every(TimeDelta interval, std::function<void(TimeDelta)> function); |
| 106 | void Every(TimeDelta interval, std::function<void()> function); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 107 | |
Sebastian Jansson | 3d4d94a | 2020-01-14 14:25:41 +0100 | [diff] [blame] | 108 | // Runs the provided function on the internal task queue. This ensure that |
| 109 | // it's run on the main thread for simulated time tests. |
| 110 | void Post(std::function<void()> function); |
| 111 | |
Sebastian Jansson | 105a10a | 2019-04-01 09:18:14 +0200 | [diff] [blame] | 112 | // Runs the provided function after given duration has passed. For real time |
Artem Titov | 1ee563d | 2021-07-27 12:46:29 +0200 | [diff] [blame] | 113 | // tests, `function` is called after `target_time_since_start` from the call |
Sebastian Jansson | 105a10a | 2019-04-01 09:18:14 +0200 | [diff] [blame] | 114 | // to Every(). |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 115 | void At(TimeDelta offset, std::function<void()> function); |
| 116 | |
Artem Titov | 1ee563d | 2021-07-27 12:46:29 +0200 | [diff] [blame] | 117 | // Sends a packet over the nodes and runs `action` when it has been delivered. |
Artem Titov | 37d1848 | 2019-01-08 15:41:45 +0100 | [diff] [blame] | 118 | void NetworkDelayedAction(std::vector<EmulatedNetworkNode*> over_nodes, |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 119 | size_t packet_size, |
| 120 | std::function<void()> action); |
| 121 | |
Sebastian Jansson | 105a10a | 2019-04-01 09:18:14 +0200 | [diff] [blame] | 122 | // Runs the scenario for the given time. |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 123 | void RunFor(TimeDelta duration); |
Artem Titov | 1ee563d | 2021-07-27 12:46:29 +0200 | [diff] [blame] | 124 | // Runs the scenario until `target_time_since_start`. |
Sebastian Jansson | 123f345 | 2019-03-13 11:22:52 +0100 | [diff] [blame] | 125 | void RunUntil(TimeDelta target_time_since_start); |
Artem Titov | 1ee563d | 2021-07-27 12:46:29 +0200 | [diff] [blame] | 126 | // Runs the scenario until `target_time_since_start` or `exit_function` |
| 127 | // returns true. `exit_function` is polled after each `check_interval` has |
Sebastian Jansson | 105a10a | 2019-04-01 09:18:14 +0200 | [diff] [blame] | 128 | // passed. |
Sebastian Jansson | 123f345 | 2019-03-13 11:22:52 +0100 | [diff] [blame] | 129 | void RunUntil(TimeDelta target_time_since_start, |
| 130 | TimeDelta check_interval, |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 131 | std::function<bool()> exit_function); |
Sebastian Jansson | 49a7843 | 2018-11-20 16:15:29 +0100 | [diff] [blame] | 132 | void Start(); |
| 133 | void Stop(); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 134 | |
| 135 | // Triggers sending of dummy packets over the given nodes. |
Artem Titov | 37d1848 | 2019-01-08 15:41:45 +0100 | [diff] [blame] | 136 | void TriggerPacketBurst(std::vector<EmulatedNetworkNode*> over_nodes, |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 137 | size_t num_packets, |
| 138 | size_t packet_size); |
| 139 | |
| 140 | ColumnPrinter TimePrinter(); |
| 141 | StatesPrinter* CreatePrinter(std::string name, |
| 142 | TimeDelta interval, |
| 143 | std::vector<ColumnPrinter> printers); |
| 144 | |
| 145 | // Returns the current time. |
| 146 | Timestamp Now(); |
| 147 | // Return the duration of the current session so far. |
Sebastian Jansson | 123f345 | 2019-03-13 11:22:52 +0100 | [diff] [blame] | 148 | TimeDelta TimeSinceStart(); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 149 | |
Sebastian Jansson | 52de8b0 | 2019-01-16 17:25:44 +0100 | [diff] [blame] | 150 | std::unique_ptr<RtcEventLogOutput> GetLogWriter(std::string name) { |
| 151 | if (!log_writer_factory_ || name.empty()) |
| 152 | return nullptr; |
| 153 | return log_writer_factory_->Create(name); |
| 154 | } |
| 155 | std::unique_ptr<LogWriterFactoryInterface> GetLogWriterFactory( |
| 156 | std::string name) { |
| 157 | if (!log_writer_factory_ || name.empty()) |
| 158 | return nullptr; |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 159 | return std::make_unique<LogWriterFactoryAddPrefix>( |
Sebastian Jansson | 52de8b0 | 2019-01-16 17:25:44 +0100 | [diff] [blame] | 160 | log_writer_factory_.get(), name); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | private: |
Sebastian Jansson | 105a10a | 2019-04-01 09:18:14 +0200 | [diff] [blame] | 164 | TimeDelta TimeUntilTarget(TimeDelta target_time_offset); |
| 165 | |
Sebastian Jansson | 105a10a | 2019-04-01 09:18:14 +0200 | [diff] [blame] | 166 | const std::unique_ptr<LogWriterFactoryInterface> log_writer_factory_; |
Sebastian Jansson | 77bd385 | 2020-01-17 13:05:54 +0100 | [diff] [blame] | 167 | NetworkEmulationManagerImpl network_manager_; |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 168 | Clock* clock_; |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 169 | |
| 170 | std::vector<std::unique_ptr<CallClient>> clients_; |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 171 | std::vector<std::unique_ptr<CallClientPair>> client_pairs_; |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 172 | std::vector<std::unique_ptr<VideoStreamPair>> video_streams_; |
| 173 | std::vector<std::unique_ptr<AudioStreamPair>> audio_streams_; |
Sebastian Jansson | a4c22b9 | 2019-04-15 21:10:00 +0200 | [diff] [blame] | 174 | std::vector<std::unique_ptr<SimulationNode>> simulation_nodes_; |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 175 | std::vector<std::unique_ptr<StatesPrinter>> printers_; |
| 176 | |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 177 | rtc::scoped_refptr<AudioDecoderFactory> audio_decoder_factory_; |
| 178 | rtc::scoped_refptr<AudioEncoderFactory> audio_encoder_factory_; |
| 179 | |
| 180 | Timestamp start_time_ = Timestamp::PlusInfinity(); |
Sebastian Jansson | 105a10a | 2019-04-01 09:18:14 +0200 | [diff] [blame] | 181 | // Defined last so it's destroyed first. |
| 182 | rtc::TaskQueue task_queue_; |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 183 | }; |
| 184 | } // namespace test |
| 185 | } // namespace webrtc |
| 186 | |
| 187 | #endif // TEST_SCENARIO_SCENARIO_H_ |