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 "media_errors.h"
17 #include <map>
18 #include <string>
19
20 namespace OHOS {
21 namespace Media {
22 using ErrorMessageFunc = std::function<std::string(const std::string& param1, const std::string& param2)>;
23 const std::map<MediaServiceErrCode, std::string> MSERRCODE_INFOS = {
24 {MSERR_OK, "success"},
25 {MSERR_NO_MEMORY, "no memory"},
26 {MSERR_INVALID_OPERATION, "operation not be permitted"},
27 {MSERR_INVALID_VAL, "invalid argument"},
28 {MSERR_UNKNOWN, "unkown error"},
29 {MSERR_SERVICE_DIED, "media service died"},
30 {MSERR_CREATE_REC_ENGINE_FAILED, "create recorder engine failed"},
31 {MSERR_CREATE_PLAYER_ENGINE_FAILED, "create player engine failed"},
32 {MSERR_CREATE_AVMETADATAHELPER_ENGINE_FAILED, "create avmetadatahelper engine failed"},
33 {MSERR_INVALID_STATE, "the state is not support this operation"},
34 {MSERR_UNSUPPORT, "unsupport interface"},
35 {MSERR_UNSUPPORT_AUD_SRC_TYPE, "unsupport audio source type"},
36 {MSERR_UNSUPPORT_AUD_SAMPLE_RATE, "unsupport audio sample rate"},
37 {MSERR_UNSUPPORT_AUD_CHANNEL_NUM, "unsupport audio channel"},
38 {MSERR_UNSUPPORT_AUD_ENC_TYPE, "unsupport audio encoder type"},
39 {MSERR_UNSUPPORT_AUD_PARAMS, "unsupport audio params(other params)"},
40 {MSERR_UNSUPPORT_VID_SRC_TYPE, "unsupport video source type"},
41 {MSERR_UNSUPPORT_VID_ENC_TYPE, "unsupport video encoder type"},
42 {MSERR_UNSUPPORT_VID_PARAMS, "unsupport video params(other params)"},
43 {MSERR_UNSUPPORT_CONTAINER_TYPE, "unsupport container format type"},
44 {MSERR_UNSUPPORT_PROTOCOL_TYPE, "unsupport protocol type"},
45 {MSERR_UNSUPPORT_VID_DEC_TYPE, "unsupport video decoder type"},
46 {MSERR_UNSUPPORT_AUD_DEC_TYPE, "unsupport audio decoder type"},
47 {MSERR_UNSUPPORT_STREAM, "internal data stream error"},
48 {MSERR_UNSUPPORT_FILE, "this appears to be a text file"},
49 {MSERR_UNSUPPORT_SOURCE, "unsupport source type"},
50 {MSERR_AUD_ENC_FAILED, "audio encode failed"},
51 {MSERR_AUD_RENDER_FAILED, "audio render failed"},
52 {MSERR_VID_ENC_FAILED, "video encode failed"},
53 {MSERR_AUD_DEC_FAILED, "audio decode failed"},
54 {MSERR_VID_DEC_FAILED, "video decode failed"},
55 {MSERR_MUXER_FAILED, "stream avmuxer failed"},
56 {MSERR_DEMUXER_FAILED, "stream demuxer or parser failed"},
57 {MSERR_OPEN_FILE_FAILED, "open file failed"},
58 {MSERR_FILE_ACCESS_FAILED, "read or write file failed"},
59 {MSERR_START_FAILED, "audio or video start failed"},
60 {MSERR_PAUSE_FAILED, "audio or video pause failed"},
61 {MSERR_STOP_FAILED, "audio or video stop failed"},
62 {MSERR_SEEK_FAILED, "audio or video seek failed"},
63 {MSERR_NETWORK_TIMEOUT, "network timeout"},
64 {MSERR_NOT_FIND_CONTAINER, "not find a demuxer"},
65 {MSERR_EXTEND_START, "extend start error code"},
66 };
67
68 const std::map<MediaServiceErrCode, MediaServiceExtErrCode> MSERRCODE_TO_EXTERRORCODE = {
69 {MSERR_OK, MSERR_EXT_OK},
70 {MSERR_NO_MEMORY, MSERR_EXT_NO_MEMORY},
71 {MSERR_INVALID_OPERATION, MSERR_EXT_OPERATE_NOT_PERMIT},
72 {MSERR_INVALID_VAL, MSERR_EXT_INVALID_VAL},
73 {MSERR_UNKNOWN, MSERR_EXT_UNKNOWN},
74 {MSERR_SERVICE_DIED, MSERR_EXT_SERVICE_DIED},
75 {MSERR_CREATE_REC_ENGINE_FAILED, MSERR_EXT_UNKNOWN},
76 {MSERR_CREATE_PLAYER_ENGINE_FAILED, MSERR_EXT_UNKNOWN},
77 {MSERR_INVALID_STATE, MSERR_EXT_INVALID_STATE},
78 {MSERR_UNSUPPORT, MSERR_EXT_UNSUPPORT},
79 {MSERR_UNSUPPORT_AUD_SRC_TYPE, MSERR_EXT_UNSUPPORT},
80 {MSERR_UNSUPPORT_AUD_SAMPLE_RATE, MSERR_EXT_UNSUPPORT},
81 {MSERR_UNSUPPORT_AUD_CHANNEL_NUM, MSERR_EXT_UNSUPPORT},
82 {MSERR_UNSUPPORT_AUD_ENC_TYPE, MSERR_EXT_UNSUPPORT},
83 {MSERR_UNSUPPORT_AUD_PARAMS, MSERR_EXT_UNSUPPORT},
84 {MSERR_UNSUPPORT_VID_SRC_TYPE, MSERR_EXT_UNSUPPORT},
85 {MSERR_UNSUPPORT_VID_ENC_TYPE, MSERR_EXT_UNSUPPORT},
86 {MSERR_UNSUPPORT_VID_PARAMS, MSERR_EXT_UNSUPPORT},
87 {MSERR_UNSUPPORT_CONTAINER_TYPE, MSERR_EXT_UNSUPPORT},
88 {MSERR_UNSUPPORT_PROTOCOL_TYPE, MSERR_EXT_UNSUPPORT},
89 {MSERR_UNSUPPORT_VID_DEC_TYPE, MSERR_EXT_UNSUPPORT},
90 {MSERR_UNSUPPORT_AUD_DEC_TYPE, MSERR_EXT_UNSUPPORT},
91 {MSERR_UNSUPPORT_STREAM, MSERR_EXT_UNSUPPORT},
92 {MSERR_UNSUPPORT_FILE, MSERR_EXT_UNSUPPORT},
93 {MSERR_UNSUPPORT_SOURCE, MSERR_EXT_UNSUPPORT},
94 {MSERR_AUD_RENDER_FAILED, MSERR_EXT_UNSUPPORT},
95 {MSERR_AUD_ENC_FAILED, MSERR_EXT_UNKNOWN},
96 {MSERR_VID_ENC_FAILED, MSERR_EXT_UNKNOWN},
97 {MSERR_AUD_DEC_FAILED, MSERR_EXT_UNKNOWN},
98 {MSERR_VID_DEC_FAILED, MSERR_EXT_UNKNOWN},
99 {MSERR_MUXER_FAILED, MSERR_EXT_UNKNOWN},
100 {MSERR_DEMUXER_FAILED, MSERR_EXT_UNKNOWN},
101 {MSERR_OPEN_FILE_FAILED, MSERR_EXT_UNKNOWN},
102 {MSERR_FILE_ACCESS_FAILED, MSERR_EXT_UNKNOWN},
103 {MSERR_START_FAILED, MSERR_EXT_UNKNOWN},
104 {MSERR_PAUSE_FAILED, MSERR_EXT_UNKNOWN},
105 {MSERR_STOP_FAILED, MSERR_EXT_UNKNOWN},
106 {MSERR_SEEK_FAILED, MSERR_EXT_UNKNOWN},
107 {MSERR_NETWORK_TIMEOUT, MSERR_EXT_TIMEOUT},
108 {MSERR_NOT_FIND_CONTAINER, MSERR_EXT_UNSUPPORT},
109 {MSERR_EXTEND_START, MSERR_EXT_EXTEND_START},
110 };
111
112 const std::map<MediaServiceExtErrCode, std::string> MSEXTERRCODE_INFOS = {
113 {MSERR_EXT_OK, "success"},
114 {MSERR_EXT_NO_MEMORY, "no memory"},
115 {MSERR_EXT_OPERATE_NOT_PERMIT, "operation not be permitted"},
116 {MSERR_EXT_INVALID_VAL, "invalid argument"},
117 {MSERR_EXT_IO, "IO error"},
118 {MSERR_EXT_TIMEOUT, "network timeout"},
119 {MSERR_EXT_UNKNOWN, "unkown error"},
120 {MSERR_EXT_SERVICE_DIED, "media service died"},
121 {MSERR_EXT_INVALID_STATE, "the state is not support this operation"},
122 {MSERR_EXT_UNSUPPORT, "unsupport interface"},
123 {MSERR_EXT_EXTEND_START, "extend err start"},
124 };
125
126 const std::map<MediaServiceErrCode, MediaServiceExtErrCodeAPI9> MSERRCODE_TO_EXTERRORCODEAPI9 = {
127 {MSERR_OK, MSERR_EXT_API9_OK},
128 {MSERR_NO_MEMORY, MSERR_EXT_API9_NO_MEMORY},
129 {MSERR_INVALID_OPERATION, MSERR_EXT_API9_OPERATE_NOT_PERMIT},
130 {MSERR_INVALID_VAL, MSERR_EXT_API9_INVALID_PARAMETER},
131 {MSERR_SERVICE_DIED, MSERR_EXT_API9_SERVICE_DIED},
132 {MSERR_CREATE_REC_ENGINE_FAILED, MSERR_EXT_API9_NO_MEMORY},
133 {MSERR_CREATE_PLAYER_ENGINE_FAILED, MSERR_EXT_API9_NO_MEMORY},
134 {MSERR_INVALID_STATE, MSERR_EXT_API9_OPERATE_NOT_PERMIT},
135 {MSERR_UNSUPPORT, MSERR_EXT_API9_UNSUPPORT_FORMAT},
136 {MSERR_UNSUPPORT_AUD_SRC_TYPE, MSERR_EXT_API9_UNSUPPORT_FORMAT},
137 {MSERR_UNSUPPORT_AUD_SAMPLE_RATE, MSERR_EXT_API9_UNSUPPORT_FORMAT},
138 {MSERR_UNSUPPORT_AUD_CHANNEL_NUM, MSERR_EXT_API9_UNSUPPORT_FORMAT},
139 {MSERR_UNSUPPORT_AUD_ENC_TYPE, MSERR_EXT_API9_UNSUPPORT_FORMAT},
140 {MSERR_UNSUPPORT_AUD_PARAMS, MSERR_EXT_API9_UNSUPPORT_FORMAT},
141 {MSERR_UNSUPPORT_VID_SRC_TYPE, MSERR_EXT_API9_UNSUPPORT_FORMAT},
142 {MSERR_UNSUPPORT_VID_ENC_TYPE, MSERR_EXT_API9_UNSUPPORT_FORMAT},
143 {MSERR_UNSUPPORT_VID_PARAMS, MSERR_EXT_API9_UNSUPPORT_FORMAT},
144 {MSERR_UNSUPPORT_CONTAINER_TYPE, MSERR_EXT_API9_UNSUPPORT_FORMAT},
145 {MSERR_UNSUPPORT_PROTOCOL_TYPE, MSERR_EXT_API9_UNSUPPORT_FORMAT},
146 {MSERR_UNSUPPORT_VID_DEC_TYPE, MSERR_EXT_API9_UNSUPPORT_FORMAT},
147 {MSERR_UNSUPPORT_AUD_DEC_TYPE, MSERR_EXT_API9_UNSUPPORT_FORMAT},
148 {MSERR_UNSUPPORT_STREAM, MSERR_EXT_API9_UNSUPPORT_FORMAT},
149 {MSERR_UNSUPPORT_FILE, MSERR_EXT_API9_UNSUPPORT_FORMAT},
150 {MSERR_UNSUPPORT_SOURCE, MSERR_EXT_API9_UNSUPPORT_FORMAT},
151 {MSERR_AUD_RENDER_FAILED, MSERR_EXT_API9_IO},
152 {MSERR_AUD_ENC_FAILED, MSERR_EXT_API9_IO},
153 {MSERR_VID_ENC_FAILED, MSERR_EXT_API9_IO},
154 {MSERR_AUD_DEC_FAILED, MSERR_EXT_API9_IO},
155 {MSERR_VID_DEC_FAILED, MSERR_EXT_API9_IO},
156 {MSERR_MUXER_FAILED, MSERR_EXT_API9_UNSUPPORT_FORMAT},
157 {MSERR_DEMUXER_FAILED, MSERR_EXT_API9_UNSUPPORT_FORMAT},
158 {MSERR_OPEN_FILE_FAILED, MSERR_EXT_API9_IO},
159 {MSERR_FILE_ACCESS_FAILED, MSERR_EXT_API9_IO},
160 {MSERR_START_FAILED, MSERR_EXT_API9_OPERATE_NOT_PERMIT},
161 {MSERR_PAUSE_FAILED, MSERR_EXT_API9_OPERATE_NOT_PERMIT},
162 {MSERR_STOP_FAILED, MSERR_EXT_API9_OPERATE_NOT_PERMIT},
163 {MSERR_SEEK_FAILED, MSERR_EXT_API9_OPERATE_NOT_PERMIT},
164 {MSERR_NETWORK_TIMEOUT, MSERR_EXT_API9_TIMEOUT},
165 {MSERR_NOT_FIND_CONTAINER, MSERR_EXT_API9_INVALID_PARAMETER},
166 {MSERR_UNKNOWN, MSERR_EXT_API9_IO},
167 };
168
169 const std::map<MediaServiceExtErrCodeAPI9, std::string> MSEXTERRCODE_API9_INFOS = {
170 {MSERR_EXT_API9_OK, "Success: "},
171 {MSERR_EXT_API9_NO_PERMISSION, "No Permission: "},
172 {MSERR_EXT_API9_PERMISSION_DENIED, "Permission Denied"},
173 {MSERR_EXT_API9_INVALID_PARAMETER, "Invalid Parameter: "},
174 {MSERR_EXT_API9_UNSUPPORT_CAPABILITY, "Unsupport Capability: "},
175 {MSERR_EXT_API9_NO_MEMORY, "No Memory: "},
176 {MSERR_EXT_API9_OPERATE_NOT_PERMIT, "Operate Not Permit: "},
177 {MSERR_EXT_API9_IO, "IO Error: "},
178 {MSERR_EXT_API9_TIMEOUT, "Network Timeout: "},
179 {MSERR_EXT_API9_SERVICE_DIED, "Service Died: "},
180 {MSERR_EXT_API9_UNSUPPORT_FORMAT, "Unsupport Format: "},
181 };
182
ErrorMessageOk(const std::string & param1,const std::string & param2)183 std::string ErrorMessageOk(const std::string& param1, const std::string& param2)
184 {
185 (void)param1;
186 (void)param2;
187 return "success";
188 }
189
ErrorMessageNoPermission(const std::string & param1,const std::string & param2)190 std::string ErrorMessageNoPermission(const std::string& param1, const std::string& param2)
191 {
192 std::string message = "Try to do " + param1 + " failed. User should request permission " + param2 +" first.";
193 return message;
194 }
195
ErrorMessageInvalidParameter(const std::string & param1,const std::string & param2)196 std::string ErrorMessageInvalidParameter(const std::string& param1, const std::string& param2)
197 {
198 (void)param2;
199 std::string message = "The Parameter " + param1 + " is invalid. Please check the type and range.";
200 return message;
201 }
202
ErrorMessageUnsupportCapability(const std::string & param1,const std::string & param2)203 std::string ErrorMessageUnsupportCapability(const std::string& param1, const std::string& param2)
204 {
205 (void)param2;
206 std::string message = "Function " + param1 + " can not work correctly due to limited device capability.";
207 return message;
208 }
209
ErrorMessageNoMemory(const std::string & param1,const std::string & param2)210 std::string ErrorMessageNoMemory(const std::string& param1, const std::string& param2)
211 {
212 (void)param2;
213 std::string message = "Create " + param1 + " failed due to system memory.";
214 return message;
215 }
216
ErrorMessageOperateNotPermit(const std::string & param1,const std::string & param2)217 std::string ErrorMessageOperateNotPermit(const std::string& param1, const std::string& param2)
218 {
219 (void)param2;
220 std::string message = "The operate " + param1 + " failed due to not permit in current state.";
221 return message;
222 }
223
ErrorMessageIO(const std::string & param1,const std::string & param2)224 std::string ErrorMessageIO(const std::string& param1, const std::string& param2)
225 {
226 (void)param2;
227 std::string message = "IO error happened due to " + param1 + ".";
228 return message;
229 }
230
ErrorMessageTimeout(const std::string & param1,const std::string & param2)231 std::string ErrorMessageTimeout(const std::string& param1, const std::string& param2)
232 {
233 std::string message = "Timeout happend when " + param1 + " due to " + param2 + ".";
234 return message;
235 }
236
ErrorMessageServiceDied(const std::string & param1,const std::string & param2)237 std::string ErrorMessageServiceDied(const std::string& param1, const std::string& param2)
238 {
239 (void)param1;
240 (void)param2;
241 std::string message = "Media Serviced Died.";
242 return message;
243 }
244
ErrorMessageUnsupportFormat(const std::string & param1,const std::string & param2)245 std::string ErrorMessageUnsupportFormat(const std::string& param1, const std::string& param2)
246 {
247 (void)param2;
248 std::string message = "The format " + param1 + " is not support.";
249 return message;
250 }
251
252 const std::map<MediaServiceExtErrCodeAPI9, ErrorMessageFunc> MSEXTERRAPI9CODE_FUNCS = {
253 {MSERR_EXT_API9_OK, &ErrorMessageOk},
254 {MSERR_EXT_API9_NO_PERMISSION, &ErrorMessageNoPermission},
255 {MSERR_EXT_API9_INVALID_PARAMETER, &ErrorMessageInvalidParameter},
256 {MSERR_EXT_API9_UNSUPPORT_CAPABILITY, &ErrorMessageUnsupportCapability},
257 {MSERR_EXT_API9_NO_MEMORY, &ErrorMessageNoMemory},
258 {MSERR_EXT_API9_OPERATE_NOT_PERMIT, &ErrorMessageOperateNotPermit},
259 {MSERR_EXT_API9_IO, &ErrorMessageIO},
260 {MSERR_EXT_API9_TIMEOUT, &ErrorMessageTimeout},
261 {MSERR_EXT_API9_SERVICE_DIED, &ErrorMessageServiceDied},
262 {MSERR_EXT_API9_UNSUPPORT_FORMAT, &ErrorMessageUnsupportFormat},
263 };
264
MSErrorToString(MediaServiceErrCode code)265 std::string MSErrorToString(MediaServiceErrCode code)
266 {
267 if (MSERRCODE_INFOS.count(code) != 0) {
268 return MSERRCODE_INFOS.at(code);
269 }
270
271 if (code > MSERR_EXTEND_START) {
272 return "extend error:" + std::to_string(static_cast<int32_t>(code - MSERR_EXTEND_START));
273 }
274
275 return "invalid error code:" + std::to_string(static_cast<int32_t>(code));
276 }
277
MSExtErrorToString(MediaServiceExtErrCode code)278 std::string MSExtErrorToString(MediaServiceExtErrCode code)
279 {
280 if (MSEXTERRCODE_INFOS.count(code) != 0) {
281 return MSEXTERRCODE_INFOS.at(code);
282 }
283
284 if (code > MSERR_EXT_EXTEND_START) {
285 return "extend error:" + std::to_string(static_cast<int32_t>(code - MSERR_EXTEND_START));
286 }
287
288 return "invalid error code:" + std::to_string(static_cast<int32_t>(code));
289 }
290
MSExtErrorAPI9ToString(MediaServiceExtErrCodeAPI9 code,const std::string & param1,const std::string & param2)291 std::string MSExtErrorAPI9ToString(MediaServiceExtErrCodeAPI9 code,
292 const std::string& param1, const std::string& param2)
293 {
294 if (MSEXTERRAPI9CODE_FUNCS.count(code) != 0) {
295 return MSEXTERRAPI9CODE_FUNCS.at(code)(param1, param2);
296 }
297
298 return "invalid error code:" + std::to_string(static_cast<int32_t>(code));
299 }
300
MSExtAVErrorToString(MediaServiceExtErrCodeAPI9 code)301 std::string MSExtAVErrorToString(MediaServiceExtErrCodeAPI9 code)
302 {
303 if (MSEXTERRCODE_API9_INFOS.count(code) != 0) {
304 return MSEXTERRCODE_API9_INFOS.at(code);
305 }
306
307 return "invalid error code:";
308 }
309
MSErrorToExtErrorString(MediaServiceErrCode code)310 std::string MSErrorToExtErrorString(MediaServiceErrCode code)
311 {
312 if (MSERRCODE_INFOS.count(code) != 0 && MSERRCODE_TO_EXTERRORCODE.count(code) != 0) {
313 MediaServiceExtErrCode extCode = MSERRCODE_TO_EXTERRORCODE.at(code);
314 if (MSEXTERRCODE_INFOS.count(extCode) != 0) {
315 return MSEXTERRCODE_INFOS.at(extCode);
316 }
317 }
318
319 return "unkown error";
320 }
321
MSErrorToExtErrorAPI9String(MediaServiceErrCode code,const std::string & param1,const std::string & param2)322 std::string MSErrorToExtErrorAPI9String(MediaServiceErrCode code, const std::string& param1, const std::string& param2)
323 {
324 if (MSERRCODE_INFOS.count(code) != 0 && MSERRCODE_TO_EXTERRORCODEAPI9.count(code) != 0) {
325 MediaServiceExtErrCodeAPI9 extCode = MSERRCODE_TO_EXTERRORCODEAPI9.at(code);
326 if (MSEXTERRAPI9CODE_FUNCS.count(extCode) != 0) {
327 return MSEXTERRAPI9CODE_FUNCS.at(extCode)(param1, param2);
328 }
329 }
330
331 return "unkown error";
332 }
333
MSErrorToExtError(MediaServiceErrCode code)334 MediaServiceExtErrCode MSErrorToExtError(MediaServiceErrCode code)
335 {
336 if (MSERRCODE_INFOS.count(code) != 0 && MSERRCODE_TO_EXTERRORCODE.count(code) != 0) {
337 return MSERRCODE_TO_EXTERRORCODE.at(code);
338 }
339
340 return MSERR_EXT_UNKNOWN;
341 }
342
MSErrorToExtErrorAPI9(MediaServiceErrCode code)343 MediaServiceExtErrCodeAPI9 MSErrorToExtErrorAPI9(MediaServiceErrCode code)
344 {
345 if (MSERRCODE_INFOS.count(code) != 0 && MSERRCODE_TO_EXTERRORCODEAPI9.count(code) != 0) {
346 return MSERRCODE_TO_EXTERRORCODEAPI9.at(code);
347 }
348 // If error not in map, need add error and should not return default MSERR_EXT_API9_IO.
349 return MSERR_EXT_API9_IO;
350 }
351 } // namespace Media
352 } // namespace OHOS
353