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