1 /* 2 * Copyright (c) 2021 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_RESOURCE_PLAYER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_VIDEO_RESOURCE_PLAYER_H 18 19 #include <list> 20 21 #include "core/animation/scheduler.h" 22 #include "core/components/common/layout/constants.h" 23 #include "core/components/video/resource/resource.h" 24 #include "core/components/video/resource/texture.h" 25 26 namespace OHOS::Ace { 27 28 constexpr uint32_t FREAUENCY_GET_CURRENT_TIME = 250; // Millisecond 29 30 class ACE_EXPORT Player : public Resource { 31 DECLARE_ACE_TYPE(Player, Resource); 32 33 public: 34 using PreparedListener = std::function<void(uint32_t, uint32_t, bool, uint32_t, uint32_t, bool)>; 35 using PlayStatusListener = std::function<void(bool)>; 36 using CurrentPosListener = std::function<void(uint32_t)>; 37 using SeekDoneListener = std::function<void(uint32_t)>; 38 using CompletionListener = std::function<void()>; 39 using RefreshRenderListener = std::function<void()>; 40 Player(int64_t textureId,const std::string & src,const WeakPtr<PipelineBase> & context,ErrorCallback && onError)41 Player(int64_t textureId, const std::string& src, const WeakPtr<PipelineBase>& context, ErrorCallback&& onError) 42 : Resource("video", context, std::move(onError)), textureId_(textureId), src_(src) 43 {} Player(const WeakPtr<PipelineBase> & context,ErrorCallback && onError)44 Player(const WeakPtr<PipelineBase>& context, ErrorCallback&& onError) 45 : Resource("video", context, std::move(onError)) 46 {} 47 ~Player() override = default; 48 49 void Create(const std::function<void(int64_t)>& onCreate); 50 void CreatePlayer(const std::function<void(int64_t)>& onCreate); 51 52 void SetSource(const std::string& src); 53 54 void SetSurfaceId(int64_t id, bool isTexture); 55 GetWidth()56 uint32_t GetWidth() const 57 { 58 return width_; 59 } 60 GetHeight()61 uint32_t GetHeight() const 62 { 63 return height_; 64 } 65 GetDuration()66 uint32_t GetDuration() const 67 { 68 return duration_; 69 } 70 GetCurrent()71 uint32_t GetCurrent() const 72 { 73 return currentPos_; 74 } 75 IsPlaying()76 bool IsPlaying() const 77 { 78 return isPlaying_; 79 } 80 SetMute(bool isMute)81 void SetMute(bool isMute) 82 { 83 isMute_ = isMute; 84 } 85 SetAutoPlay(bool isAutoPlay)86 void SetAutoPlay(bool isAutoPlay) 87 { 88 isAutoPlay_ = isAutoPlay; 89 } 90 91 void AddPreparedListener(PreparedListener&& listener); 92 void AddPlayStatusListener(PlayStatusListener&& listener); 93 void AddCurrentPosListener(CurrentPosListener&& listener); 94 void AddSeekDoneListener(SeekDoneListener&& listener); 95 void AddCompletionListener(CompletionListener&& listener); 96 void AddRefreshRenderListener(RefreshRenderListener&& listener); 97 98 void PopListener(); 99 void OnPopListener(); 100 void UnregisterEvent(); 101 102 // The following public functions should be called in UI thread. 103 void Start(); 104 void Pause(); 105 void Stop(); 106 void SeekTo(uint32_t pos); 107 void SeekTo(uint32_t pos, uint32_t mode); 108 void SetVolume(float volume); 109 void EnterFullScreen(); 110 void EnableLooping(bool loop); 111 void SetSpeed(float speed); 112 void SetDirection(std::string& direction); 113 void SetLandscape(); 114 void SetFullScreenChange(bool isFullScreen); 115 void Release(const std::function<void(bool)>& onRelease = nullptr) override; 116 117 private: 118 void OnAddPreparedListener(PreparedListener&& listener); 119 void OnAddPlayStatusListener(PlayStatusListener&& listener); 120 void OnAddCurrentPosListener(CurrentPosListener&& listener); 121 void OnAddSeekDoneListener(CurrentPosListener&& listener); 122 void OnAddCompletionListener(CompletionListener&& listener); 123 124 void OnStarted(); 125 void OnPaused(); 126 void OnTimeGetted(const std::string& result); 127 void OnStop(); 128 129 void InitPlay(); 130 void OnPrepared(const std::string& param); 131 void OnCompletion(const std::string& param); 132 void OnSeekComplete(const std::string& param); 133 void OnPlayStatus(const std::string& param); 134 void GetCurrentTime(); 135 void OnTick(uint64_t timeStamp); 136 void SetTickActive(bool isActive); 137 138 int64_t textureId_ = INVALID_ID; 139 std::string src_; 140 uint32_t duration_ = 0; 141 uint32_t width_ = 0; 142 uint32_t height_ = 0; 143 uint32_t currentPos_ = 0; 144 bool isPlaying_ = false; 145 bool isMute_ = false; 146 bool isAutoPlay_ = false; 147 bool isPrepared_ = false; 148 bool isNeedFreshForce_ = false; 149 bool isInit_ = false; 150 Method getCurrentPosMethod_; 151 Method playMethod_; 152 Method pauseMethod_; 153 Method stopMethod_; 154 Method seekMethod_; 155 Method setVolumeMethod_; 156 Method fullscreenMethod_; 157 Method enableloopingMethod_; 158 Method setSpeedMethod_; 159 Method setDirectionMethod_; 160 Method setLandsacpeMethod_; 161 162 std::list<PreparedListener> onPreparedListener_; 163 std::list<PlayStatusListener> onPlayStatusListener_; 164 std::list<CurrentPosListener> onCurrentPosListener_; 165 std::list<SeekDoneListener> onSeekDoneListener_; 166 std::list<CompletionListener> onCompletionListener_; 167 std::list<RefreshRenderListener> onRefreshRenderListener_; 168 169 RefPtr<Scheduler> scheduler_; 170 uint64_t timeStamp_ = 0; 171 }; 172 173 } // namespace OHOS::Ace 174 175 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_VIDEO_RESOURCE_PLAYER_H 176