Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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 "modules/video_coding/generic_decoder.h" |
| 12 | |
Danil Chapovalov | 7b78a31 | 2021-08-06 12:30:02 +0200 | [diff] [blame] | 13 | #include <memory> |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame] | 14 | #include <vector> |
| 15 | |
| 16 | #include "absl/types/optional.h" |
| 17 | #include "api/task_queue/default_task_queue_factory.h" |
Danil Chapovalov | 355b8d2 | 2021-08-13 16:50:37 +0200 | [diff] [blame] | 18 | #include "api/video_codecs/video_decoder.h" |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame] | 19 | #include "common_video/test/utilities.h" |
Rasmus Brandt | c4d253c | 2022-05-25 12:03:35 +0200 | [diff] [blame^] | 20 | #include "modules/video_coding/timing/timing.h" |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame] | 21 | #include "rtc_base/event.h" |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 22 | #include "rtc_base/synchronization/mutex.h" |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame] | 23 | #include "system_wrappers/include/clock.h" |
| 24 | #include "test/fake_decoder.h" |
| 25 | #include "test/gmock.h" |
| 26 | #include "test/gtest.h" |
Jonas Oreland | e02f9ee | 2022-03-25 12:43:14 +0100 | [diff] [blame] | 27 | #include "test/scoped_key_value_config.h" |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame] | 28 | |
| 29 | namespace webrtc { |
| 30 | namespace video_coding { |
| 31 | |
| 32 | class ReceiveCallback : public VCMReceiveCallback { |
| 33 | public: |
| 34 | int32_t FrameToRender(VideoFrame& videoFrame, // NOLINT |
| 35 | absl::optional<uint8_t> qp, |
Johannes Kron | bfd343b | 2019-07-01 10:07:50 +0200 | [diff] [blame] | 36 | int32_t decode_time_ms, |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame] | 37 | VideoContentType content_type) override { |
| 38 | { |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 39 | MutexLock lock(&lock_); |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame] | 40 | last_frame_ = videoFrame; |
| 41 | } |
| 42 | received_frame_event_.Set(); |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | absl::optional<VideoFrame> GetLastFrame() { |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 47 | MutexLock lock(&lock_); |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame] | 48 | return last_frame_; |
| 49 | } |
| 50 | |
| 51 | absl::optional<VideoFrame> WaitForFrame(int64_t wait_ms) { |
| 52 | if (received_frame_event_.Wait(wait_ms)) { |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 53 | MutexLock lock(&lock_); |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame] | 54 | return last_frame_; |
| 55 | } else { |
| 56 | return absl::nullopt; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | private: |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 61 | Mutex lock_; |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame] | 62 | rtc::Event received_frame_event_; |
| 63 | absl::optional<VideoFrame> last_frame_ RTC_GUARDED_BY(lock_); |
| 64 | }; |
| 65 | |
| 66 | class GenericDecoderTest : public ::testing::Test { |
| 67 | protected: |
| 68 | GenericDecoderTest() |
| 69 | : clock_(0), |
Jonas Oreland | e02f9ee | 2022-03-25 12:43:14 +0100 | [diff] [blame] | 70 | timing_(&clock_, field_trials_), |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame] | 71 | task_queue_factory_(CreateDefaultTaskQueueFactory()), |
| 72 | decoder_(task_queue_factory_.get()), |
Jonas Oreland | e02f9ee | 2022-03-25 12:43:14 +0100 | [diff] [blame] | 73 | vcm_callback_(&timing_, &clock_, field_trials_), |
Danil Chapovalov | 7b78a31 | 2021-08-06 12:30:02 +0200 | [diff] [blame] | 74 | generic_decoder_(&decoder_) {} |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame] | 75 | |
| 76 | void SetUp() override { |
| 77 | generic_decoder_.RegisterDecodeCompleteCallback(&vcm_callback_); |
| 78 | vcm_callback_.SetUserReceiveCallback(&user_callback_); |
Danil Chapovalov | 355b8d2 | 2021-08-13 16:50:37 +0200 | [diff] [blame] | 79 | VideoDecoder::Settings settings; |
| 80 | settings.set_codec_type(kVideoCodecVP8); |
| 81 | settings.set_max_render_resolution({10, 10}); |
| 82 | settings.set_number_of_cores(4); |
| 83 | generic_decoder_.Configure(settings); |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Jonas Oreland | e02f9ee | 2022-03-25 12:43:14 +0100 | [diff] [blame] | 86 | test::ScopedKeyValueConfig field_trials_; |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame] | 87 | SimulatedClock clock_; |
| 88 | VCMTiming timing_; |
| 89 | std::unique_ptr<TaskQueueFactory> task_queue_factory_; |
| 90 | webrtc::test::FakeDecoder decoder_; |
| 91 | VCMDecodedFrameCallback vcm_callback_; |
| 92 | VCMGenericDecoder generic_decoder_; |
| 93 | ReceiveCallback user_callback_; |
| 94 | }; |
| 95 | |
Chen Xing | f00bf42 | 2019-06-20 10:05:55 +0200 | [diff] [blame] | 96 | TEST_F(GenericDecoderTest, PassesPacketInfos) { |
| 97 | RtpPacketInfos packet_infos = CreatePacketInfos(3); |
| 98 | VCMEncodedFrame encoded_frame; |
| 99 | encoded_frame.SetPacketInfos(packet_infos); |
Johannes Kron | 05f8487 | 2020-01-16 14:09:33 +0100 | [diff] [blame] | 100 | generic_decoder_.Decode(encoded_frame, clock_.CurrentTime()); |
Chen Xing | f00bf42 | 2019-06-20 10:05:55 +0200 | [diff] [blame] | 101 | absl::optional<VideoFrame> decoded_frame = user_callback_.WaitForFrame(10); |
| 102 | ASSERT_TRUE(decoded_frame.has_value()); |
| 103 | EXPECT_EQ(decoded_frame->packet_infos().size(), 3U); |
| 104 | } |
| 105 | |
| 106 | TEST_F(GenericDecoderTest, PassesPacketInfosForDelayedDecoders) { |
| 107 | RtpPacketInfos packet_infos = CreatePacketInfos(3); |
| 108 | decoder_.SetDelayedDecoding(100); |
| 109 | |
| 110 | { |
| 111 | // Ensure the original frame is destroyed before the decoding is completed. |
| 112 | VCMEncodedFrame encoded_frame; |
| 113 | encoded_frame.SetPacketInfos(packet_infos); |
Johannes Kron | 05f8487 | 2020-01-16 14:09:33 +0100 | [diff] [blame] | 114 | generic_decoder_.Decode(encoded_frame, clock_.CurrentTime()); |
Chen Xing | f00bf42 | 2019-06-20 10:05:55 +0200 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | absl::optional<VideoFrame> decoded_frame = user_callback_.WaitForFrame(200); |
| 118 | ASSERT_TRUE(decoded_frame.has_value()); |
| 119 | EXPECT_EQ(decoded_frame->packet_infos().size(), 3U); |
| 120 | } |
| 121 | |
Johannes Kron | 111e981 | 2020-10-26 13:54:40 +0100 | [diff] [blame] | 122 | TEST_F(GenericDecoderTest, MaxCompositionDelayNotSetByDefault) { |
| 123 | VCMEncodedFrame encoded_frame; |
| 124 | generic_decoder_.Decode(encoded_frame, clock_.CurrentTime()); |
| 125 | absl::optional<VideoFrame> decoded_frame = user_callback_.WaitForFrame(10); |
| 126 | ASSERT_TRUE(decoded_frame.has_value()); |
| 127 | EXPECT_FALSE(decoded_frame->max_composition_delay_in_frames()); |
| 128 | } |
| 129 | |
| 130 | TEST_F(GenericDecoderTest, MaxCompositionDelayActivatedByPlayoutDelay) { |
| 131 | VCMEncodedFrame encoded_frame; |
| 132 | // VideoReceiveStream2 would set MaxCompositionDelayInFrames if playout delay |
| 133 | // is specified as X,Y, where X=0, Y>0. |
| 134 | const VideoPlayoutDelay kPlayoutDelay = {0, 50}; |
| 135 | constexpr int kMaxCompositionDelayInFrames = 3; // ~50 ms at 60 fps. |
| 136 | encoded_frame.SetPlayoutDelay(kPlayoutDelay); |
| 137 | timing_.SetMaxCompositionDelayInFrames( |
| 138 | absl::make_optional(kMaxCompositionDelayInFrames)); |
| 139 | generic_decoder_.Decode(encoded_frame, clock_.CurrentTime()); |
| 140 | absl::optional<VideoFrame> decoded_frame = user_callback_.WaitForFrame(10); |
| 141 | ASSERT_TRUE(decoded_frame.has_value()); |
| 142 | EXPECT_EQ(kMaxCompositionDelayInFrames, |
| 143 | decoded_frame->max_composition_delay_in_frames()); |
| 144 | } |
| 145 | |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame] | 146 | } // namespace video_coding |
| 147 | } // namespace webrtc |