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