• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 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_parser.h"
17 #include "dh_utils_tool.h"
18 #include "distributed_hardware_log.h"
19 
20 
21 namespace OHOS {
22 namespace DistributedHardware {
23 namespace {
24     constexpr const char *NAME = "name";
25     constexpr const char *INS = "ins";
26     constexpr const char *OUTS = "outs";
27     constexpr const char *MIME = "mime";
28     constexpr const char *SAMPLE_RATE = "sample_rate";
29     constexpr const char *AUDIO_SAMPLE_FORMAT = "sample_fmt";
30     constexpr const char *AD_MPEG_VER = "ad_mpeg_ver";
31     constexpr const char *AUDIO_AAC_PROFILE = "aac_profile";
32     constexpr const char *AUDIO_AAC_STREAM_FORMAT = "aac_stream_fmt";
33     constexpr const char *AUDIO_CHANNEL_LAYOUT = "channel_layout";
34     constexpr const char *VIDEO_PIXEL_FMT = "pixel_fmt";
35     constexpr const char *VIDEO_BIT_STREAM_FMT = "vd_bit_stream_fmt";
36 }
37 
FromJson(const cJSON * jsonObject,AudioEncoderIn & audioEncoderIn)38 void FromJson(const cJSON *jsonObject, AudioEncoderIn &audioEncoderIn)
39 {
40     if (jsonObject == nullptr) {
41         DHLOGE("Json pointer is nullptr!");
42         return;
43     }
44     cJSON *mimeJsonObj = cJSON_GetObjectItem(jsonObject, MIME);
45     if (!IsString(mimeJsonObj)) {
46         DHLOGE("AudioEncoderIn MIME is invalid!");
47         return;
48     }
49     audioEncoderIn.mime = mimeJsonObj->valuestring;
50 
51     cJSON *sampleRate = cJSON_GetObjectItem(jsonObject, SAMPLE_RATE);
52     if (!IsArray(sampleRate)) {
53         DHLOGE("AudioEncoderIn SAMPLE_RATE is invalid!");
54         return;
55     }
56     cJSON *sampleRateItem = nullptr;
57     cJSON_ArrayForEach(sampleRateItem, sampleRate) {
58         if (sampleRateItem && sampleRateItem->type == cJSON_Number) {
59             audioEncoderIn.sample_rate.push_back((uint32_t)sampleRateItem->valuedouble);
60         }
61     }
62 }
63 
FromJson(const cJSON * jsonObject,AudioEncoderOut & audioEncoderOut)64 void FromJson(const cJSON *jsonObject, AudioEncoderOut &audioEncoderOut)
65 {
66     if (jsonObject == nullptr) {
67         DHLOGE("Json pointer is nullptr!");
68         return;
69     }
70     cJSON *mimeJsonObj = cJSON_GetObjectItem(jsonObject, MIME);
71     if (!IsString(mimeJsonObj)) {
72         DHLOGE("AudioEncoderOut MIME is invalid!");
73         return;
74     }
75     audioEncoderOut.mime = mimeJsonObj->valuestring;
76 
77     cJSON *mpegVerJsonObj = cJSON_GetObjectItem(jsonObject, AD_MPEG_VER);
78     if (!IsUInt32(mpegVerJsonObj)) {
79         DHLOGE("AudioEncoderOut AD_MPEG_VER is invalid!");
80         return;
81     }
82     audioEncoderOut.ad_mpeg_ver = static_cast<uint32_t>(mpegVerJsonObj->valuedouble);
83 
84     cJSON *aacProfileJsonObj = cJSON_GetObjectItem(jsonObject, AUDIO_AAC_PROFILE);
85     if (!IsUInt8(aacProfileJsonObj)) {
86         DHLOGE("AudioEncoderOut AUDIO_AAC_PROFILE is invalid!");
87         return;
88     }
89     audioEncoderOut.aac_profile = (AudioAacProfile)aacProfileJsonObj->valuedouble;
90 
91     cJSON *aacStreamFmtJsonObj = cJSON_GetObjectItem(jsonObject, AUDIO_AAC_STREAM_FORMAT);
92     if (!IsUInt8(aacStreamFmtJsonObj)) {
93         DHLOGE("AudioEncoderOut AUDIO_AAC_STREAM_FORMAT is invalid!");
94         return;
95     }
96     audioEncoderOut.aac_stm_fmt = (AudioAacStreamFormat)aacStreamFmtJsonObj->valuedouble;
97 }
98 
FromJson(const cJSON * jsonObject,AudioEncoder & audioEncoder)99 void FromJson(const cJSON *jsonObject, AudioEncoder &audioEncoder)
100 {
101     if (jsonObject == nullptr) {
102         DHLOGE("Json pointer is nullptr!");
103         return;
104     }
105     cJSON *nameJsonObj = cJSON_GetObjectItem(jsonObject, NAME);
106     if (!IsString(nameJsonObj)) {
107         DHLOGE("AudioEncoder NAME is invalid!");
108         return;
109     }
110     audioEncoder.name = nameJsonObj->valuestring;
111 
112     cJSON *insJson = cJSON_GetObjectItem(jsonObject, INS);
113     if (!IsArray(insJson)) {
114         DHLOGE("AudioEncoder INS is invalid!");
115         return;
116     }
117     cJSON *inJson = nullptr;
118     cJSON_ArrayForEach(inJson, insJson) {
119         AudioEncoderIn in;
120         FromJson(inJson, in);
121         audioEncoder.ins.push_back(in);
122     }
123 
124     cJSON *outsJson = cJSON_GetObjectItem(jsonObject, OUTS);
125     if (!IsArray(outsJson)) {
126         DHLOGE("AudioEncoder OUTS is invalid!");
127         return;
128     }
129     cJSON *outJson = nullptr;
130     cJSON_ArrayForEach(outJson, outsJson) {
131         AudioEncoderOut out;
132         FromJson(outJson, out);
133         audioEncoder.outs.push_back(out);
134     }
135 }
136 
FromJson(const cJSON * jsonObject,AudioDecoderIn & audioDecoderIn)137 void FromJson(const cJSON *jsonObject, AudioDecoderIn &audioDecoderIn)
138 {
139     if (jsonObject == nullptr) {
140         DHLOGE("Json pointer is nullptr!");
141         return;
142     }
143     cJSON *mimeJsonObj = cJSON_GetObjectItem(jsonObject, MIME);
144     if (!IsString(mimeJsonObj)) {
145         DHLOGE("AudioDecoderIn MIME is invalid!");
146         return;
147     }
148     audioDecoderIn.mime = mimeJsonObj->valuestring;
149 
150     cJSON *channelLayoutJson = cJSON_GetObjectItem(jsonObject, AUDIO_CHANNEL_LAYOUT);
151     if (!IsArray(channelLayoutJson)) {
152         DHLOGE("AudioDecoder AUDIO_CHANNEL_LAYOUT is invalid!");
153         return;
154     }
155     const cJSON *layout = nullptr;
156     cJSON_ArrayForEach(layout, channelLayoutJson) {
157         if (layout && layout->type == cJSON_Number) {
158             audioDecoderIn.channel_layout.push_back((AudioChannelLayout)layout->valuedouble);
159         }
160     }
161 }
162 
FromJson(const cJSON * jsonObject,AudioDecoderOut & audioDecoderOut)163 void FromJson(const cJSON *jsonObject, AudioDecoderOut &audioDecoderOut)
164 {
165     if (jsonObject == nullptr) {
166         DHLOGE("Json pointer is nullptr!");
167         return;
168     }
169     cJSON *mimeJsonObj = cJSON_GetObjectItem(jsonObject, MIME);
170     if (!IsString(mimeJsonObj)) {
171         DHLOGE("AudioDecoderOut MIME is invalid!");
172         return;
173     }
174     audioDecoderOut.mime = mimeJsonObj->valuestring;
175 
176     cJSON *sampleFormatJson = cJSON_GetObjectItem(jsonObject, AUDIO_SAMPLE_FORMAT);
177     if (!IsArray(sampleFormatJson)) {
178         DHLOGE("AudioDecoderOut AUDIO_SAMPLE_FORMAT is invalid!");
179         return;
180     }
181     cJSON *format = nullptr;
182     cJSON_ArrayForEach(format, sampleFormatJson) {
183         if (format && format->type == cJSON_Number) {
184             audioDecoderOut.sample_fmt.push_back((AudioSampleFormat)format->valuedouble);
185         }
186     }
187 }
188 
FromJson(const cJSON * jsonObject,AudioDecoder & audioDecoder)189 void FromJson(const cJSON *jsonObject, AudioDecoder &audioDecoder)
190 {
191     if (jsonObject == nullptr) {
192         DHLOGE("Json pointer is nullptr!");
193         return;
194     }
195     cJSON *nameJsonObj = cJSON_GetObjectItem(jsonObject, NAME);
196     if (!IsString(nameJsonObj)) {
197         DHLOGE("AudioDecoderOut MIME is invalid!");
198         return;
199     }
200     audioDecoder.name = nameJsonObj->valuestring;
201 
202     cJSON *insJson = cJSON_GetObjectItem(jsonObject, INS);
203     if (!IsArray(insJson)) {
204         DHLOGE("AudioDecoder INS is invalid!");
205         return;
206     }
207     cJSON *inJson = nullptr;
208     cJSON_ArrayForEach(inJson, insJson) {
209         AudioDecoderIn in;
210         FromJson(inJson, in);
211         audioDecoder.ins.push_back(in);
212     }
213 
214     cJSON *outsJson = cJSON_GetObjectItem(jsonObject, OUTS);
215     if (!IsArray(outsJson)) {
216         DHLOGE("AudioDecoder OUTS is invalid!");
217         return;
218     }
219     cJSON *outJson = nullptr;
220     cJSON_ArrayForEach(outJson, outsJson) {
221         AudioDecoderOut out;
222         FromJson(outJson, out);
223         audioDecoder.outs.push_back(out);
224     }
225 }
226 
FromJson(const cJSON * jsonObject,VideoEncoderIn & videoEncoderIn)227 void FromJson(const cJSON *jsonObject, VideoEncoderIn &videoEncoderIn)
228 {
229     if (jsonObject == nullptr) {
230         DHLOGE("Json pointer is nullptr!");
231         return;
232     }
233     cJSON *mimeJsonObj = cJSON_GetObjectItem(jsonObject, MIME);
234     if (!IsString(mimeJsonObj)) {
235         DHLOGE("VideoEncoderIn MIME is invalid!");
236         return;
237     }
238     videoEncoderIn.mime = mimeJsonObj->valuestring;
239 
240     cJSON *videoPixelFmt = cJSON_GetObjectItem(jsonObject, VIDEO_PIXEL_FMT);
241     if (!IsArray(videoPixelFmt)) {
242         DHLOGE("VideoEncoderIn VIDEO_PIXEL_FMT is invalid!");
243         return;
244     }
245     cJSON *pixelFmt = nullptr;
246     cJSON_ArrayForEach(pixelFmt, videoPixelFmt) {
247         if (pixelFmt && pixelFmt->type == cJSON_Number) {
248             videoEncoderIn.pixel_fmt.push_back((VideoPixelFormat)pixelFmt->valuedouble);
249         }
250     }
251 }
252 
FromJson(const cJSON * jsonObject,VideoEncoderOut & videoEncoderOut)253 void FromJson(const cJSON *jsonObject, VideoEncoderOut &videoEncoderOut)
254 {
255     if (jsonObject == nullptr) {
256         DHLOGE("Json pointer is nullptr!");
257         return;
258     }
259     cJSON *mimeJsonObj = cJSON_GetObjectItem(jsonObject, MIME);
260     if (!IsString(mimeJsonObj)) {
261         DHLOGE("VideoEncoderOut MIME is invalid!");
262         return;
263     }
264     videoEncoderOut.mime = mimeJsonObj->valuestring;
265 }
266 
FromJson(const cJSON * jsonObject,VideoEncoder & videoEncoder)267 void FromJson(const cJSON *jsonObject, VideoEncoder &videoEncoder)
268 {
269     if (jsonObject == nullptr) {
270         DHLOGE("Json pointer is nullptr!");
271         return;
272     }
273     cJSON *nameJsonObj = cJSON_GetObjectItem(jsonObject, NAME);
274     if (!IsString(nameJsonObj)) {
275         DHLOGE("VideoEncoder NAME is invalid!");
276         return;
277     }
278     videoEncoder.name = nameJsonObj->valuestring;
279 
280     cJSON *videoEncoderInsJson = cJSON_GetObjectItem(jsonObject, INS);
281     if (!IsArray(videoEncoderInsJson)) {
282         DHLOGE("VideoEncoder INS is invalid!");
283         return;
284     }
285     cJSON *inJson = nullptr;
286     cJSON_ArrayForEach(inJson, videoEncoderInsJson) {
287         VideoEncoderIn in;
288         FromJson(inJson, in);
289         videoEncoder.ins.push_back(in);
290     }
291 
292     cJSON *videoEncoderOutsJson = cJSON_GetObjectItem(jsonObject, OUTS);
293     if (!IsArray(videoEncoderOutsJson)) {
294         DHLOGE("VideoEncoder OUTS is invalid!");
295         return;
296     }
297     cJSON *outJson = nullptr;
298     cJSON_ArrayForEach(outJson, videoEncoderOutsJson) {
299         VideoEncoderOut out;
300         FromJson(outJson, out);
301         videoEncoder.outs.push_back(out);
302     }
303 }
304 
FromJson(const cJSON * jsonObject,VideoDecoderIn & videoDecoderIn)305 void FromJson(const cJSON *jsonObject, VideoDecoderIn &videoDecoderIn)
306 {
307     if (jsonObject == nullptr) {
308         DHLOGE("Json pointer is nullptr!");
309         return;
310     }
311     cJSON *mimeJsonObj = cJSON_GetObjectItem(jsonObject, MIME);
312     if (!IsString(mimeJsonObj)) {
313         DHLOGE("VideoDecoderIn MIME is invalid!");
314         return;
315     }
316     videoDecoderIn.mime = mimeJsonObj->valuestring;
317 
318     cJSON *videoBitStreamFmtJson = cJSON_GetObjectItem(jsonObject, VIDEO_BIT_STREAM_FMT);
319     if (!IsArray(videoBitStreamFmtJson)) {
320         DHLOGE("VideoDecoderIn VIDEO_BIT_STREAM_FMT is invalid!");
321         return;
322     }
323     cJSON *fmt = nullptr;
324     cJSON_ArrayForEach(fmt, videoBitStreamFmtJson) {
325         if (fmt && fmt->type == cJSON_Number) {
326             videoDecoderIn.vd_bit_stream_fmt.push_back((VideoBitStreamFormat)(fmt->valuedouble));
327         }
328     }
329 }
330 
FromJson(const cJSON * jsonObject,VideoDecoderOut & videoDecoderOut)331 void FromJson(const cJSON *jsonObject, VideoDecoderOut &videoDecoderOut)
332 {
333     if (jsonObject == nullptr) {
334         DHLOGE("Json pointer is nullptr!");
335         return;
336     }
337     cJSON *mimeJsonObj = cJSON_GetObjectItem(jsonObject, MIME);
338     if (!IsString(mimeJsonObj)) {
339         DHLOGE("VideoDecoderOut MIME is invalid!");
340         return;
341     }
342     videoDecoderOut.mime = mimeJsonObj->valuestring;
343 
344     cJSON *videoPixelFmtJson = cJSON_GetObjectItem(jsonObject, VIDEO_PIXEL_FMT);
345     if (!IsArray(videoPixelFmtJson)) {
346         DHLOGE("videoDecoderOut VIDEO_PIXEL_FMT is invalid!");
347         return;
348     }
349     cJSON *fmt = nullptr;
350     cJSON_ArrayForEach(fmt, videoPixelFmtJson) {
351         if (fmt && fmt->type == cJSON_Number) {
352             videoDecoderOut.pixel_fmt.push_back((VideoPixelFormat)(fmt->valuedouble));
353         }
354     }
355 }
356 
FromJson(const cJSON * jsonObject,VideoDecoder & videoDecoder)357 void FromJson(const cJSON *jsonObject, VideoDecoder &videoDecoder)
358 {
359     if (jsonObject == nullptr) {
360         DHLOGE("Json pointer is nullptr!");
361         return;
362     }
363     cJSON *nameJsonObj = cJSON_GetObjectItem(jsonObject, NAME);
364     if (!IsString(nameJsonObj)) {
365         DHLOGE("VideoDecoder NAME is invalid!");
366         return;
367     }
368     videoDecoder.name = nameJsonObj->valuestring;
369 
370     cJSON *videoDecoderInsJson = cJSON_GetObjectItem(jsonObject, INS);
371     if (!IsArray(videoDecoderInsJson)) {
372         DHLOGE("VideoDecoder INS is invalid!");
373         return;
374     }
375     cJSON *inJson = nullptr;
376     cJSON_ArrayForEach(inJson, videoDecoderInsJson) {
377         VideoDecoderIn in;
378         FromJson(inJson, in);
379         videoDecoder.ins.push_back(in);
380     }
381 
382     cJSON *videoDecoderOutsJson =  cJSON_GetObjectItem(jsonObject, OUTS);
383     if (!IsArray(videoDecoderOutsJson)) {
384         DHLOGE("VideoDecoder OUTS is invalid!");
385         return;
386     }
387     cJSON *outJson = nullptr;
388     cJSON_ArrayForEach(outJson, videoDecoderOutsJson) {
389         VideoDecoderOut out;
390         FromJson(outJson, out);
391         videoDecoder.outs.push_back(out);
392     }
393 }
394 
395 template <typename T>
FromJson(const std::string & key,const cJSON * jsonObject,std::vector<T> & objs)396 void FromJson(const std::string &key, const cJSON *jsonObject, std::vector<T> &objs)
397 {
398     if (jsonObject == nullptr) {
399         DHLOGE("Json pointer is nullptr!");
400         return;
401     }
402     cJSON *json = cJSON_GetObjectItem(jsonObject, key.c_str());
403     if (json == NULL) {
404         DHLOGE("JSONObject key invalid, key: %{public}s", key.c_str());
405         return;
406     }
407     if (cJSON_IsArray(json)) {
408         cJSON *item;
409         cJSON_ArrayForEach(item, json) {
410             T obj;
411             FromJson(item, obj);
412             objs.push_back(obj);
413         }
414     } else {
415         T obj;
416         FromJson(json, obj);
417         objs.push_back(obj);
418     }
419 }
420 
421 template
422 void FromJson<AudioEncoder>(const std::string &key, const cJSON *jsonObject, std::vector<AudioEncoder> &objs);
423 template
424 void FromJson<AudioDecoder>(const std::string &key, const cJSON *jsonObject, std::vector<AudioDecoder> &objs);
425 template
426 void FromJson<VideoEncoder>(const std::string &key, const cJSON *jsonObject, std::vector<VideoEncoder> &objs);
427 template
428 void FromJson<VideoDecoder>(const std::string &key, const cJSON *jsonObject, std::vector<VideoDecoder> &objs);
429 
430 }
431 }