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