call_client.h 6.7 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_CALL_CLIENT_H_
  11. #define TEST_SCENARIO_CALL_CLIENT_H_
  12. #include <map>
  13. #include <memory>
  14. #include <string>
  15. #include <utility>
  16. #include <vector>
  17. #include "api/rtc_event_log/rtc_event_log.h"
  18. #include "api/test/time_controller.h"
  19. #include "call/call.h"
  20. #include "modules/audio_device/include/test_audio_device.h"
  21. #include "modules/congestion_controller/goog_cc/test/goog_cc_printer.h"
  22. #include "rtc_base/constructor_magic.h"
  23. #include "rtc_base/task_queue_for_test.h"
  24. #include "test/logging/log_writer.h"
  25. #include "test/network/network_emulation.h"
  26. #include "test/rtp_header_parser.h"
  27. #include "test/scenario/column_printer.h"
  28. #include "test/scenario/network_node.h"
  29. #include "test/scenario/scenario_config.h"
  30. namespace webrtc {
  31. namespace test {
  32. // Helper class to capture network controller state.
  33. class NetworkControleUpdateCache : public NetworkControllerInterface {
  34. public:
  35. explicit NetworkControleUpdateCache(
  36. std::unique_ptr<NetworkControllerInterface> controller);
  37. NetworkControlUpdate OnNetworkAvailability(NetworkAvailability msg) override;
  38. NetworkControlUpdate OnNetworkRouteChange(NetworkRouteChange msg) override;
  39. NetworkControlUpdate OnProcessInterval(ProcessInterval msg) override;
  40. NetworkControlUpdate OnRemoteBitrateReport(RemoteBitrateReport msg) override;
  41. NetworkControlUpdate OnRoundTripTimeUpdate(RoundTripTimeUpdate msg) override;
  42. NetworkControlUpdate OnSentPacket(SentPacket msg) override;
  43. NetworkControlUpdate OnReceivedPacket(ReceivedPacket msg) override;
  44. NetworkControlUpdate OnStreamsConfig(StreamsConfig msg) override;
  45. NetworkControlUpdate OnTargetRateConstraints(
  46. TargetRateConstraints msg) override;
  47. NetworkControlUpdate OnTransportLossReport(TransportLossReport msg) override;
  48. NetworkControlUpdate OnTransportPacketsFeedback(
  49. TransportPacketsFeedback msg) override;
  50. NetworkControlUpdate OnNetworkStateEstimate(
  51. NetworkStateEstimate msg) override;
  52. NetworkControlUpdate update_state() const;
  53. private:
  54. NetworkControlUpdate Update(NetworkControlUpdate update);
  55. const std::unique_ptr<NetworkControllerInterface> controller_;
  56. NetworkControlUpdate update_state_;
  57. };
  58. class LoggingNetworkControllerFactory
  59. : public NetworkControllerFactoryInterface {
  60. public:
  61. LoggingNetworkControllerFactory(LogWriterFactoryInterface* log_writer_factory,
  62. TransportControllerConfig config);
  63. RTC_DISALLOW_COPY_AND_ASSIGN(LoggingNetworkControllerFactory);
  64. ~LoggingNetworkControllerFactory();
  65. std::unique_ptr<NetworkControllerInterface> Create(
  66. NetworkControllerConfig config) override;
  67. TimeDelta GetProcessInterval() const override;
  68. // TODO(srte): Consider using the Columnprinter interface for this.
  69. void LogCongestionControllerStats(Timestamp at_time);
  70. NetworkControlUpdate GetUpdate() const;
  71. private:
  72. GoogCcDebugFactory goog_cc_factory_;
  73. NetworkControllerFactoryInterface* cc_factory_ = nullptr;
  74. bool print_cc_state_ = false;
  75. NetworkControleUpdateCache* last_controller_ = nullptr;
  76. };
  77. struct CallClientFakeAudio {
  78. rtc::scoped_refptr<AudioProcessing> apm;
  79. rtc::scoped_refptr<TestAudioDeviceModule> fake_audio_device;
  80. rtc::scoped_refptr<AudioState> audio_state;
  81. };
  82. // CallClient represents a participant in a call scenario. It is created by the
  83. // Scenario class and is used as sender and receiver when setting up a media
  84. // stream session.
  85. class CallClient : public EmulatedNetworkReceiverInterface {
  86. public:
  87. CallClient(TimeController* time_controller,
  88. std::unique_ptr<LogWriterFactoryInterface> log_writer_factory,
  89. CallClientConfig config);
  90. RTC_DISALLOW_COPY_AND_ASSIGN(CallClient);
  91. ~CallClient();
  92. ColumnPrinter StatsPrinter();
  93. Call::Stats GetStats();
  94. DataRate send_bandwidth() {
  95. return DataRate::BitsPerSec(GetStats().send_bandwidth_bps);
  96. }
  97. DataRate target_rate() const;
  98. DataRate stable_target_rate() const;
  99. DataRate padding_rate() const;
  100. void OnPacketReceived(EmulatedIpPacket packet) override;
  101. std::unique_ptr<RtcEventLogOutput> GetLogWriter(std::string name);
  102. // Exposed publicly so that tests can execute tasks such as querying stats
  103. // for media streams in the expected runtime environment (essentially what
  104. // CallClient does internally for GetStats()).
  105. void SendTask(std::function<void()> task);
  106. private:
  107. friend class Scenario;
  108. friend class CallClientPair;
  109. friend class SendVideoStream;
  110. friend class VideoStreamPair;
  111. friend class ReceiveVideoStream;
  112. friend class SendAudioStream;
  113. friend class ReceiveAudioStream;
  114. friend class AudioStreamPair;
  115. friend class NetworkNodeTransport;
  116. uint32_t GetNextVideoSsrc();
  117. uint32_t GetNextVideoLocalSsrc();
  118. uint32_t GetNextAudioSsrc();
  119. uint32_t GetNextAudioLocalSsrc();
  120. uint32_t GetNextRtxSsrc();
  121. void AddExtensions(std::vector<RtpExtension> extensions);
  122. int16_t Bind(EmulatedEndpoint* endpoint);
  123. void UnBind();
  124. TimeController* const time_controller_;
  125. Clock* clock_;
  126. const std::unique_ptr<LogWriterFactoryInterface> log_writer_factory_;
  127. std::unique_ptr<RtcEventLog> event_log_;
  128. LoggingNetworkControllerFactory network_controller_factory_;
  129. CallClientFakeAudio fake_audio_setup_;
  130. std::unique_ptr<Call> call_;
  131. std::unique_ptr<NetworkNodeTransport> transport_;
  132. std::unique_ptr<RtpHeaderParser> const header_parser_;
  133. std::vector<std::pair<EmulatedEndpoint*, uint16_t>> endpoints_;
  134. int next_video_ssrc_index_ = 0;
  135. int next_video_local_ssrc_index_ = 0;
  136. int next_rtx_ssrc_index_ = 0;
  137. int next_audio_ssrc_index_ = 0;
  138. int next_audio_local_ssrc_index_ = 0;
  139. std::map<uint32_t, MediaType> ssrc_media_types_;
  140. // Defined last so it's destroyed first.
  141. TaskQueueForTest task_queue_;
  142. rtc::scoped_refptr<SharedModuleThread> module_thread_;
  143. const FieldTrialBasedConfig field_trials_;
  144. };
  145. class CallClientPair {
  146. public:
  147. RTC_DISALLOW_COPY_AND_ASSIGN(CallClientPair);
  148. ~CallClientPair();
  149. CallClient* first() { return first_; }
  150. CallClient* second() { return second_; }
  151. std::pair<CallClient*, CallClient*> forward() { return {first(), second()}; }
  152. std::pair<CallClient*, CallClient*> reverse() { return {second(), first()}; }
  153. private:
  154. friend class Scenario;
  155. CallClientPair(CallClient* first, CallClient* second)
  156. : first_(first), second_(second) {}
  157. CallClient* const first_;
  158. CallClient* const second_;
  159. };
  160. } // namespace test
  161. } // namespace webrtc
  162. #endif // TEST_SCENARIO_CALL_CLIENT_H_