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