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_inner_mock.h"
17 
18 namespace OHOS {
19 namespace MediaAVCodec {
IsHardware()20 bool CodecListInnerMock::IsHardware()
21 {
22     if (codeclist_ != nullptr) {
23         std::shared_ptr<AVCodecInfo> codecInfo = std::make_shared<AVCodecInfo>(capabilityData_);
24         return codecInfo->IsHardwareAccelerated();
25     }
26     std::cout << "codeclist_ is nullptr" << std::endl;
27     return false;
28 }
29 
GetName()30 std::string CodecListInnerMock::GetName()
31 {
32     if (codeclist_ != nullptr) {
33         std::shared_ptr<AVCodecInfo> codecInfo = std::make_shared<AVCodecInfo>(capabilityData_);
34         return codecInfo->GetName();
35     }
36     std::cout << "codeclist_ is nullptr" << std::endl;
37     return "";
38 }
39 
GetMaxSupportedInstances()40 int32_t CodecListInnerMock::GetMaxSupportedInstances()
41 {
42     if (codeclist_ != nullptr) {
43         std::shared_ptr<AVCodecInfo> codecInfo = std::make_shared<AVCodecInfo>(capabilityData_);
44         return codecInfo->GetMaxSupportedInstances();
45     }
46     std::cout << "codeclist_ is nullptr" << std::endl;
47     return 0;
48 }
49 
GetEncoderBitrateRange()50 Range CodecListInnerMock::GetEncoderBitrateRange()
51 {
52     if (codeclist_ != nullptr) {
53         std::shared_ptr<AudioCaps> codecInfo = std::make_shared<AudioCaps>(capabilityData_);
54         return codecInfo->GetSupportedBitrate();
55     }
56     std::cout << "codeclist_ is nullptr" << std::endl;
57     return Range(0, 0);
58 }
59 
IsEncoderBitrateModeSupported(OH_BitrateMode bitrateMode)60 bool CodecListInnerMock::IsEncoderBitrateModeSupported(OH_BitrateMode bitrateMode)
61 {
62     if (codeclist_ != nullptr) {
63         std::shared_ptr<VideoCaps> codecInfo = std::make_shared<VideoCaps>(capabilityData_);
64         auto bitrateModeVec = codecInfo->GetSupportedBitrateMode();
65         return find(bitrateModeVec.begin(), bitrateModeVec.end(), bitrateMode) != bitrateModeVec.end();
66     }
67     return false;
68 }
69 
GetEncoderQualityRange()70 Range CodecListInnerMock::GetEncoderQualityRange()
71 {
72     if (codeclist_ != nullptr) {
73         std::shared_ptr<VideoCaps> codecInfo = std::make_shared<VideoCaps>(capabilityData_);
74         return codecInfo->GetSupportedEncodeQuality();
75     }
76     std::cout << "codeclist_ is nullptr" << std::endl;
77     return Range(0, 0);
78 }
79 
GetEncoderComplexityRange()80 Range CodecListInnerMock::GetEncoderComplexityRange()
81 {
82     if (codeclist_ != nullptr) {
83         std::shared_ptr<VideoCaps> codecInfo = std::make_shared<VideoCaps>(capabilityData_);
84         return codecInfo->GetSupportedComplexity();
85     }
86     std::cout << "codeclist_ is nullptr" << std::endl;
87     return Range(0, 0);
88 }
89 
GetAudioSupportedSampleRates()90 std::vector<int32_t> CodecListInnerMock::GetAudioSupportedSampleRates()
91 {
92     if (codeclist_ != nullptr) {
93         std::shared_ptr<AudioCaps> codecInfo = std::make_shared<AudioCaps>(capabilityData_);
94         auto ret = codecInfo->GetSupportedSampleRates();
95         std::sort(ret.begin(), ret.end());
96         return ret;
97     }
98     std::cout << "codeclist_ is nullptr" << std::endl;
99     return std::vector<int32_t>();
100 }
101 
GetAudioChannelsRange()102 Range CodecListInnerMock::GetAudioChannelsRange()
103 {
104     if (codeclist_ != nullptr) {
105         std::shared_ptr<AudioCaps> codecInfo = std::make_shared<AudioCaps>(capabilityData_);
106         return codecInfo->GetSupportedChannel();
107     }
108     std::cout << "codeclist_ is nullptr" << std::endl;
109     return Range(0, 0);
110 }
111 
GetVideoWidthAlignment()112 int32_t CodecListInnerMock::GetVideoWidthAlignment()
113 {
114     if (codeclist_ != nullptr) {
115         std::shared_ptr<VideoCaps> codecInfo = std::make_shared<VideoCaps>(capabilityData_);
116         return codecInfo->GetSupportedWidthAlignment();
117     }
118     std::cout << "codeclist_ is nullptr" << std::endl;
119     return 0;
120 }
121 
GetVideoHeightAlignment()122 int32_t CodecListInnerMock::GetVideoHeightAlignment()
123 {
124     if (codeclist_ != nullptr) {
125         std::shared_ptr<VideoCaps> codecInfo = std::make_shared<VideoCaps>(capabilityData_);
126         return codecInfo->GetSupportedHeightAlignment();
127     }
128     std::cout << "codeclist_ is nullptr" << std::endl;
129     return 0;
130 }
131 
GetVideoWidthRangeForHeight(int32_t height)132 Range CodecListInnerMock::GetVideoWidthRangeForHeight(int32_t height)
133 {
134     if (codeclist_ != nullptr) {
135         std::shared_ptr<VideoCaps> codecInfo = std::make_shared<VideoCaps>(capabilityData_);
136         return codecInfo->GetVideoWidthRangeForHeight(height);
137     }
138     std::cout << "codeclist_ is nullptr" << std::endl;
139     return Range(0, 0);
140 }
141 
GetVideoHeightRangeForWidth(int32_t width)142 Range CodecListInnerMock::GetVideoHeightRangeForWidth(int32_t width)
143 {
144     if (codeclist_ != nullptr) {
145         std::shared_ptr<VideoCaps> codecInfo = std::make_shared<VideoCaps>(capabilityData_);
146         return codecInfo->GetVideoHeightRangeForWidth(width);
147     }
148     std::cout << "codeclist_ is nullptr" << std::endl;
149     return Range(0, 0);
150 }
151 
GetVideoWidthRange()152 Range CodecListInnerMock::GetVideoWidthRange()
153 {
154     if (codeclist_ != nullptr) {
155         std::shared_ptr<VideoCaps> codecInfo = std::make_shared<VideoCaps>(capabilityData_);
156         return codecInfo->GetSupportedWidth();
157     }
158     std::cout << "codeclist_ is nullptr" << std::endl;
159     return Range(0, 0);
160 }
161 
GetVideoHeightRange()162 Range CodecListInnerMock::GetVideoHeightRange()
163 {
164     if (codeclist_ != nullptr) {
165         std::shared_ptr<VideoCaps> codecInfo = std::make_shared<VideoCaps>(capabilityData_);
166         return codecInfo->GetSupportedHeight();
167     }
168     std::cout << "codeclist_ is nullptr" << std::endl;
169     return Range(0, 0);
170 }
171 
IsVideoSizeSupported(int32_t width,int32_t height)172 bool CodecListInnerMock::IsVideoSizeSupported(int32_t width, int32_t height)
173 {
174     if (codeclist_ != nullptr) {
175         std::shared_ptr<VideoCaps> videoCap = std::make_shared<VideoCaps>(capabilityData_);
176         return videoCap->IsSizeSupported(width, height);
177     }
178     return false;
179 }
180 
GetVideoFrameRateRange()181 Range CodecListInnerMock::GetVideoFrameRateRange()
182 {
183     if (codeclist_ != nullptr) {
184         std::shared_ptr<VideoCaps> videoCap = std::make_shared<VideoCaps>(capabilityData_);
185         auto ret = videoCap->GetSupportedFrameRate();
186         return ret;
187     }
188     std::cout << "codeclist_ is nullptr" << std::endl;
189     return Range(0, 0);
190 }
191 
GetVideoFrameRateRangeForSize(int32_t width,int32_t height)192 Range CodecListInnerMock::GetVideoFrameRateRangeForSize(int32_t width, int32_t height)
193 {
194     if (codeclist_ != nullptr) {
195         std::shared_ptr<VideoCaps> videoCap = std::make_shared<VideoCaps>(capabilityData_);
196         return videoCap->GetSupportedFrameRatesFor(width, height);
197     }
198     std::cout << "codeclist_ is nullptr" << std::endl;
199     return Range(0, 0);
200 }
201 
AreVideoSizeAndFrameRateSupported(int32_t width,int32_t height,int32_t frameRate)202 bool CodecListInnerMock::AreVideoSizeAndFrameRateSupported(int32_t width, int32_t height, int32_t frameRate)
203 {
204     if (codeclist_ != nullptr) {
205         std::shared_ptr<VideoCaps> videoCap = std::make_shared<VideoCaps>(capabilityData_);
206         return videoCap->IsSizeAndRateSupported(width, height, frameRate);
207     }
208     std::cout << "codeclist_ is nullptr" << std::endl;
209     return false;
210 }
211 
GetVideoSupportedPixelFormats()212 std::vector<int32_t> CodecListInnerMock::GetVideoSupportedPixelFormats()
213 {
214     if (codeclist_ != nullptr) {
215         std::shared_ptr<VideoCaps> codecInfo = std::make_shared<VideoCaps>(capabilityData_);
216         auto ret = codecInfo->GetSupportedFormats();
217         std::sort(ret.begin(), ret.end());
218         return ret;
219     }
220     std::cout << "codeclist_ is nullptr" << std::endl;
221     return std::vector<int32_t>();
222 }
223 
GetSupportedProfiles()224 std::vector<int32_t> CodecListInnerMock::GetSupportedProfiles()
225 {
226     if (codeclist_ != nullptr && capabilityData_ != nullptr) {
227         std::vector<int32_t> ret;
228         if (capabilityData_->codecType == AVCODEC_TYPE_VIDEO_ENCODER ||
229             capabilityData_->codecType == AVCODEC_TYPE_VIDEO_DECODER) {
230             std::shared_ptr<VideoCaps> codecInfo = std::make_shared<VideoCaps>(capabilityData_);
231             ret = codecInfo->GetSupportedProfiles();
232         } else if (capabilityData_->codecType == AVCODEC_TYPE_AUDIO_DECODER ||
233                    capabilityData_->codecType == AVCODEC_TYPE_AUDIO_ENCODER) {
234             std::shared_ptr<AudioCaps> codecInfo = std::make_shared<AudioCaps>(capabilityData_);
235             ret = codecInfo->GetSupportedProfiles();
236         }
237         std::sort(ret.begin(), ret.end());
238         return ret;
239     }
240     std::cout << "codeclist_ or capabilityData_ is nullptr" << std::endl;
241     return std::vector<int32_t>();
242 }
243 
GetSupportedLevelsForProfile(int32_t profile)244 std::vector<int32_t> CodecListInnerMock::GetSupportedLevelsForProfile(int32_t profile)
245 {
246     if (codeclist_ != nullptr) {
247         std::shared_ptr<AVCodecInfo> codecInfo = std::make_shared<AVCodecInfo>(capabilityData_);
248         auto profileLevelsMap = codecInfo->GetSupportedLevelsForProfile();
249         auto levelsmatch = profileLevelsMap.find(profile);
250         if (levelsmatch == profileLevelsMap.end()) {
251             return std::vector<int32_t>();
252         }
253         std::sort(levelsmatch->second.begin(), levelsmatch->second.end());
254         return levelsmatch->second;
255     }
256     std::cout << "codeclist_ is nullptr" << std::endl;
257     return std::vector<int32_t>();
258 }
259 
AreProfileAndLevelSupported(int32_t profile,int32_t level)260 bool CodecListInnerMock::AreProfileAndLevelSupported(int32_t profile, int32_t level)
261 {
262     if (codeclist_ != nullptr) {
263         std::shared_ptr<AVCodecInfo> codecInfo = std::make_shared<AVCodecInfo>(capabilityData_);
264         auto profileLevelsMap = codecInfo->GetSupportedLevelsForProfile();
265         auto levels = profileLevelsMap.find(profile);
266         if (levels == profileLevelsMap.end()) {
267             return false;
268         }
269 
270         return find(levels->second.begin(), levels->second.end(), level) != levels->second.end();
271     }
272     std::cout << "codeclist_ is nullptr" << std::endl;
273     return false;
274 }
275 } // namespace MediaAVCodec
276 } // namespace OHOS