blob: 5ee017c806a1001af34bcf322f90d416597ad236 [file] [log] [blame]
Sebastian Jansson98b07e92018-09-27 13:47:01 +02001/*
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 Jansson71a091e2018-09-27 19:08:21 +020024#include "test/scenario/simulated_time.h"
Sebastian Jansson98b07e92018-09-27 13:47:01 +020025#include "test/scenario/video_stream.h"
26
27namespace webrtc {
28namespace test {
29// RepeatedActivity is created by the Scenario class and can be used to stop a
30// running activity at runtime.
31class 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
48struct 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.
61class 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 Titov8ea1e9d2018-10-04 14:46:31 +020074 std::unique_ptr<NetworkBehaviorInterface> behavior);
Sebastian Jansson98b07e92018-09-27 13:47:01 +020075
76 CallClient* CreateClient(std::string name, CallClientConfig config);
77 CallClient* CreateClient(
78 std::string name,
79 std::function<void(CallClientConfig*)> config_modifier);
80
Sebastian Jansson800e1212018-10-22 11:49:03 +020081 CallClientPair* CreateRoutes(CallClient* first,
82 std::vector<NetworkNode*> send_link,
83 CallClient* second,
84 std::vector<NetworkNode*> return_link);
85
86 CallClientPair* CreateRoutes(CallClient* first,
87 std::vector<NetworkNode*> send_link,
88 DataSize first_overhead,
89 CallClient* second,
90 std::vector<NetworkNode*> return_link,
91 DataSize second_overhead);
92
93 void ChangeRoute(std::pair<CallClient*, CallClient*> clients,
94 std::vector<NetworkNode*> over_nodes);
95
96 void ChangeRoute(std::pair<CallClient*, CallClient*> clients,
97 std::vector<NetworkNode*> over_nodes,
98 DataSize overhead);
99
Sebastian Jansson71a091e2018-09-27 19:08:21 +0200100 SimulatedTimeClient* CreateSimulatedTimeClient(
101 std::string name,
102 SimulatedTimeClientConfig config,
103 std::vector<PacketStreamConfig> stream_configs,
104 std::vector<NetworkNode*> send_link,
105 std::vector<NetworkNode*> return_link);
106
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200107 VideoStreamPair* CreateVideoStream(
Sebastian Jansson800e1212018-10-22 11:49:03 +0200108 std::pair<CallClient*, CallClient*> clients,
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200109 std::function<void(VideoStreamConfig*)> config_modifier);
Sebastian Jansson800e1212018-10-22 11:49:03 +0200110 VideoStreamPair* CreateVideoStream(
111 std::pair<CallClient*, CallClient*> clients,
112 VideoStreamConfig config);
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200113
114 AudioStreamPair* CreateAudioStream(
Sebastian Jansson800e1212018-10-22 11:49:03 +0200115 std::pair<CallClient*, CallClient*> clients,
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200116 std::function<void(AudioStreamConfig*)> config_modifier);
Sebastian Jansson800e1212018-10-22 11:49:03 +0200117 AudioStreamPair* CreateAudioStream(
118 std::pair<CallClient*, CallClient*> clients,
119 AudioStreamConfig config);
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200120
121 CrossTrafficSource* CreateCrossTraffic(
122 std::vector<NetworkNode*> over_nodes,
123 std::function<void(CrossTrafficConfig*)> config_modifier);
124 CrossTrafficSource* CreateCrossTraffic(std::vector<NetworkNode*> over_nodes,
125 CrossTrafficConfig config);
126
127 // Runs the provided function with a fixed interval.
128 RepeatedActivity* Every(TimeDelta interval,
129 std::function<void(TimeDelta)> function);
130 RepeatedActivity* Every(TimeDelta interval, std::function<void()> function);
131
132 // Runs the provided function after given duration has passed in a session.
133 void At(TimeDelta offset, std::function<void()> function);
134
135 // Sends a packet over the nodes and runs |action| when it has been delivered.
136 void NetworkDelayedAction(std::vector<NetworkNode*> over_nodes,
137 size_t packet_size,
138 std::function<void()> action);
139
140 // Runs the scenario for the given time or until the exit function returns
141 // true.
142 void RunFor(TimeDelta duration);
143 void RunUntil(TimeDelta max_duration,
144 TimeDelta probe_interval,
145 std::function<bool()> exit_function);
146
147 // Triggers sending of dummy packets over the given nodes.
148 void TriggerPacketBurst(std::vector<NetworkNode*> over_nodes,
149 size_t num_packets,
150 size_t packet_size);
151
152 ColumnPrinter TimePrinter();
153 StatesPrinter* CreatePrinter(std::string name,
154 TimeDelta interval,
155 std::vector<ColumnPrinter> printers);
156
157 // Returns the current time.
158 Timestamp Now();
159 // Return the duration of the current session so far.
160 TimeDelta Duration();
161
162 std::string GetFullPathOrEmpty(std::string name) const {
163 if (base_filename_.empty() || name.empty())
164 return std::string();
165 return base_filename_ + "." + name;
166 }
167
168 private:
169 NullReceiver null_receiver_;
170 std::string base_filename_;
171 const bool real_time_mode_;
172 SimulatedClock sim_clock_;
173 Clock* clock_;
174 // Event logs use a global clock instance, this is used to override that
175 // instance when not running in real time.
176 rtc::FakeClock event_log_fake_clock_;
177
178 std::vector<std::unique_ptr<CallClient>> clients_;
Sebastian Jansson800e1212018-10-22 11:49:03 +0200179 std::vector<std::unique_ptr<CallClientPair>> client_pairs_;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200180 std::vector<std::unique_ptr<NetworkNode>> network_nodes_;
181 std::vector<std::unique_ptr<CrossTrafficSource>> cross_traffic_sources_;
182 std::vector<std::unique_ptr<VideoStreamPair>> video_streams_;
183 std::vector<std::unique_ptr<AudioStreamPair>> audio_streams_;
184
Sebastian Jansson71a091e2018-09-27 19:08:21 +0200185 std::vector<std::unique_ptr<SimulatedTimeClient>> simulated_time_clients_;
186
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200187 std::vector<std::unique_ptr<RepeatedActivity>> repeated_activities_;
188 std::vector<std::unique_ptr<ActionReceiver>> action_receivers_;
189 std::vector<std::unique_ptr<PendingActivity>> pending_activities_;
190 std::vector<std::unique_ptr<StatesPrinter>> printers_;
191
Sebastian Jansson800e1212018-10-22 11:49:03 +0200192 int64_t next_route_id_ = 40000;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200193 rtc::scoped_refptr<AudioDecoderFactory> audio_decoder_factory_;
194 rtc::scoped_refptr<AudioEncoderFactory> audio_encoder_factory_;
195
196 Timestamp start_time_ = Timestamp::PlusInfinity();
197};
198} // namespace test
199} // namespace webrtc
200
201#endif // TEST_SCENARIO_SCENARIO_H_