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 PLAYBIN_CTRLER_BASE_H 17 #define PLAYBIN_CTRLER_BASE_H 18 19 #include <memory> 20 #include <string> 21 #include <vector> 22 #include <unordered_map> 23 #include <mutex> 24 #include <future> 25 #include <gst/gst.h> 26 #include "nocopyable.h" 27 #include "i_playbin_ctrler.h" 28 #include "state_machine.h" 29 #include "gst_msg_processor.h" 30 #include "task_queue.h" 31 #include "player_track_parse.h" 32 #include "av_common.h" 33 #include "gst_utils.h" 34 35 namespace OHOS { 36 namespace Media { 37 class PlayBinCtrlerBase; 38 using PlayBinCtrlerWrapper = ThizWrapper<PlayBinCtrlerBase>; 39 40 enum GstPlayerStatus : int32_t { 41 GST_PLAYER_STATUS_IDLE = 0, 42 GST_PLAYER_STATUS_BUFFERING, 43 GST_PLAYER_STATUS_READY, 44 GST_PLAYER_STATUS_PAUSED, 45 GST_PLAYER_STATUS_PLAYING, 46 }; 47 48 class PlayBinCtrlerBase 49 : public IPlayBinCtrler, 50 public StateMachine, 51 public std::enable_shared_from_this<PlayBinCtrlerBase> { 52 public: 53 explicit PlayBinCtrlerBase(const PlayBinCreateParam &createParam); 54 virtual ~PlayBinCtrlerBase(); 55 56 int32_t Init(); 57 bool EnableBufferingBySysParam() const; 58 int32_t SetSource(const std::string &url) override; 59 int32_t SetSource(const std::shared_ptr<GstAppsrcEngine> &appsrcWrap) override; 60 int32_t AddSubSource(const std::string &url) override; 61 int32_t PrepareAsync() override; 62 int32_t Play() override; 63 int32_t Pause() override; 64 int32_t Seek(int64_t timeUs, int32_t seekOption) override; 65 int32_t Stop(bool needWait) override; 66 int32_t SetRate(double rate) override; 67 int64_t QueryPosition() override; 68 int32_t SetLoop(bool loop) override; 69 void SetVolume(const float &leftVolume, const float &rightVolume) override; 70 void SetAudioInterruptMode(const int32_t interruptMode) override; 71 int32_t SetAudioRendererInfo(const uint32_t rendererInfo, const int32_t rendererFlag) override; 72 int32_t SetAudioEffectMode(const int32_t effectMode) override; 73 int32_t SelectBitRate(uint32_t bitRate) override; 74 75 void SetElemSetupListener(ElemSetupListener listener) final; 76 void SetElemUnSetupListener(ElemSetupListener listener) final; 77 void SetAutoPlugSortListener(AutoPlugSortListener listener) final; 78 void RemoveGstPlaySinkVideoConvertPlugin() final; 79 void SetNotifier(PlayBinMsgNotifier notifier) final; 80 void SetAutoSelectBitrate(bool enable) final; 81 int32_t GetVideoTrackInfo(std::vector<Format> &videoTrack) override; 82 int32_t GetAudioTrackInfo(std::vector<Format> &audioTrack) override; 83 int32_t GetSubtitleTrackInfo(std::vector<Format> &subtitleTrack) override; 84 int32_t SelectTrack(int32_t index) override; 85 int32_t DeselectTrack(int32_t index) override; 86 int32_t GetCurrentTrack(int32_t trackType, int32_t &index) override; 87 protected: 88 virtual int32_t OnInit() = 0; 89 90 GstPipeline *playbin_ = nullptr; 91 92 private: 93 class BaseState; 94 class IdleState; 95 class InitializedState; 96 class PreparingState; 97 class PreparedState; 98 class PlayingState; 99 class PausedState; 100 class StoppedState; 101 class StoppingState; 102 class PlaybackCompletedState; 103 104 int32_t EnterInitializedState(); 105 void ExitInitializedState(); 106 int32_t PrepareAsyncInternal(); 107 int32_t SeekInternal(int64_t timeUs, int32_t seekOption); 108 int32_t StopInternal(); 109 int32_t SetRateInternal(double rate); 110 void SetupCustomElement(); 111 GstSeekFlags ChooseSetRateFlags(double rate); 112 void SetupSourceSetupSignal(); 113 int32_t SetupSignalMessage(); 114 int32_t SetupElementUnSetupSignal(); 115 void QueryDuration(); 116 void ProcessEndOfStream(); 117 static void ElementSetup(const GstElement *playbin, GstElement *elem, gpointer userData); 118 static void ElementUnSetup(const GstElement *playbin, GstElement *subbin, GstElement *child, gpointer userData); 119 static void SourceSetup(const GstElement *playbin, GstElement *elem, gpointer userData); 120 static void OnBitRateParseCompleteCb(const GstElement *playbin, uint32_t *bitrateInfo, 121 uint32_t bitrateNum, gpointer userData); 122 static void OnSelectBitrateDoneCb(const GstElement *playbin, uint32_t bandwidth, gpointer userData); 123 static GValueArray *AutoPlugSort(const GstElement *uriDecoder, GstPad *pad, GstCaps *caps, 124 GValueArray *factories, gpointer userData); 125 static void OnInterruptEventCb(const GstElement *audioSink, const uint32_t eventType, const uint32_t forceType, 126 const uint32_t hintType, gpointer userData); 127 static void OnAudioSegmentEventCb(const GstElement *audioSink, gpointer userData); 128 static void OnAudioDiedEventCb(const GstElement *audioSink, gpointer userData); 129 static void OnIsLiveStream(const GstElement *demux, gboolean isLiveStream, gpointer userData); 130 static void AudioChanged(const GstElement *playbin, gpointer userData); 131 void SetupInterruptEventCb(); 132 void SetupAudioSegmentEventCb(); 133 void SetupAudioDiedEventCb(); 134 void OnElementSetup(GstElement &elem); 135 void OnElementUnSetup(GstElement &elem); 136 void OnSourceSetup(const GstElement *playbin, GstElement *src, 137 const std::shared_ptr<PlayBinCtrlerBase> &playbinCtrl); 138 bool OnVideoDecoderSetup(GstElement &elem); 139 bool OnAppsrcMessageReceived(const InnerMessage &msg); 140 void OnMessageReceived(const InnerMessage &msg); 141 void OnSinkMessageReceived(const PlayBinMessage &msg); 142 GValueArray *OnAutoPlugSort(GValueArray &factories); 143 void ReportMessage(const PlayBinMessage &msg); 144 int32_t Reset() noexcept; 145 bool IsLiveSource() const; 146 int32_t DoInitializeForDataSource(); 147 void DoInitializeForHttp(); 148 void HandleCacheCtrl(int32_t percent); 149 void HandleCacheCtrlCb(const InnerMessage &msg); 150 void HandleCacheCtrlWhenNoBuffering(int32_t percent); 151 void HandleCacheCtrlWhenBuffering(int32_t percent); 152 void OnAdaptiveElementSetup(GstElement &elem); 153 void OnAudioChanged(); 154 void OnSubtitleChanged(); 155 void ReportTrackChange(); 156 void OnTrackDone(); 157 void OnAddSubDone(); 158 void OnError(int32_t errorCode, std::string message); 159 void CheckAndAddSignalIds(gulong id, PlayBinCtrlerWrapper *wrapper, GstElement *elem); 160 bool SetPlayerState(GstPlayerStatus status); 161 AddSignalIds(GstElement * element,gulong signalId)162 inline void AddSignalIds(GstElement *element, gulong signalId) 163 { 164 if (signalIds_.find(element) == signalIds_.end()) { 165 signalIds_[element] = {signalId}; 166 } else { 167 signalIds_[element].push_back(signalId); 168 } 169 } RemoveSignalIds(GstElement * element)170 inline void RemoveSignalIds(GstElement *element) 171 { 172 if (signalIds_.find(element) != signalIds_.end()) { 173 for (auto id : signalIds_[element]) { 174 g_signal_handler_disconnect(element, id); 175 } 176 signalIds_.erase(element); 177 } 178 } 179 std::mutex mutex_; 180 std::mutex cacheCtrlMutex_; 181 std::mutex stateChangePropertyMutex_; 182 std::mutex listenerMutex_; 183 std::mutex appsrcMutex_; 184 std::unique_ptr<TaskQueue> msgQueue_; 185 PlayBinRenderMode renderMode_ = PlayBinRenderMode::DEFAULT_RENDER; 186 PlayBinMsgNotifier notifier_; 187 ElemSetupListener elemSetupListener_; 188 ElemSetupListener elemUnSetupListener_; 189 AutoPlugSortListener autoPlugSortListener_; 190 std::shared_ptr<PlayBinSinkProvider> sinkProvider_; 191 std::unique_ptr<GstMsgProcessor> msgProcessor_; 192 std::string uri_; 193 std::shared_ptr<PlayerTrackParse> trackParse_; 194 195 std::map<GstElement *, std::vector<gulong>> signalIds_; 196 std::vector<uint32_t> bitRateVec_; 197 bool isInitialized_ = false; 198 199 bool isErrorHappened_ = false; 200 std::future<gboolean> seekFuture_; 201 std::condition_variable preparingCond_; 202 std::condition_variable preparedCond_; 203 std::condition_variable stoppingCond_; 204 205 PlayBinSinkProvider::SinkPtr audioSink_ = nullptr; 206 PlayBinSinkProvider::SinkPtr videoSink_ = nullptr; 207 PlayBinSinkProvider::SinkPtr subtitleSink_ = nullptr; 208 209 int64_t duration_ = 0; 210 double rate_ = 0; 211 int64_t seekPos_ = 0; 212 int64_t lastTime_ = 0; 213 214 bool stopBuffering_ = false; 215 bool isSeeking_ = false; 216 bool isClosetSeeking_ = false; 217 bool isRating_ = false; 218 bool isAddingSubtitle_ = false; 219 bool isBuffering_ = false; 220 bool isSelectBitRate_ = false; 221 bool isNetWorkPlay_ = false; 222 bool isUserSetPlay_ = false; 223 bool isUserSetPause_ = false; 224 bool isReplay_ = false; 225 uint32_t rendererInfo_ = 0; 226 int32_t rendererFlag_ = 0; 227 int32_t cachePercent_ = 100; // 100% cache percent 228 uint64_t connectSpeed_ = 0; 229 GstClockTime lastStartTime_ = GST_CLOCK_TIME_NONE; 230 231 bool isTrackChanging_ = false; 232 int32_t trackChangeType_ = MediaType::MEDIA_TYPE_AUD; 233 int32_t audioIndex_ = -1; 234 bool hasSubtitleTrackSelected_ = true; 235 uint32_t subtitleTrackNum_ = 0; 236 237 std::atomic<bool> isDuration_ = false; 238 std::atomic<bool> enableLooping_ = false; 239 std::shared_ptr<GstAppsrcEngine> appsrcWrap_ = nullptr; 240 241 std::shared_ptr<IdleState> idleState_; 242 std::shared_ptr<InitializedState> initializedState_; 243 std::shared_ptr<PreparingState> preparingState_; 244 std::shared_ptr<PreparedState> preparedState_; 245 std::shared_ptr<PlayingState> playingState_; 246 std::shared_ptr<PausedState> pausedState_; 247 std::shared_ptr<StoppedState> stoppedState_; 248 std::shared_ptr<StoppingState> stoppingState_; 249 std::shared_ptr<PlaybackCompletedState> playbackCompletedState_; 250 }; 251 } // namespace Media 252 } // namespace OHOS 253 #endif // PLAYBIN_CTRLER_BASE_H