• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_VIDEO_VIDEO_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_VIDEO_VIDEO_PATTERN_H
18 #include "base/geometry/dimension.h"
19 #include "base/geometry/size.h"
20 #include "base/memory/referenced.h"
21 #include "base/utils/noncopyable.h"
22 #include "core/components/video/video_controller_v2.h"
23 #include "core/components_ng/image_provider/image_loading_context.h"
24 #include "core/components_ng/pattern/pattern.h"
25 #include "core/components_ng/pattern/video/video_accessibility_property.h"
26 #include "core/components_ng/pattern/video/video_event_hub.h"
27 #include "core/components_ng/pattern/video/video_layout_algorithm.h"
28 #include "core/components_ng/pattern/video/video_layout_property.h"
29 #include "core/components_ng/property/property.h"
30 #include "core/components_ng/render/media_player.h"
31 #include "core/components_ng/render/render_surface.h"
32 #include "core/pipeline_ng/pipeline_context.h"
33 #include "frameworks/base/geometry/rect.h"
34 
35 namespace OHOS::Ace::NG {
36 class VideoPattern : public Pattern {
37     DECLARE_ACE_TYPE(VideoPattern, Pattern);
38 
39 public:
40     using HiddenChangeEvent = std::function<void(bool)>;
41 
42     VideoPattern() = default;
43     explicit VideoPattern(const RefPtr<VideoControllerV2>& videoController);
44     ~VideoPattern() override;
45 
CreateEventHub()46     RefPtr<EventHub> CreateEventHub() override
47     {
48         return MakeRefPtr<VideoEventHub>();
49     }
50 
CreateLayoutProperty()51     RefPtr<LayoutProperty> CreateLayoutProperty() override
52     {
53         return MakeRefPtr<VideoLayoutProperty>();
54     }
55 
CreateLayoutAlgorithm()56     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
57     {
58         return MakeRefPtr<VideoLayoutAlgorithm>();
59     }
60 
CreateAccessibilityProperty()61     RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override
62     {
63         return MakeRefPtr<VideoAccessibilityProperty>();
64     }
65 
DefaultSupportDrag()66     bool DefaultSupportDrag() override
67     {
68         return true;
69     }
70 
UpdateMuted(bool muted)71     void UpdateMuted(bool muted)
72     {
73         muted_ = muted;
74     }
75 
GetMuted()76     bool GetMuted() const
77     {
78         return muted_;
79     }
80 
UpdateAutoPlay(bool autoPlay)81     void UpdateAutoPlay(bool autoPlay)
82     {
83         autoPlay_ = autoPlay;
84     }
85 
GetAutoPlay()86     bool GetAutoPlay() const
87     {
88         return autoPlay_;
89     }
90 
UpdateLoop(bool loop)91     void UpdateLoop(bool loop)
92     {
93         loop_ = loop;
94     }
95 
GetLoop()96     bool GetLoop() const
97     {
98         return loop_;
99     }
100 
101     virtual bool IsFullScreen();
102 
103     void OnColorConfigurationUpdate() override;
UpdateProgressRate(double progressRate)104     void UpdateProgressRate(double progressRate)
105     {
106         progressRate_ = progressRate;
107     }
108 
GetProgressRate()109     double GetProgressRate() const
110     {
111         return progressRate_;
112     }
113 
GetFocusPattern()114     FocusPattern GetFocusPattern() const override
115     {
116         // Video focus type is scope, it is a container, inner focus is on slider now.
117         return { FocusType::SCOPE, true };
118     }
119 
120     RefPtr<FrameNode> CreateControlBar(int32_t nodeId = -1);
121 
SetHiddenChangeEvent(HiddenChangeEvent && hiddenChangeEvent)122     void SetHiddenChangeEvent(HiddenChangeEvent&& hiddenChangeEvent)
123     {
124         hiddenChangeEvent_ = std::move(hiddenChangeEvent);
125     }
126 
GetCurrentPos()127     uint32_t GetCurrentPos() const
128     {
129         return currentPos_;
130     }
131 
GetDuration()132     uint32_t GetDuration() const
133     {
134         return duration_;
135     }
136 
GetInitialState()137     bool GetInitialState() const
138     {
139         return isInitialState_;
140     }
141 
OnBackPressed()142     virtual bool OnBackPressed()
143     {
144         return false;
145     }
146 
147     void OnVisibleChange(bool isVisible) override;
148 
149     void OnAreaChangedInner() override;
150 
151     // It is used to init mediaplayer on background.
152     void UpdateMediaPlayerOnBg();
153     void ResetMediaPlayer();
154 
155     void EnableDrag();
SetIsStop(bool isStop)156     void SetIsStop(bool isStop)
157     {
158         isStop_ = isStop;
159     }
160 
GetIsStop()161     bool GetIsStop() const
162     {
163         return isStop_;
164     }
165 
SetIsDrag(bool isDrag)166     void SetIsDrag(bool isDrag)
167     {
168         isDrag_ = isDrag;
169     }
170 
isInitialState()171     bool isInitialState() const
172     {
173         return isInitialState_;
174     }
175 
UpdateMediaParam(const RefPtr<MediaPlayer> & mediaPlayer,const RefPtr<RenderSurface> & renderSurface,const RefPtr<RenderContext> & renderContext)176     void UpdateMediaParam(const RefPtr<MediaPlayer>& mediaPlayer, const RefPtr<RenderSurface>& renderSurface,
177         const RefPtr<RenderContext>& renderContext)
178     {
179         mediaPlayer_ = AceType::Claim(AceType::RawPtr(mediaPlayer));
180         renderSurface_ = AceType::Claim(AceType::RawPtr(renderSurface));
181         renderContextForMediaPlayer_ = AceType::Claim(AceType::RawPtr(renderContext));
182     }
183 
ResetMediaParam()184     void ResetMediaParam()
185     {
186         mediaPlayer_.Reset();
187         renderSurface_.Reset();
188         renderContextForMediaPlayer_.Reset();
189     }
190 
191     void OnFullScreenChange(bool isFullScreen);
192 
193     void RecoverState(const RefPtr<VideoPattern>& videoPattern);
194 
GetFullScreenNode()195     RefPtr<FrameNode> GetFullScreenNode() const
196     {
197         if (!fullScreenNodeId_.has_value()) {
198             return nullptr;
199         }
200         return FrameNode::GetFrameNode(V2::VIDEO_ETS_TAG, fullScreenNodeId_.value());
201     }
202 
203     void OnPlayerStatus(PlaybackStatus status);
204 
205     void OnCurrentTimeChange(uint32_t currentPos);
206 
207     void OnError(const std::string& errorId);
208 
209     void OnResolutionChange() const;
210 
211     void OnStartRenderFrameCb() const;
212 
ResetLastBoundsRect()213     void ResetLastBoundsRect()
214     {
215         lastBoundsRect_.SetRect(0.0f, 0.0f, 0.0f, 0.0f);
216     }
217 
218     RefPtr<VideoPattern> GetTargetVideoPattern();
219 
220 protected:
221     void OnUpdateTime(uint32_t time, int pos) const;
222     void RegisterMediaPlayerEvent();
223 
224     // Video src.
225     std::string src_;
226     bool isInitialState_ = true; // Initial state is true. Play or seek will set it to false.
227     bool isPlaying_ = false;
228 
229     RefPtr<MediaPlayer> mediaPlayer_ = MediaPlayer::Create();
230     RefPtr<RenderSurface> renderSurface_ = RenderSurface::Create();
231     RefPtr<RenderContext> renderContextForMediaPlayer_ = RenderContext::Create();
232 
233 private:
234     void OnAttachToFrameNode() override;
235     void OnModifyDone() override;
236     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override;
237     void OnRebuildFrame() override;
238 
239     // Set properties for media player.
240     void PrepareMediaPlayer();
241     void SetMethodCall();
242 
243     bool SetSourceForMediaPlayer();
244     void UpdateLooping();
245     void UpdateSpeed();
246     void UpdateMuted();
247     void PrepareSurface();
248 
249     bool HasPlayer() const;
250 
251     // Functions for the video controller.
252     void Start();
253     void Pause();
254     void Stop();
255     void FullScreen();
256 
257     void SetCurrentTime(float currentPos, SeekMode seekMode = SeekMode::SEEK_PREVIOUS_SYNC);
258     void SetFullScreenButtonCallBack(RefPtr<FrameNode>& fullScreenBtn);
259 
260     void OnPrepared(double width, double height, uint32_t duration, uint32_t currentPos, bool needFireEvent);
261     void OnCompletion();
262     void OnSliderChange(float posTime, int32_t mode);
263 
264     void UpdatePreviewImage();
265     void UpdateControllerBar();
266     void UpdateVideoProperty();
267 
268     static RefPtr<FrameNode> CreateSVG();
269     RefPtr<FrameNode> CreateText(uint32_t time);
270     RefPtr<FrameNode> CreateSlider();
271     void ChangePlayButtonTag();
272     void ChangePlayButtonTag(RefPtr<FrameNode>& playBtn);
273 
274     void ChangeFullScreenButtonTag(bool isFullScreen, RefPtr<FrameNode>& fullScreenBtn);
275     void ResetStatus();
276     void HiddenChange(bool hidden);
277     void PrintPlayerStatus(PlaybackStatus status);
278 
279     void UpdateFsState();
280     void checkNeedAutoPlay();
281 
282     // Fire error manually, eg. src is not existed. It must run on ui.
283     void FireError();
284 
SetMediaFullScreen(bool isFullScreen)285     void SetMediaFullScreen(bool isFullScreen)
286     {
287         mediaPlayer_->FullScreenChange(isFullScreen);
288         if (SystemProperties::GetExtSurfaceEnabled()) {
289             renderSurface_->SetIsFullScreen(isFullScreen);
290         }
291     }
292 
293     RefPtr<VideoControllerV2> videoControllerV2_;
294     RefPtr<FrameNode> controlBar_;
295 
296     GestureEventFunc playBtnCallBack_;
297     GestureEventFunc pauseBtnCallBack_;
298     HiddenChangeEvent hiddenChangeEvent_;
299 
300     bool isStop_ = false;
301     bool isDrag_ = false;
302 
303     bool muted_ = false;
304     bool autoPlay_ = false;
305     bool loop_ = false;
306 
307     bool pastPlayingStatus_ = false;
308 
309     bool dragEndAutoPlay_ = false;
310 
311     uint32_t currentPos_ = 0;
312     uint32_t duration_ = 0;
313 
314     // full screen node id
315     std::optional<int32_t> fullScreenNodeId_;
316 
317     // Video playback speed.
318     double progressRate_ = 1.0;
319 
320     Rect lastBoundsRect_;
321 
322     ACE_DISALLOW_COPY_AND_MOVE(VideoPattern);
323 };
324 } // namespace OHOS::Ace::NG
325 
326 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_VIDEO_VIDEO_PATTERN_H
327