xref: /aosp_15_r20/external/webrtc/modules/video_coding/codecs/test/video_codec_unittest.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 
47    private:
48     VideoCodecUnitTest* const test_;
49   };
50 
51   class FakeDecodeCompleteCallback : public webrtc::DecodedImageCallback {
52    public:
FakeDecodeCompleteCallback(VideoCodecUnitTest * test)53     explicit FakeDecodeCompleteCallback(VideoCodecUnitTest* test)
54         : test_(test) {}
55 
Decoded(VideoFrame & frame)56     int32_t Decoded(VideoFrame& frame) override {
57       RTC_DCHECK_NOTREACHED();
58       return -1;
59     }
Decoded(VideoFrame & frame,int64_t decode_time_ms)60     int32_t Decoded(VideoFrame& frame, int64_t decode_time_ms) override {
61       RTC_DCHECK_NOTREACHED();
62       return -1;
63     }
64     void Decoded(VideoFrame& frame,
65                  absl::optional<int32_t> decode_time_ms,
66                  absl::optional<uint8_t> qp) override;
67 
68    private:
69     VideoCodecUnitTest* const test_;
70   };
71 
72   virtual std::unique_ptr<VideoEncoder> CreateEncoder() = 0;
73   virtual std::unique_ptr<VideoDecoder> CreateDecoder() = 0;
74 
75   void SetUp() override;
76 
77   virtual void ModifyCodecSettings(VideoCodec* codec_settings);
78 
79   VideoFrame NextInputFrame();
80 
81   // Helper method for waiting a single encoded frame.
82   bool WaitForEncodedFrame(EncodedImage* frame,
83                            CodecSpecificInfo* codec_specific_info);
84 
85   // Helper methods for waiting for multiple encoded frames. Caller must
86   // define how many frames are to be waited for via `num_frames` before calling
87   // Encode(). Then, they can expect to retrive them via WaitForEncodedFrames().
88   void SetWaitForEncodedFramesThreshold(size_t num_frames);
89   bool WaitForEncodedFrames(
90       std::vector<EncodedImage>* frames,
91       std::vector<CodecSpecificInfo>* codec_specific_info);
92 
93   // Helper method for waiting a single decoded frame.
94   bool WaitForDecodedFrame(std::unique_ptr<VideoFrame>* frame,
95                            absl::optional<uint8_t>* qp);
96 
97   size_t GetNumEncodedFrames();
98 
99   VideoCodec codec_settings_;
100 
101   std::unique_ptr<VideoEncoder> encoder_;
102   std::unique_ptr<VideoDecoder> decoder_;
103   std::unique_ptr<test::FrameGeneratorInterface> input_frame_generator_;
104 
105  private:
106   FakeEncodeCompleteCallback encode_complete_callback_;
107   FakeDecodeCompleteCallback decode_complete_callback_;
108 
109   rtc::Event encoded_frame_event_;
110   Mutex encoded_frame_section_;
111   size_t wait_for_encoded_frames_threshold_;
112   std::vector<EncodedImage> encoded_frames_
113       RTC_GUARDED_BY(encoded_frame_section_);
114   std::vector<CodecSpecificInfo> codec_specific_infos_
115       RTC_GUARDED_BY(encoded_frame_section_);
116 
117   rtc::Event decoded_frame_event_;
118   Mutex decoded_frame_section_;
119   absl::optional<VideoFrame> decoded_frame_
120       RTC_GUARDED_BY(decoded_frame_section_);
121   absl::optional<uint8_t> decoded_qp_ RTC_GUARDED_BY(decoded_frame_section_);
122 
123   uint32_t last_input_frame_timestamp_;
124 };
125 
126 }  // namespace webrtc
127 
128 #endif  // MODULES_VIDEO_CODING_CODECS_TEST_VIDEO_CODEC_UNITTEST_H_
129