• 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 <fcntl.h>
18 #include <js_native_api_types.h>
19 #include <multimedia/player_framework/native_avsource.h>
20 #include <multimedia/player_framework/native_avdemuxer.h>
21 #include <multimedia/player_framework/native_avcapability.h>
22 #include <multimedia/player_framework/native_avcodec_base.h>
23 #include <multimedia/player_framework/native_avformat.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include "hilog/log.h"
27 
28 #define AUDIO_LOG_TAG "AVCODEC_TAGLOG"
29 #define AUDIO_LOG_DOMAIN 0x3200
30 
31 #define LOG(fmt, ...) (void)OH_LOG_Print(LOG_APP, LOG_INFO, AUDIO_LOG_DOMAIN, AUDIO_LOG_TAG, fmt, ##__VA_ARGS__)
32 
33 #define FAIL (-1)
34 #define SUCCESS 0
35 #define ZEROVAL 0
36 #define PARAM_0666 0666
37 #define PARAM_0 0
AVSourceCreateWithURI(napi_env env,napi_callback_info info)38 static napi_value AVSourceCreateWithURI(napi_env env, napi_callback_info info)
39 {
40     int checkParam = FAIL;
41     OH_AVSource *backParam = nullptr;
42     backParam = OH_AVSource_CreateWithURI(nullptr);
43     if (backParam != nullptr) {
44         checkParam = SUCCESS;
45     }
46     OH_AVSource_Destroy(backParam);
47     backParam = nullptr;
48     napi_value result = nullptr;
49     napi_create_int32(env, checkParam, &result);
50     return result;
51 }
52 
AVSourceCreateWithFD(napi_env env,napi_callback_info info)53 static napi_value AVSourceCreateWithFD(napi_env env, napi_callback_info info)
54 {
55     int backParam = FAIL;
56     OH_AVSource *checkParam = nullptr;
57     char fileName[] = {"/data/storage/el2/base/haps/entry_test/files/demo1.mp4"};
58     int fd = open(fileName, O_RDONLY, PARAM_0666);
59     LOG("AVSourceCreateWithFD fd is %{public}d", fd);
60     struct stat fileStatus {};
61     size_t fileSize = PARAM_0;
62     if (stat(fileName, &fileStatus) == ZEROVAL) {
63         fileSize = static_cast<size_t>(fileStatus.st_size);
64     } else {
65         napi_value result = nullptr;
66         napi_create_int32(env, backParam, &result);
67         return result;
68     }
69     checkParam = OH_AVSource_CreateWithFD(fd, PARAM_0, fileSize);
70     if (checkParam != nullptr) {
71         backParam = SUCCESS;
72     }
73     OH_AVSource_Destroy(checkParam);
74     checkParam = nullptr;
75     napi_value result = nullptr;
76     close(fd);
77     napi_create_int32(env, backParam, &result);
78     return result;
79 }
80 
AVSourceDestroy(napi_env env,napi_callback_info info)81 static napi_value AVSourceDestroy(napi_env env, napi_callback_info info)
82 {
83     int backParam = FAIL;
84     OH_AVSource *getSource = nullptr;
85     napi_value result = nullptr;
86     OH_AVErrCode checkParam;
87     char fileName[] = {"/data/storage/el2/base/haps/entry_test/files/demo1.mp4"};
88     int fd = open(fileName, O_RDONLY, PARAM_0666);
89     LOG("AVSourceDestroy fd is %{public}d", fd);
90     struct stat fileStatus {};
91     size_t fileSize = PARAM_0;
92     if (stat(fileName, &fileStatus) == ZEROVAL) {
93         fileSize = static_cast<size_t>(fileStatus.st_size);
94     } else {
95         result = nullptr;
96         napi_create_int32(env, backParam, &result);
97         return result;
98     }
99     getSource = OH_AVSource_CreateWithFD(fd, PARAM_0, fileSize);
100     if (getSource != nullptr) {
101         checkParam = OH_AVSource_Destroy(getSource);
102         if (checkParam == AV_ERR_OK) {
103             backParam = SUCCESS;
104         }
105     }
106     close(fd);
107     napi_create_int32(env, backParam, &result);
108     return result;
109 }
110 
AVSourceGetSourceFormat(napi_env env,napi_callback_info info)111 static napi_value AVSourceGetSourceFormat(napi_env env, napi_callback_info info)
112 {
113     int backParam = FAIL;
114     OH_AVSource *getSource = nullptr;
115     OH_AVFormat *checkParam = nullptr;
116     char fileName[] = {"/data/storage/el2/base/haps/entry_test/files/demo1.mp4"};
117     int fd = open(fileName, O_RDONLY, PARAM_0666);
118     LOG("AVSourceGetSourceFormat fd is %{public}d", fd);
119     struct stat fileStatus {};
120     size_t fileSize = PARAM_0;
121     if (stat(fileName, &fileStatus) == ZEROVAL) {
122         fileSize = static_cast<size_t>(fileStatus.st_size);
123     } else {
124         napi_value result = nullptr;
125         napi_create_int32(env, backParam, &result);
126         return result;
127     }
128     getSource = OH_AVSource_CreateWithFD(fd, PARAM_0, fileSize);
129     if (getSource != nullptr) {
130         checkParam = OH_AVSource_GetSourceFormat(getSource);
131     }
132     if (checkParam != nullptr) {
133         backParam = SUCCESS;
134     }
135     OH_AVSource_Destroy(getSource);
136     close(fd);
137     napi_value result = nullptr;
138     napi_create_int32(env, backParam, &result);
139     return result;
140 }
141 
GetFileSize(const char * fileName)142 size_t GetFileSize(const char *fileName)
143 {
144     if (fileName == nullptr) {
145         return 0;
146     }
147     struct stat statbuf;
148     stat(fileName, &statbuf);
149     size_t filesize = statbuf.st_size;
150     return filesize;
151 }
152 
AVSourceGetTrackFormat(napi_env env,napi_callback_info info)153 static napi_value AVSourceGetTrackFormat(napi_env env, napi_callback_info info)
154 {
155     int backParam = FAIL;
156     OH_AVSource *getSource = nullptr;
157     OH_AVFormat *checkParam = nullptr;
158     napi_value result = nullptr;
159     char fileName[] = {"/data/storage/el2/base/haps/entry_test/files/demo1.mp4"};
160     int fd = open(fileName, O_RDONLY, PARAM_0666);
161     LOG("AVSourceGetTrackFormat fd is %{public}d", fd);
162     struct stat fileStatus {};
163     size_t fileSize = PARAM_0;
164     if (stat(fileName, &fileStatus) == ZEROVAL) {
165         fileSize = static_cast<size_t>(fileStatus.st_size);
166     } else {
167         backParam = 1;
168     }
169     getSource = OH_AVSource_CreateWithFD(fd, PARAM_0, fileSize);
170     if (getSource != nullptr) {
171         checkParam = OH_AVSource_GetTrackFormat(getSource, PARAM_0);
172         if (checkParam != nullptr) {
173             backParam = SUCCESS;
174         }
175     }
176     OH_AVSource_Destroy(getSource);
177     close(fd);
178     napi_create_int32(env, backParam, &result);
179     return result;
180 }
181 
182 EXTERN_C_START
Init(napi_env env,napi_value exports)183 static napi_value Init(napi_env env, napi_value exports) {
184     napi_property_descriptor desc[] = {
185         {"OH_AVSource_CreateWithURI", nullptr, AVSourceCreateWithURI, nullptr, nullptr, nullptr, napi_default, nullptr},
186         {"OH_AVSource_CreateWithFD", nullptr, AVSourceCreateWithFD, nullptr, nullptr, nullptr, napi_default, nullptr},
187         {"OH_AVSource_Destroy", nullptr, AVSourceDestroy, nullptr, nullptr, nullptr, napi_default, nullptr},
188         {"OH_AVSource_GetSourceFormat", nullptr, AVSourceGetSourceFormat, nullptr, nullptr, nullptr, napi_default,
189          nullptr},
190         {"OH_AVSource_GetTrackFormat", nullptr, AVSourceGetTrackFormat, nullptr, nullptr, nullptr, napi_default,
191          nullptr},
192     };
193     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
194     return exports;
195 }
196 EXTERN_C_END
197 
198 static napi_module demoModule = {
199     .nm_version = 1,
200     .nm_flags = 0,
201     .nm_filename = nullptr,
202     .nm_register_func = Init,
203     .nm_modname = "libmediaavsourcendk",
204     .nm_priv = ((void *)0),
205     .reserved = {0},
206 };
207 
RegisterModule(void)208 extern "C" __attribute__((constructor)) void RegisterModule(void) {
209     napi_module_register(&demoModule);
210 }
211