1 /* 2 * Copyright (c) 2021-2022 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 FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_VIDEO_VIDEO_CONTROLLER_V2_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_VIDEO_VIDEO_CONTROLLER_V2_H 18 19 #include <algorithm> 20 #include <functional> 21 #include <list> 22 23 #include "core/components/video/video_utils.h" 24 #include "frameworks/base/memory/ace_type.h" 25 26 namespace OHOS::Ace { 27 class VideoController : public virtual AceType { 28 DECLARE_ACE_TYPE(VideoController, AceType); 29 30 public: 31 using StartImpl = std::function<void()>; 32 using PauseImpl = std::function<void()>; 33 using StopImpl = std::function<void()>; 34 using SeekToImpl = std::function<void(float, SeekMode)>; 35 using RequestFullscreenImpl = std::function<void(bool)>; 36 using ExitFullscreenImpl = std::function<void(bool)>; 37 Start()38 void Start() 39 { 40 if (startImpl_) { 41 startImpl_(); 42 } 43 } 44 Pause()45 void Pause() 46 { 47 if (pauseImpl_) { 48 pauseImpl_(); 49 } 50 } 51 Stop()52 void Stop() 53 { 54 if (stopImpl_) { 55 stopImpl_(); 56 } 57 } 58 59 void SeekTo(float pos, SeekMode seekMode = SeekMode::SEEK_PREVIOUS_SYNC) 60 { 61 if (seekToImpl_) { 62 seekToImpl_(pos, seekMode); 63 } 64 } 65 RequestFullscreen(bool isPortrait)66 void RequestFullscreen(bool isPortrait) 67 { 68 if (requestFullscreenImpl_) { 69 requestFullscreenImpl_(isPortrait); 70 } 71 } 72 ExitFullscreen(bool isSync)73 void ExitFullscreen(bool isSync) 74 { 75 if (exitFullscreenImpl_) { 76 exitFullscreenImpl_(isSync); 77 } 78 } 79 SetStartImpl(StartImpl && startImpl)80 void SetStartImpl(StartImpl&& startImpl) 81 { 82 startImpl_ = std::move(startImpl); 83 } 84 SetPausetImpl(PauseImpl && pauseImpl)85 void SetPausetImpl(PauseImpl&& pauseImpl) 86 { 87 pauseImpl_ = std::move(pauseImpl); 88 } 89 SetStopImpl(StopImpl && stopImpl)90 void SetStopImpl(StopImpl&& stopImpl) 91 { 92 stopImpl_ = std::move(stopImpl); 93 } 94 SetSeekToImpl(SeekToImpl && seekToImpl)95 void SetSeekToImpl(SeekToImpl&& seekToImpl) 96 { 97 seekToImpl_ = std::move(seekToImpl); 98 } 99 SetRequestFullscreenImpl(RequestFullscreenImpl && requestFullscreenImpl)100 void SetRequestFullscreenImpl(RequestFullscreenImpl&& requestFullscreenImpl) 101 { 102 requestFullscreenImpl_ = std::move(requestFullscreenImpl); 103 } 104 SetExitFullscreenImpl(ExitFullscreenImpl && exitFullscreenImpl)105 void SetExitFullscreenImpl(ExitFullscreenImpl&& exitFullscreenImpl) 106 { 107 exitFullscreenImpl_ = std::move(exitFullscreenImpl); 108 } 109 110 private: 111 StartImpl startImpl_; 112 PauseImpl pauseImpl_; 113 StopImpl stopImpl_; 114 SeekToImpl seekToImpl_; 115 RequestFullscreenImpl requestFullscreenImpl_; 116 ExitFullscreenImpl exitFullscreenImpl_; 117 }; 118 119 class VideoControllerV2 : public virtual AceType { 120 DECLARE_ACE_TYPE(VideoControllerV2, AceType); 121 122 public: Start()123 void Start() 124 { 125 for (const auto& item : controllers_) { 126 item->Start(); 127 } 128 } 129 Pause()130 void Pause() 131 { 132 for (const auto& item : controllers_) { 133 item->Pause(); 134 } 135 } 136 Stop()137 void Stop() 138 { 139 for (const auto& item : controllers_) { 140 item->Stop(); 141 } 142 } 143 144 void SeekTo(float pos, SeekMode seekMode = SeekMode::SEEK_PREVIOUS_SYNC) 145 { 146 for (const auto& item : controllers_) { 147 item->SeekTo(pos, seekMode); 148 } 149 } 150 RequestFullscreen(bool isPortrait)151 void RequestFullscreen(bool isPortrait) 152 { 153 for (const auto& item : controllers_) { 154 item->RequestFullscreen(isPortrait); 155 } 156 } 157 ExitFullscreen(bool isSync)158 void ExitFullscreen(bool isSync) 159 { 160 for (const auto& item : controllers_) { 161 item->ExitFullscreen(isSync); 162 } 163 } 164 AddVideoController(const RefPtr<VideoController> & videoController)165 void AddVideoController(const RefPtr<VideoController>& videoController) 166 { 167 auto it = std::find(controllers_.begin(), controllers_.end(), videoController); 168 if (it != controllers_.end()) { 169 LOGW("Controller is already existed"); 170 return; 171 } 172 173 controllers_.emplace_back(videoController); 174 } 175 RemoveVideoController(const RefPtr<VideoController> & videoController)176 void RemoveVideoController(const RefPtr<VideoController>& videoController) 177 { 178 if (videoController) { 179 controllers_.remove(videoController); 180 } 181 } 182 Clear()183 void Clear() 184 { 185 controllers_.clear(); 186 } 187 188 private: 189 std::list<RefPtr<VideoController>> controllers_; 190 }; 191 } // namespace OHOS::Ace 192 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_VIDEO_VIDEO_CONTROLLER_V2_H 193