philipel | 539f9b3 | 2020-01-09 15:12:25 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2020 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_stream_decoder_impl.h" |
| 12 | |
| 13 | #include <vector> |
| 14 | |
| 15 | #include "api/video/i420_buffer.h" |
Danil Chapovalov | d08930d | 2021-08-12 11:26:55 | [diff] [blame] | 16 | #include "api/video_codecs/video_decoder.h" |
Evan Shrubsole | a0ee64c | 2022-04-26 08:09:04 | [diff] [blame] | 17 | #include "test/fake_encoded_frame.h" |
philipel | 539f9b3 | 2020-01-09 15:12:25 | [diff] [blame] | 18 | #include "test/gmock.h" |
| 19 | #include "test/gtest.h" |
Jonas Oreland | e02f9ee | 2022-03-25 11:43:14 | [diff] [blame] | 20 | #include "test/scoped_key_value_config.h" |
philipel | 539f9b3 | 2020-01-09 15:12:25 | [diff] [blame] | 21 | #include "test/time_controller/simulated_time_controller.h" |
| 22 | |
| 23 | namespace webrtc { |
| 24 | namespace { |
| 25 | using ::testing::_; |
philipel | 539f9b3 | 2020-01-09 15:12:25 | [diff] [blame] | 26 | using ::testing::NiceMock; |
| 27 | using ::testing::Return; |
| 28 | |
| 29 | class MockVideoStreamDecoderCallbacks |
| 30 | : public VideoStreamDecoderInterface::Callbacks { |
| 31 | public: |
Danil Chapovalov | 91fdc60 | 2020-05-14 17:17:51 | [diff] [blame] | 32 | MOCK_METHOD(void, OnNonDecodableState, (), (override)); |
philipel | 9aa9b8d | 2021-02-15 12:31:29 | [diff] [blame] | 33 | MOCK_METHOD(void, OnContinuousUntil, (int64_t frame_id), (override)); |
philipel | a028a1a | 2020-09-11 14:05:56 | [diff] [blame] | 34 | MOCK_METHOD( |
| 35 | void, |
| 36 | OnDecodedFrame, |
| 37 | (VideoFrame frame, |
| 38 | const VideoStreamDecoderInterface::Callbacks::FrameInfo& frame_info), |
| 39 | (override)); |
philipel | 539f9b3 | 2020-01-09 15:12:25 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | class StubVideoDecoder : public VideoDecoder { |
| 43 | public: |
Danil Chapovalov | d08930d | 2021-08-12 11:26:55 | [diff] [blame] | 44 | StubVideoDecoder() { ON_CALL(*this, Configure).WillByDefault(Return(true)); } |
| 45 | |
| 46 | MOCK_METHOD(bool, Configure, (const Settings&), (override)); |
philipel | 539f9b3 | 2020-01-09 15:12:25 | [diff] [blame] | 47 | |
| 48 | int32_t Decode(const EncodedImage& input_image, |
| 49 | bool missing_frames, |
| 50 | int64_t render_time_ms) override { |
| 51 | int32_t ret_code = DecodeCall(input_image, missing_frames, render_time_ms); |
| 52 | if (ret_code == WEBRTC_VIDEO_CODEC_OK || |
| 53 | ret_code == WEBRTC_VIDEO_CODEC_OK_REQUEST_KEYFRAME) { |
| 54 | VideoFrame frame = VideoFrame::Builder() |
| 55 | .set_video_frame_buffer(I420Buffer::Create(1, 1)) |
| 56 | .build(); |
| 57 | callback_->Decoded(frame); |
| 58 | } |
| 59 | return ret_code; |
| 60 | } |
| 61 | |
Danil Chapovalov | 91fdc60 | 2020-05-14 17:17:51 | [diff] [blame] | 62 | MOCK_METHOD(int32_t, |
| 63 | DecodeCall, |
| 64 | (const EncodedImage& input_image, |
| 65 | bool missing_frames, |
| 66 | int64_t render_time_ms), |
| 67 | ()); |
philipel | 539f9b3 | 2020-01-09 15:12:25 | [diff] [blame] | 68 | |
| 69 | int32_t Release() override { return 0; } |
| 70 | |
| 71 | int32_t RegisterDecodeCompleteCallback( |
| 72 | DecodedImageCallback* callback) override { |
| 73 | callback_ = callback; |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | private: |
| 78 | DecodedImageCallback* callback_; |
| 79 | }; |
| 80 | |
| 81 | class WrappedVideoDecoder : public VideoDecoder { |
| 82 | public: |
| 83 | explicit WrappedVideoDecoder(StubVideoDecoder* decoder) : decoder_(decoder) {} |
| 84 | |
Danil Chapovalov | d08930d | 2021-08-12 11:26:55 | [diff] [blame] | 85 | bool Configure(const Settings& settings) override { |
| 86 | return decoder_->Configure(settings); |
philipel | 539f9b3 | 2020-01-09 15:12:25 | [diff] [blame] | 87 | } |
| 88 | int32_t Decode(const EncodedImage& input_image, |
| 89 | bool missing_frames, |
| 90 | int64_t render_time_ms) override { |
| 91 | return decoder_->Decode(input_image, missing_frames, render_time_ms); |
| 92 | } |
| 93 | int32_t Release() override { return decoder_->Release(); } |
| 94 | |
| 95 | int32_t RegisterDecodeCompleteCallback( |
| 96 | DecodedImageCallback* callback) override { |
| 97 | return decoder_->RegisterDecodeCompleteCallback(callback); |
| 98 | } |
| 99 | |
| 100 | private: |
| 101 | StubVideoDecoder* decoder_; |
| 102 | }; |
| 103 | |
| 104 | class FakeVideoDecoderFactory : public VideoDecoderFactory { |
| 105 | public: |
| 106 | std::vector<SdpVideoFormat> GetSupportedFormats() const override { |
| 107 | return {}; |
| 108 | } |
| 109 | std::unique_ptr<VideoDecoder> CreateVideoDecoder( |
| 110 | const SdpVideoFormat& format) override { |
| 111 | if (format.name == "VP8") { |
| 112 | return std::make_unique<WrappedVideoDecoder>(&vp8_decoder_); |
| 113 | } |
| 114 | |
| 115 | if (format.name == "AV1") { |
| 116 | return std::make_unique<WrappedVideoDecoder>(&av1_decoder_); |
| 117 | } |
| 118 | |
| 119 | return {}; |
| 120 | } |
| 121 | |
| 122 | StubVideoDecoder& Vp8Decoder() { return vp8_decoder_; } |
| 123 | StubVideoDecoder& Av1Decoder() { return av1_decoder_; } |
| 124 | |
| 125 | private: |
| 126 | NiceMock<StubVideoDecoder> vp8_decoder_; |
| 127 | NiceMock<StubVideoDecoder> av1_decoder_; |
| 128 | }; |
| 129 | |
philipel | 539f9b3 | 2020-01-09 15:12:25 | [diff] [blame] | 130 | class VideoStreamDecoderImplTest : public ::testing::Test { |
| 131 | public: |
| 132 | VideoStreamDecoderImplTest() |
Danil Chapovalov | 0c626af | 2020-02-10 10:16:00 | [diff] [blame] | 133 | : time_controller_(Timestamp::Seconds(0)), |
Philipp Hancke | bbff58d | 2024-02-27 11:18:33 | [diff] [blame] | 134 | video_stream_decoder_( |
| 135 | &callbacks_, |
| 136 | &decoder_factory_, |
| 137 | time_controller_.GetTaskQueueFactory(), |
| 138 | {{1, std::make_pair(SdpVideoFormat::VP8(), 1)}, |
| 139 | {2, std::make_pair(SdpVideoFormat::AV1Profile0(), 1)}}, |
| 140 | &field_trials_) { |
Johannes Kron | 23bfff3 | 2021-09-28 19:31:46 | [diff] [blame] | 141 | // Set the min playout delay to a value greater than zero to not activate |
| 142 | // the low-latency renderer. |
| 143 | video_stream_decoder_.SetMinPlayoutDelay(TimeDelta::Millis(10)); |
philipel | 539f9b3 | 2020-01-09 15:12:25 | [diff] [blame] | 144 | } |
| 145 | |
Jonas Oreland | e02f9ee | 2022-03-25 11:43:14 | [diff] [blame] | 146 | test::ScopedKeyValueConfig field_trials_; |
philipel | 539f9b3 | 2020-01-09 15:12:25 | [diff] [blame] | 147 | NiceMock<MockVideoStreamDecoderCallbacks> callbacks_; |
| 148 | FakeVideoDecoderFactory decoder_factory_; |
| 149 | GlobalSimulatedTimeController time_controller_; |
| 150 | VideoStreamDecoderImpl video_stream_decoder_; |
| 151 | }; |
| 152 | |
| 153 | TEST_F(VideoStreamDecoderImplTest, InsertAndDecodeFrame) { |
Evan Shrubsole | a0ee64c | 2022-04-26 08:09:04 | [diff] [blame] | 154 | video_stream_decoder_.OnFrame( |
| 155 | test::FakeFrameBuilder().PayloadType(1).AsLast().Build()); |
philipel | 539f9b3 | 2020-01-09 15:12:25 | [diff] [blame] | 156 | EXPECT_CALL(callbacks_, OnDecodedFrame); |
Danil Chapovalov | 0c626af | 2020-02-10 10:16:00 | [diff] [blame] | 157 | time_controller_.AdvanceTime(TimeDelta::Millis(1)); |
philipel | 539f9b3 | 2020-01-09 15:12:25 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | TEST_F(VideoStreamDecoderImplTest, NonDecodableStateWaitingForKeyframe) { |
| 161 | EXPECT_CALL(callbacks_, OnNonDecodableState); |
Danil Chapovalov | 0c626af | 2020-02-10 10:16:00 | [diff] [blame] | 162 | time_controller_.AdvanceTime(TimeDelta::Millis(200)); |
philipel | 539f9b3 | 2020-01-09 15:12:25 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | TEST_F(VideoStreamDecoderImplTest, NonDecodableStateWaitingForDeltaFrame) { |
Evan Shrubsole | a0ee64c | 2022-04-26 08:09:04 | [diff] [blame] | 166 | video_stream_decoder_.OnFrame( |
| 167 | test::FakeFrameBuilder().PayloadType(1).AsLast().Build()); |
philipel | 539f9b3 | 2020-01-09 15:12:25 | [diff] [blame] | 168 | EXPECT_CALL(callbacks_, OnDecodedFrame); |
Danil Chapovalov | 0c626af | 2020-02-10 10:16:00 | [diff] [blame] | 169 | time_controller_.AdvanceTime(TimeDelta::Millis(1)); |
philipel | 539f9b3 | 2020-01-09 15:12:25 | [diff] [blame] | 170 | EXPECT_CALL(callbacks_, OnNonDecodableState); |
Danil Chapovalov | 0c626af | 2020-02-10 10:16:00 | [diff] [blame] | 171 | time_controller_.AdvanceTime(TimeDelta::Millis(3000)); |
philipel | 539f9b3 | 2020-01-09 15:12:25 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | TEST_F(VideoStreamDecoderImplTest, InsertAndDecodeFrameWithKeyframeRequest) { |
Evan Shrubsole | a0ee64c | 2022-04-26 08:09:04 | [diff] [blame] | 175 | video_stream_decoder_.OnFrame( |
| 176 | test::FakeFrameBuilder().PayloadType(1).AsLast().Build()); |
philipel | 539f9b3 | 2020-01-09 15:12:25 | [diff] [blame] | 177 | EXPECT_CALL(decoder_factory_.Vp8Decoder(), DecodeCall) |
| 178 | .WillOnce(Return(WEBRTC_VIDEO_CODEC_OK_REQUEST_KEYFRAME)); |
| 179 | EXPECT_CALL(callbacks_, OnDecodedFrame); |
| 180 | EXPECT_CALL(callbacks_, OnNonDecodableState); |
Danil Chapovalov | 0c626af | 2020-02-10 10:16:00 | [diff] [blame] | 181 | time_controller_.AdvanceTime(TimeDelta::Millis(1)); |
philipel | 539f9b3 | 2020-01-09 15:12:25 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | TEST_F(VideoStreamDecoderImplTest, FailToInitDecoder) { |
Evan Shrubsole | a0ee64c | 2022-04-26 08:09:04 | [diff] [blame] | 185 | video_stream_decoder_.OnFrame( |
| 186 | test::FakeFrameBuilder() |
| 187 | .ReceivedTime(time_controller_.GetClock()->CurrentTime()) |
| 188 | .PayloadType(1) |
| 189 | .AsLast() |
| 190 | .Build()); |
Danil Chapovalov | d08930d | 2021-08-12 11:26:55 | [diff] [blame] | 191 | ON_CALL(decoder_factory_.Vp8Decoder(), Configure) |
| 192 | .WillByDefault(Return(false)); |
philipel | 539f9b3 | 2020-01-09 15:12:25 | [diff] [blame] | 193 | EXPECT_CALL(callbacks_, OnNonDecodableState); |
Danil Chapovalov | 0c626af | 2020-02-10 10:16:00 | [diff] [blame] | 194 | time_controller_.AdvanceTime(TimeDelta::Millis(1)); |
philipel | 539f9b3 | 2020-01-09 15:12:25 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | TEST_F(VideoStreamDecoderImplTest, FailToDecodeFrame) { |
Evan Shrubsole | a0ee64c | 2022-04-26 08:09:04 | [diff] [blame] | 198 | video_stream_decoder_.OnFrame( |
| 199 | test::FakeFrameBuilder().PayloadType(1).AsLast().Build()); |
philipel | 539f9b3 | 2020-01-09 15:12:25 | [diff] [blame] | 200 | ON_CALL(decoder_factory_.Vp8Decoder(), DecodeCall) |
| 201 | .WillByDefault(Return(WEBRTC_VIDEO_CODEC_ERROR)); |
| 202 | EXPECT_CALL(callbacks_, OnNonDecodableState); |
Danil Chapovalov | 0c626af | 2020-02-10 10:16:00 | [diff] [blame] | 203 | time_controller_.AdvanceTime(TimeDelta::Millis(1)); |
philipel | 539f9b3 | 2020-01-09 15:12:25 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | TEST_F(VideoStreamDecoderImplTest, ChangeFramePayloadType) { |
Johannes Kron | 23bfff3 | 2021-09-28 19:31:46 | [diff] [blame] | 207 | constexpr TimeDelta kFrameInterval = TimeDelta::Millis(1000 / 60); |
philipel | 539f9b3 | 2020-01-09 15:12:25 | [diff] [blame] | 208 | video_stream_decoder_.OnFrame( |
Evan Shrubsole | a0ee64c | 2022-04-26 08:09:04 | [diff] [blame] | 209 | test::FakeFrameBuilder().PayloadType(1).Id(0).AsLast().Build()); |
philipel | 539f9b3 | 2020-01-09 15:12:25 | [diff] [blame] | 210 | EXPECT_CALL(decoder_factory_.Vp8Decoder(), DecodeCall); |
| 211 | EXPECT_CALL(callbacks_, OnDecodedFrame); |
Johannes Kron | 23bfff3 | 2021-09-28 19:31:46 | [diff] [blame] | 212 | time_controller_.AdvanceTime(kFrameInterval); |
philipel | 539f9b3 | 2020-01-09 15:12:25 | [diff] [blame] | 213 | |
| 214 | video_stream_decoder_.OnFrame( |
Evan Shrubsole | a0ee64c | 2022-04-26 08:09:04 | [diff] [blame] | 215 | test::FakeFrameBuilder().PayloadType(2).Id(1).AsLast().Build()); |
philipel | 539f9b3 | 2020-01-09 15:12:25 | [diff] [blame] | 216 | EXPECT_CALL(decoder_factory_.Av1Decoder(), DecodeCall); |
| 217 | EXPECT_CALL(callbacks_, OnDecodedFrame); |
Johannes Kron | 23bfff3 | 2021-09-28 19:31:46 | [diff] [blame] | 218 | time_controller_.AdvanceTime(kFrameInterval); |
philipel | 539f9b3 | 2020-01-09 15:12:25 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | } // namespace |
| 222 | } // namespace webrtc |