1 /* 2 * Copyright (c) 2021-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 HISTREAMER_SCENE_STD_HIPLAYER_IMPL_H 17 #define HISTREAMER_SCENE_STD_HIPLAYER_IMPL_H 18 19 #include <memory> 20 #include <unordered_map> 21 22 #include "hiplayer_callback_looper.h" 23 #include <i_player_engine.h> 24 #include "foundation/osal/thread/condition_variable.h" 25 #include "foundation/osal/thread/mutex.h" 26 #include "hiplayer_callback_looper.h" 27 #include "internal/state_machine.h" 28 #include "pipeline/core/error_code.h" 29 #include "pipeline/core/media_sync_manager.h" 30 #include "pipeline/core/pipeline.h" 31 #include "pipeline/core/pipeline_core.h" 32 #include "pipeline/filters/codec/audio_decoder/audio_decoder_filter.h" 33 #include "pipeline/filters/demux/demuxer_filter.h" 34 #include "pipeline/filters/sink/audio_sink/audio_sink_filter.h" 35 #include "pipeline/filters/source/media_source/media_source_filter.h" 36 #ifdef VIDEO_SUPPORT 37 #include "pipeline/filters/codec/video_decoder/video_decoder_filter.h" 38 #include "pipeline/filters/sink/video_sink/video_sink_filter.h" 39 #endif 40 #include "scene/common/media_stat_stub.h" 41 #include "play_executor.h" 42 43 namespace OHOS { 44 namespace Media { 45 class HiPlayerImpl : public Pipeline::EventReceiver, 46 public StateChangeCallback, 47 public Pipeline::FilterCallback, 48 public IPlayerEngine { 49 friend class StateMachine; 50 51 public: 52 HiPlayerImpl(int32_t appUid, int32_t appPid); 53 ~HiPlayerImpl() override; 54 HiPlayerImpl(const HiPlayerImpl& other) = delete; 55 HiPlayerImpl& operator=(const HiPlayerImpl& other) = delete; 56 ErrorCode Init(); 57 // interface from PlayerInterface 58 int32_t SetSource(const std::string& uri) override; 59 int32_t SetSource(const std::shared_ptr<IMediaDataSource>& dataSrc) override; 60 int32_t Prepare() override; 61 int32_t PrepareAsync() override; 62 int32_t Play() override; 63 int32_t Pause() override; 64 int32_t Stop() override; 65 int32_t Reset() override; 66 int32_t Seek(int32_t mSeconds, PlayerSeekMode mode) override; 67 int32_t SetVolume(float leftVolume, float rightVolume) override; 68 int32_t SetVideoSurface(sptr<Surface> surface) override; 69 int32_t SetLooping(bool loop) override; 70 int32_t SetParameter(const Format& params) override; 71 int32_t SetObs(const std::weak_ptr<IPlayerEngineObs>& obs) override; 72 int32_t GetCurrentTime(int32_t& currentPositionMs) override; 73 int32_t GetDuration(int32_t& durationMs) override; 74 int32_t SetPlaybackSpeed(PlaybackRateMode mode) override; 75 int32_t GetPlaybackSpeed(PlaybackRateMode& mode) override; 76 77 int32_t GetVideoTrackInfo(std::vector<Format>& videoTrack) override; 78 int32_t GetAudioTrackInfo(std::vector<Format>& audioTrack) override; 79 int32_t GetVideoWidth() override; 80 int32_t GetVideoHeight() override; 81 int32_t SetVideoScaleType(VideoScaleType videoScaleType) override; 82 int32_t SetAudioRendererInfo(const int32_t contentType, const int32_t streamUsage, 83 const int32_t rendererFlag) override; 84 int32_t SetAudioInterruptMode(const int32_t interruptMode) override; 85 86 // internal interfaces 87 void OnEvent(const Event& event) override; 88 void OnStateChanged(StateId state) override; 89 ErrorCode OnCallback(const Pipeline::FilterCallbackType& type, Pipeline::Filter* filter, 90 const Plugin::Any& parameter) override; 91 92 ErrorCode DoSetSource(const std::shared_ptr<MediaSource>& source); 93 ErrorCode PrepareFilters(); 94 ErrorCode DoPlay(); 95 ErrorCode DoPause(); 96 ErrorCode DoResume(); 97 ErrorCode DoStop(); 98 ErrorCode DoReset(); 99 ErrorCode DoSeek(int64_t hstTime, Plugin::SeekMode mode); 100 ErrorCode DoOnReady(); 101 ErrorCode DoOnComplete(); 102 ErrorCode DoOnError(ErrorCode); 103 104 private: 105 ErrorCode StopAsync(); 106 ErrorCode SetVolumeToSink(float volume, bool reportUpward = true); 107 Pipeline::PFilter CreateAudioDecoder(const std::string& desc); 108 ErrorCode NewAudioPortFound(Pipeline::Filter* filter, const Plugin::Any& parameter); 109 #ifdef VIDEO_SUPPORT 110 ErrorCode NewVideoPortFound(Pipeline::Filter* filter, const Plugin::Any& parameter); 111 #endif 112 ErrorCode RemoveFilterChains(Pipeline::Filter* filter, const Plugin::Any& parameter); 113 void ActiveFilters(const std::vector<Pipeline::Filter*>& filters); 114 void HandleErrorEvent(const Event& event); 115 void HandleReadyEvent(); 116 void HandleCompleteEvent(const Event& event); 117 void HandlePluginErrorEvent(const Event& event); 118 void UpdateStateNoLock(PlayerStates newState, bool notifyUpward = true); 119 double ChangeModeToSpeed(const PlaybackRateMode& mode) const; 120 PlaybackRateMode ChangeSpeedToMode(double rate) const; 121 void NotifyBufferingUpdate(const std::string_view& type, int32_t param); 122 void HandleResolutionChangeEvent(const Event& event); 123 void HandlePluginEvent(const Event& event); 124 125 OSAL::Mutex stateMutex_ {}; 126 OSAL::ConditionVariable cond_ {}; 127 int32_t appUid_ {0}; 128 int32_t appPid_ {0}; 129 std::shared_ptr<Pipeline::PipelineCore> pipeline_; 130 std::atomic<PlayerStates> pipelineStates_ {PlayerStates::PLAYER_IDLE}; // only update in UpdateStateNoLock() 131 std::queue<PlayerStates> pendingStates_ {}; 132 std::atomic<bool> initialized_ {false}; 133 134 std::weak_ptr<Plugin::Meta> sourceMeta_ {}; 135 std::vector<std::weak_ptr<Plugin::Meta>> streamMeta_ {}; 136 int64_t duration_ {-1}; 137 std::atomic<bool> singleLoop_ {false}; 138 float volume_; 139 MediaStatStub mediaStats_; 140 141 std::shared_ptr<Pipeline::MediaSourceFilter> audioSource_; 142 std::shared_ptr<Pipeline::DemuxerFilter> demuxer_; 143 std::shared_ptr<Pipeline::AudioDecoderFilter> audioDecoder_; 144 std::shared_ptr<Pipeline::AudioSinkFilter> audioSink_; 145 #ifdef VIDEO_SUPPORT 146 std::shared_ptr<Pipeline::VideoDecoderFilter> videoDecoder_; 147 std::shared_ptr<Pipeline::VideoSinkFilter> videoSink_; 148 #endif 149 std::unordered_map<std::string, std::shared_ptr<Pipeline::AudioDecoderFilter>> audioDecoderMap_; 150 std::shared_ptr<Pipeline::MediaSyncManager> syncManager_; 151 HiPlayerCallbackLooper callbackLooper_ {}; 152 sptr<Surface> surface_ {nullptr}; 153 154 int32_t videoWidth_ {0}; 155 int32_t videoHeight_ {0}; 156 std::string url_; 157 }; 158 } // namespace Media 159 } // namespace OHOS 160 #endif // HISTREAMER_SCENE_STD_HIPLAYER_IMPL_H 161