scenario.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. #include "api/test/time_controller.h"
  17. #include "rtc_base/constructor_magic.h"
  18. #include "rtc_base/fake_clock.h"
  19. #include "rtc_base/task_queue.h"
  20. #include "rtc_base/task_utils/repeating_task.h"
  21. #include "test/gtest.h"
  22. #include "test/logging/log_writer.h"
  23. #include "test/network/network_emulation_manager.h"
  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. namespace webrtc {
  31. namespace test {
  32. // Scenario is a class owning everything for a test scenario. It creates and
  33. // holds network nodes, call clients and media streams. It also provides methods
  34. // for changing behavior at runtime. Since it always keeps ownership of the
  35. // created components, it generally returns non-owning pointers. It maintains
  36. // the life of its objects until it is destroyed.
  37. // For methods accepting configuration structs, a modifier function interface is
  38. // generally provided. This allows simple partial overriding of the default
  39. // configuration.
  40. class Scenario {
  41. public:
  42. Scenario();
  43. explicit Scenario(const testing::TestInfo* test_info);
  44. explicit Scenario(std::string file_name);
  45. Scenario(std::string file_name, bool real_time);
  46. Scenario(std::unique_ptr<LogWriterFactoryInterface> log_writer_manager,
  47. bool real_time);
  48. RTC_DISALLOW_COPY_AND_ASSIGN(Scenario);
  49. ~Scenario();
  50. NetworkEmulationManagerImpl* net() { return &network_manager_; }
  51. EmulatedNetworkNode* CreateSimulationNode(NetworkSimulationConfig config);
  52. EmulatedNetworkNode* CreateSimulationNode(
  53. std::function<void(NetworkSimulationConfig*)> config_modifier);
  54. SimulationNode* CreateMutableSimulationNode(NetworkSimulationConfig config);
  55. SimulationNode* CreateMutableSimulationNode(
  56. std::function<void(NetworkSimulationConfig*)> config_modifier);
  57. CallClient* CreateClient(std::string name, CallClientConfig config);
  58. CallClient* CreateClient(
  59. std::string name,
  60. std::function<void(CallClientConfig*)> config_modifier);
  61. CallClientPair* CreateRoutes(CallClient* first,
  62. std::vector<EmulatedNetworkNode*> send_link,
  63. CallClient* second,
  64. std::vector<EmulatedNetworkNode*> return_link);
  65. CallClientPair* CreateRoutes(CallClient* first,
  66. std::vector<EmulatedNetworkNode*> send_link,
  67. DataSize first_overhead,
  68. CallClient* second,
  69. std::vector<EmulatedNetworkNode*> return_link,
  70. DataSize second_overhead);
  71. void ChangeRoute(std::pair<CallClient*, CallClient*> clients,
  72. std::vector<EmulatedNetworkNode*> over_nodes);
  73. void ChangeRoute(std::pair<CallClient*, CallClient*> clients,
  74. std::vector<EmulatedNetworkNode*> over_nodes,
  75. DataSize overhead);
  76. VideoStreamPair* CreateVideoStream(
  77. std::pair<CallClient*, CallClient*> clients,
  78. std::function<void(VideoStreamConfig*)> config_modifier);
  79. VideoStreamPair* CreateVideoStream(
  80. std::pair<CallClient*, CallClient*> clients,
  81. VideoStreamConfig config);
  82. AudioStreamPair* CreateAudioStream(
  83. std::pair<CallClient*, CallClient*> clients,
  84. std::function<void(AudioStreamConfig*)> config_modifier);
  85. AudioStreamPair* CreateAudioStream(
  86. std::pair<CallClient*, CallClient*> clients,
  87. AudioStreamConfig config);
  88. // Runs the provided function with a fixed interval. For real time tests,
  89. // |function| starts being called after |interval| from the call to Every().
  90. void Every(TimeDelta interval, std::function<void(TimeDelta)> function);
  91. void Every(TimeDelta interval, std::function<void()> function);
  92. // Runs the provided function on the internal task queue. This ensure that
  93. // it's run on the main thread for simulated time tests.
  94. void Post(std::function<void()> function);
  95. // Runs the provided function after given duration has passed. For real time
  96. // tests, |function| is called after |target_time_since_start| from the call
  97. // to Every().
  98. void At(TimeDelta offset, std::function<void()> function);
  99. // Sends a packet over the nodes and runs |action| when it has been delivered.
  100. void NetworkDelayedAction(std::vector<EmulatedNetworkNode*> over_nodes,
  101. size_t packet_size,
  102. std::function<void()> action);
  103. // Runs the scenario for the given time.
  104. void RunFor(TimeDelta duration);
  105. // Runs the scenario until |target_time_since_start|.
  106. void RunUntil(TimeDelta target_time_since_start);
  107. // Runs the scenario until |target_time_since_start| or |exit_function|
  108. // returns true. |exit_function| is polled after each |check_interval| has
  109. // passed.
  110. void RunUntil(TimeDelta target_time_since_start,
  111. TimeDelta check_interval,
  112. std::function<bool()> exit_function);
  113. void Start();
  114. void Stop();
  115. // Triggers sending of dummy packets over the given nodes.
  116. void TriggerPacketBurst(std::vector<EmulatedNetworkNode*> over_nodes,
  117. size_t num_packets,
  118. size_t packet_size);
  119. ColumnPrinter TimePrinter();
  120. StatesPrinter* CreatePrinter(std::string name,
  121. TimeDelta interval,
  122. std::vector<ColumnPrinter> printers);
  123. // Returns the current time.
  124. Timestamp Now();
  125. // Return the duration of the current session so far.
  126. TimeDelta TimeSinceStart();
  127. std::unique_ptr<RtcEventLogOutput> GetLogWriter(std::string name) {
  128. if (!log_writer_factory_ || name.empty())
  129. return nullptr;
  130. return log_writer_factory_->Create(name);
  131. }
  132. std::unique_ptr<LogWriterFactoryInterface> GetLogWriterFactory(
  133. std::string name) {
  134. if (!log_writer_factory_ || name.empty())
  135. return nullptr;
  136. return std::make_unique<LogWriterFactoryAddPrefix>(
  137. log_writer_factory_.get(), name);
  138. }
  139. private:
  140. TimeDelta TimeUntilTarget(TimeDelta target_time_offset);
  141. const std::unique_ptr<LogWriterFactoryInterface> log_writer_factory_;
  142. NetworkEmulationManagerImpl network_manager_;
  143. Clock* clock_;
  144. std::vector<std::unique_ptr<CallClient>> clients_;
  145. std::vector<std::unique_ptr<CallClientPair>> client_pairs_;
  146. std::vector<std::unique_ptr<VideoStreamPair>> video_streams_;
  147. std::vector<std::unique_ptr<AudioStreamPair>> audio_streams_;
  148. std::vector<std::unique_ptr<SimulationNode>> simulation_nodes_;
  149. std::vector<std::unique_ptr<StatesPrinter>> printers_;
  150. rtc::scoped_refptr<AudioDecoderFactory> audio_decoder_factory_;
  151. rtc::scoped_refptr<AudioEncoderFactory> audio_encoder_factory_;
  152. Timestamp start_time_ = Timestamp::PlusInfinity();
  153. // Defined last so it's destroyed first.
  154. rtc::TaskQueue task_queue_;
  155. };
  156. } // namespace test
  157. } // namespace webrtc
  158. #endif // TEST_SCENARIO_SCENARIO_H_