• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gst/gst.h>
25 #include "nocopyable.h"
26 #include "i_playbin_ctrler.h"
27 #include "state_machine.h"
28 #include "gst_msg_processor.h"
29 #include "task_queue.h"
30 
31 namespace OHOS {
32 namespace Media {
33 enum GstPlayerStatus : int32_t {
34     GST_PLAYER_STATUS_IDLE = 0,
35     GST_PLAYER_STATUS_BUFFERING,
36     GST_PLAYER_STATUS_PAUSED,
37     GST_PLAYER_STATUS_PLAYING,
38 };
39 
40 class PlayBinCtrlerBase
41     : public IPlayBinCtrler,
42       public StateMachine,
43       public std::enable_shared_from_this<PlayBinCtrlerBase> {
44 public:
45     explicit PlayBinCtrlerBase(const PlayBinCreateParam &createParam);
46     virtual ~PlayBinCtrlerBase();
47 
48     int32_t Init();
49     int32_t SetSource(const std::string &url)  override;
50     int32_t SetSource(const std::shared_ptr<GstAppsrcWrap> &appsrcWrap) override;
51     int32_t Prepare() override;
52     int32_t PrepareAsync() override;
53     int32_t Play() override;
54     int32_t Pause() override;
55     int32_t Seek(int64_t timeUs, int32_t seekOption) override;
56     int32_t Stop(bool needWait) override;
57     int32_t SetRate(double rate) override;
58     double GetRate() override;
59     int64_t QueryPosition() override;
60     int32_t SetLoop(bool loop) override;
61     void SetVolume(const float &leftVolume, const float &rightVolume) override;
62     void SetAudioInterruptMode(const int32_t interruptMode) override;
63     int32_t SetAudioRendererInfo(const uint32_t rendererInfo, const int32_t rendererFlag) override;
64     int32_t SelectBitRate(uint32_t bitRate) override;
65 
66     void SetElemSetupListener(ElemSetupListener listener) final;
67     void SetElemUnSetupListener(ElemSetupListener listener) final;
68     void SetAutoPlugSortListener(AutoPlugSortListener listener) final;
69     void RemoveGstPlaySinkVideoConvertPlugin() final;
70     void SetNotifier(PlayBinMsgNotifier notifier) final;
71 protected:
72     virtual int32_t OnInit() = 0;
73 
74     GstPipeline *playbin_ = nullptr;
75 
76 private:
77     class BaseState;
78     class IdleState;
79     class InitializedState;
80     class PreparingState;
81     class PreparedState;
82     class PlayingState;
83     class PausedState;
84     class StoppedState;
85     class StoppingState;
86     class PlaybackCompletedState;
87 
88     int32_t EnterInitializedState();
89     void ExitInitializedState();
90     int32_t PrepareAsyncInternal();
91     int32_t SeekInternal(int64_t timeUs, int32_t seekOption);
92     int32_t StopInternal();
93     int32_t SetRateInternal(double rate);
94     void SetupCustomElement();
95     GstSeekFlags ChooseSetRateFlags(double rate);
96     int32_t SetupSignalMessage();
97     int32_t SetupElementUnSetupSignal();
98     void QueryDuration();
99     void ProcessEndOfStream();
100     static void ElementSetup(const GstElement *playbin, GstElement *elem, gpointer userData);
101     static void ElementUnSetup(const GstElement *playbin, GstElement *subbin, GstElement *child, gpointer userData);
102     static void SourceSetup(const GstElement *playbin, GstElement *elem, gpointer userData);
103     static void OnBitRateParseCompleteCb(const GstElement *playbin, uint32_t *bitrateInfo,
104         uint32_t bitrateNum, gpointer userData);
105     static GValueArray *AutoPlugSort(const GstElement *uriDecoder, GstPad *pad, GstCaps *caps,
106         GValueArray *factories, gpointer userData);
107     static void OnInterruptEventCb(const GstElement *audioSink, const uint32_t eventType, const uint32_t forceType,
108         const uint32_t hintType, gpointer userData);
109     static void OnAudioStateEventCb(const GstElement *audioSink, const uint32_t audioState, gpointer userData);
110     void SetupInterruptEventCb();
111     void SetupAudioStateEventCb();
112     void OnElementSetup(GstElement &elem);
113     void OnElementUnSetup(GstElement &elem);
114     void OnSourceSetup(const GstElement *playbin, GstElement *src,
115         const std::shared_ptr<PlayBinCtrlerBase> &playbinCtrl);
116     bool OnVideoDecoderSetup(GstElement &elem);
117     void OnAppsrcErrorMessageReceived(int32_t errorCode);
118     void OnMessageReceived(const InnerMessage &msg);
119     void OnSinkMessageReceived(const PlayBinMessage &msg);
120     GValueArray *OnAutoPlugSort(GValueArray &factories);
121     void ReportMessage(const PlayBinMessage &msg);
122     int32_t Reset() noexcept;
123     bool IsLiveSource() const;
124     int32_t DoInitializeForDataSource();
125     void DoInitializeForHttp();
126     void HandleCacheCtrl(int32_t percent);
127     void HandleCacheCtrlCb(const InnerMessage &msg);
128     void HandleCacheCtrlWhenNoBuffering(int32_t percent);
129     void HandleCacheCtrlWhenBuffering(int32_t percent);
130 
131     std::mutex mutex_;
132     std::mutex cacheCtrlMutex_;
133     std::mutex listenerMutex_;
134     std::mutex appsrcMutex_;
135     std::unique_ptr<TaskQueue> msgQueue_;
136     PlayBinRenderMode renderMode_ = PlayBinRenderMode::DEFAULT_RENDER;
137     PlayBinMsgNotifier notifier_;
138     ElemSetupListener elemSetupListener_;
139     ElemSetupListener elemUnSetupListener_;
140     AutoPlugSortListener autoPlugSortListener_;
141     std::shared_ptr<PlayBinSinkProvider> sinkProvider_;
142     std::unique_ptr<GstMsgProcessor> msgProcessor_;
143     std::string uri_;
144 
145     struct SignalInfo {
146         GstElement *element;
147         gulong signalId;
148     };
149     std::vector<SignalInfo> signalIds_;
150     std::vector<uint32_t> bitRateVec_;
151     bool isInitialized_ = false;
152 
153     bool isErrorHappened_ = false;
154     std::condition_variable preparingCond_;
155     std::condition_variable preparedCond_;
156     std::condition_variable stoppingCond_;
157 
158     PlayBinSinkProvider::SinkPtr audioSink_ = nullptr;
159     PlayBinSinkProvider::SinkPtr videoSink_ = nullptr;
160 
161     int64_t duration_ = 0;
162     double rate_ = 0;
163     int64_t seekPos_ = 0;
164     int64_t lastTime_ = 0;
165 
166     bool isSeeking_ = false;
167     bool isRating_ = false;
168     bool isBuffering_ = false;
169     bool isNetWorkPlay_ = false;
170     bool isUserSetPlay_ = false;
171     bool isUserSetPause_ = false;
172     bool isReplay_ = false;
173     uint32_t rendererInfo_ = 0;
174     int32_t rendererFlag_ = 0;
175     int32_t cachePercent_ = 100; // 100% cache percent
176 
177     std::atomic<bool> isDuration_ = false;
178     std::atomic<bool> enableLooping_ = false;
179     std::shared_ptr<GstAppsrcWrap> appsrcWrap_ = nullptr;
180 
181     std::shared_ptr<IdleState> idleState_;
182     std::shared_ptr<InitializedState> initializedState_;
183     std::shared_ptr<PreparingState> preparingState_;
184     std::shared_ptr<PreparedState> preparedState_;
185     std::shared_ptr<PlayingState> playingState_;
186     std::shared_ptr<PausedState> pausedState_;
187     std::shared_ptr<StoppedState> stoppedState_;
188     std::shared_ptr<StoppingState> stoppingState_;
189     std::shared_ptr<PlaybackCompletedState> playbackCompletedState_;
190 };
191 } // namespace Media
192 } // namespace OHOS
193 #endif // PLAYBIN_CTRLER_BASE_H