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