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 PLAYER_SERVICE_SERVER_H 17 #define PLAYER_SERVICE_SERVER_H 18 19 #include "i_player_service.h" 20 #include "i_player_engine.h" 21 #include "nocopyable.h" 22 #include "uri_helper.h" 23 #include "player_server_task_mgr.h" 24 #include "audio_effect.h" 25 #include "account_subscriber.h" 26 #include "os_account_manager.h" 27 #include "hitrace/tracechain.h" 28 29 namespace OHOS { 30 namespace Media { 31 class PlayerServerState { 32 public: PlayerServerState(const std::string & name)33 explicit PlayerServerState(const std::string &name) : name_(name) {} 34 virtual ~PlayerServerState() = default; 35 36 std::string GetStateName() const; 37 38 DISALLOW_COPY_AND_MOVE(PlayerServerState); 39 40 protected: StateEnter()41 virtual void StateEnter() {} StateExit()42 virtual void StateExit() {} 43 virtual int32_t OnMessageReceived(PlayerOnInfoType type, int32_t extra, const Format &infoBody) = 0; 44 45 friend class PlayerServerStateMachine; 46 47 private: 48 std::string name_; 49 }; 50 51 class PlayerServerStateMachine { 52 public: 53 PlayerServerStateMachine() = default; 54 virtual ~PlayerServerStateMachine() = default; 55 56 DISALLOW_COPY_AND_MOVE(PlayerServerStateMachine); 57 58 protected: 59 int32_t HandleMessage(PlayerOnInfoType type, int32_t extra, const Format &infoBody); 60 void Init(const std::shared_ptr<PlayerServerState> &state); 61 void ChangeState(const std::shared_ptr<PlayerServerState> &state); 62 std::shared_ptr<PlayerServerState> GetCurrState(); 63 64 private: 65 std::recursive_mutex recMutex_; 66 std::shared_ptr<PlayerServerState> currState_ = nullptr; 67 }; 68 69 class PlayerServer 70 : public IPlayerService, 71 public IPlayerEngineObs, 72 public NoCopyable, 73 public PlayerServerStateMachine { 74 public: 75 static std::shared_ptr<IPlayerService> Create(); 76 PlayerServer(); 77 virtual ~PlayerServer(); 78 79 int32_t Play() override; 80 int32_t Prepare() override; 81 int32_t SetRenderFirstFrame(bool display) override; 82 int32_t SetPlayRange(int64_t start, int64_t end) override; 83 int32_t SetPlayRangeWithMode(int64_t start, int64_t end, PlayerSeekMode mode) override; 84 int32_t PrepareAsync() override; 85 int32_t Stop() override; 86 int32_t Reset() override; 87 int32_t Release() override; 88 int32_t Pause() override; 89 int32_t SetVolume(float leftVolume, float rightVolume) override; 90 int32_t Seek(int32_t mSeconds, PlayerSeekMode mode) override; 91 int32_t GetCurrentTime(int32_t ¤tTime) override; 92 int32_t GetVideoWidth() override; 93 int32_t GetVideoHeight() override; 94 int32_t GetVideoTrackInfo(std::vector<Format> &videoTrack) override; 95 int32_t GetPlaybackInfo(Format &playbackInfo) override; 96 int32_t GetAudioTrackInfo(std::vector<Format> &audioTrack) override; 97 int32_t GetSubtitleTrackInfo(std::vector<Format> &subtitleTrack) override; 98 int32_t GetDuration(int32_t &duration) override; 99 int32_t GetApiVersion(int32_t &apiVersion) override; 100 int32_t SetPlaybackSpeed(PlaybackRateMode mode) override; 101 int32_t SetSource(const std::string &url) override; 102 int32_t SetSource(const std::shared_ptr<IMediaDataSource> &dataSrc) override; 103 int32_t SetSource(int32_t fd, int64_t offset, int64_t size) override; 104 int32_t AddSubSource(const std::string &url) override; 105 int32_t AddSubSource(int32_t fd, int64_t offset, int64_t size) override; 106 int32_t GetPlaybackSpeed(PlaybackRateMode &mode) override; 107 int32_t SetMediaSource(const std::shared_ptr<AVMediaSource> &mediaSource, AVPlayStrategy strategy) override; 108 int32_t SetPlaybackStrategy(AVPlayStrategy playbackStrategy) override; 109 int32_t SetMediaMuted(MediaType mediaType, bool isMuted) override; GetInterruptState()110 bool GetInterruptState() { return isInterruptNeeded_.load(); }; 111 #ifdef SUPPORT_VIDEO 112 int32_t SetVideoSurface(sptr<Surface> surface) override; 113 #endif 114 int32_t SetDecryptConfig(const sptr<DrmStandard::IMediaKeySessionService> &keySessionProxy, 115 bool svp) override; 116 bool IsPlaying() override; 117 bool IsLooping() override; 118 int32_t SetLooping(bool loop) override; 119 int32_t SetParameter(const Format ¶m) override; 120 int32_t SetPlayerCallback(const std::shared_ptr<PlayerCallback> &callback) override; 121 virtual int32_t DumpInfo(int32_t fd); 122 int32_t SelectBitRate(uint32_t bitRate) override; 123 int32_t BackGroundChangeState(PlayerStates state, bool isBackGroundCb); 124 int32_t SelectTrack(int32_t index, PlayerSwitchMode mode) override; 125 int32_t DeselectTrack(int32_t index) override; 126 int32_t GetCurrentTrack(int32_t trackType, int32_t &index) override; 127 128 // IPlayerEngineObs override 129 void OnError(PlayerErrorType errorType, int32_t errorCode) override; 130 void OnErrorMessage(int32_t errorCode, const std::string &errorMsg) override; 131 void OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody = {}) override; 132 void OnSystemOperation(PlayerOnSystemOperationType type, PlayerOperationReason reason) override; 133 void OnBufferingUpdate(PlayerOnInfoType type, int32_t extra, const Format &infoBody); 134 void OnNotifyBufferingStart(); 135 void OnNotifyBufferingEnd(); 136 137 void OnCommonEventReceived(const std::string &event); 138 int32_t GetUserId(); 139 std::shared_ptr<CommonEventReceiver> GetCommonEventReceiver(); 140 bool IsBootCompleted(); 141 int32_t SetMaxAmplitudeCbStatus(bool status) override; 142 int32_t SetDeviceChangeCbStatus(bool status) override; 143 bool IsSeekContinuousSupported() override; 144 145 protected: 146 class BaseState; 147 class IdleState; 148 class InitializedState; 149 class PreparingState; 150 class PreparedState; 151 class PlayingState; 152 class PausedState; 153 class StoppedState; 154 class PlaybackCompletedState; 155 std::shared_ptr<IdleState> idleState_; 156 std::shared_ptr<InitializedState> initializedState_; 157 std::shared_ptr<PreparingState> preparingState_; 158 std::shared_ptr<PreparedState> preparedState_; 159 std::shared_ptr<PlayingState> playingState_; 160 std::shared_ptr<PausedState> pausedState_; 161 std::shared_ptr<StoppedState> stoppedState_; 162 std::shared_ptr<PlaybackCompletedState> playbackCompletedState_; 163 std::shared_ptr<CommonEventReceiver> commonEventReceiver_ = nullptr; 164 165 std::shared_ptr<PlayerCallback> playerCb_ = nullptr; 166 std::unique_ptr<IPlayerEngine> playerEngine_ = nullptr; 167 bool errorCbOnce_ = false; 168 bool disableStoppedCb_ = false; 169 std::string lastErrMsg_; 170 std::unique_ptr<UriHelper> uriHelper_ = nullptr; 171 std::vector<std::shared_ptr<UriHelper>> subUriHelpers_; 172 std::mutex mutex_; 173 std::mutex mutexCb_; 174 std::atomic<PlayerStates> lastOpStatus_ = PLAYER_IDLE; 175 PlayerServerTaskMgr taskMgr_; 176 bool isLiveStream_ = false; 177 virtual int32_t Init(); 178 void ClearConfigInfo(); 179 bool IsPrepared(); 180 bool IsCompleted(); 181 bool IsValidSeekMode(PlayerSeekMode mode); 182 void OnInfoNoChangeStatus(PlayerOnInfoType type, int32_t extra, const Format &infoBody = {}); 183 const std::string &GetStatusDescription(int32_t status); 184 struct ConfigInfo { 185 std::atomic<bool> looping = false; 186 float leftVolume = INVALID_VALUE; 187 float rightVolume = INVALID_VALUE; 188 PlaybackRateMode speedMode = SPEED_FORWARD_1_00_X; 189 std::string url; 190 int32_t effectMode = OHOS::AudioStandard::AudioEffectMode::EFFECT_DEFAULT; 191 std::map<std::string, std::string> header; 192 AVPlayStrategy strategy_; 193 } config_; 194 195 private: 196 bool IsEngineStarted(); 197 int32_t InitPlayEngine(const std::string &url); 198 int32_t OnPrepare(bool sync); 199 int32_t OnPlay(); 200 int32_t OnPause(bool isSystemOperation); 201 int32_t OnStop(bool sync); 202 int32_t OnReset(); 203 int32_t HandlePrepare(); 204 int32_t HandlePlay(); 205 int32_t HandlePause(bool isSystemOperation); 206 int32_t HandlePauseDemuxer(); 207 int32_t HandleResumeDemuxer(); 208 int32_t HandleStop(); 209 int32_t HandleReset(); 210 int32_t HandleSeek(int32_t mSeconds, PlayerSeekMode mode); 211 int32_t HandleEosPlay(); 212 int32_t HandleSetPlayRange(int64_t start, int64_t end, PlayerSeekMode mode); 213 int32_t HandleSetPlaybackSpeed(PlaybackRateMode mode); 214 int32_t SetAudioEffectMode(const int32_t effectMode); 215 216 void HandleEos(); 217 void PreparedHandleEos(); 218 void HandleInterruptEvent(const Format &infoBody); 219 void HandleAudioDeviceChangeEvent(const Format &infoBody); 220 void FormatToString(std::string &dumpString, std::vector<Format> &videoTrack); 221 void OnErrorCb(int32_t errorCode, const std::string &errorMsg); 222 void InnerOnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody, const int32_t ret); 223 224 int32_t CheckSeek(int32_t mSeconds, PlayerSeekMode mode); 225 int32_t SeekContinous(int32_t mSeconds); 226 int32_t HandleSeekContinous(int32_t mSeconds, int64_t batchNo); 227 int32_t ExitSeekContinous(bool align); 228 void UpdateContinousBatchNo(); 229 230 bool CheckState(PlayerOnInfoType type, int32_t extra); 231 232 #ifdef SUPPORT_VIDEO 233 sptr<Surface> surface_ = nullptr; 234 #endif 235 std::shared_ptr<IMediaDataSource> dataSrc_ = nullptr; 236 static constexpr float INVALID_VALUE = 2.0f; 237 bool disableNextSeekDone_ = false; 238 bool isBackgroundCb_ = false; 239 bool isBackgroundChanged_ = false; 240 PlayerStates backgroundState_ = PLAYER_IDLE; 241 PlayerStates interruptEventState_ = PLAYER_IDLE; 242 PlayerStates audioDeviceChangeState_ = PLAYER_IDLE; 243 uint32_t appTokenId_ = 0; 244 uint32_t subtitleTrackNum_ = 0; 245 int32_t appUid_ = 0; 246 int32_t appPid_ = 0; 247 int32_t apiVersion_ = -1; 248 std::atomic<bool> inReleasing_ = false; 249 std::atomic<int32_t> userId_ = -1; 250 std::atomic<bool> isBootCompleted_ = false; 251 std::shared_ptr<AVMediaSource> mediaSource_ = nullptr; 252 AVPlayStrategy strategy_; 253 uint64_t instanceId_ = 0; 254 std::atomic<bool> isInterruptNeeded_{false}; 255 bool isAudioMuted_ = false; 256 std::mutex seekContinousMutex_; 257 std::atomic<bool> isInSeekContinous_ {false}; 258 std::atomic<int64_t> seekContinousBatchNo_ {-1}; 259 bool deviceChangeCallbackflag_ = false; 260 bool maxAmplitudeCbStatus_ = false; 261 std::mutex surfaceMutex_; 262 }; 263 } // namespace Media 264 } // namespace OHOS 265 #endif // PLAYER_SERVICE_SERVER_H 266