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