1 /* 2 * Copyright (c) 2025 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 #ifndef OHOS_PICTURE_IN_PICTURE_CONTROLLER_BASE_H 17 #define OHOS_PICTURE_IN_PICTURE_CONTROLLER_BASE_H 18 19 #define PIP_WINDOW_NAME "pip_window" 20 21 #include <ability_context.h> 22 #include <refbase.h> 23 #include "navigation_controller.h" 24 #include "picture_in_picture_interface.h" 25 #include "picture_in_picture_option.h" 26 #include "pip_report.h" 27 #include "xcomponent_controller.h" 28 #include "window.h" 29 #include "wm_common.h" 30 31 namespace OHOS { 32 namespace Rosen { 33 enum class StartPipType : uint32_t { 34 NULL_START = 0, 35 USER_START = 1, 36 AUTO_START = 2, 37 ONE_STEP_START = 3, 38 NATIVE_START = 4, 39 }; 40 41 enum class StopPipType : uint32_t { 42 NULL_STOP = 0, 43 USER_STOP = 1, 44 OTHER_PACKAGE_STOP = 2, 45 PACKAGE_STOP = 3, 46 NATIVE_STOP = 4, 47 }; 48 49 namespace PipConst { 50 constexpr int32_t PIP_SUCCESS = 1; 51 constexpr int32_t FAILED = 0; 52 const int DEFAULT_ASPECT_RATIOS[] = {16, 9}; 53 } 54 55 using namespace Ace; 56 class PictureInPictureControllerBase : public RefBase { 57 public: PictureInPictureControllerBase()58 PictureInPictureControllerBase() { weakRef_ = this; } 59 PictureInPictureControllerBase(sptr<PipOption> pipOption, sptr<Window> mainWindow, uint32_t windowId, napi_env env); 60 virtual ~PictureInPictureControllerBase(); 61 WMError StopPictureInPicture(bool destroyWindow, StopPipType stopPipType, bool withAnim = true); 62 WMError StopPictureInPictureFromClient(); 63 WMError DestroyPictureInPictureWindow(); 64 sptr<Window> GetPipWindow() const; 65 uint32_t GetMainWindowId() const; 66 void SetControllerId(uint32_t controllerId); 67 uint32_t GetControllerId() const; 68 void SetPipWindow(sptr<Window> window); 69 void UpdatePiPControlStatus(PiPControlType controlType, PiPControlStatus status); 70 bool IsContentSizeChanged(float width, float height, float posX, float posY); 71 void DoActionEvent(const std::string& actionName, int32_t status); 72 void DoControlEvent(PiPControlType controlType, PiPControlStatus status); 73 void LocateSource(); 74 void PipSizeChange(double width, double height, double scale); 75 void PreRestorePictureInPicture(); 76 void GetPipPossible(bool& pipPossible); 77 WMError RegisterPiPLifecycle(const sptr<IPiPLifeCycle>& listener); 78 WMError RegisterPiPActionObserver(const sptr<IPiPActionObserver>& listener); 79 WMError RegisterPiPControlObserver(const sptr<IPiPControlObserver>& listener); 80 WMError RegisterPiPWindowSize(const sptr<IPiPWindowSize>& listener); 81 WMError RegisterPiPTypeNodeChange(const sptr<IPiPTypeNodeObserver>& listener); 82 WMError RegisterPiPStart(const sptr<IPiPStartObserver>& listener); 83 WMError UnregisterPiPLifecycle(const sptr<IPiPLifeCycle>& listener); 84 WMError UnregisterPiPActionObserver(const sptr<IPiPActionObserver>& listener); 85 WMError UnregisterPiPControlObserver(const sptr<IPiPControlObserver>& listener); 86 WMError UnregisterPiPWindowSize(const sptr<IPiPWindowSize>& listener); 87 WMError UnRegisterPiPTypeNodeChange(const sptr<IPiPTypeNodeObserver>& listener); 88 WMError UnregisterPiPStart(const sptr<IPiPStartObserver>& listener); 89 void UnregisterAllPiPLifecycle(); 90 void UnregisterAllPiPControlObserver(); 91 void UnregisterAllPiPWindowSize(); 92 void UnregisterAllPiPStart(); 93 std::vector<sptr<IPiPLifeCycle>> GetPictureInPictureLifecycle() const; 94 std::vector<sptr<IPiPActionObserver>> GetPictureInPictureActionObserver() const; 95 std::vector<sptr<IPiPControlObserver>> GetPictureInPictureControlObserver() const; 96 std::vector<sptr<IPiPWindowSize>> GetPictureInPictureSizeObserver() const; 97 std::vector<sptr<IPiPStartObserver>> GetPictureInPictureStartObserver() const; 98 PiPWindowState GetControllerState() const; 99 void OnPictureInPictureStart(); 100 void SetSurfaceId(uint64_t surfaceId); 101 static bool GetPipEnabled(); 102 uint64_t GetSurfaceId() const; 103 bool GetPipSettingSwitchStatusEnabled(); 104 bool GetPiPSettingSwitchStatus(); 105 bool isWeb_ = false; 106 107 // diffrent between normal and web 108 virtual WMError StartPictureInPicture(StartPipType startType) = 0; 109 virtual void UpdateContentSize(int32_t width, int32_t height) = 0; 110 virtual WMError SetXComponentController(std::shared_ptr<XComponentController> xComponentController) = 0; 111 virtual void RestorePictureInPictureWindow() = 0; 112 113 // normal SetAutoStartEnabled(bool enable)114 virtual void SetAutoStartEnabled(bool enable) {} IsAutoStartEnabled(bool & enable)115 virtual void IsAutoStartEnabled(bool& enable) const {}; UpdateContentNodeRef(napi_ref nodeRef)116 virtual void UpdateContentNodeRef(napi_ref nodeRef) {}; PrepareSource()117 virtual void PrepareSource() {}; GetCustomNodeController()118 virtual napi_ref GetCustomNodeController() { return nullptr; }; GetTypeNode()119 virtual napi_ref GetTypeNode() const { return nullptr; }; IsTypeNodeEnabled()120 virtual bool IsTypeNodeEnabled() const { return false; }; 121 122 // web GetWebRequestId()123 virtual uint8_t GetWebRequestId() { return 0; }; SetPipInitialSurfaceRect(int32_t positionX,int32_t positionY,uint32_t width,uint32_t height)124 virtual void SetPipInitialSurfaceRect(int32_t positionX, int32_t positionY, uint32_t width, uint32_t height) {}; 125 126 protected: 127 class WindowLifeCycleListener : public IWindowLifeCycle { 128 public: 129 void AfterDestroyed() override; 130 }; 131 132 void NotifyOpretationError(WMError errCode, StartPipType startType); 133 WMError ShowPictureInPictureWindow(StartPipType startType); 134 WMError StartPictureInPictureInner(StartPipType startType); 135 WMError StopPictureInPictureInner(StopPipType stopType, bool withAnim); 136 template<typename T> WMError RegisterListener(std::vector<sptr<T>>& holder, const sptr<T>& listener); 137 template<typename T> WMError UnregisterListener(std::vector<sptr<T>>& holder, const sptr<T>& listener); 138 template<typename T> WMError WebRegisterListener(std::vector<sptr<T>>& holder, const sptr<T>& listener); 139 template<typename T> WMError WebUnregisterListener(std::vector<sptr<T>>& holder, const sptr<T>& listener); 140 141 sptr<PipOption> pipOption_ = nullptr; 142 uint32_t controllerId_ = 0; 143 std::vector<sptr<IPiPLifeCycle>> pipLifeCycleListeners_; 144 std::vector<sptr<IPiPActionObserver>> pipActionObservers_; 145 std::vector<sptr<IPiPControlObserver>> pipControlObservers_; 146 std::vector<sptr<IPiPWindowSize>> pipWindowSizeListeners_; 147 std::vector<sptr<IPiPTypeNodeObserver>> pipTypeNodeObserver_; 148 std::vector<sptr<IPiPStartObserver>> pipStartListeners_; 149 sptr<Window> window_ = nullptr; 150 sptr<Window> mainWindow_ = nullptr; 151 sptr<IWindowLifeCycle> mainWindowLifeCycleListener_ = nullptr; 152 uint32_t mainWindowId_ = 0; 153 Rect windowRect_ = {0, 0, 0, 0}; 154 bool isAutoStartEnabled_ = false; 155 PiPWindowState curState_ = PiPWindowState::STATE_UNDEFINED; 156 std::shared_ptr<XComponentController> pipXComponentController_ = nullptr; 157 napi_env env_ = nullptr; 158 bool isStoppedFromClient_ = false; 159 int32_t handleId_ = -1; 160 uint64_t surfaceId_ = 0; 161 162 // diffrent between normal and web 163 virtual WMError CreatePictureInPictureWindow(StartPipType startType) = 0; 164 virtual void UpdateWinRectByComponent() = 0; 165 virtual void SetUIContent() const = 0; 166 virtual void UpdatePiPSourceRect() const = 0; 167 168 // normal ResetExtController()169 virtual void ResetExtController() {}; NotifyNodeUpdate(napi_ref nodeRef)170 virtual void NotifyNodeUpdate(napi_ref nodeRef) {}; 171 172 private: 173 wptr<PictureInPictureControllerBase> weakRef_ = nullptr; 174 }; 175 } // namespace Rosen 176 } // namespace OHOS 177 #endif // OHOS_PICTURE_IN_PICTURE_CONTROLLER_H