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