• 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 "i_player_engine.h"
25 #include "i_playbin_ctrler.h"
26 #include "gst_appsrc_wrap.h"
27 #include "player_track_parse.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);
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 SetObs(const std::weak_ptr<IPlayerEngineObs> &obs) override;
41     int32_t SetVideoSurface(sptr<Surface> surface) override;
42     int32_t Prepare() 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 GetVideoWidth() override;
54     int32_t GetVideoHeight() override;
55     int32_t GetDuration(int32_t &duration) override;
56     int32_t SetPlaybackSpeed(PlaybackRateMode mode) override;
57     int32_t GetPlaybackSpeed(PlaybackRateMode &mode) override;
58     int32_t SetParameter(const Format &param) override;
59     int32_t SetLooping(bool loop) override;
60     int32_t SelectBitRate(uint32_t bitRate) override;
61     int32_t SetVideoScaleType(VideoScaleType videoScaleType) override;
62     int32_t SetAudioRendererInfo(const int32_t contentType, const int32_t streamUsage,
63         const int32_t rendererFlag) override;
64     int32_t SetAudioInterruptMode(const int32_t interruptMode) override;
65 
66 private:
67     void OnNotifyMessage(const PlayBinMessage &msg);
68     void OnNotifyElemSetup(GstElement &elem);
69     void OnNotifyElemUnSetup(GstElement &elem);
70     void OnReset();
71     GValueArray *OnNotifyAutoPlugSort(GValueArray &factories);
72     double ChangeModeToSpeed(const PlaybackRateMode &mode) const;
73     PlaybackRateMode ChangeSpeedToMode(double rate) const;
74     int32_t PlayBinCtrlerInit();
75     int32_t PlayBinCtrlerPrepare();
76     void PlayBinCtrlerDeInit();
77     int32_t GetRealPath(const std::string &url, std::string &realUrlPath) const;
78     bool IsFileUrl(const std::string &url) const;
79     void HandleErrorMessage(const PlayBinMessage &msg);
80     void HandleInfoMessage(const PlayBinMessage &msg);
81     void HandleSeekDoneMessage(const PlayBinMessage &msg);
82     void HandleSpeedDoneMessage(const PlayBinMessage &msg);
83     void HandleSubTypeMessage(const PlayBinMessage &msg);
84     void HandleBufferingStart();
85     void HandleBufferingEnd();
86     void HandleBufferingTime(const PlayBinMessage &msg);
87     void HandleBufferingPercent(const PlayBinMessage &msg);
88     void HandleBufferingUsedMqNum(const PlayBinMessage &msg);
89     void HandleVideoRenderingStart();
90     void HandleVideoSizeChanged(const PlayBinMessage &msg);
91     void HandleBitRateCollect(const PlayBinMessage &msg);
92     void HandleAudioMessage(const PlayBinMessage &msg);
93     void HandleInterruptMessage(const PlayBinMessage &msg);
94     void HandleAudioStateMessage(const PlayBinMessage &msg);
95     void HandlePositionUpdateMessage(const PlayBinMessage &msg);
96     void OnCapsFixError();
97     void ResetPlaybinToSoftDec();
98 
99     std::mutex mutex_;
100     std::mutex trackParseMutex_;
101     std::shared_ptr<IPlayBinCtrler> playBinCtrler_ = nullptr;
102     std::shared_ptr<PlayBinSinkProvider> sinkProvider_;
103     std::weak_ptr<IPlayerEngineObs> obs_;
104     sptr<Surface> producerSurface_ = nullptr;
105     std::string url_ = "";
106     std::shared_ptr<GstAppsrcWrap> appsrcWrap_ = nullptr;
107     std::shared_ptr<PlayerTrackParse> trackParse_ = nullptr;
108     PlayerCodecCtrl codecCtrl_;
109     int32_t videoWidth_ = 0;
110     int32_t videoHeight_ = 0;
111     int32_t percent_ = 0;
112     uint32_t mqNumUsedBuffering_ = 0;
113     uint64_t bufferingTime_ = 0;
114     int32_t appuid_ = 0;
115     int32_t apppid_ = 0;
116     std::map<uint32_t, uint64_t> mqBufferingTime_;
117     VideoScaleType videoScaleType_ = VIDEO_SCALE_TYPE_FIT;
118     int32_t contentType_ = 0;
119     int32_t streamUsage_ = 0;
120     int32_t rendererFlag_ = 0;
121     int32_t currentTime_ = -1;
122     int32_t duration_ = -1;
123     int32_t currentTimeOnInfoCnt_ = 0;
124     bool isPlaySinkFlagsSet_ = false;
125     bool useSoftDec_ = false;
126     std::unique_ptr<TaskQueue> taskQueue_;
127 };
128 } // namespace Media
129 } // namespace OHOS
130 #endif // PLAYER_ENGINE_GST_IMPL
131