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 int32_t SelectBitRate(uint32_t bitRate) override; 86 87 // internal interfaces 88 void OnEvent(const Event& event) override; 89 void OnStateChanged(StateId state) override; 90 ErrorCode OnCallback(const Pipeline::FilterCallbackType& type, Pipeline::Filter* filter, 91 const Plugin::Any& parameter) override; 92 93 ErrorCode DoSetSource(const std::shared_ptr<MediaSource>& source); 94 ErrorCode PrepareFilters(); 95 ErrorCode DoPlay(); 96 ErrorCode DoPause(); 97 ErrorCode DoResume(); 98 ErrorCode DoStop(); 99 ErrorCode DoReset(); 100 ErrorCode DoSeek(int64_t hstTime, Plugin::SeekMode mode); 101 ErrorCode DoOnReady(); 102 ErrorCode DoOnComplete(); 103 ErrorCode DoOnError(ErrorCode); 104 105 private: 106 ErrorCode StopAsync(); 107 ErrorCode SetVolumeToSink(float volume, bool reportUpward = true); 108 Pipeline::PFilter CreateAudioDecoder(const std::string& desc); 109 ErrorCode NewAudioPortFound(Pipeline::Filter* filter, const Plugin::Any& parameter); 110 #ifdef VIDEO_SUPPORT 111 ErrorCode NewVideoPortFound(Pipeline::Filter* filter, const Plugin::Any& parameter); 112 #endif 113 ErrorCode RemoveFilterChains(Pipeline::Filter* filter, const Plugin::Any& parameter); 114 void ActiveFilters(const std::vector<Pipeline::Filter*>& filters); 115 void HandleErrorEvent(const Event& event); 116 void HandleReadyEvent(); 117 void HandleCompleteEvent(const Event& event); 118 void HandlePluginErrorEvent(const Event& event); 119 void UpdateStateNoLock(PlayerStates newState, bool notifyUpward = true); 120 double ChangeModeToSpeed(const PlaybackRateMode& mode) const; 121 PlaybackRateMode ChangeSpeedToMode(double rate) const; 122 void NotifyBufferingUpdate(const std::string_view& type, int32_t param); 123 void HandleResolutionChangeEvent(const Event& event); 124 void HandlePluginEvent(const Event& event); 125 126 OSAL::Mutex stateMutex_ {}; 127 OSAL::ConditionVariable cond_ {}; 128 int32_t appUid_ {0}; 129 int32_t appPid_ {0}; 130 std::shared_ptr<Pipeline::PipelineCore> pipeline_; 131 std::atomic<PlayerStates> pipelineStates_ {PlayerStates::PLAYER_IDLE}; // only update in UpdateStateNoLock() 132 std::queue<PlayerStates> pendingStates_ {}; 133 std::atomic<bool> initialized_ {false}; 134 135 std::weak_ptr<Plugin::Meta> sourceMeta_ {}; 136 std::vector<std::weak_ptr<Plugin::Meta>> streamMeta_ {}; 137 int64_t duration_ {-1}; 138 std::atomic<bool> singleLoop_ {false}; 139 float volume_; 140 MediaStatStub mediaStats_; 141 142 std::shared_ptr<Pipeline::MediaSourceFilter> audioSource_; 143 std::shared_ptr<Pipeline::DemuxerFilter> demuxer_; 144 std::shared_ptr<Pipeline::AudioDecoderFilter> audioDecoder_; 145 std::shared_ptr<Pipeline::AudioSinkFilter> audioSink_; 146 #ifdef VIDEO_SUPPORT 147 std::shared_ptr<Pipeline::VideoDecoderFilter> videoDecoder_; 148 std::shared_ptr<Pipeline::VideoSinkFilter> videoSink_; 149 #endif 150 std::unordered_map<std::string, std::shared_ptr<Pipeline::AudioDecoderFilter>> audioDecoderMap_; 151 std::shared_ptr<Pipeline::MediaSyncManager> syncManager_; 152 HiPlayerCallbackLooper callbackLooper_ {}; 153 sptr<Surface> surface_ {nullptr}; 154 155 int32_t videoWidth_ {0}; 156 int32_t videoHeight_ {0}; 157 std::string url_; 158 }; 159 } // namespace Media 160 } // namespace OHOS 161 #endif // HISTREAMER_SCENE_STD_HIPLAYER_IMPL_H 162