videoprocessor_unittest.cc (6912B)
1 /* 2 * Copyright (c) 2012 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/codecs/test/videoprocessor.h" 12 13 #include <cstdint> 14 #include <memory> 15 #include <vector> 16 17 #include "api/environment/environment_factory.h" 18 #include "api/test/mock_video_decoder.h" 19 #include "api/test/mock_video_encoder.h" 20 #include "api/test/videocodec_test_fixture.h" 21 #include "api/video/i420_buffer.h" 22 #include "api/video/video_frame.h" 23 #include "api/video_codecs/video_decoder.h" 24 #include "api/video_codecs/video_encoder.h" 25 #include "media/base/media_constants.h" 26 #include "modules/video_coding/codecs/test/videocodec_test_stats_impl.h" 27 #include "rtc_base/task_queue_for_test.h" 28 #include "test/gmock.h" 29 #include "test/gtest.h" 30 #include "test/testsupport/mock/mock_frame_reader.h" 31 32 using ::testing::_; 33 using ::testing::AllOf; 34 using ::testing::Field; 35 using ::testing::Property; 36 using ::testing::ResultOf; 37 using ::testing::Return; 38 39 namespace webrtc { 40 namespace test { 41 42 namespace { 43 44 constexpr int kWidth = 352; 45 constexpr int kHeight = 288; 46 47 } // namespace 48 49 class VideoProcessorTest : public ::testing::Test { 50 protected: 51 VideoProcessorTest() : q_("VP queue") { 52 config_.SetCodecSettings(kVp8CodecName, 1, 1, 1, false, false, false, 53 kWidth, kHeight); 54 55 decoder_mock_ = new MockVideoDecoder(); 56 decoders_.push_back(std::unique_ptr<VideoDecoder>(decoder_mock_)); 57 58 ExpectInit(); 59 q_.SendTask([this] { 60 video_processor_ = std::make_unique<VideoProcessor>( 61 CreateEnvironment(), &encoder_mock_, &decoders_, &frame_reader_mock_, 62 config_, &stats_, &encoded_frame_writers_, 63 /*decoded_frame_writers=*/nullptr); 64 }); 65 } 66 67 ~VideoProcessorTest() override { 68 q_.SendTask([this] { video_processor_.reset(); }); 69 } 70 71 void ExpectInit() { 72 EXPECT_CALL(encoder_mock_, InitEncode(_, _)); 73 EXPECT_CALL(encoder_mock_, RegisterEncodeCompleteCallback); 74 EXPECT_CALL(*decoder_mock_, Configure); 75 EXPECT_CALL(*decoder_mock_, RegisterDecodeCompleteCallback); 76 } 77 78 void ExpectRelease() { 79 EXPECT_CALL(encoder_mock_, Release()).Times(1); 80 EXPECT_CALL(encoder_mock_, RegisterEncodeCompleteCallback(_)).Times(1); 81 EXPECT_CALL(*decoder_mock_, Release()).Times(1); 82 EXPECT_CALL(*decoder_mock_, RegisterDecodeCompleteCallback(_)).Times(1); 83 } 84 85 TaskQueueForTest q_; 86 87 VideoCodecTestFixture::Config config_; 88 89 MockVideoEncoder encoder_mock_; 90 MockVideoDecoder* decoder_mock_; 91 std::vector<std::unique_ptr<VideoDecoder>> decoders_; 92 MockFrameReader frame_reader_mock_; 93 VideoCodecTestStatsImpl stats_; 94 VideoProcessor::IvfFileWriterMap encoded_frame_writers_; 95 std::unique_ptr<VideoProcessor> video_processor_; 96 }; 97 98 TEST_F(VideoProcessorTest, InitRelease) { 99 ExpectRelease(); 100 } 101 102 TEST_F(VideoProcessorTest, ProcessFrames_FixedFramerate) { 103 const int kBitrateKbps = 456; 104 const int kFramerateFps = 31; 105 EXPECT_CALL( 106 encoder_mock_, 107 SetRates(Field(&VideoEncoder::RateControlParameters::framerate_fps, 108 static_cast<double>(kFramerateFps)))) 109 .Times(1); 110 q_.SendTask( 111 [this] { video_processor_->SetRates(kBitrateKbps, kFramerateFps); }); 112 113 EXPECT_CALL(frame_reader_mock_, PullFrame(_, _, _)) 114 .WillRepeatedly(Return(I420Buffer::Create(kWidth, kHeight))); 115 EXPECT_CALL(encoder_mock_, Encode(Property(&VideoFrame::rtp_timestamp, 116 1 * 90000 / kFramerateFps), 117 _)) 118 .Times(1); 119 q_.SendTask([this] { video_processor_->ProcessFrame(); }); 120 121 EXPECT_CALL(encoder_mock_, Encode(Property(&VideoFrame::rtp_timestamp, 122 2 * 90000 / kFramerateFps), 123 _)) 124 .Times(1); 125 q_.SendTask([this] { video_processor_->ProcessFrame(); }); 126 127 ExpectRelease(); 128 } 129 130 TEST_F(VideoProcessorTest, ProcessFrames_VariableFramerate) { 131 const int kBitrateKbps = 456; 132 const int kStartFramerateFps = 27; 133 const int kStartTimestamp = 90000 / kStartFramerateFps; 134 EXPECT_CALL( 135 encoder_mock_, 136 SetRates(Field(&VideoEncoder::RateControlParameters::framerate_fps, 137 static_cast<double>(kStartFramerateFps)))) 138 .Times(1); 139 q_.SendTask( 140 [this] { video_processor_->SetRates(kBitrateKbps, kStartFramerateFps); }); 141 142 EXPECT_CALL(frame_reader_mock_, PullFrame(_, _, _)) 143 .WillRepeatedly(Return(I420Buffer::Create(kWidth, kHeight))); 144 EXPECT_CALL(encoder_mock_, 145 Encode(Property(&VideoFrame::rtp_timestamp, kStartTimestamp), _)) 146 .Times(1); 147 q_.SendTask([this] { video_processor_->ProcessFrame(); }); 148 149 const int kNewFramerateFps = 13; 150 EXPECT_CALL( 151 encoder_mock_, 152 SetRates(Field(&VideoEncoder::RateControlParameters::framerate_fps, 153 static_cast<double>(kNewFramerateFps)))) 154 .Times(1); 155 q_.SendTask( 156 [this] { video_processor_->SetRates(kBitrateKbps, kNewFramerateFps); }); 157 158 EXPECT_CALL(encoder_mock_, 159 Encode(Property(&VideoFrame::rtp_timestamp, 160 kStartTimestamp + 90000 / kNewFramerateFps), 161 _)) 162 .Times(1); 163 q_.SendTask([this] { video_processor_->ProcessFrame(); }); 164 165 ExpectRelease(); 166 } 167 168 TEST_F(VideoProcessorTest, SetRates) { 169 const uint32_t kBitrateKbps = 123; 170 const int kFramerateFps = 17; 171 172 EXPECT_CALL( 173 encoder_mock_, 174 SetRates(AllOf(ResultOf( 175 [](const VideoEncoder::RateControlParameters& params) { 176 return params.bitrate.get_sum_kbps(); 177 }, 178 kBitrateKbps), 179 Field(&VideoEncoder::RateControlParameters::framerate_fps, 180 static_cast<double>(kFramerateFps))))) 181 .Times(1); 182 q_.SendTask( 183 [this] { video_processor_->SetRates(kBitrateKbps, kFramerateFps); }); 184 185 const uint32_t kNewBitrateKbps = 456; 186 const int kNewFramerateFps = 34; 187 EXPECT_CALL( 188 encoder_mock_, 189 SetRates(AllOf(ResultOf( 190 [](const VideoEncoder::RateControlParameters& params) { 191 return params.bitrate.get_sum_kbps(); 192 }, 193 kNewBitrateKbps), 194 Field(&VideoEncoder::RateControlParameters::framerate_fps, 195 static_cast<double>(kNewFramerateFps))))) 196 .Times(1); 197 q_.SendTask([this] { 198 video_processor_->SetRates(kNewBitrateKbps, kNewFramerateFps); 199 }); 200 201 ExpectRelease(); 202 } 203 204 } // namespace test 205 } // namespace webrtc