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