• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "movingphoto_napi.h"
17 
18 #include "ext_napi_utils.h"
19 #include "movingphoto_controller.h"
20 #include "movingphoto_model_ng.h"
21 
22 #include "base/utils/utils.h"
23 #include "core/pipeline/pipeline_base.h"
24 #include "base/resource/data_provider_manager.h"
25 
26 extern const char _binary_multimedia_movingphotoview_js_start[];
27 extern const char _binary_multimedia_movingphotoview_abc_start[];
28 #if !defined(IOS_PLATFORM)
29 extern const char _binary_multimedia_movingphotoview_js_end[];
30 extern const char _binary_multimedia_movingphotoview_abc_end[];
31 #else
32 extern const char* _binary_multimedia_movingphotoview_js_end;
33 extern const char* _binary_multimedia_movingphotoview_abc_end;
34 #endif
35 
36 namespace OHOS::Ace {
37 namespace {
38 static constexpr size_t MAX_ARG_NUM = 10;
39 static constexpr int32_t ARG_NUM_ONE = 1;
40 static constexpr int32_t ARG_NUM_TWO = 2;
41 static constexpr int32_t PARAM_INDEX_ZERO = 0;
42 static constexpr int32_t PARAM_INDEX_ONE = 1;
43 } // namespace
44 
45 std::unique_ptr<NG::MovingPhotoModelNG> NG::MovingPhotoModelNG::instance_ = nullptr;
46 std::mutex NG::MovingPhotoModelNG::mutex_;
47 
GetInstance()48 NG::MovingPhotoModelNG* NG::MovingPhotoModelNG::GetInstance()
49 {
50     if (!instance_) {
51         std::lock_guard<std::mutex> lock(mutex_);
52         if (!instance_) {
53             instance_.reset(new NG::MovingPhotoModelNG());
54         }
55     }
56     return instance_.get();
57 }
58 
JsCreate(napi_env env,napi_callback_info info)59 napi_value JsCreate(napi_env env, napi_callback_info info)
60 {
61     size_t argc = MAX_ARG_NUM;
62     napi_value argv[MAX_ARG_NUM] = { nullptr };
63     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
64     NAPI_ASSERT(env, argc >= ARG_NUM_ONE, "Wrong number of arguments");
65 
66     if (!ExtNapiUtils::CheckTypeForNapiValue(env, argv[0], napi_object)) {
67         return ExtNapiUtils::CreateNull(env);
68     }
69 
70     napi_value jsController = nullptr;
71     NG::MovingPhotoController* controller = nullptr;
72     napi_get_named_property(env, argv[0], "controller", &jsController);
73     if (ExtNapiUtils::CheckTypeForNapiValue(env, jsController, napi_object)) {
74         napi_unwrap(env, jsController, (void**)&controller);
75     }
76     NG::MovingPhotoModelNG::GetInstance()->Create(Referenced::Claim(controller));
77 
78     napi_value jsData = nullptr;
79     napi_get_named_property(env, argv[0], "movingPhoto", &jsData);
80     if (!ExtNapiUtils::CheckTypeForNapiValue(env, jsData, napi_object)) {
81         return ExtNapiUtils::CreateNull(env);
82     }
83 
84     napi_value getUri = nullptr;
85     napi_get_named_property(env, jsData, "getUri", &getUri);
86     if (!ExtNapiUtils::CheckTypeForNapiValue(env, getUri, napi_function)) {
87         return ExtNapiUtils::CreateNull(env);
88     }
89     napi_value imageUri;
90     napi_call_function(env, jsData, getUri, 0, nullptr, &imageUri);
91     std::string imageUriStr = ExtNapiUtils::GetStringFromValueUtf8(env, imageUri);
92     NG::MovingPhotoModelNG::GetInstance()->SetImageSrc(imageUriStr);
93 
94     return ExtNapiUtils::CreateNull(env);
95 }
96 
JsMuted(napi_env env,napi_callback_info info)97 napi_value JsMuted(napi_env env, napi_callback_info info)
98 {
99     size_t argc = MAX_ARG_NUM;
100     napi_value argv[MAX_ARG_NUM] = { nullptr };
101     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
102     NAPI_ASSERT(env, argc >= ARG_NUM_ONE, "Wrong number of arguments");
103 
104     bool muted = false;
105     if (ExtNapiUtils::CheckTypeForNapiValue(env, argv[0], napi_boolean)) {
106         muted = ExtNapiUtils::GetBool(env, argv[0]);
107     }
108     NG::MovingPhotoModelNG::GetInstance()->SetMuted(muted);
109 
110     return ExtNapiUtils::CreateNull(env);
111 }
112 
JsObjectFit(napi_env env,napi_callback_info info)113 napi_value JsObjectFit(napi_env env, napi_callback_info info)
114 {
115     size_t argc = MAX_ARG_NUM;
116     napi_value argv[MAX_ARG_NUM] = { nullptr };
117     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
118     NAPI_ASSERT(env, argc >= ARG_NUM_ONE, "Wrong number of arguments");
119 
120     auto objectFit = ImageFit::COVER;
121     if (ExtNapiUtils::CheckTypeForNapiValue(env, argv[0], napi_number)) {
122         objectFit = static_cast<ImageFit>(ExtNapiUtils::GetCInt32(env, argv[0]));
123     }
124     NG::MovingPhotoModelNG::GetInstance()->SetObjectFit(objectFit);
125 
126     return ExtNapiUtils::CreateNull(env);
127 }
128 
JsOnComplete(napi_env env,napi_callback_info info)129 napi_value JsOnComplete(napi_env env, napi_callback_info info)
130 {
131     size_t argc = MAX_ARG_NUM;
132     napi_value thisVal = nullptr;
133     napi_value argv[MAX_ARG_NUM] = { nullptr };
134     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVal, nullptr));
135     NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
136     if (!ExtNapiUtils::CheckTypeForNapiValue(env, argv[0], napi_function)) {
137         return ExtNapiUtils::CreateNull(env);
138     }
139     auto asyncEvent = std::make_shared<NapiAsyncEvent>(env, argv[0]);
140     auto onComplete = [asyncEvent]() {
141         asyncEvent->Call(0, nullptr);
142     };
143     NG::MovingPhotoModelNG::GetInstance()->SetOnComplete(std::move(onComplete));
144     return ExtNapiUtils::CreateNull(env);
145 }
146 
JsOnStart(napi_env env,napi_callback_info info)147 napi_value JsOnStart(napi_env env, napi_callback_info info)
148 {
149     size_t argc = MAX_ARG_NUM;
150     napi_value thisVal = nullptr;
151     napi_value argv[MAX_ARG_NUM] = { nullptr };
152     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVal, nullptr));
153     NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
154     if (!ExtNapiUtils::CheckTypeForNapiValue(env, argv[0], napi_function)) {
155         return ExtNapiUtils::CreateNull(env);
156     }
157     auto asyncEvent = std::make_shared<NapiAsyncEvent>(env, argv[0]);
158     auto onStart = [asyncEvent]() {
159         asyncEvent->Call(0, nullptr);
160     };
161     NG::MovingPhotoModelNG::GetInstance()->SetOnStart(std::move(onStart));
162     return ExtNapiUtils::CreateNull(env);
163 }
164 
JsOnStop(napi_env env,napi_callback_info info)165 napi_value JsOnStop(napi_env env, napi_callback_info info)
166 {
167     size_t argc = MAX_ARG_NUM;
168     napi_value thisVal = nullptr;
169     napi_value argv[MAX_ARG_NUM] = { nullptr };
170     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVal, nullptr));
171     NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
172     if (!ExtNapiUtils::CheckTypeForNapiValue(env, argv[0], napi_function)) {
173         return ExtNapiUtils::CreateNull(env);
174     }
175     auto asyncEvent = std::make_shared<NapiAsyncEvent>(env, argv[0]);
176     auto onStop = [asyncEvent]() {
177         asyncEvent->Call(0, nullptr);
178     };
179     NG::MovingPhotoModelNG::GetInstance()->SetOnStop(std::move(onStop));
180     return ExtNapiUtils::CreateNull(env);
181 }
182 
JsOnPause(napi_env env,napi_callback_info info)183 napi_value JsOnPause(napi_env env, napi_callback_info info)
184 {
185     size_t argc = MAX_ARG_NUM;
186     napi_value thisVal = nullptr;
187     napi_value argv[MAX_ARG_NUM] = { nullptr };
188     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVal, nullptr));
189     NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
190     if (!ExtNapiUtils::CheckTypeForNapiValue(env, argv[0], napi_function)) {
191         return ExtNapiUtils::CreateNull(env);
192     }
193     auto asyncEvent = std::make_shared<NapiAsyncEvent>(env, argv[0]);
194     auto onPause = [asyncEvent]() {
195         asyncEvent->Call(0, nullptr);
196     };
197     NG::MovingPhotoModelNG::GetInstance()->SetOnPause(std::move(onPause));
198     return ExtNapiUtils::CreateNull(env);
199 }
200 
JsOnFinish(napi_env env,napi_callback_info info)201 napi_value JsOnFinish(napi_env env, napi_callback_info info)
202 {
203     size_t argc = MAX_ARG_NUM;
204     napi_value thisVal = nullptr;
205     napi_value argv[MAX_ARG_NUM] = { nullptr };
206     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVal, nullptr));
207     NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
208     if (!ExtNapiUtils::CheckTypeForNapiValue(env, argv[0], napi_function)) {
209         return ExtNapiUtils::CreateNull(env);
210     }
211     auto asyncEvent = std::make_shared<NapiAsyncEvent>(env, argv[0]);
212     auto onFinish = [asyncEvent]() {
213         asyncEvent->Call(0, nullptr);
214     };
215     NG::MovingPhotoModelNG::GetInstance()->SetOnFinish(std::move(onFinish));
216     return ExtNapiUtils::CreateNull(env);
217 }
218 
JsOnError(napi_env env,napi_callback_info info)219 napi_value JsOnError(napi_env env, napi_callback_info info)
220 {
221     size_t argc = MAX_ARG_NUM;
222     napi_value thisVal = nullptr;
223     napi_value argv[MAX_ARG_NUM] = { nullptr };
224     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVal, nullptr));
225     NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
226     if (!ExtNapiUtils::CheckTypeForNapiValue(env, argv[0], napi_function)) {
227         return ExtNapiUtils::CreateNull(env);
228     }
229     auto asyncEvent = std::make_shared<NapiAsyncEvent>(env, argv[0]);
230     auto onError = [asyncEvent]() {
231         asyncEvent->Call(0, nullptr);
232     };
233     NG::MovingPhotoModelNG::GetInstance()->SetOnError(std::move(onError));
234     return ExtNapiUtils::CreateNull(env);
235 }
236 
InitView(napi_env env,napi_value exports)237 napi_value InitView(napi_env env, napi_value exports)
238 {
239     static napi_property_descriptor desc[] = {
240         DECLARE_NAPI_FUNCTION("create", JsCreate),
241         DECLARE_NAPI_FUNCTION("muted", JsMuted),
242         DECLARE_NAPI_FUNCTION("objectFit", JsObjectFit),
243         DECLARE_NAPI_FUNCTION("onComplete", JsOnComplete),
244         DECLARE_NAPI_FUNCTION("onStart", JsOnStart),
245         DECLARE_NAPI_FUNCTION("onStop", JsOnStop),
246         DECLARE_NAPI_FUNCTION("onPause", JsOnPause),
247         DECLARE_NAPI_FUNCTION("onFinish", JsOnFinish),
248         DECLARE_NAPI_FUNCTION("onError", JsOnError),
249         DECLARE_NAPI_FUNCTION("autoPlayPeriod", JsAutoPlayPeriod),
250         DECLARE_NAPI_FUNCTION("autoPlay", JsAutoPlay),
251         DECLARE_NAPI_FUNCTION("repeatPlay", JsRepeatPlay),
252     };
253     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
254     return exports;
255 }
256 
StartPlayback(napi_env env,napi_callback_info info)257 napi_value StartPlayback(napi_env env, napi_callback_info info)
258 {
259     napi_value thisVar = nullptr;
260     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, NULL));
261     NG::MovingPhotoController* controller = nullptr;
262     napi_unwrap(env, thisVar, (void**)&controller);
263     if (controller == nullptr) {
264         return ExtNapiUtils::CreateNull(env);
265     }
266     controller->StartPlayback();
267     return ExtNapiUtils::CreateNull(env);
268 }
269 
StopPlayback(napi_env env,napi_callback_info info)270 napi_value StopPlayback(napi_env env, napi_callback_info info)
271 {
272     napi_value thisVar = nullptr;
273     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, NULL));
274     NG::MovingPhotoController* controller = nullptr;
275     napi_unwrap(env, thisVar, (void**)&controller);
276     if (controller == nullptr) {
277         return ExtNapiUtils::CreateNull(env);
278     }
279     controller->StopPlayback();
280     return ExtNapiUtils::CreateNull(env);
281 }
282 
MovingPhotoControllerConstructor(napi_env env,napi_callback_info info)283 napi_value MovingPhotoControllerConstructor(napi_env env, napi_callback_info info)
284 {
285     napi_value thisVar = nullptr;
286     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
287     auto controller = AceType::MakeRefPtr<NG::MovingPhotoController>();
288     if (controller == nullptr) {
289         return ExtNapiUtils::CreateNull(env);
290     }
291     controller->IncRefCount();
292     napi_wrap(
293         env, thisVar, AceType::RawPtr(controller),
294         [](napi_env env, void* data, void* hint) {
295             auto* controller = reinterpret_cast<NG::MovingPhotoController*>(data);
296             controller->DecRefCount();
297         },
298         nullptr, nullptr);
299     return thisVar;
300 }
301 
InitController(napi_env env,napi_value exports)302 napi_value InitController(napi_env env, napi_value exports)
303 {
304     napi_value movingphotoControllerClass = nullptr;
305     napi_property_descriptor properties[] = {
306         DECLARE_NAPI_FUNCTION("startPlayback", StartPlayback),
307         DECLARE_NAPI_FUNCTION("stopPlayback", StopPlayback),
308     };
309     NAPI_CALL(env, napi_define_class(env, "MovingPhotoViewController", NAPI_AUTO_LENGTH,
310         MovingPhotoControllerConstructor, nullptr, sizeof(properties) / sizeof(*properties), properties,
311         &movingphotoControllerClass));
312     NAPI_CALL(env, napi_set_named_property(env, exports, "MovingPhotoViewController", movingphotoControllerClass));
313     return exports;
314 }
315 
ExportMovingPhoto(napi_env env,napi_value exports)316 napi_value ExportMovingPhoto(napi_env env, napi_value exports)
317 {
318     InitView(env, exports);
319     InitController(env, exports);
320     return exports;
321 }
322 
JsAutoPlayPeriod(napi_env env,napi_callback_info info)323 napi_value JsAutoPlayPeriod(napi_env env, napi_callback_info info)
324 {
325     size_t argc = MAX_ARG_NUM;
326     napi_value argv[MAX_ARG_NUM] = { nullptr };
327     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
328     NAPI_ASSERT(env, argc >= ARG_NUM_TWO, "Wrong number of arguments");
329 
330     int64_t startTime = 0;
331     if (ExtNapiUtils::CheckTypeForNapiValue(env, argv[PARAM_INDEX_ZERO], napi_number)) {
332         startTime = ExtNapiUtils::GetCInt64(env, argv[PARAM_INDEX_ZERO]);
333     }
334     int64_t endTime = 0;
335     if (ExtNapiUtils::CheckTypeForNapiValue(env, argv[PARAM_INDEX_ONE], napi_number)) {
336         endTime = ExtNapiUtils::GetCInt64(env, argv[PARAM_INDEX_ONE]);
337     }
338     NG::MovingPhotoModelNG::GetInstance()->AutoPlayPeriod(startTime, endTime);
339 
340     return ExtNapiUtils::CreateNull(env);
341 }
342 
JsAutoPlay(napi_env env,napi_callback_info info)343 napi_value JsAutoPlay(napi_env env, napi_callback_info info)
344 {
345     size_t argc = MAX_ARG_NUM;
346     napi_value argv[MAX_ARG_NUM] = { nullptr };
347     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
348     NAPI_ASSERT(env, argc >= ARG_NUM_ONE, "Wrong number of arguments");
349 
350     bool isAutoPlay = false;
351     if (ExtNapiUtils::CheckTypeForNapiValue(env, argv[PARAM_INDEX_ZERO], napi_boolean)) {
352         isAutoPlay = ExtNapiUtils::GetBool(env, argv[PARAM_INDEX_ZERO]);
353     }
354     NG::MovingPhotoModelNG::GetInstance()->AutoPlay(isAutoPlay);
355 
356     return ExtNapiUtils::CreateNull(env);
357 }
358 
JsRepeatPlay(napi_env env,napi_callback_info info)359 napi_value JsRepeatPlay(napi_env env, napi_callback_info info)
360 {
361     size_t argc = MAX_ARG_NUM;
362     napi_value argv[MAX_ARG_NUM] = { nullptr };
363     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
364     NAPI_ASSERT(env, argc >= ARG_NUM_ONE, "Wrong number of arguments");
365 
366     bool isRepeatPlay = false;
367     if (ExtNapiUtils::CheckTypeForNapiValue(env, argv[PARAM_INDEX_ZERO], napi_boolean)) {
368         isRepeatPlay = ExtNapiUtils::GetBool(env, argv[PARAM_INDEX_ZERO]);
369     }
370     NG::MovingPhotoModelNG::GetInstance()->RepeatPlay(isRepeatPlay);
371 
372     return ExtNapiUtils::CreateNull(env);
373 }
374 
375 } // namespace OHOS::Ace
376 
NAPI_multimedia_movingphotoview_GetJSCode(const char ** buf,int * bufLen)377 extern "C" __attribute__((visibility("default"))) void NAPI_multimedia_movingphotoview_GetJSCode(
378     const char** buf, int* bufLen)
379 {
380     if (buf != nullptr) {
381         *buf = _binary_multimedia_movingphotoview_js_start;
382     }
383 
384     if (bufLen != nullptr) {
385         *bufLen = _binary_multimedia_movingphotoview_js_end - _binary_multimedia_movingphotoview_js_start;
386     }
387 }
388 
389 // multimedia_movingphotoview JS register
NAPI_multimedia_movingphotoview_GetABCCode(const char ** buf,int * buflen)390 extern "C" __attribute__((visibility("default"))) void NAPI_multimedia_movingphotoview_GetABCCode(
391     const char** buf, int* buflen)
392 {
393     if (buf != nullptr) {
394         *buf = _binary_multimedia_movingphotoview_abc_start;
395     }
396     if (buflen != nullptr) {
397         *buflen = _binary_multimedia_movingphotoview_abc_end - _binary_multimedia_movingphotoview_abc_start;
398     }
399 }
400 
401 static napi_module movingphotoModule  = {
402     .nm_version = 1,
403     .nm_flags = 0,
404     .nm_filename = nullptr,
405     .nm_register_func = OHOS::Ace::ExportMovingPhoto,
406     .nm_modname = "multimedia.movingphotoview",
407     .nm_priv = ((void*)0),
408     .reserved = { 0 },
409 };
410 
RegisterModuleMovingPhoto()411 extern "C" __attribute__((constructor)) void RegisterModuleMovingPhoto()
412 {
413     napi_module_register(&movingphotoModule);
414 }
415