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 #ifndef OHOS_PICTURE_IN_PICTURE_CONTROLLER_H 17 #define OHOS_PICTURE_IN_PICTURE_CONTROLLER_H 18 19 #define PIP_WINDOW_NAME "pip_window" 20 21 #include <refbase.h> 22 #include <ability_context.h> 23 #include "picture_in_picture_option.h" 24 #include "window.h" 25 #include "wm_common.h" 26 #include "picture_in_picture_interface.h" 27 #include "xcomponent_controller.h" 28 #include "pip_report.h" 29 #include "navigation_controller.h" 30 31 namespace OHOS { 32 namespace Rosen { 33 34 enum class StartPipType : uint32_t { 35 NULL_START = 0, 36 USER_START = 1, 37 AUTO_START = 2, 38 ONE_STEP_START = 3, 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 }; 47 48 static std::map<std::string, PiPControlType> CONTROL_TYPE_MAP = { 49 {"playbackStateChanged", PiPControlType::VIDEO_PLAY_PAUSE}, 50 {"nextVideo", PiPControlType::VIDEO_NEXT}, 51 {"previousVideo", PiPControlType::VIDEO_PREVIOUS}, 52 {"hangUp", PiPControlType::HANG_UP_BUTTON}, 53 {"micStateChanged", PiPControlType::MICROPHONE_SWITCH}, 54 {"videoStateChanged", PiPControlType::CAMERA_SWITCH}, 55 {"voiceStateChanged", PiPControlType::MUTE_SWITCH}, 56 {"fastForward", PiPControlType::FAST_FORWARD}, 57 {"fastBackward", PiPControlType::FAST_BACKWARD} 58 }; 59 60 using namespace Ace; 61 class PictureInPictureController : virtual public RefBase { 62 public: 63 PictureInPictureController(sptr<PipOption> pipOption, sptr<Window> mainWindow, uint32_t mainWindowId, napi_env env); 64 ~PictureInPictureController(); 65 WMError StartPictureInPicture(StartPipType startType); 66 WMError StopPictureInPicture(bool destroyWindow, StopPipType stopPipType, bool withAnim = true); 67 WMError StopPictureInPictureFromClient(); 68 WMError DestroyPictureInPictureWindow(); 69 sptr<Window> GetPipWindow() const; 70 uint32_t GetMainWindowId(); 71 void SetPipWindow(sptr<Window> window); 72 void SetAutoStartEnabled(bool enable); 73 void IsAutoStartEnabled(bool& enable) const; 74 void UpdateContentSize(int32_t width, int32_t height); 75 void UpdateContentNodeRef(napi_ref nodeRef); 76 void UpdatePiPControlStatus(PiPControlType controlType, PiPControlStatus status); 77 bool IsContentSizeChanged(float width, float height, float posX, float posY); 78 void DoActionEvent(const std::string& actionName, int32_t status); 79 void DoControlEvent(PiPControlType controlType, PiPControlStatus status); 80 void PipSizeChange(double width, double height, double scale); 81 void PreRestorePictureInPicture(); 82 void RestorePictureInPictureWindow(); 83 void PrepareSource(); 84 void LocateSource(); 85 void GetPipPossible(bool& pipPossible); 86 WMError RegisterPiPLifecycle(const sptr<IPiPLifeCycle>& listener); 87 WMError RegisterPiPActionObserver(const sptr<IPiPActionObserver>& listener); 88 WMError RegisterPiPControlObserver(const sptr<IPiPControlObserver>& listener); 89 WMError RegisterPiPWindowSize(const sptr<IPiPWindowSize>& listener); 90 WMError RegisterPipContentListenerWithType(const std::string&, 91 std::shared_ptr<NativeReference> updateNodeCallbackRef); 92 WMError UnregisterPiPLifecycle(const sptr<IPiPLifeCycle>& listener); 93 WMError UnregisterPiPActionObserver(const sptr<IPiPActionObserver>& listener); 94 WMError UnregisterPiPControlObserver(const sptr<IPiPControlObserver>& listener); 95 WMError UnregisterPiPWindowSize(const sptr<IPiPWindowSize>& listener); 96 WMError UnRegisterPipContentListenerWithType(const std::string&); 97 sptr<IPiPLifeCycle> GetPictureInPictureLifecycle() const; 98 sptr<IPiPActionObserver> GetPictureInPictureActionObserver() const; 99 sptr<IPiPControlObserver> GetPictureInPictureControlObserver() const; 100 std::shared_ptr<NativeReference> GetPipContentCallbackRef(const std::string&); 101 WMError SetXComponentController(std::shared_ptr<XComponentController> xComponentController); 102 PiPWindowState GetControllerState(); 103 std::string GetPiPNavigationId(); 104 napi_ref GetCustomNodeController(); 105 napi_ref GetTypeNode() const; 106 void OnPictureInPictureStart(); 107 bool IsTypeNodeEnabled() const; 108 109 private: 110 class WindowLifeCycleListener : public IWindowLifeCycle { 111 public: 112 void AfterDestroyed() override; 113 }; 114 115 private: 116 uint32_t GetPipPriority(uint32_t pipTemplateType); 117 WMError CreatePictureInPictureWindow(StartPipType startType); 118 WMError ShowPictureInPictureWindow(StartPipType startType); 119 WMError StartPictureInPictureInner(StartPipType startType); 120 WMError StopPictureInPictureInner(StopPipType stopType, bool withAnim); 121 void UpdateWinRectByComponent(); 122 void SetUIContent() const; 123 void UpdatePiPSourceRect() const; 124 void ResetExtController(); 125 bool IsPullPiPAndHandleNavigation(); 126 template<typename T> WMError RegisterListener(std::vector<sptr<T>>& holder, const sptr<T>& listener); 127 template<typename T> WMError UnregisterListener(std::vector<sptr<T>>& holder, const sptr<T>& listener); 128 void NotifyNodeUpdate(napi_ref nodeRef); 129 void NotifyStateChangeInner(napi_env env, PiPState state); 130 wptr<PictureInPictureController> weakRef_ = nullptr; 131 sptr<PipOption> pipOption_ = nullptr; 132 std::vector<sptr<IPiPLifeCycle>> pipLifeCycleListeners_; 133 std::vector<sptr<IPiPActionObserver>> pipActionObservers_; 134 std::vector<sptr<IPiPControlObserver>> pipControlObservers_; 135 std::vector<sptr<IPiPWindowSize>> pipWindowSizeListeners_; 136 sptr<Window> window_ = nullptr; 137 sptr<Window> mainWindow_ = nullptr; 138 sptr<IWindowLifeCycle> mainWindowLifeCycleListener_ = nullptr; 139 uint32_t mainWindowId_ = 0; 140 Rect windowRect_ = {0, 0, 0, 0}; 141 bool isAutoStartEnabled_ = false; 142 PiPWindowState curState_ = PiPWindowState::STATE_UNDEFINED; 143 std::shared_ptr<XComponentController> pipXComponentController_ = nullptr; 144 std::shared_ptr<XComponentController> mainWindowXComponentController_ = nullptr; 145 napi_env env_ = nullptr; 146 int32_t handleId_ = -1; 147 bool isStoppedFromClient_ = false; 148 int32_t firstHandleId_ = -1; 149 }; 150 } // namespace Rosen 151 } // namespace OHOS 152 #endif // OHOS_PICTURE_IN_PICTURE_CONTROLLER_H 153