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