video_codec_unittest.h (4752B)
1 /* 2 * Copyright (c) 2017 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 #ifndef MODULES_VIDEO_CODING_CODECS_TEST_VIDEO_CODEC_UNITTEST_H_ 12 #define MODULES_VIDEO_CODING_CODECS_TEST_VIDEO_CODEC_UNITTEST_H_ 13 14 #include <cstddef> 15 #include <cstdint> 16 #include <memory> 17 #include <optional> 18 #include <vector> 19 20 #include "api/environment/environment.h" 21 #include "api/environment/environment_factory.h" 22 #include "api/field_trials.h" 23 #include "api/test/frame_generator_interface.h" 24 #include "api/video/encoded_image.h" 25 #include "api/video/video_frame.h" 26 #include "api/video_codecs/video_codec.h" 27 #include "api/video_codecs/video_decoder.h" 28 #include "api/video_codecs/video_encoder.h" 29 #include "modules/video_coding/include/video_codec_interface.h" 30 #include "rtc_base/checks.h" 31 #include "rtc_base/event.h" 32 #include "rtc_base/synchronization/mutex.h" 33 #include "rtc_base/thread_annotations.h" 34 #include "test/create_test_field_trials.h" 35 #include "test/gtest.h" 36 37 namespace webrtc { 38 39 class VideoCodecUnitTest : public ::testing::Test { 40 public: 41 VideoCodecUnitTest() 42 : field_trials_(CreateTestFieldTrials()), 43 env_(CreateEnvironment(field_trials_.CreateCopy())), 44 encode_complete_callback_(this), 45 decode_complete_callback_(this), 46 wait_for_encoded_frames_threshold_(1), 47 last_input_frame_timestamp_(0) {} 48 49 protected: 50 class FakeEncodeCompleteCallback : public EncodedImageCallback { 51 public: 52 explicit FakeEncodeCompleteCallback(VideoCodecUnitTest* test) 53 : test_(test) {} 54 55 Result OnEncodedImage(const EncodedImage& frame, 56 const CodecSpecificInfo* codec_specific_info); 57 58 private: 59 VideoCodecUnitTest* const test_; 60 }; 61 62 class FakeDecodeCompleteCallback : public DecodedImageCallback { 63 public: 64 explicit FakeDecodeCompleteCallback(VideoCodecUnitTest* test) 65 : test_(test) {} 66 67 int32_t Decoded(VideoFrame& /* frame */) override { 68 RTC_DCHECK_NOTREACHED(); 69 return -1; 70 } 71 int32_t Decoded(VideoFrame& /* frame */, 72 int64_t /* decode_time_ms */) override { 73 RTC_DCHECK_NOTREACHED(); 74 return -1; 75 } 76 void Decoded(VideoFrame& frame, 77 std::optional<int32_t> decode_time_ms, 78 std::optional<uint8_t> qp) override; 79 80 private: 81 VideoCodecUnitTest* const test_; 82 }; 83 84 virtual std::unique_ptr<VideoEncoder> CreateEncoder() = 0; 85 virtual std::unique_ptr<VideoDecoder> CreateDecoder() = 0; 86 87 void SetUp() override; 88 89 virtual void ModifyCodecSettings(VideoCodec* codec_settings); 90 91 VideoFrame NextInputFrame(); 92 93 // Helper method for waiting a single encoded frame. 94 bool WaitForEncodedFrame(EncodedImage* frame, 95 CodecSpecificInfo* codec_specific_info); 96 97 // Helper methods for waiting for multiple encoded frames. Caller must 98 // define how many frames are to be waited for via `num_frames` before calling 99 // Encode(). Then, they can expect to retrive them via WaitForEncodedFrames(). 100 void SetWaitForEncodedFramesThreshold(size_t num_frames); 101 bool WaitForEncodedFrames( 102 std::vector<EncodedImage>* frames, 103 std::vector<CodecSpecificInfo>* codec_specific_info); 104 105 // Helper method for waiting a single decoded frame. 106 bool WaitForDecodedFrame(std::unique_ptr<VideoFrame>* frame, 107 std::optional<uint8_t>* qp); 108 109 size_t GetNumEncodedFrames(); 110 111 FieldTrials field_trials_; 112 Environment env_; 113 VideoCodec codec_settings_; 114 115 std::unique_ptr<VideoEncoder> encoder_; 116 std::unique_ptr<VideoDecoder> decoder_; 117 std::unique_ptr<test::FrameGeneratorInterface> input_frame_generator_; 118 119 private: 120 FakeEncodeCompleteCallback encode_complete_callback_; 121 FakeDecodeCompleteCallback decode_complete_callback_; 122 123 Event encoded_frame_event_; 124 Mutex encoded_frame_section_; 125 size_t wait_for_encoded_frames_threshold_; 126 std::vector<EncodedImage> encoded_frames_ 127 RTC_GUARDED_BY(encoded_frame_section_); 128 std::vector<CodecSpecificInfo> codec_specific_infos_ 129 RTC_GUARDED_BY(encoded_frame_section_); 130 131 Event decoded_frame_event_; 132 Mutex decoded_frame_section_; 133 std::optional<VideoFrame> decoded_frame_ 134 RTC_GUARDED_BY(decoded_frame_section_); 135 std::optional<uint8_t> decoded_qp_ RTC_GUARDED_BY(decoded_frame_section_); 136 137 uint32_t last_input_frame_timestamp_; 138 }; 139 140 } // namespace webrtc 141 142 #endif // MODULES_VIDEO_CODING_CODECS_TEST_VIDEO_CODEC_UNITTEST_H_