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