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