• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_parser.h"
17 #include "dh_utils_tool.h"
18 #include "distributed_hardware_log.h"
19 
20 
21 namespace OHOS {
22 namespace DistributedHardware {
23 
24 static const std::string NAME = "name";
25 static const std::string INS = "ins";
26 static const std::string OUTS = "outs";
27 static const std::string MIME = "mime";
28 static const std::string SAMPLE_RATE = "sample_rate";
29 static const std::string AUDIO_SAMPLE_FORMAT = "sample_fmt";
30 static const std::string AD_MPEG_VER = "ad_mpeg_ver";
31 static const std::string AUDIO_AAC_PROFILE = "aac_profile";
32 static const std::string AUDIO_AAC_STREAM_FORMAT = "aac_stm_fmt";
33 static const std::string AUDIO_CHANNEL_LAYOUT = "channel_layout";
34 
35 static const std::string VIDEO_PIXEL_FMT = "pixel_fmt";
36 static const std::string VIDEO_BIT_STREAM_FMT = "vd_bit_stream_fmt";
37 
FromJson(const nlohmann::json & jsonObject,AudioEncoderIn & audioEncoderIn)38 void FromJson(const nlohmann::json &jsonObject, AudioEncoderIn &audioEncoderIn)
39 {
40     if (!IsString(jsonObject, MIME)) {
41         DHLOGE("AudioEncoderIn MIME is invalid!");
42         return;
43     }
44     audioEncoderIn.mime = jsonObject.at(MIME).get<std::string>();
45     if (jsonObject.find(SAMPLE_RATE) == jsonObject.end()) {
46         DHLOGE("AudioEncoderIn SAMPLE_RATE is invalid");
47     }
48     audioEncoderIn.sample_rate = jsonObject.at(SAMPLE_RATE).get<std::vector<uint32_t>>();
49 }
50 
FromJson(const nlohmann::json & jsonObject,AudioEncoderOut & audioEncoderOut)51 void FromJson(const nlohmann::json &jsonObject, AudioEncoderOut &audioEncoderOut)
52 {
53     if (!IsString(jsonObject, MIME)) {
54         DHLOGE("AudioEncoderOut MIME is invalid!");
55         return;
56     }
57     audioEncoderOut.mime = jsonObject.at(MIME).get<std::string>();
58 
59     if (!IsUInt32(jsonObject, AD_MPEG_VER)) {
60         DHLOGE("AudioEncoderOut AD_MPEG_VER is invalid");
61         return;
62     }
63     audioEncoderOut.ad_mpeg_ver = jsonObject.at(AD_MPEG_VER).get<uint32_t>();
64 
65     if (!IsUInt8(jsonObject, AUDIO_AAC_PROFILE)) {
66         DHLOGE("AudioEncoderOut AUDIO_AAC_PROFILE is invalid");
67         return;
68     }
69     audioEncoderOut.aac_profile = (AudioAacProfile)jsonObject.at(AUDIO_AAC_PROFILE).get<uint8_t>();
70 
71     if (!IsUInt8(jsonObject, AUDIO_AAC_STREAM_FORMAT)) {
72         DHLOGE("AudioEncoderOut AUDIO_AAC_STREAM_FORMAT is invalid");
73         return;
74     }
75     audioEncoderOut.aac_stm_fmt = (AudioAacStreamFormat)jsonObject.at(AUDIO_AAC_STREAM_FORMAT).get<uint8_t>();
76 }
77 
FromJson(const nlohmann::json & jsonObject,AudioEncoder & audioEncoder)78 void FromJson(const nlohmann::json &jsonObject, AudioEncoder &audioEncoder)
79 {
80     if (!IsString(jsonObject, NAME)) {
81         DHLOGE("AudioEncoder NAME is invalid");
82         return;
83     }
84     audioEncoder.name = jsonObject.at(NAME).get<std::string>();
85 
86     if (jsonObject.find(INS) == jsonObject.end()) {
87         DHLOGE("AudioEncoder INS is invalid");
88         return;
89     }
90 
91     nlohmann::json audioEncoderInsJson = jsonObject[INS];
92     for (const auto &inJson : audioEncoderInsJson) {
93         AudioEncoderIn in;
94         FromJson(inJson, in);
95         audioEncoder.ins.push_back(in);
96     }
97 
98     if (jsonObject.find(OUTS) == jsonObject.end()) {
99         DHLOGE("AudioEncoder OUTS is invalid");
100         return;
101     }
102     nlohmann::json audioEncoderOutsJson = jsonObject[OUTS];
103     for (const auto &outJson : audioEncoderOutsJson) {
104         AudioEncoderOut out;
105         FromJson(outJson, out);
106         audioEncoder.outs.push_back(out);
107     }
108 }
109 
FromJson(const nlohmann::json & jsonObject,AudioDecoderIn & audioDecoderIn)110 void FromJson(const nlohmann::json &jsonObject, AudioDecoderIn &audioDecoderIn)
111 {
112     if (!IsString(jsonObject, MIME)) {
113         DHLOGE("AudioDecoderIn MIME is invalid");
114         return;
115     }
116     audioDecoderIn.mime = jsonObject.at(MIME).get<std::string>();
117 
118     if (jsonObject.find(AUDIO_CHANNEL_LAYOUT) == jsonObject.end()) {
119         DHLOGE("AudioEncoder AUDIO_CHANNEL_LAYOUT is invalid");
120         return;
121     }
122     nlohmann::json channelLayoutJson = jsonObject[AUDIO_CHANNEL_LAYOUT];
123     for (auto layout : channelLayoutJson) {
124         audioDecoderIn.channel_layout.push_back((AudioChannelLayout)layout);
125     }
126 }
127 
FromJson(const nlohmann::json & jsonObject,AudioDecoderOut & audioDecoderOut)128 void FromJson(const nlohmann::json &jsonObject, AudioDecoderOut &audioDecoderOut)
129 {
130     if (!IsString(jsonObject, MIME)) {
131         DHLOGE("AudioDecoderOut MIME is invalid");
132         return;
133     }
134     audioDecoderOut.mime = jsonObject.at(MIME).get<std::string>();
135 
136     if (jsonObject.find(AUDIO_SAMPLE_FORMAT) == jsonObject.end()) {
137         DHLOGE("AudioDecoderOut AUDIO_SAMPLE_FORMAT is invalid");
138         return;
139     }
140 
141     for (auto sampleFormatJson : jsonObject[AUDIO_SAMPLE_FORMAT]) {
142         audioDecoderOut.sample_fmt.push_back((AudioSampleFormat)sampleFormatJson);
143     }
144 }
145 
FromJson(const nlohmann::json & jsonObject,AudioDecoder & audioDecoder)146 void FromJson(const nlohmann::json &jsonObject, AudioDecoder &audioDecoder)
147 {
148     if (!IsString(jsonObject, NAME)) {
149         DHLOGE("AudioDecoder NAME is invalid");
150         return;
151     }
152     audioDecoder.name = jsonObject.at(NAME).get<std::string>();
153 
154     if (jsonObject.find(INS) == jsonObject.end()) {
155         DHLOGE("AudioDecoder INS is invalid");
156         return;
157     }
158 
159     nlohmann::json audioDecoderInsJson = jsonObject[INS];
160     for (const auto &inJson : audioDecoderInsJson) {
161         AudioDecoderIn in;
162         FromJson(inJson, in);
163         audioDecoder.ins.push_back(in);
164     }
165 
166     if (jsonObject.find(OUTS) == jsonObject.end()) {
167         DHLOGE("AudioDecoder OUTS is invalid");
168         return;
169     }
170     nlohmann::json audioDecoderOutsJson = jsonObject[OUTS];
171     for (const auto &outJson : audioDecoderOutsJson) {
172         AudioDecoderOut out;
173         FromJson(outJson, out);
174         audioDecoder.outs.push_back(out);
175     }
176 }
177 
FromJson(const nlohmann::json & jsonObject,VideoEncoderIn & videoEncoderIn)178 void FromJson(const nlohmann::json &jsonObject, VideoEncoderIn &videoEncoderIn)
179 {
180     if (!IsString(jsonObject, MIME)) {
181         DHLOGE("VideoEncoderIn MIME is invalid");
182         return;
183     }
184     videoEncoderIn.mime = jsonObject.at(MIME).get<std::string>();
185 
186     if (jsonObject.find(VIDEO_PIXEL_FMT) == jsonObject.end()) {
187         DHLOGE("VideoEncoderIn VIDEO_PIXEL_FMT is invalid");
188         return;
189     }
190     for (auto fmt : jsonObject[VIDEO_PIXEL_FMT]) {
191         videoEncoderIn.pixel_fmt.push_back((VideoPixelFormat)fmt);
192     }
193 }
194 
FromJson(const nlohmann::json & jsonObject,VideoEncoderOut & videoEncoderOut)195 void FromJson(const nlohmann::json &jsonObject, VideoEncoderOut &videoEncoderOut)
196 {
197     if (!IsString(jsonObject, MIME)) {
198         DHLOGE("VideoEncoderOut MIME is invalid");
199         return;
200     }
201     videoEncoderOut.mime = jsonObject[MIME].get<std::string>();
202 }
203 
FromJson(const nlohmann::json & jsonObject,VideoEncoder & videoEncoder)204 void FromJson(const nlohmann::json &jsonObject, VideoEncoder &videoEncoder)
205 {
206     if (!IsString(jsonObject, NAME)) {
207         DHLOGE("VideoEncoder NAME is invalid");
208         return;
209     }
210     videoEncoder.name = jsonObject.at(NAME).get<std::string>();
211 
212     if (jsonObject.find(INS) == jsonObject.end()) {
213         DHLOGE("VideoEncoder INS is invalid");
214         return;
215     }
216 
217     nlohmann::json videoEncoderInsJson = jsonObject[INS];
218     for (const auto &inJson : videoEncoderInsJson) {
219         VideoEncoderIn in;
220         FromJson(inJson, in);
221         videoEncoder.ins.push_back(in);
222     }
223 
224     if (jsonObject.find(OUTS) == jsonObject.end()) {
225         DHLOGE("VideoEncoder OUTS is invalid");
226         return;
227     }
228     nlohmann::json videoEncoderOutsJson = jsonObject[OUTS];
229     for (const auto &outJson : videoEncoderOutsJson) {
230         VideoEncoderOut out;
231         FromJson(outJson, out);
232         videoEncoder.outs.push_back(out);
233     }
234 }
235 
236 
FromJson(const nlohmann::json & jsonObject,VideoDecoderIn & videoDecoderIn)237 void FromJson(const nlohmann::json &jsonObject, VideoDecoderIn &videoDecoderIn)
238 {
239     if (!IsString(jsonObject, MIME)) {
240         DHLOGE("VideoDecoderIn MIME is invalid");
241         return;
242     }
243     videoDecoderIn.mime = jsonObject.at(MIME).get<std::string>();
244 
245     if (jsonObject.find(VIDEO_BIT_STREAM_FMT) == jsonObject.end()) {
246         DHLOGE("VideoDecoderIn VIDEO_BIT_STREAM_FMT is invalid");
247         return;
248     }
249     for (auto fmt : jsonObject[VIDEO_BIT_STREAM_FMT]) {
250         videoDecoderIn.vd_bit_stream_fmt.push_back((VideoBitStreamFormat)fmt);
251     }
252 }
253 
FromJson(const nlohmann::json & jsonObject,VideoDecoderOut & videoDecoderOut)254 void FromJson(const nlohmann::json &jsonObject, VideoDecoderOut &videoDecoderOut)
255 {
256     if (!IsString(jsonObject, MIME)) {
257         DHLOGE("VideoDecoderOut MIME is invalid");
258         return;
259     }
260     videoDecoderOut.mime = jsonObject.at(MIME).get<std::string>();
261 
262     if (jsonObject.find(VIDEO_PIXEL_FMT) == jsonObject.end()) {
263         DHLOGE("VideoDecoderOut VIDEO_PIXEL_FMT is invalid");
264         return;
265     }
266     for (auto fmt : jsonObject[VIDEO_PIXEL_FMT]) {
267         videoDecoderOut.pixel_fmt.push_back((VideoPixelFormat)fmt);
268     }
269 }
270 
FromJson(const nlohmann::json & jsonObject,VideoDecoder & videoDecoder)271 void FromJson(const nlohmann::json &jsonObject, VideoDecoder &videoDecoder)
272 {
273     if (!IsString(jsonObject, NAME)) {
274         DHLOGE("VideoDecoder NAME is invalid");
275         return;
276     }
277     videoDecoder.name = jsonObject.at(NAME).get<std::string>();
278 
279     if (jsonObject.find(INS) == jsonObject.end()) {
280         DHLOGE("VideoDecoder INS is invalid");
281         return;
282     }
283 
284     nlohmann::json videoDecoderInsJson = jsonObject[INS];
285     for (const auto &inJson : videoDecoderInsJson) {
286         VideoDecoderIn in;
287         FromJson(inJson, in);
288         videoDecoder.ins.push_back(in);
289     }
290 
291     if (jsonObject.find(OUTS) == jsonObject.end()) {
292         DHLOGE("VideoDecoder OUTS is invalid");
293         return;
294     }
295     nlohmann::json videoDecoderOutsJson = jsonObject[OUTS];
296     for (const auto &outJson : videoDecoderOutsJson) {
297         VideoDecoderOut out;
298         FromJson(outJson, out);
299         videoDecoder.outs.push_back(out);
300     }
301 }
302 
303 template<typename T>
FromJson(const std::string & key,const nlohmann::json & jsonObject,std::vector<T> & objs)304 void FromJson(const std::string &key, const nlohmann::json &jsonObject, std::vector<T> &objs)
305 {
306     if (jsonObject.find(key) == jsonObject.end()) {
307         DHLOGE("JSONObject key invalid, key: %s", key.c_str());
308         return;
309     }
310     for (auto &json : jsonObject[key]) {
311         T obj;
312         FromJson(json, obj);
313         objs.push_back(obj);
314     }
315 }
316 template
317 void FromJson<AudioEncoder>(const std::string &key, const nlohmann::json &jsonObject, std::vector<AudioEncoder> &objs);
318 template
319 void FromJson<AudioDecoder>(const std::string &key, const nlohmann::json &jsonObject, std::vector<AudioDecoder> &objs);
320 template
321 void FromJson<VideoEncoder>(const std::string &key, const nlohmann::json &jsonObject, std::vector<VideoEncoder> &objs);
322 template
323 void FromJson<VideoDecoder>(const std::string &key, const nlohmann::json &jsonObject, std::vector<VideoDecoder> &objs);
324 }
325 }