1 /* 2 * Copyright (c) 2021-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_VIDEO_VIDEO_ELEMENT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_VIDEO_VIDEO_ELEMENT_H 18 19 #include <stdint.h> 20 21 #include "base/resource/asset_manager.h" 22 #include "core/components/common/properties/edge.h" 23 #include "core/components/multimodal/render_multimodal.h" 24 #include "core/components/slider/slider_theme.h" 25 #include "core/components/video/resource/player.h" 26 #include "core/components/video/resource/texture.h" 27 #include "core/components/video/video_component.h" 28 #include "core/components/video/video_theme.h" 29 #include "core/focus/focus_node.h" 30 #include "core/pipeline/base/render_element.h" 31 32 #ifdef OHOS_STANDARD_SYSTEM 33 #include "player.h" 34 #include "surface_delegate.h" 35 36 #include "core/components/video/media_player_callback.h" 37 #endif 38 39 namespace OHOS::Ace { 40 41 constexpr double VIDEO_CHILD_COMMON_FLEX_GROW = 1.0; 42 constexpr double VIDEO_CHILD_COMMON_FLEX_SHRINK = 1.0; 43 constexpr double VIDEO_CHILD_COMMON_FLEX_BASIS = 0.0; 44 constexpr uint32_t VIDEO_SEEK_STEP = 5; 45 46 class VideoElement : public RenderElement, public FocusNode { 47 DECLARE_ACE_TYPE(VideoElement, RenderElement, FocusNode); 48 49 public: 50 using EventCallback = std::function<void(const std::string&)>; 51 using FullscreenEvent = std::function<RefPtr<Component>(bool, const WeakPtr<Player>&, const WeakPtr<Texture>&)>; 52 using MediaExitFullscreenEvent = std::function<void(bool, bool, int32_t)>; 53 using MediaFullscreenEvent = std::function<RefPtr<Component>(bool, bool, const WeakPtr<Texture>&)>; 54 using ErrorCallback = std::function<void(const std::string&, const std::string&)>; 55 56 VideoElement() = default; 57 ~VideoElement() override; 58 59 void SetNewComponent(const RefPtr<Component>& newComponent) override; 60 void Prepare(const WeakPtr<Element>& parent) override; 61 void PerformBuild() override; 62 void Dump() override; 63 64 void Start(); 65 void Pause(); 66 void Stop(); 67 void SetCurrentTime(float currentPos, SeekMode seekMode = SeekMode::SEEK_PREVIOUS_SYNC); 68 void FullScreen(); 69 void ExitFullScreen(); 70 void SetVolume(float volume); 71 void SetSpeed(float speed); 72 73 protected: 74 virtual void InitStatus(const RefPtr<VideoComponent>& videoComponent); 75 bool IsDeclarativePara(); OnPreFullScreen(bool isPortrait)76 virtual void OnPreFullScreen(bool isPortrait) {} 77 void UpdateChildInner(const RefPtr<Component>& childComponent); 78 const RefPtr<Component> CreateChild(); 79 80 bool isFullScreen_ = false; 81 bool isInitialState_ = true; // Initial state is true. Play or seek will set it to true. 82 bool isError_ = false; 83 std::string direction_ = "auto"; 84 bool isExternalResource_ = false; 85 bool isPlaying_ = false; 86 RefPtr<Player> player_; 87 RefPtr<Texture> texture_; 88 EventCallback onFullScreenChange_; 89 int32_t currentPlatformVersion_ = 0; 90 uint32_t currentPos_ = 0; 91 92 private: 93 void OnError(const std::string& errorId, const std::string& param); 94 void OnResolutionChange() const; 95 void OnPrepared( 96 uint32_t width, uint32_t height, bool isPlaying, uint32_t duration, uint32_t currentPos, bool needFireEvent); 97 void OnPlayerStatus(PlaybackStatus status); 98 void OnCurrentTimeChange(uint32_t currentPos); 99 void OnCompletion(); 100 void OnStartBtnClick(); 101 void OnFullScreenBtnClick(); 102 void OnSliderChange(const std::string& param); 103 void OnSliderMoving(const std::string& param); 104 void IntTimeToText(uint32_t time, std::string& timeText); 105 void InitEvent(const RefPtr<VideoComponent>& videoComponent); 106 void SetMethodCall(const RefPtr<VideoComponent>& videoComponent); 107 void SetRespondChildEvent(); 108 void CreatePlatformResource(); 109 void CreatePlayer(int64_t id, ErrorCallback&& errorCallback); 110 void ReleasePlatformResource(); 111 void InitListener(); 112 void ResetStatus(); 113 bool OnKeyEvent(const KeyEvent& keyEvent) override; 114 void OnKeyLeft(); 115 void OnKeyRight(); 116 void OnTextureRefresh(); 117 void HiddenChange(bool hidden); 118 void OnTextureSize(int64_t textureId, int32_t textureWidth, int32_t textureHeight); 119 void EnableLooping(bool loop); 120 // Interim programme 121 void SetMediaSource(std::string& filePath, int32_t& fd); 122 void MediaPlay(const std::string& filePath); 123 void RawFilePlay(const std::string& filePath); 124 void RelativePathPlay(const std::string& filePath); 125 bool GetResourceId(const std::string& path, uint32_t& resId); 126 127 const RefPtr<Component> CreatePoster(); 128 const RefPtr<Component> CreateControl(); 129 const RefPtr<Component> CreateCurrentText(); 130 const RefPtr<Component> CreateDurationText(); 131 const RefPtr<Component> CreateSlider(); 132 const RefPtr<Component> CreatePlayBtn(); 133 const RefPtr<Component> CreateFullScreenBtn(); 134 const RefPtr<Component> CreateErrorText(const std::string& errorMsg); 135 const RefPtr<Component> SetPadding(const RefPtr<Component>& component, Edge&& edge); 136 137 void PrepareMultiModalEvent(); 138 bool SubscribeMultiModal(); 139 bool UnSubscribeMultiModal(); 140 bool HasPlayer() const; 141 142 bool isSubscribeMultimodal_ = false; 143 RefPtr<VideoTheme> theme_; 144 RefPtr<SliderTheme> sliderTheme_; 145 bool isStop_ = false; 146 147 ImageFit imageFit_ { ImageFit::CONTAIN }; 148 ImageObjectPosition imagePosition_; 149 150 bool hasSrcChanged_ = true; 151 bool needControls_ = true; 152 bool isAutoPlay_ = false; 153 bool isMute_ = false; 154 std::string src_; 155 std::string poster_; 156 RefPtr<ImageComponent> posterImage_; 157 uint32_t duration_ = 0; 158 bool pastPlayingStatus_ = false; // Record the player status before dragging the progress bar. 159 bool isMediaPlayerFullStatus_ = false; 160 bool isReady_ = false; 161 bool isElementPrepared_ = false; 162 double videoWidth_ = 0.0; 163 double videoHeight_ = 0.0; 164 bool isLoop_ = false; 165 int32_t startTime_ = 0; 166 std::string durationText_; 167 std::string currentPosText_; 168 TextDirection textDirection_ = TextDirection::LTR; 169 float speed_ = -1.0f; 170 171 EventMarker shieldId_; // Shield the event on the control bar. 172 EventMarker startBtnClickId_; 173 EventMarker fullscreenBtnClickId_; 174 EventMarker sliderMovedCallbackId_; 175 EventMarker sliderMovingCallbackId_; 176 EventCallback onPrepared_; 177 EventCallback onStart_; 178 EventCallback onPause_; 179 EventCallback onStop_; 180 EventCallback onFinish_; 181 EventCallback onError_; 182 EventCallback onSeeking_; 183 EventCallback onSeeked_; 184 EventCallback onTimeUpdate_; 185 186 // multimodal required param 187 MultimodalEventCallback multimodalEventFullscreen_; 188 MultimodalEventCallback multimodalEventFullscreenExit_; 189 MultimodalEventCallback multimodalEventPlay_; 190 MultimodalEventCallback multimodalEventPause_; 191 WeakPtr<MultiModalScene> multiModalScene_; 192 VoiceEvent playVoiceEvent_; 193 VoiceEvent pauseVoiceEvent_; 194 VoiceEvent fullscreenVoiceEvent_; 195 VoiceEvent exitFullscreenVoiceEvent_; 196 197 FullscreenEvent fullscreenEvent_; 198 MediaFullscreenEvent mediaFullscreenEvent_; 199 MediaExitFullscreenEvent mediaExitFullscreenEvent_; 200 201 #ifdef OHOS_STANDARD_SYSTEM 202 void RegisterMediaPlayerEvent(); 203 void CreateMediaPlayer(); 204 void PreparePlayer(); 205 std::string GetAssetAbsolutePath(const std::string& fileName); 206 void OnTextureOffset(int64_t textureId, int32_t x, int32_t y); 207 208 std::shared_ptr<OHOS::Media::Player> mediaPlayer_ = nullptr; 209 std::shared_ptr<MediaPlayerCallback> mediaPlayerCallback_ = nullptr; 210 sptr<OHOS::SurfaceDelegate> surfaceDelegate_; 211 bool hasMediaPrepared_ = false; 212 int32_t textureWidth_ = 0; 213 int32_t textureHeight_ = 0; 214 int32_t textureOffsetX_ = 0; 215 int32_t textureOffsetY_ = 0; 216 #endif 217 }; 218 219 } // namespace OHOS::Ace 220 221 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_VIDEO_VIDEO_ELEMENT_H 222