• 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 PLAYER_ENGINE_GST_IMPL
17 #define PLAYER_ENGINE_GST_IMPL
18 
19 #include <mutex>
20 #include <cstdint>
21 #include <thread>
22 #include <map>
23 
24 #include "audio_info.h"
25 #include "i_player_engine.h"
26 #include "i_playbin_ctrler.h"
27 #include "gst_appsrc_engine.h"
28 #include "player_codec_ctrl.h"
29 #include "task_queue.h"
30 
31 namespace OHOS {
32 namespace Media {
33 class PlayerEngineGstImpl : public IPlayerEngine, public NoCopyable {
34 public:
35     explicit PlayerEngineGstImpl(int32_t uid = 0, int32_t pid = 0, uint32_t tokenId = 0);
36     ~PlayerEngineGstImpl();
37 
38     int32_t SetSource(const std::string &url) override;
39     int32_t SetSource(const std::shared_ptr<IMediaDataSource> &dataSrc) override;
40     int32_t AddSubSource(const std::string &url) override;
41     int32_t SetObs(const std::weak_ptr<IPlayerEngineObs> &obs) override;
42     int32_t SetVideoSurface(sptr<Surface> surface) override;
43     int32_t PrepareAsync() override;
44     int32_t Play() override;
45     int32_t Pause() override;
46     int32_t Stop() override;
47     int32_t Reset() override;
48     int32_t SetVolume(float leftVolume, float rightVolume) override;
49     int32_t Seek(int32_t mSeconds, PlayerSeekMode mode) override;
50     int32_t GetCurrentTime(int32_t &currentTime) override;
51     int32_t GetVideoTrackInfo(std::vector<Format> &videoTrack) override;
52     int32_t GetAudioTrackInfo(std::vector<Format> &audioTrack) override;
53     int32_t GetSubtitleTrackInfo(std::vector<Format> &subtitleTrack) override;
54     int32_t GetVideoWidth() override;
55     int32_t GetVideoHeight() override;
56     int32_t GetDuration(int32_t &duration) override;
57     int32_t SetPlaybackSpeed(PlaybackRateMode mode) override;
58     int32_t GetPlaybackSpeed(PlaybackRateMode &mode) override;
59     int32_t SetParameter(const Format &param) override;
60     int32_t SetLooping(bool loop) override;
61     int32_t SelectBitRate(uint32_t bitRate) override;
62     int32_t SetVideoScaleType(VideoScaleType videoScaleType) override;
63     int32_t SetAudioRendererInfo(const int32_t contentType, const int32_t streamUsage,
64         const int32_t rendererFlag) override;
65     int32_t SetAudioInterruptMode(const int32_t interruptMode) override;
66     int32_t SelectTrack(int32_t index) override;
67     int32_t DeselectTrack(int32_t index) override;
68     int32_t GetCurrentTrack(int32_t trackType, int32_t &index) override;
69     int32_t SetAudioEffectMode(const int32_t effectMode) override;
70     int32_t GetHEBCMode() override;
71     int32_t HandleCodecBuffers(bool enable) override;
72     int32_t SeekToCurrentTime(int32_t mSeconds, PlayerSeekMode mode) override;
73 
74 private:
75     void OnNotifyMessage(const PlayBinMessage &msg);
76     void OnNotifyElemSetup(GstElement &elem);
77     void OnNotifyElemUnSetup(GstElement &elem);
78     void OnReset();
79     GValueArray *OnNotifyAutoPlugSort(GValueArray &factories);
80     double ChangeModeToSpeed(const PlaybackRateMode &mode) const;
81     PlaybackRateMode ChangeSpeedToMode(double rate) const;
82     int32_t PlayBinCtrlerInit();
83     int32_t PlayBinCtrlerPrepare();
84     void PlayBinCtrlerDeInit();
85     int32_t GetRealPath(const std::string &url, std::string &realUrlPath) const;
86     bool IsFileUrl(const std::string &url) const;
87     void HandleErrorMessage(const PlayBinMessage &msg);
88     void HandleInfoMessage(const PlayBinMessage &msg);
89     void HandleSeekDoneMessage(const PlayBinMessage &msg);
90     void HandleSpeedDoneMessage(const PlayBinMessage &msg);
91     void HandleSubTypeMessage(const PlayBinMessage &msg);
92     void HandleBufferingStart(const PlayBinMessage &msg);
93     void HandleBufferingEnd(const PlayBinMessage &msg);
94     void HandleBufferingTime(const PlayBinMessage &msg);
95     void HandleBufferingPercent(const PlayBinMessage &msg);
96     void HandleBufferingUsedMqNum(const PlayBinMessage &msg);
97     void HandleVideoRenderingStart(const PlayBinMessage &msg);
98     void HandleVideoSizeChanged(const PlayBinMessage &msg);
99     void HandleBitRateCollect(const PlayBinMessage &msg);
100     void HandleIsLiveStream(const PlayBinMessage &msg);
101     void HandleTrackNumUpdate(const PlayBinMessage &msg);
102     void HandleTrackInfoUpdate(const PlayBinMessage &msg);
103     void HandleAudioMessage(const PlayBinMessage &msg);
104     void HandleInterruptMessage(const PlayBinMessage &msg);
105     void HandlePositionUpdateMessage(const PlayBinMessage &msg);
106     void HandleSubtitleUpdate(const PlayBinMessage &msg);
107     void HandleTrackChanged(const PlayBinMessage &msg);
108     void HandleDefaultTrack(const PlayBinMessage &msg);
109     void HandleTrackDone(const PlayBinMessage &msg);
110     void HandleAddSubDone(const PlayBinMessage &msg);
111     void HandleOnError(const PlayBinMessage &msg);
112     void OnCapsFixError();
113     void ResetPlaybinToSoftDec();
114 
115     std::mutex mutex_;
116     std::mutex sinkProviderMutex_;
117     std::shared_ptr<IPlayBinCtrler> playBinCtrler_ = nullptr;
118     std::shared_ptr<PlayBinSinkProvider> sinkProvider_;
119     std::weak_ptr<IPlayerEngineObs> obs_;
120     sptr<Surface> producerSurface_ = nullptr;
121     std::string url_ = "";
122     std::shared_ptr<GstAppsrcEngine> appsrcWrap_ = nullptr;
123     PlayerCodecCtrl codecCtrl_;
124     int32_t videoWidth_ = 0;
125     int32_t videoHeight_ = 0;
126     int32_t updatePercent_ = -1;
127     uint32_t mqNum_ = 0;
128     uint64_t bufferingTime_ = 0;
129     int32_t appuid_ = 0;
130     int32_t apppid_ = 0;
131     uint32_t apptokenid_ = 0;
132     std::map<uint32_t, uint64_t> mqBufferingTime_;
133     VideoScaleType videoScaleType_ = VIDEO_SCALE_TYPE_FIT;
134     int32_t contentType_ = AudioStandard::CONTENT_TYPE_MUSIC; // CONTENT_TYPE_MUSIC
135     int32_t streamUsage_ = AudioStandard::STREAM_USAGE_MEDIA; // STREAM_USAGE_MEDIA
136     int32_t rendererFlag_ = 0;
137     int32_t currentTime_ = -1;
138     int32_t duration_ = -1;
139     int32_t currentTimeOnInfoCnt_ = 0;
140     bool isPlaySinkFlagsSet_ = false;
141     bool useSoftDec_ = false;
142     std::unique_ptr<TaskQueue> taskQueue_;
143     bool isAdaptiveLiveStream_ = false;
144     std::map<int32_t, void(PlayerEngineGstImpl::*)(const PlayBinMessage &msg)> subMsgHandler_;
145 };
146 } // namespace Media
147 } // namespace OHOS
148 #endif // PLAYER_ENGINE_GST_IMPL
149