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 "napi/native_api.h"
17 #include <condition_variable>
18 #include <js_native_api_types.h>
19 #include <multimedia/player_framework/native_avcodec_videodecoder.h>
20 #include <multimedia/player_framework/native_avcapability.h>
21 #include <multimedia/player_framework/native_avcodec_base.h>
22 #include <multimedia/player_framework/native_avformat.h>
23 #include <pthread.h>
24 #include <iostream>
25 #include <fstream>
26 #include <queue>
27
28 #define FAIL (-1)
29 #define SUCCESS 0
30 #define PARAM_0 0
31 #define PARAM_1 1
32 #define PARAM_2 2
33 #define PARAM_3 3
34 #define PARAM_4 4
35 #define PARAM_5 5
36 #define PARAM_6 6
37 #define PARAM_7 7
38 using namespace std;
39
40 constexpr uint32_t DEFAULT_WIDTH = 320;
41
42 constexpr uint32_t DEFAULT_HEIGHT = 240;
43
44 constexpr OH_AVPixelFormat DEFAULT_PIXELFORMAT = AV_PIXEL_FORMAT_YUVI420;
45
OHVideoDecoderCreateByMime(napi_env env,napi_callback_info info)46 static napi_value OHVideoDecoderCreateByMime(napi_env env, napi_callback_info info)
47 {
48 int backParam = FAIL;
49 OH_AVCodec *checkParam = nullptr;
50 checkParam = OH_VideoDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_HEVC);
51 if (checkParam != nullptr) {
52 backParam = SUCCESS;
53 }
54 napi_value result = nullptr;
55 OH_VideoDecoder_Destroy(checkParam);
56 napi_create_int32(env, backParam, &result);
57 return result;
58 }
59
OHVideoDecoderCreateByName(napi_env env,napi_callback_info info)60 static napi_value OHVideoDecoderCreateByName(napi_env env, napi_callback_info info)
61 {
62 int backParam = FAIL;
63 OH_AVCodec *checkParam = nullptr;
64 OH_AVCapability *capability = OH_AVCodec_GetCapability(OH_AVCODEC_MIMETYPE_VIDEO_AVC, false);
65 const char *codecName = OH_AVCapability_GetName(capability);
66 checkParam = OH_VideoDecoder_CreateByName(codecName);
67 if (checkParam != nullptr) {
68 backParam = SUCCESS;
69 }
70 napi_value result = nullptr;
71 OH_VideoDecoder_Destroy(checkParam);
72 napi_create_int32(env, backParam, &result);
73 return result;
74 }
75
OHVideoDecoderDestroy(napi_env env,napi_callback_info info)76 static napi_value OHVideoDecoderDestroy(napi_env env, napi_callback_info info)
77 {
78 int backParam = FAIL;
79 napi_value result = nullptr;
80 OH_AVCodec *videoDec = nullptr;
81 OH_AVErrCode checkParam;
82 videoDec = OH_VideoDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
83 checkParam = OH_VideoDecoder_Destroy(videoDec);
84 if (checkParam == AV_ERR_OK) {
85 backParam = SUCCESS;
86 }
87 videoDec = nullptr;
88 napi_create_int32(env, backParam, &result);
89 return result;
90 }
91
92 class VDecSignal {
93 public:
94 std::mutex inMutex_;
95 std::mutex outMutex_;
96 std::condition_variable inCond_;
97 std::condition_variable outCond_;
98 std::queue<uint32_t> inQueue_;
99 std::queue<uint32_t> outQueue_;
100 std::queue<OH_AVMemory *> inBufferQueue_;
101 std::queue<OH_AVMemory *> outBufferQueue_;
102 std::queue<OH_AVCodecBufferAttr> attrQueue_;
103 };
OnError(OH_AVCodec * codec,int32_t errorCode,void * userData)104 static void OnError(OH_AVCodec *codec, int32_t errorCode, void *userData)
105 {
106 (void)codec;
107 (void)errorCode;
108 (void)userData;
109 }
110
OnStreamChanged(OH_AVCodec * codec,OH_AVFormat * format,void * userData)111 static void OnStreamChanged(OH_AVCodec *codec, OH_AVFormat *format, void *userData)
112 {
113 (void)codec;
114 (void)format;
115 (void)userData;
116 }
117
OnNeedInputData(OH_AVCodec * codec,uint32_t index,OH_AVMemory * mem,void * userData)118 static void OnNeedInputData(OH_AVCodec *codec, uint32_t index, OH_AVMemory *mem, void *userData)
119 {
120 (void)userData;
121 }
122
OnNeedOutputData(OH_AVCodec * codec,uint32_t index,OH_AVMemory * mem,OH_AVCodecBufferAttr * attr,void * userData)123 static void OnNeedOutputData(OH_AVCodec *codec, uint32_t index, OH_AVMemory *mem, OH_AVCodecBufferAttr *attr,
124 void *userData)
125 {
126 (void)userData;
127 }
128
OnNeedInputBuffer(OH_AVCodec * codec,uint32_t index,OH_AVBuffer * buffer,void * userData)129 static void OnNeedInputBuffer(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *buffer, void *userData)
130 {
131 (void)userData;
132 }
133
OnNeedOutputBuffer(OH_AVCodec * codec,uint32_t index,OH_AVBuffer * buffer,void * userData)134 static void OnNeedOutputBuffer(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *buffer, void *userData)
135 {
136 (void)userData;
137 }
138
OHVideoDecoderSetCallback(napi_env env,napi_callback_info info)139 static napi_value OHVideoDecoderSetCallback(napi_env env, napi_callback_info info)
140 {
141 int backParam = FAIL;
142 napi_value result = nullptr;
143 OH_AVCodec *videoDec = nullptr;
144 OH_AVErrCode checkParam;
145 videoDec = OH_VideoDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
146 OH_AVCodecAsyncCallback callback = { &OnError, &OnStreamChanged, &OnNeedInputData, &OnNeedOutputData };
147 checkParam = OH_VideoDecoder_SetCallback(videoDec, callback, nullptr);
148 if (checkParam == AV_ERR_OK) {
149 backParam = SUCCESS;
150 }
151 OH_VideoDecoder_Destroy(videoDec);
152 napi_create_int32(env, backParam, &result);
153 return result;
154 }
155
OHVideoDecoderRegisterCallback(napi_env env,napi_callback_info info)156 static napi_value OHVideoDecoderRegisterCallback(napi_env env, napi_callback_info info)
157 {
158 int backParam = FAIL;
159 napi_value result = nullptr;
160 OH_AVCodec *videoDec = nullptr;
161 OH_AVErrCode checkParam;
162 videoDec = OH_VideoDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
163 OH_AVCodecCallback callback = { &OnError, &OnStreamChanged, &OnNeedInputBuffer, &OnNeedOutputBuffer };
164 checkParam = OH_VideoDecoder_RegisterCallback(videoDec, callback, nullptr);
165 if (checkParam == AV_ERR_OK) {
166 backParam = SUCCESS;
167 }
168 OH_VideoDecoder_Destroy(videoDec);
169 napi_create_int32(env, backParam, &result);
170 return result;
171 }
172
OHVideoDecoderConfigure(napi_env env,napi_callback_info info)173 static napi_value OHVideoDecoderConfigure(napi_env env, napi_callback_info info)
174 {
175 int backParam = FAIL;
176 napi_value result = nullptr;
177 OH_AVCodec *videoDec = nullptr;
178 OH_AVErrCode checkParam;
179 OH_AVFormat *format = nullptr;
180 format = OH_AVFormat_Create();
181 OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
182 OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
183 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV21);
184 videoDec = OH_VideoDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
185 checkParam = OH_VideoDecoder_Configure(videoDec, format);
186 if (checkParam == AV_ERR_OK) {
187 backParam = SUCCESS;
188 OH_AVFormat_Destroy(format);
189 }
190 OH_VideoDecoder_Destroy(videoDec);
191 napi_create_int32(env, backParam, &result);
192 return result;
193 }
194
OHVideoDecoderPrepare(napi_env env,napi_callback_info info)195 static napi_value OHVideoDecoderPrepare(napi_env env, napi_callback_info info)
196 {
197 int backParam = FAIL;
198 napi_value result = nullptr;
199 OH_AVCodec *videoDec = nullptr;
200 OH_AVErrCode checkParam;
201 OH_AVFormat *format = nullptr;
202 format = OH_AVFormat_Create();
203 OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
204 OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
205 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, DEFAULT_PIXELFORMAT);
206 videoDec = OH_VideoDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
207 OH_VideoDecoder_Configure(videoDec, format);
208 checkParam = OH_VideoDecoder_Prepare(videoDec);
209 if (checkParam == AV_ERR_OK) {
210 backParam = SUCCESS;
211 OH_AVFormat_Destroy(format);
212 }
213 OH_VideoDecoder_Destroy(videoDec);
214 napi_create_int32(env, backParam, &result);
215 return result;
216 }
217
OHVideoDecoderStart(napi_env env,napi_callback_info info)218 static napi_value OHVideoDecoderStart(napi_env env, napi_callback_info info)
219 {
220 size_t argc = PARAM_6;
221 napi_value args[PARAM_6] = {nullptr};
222 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
223 int firstParam;
224 int secondParam;
225 int thirdParam;
226 int fourthParam;
227 int fifthParam;
228 int sixthParam;
229 const char *mimeType = nullptr;
230 napi_get_value_int32(env, args[PARAM_0], &firstParam);
231 napi_get_value_int32(env, args[PARAM_1], &secondParam);
232 napi_get_value_int32(env, args[PARAM_2], &thirdParam);
233 napi_get_value_int32(env, args[PARAM_3], &fourthParam);
234 napi_get_value_int32(env, args[PARAM_4], &fifthParam);
235 napi_get_value_int32(env, args[PARAM_5], &sixthParam);
236
237 int backParam = FAIL;
238 napi_value result = nullptr;
239 OH_AVCodec *videoDec = nullptr;
240 OH_AVErrCode checkParam;
241 OH_AVFormat *format = nullptr;
242
243 if (firstParam == PARAM_1) {
244 mimeType = OH_AVCODEC_MIMETYPE_VIDEO_AVC;
245 } else if (firstParam == PARAM_0) {
246 mimeType = nullptr;
247 } else if (firstParam == PARAM_2) {
248 mimeType = OH_AVCODEC_MIMETYPE_VIDEO_HEVC;
249 }
250
251 if (secondParam == PARAM_1) {
252 videoDec = OH_VideoDecoder_CreateByMime(mimeType);
253 } else if (secondParam == PARAM_0) {
254 videoDec = nullptr;
255 }
256
257 if (thirdParam == PARAM_1) {
258 format = OH_AVFormat_Create();
259 OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
260 OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
261 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, DEFAULT_PIXELFORMAT);
262 }
263
264 OH_VideoDecoder_Configure(videoDec, format);
265
266 if (fourthParam == PARAM_1) {
267 OH_VideoDecoder_Prepare(videoDec);
268 } else if (fourthParam == PARAM_0) {
269 OH_VideoDecoder_Prepare(nullptr);
270 }
271
272 if (fifthParam == PARAM_1) {
273 checkParam = OH_VideoDecoder_Start(videoDec);
274 } else if (fifthParam == PARAM_0) {
275 checkParam = OH_VideoDecoder_Start(nullptr);
276 }
277
278 if (sixthParam == PARAM_1) {
279 if (checkParam == AV_ERR_OK) {
280 backParam = SUCCESS;
281 }
282 } else if (sixthParam == PARAM_0) {
283 if (checkParam != AV_ERR_OK) {
284 backParam = SUCCESS;
285 }
286 }
287 OH_AVFormat_Destroy(format);
288 OH_VideoDecoder_Destroy(videoDec);
289 napi_create_int32(env, backParam, &result);
290 return result;
291 }
292
OHVideoDecoderStop(napi_env env,napi_callback_info info)293 static napi_value OHVideoDecoderStop(napi_env env, napi_callback_info info)
294 {
295 size_t argc = PARAM_7;
296 napi_value args[PARAM_7] = {nullptr};
297 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
298 int firstParam;
299 int secondParam;
300 int thirdParam;
301 int fourthParam;
302 int fifthParam;
303 int sixthParam;
304 int seventhParam;
305 const char *mimeType = nullptr;
306 napi_get_value_int32(env, args[PARAM_0], &firstParam);
307 napi_get_value_int32(env, args[PARAM_1], &secondParam);
308 napi_get_value_int32(env, args[PARAM_2], &thirdParam);
309 napi_get_value_int32(env, args[PARAM_3], &fourthParam);
310 napi_get_value_int32(env, args[PARAM_4], &fifthParam);
311 napi_get_value_int32(env, args[PARAM_5], &sixthParam);
312 napi_get_value_int32(env, args[PARAM_6], &seventhParam);
313
314 int backParam = FAIL;
315 napi_value result = nullptr;
316 OH_AVCodec *videoDec = nullptr;
317 OH_AVErrCode checkParam;
318 OH_AVFormat *format = nullptr;
319
320 if (firstParam == PARAM_1) {
321 mimeType = OH_AVCODEC_MIMETYPE_VIDEO_AVC;
322 } else if (firstParam == PARAM_0) {
323 mimeType = nullptr;
324 } else if (firstParam == PARAM_2) {
325 mimeType = OH_AVCODEC_MIMETYPE_VIDEO_HEVC;
326 }
327
328 if (secondParam == PARAM_1) {
329 videoDec = OH_VideoDecoder_CreateByMime(mimeType);
330 } else if (secondParam == PARAM_0) {
331 videoDec = nullptr;
332 }
333
334 if (thirdParam == PARAM_1) {
335 format = OH_AVFormat_Create();
336 OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
337 OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
338 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, DEFAULT_PIXELFORMAT);
339 }
340
341 OH_VideoDecoder_Configure(videoDec, format);
342
343 if (fourthParam == PARAM_1) {
344 OH_VideoDecoder_Prepare(videoDec);
345 } else if (fourthParam == PARAM_0) {
346 OH_VideoDecoder_Prepare(nullptr);
347 }
348
349 if (fifthParam == PARAM_1) {
350 OH_VideoDecoder_Start(videoDec);
351 } else if (fifthParam == PARAM_0) {
352 OH_VideoDecoder_Start(nullptr);
353 }
354
355 if (sixthParam == PARAM_1) {
356 checkParam = OH_VideoDecoder_Stop(videoDec);
357 } else if (sixthParam == PARAM_0) {
358 checkParam = OH_VideoDecoder_Stop(nullptr);
359 }
360
361 if (seventhParam == PARAM_1) {
362 if (checkParam == AV_ERR_OK) {
363 backParam = SUCCESS;
364 }
365 } else if (seventhParam == PARAM_0) {
366 if (checkParam != AV_ERR_OK) {
367 backParam = SUCCESS;
368 }
369 }
370 OH_AVFormat_Destroy(format);
371 OH_VideoDecoder_Destroy(videoDec);
372 napi_create_int32(env, backParam, &result);
373 return result;
374 }
375
OHVideoDecoderFlush(napi_env env,napi_callback_info info)376 static napi_value OHVideoDecoderFlush(napi_env env, napi_callback_info info)
377 {
378 size_t argc = PARAM_7;
379 napi_value args[PARAM_7] = {nullptr};
380 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
381 int firstParam;
382 int secondParam;
383 int thirdParam;
384 int fourthParam;
385 int fifthParam;
386 int sixthParam;
387 int seventhParam;
388 const char *mimeType = nullptr;
389 napi_get_value_int32(env, args[PARAM_0], &firstParam);
390 napi_get_value_int32(env, args[PARAM_1], &secondParam);
391 napi_get_value_int32(env, args[PARAM_2], &thirdParam);
392 napi_get_value_int32(env, args[PARAM_3], &fourthParam);
393 napi_get_value_int32(env, args[PARAM_4], &fifthParam);
394 napi_get_value_int32(env, args[PARAM_5], &sixthParam);
395 napi_get_value_int32(env, args[PARAM_6], &seventhParam);
396
397 int backParam = FAIL;
398 napi_value result = nullptr;
399 OH_AVCodec *videoDec = nullptr;
400 OH_AVErrCode checkParam;
401 OH_AVFormat *format = nullptr;
402
403 if (firstParam == PARAM_1) {
404 mimeType = OH_AVCODEC_MIMETYPE_VIDEO_AVC;
405 } else if (firstParam == PARAM_0) {
406 mimeType = nullptr;
407 } else if (firstParam == PARAM_2) {
408 mimeType = OH_AVCODEC_MIMETYPE_VIDEO_HEVC;
409 }
410
411 if (secondParam == PARAM_1) {
412 videoDec = OH_VideoDecoder_CreateByMime(mimeType);
413 } else if (secondParam == PARAM_0) {
414 videoDec = nullptr;
415 }
416
417 if (thirdParam == PARAM_1) {
418 format = OH_AVFormat_Create();
419 OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
420 OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
421 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, DEFAULT_PIXELFORMAT);
422 }
423
424 OH_VideoDecoder_Configure(videoDec, format);
425
426 if (fourthParam == PARAM_1) {
427 OH_VideoDecoder_Prepare(videoDec);
428 } else if (fourthParam == PARAM_0) {
429 OH_VideoDecoder_Prepare(nullptr);
430 }
431
432 if (fifthParam == PARAM_1) {
433 OH_VideoDecoder_Start(videoDec);
434 } else if (fifthParam == PARAM_0) {
435 OH_VideoDecoder_Start(nullptr);
436 }
437
438 if (sixthParam == PARAM_1) {
439 checkParam = OH_VideoDecoder_Flush(videoDec);
440 } else if (sixthParam == PARAM_0) {
441 checkParam = OH_VideoDecoder_Flush(nullptr);
442 }
443
444 if (seventhParam == PARAM_1) {
445 if (checkParam == AV_ERR_OK) {
446 backParam = SUCCESS;
447 }
448 } else if (seventhParam == PARAM_0) {
449 if (checkParam != AV_ERR_OK) {
450 backParam = SUCCESS;
451 }
452 }
453 OH_VideoDecoder_Stop(videoDec);
454 OH_AVFormat_Destroy(format);
455 OH_VideoDecoder_Destroy(videoDec);
456 napi_create_int32(env, backParam, &result);
457 return result;
458 }
459
OHVideoDecoderReset(napi_env env,napi_callback_info info)460 static napi_value OHVideoDecoderReset(napi_env env, napi_callback_info info)
461 {
462 int backParam = FAIL;
463 napi_value result = nullptr;
464 OH_AVCodec *videoDec = nullptr;
465 OH_AVErrCode checkParam;
466 OH_AVFormat *format = nullptr;
467 format = OH_AVFormat_Create();
468 OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
469 OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
470 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, DEFAULT_PIXELFORMAT);
471 videoDec = OH_VideoDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
472 OH_VideoDecoder_Configure(videoDec, format);
473 OH_VideoDecoder_Prepare(videoDec);
474 if (OH_VideoDecoder_Start(videoDec) == AV_ERR_OK) {
475 checkParam = OH_VideoDecoder_Reset(videoDec);
476 if (checkParam == AV_ERR_OK) {
477 backParam = SUCCESS;
478 OH_AVFormat_Destroy(format);
479 }
480 }
481 OH_VideoDecoder_Destroy(videoDec);
482 napi_create_int32(env, backParam, &result);
483 return result;
484 }
485
OHVideoDecoderGetOutputDescription(napi_env env,napi_callback_info info)486 static napi_value OHVideoDecoderGetOutputDescription(napi_env env, napi_callback_info info)
487 {
488 int backParam = FAIL;
489 napi_value result = nullptr;
490 OH_AVCodec *videoDec = nullptr;
491 OH_AVFormat *checkParam = nullptr;
492 OH_AVFormat *format = nullptr;
493 format = OH_AVFormat_Create();
494 OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
495 OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
496 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, DEFAULT_PIXELFORMAT);
497 videoDec = OH_VideoDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
498 OH_VideoDecoder_Configure(videoDec, format);
499 OH_VideoDecoder_Prepare(videoDec);
500 if (OH_VideoDecoder_Start(videoDec) == AV_ERR_OK) {
501 checkParam = OH_VideoDecoder_GetOutputDescription(videoDec);
502 if (checkParam != nullptr) {
503 backParam = SUCCESS;
504 OH_VideoDecoder_Stop(videoDec);
505 OH_AVFormat_Destroy(format);
506 }
507 }
508 OH_VideoDecoder_Destroy(videoDec);
509 napi_create_int32(env, backParam, &result);
510 return result;
511 }
512
OHVideoDecoderSetParameter(napi_env env,napi_callback_info info)513 static napi_value OHVideoDecoderSetParameter(napi_env env, napi_callback_info info)
514 {
515 int backParam = FAIL;
516 napi_value result = nullptr;
517 OH_AVCodec *videoDec = nullptr;
518 OH_AVErrCode checkParam;
519 OH_AVFormat *format = nullptr;
520 format = OH_AVFormat_Create();
521 OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
522 OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
523 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, DEFAULT_PIXELFORMAT);
524 videoDec = OH_VideoDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
525 OH_VideoDecoder_Configure(videoDec, format);
526 OH_VideoDecoder_Prepare(videoDec);
527 checkParam = OH_VideoDecoder_Start(videoDec);
528 if (checkParam == AV_ERR_OK) {
529 checkParam = OH_VideoDecoder_SetParameter(videoDec, format);
530 if (checkParam == AV_ERR_OK) {
531 backParam = SUCCESS;
532 OH_VideoDecoder_Stop(videoDec);
533 OH_AVFormat_Destroy(format);
534 }
535 }
536 OH_VideoDecoder_Destroy(videoDec);
537 napi_create_int32(env, backParam, &result);
538 return result;
539 }
540
OHVideoDecoderPushInputData(napi_env env,napi_callback_info info)541 static napi_value OHVideoDecoderPushInputData(napi_env env, napi_callback_info info)
542 {
543 int backParam = FAIL;
544 napi_value result = nullptr;
545 OH_AVCodec *videoDec = nullptr;
546 uint32_t index = PARAM_1;
547 OH_AVErrCode checkParam;
548 OH_AVFormat *format = nullptr;
549 format = OH_AVFormat_Create();
550 OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
551 OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
552 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, DEFAULT_PIXELFORMAT);
553 videoDec = OH_VideoDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
554 OH_AVCodecBufferAttr info_;
555 info_.offset = 0;
556 info_.flags = AVCODEC_BUFFER_FLAGS_NONE;
557 checkParam = OH_VideoDecoder_PushInputData(videoDec, index, info_);
558 if (checkParam == AV_ERR_OK) {
559 backParam = SUCCESS;
560 }
561 OH_VideoDecoder_Destroy(videoDec);
562 OH_AVFormat_Destroy(format);
563 napi_create_int32(env, backParam, &result);
564 return result;
565 }
566
OHVideoDecoderPushInputBuffer(napi_env env,napi_callback_info info)567 static napi_value OHVideoDecoderPushInputBuffer(napi_env env, napi_callback_info info)
568 {
569 int backParam = FAIL;
570 napi_value result = nullptr;
571 OH_AVCodec *videoDec = nullptr;
572 uint32_t index = PARAM_1;
573 OH_AVErrCode checkParam;
574 videoDec = OH_VideoDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
575 checkParam = OH_VideoDecoder_PushInputBuffer(videoDec, index);
576 if (checkParam != AV_ERR_OK) {
577 backParam = SUCCESS;
578 }
579 OH_VideoDecoder_Destroy(videoDec);
580 napi_create_int32(env, backParam, &result);
581 return result;
582 }
583
OHVideoDecoderFreeOutputBuffer(napi_env env,napi_callback_info info)584 static napi_value OHVideoDecoderFreeOutputBuffer(napi_env env, napi_callback_info info)
585 {
586 int backParam = FAIL;
587 napi_value result = nullptr;
588 OH_AVCodec *videoDec = nullptr;
589 uint32_t index = PARAM_1;
590 OH_AVErrCode checkParam;
591 videoDec = OH_VideoDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
592 checkParam = OH_VideoDecoder_FreeOutputBuffer(videoDec, index);
593 if (checkParam != AV_ERR_OK) {
594 backParam = SUCCESS;
595 }
596 OH_VideoDecoder_Destroy(videoDec);
597 napi_create_int32(env, backParam, &result);
598 return result;
599 }
600
OHVideoDecoderRenderOutputData(napi_env env,napi_callback_info info)601 static napi_value OHVideoDecoderRenderOutputData(napi_env env, napi_callback_info info)
602 {
603 int backParam = FAIL;
604 napi_value result = nullptr;
605 OH_AVCodec *videoDec = nullptr;
606 OH_AVErrCode checkParam;
607 uint32_t index = PARAM_1;
608 videoDec = OH_VideoDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
609 checkParam = OH_VideoDecoder_RenderOutputData(videoDec, index);
610 if (checkParam == AV_ERR_OK) {
611 backParam = SUCCESS;
612 OH_VideoDecoder_Stop(videoDec);
613 }
614 OH_VideoDecoder_Destroy(videoDec);
615 napi_create_int32(env, backParam, &result);
616 return result;
617 }
618
OHVideoDecoderRenderOutputBuffer(napi_env env,napi_callback_info info)619 static napi_value OHVideoDecoderRenderOutputBuffer(napi_env env, napi_callback_info info)
620 {
621 int backParam = FAIL;
622 napi_value result = nullptr;
623 OH_AVCodec *videoDec = nullptr;
624 OH_AVErrCode checkParam;
625 uint32_t index = PARAM_1;
626 videoDec = OH_VideoDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
627 checkParam = OH_VideoDecoder_RenderOutputBuffer(videoDec, index);
628 if (checkParam != AV_ERR_OK) {
629 backParam = SUCCESS;
630 OH_VideoDecoder_Stop(videoDec);
631 }
632 OH_VideoDecoder_Destroy(videoDec);
633 napi_create_int32(env, backParam, &result);
634 return result;
635 }
636
OHVideoDecoderIsValid(napi_env env,napi_callback_info info)637 static napi_value OHVideoDecoderIsValid(napi_env env, napi_callback_info info)
638 {
639 int backParam = FAIL;
640 napi_value result = nullptr;
641 OH_AVCodec *videoDec = nullptr;
642 OH_AVErrCode checkParam;
643 bool status = true;
644 videoDec = OH_VideoDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
645 checkParam = OH_VideoDecoder_IsValid(videoDec, &status);
646 if (checkParam == AV_ERR_OK) {
647 backParam = SUCCESS;
648 OH_VideoDecoder_Stop(videoDec);
649 }
650 OH_VideoDecoder_Destroy(videoDec);
651 napi_create_int32(env, backParam, &result);
652 return result;
653 }
654
655 EXTERN_C_START
Init(napi_env env,napi_value exports)656 static napi_value Init(napi_env env, napi_value exports)
657 {
658 napi_property_descriptor desc[] = {
659 {"oHVideoDecoderCreateByMime", nullptr, OHVideoDecoderCreateByMime, nullptr, nullptr, nullptr, napi_default,
660 nullptr},
661 {"oHVideoDecoderCreateByName", nullptr, OHVideoDecoderCreateByName, nullptr, nullptr, nullptr, napi_default,
662 nullptr},
663 {"oHVideoDecoderDestroy", nullptr, OHVideoDecoderDestroy, nullptr, nullptr, nullptr, napi_default, nullptr},
664 {"oHVideoDecoderConfigure", nullptr, OHVideoDecoderConfigure, nullptr, nullptr, nullptr, napi_default, nullptr},
665 {"oHVideoDecoderPrepare", nullptr, OHVideoDecoderPrepare, nullptr, nullptr, nullptr, napi_default, nullptr},
666 {"oHVideoDecoderStart", nullptr, OHVideoDecoderStart, nullptr, nullptr, nullptr, napi_default, nullptr},
667 {"oHVideoDecoderStop", nullptr, OHVideoDecoderStop, nullptr, nullptr, nullptr, napi_default, nullptr},
668 {"oHVideoDecoderFlush", nullptr, OHVideoDecoderFlush, nullptr, nullptr, nullptr, napi_default, nullptr},
669 {"oHVideoDecoderReset", nullptr, OHVideoDecoderReset, nullptr, nullptr, nullptr, napi_default, nullptr},
670 {"oHVideoDecoderGetOutputDescription", nullptr, OHVideoDecoderGetOutputDescription, nullptr, nullptr, nullptr,
671 napi_default, nullptr},
672 {"oHVideoDecoderSetParameter", nullptr, OHVideoDecoderSetParameter, nullptr, nullptr, nullptr, napi_default,
673 nullptr},
674 {"oHVideoDecoderPushInputData", nullptr, OHVideoDecoderPushInputData, nullptr, nullptr, nullptr, napi_default,
675 nullptr},
676 {"oHVideoDecoderRenderOutputData", nullptr, OHVideoDecoderRenderOutputData, nullptr, nullptr, nullptr,
677 napi_default, nullptr},
678 {"oHVideoDecoderIsValid", nullptr, OHVideoDecoderIsValid, nullptr, nullptr, nullptr, napi_default, nullptr},
679 {"oHVideoDecoderSetCallback", nullptr, OHVideoDecoderSetCallback, nullptr, nullptr, nullptr, napi_default,
680 nullptr},
681 {"oHVideoDecoderRegisterCallback", nullptr, OHVideoDecoderRegisterCallback, nullptr, nullptr, nullptr, napi_default,
682 nullptr},
683 {"oHVideoDecoderPushInputBuffer", nullptr, OHVideoDecoderPushInputBuffer, nullptr, nullptr, nullptr, napi_default,
684 nullptr},
685 {"oHVideoDecoderFreeOutputBuffer", nullptr, OHVideoDecoderFreeOutputBuffer, nullptr, nullptr, nullptr, napi_default,
686 nullptr},
687 {"oHVideoDecoderRenderOutputBuffer", nullptr, OHVideoDecoderRenderOutputBuffer, nullptr, nullptr, nullptr,
688 napi_default, nullptr},
689 };
690 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
691 return exports;
692 }
693 EXTERN_C_END
694
695 static napi_module demoModule = {
696 .nm_version = 1,
697 .nm_flags = 0,
698 .nm_filename = nullptr,
699 .nm_register_func = Init,
700 .nm_modname = "libvideodecoderndk",
701 .nm_priv = ((void *)0),
702 .reserved = { 0 },
703 };
704
RegisterModule(void)705 extern "C" __attribute__((constructor)) void RegisterModule(void)
706 {
707 napi_module_register(&demoModule);
708 }
709