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