1 /*
2  * Copyright (c) 2023-2023 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 HI_PLAYER_IMPL_H
17 #define HI_PLAYER_IMPL_H
18 
19 #include <memory>
20 #include <unordered_map>
21 #include <queue>
22 
23 #include "audio_decoder_filter.h"
24 #include "audio_sink_filter.h"
25 #include "common/status.h"
26 #include "demuxer_filter.h"
27 #include "filter/filter.h"
28 #include "filter/filter_factory.h"
29 #include "hiplayer_callback_looper.h"
30 #include "media_utils.h"
31 #include "i_player_engine.h"
32 #include "media_sync_manager.h"
33 #include "pipeline/pipeline.h"
34 #include "seek_agent.h"
35 #include "dfx_agent.h"
36 #include "subtitle_sink_filter.h"
37 #include "meta/meta.h"
38 #include <chrono>
39 #include "dragging_player_agent.h"
40 #ifdef SUPPORT_VIDEO
41 #include "decoder_surface_filter.h"
42 #endif
43 
44 namespace OHOS {
45 namespace Media {
46 using namespace Pipeline;
47 struct PlayStatisticalInfo {
48     int32_t errCode {0};
49     std::string errMsg {};
50     int32_t playDuration {0};
51     int32_t sourceType {0};
52     std::string sourceUrl {};
53     int32_t avgDownloadRate {0};
54     std::string containerMime {};
55     std::string videoMime {};
56     std::string videoResolution {};
57     float videoFrameRate {0.0};
58     int8_t videoBitdepth {0};
59     int32_t videoBitrate {0};
60     int8_t hdrType {0};
61     std::string audioMime {};
62     int32_t audioSampleRate {0};
63     int32_t audioChannelCount {0};
64     int32_t audioBitrate {0};
65     std::string subtitleMime {};
66     std::string subtitleLang {};
67     bool isDrmProtected {false};
68     int32_t startLatency {0};
69     int32_t avgDownloadSpeed {0};
70     int32_t maxSeekLatency {0};
71     int32_t maxAccurateSeekLatency {0};
72     int32_t lagTimes {0};
73     int32_t maxLagDuration {0};
74     int32_t avgLagDuration {0};
75     int32_t maxSurfaceSwapLatency {0};
76     uint64_t totalDownLoadBits {0};
77     bool isTimeOut {false};
78 };
79 
80 enum VideoHdrType : int32_t {
81     /**
82      * This option is used to mark none HDR type.
83      */
84     VIDEO_HDR_TYPE_NONE,
85     /**
86      * This option is used to mark HDR Vivid type.
87      */
88     VIDEO_HDR_TYPE_VIVID,
89 };
90 
91 
92 class HiPlayerImpl : public IPlayerEngine, public std::enable_shared_from_this<HiPlayerImpl> {
93 public:
94     HiPlayerImpl(int32_t appUid, int32_t appPid, uint32_t appTokenId, uint64_t appFullTokenId);
95     ~HiPlayerImpl() override;
96     HiPlayerImpl(const HiPlayerImpl& other) = delete;
97     HiPlayerImpl& operator=(const HiPlayerImpl& other) = delete;
98     Status Init();
99     // interface from PlayerInterface
100     int32_t SetSource(const std::string& uri) override;
101     int32_t SetSource(const std::shared_ptr<IMediaDataSource>& dataSrc) override;
102     int32_t AddSubSource(const std::string &url) override;
103     int32_t Prepare() override;
104     int32_t SetRenderFirstFrame(bool display) override;
105     int32_t SetPlayRange(int64_t start, int64_t end) override;
106     int32_t SetPlayRangeWithMode(int64_t start, int64_t end, PlayerSeekMode mode) override;
107     int32_t PrepareAsync() override;
108     int32_t Play() override;
109     int32_t Pause(bool isSystemOperation) override;
110     int32_t Stop() override;
111     int32_t Reset() override;
112     int32_t Seek(int32_t mSeconds, PlayerSeekMode mode) override;
113     int32_t SetVolume(float leftVolume, float rightVolume) override;
114     int32_t SetVideoSurface(sptr<Surface> surface) override;
115     int32_t SetDecryptConfig(const sptr<OHOS::DrmStandard::IMediaKeySessionService> &keySessionProxy,
116         bool svp) override;
117     int32_t SetLooping(bool loop) override;
118     int32_t SetParameter(const Format& params) override;
119     int32_t SetObs(const std::weak_ptr<IPlayerEngineObs>& obs) override;
120     int32_t GetCurrentTime(int32_t& currentPositionMs) override;
121     int32_t GetLiveStreamCurrentTime(int32_t& durationMs) override;
122     int32_t GetDuration(int32_t& durationMs) override;
123     int32_t SetPlaybackSpeed(PlaybackRateMode mode) override;
124     int32_t SetMediaSource(const std::shared_ptr<AVMediaSource> &mediaSource, AVPlayStrategy strategy) override;
125     int32_t GetPlaybackSpeed(PlaybackRateMode& mode) override;
126     int32_t SelectBitRate(uint32_t bitRate) override;
127     int32_t StopBufferring(bool flag) override;
128     int32_t GetAudioEffectMode(int32_t &effectMode) override;
129     int32_t SetAudioEffectMode(int32_t effectMode) override;
130 
131     int32_t GetCurrentTrack(int32_t trackType, int32_t &index) override;
132     int32_t SelectTrack(int32_t trackId, PlayerSwitchMode mode) override;
133     int32_t DeselectTrack(int32_t trackId) override;
134     int32_t GetVideoTrackInfo(std::vector<Format>& videoTrack) override;
135     int32_t GetPlaybackInfo(Format& playbackInfo) override;
136     int32_t GetAudioTrackInfo(std::vector<Format>& audioTrack) override;
137     int32_t GetSubtitleTrackInfo(std::vector<Format>& subtitleTrack) override;
138     int32_t GetVideoWidth() override;
139     int32_t GetVideoHeight() override;
140     int32_t SetVideoScaleType(VideoScaleType videoScaleType) override;
141     int32_t SetAudioRendererInfo(const int32_t contentType, const int32_t streamUsage,
142                                  const int32_t rendererFlag) override;
143     int32_t SetAudioInterruptMode(const int32_t interruptMode) override;
144     int32_t SeekToCurrentTime(int32_t mSeconds, PlayerSeekMode mode) override;
145     void SetInterruptState(bool isInterruptNeeded) override;
146     void OnDumpInfo(int32_t fd) override;
147     void SetInstancdId(uint64_t instanceId) override;
148     int64_t GetPlayRangeStartTime() override;
149     int64_t GetPlayRangeEndTime() override;
150     int32_t GetPlayRangeSeekMode() override;
151 
152     // internal interfaces
153     void OnEvent(const Event &event);
154     void OnEventSub(const Event &event);
155     void OnEventSubTrackChange(const Event &event);
156     void OnDfxEvent(const Event &event);
157     void OnStateChanged(PlayerStateId state, bool isSystemOperation = false);
158     Status OnCallback(std::shared_ptr<Filter> filter, const FilterCallBackCommand cmd,
159                     StreamType outType);
160     int32_t SeekContinous(int32_t mSeconds, int64_t seekContinousBatchNo) override;
161     int32_t ExitSeekContinous(bool align, int64_t seekContinousBatchNo) override;
162     int32_t PauseDemuxer() override;
163     int32_t ResumeDemuxer() override;
164     int32_t SetPlaybackStrategy(AVPlayStrategy playbackStrategy) override;
165     int32_t SetMediaMuted(OHOS::Media::MediaType mediaType, bool isMuted) override;
166     float GetMaxAmplitude() override;
167     int32_t SetMaxAmplitudeCbStatus(bool status) override;
168 
169 private:
170     enum HiplayerSvpMode : int32_t {
171         SVP_CLEAR = -1, /* it's not a protection video */
172         SVP_FALSE, /* it's a protection video but not need secure decoder */
173         SVP_TRUE, /* it's a protection video and need secure decoder */
174     };
175 
176     Status DoSetSource(const std::shared_ptr<MediaSource> source);
177     void DoSetPlayStrategy(const std::shared_ptr<MediaSource> source);
178     Status Resume();
179     void GetDumpFlag();
180     void HandleCompleteEvent(const Event& event);
181     void HandleInitialPlayingStateChange(const EventType& eventType);
182     void HandleDrmInfoUpdatedEvent(const Event& event);
183     void HandleIsLiveStreamEvent(bool isLiveStream);
184     void HandleErrorEvent(int32_t errorCode);
185     void HandleResolutionChangeEvent(const Event& event);
186     void HandleBitrateStartEvent(const Event& event);
187     void HandleAudioTrackChangeEvent(const Event& event);
188     void HandleVideoTrackChangeEvent(const Event& event);
189     void HandleSubtitleTrackChangeEvent(const Event& event);
190     void NotifyBufferingStart(int32_t param);
191     void NotifyBufferingEnd(int32_t param);
192     void NotifyCachedDuration(int32_t param);
193     void UpdateStateNoLock(PlayerStates newState, bool notifyUpward = true, bool isSystemOperation = false);
194     void NotifyBufferingUpdate(const std::string_view& type, int32_t param);
195     void NotifyDurationUpdate(const std::string_view& type, int32_t param);
196     void NotifySeekDone(int32_t seekPos);
197     void NotifyAudioInterrupt(const Event& event);
198     void NotifyAudioDeviceChange(const Event& event);
199     void NotifyAudioServiceDied();
200     void NotifyAudioFirstFrame(const Event& event);
201     void NotifyResolutionChange();
202     void NotifyPositionUpdate();
203     Status LinkAudioDecoderFilter(const std::shared_ptr<Filter>& preFilter, StreamType type);
204     Status LinkAudioSinkFilter(const std::shared_ptr<Filter>& preFilter, StreamType type);
205     Status LinkSubtitleSinkFilter(const std::shared_ptr<Filter>& preFilter, StreamType type);
206     void NotifySubtitleUpdate(const Event& event);
207     void DoInitializeForHttp();
208     bool EnableBufferingBySysParam() const;
209     bool IsFileUrl(const std::string &url) const;
210     bool IsValidPlayRange(int64_t start, int64_t end) const;
211     int32_t GetRealPath(const std::string &url, std::string &realUrlPath) const;
212     void SetDefaultAudioRenderInfo(const std::vector<std::shared_ptr<Meta>> &trackInfos);
213     void AppendPlayerMediaInfo();
214     int64_t GetCurrentMillisecond();
215     void UpdatePlayStatistics();
216     void DoSetMediaSource(Status& ret);
217     void UpdatePlayerStateAndNotify();
218     void UpdateMediaFirstPts();
219     void UpdateMaxSeekLatency(PlayerSeekMode mode, int64_t seekStartTime);
220 #ifdef SUPPORT_VIDEO
221     Status LinkVideoDecoderFilter(const std::shared_ptr<Filter>& preFilter, StreamType type);
222     bool IsVideoMime(const std::string& mime);
223 #endif
224     bool IsAudioMime(const std::string& mime);
225     bool IsSubtitleMime(const std::string& mime);
226     Status Seek(int64_t mSeconds, PlayerSeekMode mode, bool notifySeekDone);
227     Status HandleSeek(int64_t seekPos, PlayerSeekMode mode);
228 
229     Status doPreparedSeek(int64_t seekPos, PlayerSeekMode mode);
230     Status doStartedSeek(int64_t seekPos, PlayerSeekMode mode);
231     Status doPausedSeek(int64_t seekPos, PlayerSeekMode mode);
232     Status doCompletedSeek(int64_t seekPos, PlayerSeekMode mode);
233     Status doSeek(int64_t seekPos, PlayerSeekMode mode);
234     Status HandleSeekClosest(int64_t seekPos, int64_t seekTimeUs);
235     void NotifySeek(Status rtv, bool flag, int64_t seekPos);
236     void ResetIfSourceExisted();
237     void ReleaseInner();
238     int32_t InitDuration();
239     int32_t InitVideoWidthAndHeight();
240     int32_t SetFrameRateForSeekPerformance(double frameRate);
241     void SetBundleName(std::string bundleName);
242     Status InitAudioDefaultTrackIndex();
243     Status InitVideoDefaultTrackIndex();
244     Status InitSubtitleDefaultTrackIndex();
245     bool BreakIfInterruptted();
246     bool IsSeekInSitu(int64_t mSeconds);
247     void CollectionErrorInfo(int32_t errCode, const std::string& errMsg);
248     void NotifyUpdateTrackInfo();
249     Status SelectSeekType(int64_t seekPos, PlayerSeekMode mode);
250     Status DoSetPlayRange();
251     void ResetPlayRangeParameter();
252     bool IsInValidSeekTime(int32_t seekPos);
253     int64_t GetPlayStartTime();
254     Status StartSeekContinous();
255     int32_t InnerSelectTrack(std::string mime, int32_t trackId, PlayerSwitchMode mode);
256     bool NeedSeekClosest();
257     void HandleEosFlagState(const Event& event);
258     int32_t GetSarVideoWidth(std::shared_ptr<Meta> trackInfo);
259     int32_t GetSarVideoHeight(std::shared_ptr<Meta> trackInfo);
260 
261     bool isNetWorkPlay_ = false;
262     bool isDump_ = false;
263     int32_t appUid_{0};
264     int32_t appPid_{0};
265     int32_t appTokenId_{0};
266     int64_t appFullTokenId_{0};
267     OHOS::Media::Mutex stateMutex_{};
268     OHOS::Media::ConditionVariable cond_{};
269     std::atomic<bool> renderFirstFrame_ {false};
270     std::atomic<bool> singleLoop_ {false};
271     std::atomic<bool> isSeek_ {false};
272     std::atomic<bool> isSeekClosest_ {false};
273     std::atomic<PlaybackRateMode> playbackRateMode_ {PlaybackRateMode::SPEED_FORWARD_1_00_X};
274 
275     std::shared_ptr<EventReceiver> playerEventReceiver_;
276     std::shared_ptr<FilterCallback> playerFilterCallback_;
277     std::vector<std::weak_ptr<Meta>> streamMeta_{};
278     std::atomic<PlayerStates> pipelineStates_ {PlayerStates::PLAYER_IDLE}; // only update in UpdateStateNoLock()
279     std::queue<PlayerStates> pendingStates_ {};
280     std::shared_ptr<OHOS::Media::Pipeline::Pipeline> pipeline_;
281     std::shared_ptr<DemuxerFilter> demuxer_;
282     std::shared_ptr<AudioDecoderFilter> audioDecoder_;
283     std::shared_ptr<AudioSinkFilter> audioSink_;
284     std::shared_ptr<SubtitleSinkFilter> subtitleSink_;
285 #ifdef SUPPORT_VIDEO
286     std::shared_ptr<DecoderSurfaceFilter> videoDecoder_;
287 #endif
288     std::shared_ptr<MediaSyncManager> syncManager_;
289     std::atomic<PlayerStateId> curState_;
290     HiPlayerCallbackLooper callbackLooper_{};
291     sptr<Surface> surface_ {nullptr};
292     std::string url_;
293     std::string subUrl_;
294     bool hasExtSub_ {false};
295     std::atomic<int32_t> durationMs_{-1};
296     int64_t mediaStartPts_{0};
297     std::shared_ptr<IMediaDataSource> dataSrc_{nullptr};
298     std::atomic<int32_t> videoWidth_{0};
299     std::atomic<int32_t> videoHeight_{0};
300     std::atomic<bool> needSwapWH_{false};
301     std::atomic<bool> isInterruptNeeded_{false};
302 
303     std::shared_ptr<Meta> audioRenderInfo_{nullptr};
304     std::shared_ptr<Meta> audioInterruptMode_{nullptr};
305 
306     bool isStreaming_{false};
307 
308     int32_t rotation90 = 90;
309     int32_t rotation270 = 270;
310 
311     std::shared_ptr<SeekAgent> seekAgent_;
312 
313     std::mutex drmMutex_;
314     std::condition_variable drmConfigCond_;
315     bool isDrmProtected_ = false;
316     bool isDrmPrepared_ = false;
317     bool stopWaitingDrmConfig_ = false;
318     sptr<DrmStandard::IMediaKeySessionService> keySessionServiceProxy_{nullptr};
319     int32_t svpMode_ = HiplayerSvpMode::SVP_CLEAR;
320 
321     bool isInitialPlay_ = true;
322     std::vector<std::pair<EventType, bool>> initialAVStates_;
323     std::vector<std::pair<std::string, bool>> completeState_;
324     std::mutex seekMutex_;
325     std::string bundleName_ {};
326 
327     std::map<std::string, std::string> header_;
328     uint32_t preferedWidth_ = 0;
329     uint32_t preferedHeight_ = 0;
330     uint32_t bufferDuration_ = 0;
331     bool preferHDR_ = false;
332     OHOS::Media::MediaType mutedMediaType_ = OHOS::Media::MediaType::MEDIA_TYPE_MAX_COUNT;
333     std::string audioLanguage_;
334     std::string subtitleLanguage_;
335     std::string playerId_;
336     std::string mimeType_;
337     int32_t currentAudioTrackId_ = -1;
338     int32_t defaultAudioTrackId_ = -1;
339     int32_t currentVideoTrackId_ = -1;
340     int32_t defaultVideoTrackId_ = -1;
341     int32_t currentSubtitleTrackId_ = -1;
342     int32_t defaultSubtitleTrackId_ = -1;
343     PlayStatisticalInfo playStatisticalInfo_;
344     int64_t startTime_ = 0;
345     int64_t maxSeekLatency_ = 0;
346     int64_t maxAccurateSeekLatency_ = 0;
347     uint64_t instanceId_ = 0;
348     int64_t maxSurfaceSwapLatency_ = 0;
349     int64_t playTotalDuration_ = 0;
350     bool inEosSeek_ = false;
351     std::atomic<bool> isDoCompletedSeek_{false};
352     OHOS::Media::Mutex stateChangeMutex_{};
353     int64_t playRangeStartTime_ = -1;
354     int64_t playRangeEndTime_ = -1;
355     bool isSetPlayRange_ = false;
356     int64_t startTimeWithMode_ = -1;
357     int64_t endTimeWithMode_ = -1;
358     PlayerSeekMode playRangeSeekMode_ = PlayerSeekMode::SEEK_PREVIOUS_SYNC;
359     std::mutex seekContinousMutex_;
360     std::atomic<int64_t> seekContinousBatchNo_ {-1};
361     std::shared_ptr<DraggingPlayerAgent> draggingPlayerAgent_ {nullptr};
362     int64_t lastSeekContinousPos_ {-1};
363     bool isSetVideoSurface_ = false;
364     std::atomic<bool> needUpdateSubtitle_ {true};
365     std::shared_ptr<DfxAgent> dfxAgent_{};
366     bool maxAmplitudeCbStatus_ {false};
367     OHOS::Media::Mutex handleCompleteMutex_{};
368     int64_t playStartTime_ = 0;
369     std::atomic<bool> isBufferingStartNotified_ {false};
370 };
371 } // namespace Media
372 } // namespace OHOS
373 #endif // HI_PLAYER_IMPL_H
374