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 MEDIA_SOURCE_H 17 #define MEDIA_SOURCE_H 18 19 #include <memory> 20 #include <string> 21 22 #include "osal/task/task.h" 23 #include "osal/utils/util.h" 24 #include "common/media_source.h" 25 #include "plugin/plugin_buffer.h" 26 #include "plugin/plugin_info.h" 27 #include "plugin/plugin_base.h" 28 #include "plugin/plugin_event.h" 29 #include "plugin/source_plugin.h" 30 #include "meta/media_types.h" 31 #include "demuxer/media_demuxer.h" 32 #include "performance_utils.h" 33 34 namespace OHOS { 35 namespace Media { 36 using SourceType = OHOS::Media::Plugins::SourceType; 37 using MediaSource = OHOS::Media::Plugins::MediaSource; 38 39 class CallbackImpl : public Plugins::Callback { 40 public: OnEvent(const Plugins::PluginEvent & event)41 void OnEvent(const Plugins::PluginEvent &event) override 42 { 43 if (callbackWrap_) { 44 callbackWrap_->OnEvent(event); 45 } 46 } 47 OnDfxEvent(const Plugins::PluginDfxEvent & event)48 void OnDfxEvent(const Plugins::PluginDfxEvent &event) override 49 { 50 if (callbackWrap_) { 51 callbackWrap_->OnDfxEvent(event); 52 } 53 } 54 SetSelectBitRateFlag(bool flag,uint32_t desBitRate)55 void SetSelectBitRateFlag(bool flag, uint32_t desBitRate) override 56 { 57 if (callbackWrap_) { 58 callbackWrap_->SetSelectBitRateFlag(flag, desBitRate); 59 } 60 } 61 CanAutoSelectBitRate()62 bool CanAutoSelectBitRate() override 63 { 64 if (callbackWrap_) { 65 return callbackWrap_->CanAutoSelectBitRate(); 66 } 67 return false; 68 } 69 SetCallbackWrap(Callback * callbackWrap)70 void SetCallbackWrap(Callback* callbackWrap) 71 { 72 callbackWrap_ = callbackWrap; 73 } 74 75 private: 76 Callback* callbackWrap_ {nullptr}; 77 }; 78 79 class Source : public Plugins::Callback { 80 public: 81 explicit Source(); 82 ~Source() override; 83 84 virtual Status SetSource(const std::shared_ptr<MediaSource>& source); 85 void SetBundleName(const std::string& bundleName); 86 Status Prepare(); 87 Status Start(); 88 Status Stop(); 89 Status Pause(); 90 Status Resume(); 91 Status SetReadBlockingFlag(bool isReadBlockingAllowed); 92 Plugins::Seekable GetSeekable(); 93 94 Status GetSize(uint64_t &fileSize); 95 96 void OnEvent(const Plugins::PluginEvent &event) override; 97 void SetSelectBitRateFlag(bool flag, uint32_t desBitRate) override; 98 bool CanAutoSelectBitRate() override; 99 100 bool IsSeekToTimeSupported(); 101 int64_t GetDuration(); 102 Status SeekToTime(int64_t seekTime, SeekMode mode); 103 Status GetBitRates(std::vector<uint32_t>& bitRates); 104 Status SelectBitRate(uint32_t bitRate); 105 Status StopBufferring(bool flag); 106 Status SetStartPts(int64_t startPts); 107 Status SetExtraCache(uint64_t cacheDuration); 108 Status SetCurrentBitRate(int32_t bitRate, int32_t streamID); 109 void SetCallback(Callback* callback); 110 bool IsNeedPreDownload(); 111 void SetDemuxerState(int32_t streamId); 112 Status GetStreamInfo(std::vector<StreamInfo>& streams); 113 Status Read(int32_t streamID, std::shared_ptr<Buffer>& buffer, uint64_t offset, size_t expectedLen); 114 Status SeekTo(uint64_t offset); 115 void SetInterruptState(bool isInterruptNeeded); 116 Status GetDownloadInfo(DownloadInfo& downloadInfo); 117 Status GetPlaybackInfo(PlaybackInfo& playbackInfo); 118 Status SelectStream(int32_t streamID); 119 void SetEnableOnlineFdCache(bool isEnableFdCache); 120 size_t GetSegmentOffset(); 121 bool GetHLSDiscontinuity(); 122 void WaitForBufferingEnd(); 123 bool SetInitialBufferSize(int32_t offset, int32_t size); 124 Status SetPerfRecEnabled(bool perfRecEnabled); 125 void NotifyInitSuccess(); 126 bool IsLocalFd(); 127 bool IsFlvLiveStream() const; 128 uint64_t GetCachedDuration(); 129 void RestartAndClearBuffer(); 130 bool IsFlvLive(); 131 bool IsHlsFmp4(); 132 133 private: 134 Status InitPlugin(const std::shared_ptr<MediaSource>& source); 135 static std::string GetUriSuffix(const std::string& uri); 136 bool GetProtocolByUri(); 137 bool ParseProtocol(const std::shared_ptr<MediaSource>& source); 138 Status FindPlugin(const std::shared_ptr<MediaSource>& source); 139 void ClearData(); 140 Status ReadWithPerfRecord(int32_t streamID, std::shared_ptr<Buffer>& buffer, uint64_t offset, size_t expectedLen); 141 142 std::string protocol_; 143 bool seekToTimeFlag_{false}; 144 std::string uri_; 145 Plugins::Seekable seekable_; 146 147 std::shared_ptr<Plugins::SourcePlugin> plugin_; 148 149 std::shared_ptr<Plugins::PluginInfo> pluginInfo_{}; 150 bool isPluginReady_ {false}; 151 bool isAboveWaterline_ {false}; 152 153 std::shared_ptr<CallbackImpl> mediaDemuxerCallback_; 154 std::atomic<bool> isInterruptNeeded_{false}; 155 std::mutex mutex_; 156 std::condition_variable seekCond_; 157 bool isEnableFdCache_{ true }; 158 bool perfRecEnabled_ { false }; 159 PerfRecorder perfRecorder_ {}; 160 bool isFlvLiveStream_ {false}; 161 }; 162 } // namespace Media 163 } // namespace OHOS 164 #endif