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 GST_PLAYER_CTRL_H 17 #define GST_PLAYER_CTRL_H 18 19 #include <memory> 20 #include <string> 21 #include <vector> 22 #include <gst/gst.h> 23 #include <gst/player/player.h> 24 #include "i_player_engine.h" 25 #include "task_queue.h" 26 #include "gst_appsrc_warp.h" 27 #include "gst_player_track_parse.h" 28 29 namespace OHOS { 30 namespace Media { 31 class GstPlayerCtrl : public NoCopyable { 32 public: 33 explicit GstPlayerCtrl(GstPlayer *gstPlayer); 34 ~GstPlayerCtrl(); 35 36 int32_t SetUrl(const std::string &url); 37 int32_t SetSource(const std::shared_ptr<GstAppsrcWarp> &appsrcWarp); 38 int32_t SetCallbacks(const std::weak_ptr<IPlayerEngineObs> &obs); 39 void SetVideoTrack(bool enable); 40 void Pause(); 41 void Play(); 42 void Prepare(); 43 void PrepareAsync(); 44 int32_t Seek(uint64_t position, const PlayerSeekMode mode); 45 void Stop(); 46 int32_t SetLoop(bool loop); 47 void SetVolume(const float &leftVolume, const float &rightVolume); 48 int32_t SetParameter(const Format ¶m); 49 uint64_t GetPosition(); 50 uint64_t GetDuration(); 51 int32_t GetVideoTrackInfo(std::vector<Format> &videoTrack); 52 int32_t GetAudioTrackInfo(std::vector<Format> &audioTrack); 53 int32_t GetVideoWidth(); 54 int32_t GetVideoHeight(); 55 int32_t SetRate(double rate); 56 double GetRate(); 57 PlayerStates GetState() const; 58 void SetRingBufferMaxSize(uint64_t size); 59 void SetBufferingInfo(); 60 void SetHttpTimeOut(); 61 static void OnStateChangedCb(const GstPlayer *player, GstPlayerState state, GstPlayerCtrl *playerGst); 62 static void OnEndOfStreamCb(const GstPlayer *player, GstPlayerCtrl *playerGst); 63 static void StreamDecErrorParse(const gchar *name, int32_t &errorCode); 64 static void StreamErrorParse(const gchar *name, const GError *err, int32_t &errorCode); 65 static void ResourceErrorParse(const GError *err, int32_t &errorCode); 66 static void MessageErrorProcess(const char *name, const GError *err, 67 PlayerErrorType &errorType, int32_t &errorCode); 68 static void ErrorProcess(const GstMessage *msg, PlayerErrorType &errorType, int32_t &errorCode); 69 static void OnErrorCb(const GstPlayer *player, const GstMessage *msg, GstPlayerCtrl *playerGst); 70 static void OnSeekDoneCb(const GstPlayer *player, guint64 position, GstPlayerCtrl *playerGst); 71 static void OnPositionUpdatedCb(const GstPlayer *player, guint64 position, GstPlayerCtrl *playerGst); 72 static void OnVolumeChangeCb(const GObject *combiner, const GParamSpec *pspec, const GstPlayerCtrl *playerGst); 73 static void OnSourceSetupCb(const GstPlayer *player, GstElement *src, GstPlayerCtrl *playerGst); 74 static void OnElementSetupCb(const GstPlayer *player, GstElement *src, GstPlayerCtrl *playerGst); 75 static void OnResolutionChanegdCb(const GstPlayer *player, 76 int32_t width, int32_t height, GstPlayerCtrl *playerGst); 77 static void OnCachedPercentCb(const GstPlayer *player, guint percent, GstPlayerCtrl *playerGst); 78 static void OnBufferingTimeCb(const GstPlayer *player, guint64 bufferingTime, guint mqNumId, 79 GstPlayerCtrl *playerGst); 80 static void OnMqNumUseBufferingCb(const GstPlayer *player, guint mqNumUseBuffering, GstPlayerCtrl *playerGst); 81 private: 82 PlayerStates ProcessStoppedState(); 83 PlayerStates ProcessPausedState(); 84 int32_t ChangeSeekModeToGstFlag(const PlayerSeekMode mode) const; 85 void ProcessStateChanged(const GstPlayer *cbPlayer, GstPlayerState state); 86 void ProcessSeekDone(const GstPlayer *cbPlayer, uint64_t position); 87 void ProcessPositionUpdated(const GstPlayer *cbPlayer, uint64_t position); 88 void ProcessEndOfStream(const GstPlayer *cbPlayer); 89 void OnStateChanged(PlayerStates state); 90 void OnVolumeChange() const; 91 void OnSeekDone(); 92 void OnEndOfStream(); 93 void OnMessage(int32_t extra) const; 94 void OnBufferingUpdate(const std::string Message) const; 95 void OnResolutionChange(int32_t width, int32_t height); 96 void InitDuration(); 97 void PlaySync(); 98 void SeekSync(uint64_t position, const PlayerSeekMode mode); 99 void SetRateSync(double rate); 100 void PauseSync(); 101 void OnNotify(PlayerStates state); 102 void GetAudioSink(); 103 void HandleStopNotify(); 104 void HandlePlayBackNotify(); 105 uint64_t GetPositionInner(); 106 void ProcessCachedPercent(const GstPlayer *cbPlayer, int32_t percent); 107 void ProcessBufferingTime(const GstPlayer *cbPlayer, guint64 bufferingTime, guint mqNumId); 108 void ProcessMqNumUseBuffering(const GstPlayer *cbPlayer, uint32_t mqNumUseBuffering); 109 bool IsLiveMode() const; 110 bool SetAudioRendererInfo(const Format ¶m); 111 std::mutex mutex_; 112 std::condition_variable condVarPlaySync_; 113 std::condition_variable condVarPauseSync_; 114 std::condition_variable condVarStopSync_; 115 std::condition_variable condVarSeekSync_; 116 std::condition_variable condVarPreparingSync_; 117 GstPlayer *gstPlayer_ = nullptr; 118 TaskQueue taskQue_; 119 std::weak_ptr<IPlayerEngineObs> obs_; 120 bool enableLooping_ = false; 121 bool bufferingStart_ = false; 122 bool nextSeekFlag_ = false; 123 bool userStop_ = false; 124 bool locatedInEos_ = false; 125 bool stopTimeFlag_ = false; 126 bool errorFlag_ = false; 127 PlayerStates currentState_ = PLAYER_IDLE; 128 uint64_t sourceDuration_ = 0; 129 uint64_t seekDonePosition_ = 0; 130 uint64_t bufferingTime_ = 0; 131 int32_t percent_ = 0; 132 uint32_t mqNumUseBuffering_ = 0; 133 bool seekDoneNeedCb_ = false; 134 bool endOfStreamCb_ = false; 135 bool preparing_ = false; 136 std::vector<gulong> signalIds_; 137 gulong signalIdVolume_ = 0; 138 GstElement *audioSink_ = nullptr; 139 float volume_; // inited at the constructor 140 std::shared_ptr<GstAppsrcWarp> appsrcWarp_ = nullptr; 141 std::shared_ptr<ITaskHandler> seekTask_ = nullptr; 142 std::shared_ptr<ITaskHandler> rateTask_ = nullptr; 143 double rate_; // inited at the constructor 144 uint64_t lastTime_ = 0; 145 bool speeding_ = false; 146 bool isExit_ = true; 147 bool seeking_ = false; 148 std::map<guint, guint64> mqBufferingTime_; 149 std::shared_ptr<GstPlayerTrackParse> trackParse_ = nullptr; 150 int32_t videoWidth_ = 0; 151 int32_t videoHeight_ = 0; 152 bool isHardWare_ = false; 153 }; 154 } // namespace Media 155 } // namespace OHOS 156 #endif // GST_PLAYER_CTRL_H 157