• 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 "avcodec_errors.h"
17 #include <map>
18 #include <string>
19 
20 namespace OHOS {
21 namespace MediaAVCodec {
22 using namespace Media;
23 using ErrorMessageFunc = std::function<std::string(const std::string &param1, const std::string &param2)>;
24 const std::map<AVCodecServiceErrCode, std::string> AVCS_ERRCODE_INFOS = {
25     {AVCS_ERR_OK,                                    "success"},
26     {AVCS_ERR_NO_MEMORY,                             "no memory"},
27     {AVCS_ERR_INVALID_OPERATION,                     "operation not be permitted"},
28     {AVCS_ERR_INVALID_VAL,                           "invalid argument"},
29     {AVCS_ERR_UNKNOWN,                               "unkown error"},
30     {AVCS_ERR_SERVICE_DIED,                          "avcodec service died"},
31     {AVCS_ERR_CREATE_AVCODEC_SUB_SERVICE_FAILED,     "create avcodec sub service failed"},
32     {AVCS_ERR_CREATE_MUXER_SUB_SERVICE_FAILED,       "create muxer sub service failed"},
33     {AVCS_ERR_CREATE_DEMUXER_SUB_SERVICE_FAILED,     "create demuxer sub service failed"},
34     {AVCS_ERR_INVALID_STATE,                         "the state is not support this operation"},
35     {AVCS_ERR_UNSUPPORT,                             "unsupport interface"},
36     {AVCS_ERR_UNSUPPORT_AUD_SRC_TYPE,                "unsupport audio source type"},
37     {AVCS_ERR_UNSUPPORT_AUD_SAMPLE_RATE,             "unsupport audio sample rate"},
38     {AVCS_ERR_UNSUPPORT_AUD_CHANNEL_NUM,             "unsupport audio channel"},
39     {AVCS_ERR_UNSUPPORT_AUD_ENC_TYPE,                "unsupport audio encoder type"},
40     {AVCS_ERR_UNSUPPORT_AUD_PARAMS,                  "unsupport audio params(other params)"},
41     {AVCS_ERR_UNSUPPORT_VID_SRC_TYPE,                "unsupport video source type"},
42     {AVCS_ERR_UNSUPPORT_VID_ENC_TYPE,                "unsupport video encoder type"},
43     {AVCS_ERR_UNSUPPORT_VID_PARAMS,                  "unsupport video params(other params)"},
44     {AVCS_ERR_UNSUPPORT_FILE_TYPE,                   "unsupport file format type"},
45     {AVCS_ERR_UNSUPPORT_PROTOCOL_TYPE,               "unsupport protocol type"},
46     {AVCS_ERR_UNSUPPORT_VID_DEC_TYPE,                "unsupport video decoder type"},
47     {AVCS_ERR_UNSUPPORT_AUD_DEC_TYPE,                "unsupport audio decoder type"},
48     {AVCS_ERR_UNSUPPORT_STREAM,                      "internal data stream error"},
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_FILE,                         "not find a file"},
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_FILE_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_SOURCE,                    AV_ERR_UNSUPPORT},
100     {AVCS_ERR_AUD_RENDER_FAILED,                   AV_ERR_UNSUPPORT},
101     {AVCS_ERR_AUD_ENC_FAILED,                      AV_ERR_UNKNOWN},
102     {AVCS_ERR_VID_ENC_FAILED,                      AV_ERR_UNKNOWN},
103     {AVCS_ERR_AUD_DEC_FAILED,                      AV_ERR_UNKNOWN},
104     {AVCS_ERR_VID_DEC_FAILED,                      AV_ERR_UNKNOWN},
105     {AVCS_ERR_MUXER_FAILED,                        AV_ERR_UNKNOWN},
106     {AVCS_ERR_DEMUXER_FAILED,                      AV_ERR_UNKNOWN},
107     {AVCS_ERR_OPEN_FILE_FAILED,                    AV_ERR_IO},
108     {AVCS_ERR_FILE_ACCESS_FAILED,                  AV_ERR_IO},
109     {AVCS_ERR_START_FAILED,                        AV_ERR_UNKNOWN},
110     {AVCS_ERR_PAUSE_FAILED,                        AV_ERR_UNKNOWN},
111     {AVCS_ERR_STOP_FAILED,                         AV_ERR_UNKNOWN},
112     {AVCS_ERR_SEEK_FAILED,                         AV_ERR_UNKNOWN},
113     {AVCS_ERR_NETWORK_TIMEOUT,                     AV_ERR_TIMEOUT},
114     {AVCS_ERR_NOT_FIND_FILE,                  AV_ERR_UNSUPPORT},
115     {AVCS_ERR_NOT_ENOUGH_DATA,                     AV_ERR_UNKNOWN},
116     {AVCS_ERR_END_OF_STREAM,                       AV_ERR_UNKNOWN},
117     {AVCS_ERR_CONFIGURE_MISMATCH_CHANNEL_COUNT,    AV_ERR_UNSUPPORT},
118     {AVCS_ERR_MISMATCH_SAMPLE_RATE,                AV_ERR_UNSUPPORT},
119     {AVCS_ERR_MISMATCH_BIT_RATE,                   AV_ERR_UNSUPPORT},
120     {AVCS_ERR_CONFIGURE_ERROR,                     AV_ERR_UNSUPPORT},
121     {AVCS_ERR_EXTEND_START,                        AV_ERR_EXTEND_START}};
122 
123 const std::map<OH_AVErrCode, std::string> OHAVCODECERRCODE_INFOS = {
124     {AV_ERR_OK,                    "success"},
125     {AV_ERR_NO_MEMORY,             "no memory"},
126     {AV_ERR_OPERATE_NOT_PERMIT,    "operation not be permitted"},
127     {AV_ERR_INVALID_VAL,           "invalid argument"},
128     {AV_ERR_IO,                    "IO error"},
129     {AV_ERR_TIMEOUT,               "network timeout"},
130     {AV_ERR_UNKNOWN,               "unkown error"},
131     {AV_ERR_SERVICE_DIED,          "avcodec service died"},
132     {AV_ERR_INVALID_STATE,         "the state is not support this operation"},
133     {AV_ERR_UNSUPPORT,             "unsupport interface"},
134     {AV_ERR_EXTEND_START,          "extend err start"},
135 };
136 
137 const std::map<Status, AVCodecServiceErrCode> STATUS_TO_AVCSERRCODE = {
138     {Status::END_OF_STREAM, AVCodecServiceErrCode::AVCS_ERR_OK},
139     {Status::OK, AVCodecServiceErrCode::AVCS_ERR_OK},
140     {Status::NO_ERROR, AVCodecServiceErrCode::AVCS_ERR_OK},
141     {Status::ERROR_UNKNOWN, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN},
142     {Status::ERROR_PLUGIN_ALREADY_EXISTS, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN},
143     {Status::ERROR_INCOMPATIBLE_VERSION, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN},
144     {Status::ERROR_NO_MEMORY, AVCodecServiceErrCode::AVCS_ERR_NO_MEMORY},
145     {Status::ERROR_WRONG_STATE, AVCodecServiceErrCode::AVCS_ERR_INVALID_OPERATION},
146     {Status::ERROR_UNIMPLEMENTED, AVCodecServiceErrCode::AVCS_ERR_UNSUPPORT},
147     {Status::ERROR_INVALID_PARAMETER, AVCodecServiceErrCode::AVCS_ERR_INVALID_VAL},
148     {Status::ERROR_INVALID_DATA, AVCodecServiceErrCode::AVCS_ERR_INVALID_VAL},
149     {Status::ERROR_MISMATCHED_TYPE, AVCodecServiceErrCode::AVCS_ERR_INVALID_VAL},
150     {Status::ERROR_TIMED_OUT, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN},
151     {Status::ERROR_UNSUPPORTED_FORMAT, AVCodecServiceErrCode::AVCS_ERR_UNSUPPORT_FILE_TYPE},
152     {Status::ERROR_NOT_ENOUGH_DATA, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN},
153     {Status::ERROR_NOT_EXISTED, AVCodecServiceErrCode::AVCS_ERR_OPEN_FILE_FAILED},
154     {Status::ERROR_AGAIN, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN},
155     {Status::ERROR_PERMISSION_DENIED, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN},
156     {Status::ERROR_NULL_POINTER, AVCodecServiceErrCode::AVCS_ERR_INVALID_VAL},
157     {Status::ERROR_INVALID_OPERATION, AVCodecServiceErrCode::AVCS_ERR_INVALID_OPERATION},
158     {Status::ERROR_CLIENT, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN},
159     {Status::ERROR_SERVER, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN},
160     {Status::ERROR_DELAY_READY, AVCodecServiceErrCode::AVCS_ERR_OK},
161     {Status::ERROR_INVALID_STATE, AVCodecServiceErrCode::AVCS_ERR_INVALID_OPERATION},
162     {Status::ERROR_INVALID_BUFFER_SIZE, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN},
163     {Status::ERROR_UNEXPECTED_MEMORY_TYPE, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN},
164     {Status::ERROR_CREATE_BUFFER, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN},
165     {Status::ERROR_NULL_POINT_BUFFER, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN},
166     {Status::ERROR_INVALID_BUFFER_ID, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN},
167     {Status::ERROR_INVALID_BUFFER_STATE, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN},
168     {Status::ERROR_NO_FREE_BUFFER, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN},
169     {Status::ERROR_NO_DIRTY_BUFFER, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN},
170     {Status::ERROR_NO_CONSUMER_LISTENER, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN},
171     {Status::ERROR_NULL_BUFFER_QUEUE, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN},
172     {Status::ERROR_WAIT_TIMEOUT, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN},
173     {Status::ERROR_OUT_OF_RANGE, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN},
174     {Status::ERROR_NULL_SURFACE, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN},
175     {Status::ERROR_SURFACE_INNER, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN},
176     {Status::ERROR_NULL_SURFACE_BUFFER, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN},
177     {Status::ERROR_IPC_WRITE_INTERFACE_TOKEN, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN},
178     {Status::ERROR_IPC_SEND_REQUEST, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN}};
179 
ErrorMessageOk(const std::string & param1,const std::string & param2)180 std::string ErrorMessageOk(const std::string &param1, const std::string &param2)
181 {
182     (void)param1;
183     (void)param2;
184     return "success";
185 }
186 
ErrorMessageNoPermission(const std::string & param1,const std::string & param2)187 std::string ErrorMessageNoPermission(const std::string &param1, const std::string &param2)
188 {
189     std::string message = "Try to do " + param1 + " failed. User should request permission " + param2 + " first.";
190     return message;
191 }
192 
ErrorMessageInvalidParameter(const std::string & param1,const std::string & param2)193 std::string ErrorMessageInvalidParameter(const std::string &param1, const std::string &param2)
194 {
195     (void)param2;
196     std::string message = "The Parameter " + param1 + " is invalid. Please check the type and range.";
197     return message;
198 }
199 
ErrorMessageUnsupportCapability(const std::string & param1,const std::string & param2)200 std::string ErrorMessageUnsupportCapability(const std::string &param1, const std::string &param2)
201 {
202     (void)param2;
203     std::string message = "Function " + param1 + " can not work correctly due to limited device capability.";
204     return message;
205 }
206 
ErrorMessageNoMemory(const std::string & param1,const std::string & param2)207 std::string ErrorMessageNoMemory(const std::string &param1, const std::string &param2)
208 {
209     (void)param2;
210     std::string message = "Create " + param1 + " failed due to system memory.";
211     return message;
212 }
213 
ErrorMessageOperateNotPermit(const std::string & param1,const std::string & param2)214 std::string ErrorMessageOperateNotPermit(const std::string &param1, const std::string &param2)
215 {
216     (void)param2;
217     std::string message = "The operate " + param1 + " failed due to not permit in current state.";
218     return message;
219 }
220 
ErrorMessageIO(const std::string & param1,const std::string & param2)221 std::string ErrorMessageIO(const std::string &param1, const std::string &param2)
222 {
223     (void)param2;
224     std::string message = "IO error happened due to " + param1 + ".";
225     return message;
226 }
227 
ErrorMessageTimeout(const std::string & param1,const std::string & param2)228 std::string ErrorMessageTimeout(const std::string &param1, const std::string &param2)
229 {
230     std::string message = "Timeout happend when " + param1 + " due to " + param2 + ".";
231     return message;
232 }
233 
ErrorMessageServiceDied(const std::string & param1,const std::string & param2)234 std::string ErrorMessageServiceDied(const std::string &param1, const std::string &param2)
235 {
236     (void)param1;
237     (void)param2;
238     std::string message = "AVCodec Serviced Died.";
239     return message;
240 }
241 
ErrorMessageUnsupportFormat(const std::string & param1,const std::string & param2)242 std::string ErrorMessageUnsupportFormat(const std::string &param1, const std::string &param2)
243 {
244     (void)param2;
245     std::string message = "The format " + param1 + " is not support.";
246     return message;
247 }
248 
AVCSErrorToString(AVCodecServiceErrCode code)249 std::string AVCSErrorToString(AVCodecServiceErrCode code)
250 {
251     if (AVCS_ERRCODE_INFOS.count(code) != 0) {
252         return AVCS_ERRCODE_INFOS.at(code);
253     }
254 
255     return "unkown error";
256 }
257 
OHAVErrCodeToString(OH_AVErrCode code)258 std::string OHAVErrCodeToString(OH_AVErrCode code)
259 {
260     if (OHAVCODECERRCODE_INFOS.count(code) != 0) {
261         return OHAVCODECERRCODE_INFOS.at(code);
262     }
263 
264     return "unkown error";
265 }
266 
AVCSErrorToOHAVErrCodeString(AVCodecServiceErrCode code)267 std::string AVCSErrorToOHAVErrCodeString(AVCodecServiceErrCode code)
268 {
269     if (AVCS_ERRCODE_INFOS.count(code) != 0 && AVCSERRCODE_TO_OHAVCODECERRCODE.count(code) != 0) {
270         OH_AVErrCode extCode = AVCSERRCODE_TO_OHAVCODECERRCODE.at(code);
271         if (OHAVCODECERRCODE_INFOS.count(extCode) != 0) {
272             return OHAVCODECERRCODE_INFOS.at(extCode);
273         }
274     }
275 
276     return "unkown error";
277 }
278 
AVCSErrorToOHAVErrCode(AVCodecServiceErrCode code)279 OH_AVErrCode AVCSErrorToOHAVErrCode(AVCodecServiceErrCode code)
280 {
281     if (AVCS_ERRCODE_INFOS.count(code) != 0 && AVCSERRCODE_TO_OHAVCODECERRCODE.count(code) != 0) {
282         return AVCSERRCODE_TO_OHAVCODECERRCODE.at(code);
283     }
284 
285     return AV_ERR_UNKNOWN;
286 }
287 
StatusToAVCodecServiceErrCode(Status code)288 AVCodecServiceErrCode StatusToAVCodecServiceErrCode(Status code)
289 {
290     if (STATUS_TO_AVCSERRCODE.count(code) != 0) {
291         return STATUS_TO_AVCSERRCODE.at(code);
292     }
293 
294     return AVCodecServiceErrCode::AVCS_ERR_UNKNOWN;
295 }
296 } // namespace MediaAVCodec
297 } // namespace OHOS
298