• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 "avcodeclist_demo.h"
17 #include <iostream>
18 #include "media_errors.h"
19 #include "string_ex.h"
20 
21 using namespace std;
22 
23 namespace OHOS {
24 namespace Media {
DoNext()25 void AVCodecListDemo::DoNext()
26 {
27     cout << "Enter your step:" << endl;
28     std::string cmd;
29     Format format;
30     std::string codecName;
31     while (std::getline(std::cin, cmd)) {
32         if (cmd.find("fvd") != std::string::npos || cmd.find("FindVideoDecoder") != std::string::npos) {
33             if (BuildFormat(format)) {
34                 codecName = avCodecList_->FindVideoDecoder(format);
35                 cout << "FindVideoDecoder : " << codecName << endl;
36             }
37         } else if (cmd.find("fve") != std::string::npos || cmd.find("FindVideoEncoder") != std::string::npos) {
38             if (BuildFormat(format)) {
39                 codecName = avCodecList_->FindVideoEncoder(format);
40                 cout << "FindVideoEncoder : " << codecName << endl;
41             }
42         } else if (cmd.find("fad") != std::string::npos || cmd.find("FindAudioDecoder") != std::string::npos) {
43             if (BuildFormat(format)) {
44                 codecName = avCodecList_->FindAudioDecoder(format);
45                 cout << "FindAudioDecoder : " << codecName << endl;
46             }
47         } else if (cmd.find("fae") != std::string::npos || cmd.find("FindAudioEncoder") != std::string::npos) {
48             if (BuildFormat(format)) {
49                 codecName = avCodecList_->FindAudioEncoder(format);
50                 cout << "FindAudioEncoder : " << codecName << endl;
51             }
52         } else if (cmd.find("gvd") != std::string::npos || cmd.find("GetVideoDecoderCaps") != std::string::npos) {
53             std::vector<std::shared_ptr<VideoCaps>> videoDecoderArray = avCodecList_->GetVideoDecoderCaps();
54             PrintVideoCapsArray(videoDecoderArray);
55         } else if (cmd.find("gve") != std::string::npos || cmd.find("GetVideoEncoderCaps") != std::string::npos) {
56             std::vector<std::shared_ptr<VideoCaps>> videoEncoderArray = avCodecList_->GetVideoEncoderCaps();
57             PrintVideoCapsArray(videoEncoderArray);
58         } else if (cmd.find("gad") != std::string::npos || cmd.find("GetAudioDecoderCaps") != std::string::npos) {
59             std::vector<std::shared_ptr<AudioCaps>> audioDecoderArray = avCodecList_->GetAudioDecoderCaps();
60             PrintAudioCapsArray(audioDecoderArray);
61         } else if (cmd.find("gae") != std::string::npos || cmd.find("GetAudioEncoderCaps") != std::string::npos) {
62             std::vector<std::shared_ptr<AudioCaps>> audioEncoderArray = avCodecList_->GetAudioEncoderCaps();
63             PrintAudioCapsArray(audioEncoderArray);
64         } else if (cmd.find("gsfr") != std::string::npos || cmd.find("GetSupportedFrameRates") != std::string::npos) {
65             GetSupportedFrameRatesDemo();
66         } else if (cmd.find("gpfr") != std::string::npos || cmd.find("GetPreferredFrameRate") != std::string::npos) {
67             GetPreferredFrameRateDemo();
68         }
69     }
70 }
71 
GetSupportedFrameRatesDemo()72 void AVCodecListDemo::GetSupportedFrameRatesDemo()
73 {
74     Range ret;
75     ImgSize size = SetSize();
76 
77     std::vector<std::shared_ptr<VideoCaps>> videoEncoderArray = avCodecList_->GetVideoEncoderCaps();
78     for (auto iter = videoEncoderArray.begin(); iter != videoEncoderArray.end(); iter++) {
79         ret = (*iter)->GetSupportedFrameRatesFor(size.width, size.height);
80         cout << "name = " << (*iter)->GetCodecInfo()->GetName() << ":" << endl;
81         cout << "framerate = " << ret.minVal << ", " << ret.maxVal << endl;
82     }
83     std::vector<std::shared_ptr<VideoCaps>> videoDecoderArray = avCodecList_->GetVideoDecoderCaps();
84     for (auto iter = videoDecoderArray.begin(); iter != videoDecoderArray.end(); iter++) {
85         ret = (*iter)->GetSupportedFrameRatesFor(size.width, size.height);
86         cout << "name = " << (*iter)->GetCodecInfo()->GetName() << ":" << endl;
87         cout << "framerate = " << ret.minVal << ", " << ret.maxVal << endl;
88     }
89 }
90 
GetPreferredFrameRateDemo()91 void AVCodecListDemo::GetPreferredFrameRateDemo()
92 {
93     Range ret;
94     ImgSize size = SetSize();
95 
96     std::vector<std::shared_ptr<VideoCaps>> videoEncoderArray = avCodecList_->GetVideoEncoderCaps();
97     for (auto iter = videoEncoderArray.begin(); iter != videoEncoderArray.end(); iter++) {
98         ret = (*iter)->GetPreferredFrameRate(size.width, size.height);
99         cout << "name = " << (*iter)->GetCodecInfo()->GetName() << ":" << endl;
100         cout << "framerate = " << ret.minVal << ", " << ret.maxVal << endl;
101     }
102     std::vector<std::shared_ptr<VideoCaps>> videoDecoderArray = avCodecList_->GetVideoDecoderCaps();
103     for (auto iter = videoDecoderArray.begin(); iter != videoDecoderArray.end(); iter++) {
104         ret = (*iter)->GetPreferredFrameRate(size.width, size.height);
105         cout << "name = " << (*iter)->GetCodecInfo()->GetName() << ":" << endl;
106         cout << "framerate = " << ret.minVal << ", " << ret.maxVal << endl;
107     }
108 }
109 
SetSize()110 ImgSize AVCodecListDemo::SetSize()
111 {
112     string str;
113     cout << "Enter width : " << endl;
114     (void)getline(cin, str);
115     int32_t width = -1;
116     if (!StrToInt(str, width)) {
117         cout << "call StrToInt func false, input str is:" << str.c_str();
118         return ImgSize(0, 0);
119     }
120     cout << "Enter height : " << endl;
121 
122     (void)getline(cin, str);
123     int32_t height = -1;
124     if (!StrToInt(str, height)) {
125         cout << "call StrToInt func false, input str is:" << str.c_str();
126         return ImgSize(0, 0);
127     }
128     cout << "width = " << width << ", height = " << height << endl;
129     return ImgSize(width, height);
130 }
131 
BuildFormat(Format & format)132 bool AVCodecListDemo::BuildFormat(Format &format)
133 {
134     format = Format();
135     const std::unordered_map<std::string, int32_t> MIME_TO_MEDIA_TYPE_MAP = {
136         {"audio/mpeg", MEDIA_TYPE_AUD},
137         {"audio/mp4a-latm", MEDIA_TYPE_AUD},
138         {"audio/vorbis", MEDIA_TYPE_AUD},
139         {"audio/flac", MEDIA_TYPE_AUD},
140         {"video/avc", MEDIA_TYPE_VID},
141         {"video/h263", MEDIA_TYPE_VID},
142         {"video/mpeg2", MEDIA_TYPE_VID},
143         {"video/mp4v-es", MEDIA_TYPE_VID},
144         {"video/x-vnd.on2.vp8", MEDIA_TYPE_VID}
145     };
146 
147     cout << "Enter the MediaDescription of codec :" << endl;
148     cout << "Enter the codec mime :" << endl;
149     std::string codecMime;
150     (void)getline(cin, codecMime);
151     if (codecMime == "") {
152         cout << "Failed! The target of codec_mime cannot be set to null " << endl;
153         return false;
154     }
155     format.PutStringValue("codec_mime", codecMime);
156     (void)SetMediaDescriptionToFormat(format, "bitrate");
157 
158     if (MIME_TO_MEDIA_TYPE_MAP.find(codecMime) != MIME_TO_MEDIA_TYPE_MAP.end()) {
159         if (MIME_TO_MEDIA_TYPE_MAP.at(codecMime) == MEDIA_TYPE_VID) {
160             (void)SetMediaDescriptionToFormat(format, "width");
161             (void)SetMediaDescriptionToFormat(format, "height");
162             (void)SetMediaDescriptionToFormat(format, "pixel_format");
163             (void)SetMediaDescriptionToFormat(format, "frame_rate");
164         } else if (MIME_TO_MEDIA_TYPE_MAP.at(codecMime) == MEDIA_TYPE_AUD) {
165             (void)SetMediaDescriptionToFormat(format, "channel_count");
166             (void)SetMediaDescriptionToFormat(format, "samplerate");
167         }
168     } else {
169         cout << "Failed! The target of codec_mime cannot be set to " << codecMime << endl;
170         return false;
171     }
172 
173     return true;
174 }
175 
SetMediaDescriptionToFormat(Format & format,const std::string & key)176 void AVCodecListDemo::SetMediaDescriptionToFormat(Format &format, const std::string &key)
177 {
178     cout << "Set the " << key << " :" << endl;
179     string mediaDescription;
180     if (key == "pixel_format") {
181         cout << "1:YUVI420" << endl;
182         cout << "2:NV12" << endl;
183         cout << "3:NV21" << endl;
184         cout << "4:RGBA" << endl;
185     }
186     (void)getline(cin, mediaDescription);
187     if (mediaDescription == "") {
188         cout << key << " is setting to null" << endl;
189         return;
190     }
191     int32_t number = -1;
192     if (!StrToInt(mediaDescription, number)) {
193         cout << "call StrToInt func false, input str is:" << mediaDescription.c_str();
194         return;
195     }
196     format.PutIntValue(key, number);
197     cin.clear();
198 }
199 
PrintVideoCapsArray(const std::vector<std::shared_ptr<VideoCaps>> & videoCapsArray) const200 void AVCodecListDemo::PrintVideoCapsArray(const std::vector<std::shared_ptr<VideoCaps>> &videoCapsArray) const
201 {
202     for (auto iter = videoCapsArray.begin(); iter != videoCapsArray.end(); iter++) {
203         std::shared_ptr<VideoCaps> pVideoCaps = *iter;
204         if (pVideoCaps == nullptr) {
205             cout << "pVideoCaps is nullptr" << endl;
206             break;
207         }
208         std::shared_ptr<AVCodecInfo> pVideoCodecCaps;
209         pVideoCodecCaps = pVideoCaps->GetCodecInfo();
210         cout << "This codec capability is :" << endl;
211         cout << "GetName = "<< pVideoCodecCaps->GetName() << endl;
212         cout << "GetType = "<< pVideoCodecCaps->GetType() << endl;
213         cout << "GetMimeType = "<< pVideoCodecCaps->GetMimeType() << endl;
214         cout << "IsHardwareAccelerated = "<< pVideoCodecCaps->IsHardwareAccelerated() << endl;
215         cout << "IsSoftwareOnly = "<< pVideoCodecCaps->IsSoftwareOnly() << endl;
216         cout << "IsVendor = "<< pVideoCodecCaps->IsVendor() << endl;
217         cout << "GetSupportedBitrate = "<< pVideoCaps->GetSupportedBitrate().minVal <<\
218                 " - " << pVideoCaps->GetSupportedBitrate().maxVal << endl;
219         cout << "GetSupportedWidthAlignment = "<< pVideoCaps->GetSupportedWidthAlignment() << endl;
220         cout << "GetSupportedHeightAlignment = "<< pVideoCaps->GetSupportedHeightAlignment() << endl;
221         cout << "GetSupportedWidth = "<< pVideoCaps->GetSupportedWidth().minVal <<\
222                 " - " << pVideoCaps->GetSupportedWidth().maxVal << endl;
223         cout << "GetSupportedHeight = "<< pVideoCaps->GetSupportedHeight().minVal <<\
224                 " - " << pVideoCaps->GetSupportedHeight().maxVal << endl;
225         cout << "GetSupportedFrameRate = "<< pVideoCaps->GetSupportedFrameRate().minVal <<\
226                 " - " << pVideoCaps->GetSupportedFrameRate().maxVal << endl;
227         cout << "GetSupportedEncodeQuality = "<< pVideoCaps->GetSupportedEncodeQuality().minVal <<\
228                 " - " << pVideoCaps->GetSupportedEncodeQuality().maxVal << endl;
229         cout << "GetSupportedQuality = "<< pVideoCaps->GetSupportedQuality().minVal <<\
230                 " - " << pVideoCaps->GetSupportedQuality().maxVal << endl;
231         PrintIntArray(pVideoCaps->GetSupportedFormats(), "GetSupportedFormats");
232         PrintIntArray(pVideoCaps->GetSupportedProfiles(), "GetSupportedProfiles");
233         PrintIntArray(pVideoCaps->GetSupportedBitrateMode(), "GetSupportedBitrateMode");
234         PrintIntArray(pVideoCaps->GetSupportedLevels(), "GetSupportedBitrateMode");
235     }
236 }
237 
PrintAudioCapsArray(const std::vector<std::shared_ptr<AudioCaps>> & audioCapsArray) const238 void AVCodecListDemo::PrintAudioCapsArray(const std::vector<std::shared_ptr<AudioCaps>> &audioCapsArray) const
239 {
240     for (auto iter = audioCapsArray.begin(); iter != audioCapsArray.end(); iter++) {
241         std::shared_ptr<AudioCaps> pAudioCaps = *iter;
242         std::shared_ptr<AVCodecInfo> pAudioCodecCaps = pAudioCaps->GetCodecInfo();
243         if (pAudioCodecCaps == nullptr) {
244             cout << "pAudioCodecCaps is nullptr" << endl;
245             break;
246         }
247         cout << "This codec capability is :" << endl;
248         cout << "GetName = "<< pAudioCodecCaps->GetName() << endl;
249         cout << "GetType = "<< pAudioCodecCaps->GetType() << endl;
250         cout << "GetMimeType = "<< pAudioCodecCaps->GetMimeType() << endl;
251         cout << "IsHardwareAccelerated = "<< pAudioCodecCaps->IsHardwareAccelerated() << endl;
252         cout << "IsSoftwareOnly = "<< pAudioCodecCaps->IsSoftwareOnly() << endl;
253         cout << "IsVendor = "<< pAudioCodecCaps->IsVendor() << endl;
254         cout << "GetSupportedBitrate = "<< pAudioCaps->GetSupportedBitrate().minVal <<\
255                 " - " << pAudioCaps->GetSupportedBitrate().maxVal << endl;
256         cout << "GetSupportedChannel = "<< pAudioCaps->GetSupportedChannel().minVal <<\
257                 " - " << pAudioCaps->GetSupportedChannel().maxVal << endl;
258         cout << "GetSupportedComplexity = "<< pAudioCaps->GetSupportedComplexity().minVal <<\
259                 " - "<< pAudioCaps->GetSupportedComplexity().maxVal << endl;
260         PrintIntArray(pAudioCaps->GetSupportedFormats(), "GetSupportedFormats");
261         PrintIntArray(pAudioCaps->GetSupportedSampleRates(), "GetSupportedSampleRates");
262         PrintIntArray(pAudioCaps->GetSupportedProfiles(), "GetSupportedProfiles");
263         PrintIntArray(pAudioCaps->GetSupportedLevels(), "GetSupportedBitrateMode");
264     }
265 }
266 
PrintIntArray(const std::vector<int32_t> & array,const std::string & logmsg) const267 void AVCodecListDemo::PrintIntArray(const std::vector<int32_t> &array, const std::string &logmsg) const
268 {
269     cout << logmsg << ": ";
270     for (auto iter = array.begin(); iter != array.end(); iter++) {
271         cout << *iter << ", ";
272     }
273     cout  << endl;
274 }
275 
RunCase(const string & path)276 void AVCodecListDemo::RunCase(const string &path)
277 {
278     avCodecList_ = OHOS::Media::AVCodecListFactory::CreateAVCodecList();
279     if (avCodecList_ == nullptr) {
280         cout << "avCodecList_ is null" << endl;
281         return;
282     }
283 
284     DoNext();
285 }
286 } // namespace Media
287 } // namespace OHOS