• 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_model_ng.h"
20 
21 extern const char _binary_multimedia_movingphotoview_js_start[];
22 extern const char _binary_multimedia_movingphotoview_abc_start[];
23 #if !defined(IOS_PLATFORM)
24 extern const char _binary_multimedia_movingphotoview_js_end[];
25 extern const char _binary_multimedia_movingphotoview_abc_end[];
26 #else
27 extern const char* _binary_multimedia_movingphotoview_js_end;
28 extern const char* _binary_multimedia_movingphotoview_abc_end;
29 #endif
30 
31 namespace OHOS::Ace {
32 namespace {
33 static constexpr size_t MAX_ARG_NUM = 10;
34 static constexpr int32_t ARG_NUM_ONE = 1;
35 static constexpr int32_t ARG_NUM_TWO = 2;
36 static constexpr int32_t PARAM_INDEX_ZERO = 0;
37 static constexpr int32_t PARAM_INDEX_ONE = 1;
38 static constexpr int32_t US_CONVERT = 1000;
39 } // namespace
40 
41 std::unique_ptr<NG::MovingPhotoModelNG> NG::MovingPhotoModelNG::instance_ = nullptr;
42 std::mutex NG::MovingPhotoModelNG::mutex_;
43 
GetInstance()44 NG::MovingPhotoModelNG* NG::MovingPhotoModelNG::GetInstance()
45 {
46     if (!instance_) {
47         std::lock_guard<std::mutex> lock(mutex_);
48         if (!instance_) {
49             instance_.reset(new NG::MovingPhotoModelNG());
50         }
51     }
52     return instance_.get();
53 }
54 
JsCreate(napi_env env,napi_callback_info info)55 napi_value JsCreate(napi_env env, napi_callback_info info)
56 {
57     size_t argc = MAX_ARG_NUM;
58     napi_value argv[MAX_ARG_NUM] = { nullptr };
59     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
60     NAPI_ASSERT(env, argc >= ARG_NUM_ONE, "Wrong number of arguments");
61 
62     if (!ExtNapiUtils::CheckTypeForNapiValue(env, argv[0], napi_object)) {
63         return ExtNapiUtils::CreateNull(env);
64     }
65 
66     napi_value jsController = nullptr;
67     NG::MovingPhotoController* controller = nullptr;
68     napi_get_named_property(env, argv[0], "controller", &jsController);
69     if (ExtNapiUtils::CheckTypeForNapiValue(env, jsController, napi_object)) {
70         napi_unwrap(env, jsController, (void**)&controller);
71     }
72     NG::MovingPhotoModelNG::GetInstance()->Create(Referenced::Claim(controller));
73 
74     napi_value jsData = nullptr;
75     napi_get_named_property(env, argv[0], "movingPhoto", &jsData);
76     if (!ExtNapiUtils::CheckTypeForNapiValue(env, jsData, napi_object)) {
77         TAG_LOGE(AceLogTag::ACE_MOVING_PHOTO, "when create movingphoto is null.");
78         return ExtNapiUtils::CreateNull(env);
79     }
80 
81     napi_value jsImageAIOptions = nullptr;
82     napi_get_named_property(env, argv[0], "imageAIOptions", &jsImageAIOptions);
83     NG::MovingPhotoModelNG::GetInstance()->SetImageAIOptions(jsImageAIOptions);
84 
85     SetDynamicRangeMode(env, argv[0]);
86     SetMovingPhotoFormat(env, argv[0]);
87     SetWaterMask(env, argv[0]);
88 
89     napi_value getUri = nullptr;
90     napi_get_named_property(env, jsData, "getUri", &getUri);
91     if (!ExtNapiUtils::CheckTypeForNapiValue(env, getUri, napi_function)) {
92         TAG_LOGE(AceLogTag::ACE_MOVING_PHOTO, "when create getUri type is error.");
93         return ExtNapiUtils::CreateNull(env);
94     }
95     napi_value imageUri;
96     napi_call_function(env, jsData, getUri, 0, nullptr, &imageUri);
97     std::string imageUriStr = ExtNapiUtils::GetStringFromValueUtf8(env, imageUri);
98     NG::MovingPhotoModelNG::GetInstance()->SetImageSrc(imageUriStr);
99 
100     return ExtNapiUtils::CreateNull(env);
101 }
102 
SetWaterMask(napi_env env,napi_value object)103 napi_value SetWaterMask(napi_env env, napi_value object)
104 {
105     napi_value jsWaterMask = nullptr;
106     napi_get_named_property(env, object, "playWithMask", &jsWaterMask);
107     bool waterMask = false;
108     if (ExtNapiUtils::CheckTypeForNapiValue(env, jsWaterMask, napi_boolean)) {
109         waterMask = ExtNapiUtils::GetBool(env, jsWaterMask);
110         NG::MovingPhotoModelNG::GetInstance()->SetWaterMask(waterMask);
111     }
112     return ExtNapiUtils::CreateNull(env);
113 }
114 
SetMovingPhotoFormat(napi_env env,napi_value object)115 napi_value SetMovingPhotoFormat(napi_env env, napi_value object)
116 {
117     napi_value jsMovingPhotoFormat = nullptr;
118     napi_get_named_property(env, object, "movingPhotoFormat", &jsMovingPhotoFormat);
119     auto format = MovingPhotoFormat::UNKNOWN;
120     if (ExtNapiUtils::CheckTypeForNapiValue(env, jsMovingPhotoFormat, napi_number)) {
121         format = static_cast<MovingPhotoFormat>(ExtNapiUtils::GetCInt32(env, jsMovingPhotoFormat));
122         NG::MovingPhotoModelNG::GetInstance()->SetMovingPhotoFormat(format);
123     }
124     return ExtNapiUtils::CreateNull(env);
125 }
126 
SetDynamicRangeMode(napi_env env,napi_value object)127 napi_value SetDynamicRangeMode(napi_env env, napi_value object)
128 {
129     napi_value jsDynamicRangeMode = nullptr;
130     napi_get_named_property(env, object, "dynamicRangeMode", &jsDynamicRangeMode);
131     auto rangeMode = DynamicRangeMode::HIGH;
132     if (ExtNapiUtils::CheckTypeForNapiValue(env, jsDynamicRangeMode, napi_number)) {
133         rangeMode = static_cast<DynamicRangeMode>(ExtNapiUtils::GetCInt32(env, jsDynamicRangeMode));
134         NG::MovingPhotoModelNG::GetInstance()->SetDynamicRangeMode(rangeMode);
135     }
136     return ExtNapiUtils::CreateNull(env);
137 }
138 
JsMuted(napi_env env,napi_callback_info info)139 napi_value JsMuted(napi_env env, napi_callback_info info)
140 {
141     size_t argc = MAX_ARG_NUM;
142     napi_value argv[MAX_ARG_NUM] = { nullptr };
143     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
144     NAPI_ASSERT(env, argc >= ARG_NUM_ONE, "Wrong number of arguments");
145 
146     bool muted = false;
147     if (ExtNapiUtils::CheckTypeForNapiValue(env, argv[0], napi_boolean)) {
148         muted = ExtNapiUtils::GetBool(env, argv[0]);
149     }
150     NG::MovingPhotoModelNG::GetInstance()->SetMuted(muted);
151 
152     return ExtNapiUtils::CreateNull(env);
153 }
154 
JsObjectFit(napi_env env,napi_callback_info info)155 napi_value JsObjectFit(napi_env env, napi_callback_info info)
156 {
157     size_t argc = MAX_ARG_NUM;
158     napi_value argv[MAX_ARG_NUM] = { nullptr };
159     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
160     NAPI_ASSERT(env, argc >= ARG_NUM_ONE, "Wrong number of arguments");
161 
162     auto objectFit = ImageFit::COVER;
163     if (ExtNapiUtils::CheckTypeForNapiValue(env, argv[0], napi_number)) {
164         objectFit = static_cast<ImageFit>(ExtNapiUtils::GetCInt32(env, argv[0]));
165     }
166     NG::MovingPhotoModelNG::GetInstance()->SetObjectFit(objectFit);
167 
168     return ExtNapiUtils::CreateNull(env);
169 }
170 
JsOnComplete(napi_env env,napi_callback_info info)171 napi_value JsOnComplete(napi_env env, napi_callback_info info)
172 {
173     size_t argc = MAX_ARG_NUM;
174     napi_value thisVal = nullptr;
175     napi_value argv[MAX_ARG_NUM] = { nullptr };
176     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVal, nullptr));
177     NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
178     if (!ExtNapiUtils::CheckTypeForNapiValue(env, argv[0], napi_function)) {
179         return ExtNapiUtils::CreateNull(env);
180     }
181     auto asyncEvent = std::make_shared<NapiAsyncEvent>(env, argv[0]);
182     auto onComplete = [asyncEvent]() {
183         asyncEvent->Call(0, nullptr);
184     };
185     NG::MovingPhotoModelNG::GetInstance()->SetOnComplete(std::move(onComplete));
186     return ExtNapiUtils::CreateNull(env);
187 }
188 
JsOnStart(napi_env env,napi_callback_info info)189 napi_value JsOnStart(napi_env env, napi_callback_info info)
190 {
191     size_t argc = MAX_ARG_NUM;
192     napi_value thisVal = nullptr;
193     napi_value argv[MAX_ARG_NUM] = { nullptr };
194     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVal, nullptr));
195     NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
196     if (!ExtNapiUtils::CheckTypeForNapiValue(env, argv[0], napi_function)) {
197         return ExtNapiUtils::CreateNull(env);
198     }
199     auto asyncEvent = std::make_shared<NapiAsyncEvent>(env, argv[0]);
200     auto onStart = [asyncEvent]() {
201         asyncEvent->Call(0, nullptr);
202     };
203     NG::MovingPhotoModelNG::GetInstance()->SetOnStart(std::move(onStart));
204     return ExtNapiUtils::CreateNull(env);
205 }
206 
JsOnStop(napi_env env,napi_callback_info info)207 napi_value JsOnStop(napi_env env, napi_callback_info info)
208 {
209     size_t argc = MAX_ARG_NUM;
210     napi_value thisVal = nullptr;
211     napi_value argv[MAX_ARG_NUM] = { nullptr };
212     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVal, nullptr));
213     NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
214     if (!ExtNapiUtils::CheckTypeForNapiValue(env, argv[0], napi_function)) {
215         return ExtNapiUtils::CreateNull(env);
216     }
217     auto asyncEvent = std::make_shared<NapiAsyncEvent>(env, argv[0]);
218     auto onStop = [asyncEvent]() {
219         asyncEvent->Call(0, nullptr);
220     };
221     NG::MovingPhotoModelNG::GetInstance()->SetOnStop(std::move(onStop));
222     return ExtNapiUtils::CreateNull(env);
223 }
224 
JsOnPause(napi_env env,napi_callback_info info)225 napi_value JsOnPause(napi_env env, napi_callback_info info)
226 {
227     size_t argc = MAX_ARG_NUM;
228     napi_value thisVal = nullptr;
229     napi_value argv[MAX_ARG_NUM] = { nullptr };
230     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVal, nullptr));
231     NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
232     if (!ExtNapiUtils::CheckTypeForNapiValue(env, argv[0], napi_function)) {
233         return ExtNapiUtils::CreateNull(env);
234     }
235     auto asyncEvent = std::make_shared<NapiAsyncEvent>(env, argv[0]);
236     auto onPause = [asyncEvent]() {
237         asyncEvent->Call(0, nullptr);
238     };
239     NG::MovingPhotoModelNG::GetInstance()->SetOnPause(std::move(onPause));
240     return ExtNapiUtils::CreateNull(env);
241 }
242 
JsOnFinish(napi_env env,napi_callback_info info)243 napi_value JsOnFinish(napi_env env, napi_callback_info info)
244 {
245     size_t argc = MAX_ARG_NUM;
246     napi_value thisVal = nullptr;
247     napi_value argv[MAX_ARG_NUM] = { nullptr };
248     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVal, nullptr));
249     NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
250     if (!ExtNapiUtils::CheckTypeForNapiValue(env, argv[0], napi_function)) {
251         return ExtNapiUtils::CreateNull(env);
252     }
253     auto asyncEvent = std::make_shared<NapiAsyncEvent>(env, argv[0]);
254     auto onFinish = [asyncEvent]() {
255         asyncEvent->Call(0, nullptr);
256     };
257     NG::MovingPhotoModelNG::GetInstance()->SetOnFinish(std::move(onFinish));
258     return ExtNapiUtils::CreateNull(env);
259 }
260 
JsOnError(napi_env env,napi_callback_info info)261 napi_value JsOnError(napi_env env, napi_callback_info info)
262 {
263     size_t argc = MAX_ARG_NUM;
264     napi_value thisVal = nullptr;
265     napi_value argv[MAX_ARG_NUM] = { nullptr };
266     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVal, nullptr));
267     NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
268     if (!ExtNapiUtils::CheckTypeForNapiValue(env, argv[0], napi_function)) {
269         return ExtNapiUtils::CreateNull(env);
270     }
271     auto asyncEvent = std::make_shared<NapiAsyncEvent>(env, argv[0]);
272     auto onError = [asyncEvent]() {
273         asyncEvent->Call(0, nullptr);
274     };
275     NG::MovingPhotoModelNG::GetInstance()->SetOnError(std::move(onError));
276     return ExtNapiUtils::CreateNull(env);
277 }
278 
JsOnPrepared(napi_env env,napi_callback_info info)279 napi_value JsOnPrepared(napi_env env, napi_callback_info info)
280 {
281     size_t argc = MAX_ARG_NUM;
282     napi_value thisVal = nullptr;
283     napi_value argv[MAX_ARG_NUM] = { nullptr };
284     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVal, nullptr));
285     NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
286     if (!ExtNapiUtils::CheckTypeForNapiValue(env, argv[0], napi_function)) {
287         return ExtNapiUtils::CreateNull(env);
288     }
289     auto asyncEvent = std::make_shared<NapiAsyncEvent>(env, argv[0]);
290     auto onPrepared = [asyncEvent]() {
291         asyncEvent->Call(0, nullptr);
292     };
293     NG::MovingPhotoModelNG::GetInstance()->SetOnPrepared(std::move(onPrepared));
294     return ExtNapiUtils::CreateNull(env);
295 }
296 
InitView(napi_env env,napi_value exports)297 napi_value InitView(napi_env env, napi_value exports)
298 {
299     static napi_property_descriptor desc[] = {
300         DECLARE_NAPI_FUNCTION("create", JsCreate),
301         DECLARE_NAPI_FUNCTION("muted", JsMuted),
302         DECLARE_NAPI_FUNCTION("objectFit", JsObjectFit),
303         DECLARE_NAPI_FUNCTION("onComplete", JsOnComplete),
304         DECLARE_NAPI_FUNCTION("onStart", JsOnStart),
305         DECLARE_NAPI_FUNCTION("onStop", JsOnStop),
306         DECLARE_NAPI_FUNCTION("onPause", JsOnPause),
307         DECLARE_NAPI_FUNCTION("onFinish", JsOnFinish),
308         DECLARE_NAPI_FUNCTION("onError", JsOnError),
309         DECLARE_NAPI_FUNCTION("onPrepared", JsOnPrepared),
310         DECLARE_NAPI_FUNCTION("autoPlayPeriod", JsAutoPlayPeriod),
311         DECLARE_NAPI_FUNCTION("autoPlay", JsAutoPlay),
312         DECLARE_NAPI_FUNCTION("repeatPlay", JsRepeatPlay),
313         DECLARE_NAPI_FUNCTION("enableAnalyzer", JsEnableAnalyzer),
314         DECLARE_NAPI_FUNCTION("hdrBrightness", JsHdrBrightness),
315         DECLARE_NAPI_FUNCTION("setPlaybackStrategy", JsSetPlaybackStrategy),
316     };
317     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
318     return exports;
319 }
320 
StartPlayback(napi_env env,napi_callback_info info)321 napi_value StartPlayback(napi_env env, napi_callback_info info)
322 {
323     napi_value thisVar = nullptr;
324     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, NULL));
325     NG::MovingPhotoController* controller = nullptr;
326     napi_unwrap(env, thisVar, (void**)&controller);
327     if (controller == nullptr) {
328         return ExtNapiUtils::CreateNull(env);
329     }
330     controller->StartPlayback();
331     return ExtNapiUtils::CreateNull(env);
332 }
333 
StopPlayback(napi_env env,napi_callback_info info)334 napi_value StopPlayback(napi_env env, napi_callback_info info)
335 {
336     napi_value thisVar = nullptr;
337     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, NULL));
338     NG::MovingPhotoController* controller = nullptr;
339     napi_unwrap(env, thisVar, (void**)&controller);
340     if (controller == nullptr) {
341         return ExtNapiUtils::CreateNull(env);
342     }
343     controller->StopPlayback();
344     return ExtNapiUtils::CreateNull(env);
345 }
346 
RefreshMovingPhoto(napi_env env,napi_callback_info info)347 napi_value RefreshMovingPhoto(napi_env env, napi_callback_info info)
348 {
349     napi_value thisVar = nullptr;
350     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, NULL));
351     NG::MovingPhotoController* controller = nullptr;
352     napi_unwrap(env, thisVar, (void**)&controller);
353     if (controller == nullptr) {
354         return ExtNapiUtils::CreateNull(env);
355     }
356     controller->RefreshMovingPhoto();
357     return ExtNapiUtils::CreateNull(env);
358 }
359 
PausePlayback(napi_env env,napi_callback_info info)360 napi_value PausePlayback(napi_env env, napi_callback_info info)
361 {
362     napi_value thisVar = nullptr;
363     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, NULL));
364     NG::MovingPhotoController* controller = nullptr;
365     napi_unwrap(env, thisVar, (void**)&controller);
366     if (controller == nullptr) {
367         return ExtNapiUtils::CreateNull(env);
368     }
369     controller->Pause();
370     return ExtNapiUtils::CreateNull(env);
371 }
372 
Reset(napi_env env,napi_callback_info info)373 napi_value Reset(napi_env env, napi_callback_info info)
374 {
375     napi_value thisVar = nullptr;
376     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, NULL));
377     NG::MovingPhotoController* controller = nullptr;
378     napi_unwrap(env, thisVar, (void**)&controller);
379     if (controller == nullptr) {
380         return ExtNapiUtils::CreateNull(env);
381     }
382     controller->Reset();
383     return ExtNapiUtils::CreateNull(env);
384 }
385 
Restart(napi_env env,napi_callback_info info)386 napi_value Restart(napi_env env, napi_callback_info info)
387 {
388     napi_value thisVar = nullptr;
389     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, NULL));
390     NG::MovingPhotoController* controller = nullptr;
391     napi_unwrap(env, thisVar, (void**)&controller);
392     if (controller == nullptr) {
393         return ExtNapiUtils::CreateNull(env);
394     }
395     controller->Restart();
396     return ExtNapiUtils::CreateNull(env);
397 }
398 
EnableTransition(napi_env env,napi_callback_info info)399 napi_value EnableTransition(napi_env env, napi_callback_info info)
400 {
401     napi_value thisVar = nullptr;
402     size_t argc = MAX_ARG_NUM;
403     napi_value argv[MAX_ARG_NUM] = { nullptr };
404     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
405     NAPI_ASSERT(env, argc >= ARG_NUM_ONE, "Wrong number of arguments");
406     bool enabled = true;
407     if (ExtNapiUtils::CheckTypeForNapiValue(env, argv[PARAM_INDEX_ZERO], napi_boolean)) {
408         enabled = ExtNapiUtils::GetBool(env, argv[PARAM_INDEX_ZERO]);
409     }
410     NG::MovingPhotoController* controller = nullptr;
411     napi_unwrap(env, thisVar, (void**)&controller);
412     if (controller == nullptr) {
413         return ExtNapiUtils::CreateNull(env);
414     }
415     controller->EnableTransition(enabled);
416     return ExtNapiUtils::CreateNull(env);
417 }
418 
SetPlaybackPeriod(napi_env env,napi_callback_info info)419 napi_value SetPlaybackPeriod(napi_env env, napi_callback_info info)
420 {
421     napi_value thisVar = nullptr;
422     size_t argc = MAX_ARG_NUM;
423     napi_value argv[MAX_ARG_NUM] = { nullptr };
424     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
425     NAPI_ASSERT(env, argc >= ARG_NUM_TWO, "Wrong number of arguments");
426     int64_t startTime = 0;
427     if (ExtNapiUtils::CheckTypeForNapiValue(env, argv[PARAM_INDEX_ZERO], napi_number)) {
428         startTime = static_cast<int64_t>(ExtNapiUtils::GetDouble(env, argv[PARAM_INDEX_ZERO]) * US_CONVERT);
429     }
430     int64_t endTime = 0;
431     if (ExtNapiUtils::CheckTypeForNapiValue(env, argv[PARAM_INDEX_ONE], napi_number)) {
432         endTime = static_cast<int64_t>(ExtNapiUtils::GetDouble(env, argv[PARAM_INDEX_ONE]) * US_CONVERT);
433     }
434     NG::MovingPhotoController* controller = nullptr;
435     napi_unwrap(env, thisVar, (void**)&controller);
436     if (controller == nullptr) {
437         return ExtNapiUtils::CreateNull(env);
438     }
439     controller->SetPlaybackPeriod(startTime, endTime);
440     return ExtNapiUtils::CreateNull(env);
441 }
442 
EnableAutoPlay(napi_env env,napi_callback_info info)443 napi_value EnableAutoPlay(napi_env env, napi_callback_info info)
444 {
445     napi_value thisVar = nullptr;
446     size_t argc = MAX_ARG_NUM;
447     napi_value argv[MAX_ARG_NUM] = { nullptr };
448     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
449     NAPI_ASSERT(env, argc >= ARG_NUM_ONE, "Wrong number of arguments");
450     bool enabled = true;
451     if (ExtNapiUtils::CheckTypeForNapiValue(env, argv[PARAM_INDEX_ZERO], napi_boolean)) {
452         enabled = ExtNapiUtils::GetBool(env, argv[PARAM_INDEX_ZERO]);
453     }
454     NG::MovingPhotoController* controller = nullptr;
455     napi_unwrap(env, thisVar, (void**)&controller);
456     if (controller == nullptr) {
457         return ExtNapiUtils::CreateNull(env);
458     }
459     controller->EnableAutoPlay(enabled);
460     return ExtNapiUtils::CreateNull(env);
461 }
462 
NotifyTransition(napi_env env,napi_callback_info info)463 napi_value NotifyTransition(napi_env env, napi_callback_info info)
464 {
465     napi_value thisVar = nullptr;
466     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, NULL));
467     NG::MovingPhotoController* controller = nullptr;
468     napi_unwrap(env, thisVar, (void**)&controller);
469     if (controller == nullptr) {
470         return ExtNapiUtils::CreateNull(env);
471     }
472     controller->NotifyTransition();
473     return ExtNapiUtils::CreateNull(env);
474 }
475 
476 
MovingPhotoControllerConstructor(napi_env env,napi_callback_info info)477 napi_value MovingPhotoControllerConstructor(napi_env env, napi_callback_info info)
478 {
479     napi_value thisVar = nullptr;
480     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
481     auto controller = AceType::MakeRefPtr<NG::MovingPhotoController>();
482     if (controller == nullptr) {
483         return ExtNapiUtils::CreateNull(env);
484     }
485     controller->IncRefCount();
486     napi_wrap(
487         env, thisVar, AceType::RawPtr(controller),
488         [](napi_env env, void* data, void* hint) {
489             auto* controller = reinterpret_cast<NG::MovingPhotoController*>(data);
490             controller->DecRefCount();
491         },
492         nullptr, nullptr);
493     return thisVar;
494 }
495 
InitController(napi_env env,napi_value exports)496 napi_value InitController(napi_env env, napi_value exports)
497 {
498     napi_value movingphotoControllerClass = nullptr;
499     napi_property_descriptor properties[] = {
500         DECLARE_NAPI_FUNCTION("startPlayback", StartPlayback),
501         DECLARE_NAPI_FUNCTION("stopPlayback", StopPlayback),
502         DECLARE_NAPI_FUNCTION("refreshMovingPhoto", RefreshMovingPhoto),
503         DECLARE_NAPI_FUNCTION("pausePlayback", PausePlayback),
504         DECLARE_NAPI_FUNCTION("reset", Reset),
505         DECLARE_NAPI_FUNCTION("restart", Restart),
506         DECLARE_NAPI_FUNCTION("enableTransition", EnableTransition),
507         DECLARE_NAPI_FUNCTION("setPlaybackPeriod", SetPlaybackPeriod),
508         DECLARE_NAPI_FUNCTION("enableAutoPlay", EnableAutoPlay),
509         DECLARE_NAPI_FUNCTION("notifyMovingPhotoTransition", NotifyTransition),
510     };
511     NAPI_CALL(env, napi_define_class(env, "MovingPhotoViewController", NAPI_AUTO_LENGTH,
512         MovingPhotoControllerConstructor, nullptr, sizeof(properties) / sizeof(*properties), properties,
513         &movingphotoControllerClass));
514     NAPI_CALL(env, napi_set_named_property(env, exports, "MovingPhotoViewController", movingphotoControllerClass));
515     return exports;
516 }
517 
ExportMovingPhoto(napi_env env,napi_value exports)518 napi_value ExportMovingPhoto(napi_env env, napi_value exports)
519 {
520     InitView(env, exports);
521     InitController(env, exports);
522     return exports;
523 }
524 
JsAutoPlayPeriod(napi_env env,napi_callback_info info)525 napi_value JsAutoPlayPeriod(napi_env env, napi_callback_info info)
526 {
527     size_t argc = MAX_ARG_NUM;
528     napi_value argv[MAX_ARG_NUM] = { nullptr };
529     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
530     NAPI_ASSERT(env, argc >= ARG_NUM_TWO, "Wrong number of arguments");
531 
532     int64_t startTime = 0;
533     if (ExtNapiUtils::CheckTypeForNapiValue(env, argv[PARAM_INDEX_ZERO], napi_number)) {
534         startTime = static_cast<int64_t>(ExtNapiUtils::GetDouble(env, argv[PARAM_INDEX_ZERO]) * US_CONVERT);
535     }
536     int64_t endTime = 0;
537     if (ExtNapiUtils::CheckTypeForNapiValue(env, argv[PARAM_INDEX_ONE], napi_number)) {
538         endTime = static_cast<int64_t>(ExtNapiUtils::GetDouble(env, argv[PARAM_INDEX_ONE]) * US_CONVERT);
539     }
540     NG::MovingPhotoModelNG::GetInstance()->AutoPlayPeriod(startTime, endTime);
541 
542     return ExtNapiUtils::CreateNull(env);
543 }
544 
JsAutoPlay(napi_env env,napi_callback_info info)545 napi_value JsAutoPlay(napi_env env, napi_callback_info info)
546 {
547     size_t argc = MAX_ARG_NUM;
548     napi_value argv[MAX_ARG_NUM] = { nullptr };
549     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
550     NAPI_ASSERT(env, argc >= ARG_NUM_ONE, "Wrong number of arguments");
551 
552     bool isAutoPlay = false;
553     if (ExtNapiUtils::CheckTypeForNapiValue(env, argv[PARAM_INDEX_ZERO], napi_boolean)) {
554         isAutoPlay = ExtNapiUtils::GetBool(env, argv[PARAM_INDEX_ZERO]);
555     }
556     NG::MovingPhotoModelNG::GetInstance()->AutoPlay(isAutoPlay);
557 
558     return ExtNapiUtils::CreateNull(env);
559 }
560 
JsEnableAnalyzer(napi_env env,napi_callback_info info)561 napi_value JsEnableAnalyzer(napi_env env, napi_callback_info info)
562 {
563     size_t argc = MAX_ARG_NUM;
564     napi_value argv[MAX_ARG_NUM] = { nullptr };
565     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
566     NAPI_ASSERT(env, argc >= ARG_NUM_ONE, "Wrong number of arguments");
567 
568     bool enabled = false;
569     if (ExtNapiUtils::CheckTypeForNapiValue(env, argv[PARAM_INDEX_ZERO], napi_boolean)) {
570         enabled = ExtNapiUtils::GetBool(env, argv[PARAM_INDEX_ZERO]);
571     }
572     NG::MovingPhotoModelNG::GetInstance()->EnableAnalyzer(enabled);
573 
574     return ExtNapiUtils::CreateNull(env);
575 }
576 
JsHdrBrightness(napi_env env,napi_callback_info info)577 napi_value JsHdrBrightness(napi_env env, napi_callback_info info)
578 {
579     size_t argc = MAX_ARG_NUM;
580     napi_value argv[MAX_ARG_NUM] = { nullptr };
581     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
582     NAPI_ASSERT(env, argc >= ARG_NUM_ONE, "Wrong number of arguments");
583 
584     float hdrBrightness = 1.0f;
585     if (ExtNapiUtils::CheckTypeForNapiValue(env, argv[0], napi_number)) {
586         hdrBrightness = static_cast<float>(ExtNapiUtils::GetDouble(env, argv[0]));
587         hdrBrightness = (hdrBrightness >= 0.0f && hdrBrightness <= 1.0f) ? hdrBrightness : 1.0f;
588     }
589     NG::MovingPhotoModelNG::GetInstance()->SetHdrBrightness(hdrBrightness);
590 
591     return ExtNapiUtils::CreateNull(env);
592 }
593 
JsSetPlaybackStrategy(napi_env env,napi_callback_info info)594 napi_value JsSetPlaybackStrategy(napi_env env, napi_callback_info info)
595 {
596     size_t argc = MAX_ARG_NUM;
597     napi_value argv[MAX_ARG_NUM] = { nullptr };
598     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
599     NAPI_ASSERT(env, argc >= ARG_NUM_ONE, "Wrong number of arguments");
600 
601     if (!ExtNapiUtils::CheckTypeForNapiValue(env, argv[0], napi_object)) {
602         return ExtNapiUtils::CreateNull(env);
603     }
604     napi_value jsEnabled = nullptr;
605     if (napi_get_named_property(env, argv[0], "enableCameraPostprocessing", &jsEnabled) != napi_ok) {
606         return ExtNapiUtils::CreateNull(env);
607     }
608     bool isEnabled = ExtNapiUtils::GetBool(env, jsEnabled);
609     NG::MovingPhotoModelNG::GetInstance()->SetEnableCameraPostprocessing(isEnabled);
610     TAG_LOGI(AceLogTag::ACE_MOVING_PHOTO, "napi SetEnableCameraPostprocessing = %{public}d.", isEnabled);
611     return ExtNapiUtils::CreateNull(env);
612 }
613 
JsRepeatPlay(napi_env env,napi_callback_info info)614 napi_value JsRepeatPlay(napi_env env, napi_callback_info info)
615 {
616     size_t argc = MAX_ARG_NUM;
617     napi_value argv[MAX_ARG_NUM] = { nullptr };
618     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
619     NAPI_ASSERT(env, argc >= ARG_NUM_ONE, "Wrong number of arguments");
620 
621     bool isRepeatPlay = false;
622     if (ExtNapiUtils::CheckTypeForNapiValue(env, argv[PARAM_INDEX_ZERO], napi_boolean)) {
623         isRepeatPlay = ExtNapiUtils::GetBool(env, argv[PARAM_INDEX_ZERO]);
624     }
625     NG::MovingPhotoModelNG::GetInstance()->RepeatPlay(isRepeatPlay);
626 
627     return ExtNapiUtils::CreateNull(env);
628 }
629 
630 } // namespace OHOS::Ace
631 
NAPI_multimedia_movingphotoview_GetJSCode(const char ** buf,int * bufLen)632 extern "C" __attribute__((visibility("default"))) void NAPI_multimedia_movingphotoview_GetJSCode(
633     const char** buf, int* bufLen)
634 {
635     if (buf != nullptr) {
636         *buf = _binary_multimedia_movingphotoview_js_start;
637     }
638 
639     if (bufLen != nullptr) {
640         *bufLen = _binary_multimedia_movingphotoview_js_end - _binary_multimedia_movingphotoview_js_start;
641     }
642 }
643 
644 // multimedia_movingphotoview JS register
NAPI_multimedia_movingphotoview_GetABCCode(const char ** buf,int * buflen)645 extern "C" __attribute__((visibility("default"))) void NAPI_multimedia_movingphotoview_GetABCCode(
646     const char** buf, int* buflen)
647 {
648     if (buf != nullptr) {
649         *buf = _binary_multimedia_movingphotoview_abc_start;
650     }
651     if (buflen != nullptr) {
652         *buflen = _binary_multimedia_movingphotoview_abc_end - _binary_multimedia_movingphotoview_abc_start;
653     }
654 }
655 
656 static napi_module movingphotoModule  = {
657     .nm_version = 1,
658     .nm_flags = 0,
659     .nm_filename = nullptr,
660     .nm_register_func = OHOS::Ace::ExportMovingPhoto,
661     .nm_modname = "multimedia.movingphotoview",
662     .nm_priv = ((void*)0),
663     .reserved = { 0 },
664 };
665 
RegisterModuleMovingPhoto()666 extern "C" __attribute__((constructor)) void RegisterModuleMovingPhoto()
667 {
668     napi_module_register(&movingphotoModule);
669 }
670