blob: 93d55a67d4d9f85379f978142ff276ed39dee9b8 [file] [log] [blame]
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +02001/*
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
13#include <vector>
14
15#include "absl/types/optional.h"
16#include "api/task_queue/default_task_queue_factory.h"
17#include "common_video/test/utilities.h"
18#include "modules/video_coding/timing.h"
19#include "rtc_base/critical_section.h"
20#include "rtc_base/event.h"
21#include "system_wrappers/include/clock.h"
22#include "test/fake_decoder.h"
23#include "test/gmock.h"
24#include "test/gtest.h"
25
26namespace webrtc {
27namespace video_coding {
28
29class ReceiveCallback : public VCMReceiveCallback {
30 public:
31 int32_t FrameToRender(VideoFrame& videoFrame, // NOLINT
32 absl::optional<uint8_t> qp,
Johannes Kronbfd343b2019-07-01 10:07:50 +020033 int32_t decode_time_ms,
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +020034 VideoContentType content_type) override {
35 {
36 rtc::CritScope cs(&lock_);
37 last_frame_ = videoFrame;
38 }
39 received_frame_event_.Set();
40 return 0;
41 }
42
43 absl::optional<VideoFrame> GetLastFrame() {
44 rtc::CritScope cs(&lock_);
45 return last_frame_;
46 }
47
48 absl::optional<VideoFrame> WaitForFrame(int64_t wait_ms) {
49 if (received_frame_event_.Wait(wait_ms)) {
50 rtc::CritScope cs(&lock_);
51 return last_frame_;
52 } else {
53 return absl::nullopt;
54 }
55 }
56
57 private:
58 rtc::CriticalSection lock_;
59 rtc::Event received_frame_event_;
60 absl::optional<VideoFrame> last_frame_ RTC_GUARDED_BY(lock_);
61};
62
63class GenericDecoderTest : public ::testing::Test {
64 protected:
65 GenericDecoderTest()
66 : clock_(0),
67 timing_(&clock_),
68 task_queue_factory_(CreateDefaultTaskQueueFactory()),
69 decoder_(task_queue_factory_.get()),
70 vcm_callback_(&timing_, &clock_),
71 generic_decoder_(&decoder_, /*isExternal=*/true) {}
72
73 void SetUp() override {
74 generic_decoder_.RegisterDecodeCompleteCallback(&vcm_callback_);
75 vcm_callback_.SetUserReceiveCallback(&user_callback_);
76 VideoCodec settings;
77 settings.codecType = kVideoCodecVP8;
78 settings.width = 10;
79 settings.height = 10;
80 generic_decoder_.InitDecode(&settings, /*numberOfCores=*/4);
81 }
82
83 SimulatedClock clock_;
84 VCMTiming timing_;
85 std::unique_ptr<TaskQueueFactory> task_queue_factory_;
86 webrtc::test::FakeDecoder decoder_;
87 VCMDecodedFrameCallback vcm_callback_;
88 VCMGenericDecoder generic_decoder_;
89 ReceiveCallback user_callback_;
90};
91
92TEST_F(GenericDecoderTest, PassesColorSpace) {
93 webrtc::ColorSpace color_space =
94 CreateTestColorSpace(/*with_hdr_metadata=*/true);
95 VCMEncodedFrame encoded_frame;
96 encoded_frame.SetColorSpace(color_space);
97 generic_decoder_.Decode(encoded_frame, clock_.TimeInMilliseconds());
98 absl::optional<VideoFrame> decoded_frame = user_callback_.WaitForFrame(10);
99 ASSERT_TRUE(decoded_frame.has_value());
100 absl::optional<webrtc::ColorSpace> decoded_color_space =
101 decoded_frame->color_space();
102 ASSERT_TRUE(decoded_color_space.has_value());
103 EXPECT_EQ(*decoded_color_space, color_space);
104}
105
106TEST_F(GenericDecoderTest, PassesColorSpaceForDelayedDecoders) {
107 webrtc::ColorSpace color_space =
108 CreateTestColorSpace(/*with_hdr_metadata=*/true);
109 decoder_.SetDelayedDecoding(100);
110
111 {
112 // Ensure the original frame is destroyed before the decoding is completed.
113 VCMEncodedFrame encoded_frame;
114 encoded_frame.SetColorSpace(color_space);
115 generic_decoder_.Decode(encoded_frame, clock_.TimeInMilliseconds());
116 }
117
118 absl::optional<VideoFrame> decoded_frame = user_callback_.WaitForFrame(200);
119 ASSERT_TRUE(decoded_frame.has_value());
120 absl::optional<webrtc::ColorSpace> decoded_color_space =
121 decoded_frame->color_space();
122 ASSERT_TRUE(decoded_color_space.has_value());
123 EXPECT_EQ(*decoded_color_space, color_space);
124}
125
Chen Xingf00bf422019-06-20 10:05:55 +0200126TEST_F(GenericDecoderTest, PassesPacketInfos) {
127 RtpPacketInfos packet_infos = CreatePacketInfos(3);
128 VCMEncodedFrame encoded_frame;
129 encoded_frame.SetPacketInfos(packet_infos);
130 generic_decoder_.Decode(encoded_frame, clock_.TimeInMilliseconds());
131 absl::optional<VideoFrame> decoded_frame = user_callback_.WaitForFrame(10);
132 ASSERT_TRUE(decoded_frame.has_value());
133 EXPECT_EQ(decoded_frame->packet_infos().size(), 3U);
134}
135
136TEST_F(GenericDecoderTest, PassesPacketInfosForDelayedDecoders) {
137 RtpPacketInfos packet_infos = CreatePacketInfos(3);
138 decoder_.SetDelayedDecoding(100);
139
140 {
141 // Ensure the original frame is destroyed before the decoding is completed.
142 VCMEncodedFrame encoded_frame;
143 encoded_frame.SetPacketInfos(packet_infos);
144 generic_decoder_.Decode(encoded_frame, clock_.TimeInMilliseconds());
145 }
146
147 absl::optional<VideoFrame> decoded_frame = user_callback_.WaitForFrame(200);
148 ASSERT_TRUE(decoded_frame.has_value());
149 EXPECT_EQ(decoded_frame->packet_infos().size(), 3U);
150}
151
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +0200152} // namespace video_coding
153} // namespace webrtc