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 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_video_bridge.h"
16
17 #include "bridge/declarative_frontend/engine/jsi/jsi_types.h"
18 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_common_bridge.h"
19 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
20 #include "core/components/common/layout/constants.h"
21
22 namespace OHOS::Ace::NG {
23 constexpr int32_t NUM_0 = 0;
24 constexpr int32_t NUM_1 = 1;
25
26 namespace {
ParseColorMetricsToColor(const EcmaVM * vm,const Local<JSValueRef> & jsValue,Color & result)27 bool ParseColorMetricsToColor(const EcmaVM* vm, const Local<JSValueRef>& jsValue, Color& result)
28 {
29 if (!jsValue->IsObject(vm)) {
30 return false;
31 }
32 auto obj = jsValue->ToObject(vm);
33 auto toNumericProp = obj->Get(vm, "toNumeric");
34 if (toNumericProp->IsFunction(vm)) {
35 panda::Local<panda::FunctionRef> func = toNumericProp;
36 auto colorVal = func->Call(vm, obj, nullptr, 0);
37 result.SetValue(colorVal->Uint32Value(vm));
38 return true;
39 }
40 return false;
41 }
42 } // namespace
43
SetAutoPlay(ArkUIRuntimeCallInfo * runtimeCallInfo)44 ArkUINativeModuleValue VideoBridge::SetAutoPlay(ArkUIRuntimeCallInfo* runtimeCallInfo)
45 {
46 EcmaVM* vm = runtimeCallInfo->GetVM();
47 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
48 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
49 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(1);
50 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
51 if (secondArg->IsBoolean()) {
52 uint32_t autoPlay = static_cast<uint32_t>(secondArg->ToBoolean(vm)->Value());
53 GetArkUINodeModifiers()->getVideoModifier()->setAutoPlay(nativeNode, autoPlay);
54 } else {
55 GetArkUINodeModifiers()->getVideoModifier()->resetAutoPlay(nativeNode);
56 }
57 return panda::JSValueRef::Undefined(vm);
58 }
59
ResetAutoPlay(ArkUIRuntimeCallInfo * runtimeCallInfo)60 ArkUINativeModuleValue VideoBridge::ResetAutoPlay(ArkUIRuntimeCallInfo* runtimeCallInfo)
61 {
62 EcmaVM* vm = runtimeCallInfo->GetVM();
63 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
64 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
65 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
66 GetArkUINodeModifiers()->getVideoModifier()->resetAutoPlay(nativeNode);
67 return panda::JSValueRef::Undefined(vm);
68 }
69
SetControls(ArkUIRuntimeCallInfo * runtimeCallInfo)70 ArkUINativeModuleValue VideoBridge::SetControls(ArkUIRuntimeCallInfo* runtimeCallInfo)
71 {
72 EcmaVM* vm = runtimeCallInfo->GetVM();
73 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
74 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
75 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(1);
76 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
77 if (secondArg->IsBoolean()) {
78 uint32_t controlsValue = static_cast<uint32_t>(secondArg->ToBoolean(vm)->Value());
79 GetArkUINodeModifiers()->getVideoModifier()->setVideoControls(nativeNode, controlsValue);
80 } else {
81 GetArkUINodeModifiers()->getVideoModifier()->resetVideoControls(nativeNode);
82 }
83 return panda::JSValueRef::Undefined(vm);
84 }
85
ResetControls(ArkUIRuntimeCallInfo * runtimeCallInfo)86 ArkUINativeModuleValue VideoBridge::ResetControls(ArkUIRuntimeCallInfo* runtimeCallInfo)
87 {
88 EcmaVM* vm = runtimeCallInfo->GetVM();
89 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
90 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
91 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
92 GetArkUINodeModifiers()->getVideoModifier()->resetVideoControls(nativeNode);
93 return panda::JSValueRef::Undefined(vm);
94 }
95
SetObjectFit(ArkUIRuntimeCallInfo * runtimeCallInfo)96 ArkUINativeModuleValue VideoBridge::SetObjectFit(ArkUIRuntimeCallInfo* runtimeCallInfo)
97 {
98 EcmaVM* vm = runtimeCallInfo->GetVM();
99 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
100 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
101 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
102 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
103 if (secondArg->IsInt()) {
104 int32_t imageFit = secondArg->Int32Value(vm);
105 GetArkUINodeModifiers()->getVideoModifier()->setVideoObjectFit(nativeNode, imageFit);
106 } else {
107 GetArkUINodeModifiers()->getVideoModifier()->resetVideoObjectFit(nativeNode);
108 }
109 return panda::JSValueRef::Undefined(vm);
110 }
111
ResetObjectFit(ArkUIRuntimeCallInfo * runtimeCallInfo)112 ArkUINativeModuleValue VideoBridge::ResetObjectFit(ArkUIRuntimeCallInfo* runtimeCallInfo)
113 {
114 EcmaVM* vm = runtimeCallInfo->GetVM();
115 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
116 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
117 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
118 GetArkUINodeModifiers()->getVideoModifier()->resetVideoObjectFit(nativeNode);
119 return panda::JSValueRef::Undefined(vm);
120 }
121
SetLoop(ArkUIRuntimeCallInfo * runtimeCallInfo)122 ArkUINativeModuleValue VideoBridge::SetLoop(ArkUIRuntimeCallInfo* runtimeCallInfo)
123 {
124 EcmaVM* vm = runtimeCallInfo->GetVM();
125 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
126 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
127 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(1);
128 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
129 if (secondArg->IsBoolean()) {
130 uint32_t value = static_cast<uint32_t>(secondArg->ToBoolean(vm)->Value());
131 GetArkUINodeModifiers()->getVideoModifier()->setVideoLoop(nativeNode, value);
132 } else {
133 GetArkUINodeModifiers()->getVideoModifier()->resetVideoLoop(nativeNode);
134 }
135 return panda::JSValueRef::Undefined(vm);
136 }
137
ResetLoop(ArkUIRuntimeCallInfo * runtimeCallInfo)138 ArkUINativeModuleValue VideoBridge::ResetLoop(ArkUIRuntimeCallInfo* runtimeCallInfo)
139 {
140 EcmaVM* vm = runtimeCallInfo->GetVM();
141 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
142 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
143 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
144 GetArkUINodeModifiers()->getVideoModifier()->resetVideoLoop(nativeNode);
145 return panda::JSValueRef::Undefined(vm);
146 }
147
SetMuted(ArkUIRuntimeCallInfo * runtimeCallInfo)148 ArkUINativeModuleValue VideoBridge::SetMuted(ArkUIRuntimeCallInfo* runtimeCallInfo)
149 {
150 EcmaVM* vm = runtimeCallInfo->GetVM();
151 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
152 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
153 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(1);
154 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
155 if (secondArg->IsBoolean()) {
156 uint32_t value = static_cast<uint32_t>(secondArg->ToBoolean(vm)->Value());
157 GetArkUINodeModifiers()->getVideoModifier()->setVideoMuted(nativeNode, value);
158 } else {
159 GetArkUINodeModifiers()->getVideoModifier()->resetVideoMuted(nativeNode);
160 }
161 return panda::JSValueRef::Undefined(vm);
162 }
163
ResetMuted(ArkUIRuntimeCallInfo * runtimeCallInfo)164 ArkUINativeModuleValue VideoBridge::ResetMuted(ArkUIRuntimeCallInfo* runtimeCallInfo)
165 {
166 EcmaVM* vm = runtimeCallInfo->GetVM();
167 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
168 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
169 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
170 GetArkUINodeModifiers()->getVideoModifier()->resetVideoMuted(nativeNode);
171 return panda::JSValueRef::Undefined(vm);
172 }
173
SetOpacity(ArkUIRuntimeCallInfo * runtimeCallInfo)174 ArkUINativeModuleValue VideoBridge::SetOpacity(ArkUIRuntimeCallInfo* runtimeCallInfo)
175 {
176 EcmaVM* vm = runtimeCallInfo->GetVM();
177 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
178 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
179 Local<JSValueRef> opacityArg = runtimeCallInfo->GetCallArgRef(1);
180 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
181 double opacity;
182 if (!ArkTSUtils::ParseJsDouble(vm, opacityArg, opacity)) {
183 GetArkUINodeModifiers()->getVideoModifier()->resetVideoOpacity(nativeNode);
184 } else {
185 GetArkUINodeModifiers()->getVideoModifier()->setVideoOpacity(nativeNode, static_cast<ArkUI_Float32>(opacity));
186 }
187 return panda::JSValueRef::Undefined(vm);
188 }
189
ResetOpacity(ArkUIRuntimeCallInfo * runtimeCallInfo)190 ArkUINativeModuleValue VideoBridge::ResetOpacity(ArkUIRuntimeCallInfo* runtimeCallInfo)
191 {
192 EcmaVM *vm = runtimeCallInfo->GetVM();
193 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
194 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
195 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
196 GetArkUINodeModifiers()->getVideoModifier()->resetVideoOpacity(nativeNode);
197 return panda::JSValueRef::Undefined(vm);
198 }
199
SetTransition(ArkUIRuntimeCallInfo * runtimeCallInfo)200 ArkUINativeModuleValue VideoBridge::SetTransition(ArkUIRuntimeCallInfo* runtimeCallInfo)
201 {
202 EcmaVM* vm = runtimeCallInfo->GetVM();
203 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
204 CommonBridge::SetTransitionPassThrough(runtimeCallInfo);
205 return panda::JSValueRef::Undefined(vm);
206 }
207
ResetTransition(ArkUIRuntimeCallInfo * runtimeCallInfo)208 ArkUINativeModuleValue VideoBridge::ResetTransition(ArkUIRuntimeCallInfo* runtimeCallInfo)
209 {
210 EcmaVM* vm = runtimeCallInfo->GetVM();
211 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
212 CommonBridge::ResetTransitionPassThrough(runtimeCallInfo);
213 return panda::JSValueRef::Undefined(vm);
214 }
215
SetVideoSurfaceBackgroundColor(ArkUIRuntimeCallInfo * runtimeCallInfo)216 ArkUINativeModuleValue VideoBridge::SetVideoSurfaceBackgroundColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
217 {
218 EcmaVM* vm = runtimeCallInfo->GetVM();
219 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
220 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
221 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(1);
222 if (!firstArg->IsNativePointer(vm)) {
223 return panda::JSValueRef::Undefined(vm);
224 }
225 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
226 Color color;
227 if (!ParseColorMetricsToColor(vm, secondArg, color)) {
228 GetArkUINodeModifiers()->getVideoModifier()->resetVideoSurfaceBackgroundColor(nativeNode);
229 } else {
230 GetArkUINodeModifiers()->getVideoModifier()->setVideoSurfaceBackgroundColor(nativeNode, color.GetValue());
231 }
232 return panda::JSValueRef::Undefined(vm);
233 }
234
ResetVideoSurfaceBackgroundColor(ArkUIRuntimeCallInfo * runtimeCallInfo)235 ArkUINativeModuleValue VideoBridge::ResetVideoSurfaceBackgroundColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
236 {
237 EcmaVM* vm = runtimeCallInfo->GetVM();
238 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
239 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
240 if (!firstArg->IsNativePointer(vm)) {
241 return panda::JSValueRef::Undefined(vm);
242 }
243 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
244
245 GetArkUINodeModifiers()->getVideoModifier()->resetVideoSurfaceBackgroundColor(nativeNode);
246 return panda::JSValueRef::Undefined(vm);
247 }
248
SetShortcutKeyEnabled(ArkUIRuntimeCallInfo * runtimeCallInfo)249 ArkUINativeModuleValue VideoBridge::SetShortcutKeyEnabled(ArkUIRuntimeCallInfo* runtimeCallInfo)
250 {
251 EcmaVM* vm = runtimeCallInfo->GetVM();
252 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
253 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
254 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(1);
255 if (!firstArg->IsNativePointer(vm)) {
256 return panda::JSValueRef::Undefined(vm);
257 }
258 auto* nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
259 if (secondArg->IsBoolean()) {
260 uint32_t value = static_cast<uint32_t>(secondArg->ToBoolean(vm)->Value());
261 GetArkUINodeModifiers()->getVideoModifier()->setVideoShortcutKeyEnabled(nativeNode, value);
262 } else {
263 GetArkUINodeModifiers()->getVideoModifier()->resetVideoShortcutKeyEnabled(nativeNode);
264 }
265 return panda::JSValueRef::Undefined(vm);
266 }
267
ResetShortcutKeyEnabled(ArkUIRuntimeCallInfo * runtimeCallInfo)268 ArkUINativeModuleValue VideoBridge::ResetShortcutKeyEnabled(ArkUIRuntimeCallInfo* runtimeCallInfo)
269 {
270 EcmaVM* vm = runtimeCallInfo->GetVM();
271 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
272 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
273 if (!firstArg->IsNativePointer(vm)) {
274 return panda::JSValueRef::Undefined(vm);
275 }
276 auto* nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
277 GetArkUINodeModifiers()->getVideoModifier()->resetVideoShortcutKeyEnabled(nativeNode);
278 return panda::JSValueRef::Undefined(vm);
279 }
280 } // namespace OHOS::Ace::NG