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 36static 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 54static 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 muxer = nullptr; 84 napi_create_int32(env, returnValue, &result); 85 return result; 86} 87 88static napi_value OHAVMuxerStart(napi_env env, napi_callback_info info) 89{ 90 OH_AVMuxer *muxer = nullptr; 91 int audioTrackId = MUNUSONE; 92 int32_t rotation = ZEROVAL; 93 int trackId = audioTrackId; 94 OH_AVCodecBufferAttr attrInfo; 95 attrInfo.pts = ONETWOVAL; 96 attrInfo.size = ONEFIVEVAL; 97 attrInfo.offset = ZEROVAL; 98 attrInfo.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME; 99 OH_AVMemory *sample = OH_AVMemory_Create(ONEONEVAL); 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_AVMuxer_SetRotation(muxer, rotation); 105 OH_AVFormat_SetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AAC); 106 OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_SAMPLE_RATE, BIGVAL); 107 OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_CHANNEL_COUNT, TWOVAL); 108 OH_AVMuxer_AddTrack(muxer, &audioTrackId, trackFormat); 109 OH_AVErrCode backInfo = OH_AVMuxer_Start(muxer); 110 OH_AVMuxer_WriteSample(muxer, trackId, sample, attrInfo); 111 OH_AVMuxer_Stop(muxer); 112 int returnValue = FAIL; 113 if (backInfo == AV_ERR_OK) { 114 returnValue = SUCCESS; 115 } 116 close(fileDescribe); 117 napi_value result = nullptr; 118 OH_AVMemory_Destroy(sample); 119 OH_AVMuxer_Destroy(muxer); 120 muxer = nullptr; 121 napi_create_int32(env, returnValue, &result); 122 return result; 123} 124 125static napi_value OHAVMuxerStop(napi_env env, napi_callback_info info) 126{ 127 OH_AVMuxer *muxer = nullptr; 128 int audioTrackId = MUNUSONE; 129 int32_t rotation = ZEROVAL; 130 int trackId = audioTrackId; 131 OH_AVCodecBufferAttr attrInfo; 132 attrInfo.pts = ONETWOVAL; 133 attrInfo.size = ONEFIVEVAL; 134 attrInfo.offset = ZEROVAL; 135 attrInfo.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME; 136 OH_AVMemory *sample = OH_AVMemory_Create(ONEONEVAL); 137 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4; 138 int fileDescribe = open("/data/storage/el2/base/files/demo.mp4", O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR); 139 muxer = OH_AVMuxer_Create(fileDescribe, format); 140 OH_AVFormat *trackFormat = OH_AVFormat_Create(); 141 OH_AVMuxer_SetRotation(muxer, rotation); 142 OH_AVFormat_SetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AAC); 143 OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_SAMPLE_RATE, BIGVAL); 144 OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_CHANNEL_COUNT, TWOVAL); 145 OH_AVMuxer_AddTrack(muxer, &audioTrackId, trackFormat); 146 OH_AVMuxer_Start(muxer); 147 OH_AVMuxer_WriteSample(muxer, trackId, sample, attrInfo); 148 OH_AVErrCode backInfo = OH_AVMuxer_Stop(muxer); 149 int returnValue = FAIL; 150 if (backInfo == AV_ERR_OK) { 151 returnValue = SUCCESS; 152 } 153 close(fileDescribe); 154 napi_value result = nullptr; 155 OH_AVMemory_Destroy(sample); 156 OH_AVMuxer_Destroy(muxer); 157 muxer = nullptr; 158 napi_create_int32(env, returnValue, &result); 159 return result; 160} 161 162static napi_value OHAVMuxerWriteSample(napi_env env, napi_callback_info info) 163{ 164 OH_AVMuxer *muxer = nullptr; 165 int audioTrackId = MUNUSONE; 166 int32_t rotation = ZEROVAL; 167 OH_AVCodecBufferAttr attrInfo; 168 attrInfo.pts = ONEONEVAL; 169 attrInfo.size = ONEONEVAL; 170 attrInfo.offset = ZEROVAL; 171 attrInfo.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME; 172 int trackId = audioTrackId; 173 OH_AVMemory *sample = OH_AVMemory_Create(TWOTWOVAL); 174 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_DEFAULT; 175 int fileDescribe = open("/data/storage/el2/base/files/demo.mp4", O_CREAT | O_RDWR | O_TRUNC, PARAM_0666); 176 muxer = OH_AVMuxer_Create(fileDescribe, format); 177 OH_AVFormat *trackFormat = OH_AVFormat_Create(); 178 OH_AVMuxer_SetRotation(muxer, rotation); 179 OH_AVFormat_SetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AAC); 180 OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_SAMPLE_RATE, BIGVAL); 181 OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_CHANNEL_COUNT, TWOVAL); 182 OH_AVMuxer_AddTrack(muxer, &trackId, trackFormat); 183 OH_AVMuxer_Start(muxer); 184 int backInfo = OH_AVMuxer_WriteSample(muxer, trackId, sample, attrInfo); 185 int returnValue = FAIL; 186 OH_AVMuxer_Stop(muxer); 187 if (backInfo == AV_ERR_OK) { 188 returnValue = SUCCESS; 189 } 190 close(fileDescribe); 191 napi_value result = nullptr; 192 OH_AVMemory_Destroy(sample); 193 OH_AVMuxer_Destroy(muxer); 194 muxer = nullptr; 195 napi_create_int32(env, returnValue, &result); 196 return result; 197} 198 199static napi_value OHAVMuxerWriteSampleBuffer(napi_env env, napi_callback_info info) { 200 OH_AVMuxer *muxer = nullptr; 201 int audioTrackId = MUNUSONE; 202 int32_t rotation = ZEROVAL; 203 OH_AVCodecBufferAttr attrInfo; 204 attrInfo.pts = ONEONEVAL; 205 attrInfo.size = ONEONEVAL; 206 attrInfo.offset = ZEROVAL; 207 attrInfo.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME; 208 int trackId = audioTrackId; 209 OH_AVBuffer *sample = OH_AVBuffer_Create(TWOTWOVAL); 210 OH_AVBuffer_SetBufferAttr(sample, &attrInfo); 211 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_DEFAULT; 212 int fileDescribe = open("/data/storage/el2/base/files/demo.mp4", O_CREAT | O_RDWR | O_TRUNC, PARAM_0666); 213 muxer = OH_AVMuxer_Create(fileDescribe, format); 214 OH_AVFormat *trackFormat = OH_AVFormat_Create(); 215 OH_AVMuxer_SetRotation(muxer, rotation); 216 OH_AVFormat_SetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AAC); 217 OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_SAMPLE_RATE, BIGVAL); 218 OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_CHANNEL_COUNT, TWOVAL); 219 OH_AVMuxer_AddTrack(muxer, &trackId, trackFormat); 220 OH_AVMuxer_Start(muxer); 221 int backInfo = OH_AVMuxer_WriteSampleBuffer(muxer, trackId, sample); 222 int returnValue = FAIL; 223 OH_AVMuxer_Stop(muxer); 224 if (backInfo == AV_ERR_OK) { 225 returnValue = SUCCESS; 226 } 227 close(fileDescribe); 228 napi_value result = nullptr; 229 OH_AVBuffer_Destroy(sample); 230 OH_AVMuxer_Destroy(muxer); 231 OH_AVFormat_Destroy(trackFormat); 232 muxer = nullptr; 233 napi_create_int32(env, returnValue, &result); 234 return result; 235} 236 237static napi_value OHAVMuxerAddTrack(napi_env env, napi_callback_info info) 238{ 239 OH_AVMuxer *muxer = nullptr; 240 int audioTrackId = MUNUSONE; 241 int32_t rotation = ZEROVAL; 242 int trackId = audioTrackId; 243 OH_AVCodecBufferAttr attrInfo; 244 attrInfo.pts = ONETWOVAL; 245 attrInfo.size = ONEFIVEVAL; 246 attrInfo.offset = ZEROVAL; 247 attrInfo.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME; 248 OH_AVMemory *sample = OH_AVMemory_Create(ONEONEVAL); 249 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4; 250 int fileDescribe = open("/data/storage/el2/base/files/demo.mp4", O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR); 251 muxer = OH_AVMuxer_Create(fileDescribe, format); 252 OH_AVFormat *trackFormat = OH_AVFormat_Create(); 253 OH_AVMuxer_SetRotation(muxer, rotation); 254 OH_AVFormat_SetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AAC); 255 OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_SAMPLE_RATE, BIGVAL); 256 OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_CHANNEL_COUNT, TWOVAL); 257 int ret = OH_AVMuxer_AddTrack(muxer, &audioTrackId, trackFormat); 258 OH_AVMuxer_Start(muxer); 259 OH_AVMuxer_WriteSample(muxer, trackId, sample, attrInfo); 260 OH_AVMuxer_Stop(muxer); 261 int returnValue = FAIL; 262 if (ret == AV_ERR_OK) { 263 returnValue = SUCCESS; 264 } 265 close(fileDescribe); 266 napi_value result = nullptr; 267 OH_AVMemory_Destroy(sample); 268 OH_AVMuxer_Destroy(muxer); 269 muxer = nullptr; 270 napi_create_int32(env, returnValue, &result); 271 return result; 272} 273 274static napi_value OHAVMuxerDestroy(napi_env env, napi_callback_info info) 275{ 276 OH_AVMuxer *muxer = nullptr; 277 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4; 278 int fileDescribe = open("/data/storage/el2/base/files/demo.mp4", O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR); 279 muxer = OH_AVMuxer_Create(fileDescribe, format); 280 int returnValue = FAIL; 281 napi_value result = nullptr; 282 OH_AVErrCode desval = OH_AVMuxer_Destroy(muxer); 283 if (desval == AV_ERR_OK) { 284 returnValue = SUCCESS; 285 } 286 close(fileDescribe); 287 muxer = nullptr; 288 napi_create_int32(env, returnValue, &result); 289 return result; 290} 291 292static napi_value OHAVMuxerCreateAbnormal(napi_env env, napi_callback_info info) 293{ 294 OH_AVMuxer *muxer = nullptr; 295 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4; 296 int fileDescribe = MUNUSONE; 297 muxer = OH_AVMuxer_Create(fileDescribe, format); 298 int returnValue = FAIL; 299 if (muxer == nullptr) { 300 returnValue = SUCCESS; 301 } 302 napi_value result = nullptr; 303 napi_create_int32(env, returnValue, &result); 304 return result; 305} 306 307static napi_value OHAVMuxerSetRotationAbnormal(napi_env env, napi_callback_info info) 308{ 309 OH_AVMuxer *muxer = nullptr; 310 int audioTrackId = MUNUSONE; 311 int32_t rotation = ZEROVAL; 312 int trackId = audioTrackId; 313 OH_AVCodecBufferAttr attrInfo; 314 attrInfo.pts = ONETWOVAL; 315 attrInfo.size = ONEFIVEVAL; 316 attrInfo.offset = ZEROVAL; 317 attrInfo.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME; 318 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4; 319 int fileDescribe = open("/data/storage/el2/base/files/demo.mp4", O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR); 320 muxer = OH_AVMuxer_Create(fileDescribe, format); 321 OH_AVFormat *trackFormat = OH_AVFormat_Create(); 322 OH_AVMemory *sample = OH_AVMemory_Create(ONEONEVAL); 323 OH_AVErrCode backInfo = OH_AVMuxer_SetRotation(nullptr, rotation); 324 OH_AVMuxer_AddTrack(muxer, &audioTrackId, trackFormat); 325 OH_AVMuxer_Start(muxer); 326 OH_AVMuxer_WriteSample(muxer, trackId, sample, attrInfo); 327 OH_AVMuxer_Stop(muxer); 328 int returnValue = FAIL; 329 if (backInfo == AV_ERR_INVALID_VAL) { 330 returnValue = SUCCESS; 331 } 332 close(fileDescribe); 333 napi_value result = nullptr; 334 OH_AVMemory_Destroy(sample); 335 OH_AVMuxer_Destroy(muxer); 336 muxer = nullptr; 337 napi_create_int32(env, returnValue, &result); 338 return result; 339} 340 341static napi_value OHAVMuxerStartAbnormal(napi_env env, napi_callback_info info) 342{ 343 OH_AVMuxer *muxer = nullptr; 344 int audioTrackId = MUNUSONE; 345 int32_t rotation = ZEROVAL; 346 int trackId = audioTrackId; 347 OH_AVCodecBufferAttr attrInfo; 348 attrInfo.pts = ONETWOVAL; 349 attrInfo.size = ONEFIVEVAL; 350 attrInfo.offset = ZEROVAL; 351 attrInfo.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME; 352 OH_AVMemory *sample = OH_AVMemory_Create(ONEONEVAL); 353 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4; 354 int fileDescribe = open("/data/storage/el2/base/files/demo.mp4", O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR); 355 muxer = OH_AVMuxer_Create(fileDescribe, format); 356 OH_AVFormat *trackFormat = OH_AVFormat_Create(); 357 OH_AVMuxer_SetRotation(muxer, rotation); 358 OH_AVMuxer_AddTrack(muxer, &audioTrackId, trackFormat); 359 OH_AVErrCode backInfo = OH_AVMuxer_Start(nullptr); 360 OH_AVMuxer_WriteSample(muxer, trackId, sample, attrInfo); 361 OH_AVMuxer_Stop(muxer); 362 int returnValue = FAIL; 363 if (backInfo == AV_ERR_INVALID_VAL) { 364 returnValue = SUCCESS; 365 } 366 close(fileDescribe); 367 napi_value result = nullptr; 368 OH_AVMemory_Destroy(sample); 369 OH_AVMuxer_Destroy(muxer); 370 muxer = nullptr; 371 napi_create_int32(env, returnValue, &result); 372 return result; 373} 374 375static napi_value OHAVMuxerStopAbnormal(napi_env env, napi_callback_info info) 376{ 377 OH_AVMuxer *muxer = nullptr; 378 int audioTrackId = MUNUSONE; 379 int32_t rotation = ZEROVAL; 380 int trackId = audioTrackId; 381 OH_AVCodecBufferAttr attrInfo; 382 attrInfo.pts = ONETWOVAL; 383 attrInfo.size = ONEFIVEVAL; 384 attrInfo.offset = ZEROVAL; 385 attrInfo.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME; 386 OH_AVMemory *sample = OH_AVMemory_Create(ONEONEVAL); 387 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4; 388 int fileDescribe = open("/data/storage/el2/base/files/demo.mp4", O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR); 389 muxer = OH_AVMuxer_Create(fileDescribe, format); 390 OH_AVFormat *trackFormat = OH_AVFormat_Create(); 391 OH_AVMuxer_SetRotation(muxer, rotation); 392 OH_AVMuxer_AddTrack(muxer, &audioTrackId, trackFormat); 393 OH_AVMuxer_Start(muxer); 394 OH_AVMuxer_WriteSample(muxer, trackId, sample, attrInfo); 395 OH_AVErrCode backInfo = OH_AVMuxer_Stop(nullptr); 396 int returnValue = FAIL; 397 if (backInfo == AV_ERR_INVALID_VAL) { 398 returnValue = SUCCESS; 399 } 400 close(fileDescribe); 401 napi_value result = nullptr; 402 OH_AVMemory_Destroy(sample); 403 OH_AVMuxer_Destroy(muxer); 404 muxer = nullptr; 405 napi_create_int32(env, returnValue, &result); 406 return result; 407} 408 409static napi_value OHAVMuxerWriteSampleAbnormal(napi_env env, napi_callback_info info) 410{ 411 OH_AVMuxer *muxer = nullptr; 412 int audioTrackId = MUNUSONE; 413 int32_t rotation = ZEROVAL; 414 int trackId = audioTrackId; 415 OH_AVCodecBufferAttr attrInfo; 416 attrInfo.pts = ONETWOVAL; 417 attrInfo.size = ONEFIVEVAL; 418 attrInfo.offset = ZEROVAL; 419 attrInfo.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME; 420 OH_AVMemory *sample = OH_AVMemory_Create(ONEONEVAL); 421 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4; 422 int fileDescribe = open("/data/storage/el2/base/files/demo.mp4", O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR); 423 muxer = OH_AVMuxer_Create(fileDescribe, format); 424 OH_AVFormat *trackFormat = OH_AVFormat_Create(); 425 OH_AVMuxer_SetRotation(muxer, rotation); 426 OH_AVMuxer_AddTrack(muxer, &audioTrackId, trackFormat); 427 OH_AVMuxer_Start(muxer); 428 OH_AVErrCode backInfo = OH_AVMuxer_WriteSample(nullptr, trackId, sample, attrInfo); 429 OH_AVMuxer_Stop(muxer); 430 int returnValue = FAIL; 431 if (backInfo == AV_ERR_INVALID_VAL) { 432 returnValue = SUCCESS; 433 } 434 close(fileDescribe); 435 napi_value result = nullptr; 436 OH_AVMemory_Destroy(sample); 437 OH_AVMuxer_Destroy(muxer); 438 muxer = nullptr; 439 napi_create_int32(env, returnValue, &result); 440 return result; 441} 442 443static napi_value OHAVMuxerAddTrackAbnormal(napi_env env, napi_callback_info info) 444{ 445 OH_AVMuxer *muxer = nullptr; 446 int audioTrackId = MUNUSONE; 447 int32_t rotation = ZEROVAL; 448 int trackId = audioTrackId; 449 OH_AVCodecBufferAttr attrInfo; 450 attrInfo.pts = ONETWOVAL; 451 attrInfo.size = ONEFIVEVAL; 452 attrInfo.offset = ZEROVAL; 453 attrInfo.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME; 454 OH_AVMemory *sample = OH_AVMemory_Create(ONEONEVAL); 455 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4; 456 int fileDescribe = open("/data/storage/el2/base/files/demo.mp4", O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR); 457 muxer = OH_AVMuxer_Create(fileDescribe, format); 458 OH_AVFormat *trackFormat = OH_AVFormat_Create(); 459 OH_AVMuxer_SetRotation(muxer, rotation); 460 int ret = OH_AVMuxer_AddTrack(nullptr, &audioTrackId, trackFormat); 461 OH_AVMuxer_Start(muxer); 462 OH_AVMuxer_WriteSample(muxer, trackId, sample, attrInfo); 463 OH_AVMuxer_Stop(muxer); 464 int returnValue = FAIL; 465 if (ret == AV_ERR_INVALID_VAL) { 466 returnValue = SUCCESS; 467 } 468 close(fileDescribe); 469 napi_value result = nullptr; 470 OH_AVMemory_Destroy(sample); 471 OH_AVMuxer_Destroy(muxer); 472 muxer = nullptr; 473 napi_create_int32(env, returnValue, &result); 474 return result; 475} 476 477static napi_value OHAVMuxerDestroyAbnormal(napi_env env, napi_callback_info info) 478{ 479 int returnValue = FAIL; 480 napi_value result = nullptr; 481 OH_AVErrCode desval = OH_AVMuxer_Destroy(nullptr); 482 if (desval == AV_ERR_INVALID_VAL) { 483 returnValue = SUCCESS; 484 } 485 napi_create_int32(env, returnValue, &result); 486 return result; 487} 488EXTERN_C_START 489static napi_value Init(napi_env env, napi_value exports) 490{ 491 napi_property_descriptor desc[] = { 492 {"oHAVMuxerCreate", nullptr, OHAVMuxerCreate, nullptr, nullptr, nullptr, napi_default, nullptr}, 493 {"oHAVMuxerSetRotation", nullptr, OHAVMuxerSetRotation, nullptr, nullptr, nullptr, napi_default, nullptr}, 494 {"oHAVMuxerStart", nullptr, OHAVMuxerStart, nullptr, nullptr, nullptr, napi_default, nullptr}, 495 {"oHAVMuxerStop", nullptr, OHAVMuxerStop, nullptr, nullptr, nullptr, napi_default, nullptr}, 496 {"oHAVMuxerWriteSample", nullptr, OHAVMuxerWriteSample, nullptr, nullptr, nullptr, napi_default, nullptr}, 497 {"OHAVMuxerWriteSampleBuffer", nullptr, OHAVMuxerWriteSampleBuffer, nullptr, nullptr, nullptr, napi_default, nullptr}, 498 {"oHAVMuxerAddTrack", nullptr, OHAVMuxerAddTrack, nullptr, nullptr, nullptr, napi_default, nullptr}, 499 {"oHAVMuxerDestroy", nullptr, OHAVMuxerDestroy, nullptr, nullptr, nullptr, napi_default, nullptr}, 500 501 {"oHAVMuxerCreateAbnormal", nullptr, OHAVMuxerCreateAbnormal, nullptr, nullptr, nullptr, napi_default, nullptr}, 502 {"oHAVMuxerSetRotationAbnormal", nullptr, OHAVMuxerSetRotationAbnormal, nullptr, nullptr, nullptr, napi_default, 503 nullptr}, 504 {"oHAVMuxerStartAbnormal", nullptr, OHAVMuxerStartAbnormal, nullptr, nullptr, nullptr, napi_default, nullptr}, 505 {"oHAVMuxerStopAbnormal", nullptr, OHAVMuxerStopAbnormal, nullptr, nullptr, nullptr, napi_default, nullptr}, 506 {"oHAVMuxerWriteSampleAbnormal", nullptr, OHAVMuxerWriteSampleAbnormal, nullptr, nullptr, nullptr, napi_default, 507 nullptr}, 508 {"oHAVMuxerAddTrackAbnormal", nullptr, OHAVMuxerAddTrackAbnormal, nullptr, nullptr, nullptr, napi_default, 509 nullptr}, 510 {"oHAVMuxerDestroyAbnormal", nullptr, OHAVMuxerDestroyAbnormal, nullptr, nullptr, nullptr, napi_default, 511 nullptr}, 512 513 }; 514 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); 515 return exports; 516} 517 518EXTERN_C_END 519 520static napi_module demoModule = { 521 .nm_version = 1, 522 .nm_flags = 0, 523 .nm_filename = nullptr, 524 .nm_register_func = Init, 525 .nm_modname = "nativeavmuxer", 526 .nm_priv = ((void *)0), 527 .reserved = {0}, 528}; 529 530extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } 531