1 /*
2 * Copyright (c) 2023-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 "js_pip_window_manager.h"
17
18 #include "js_pip_controller.h"
19 #include "js_pip_utils.h"
20 #include "js_runtime_utils.h"
21 #include "window_manager_hilog.h"
22 #include "window.h"
23 #include "xcomponent_controller.h"
24
25 namespace OHOS {
26 namespace Rosen {
27 using namespace AbilityRuntime;
28 using namespace Ace;
29 namespace {
30 constexpr uint32_t MAX_CONTROL_GROUP_NUM = 3;
31 const std::set<PiPControlGroup> VIDEO_PLAY_CONTROLS {
32 PiPControlGroup::VIDEO_PREVIOUS_NEXT,
33 PiPControlGroup::FAST_FORWARD_BACKWARD,
34 };
35 const std::set<PiPControlGroup> VIDEO_CALL_CONTROLS {
36 PiPControlGroup::VIDEO_CALL_MICROPHONE_SWITCH,
37 PiPControlGroup::VIDEO_CALL_HANG_UP_BUTTON,
38 PiPControlGroup::VIDEO_CALL_CAMERA_SWITCH,
39 PiPControlGroup::VIDEO_CALL_MUTE_SWITCH,
40 };
41 const std::set<PiPControlGroup> VIDEO_MEETING_CONTROLS {
42 PiPControlGroup::VIDEO_MEETING_HANG_UP_BUTTON,
43 PiPControlGroup::VIDEO_MEETING_CAMERA_SWITCH,
44 PiPControlGroup::VIDEO_MEETING_MUTE_SWITCH,
45 PiPControlGroup::VIDEO_MEETING_MICROPHONE_SWITCH,
46 };
47 const std::set<PiPControlGroup> VIDEO_LIVE_CONTROLS {
48 PiPControlGroup::VIDEO_PLAY_PAUSE,
49 PiPControlGroup::VIDEO_LIVE_MUTE_SWITCH,
50 };
51 const std::map<PiPTemplateType, std::set<PiPControlGroup>> TEMPLATE_CONTROL_MAP {
52 {PiPTemplateType::VIDEO_PLAY, VIDEO_PLAY_CONTROLS},
53 {PiPTemplateType::VIDEO_CALL, VIDEO_CALL_CONTROLS},
54 {PiPTemplateType::VIDEO_MEETING, VIDEO_MEETING_CONTROLS},
55 {PiPTemplateType::VIDEO_LIVE, VIDEO_LIVE_CONTROLS},
56 };
57 }
58
checkControlsRules(uint32_t pipTemplateType,std::vector<std::uint32_t> controlGroups)59 static int32_t checkControlsRules(uint32_t pipTemplateType, std::vector<std::uint32_t> controlGroups)
60 {
61 auto iter = TEMPLATE_CONTROL_MAP.find(static_cast<PiPTemplateType>(pipTemplateType));
62 auto controls = iter->second;
63 if (controlGroups.size() > MAX_CONTROL_GROUP_NUM) {
64 return -1;
65 }
66 for (auto control : controlGroups) {
67 if (controls.find(static_cast<PiPControlGroup>(control)) == controls.end()) {
68 TLOGE(WmsLogTag::WMS_PIP, "pipOption param error, controlGroup not matches, controlGroup: %{public}u",
69 control);
70 return -1;
71 }
72 }
73 if (pipTemplateType == static_cast<uint32_t>(PiPTemplateType::VIDEO_PLAY)) {
74 auto iterFirst = std::find(controlGroups.begin(), controlGroups.end(),
75 static_cast<uint32_t>(PiPControlGroup::VIDEO_PREVIOUS_NEXT));
76 auto iterSecond = std::find(controlGroups.begin(), controlGroups.end(),
77 static_cast<uint32_t>(PiPControlGroup::FAST_FORWARD_BACKWARD));
78 if (iterFirst != controlGroups.end() && iterSecond != controlGroups.end()) {
79 TLOGE(WmsLogTag::WMS_PIP, "pipOption param error, %{public}u conflicts with %{public}u in controlGroups",
80 static_cast<uint32_t>(PiPControlGroup::VIDEO_PREVIOUS_NEXT),
81 static_cast<uint32_t>(PiPControlGroup::FAST_FORWARD_BACKWARD));
82 return -1;
83 }
84 }
85 return 0;
86 }
87
checkOptionParams(PipOption & option)88 static int32_t checkOptionParams(PipOption& option)
89 {
90 if (option.GetContext() == nullptr) {
91 TLOGE(WmsLogTag::WMS_PIP, "pipOption param error, context is nullptr.");
92 return -1;
93 }
94 if (option.GetXComponentController() == nullptr) {
95 TLOGE(WmsLogTag::WMS_PIP, "pipOption param error, XComponentController is nullptr.");
96 return -1;
97 }
98 uint32_t pipTemplateType = option.GetPipTemplate();
99 if (TEMPLATE_CONTROL_MAP.find(static_cast<PiPTemplateType>(pipTemplateType)) ==
100 TEMPLATE_CONTROL_MAP.end()) {
101 TLOGE(WmsLogTag::WMS_PIP, "pipOption param error, pipTemplateType not exists.");
102 return -1;
103 }
104 return checkControlsRules(pipTemplateType, option.GetControlGroup());
105 }
106
GetControlGroupFromJs(napi_env env,napi_value controlGroup,std::vector<std::uint32_t> & controls,uint32_t templateType)107 static bool GetControlGroupFromJs(napi_env env, napi_value controlGroup, std::vector<std::uint32_t>& controls,
108 uint32_t templateType)
109 {
110 if (controlGroup == nullptr) {
111 return false;
112 }
113 napi_valuetype type;
114 napi_typeof(env, controlGroup, &type);
115 if (type == napi_undefined && templateType == static_cast<uint32_t>(PiPTemplateType::VIDEO_LIVE)) {
116 TLOGI(WmsLogTag::WMS_PIP, "controls is undefined");
117 controls.push_back(static_cast<uint32_t>(PiPControlGroup::VIDEO_PLAY_PAUSE));
118 }
119 uint32_t size = 0;
120 napi_get_array_length(env, controlGroup, &size);
121 for (uint32_t i = 0; i < size; i++) {
122 uint32_t controlType;
123 napi_value getElementValue = nullptr;
124 napi_get_element(env, controlGroup, i, &getElementValue);
125 if (!ConvertFromJsValue(env, getElementValue, controlType)) {
126 TLOGE(WmsLogTag::WMS_PIP, "Failed to convert parameter to controlType");
127 continue;
128 }
129 auto iter = std::find(controls.begin(), controls.end(), controlType);
130 if (iter != controls.end()) {
131 TLOGI(WmsLogTag::WMS_PIP, "The controlType already exists. controlType: %{public}u", controlType);
132 } else {
133 controls.push_back(controlType);
134 }
135 }
136 return true;
137 }
138
GetPictureInPictureOptionFromJs(napi_env env,napi_value optionObject,PipOption & option)139 static int32_t GetPictureInPictureOptionFromJs(napi_env env, napi_value optionObject, PipOption& option)
140 {
141 napi_value contextPtrValue = nullptr;
142 napi_value navigationIdValue = nullptr;
143 napi_value templateTypeValue = nullptr;
144 napi_value widthValue = nullptr;
145 napi_value heightValue = nullptr;
146 napi_value xComponentControllerValue = nullptr;
147 napi_value controlGroup = nullptr;
148 napi_value nodeController = nullptr;
149 napi_ref nodeControllerRef = nullptr;
150 void* contextPtr = nullptr;
151 std::string navigationId = "";
152 uint32_t templateType = static_cast<uint32_t>(PiPTemplateType::VIDEO_PLAY);
153 uint32_t width = 0;
154 uint32_t height = 0;
155 std::vector<std::uint32_t> controls;
156
157 napi_get_named_property(env, optionObject, "context", &contextPtrValue);
158 napi_get_named_property(env, optionObject, "navigationId", &navigationIdValue);
159 napi_get_named_property(env, optionObject, "templateType", &templateTypeValue);
160 napi_get_named_property(env, optionObject, "contentWidth", &widthValue);
161 napi_get_named_property(env, optionObject, "contentHeight", &heightValue);
162 napi_get_named_property(env, optionObject, "componentController", &xComponentControllerValue);
163 napi_get_named_property(env, optionObject, "controlGroups", &controlGroup);
164 napi_get_named_property(env, optionObject, "customUIController", &nodeController);
165 napi_create_reference(env, nodeController, 1, &nodeControllerRef);
166 napi_unwrap(env, contextPtrValue, &contextPtr);
167 ConvertFromJsValue(env, navigationIdValue, navigationId);
168 ConvertFromJsValue(env, templateTypeValue, templateType);
169 ConvertFromJsValue(env, widthValue, width);
170 ConvertFromJsValue(env, heightValue, height);
171 GetControlGroupFromJs(env, controlGroup, controls, templateType);
172 std::shared_ptr<XComponentController> xComponentControllerResult =
173 XComponentController::GetXComponentControllerFromNapiValue(env, xComponentControllerValue);
174 option.SetContext(contextPtr);
175 option.SetNavigationId(navigationId);
176 option.SetPipTemplate(templateType);
177 option.SetContentSize(width, height);
178 option.SetControlGroup(controls);
179 option.SetXComponentController(xComponentControllerResult);
180 option.SetNodeControllerRef(nodeControllerRef);
181 return checkOptionParams(option);
182 }
183
JsPipWindowManager()184 JsPipWindowManager::JsPipWindowManager()
185 {
186 }
187
~JsPipWindowManager()188 JsPipWindowManager::~JsPipWindowManager()
189 {
190 }
191
Finalizer(napi_env env,void * data,void * hint)192 void JsPipWindowManager::Finalizer(napi_env env, void* data, void* hint)
193 {
194 TLOGD(WmsLogTag::WMS_PIP, "Finalizer");
195 std::unique_ptr<JsPipWindowManager>(static_cast<JsPipWindowManager*>(data));
196 }
197
IsPipEnabled(napi_env env,napi_callback_info info)198 napi_value JsPipWindowManager::IsPipEnabled(napi_env env, napi_callback_info info)
199 {
200 JsPipWindowManager* me = CheckParamsAndGetThis<JsPipWindowManager>(env, info);
201 return (me != nullptr) ? me->OnIsPipEnabled(env, info) : nullptr;
202 }
203
OnIsPipEnabled(napi_env env,napi_callback_info info)204 napi_value JsPipWindowManager::OnIsPipEnabled(napi_env env, napi_callback_info info)
205 {
206 TLOGD(WmsLogTag::WMS_PIP, "OnIsPipEnabled called");
207 return CreateJsValue(env, true);
208 }
209
CreatePipController(napi_env env,napi_callback_info info)210 napi_value JsPipWindowManager::CreatePipController(napi_env env, napi_callback_info info)
211 {
212 JsPipWindowManager* me = CheckParamsAndGetThis<JsPipWindowManager>(env, info);
213 return (me != nullptr) ? me->OnCreatePipController(env, info) : nullptr;
214 }
215
CreateEmptyAsyncTask(napi_env env,napi_value * result)216 std::unique_ptr<NapiAsyncTask> JsPipWindowManager::CreateEmptyAsyncTask(napi_env env, napi_value* result)
217 {
218 napi_deferred nativeDeferred = nullptr;
219 napi_create_promise(env, &nativeDeferred, result);
220 return std::make_unique<NapiAsyncTask>(nativeDeferred, std::unique_ptr<NapiAsyncTask::ExecuteCallback>(),
221 std::unique_ptr<NapiAsyncTask::CompleteCallback>());
222 }
223
NapiSendTask(napi_env env,PipOption & pipOption)224 napi_value JsPipWindowManager::NapiSendTask(napi_env env, PipOption& pipOption)
225 {
226 napi_value result = nullptr;
227 std::shared_ptr<NapiAsyncTask> napiAsyncTask = CreateEmptyAsyncTask(env, &result);
228 auto asyncTask = [this, env, task = napiAsyncTask, pipOption]() {
229 if (!PictureInPictureManager::IsSupportPiP()) {
230 task->Reject(env, CreateJsError(env, static_cast<int32_t>(
231 WMError::WM_ERROR_DEVICE_NOT_SUPPORT), "device not support pip."));
232 return;
233 }
234 sptr<PipOption> pipOptionPtr = new PipOption(pipOption);
235 auto context = static_cast<std::weak_ptr<AbilityRuntime::Context>*>(pipOptionPtr->GetContext());
236 if (context == nullptr) {
237 task->Reject(env, CreateJsError(env, static_cast<int32_t>(
238 WMError::WM_ERROR_PIP_INTERNAL_ERROR), "Invalid context"));
239 return;
240 }
241 sptr<Window> mainWindow = Window::GetMainWindowWithContext(context->lock());
242 if (mainWindow == nullptr) {
243 task->Reject(env, CreateJsError(env, static_cast<int32_t>(
244 WMError::WM_ERROR_PIP_INTERNAL_ERROR), "Invalid mainWindow"));
245 return;
246 }
247 sptr<PictureInPictureController> pipController =
248 new PictureInPictureController(pipOptionPtr, mainWindow, mainWindow->GetWindowId(), env);
249 task->Resolve(env, CreateJsPipControllerObject(env, pipController));
250 };
251 if (napi_status::napi_ok != napi_send_event(env, asyncTask, napi_eprio_immediate)) {
252 napiAsyncTask->Reject(env, CreateJsError(env,
253 static_cast<int32_t>(WMError::WM_ERROR_PIP_INTERNAL_ERROR), "Send event failed"));
254 }
255 return result;
256 }
257
OnCreatePipController(napi_env env,napi_callback_info info)258 napi_value JsPipWindowManager::OnCreatePipController(napi_env env, napi_callback_info info)
259 {
260 TLOGI(WmsLogTag::WMS_PIP, "[NAPI]");
261 size_t argc = 4;
262 napi_value argv[4] = {nullptr};
263 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
264 if (argc < 1) {
265 return NapiThrowInvalidParam(env, "Missing args when creating pipController");
266 }
267 napi_value config = argv[0];
268 if (config == nullptr) {
269 TLOGE(WmsLogTag::WMS_PIP, "config is null");
270 return NapiThrowInvalidParam(env, "Failed to convert object to pipConfiguration or pipConfiguration is null");
271 }
272 PipOption pipOption;
273 if (GetPictureInPictureOptionFromJs(env, config, pipOption) == -1) {
274 std::string errMsg = "Invalid parameters in config, please check if context/xComponentController is null,"
275 " or controlGroup mismatch the corresponding pipTemplateType";
276 TLOGE(WmsLogTag::WMS_PIP, "%{public}s", errMsg.c_str());
277 return NapiThrowInvalidParam(env, errMsg);
278 }
279 if (argc > 1) {
280 napi_value typeNode = argv[1];
281 napi_ref typeNodeRef = nullptr;
282 if (typeNode != nullptr && GetType(env, typeNode) != napi_undefined) {
283 TLOGI(WmsLogTag::WMS_PIP, "typeNode enabled");
284 pipOption.SetTypeNodeEnabled(true);
285 napi_create_reference(env, typeNode, 1, &typeNodeRef);
286 pipOption.SetTypeNodeRef(typeNodeRef);
287 } else {
288 pipOption.SetTypeNodeEnabled(false);
289 pipOption.SetTypeNodeRef(nullptr);
290 }
291 }
292 return NapiSendTask(env, pipOption);
293 }
294
JsPipWindowManagerInit(napi_env env,napi_value exportObj)295 napi_value JsPipWindowManagerInit(napi_env env, napi_value exportObj)
296 {
297 TLOGD(WmsLogTag::WMS_PIP, "JsPipWindowManagerInit");
298 if (env == nullptr || exportObj == nullptr) {
299 TLOGE(WmsLogTag::WMS_PIP, "JsPipWindowManagerInit env or exportObj is nullptr");
300 return nullptr;
301 }
302 std::unique_ptr<JsPipWindowManager> jsPipManager = std::make_unique<JsPipWindowManager>();
303 napi_wrap(env, exportObj, jsPipManager.release(), JsPipWindowManager::Finalizer, nullptr, nullptr);
304 const char* moduleName = "JsPipWindowManager";
305 BindNativeFunction(env, exportObj, "create", moduleName, JsPipWindowManager::CreatePipController);
306 BindNativeFunction(env, exportObj, "isPiPEnabled", moduleName, JsPipWindowManager::IsPipEnabled);
307 InitEnums(env, exportObj);
308 return NapiGetUndefined(env);
309 }
310 }
311 }