1 /* 2 * Copyright (c) 2014 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_CALL_TEST_H_ 11 #define TEST_CALL_TEST_H_ 12 13 #include <map> 14 #include <memory> 15 #include <string> 16 #include <vector> 17 18 #include "absl/types/optional.h" 19 #include "api/rtc_event_log/rtc_event_log.h" 20 #include "api/task_queue/task_queue_base.h" 21 #include "api/task_queue/task_queue_factory.h" 22 #include "api/test/video/function_video_decoder_factory.h" 23 #include "api/test/video/function_video_encoder_factory.h" 24 #include "api/units/time_delta.h" 25 #include "api/video/video_bitrate_allocator_factory.h" 26 #include "call/call.h" 27 #include "modules/audio_device/include/test_audio_device.h" 28 #include "test/encoder_settings.h" 29 #include "test/fake_decoder.h" 30 #include "test/fake_videorenderer.h" 31 #include "test/fake_vp8_encoder.h" 32 #include "test/frame_generator_capturer.h" 33 #include "test/rtp_rtcp_observer.h" 34 #include "test/run_loop.h" 35 #include "test/scoped_key_value_config.h" 36 37 namespace webrtc { 38 namespace test { 39 40 class BaseTest; 41 42 class CallTest : public ::testing::Test, public RtpPacketSinkInterface { 43 public: 44 CallTest(); 45 virtual ~CallTest(); 46 47 static constexpr size_t kNumSsrcs = 6; 48 static const int kNumSimulcastStreams = 3; 49 static const int kDefaultWidth = 320; 50 static const int kDefaultHeight = 180; 51 static const int kDefaultFramerate = 30; 52 static constexpr TimeDelta kDefaultTimeout = TimeDelta::Seconds(30); 53 static constexpr TimeDelta kLongTimeout = TimeDelta::Seconds(120); 54 enum classPayloadTypes : uint8_t { 55 kSendRtxPayloadType = 98, 56 kRtxRedPayloadType = 99, 57 kVideoSendPayloadType = 100, 58 kAudioSendPayloadType = 103, 59 kRedPayloadType = 118, 60 kUlpfecPayloadType = 119, 61 kFlexfecPayloadType = 120, 62 kPayloadTypeH264 = 122, 63 kPayloadTypeVP8 = 123, 64 kPayloadTypeVP9 = 124, 65 kPayloadTypeGeneric = 125, 66 kFakeVideoSendPayloadType = 126, 67 }; 68 static const uint32_t kSendRtxSsrcs[kNumSsrcs]; 69 static const uint32_t kVideoSendSsrcs[kNumSsrcs]; 70 static const uint32_t kAudioSendSsrc; 71 static const uint32_t kFlexfecSendSsrc; 72 static const uint32_t kReceiverLocalVideoSsrc; 73 static const uint32_t kReceiverLocalAudioSsrc; 74 static const int kNackRtpHistoryMs; 75 static const std::map<uint8_t, MediaType> payload_type_map_; 76 77 protected: 78 void RegisterRtpExtension(const RtpExtension& extension); 79 80 // RunBaseTest overwrites the audio_state of the send and receive Call configs 81 // to simplify test code. 82 void RunBaseTest(BaseTest* test); 83 84 void CreateCalls(); 85 void CreateCalls(const Call::Config& sender_config, 86 const Call::Config& receiver_config); 87 void CreateSenderCall(); 88 void CreateSenderCall(const Call::Config& config); 89 void CreateReceiverCall(const Call::Config& config); 90 void DestroyCalls(); 91 92 void CreateVideoSendConfig(VideoSendStream::Config* video_config, 93 size_t num_video_streams, 94 size_t num_used_ssrcs, 95 Transport* send_transport); 96 void CreateAudioAndFecSendConfigs(size_t num_audio_streams, 97 size_t num_flexfec_streams, 98 Transport* send_transport); 99 void SetAudioConfig(const AudioSendStream::Config& config); 100 101 void SetSendFecConfig(std::vector<uint32_t> video_send_ssrcs); 102 void SetSendUlpFecConfig(VideoSendStream::Config* send_config); 103 void SetReceiveUlpFecConfig( 104 VideoReceiveStreamInterface::Config* receive_config); 105 void CreateSendConfig(size_t num_video_streams, 106 size_t num_audio_streams, 107 size_t num_flexfec_streams, 108 Transport* send_transport); 109 110 void CreateMatchingVideoReceiveConfigs( 111 const VideoSendStream::Config& video_send_config, 112 Transport* rtcp_send_transport); 113 void CreateMatchingVideoReceiveConfigs( 114 const VideoSendStream::Config& video_send_config, 115 Transport* rtcp_send_transport, 116 bool send_side_bwe, 117 VideoDecoderFactory* decoder_factory, 118 absl::optional<size_t> decode_sub_stream, 119 bool receiver_reference_time_report, 120 int rtp_history_ms); 121 void AddMatchingVideoReceiveConfigs( 122 std::vector<VideoReceiveStreamInterface::Config>* receive_configs, 123 const VideoSendStream::Config& video_send_config, 124 Transport* rtcp_send_transport, 125 bool send_side_bwe, 126 VideoDecoderFactory* decoder_factory, 127 absl::optional<size_t> decode_sub_stream, 128 bool receiver_reference_time_report, 129 int rtp_history_ms); 130 131 void CreateMatchingAudioAndFecConfigs(Transport* rtcp_send_transport); 132 void CreateMatchingAudioConfigs(Transport* transport, std::string sync_group); 133 static AudioReceiveStreamInterface::Config CreateMatchingAudioConfig( 134 const AudioSendStream::Config& send_config, 135 rtc::scoped_refptr<AudioDecoderFactory> audio_decoder_factory, 136 Transport* transport, 137 std::string sync_group); 138 void CreateMatchingFecConfig( 139 Transport* transport, 140 const VideoSendStream::Config& video_send_config); 141 void CreateMatchingReceiveConfigs(Transport* rtcp_send_transport); 142 143 void CreateFrameGeneratorCapturerWithDrift(Clock* drift_clock, 144 float speed, 145 int framerate, 146 int width, 147 int height); 148 void CreateFrameGeneratorCapturer(int framerate, int width, int height); 149 void CreateFakeAudioDevices( 150 std::unique_ptr<TestAudioDeviceModule::Capturer> capturer, 151 std::unique_ptr<TestAudioDeviceModule::Renderer> renderer); 152 153 void CreateVideoStreams(); 154 void CreateVideoSendStreams(); 155 void CreateVideoSendStream(const VideoEncoderConfig& encoder_config); 156 void CreateAudioStreams(); 157 void CreateFlexfecStreams(); 158 159 void ConnectVideoSourcesToStreams(); 160 161 void Start(); 162 void StartVideoStreams(); 163 void Stop(); 164 void StopVideoStreams(); 165 void DestroyStreams(); 166 void DestroyVideoSendStreams(); 167 void SetFakeVideoCaptureRotation(VideoRotation rotation); 168 169 void SetVideoDegradation(DegradationPreference preference); 170 171 VideoSendStream::Config* GetVideoSendConfig(); 172 void SetVideoSendConfig(const VideoSendStream::Config& config); 173 VideoEncoderConfig* GetVideoEncoderConfig(); 174 void SetVideoEncoderConfig(const VideoEncoderConfig& config); 175 VideoSendStream* GetVideoSendStream(); 176 FlexfecReceiveStream::Config* GetFlexFecConfig(); task_queue()177 TaskQueueBase* task_queue() { return task_queue_.get(); } 178 179 // RtpPacketSinkInterface implementation. 180 void OnRtpPacket(const RtpPacketReceived& packet) override; 181 182 test::RunLoop loop_; 183 184 Clock* const clock_; 185 test::ScopedKeyValueConfig field_trials_; 186 187 std::unique_ptr<TaskQueueFactory> task_queue_factory_; 188 std::unique_ptr<webrtc::RtcEventLog> send_event_log_; 189 std::unique_ptr<webrtc::RtcEventLog> recv_event_log_; 190 std::unique_ptr<Call> sender_call_; 191 std::unique_ptr<PacketTransport> send_transport_; 192 std::vector<VideoSendStream::Config> video_send_configs_; 193 std::vector<VideoEncoderConfig> video_encoder_configs_; 194 std::vector<VideoSendStream*> video_send_streams_; 195 AudioSendStream::Config audio_send_config_; 196 AudioSendStream* audio_send_stream_; 197 198 std::unique_ptr<Call> receiver_call_; 199 std::unique_ptr<PacketTransport> receive_transport_; 200 std::vector<VideoReceiveStreamInterface::Config> video_receive_configs_; 201 std::vector<VideoReceiveStreamInterface*> video_receive_streams_; 202 std::vector<AudioReceiveStreamInterface::Config> audio_receive_configs_; 203 std::vector<AudioReceiveStreamInterface*> audio_receive_streams_; 204 std::vector<FlexfecReceiveStream::Config> flexfec_receive_configs_; 205 std::vector<FlexfecReceiveStream*> flexfec_receive_streams_; 206 207 test::FrameGeneratorCapturer* frame_generator_capturer_; 208 std::vector<std::unique_ptr<rtc::VideoSourceInterface<VideoFrame>>> 209 video_sources_; 210 DegradationPreference degradation_preference_ = 211 DegradationPreference::MAINTAIN_FRAMERATE; 212 213 std::unique_ptr<FecControllerFactoryInterface> fec_controller_factory_; 214 std::unique_ptr<NetworkStatePredictorFactoryInterface> 215 network_state_predictor_factory_; 216 std::unique_ptr<NetworkControllerFactoryInterface> 217 network_controller_factory_; 218 219 test::FunctionVideoEncoderFactory fake_encoder_factory_; 220 int fake_encoder_max_bitrate_ = -1; 221 test::FunctionVideoDecoderFactory fake_decoder_factory_; 222 std::unique_ptr<VideoBitrateAllocatorFactory> bitrate_allocator_factory_; 223 // Number of simulcast substreams. 224 size_t num_video_streams_; 225 size_t num_audio_streams_; 226 size_t num_flexfec_streams_; 227 rtc::scoped_refptr<AudioDecoderFactory> audio_decoder_factory_; 228 rtc::scoped_refptr<AudioEncoderFactory> audio_encoder_factory_; 229 test::FakeVideoRenderer fake_renderer_; 230 231 232 private: 233 absl::optional<RtpExtension> GetRtpExtensionByUri( 234 const std::string& uri) const; 235 236 void AddRtpExtensionByUri(const std::string& uri, 237 std::vector<RtpExtension>* extensions) const; 238 239 std::unique_ptr<TaskQueueBase, TaskQueueDeleter> task_queue_; 240 std::vector<RtpExtension> rtp_extensions_; 241 rtc::scoped_refptr<AudioProcessing> apm_send_; 242 rtc::scoped_refptr<AudioProcessing> apm_recv_; 243 rtc::scoped_refptr<TestAudioDeviceModule> fake_send_audio_device_; 244 rtc::scoped_refptr<TestAudioDeviceModule> fake_recv_audio_device_; 245 }; 246 247 class BaseTest : public RtpRtcpObserver { 248 public: 249 BaseTest(); 250 explicit BaseTest(TimeDelta timeout); 251 virtual ~BaseTest(); 252 253 virtual void PerformTest() = 0; 254 virtual bool ShouldCreateReceivers() const = 0; 255 256 virtual size_t GetNumVideoStreams() const; 257 virtual size_t GetNumAudioStreams() const; 258 virtual size_t GetNumFlexfecStreams() const; 259 260 virtual std::unique_ptr<TestAudioDeviceModule::Capturer> CreateCapturer(); 261 virtual std::unique_ptr<TestAudioDeviceModule::Renderer> CreateRenderer(); 262 virtual void OnFakeAudioDevicesCreated( 263 TestAudioDeviceModule* send_audio_device, 264 TestAudioDeviceModule* recv_audio_device); 265 266 virtual void ModifySenderBitrateConfig(BitrateConstraints* bitrate_config); 267 virtual void ModifyReceiverBitrateConfig(BitrateConstraints* bitrate_config); 268 269 virtual void OnCallsCreated(Call* sender_call, Call* receiver_call); 270 271 virtual std::unique_ptr<test::PacketTransport> CreateSendTransport( 272 TaskQueueBase* task_queue, 273 Call* sender_call); 274 virtual std::unique_ptr<test::PacketTransport> CreateReceiveTransport( 275 TaskQueueBase* task_queue); 276 277 virtual void ModifyVideoConfigs( 278 VideoSendStream::Config* send_config, 279 std::vector<VideoReceiveStreamInterface::Config>* receive_configs, 280 VideoEncoderConfig* encoder_config); 281 virtual void ModifyVideoCaptureStartResolution(int* width, 282 int* heigt, 283 int* frame_rate); 284 virtual void ModifyVideoDegradationPreference( 285 DegradationPreference* degradation_preference); 286 287 virtual void OnVideoStreamsCreated( 288 VideoSendStream* send_stream, 289 const std::vector<VideoReceiveStreamInterface*>& receive_streams); 290 291 virtual void ModifyAudioConfigs( 292 AudioSendStream::Config* send_config, 293 std::vector<AudioReceiveStreamInterface::Config>* receive_configs); 294 virtual void OnAudioStreamsCreated( 295 AudioSendStream* send_stream, 296 const std::vector<AudioReceiveStreamInterface*>& receive_streams); 297 298 virtual void ModifyFlexfecConfigs( 299 std::vector<FlexfecReceiveStream::Config>* receive_configs); 300 virtual void OnFlexfecStreamsCreated( 301 const std::vector<FlexfecReceiveStream*>& receive_streams); 302 303 virtual void OnFrameGeneratorCapturerCreated( 304 FrameGeneratorCapturer* frame_generator_capturer); 305 306 virtual void OnStreamsStopped(); 307 }; 308 309 class SendTest : public BaseTest { 310 public: 311 explicit SendTest(TimeDelta timeout); 312 313 bool ShouldCreateReceivers() const override; 314 }; 315 316 class EndToEndTest : public BaseTest { 317 public: 318 EndToEndTest(); 319 explicit EndToEndTest(TimeDelta timeout); 320 321 bool ShouldCreateReceivers() const override; 322 }; 323 324 } // namespace test 325 } // namespace webrtc 326 327 #endif // TEST_CALL_TEST_H_ 328