1 /*
2 * Copyright (c) 2019 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 <memory>
12 #include <vector>
13
14 #include "absl/types/optional.h"
15 #include "api/test/create_frame_generator.h"
16 #include "api/video/encoded_image.h"
17 #include "api/video/video_codec_type.h"
18 #include "api/video_codecs/video_codec.h"
19 #include "api/video_codecs/video_encoder.h"
20 #include "common_video/libyuv/include/webrtc_libyuv.h"
21 #include "media/base/codec.h"
22 #include "media/base/media_constants.h"
23 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
24 #include "modules/video_coding/codecs/vp8/include/vp8.h"
25 #include "modules/video_coding/codecs/vp9/include/vp9.h"
26 #include "modules/video_coding/include/video_error_codes.h"
27 #include "modules/video_coding/utility/ivf_file_writer.h"
28 #include "rtc_base/event.h"
29 #include "test/gtest.h"
30 #include "test/testsupport/file_utils.h"
31 #include "test/testsupport/ivf_video_frame_generator.h"
32 #include "test/video_codec_settings.h"
33
34 #if defined(WEBRTC_USE_H264)
35 #include "modules/video_coding/codecs/h264/include/h264.h"
36 #include "rtc_base/synchronization/mutex.h"
37
38 #endif
39
40 namespace webrtc {
41 namespace test {
42 namespace {
43
44 constexpr int kWidth = 320;
45 constexpr int kHeight = 240;
46 constexpr int kVideoFramesCount = 30;
47 constexpr int kMaxFramerate = 30;
48 constexpr int kMaxFrameEncodeWaitTimeoutMs = 2000;
49 static const VideoEncoder::Capabilities kCapabilities(false);
50
51 #if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
52 constexpr double kExpectedMinPsnr = 35;
53 #else
54 constexpr double kExpectedMinPsnr = 39;
55 #endif
56
57 class IvfFileWriterEncodedCallback : public EncodedImageCallback {
58 public:
IvfFileWriterEncodedCallback(const std::string & file_name,VideoCodecType video_codec_type,int expected_frames_count)59 IvfFileWriterEncodedCallback(const std::string& file_name,
60 VideoCodecType video_codec_type,
61 int expected_frames_count)
62 : file_writer_(
63 IvfFileWriter::Wrap(FileWrapper::OpenWriteOnly(file_name), 0)),
64 video_codec_type_(video_codec_type),
65 expected_frames_count_(expected_frames_count) {
66 EXPECT_TRUE(file_writer_.get());
67 }
~IvfFileWriterEncodedCallback()68 ~IvfFileWriterEncodedCallback() { EXPECT_TRUE(file_writer_->Close()); }
69
OnEncodedImage(const EncodedImage & encoded_image,const CodecSpecificInfo * codec_specific_info)70 Result OnEncodedImage(const EncodedImage& encoded_image,
71 const CodecSpecificInfo* codec_specific_info) override {
72 EXPECT_TRUE(file_writer_->WriteFrame(encoded_image, video_codec_type_));
73
74 MutexLock lock(&lock_);
75 received_frames_count_++;
76 RTC_CHECK_LE(received_frames_count_, expected_frames_count_);
77 if (received_frames_count_ == expected_frames_count_) {
78 expected_frames_count_received_.Set();
79 }
80 return Result(Result::Error::OK);
81 }
82
WaitForExpectedFramesReceived(int timeout_ms)83 bool WaitForExpectedFramesReceived(int timeout_ms) {
84 return expected_frames_count_received_.Wait(timeout_ms);
85 }
86
87 private:
88 std::unique_ptr<IvfFileWriter> file_writer_;
89 const VideoCodecType video_codec_type_;
90 const int expected_frames_count_;
91
92 Mutex lock_;
93 int received_frames_count_ RTC_GUARDED_BY(lock_) = 0;
94 rtc::Event expected_frames_count_received_;
95 };
96
97 class IvfVideoFrameGeneratorTest : public ::testing::Test {
98 protected:
SetUp()99 void SetUp() override {
100 file_name_ =
101 webrtc::test::TempFilename(webrtc::test::OutputPath(), "test_file.ivf");
102 }
TearDown()103 void TearDown() override { webrtc::test::RemoveFile(file_name_); }
104
BuildFrame(FrameGeneratorInterface::VideoFrameData frame_data)105 VideoFrame BuildFrame(FrameGeneratorInterface::VideoFrameData frame_data) {
106 return VideoFrame::Builder()
107 .set_video_frame_buffer(frame_data.buffer)
108 .set_update_rect(frame_data.update_rect)
109 .build();
110 }
111
CreateTestVideoFile(VideoCodecType video_codec_type,std::unique_ptr<VideoEncoder> video_encoder)112 void CreateTestVideoFile(VideoCodecType video_codec_type,
113 std::unique_ptr<VideoEncoder> video_encoder) {
114 std::unique_ptr<test::FrameGeneratorInterface> frame_generator =
115 test::CreateSquareFrameGenerator(
116 kWidth, kHeight, test::FrameGeneratorInterface::OutputType::kI420,
117 absl::nullopt);
118
119 VideoCodec codec_settings;
120 webrtc::test::CodecSettings(video_codec_type, &codec_settings);
121 codec_settings.width = kWidth;
122 codec_settings.height = kHeight;
123 codec_settings.maxFramerate = kMaxFramerate;
124 const uint32_t kBitrateBps = 500000;
125 VideoBitrateAllocation bitrate_allocation;
126 bitrate_allocation.SetBitrate(0, 0, kBitrateBps);
127
128 IvfFileWriterEncodedCallback ivf_writer_callback(
129 file_name_, video_codec_type, kVideoFramesCount);
130
131 video_encoder->RegisterEncodeCompleteCallback(&ivf_writer_callback);
132 video_encoder->SetRates(VideoEncoder::RateControlParameters(
133 bitrate_allocation, static_cast<double>(codec_settings.maxFramerate)));
134 ASSERT_EQ(WEBRTC_VIDEO_CODEC_OK,
135 video_encoder->InitEncode(
136 &codec_settings,
137 VideoEncoder::Settings(kCapabilities, /*number_of_cores=*/1,
138 /*max_payload_size=*/0)));
139
140 uint32_t last_frame_timestamp = 0;
141
142 for (int i = 0; i < kVideoFramesCount; ++i) {
143 VideoFrame frame = BuildFrame(frame_generator->NextFrame());
144 const uint32_t timestamp =
145 last_frame_timestamp +
146 kVideoPayloadTypeFrequency / codec_settings.maxFramerate;
147 frame.set_timestamp(timestamp);
148
149 last_frame_timestamp = timestamp;
150
151 ASSERT_EQ(WEBRTC_VIDEO_CODEC_OK, video_encoder->Encode(frame, nullptr));
152 video_frames_.push_back(frame);
153 }
154
155 ASSERT_TRUE(ivf_writer_callback.WaitForExpectedFramesReceived(
156 kMaxFrameEncodeWaitTimeoutMs));
157 }
158
159 std::string file_name_;
160 std::vector<VideoFrame> video_frames_;
161 };
162
163 } // namespace
164
TEST_F(IvfVideoFrameGeneratorTest,Vp8)165 TEST_F(IvfVideoFrameGeneratorTest, Vp8) {
166 CreateTestVideoFile(VideoCodecType::kVideoCodecVP8, VP8Encoder::Create());
167 IvfVideoFrameGenerator generator(file_name_);
168 for (size_t i = 0; i < video_frames_.size(); ++i) {
169 auto& expected_frame = video_frames_[i];
170 VideoFrame actual_frame = BuildFrame(generator.NextFrame());
171 EXPECT_GT(I420PSNR(&expected_frame, &actual_frame), kExpectedMinPsnr);
172 }
173 }
174
TEST_F(IvfVideoFrameGeneratorTest,Vp8DoubleRead)175 TEST_F(IvfVideoFrameGeneratorTest, Vp8DoubleRead) {
176 CreateTestVideoFile(VideoCodecType::kVideoCodecVP8, VP8Encoder::Create());
177 IvfVideoFrameGenerator generator(file_name_);
178 for (size_t i = 0; i < video_frames_.size() * 2; ++i) {
179 auto& expected_frame = video_frames_[i % video_frames_.size()];
180 VideoFrame actual_frame = BuildFrame(generator.NextFrame());
181 EXPECT_GT(I420PSNR(&expected_frame, &actual_frame), kExpectedMinPsnr);
182 }
183 }
184
TEST_F(IvfVideoFrameGeneratorTest,Vp9)185 TEST_F(IvfVideoFrameGeneratorTest, Vp9) {
186 CreateTestVideoFile(VideoCodecType::kVideoCodecVP9, VP9Encoder::Create());
187 IvfVideoFrameGenerator generator(file_name_);
188 for (size_t i = 0; i < video_frames_.size(); ++i) {
189 auto& expected_frame = video_frames_[i];
190 VideoFrame actual_frame = BuildFrame(generator.NextFrame());
191 EXPECT_GT(I420PSNR(&expected_frame, &actual_frame), kExpectedMinPsnr);
192 }
193 }
194
195 #if defined(WEBRTC_USE_H264)
TEST_F(IvfVideoFrameGeneratorTest,H264)196 TEST_F(IvfVideoFrameGeneratorTest, H264) {
197 CreateTestVideoFile(
198 VideoCodecType::kVideoCodecH264,
199 H264Encoder::Create(cricket::VideoCodec(cricket::kH264CodecName)));
200 IvfVideoFrameGenerator generator(file_name_);
201 for (size_t i = 0; i < video_frames_.size(); ++i) {
202 auto& expected_frame = video_frames_[i];
203 VideoFrame actual_frame = BuildFrame(generator.NextFrame());
204 EXPECT_GT(I420PSNR(&expected_frame, &actual_frame), kExpectedMinPsnr);
205 }
206 }
207 #endif
208
209 } // namespace test
210 } // namespace webrtc
211