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 #include "plugin/plugin_time.h" 29 30 namespace OHOS { 31 namespace Media { 32 class PlayerServerState { 33 public: PlayerServerState(const std::string & name)34 explicit PlayerServerState(const std::string &name) : name_(name) {} 35 virtual ~PlayerServerState() = default; 36 37 std::string GetStateName() const; 38 39 DISALLOW_COPY_AND_MOVE(PlayerServerState); 40 41 protected: StateEnter()42 virtual void StateEnter() {} StateExit()43 virtual void StateExit() {} 44 virtual int32_t OnMessageReceived(PlayerOnInfoType type, int32_t extra, const Format &infoBody) = 0; 45 46 friend class PlayerServerStateMachine; 47 48 private: 49 std::string name_; 50 }; 51 52 class PlayerServerStateMachine { 53 public: 54 PlayerServerStateMachine() = default; 55 virtual ~PlayerServerStateMachine() = default; 56 57 DISALLOW_COPY_AND_MOVE(PlayerServerStateMachine); 58 59 protected: 60 int32_t HandleMessage(PlayerOnInfoType type, int32_t extra, const Format &infoBody); 61 void Init(const std::shared_ptr<PlayerServerState> &state); 62 void ChangeState(const std::shared_ptr<PlayerServerState> &state); 63 std::shared_ptr<PlayerServerState> GetCurrState(); 64 65 private: 66 std::recursive_mutex recMutex_; 67 std::shared_ptr<PlayerServerState> currState_ = nullptr; 68 }; 69 70 class PlayerServer 71 : public IPlayerService, 72 public IPlayerEngineObs, 73 public NoCopyable, 74 public PlayerServerStateMachine { 75 public: 76 static std::shared_ptr<IPlayerService> Create(); 77 PlayerServer(); 78 virtual ~PlayerServer(); 79 80 int32_t Freeze() override; 81 int32_t UnFreeze() override; 82 int32_t Play() override; 83 int32_t Prepare() override; 84 int32_t SetRenderFirstFrame(bool display) override; 85 int32_t SetPlayRange(int64_t start, int64_t end) override; 86 int32_t SetPlayRangeWithMode(int64_t start, int64_t end, PlayerSeekMode mode) override; 87 int32_t PrepareAsync() override; 88 int32_t Stop() override; 89 int32_t Reset() override; 90 int32_t Release() override; 91 int32_t Pause() override; 92 int32_t SetVolume(float leftVolume, float rightVolume) override; 93 int32_t SetVolumeMode(int32_t mode) override; 94 int32_t Seek(int32_t mSeconds, PlayerSeekMode mode) override; 95 int32_t GetCurrentTime(int32_t ¤tTime) override; 96 int32_t GetPlaybackPosition(int32_t &playbackPosition) override; 97 int32_t GetVideoWidth() override; 98 int32_t GetVideoHeight() override; 99 int32_t GetVideoTrackInfo(std::vector<Format> &videoTrack) override; 100 int32_t GetPlaybackInfo(Format &playbackInfo) override; 101 int32_t GetAudioTrackInfo(std::vector<Format> &audioTrack) override; 102 int32_t GetSubtitleTrackInfo(std::vector<Format> &subtitleTrack) override; 103 int32_t GetDuration(int32_t &duration) override; 104 int32_t GetApiVersion(int32_t &apiVersion) override; 105 int32_t SetPlaybackSpeed(PlaybackRateMode mode) override; 106 int32_t SetPlaybackRate(float rate) override; 107 int32_t SetPlayerProducer(const PlayerProducer producer) override; 108 int32_t SetSource(const std::string &url) override; 109 int32_t SetSource(const std::shared_ptr<IMediaDataSource> &dataSrc) override; 110 int32_t SetSource(int32_t fd, int64_t offset, int64_t size) override; 111 int32_t AddSubSource(const std::string &url) override; 112 int32_t AddSubSource(int32_t fd, int64_t offset, int64_t size) override; 113 int32_t GetPlaybackSpeed(PlaybackRateMode &mode) override; 114 int32_t SetMediaSource(const std::shared_ptr<AVMediaSource> &mediaSource, AVPlayStrategy strategy) override; 115 int32_t SetPlaybackStrategy(AVPlayStrategy playbackStrategy) override; 116 int32_t SetMediaMuted(MediaType mediaType, bool isMuted) override; 117 int32_t SetSuperResolution(bool enabled) override; 118 int32_t SetVideoWindowSize(int32_t width, int32_t height) override; GetInterruptState()119 bool GetInterruptState() { return isInterruptNeeded_.load(); }; 120 #ifdef SUPPORT_VIDEO 121 int32_t SetVideoSurface(sptr<Surface> surface) override; 122 #endif 123 int32_t SetDecryptConfig(const sptr<DrmStandard::IMediaKeySessionService> &keySessionProxy, 124 bool svp) override; 125 bool IsPlaying() override; 126 bool IsLooping() override; 127 int32_t SetLooping(bool loop) override; 128 int32_t SetParameter(const Format ¶m) override; 129 int32_t SetPlayerCallback(const std::shared_ptr<PlayerCallback> &callback) override; 130 virtual int32_t DumpInfo(int32_t fd); 131 int32_t SelectBitRate(uint32_t bitRate) override; 132 int32_t BackGroundChangeState(PlayerStates state, bool isBackGroundCb); 133 int32_t SelectTrack(int32_t index, PlayerSwitchMode mode) override; 134 int32_t DeselectTrack(int32_t index) override; 135 int32_t GetCurrentTrack(int32_t trackType, int32_t &index) override; 136 137 // IPlayerEngineObs override 138 void OnError(PlayerErrorType errorType, int32_t errorCode) override; 139 void OnErrorMessage(int32_t errorCode, const std::string &errorMsg) override; 140 void OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody = {}) override; 141 void OnSystemOperation(PlayerOnSystemOperationType type, PlayerOperationReason reason) override; 142 void OnDfxInfo(const DfxEvent &event) override; 143 void OnBufferingUpdate(PlayerOnInfoType type, int32_t extra, const Format &infoBody); 144 void OnNotifyBufferingStart(); 145 void OnNotifyBufferingEnd(); 146 147 uint32_t GetMemoryUsage() override; 148 void OnCommonEventReceived(const std::string &event); 149 int32_t GetUserId(); 150 std::shared_ptr<CommonEventReceiver> GetCommonEventReceiver(); 151 bool IsBootCompleted(); 152 int32_t SetMaxAmplitudeCbStatus(bool status) override; 153 int32_t SetDeviceChangeCbStatus(bool status) override; 154 bool IsSeekContinuousSupported() override; 155 int32_t SetSeiMessageCbStatus(bool status, const std::vector<int32_t> &payloadTypes) override; 156 int32_t SetStartFrameRateOptEnabled(bool enabled) override; 157 int32_t SetReopenFd(int32_t fd) override; 158 int32_t EnableCameraPostprocessing() override; 159 int32_t SetCameraPostprocessing(bool isOpen) override; 160 int32_t EnableReportMediaProgress(bool enable) override; 161 int32_t EnableReportAudioInterrupt(bool enable) override; 162 int32_t ForceLoadVideo(bool status) override; 163 164 protected: 165 class BaseState; 166 class IdleState; 167 class InitializedState; 168 class PreparingState; 169 class PreparedState; 170 class PlayingState; 171 class PausedState; 172 class StoppedState; 173 class PlaybackCompletedState; 174 std::shared_ptr<IdleState> idleState_; 175 std::shared_ptr<InitializedState> initializedState_; 176 std::shared_ptr<PreparingState> preparingState_; 177 std::shared_ptr<PreparedState> preparedState_; 178 std::shared_ptr<PlayingState> playingState_; 179 std::shared_ptr<PausedState> pausedState_; 180 std::shared_ptr<StoppedState> stoppedState_; 181 std::shared_ptr<PlaybackCompletedState> playbackCompletedState_; 182 std::shared_ptr<CommonEventReceiver> commonEventReceiver_ = nullptr; 183 184 std::shared_ptr<PlayerCallback> playerCb_ = nullptr; 185 std::unique_ptr<IPlayerEngine> playerEngine_ = nullptr; 186 bool errorCbOnce_ = false; 187 bool disableStoppedCb_ = false; 188 std::string lastErrMsg_; 189 std::unique_ptr<UriHelper> uriHelper_ = nullptr; 190 std::vector<std::shared_ptr<UriHelper>> subUriHelpers_; 191 std::mutex mutex_; 192 std::mutex mutexCb_; 193 std::atomic<PlayerStates> lastOpStatus_ = PLAYER_IDLE; 194 PlayerServerTaskMgr taskMgr_; 195 bool isLiveStream_ = false; 196 virtual int32_t Init(); 197 void ClearConfigInfo(); 198 bool IsPrepared(); 199 bool IsCompleted(); 200 bool IsValidSeekMode(PlayerSeekMode mode); 201 void OnInfoNoChangeStatus(PlayerOnInfoType type, int32_t extra, const Format &infoBody = {}); 202 const std::string &GetStatusDescription(int32_t status); 203 struct ConfigInfo { 204 std::atomic<bool> looping = false; 205 float leftVolume = INVALID_VALUE; 206 float rightVolume = INVALID_VALUE; 207 PlaybackRateMode speedMode = SPEED_FORWARD_1_00_X; 208 float speedRate = 1.0f; 209 std::string url; 210 int32_t effectMode = OHOS::AudioStandard::AudioEffectMode::EFFECT_DEFAULT; 211 std::map<std::string, std::string> header; 212 AVPlayStrategy strategy_; 213 } config_; 214 215 private: 216 bool IsEngineStarted(); 217 int32_t InitPlayEngine(const std::string &url); 218 int32_t OnPrepare(bool sync); 219 int32_t OnPlay(); 220 int32_t OnFreeze(); 221 int32_t OnUnFreeze(); 222 int32_t OnPause(bool isSystemOperation); 223 int32_t OnStop(bool sync); 224 int32_t OnReset(); 225 int32_t HandlePrepare(); 226 int32_t HandlePlay(); 227 int32_t HandlePause(bool isSystemOperation); 228 int32_t HandleFreeze(); 229 int32_t HandleLiteFreeze(); 230 int32_t HandleUnFreeze(); 231 int32_t HandleLiteUnFreeze(); 232 int32_t HandlePauseDemuxer(); 233 int32_t HandleResumeDemuxer(); 234 int32_t HandleStop(); 235 int32_t HandleReset(); 236 int32_t HandleSeek(int32_t mSeconds, PlayerSeekMode mode); 237 int32_t HandleSetPlayRange(int64_t start, int64_t end, PlayerSeekMode mode); 238 int32_t HandleSetPlaybackSpeed(PlaybackRateMode mode); 239 int32_t HandleSetPlaybackRate(float rate); 240 int32_t SetAudioEffectMode(const int32_t effectMode); 241 int32_t CheckandDoUnFreeze(); 242 243 void HandleEos(); 244 void PreparedHandleEos(); 245 void HandleInterruptEvent(const Format &infoBody); 246 void HandleAudioDeviceChangeEvent(const Format &infoBody); 247 void OnFlvAutoSelectBitRate(uint32_t bitRate); 248 void FormatToString(std::string &dumpString, std::vector<Format> &videoTrack); 249 void OnErrorCb(int32_t errorCode, const std::string &errorMsg); 250 void InnerOnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody, const int32_t ret); 251 252 int32_t CheckSeek(int32_t mSeconds, PlayerSeekMode mode); 253 int32_t SeekContinous(int32_t mSeconds); 254 int32_t HandleSeekContinous(int32_t mSeconds, int64_t batchNo); 255 int32_t ExitSeekContinous(bool align); 256 int32_t ExitSeekContinousAsync(bool align); 257 void UpdateContinousBatchNo(); 258 259 bool CheckState(PlayerOnInfoType type, int32_t extra); 260 void DoCheckLiveDelayTime(); 261 int64_t CalculatePauseTime(); 262 void HandleFlvLiveRestartLink(); 263 void TryFlvLiveRestartLink(); 264 void UpdateFlvLivePauseTime(); 265 266 #ifdef SUPPORT_VIDEO 267 sptr<Surface> surface_ = nullptr; 268 #endif 269 std::shared_ptr<IMediaDataSource> dataSrc_ = nullptr; 270 static constexpr float INVALID_VALUE = 2.0f; 271 bool disableNextSeekDone_ = false; 272 bool isBackgroundCb_ = false; 273 bool isBackgroundChanged_ = false; 274 PlayerStates backgroundState_ = PLAYER_IDLE; 275 PlayerStates interruptEventState_ = PLAYER_IDLE; 276 PlayerStates audioDeviceChangeState_ = PLAYER_IDLE; 277 uint32_t appTokenId_ = 0; 278 uint32_t subtitleTrackNum_ = 0; 279 int32_t appUid_ = 0; 280 int32_t appPid_ = 0; 281 std::string appName_; 282 std::atomic<int32_t> apiVersion_ = -1; 283 std::atomic<bool> inReleasing_ = false; 284 std::atomic<int32_t> userId_ = -1; 285 std::atomic<bool> isBootCompleted_ = false; 286 std::shared_ptr<AVMediaSource> mediaSource_ = nullptr; 287 AVPlayStrategy strategy_; 288 uint64_t instanceId_ = 0; 289 std::atomic<bool> isInterruptNeeded_{false}; 290 bool isAudioMuted_ = false; 291 std::mutex seekContinousMutex_; 292 std::atomic<bool> isInSeekContinous_ {false}; 293 std::atomic<int64_t> seekContinousBatchNo_ {-1}; 294 bool deviceChangeCallbackflag_ = false; 295 bool maxAmplitudeCbStatus_ = false; 296 bool seiMessageCbStatus_ = false; 297 bool enableReportMediaProgress_ = false; 298 bool enableReportAudioInterrupt_ = false; 299 std::vector<int32_t> payloadTypes_ {}; 300 bool isStreamUsagePauseRequired_ = true; 301 std::mutex surfaceMutex_; 302 int64_t pauseTimestamp_ {Plugins::HST_TIME_NONE}; 303 int64_t sumPauseTime_ {0}; 304 bool isXSpeedPlay_ {false}; 305 bool isCalledBySystemApp_ = false; 306 bool isForceLoadVideo_ {false}; 307 std::atomic<uint32_t> totalMemoryUage_ {0}; 308 PlayerProducer playerProducer_ = PlayerProducer::INNER; 309 std::atomic<bool> isFrozen_ = false; 310 bool isMemoryExchanged_ = false; 311 OHOS::Media::MediaType mutedMediaType_ = OHOS::Media::MediaType::MEDIA_TYPE_MAX_COUNT; 312 }; 313 } // namespace Media 314 } // namespace OHOS 315 #endif // PLAYER_SERVICE_SERVER_H 316