1 /*
2  * Copyright (c) 2023-2024 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 GetDuration(int32_t& durationMs) override;
122     int32_t SetPlaybackSpeed(PlaybackRateMode mode) override;
123     int32_t SetMediaSource(const std::shared_ptr<AVMediaSource> &mediaSource, AVPlayStrategy strategy) override;
124     int32_t GetPlaybackSpeed(PlaybackRateMode& mode) override;
125     int32_t SelectBitRate(uint32_t bitRate) override;
126     int32_t GetAudioEffectMode(int32_t &effectMode) override;
127     int32_t SetAudioEffectMode(int32_t effectMode) override;
128 
129     int32_t GetCurrentTrack(int32_t trackType, int32_t &index) override;
130     int32_t SelectTrack(int32_t trackId, PlayerSwitchMode mode) override;
131     int32_t DeselectTrack(int32_t trackId) override;
132     int32_t GetVideoTrackInfo(std::vector<Format>& videoTrack) override;
133     int32_t GetPlaybackInfo(Format& playbackInfo) override;
134     int32_t GetAudioTrackInfo(std::vector<Format>& audioTrack) override;
135     int32_t GetSubtitleTrackInfo(std::vector<Format>& subtitleTrack) override;
136     int32_t GetVideoWidth() override;
137     int32_t GetVideoHeight() override;
138     int32_t SetVideoScaleType(VideoScaleType videoScaleType) override;
139     int32_t SetAudioRendererInfo(const int32_t contentType, const int32_t streamUsage,
140                                  const int32_t rendererFlag) override;
141     int32_t SetAudioInterruptMode(const int32_t interruptMode) override;
142     int32_t SeekToCurrentTime(int32_t mSeconds, PlayerSeekMode mode) override;
143     void SetInterruptState(bool isInterruptNeeded) override;
144     void OnDumpInfo(int32_t fd) override;
145     void SetInstancdId(uint64_t instanceId) override;
146     int64_t GetPlayRangeStartTime() override;
147     int64_t GetPlayRangeEndTime() override;
148     int32_t GetPlayRangeSeekMode() override;
149 
150     // internal interfaces
151     void OnEvent(const Event &event);
152     void OnEventSub(const Event &event);
153     void OnEventSubTrackChange(const Event &event);
154     void HandleDfxEvent(const DfxEvent &event);
155     void OnStateChanged(PlayerStateId state, bool isSystemOperation = false);
156     Status OnCallback(std::shared_ptr<Filter> filter, const FilterCallBackCommand cmd,
157                     StreamType outType);
158     int32_t SeekContinous(int32_t mSeconds, int64_t seekContinousBatchNo) override;
159     int32_t ExitSeekContinous(bool align, int64_t seekContinousBatchNo) override;
160     int32_t PauseDemuxer() override;
161     int32_t ResumeDemuxer() override;
162     int32_t SetPlaybackStrategy(AVPlayStrategy playbackStrategy) override;
163     int32_t SetMediaMuted(OHOS::Media::MediaType mediaType, bool isMuted) override;
164     float GetMaxAmplitude() override;
165     int32_t SetMaxAmplitudeCbStatus(bool status) override;
166     int32_t IsSeekContinuousSupported(bool &isSeekContinuousSupported) override;
167 private:
168     enum HiplayerSvpMode : int32_t {
169         SVP_CLEAR = -1, /* it's not a protection video */
170         SVP_FALSE, /* it's a protection video but not need secure decoder */
171         SVP_TRUE, /* it's a protection video and need secure decoder */
172     };
173 
174     Status DoSetSource(const std::shared_ptr<MediaSource> source);
175     void DoSetPlayStrategy(const std::shared_ptr<MediaSource> source);
176     Status Resume();
177     void GetDumpFlag();
178     void HandleCompleteEvent(const Event& event);
179     void HandleInitialPlayingStateChange(const EventType& eventType);
180     void HandleDrmInfoUpdatedEvent(const Event& event);
181     void HandleIsLiveStreamEvent(bool isLiveStream);
182     void HandleErrorEvent(int32_t errorCode);
183     void HandleResolutionChangeEvent(const Event& event);
184     void HandleBitrateStartEvent(const Event& event);
185     void HandleAudioTrackChangeEvent(const Event& event);
186     void HandleVideoTrackChangeEvent(const Event& event);
187     void HandleSubtitleTrackChangeEvent(const Event& event);
188     void NotifyBufferingStart(int32_t param);
189     void NotifyBufferingEnd(int32_t param);
190     void NotifyCachedDuration(int32_t param);
191     void UpdateStateNoLock(PlayerStates newState, bool notifyUpward = true, bool isSystemOperation = false);
192     void NotifyBufferingUpdate(const std::string_view& type, int32_t param);
193     void NotifyDurationUpdate(const std::string_view& type, int32_t param);
194     void NotifySeekDone(int32_t seekPos);
195     void NotifyAudioInterrupt(const Event& event);
196     void NotifyAudioDeviceChange(const Event& event);
197     void NotifyAudioServiceDied();
198     void NotifyAudioFirstFrame(const Event& event);
199     void NotifyResolutionChange();
200     void NotifyPositionUpdate();
201     Status LinkAudioDecoderFilter(const std::shared_ptr<Filter>& preFilter, StreamType type);
202     Status LinkAudioSinkFilter(const std::shared_ptr<Filter>& preFilter, StreamType type);
203     Status LinkSubtitleSinkFilter(const std::shared_ptr<Filter>& preFilter, StreamType type);
204     void NotifySubtitleUpdate(const Event& event);
205     void DoInitializeForHttp();
206     bool EnableBufferingBySysParam() const;
207     bool IsFileUrl(const std::string &url) const;
208     bool IsValidPlayRange(int64_t start, int64_t end) const;
209     int32_t GetRealPath(const std::string &url, std::string &realUrlPath) const;
210     void SetDefaultAudioRenderInfo(const std::vector<std::shared_ptr<Meta>> &trackInfos);
211     void AppendPlayerMediaInfo();
212     int64_t GetCurrentMillisecond();
213     void UpdatePlayStatistics();
214     void DoSetMediaSource(Status& ret);
215     void UpdatePlayerStateAndNotify();
216     void UpdateMediaFirstPts();
217     void UpdateMaxSeekLatency(PlayerSeekMode mode, int64_t seekStartTime);
218 #ifdef SUPPORT_VIDEO
219     Status LinkVideoDecoderFilter(const std::shared_ptr<Filter>& preFilter, StreamType type);
220     bool IsVideoMime(const std::string& mime);
221 #endif
222     bool IsAudioMime(const std::string& mime);
223     bool IsSubtitleMime(const std::string& mime);
224     Status Seek(int64_t mSeconds, PlayerSeekMode mode, bool notifySeekDone);
225     Status PrepareForSeek();
226     Status HandleSeek(int64_t seekPos, PlayerSeekMode mode);
227 
228     Status doPreparedSeek(int64_t seekPos, PlayerSeekMode mode);
229     Status doStartedSeek(int64_t seekPos, PlayerSeekMode mode);
230     Status doPausedSeek(int64_t seekPos, PlayerSeekMode mode);
231     Status doCompletedSeek(int64_t seekPos, PlayerSeekMode mode);
232     Status doSeek(int64_t seekPos, PlayerSeekMode mode);
233     Status HandleSeekClosest(int64_t seekPos, int64_t seekTimeUs);
234     void NotifySeek(Status rtv, bool flag, int64_t seekPos);
235     void ResetIfSourceExisted();
236     void ReleaseInner();
237     int32_t InitDuration();
238     int32_t InitVideoWidthAndHeight();
239     int32_t SetFrameRateForSeekPerformance(double frameRate);
240     void SetBundleName(std::string bundleName);
241     Status InitAudioDefaultTrackIndex();
242     Status InitVideoDefaultTrackIndex();
243     Status InitSubtitleDefaultTrackIndex();
244     bool BreakIfInterruptted();
245     bool IsSeekInSitu(int64_t mSeconds);
246     void CollectionErrorInfo(int32_t errCode, const std::string& errMsg);
247     void NotifyUpdateTrackInfo();
248     Status SelectSeekType(int64_t seekPos, PlayerSeekMode mode);
249     Status DoSetPlayRange();
250     void ResetPlayRangeParameter();
251     bool IsInValidSeekTime(int32_t seekPos);
252     int64_t GetPlayStartTime();
253     Status StartSeekContinous();
254     int32_t InnerSelectTrack(std::string mime, int32_t trackId, PlayerSwitchMode mode);
255     bool NeedSeekClosest();
256     void HandleEosFlagState(const Event& event);
257     Status InnerDoSeek(int64_t seekTimeUs, int64_t seekPos, PlayerSeekMode mode);
258     int32_t GetSarVideoWidth(std::shared_ptr<Meta> trackInfo);
259     int32_t GetSarVideoHeight(std::shared_ptr<Meta> trackInfo);
260     int32_t HandleEosPlay() override;
261 
262     bool isNetWorkPlay_ = false;
263     bool isDump_ = false;
264     int32_t appUid_{0};
265     int32_t appPid_{0};
266     int32_t appTokenId_{0};
267     int64_t appFullTokenId_{0};
268     OHOS::Media::Mutex stateMutex_{};
269     OHOS::Media::Mutex initialPlayingEventMutex_{};
270     OHOS::Media::ConditionVariable cond_{};
271     std::atomic<bool> renderFirstFrame_ {false};
272     std::atomic<bool> singleLoop_ {false};
273     std::atomic<bool> isSeek_ {false};
274     std::atomic<bool> isSeekClosest_ {false};
275     std::atomic<PlaybackRateMode> playbackRateMode_ {PlaybackRateMode::SPEED_FORWARD_1_00_X};
276 
277     std::shared_ptr<EventReceiver> playerEventReceiver_;
278     std::shared_ptr<FilterCallback> playerFilterCallback_;
279     std::vector<std::weak_ptr<Meta>> streamMeta_{};
280     std::atomic<PlayerStates> pipelineStates_ {PlayerStates::PLAYER_IDLE}; // only update in UpdateStateNoLock()
281     std::queue<PlayerStates> pendingStates_ {};
282     std::shared_ptr<OHOS::Media::Pipeline::Pipeline> pipeline_;
283     std::shared_ptr<DemuxerFilter> demuxer_;
284     std::shared_ptr<AudioDecoderFilter> audioDecoder_;
285     std::shared_ptr<AudioSinkFilter> audioSink_;
286     std::shared_ptr<SubtitleSinkFilter> subtitleSink_;
287 #ifdef SUPPORT_VIDEO
288     std::shared_ptr<DecoderSurfaceFilter> videoDecoder_;
289 #endif
290     std::shared_ptr<MediaSyncManager> syncManager_;
291     std::atomic<PlayerStateId> curState_;
292     HiPlayerCallbackLooper callbackLooper_{};
293     sptr<Surface> surface_ {nullptr};
294     std::string url_;
295     std::string subUrl_;
296     bool hasExtSub_ {false};
297     std::atomic<int32_t> durationMs_{-1};
298     int64_t mediaStartPts_{0};
299     std::shared_ptr<IMediaDataSource> dataSrc_{nullptr};
300     std::atomic<int32_t> videoWidth_{0};
301     std::atomic<int32_t> videoHeight_{0};
302     std::atomic<bool> needSwapWH_{false};
303     std::atomic<bool> isInterruptNeeded_{false};
304 
305     std::shared_ptr<Meta> audioRenderInfo_{nullptr};
306     std::shared_ptr<Meta> audioInterruptMode_{nullptr};
307 
308     bool isStreaming_{false};
309 
310     int32_t rotation90 = 90;
311     int32_t rotation270 = 270;
312 
313     std::shared_ptr<SeekAgent> seekAgent_;
314 
315     std::mutex drmMutex_;
316     std::condition_variable drmConfigCond_;
317     bool isDrmProtected_ = false;
318     bool isDrmPrepared_ = false;
319     bool stopWaitingDrmConfig_ = false;
320     sptr<DrmStandard::IMediaKeySessionService> keySessionServiceProxy_{nullptr};
321     int32_t svpMode_ = HiplayerSvpMode::SVP_CLEAR;
322 
323     bool isInitialPlay_ = true;
324     std::vector<std::pair<EventType, bool>> initialAVStates_;
325     std::vector<std::pair<std::string, bool>> completeState_;
326     std::mutex seekMutex_;
327     std::string bundleName_ {};
328 
329     bool isInCompleted_ {false};
330 
331     std::map<std::string, std::string> header_;
332     uint32_t preferedWidth_ = 0;
333     uint32_t preferedHeight_ = 0;
334     uint32_t bufferDuration_ = 0;
335     bool preferHDR_ = false;
336     OHOS::Media::MediaType mutedMediaType_ = OHOS::Media::MediaType::MEDIA_TYPE_MAX_COUNT;
337     std::string audioLanguage_;
338     std::string subtitleLanguage_;
339     std::string playerId_;
340     std::string mimeType_;
341     int32_t currentAudioTrackId_ = -1;
342     int32_t defaultAudioTrackId_ = -1;
343     int32_t currentVideoTrackId_ = -1;
344     int32_t defaultVideoTrackId_ = -1;
345     int32_t currentSubtitleTrackId_ = -1;
346     int32_t defaultSubtitleTrackId_ = -1;
347     PlayStatisticalInfo playStatisticalInfo_;
348     int64_t startTime_ = 0;
349     int64_t maxSeekLatency_ = 0;
350     int64_t maxAccurateSeekLatency_ = 0;
351     uint64_t instanceId_ = 0;
352     int64_t maxSurfaceSwapLatency_ = 0;
353     int64_t playTotalDuration_ = 0;
354     bool inEosSeek_ = false;
355     std::atomic<bool> isDoCompletedSeek_{false};
356     OHOS::Media::Mutex stateChangeMutex_{};
357     int64_t playRangeStartTime_ = -1;
358     int64_t playRangeEndTime_ = -1;
359     bool isSetPlayRange_ = false;
360     int64_t startTimeWithMode_ = -1;
361     int64_t endTimeWithMode_ = -1;
362     PlayerSeekMode playRangeSeekMode_ = PlayerSeekMode::SEEK_PREVIOUS_SYNC;
363 
364     std::mutex seekContinousMutex_;
365     std::atomic<int64_t> seekContinousBatchNo_ {-1};
366     std::shared_ptr<DraggingPlayerAgent> draggingPlayerAgent_ {nullptr};
367     int64_t lastSeekContinousPos_ {-1};
368     std::atomic<bool> needUpdateSubtitle_ {true};
369     std::shared_ptr<DfxAgent> dfxAgent_{};
370     bool maxAmplitudeCbStatus_ {false};
371     OHOS::Media::Mutex handleCompleteMutex_{};
372     int64_t playStartTime_ = 0;
373     std::atomic<bool> isBufferingStartNotified_ {false};
374 };
375 } // namespace Media
376 } // namespace OHOS
377 #endif // HI_PLAYER_IMPL_H
378