1 /*
2  *  Copyright 2017 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 
11 #include "video/video_receive_stream.h"
12 
13 #include <algorithm>
14 #include <memory>
15 #include <utility>
16 #include <vector>
17 
18 #include "api/task_queue/default_task_queue_factory.h"
19 #include "api/test/video/function_video_decoder_factory.h"
20 #include "api/video_codecs/video_decoder.h"
21 #include "call/rtp_stream_receiver_controller.h"
22 #include "common_video/test/utilities.h"
23 #include "media/base/fake_video_renderer.h"
24 #include "modules/pacing/packet_router.h"
25 #include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
26 #include "modules/utility/include/process_thread.h"
27 #include "modules/video_coding/encoded_frame.h"
28 #include "rtc_base/event.h"
29 #include "system_wrappers/include/clock.h"
30 #include "test/fake_decoder.h"
31 #include "test/field_trial.h"
32 #include "test/gmock.h"
33 #include "test/gtest.h"
34 #include "test/time_controller/simulated_time_controller.h"
35 #include "test/video_decoder_proxy_factory.h"
36 #include "video/call_stats.h"
37 
38 namespace webrtc {
39 namespace {
40 
41 using ::testing::_;
42 using ::testing::ElementsAreArray;
43 using ::testing::Invoke;
44 using ::testing::IsEmpty;
45 using ::testing::SizeIs;
46 
47 constexpr int kDefaultTimeOutMs = 50;
48 
49 class MockTransport : public Transport {
50  public:
51   MOCK_METHOD(bool,
52               SendRtp,
53               (const uint8_t*, size_t length, const PacketOptions& options),
54               (override));
55   MOCK_METHOD(bool, SendRtcp, (const uint8_t*, size_t length), (override));
56 };
57 
58 class MockVideoDecoder : public VideoDecoder {
59  public:
60   MOCK_METHOD(int32_t,
61               InitDecode,
62               (const VideoCodec*, int32_t number_of_cores),
63               (override));
64   MOCK_METHOD(int32_t,
65               Decode,
66               (const EncodedImage& input,
67                bool missing_frames,
68                int64_t render_time_ms),
69               (override));
70   MOCK_METHOD(int32_t,
71               RegisterDecodeCompleteCallback,
72               (DecodedImageCallback*),
73               (override));
74   MOCK_METHOD(int32_t, Release, (), (override));
ImplementationName() const75   const char* ImplementationName() const { return "MockVideoDecoder"; }
76 };
77 
78 class FrameObjectFake : public video_coding::EncodedFrame {
79  public:
SetPayloadType(uint8_t payload_type)80   void SetPayloadType(uint8_t payload_type) { _payloadType = payload_type; }
81 
SetRotation(const VideoRotation & rotation)82   void SetRotation(const VideoRotation& rotation) { rotation_ = rotation; }
83 
SetNtpTime(int64_t ntp_time_ms)84   void SetNtpTime(int64_t ntp_time_ms) { ntp_time_ms_ = ntp_time_ms; }
85 
ReceivedTime() const86   int64_t ReceivedTime() const override { return 0; }
87 
RenderTime() const88   int64_t RenderTime() const override { return _renderTimeMs; }
89 };
90 
91 }  // namespace
92 
93 class VideoReceiveStreamTest : public ::testing::Test {
94  public:
VideoReceiveStreamTest()95   VideoReceiveStreamTest()
96       : process_thread_(ProcessThread::Create("TestThread")),
97         task_queue_factory_(CreateDefaultTaskQueueFactory()),
98         config_(&mock_transport_),
99         call_stats_(Clock::GetRealTimeClock(), process_thread_.get()),
100         h264_decoder_factory_(&mock_h264_video_decoder_),
101         null_decoder_factory_(&mock_null_video_decoder_) {}
102 
SetUp()103   void SetUp() {
104     constexpr int kDefaultNumCpuCores = 2;
105     config_.rtp.remote_ssrc = 1111;
106     config_.rtp.local_ssrc = 2222;
107     config_.renderer = &fake_renderer_;
108     VideoReceiveStream::Decoder h264_decoder;
109     h264_decoder.payload_type = 99;
110     h264_decoder.video_format = SdpVideoFormat("H264");
111     h264_decoder.video_format.parameters.insert(
112         {"sprop-parameter-sets", "Z0IACpZTBYmI,aMljiA=="});
113     h264_decoder.decoder_factory = &h264_decoder_factory_;
114     config_.decoders.push_back(h264_decoder);
115     VideoReceiveStream::Decoder null_decoder;
116     null_decoder.payload_type = 98;
117     null_decoder.video_format = SdpVideoFormat("null");
118     null_decoder.decoder_factory = &null_decoder_factory_;
119     config_.decoders.push_back(null_decoder);
120 
121     clock_ = Clock::GetRealTimeClock();
122     timing_ = new VCMTiming(clock_);
123 
124     video_receive_stream_ =
125         std::make_unique<webrtc::internal::VideoReceiveStream>(
126             task_queue_factory_.get(), &rtp_stream_receiver_controller_,
127             kDefaultNumCpuCores, &packet_router_, config_.Copy(),
128             process_thread_.get(), &call_stats_, clock_, timing_);
129   }
130 
131  protected:
132   std::unique_ptr<ProcessThread> process_thread_;
133   const std::unique_ptr<TaskQueueFactory> task_queue_factory_;
134   VideoReceiveStream::Config config_;
135   CallStats call_stats_;
136   MockVideoDecoder mock_h264_video_decoder_;
137   MockVideoDecoder mock_null_video_decoder_;
138   test::VideoDecoderProxyFactory h264_decoder_factory_;
139   test::VideoDecoderProxyFactory null_decoder_factory_;
140   cricket::FakeVideoRenderer fake_renderer_;
141   MockTransport mock_transport_;
142   PacketRouter packet_router_;
143   RtpStreamReceiverController rtp_stream_receiver_controller_;
144   std::unique_ptr<webrtc::internal::VideoReceiveStream> video_receive_stream_;
145   Clock* clock_;
146   VCMTiming* timing_;
147 };
148 
TEST_F(VideoReceiveStreamTest,CreateFrameFromH264FmtpSpropAndIdr)149 TEST_F(VideoReceiveStreamTest, CreateFrameFromH264FmtpSpropAndIdr) {
150   constexpr uint8_t idr_nalu[] = {0x05, 0xFF, 0xFF, 0xFF};
151   RtpPacketToSend rtppacket(nullptr);
152   uint8_t* payload = rtppacket.AllocatePayload(sizeof(idr_nalu));
153   memcpy(payload, idr_nalu, sizeof(idr_nalu));
154   rtppacket.SetMarker(true);
155   rtppacket.SetSsrc(1111);
156   rtppacket.SetPayloadType(99);
157   rtppacket.SetSequenceNumber(1);
158   rtppacket.SetTimestamp(0);
159   rtc::Event init_decode_event_;
160   EXPECT_CALL(mock_h264_video_decoder_, InitDecode(_, _))
161       .WillOnce(Invoke([&init_decode_event_](const VideoCodec* config,
162                                              int32_t number_of_cores) {
163         init_decode_event_.Set();
164         return 0;
165       }));
166   EXPECT_CALL(mock_h264_video_decoder_, RegisterDecodeCompleteCallback(_));
167   video_receive_stream_->Start();
168   EXPECT_CALL(mock_h264_video_decoder_, Decode(_, false, _));
169   RtpPacketReceived parsed_packet;
170   ASSERT_TRUE(parsed_packet.Parse(rtppacket.data(), rtppacket.size()));
171   rtp_stream_receiver_controller_.OnRtpPacket(parsed_packet);
172   EXPECT_CALL(mock_h264_video_decoder_, Release());
173   // Make sure the decoder thread had a chance to run.
174   init_decode_event_.Wait(kDefaultTimeOutMs);
175 }
176 
TEST_F(VideoReceiveStreamTest,PlayoutDelay)177 TEST_F(VideoReceiveStreamTest, PlayoutDelay) {
178   const PlayoutDelay kPlayoutDelayMs = {123, 321};
179   std::unique_ptr<FrameObjectFake> test_frame(new FrameObjectFake());
180   test_frame->id.picture_id = 0;
181   test_frame->SetPlayoutDelay(kPlayoutDelayMs);
182 
183   video_receive_stream_->OnCompleteFrame(std::move(test_frame));
184   EXPECT_EQ(kPlayoutDelayMs.min_ms, timing_->min_playout_delay());
185   EXPECT_EQ(kPlayoutDelayMs.max_ms, timing_->max_playout_delay());
186 
187   // Check that the biggest minimum delay is chosen.
188   video_receive_stream_->SetMinimumPlayoutDelay(400);
189   EXPECT_EQ(400, timing_->min_playout_delay());
190 
191   // Check base minimum delay validation.
192   EXPECT_FALSE(video_receive_stream_->SetBaseMinimumPlayoutDelayMs(12345));
193   EXPECT_FALSE(video_receive_stream_->SetBaseMinimumPlayoutDelayMs(-1));
194   EXPECT_TRUE(video_receive_stream_->SetBaseMinimumPlayoutDelayMs(500));
195   EXPECT_EQ(500, timing_->min_playout_delay());
196 
197   // Check that intermidiate values are remembered and the biggest remembered
198   // is chosen.
199   video_receive_stream_->SetBaseMinimumPlayoutDelayMs(0);
200   EXPECT_EQ(400, timing_->min_playout_delay());
201 
202   video_receive_stream_->SetMinimumPlayoutDelay(0);
203   EXPECT_EQ(123, timing_->min_playout_delay());
204 }
205 
TEST_F(VideoReceiveStreamTest,PlayoutDelayPreservesDefaultMaxValue)206 TEST_F(VideoReceiveStreamTest, PlayoutDelayPreservesDefaultMaxValue) {
207   const int default_max_playout_latency = timing_->max_playout_delay();
208   const PlayoutDelay kPlayoutDelayMs = {123, -1};
209 
210   std::unique_ptr<FrameObjectFake> test_frame(new FrameObjectFake());
211   test_frame->id.picture_id = 0;
212   test_frame->SetPlayoutDelay(kPlayoutDelayMs);
213 
214   video_receive_stream_->OnCompleteFrame(std::move(test_frame));
215 
216   // Ensure that -1 preserves default maximum value from |timing_|.
217   EXPECT_EQ(kPlayoutDelayMs.min_ms, timing_->min_playout_delay());
218   EXPECT_NE(kPlayoutDelayMs.max_ms, timing_->max_playout_delay());
219   EXPECT_EQ(default_max_playout_latency, timing_->max_playout_delay());
220 }
221 
TEST_F(VideoReceiveStreamTest,PlayoutDelayPreservesDefaultMinValue)222 TEST_F(VideoReceiveStreamTest, PlayoutDelayPreservesDefaultMinValue) {
223   const int default_min_playout_latency = timing_->min_playout_delay();
224   const PlayoutDelay kPlayoutDelayMs = {-1, 321};
225 
226   std::unique_ptr<FrameObjectFake> test_frame(new FrameObjectFake());
227   test_frame->id.picture_id = 0;
228   test_frame->SetPlayoutDelay(kPlayoutDelayMs);
229 
230   video_receive_stream_->OnCompleteFrame(std::move(test_frame));
231 
232   // Ensure that -1 preserves default minimum value from |timing_|.
233   EXPECT_NE(kPlayoutDelayMs.min_ms, timing_->min_playout_delay());
234   EXPECT_EQ(kPlayoutDelayMs.max_ms, timing_->max_playout_delay());
235   EXPECT_EQ(default_min_playout_latency, timing_->min_playout_delay());
236 }
237 
238 class VideoReceiveStreamTestWithFakeDecoder : public ::testing::Test {
239  public:
VideoReceiveStreamTestWithFakeDecoder()240   VideoReceiveStreamTestWithFakeDecoder()
241       : fake_decoder_factory_(
242             []() { return std::make_unique<test::FakeDecoder>(); }),
243         process_thread_(ProcessThread::Create("TestThread")),
244         task_queue_factory_(CreateDefaultTaskQueueFactory()),
245         config_(&mock_transport_),
246         call_stats_(Clock::GetRealTimeClock(), process_thread_.get()) {}
247 
SetUp()248   void SetUp() {
249     config_.rtp.remote_ssrc = 1111;
250     config_.rtp.local_ssrc = 2222;
251     config_.renderer = &fake_renderer_;
252     VideoReceiveStream::Decoder fake_decoder;
253     fake_decoder.payload_type = 99;
254     fake_decoder.video_format = SdpVideoFormat("VP8");
255     fake_decoder.decoder_factory = &fake_decoder_factory_;
256     config_.decoders.push_back(fake_decoder);
257     clock_ = Clock::GetRealTimeClock();
258     ReCreateReceiveStream(VideoReceiveStream::RecordingState());
259   }
260 
ReCreateReceiveStream(VideoReceiveStream::RecordingState state)261   void ReCreateReceiveStream(VideoReceiveStream::RecordingState state) {
262     constexpr int kDefaultNumCpuCores = 2;
263     video_receive_stream_ = nullptr;
264     timing_ = new VCMTiming(clock_);
265     video_receive_stream_.reset(new webrtc::internal::VideoReceiveStream(
266         task_queue_factory_.get(), &rtp_stream_receiver_controller_,
267         kDefaultNumCpuCores, &packet_router_, config_.Copy(),
268         process_thread_.get(), &call_stats_, clock_, timing_));
269     video_receive_stream_->SetAndGetRecordingState(std::move(state), false);
270   }
271 
272  protected:
273   test::FunctionVideoDecoderFactory fake_decoder_factory_;
274   std::unique_ptr<ProcessThread> process_thread_;
275   const std::unique_ptr<TaskQueueFactory> task_queue_factory_;
276   VideoReceiveStream::Config config_;
277   CallStats call_stats_;
278   cricket::FakeVideoRenderer fake_renderer_;
279   MockTransport mock_transport_;
280   PacketRouter packet_router_;
281   RtpStreamReceiverController rtp_stream_receiver_controller_;
282   std::unique_ptr<webrtc::internal::VideoReceiveStream> video_receive_stream_;
283   Clock* clock_;
284   VCMTiming* timing_;
285 };
286 
TEST_F(VideoReceiveStreamTestWithFakeDecoder,PassesNtpTime)287 TEST_F(VideoReceiveStreamTestWithFakeDecoder, PassesNtpTime) {
288   const int64_t kNtpTimestamp = 12345;
289   auto test_frame = std::make_unique<FrameObjectFake>();
290   test_frame->SetPayloadType(99);
291   test_frame->id.picture_id = 0;
292   test_frame->SetNtpTime(kNtpTimestamp);
293 
294   video_receive_stream_->Start();
295   video_receive_stream_->OnCompleteFrame(std::move(test_frame));
296   EXPECT_TRUE(fake_renderer_.WaitForRenderedFrame(kDefaultTimeOutMs));
297   EXPECT_EQ(kNtpTimestamp, fake_renderer_.ntp_time_ms());
298 }
299 
TEST_F(VideoReceiveStreamTestWithFakeDecoder,PassesRotation)300 TEST_F(VideoReceiveStreamTestWithFakeDecoder, PassesRotation) {
301   const webrtc::VideoRotation kRotation = webrtc::kVideoRotation_180;
302   auto test_frame = std::make_unique<FrameObjectFake>();
303   test_frame->SetPayloadType(99);
304   test_frame->id.picture_id = 0;
305   test_frame->SetRotation(kRotation);
306 
307   video_receive_stream_->Start();
308   video_receive_stream_->OnCompleteFrame(std::move(test_frame));
309   EXPECT_TRUE(fake_renderer_.WaitForRenderedFrame(kDefaultTimeOutMs));
310 
311   EXPECT_EQ(kRotation, fake_renderer_.rotation());
312 }
313 
TEST_F(VideoReceiveStreamTestWithFakeDecoder,PassesPacketInfos)314 TEST_F(VideoReceiveStreamTestWithFakeDecoder, PassesPacketInfos) {
315   auto test_frame = std::make_unique<FrameObjectFake>();
316   test_frame->SetPayloadType(99);
317   test_frame->id.picture_id = 0;
318   RtpPacketInfos packet_infos = CreatePacketInfos(3);
319   test_frame->SetPacketInfos(packet_infos);
320 
321   video_receive_stream_->Start();
322   video_receive_stream_->OnCompleteFrame(std::move(test_frame));
323   EXPECT_TRUE(fake_renderer_.WaitForRenderedFrame(kDefaultTimeOutMs));
324 
325   EXPECT_THAT(fake_renderer_.packet_infos(), ElementsAreArray(packet_infos));
326 }
327 
TEST_F(VideoReceiveStreamTestWithFakeDecoder,RenderedFrameUpdatesGetSources)328 TEST_F(VideoReceiveStreamTestWithFakeDecoder, RenderedFrameUpdatesGetSources) {
329   constexpr uint32_t kSsrc = 1111;
330   constexpr uint32_t kCsrc = 9001;
331   constexpr uint32_t kRtpTimestamp = 12345;
332 
333   // Prepare one video frame with per-packet information.
334   auto test_frame = std::make_unique<FrameObjectFake>();
335   test_frame->SetPayloadType(99);
336   test_frame->id.picture_id = 0;
337   RtpPacketInfos packet_infos;
338   {
339     RtpPacketInfos::vector_type infos;
340 
341     RtpPacketInfo info;
342     info.set_ssrc(kSsrc);
343     info.set_csrcs({kCsrc});
344     info.set_rtp_timestamp(kRtpTimestamp);
345 
346     info.set_receive_time_ms(clock_->TimeInMilliseconds() - 5000);
347     infos.push_back(info);
348 
349     info.set_receive_time_ms(clock_->TimeInMilliseconds() - 3000);
350     infos.push_back(info);
351 
352     info.set_receive_time_ms(clock_->TimeInMilliseconds() - 2000);
353     infos.push_back(info);
354 
355     info.set_receive_time_ms(clock_->TimeInMilliseconds() - 4000);
356     infos.push_back(info);
357 
358     packet_infos = RtpPacketInfos(std::move(infos));
359   }
360   test_frame->SetPacketInfos(packet_infos);
361 
362   // Start receive stream.
363   video_receive_stream_->Start();
364   EXPECT_THAT(video_receive_stream_->GetSources(), IsEmpty());
365 
366   // Render one video frame.
367   int64_t timestamp_ms_min = clock_->TimeInMilliseconds();
368   video_receive_stream_->OnCompleteFrame(std::move(test_frame));
369   EXPECT_TRUE(fake_renderer_.WaitForRenderedFrame(kDefaultTimeOutMs));
370   int64_t timestamp_ms_max = clock_->TimeInMilliseconds();
371 
372   // Verify that the per-packet information is passed to the renderer.
373   EXPECT_THAT(fake_renderer_.packet_infos(), ElementsAreArray(packet_infos));
374 
375   // Verify that the per-packet information also updates |GetSources()|.
376   std::vector<RtpSource> sources = video_receive_stream_->GetSources();
377   ASSERT_THAT(sources, SizeIs(2));
378   {
379     auto it = std::find_if(sources.begin(), sources.end(),
380                            [](const RtpSource& source) {
381                              return source.source_type() == RtpSourceType::SSRC;
382                            });
383     ASSERT_NE(it, sources.end());
384 
385     EXPECT_EQ(it->source_id(), kSsrc);
386     EXPECT_EQ(it->source_type(), RtpSourceType::SSRC);
387     EXPECT_EQ(it->rtp_timestamp(), kRtpTimestamp);
388     EXPECT_GE(it->timestamp_ms(), timestamp_ms_min);
389     EXPECT_LE(it->timestamp_ms(), timestamp_ms_max);
390   }
391   {
392     auto it = std::find_if(sources.begin(), sources.end(),
393                            [](const RtpSource& source) {
394                              return source.source_type() == RtpSourceType::CSRC;
395                            });
396     ASSERT_NE(it, sources.end());
397 
398     EXPECT_EQ(it->source_id(), kCsrc);
399     EXPECT_EQ(it->source_type(), RtpSourceType::CSRC);
400     EXPECT_EQ(it->rtp_timestamp(), kRtpTimestamp);
401     EXPECT_GE(it->timestamp_ms(), timestamp_ms_min);
402     EXPECT_LE(it->timestamp_ms(), timestamp_ms_max);
403   }
404 }
405 
MakeFrame(VideoFrameType frame_type,int picture_id)406 std::unique_ptr<FrameObjectFake> MakeFrame(VideoFrameType frame_type,
407                                            int picture_id) {
408   auto frame = std::make_unique<FrameObjectFake>();
409   frame->SetPayloadType(99);
410   frame->id.picture_id = picture_id;
411   frame->SetFrameType(frame_type);
412   return frame;
413 }
414 
TEST_F(VideoReceiveStreamTestWithFakeDecoder,PassesFrameWhenEncodedFramesCallbackSet)415 TEST_F(VideoReceiveStreamTestWithFakeDecoder,
416        PassesFrameWhenEncodedFramesCallbackSet) {
417   testing::MockFunction<void(const RecordableEncodedFrame&)> callback;
418   video_receive_stream_->Start();
419   // Expect a keyframe request to be generated
420   EXPECT_CALL(mock_transport_, SendRtcp);
421   EXPECT_CALL(callback, Call);
422   video_receive_stream_->SetAndGetRecordingState(
423       VideoReceiveStream::RecordingState(callback.AsStdFunction()), true);
424   video_receive_stream_->OnCompleteFrame(
425       MakeFrame(VideoFrameType::kVideoFrameKey, 0));
426   EXPECT_TRUE(fake_renderer_.WaitForRenderedFrame(kDefaultTimeOutMs));
427   video_receive_stream_->Stop();
428 }
429 
TEST_F(VideoReceiveStreamTestWithFakeDecoder,MovesEncodedFrameDispatchStateWhenReCreating)430 TEST_F(VideoReceiveStreamTestWithFakeDecoder,
431        MovesEncodedFrameDispatchStateWhenReCreating) {
432   testing::MockFunction<void(const RecordableEncodedFrame&)> callback;
433   video_receive_stream_->Start();
434   // Expect a key frame request over RTCP.
435   EXPECT_CALL(mock_transport_, SendRtcp).Times(1);
436   video_receive_stream_->SetAndGetRecordingState(
437       VideoReceiveStream::RecordingState(callback.AsStdFunction()), true);
438   video_receive_stream_->Stop();
439   VideoReceiveStream::RecordingState old_state =
440       video_receive_stream_->SetAndGetRecordingState(
441           VideoReceiveStream::RecordingState(), false);
442   ReCreateReceiveStream(std::move(old_state));
443   video_receive_stream_->Stop();
444 }
445 
446 class VideoReceiveStreamTestWithSimulatedClock : public ::testing::Test {
447  public:
448   class FakeDecoder2 : public test::FakeDecoder {
449    public:
FakeDecoder2(std::function<void ()> decode_callback)450     explicit FakeDecoder2(std::function<void()> decode_callback)
451         : callback_(decode_callback) {}
452 
Decode(const EncodedImage & input,bool missing_frames,int64_t render_time_ms)453     int32_t Decode(const EncodedImage& input,
454                    bool missing_frames,
455                    int64_t render_time_ms) override {
456       int32_t result =
457           FakeDecoder::Decode(input, missing_frames, render_time_ms);
458       callback_();
459       return result;
460     }
461 
462    private:
463     std::function<void()> callback_;
464   };
465 
GetConfig(Transport * transport,VideoDecoderFactory * decoder_factory,rtc::VideoSinkInterface<webrtc::VideoFrame> * renderer)466   static VideoReceiveStream::Config GetConfig(
467       Transport* transport,
468       VideoDecoderFactory* decoder_factory,
469       rtc::VideoSinkInterface<webrtc::VideoFrame>* renderer) {
470     VideoReceiveStream::Config config(transport);
471     config.rtp.remote_ssrc = 1111;
472     config.rtp.local_ssrc = 2222;
473     config.renderer = renderer;
474     VideoReceiveStream::Decoder fake_decoder;
475     fake_decoder.payload_type = 99;
476     fake_decoder.video_format = SdpVideoFormat("VP8");
477     fake_decoder.decoder_factory = decoder_factory;
478     config.decoders.push_back(fake_decoder);
479     return config;
480   }
481 
VideoReceiveStreamTestWithSimulatedClock()482   VideoReceiveStreamTestWithSimulatedClock()
483       : time_controller_(Timestamp::Millis(4711)),
484         fake_decoder_factory_([this] {
485           return std::make_unique<FakeDecoder2>([this] { OnFrameDecoded(); });
486         }),
487         process_thread_(time_controller_.CreateProcessThread("ProcessThread")),
488         config_(GetConfig(&mock_transport_,
489                           &fake_decoder_factory_,
490                           &fake_renderer_)),
491         call_stats_(time_controller_.GetClock(), process_thread_.get()),
492         video_receive_stream_(time_controller_.GetTaskQueueFactory(),
493                               &rtp_stream_receiver_controller_,
494                               /*num_cores=*/2,
495                               &packet_router_,
496                               config_.Copy(),
497                               process_thread_.get(),
498                               &call_stats_,
499                               time_controller_.GetClock(),
500                               new VCMTiming(time_controller_.GetClock())) {
501     video_receive_stream_.Start();
502   }
503 
OnFrameDecoded()504   void OnFrameDecoded() { event_->Set(); }
505 
PassEncodedFrameAndWait(std::unique_ptr<video_coding::EncodedFrame> frame)506   void PassEncodedFrameAndWait(
507       std::unique_ptr<video_coding::EncodedFrame> frame) {
508       event_ = std::make_unique<rtc::Event>();
509       // This call will eventually end up in the Decoded method where the
510       // event is set.
511       video_receive_stream_.OnCompleteFrame(std::move(frame));
512       event_->Wait(rtc::Event::kForever);
513   }
514 
515  protected:
516   GlobalSimulatedTimeController time_controller_;
517   test::FunctionVideoDecoderFactory fake_decoder_factory_;
518   std::unique_ptr<ProcessThread> process_thread_;
519   MockTransport mock_transport_;
520   cricket::FakeVideoRenderer fake_renderer_;
521   VideoReceiveStream::Config config_;
522   CallStats call_stats_;
523   PacketRouter packet_router_;
524   RtpStreamReceiverController rtp_stream_receiver_controller_;
525   webrtc::internal::VideoReceiveStream video_receive_stream_;
526   std::unique_ptr<rtc::Event> event_;
527 };
528 
TEST_F(VideoReceiveStreamTestWithSimulatedClock,RequestsKeyFramesUntilKeyFrameReceived)529 TEST_F(VideoReceiveStreamTestWithSimulatedClock,
530        RequestsKeyFramesUntilKeyFrameReceived) {
531   auto tick = TimeDelta::Millis(
532       internal::VideoReceiveStream::kMaxWaitForKeyFrameMs / 2);
533   EXPECT_CALL(mock_transport_, SendRtcp).Times(1);
534   video_receive_stream_.GenerateKeyFrame();
535   PassEncodedFrameAndWait(MakeFrame(VideoFrameType::kVideoFrameDelta, 0));
536   time_controller_.AdvanceTime(tick);
537   PassEncodedFrameAndWait(MakeFrame(VideoFrameType::kVideoFrameDelta, 1));
538   testing::Mock::VerifyAndClearExpectations(&mock_transport_);
539 
540   // T+200ms: still no key frame received, expect key frame request sent again.
541   EXPECT_CALL(mock_transport_, SendRtcp).Times(1);
542   time_controller_.AdvanceTime(tick);
543   PassEncodedFrameAndWait(MakeFrame(VideoFrameType::kVideoFrameDelta, 2));
544   testing::Mock::VerifyAndClearExpectations(&mock_transport_);
545 
546   // T+200ms: now send a key frame - we should not observe new key frame
547   // requests after this.
548   EXPECT_CALL(mock_transport_, SendRtcp).Times(0);
549   PassEncodedFrameAndWait(MakeFrame(VideoFrameType::kVideoFrameKey, 3));
550   time_controller_.AdvanceTime(2 * tick);
551   PassEncodedFrameAndWait(MakeFrame(VideoFrameType::kVideoFrameDelta, 4));
552 }
553 
554 }  // namespace webrtc
555