• 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 {
36 class ImageAnalyzerManager;
37 }
38 namespace OHOS::Ace::NG {
39 class VideoPattern : public Pattern {
40     DECLARE_ACE_TYPE(VideoPattern, Pattern);
41 
42 public:
43     using HiddenChangeEvent = std::function<void(bool)>;
44 
45     VideoPattern() = delete;
46     explicit VideoPattern(const RefPtr<VideoControllerV2>& videoController);
47     ~VideoPattern() override;
48 
CreateEventHub()49     RefPtr<EventHub> CreateEventHub() override
50     {
51         return MakeRefPtr<VideoEventHub>();
52     }
53 
CreateLayoutProperty()54     RefPtr<LayoutProperty> CreateLayoutProperty() override
55     {
56         return MakeRefPtr<VideoLayoutProperty>();
57     }
58 
CreateLayoutAlgorithm()59     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
60     {
61         return MakeRefPtr<VideoLayoutAlgorithm>();
62     }
63 
CreateAccessibilityProperty()64     RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override
65     {
66         return MakeRefPtr<VideoAccessibilityProperty>();
67     }
68 
DefaultSupportDrag()69     bool DefaultSupportDrag() override
70     {
71         return true;
72     }
73 
IsSupportDrawModifier()74     bool IsSupportDrawModifier() const override
75     {
76         return false;
77     }
78 
UpdateMuted(bool muted)79     void UpdateMuted(bool muted)
80     {
81         muted_ = muted;
82     }
83 
GetMuted()84     bool GetMuted() const
85     {
86         return muted_;
87     }
88 
UpdateAutoPlay(bool autoPlay)89     void UpdateAutoPlay(bool autoPlay)
90     {
91         autoPlay_ = autoPlay;
92     }
93 
GetAutoPlay()94     bool GetAutoPlay() const
95     {
96         return autoPlay_;
97     }
98 
UpdateLoop(bool loop)99     void UpdateLoop(bool loop)
100     {
101         loop_ = loop;
102     }
103 
GetLoop()104     bool GetLoop() const
105     {
106         return loop_;
107     }
108 
109     virtual bool IsFullScreen() const;
110 
111     void OnColorConfigurationUpdate() override;
UpdateProgressRate(double progressRate)112     void UpdateProgressRate(double progressRate)
113     {
114         progressRate_ = progressRate;
115     }
116 
GetProgressRate()117     double GetProgressRate() const
118     {
119         return progressRate_;
120     }
121 
GetFocusPattern()122     FocusPattern GetFocusPattern() const override
123     {
124         // Video focus type is scope, it is a container, inner focus is on slider now.
125         return { FocusType::SCOPE, true };
126     }
127 
128     RefPtr<FrameNode> CreateControlBar(int32_t nodeId = -1);
129 
SetHiddenChangeEvent(HiddenChangeEvent && hiddenChangeEvent)130     void SetHiddenChangeEvent(HiddenChangeEvent&& hiddenChangeEvent)
131     {
132         hiddenChangeEvent_ = std::move(hiddenChangeEvent);
133     }
134 
GetCurrentPos()135     uint32_t GetCurrentPos() const
136     {
137         return currentPos_;
138     }
139 
GetDuration()140     uint32_t GetDuration() const
141     {
142         return duration_;
143     }
144 
GetInitialState()145     bool GetInitialState() const
146     {
147         return isInitialState_;
148     }
149 
OnBackPressed()150     virtual bool OnBackPressed()
151     {
152         return false;
153     }
154 
155     void OnVisibleChange(bool isVisible) override;
156 
157     void OnAreaChangedInner() override;
158 
159     // It is used to init mediaplayer on background.
160     void UpdateMediaPlayerOnBg();
161     void ResetMediaPlayer();
162 
163     void EnableDrag();
SetIsStop(bool isStop)164     void SetIsStop(bool isStop)
165     {
166         isStop_ = isStop;
167     }
168 
GetIsStop()169     bool GetIsStop() const
170     {
171         return isStop_;
172     }
173 
SetIsDrag(bool isDrag)174     void SetIsDrag(bool isDrag)
175     {
176         isDrag_ = isDrag;
177     }
178 
IsInitialState()179     bool IsInitialState() const
180     {
181         return isInitialState_;
182     }
183 
SetIsDragEndAutoPlay(bool isDragEndAutoPlay)184     void SetIsDragEndAutoPlay(bool isDragEndAutoPlay)
185     {
186         dragEndAutoPlay_ = isDragEndAutoPlay;
187     }
188 
GetSrc()189     const std::string& GetSrc() const
190     {
191         return src_;
192     }
193 
UpdateMediaParam(const RefPtr<MediaPlayer> & mediaPlayer,const RefPtr<RenderSurface> & renderSurface,const RefPtr<RenderContext> & renderContext)194     void UpdateMediaParam(const RefPtr<MediaPlayer>& mediaPlayer, const RefPtr<RenderSurface>& renderSurface,
195         const RefPtr<RenderContext>& renderContext)
196     {
197         mediaPlayer_ = AceType::Claim(AceType::RawPtr(mediaPlayer));
198         renderSurface_ = AceType::Claim(AceType::RawPtr(renderSurface));
199         renderContextForMediaPlayer_ = AceType::Claim(AceType::RawPtr(renderContext));
200     }
201 
ResetMediaParam()202     void ResetMediaParam()
203     {
204         mediaPlayer_.Reset();
205         renderSurface_.Reset();
206         RemoveMediaPlayerSurfaceNode();
207         renderContextForMediaPlayer_.Reset();
208     }
209 
210     void RemoveMediaPlayerSurfaceNode();
211 
212     void OnFullScreenChange(bool isFullScreen);
213 
214     void RecoverState(const RefPtr<VideoPattern>& videoPattern);
215 
216     bool NeedLift() const;
217 
GetFullScreenNode()218     RefPtr<FrameNode> GetFullScreenNode() const
219     {
220         if (!fullScreenNodeId_.has_value()) {
221             return nullptr;
222         }
223         return FrameNode::GetFrameNode(V2::VIDEO_ETS_TAG, fullScreenNodeId_.value());
224     }
225 
226     void OnPlayerStatus(PlaybackStatus status);
227 
228     void OnCurrentTimeChange(uint32_t currentPos);
229 
230     void OnError(const std::string& errorId);
231 
232     void OnResolutionChange() const;
233 
234     void OnStartRenderFrameCb() const;
235 
ResetLastBoundsRect()236     void ResetLastBoundsRect()
237     {
238         lastBoundsRect_.SetRect(0.0f, 0.0f, 0.0f, 0.0f);
239     }
240 
241     RefPtr<VideoPattern> GetTargetVideoPattern();
242     void EnableAnalyzer(bool enable);
243     void SetImageAnalyzerConfig(void* config);
244     void SetImageAIOptions(void* options);
245     bool GetAnalyzerState();
UpdateAnalyzerState(bool isCreated)246     void UpdateAnalyzerState(bool isCreated)
247     {
248         isAnalyzerCreated_ = isCreated;
249     }
SetIsSeeking(bool isSeeking)250     void SetIsSeeking(bool isSeeking)
251     {
252         isSeeking_ = isSeeking;
253     }
254 
255 #ifdef RENDER_EXTRACT_SUPPORTED
256     void OnTextureRefresh(void* surface);
257 #endif
258 
259 protected:
260     void OnUpdateTime(uint32_t time, int pos) const;
261     void RegisterMediaPlayerEvent();
262 
263     RefPtr<MediaPlayer> mediaPlayer_ = MediaPlayer::Create();
264     RefPtr<RenderSurface> renderSurface_ = RenderSurface::Create();
265     RefPtr<RenderContext> renderContextForMediaPlayer_ = RenderContext::Create();
266 
267     int32_t instanceId_;
268 
269 #if defined(RENDER_EXTRACT_SUPPORTED) && defined(ENABLE_ROSEN_BACKEND)
270     WeakPtr<RenderSurface> renderSurfaceWeakPtr_;
271     WeakPtr<RenderContext> renderContextForMediaPlayerWeakPtr_;
272 #endif
273 
274 private:
275     void OnAttachToFrameNode() override;
276     void OnDetachFromFrameNode(FrameNode* frameNode) override;
277     void OnDetachFromMainTree() override;
278     void OnModifyDone() override;
279     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override;
280     void OnRebuildFrame() override;
281     void OnWindowHide() override;
282 
283     // Set properties for media player.
284     void PrepareMediaPlayer();
285     void SetStartImpl(
286         const RefPtr<VideoController>& videoController, const SingleTaskExecutor& uiTaskExecutor);
287     void SetPausetImpl(
288         const RefPtr<VideoController>& videoController, const SingleTaskExecutor& uiTaskExecutor);
289     void SetStopImpl(
290         const RefPtr<VideoController>& videoController, const SingleTaskExecutor& uiTaskExecutor);
291     void SetSeekToImpl(
292         const RefPtr<VideoController>& videoController, const SingleTaskExecutor& uiTaskExecutor);
293     void SetRequestFullscreenImpl(
294         const RefPtr<VideoController>& videoController, const SingleTaskExecutor& uiTaskExecutor);
295     void SetExitFullscreenImpl(
296         const RefPtr<VideoController>& videoController, const SingleTaskExecutor& uiTaskExecutor);
297     void SetResetImpl(
298         const RefPtr<VideoController>& videoController, const SingleTaskExecutor& uiTaskExecutor);
299 
300     void SetMethodCall();
301 
302     bool SetSourceForMediaPlayer();
303     void UpdateLooping();
304     void UpdateSpeed();
305     void UpdateMuted();
306     void PrepareSurface();
307 
308     bool HasPlayer() const;
309 
310     // Functions for the video controller.
311     void Start();
312     void Pause();
313     void Stop();
314     void FullScreen();
315 
316     void SetCurrentTime(float currentPos, SeekMode seekMode = SeekMode::SEEK_PREVIOUS_SYNC);
317     void SetFullScreenButtonCallBack(RefPtr<FrameNode>& fullScreenBtn);
318 
319     void OnPrepared(uint32_t duration, uint32_t currentPos, bool needFireEvent);
320     void OnCompletion();
321     void OnSliderChange(float posTime, int32_t mode);
322 
323     void UpdatePreviewImage();
324     void UpdateControllerBar();
325     void UpdateVideoProperty();
326 
327     RefPtr<FrameNode> CreateSVG();
328     RefPtr<FrameNode> CreateText(uint32_t time);
329     RefPtr<FrameNode> CreateSlider();
330     void ChangePlayButtonTag();
331     void ChangePlayButtonTag(RefPtr<FrameNode>& playBtn);
332 
333     void ChangeFullScreenButtonTag(bool isFullScreen, RefPtr<FrameNode>& fullScreenBtn);
334     void ResetStatus();
335     void HiddenChange(bool hidden);
336     void PrintPlayerStatus(PlaybackStatus status);
337 
338     void UpdateFsState();
339     void checkNeedAutoPlay();
340 
341     // Fire error manually, eg. src is not existed. It must run on ui.
342     void FireError();
343 
344     HiddenChangeEvent CreateHiddenChangeEvent();
345 
SetMediaFullScreen(bool isFullScreen)346     void SetMediaFullScreen(bool isFullScreen)
347     {
348         mediaPlayer_->FullScreenChange(isFullScreen);
349         if (SystemProperties::GetExtSurfaceEnabled()) {
350             renderSurface_->SetIsFullScreen(isFullScreen);
351         }
352     }
353 
354 #ifdef RENDER_EXTRACT_SUPPORTED
355     void* GetNativeWindow(int32_t instanceId, int64_t textureId);
356 #endif
357 
358     void ChangePlayerStatus(bool isPlaying, const PlaybackStatus& status);
359 
360     bool IsSupportImageAnalyzer();
361     bool ShouldUpdateImageAnalyzer();
362     void StartImageAnalyzer();
363     void StartUpdateImageAnalyzer();
364     void CreateAnalyzerOverlay();
365     void DestroyAnalyzerOverlay();
366     void UpdateAnalyzerOverlay();
367     void UpdateAnalyzerUIConfig(const RefPtr<NG::GeometryNode>& geometryNode);
368     void UpdateOverlayVisibility(VisibleType type);
369 
370     RefPtr<VideoControllerV2> videoControllerV2_;
371     RefPtr<FrameNode> controlBar_;
372 
373     GestureEventFunc playBtnCallBack_;
374     GestureEventFunc pauseBtnCallBack_;
375     HiddenChangeEvent hiddenChangeEvent_;
376 
377     // Video src.
378     std::string src_;
379     bool isInitialState_ = true; // Initial state is true. Play or seek will set it to false.
380     bool isPlaying_ = false;
381 
382     bool isStop_ = false;
383     bool isDrag_ = false;
384 
385     bool muted_ = false;
386     bool autoPlay_ = false;
387     bool loop_ = false;
388 
389     bool pastPlayingStatus_ = false;
390 
391     bool dragEndAutoPlay_ = false;
392     bool isEnableAnalyzer_ = false;
393     bool isAnalyzerCreated_ = false;
394     bool isPaused_ = false;
395     bool isContentSizeChanged_ = false;
396     bool isSeeking_ = false;
397 
398     uint32_t currentPos_ = 0;
399     uint32_t duration_ = 0;
400 
401     // full screen node id
402     std::optional<int32_t> fullScreenNodeId_;
403 
404     // Video playback speed.
405     double progressRate_ = 1.0;
406 
407     Rect lastBoundsRect_;
408     Rect contentRect_;
409     std::shared_ptr<ImageAnalyzerManager> imageAnalyzerManager_;
410 
411     ACE_DISALLOW_COPY_AND_MOVE(VideoPattern);
412 };
413 } // namespace OHOS::Ace::NG
414 
415 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_VIDEO_VIDEO_PATTERN_H
416