1 /* 2 * Copyright (c) 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_NG_PATTERNS_VIDEO_VIDEO_PATTERN_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_VIDEO_VIDEO_PATTERN_H 18 19 #include "base/geometry/dimension.h" 20 #include "base/geometry/size.h" 21 #include "base/memory/referenced.h" 22 #include "base/utils/noncopyable.h" 23 #include "core/components/video/video_controller_v2.h" 24 #include "core/components_ng/image_provider/image_loading_context.h" 25 #include "core/components_ng/pattern/pattern.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 = default; 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 UpdateMuted(bool muted)61 void UpdateMuted(bool muted) 62 { 63 muted_ = muted; 64 } 65 GetMuted()66 bool GetMuted() const 67 { 68 return muted_; 69 } 70 UpdateAutoPlay(bool autoPlay)71 void UpdateAutoPlay(bool autoPlay) 72 { 73 autoPlay_ = autoPlay; 74 } 75 GetAutoPlay()76 bool GetAutoPlay() const 77 { 78 return autoPlay_; 79 } 80 UpdateLoop(bool loop)81 void UpdateLoop(bool loop) 82 { 83 loop_ = loop; 84 } 85 GetLoop()86 bool GetLoop() const 87 { 88 return loop_; 89 } 90 IsFullScreen()91 bool IsFullScreen() const 92 { 93 return isFullScreen_; 94 } 95 UpdateProgressRate(double progressRate)96 void UpdateProgressRate(double progressRate) 97 { 98 progressRate_ = progressRate; 99 } 100 GetProgressRate()101 double GetProgressRate() const 102 { 103 return progressRate_; 104 } 105 GetFocusPattern()106 FocusPattern GetFocusPattern() const override 107 { 108 return { FocusType::NODE, true }; 109 } 110 SetHiddenChangeEvent(HiddenChangeEvent && hiddenChangeEvent)111 void SetHiddenChangeEvent(HiddenChangeEvent&& hiddenChangeEvent) 112 { 113 hiddenChangeEvent_ = std::move(hiddenChangeEvent); 114 } 115 116 bool OnBackPressed(); 117 118 void OnVisibleChange(bool isVisible) override; 119 120 void OnAreaChangedInner() override; 121 122 private: 123 void OnAttachToFrameNode() override; 124 void OnModifyDone() override; 125 bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override; 126 void OnRebuildFrame() override; 127 128 void UpdateMediaPlayer(); 129 void PrepareMediaPlayer(); 130 void PrepareSurface(); 131 void RegisterMediaPlayerEvent(); 132 void SetMethodCall(); 133 bool SetSourceForMediaPlayer(); 134 bool HasPlayer() const; 135 136 void Start(); 137 void Pause(); 138 void Stop(); 139 void SetCurrentTime(float currentPos, SeekMode seekMode = SeekMode::SEEK_PREVIOUS_SYNC); 140 void OnFullScreenChange(bool isFullScreen); 141 void FullScreen(); 142 void ExitFullScreen(); 143 void UpdateLooping(); 144 void SetSpeed(); 145 void UpdateMuted(); 146 147 void OnCurrentTimeChange(uint32_t currentPos); 148 void OnPlayerStatus(PlaybackStatus status); 149 void OnError(const std::string& errorId); 150 void OnResolutionChange() const; 151 void OnPrepared(double width, double height, uint32_t duration, uint32_t currentPos, bool needFireEvent); 152 void OnCompletion(); 153 void OnSliderChange(float posTime, int32_t mode); 154 155 void OnUpdateTime(uint32_t time, int pos) const; 156 157 void AddPreviewNodeIfNeeded(); 158 void AddControlBarNodeIfNeeded(); 159 void UpdateVideoProperty(); 160 RefPtr<FrameNode> CreateControlBar(); 161 static RefPtr<FrameNode> CreateSVG(); 162 static RefPtr<FrameNode> CreateText(uint32_t time); 163 RefPtr<FrameNode> CreateSlider(); 164 void ChangePlayButtonTag(); 165 void ChangePlayButtonTag(RefPtr<FrameNode>& playBtn); 166 void SetFullScreenButtonCallBack(RefPtr<FrameNode>& fullScreenBtn); 167 void ChangeFullScreenButtonTag(bool isFullScreen, RefPtr<FrameNode>& fullScreenBtn); 168 void ResetStatus(); 169 void HiddenChange(bool hidden); 170 171 RefPtr<VideoControllerV2> videoControllerV2_; 172 RefPtr<RenderSurface> renderSurface_ = RenderSurface::Create(); 173 RefPtr<MediaPlayer> mediaPlayer_ = MediaPlayer::Create(); 174 RefPtr<RenderContext> renderContextForMediaPlayer_ = RenderContext::Create(); 175 176 GestureEventFunc playBtnCallBack_; 177 GestureEventFunc pauseBtnCallBack_; 178 HiddenChangeEvent hiddenChangeEvent_; 179 180 bool isStop_ = false; 181 std::string src_; 182 183 uint32_t duration_ = 0; 184 uint32_t currentPos_ = 0; 185 186 bool muted_ = false; 187 bool autoPlay_ = false; 188 bool loop_ = false; 189 bool isFullScreen_ = false; 190 bool isInitialState_ = true; // Initial state is true. Play or seek will set it to false. 191 bool isPlaying_ = false; 192 bool pastPlayingStatus_ = false; 193 double progressRate_ = 1.0; 194 195 Rect lastBoundsRect_; 196 197 ACE_DISALLOW_COPY_AND_MOVE(VideoPattern); 198 }; 199 } // namespace OHOS::Ace::NG 200 201 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_VIDEO_VIDEO_PATTERN_H 202