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 "node_api.h"
18 #include <fcntl.h>
19 #include <multimedia/player_framework/native_avcodec_audiodecoder.h>
20 #include <multimedia/player_framework/native_avcodec_base.h>
21 #include <multimedia/player_framework/native_avmuxer.h>
22 #include <unistd.h>
23 #include "hilog/log.h"
24
25 #define AUDIO_LOG_TAG "AVCODEC_TAGLOG"
26 #define AUDIO_LOG_DOMAIN 0x3200
27
28 #define LOG(fmt, ...) (void)OH_LOG_Print(LOG_APP, LOG_INFO, AUDIO_LOG_DOMAIN, AUDIO_LOG_TAG, fmt, ##__VA_ARGS__)
29 #define MUNUSONE (-1)
30 #define ZEROVAL 0
31 #define ONEONEVAL 11
32 #define SUCCESS 0
33 #define FAIL (-1)
34 #define ONETWOVAL 12
35 #define ONEFIVEVAL 15
36 #define TWOTWOVAL 22
37 #define BIGVAL 44100
38 #define TWOVAL 2
39 #define PARAM_0666 0666
40
OHAVMuxerCreate(napi_env env,napi_callback_info info)41 static napi_value OHAVMuxerCreate(napi_env env, napi_callback_info info)
42 {
43 OH_AVMuxer *muxer = nullptr;
44 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
45 int fileDescribe = open("/data/storage/el2/base/haps/entry_test/files/demo.mp4",
46 O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
47 LOG("OHAVMuxerCreate fd is %{public}d", fileDescribe);
48 muxer = OH_AVMuxer_Create(fileDescribe, format);
49 int returnValue = FAIL;
50 if (muxer != nullptr) {
51 returnValue = SUCCESS;
52 }
53 close(fileDescribe);
54 napi_value result = nullptr;
55 OH_AVMuxer_Destroy(muxer);
56 muxer = nullptr;
57 napi_create_int32(env, returnValue, &result);
58 return result;
59 }
60
OHAVMuxerSetRotation(napi_env env,napi_callback_info info)61 static napi_value OHAVMuxerSetRotation(napi_env env, napi_callback_info info)
62 {
63 OH_AVMuxer *muxer = nullptr;
64 int audioTrackId = MUNUSONE;
65 int32_t rotation = ZEROVAL;
66 int trackId = audioTrackId;
67 OH_AVCodecBufferAttr attrInfo;
68 attrInfo.pts = ONETWOVAL;
69 attrInfo.size = ONEFIVEVAL;
70 attrInfo.offset = ZEROVAL;
71 attrInfo.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME;
72 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
73 int fileDescribe = open("/data/storage/el2/base/haps/entry_test/files/demo.mp4",
74 O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
75 LOG("OHAVMuxerSetRotation fd is %{public}d", fileDescribe);
76 muxer = OH_AVMuxer_Create(fileDescribe, format);
77 OH_AVFormat *trackFormat = OH_AVFormat_Create();
78 OH_AVMemory *sample = OH_AVMemory_Create(ONEONEVAL);
79 OH_AVErrCode backInfo = OH_AVMuxer_SetRotation(muxer, rotation);
80 OH_AVMuxer_AddTrack(muxer, &audioTrackId, trackFormat);
81 OH_AVMuxer_Start(muxer);
82 OH_AVMuxer_WriteSample(muxer, trackId, sample, attrInfo);
83 OH_AVMuxer_Stop(muxer);
84 int returnValue = FAIL;
85 if (backInfo == AV_ERR_OK) {
86 returnValue = SUCCESS;
87 }
88 close(fileDescribe);
89 napi_value result = nullptr;
90 OH_AVMemory_Destroy(sample);
91 OH_AVMuxer_Destroy(muxer);
92 OH_AVFormat_Destroy(trackFormat);
93 muxer = nullptr;
94 napi_create_int32(env, returnValue, &result);
95 return result;
96 }
97
OHAVMuxerSetFormat(napi_env env,napi_callback_info info)98 static napi_value OHAVMuxerSetFormat(napi_env env, napi_callback_info info)
99 {
100 OH_AVMuxer *muxer = nullptr;
101 int audioTrackId = MUNUSONE;
102 int32_t rotation = ZEROVAL;
103 int trackId = audioTrackId;
104 OH_AVCodecBufferAttr attrInfo;
105 attrInfo.pts = ONETWOVAL;
106 attrInfo.size = ONEFIVEVAL;
107 attrInfo.offset = ZEROVAL;
108 attrInfo.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME;
109 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
110 int fileDescribe = open("/data/storage/el2/base/haps/entry_test/files/demo.mp4",
111 O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
112 LOG("OHAVMuxerSetFormat fd is %{public}d", fileDescribe);
113 muxer = OH_AVMuxer_Create(fileDescribe, format);
114 OH_AVFormat *trackFormat = OH_AVFormat_Create();
115 OH_AVFormat *fileFormat = OH_AVFormat_Create();
116 OH_AVMemory *sample = OH_AVMemory_Create(ONEONEVAL);
117
118 OH_AVFormat_SetStringValue(fileFormat, OH_MD_KEY_CREATION_TIME, "2024-12-28T01:23:45.000000Z");
119 OH_AVErrCode backInfo = OH_AVMuxer_SetFormat(muxer, fileFormat);
120 OH_AVMuxer_AddTrack(muxer, &audioTrackId, trackFormat);
121 OH_AVMuxer_Start(muxer);
122 OH_AVMuxer_WriteSample(muxer, trackId, sample, attrInfo);
123 OH_AVMuxer_Stop(muxer);
124 int returnValue = FAIL;
125 if (backInfo == AV_ERR_OK) {
126 returnValue = SUCCESS;
127 }
128 close(fileDescribe);
129 napi_value result = nullptr;
130 OH_AVMemory_Destroy(sample);
131 OH_AVMuxer_Destroy(muxer);
132 OH_AVFormat_Destroy(trackFormat);
133 OH_AVFormat_Destroy(fileFormat);
134 muxer = nullptr;
135 napi_create_int32(env, returnValue, &result);
136 return result;
137 }
138
OHAVMuxerStart(napi_env env,napi_callback_info info)139 static napi_value OHAVMuxerStart(napi_env env, napi_callback_info info)
140 {
141 OH_AVMuxer *muxer = nullptr;
142 int audioTrackId = MUNUSONE;
143 int32_t rotation = ZEROVAL;
144 int trackId = audioTrackId;
145 OH_AVCodecBufferAttr attrInfo;
146 attrInfo.pts = ONETWOVAL;
147 attrInfo.size = ONEFIVEVAL;
148 attrInfo.offset = ZEROVAL;
149 attrInfo.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME;
150 OH_AVMemory *sample = OH_AVMemory_Create(ONEONEVAL);
151 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
152 int fileDescribe = open("/data/storage/el2/base/haps/entry_test/files/demo.mp4",
153 O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
154 LOG("OHAVMuxerStart fd is %{public}d", fileDescribe);
155 muxer = OH_AVMuxer_Create(fileDescribe, format);
156 OH_AVFormat *trackFormat = OH_AVFormat_Create();
157 OH_AVMuxer_SetRotation(muxer, rotation);
158 OH_AVFormat_SetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AAC);
159 OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_SAMPLE_RATE, BIGVAL);
160 OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_CHANNEL_COUNT, TWOVAL);
161 OH_AVMuxer_AddTrack(muxer, &audioTrackId, trackFormat);
162 OH_AVErrCode backInfo = OH_AVMuxer_Start(muxer);
163 OH_AVMuxer_WriteSample(muxer, trackId, sample, attrInfo);
164 OH_AVMuxer_Stop(muxer);
165 int returnValue = FAIL;
166 if (backInfo == AV_ERR_OK) {
167 returnValue = SUCCESS;
168 }
169 close(fileDescribe);
170 napi_value result = nullptr;
171 OH_AVMemory_Destroy(sample);
172 OH_AVMuxer_Destroy(muxer);
173 OH_AVFormat_Destroy(trackFormat);
174 muxer = nullptr;
175 napi_create_int32(env, returnValue, &result);
176 return result;
177 }
178
OHAVMuxerStop(napi_env env,napi_callback_info info)179 static napi_value OHAVMuxerStop(napi_env env, napi_callback_info info)
180 {
181 OH_AVMuxer *muxer = nullptr;
182 int audioTrackId = MUNUSONE;
183 int32_t rotation = ZEROVAL;
184 int trackId = audioTrackId;
185 OH_AVCodecBufferAttr attrInfo;
186 attrInfo.pts = ONETWOVAL;
187 attrInfo.size = ONEFIVEVAL;
188 attrInfo.offset = ZEROVAL;
189 attrInfo.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME;
190 OH_AVMemory *sample = OH_AVMemory_Create(ONEONEVAL);
191 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
192 int fileDescribe = open("/data/storage/el2/base/haps/entry_test/files/demo.mp4",
193 O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
194 LOG("OHAVMuxerStop fd is %{public}d", fileDescribe);
195 muxer = OH_AVMuxer_Create(fileDescribe, format);
196 OH_AVFormat *trackFormat = OH_AVFormat_Create();
197 OH_AVMuxer_SetRotation(muxer, rotation);
198 OH_AVFormat_SetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AAC);
199 OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_SAMPLE_RATE, BIGVAL);
200 OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_CHANNEL_COUNT, TWOVAL);
201 OH_AVMuxer_AddTrack(muxer, &audioTrackId, trackFormat);
202 OH_AVMuxer_Start(muxer);
203 OH_AVMuxer_WriteSample(muxer, trackId, sample, attrInfo);
204 OH_AVErrCode backInfo = OH_AVMuxer_Stop(muxer);
205 int returnValue = FAIL;
206 if (backInfo == AV_ERR_OK) {
207 returnValue = SUCCESS;
208 }
209 close(fileDescribe);
210 napi_value result = nullptr;
211 OH_AVMemory_Destroy(sample);
212 OH_AVMuxer_Destroy(muxer);
213 OH_AVFormat_Destroy(trackFormat);
214 muxer = nullptr;
215 napi_create_int32(env, returnValue, &result);
216 return result;
217 }
218
OHAVMuxerWriteSample(napi_env env,napi_callback_info info)219 static napi_value OHAVMuxerWriteSample(napi_env env, napi_callback_info info)
220 {
221 OH_AVMuxer *muxer = nullptr;
222 int audioTrackId = MUNUSONE;
223 int32_t rotation = ZEROVAL;
224 OH_AVCodecBufferAttr attrInfo;
225 attrInfo.pts = ONEONEVAL;
226 attrInfo.size = ONEONEVAL;
227 attrInfo.offset = ZEROVAL;
228 attrInfo.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME;
229 int trackId = audioTrackId;
230 OH_AVMemory *sample = OH_AVMemory_Create(TWOTWOVAL);
231 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_DEFAULT;
232 int fileDescribe = open("/data/storage/el2/base/haps/entry_test/files/demo.mp4",
233 O_RDWR | O_TRUNC, PARAM_0666);
234 LOG("OHAVMuxerWriteSample fd is %{public}d", fileDescribe);
235 muxer = OH_AVMuxer_Create(fileDescribe, format);
236 OH_AVFormat *trackFormat = OH_AVFormat_Create();
237 OH_AVMuxer_SetRotation(muxer, rotation);
238 OH_AVFormat_SetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AAC);
239 OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_SAMPLE_RATE, BIGVAL);
240 OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_CHANNEL_COUNT, TWOVAL);
241 OH_AVMuxer_AddTrack(muxer, &trackId, trackFormat);
242 OH_AVMuxer_Start(muxer);
243 int backInfo = OH_AVMuxer_WriteSample(muxer, trackId, sample, attrInfo);
244 int returnValue = FAIL;
245 OH_AVMuxer_Stop(muxer);
246 if (backInfo == AV_ERR_OK) {
247 returnValue = SUCCESS;
248 }
249 close(fileDescribe);
250 napi_value result = nullptr;
251 OH_AVMemory_Destroy(sample);
252 OH_AVMuxer_Destroy(muxer);
253 OH_AVFormat_Destroy(trackFormat);
254 muxer = nullptr;
255 napi_create_int32(env, returnValue, &result);
256 return result;
257 }
258
OHAVMuxerWriteSampleBuffer(napi_env env,napi_callback_info info)259 static napi_value OHAVMuxerWriteSampleBuffer(napi_env env, napi_callback_info info) {
260 OH_AVMuxer *muxer = nullptr;
261 int audioTrackId = MUNUSONE;
262 int32_t rotation = ZEROVAL;
263 OH_AVCodecBufferAttr attrInfo;
264 attrInfo.pts = ONEONEVAL;
265 attrInfo.size = ONEONEVAL;
266 attrInfo.offset = ZEROVAL;
267 attrInfo.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME;
268 int trackId = audioTrackId;
269 OH_AVBuffer *sample = OH_AVBuffer_Create(TWOTWOVAL);
270 OH_AVBuffer_SetBufferAttr(sample, &attrInfo);
271 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_DEFAULT;
272 int fileDescribe = open("/data/storage/el2/base/haps/entry_test/files/demo.mp4",
273 O_RDWR | O_TRUNC, PARAM_0666);
274 LOG("OHAVMuxerWriteSampleBuffer fd is %{public}d", fileDescribe);
275 muxer = OH_AVMuxer_Create(fileDescribe, format);
276 OH_AVFormat *trackFormat = OH_AVFormat_Create();
277 OH_AVMuxer_SetRotation(muxer, rotation);
278 OH_AVFormat_SetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AAC);
279 OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_SAMPLE_RATE, BIGVAL);
280 OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_CHANNEL_COUNT, TWOVAL);
281 OH_AVMuxer_AddTrack(muxer, &trackId, trackFormat);
282 OH_AVMuxer_Start(muxer);
283 int backInfo = OH_AVMuxer_WriteSampleBuffer(muxer, trackId, sample);
284 int returnValue = FAIL;
285 OH_AVMuxer_Stop(muxer);
286 if (backInfo == AV_ERR_OK) {
287 returnValue = SUCCESS;
288 }
289 close(fileDescribe);
290 napi_value result = nullptr;
291 OH_AVBuffer_Destroy(sample);
292 OH_AVMuxer_Destroy(muxer);
293 OH_AVFormat_Destroy(trackFormat);
294 muxer = nullptr;
295 napi_create_int32(env, returnValue, &result);
296 return result;
297 }
298
OHAVMuxerAddTrack(napi_env env,napi_callback_info info)299 static napi_value OHAVMuxerAddTrack(napi_env env, napi_callback_info info)
300 {
301 OH_AVMuxer *muxer = nullptr;
302 int audioTrackId = MUNUSONE;
303 int32_t rotation = ZEROVAL;
304 int trackId = audioTrackId;
305 OH_AVCodecBufferAttr attrInfo;
306 attrInfo.pts = ONETWOVAL;
307 attrInfo.size = ONEFIVEVAL;
308 attrInfo.offset = ZEROVAL;
309 attrInfo.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME;
310 OH_AVMemory *sample = OH_AVMemory_Create(ONEONEVAL);
311 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
312 int fileDescribe = open("/data/storage/el2/base/haps/entry_test/files/demo.mp4",
313 O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
314 LOG("OHAVMuxerAddTrack fd is %{public}d", fileDescribe);
315 muxer = OH_AVMuxer_Create(fileDescribe, format);
316 OH_AVFormat *trackFormat = OH_AVFormat_Create();
317 OH_AVMuxer_SetRotation(muxer, rotation);
318 OH_AVFormat_SetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AAC);
319 OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_SAMPLE_RATE, BIGVAL);
320 OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_CHANNEL_COUNT, TWOVAL);
321 int ret = OH_AVMuxer_AddTrack(muxer, &audioTrackId, trackFormat);
322 OH_AVMuxer_Start(muxer);
323 OH_AVMuxer_WriteSample(muxer, trackId, sample, attrInfo);
324 OH_AVMuxer_Stop(muxer);
325 int returnValue = FAIL;
326 if (ret == AV_ERR_OK) {
327 returnValue = SUCCESS;
328 }
329 close(fileDescribe);
330 napi_value result = nullptr;
331 OH_AVMemory_Destroy(sample);
332 OH_AVMuxer_Destroy(muxer);
333 OH_AVFormat_Destroy(trackFormat);
334 muxer = nullptr;
335 napi_create_int32(env, returnValue, &result);
336 return result;
337 }
338
OHAVMuxerDestroy(napi_env env,napi_callback_info info)339 static napi_value OHAVMuxerDestroy(napi_env env, napi_callback_info info)
340 {
341 OH_AVMuxer *muxer = nullptr;
342 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
343 int fileDescribe = open("/data/storage/el2/base/haps/entry_test/files/demo.mp4",
344 O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
345 LOG("OHAVMuxerDestroy fd is %{public}d", fileDescribe);
346 muxer = OH_AVMuxer_Create(fileDescribe, format);
347 int returnValue = FAIL;
348 napi_value result = nullptr;
349 OH_AVErrCode desval = OH_AVMuxer_Destroy(muxer);
350 if (desval == AV_ERR_OK) {
351 returnValue = SUCCESS;
352 }
353 close(fileDescribe);
354 muxer = nullptr;
355 napi_create_int32(env, returnValue, &result);
356 return result;
357 }
358
OHAVMuxerCreateAbnormal(napi_env env,napi_callback_info info)359 static napi_value OHAVMuxerCreateAbnormal(napi_env env, napi_callback_info info)
360 {
361 OH_AVMuxer *muxer = nullptr;
362 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
363 int fileDescribe = MUNUSONE;
364 muxer = OH_AVMuxer_Create(fileDescribe, format);
365 int returnValue = FAIL;
366 if (muxer == nullptr) {
367 returnValue = SUCCESS;
368 }
369 napi_value result = nullptr;
370 napi_create_int32(env, returnValue, &result);
371 return result;
372 }
373
OHAVMuxerSetRotationAbnormal(napi_env env,napi_callback_info info)374 static napi_value OHAVMuxerSetRotationAbnormal(napi_env env, napi_callback_info info)
375 {
376 OH_AVMuxer *muxer = nullptr;
377 int audioTrackId = MUNUSONE;
378 int32_t rotation = ZEROVAL;
379 int trackId = audioTrackId;
380 OH_AVCodecBufferAttr attrInfo;
381 attrInfo.pts = ONETWOVAL;
382 attrInfo.size = ONEFIVEVAL;
383 attrInfo.offset = ZEROVAL;
384 attrInfo.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME;
385 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
386 int fileDescribe = open("/data/storage/el2/base/haps/entry_test/files/demo.mp4",
387 O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
388 LOG("OHAVMuxerSetRotationAbnormal fd is %{public}d", fileDescribe);
389 muxer = OH_AVMuxer_Create(fileDescribe, format);
390 OH_AVFormat *trackFormat = OH_AVFormat_Create();
391 OH_AVMemory *sample = OH_AVMemory_Create(ONEONEVAL);
392 OH_AVErrCode backInfo = OH_AVMuxer_SetRotation(nullptr, rotation);
393 OH_AVMuxer_AddTrack(muxer, &audioTrackId, trackFormat);
394 OH_AVMuxer_Start(muxer);
395 OH_AVMuxer_WriteSample(muxer, trackId, sample, attrInfo);
396 OH_AVMuxer_Stop(muxer);
397 int returnValue = FAIL;
398 if (backInfo == AV_ERR_INVALID_VAL) {
399 returnValue = SUCCESS;
400 }
401 close(fileDescribe);
402 napi_value result = nullptr;
403 OH_AVMemory_Destroy(sample);
404 OH_AVMuxer_Destroy(muxer);
405 OH_AVFormat_Destroy(trackFormat);
406 muxer = nullptr;
407 napi_create_int32(env, returnValue, &result);
408 return result;
409 }
410
OHAVMuxerSetFormatAbnormal(napi_env env,napi_callback_info info)411 static napi_value OHAVMuxerSetFormatAbnormal(napi_env env, napi_callback_info info)
412 {
413 OH_AVMuxer *muxer = nullptr;
414 int audioTrackId = MUNUSONE;
415 int32_t rotation = ZEROVAL;
416 int trackId = audioTrackId;
417 OH_AVCodecBufferAttr attrInfo;
418 attrInfo.pts = ONETWOVAL;
419 attrInfo.size = ONEFIVEVAL;
420 attrInfo.offset = ZEROVAL;
421 attrInfo.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME;
422 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
423 int fileDescribe = open("/data/storage/el2/base/haps/entry_test/files/demo.mp4",
424 O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
425 LOG("OHAVMuxerSetFormatAbnormal fd is %{public}d", fileDescribe);
426 muxer = OH_AVMuxer_Create(fileDescribe, format);
427 OH_AVFormat *trackFormat = OH_AVFormat_Create();
428 OH_AVFormat *fileFormat = OH_AVFormat_Create();
429 OH_AVMemory *sample = OH_AVMemory_Create(ONEONEVAL);
430
431 OH_AVFormat_SetStringValue(fileFormat, OH_MD_KEY_CREATION_TIME, "This is abnormal case");
432 OH_AVErrCode backInfo = OH_AVMuxer_SetFormat(muxer, fileFormat);
433 OH_AVMuxer_AddTrack(muxer, &audioTrackId, trackFormat);
434 OH_AVMuxer_Start(muxer);
435 OH_AVMuxer_WriteSample(muxer, trackId, sample, attrInfo);
436 OH_AVMuxer_Stop(muxer);
437 int returnValue = FAIL;
438 if (backInfo == AV_ERR_INVALID_VAL) { // Expected Returned AV_ERR_INVALID_VAL
439 returnValue = SUCCESS;
440 }
441 close(fileDescribe);
442 napi_value result = nullptr;
443 OH_AVMemory_Destroy(sample);
444 OH_AVMuxer_Destroy(muxer);
445 OH_AVFormat_Destroy(trackFormat);
446 OH_AVFormat_Destroy(fileFormat);
447 muxer = nullptr;
448 napi_create_int32(env, returnValue, &result);
449 return result;
450 }
451
OHAVMuxerStartAbnormal(napi_env env,napi_callback_info info)452 static napi_value OHAVMuxerStartAbnormal(napi_env env, napi_callback_info info)
453 {
454 OH_AVMuxer *muxer = nullptr;
455 int audioTrackId = MUNUSONE;
456 int32_t rotation = ZEROVAL;
457 int trackId = audioTrackId;
458 OH_AVCodecBufferAttr attrInfo;
459 attrInfo.pts = ONETWOVAL;
460 attrInfo.size = ONEFIVEVAL;
461 attrInfo.offset = ZEROVAL;
462 attrInfo.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME;
463 OH_AVMemory *sample = OH_AVMemory_Create(ONEONEVAL);
464 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
465 int fileDescribe = open("/data/storage/el2/base/haps/entry_test/files/demo.mp4",
466 O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
467 LOG("OHAVMuxerStartAbnormal fd is %{public}d", fileDescribe);
468 muxer = OH_AVMuxer_Create(fileDescribe, format);
469 OH_AVFormat *trackFormat = OH_AVFormat_Create();
470 OH_AVMuxer_SetRotation(muxer, rotation);
471 OH_AVMuxer_AddTrack(muxer, &audioTrackId, trackFormat);
472 OH_AVErrCode backInfo = OH_AVMuxer_Start(nullptr);
473 OH_AVMuxer_WriteSample(muxer, trackId, sample, attrInfo);
474 OH_AVMuxer_Stop(muxer);
475 int returnValue = FAIL;
476 if (backInfo == AV_ERR_INVALID_VAL) {
477 returnValue = SUCCESS;
478 }
479 close(fileDescribe);
480 napi_value result = nullptr;
481 OH_AVMemory_Destroy(sample);
482 OH_AVMuxer_Destroy(muxer);
483 OH_AVFormat_Destroy(trackFormat);
484 muxer = nullptr;
485 napi_create_int32(env, returnValue, &result);
486 return result;
487 }
488
OHAVMuxerStopAbnormal(napi_env env,napi_callback_info info)489 static napi_value OHAVMuxerStopAbnormal(napi_env env, napi_callback_info info)
490 {
491 OH_AVMuxer *muxer = nullptr;
492 int audioTrackId = MUNUSONE;
493 int32_t rotation = ZEROVAL;
494 int trackId = audioTrackId;
495 OH_AVCodecBufferAttr attrInfo;
496 attrInfo.pts = ONETWOVAL;
497 attrInfo.size = ONEFIVEVAL;
498 attrInfo.offset = ZEROVAL;
499 attrInfo.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME;
500 OH_AVMemory *sample = OH_AVMemory_Create(ONEONEVAL);
501 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
502 int fileDescribe = open("/data/storage/el2/base/haps/entry_test/files/demo.mp4",
503 O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
504 LOG("OHAVMuxerStopAbnormal fd is %{public}d", fileDescribe);
505 muxer = OH_AVMuxer_Create(fileDescribe, format);
506 OH_AVFormat *trackFormat = OH_AVFormat_Create();
507 OH_AVMuxer_SetRotation(muxer, rotation);
508 OH_AVMuxer_AddTrack(muxer, &audioTrackId, trackFormat);
509 OH_AVMuxer_Start(muxer);
510 OH_AVMuxer_WriteSample(muxer, trackId, sample, attrInfo);
511 OH_AVErrCode backInfo = OH_AVMuxer_Stop(nullptr);
512 int returnValue = FAIL;
513 if (backInfo == AV_ERR_INVALID_VAL) {
514 returnValue = SUCCESS;
515 }
516 close(fileDescribe);
517 napi_value result = nullptr;
518 OH_AVMemory_Destroy(sample);
519 OH_AVMuxer_Destroy(muxer);
520 OH_AVFormat_Destroy(trackFormat);
521 muxer = nullptr;
522 napi_create_int32(env, returnValue, &result);
523 return result;
524 }
525
OHAVMuxerWriteSampleAbnormal(napi_env env,napi_callback_info info)526 static napi_value OHAVMuxerWriteSampleAbnormal(napi_env env, napi_callback_info info)
527 {
528 OH_AVMuxer *muxer = nullptr;
529 int audioTrackId = MUNUSONE;
530 int32_t rotation = ZEROVAL;
531 int trackId = audioTrackId;
532 OH_AVCodecBufferAttr attrInfo;
533 attrInfo.pts = ONETWOVAL;
534 attrInfo.size = ONEFIVEVAL;
535 attrInfo.offset = ZEROVAL;
536 attrInfo.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME;
537 OH_AVMemory *sample = OH_AVMemory_Create(ONEONEVAL);
538 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
539 int fileDescribe = open("/data/storage/el2/base/haps/entry_test/files/demo.mp4",
540 O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
541 LOG("OHAVMuxerWriteSampleAbnormal fd is %{public}d", fileDescribe);
542 muxer = OH_AVMuxer_Create(fileDescribe, format);
543 OH_AVFormat *trackFormat = OH_AVFormat_Create();
544 OH_AVMuxer_SetRotation(muxer, rotation);
545 OH_AVMuxer_AddTrack(muxer, &audioTrackId, trackFormat);
546 OH_AVMuxer_Start(muxer);
547 OH_AVErrCode backInfo = OH_AVMuxer_WriteSample(nullptr, trackId, sample, attrInfo);
548 OH_AVMuxer_Stop(muxer);
549 int returnValue = FAIL;
550 if (backInfo == AV_ERR_INVALID_VAL) {
551 returnValue = SUCCESS;
552 }
553 close(fileDescribe);
554 napi_value result = nullptr;
555 OH_AVMemory_Destroy(sample);
556 OH_AVMuxer_Destroy(muxer);
557 OH_AVFormat_Destroy(trackFormat);
558 muxer = nullptr;
559 napi_create_int32(env, returnValue, &result);
560 return result;
561 }
562
OHAVMuxerAddTrackAbnormal(napi_env env,napi_callback_info info)563 static napi_value OHAVMuxerAddTrackAbnormal(napi_env env, napi_callback_info info)
564 {
565 OH_AVMuxer *muxer = nullptr;
566 int audioTrackId = MUNUSONE;
567 int32_t rotation = ZEROVAL;
568 int trackId = audioTrackId;
569 OH_AVCodecBufferAttr attrInfo;
570 attrInfo.pts = ONETWOVAL;
571 attrInfo.size = ONEFIVEVAL;
572 attrInfo.offset = ZEROVAL;
573 attrInfo.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME;
574 OH_AVMemory *sample = OH_AVMemory_Create(ONEONEVAL);
575 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
576 int fileDescribe = open("/data/storage/el2/base/haps/entry_test/files/demo.mp4",
577 O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
578 muxer = OH_AVMuxer_Create(fileDescribe, format);
579 OH_AVFormat *trackFormat = OH_AVFormat_Create();
580 OH_AVMuxer_SetRotation(muxer, rotation);
581 int ret = OH_AVMuxer_AddTrack(nullptr, &audioTrackId, trackFormat);
582 OH_AVMuxer_Start(muxer);
583 OH_AVMuxer_WriteSample(muxer, trackId, sample, attrInfo);
584 OH_AVMuxer_Stop(muxer);
585 int returnValue = FAIL;
586 if (ret == AV_ERR_INVALID_VAL) {
587 returnValue = SUCCESS;
588 }
589 close(fileDescribe);
590 napi_value result = nullptr;
591 OH_AVMemory_Destroy(sample);
592 OH_AVMuxer_Destroy(muxer);
593 OH_AVFormat_Destroy(trackFormat);
594 muxer = nullptr;
595 napi_create_int32(env, returnValue, &result);
596 return result;
597 }
598
OHAVMuxerDestroyAbnormal(napi_env env,napi_callback_info info)599 static napi_value OHAVMuxerDestroyAbnormal(napi_env env, napi_callback_info info)
600 {
601 int returnValue = FAIL;
602 napi_value result = nullptr;
603 OH_AVErrCode desval = OH_AVMuxer_Destroy(nullptr);
604 if (desval == AV_ERR_INVALID_VAL) {
605 returnValue = SUCCESS;
606 }
607 napi_create_int32(env, returnValue, &result);
608 return result;
609 }
610 EXTERN_C_START
Init(napi_env env,napi_value exports)611 static napi_value Init(napi_env env, napi_value exports)
612 {
613 napi_property_descriptor desc[] = {
614 {"oHAVMuxerCreate", nullptr, OHAVMuxerCreate, nullptr, nullptr, nullptr, napi_default, nullptr},
615 {"oHAVMuxerSetRotation", nullptr, OHAVMuxerSetRotation, nullptr, nullptr, nullptr, napi_default, nullptr},
616 {"oHAVMuxerSetFormat", nullptr, OHAVMuxerSetFormat, nullptr, nullptr, nullptr, napi_default, nullptr},
617 {"oHAVMuxerStart", nullptr, OHAVMuxerStart, nullptr, nullptr, nullptr, napi_default, nullptr},
618 {"oHAVMuxerStop", nullptr, OHAVMuxerStop, nullptr, nullptr, nullptr, napi_default, nullptr},
619 {"oHAVMuxerWriteSample", nullptr, OHAVMuxerWriteSample, nullptr, nullptr, nullptr, napi_default, nullptr},
620 {"OHAVMuxerWriteSampleBuffer", nullptr, OHAVMuxerWriteSampleBuffer, nullptr, nullptr, nullptr, napi_default, nullptr},
621 {"oHAVMuxerAddTrack", nullptr, OHAVMuxerAddTrack, nullptr, nullptr, nullptr, napi_default, nullptr},
622 {"oHAVMuxerDestroy", nullptr, OHAVMuxerDestroy, nullptr, nullptr, nullptr, napi_default, nullptr},
623 {"oHAVMuxerCreateAbnormal", nullptr, OHAVMuxerCreateAbnormal, nullptr, nullptr, nullptr, napi_default, nullptr},
624 {"oHAVMuxerSetRotationAbnormal", nullptr, OHAVMuxerSetRotationAbnormal, nullptr, nullptr, nullptr, napi_default,
625 nullptr},
626 {"oHAVMuxerSetFormatAbnormal", nullptr, OHAVMuxerSetFormatAbnormal, nullptr, nullptr, nullptr, napi_default,
627 nullptr},
628 {"oHAVMuxerStartAbnormal", nullptr, OHAVMuxerStartAbnormal, nullptr, nullptr, nullptr, napi_default, nullptr},
629 {"oHAVMuxerStopAbnormal", nullptr, OHAVMuxerStopAbnormal, nullptr, nullptr, nullptr, napi_default, nullptr},
630 {"oHAVMuxerWriteSampleAbnormal", nullptr, OHAVMuxerWriteSampleAbnormal, nullptr, nullptr, nullptr, napi_default,
631 nullptr},
632 {"oHAVMuxerAddTrackAbnormal", nullptr, OHAVMuxerAddTrackAbnormal, nullptr, nullptr, nullptr, napi_default,
633 nullptr},
634 {"oHAVMuxerDestroyAbnormal", nullptr, OHAVMuxerDestroyAbnormal, nullptr, nullptr, nullptr, napi_default,
635 nullptr},
636
637 };
638 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
639 return exports;
640 }
641
642 EXTERN_C_END
643
644 static napi_module demoModule = {
645 .nm_version = 1,
646 .nm_flags = 0,
647 .nm_filename = nullptr,
648 .nm_register_func = Init,
649 .nm_modname = "nativeavmuxer",
650 .nm_priv = ((void *)0),
651 .reserved = {0},
652 };
653
RegisterEntryModule(void)654 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }
655