1 /*
2  * Copyright (C) 2023 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 #include "codeclist_capi_mock.h"
17 
18 using namespace OHOS::MediaAVCodec::CodecListTestParam;
19 
20 namespace OHOS {
21 namespace MediaAVCodec {
IsHardware()22 bool CodecListCapiMock::IsHardware()
23 {
24     if (codeclist_ != nullptr) {
25         return OH_AVCapability_IsHardware(codeclist_);
26     }
27     std::cout << "codeclist_ is nullptr" << std::endl;
28     return false;
29 }
30 
StackMemOverWrite()31 void StackMemOverWrite()
32 {
33     for (int i = 0; i < STACK_BUF_OVERWRITE_TIMES; i++) {
34         std::vector<int32_t> vectorTest((i % STACK_BUF_OVERWRITE_MAX_BUF_SIZE) + 1, 0);
35         if (i % STACK_BUF_OVERWRITE_MAX_BUF_SIZE == 0) {
36             std::cout << "StackMemOverWrite addr " << vectorTest.data() << "\r";
37         }
38     }
39     std::cout << "\n";
40 }
41 
GetName()42 std::string CodecListCapiMock::GetName()
43 {
44     if (codeclist_ != nullptr) {
45         const char *tmp = OH_AVCapability_GetName(codeclist_);
46 
47         StackMemOverWrite();
48         std::string tmpStr = tmp;
49         return tmpStr;
50     }
51     std::cout << "codeclist_ is nullptr" << std::endl;
52     return "";
53 }
54 
GetMaxSupportedInstances()55 int32_t CodecListCapiMock::GetMaxSupportedInstances()
56 {
57     if (codeclist_ != nullptr) {
58         return OH_AVCapability_GetMaxSupportedInstances(codeclist_);
59     }
60     std::cout << "codeclist_ is nullptr" << std::endl;
61     return 0;
62 }
63 
GetEncoderBitrateRange()64 Range CodecListCapiMock::GetEncoderBitrateRange()
65 {
66     Range retRange(0, 0);
67     if (codeclist_ != nullptr) {
68         OH_AVRange range;
69         int32_t ret = OH_AVCapability_GetEncoderBitrateRange(codeclist_, &range);
70         if (ret != AV_ERR_OK) {
71             std::cout << "OH_AVCapability_GetEncoderBitrateRange returns error: " << ret << std::endl;
72             return retRange;
73         }
74         retRange.minVal = range.minVal;
75         retRange.maxVal = range.maxVal;
76         return retRange;
77     }
78     std::cout << "codeclist_ is nullptr" << std::endl;
79     return retRange;
80 }
81 
IsEncoderBitrateModeSupported(OH_BitrateMode bitrateMode)82 bool CodecListCapiMock::IsEncoderBitrateModeSupported(OH_BitrateMode bitrateMode)
83 {
84     if (codeclist_ != nullptr) {
85         return OH_AVCapability_IsEncoderBitrateModeSupported(codeclist_, bitrateMode);
86     }
87     std::cout << "codeclist_ is nullptr" << std::endl;
88     return false;
89 }
90 
GetEncoderQualityRange()91 Range CodecListCapiMock::GetEncoderQualityRange()
92 {
93     Range retRange(0, 0);
94     if (codeclist_ != nullptr) {
95         OH_AVRange range;
96         int32_t ret = OH_AVCapability_GetEncoderQualityRange(codeclist_, &range);
97         if (ret != AV_ERR_OK) {
98             std::cout << "OH_AVCapability_GetEncoderQualityRange returns error: " << ret << std::endl;
99             return retRange;
100         }
101         retRange.minVal = range.minVal;
102         retRange.maxVal = range.maxVal;
103         return retRange;
104     }
105     std::cout << "codeclist_ is nullptr" << std::endl;
106     return retRange;
107 }
108 
GetEncoderComplexityRange()109 Range CodecListCapiMock::GetEncoderComplexityRange()
110 {
111     Range retRange(0, 0);
112     if (codeclist_ != nullptr) {
113         OH_AVRange range;
114         int32_t ret = OH_AVCapability_GetEncoderComplexityRange(codeclist_, &range);
115         if (ret != AV_ERR_OK) {
116             std::cout << "OH_AVCapability_GetEncoderComplexityRange returns error: " << ret << std::endl;
117             return retRange;
118         }
119         retRange.minVal = range.minVal;
120         retRange.maxVal = range.maxVal;
121         return retRange;
122     }
123     std::cout << "codeclist_ is nullptr" << std::endl;
124     return retRange;
125 }
126 
GetAudioSupportedSampleRates()127 std::vector<int32_t> CodecListCapiMock::GetAudioSupportedSampleRates()
128 {
129     if (codeclist_ != nullptr) {
130         const int32_t *sampleRates = nullptr;
131         uint32_t sampleRateNum = 0;
132         int32_t ret = OH_AVCapability_GetAudioSupportedSampleRates(codeclist_, &sampleRates, &sampleRateNum);
133         if (ret != AV_ERR_OK) {
134             std::cout << "OH_AVCapability_GetAudioSupportedSampleRates returns error: " << ret << std::endl;
135             return std::vector<int32_t>();
136         }
137         StackMemOverWrite();
138         std::vector<int32_t> retVector = std::vector<int32_t>(sampleRates, sampleRates + sampleRateNum);
139         std::sort(retVector.begin(), retVector.end());
140         return retVector;
141     }
142     std::cout << "codeclist_ is nullptr" << std::endl;
143     return std::vector<int32_t>();
144 }
145 
GetAudioChannelsRange()146 Range CodecListCapiMock::GetAudioChannelsRange()
147 {
148     Range retRange(0, 0);
149     if (codeclist_ != nullptr) {
150         OH_AVRange range;
151         int32_t ret = OH_AVCapability_GetAudioChannelCountRange(codeclist_, &range);
152         if (ret != AV_ERR_OK) {
153             std::cout << "OH_AVCapability_GetAudioChannelCountRange returns error: " << ret << std::endl;
154             return retRange;
155         }
156         retRange.minVal = range.minVal;
157         retRange.maxVal = range.maxVal;
158         return retRange;
159     }
160     std::cout << "codeclist_ is nullptr" << std::endl;
161     return retRange;
162 }
163 
GetVideoWidthAlignment()164 int32_t CodecListCapiMock::GetVideoWidthAlignment()
165 {
166     if (codeclist_ != nullptr) {
167         int32_t widthAlignment = 0;
168         int32_t ret = OH_AVCapability_GetVideoWidthAlignment(codeclist_, &widthAlignment);
169         if (ret != AV_ERR_OK) {
170             std::cout << "OH_AVCapability_GetVideoWidthAlignment returns error: " << ret << std::endl;
171             return 0;
172         }
173         return widthAlignment;
174     }
175     std::cout << "codeclist_ is nullptr" << std::endl;
176     return 0;
177 }
178 
GetVideoHeightAlignment()179 int32_t CodecListCapiMock::GetVideoHeightAlignment()
180 {
181     if (codeclist_ != nullptr) {
182         int32_t heightAlignment = 0;
183         int32_t ret = OH_AVCapability_GetVideoHeightAlignment(codeclist_, &heightAlignment);
184         if (ret != AV_ERR_OK) {
185             std::cout << "OH_AVCapability_GetVideoHeightAlignment returns error: " << ret << std::endl;
186             return 0;
187         }
188         return heightAlignment;
189     }
190     std::cout << "codeclist_ is nullptr" << std::endl;
191     return 0;
192 }
193 
GetVideoWidthRangeForHeight(int32_t height)194 Range CodecListCapiMock::GetVideoWidthRangeForHeight(int32_t height)
195 {
196     Range retRange(0, 0);
197     if (codeclist_ != nullptr) {
198         OH_AVRange range;
199         int32_t ret = OH_AVCapability_GetVideoWidthRangeForHeight(codeclist_, height, &range);
200         if (ret != AV_ERR_OK) {
201             std::cout << "OH_AVCapability_GetVideoWidthRangeForHeight returns error: " << ret << std::endl;
202             return retRange;
203         }
204         retRange.minVal = range.minVal;
205         retRange.maxVal = range.maxVal;
206         return retRange;
207     }
208     std::cout << "codeclist_ is nullptr" << std::endl;
209     return retRange;
210 }
211 
GetVideoHeightRangeForWidth(int32_t width)212 Range CodecListCapiMock::GetVideoHeightRangeForWidth(int32_t width)
213 {
214     Range retRange(0, 0);
215     if (codeclist_ != nullptr) {
216         OH_AVRange range;
217         int32_t ret = OH_AVCapability_GetVideoHeightRangeForWidth(codeclist_, width, &range);
218         if (ret != AV_ERR_OK) {
219             std::cout << "OH_AVCapability_GetVideoHeightRangeForWidth returns error: " << ret << std::endl;
220             return retRange;
221         }
222         retRange.minVal = range.minVal;
223         retRange.maxVal = range.maxVal;
224         return retRange;
225     }
226     std::cout << "codeclist_ is nullptr" << std::endl;
227     return retRange;
228 }
229 
GetVideoWidthRange()230 Range CodecListCapiMock::GetVideoWidthRange()
231 {
232     Range retRange(0, 0);
233     if (codeclist_ != nullptr) {
234         OH_AVRange range;
235         int32_t ret = OH_AVCapability_GetVideoWidthRange(codeclist_, &range);
236         if (ret != AV_ERR_OK) {
237             std::cout << "OH_AVCapability_GetVideoWidthRange returns error: " << ret << std::endl;
238             return retRange;
239         }
240         retRange.minVal = range.minVal;
241         retRange.maxVal = range.maxVal;
242         return retRange;
243     }
244     std::cout << "codeclist_ is nullptr" << std::endl;
245     return retRange;
246 }
247 
GetVideoHeightRange()248 Range CodecListCapiMock::GetVideoHeightRange()
249 {
250     Range retRange(0, 0);
251     if (codeclist_ != nullptr) {
252         OH_AVRange range;
253         int32_t ret = OH_AVCapability_GetVideoHeightRange(codeclist_, &range);
254         if (ret != AV_ERR_OK) {
255             std::cout << "OH_AVCapability_GetVideoHeightRange returns error: " << ret << std::endl;
256             return retRange;
257         }
258         retRange.minVal = range.minVal;
259         retRange.maxVal = range.maxVal;
260         return retRange;
261     }
262     std::cout << "codeclist_ is nullptr" << std::endl;
263     return retRange;
264 }
265 
IsVideoSizeSupported(int32_t width,int32_t height)266 bool CodecListCapiMock::IsVideoSizeSupported(int32_t width, int32_t height)
267 {
268     if (codeclist_ != nullptr) {
269         return OH_AVCapability_IsVideoSizeSupported(codeclist_, width, height);
270     }
271     std::cout << "codeclist_ is nullptr" << std::endl;
272     return false;
273 }
274 
GetVideoFrameRateRange()275 Range CodecListCapiMock::GetVideoFrameRateRange()
276 {
277     Range retRange(0, 0);
278     if (codeclist_ != nullptr) {
279         OH_AVRange range;
280         int32_t ret = OH_AVCapability_GetVideoFrameRateRange(codeclist_, &range);
281         if (ret != AV_ERR_OK) {
282             std::cout << "OH_AVCapability_GetVideoFrameRateRange returns error: " << ret << std::endl;
283             return retRange;
284         }
285         retRange.minVal = range.minVal;
286         retRange.maxVal = range.maxVal;
287         return retRange;
288     }
289     std::cout << "codeclist_ is nullptr" << std::endl;
290     return retRange;
291 }
292 
GetVideoFrameRateRangeForSize(int32_t width,int32_t height)293 Range CodecListCapiMock::GetVideoFrameRateRangeForSize(int32_t width, int32_t height)
294 {
295     Range retRange(0, 0);
296     if (codeclist_ != nullptr) {
297         OH_AVRange range;
298         int32_t ret = OH_AVCapability_GetVideoFrameRateRangeForSize(codeclist_, width, height, &range);
299         if (ret != AV_ERR_OK) {
300             std::cout << "OH_AVCapability_GetVideoFrameRateRangeForSize returns error: " << ret << std::endl;
301             return retRange;
302         }
303         retRange.minVal = range.minVal;
304         retRange.maxVal = range.maxVal;
305         return retRange;
306     }
307     std::cout << "codeclist_ is nullptr" << std::endl;
308     return retRange;
309 }
310 
AreVideoSizeAndFrameRateSupported(int32_t width,int32_t height,int32_t frameRate)311 bool CodecListCapiMock::AreVideoSizeAndFrameRateSupported(int32_t width, int32_t height, int32_t frameRate)
312 {
313     if (codeclist_ != nullptr) {
314         return OH_AVCapability_AreVideoSizeAndFrameRateSupported(codeclist_, width, height, frameRate);
315     }
316     std::cout << "codeclist_ is nullptr" << std::endl;
317     return false;
318 }
319 
GetVideoSupportedPixelFormats()320 std::vector<int32_t> CodecListCapiMock::GetVideoSupportedPixelFormats()
321 {
322     if (codeclist_ != nullptr) {
323         const int32_t *pixFormats = nullptr;
324         uint32_t pixFormatNum = 0;
325         int32_t ret = OH_AVCapability_GetVideoSupportedPixelFormats(codeclist_, &pixFormats, &pixFormatNum);
326         if (ret != AV_ERR_OK) {
327             std::cout << "OH_AVCapability_GetVideoSupportedPixelFormats returns error: " << ret << std::endl;
328             return std::vector<int32_t>();
329         }
330 
331         StackMemOverWrite();
332         std::vector<int32_t> retVector = std::vector<int32_t>(pixFormats, pixFormats + pixFormatNum);
333         std::sort(retVector.begin(), retVector.end());
334         return retVector;
335     }
336     std::cout << "codeclist_ is nullptr" << std::endl;
337     return std::vector<int32_t>();
338 }
339 
GetSupportedProfiles()340 std::vector<int32_t> CodecListCapiMock::GetSupportedProfiles()
341 {
342     if (codeclist_ != nullptr) {
343         const int32_t *profiles = nullptr;
344         uint32_t profileNum = 0;
345         int32_t ret = OH_AVCapability_GetSupportedProfiles(codeclist_, &profiles, &profileNum);
346         if (ret != AV_ERR_OK) {
347             std::cout << "OH_AVCapability_GetSupportedProfiles returns error: " << ret << std::endl;
348             return std::vector<int32_t>();
349         }
350 
351         StackMemOverWrite();
352         std::vector<int32_t> retVector = std::vector<int32_t>(profiles, profiles + profileNum);
353         std::sort(retVector.begin(), retVector.end());
354         return retVector;
355     }
356     std::cout << "codeclist_ is nullptr" << std::endl;
357     return std::vector<int32_t>();
358 }
359 
GetSupportedLevelsForProfile(int32_t profile)360 std::vector<int32_t> CodecListCapiMock::GetSupportedLevelsForProfile(int32_t profile)
361 {
362     if (codeclist_ != nullptr) {
363         const int32_t *levels = nullptr;
364         uint32_t levelNum = 0;
365         int32_t ret = OH_AVCapability_GetSupportedLevelsForProfile(codeclist_, profile, &levels, &levelNum);
366         if (ret != AV_ERR_OK) {
367             std::cout << "OH_AVCapability_GetSupportedLevelsForProfile returns error: " << ret << std::endl;
368             return std::vector<int32_t>();
369         }
370 
371         StackMemOverWrite();
372         std::vector<int32_t> retVector = std::vector<int32_t>(levels, levels + levelNum);
373         std::sort(retVector.begin(), retVector.end());
374         return retVector;
375     }
376     std::cout << "codeclist_ is nullptr" << std::endl;
377     return std::vector<int32_t>();
378 }
379 
AreProfileAndLevelSupported(int32_t profile,int32_t level)380 bool CodecListCapiMock::AreProfileAndLevelSupported(int32_t profile, int32_t level)
381 {
382     if (codeclist_ != nullptr) {
383         return OH_AVCapability_AreProfileAndLevelSupported(codeclist_, profile, level);
384     }
385     std::cout << "codeclist_ is nullptr" << std::endl;
386     return false;
387 }
388 } // namespace MediaAVCodec
389 } // namespace OHOS