123b3eb3cSopenharmony_ci/* 223b3eb3cSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License. 523b3eb3cSopenharmony_ci * You may obtain a copy of the License at 623b3eb3cSopenharmony_ci * 723b3eb3cSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 823b3eb3cSopenharmony_ci * 923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and 1323b3eb3cSopenharmony_ci * limitations under the License. 1423b3eb3cSopenharmony_ci */ 1523b3eb3cSopenharmony_ci 1623b3eb3cSopenharmony_ci#include "movingphoto_napi.h" 1723b3eb3cSopenharmony_ci 1823b3eb3cSopenharmony_ci#include "ext_napi_utils.h" 1923b3eb3cSopenharmony_ci#include "movingphoto_model_ng.h" 2023b3eb3cSopenharmony_ci 2123b3eb3cSopenharmony_ciextern const char _binary_multimedia_movingphotoview_js_start[]; 2223b3eb3cSopenharmony_ciextern const char _binary_multimedia_movingphotoview_abc_start[]; 2323b3eb3cSopenharmony_ci#if !defined(IOS_PLATFORM) 2423b3eb3cSopenharmony_ciextern const char _binary_multimedia_movingphotoview_js_end[]; 2523b3eb3cSopenharmony_ciextern const char _binary_multimedia_movingphotoview_abc_end[]; 2623b3eb3cSopenharmony_ci#else 2723b3eb3cSopenharmony_ciextern const char* _binary_multimedia_movingphotoview_js_end; 2823b3eb3cSopenharmony_ciextern const char* _binary_multimedia_movingphotoview_abc_end; 2923b3eb3cSopenharmony_ci#endif 3023b3eb3cSopenharmony_ci 3123b3eb3cSopenharmony_cinamespace OHOS::Ace { 3223b3eb3cSopenharmony_cinamespace { 3323b3eb3cSopenharmony_cistatic constexpr size_t MAX_ARG_NUM = 10; 3423b3eb3cSopenharmony_cistatic constexpr int32_t ARG_NUM_ONE = 1; 3523b3eb3cSopenharmony_cistatic constexpr int32_t ARG_NUM_TWO = 2; 3623b3eb3cSopenharmony_cistatic constexpr int32_t PARAM_INDEX_ZERO = 0; 3723b3eb3cSopenharmony_cistatic constexpr int32_t PARAM_INDEX_ONE = 1; 3823b3eb3cSopenharmony_ci} // namespace 3923b3eb3cSopenharmony_ci 4023b3eb3cSopenharmony_cistd::unique_ptr<NG::MovingPhotoModelNG> NG::MovingPhotoModelNG::instance_ = nullptr; 4123b3eb3cSopenharmony_cistd::mutex NG::MovingPhotoModelNG::mutex_; 4223b3eb3cSopenharmony_ci 4323b3eb3cSopenharmony_ciNG::MovingPhotoModelNG* NG::MovingPhotoModelNG::GetInstance() 4423b3eb3cSopenharmony_ci{ 4523b3eb3cSopenharmony_ci if (!instance_) { 4623b3eb3cSopenharmony_ci std::lock_guard<std::mutex> lock(mutex_); 4723b3eb3cSopenharmony_ci if (!instance_) { 4823b3eb3cSopenharmony_ci instance_.reset(new NG::MovingPhotoModelNG()); 4923b3eb3cSopenharmony_ci } 5023b3eb3cSopenharmony_ci } 5123b3eb3cSopenharmony_ci return instance_.get(); 5223b3eb3cSopenharmony_ci} 5323b3eb3cSopenharmony_ci 5423b3eb3cSopenharmony_cinapi_value JsCreate(napi_env env, napi_callback_info info) 5523b3eb3cSopenharmony_ci{ 5623b3eb3cSopenharmony_ci size_t argc = MAX_ARG_NUM; 5723b3eb3cSopenharmony_ci napi_value argv[MAX_ARG_NUM] = { nullptr }; 5823b3eb3cSopenharmony_ci NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); 5923b3eb3cSopenharmony_ci NAPI_ASSERT(env, argc >= ARG_NUM_ONE, "Wrong number of arguments"); 6023b3eb3cSopenharmony_ci 6123b3eb3cSopenharmony_ci if (!ExtNapiUtils::CheckTypeForNapiValue(env, argv[0], napi_object)) { 6223b3eb3cSopenharmony_ci return ExtNapiUtils::CreateNull(env); 6323b3eb3cSopenharmony_ci } 6423b3eb3cSopenharmony_ci 6523b3eb3cSopenharmony_ci napi_value jsController = nullptr; 6623b3eb3cSopenharmony_ci NG::MovingPhotoController* controller = nullptr; 6723b3eb3cSopenharmony_ci napi_get_named_property(env, argv[0], "controller", &jsController); 6823b3eb3cSopenharmony_ci if (ExtNapiUtils::CheckTypeForNapiValue(env, jsController, napi_object)) { 6923b3eb3cSopenharmony_ci napi_unwrap(env, jsController, (void**)&controller); 7023b3eb3cSopenharmony_ci } 7123b3eb3cSopenharmony_ci NG::MovingPhotoModelNG::GetInstance()->Create(Referenced::Claim(controller)); 7223b3eb3cSopenharmony_ci 7323b3eb3cSopenharmony_ci napi_value jsData = nullptr; 7423b3eb3cSopenharmony_ci napi_get_named_property(env, argv[0], "movingPhoto", &jsData); 7523b3eb3cSopenharmony_ci if (!ExtNapiUtils::CheckTypeForNapiValue(env, jsData, napi_object)) { 7623b3eb3cSopenharmony_ci return ExtNapiUtils::CreateNull(env); 7723b3eb3cSopenharmony_ci } 7823b3eb3cSopenharmony_ci 7923b3eb3cSopenharmony_ci napi_value jsImageAIOptions = nullptr; 8023b3eb3cSopenharmony_ci napi_get_named_property(env, argv[0], "imageAIOptions", &jsImageAIOptions); 8123b3eb3cSopenharmony_ci NG::MovingPhotoModelNG::GetInstance()->SetImageAIOptions(jsImageAIOptions); 8223b3eb3cSopenharmony_ci 8323b3eb3cSopenharmony_ci napi_value getUri = nullptr; 8423b3eb3cSopenharmony_ci napi_get_named_property(env, jsData, "getUri", &getUri); 8523b3eb3cSopenharmony_ci if (!ExtNapiUtils::CheckTypeForNapiValue(env, getUri, napi_function)) { 8623b3eb3cSopenharmony_ci return ExtNapiUtils::CreateNull(env); 8723b3eb3cSopenharmony_ci } 8823b3eb3cSopenharmony_ci napi_value imageUri; 8923b3eb3cSopenharmony_ci napi_call_function(env, jsData, getUri, 0, nullptr, &imageUri); 9023b3eb3cSopenharmony_ci std::string imageUriStr = ExtNapiUtils::GetStringFromValueUtf8(env, imageUri); 9123b3eb3cSopenharmony_ci NG::MovingPhotoModelNG::GetInstance()->SetImageSrc(imageUriStr); 9223b3eb3cSopenharmony_ci 9323b3eb3cSopenharmony_ci return ExtNapiUtils::CreateNull(env); 9423b3eb3cSopenharmony_ci} 9523b3eb3cSopenharmony_ci 9623b3eb3cSopenharmony_cinapi_value JsMuted(napi_env env, napi_callback_info info) 9723b3eb3cSopenharmony_ci{ 9823b3eb3cSopenharmony_ci size_t argc = MAX_ARG_NUM; 9923b3eb3cSopenharmony_ci napi_value argv[MAX_ARG_NUM] = { nullptr }; 10023b3eb3cSopenharmony_ci NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); 10123b3eb3cSopenharmony_ci NAPI_ASSERT(env, argc >= ARG_NUM_ONE, "Wrong number of arguments"); 10223b3eb3cSopenharmony_ci 10323b3eb3cSopenharmony_ci bool muted = false; 10423b3eb3cSopenharmony_ci if (ExtNapiUtils::CheckTypeForNapiValue(env, argv[0], napi_boolean)) { 10523b3eb3cSopenharmony_ci muted = ExtNapiUtils::GetBool(env, argv[0]); 10623b3eb3cSopenharmony_ci } 10723b3eb3cSopenharmony_ci NG::MovingPhotoModelNG::GetInstance()->SetMuted(muted); 10823b3eb3cSopenharmony_ci 10923b3eb3cSopenharmony_ci return ExtNapiUtils::CreateNull(env); 11023b3eb3cSopenharmony_ci} 11123b3eb3cSopenharmony_ci 11223b3eb3cSopenharmony_cinapi_value JsObjectFit(napi_env env, napi_callback_info info) 11323b3eb3cSopenharmony_ci{ 11423b3eb3cSopenharmony_ci size_t argc = MAX_ARG_NUM; 11523b3eb3cSopenharmony_ci napi_value argv[MAX_ARG_NUM] = { nullptr }; 11623b3eb3cSopenharmony_ci NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); 11723b3eb3cSopenharmony_ci NAPI_ASSERT(env, argc >= ARG_NUM_ONE, "Wrong number of arguments"); 11823b3eb3cSopenharmony_ci 11923b3eb3cSopenharmony_ci auto objectFit = ImageFit::COVER; 12023b3eb3cSopenharmony_ci if (ExtNapiUtils::CheckTypeForNapiValue(env, argv[0], napi_number)) { 12123b3eb3cSopenharmony_ci objectFit = static_cast<ImageFit>(ExtNapiUtils::GetCInt32(env, argv[0])); 12223b3eb3cSopenharmony_ci } 12323b3eb3cSopenharmony_ci NG::MovingPhotoModelNG::GetInstance()->SetObjectFit(objectFit); 12423b3eb3cSopenharmony_ci 12523b3eb3cSopenharmony_ci return ExtNapiUtils::CreateNull(env); 12623b3eb3cSopenharmony_ci} 12723b3eb3cSopenharmony_ci 12823b3eb3cSopenharmony_cinapi_value JsOnComplete(napi_env env, napi_callback_info info) 12923b3eb3cSopenharmony_ci{ 13023b3eb3cSopenharmony_ci size_t argc = MAX_ARG_NUM; 13123b3eb3cSopenharmony_ci napi_value thisVal = nullptr; 13223b3eb3cSopenharmony_ci napi_value argv[MAX_ARG_NUM] = { nullptr }; 13323b3eb3cSopenharmony_ci NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVal, nullptr)); 13423b3eb3cSopenharmony_ci NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments"); 13523b3eb3cSopenharmony_ci if (!ExtNapiUtils::CheckTypeForNapiValue(env, argv[0], napi_function)) { 13623b3eb3cSopenharmony_ci return ExtNapiUtils::CreateNull(env); 13723b3eb3cSopenharmony_ci } 13823b3eb3cSopenharmony_ci auto asyncEvent = std::make_shared<NapiAsyncEvent>(env, argv[0]); 13923b3eb3cSopenharmony_ci auto onComplete = [asyncEvent]() { 14023b3eb3cSopenharmony_ci asyncEvent->Call(0, nullptr); 14123b3eb3cSopenharmony_ci }; 14223b3eb3cSopenharmony_ci NG::MovingPhotoModelNG::GetInstance()->SetOnComplete(std::move(onComplete)); 14323b3eb3cSopenharmony_ci return ExtNapiUtils::CreateNull(env); 14423b3eb3cSopenharmony_ci} 14523b3eb3cSopenharmony_ci 14623b3eb3cSopenharmony_cinapi_value JsOnStart(napi_env env, napi_callback_info info) 14723b3eb3cSopenharmony_ci{ 14823b3eb3cSopenharmony_ci size_t argc = MAX_ARG_NUM; 14923b3eb3cSopenharmony_ci napi_value thisVal = nullptr; 15023b3eb3cSopenharmony_ci napi_value argv[MAX_ARG_NUM] = { nullptr }; 15123b3eb3cSopenharmony_ci NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVal, nullptr)); 15223b3eb3cSopenharmony_ci NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments"); 15323b3eb3cSopenharmony_ci if (!ExtNapiUtils::CheckTypeForNapiValue(env, argv[0], napi_function)) { 15423b3eb3cSopenharmony_ci return ExtNapiUtils::CreateNull(env); 15523b3eb3cSopenharmony_ci } 15623b3eb3cSopenharmony_ci auto asyncEvent = std::make_shared<NapiAsyncEvent>(env, argv[0]); 15723b3eb3cSopenharmony_ci auto onStart = [asyncEvent]() { 15823b3eb3cSopenharmony_ci asyncEvent->Call(0, nullptr); 15923b3eb3cSopenharmony_ci }; 16023b3eb3cSopenharmony_ci NG::MovingPhotoModelNG::GetInstance()->SetOnStart(std::move(onStart)); 16123b3eb3cSopenharmony_ci return ExtNapiUtils::CreateNull(env); 16223b3eb3cSopenharmony_ci} 16323b3eb3cSopenharmony_ci 16423b3eb3cSopenharmony_cinapi_value JsOnStop(napi_env env, napi_callback_info info) 16523b3eb3cSopenharmony_ci{ 16623b3eb3cSopenharmony_ci size_t argc = MAX_ARG_NUM; 16723b3eb3cSopenharmony_ci napi_value thisVal = nullptr; 16823b3eb3cSopenharmony_ci napi_value argv[MAX_ARG_NUM] = { nullptr }; 16923b3eb3cSopenharmony_ci NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVal, nullptr)); 17023b3eb3cSopenharmony_ci NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments"); 17123b3eb3cSopenharmony_ci if (!ExtNapiUtils::CheckTypeForNapiValue(env, argv[0], napi_function)) { 17223b3eb3cSopenharmony_ci return ExtNapiUtils::CreateNull(env); 17323b3eb3cSopenharmony_ci } 17423b3eb3cSopenharmony_ci auto asyncEvent = std::make_shared<NapiAsyncEvent>(env, argv[0]); 17523b3eb3cSopenharmony_ci auto onStop = [asyncEvent]() { 17623b3eb3cSopenharmony_ci asyncEvent->Call(0, nullptr); 17723b3eb3cSopenharmony_ci }; 17823b3eb3cSopenharmony_ci NG::MovingPhotoModelNG::GetInstance()->SetOnStop(std::move(onStop)); 17923b3eb3cSopenharmony_ci return ExtNapiUtils::CreateNull(env); 18023b3eb3cSopenharmony_ci} 18123b3eb3cSopenharmony_ci 18223b3eb3cSopenharmony_cinapi_value JsOnPause(napi_env env, napi_callback_info info) 18323b3eb3cSopenharmony_ci{ 18423b3eb3cSopenharmony_ci size_t argc = MAX_ARG_NUM; 18523b3eb3cSopenharmony_ci napi_value thisVal = nullptr; 18623b3eb3cSopenharmony_ci napi_value argv[MAX_ARG_NUM] = { nullptr }; 18723b3eb3cSopenharmony_ci NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVal, nullptr)); 18823b3eb3cSopenharmony_ci NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments"); 18923b3eb3cSopenharmony_ci if (!ExtNapiUtils::CheckTypeForNapiValue(env, argv[0], napi_function)) { 19023b3eb3cSopenharmony_ci return ExtNapiUtils::CreateNull(env); 19123b3eb3cSopenharmony_ci } 19223b3eb3cSopenharmony_ci auto asyncEvent = std::make_shared<NapiAsyncEvent>(env, argv[0]); 19323b3eb3cSopenharmony_ci auto onPause = [asyncEvent]() { 19423b3eb3cSopenharmony_ci asyncEvent->Call(0, nullptr); 19523b3eb3cSopenharmony_ci }; 19623b3eb3cSopenharmony_ci NG::MovingPhotoModelNG::GetInstance()->SetOnPause(std::move(onPause)); 19723b3eb3cSopenharmony_ci return ExtNapiUtils::CreateNull(env); 19823b3eb3cSopenharmony_ci} 19923b3eb3cSopenharmony_ci 20023b3eb3cSopenharmony_cinapi_value JsOnFinish(napi_env env, napi_callback_info info) 20123b3eb3cSopenharmony_ci{ 20223b3eb3cSopenharmony_ci size_t argc = MAX_ARG_NUM; 20323b3eb3cSopenharmony_ci napi_value thisVal = nullptr; 20423b3eb3cSopenharmony_ci napi_value argv[MAX_ARG_NUM] = { nullptr }; 20523b3eb3cSopenharmony_ci NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVal, nullptr)); 20623b3eb3cSopenharmony_ci NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments"); 20723b3eb3cSopenharmony_ci if (!ExtNapiUtils::CheckTypeForNapiValue(env, argv[0], napi_function)) { 20823b3eb3cSopenharmony_ci return ExtNapiUtils::CreateNull(env); 20923b3eb3cSopenharmony_ci } 21023b3eb3cSopenharmony_ci auto asyncEvent = std::make_shared<NapiAsyncEvent>(env, argv[0]); 21123b3eb3cSopenharmony_ci auto onFinish = [asyncEvent]() { 21223b3eb3cSopenharmony_ci asyncEvent->Call(0, nullptr); 21323b3eb3cSopenharmony_ci }; 21423b3eb3cSopenharmony_ci NG::MovingPhotoModelNG::GetInstance()->SetOnFinish(std::move(onFinish)); 21523b3eb3cSopenharmony_ci return ExtNapiUtils::CreateNull(env); 21623b3eb3cSopenharmony_ci} 21723b3eb3cSopenharmony_ci 21823b3eb3cSopenharmony_cinapi_value JsOnError(napi_env env, napi_callback_info info) 21923b3eb3cSopenharmony_ci{ 22023b3eb3cSopenharmony_ci size_t argc = MAX_ARG_NUM; 22123b3eb3cSopenharmony_ci napi_value thisVal = nullptr; 22223b3eb3cSopenharmony_ci napi_value argv[MAX_ARG_NUM] = { nullptr }; 22323b3eb3cSopenharmony_ci NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVal, nullptr)); 22423b3eb3cSopenharmony_ci NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments"); 22523b3eb3cSopenharmony_ci if (!ExtNapiUtils::CheckTypeForNapiValue(env, argv[0], napi_function)) { 22623b3eb3cSopenharmony_ci return ExtNapiUtils::CreateNull(env); 22723b3eb3cSopenharmony_ci } 22823b3eb3cSopenharmony_ci auto asyncEvent = std::make_shared<NapiAsyncEvent>(env, argv[0]); 22923b3eb3cSopenharmony_ci auto onError = [asyncEvent]() { 23023b3eb3cSopenharmony_ci asyncEvent->Call(0, nullptr); 23123b3eb3cSopenharmony_ci }; 23223b3eb3cSopenharmony_ci NG::MovingPhotoModelNG::GetInstance()->SetOnError(std::move(onError)); 23323b3eb3cSopenharmony_ci return ExtNapiUtils::CreateNull(env); 23423b3eb3cSopenharmony_ci} 23523b3eb3cSopenharmony_ci 23623b3eb3cSopenharmony_cinapi_value InitView(napi_env env, napi_value exports) 23723b3eb3cSopenharmony_ci{ 23823b3eb3cSopenharmony_ci static napi_property_descriptor desc[] = { 23923b3eb3cSopenharmony_ci DECLARE_NAPI_FUNCTION("create", JsCreate), 24023b3eb3cSopenharmony_ci DECLARE_NAPI_FUNCTION("muted", JsMuted), 24123b3eb3cSopenharmony_ci DECLARE_NAPI_FUNCTION("objectFit", JsObjectFit), 24223b3eb3cSopenharmony_ci DECLARE_NAPI_FUNCTION("onComplete", JsOnComplete), 24323b3eb3cSopenharmony_ci DECLARE_NAPI_FUNCTION("onStart", JsOnStart), 24423b3eb3cSopenharmony_ci DECLARE_NAPI_FUNCTION("onStop", JsOnStop), 24523b3eb3cSopenharmony_ci DECLARE_NAPI_FUNCTION("onPause", JsOnPause), 24623b3eb3cSopenharmony_ci DECLARE_NAPI_FUNCTION("onFinish", JsOnFinish), 24723b3eb3cSopenharmony_ci DECLARE_NAPI_FUNCTION("onError", JsOnError), 24823b3eb3cSopenharmony_ci DECLARE_NAPI_FUNCTION("autoPlayPeriod", JsAutoPlayPeriod), 24923b3eb3cSopenharmony_ci DECLARE_NAPI_FUNCTION("autoPlay", JsAutoPlay), 25023b3eb3cSopenharmony_ci DECLARE_NAPI_FUNCTION("repeatPlay", JsRepeatPlay), 25123b3eb3cSopenharmony_ci DECLARE_NAPI_FUNCTION("enableAnalyzer", JsEnableAnalyzer), 25223b3eb3cSopenharmony_ci }; 25323b3eb3cSopenharmony_ci NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); 25423b3eb3cSopenharmony_ci return exports; 25523b3eb3cSopenharmony_ci} 25623b3eb3cSopenharmony_ci 25723b3eb3cSopenharmony_cinapi_value StartPlayback(napi_env env, napi_callback_info info) 25823b3eb3cSopenharmony_ci{ 25923b3eb3cSopenharmony_ci napi_value thisVar = nullptr; 26023b3eb3cSopenharmony_ci NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, NULL)); 26123b3eb3cSopenharmony_ci NG::MovingPhotoController* controller = nullptr; 26223b3eb3cSopenharmony_ci napi_unwrap(env, thisVar, (void**)&controller); 26323b3eb3cSopenharmony_ci if (controller == nullptr) { 26423b3eb3cSopenharmony_ci return ExtNapiUtils::CreateNull(env); 26523b3eb3cSopenharmony_ci } 26623b3eb3cSopenharmony_ci controller->StartPlayback(); 26723b3eb3cSopenharmony_ci return ExtNapiUtils::CreateNull(env); 26823b3eb3cSopenharmony_ci} 26923b3eb3cSopenharmony_ci 27023b3eb3cSopenharmony_cinapi_value StopPlayback(napi_env env, napi_callback_info info) 27123b3eb3cSopenharmony_ci{ 27223b3eb3cSopenharmony_ci napi_value thisVar = nullptr; 27323b3eb3cSopenharmony_ci NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, NULL)); 27423b3eb3cSopenharmony_ci NG::MovingPhotoController* controller = nullptr; 27523b3eb3cSopenharmony_ci napi_unwrap(env, thisVar, (void**)&controller); 27623b3eb3cSopenharmony_ci if (controller == nullptr) { 27723b3eb3cSopenharmony_ci return ExtNapiUtils::CreateNull(env); 27823b3eb3cSopenharmony_ci } 27923b3eb3cSopenharmony_ci controller->StopPlayback(); 28023b3eb3cSopenharmony_ci return ExtNapiUtils::CreateNull(env); 28123b3eb3cSopenharmony_ci} 28223b3eb3cSopenharmony_ci 28323b3eb3cSopenharmony_cinapi_value MovingPhotoControllerConstructor(napi_env env, napi_callback_info info) 28423b3eb3cSopenharmony_ci{ 28523b3eb3cSopenharmony_ci napi_value thisVar = nullptr; 28623b3eb3cSopenharmony_ci NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); 28723b3eb3cSopenharmony_ci auto controller = AceType::MakeRefPtr<NG::MovingPhotoController>(); 28823b3eb3cSopenharmony_ci if (controller == nullptr) { 28923b3eb3cSopenharmony_ci return ExtNapiUtils::CreateNull(env); 29023b3eb3cSopenharmony_ci } 29123b3eb3cSopenharmony_ci controller->IncRefCount(); 29223b3eb3cSopenharmony_ci napi_wrap( 29323b3eb3cSopenharmony_ci env, thisVar, AceType::RawPtr(controller), 29423b3eb3cSopenharmony_ci [](napi_env env, void* data, void* hint) { 29523b3eb3cSopenharmony_ci auto* controller = reinterpret_cast<NG::MovingPhotoController*>(data); 29623b3eb3cSopenharmony_ci controller->DecRefCount(); 29723b3eb3cSopenharmony_ci }, 29823b3eb3cSopenharmony_ci nullptr, nullptr); 29923b3eb3cSopenharmony_ci return thisVar; 30023b3eb3cSopenharmony_ci} 30123b3eb3cSopenharmony_ci 30223b3eb3cSopenharmony_cinapi_value InitController(napi_env env, napi_value exports) 30323b3eb3cSopenharmony_ci{ 30423b3eb3cSopenharmony_ci napi_value movingphotoControllerClass = nullptr; 30523b3eb3cSopenharmony_ci napi_property_descriptor properties[] = { 30623b3eb3cSopenharmony_ci DECLARE_NAPI_FUNCTION("startPlayback", StartPlayback), 30723b3eb3cSopenharmony_ci DECLARE_NAPI_FUNCTION("stopPlayback", StopPlayback), 30823b3eb3cSopenharmony_ci }; 30923b3eb3cSopenharmony_ci NAPI_CALL(env, napi_define_class(env, "MovingPhotoViewController", NAPI_AUTO_LENGTH, 31023b3eb3cSopenharmony_ci MovingPhotoControllerConstructor, nullptr, sizeof(properties) / sizeof(*properties), properties, 31123b3eb3cSopenharmony_ci &movingphotoControllerClass)); 31223b3eb3cSopenharmony_ci NAPI_CALL(env, napi_set_named_property(env, exports, "MovingPhotoViewController", movingphotoControllerClass)); 31323b3eb3cSopenharmony_ci return exports; 31423b3eb3cSopenharmony_ci} 31523b3eb3cSopenharmony_ci 31623b3eb3cSopenharmony_cinapi_value ExportMovingPhoto(napi_env env, napi_value exports) 31723b3eb3cSopenharmony_ci{ 31823b3eb3cSopenharmony_ci InitView(env, exports); 31923b3eb3cSopenharmony_ci InitController(env, exports); 32023b3eb3cSopenharmony_ci return exports; 32123b3eb3cSopenharmony_ci} 32223b3eb3cSopenharmony_ci 32323b3eb3cSopenharmony_cinapi_value JsAutoPlayPeriod(napi_env env, napi_callback_info info) 32423b3eb3cSopenharmony_ci{ 32523b3eb3cSopenharmony_ci size_t argc = MAX_ARG_NUM; 32623b3eb3cSopenharmony_ci napi_value argv[MAX_ARG_NUM] = { nullptr }; 32723b3eb3cSopenharmony_ci NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); 32823b3eb3cSopenharmony_ci NAPI_ASSERT(env, argc >= ARG_NUM_TWO, "Wrong number of arguments"); 32923b3eb3cSopenharmony_ci 33023b3eb3cSopenharmony_ci int64_t startTime = 0; 33123b3eb3cSopenharmony_ci if (ExtNapiUtils::CheckTypeForNapiValue(env, argv[PARAM_INDEX_ZERO], napi_number)) { 33223b3eb3cSopenharmony_ci startTime = ExtNapiUtils::GetCInt64(env, argv[PARAM_INDEX_ZERO]); 33323b3eb3cSopenharmony_ci } 33423b3eb3cSopenharmony_ci int64_t endTime = 0; 33523b3eb3cSopenharmony_ci if (ExtNapiUtils::CheckTypeForNapiValue(env, argv[PARAM_INDEX_ONE], napi_number)) { 33623b3eb3cSopenharmony_ci endTime = ExtNapiUtils::GetCInt64(env, argv[PARAM_INDEX_ONE]); 33723b3eb3cSopenharmony_ci } 33823b3eb3cSopenharmony_ci NG::MovingPhotoModelNG::GetInstance()->AutoPlayPeriod(startTime, endTime); 33923b3eb3cSopenharmony_ci 34023b3eb3cSopenharmony_ci return ExtNapiUtils::CreateNull(env); 34123b3eb3cSopenharmony_ci} 34223b3eb3cSopenharmony_ci 34323b3eb3cSopenharmony_cinapi_value JsAutoPlay(napi_env env, napi_callback_info info) 34423b3eb3cSopenharmony_ci{ 34523b3eb3cSopenharmony_ci size_t argc = MAX_ARG_NUM; 34623b3eb3cSopenharmony_ci napi_value argv[MAX_ARG_NUM] = { nullptr }; 34723b3eb3cSopenharmony_ci NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); 34823b3eb3cSopenharmony_ci NAPI_ASSERT(env, argc >= ARG_NUM_ONE, "Wrong number of arguments"); 34923b3eb3cSopenharmony_ci 35023b3eb3cSopenharmony_ci bool isAutoPlay = false; 35123b3eb3cSopenharmony_ci if (ExtNapiUtils::CheckTypeForNapiValue(env, argv[PARAM_INDEX_ZERO], napi_boolean)) { 35223b3eb3cSopenharmony_ci isAutoPlay = ExtNapiUtils::GetBool(env, argv[PARAM_INDEX_ZERO]); 35323b3eb3cSopenharmony_ci } 35423b3eb3cSopenharmony_ci NG::MovingPhotoModelNG::GetInstance()->AutoPlay(isAutoPlay); 35523b3eb3cSopenharmony_ci 35623b3eb3cSopenharmony_ci return ExtNapiUtils::CreateNull(env); 35723b3eb3cSopenharmony_ci} 35823b3eb3cSopenharmony_ci 35923b3eb3cSopenharmony_cinapi_value JsEnableAnalyzer(napi_env env, napi_callback_info info) 36023b3eb3cSopenharmony_ci{ 36123b3eb3cSopenharmony_ci size_t argc = MAX_ARG_NUM; 36223b3eb3cSopenharmony_ci napi_value argv[MAX_ARG_NUM] = { nullptr }; 36323b3eb3cSopenharmony_ci NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); 36423b3eb3cSopenharmony_ci NAPI_ASSERT(env, argc >= ARG_NUM_ONE, "Wrong number of arguments"); 36523b3eb3cSopenharmony_ci 36623b3eb3cSopenharmony_ci bool enabled = false; 36723b3eb3cSopenharmony_ci if (ExtNapiUtils::CheckTypeForNapiValue(env, argv[PARAM_INDEX_ZERO], napi_boolean)) { 36823b3eb3cSopenharmony_ci enabled = ExtNapiUtils::GetBool(env, argv[PARAM_INDEX_ZERO]); 36923b3eb3cSopenharmony_ci } 37023b3eb3cSopenharmony_ci NG::MovingPhotoModelNG::GetInstance()->EnableAnalyzer(enabled); 37123b3eb3cSopenharmony_ci 37223b3eb3cSopenharmony_ci return ExtNapiUtils::CreateNull(env); 37323b3eb3cSopenharmony_ci} 37423b3eb3cSopenharmony_ci 37523b3eb3cSopenharmony_cinapi_value JsRepeatPlay(napi_env env, napi_callback_info info) 37623b3eb3cSopenharmony_ci{ 37723b3eb3cSopenharmony_ci size_t argc = MAX_ARG_NUM; 37823b3eb3cSopenharmony_ci napi_value argv[MAX_ARG_NUM] = { nullptr }; 37923b3eb3cSopenharmony_ci NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); 38023b3eb3cSopenharmony_ci NAPI_ASSERT(env, argc >= ARG_NUM_ONE, "Wrong number of arguments"); 38123b3eb3cSopenharmony_ci 38223b3eb3cSopenharmony_ci bool isRepeatPlay = false; 38323b3eb3cSopenharmony_ci if (ExtNapiUtils::CheckTypeForNapiValue(env, argv[PARAM_INDEX_ZERO], napi_boolean)) { 38423b3eb3cSopenharmony_ci isRepeatPlay = ExtNapiUtils::GetBool(env, argv[PARAM_INDEX_ZERO]); 38523b3eb3cSopenharmony_ci } 38623b3eb3cSopenharmony_ci NG::MovingPhotoModelNG::GetInstance()->RepeatPlay(isRepeatPlay); 38723b3eb3cSopenharmony_ci 38823b3eb3cSopenharmony_ci return ExtNapiUtils::CreateNull(env); 38923b3eb3cSopenharmony_ci} 39023b3eb3cSopenharmony_ci 39123b3eb3cSopenharmony_ci} // namespace OHOS::Ace 39223b3eb3cSopenharmony_ci 39323b3eb3cSopenharmony_ciextern "C" __attribute__((visibility("default"))) void NAPI_multimedia_movingphotoview_GetJSCode( 39423b3eb3cSopenharmony_ci const char** buf, int* bufLen) 39523b3eb3cSopenharmony_ci{ 39623b3eb3cSopenharmony_ci if (buf != nullptr) { 39723b3eb3cSopenharmony_ci *buf = _binary_multimedia_movingphotoview_js_start; 39823b3eb3cSopenharmony_ci } 39923b3eb3cSopenharmony_ci 40023b3eb3cSopenharmony_ci if (bufLen != nullptr) { 40123b3eb3cSopenharmony_ci *bufLen = _binary_multimedia_movingphotoview_js_end - _binary_multimedia_movingphotoview_js_start; 40223b3eb3cSopenharmony_ci } 40323b3eb3cSopenharmony_ci} 40423b3eb3cSopenharmony_ci 40523b3eb3cSopenharmony_ci// multimedia_movingphotoview JS register 40623b3eb3cSopenharmony_ciextern "C" __attribute__((visibility("default"))) void NAPI_multimedia_movingphotoview_GetABCCode( 40723b3eb3cSopenharmony_ci const char** buf, int* buflen) 40823b3eb3cSopenharmony_ci{ 40923b3eb3cSopenharmony_ci if (buf != nullptr) { 41023b3eb3cSopenharmony_ci *buf = _binary_multimedia_movingphotoview_abc_start; 41123b3eb3cSopenharmony_ci } 41223b3eb3cSopenharmony_ci if (buflen != nullptr) { 41323b3eb3cSopenharmony_ci *buflen = _binary_multimedia_movingphotoview_abc_end - _binary_multimedia_movingphotoview_abc_start; 41423b3eb3cSopenharmony_ci } 41523b3eb3cSopenharmony_ci} 41623b3eb3cSopenharmony_ci 41723b3eb3cSopenharmony_cistatic napi_module movingphotoModule = { 41823b3eb3cSopenharmony_ci .nm_version = 1, 41923b3eb3cSopenharmony_ci .nm_flags = 0, 42023b3eb3cSopenharmony_ci .nm_filename = nullptr, 42123b3eb3cSopenharmony_ci .nm_register_func = OHOS::Ace::ExportMovingPhoto, 42223b3eb3cSopenharmony_ci .nm_modname = "multimedia.movingphotoview", 42323b3eb3cSopenharmony_ci .nm_priv = ((void*)0), 42423b3eb3cSopenharmony_ci .reserved = { 0 }, 42523b3eb3cSopenharmony_ci}; 42623b3eb3cSopenharmony_ci 42723b3eb3cSopenharmony_ciextern "C" __attribute__((constructor)) void RegisterModuleMovingPhoto() 42823b3eb3cSopenharmony_ci{ 42923b3eb3cSopenharmony_ci napi_module_register(&movingphotoModule); 43023b3eb3cSopenharmony_ci} 431