• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef AVCODEC_MOCK_H
17 #define AVCODEC_MOCK_H
18 
19 #include <string>
20 #include "enum_mock.h"
21 #include "nocopyable.h"
22 
23 namespace OHOS {
24 namespace Media {
25 enum AVCodecTypeMock : int32_t {
26     AVCODEC_TYPE_MOCK_NONE = -1,
27     AVCODEC_TYPE_MOCK_VIDEO_ENCODER = 0,
28     AVCODEC_TYPE_MOCK_VIDEO_DECODER,
29     AVCODEC_TYPE_MOCK_AUDIO_ENCODER,
30     AVCODEC_TYPE_MOCK_AUDIO_DECODER,
31 };
32 
33 struct RangeMock {
34     int32_t minVal = 0;
35     int32_t maxVal = 0;
36 };
37 
38 class SurfaceMock : public NoCopyable {
39 public:
40     virtual ~SurfaceMock() = default;
41 };
42 
43 class FormatMock : public NoCopyable {
44 public:
45     virtual ~FormatMock() = default;
46     virtual bool PutIntValue(const std::string_view &key, int32_t value) = 0;
47     virtual bool GetIntValue(const std::string_view &key, int32_t &value) = 0;
48     virtual bool PutLongValue(const std::string_view &key, int64_t value) = 0;
49     virtual bool GetLongValue(const std::string_view &key, int64_t &value) = 0;
50     virtual bool PutFloatValue(const std::string_view &key, float value) = 0;
51     virtual bool GetFloatValue(const std::string_view &key, float &value) = 0;
52     virtual bool PutDoubleValue(const std::string_view &key, double value) = 0;
53     virtual bool GetDoubleValue(const std::string_view &key, double &value) = 0;
54     virtual bool PutStringValue(const std::string_view &key, const std::string_view &value) = 0;
55     virtual bool GetStringValue(const std::string_view &key, std::string &value) = 0;
56     virtual bool GetBuffer(const std::string_view &key, uint8_t **addr, size_t &size) = 0;
57     virtual bool PutBuffer(const std::string_view &key, const uint8_t *addr, size_t size) = 0;
58     virtual const char *DumpInfo() = 0;
59     virtual void Destroy() = 0;
60 };
61 
62 class AVMemoryMock : public NoCopyable {
63 public:
64     virtual ~AVMemoryMock() = default;
65     virtual uint8_t *GetAddr() const = 0;
66     virtual int32_t GetSize() const = 0;
67     virtual uint32_t GetFlags() const = 0;
68 };
69 
70 class AVCodecInfoMock : public NoCopyable {
71 public:
72     virtual ~AVCodecInfoMock() = default;
73     virtual std::string GetName() const = 0;
74     virtual int32_t GetType() const = 0;
75     virtual std::string GetMimeType() const = 0;
76     virtual bool IsHardwareAccelerated() const = 0;
77     virtual bool IsSoftwareOnly() const = 0;
78     virtual bool IsVendor() const = 0;
79 };
80 
81 class VideoCapsMock : public NoCopyable {
82 public:
83     virtual ~VideoCapsMock() = default;
84     virtual std::shared_ptr<AVCodecInfoMock> GetCodecInfo() const = 0;
85     virtual RangeMock GetSupportedBitrate() const = 0;
86     virtual std::vector<int32_t> GetSupportedFormats() const = 0;
87     virtual int32_t GetSupportedHeightAlignment() const = 0;
88     virtual int32_t GetSupportedWidthAlignment() const = 0;
89     virtual RangeMock GetSupportedWidth() const = 0;
90     virtual RangeMock GetSupportedHeight() const = 0;
91     virtual std::vector<int32_t> GetSupportedProfiles() const = 0;
92     virtual std::vector<int32_t> GetSupportedLevels() const = 0;
93     virtual RangeMock GetSupportedEncodeQuality() const = 0;
94     virtual bool IsSizeSupported(int32_t width, int32_t height) const = 0;
95     virtual RangeMock GetSupportedFrameRate() const = 0;
96     virtual RangeMock GetSupportedFrameRatesFor(int32_t width, int32_t height) const = 0;
97     virtual bool IsSizeAndRateSupported(int32_t width, int32_t height, double frameRate) const = 0;
98     virtual RangeMock GetPreferredFrameRate(int32_t width, int32_t height) const = 0;
99     virtual std::vector<int32_t> GetSupportedBitrateMode() const = 0;
100     virtual RangeMock GetSupportedQuality() const = 0;
101     virtual RangeMock GetSupportedComplexity() const = 0;
102     virtual bool IsSupportDynamicIframe() const = 0;
103 };
104 
105 class AudioCapsMock : public NoCopyable {
106 public:
107     virtual ~AudioCapsMock() = default;
108     virtual std::shared_ptr<AVCodecInfoMock> GetCodecInfo() const = 0;
109     virtual RangeMock GetSupportedBitrate() const = 0;
110     virtual RangeMock GetSupportedChannel() const = 0;
111     virtual std::vector<int32_t> GetSupportedFormats() const = 0;
112     virtual std::vector<int32_t> GetSupportedSampleRates() const = 0;
113     virtual std::vector<int32_t> GetSupportedProfiles() const = 0;
114     virtual std::vector<int32_t> GetSupportedLevels() const = 0;
115     virtual RangeMock GetSupportedComplexity() const = 0;
116 };
117 
118 class AVCodecListMock : public NoCopyable {
119 public:
120     virtual ~AVCodecListMock() = default;
121     virtual std::string FindVideoDecoder(std::shared_ptr<FormatMock> format) const = 0;
122     virtual std::string FindVideoEncoder(std::shared_ptr<FormatMock> format) const = 0;
123     virtual std::string FindAudioDecoder(std::shared_ptr<FormatMock> format) const = 0;
124     virtual std::string FindAudioEncoder(std::shared_ptr<FormatMock> format) const = 0;
125     virtual std::vector<std::shared_ptr<VideoCapsMock>> GetVideoDecoderCaps() const = 0;
126     virtual std::vector<std::shared_ptr<VideoCapsMock>> GetVideoEncoderCaps() const = 0;
127     virtual std::vector<std::shared_ptr<AudioCapsMock>> GetAudioDecoderCaps() const = 0;
128     virtual std::vector<std::shared_ptr<AudioCapsMock>> GetAudioEncoderCaps() const = 0;
129 };
130 
131 struct AVCodecBufferAttrMock {
132     int64_t pts = 0;
133     int32_t size = 0;
134     int32_t offset = 0;
135     uint32_t flags = 0;
136 };
137 
138 class AVCodecCallbackMock : public NoCopyable {
139 public:
140     virtual ~AVCodecCallbackMock() = default;
141     virtual void OnError(int32_t errorCode) = 0;
142     virtual void OnStreamChanged(std::shared_ptr<FormatMock> format) = 0;
143     virtual void OnNeedInputData(uint32_t index, std::shared_ptr<AVMemoryMock> data) = 0;
144     virtual void OnNewOutputData(uint32_t index, std::shared_ptr<AVMemoryMock> data, AVCodecBufferAttrMock attr) = 0;
145 };
146 
147 class VideoDecMock : public NoCopyable {
148 public:
149     virtual ~VideoDecMock() = default;
150     virtual int32_t SetCallback(std::shared_ptr<AVCodecCallbackMock> cb) = 0;
151     virtual int32_t SetOutputSurface(std::shared_ptr<SurfaceMock> surface) = 0;
152     virtual int32_t Configure(std::shared_ptr<FormatMock> format) = 0;
153     virtual int32_t Prepare() = 0;
154     virtual int32_t Start() = 0;
155     virtual int32_t Stop() = 0;
156     virtual int32_t Flush() = 0;
157     virtual int32_t Reset() = 0;
158     virtual int32_t Release() = 0;
159     virtual std::shared_ptr<FormatMock> GetOutputMediaDescription() = 0;
160     virtual int32_t SetParameter(std::shared_ptr<FormatMock> format) = 0;
161     virtual int32_t PushInputData(uint32_t index, AVCodecBufferAttrMock &attr) = 0;
162     virtual int32_t RenderOutputData(uint32_t index) = 0;
163     virtual int32_t FreeOutputData(uint32_t index) = 0;
164 };
165 
166 class VideoEncMock : public NoCopyable {
167 public:
168     virtual ~VideoEncMock() = default;
169     virtual int32_t SetCallback(std::shared_ptr<AVCodecCallbackMock> cb) = 0;
170     virtual std::shared_ptr<SurfaceMock> GetInputSurface() = 0;
171     virtual int32_t Configure(std::shared_ptr<FormatMock> format) = 0;
172     virtual int32_t Prepare() = 0;
173     virtual int32_t Start() = 0;
174     virtual int32_t Stop() = 0;
175     virtual int32_t Flush() = 0;
176     virtual int32_t Reset() = 0;
177     virtual int32_t Release() = 0;
178     virtual int32_t NotifyEos() = 0;
179     virtual std::shared_ptr<FormatMock> GetOutputMediaDescription() = 0;
180     virtual int32_t SetParameter(std::shared_ptr<FormatMock> format) = 0;
181     virtual int32_t FreeOutputData(uint32_t index) = 0;
182 };
183 
184 class AudioDecMock : public NoCopyable {
185 public:
186     virtual ~AudioDecMock() = default;
187     virtual int32_t SetCallback(std::shared_ptr<AVCodecCallbackMock> cb) = 0;
188     virtual int32_t Configure(std::shared_ptr<FormatMock> format) = 0;
189     virtual int32_t Prepare() = 0;
190     virtual int32_t Start() = 0;
191     virtual int32_t Stop() = 0;
192     virtual int32_t Flush() = 0;
193     virtual int32_t Reset() = 0;
194     virtual int32_t Release() = 0;
195     virtual int32_t PushInputData(uint32_t index, AVCodecBufferAttrMock &attr) = 0;
196     virtual std::shared_ptr<FormatMock> GetOutputMediaDescription() = 0;
197     virtual int32_t SetParameter(std::shared_ptr<FormatMock> format) = 0;
198     virtual int32_t FreeOutputData(uint32_t index) = 0;
199 };
200 
201 class AudioEncMock : public NoCopyable {
202 public:
203     virtual ~AudioEncMock() = default;
204     virtual int32_t SetCallback(std::shared_ptr<AVCodecCallbackMock> cb) = 0;
205     virtual int32_t Configure(std::shared_ptr<FormatMock> format) = 0;
206     virtual int32_t Prepare() = 0;
207     virtual int32_t Start() = 0;
208     virtual int32_t Stop() = 0;
209     virtual int32_t Flush() = 0;
210     virtual int32_t Reset() = 0;
211     virtual int32_t Release() = 0;
212     virtual int32_t PushInputData(uint32_t index, AVCodecBufferAttrMock &attr) = 0;
213     virtual std::shared_ptr<FormatMock> GetOutputMediaDescription() = 0;
214     virtual int32_t SetParameter(std::shared_ptr<FormatMock> format) = 0;
215     virtual int32_t FreeOutputData(uint32_t index) = 0;
216 };
217 
218 class __attribute__((visibility("default"))) AVCodecMockFactory {
219 public:
220     static std::shared_ptr<VideoDecMock> CreateVideoDecMockByMime(const std::string &mime);
221     static std::shared_ptr<VideoDecMock> CreateVideoDecMockByName(const std::string &name);
222     static std::shared_ptr<VideoEncMock> CreateVideoEncMockByMime(const std::string &mime);
223     static std::shared_ptr<VideoEncMock> CreateVideoEncMockByName(const std::string &name);
224     static std::shared_ptr<AudioDecMock> CreateAudioDecMockByMime(const std::string &mime);
225     static std::shared_ptr<AudioDecMock> CreateAudioDecMockByName(const std::string &name);
226     static std::shared_ptr<AudioEncMock> CreateAudioEncMockByMime(const std::string &mime);
227     static std::shared_ptr<AudioEncMock> CreateAudioEncMockByName(const std::string &name);
228     static std::shared_ptr<FormatMock> CreateFormat();
229     static std::shared_ptr<SurfaceMock> CreateSurface();
230     static std::shared_ptr<AVCodecInfoMock> CreateAVCodecInfo();
231     static std::shared_ptr<AVCodecListMock> CreateAVCodecList();
232     static std::shared_ptr<VideoCapsMock> CreateVideoCaps();
233     static std::shared_ptr<EnumMock> CreateEnum();
234 private:
235     AVCodecMockFactory() = delete;
236     ~AVCodecMockFactory() = delete;
237 };
238 } // Media
239 } // OHOS
240 #endif // AVCODEC_MOCK_H