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