blob: 6942e066792c222f590d893fcfeec10add000a02 [file] [log] [blame]
philipel539f9b32020-01-09 15:12:251/*
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 Chapovalovd08930d2021-08-12 11:26:5516#include "api/video_codecs/video_decoder.h"
Evan Shrubsolea0ee64c2022-04-26 08:09:0417#include "test/fake_encoded_frame.h"
philipel539f9b32020-01-09 15:12:2518#include "test/gmock.h"
19#include "test/gtest.h"
Jonas Orelande02f9ee2022-03-25 11:43:1420#include "test/scoped_key_value_config.h"
philipel539f9b32020-01-09 15:12:2521#include "test/time_controller/simulated_time_controller.h"
22
23namespace webrtc {
24namespace {
25using ::testing::_;
philipel539f9b32020-01-09 15:12:2526using ::testing::NiceMock;
27using ::testing::Return;
28
29class MockVideoStreamDecoderCallbacks
30 : public VideoStreamDecoderInterface::Callbacks {
31 public:
Danil Chapovalov91fdc602020-05-14 17:17:5132 MOCK_METHOD(void, OnNonDecodableState, (), (override));
philipel9aa9b8d2021-02-15 12:31:2933 MOCK_METHOD(void, OnContinuousUntil, (int64_t frame_id), (override));
philipela028a1a2020-09-11 14:05:5634 MOCK_METHOD(
35 void,
36 OnDecodedFrame,
37 (VideoFrame frame,
38 const VideoStreamDecoderInterface::Callbacks::FrameInfo& frame_info),
39 (override));
philipel539f9b32020-01-09 15:12:2540};
41
42class StubVideoDecoder : public VideoDecoder {
43 public:
Danil Chapovalovd08930d2021-08-12 11:26:5544 StubVideoDecoder() { ON_CALL(*this, Configure).WillByDefault(Return(true)); }
45
46 MOCK_METHOD(bool, Configure, (const Settings&), (override));
philipel539f9b32020-01-09 15:12:2547
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 Chapovalov91fdc602020-05-14 17:17:5162 MOCK_METHOD(int32_t,
63 DecodeCall,
64 (const EncodedImage& input_image,
65 bool missing_frames,
66 int64_t render_time_ms),
67 ());
philipel539f9b32020-01-09 15:12:2568
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
81class WrappedVideoDecoder : public VideoDecoder {
82 public:
83 explicit WrappedVideoDecoder(StubVideoDecoder* decoder) : decoder_(decoder) {}
84
Danil Chapovalovd08930d2021-08-12 11:26:5585 bool Configure(const Settings& settings) override {
86 return decoder_->Configure(settings);
philipel539f9b32020-01-09 15:12:2587 }
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
104class 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
philipel539f9b32020-01-09 15:12:25130class VideoStreamDecoderImplTest : public ::testing::Test {
131 public:
132 VideoStreamDecoderImplTest()
Danil Chapovalov0c626af2020-02-10 10:16:00133 : time_controller_(Timestamp::Seconds(0)),
Philipp Hanckebbff58d2024-02-27 11:18:33134 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 Kron23bfff32021-09-28 19:31:46141 // 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));
philipel539f9b32020-01-09 15:12:25144 }
145
Jonas Orelande02f9ee2022-03-25 11:43:14146 test::ScopedKeyValueConfig field_trials_;
philipel539f9b32020-01-09 15:12:25147 NiceMock<MockVideoStreamDecoderCallbacks> callbacks_;
148 FakeVideoDecoderFactory decoder_factory_;
149 GlobalSimulatedTimeController time_controller_;
150 VideoStreamDecoderImpl video_stream_decoder_;
151};
152
153TEST_F(VideoStreamDecoderImplTest, InsertAndDecodeFrame) {
Evan Shrubsolea0ee64c2022-04-26 08:09:04154 video_stream_decoder_.OnFrame(
155 test::FakeFrameBuilder().PayloadType(1).AsLast().Build());
philipel539f9b32020-01-09 15:12:25156 EXPECT_CALL(callbacks_, OnDecodedFrame);
Danil Chapovalov0c626af2020-02-10 10:16:00157 time_controller_.AdvanceTime(TimeDelta::Millis(1));
philipel539f9b32020-01-09 15:12:25158}
159
160TEST_F(VideoStreamDecoderImplTest, NonDecodableStateWaitingForKeyframe) {
161 EXPECT_CALL(callbacks_, OnNonDecodableState);
Danil Chapovalov0c626af2020-02-10 10:16:00162 time_controller_.AdvanceTime(TimeDelta::Millis(200));
philipel539f9b32020-01-09 15:12:25163}
164
165TEST_F(VideoStreamDecoderImplTest, NonDecodableStateWaitingForDeltaFrame) {
Evan Shrubsolea0ee64c2022-04-26 08:09:04166 video_stream_decoder_.OnFrame(
167 test::FakeFrameBuilder().PayloadType(1).AsLast().Build());
philipel539f9b32020-01-09 15:12:25168 EXPECT_CALL(callbacks_, OnDecodedFrame);
Danil Chapovalov0c626af2020-02-10 10:16:00169 time_controller_.AdvanceTime(TimeDelta::Millis(1));
philipel539f9b32020-01-09 15:12:25170 EXPECT_CALL(callbacks_, OnNonDecodableState);
Danil Chapovalov0c626af2020-02-10 10:16:00171 time_controller_.AdvanceTime(TimeDelta::Millis(3000));
philipel539f9b32020-01-09 15:12:25172}
173
174TEST_F(VideoStreamDecoderImplTest, InsertAndDecodeFrameWithKeyframeRequest) {
Evan Shrubsolea0ee64c2022-04-26 08:09:04175 video_stream_decoder_.OnFrame(
176 test::FakeFrameBuilder().PayloadType(1).AsLast().Build());
philipel539f9b32020-01-09 15:12:25177 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 Chapovalov0c626af2020-02-10 10:16:00181 time_controller_.AdvanceTime(TimeDelta::Millis(1));
philipel539f9b32020-01-09 15:12:25182}
183
184TEST_F(VideoStreamDecoderImplTest, FailToInitDecoder) {
Evan Shrubsolea0ee64c2022-04-26 08:09:04185 video_stream_decoder_.OnFrame(
186 test::FakeFrameBuilder()
187 .ReceivedTime(time_controller_.GetClock()->CurrentTime())
188 .PayloadType(1)
189 .AsLast()
190 .Build());
Danil Chapovalovd08930d2021-08-12 11:26:55191 ON_CALL(decoder_factory_.Vp8Decoder(), Configure)
192 .WillByDefault(Return(false));
philipel539f9b32020-01-09 15:12:25193 EXPECT_CALL(callbacks_, OnNonDecodableState);
Danil Chapovalov0c626af2020-02-10 10:16:00194 time_controller_.AdvanceTime(TimeDelta::Millis(1));
philipel539f9b32020-01-09 15:12:25195}
196
197TEST_F(VideoStreamDecoderImplTest, FailToDecodeFrame) {
Evan Shrubsolea0ee64c2022-04-26 08:09:04198 video_stream_decoder_.OnFrame(
199 test::FakeFrameBuilder().PayloadType(1).AsLast().Build());
philipel539f9b32020-01-09 15:12:25200 ON_CALL(decoder_factory_.Vp8Decoder(), DecodeCall)
201 .WillByDefault(Return(WEBRTC_VIDEO_CODEC_ERROR));
202 EXPECT_CALL(callbacks_, OnNonDecodableState);
Danil Chapovalov0c626af2020-02-10 10:16:00203 time_controller_.AdvanceTime(TimeDelta::Millis(1));
philipel539f9b32020-01-09 15:12:25204}
205
206TEST_F(VideoStreamDecoderImplTest, ChangeFramePayloadType) {
Johannes Kron23bfff32021-09-28 19:31:46207 constexpr TimeDelta kFrameInterval = TimeDelta::Millis(1000 / 60);
philipel539f9b32020-01-09 15:12:25208 video_stream_decoder_.OnFrame(
Evan Shrubsolea0ee64c2022-04-26 08:09:04209 test::FakeFrameBuilder().PayloadType(1).Id(0).AsLast().Build());
philipel539f9b32020-01-09 15:12:25210 EXPECT_CALL(decoder_factory_.Vp8Decoder(), DecodeCall);
211 EXPECT_CALL(callbacks_, OnDecodedFrame);
Johannes Kron23bfff32021-09-28 19:31:46212 time_controller_.AdvanceTime(kFrameInterval);
philipel539f9b32020-01-09 15:12:25213
214 video_stream_decoder_.OnFrame(
Evan Shrubsolea0ee64c2022-04-26 08:09:04215 test::FakeFrameBuilder().PayloadType(2).Id(1).AsLast().Build());
philipel539f9b32020-01-09 15:12:25216 EXPECT_CALL(decoder_factory_.Av1Decoder(), DecodeCall);
217 EXPECT_CALL(callbacks_, OnDecodedFrame);
Johannes Kron23bfff32021-09-28 19:31:46218 time_controller_.AdvanceTime(kFrameInterval);
philipel539f9b32020-01-09 15:12:25219}
220
221} // namespace
222} // namespace webrtc