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 "avcodec_errors.h"
17 #include <map>
18 #include <string>
19
20 namespace OHOS {
21 namespace MediaAVCodec {
22 using ErrorMessageFunc = std::function<std::string(const std::string ¶m1, const std::string ¶m2)>;
23 const std::map<AVCodecServiceErrCode, std::string> AVCS_ERRCODE_INFOS = {
24 {AVCS_ERR_OK, "success"},
25 {AVCS_ERR_NO_MEMORY, "no memory"},
26 {AVCS_ERR_INVALID_OPERATION, "operation not be permitted"},
27 {AVCS_ERR_INVALID_VAL, "invalid argument"},
28 {AVCS_ERR_UNKNOWN, "unkown error"},
29 {AVCS_ERR_SERVICE_DIED, "avcodec service died"},
30 {AVCS_ERR_CREATE_AVCODEC_SUB_SERVICE_FAILED, "create avcodec sub service failed"},
31 {AVCS_ERR_CREATE_MUXER_SUB_SERVICE_FAILED, "create muxer sub service failed"},
32 {AVCS_ERR_CREATE_DEMUXER_SUB_SERVICE_FAILED, "create demuxer sub service failed"},
33 {AVCS_ERR_INVALID_STATE, "the state is not support this operation"},
34 {AVCS_ERR_UNSUPPORT, "unsupport interface"},
35 {AVCS_ERR_UNSUPPORT_AUD_SRC_TYPE, "unsupport audio source type"},
36 {AVCS_ERR_UNSUPPORT_AUD_SAMPLE_RATE, "unsupport audio sample rate"},
37 {AVCS_ERR_UNSUPPORT_AUD_CHANNEL_NUM, "unsupport audio channel"},
38 {AVCS_ERR_UNSUPPORT_AUD_ENC_TYPE, "unsupport audio encoder type"},
39 {AVCS_ERR_UNSUPPORT_AUD_PARAMS, "unsupport audio params(other params)"},
40 {AVCS_ERR_UNSUPPORT_VID_SRC_TYPE, "unsupport video source type"},
41 {AVCS_ERR_UNSUPPORT_VID_ENC_TYPE, "unsupport video encoder type"},
42 {AVCS_ERR_UNSUPPORT_VID_PARAMS, "unsupport video params(other params)"},
43 {AVCS_ERR_UNSUPPORT_CONTAINER_TYPE, "unsupport container format type"},
44 {AVCS_ERR_UNSUPPORT_PROTOCOL_TYPE, "unsupport protocol type"},
45 {AVCS_ERR_UNSUPPORT_VID_DEC_TYPE, "unsupport video decoder type"},
46 {AVCS_ERR_UNSUPPORT_AUD_DEC_TYPE, "unsupport audio decoder type"},
47 {AVCS_ERR_UNSUPPORT_STREAM, "internal data stream error"},
48 {AVCS_ERR_UNSUPPORT_FILE, "this appears to be a text file"},
49 {AVCS_ERR_UNSUPPORT_SOURCE, "unsupport source type"},
50 {AVCS_ERR_AUD_ENC_FAILED, "audio encode failed"},
51 {AVCS_ERR_AUD_RENDER_FAILED, "audio render failed"},
52 {AVCS_ERR_VID_ENC_FAILED, "video encode failed"},
53 {AVCS_ERR_AUD_DEC_FAILED, "audio decode failed"},
54 {AVCS_ERR_VID_DEC_FAILED, "video decode failed"},
55 {AVCS_ERR_MUXER_FAILED, "stream avmuxer failed"},
56 {AVCS_ERR_DEMUXER_FAILED, "stream demuxer or parser failed"},
57 {AVCS_ERR_OPEN_FILE_FAILED, "open file failed"},
58 {AVCS_ERR_FILE_ACCESS_FAILED, "read or write file failed"},
59 {AVCS_ERR_START_FAILED, "audio or video start failed"},
60 {AVCS_ERR_PAUSE_FAILED, "audio or video pause failed"},
61 {AVCS_ERR_STOP_FAILED, "audio or video stop failed"},
62 {AVCS_ERR_SEEK_FAILED, "audio or video seek failed"},
63 {AVCS_ERR_NETWORK_TIMEOUT, "network timeout"},
64 {AVCS_ERR_NOT_FIND_CONTAINER, "not find a demuxer"},
65 {AVCS_ERR_NOT_ENOUGH_DATA, "audio output buffer not enough of a pack"},
66 {AVCS_ERR_END_OF_STREAM, "end of stream"},
67 {AVCS_ERR_CONFIGURE_MISMATCH_CHANNEL_COUNT, "missing channel count attribute in configure"},
68 {AVCS_ERR_MISMATCH_SAMPLE_RATE, "missing sample rate attribute in configure"},
69 {AVCS_ERR_MISMATCH_BIT_RATE, "missing bit rate attribute in configure"},
70 {AVCS_ERR_CONFIGURE_ERROR, "compression level incorrect in flac encoder"},
71 {AVCS_ERR_EXTEND_START, "extend start error code"}};
72
73 const std::map<AVCodecServiceErrCode, OH_AVErrCode> AVCSERRCODE_TO_OHAVCODECERRCODE = {
74 {AVCS_ERR_OK, AV_ERR_OK},
75 {AVCS_ERR_NO_MEMORY, AV_ERR_NO_MEMORY},
76 {AVCS_ERR_INVALID_OPERATION, AV_ERR_OPERATE_NOT_PERMIT},
77 {AVCS_ERR_INVALID_VAL, AV_ERR_INVALID_VAL},
78 {AVCS_ERR_INVALID_DATA, AV_ERR_INVALID_VAL},
79 {AVCS_ERR_UNKNOWN, AV_ERR_UNKNOWN},
80 {AVCS_ERR_SERVICE_DIED, AV_ERR_SERVICE_DIED},
81 {AVCS_ERR_CREATE_AVCODEC_SUB_SERVICE_FAILED, AV_ERR_UNKNOWN},
82 {AVCS_ERR_CREATE_MUXER_SUB_SERVICE_FAILED, AV_ERR_UNKNOWN},
83 {AVCS_ERR_CREATE_DEMUXER_SUB_SERVICE_FAILED, AV_ERR_UNKNOWN},
84 {AVCS_ERR_INVALID_STATE, AV_ERR_INVALID_STATE},
85 {AVCS_ERR_UNSUPPORT, AV_ERR_UNSUPPORT},
86 {AVCS_ERR_UNSUPPORT_AUD_SRC_TYPE, AV_ERR_UNSUPPORT},
87 {AVCS_ERR_UNSUPPORT_AUD_SAMPLE_RATE, AV_ERR_UNSUPPORT},
88 {AVCS_ERR_UNSUPPORT_AUD_CHANNEL_NUM, AV_ERR_UNSUPPORT},
89 {AVCS_ERR_UNSUPPORT_AUD_ENC_TYPE, AV_ERR_UNSUPPORT},
90 {AVCS_ERR_UNSUPPORT_AUD_PARAMS, AV_ERR_UNSUPPORT},
91 {AVCS_ERR_UNSUPPORT_VID_SRC_TYPE, AV_ERR_UNSUPPORT},
92 {AVCS_ERR_UNSUPPORT_VID_ENC_TYPE, AV_ERR_UNSUPPORT},
93 {AVCS_ERR_UNSUPPORT_VID_PARAMS, AV_ERR_UNSUPPORT},
94 {AVCS_ERR_UNSUPPORT_CONTAINER_TYPE, AV_ERR_UNSUPPORT},
95 {AVCS_ERR_UNSUPPORT_PROTOCOL_TYPE, AV_ERR_UNSUPPORT},
96 {AVCS_ERR_UNSUPPORT_VID_DEC_TYPE, AV_ERR_UNSUPPORT},
97 {AVCS_ERR_UNSUPPORT_AUD_DEC_TYPE, AV_ERR_UNSUPPORT},
98 {AVCS_ERR_UNSUPPORT_STREAM, AV_ERR_UNSUPPORT},
99 {AVCS_ERR_UNSUPPORT_FILE, AV_ERR_UNSUPPORT},
100 {AVCS_ERR_UNSUPPORT_SOURCE, AV_ERR_UNSUPPORT},
101 {AVCS_ERR_AUD_RENDER_FAILED, AV_ERR_UNSUPPORT},
102 {AVCS_ERR_AUD_ENC_FAILED, AV_ERR_UNKNOWN},
103 {AVCS_ERR_VID_ENC_FAILED, AV_ERR_UNKNOWN},
104 {AVCS_ERR_AUD_DEC_FAILED, AV_ERR_UNKNOWN},
105 {AVCS_ERR_VID_DEC_FAILED, AV_ERR_UNKNOWN},
106 {AVCS_ERR_MUXER_FAILED, AV_ERR_UNKNOWN},
107 {AVCS_ERR_DEMUXER_FAILED, AV_ERR_UNKNOWN},
108 {AVCS_ERR_OPEN_FILE_FAILED, AV_ERR_IO},
109 {AVCS_ERR_FILE_ACCESS_FAILED, AV_ERR_IO},
110 {AVCS_ERR_START_FAILED, AV_ERR_UNKNOWN},
111 {AVCS_ERR_PAUSE_FAILED, AV_ERR_UNKNOWN},
112 {AVCS_ERR_STOP_FAILED, AV_ERR_UNKNOWN},
113 {AVCS_ERR_SEEK_FAILED, AV_ERR_UNKNOWN},
114 {AVCS_ERR_NETWORK_TIMEOUT, AV_ERR_TIMEOUT},
115 {AVCS_ERR_NOT_FIND_CONTAINER, AV_ERR_UNSUPPORT},
116 {AVCS_ERR_NOT_ENOUGH_DATA, AV_ERR_UNKNOWN},
117 {AVCS_ERR_END_OF_STREAM, AV_ERR_UNKNOWN},
118 {AVCS_ERR_CONFIGURE_MISMATCH_CHANNEL_COUNT, AV_ERR_UNSUPPORT},
119 {AVCS_ERR_MISMATCH_SAMPLE_RATE, AV_ERR_UNSUPPORT},
120 {AVCS_ERR_MISMATCH_BIT_RATE, AV_ERR_UNSUPPORT},
121 {AVCS_ERR_CONFIGURE_ERROR, AV_ERR_UNSUPPORT},
122 {AVCS_ERR_EXTEND_START, AV_ERR_EXTEND_START}};
123
124 const std::map<OH_AVErrCode, std::string> OHAVCODECERRCODE_INFOS = {
125 {AV_ERR_OK, "success"},
126 {AV_ERR_NO_MEMORY, "no memory"},
127 {AV_ERR_OPERATE_NOT_PERMIT, "operation not be permitted"},
128 {AV_ERR_INVALID_VAL, "invalid argument"},
129 {AV_ERR_IO, "IO error"},
130 {AV_ERR_TIMEOUT, "network timeout"},
131 {AV_ERR_UNKNOWN, "unkown error"},
132 {AV_ERR_SERVICE_DIED, "avcodec service died"},
133 {AV_ERR_INVALID_STATE, "the state is not support this operation"},
134 {AV_ERR_UNSUPPORT, "unsupport interface"},
135 {AV_ERR_EXTEND_START, "extend err start"},
136 };
137
ErrorMessageOk(const std::string & param1,const std::string & param2)138 std::string ErrorMessageOk(const std::string ¶m1, const std::string ¶m2)
139 {
140 (void)param1;
141 (void)param2;
142 return "success";
143 }
144
ErrorMessageNoPermission(const std::string & param1,const std::string & param2)145 std::string ErrorMessageNoPermission(const std::string ¶m1, const std::string ¶m2)
146 {
147 std::string message = "Try to do " + param1 + " failed. User should request permission " + param2 + " first.";
148 return message;
149 }
150
ErrorMessageInvalidParameter(const std::string & param1,const std::string & param2)151 std::string ErrorMessageInvalidParameter(const std::string ¶m1, const std::string ¶m2)
152 {
153 (void)param2;
154 std::string message = "The Parameter " + param1 + " is invalid. Please check the type and range.";
155 return message;
156 }
157
ErrorMessageUnsupportCapability(const std::string & param1,const std::string & param2)158 std::string ErrorMessageUnsupportCapability(const std::string ¶m1, const std::string ¶m2)
159 {
160 (void)param2;
161 std::string message = "Function " + param1 + " can not work correctly due to limited device capability.";
162 return message;
163 }
164
ErrorMessageNoMemory(const std::string & param1,const std::string & param2)165 std::string ErrorMessageNoMemory(const std::string ¶m1, const std::string ¶m2)
166 {
167 (void)param2;
168 std::string message = "Create " + param1 + " failed due to system memory.";
169 return message;
170 }
171
ErrorMessageOperateNotPermit(const std::string & param1,const std::string & param2)172 std::string ErrorMessageOperateNotPermit(const std::string ¶m1, const std::string ¶m2)
173 {
174 (void)param2;
175 std::string message = "The operate " + param1 + " failed due to not permit in current state.";
176 return message;
177 }
178
ErrorMessageIO(const std::string & param1,const std::string & param2)179 std::string ErrorMessageIO(const std::string ¶m1, const std::string ¶m2)
180 {
181 (void)param2;
182 std::string message = "IO error happened due to " + param1 + ".";
183 return message;
184 }
185
ErrorMessageTimeout(const std::string & param1,const std::string & param2)186 std::string ErrorMessageTimeout(const std::string ¶m1, const std::string ¶m2)
187 {
188 std::string message = "Timeout happend when " + param1 + " due to " + param2 + ".";
189 return message;
190 }
191
ErrorMessageServiceDied(const std::string & param1,const std::string & param2)192 std::string ErrorMessageServiceDied(const std::string ¶m1, const std::string ¶m2)
193 {
194 (void)param1;
195 (void)param2;
196 std::string message = "AVCodec Serviced Died.";
197 return message;
198 }
199
ErrorMessageUnsupportFormat(const std::string & param1,const std::string & param2)200 std::string ErrorMessageUnsupportFormat(const std::string ¶m1, const std::string ¶m2)
201 {
202 (void)param2;
203 std::string message = "The format " + param1 + " is not support.";
204 return message;
205 }
206
AVCSErrorToString(AVCodecServiceErrCode code)207 std::string AVCSErrorToString(AVCodecServiceErrCode code)
208 {
209 if (AVCS_ERRCODE_INFOS.count(code) != 0) {
210 return AVCS_ERRCODE_INFOS.at(code);
211 }
212
213 return "unkown error";
214 }
215
OHAVErrCodeToString(OH_AVErrCode code)216 std::string OHAVErrCodeToString(OH_AVErrCode code)
217 {
218 if (OHAVCODECERRCODE_INFOS.count(code) != 0) {
219 return OHAVCODECERRCODE_INFOS.at(code);
220 }
221
222 return "unkown error";
223 }
224
AVCSErrorToOHAVErrCodeString(AVCodecServiceErrCode code)225 std::string AVCSErrorToOHAVErrCodeString(AVCodecServiceErrCode code)
226 {
227 if (AVCS_ERRCODE_INFOS.count(code) != 0 && AVCSERRCODE_TO_OHAVCODECERRCODE.count(code) != 0) {
228 OH_AVErrCode extCode = AVCSERRCODE_TO_OHAVCODECERRCODE.at(code);
229 if (OHAVCODECERRCODE_INFOS.count(extCode) != 0) {
230 return OHAVCODECERRCODE_INFOS.at(extCode);
231 }
232 }
233
234 return "unkown error";
235 }
236
AVCSErrorToOHAVErrCode(AVCodecServiceErrCode code)237 OH_AVErrCode AVCSErrorToOHAVErrCode(AVCodecServiceErrCode code)
238 {
239 if (AVCS_ERRCODE_INFOS.count(code) != 0 && AVCSERRCODE_TO_OHAVCODECERRCODE.count(code) != 0) {
240 return AVCSERRCODE_TO_OHAVCODECERRCODE.at(code);
241 }
242
243 return AV_ERR_UNKNOWN;
244 }
245 } // namespace MediaAVCodec
246 } // namespace OHOS
247