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