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 <linux/usb/charger.h>
20 #include <multimedia/player_framework/native_avcodec_audiodecoder.h>
21 #include <multimedia/player_framework/native_avcodec_base.h>
22 #include <multimedia/player_framework/native_avdemuxer.h>
23 #include <multimedia/player_framework/native_averrors.h>
24 #include <multimedia/player_framework/native_avmuxer.h>
25 #include <multimedia/player_framework/native_avsource.h>
26 #include <multimedia/player_framework/native_avformat.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29
30 #define MUNUSONE (-1)
31 #define ZEROVAL 0
32 #define BIGVAL (2160*3840)
33 #define TWOVAL 2
34 #define ONEONEVAL 11
35 #define PARAM_1 1
36 #define TWOTWOVAL (1024 * 1280 * 1.5)
37 #define SUCCESS 0
38 #define FAIL (-1)
39 #define DENSITY 240
40 #define ONEHUNVAL 100
41 #define PARAM_0666 0666
42 #define PARAM_0 0
43 #define PATH "/data/storage/el2/base/haps/entry_test/files/demo.mp4"
44 #include "hilog/log.h"
45
46 #define AUDIO_LOG_TAG "AVCODEC_TAGLOG"
47 #define AUDIO_LOG_DOMAIN 0x3200
48
49 #define LOG(fmt, ...) (void)OH_LOG_Print(LOG_APP, LOG_INFO, AUDIO_LOG_DOMAIN, AUDIO_LOG_TAG, fmt, ##__VA_ARGS__)
50
51
GetFileSize(const char * fileName)52 size_t GetFileSize(const char *fileName)
53 {
54 if (fileName == nullptr) {
55 return 0;
56 }
57 struct stat statbuf;
58 stat(fileName, &statbuf);
59 size_t filesize = statbuf.st_size;
60 return filesize;
61 }
62
GetSource()63 static OH_AVSource *GetSource()
64 {
65 char fileName[] = {"/data/storage/el2/base/haps/entry_test/files/demo.mp4"};
66 int fd = open(fileName, O_RDONLY, PARAM_0666);
67 LOG("GetSource fd is %{public}d", fd);
68 OH_AVSource *source;
69 int64_t offset = ZEROVAL;
70 source = OH_AVSource_CreateWithFD(fd, offset, GetFileSize(fileName));
71 close(fd);
72 return source;
73 }
74
OHAVDemuxerCreateWithSource(napi_env env,napi_callback_info info)75 static napi_value OHAVDemuxerCreateWithSource(napi_env env, napi_callback_info info)
76 {
77 OH_AVDemuxer *demuxer = nullptr;
78 OH_AVSource *source = GetSource();
79 demuxer = OH_AVDemuxer_CreateWithSource(source);
80 int returnValue = FAIL;
81 if (demuxer != nullptr) {
82 returnValue = SUCCESS;
83 } else if (demuxer == nullptr) {
84 returnValue = ONEHUNVAL;
85 }
86 napi_value result = nullptr;
87 napi_create_int32(env, returnValue, &result);
88 OH_AVDemuxer_Destroy(demuxer);
89 return result;
90 }
91
OHAVDemuxerDestroy(napi_env env,napi_callback_info info)92 static napi_value OHAVDemuxerDestroy(napi_env env, napi_callback_info info)
93 {
94 OH_AVErrCode backInfo;
95 OH_AVDemuxer *demuxer = nullptr;
96 demuxer = OH_AVDemuxer_CreateWithSource(GetSource());
97 int returnValue = FAIL;
98 backInfo = OH_AVDemuxer_Destroy(demuxer);
99 if (backInfo == AV_ERR_OK) {
100 returnValue = SUCCESS;
101 }
102 demuxer = nullptr;
103 napi_value result = nullptr;
104 napi_create_int32(env, returnValue, &result);
105 return result;
106 }
107
OHAVDemuxerUnselectTrackByID(napi_env env,napi_callback_info info)108 static napi_value OHAVDemuxerUnselectTrackByID(napi_env env, napi_callback_info info)
109 {
110 OH_AVErrCode backInfo = AV_ERR_OK;
111 OH_AVDemuxer *demuxer = nullptr;
112 uint32_t audioTrackIndex = ZEROVAL;
113 uint32_t videoTrackIndex = ZEROVAL;
114 demuxer = OH_AVDemuxer_CreateWithSource(GetSource());
115 OH_AVDemuxer_SelectTrackByID(demuxer, audioTrackIndex);
116 OH_AVDemuxer_SelectTrackByID(demuxer, videoTrackIndex);
117 backInfo = OH_AVDemuxer_UnselectTrackByID(demuxer, audioTrackIndex);
118 int returnValue = FAIL;
119 if (backInfo == AV_ERR_OK) {
120 returnValue = SUCCESS;
121 } else if (demuxer == nullptr) {
122 returnValue = ONEHUNVAL;
123 }
124 napi_value result = nullptr;
125 napi_create_int32(env, returnValue, &result);
126 OH_AVDemuxer_Destroy(demuxer);
127 return result;
128 }
129
OHAVDemuxerSeekToTime(napi_env env,napi_callback_info info)130 static napi_value OHAVDemuxerSeekToTime(napi_env env, napi_callback_info info)
131 {
132 OH_AVErrCode backInfo = AV_ERR_OK;
133 OH_AVDemuxer *demuxer = nullptr;
134 uint32_t audioTrackIndex = ZEROVAL;
135 uint32_t videoTrackIndex = ZEROVAL;
136 char fileName[] = {"/data/storage/el2/base/haps/entry_test/files/demo.mp4"};
137 int fd = open(fileName, O_RDONLY, PARAM_0666);
138 LOG("OHAVDemuxerSeekToTime fd is %{public}d", fd);
139 OH_AVSource *source;
140 int64_t offset = ZEROVAL;
141 source = OH_AVSource_CreateWithFD(fd, offset, GetFileSize(fileName));
142 close(fd);
143 demuxer = OH_AVDemuxer_CreateWithSource(source);
144 OH_AVDemuxer_SelectTrackByID(demuxer, audioTrackIndex);
145 OH_AVDemuxer_SelectTrackByID(demuxer, videoTrackIndex);
146 int returnValue = FAIL;
147 backInfo = OH_AVDemuxer_SeekToTime(demuxer, ZEROVAL, OH_AVSeekMode::SEEK_MODE_NEXT_SYNC);
148 if (backInfo == AV_ERR_OK) {
149 returnValue = SUCCESS;
150 }
151 if (demuxer == nullptr) {
152 returnValue = ONEHUNVAL;
153 }
154 napi_value result = nullptr;
155 napi_create_int32(env, returnValue, &result);
156 OH_AVDemuxer_Destroy(demuxer);
157 return result;
158 }
159
OHAVDemuxerSelectTrackByID(napi_env env,napi_callback_info info)160 static napi_value OHAVDemuxerSelectTrackByID(napi_env env, napi_callback_info info)
161 {
162 OH_AVErrCode backInfo = AV_ERR_OK;
163 OH_AVErrCode backInfoo = AV_ERR_OK;
164 OH_AVDemuxer *demuxer = nullptr;
165 uint32_t audioTrackIndex = ZEROVAL;
166 uint32_t videoTrackIndex = ZEROVAL;
167 char fileName[] = {"/data/storage/el2/base/haps/entry_test/files/demo.mp4"};
168 int fd = open(fileName, O_RDONLY, PARAM_0666);
169 LOG("OHAVDemuxerSelectTrackByID fd is %{public}d", fd);
170 OH_AVSource *source;
171 int64_t offset = ZEROVAL;
172 source = OH_AVSource_CreateWithFD(fd, offset, GetFileSize(fileName));
173 close(fd);
174 demuxer = OH_AVDemuxer_CreateWithSource(source);
175 int returnValue = FAIL;
176 backInfo = OH_AVDemuxer_SelectTrackByID(demuxer, audioTrackIndex);
177 backInfoo = OH_AVDemuxer_SelectTrackByID(demuxer, videoTrackIndex);
178
179 int32_t isHDRVivid = 0;
180 OH_AVFormat *trackFormat = OH_AVSource_GetTrackFormat(GetSource(), videoTrackIndex);
181 OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_VIDEO_IS_HDR_VIVID, &isHDRVivid);
182
183 if (backInfo == AV_ERR_OK) {
184 returnValue = SUCCESS;
185 }
186 if (backInfoo == AV_ERR_OK) {
187 returnValue = SUCCESS;
188 }
189 if (demuxer == nullptr) {
190 returnValue = ONEHUNVAL;
191 }
192 napi_value result = nullptr;
193 napi_create_int32(env, returnValue, &result);
194 OH_AVDemuxer_Destroy(demuxer);
195 return result;
196 }
197
OHAVDemuxerReadSample(napi_env env,napi_callback_info info)198 static napi_value OHAVDemuxerReadSample(napi_env env, napi_callback_info info)
199 {
200 OH_AVSource *oH_AVSource = GetSource();
201 OH_AVDemuxer *demuxer;
202 demuxer = OH_AVDemuxer_CreateWithSource(oH_AVSource);
203 int returnValue = FAIL;
204 OH_AVMemory *buffer = OH_AVMemory_Create(TWOTWOVAL);
205 uint32_t audioTrackIndex = ZEROVAL;
206 OH_AVDemuxer_SelectTrackByID(demuxer, audioTrackIndex);
207 OH_AVCodecBufferAttr attrInfo;
208 attrInfo.pts = ZEROVAL;
209 attrInfo.size = ZEROVAL;
210 attrInfo.offset = ZEROVAL;
211 attrInfo.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME;
212 OH_AVDemuxer_SeekToTime(demuxer, ZEROVAL, OH_AVSeekMode::SEEK_MODE_CLOSEST_SYNC);
213 if (OH_AVDemuxer_ReadSample(demuxer, audioTrackIndex, buffer, &attrInfo) == AV_ERR_OK) {
214 returnValue = SUCCESS;
215 }
216 napi_value result = nullptr;
217 OH_AVMemory_Destroy(buffer);
218 if (oH_AVSource == nullptr) {
219 returnValue = PARAM_1;
220 }
221 if (demuxer == nullptr) {
222 returnValue = TWOTWOVAL;
223 }
224 napi_create_int32(env, returnValue, &result);
225 OH_AVDemuxer_Destroy(demuxer);
226 return result;
227 }
228
OHAVDemuxerReadSampleBuffer(napi_env env,napi_callback_info info)229 static napi_value OHAVDemuxerReadSampleBuffer(napi_env env, napi_callback_info info)
230 {
231 OH_AVSource *oH_AVSource = GetSource();
232 OH_AVDemuxer *demuxer;
233 demuxer = OH_AVDemuxer_CreateWithSource(oH_AVSource);
234 int returnValue = FAIL;
235 OH_AVBuffer *buffer = OH_AVBuffer_Create(TWOTWOVAL);
236 uint32_t audioTrackIndex = ZEROVAL;
237 OH_AVDemuxer_SelectTrackByID(demuxer, audioTrackIndex);
238 OH_AVDemuxer_SeekToTime(demuxer, ZEROVAL, OH_AVSeekMode::SEEK_MODE_CLOSEST_SYNC);
239 if (OH_AVDemuxer_ReadSampleBuffer(demuxer, audioTrackIndex, buffer) == AV_ERR_OK) {
240 returnValue = SUCCESS;
241 }
242 napi_value result = nullptr;
243 OH_AVBuffer_Destroy(buffer);
244 if (oH_AVSource == nullptr) {
245 returnValue = PARAM_1;
246 }
247 if (demuxer == nullptr) {
248 returnValue = TWOTWOVAL;
249 }
250 napi_create_int32(env, returnValue, &result);
251 OH_AVDemuxer_Destroy(demuxer);
252 return result;
253 }
254
255 EXTERN_C_START
Init(napi_env env,napi_value exports)256 static napi_value Init(napi_env env, napi_value exports)
257 {
258 napi_property_descriptor desc[] = {
259 {"oHAVDemuxerCreateWithSource", nullptr, OHAVDemuxerCreateWithSource, nullptr, nullptr, nullptr, napi_default,
260 nullptr},
261 {"oHAVDemuxerDestroy", nullptr, OHAVDemuxerDestroy, nullptr, nullptr, nullptr, napi_default, nullptr},
262 {"oHAVDemuxerUnselectTrackByID", nullptr, OHAVDemuxerUnselectTrackByID, nullptr, nullptr, nullptr, napi_default,
263 nullptr},
264 {"oHAVDemuxerReadSample", nullptr, OHAVDemuxerReadSample, nullptr, nullptr, nullptr, napi_default, nullptr},
265 {"oHAVDemuxerSeekToTime", nullptr, OHAVDemuxerSeekToTime, nullptr, nullptr, nullptr, napi_default, nullptr},
266 {"oHAVDemuxerSelectTrackByID", nullptr, OHAVDemuxerSelectTrackByID, nullptr, nullptr, nullptr, napi_default,
267 nullptr},
268 {"oHAVDemuxerReadSampleBuffer", nullptr, OHAVDemuxerReadSampleBuffer, nullptr, nullptr, nullptr, napi_default,
269 nullptr},
270
271 };
272 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
273 return exports;
274 }
275
276 EXTERN_C_END
277
278 static napi_module demoModule = {
279 .nm_version = 1,
280 .nm_flags = 0,
281 .nm_filename = nullptr,
282 .nm_register_func = Init,
283 .nm_modname = "nativeavdemuxer",
284 .nm_priv = ((void *)0),
285 .reserved = { 0 },
286 };
287
RegisterEntryModule(void)288 extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
289 {
290 napi_module_register(&demoModule);
291 }
292