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 "histreamer_ability_querier.h"
17 
18 #include <algorithm>
19 #include <memory>
20 #include <vector>
21 #include <securec.h>
22 #include "plugin_caps.h"
23 #include "plugin_manager.h"
24 #include "pipeline/filters/common/plugin_utils.h"
25 
26 #include "av_trans_log.h"
27 
28 using OHOS::Media::Plugin::PluginManager;
29 using OHOS::Media::Plugin::PluginType;
30 using OHOS::Media::Plugin::PluginInfo;
31 using OHOS::Media::Plugin::Capability;
32 using OHOS::Media::Plugin::CapabilitySet;
33 using OHOS::Media::Plugin::AnyCast;
34 using CapabilityID = OHOS::Media::Plugin::Capability::Key;
35 
36 namespace OHOS {
37 namespace DistributedHardware {
38 
39 const uint32_t MAX_MESSAGES_LEN = 1 * 1024 * 1024;
40 static const std::string NAME = "name";
41 static const std::string INS = "ins";
42 static const std::string OUTS = "outs";
43 static const std::string MIME = "mime";
44 static const std::string SAMPLE_RATE = "sample_rate";
45 static const std::string AUDIO_SAMPLE_FORMAT = "sample_fmt";
46 static const std::string AD_MPEG_VER = "ad_mpeg_ver";
47 static const std::string AUDIO_AAC_PROFILE = "aac_profile";
48 static const std::string AUDIO_AAC_STREAM_FORMAT = "aac_stm_fmt";
49 static const std::string AUDIO_CHANNEL_LAYOUT = "channel_layout";
50 
51 static const std::string VIDEO_PIXEL_FMT = "pixel_fmt";
52 static const std::string VIDEO_BIT_STREAM_FMT = "vd_bit_stream_fmt";
53 
54 static const std::string AUDIO_ENCODERS = "audioEncoders";
55 static const std::string AUDIO_DECODERS = "audioDecoders";
56 static const std::string VIDEO_ENCODERS = "videoEncoders";
57 static const std::string VIDEO_DECODERS = "videoDecoders";
58 
59 static const std::vector<std::string> AUDIO_ENCODER_WANT = { "ffmpegAuEnc_aac" };
60 static const std::vector<std::string> AUDIO_DECODER_WANT = { "ffmpegAuDec_aac" };
61 static const std::vector<std::string> VIDEO_ENCODER_WANT = {
62     "HdiCodecAdapter.OMX.hisi.video.encoder.hevc",
63     "HdiCodecAdapter.OMX.hisi.video.encoder.avc",
64     "HdiCodecAdapter.OMX.rk.video_encoder.hevc",
65     "HdiCodecAdapter.OMX.rk.video_encoder.avc"
66     };
67 static const std::vector<std::string> VIDEO_DECODER_WANT = {
68     "HdiCodecAdapter.OMX.hisi.video.decoder.hevc",
69     "HdiCodecAdapter.OMX.hisi.video.decoder.avc",
70     "HdiCodecAdapter.OMX.rk.video_decoder.hevc",
71     "HdiCodecAdapter.OMX.rk.video_decoder.avc"
72     };
73 
IsString(const cJSON * jsonObj,const std::string & key)74 bool IsString(const cJSON *jsonObj, const std::string &key)
75 {
76     cJSON *keyObj = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str());
77     bool res = (keyObj != nullptr) && cJSON_IsString(keyObj) &&
78         strlen(cJSON_GetStringValue(keyObj)) <= MAX_MESSAGES_LEN;
79     if (!res) {
80         AVTRANS_LOGE("the key %{public}s in jsonObj is invalid.", key.c_str());
81     }
82     return res;
83 }
84 
IsUInt8(const cJSON * jsonObj,const std::string & key)85 bool IsUInt8(const cJSON *jsonObj, const std::string &key)
86 {
87     cJSON *keyObj = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str());
88     bool res = (keyObj != nullptr) && cJSON_IsNumber(keyObj) &&
89         static_cast<uint8_t>(keyObj->valueint) <= UINT8_MAX;
90     return res;
91 }
92 
IsUInt32(const cJSON * jsonObj,const std::string & key)93 bool IsUInt32(const cJSON *jsonObj, const std::string &key)
94 {
95     cJSON *keyObj = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str());
96     bool res = (keyObj != nullptr) && cJSON_IsNumber(keyObj) &&
97         static_cast<uint32_t>(keyObj->valueint) <= UINT32_MAX;
98     if (!res) {
99         AVTRANS_LOGE("the key %{public}s in jsonObj is invalid.", key.c_str());
100     }
101     return res;
102 }
103 
ParseAudioEncoderIn(CapabilitySet & inCaps)104 std::vector<AudioEncoderIn> ParseAudioEncoderIn(CapabilitySet &inCaps)
105 {
106     std::vector<AudioEncoderIn> ins;
107     for (auto &cap : inCaps) {
108         AVTRANS_LOGD("AudioEncoderIn Raw: %{public}s", OHOS::Media::Pipeline::Capability2String(cap).c_str());
109         AudioEncoderIn in;
110         in.mime = cap.mime;
111         in.sample_rate = AnyCast<std::vector<uint32_t>>(cap.keys[CapabilityID::AUDIO_SAMPLE_RATE]);
112         ins.push_back(in);
113     }
114 
115     return ins;
116 }
117 
ParseAudioEncoderOut(CapabilitySet & outCaps)118 std::vector<AudioEncoderOut> ParseAudioEncoderOut(CapabilitySet &outCaps)
119 {
120     std::vector<AudioEncoderOut> outs;
121     for (auto &cap : outCaps) {
122         AVTRANS_LOGD("AudioEncoderOut Raw: %{public}s", OHOS::Media::Pipeline::Capability2String(cap).c_str());
123         AudioEncoderOut out;
124         out.mime = cap.mime;
125         out.ad_mpeg_ver = AnyCast<uint32_t>(cap.keys[CapabilityID::AUDIO_MPEG_VERSION]);
126         outs.push_back(out);
127     }
128 
129     return outs;
130 }
131 
QueryAudioEncoderAbility()132 std::vector<AudioEncoder> QueryAudioEncoderAbility()
133 {
134     std::vector<AudioEncoder> audioEncoders;
135     auto audioEncNameList = PluginManager::Instance().ListPlugins(PluginType::AUDIO_ENCODER);
136     for (const std::string& name : audioEncNameList) {
137         if (find(AUDIO_ENCODER_WANT.begin(), AUDIO_ENCODER_WANT.end(), name) == AUDIO_ENCODER_WANT.end()) {
138             AVTRANS_LOGI("AudioEncoder Plugin not want: %{public}s", name.c_str());
139             continue;
140         }
141         auto pluginInfo = PluginManager::Instance().GetPluginInfo(PluginType::AUDIO_ENCODER, name);
142         if (pluginInfo == nullptr) {
143             AVTRANS_LOGE("pluginInfo is nullptr");
144             continue;
145         }
146         AudioEncoder audioEncoder;
147         audioEncoder.name = name;
148         audioEncoder.ins = ParseAudioEncoderIn(pluginInfo->inCaps);
149         audioEncoder.outs = ParseAudioEncoderOut(pluginInfo->outCaps);
150         audioEncoders.push_back(audioEncoder);
151     }
152 
153     return audioEncoders;
154 }
155 
ToJson(cJSON * jsonObject,const AudioEncoderIn & audioEncoderIn)156 void ToJson(cJSON *jsonObject, const AudioEncoderIn &audioEncoderIn)
157 {
158     if (jsonObject == nullptr) {
159         return;
160     }
161     cJSON_AddStringToObject(jsonObject, MIME.c_str(), audioEncoderIn.mime.c_str());
162     cJSON *sampleRateJson = cJSON_CreateArray();
163     if (sampleRateJson == nullptr) {
164         return;
165     }
166     for (auto rate : audioEncoderIn.sample_rate) {
167         cJSON_AddItemToArray(sampleRateJson, cJSON_CreateNumber(rate));
168     }
169     cJSON_AddItemToObject(jsonObject, SAMPLE_RATE.c_str(), sampleRateJson);
170 }
171 
FromJson(const cJSON * jsonObject,AudioEncoderIn & audioEncoderIn)172 void FromJson(const cJSON *jsonObject, AudioEncoderIn &audioEncoderIn)
173 {
174     if (!IsString(jsonObject, MIME)) {
175         AVTRANS_LOGE("AudioEncoderIn MIME is invalid!");
176         return;
177     }
178     cJSON *mimeObj = cJSON_GetObjectItemCaseSensitive(jsonObject, MIME.c_str());
179     if (mimeObj == nullptr || !cJSON_IsString(mimeObj)) {
180         return;
181     }
182     audioEncoderIn.mime = mimeObj->valuestring;
183     cJSON *rateJson = cJSON_GetObjectItemCaseSensitive(jsonObject, SAMPLE_RATE.c_str());
184     if (rateJson == nullptr || !cJSON_IsArray(rateJson)) {
185         AVTRANS_LOGE("AudioEncoderIn SAMPLE_RATE is invalid");
186         return;
187     }
188     cJSON *rateInfo = nullptr;
189     cJSON_ArrayForEach(rateInfo, rateJson) {
190         if (rateInfo && rateInfo->type == cJSON_Number) {
191             audioEncoderIn.sample_rate.push_back(static_cast<uint32_t>(rateInfo->valueint));
192         }
193     }
194 }
195 
ToJson(cJSON * jsonObject,const AudioEncoderOut & audioEncoderOut)196 void ToJson(cJSON *jsonObject, const AudioEncoderOut &audioEncoderOut)
197 {
198     if (jsonObject == nullptr) {
199         return;
200     }
201     cJSON_AddStringToObject(jsonObject, MIME.c_str(), audioEncoderOut.mime.c_str());
202     cJSON_AddNumberToObject(jsonObject, AD_MPEG_VER.c_str(), static_cast<uint8_t>(audioEncoderOut.ad_mpeg_ver));
203     cJSON_AddNumberToObject(jsonObject, AUDIO_AAC_PROFILE.c_str(), static_cast<uint8_t>(audioEncoderOut.aac_profile));
204     cJSON_AddNumberToObject(jsonObject, AUDIO_AAC_STREAM_FORMAT.c_str(),
205         static_cast<uint8_t>(audioEncoderOut.aac_stm_fmt));
206 }
207 
FromJson(const cJSON * jsonObject,AudioEncoderOut & audioEncoderOut)208 void FromJson(const cJSON *jsonObject, AudioEncoderOut &audioEncoderOut)
209 {
210     if (jsonObject == nullptr) {
211         return;
212     }
213     if (!IsString(jsonObject, MIME)) {
214         AVTRANS_LOGE("AudioEncoderOut MIME is invalid!");
215         return;
216     }
217     cJSON *mimeObj = cJSON_GetObjectItemCaseSensitive(jsonObject, MIME.c_str());
218     if (mimeObj == nullptr || !cJSON_IsString(mimeObj)) {
219         return;
220     }
221     audioEncoderOut.mime = mimeObj->valuestring;
222     if (!IsUInt32(jsonObject, AD_MPEG_VER)) {
223         AVTRANS_LOGE("AudioEncoderOut AD_MPEG_VER is invalid");
224         return;
225     }
226     cJSON *verObj = cJSON_GetObjectItemCaseSensitive(jsonObject, AD_MPEG_VER.c_str());
227     if (verObj == nullptr || !cJSON_IsNumber(verObj)) {
228         return;
229     }
230     audioEncoderOut.ad_mpeg_ver = static_cast<uint32_t>(verObj->valueint);
231 
232     if (!IsUInt8(jsonObject, AUDIO_AAC_PROFILE)) {
233         AVTRANS_LOGE("AudioEncoderOut AUDIO_AAC_PROFILE is invalid");
234         return;
235     }
236     cJSON *accObj = cJSON_GetObjectItemCaseSensitive(jsonObject, AUDIO_AAC_PROFILE.c_str());
237     if (accObj == nullptr || !cJSON_IsNumber(accObj)) {
238         return;
239     }
240     audioEncoderOut.aac_profile = static_cast<AudioAacProfile>(accObj->valueint);
241     if (!IsUInt8(jsonObject, AUDIO_AAC_STREAM_FORMAT)) {
242         AVTRANS_LOGE("AudioEncoderOut AUDIO_AAC_STREAM_FORMAT is invalid");
243         return;
244     }
245     cJSON *formatObj = cJSON_GetObjectItemCaseSensitive(jsonObject, AUDIO_AAC_STREAM_FORMAT.c_str());
246     if (formatObj == nullptr || !cJSON_IsNumber(formatObj)) {
247         return;
248     }
249     audioEncoderOut.aac_stm_fmt = static_cast<AudioAacStreamFormat>(formatObj->valueint);
250 }
251 
ToJson(cJSON * jsonObject,const AudioEncoder & audioEncoder)252 void ToJson(cJSON *jsonObject, const AudioEncoder &audioEncoder)
253 {
254     if (jsonObject == nullptr) {
255         return;
256     }
257     cJSON_AddStringToObject(jsonObject, NAME.c_str(), audioEncoder.name.c_str());
258     cJSON *audioEncoderInsJson = cJSON_CreateArray();
259     if (audioEncoderInsJson == nullptr) {
260         return;
261     }
262     for (const auto &in : audioEncoder.ins) {
263         cJSON *audioEncoderInJson = cJSON_CreateObject();
264         if (audioEncoderInJson == nullptr) {
265             cJSON_Delete(audioEncoderInsJson);
266             return;
267         }
268         ToJson(audioEncoderInJson, in);
269         cJSON_AddItemToArray(audioEncoderInsJson, audioEncoderInJson);
270     }
271     cJSON_AddItemToObject(jsonObject, INS.c_str(), audioEncoderInsJson);
272 
273     cJSON *audioEncoderOutsJson = cJSON_CreateArray();
274     if (audioEncoderOutsJson == nullptr) {
275         return;
276     }
277     for (const auto &out : audioEncoder.outs) {
278         cJSON *audioEncoderOutJson = cJSON_CreateObject();
279         if (audioEncoderOutJson == nullptr) {
280             cJSON_Delete(audioEncoderOutsJson);
281             return;
282         }
283         ToJson(audioEncoderOutJson, out);
284         cJSON_AddItemToArray(audioEncoderOutsJson, audioEncoderOutJson);
285     }
286     cJSON_AddItemToObject(jsonObject, OUTS.c_str(), audioEncoderOutsJson);
287 }
288 
FromJson(const cJSON * jsonObject,AudioEncoder & audioEncoder)289 void FromJson(const cJSON *jsonObject, AudioEncoder &audioEncoder)
290 {
291     if (!IsString(jsonObject, NAME)) {
292         AVTRANS_LOGE("AudioEncoder NAME is invalid");
293         return;
294     }
295     cJSON *nameObj = cJSON_GetObjectItemCaseSensitive(jsonObject, NAME.c_str());
296     if (nameObj == nullptr || !cJSON_IsString(nameObj)) {
297         return;
298     }
299     audioEncoder.name = nameObj->valuestring;
300 
301     cJSON *audioEncoderInsJson = cJSON_GetObjectItemCaseSensitive(jsonObject, INS.c_str());
302     if (audioEncoderInsJson == nullptr || !cJSON_IsArray(audioEncoderInsJson)) {
303         AVTRANS_LOGE("AudioEncoder INS is invalid");
304         return;
305     }
306     cJSON *inJson = nullptr;
307     cJSON_ArrayForEach(inJson, audioEncoderInsJson) {
308         AudioEncoderIn in;
309         FromJson(inJson, in);
310         audioEncoder.ins.push_back(in);
311     }
312 
313     cJSON *audioEncoderOutsJson = cJSON_GetObjectItemCaseSensitive(jsonObject, OUTS.c_str());
314     if (audioEncoderOutsJson == nullptr || !cJSON_IsArray(audioEncoderOutsJson)) {
315         AVTRANS_LOGE("AudioEncoder OUTS is invalid");
316         return;
317     }
318     cJSON *outJson = nullptr;
319     cJSON_ArrayForEach(outJson, audioEncoderOutsJson) {
320         AudioEncoderOut out;
321         FromJson(outJson, out);
322         audioEncoder.outs.push_back(out);
323     }
324 }
325 
ParseAudioDecoderIn(CapabilitySet & inCaps)326 std::vector<AudioDecoderIn> ParseAudioDecoderIn(CapabilitySet &inCaps)
327 {
328     std::vector<AudioDecoderIn> ins;
329     for (auto &cap : inCaps) {
330         AVTRANS_LOGD("AudioDecoderIn Raw: %{public}s", OHOS::Media::Pipeline::Capability2String(cap).c_str());
331         AudioDecoderIn in;
332         in.mime = cap.mime;
333         in.channel_layout = AnyCast<std::vector<AudioChannelLayout>>(cap.keys[CapabilityID::AUDIO_CHANNEL_LAYOUT]);
334         ins.push_back(in);
335     }
336 
337     return ins;
338 }
339 
ParseAudioDecoderOut(CapabilitySet & outCaps)340 std::vector<AudioDecoderOut> ParseAudioDecoderOut(CapabilitySet &outCaps)
341 {
342     std::vector<AudioDecoderOut> outs;
343     for (auto &cap : outCaps) {
344         AVTRANS_LOGD("AudioDecoderOut Raw: %{public}s", OHOS::Media::Pipeline::Capability2String(cap).c_str());
345         AudioDecoderOut out;
346         out.mime = cap.mime;
347         out.sample_fmt = AnyCast<std::vector<AudioSampleFormat>>(cap.keys[CapabilityID::AUDIO_SAMPLE_FORMAT]);
348         outs.push_back(out);
349     }
350 
351     return outs;
352 }
353 
QueryAudioDecoderAbility()354 std::vector<AudioDecoder> QueryAudioDecoderAbility()
355 {
356     std::vector<AudioDecoder> audioDecoders;
357     auto audioDecNameList = PluginManager::Instance().ListPlugins(PluginType::AUDIO_DECODER);
358     for (const std::string &name : audioDecNameList) {
359         if (find(AUDIO_DECODER_WANT.begin(), AUDIO_DECODER_WANT.end(), name) == AUDIO_DECODER_WANT.end()) {
360             AVTRANS_LOGI("AudioDecoder Plugin not want: %{public}s", name.c_str());
361             continue;
362         }
363         auto pluginInfo = PluginManager::Instance().GetPluginInfo(PluginType::AUDIO_DECODER, name);
364         if (pluginInfo == nullptr) {
365             AVTRANS_LOGE("pluginInfo is nullptr");
366             continue;
367         }
368         AudioDecoder audioDecoder;
369         audioDecoder.name = name;
370         audioDecoder.ins = ParseAudioDecoderIn(pluginInfo->inCaps);
371         audioDecoder.outs = ParseAudioDecoderOut(pluginInfo->outCaps);
372         audioDecoders.push_back(audioDecoder);
373     }
374 
375     return audioDecoders;
376 }
377 
ToJson(cJSON * jsonObject,const AudioDecoderIn & audioDecoderIn)378 void ToJson(cJSON *jsonObject, const AudioDecoderIn &audioDecoderIn)
379 {
380     if (jsonObject == nullptr) {
381         return;
382     }
383     cJSON_AddStringToObject(jsonObject, MIME.c_str(), audioDecoderIn.mime.c_str());
384     cJSON *channelLayoutJson = cJSON_CreateArray();
385     if (channelLayoutJson == nullptr) {
386         return;
387     }
388     for (auto &layout : audioDecoderIn.channel_layout) {
389         cJSON_AddItemToArray(channelLayoutJson, cJSON_CreateNumber(static_cast<uint64_t>(layout)));
390     }
391     cJSON_AddItemToObject(jsonObject, AUDIO_CHANNEL_LAYOUT.c_str(), channelLayoutJson);
392 }
393 
FromJson(const cJSON * jsonObject,AudioDecoderIn & audioDecoderIn)394 void FromJson(const cJSON *jsonObject, AudioDecoderIn &audioDecoderIn)
395 {
396     if (!IsString(jsonObject, MIME)) {
397         AVTRANS_LOGE("AudioDecoderIn MIME is invalid");
398         return;
399     }
400     cJSON *mimeObj = cJSON_GetObjectItemCaseSensitive(jsonObject, MIME.c_str());
401     if (mimeObj == nullptr || !cJSON_IsString(mimeObj)) {
402         return;
403     }
404     audioDecoderIn.mime = mimeObj->valuestring;
405 
406     cJSON *channelLayoutJson = cJSON_GetObjectItemCaseSensitive(jsonObject, AUDIO_CHANNEL_LAYOUT.c_str());
407     if (channelLayoutJson == nullptr || !cJSON_IsArray(channelLayoutJson)) {
408         AVTRANS_LOGE("AudioEncoder AUDIO_CHANNEL_LAYOUT is invalid");
409         return;
410     }
411     cJSON *layoutInfo = nullptr;
412     cJSON_ArrayForEach(layoutInfo, channelLayoutJson) {
413         if (layoutInfo && layoutInfo->type == cJSON_Number) {
414             audioDecoderIn.channel_layout.push_back(static_cast<AudioChannelLayout>(layoutInfo->valueint));
415         }
416     }
417 }
418 
ToJson(cJSON * jsonObject,const AudioDecoderOut & audioDecoderOut)419 void ToJson(cJSON *jsonObject, const AudioDecoderOut &audioDecoderOut)
420 {
421     if (jsonObject == nullptr) {
422         return;
423     }
424     cJSON_AddStringToObject(jsonObject, MIME.c_str(), audioDecoderOut.mime.c_str());
425     cJSON *sampleFormatsJson = cJSON_CreateArray();
426     if (sampleFormatsJson == nullptr) {
427         return;
428     }
429     for (auto &sampleFormat : audioDecoderOut.sample_fmt) {
430         cJSON_AddItemToArray(sampleFormatsJson, cJSON_CreateNumber(static_cast<uint8_t>(sampleFormat)));
431     }
432     cJSON_AddItemToObject(jsonObject, AUDIO_SAMPLE_FORMAT.c_str(), sampleFormatsJson);
433 }
434 
FromJson(const cJSON * jsonObject,AudioDecoderOut & audioDecoderOut)435 void FromJson(const cJSON *jsonObject, AudioDecoderOut &audioDecoderOut)
436 {
437     if (!IsString(jsonObject, MIME)) {
438         AVTRANS_LOGE("AudioDecoderOut MIME is invalid");
439         return;
440     }
441     cJSON *mimeObj = cJSON_GetObjectItemCaseSensitive(jsonObject, MIME.c_str());
442     if (mimeObj == nullptr || !cJSON_IsString(mimeObj)) {
443         return;
444     }
445     audioDecoderOut.mime = mimeObj->valuestring;
446 
447     cJSON *sampleFormatJson = cJSON_GetObjectItemCaseSensitive(jsonObject, AUDIO_SAMPLE_FORMAT.c_str());
448     if (sampleFormatJson == nullptr || !cJSON_IsArray(sampleFormatJson)) {
449         AVTRANS_LOGE("AudioDecoderOut AUDIO_SAMPLE_FORMAT is invalid");
450         return;
451     }
452 
453     cJSON *sampleInfo = nullptr;
454     cJSON_ArrayForEach(sampleInfo, sampleFormatJson) {
455         if (sampleInfo && sampleInfo->type == cJSON_Number) {
456             audioDecoderOut.sample_fmt.push_back(static_cast<AudioSampleFormat>(sampleInfo->valueint));
457         }
458     }
459 }
460 
ToJson(cJSON * jsonObject,const AudioDecoder & audioDecoder)461 void ToJson(cJSON *jsonObject, const AudioDecoder &audioDecoder)
462 {
463     if (jsonObject == nullptr) {
464         return;
465     }
466     cJSON_AddStringToObject(jsonObject, NAME.c_str(), audioDecoder.name.c_str());
467     cJSON *audioDecoderInsJson = cJSON_CreateArray();
468     if (audioDecoderInsJson == nullptr) {
469         return;
470     }
471 
472     for (const auto &in : audioDecoder.ins) {
473         cJSON *audioDecoderInJson = cJSON_CreateObject();
474         if (audioDecoderInJson == nullptr) {
475             cJSON_Delete(audioDecoderInsJson);
476             return;
477         }
478         ToJson(audioDecoderInJson, in);
479         cJSON_AddItemToArray(audioDecoderInsJson, audioDecoderInJson);
480     }
481     cJSON_AddItemToObject(jsonObject, INS.c_str(), audioDecoderInsJson);
482 
483     cJSON *audioDecoderOutsJson = cJSON_CreateArray();
484     if (audioDecoderOutsJson == nullptr) {
485         return;
486     }
487     for (const auto &out : audioDecoder.outs) {
488         cJSON *audioDecoderOutJson = cJSON_CreateObject();
489         if (audioDecoderOutJson == nullptr) {
490             cJSON_Delete(audioDecoderOutsJson);
491             return;
492         }
493         ToJson(audioDecoderOutJson, out);
494         cJSON_AddItemToArray(audioDecoderOutsJson, audioDecoderOutJson);
495     }
496     cJSON_AddItemToObject(jsonObject, OUTS.c_str(), audioDecoderOutsJson);
497 }
498 
FromJson(const cJSON * jsonObject,AudioDecoder & audioDecoder)499 void FromJson(const cJSON *jsonObject, AudioDecoder &audioDecoder)
500 {
501     if (!IsString(jsonObject, NAME)) {
502         AVTRANS_LOGE("AudioDecoder NAME is invalid");
503         return;
504     }
505     cJSON *nameObj = cJSON_GetObjectItemCaseSensitive(jsonObject, NAME.c_str());
506     if (nameObj == nullptr || !cJSON_IsString(nameObj)) {
507         return;
508     }
509     audioDecoder.name = nameObj->valuestring;
510 
511     cJSON *audioDecoderInsJson = cJSON_GetObjectItemCaseSensitive(jsonObject, INS.c_str());
512     if (audioDecoderInsJson == nullptr || !cJSON_IsArray(audioDecoderInsJson)) {
513         AVTRANS_LOGE("AudioDecoder INS is invalid");
514         return;
515     }
516     cJSON *inJson = nullptr;
517     cJSON_ArrayForEach(inJson, audioDecoderInsJson) {
518         AudioDecoderIn in;
519         FromJson(inJson, in);
520         audioDecoder.ins.push_back(in);
521     }
522     cJSON *audioDecoderOutsJson = cJSON_GetObjectItemCaseSensitive(jsonObject, OUTS.c_str());
523     if (audioDecoderOutsJson == nullptr || !cJSON_IsArray(audioDecoderOutsJson)) {
524         AVTRANS_LOGE("AudioDecoder OUTS is invalid");
525         return;
526     }
527     cJSON *outJson = nullptr;
528     cJSON_ArrayForEach(outJson, audioDecoderOutsJson) {
529         AudioDecoderOut out;
530         FromJson(outJson, out);
531         audioDecoder.outs.push_back(out);
532     }
533 }
534 
ParseVideoEncoderIn(CapabilitySet & inCaps)535 std::vector<VideoEncoderIn> ParseVideoEncoderIn(CapabilitySet &inCaps)
536 {
537     std::vector<VideoEncoderIn> ins;
538     for (auto &cap : inCaps) {
539         AVTRANS_LOGD("VideoEncoderIn Raw: %{public}s", OHOS::Media::Pipeline::Capability2String(cap).c_str());
540         VideoEncoderIn in;
541         in.mime = cap.mime;
542         in.pixel_fmt = AnyCast<std::vector<VideoPixelFormat>>(cap.keys[CapabilityID::VIDEO_PIXEL_FORMAT]);
543         ins.push_back(in);
544     }
545 
546     return ins;
547 }
548 
ParseVideoEncoderOut(CapabilitySet & outCaps)549 std::vector<VideoEncoderOut> ParseVideoEncoderOut(CapabilitySet &outCaps)
550 {
551     std::vector<VideoEncoderOut> outs;
552     for (auto &cap : outCaps) {
553         AVTRANS_LOGD("VideoEncoderOut Raw: %{public}s", OHOS::Media::Pipeline::Capability2String(cap).c_str());
554         VideoEncoderOut out;
555         out.mime = cap.mime;
556         outs.push_back(out);
557     }
558 
559     return outs;
560 }
561 
QueryVideoEncoderAbility()562 std::vector<VideoEncoder> QueryVideoEncoderAbility()
563 {
564     std::vector<VideoEncoder> videoEncoders;
565     auto videoEncNameList = PluginManager::Instance().ListPlugins(PluginType::VIDEO_ENCODER);
566     for (const std::string& name : videoEncNameList) {
567         if (find(VIDEO_ENCODER_WANT.begin(), VIDEO_ENCODER_WANT.end(), name) == VIDEO_ENCODER_WANT.end()) {
568             AVTRANS_LOGI("VideoEncoder Plugin not want: %{public}s", name.c_str());
569             continue;
570         }
571         auto pluginInfo = PluginManager::Instance().GetPluginInfo(PluginType::VIDEO_ENCODER, name);
572         if (pluginInfo == nullptr) {
573             AVTRANS_LOGE("pluginInfo is nullptr");
574             continue;
575         }
576         VideoEncoder videoEncoder;
577         videoEncoder.name = name;
578         videoEncoder.ins = ParseVideoEncoderIn(pluginInfo->inCaps);
579         videoEncoder.outs = ParseVideoEncoderOut(pluginInfo->outCaps);
580         videoEncoders.push_back(videoEncoder);
581     }
582 
583     return videoEncoders;
584 }
585 
ToJson(cJSON * jsonObject,const VideoEncoderIn & videoEncoderIn)586 void ToJson(cJSON *jsonObject, const VideoEncoderIn &videoEncoderIn)
587 {
588     if (jsonObject == nullptr) {
589         return;
590     }
591     cJSON_AddStringToObject(jsonObject, MIME.c_str(), videoEncoderIn.mime.c_str());
592     cJSON *pixelFmtJson = cJSON_CreateArray();
593     if (pixelFmtJson == nullptr) {
594         return;
595     }
596     for (auto &fmt : videoEncoderIn.pixel_fmt) {
597         cJSON_AddItemToArray(pixelFmtJson, cJSON_CreateNumber(static_cast<uint32_t>(fmt)));
598     }
599     cJSON_AddItemToObject(jsonObject, VIDEO_PIXEL_FMT.c_str(), pixelFmtJson);
600 }
601 
FromJson(const cJSON * jsonObject,VideoEncoderIn & videoEncoderIn)602 void FromJson(const cJSON *jsonObject, VideoEncoderIn &videoEncoderIn)
603 {
604     if (jsonObject == nullptr) {
605         return;
606     }
607     if (!IsString(jsonObject, MIME)) {
608         AVTRANS_LOGE("VideoEncoderIn MIME is invalid");
609         return;
610     }
611     cJSON *mimeObj = cJSON_GetObjectItemCaseSensitive(jsonObject, MIME.c_str());
612     if (mimeObj == nullptr || !cJSON_IsString(mimeObj)) {
613         return;
614     }
615     videoEncoderIn.mime = mimeObj->valuestring;
616 
617     cJSON *videoPixelFmtJson = cJSON_GetObjectItemCaseSensitive(jsonObject, VIDEO_PIXEL_FMT.c_str());
618     if (videoPixelFmtJson == nullptr || !cJSON_IsArray(videoPixelFmtJson)) {
619         AVTRANS_LOGE("VideoEncoderIn VIDEO_PIXEL_FMT is invalid");
620         return;
621     }
622     cJSON *fmt = nullptr;
623     cJSON_ArrayForEach(fmt, videoPixelFmtJson) {
624         if (fmt && fmt->type == cJSON_Number) {
625             videoEncoderIn.pixel_fmt.push_back(static_cast<VideoPixelFormat>(fmt->valueint));
626         }
627     }
628 }
629 
ToJson(cJSON * jsonObject,const VideoEncoderOut & videoEncoderOut)630 void ToJson(cJSON *jsonObject, const VideoEncoderOut &videoEncoderOut)
631 {
632     if (jsonObject == nullptr) {
633         return;
634     }
635     cJSON_AddStringToObject(jsonObject, MIME.c_str(), videoEncoderOut.mime.c_str());
636 }
637 
FromJson(const cJSON * jsonObject,VideoEncoderOut & videoEncoderOut)638 void FromJson(const cJSON *jsonObject, VideoEncoderOut &videoEncoderOut)
639 {
640     if (jsonObject == nullptr) {
641         return;
642     }
643     if (!IsString(jsonObject, MIME)) {
644         AVTRANS_LOGE("VideoEncoderOut MIME is invalid");
645         return;
646     }
647     cJSON *mimeObj = cJSON_GetObjectItemCaseSensitive(jsonObject, MIME.c_str());
648     if (mimeObj == nullptr || !cJSON_IsString(mimeObj)) {
649         return;
650     }
651     videoEncoderOut.mime = mimeObj->valuestring;
652 }
653 
ToJson(cJSON * jsonObject,const VideoEncoder & videoEncoder)654 void ToJson(cJSON *jsonObject, const VideoEncoder &videoEncoder)
655 {
656     if (jsonObject == nullptr) {
657         return;
658     }
659     cJSON_AddStringToObject(jsonObject, NAME.c_str(), videoEncoder.name.c_str());
660     cJSON *videoEncoderInsJson = cJSON_CreateArray();
661     if (videoEncoderInsJson == nullptr) {
662         return;
663     }
664     for (const auto &in : videoEncoder.ins) {
665         cJSON *videoEncoderInJson = cJSON_CreateObject();
666         if (videoEncoderInJson == nullptr) {
667             cJSON_Delete(videoEncoderInsJson);
668             return;
669         }
670         ToJson(videoEncoderInJson, in);
671         cJSON_AddItemToArray(videoEncoderInsJson, videoEncoderInJson);
672     }
673     cJSON_AddItemToObject(jsonObject, INS.c_str(), videoEncoderInsJson);
674 
675     cJSON *videoEncoderOutsJson = cJSON_CreateArray();
676     if (videoEncoderOutsJson == nullptr) {
677         return;
678     }
679     for (const auto &out : videoEncoder.outs) {
680         cJSON *videoEncoderOutJson = cJSON_CreateObject();
681         if (videoEncoderOutJson == nullptr) {
682             cJSON_Delete(videoEncoderOutsJson);
683             return;
684         }
685         ToJson(videoEncoderOutJson, out);
686         cJSON_AddItemToArray(videoEncoderOutsJson, videoEncoderOutJson);
687     }
688     cJSON_AddItemToObject(jsonObject, OUTS.c_str(), videoEncoderOutsJson);
689 }
690 
FromJson(const cJSON * jsonObject,VideoEncoder & videoEncoder)691 void FromJson(const cJSON *jsonObject, VideoEncoder &videoEncoder)
692 {
693     if (jsonObject == nullptr) {
694         return;
695     }
696     if (!IsString(jsonObject, NAME)) {
697         AVTRANS_LOGE("VideoEncoder NAME is invalid");
698         return;
699     }
700     cJSON *nameObj = cJSON_GetObjectItemCaseSensitive(jsonObject, NAME.c_str());
701     if (nameObj == nullptr || !cJSON_IsString(nameObj)) {
702         return;
703     }
704     videoEncoder.name = nameObj->valuestring;
705 
706     cJSON *videoEncoderInsJson = cJSON_GetObjectItemCaseSensitive(jsonObject, INS.c_str());
707     if (videoEncoderInsJson == nullptr || !cJSON_IsArray(videoEncoderInsJson)) {
708         AVTRANS_LOGE("VideoEncoder INS is invalid");
709         return;
710     }
711     cJSON *inJson = nullptr;
712     cJSON_ArrayForEach(inJson, videoEncoderInsJson) {
713         VideoEncoderIn in;
714         FromJson(inJson, in);
715         videoEncoder.ins.push_back(in);
716     }
717 
718     cJSON *videoEncoderOutsJson = cJSON_GetObjectItemCaseSensitive(jsonObject, OUTS.c_str());
719     if (videoEncoderOutsJson == nullptr || !cJSON_IsArray(videoEncoderOutsJson)) {
720         AVTRANS_LOGE("VideoEncoder OUTS is invalid");
721         return;
722     }
723     cJSON *outJson = nullptr;
724     cJSON_ArrayForEach(outJson, videoEncoderOutsJson) {
725         VideoEncoderOut out;
726         FromJson(outJson, out);
727         videoEncoder.outs.push_back(out);
728     }
729 }
730 
ParseVideoDecoderIn(CapabilitySet & inCaps)731 std::vector<VideoDecoderIn> ParseVideoDecoderIn(CapabilitySet &inCaps)
732 {
733     std::vector<VideoDecoderIn> ins;
734     for (auto &cap : inCaps) {
735         AVTRANS_LOGD("VideoDecoderIn Raw: %{public}s", OHOS::Media::Pipeline::Capability2String(cap).c_str());
736         VideoDecoderIn in;
737         in.mime = cap.mime;
738         in.vd_bit_stream_fmt =
739             AnyCast<std::vector<VideoBitStreamFormat>>(cap.keys[CapabilityID::VIDEO_BIT_STREAM_FORMAT]);
740         ins.push_back(in);
741     }
742 
743     return ins;
744 }
745 
ParseVideoDecoderOut(CapabilitySet & outCaps)746 std::vector<VideoDecoderOut> ParseVideoDecoderOut(CapabilitySet &outCaps)
747 {
748     std::vector<VideoDecoderOut> outs;
749     for (auto &cap : outCaps) {
750         AVTRANS_LOGD("VideoDecoderOut Raw: %{public}s", OHOS::Media::Pipeline::Capability2String(cap).c_str());
751         VideoDecoderOut out;
752         out.mime = cap.mime;
753         out.pixel_fmt = AnyCast<std::vector<VideoPixelFormat>>(cap.keys[CapabilityID::VIDEO_PIXEL_FORMAT]);
754         outs.push_back(out);
755     }
756 
757     return outs;
758 }
759 
QueryVideoDecoderAbility()760 std::vector<VideoDecoder> QueryVideoDecoderAbility()
761 {
762     std::vector<VideoDecoder> VideoDecoders;
763     auto videoDecNameList = PluginManager::Instance().ListPlugins(PluginType::VIDEO_DECODER);
764     for (const std::string& name : videoDecNameList) {
765         if (find(VIDEO_DECODER_WANT.begin(), VIDEO_DECODER_WANT.end(), name) == VIDEO_DECODER_WANT.end()) {
766             AVTRANS_LOGI("VideoDecoder Plugin not want: %{public}s", name.c_str());
767             continue;
768         }
769         auto pluginInfo = PluginManager::Instance().GetPluginInfo(PluginType::VIDEO_DECODER, name);
770         if (pluginInfo == nullptr) {
771             AVTRANS_LOGE("pluginInfo is nullptr");
772             continue;
773         }
774         VideoDecoder videoDecoder;
775         videoDecoder.name = name;
776         videoDecoder.ins = ParseVideoDecoderIn(pluginInfo->inCaps);
777         videoDecoder.outs = ParseVideoDecoderOut(pluginInfo->outCaps);
778         VideoDecoders.push_back(videoDecoder);
779     }
780 
781     return VideoDecoders;
782 }
783 
ToJson(cJSON * jsonObject,const VideoDecoderIn & videoDecoderIn)784 void ToJson(cJSON *jsonObject, const VideoDecoderIn &videoDecoderIn)
785 {
786     if (jsonObject == nullptr) {
787         return;
788     }
789     cJSON_AddStringToObject(jsonObject, MIME.c_str(), videoDecoderIn.mime.c_str());
790     cJSON *fmtJson = cJSON_CreateArray();
791     if (fmtJson == nullptr) {
792         return;
793     }
794     for (auto &fmt : videoDecoderIn.vd_bit_stream_fmt) {
795         cJSON_AddItemToArray(fmtJson, cJSON_CreateNumber(static_cast<uint32_t>(fmt)));
796     }
797     cJSON_AddItemToObject(jsonObject, VIDEO_BIT_STREAM_FMT.c_str(), fmtJson);
798 }
799 
FromJson(const cJSON * jsonObject,VideoDecoderIn & videoDecoderIn)800 void FromJson(const cJSON *jsonObject, VideoDecoderIn &videoDecoderIn)
801 {
802     if (jsonObject == nullptr) {
803         return;
804     }
805     if (!IsString(jsonObject, MIME)) {
806         AVTRANS_LOGE("VideoDecoderIn MIME is invalid");
807         return;
808     }
809     cJSON *mimeObj = cJSON_GetObjectItemCaseSensitive(jsonObject, MIME.c_str());
810     if (mimeObj == nullptr || !cJSON_IsString(mimeObj)) {
811         return;
812     }
813     videoDecoderIn.mime = mimeObj->valuestring;
814 
815     cJSON *videoBitStreamJson = cJSON_GetObjectItemCaseSensitive(jsonObject, VIDEO_BIT_STREAM_FMT.c_str());
816     if (videoBitStreamJson == nullptr || !cJSON_IsArray(videoBitStreamJson)) {
817         AVTRANS_LOGE("VideoDecoderIn VIDEO_BIT_STREAM_FMT is invalid");
818         return;
819     }
820     cJSON *fmt = nullptr;
821     cJSON_ArrayForEach(fmt, videoBitStreamJson) {
822         if (fmt->type == cJSON_Number) {
823             videoDecoderIn.vd_bit_stream_fmt.push_back(static_cast<VideoBitStreamFormat>(fmt->valueint));
824         }
825     }
826 }
827 
ToJson(cJSON * jsonObject,const VideoDecoderOut & videoDecoderOut)828 void ToJson(cJSON *jsonObject, const VideoDecoderOut &videoDecoderOut)
829 {
830     if (jsonObject == nullptr) {
831         return;
832     }
833     cJSON_AddStringToObject(jsonObject, MIME.c_str(), videoDecoderOut.mime.c_str());
834     cJSON *fmtJson = cJSON_CreateArray();
835     if (fmtJson == nullptr) {
836         return;
837     }
838     for (auto &fmt : videoDecoderOut.pixel_fmt) {
839         cJSON_AddItemToArray(fmtJson, cJSON_CreateNumber(static_cast<uint32_t>(fmt)));
840     }
841     cJSON_AddItemToObject(jsonObject, VIDEO_PIXEL_FMT.c_str(), fmtJson);
842 }
843 
FromJson(const cJSON * jsonObject,VideoDecoderOut & videoDecoderOut)844 void FromJson(const cJSON *jsonObject, VideoDecoderOut &videoDecoderOut)
845 {
846     if (jsonObject == nullptr) {
847         return;
848     }
849     if (!IsString(jsonObject, MIME)) {
850         AVTRANS_LOGE("VideoDecoderOut MIME is invalid");
851         return;
852     }
853     cJSON *mimeObj = cJSON_GetObjectItemCaseSensitive(jsonObject, MIME.c_str());
854     if (mimeObj == nullptr || !cJSON_IsString(mimeObj)) {
855         return;
856     }
857     videoDecoderOut.mime = mimeObj->valuestring;
858 
859     cJSON *videoPixelFmtJson = cJSON_GetObjectItemCaseSensitive(jsonObject, VIDEO_PIXEL_FMT.c_str());
860     if (videoPixelFmtJson == nullptr || !cJSON_IsArray(videoPixelFmtJson)) {
861         AVTRANS_LOGE("VideoDecoderOut VIDEO_PIXEL_FMT is invalid");
862         return;
863     }
864     cJSON *fmtInfo = nullptr;
865     cJSON_ArrayForEach(fmtInfo, videoPixelFmtJson) {
866         if (fmtInfo && fmtInfo->type == cJSON_Number) {
867             videoDecoderOut.pixel_fmt.push_back(static_cast<VideoPixelFormat>(fmtInfo->valueint));
868         }
869     }
870 }
871 
ToJson(cJSON * jsonObject,const VideoDecoder & videoDecoder)872 void ToJson(cJSON *jsonObject, const VideoDecoder &videoDecoder)
873 {
874     if (jsonObject == nullptr) {
875         return;
876     }
877     cJSON_AddStringToObject(jsonObject, NAME.c_str(), videoDecoder.name.c_str());
878     cJSON *videoDecoderInsJson = cJSON_CreateArray();
879     if (videoDecoderInsJson == nullptr) {
880         return;
881     }
882     for (const auto &in : videoDecoder.ins) {
883         cJSON *videoDecoderInJson = cJSON_CreateObject();
884         if (videoDecoderInJson == nullptr) {
885             cJSON_Delete(videoDecoderInsJson);
886             return;
887         }
888         ToJson(videoDecoderInJson, in);
889         cJSON_AddItemToArray(videoDecoderInsJson, videoDecoderInJson);
890     }
891     cJSON_AddItemToObject(jsonObject, INS.c_str(), videoDecoderInsJson);
892 
893     cJSON *videoDecoderOutsJson = cJSON_CreateArray();
894     if (videoDecoderOutsJson == nullptr) {
895         return;
896     }
897     for (const auto &out : videoDecoder.outs) {
898         cJSON *videoDecoderOutJson = cJSON_CreateObject();
899         if (videoDecoderOutJson == nullptr) {
900             cJSON_Delete(videoDecoderOutsJson);
901             return;
902         }
903         ToJson(videoDecoderOutJson, out);
904         cJSON_AddItemToArray(videoDecoderOutsJson, videoDecoderOutJson);
905     }
906     cJSON_AddItemToObject(jsonObject, OUTS.c_str(), videoDecoderOutsJson);
907 }
908 
FromJson(const cJSON * jsonObject,VideoDecoder & videoDecoder)909 void FromJson(const cJSON *jsonObject, VideoDecoder &videoDecoder)
910 {
911     if (jsonObject == nullptr) {
912         return;
913     }
914     if (!IsString(jsonObject, NAME)) {
915         AVTRANS_LOGE("VideoDecoder NAME is invalid");
916         return;
917     }
918     cJSON *nameObj = cJSON_GetObjectItemCaseSensitive(jsonObject, NAME.c_str());
919     if (nameObj == nullptr || !cJSON_IsString(nameObj)) {
920         return;
921     }
922     videoDecoder.name = nameObj->valuestring;
923 
924     cJSON *videoDecoderInsJson = cJSON_GetObjectItemCaseSensitive(jsonObject, INS.c_str());
925     if (videoDecoderInsJson == nullptr || !cJSON_IsArray(videoDecoderInsJson)) {
926         AVTRANS_LOGE("VideoDecoder INS is invalid");
927         return;
928     }
929 
930     cJSON *inJson = nullptr;
931     cJSON_ArrayForEach(inJson, videoDecoderInsJson) {
932         VideoDecoderIn in;
933         FromJson(inJson, in);
934         videoDecoder.ins.push_back(in);
935     }
936 
937     cJSON *videoDecoderOutsJson = cJSON_GetObjectItemCaseSensitive(jsonObject, OUTS.c_str());
938     if (videoDecoderOutsJson == nullptr || !cJSON_IsArray(videoDecoderOutsJson)) {
939         AVTRANS_LOGE("VideoDecoder OUTS is invalid");
940         return;
941     }
942     cJSON *outJson = nullptr;
943     cJSON_ArrayForEach(outJson, videoDecoderOutsJson) {
944         VideoDecoderOut out;
945         FromJson(outJson, out);
946         videoDecoder.outs.push_back(out);
947     }
948 }
949 
950 template<typename T>
ToJson(const std::string & key,cJSON * jsonObject,std::vector<T> & objs)951 void ToJson(const std::string &key, cJSON *jsonObject, std::vector<T> &objs)
952 {
953     cJSON *jsonObjs = cJSON_CreateArray();
954     if (jsonObjs == nullptr) {
955         return;
956     }
957     for (auto &obj : objs) {
958         cJSON *jsonObj = cJSON_CreateObject();
959         if (jsonObj == nullptr) {
960             cJSON_Delete(jsonObjs);
961             return;
962         }
963         ToJson(jsonObj, obj);
964         cJSON_AddItemToArray(jsonObjs, jsonObj);
965     }
966     cJSON_AddItemToObject(jsonObject, key.c_str(), jsonObjs);
967 }
968 
969 template<typename T>
FromJson(const std::string & key,const cJSON * jsonObject,std::vector<T> & objs)970 void FromJson(const std::string &key, const cJSON *jsonObject, std::vector<T> &objs)
971 {
972     cJSON *objJson = cJSON_GetObjectItemCaseSensitive(jsonObject, key.c_str());
973     if (objJson == nullptr || !cJSON_IsArray(objJson)) {
974         AVTRANS_LOGE("JSONObject key invalid, key: %{public}s", key.c_str());
975         return;
976     }
977     cJSON *json = nullptr;
978     cJSON_ArrayForEach(json, objJson) {
979         T obj;
980         FromJson(json, obj);
981         objs.push_back(obj);
982     }
983 }
984 
QueryAudioEncoderAbilityStr(char * res)985 int32_t QueryAudioEncoderAbilityStr(char* res)
986 {
987     std::vector<AudioEncoder> audioEncoders = QueryAudioEncoderAbility();
988     cJSON *jsonObject = cJSON_CreateObject();
989     if (jsonObject == nullptr) {
990         return 0;
991     }
992     ToJson<AudioEncoder>(AUDIO_ENCODERS, jsonObject, audioEncoders);
993     char *jsonStr = cJSON_PrintUnformatted(jsonObject);
994     if (jsonStr == nullptr) {
995         cJSON_Delete(jsonObject);
996         return 0;
997     }
998     std::string ret = std::string(jsonStr);
999     cJSON_free(jsonStr);
1000     cJSON_Delete(jsonObject);
1001     AVTRANS_LOGI("QueryAudioEncoderAbilityStr ret: %{public}s", ret.c_str());
1002     if (ret.length() > MAX_MESSAGES_LEN) {
1003         AVTRANS_LOGE("QueryAudioEncoderAbilityStr too long");
1004         return 0;
1005     }
1006     if (memcpy_s(res, MAX_MESSAGES_LEN, ret.c_str(), ret.length()) != EOK) {
1007         return 0;
1008     }
1009     return ret.length();
1010 }
1011 
QueryAudioDecoderAbilityStr(char * res)1012 int32_t QueryAudioDecoderAbilityStr(char* res)
1013 {
1014     std::vector<AudioDecoder> audioDecoders = QueryAudioDecoderAbility();
1015     cJSON *jsonObject = cJSON_CreateObject();
1016     if (jsonObject == nullptr) {
1017         return 0;
1018     }
1019     ToJson<AudioDecoder>(AUDIO_DECODERS, jsonObject, audioDecoders);
1020     char *jsonStr = cJSON_PrintUnformatted(jsonObject);
1021     if (jsonStr == nullptr) {
1022         cJSON_Delete(jsonObject);
1023         return 0;
1024     }
1025     std::string ret = std::string(jsonStr);
1026     cJSON_free(jsonStr);
1027     cJSON_Delete(jsonObject);
1028     AVTRANS_LOGI("QueryAudioDecoderAbilityStr ret: %{public}s", ret.c_str());
1029     if (ret.length() > MAX_MESSAGES_LEN) {
1030         AVTRANS_LOGE("QueryAudioDecoderAbilityStr too long");
1031         return 0;
1032     }
1033     if (memcpy_s(res, MAX_MESSAGES_LEN, ret.c_str(), ret.length()) != EOK) {
1034         return 0;
1035     }
1036     return ret.length();
1037 }
1038 
QueryVideoEncoderAbilityStr(char * res)1039 int32_t QueryVideoEncoderAbilityStr(char* res)
1040 {
1041     std::vector<VideoEncoder> viudeoEncoders = QueryVideoEncoderAbility();
1042     cJSON *jsonObject = cJSON_CreateObject();
1043     if (jsonObject == nullptr) {
1044         return 0;
1045     }
1046     ToJson<VideoEncoder>(VIDEO_ENCODERS, jsonObject, viudeoEncoders);
1047     char *jsonStr = cJSON_PrintUnformatted(jsonObject);
1048     if (jsonStr == nullptr) {
1049         cJSON_Delete(jsonObject);
1050         return 0;
1051     }
1052     std::string ret = std::string(jsonStr);
1053     cJSON_free(jsonStr);
1054     cJSON_Delete(jsonObject);
1055     AVTRANS_LOGI("QueryVideoEncoderAbilityStr ret: %{public}s", ret.c_str());
1056     if (ret.length() > MAX_MESSAGES_LEN) {
1057         AVTRANS_LOGE("QueryVideoEncoderAbilityStr too long");
1058         return 0;
1059     }
1060     if (memcpy_s(res, MAX_MESSAGES_LEN, ret.c_str(), ret.length()) != EOK) {
1061         return 0;
1062     }
1063     return ret.length();
1064 }
1065 
QueryVideoDecoderAbilityStr(char * res)1066 int32_t QueryVideoDecoderAbilityStr(char* res)
1067 {
1068     std::vector<VideoDecoder> videoDecoders = QueryVideoDecoderAbility();
1069     cJSON *jsonObject = cJSON_CreateObject();
1070     if (jsonObject == nullptr) {
1071         return 0;
1072     }
1073     ToJson<VideoDecoder>(VIDEO_DECODERS, jsonObject, videoDecoders);
1074     char *jsonStr = cJSON_PrintUnformatted(jsonObject);
1075     if (jsonStr == nullptr) {
1076         cJSON_Delete(jsonObject);
1077         return 0;
1078     }
1079     std::string ret = std::string(jsonStr);
1080     cJSON_free(jsonStr);
1081     cJSON_Delete(jsonObject);
1082     AVTRANS_LOGI("QueryVideoDecoderAbilityStr ret: %{public}s", ret.c_str());
1083     if (ret.length() > MAX_MESSAGES_LEN) {
1084         AVTRANS_LOGE("QueryVideoDecoderAbilityStr too long");
1085         return 0;
1086     }
1087     if (memcpy_s(res, MAX_MESSAGES_LEN, ret.c_str(), ret.length()) != EOK) {
1088         return 0;
1089     }
1090     return ret.length();
1091 }
1092 }
1093 }