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