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_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_ENABLE_MOOV_FRONT, 1);
162 OH_AVMuxer_AddTrack(muxer, &audioTrackId, trackFormat);
163 OH_AVErrCode backInfo = OH_AVMuxer_Start(muxer);
164 OH_AVMuxer_WriteSample(muxer, trackId, sample, attrInfo);
165 OH_AVMuxer_Stop(muxer);
166 int returnValue = FAIL;
167 if (backInfo == AV_ERR_OK) {
168 returnValue = SUCCESS;
169 }
170 close(fileDescribe);
171 napi_value result = nullptr;
172 OH_AVMemory_Destroy(sample);
173 OH_AVMuxer_Destroy(muxer);
174 OH_AVFormat_Destroy(trackFormat);
175 muxer = nullptr;
176 napi_create_int32(env, returnValue, &result);
177 return result;
178 }
179
OHAVMuxerStop(napi_env env,napi_callback_info info)180 static napi_value OHAVMuxerStop(napi_env env, napi_callback_info info)
181 {
182 OH_AVMuxer *muxer = nullptr;
183 int audioTrackId = MUNUSONE;
184 int32_t rotation = ZEROVAL;
185 int trackId = audioTrackId;
186 OH_AVCodecBufferAttr attrInfo;
187 attrInfo.pts = ONETWOVAL;
188 attrInfo.size = ONEFIVEVAL;
189 attrInfo.offset = ZEROVAL;
190 attrInfo.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME;
191 OH_AVMemory *sample = OH_AVMemory_Create(ONEONEVAL);
192 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
193 int fileDescribe = open("/data/storage/el2/base/haps/entry_test/files/demo.mp4",
194 O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
195 LOG("OHAVMuxerStop fd is %{public}d", fileDescribe);
196 muxer = OH_AVMuxer_Create(fileDescribe, format);
197 OH_AVFormat *trackFormat = OH_AVFormat_Create();
198 OH_AVMuxer_SetRotation(muxer, rotation);
199 OH_AVFormat_SetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AAC);
200 OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_SAMPLE_RATE, BIGVAL);
201 OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_CHANNEL_COUNT, TWOVAL);
202 OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_ENABLE_MOOV_FRONT, 1);
203 OH_AVMuxer_AddTrack(muxer, &audioTrackId, trackFormat);
204 OH_AVMuxer_Start(muxer);
205 OH_AVMuxer_WriteSample(muxer, trackId, sample, attrInfo);
206 OH_AVErrCode backInfo = OH_AVMuxer_Stop(muxer);
207 int returnValue = FAIL;
208 if (backInfo == AV_ERR_OK) {
209 returnValue = SUCCESS;
210 }
211 close(fileDescribe);
212 napi_value result = nullptr;
213 OH_AVMemory_Destroy(sample);
214 OH_AVMuxer_Destroy(muxer);
215 OH_AVFormat_Destroy(trackFormat);
216 muxer = nullptr;
217 napi_create_int32(env, returnValue, &result);
218 return result;
219 }
220
OHAVMuxerWriteSample(napi_env env,napi_callback_info info)221 static napi_value OHAVMuxerWriteSample(napi_env env, napi_callback_info info)
222 {
223 OH_AVMuxer *muxer = nullptr;
224 int audioTrackId = MUNUSONE;
225 int32_t rotation = ZEROVAL;
226 OH_AVCodecBufferAttr attrInfo;
227 attrInfo.pts = ONEONEVAL;
228 attrInfo.size = ONEONEVAL;
229 attrInfo.offset = ZEROVAL;
230 attrInfo.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME;
231 int trackId = audioTrackId;
232 OH_AVMemory *sample = OH_AVMemory_Create(TWOTWOVAL);
233 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_DEFAULT;
234 int fileDescribe = open("/data/storage/el2/base/haps/entry_test/files/demo.mp4",
235 O_RDWR | O_TRUNC, PARAM_0666);
236 LOG("OHAVMuxerWriteSample fd is %{public}d", fileDescribe);
237 muxer = OH_AVMuxer_Create(fileDescribe, format);
238 OH_AVFormat *trackFormat = OH_AVFormat_Create();
239 OH_AVMuxer_SetRotation(muxer, rotation);
240 OH_AVFormat_SetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AAC);
241 OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_SAMPLE_RATE, BIGVAL);
242 OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_CHANNEL_COUNT, TWOVAL);
243 OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_ENABLE_MOOV_FRONT, 1);
244 OH_AVMuxer_AddTrack(muxer, &trackId, trackFormat);
245 OH_AVMuxer_Start(muxer);
246 int backInfo = OH_AVMuxer_WriteSample(muxer, trackId, sample, attrInfo);
247 int returnValue = FAIL;
248 OH_AVMuxer_Stop(muxer);
249 if (backInfo == AV_ERR_OK) {
250 returnValue = SUCCESS;
251 }
252 close(fileDescribe);
253 napi_value result = nullptr;
254 OH_AVMemory_Destroy(sample);
255 OH_AVMuxer_Destroy(muxer);
256 OH_AVFormat_Destroy(trackFormat);
257 muxer = nullptr;
258 napi_create_int32(env, returnValue, &result);
259 return result;
260 }
261
OHAVMuxerWriteSampleBuffer(napi_env env,napi_callback_info info)262 static napi_value OHAVMuxerWriteSampleBuffer(napi_env env, napi_callback_info info) {
263 OH_AVMuxer *muxer = nullptr;
264 int audioTrackId = MUNUSONE;
265 int32_t rotation = ZEROVAL;
266 OH_AVCodecBufferAttr attrInfo;
267 attrInfo.pts = ONEONEVAL;
268 attrInfo.size = ONEONEVAL;
269 attrInfo.offset = ZEROVAL;
270 attrInfo.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME;
271 int trackId = audioTrackId;
272 OH_AVBuffer *sample = OH_AVBuffer_Create(TWOTWOVAL);
273 OH_AVBuffer_SetBufferAttr(sample, &attrInfo);
274 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_DEFAULT;
275 int fileDescribe = open("/data/storage/el2/base/haps/entry_test/files/demo.mp4",
276 O_RDWR | O_TRUNC, PARAM_0666);
277 LOG("OHAVMuxerWriteSampleBuffer fd is %{public}d", fileDescribe);
278 muxer = OH_AVMuxer_Create(fileDescribe, format);
279 OH_AVFormat *trackFormat = OH_AVFormat_Create();
280 OH_AVMuxer_SetRotation(muxer, rotation);
281 OH_AVFormat_SetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AAC);
282 OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_SAMPLE_RATE, BIGVAL);
283 OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_CHANNEL_COUNT, TWOVAL);
284 OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_ENABLE_MOOV_FRONT, 0);
285 OH_AVMuxer_AddTrack(muxer, &trackId, trackFormat);
286 OH_AVMuxer_Start(muxer);
287 int backInfo = OH_AVMuxer_WriteSampleBuffer(muxer, trackId, sample);
288 int returnValue = FAIL;
289 OH_AVMuxer_Stop(muxer);
290 if (backInfo == AV_ERR_OK) {
291 returnValue = SUCCESS;
292 }
293 close(fileDescribe);
294 napi_value result = nullptr;
295 OH_AVBuffer_Destroy(sample);
296 OH_AVMuxer_Destroy(muxer);
297 OH_AVFormat_Destroy(trackFormat);
298 muxer = nullptr;
299 napi_create_int32(env, returnValue, &result);
300 return result;
301 }
302
OHAVMuxerAddTrack(napi_env env,napi_callback_info info)303 static napi_value OHAVMuxerAddTrack(napi_env env, napi_callback_info info)
304 {
305 OH_AVMuxer *muxer = nullptr;
306 int audioTrackId = MUNUSONE;
307 int32_t rotation = ZEROVAL;
308 int trackId = audioTrackId;
309 OH_AVCodecBufferAttr attrInfo;
310 attrInfo.pts = ONETWOVAL;
311 attrInfo.size = ONEFIVEVAL;
312 attrInfo.offset = ZEROVAL;
313 attrInfo.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME;
314 OH_AVMemory *sample = OH_AVMemory_Create(ONEONEVAL);
315 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
316 int fileDescribe = open("/data/storage/el2/base/haps/entry_test/files/demo.mp4",
317 O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
318 LOG("OHAVMuxerAddTrack fd is %{public}d", fileDescribe);
319 muxer = OH_AVMuxer_Create(fileDescribe, format);
320 OH_AVFormat *trackFormat = OH_AVFormat_Create();
321 OH_AVMuxer_SetRotation(muxer, rotation);
322 OH_AVFormat_SetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AAC);
323 OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_SAMPLE_RATE, BIGVAL);
324 OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_CHANNEL_COUNT, TWOVAL);
325 OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_ENABLE_MOOV_FRONT, 1);
326 int ret = OH_AVMuxer_AddTrack(muxer, &audioTrackId, trackFormat);
327 OH_AVMuxer_Start(muxer);
328 OH_AVMuxer_WriteSample(muxer, trackId, sample, attrInfo);
329 OH_AVMuxer_Stop(muxer);
330 int returnValue = FAIL;
331 if (ret == AV_ERR_OK) {
332 returnValue = SUCCESS;
333 }
334 close(fileDescribe);
335 napi_value result = nullptr;
336 OH_AVMemory_Destroy(sample);
337 OH_AVMuxer_Destroy(muxer);
338 OH_AVFormat_Destroy(trackFormat);
339 muxer = nullptr;
340 napi_create_int32(env, returnValue, &result);
341 return result;
342 }
343
OHAVMuxerDestroy(napi_env env,napi_callback_info info)344 static napi_value OHAVMuxerDestroy(napi_env env, napi_callback_info info)
345 {
346 OH_AVMuxer *muxer = nullptr;
347 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
348 int fileDescribe = open("/data/storage/el2/base/haps/entry_test/files/demo.mp4",
349 O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
350 LOG("OHAVMuxerDestroy fd is %{public}d", fileDescribe);
351 muxer = OH_AVMuxer_Create(fileDescribe, format);
352 int returnValue = FAIL;
353 napi_value result = nullptr;
354 OH_AVErrCode desval = OH_AVMuxer_Destroy(muxer);
355 if (desval == AV_ERR_OK) {
356 returnValue = SUCCESS;
357 }
358 close(fileDescribe);
359 muxer = nullptr;
360 napi_create_int32(env, returnValue, &result);
361 return result;
362 }
363
OHAVMuxerCreateAbnormal(napi_env env,napi_callback_info info)364 static napi_value OHAVMuxerCreateAbnormal(napi_env env, napi_callback_info info)
365 {
366 OH_AVMuxer *muxer = nullptr;
367 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
368 int fileDescribe = MUNUSONE;
369 muxer = OH_AVMuxer_Create(fileDescribe, format);
370 int returnValue = FAIL;
371 if (muxer == nullptr) {
372 returnValue = SUCCESS;
373 }
374 napi_value result = nullptr;
375 napi_create_int32(env, returnValue, &result);
376 return result;
377 }
378
OHAVMuxerSetRotationAbnormal(napi_env env,napi_callback_info info)379 static napi_value OHAVMuxerSetRotationAbnormal(napi_env env, napi_callback_info info)
380 {
381 OH_AVMuxer *muxer = nullptr;
382 int audioTrackId = MUNUSONE;
383 int32_t rotation = ZEROVAL;
384 int trackId = audioTrackId;
385 OH_AVCodecBufferAttr attrInfo;
386 attrInfo.pts = ONETWOVAL;
387 attrInfo.size = ONEFIVEVAL;
388 attrInfo.offset = ZEROVAL;
389 attrInfo.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME;
390 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
391 int fileDescribe = open("/data/storage/el2/base/haps/entry_test/files/demo.mp4",
392 O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
393 LOG("OHAVMuxerSetRotationAbnormal fd is %{public}d", fileDescribe);
394 muxer = OH_AVMuxer_Create(fileDescribe, format);
395 OH_AVFormat *trackFormat = OH_AVFormat_Create();
396 OH_AVMemory *sample = OH_AVMemory_Create(ONEONEVAL);
397 OH_AVErrCode backInfo = OH_AVMuxer_SetRotation(nullptr, rotation);
398 OH_AVMuxer_AddTrack(muxer, &audioTrackId, trackFormat);
399 OH_AVMuxer_Start(muxer);
400 OH_AVMuxer_WriteSample(muxer, trackId, sample, attrInfo);
401 OH_AVMuxer_Stop(muxer);
402 int returnValue = FAIL;
403 if (backInfo == AV_ERR_INVALID_VAL) {
404 returnValue = SUCCESS;
405 }
406 close(fileDescribe);
407 napi_value result = nullptr;
408 OH_AVMemory_Destroy(sample);
409 OH_AVMuxer_Destroy(muxer);
410 OH_AVFormat_Destroy(trackFormat);
411 muxer = nullptr;
412 napi_create_int32(env, returnValue, &result);
413 return result;
414 }
415
OHAVMuxerSetFormatAbnormal(napi_env env,napi_callback_info info)416 static napi_value OHAVMuxerSetFormatAbnormal(napi_env env, napi_callback_info info)
417 {
418 OH_AVMuxer *muxer = nullptr;
419 int audioTrackId = MUNUSONE;
420 int32_t rotation = ZEROVAL;
421 int trackId = audioTrackId;
422 OH_AVCodecBufferAttr attrInfo;
423 attrInfo.pts = ONETWOVAL;
424 attrInfo.size = ONEFIVEVAL;
425 attrInfo.offset = ZEROVAL;
426 attrInfo.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME;
427 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
428 int fileDescribe = open("/data/storage/el2/base/haps/entry_test/files/demo.mp4",
429 O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
430 LOG("OHAVMuxerSetFormatAbnormal fd is %{public}d", fileDescribe);
431 muxer = OH_AVMuxer_Create(fileDescribe, format);
432 OH_AVFormat *trackFormat = OH_AVFormat_Create();
433 OH_AVFormat *fileFormat = OH_AVFormat_Create();
434 OH_AVMemory *sample = OH_AVMemory_Create(ONEONEVAL);
435
436 OH_AVFormat_SetStringValue(fileFormat, OH_MD_KEY_CREATION_TIME, "This is abnormal case");
437 OH_AVErrCode backInfo = OH_AVMuxer_SetFormat(muxer, fileFormat);
438 OH_AVMuxer_AddTrack(muxer, &audioTrackId, trackFormat);
439 OH_AVMuxer_Start(muxer);
440 OH_AVMuxer_WriteSample(muxer, trackId, sample, attrInfo);
441 OH_AVMuxer_Stop(muxer);
442 int returnValue = FAIL;
443 if (backInfo == AV_ERR_INVALID_VAL) { // Expected Returned AV_ERR_INVALID_VAL
444 returnValue = SUCCESS;
445 }
446 close(fileDescribe);
447 napi_value result = nullptr;
448 OH_AVMemory_Destroy(sample);
449 OH_AVMuxer_Destroy(muxer);
450 OH_AVFormat_Destroy(trackFormat);
451 OH_AVFormat_Destroy(fileFormat);
452 muxer = nullptr;
453 napi_create_int32(env, returnValue, &result);
454 return result;
455 }
456
OHAVMuxerStartAbnormal(napi_env env,napi_callback_info info)457 static napi_value OHAVMuxerStartAbnormal(napi_env env, napi_callback_info info)
458 {
459 OH_AVMuxer *muxer = nullptr;
460 int audioTrackId = MUNUSONE;
461 int32_t rotation = ZEROVAL;
462 int trackId = audioTrackId;
463 OH_AVCodecBufferAttr attrInfo;
464 attrInfo.pts = ONETWOVAL;
465 attrInfo.size = ONEFIVEVAL;
466 attrInfo.offset = ZEROVAL;
467 attrInfo.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME;
468 OH_AVMemory *sample = OH_AVMemory_Create(ONEONEVAL);
469 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
470 int fileDescribe = open("/data/storage/el2/base/haps/entry_test/files/demo.mp4",
471 O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
472 LOG("OHAVMuxerStartAbnormal fd is %{public}d", fileDescribe);
473 muxer = OH_AVMuxer_Create(fileDescribe, format);
474 OH_AVFormat *trackFormat = OH_AVFormat_Create();
475 OH_AVMuxer_SetRotation(muxer, rotation);
476 OH_AVMuxer_AddTrack(muxer, &audioTrackId, trackFormat);
477 OH_AVErrCode backInfo = OH_AVMuxer_Start(nullptr);
478 OH_AVMuxer_WriteSample(muxer, trackId, sample, attrInfo);
479 OH_AVMuxer_Stop(muxer);
480 int returnValue = FAIL;
481 if (backInfo == AV_ERR_INVALID_VAL) {
482 returnValue = SUCCESS;
483 }
484 close(fileDescribe);
485 napi_value result = nullptr;
486 OH_AVMemory_Destroy(sample);
487 OH_AVMuxer_Destroy(muxer);
488 OH_AVFormat_Destroy(trackFormat);
489 muxer = nullptr;
490 napi_create_int32(env, returnValue, &result);
491 return result;
492 }
493
OHAVMuxerStopAbnormal(napi_env env,napi_callback_info info)494 static napi_value OHAVMuxerStopAbnormal(napi_env env, napi_callback_info info)
495 {
496 OH_AVMuxer *muxer = nullptr;
497 int audioTrackId = MUNUSONE;
498 int32_t rotation = ZEROVAL;
499 int trackId = audioTrackId;
500 OH_AVCodecBufferAttr attrInfo;
501 attrInfo.pts = ONETWOVAL;
502 attrInfo.size = ONEFIVEVAL;
503 attrInfo.offset = ZEROVAL;
504 attrInfo.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME;
505 OH_AVMemory *sample = OH_AVMemory_Create(ONEONEVAL);
506 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
507 int fileDescribe = open("/data/storage/el2/base/haps/entry_test/files/demo.mp4",
508 O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
509 LOG("OHAVMuxerStopAbnormal fd is %{public}d", fileDescribe);
510 muxer = OH_AVMuxer_Create(fileDescribe, format);
511 OH_AVFormat *trackFormat = OH_AVFormat_Create();
512 OH_AVMuxer_SetRotation(muxer, rotation);
513 OH_AVMuxer_AddTrack(muxer, &audioTrackId, trackFormat);
514 OH_AVMuxer_Start(muxer);
515 OH_AVMuxer_WriteSample(muxer, trackId, sample, attrInfo);
516 OH_AVErrCode backInfo = OH_AVMuxer_Stop(nullptr);
517 int returnValue = FAIL;
518 if (backInfo == AV_ERR_INVALID_VAL) {
519 returnValue = SUCCESS;
520 }
521 close(fileDescribe);
522 napi_value result = nullptr;
523 OH_AVMemory_Destroy(sample);
524 OH_AVMuxer_Destroy(muxer);
525 OH_AVFormat_Destroy(trackFormat);
526 muxer = nullptr;
527 napi_create_int32(env, returnValue, &result);
528 return result;
529 }
530
OHAVMuxerWriteSampleAbnormal(napi_env env,napi_callback_info info)531 static napi_value OHAVMuxerWriteSampleAbnormal(napi_env env, napi_callback_info info)
532 {
533 OH_AVMuxer *muxer = nullptr;
534 int audioTrackId = MUNUSONE;
535 int32_t rotation = ZEROVAL;
536 int trackId = audioTrackId;
537 OH_AVCodecBufferAttr attrInfo;
538 attrInfo.pts = ONETWOVAL;
539 attrInfo.size = ONEFIVEVAL;
540 attrInfo.offset = ZEROVAL;
541 attrInfo.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME;
542 OH_AVMemory *sample = OH_AVMemory_Create(ONEONEVAL);
543 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
544 int fileDescribe = open("/data/storage/el2/base/haps/entry_test/files/demo.mp4",
545 O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
546 LOG("OHAVMuxerWriteSampleAbnormal fd is %{public}d", fileDescribe);
547 muxer = OH_AVMuxer_Create(fileDescribe, format);
548 OH_AVFormat *trackFormat = OH_AVFormat_Create();
549 OH_AVMuxer_SetRotation(muxer, rotation);
550 OH_AVMuxer_AddTrack(muxer, &audioTrackId, trackFormat);
551 OH_AVMuxer_Start(muxer);
552 OH_AVErrCode backInfo = OH_AVMuxer_WriteSample(nullptr, trackId, sample, attrInfo);
553 OH_AVMuxer_Stop(muxer);
554 int returnValue = FAIL;
555 if (backInfo == AV_ERR_INVALID_VAL) {
556 returnValue = SUCCESS;
557 }
558 close(fileDescribe);
559 napi_value result = nullptr;
560 OH_AVMemory_Destroy(sample);
561 OH_AVMuxer_Destroy(muxer);
562 OH_AVFormat_Destroy(trackFormat);
563 muxer = nullptr;
564 napi_create_int32(env, returnValue, &result);
565 return result;
566 }
567
OHAVMuxerAddTrackAbnormal(napi_env env,napi_callback_info info)568 static napi_value OHAVMuxerAddTrackAbnormal(napi_env env, napi_callback_info info)
569 {
570 OH_AVMuxer *muxer = nullptr;
571 int audioTrackId = MUNUSONE;
572 int32_t rotation = ZEROVAL;
573 int trackId = audioTrackId;
574 OH_AVCodecBufferAttr attrInfo;
575 attrInfo.pts = ONETWOVAL;
576 attrInfo.size = ONEFIVEVAL;
577 attrInfo.offset = ZEROVAL;
578 attrInfo.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME;
579 OH_AVMemory *sample = OH_AVMemory_Create(ONEONEVAL);
580 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
581 int fileDescribe = open("/data/storage/el2/base/haps/entry_test/files/demo.mp4",
582 O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
583 muxer = OH_AVMuxer_Create(fileDescribe, format);
584 OH_AVFormat *trackFormat = OH_AVFormat_Create();
585 OH_AVMuxer_SetRotation(muxer, rotation);
586 int ret = OH_AVMuxer_AddTrack(nullptr, &audioTrackId, trackFormat);
587 OH_AVMuxer_Start(muxer);
588 OH_AVMuxer_WriteSample(muxer, trackId, sample, attrInfo);
589 OH_AVMuxer_Stop(muxer);
590 int returnValue = FAIL;
591 if (ret == AV_ERR_INVALID_VAL) {
592 returnValue = SUCCESS;
593 }
594 close(fileDescribe);
595 napi_value result = nullptr;
596 OH_AVMemory_Destroy(sample);
597 OH_AVMuxer_Destroy(muxer);
598 OH_AVFormat_Destroy(trackFormat);
599 muxer = nullptr;
600 napi_create_int32(env, returnValue, &result);
601 return result;
602 }
603
OHAVMuxerDestroyAbnormal(napi_env env,napi_callback_info info)604 static napi_value OHAVMuxerDestroyAbnormal(napi_env env, napi_callback_info info)
605 {
606 int returnValue = FAIL;
607 napi_value result = nullptr;
608 OH_AVErrCode desval = OH_AVMuxer_Destroy(nullptr);
609 if (desval == AV_ERR_INVALID_VAL) {
610 returnValue = SUCCESS;
611 }
612 napi_create_int32(env, returnValue, &result);
613 return result;
614 }
615 EXTERN_C_START
Init(napi_env env,napi_value exports)616 static napi_value Init(napi_env env, napi_value exports)
617 {
618 napi_property_descriptor desc[] = {
619 {"oHAVMuxerCreate", nullptr, OHAVMuxerCreate, nullptr, nullptr, nullptr, napi_default, nullptr},
620 {"oHAVMuxerSetRotation", nullptr, OHAVMuxerSetRotation, nullptr, nullptr, nullptr, napi_default, nullptr},
621 {"oHAVMuxerSetFormat", nullptr, OHAVMuxerSetFormat, nullptr, nullptr, nullptr, napi_default, nullptr},
622 {"oHAVMuxerStart", nullptr, OHAVMuxerStart, nullptr, nullptr, nullptr, napi_default, nullptr},
623 {"oHAVMuxerStop", nullptr, OHAVMuxerStop, nullptr, nullptr, nullptr, napi_default, nullptr},
624 {"oHAVMuxerWriteSample", nullptr, OHAVMuxerWriteSample, nullptr, nullptr, nullptr, napi_default, nullptr},
625 {"OHAVMuxerWriteSampleBuffer", nullptr, OHAVMuxerWriteSampleBuffer, nullptr, nullptr, nullptr, napi_default, nullptr},
626 {"oHAVMuxerAddTrack", nullptr, OHAVMuxerAddTrack, nullptr, nullptr, nullptr, napi_default, nullptr},
627 {"oHAVMuxerDestroy", nullptr, OHAVMuxerDestroy, nullptr, nullptr, nullptr, napi_default, nullptr},
628 {"oHAVMuxerCreateAbnormal", nullptr, OHAVMuxerCreateAbnormal, nullptr, nullptr, nullptr, napi_default, nullptr},
629 {"oHAVMuxerSetRotationAbnormal", nullptr, OHAVMuxerSetRotationAbnormal, nullptr, nullptr, nullptr, napi_default,
630 nullptr},
631 {"oHAVMuxerSetFormatAbnormal", nullptr, OHAVMuxerSetFormatAbnormal, nullptr, nullptr, nullptr, napi_default,
632 nullptr},
633 {"oHAVMuxerStartAbnormal", nullptr, OHAVMuxerStartAbnormal, nullptr, nullptr, nullptr, napi_default, nullptr},
634 {"oHAVMuxerStopAbnormal", nullptr, OHAVMuxerStopAbnormal, nullptr, nullptr, nullptr, napi_default, nullptr},
635 {"oHAVMuxerWriteSampleAbnormal", nullptr, OHAVMuxerWriteSampleAbnormal, nullptr, nullptr, nullptr, napi_default,
636 nullptr},
637 {"oHAVMuxerAddTrackAbnormal", nullptr, OHAVMuxerAddTrackAbnormal, nullptr, nullptr, nullptr, napi_default,
638 nullptr},
639 {"oHAVMuxerDestroyAbnormal", nullptr, OHAVMuxerDestroyAbnormal, nullptr, nullptr, nullptr, napi_default,
640 nullptr},
641
642 };
643 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
644 return exports;
645 }
646
647 EXTERN_C_END
648
649 static napi_module demoModule = {
650 .nm_version = 1,
651 .nm_flags = 0,
652 .nm_filename = nullptr,
653 .nm_register_func = Init,
654 .nm_modname = "nativeavmuxer",
655 .nm_priv = ((void *)0),
656 .reserved = {0},
657 };
658
RegisterEntryModule(void)659 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }
660