1 /* 2 * Copyright (c) 2023-2023 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_DEMUXER_H 17 #define MEDIA_DEMUXER_H 18 19 #include <atomic> 20 #include <limits> 21 #include <string> 22 #include <shared_mutex> 23 24 #include "avcodec_common.h" 25 #include "buffer/avbuffer.h" 26 #include "common/media_source.h" 27 #include "demuxer/data_packer.h" 28 #include "demuxer/type_finder.h" 29 #include "filter/filter.h" 30 #include "meta/media_types.h" 31 #include "osal/task/autolock.h" 32 #include "osal/task/task.h" 33 #include "plugin/plugin_base.h" 34 #include "plugin/plugin_info.h" 35 #include "plugin/plugin_time.h" 36 #include "plugin/demuxer_plugin.h" 37 38 namespace OHOS { 39 namespace Media { 40 namespace { 41 constexpr uint32_t TRACK_ID_DUMMY = std::numeric_limits<uint32_t>::max(); 42 } 43 44 enum class DemuxerState { 45 DEMUXER_STATE_NULL, 46 DEMUXER_STATE_PARSE_HEADER, 47 DEMUXER_STATE_PARSE_FIRST_FRAME, 48 DEMUXER_STATE_PARSE_FRAME 49 }; 50 51 using MediaSource = OHOS::Media::Plugins::MediaSource; 52 class DataPacker; 53 class TypeFinder; 54 class Source; 55 56 class MediaDemuxer; 57 class AVBufferQueueProducer; 58 class PushDataImpl { 59 public: 60 explicit PushDataImpl(std::shared_ptr<MediaDemuxer> demuxer); 61 ~PushDataImpl() = default; 62 Status PushData(std::shared_ptr<Buffer>& buffer, int64_t offset); 63 private: 64 std::weak_ptr<MediaDemuxer> demuxer_; 65 }; 66 67 class MediaDemuxer : public std::enable_shared_from_this<MediaDemuxer>, public Plugins::Callback { 68 public: 69 explicit MediaDemuxer(); 70 ~MediaDemuxer() override; 71 72 Status SetDataSource(const std::shared_ptr<MediaSource> &source); 73 Status SetOutputBufferQueue(int32_t trackId, const sptr<AVBufferQueueProducer>& producer); 74 75 std::shared_ptr<Meta> GetGlobalMetaInfo() const; 76 std::vector<std::shared_ptr<Meta>> GetStreamMetaInfo() const; 77 78 Status SeekTo(int64_t seekTime, Plugins::SeekMode mode, int64_t& realSeekTime); 79 Status Reset(); 80 Status Start(); 81 Status Stop(); 82 Status Pause(); 83 Status Resume(); 84 Status Flush(); 85 86 Status SelectTrack(int32_t trackId); 87 Status UnselectTrack(int32_t trackId); 88 Status ReadSample(uint32_t trackId, std::shared_ptr<AVBuffer> sample); 89 Status GetBitRates(std::vector<uint32_t> &bitRates); 90 Status SelectBitRate(uint32_t bitRate); 91 92 Status GetMediaKeySystemInfo(std::multimap<std::string, std::vector<uint8_t>> &infos); 93 void SetDrmCallback(const std::shared_ptr<OHOS::MediaAVCodec::AVDemuxerCallback> &callback); 94 void SetDemuxerState(DemuxerState state); 95 void OnEvent(const Plugins::PluginEvent &event) override; 96 97 void PushData(std::shared_ptr<Buffer>& bufferPtr, uint64_t offset); 98 void SetEos(); 99 100 void SetEventReceiver(const std::shared_ptr<Pipeline::EventReceiver> &receiver); 101 bool GetDuration(int64_t& durationMs); 102 private: 103 class DataSourceImpl; 104 105 struct MediaMetaData { 106 std::vector<std::shared_ptr<Meta>> trackMetas; 107 std::shared_ptr<Meta> globalMeta; 108 }; 109 110 bool isHttpSource_ = false; 111 std::string videoMime_{}; 112 bool IsContainIdrFrame(const uint8_t* buff, size_t bufSize); 113 114 void InitTypeFinder(); 115 bool CreatePlugin(std::string pluginName); 116 bool InitPlugin(std::string pluginName); 117 118 void ActivatePullMode(); 119 void ActivatePushMode(); 120 121 void ReportIsLiveStreamEvent(); 122 void MediaTypeFound(std::string pluginName); 123 void InitMediaMetaData(const Plugins::MediaInfo& mediaInfo); 124 bool IsOffsetValid(int64_t offset) const; 125 std::shared_ptr<Meta> GetTrackMeta(uint32_t trackId); 126 void HandleFrame(const AVBuffer& bufferPtr, uint32_t trackId); 127 128 Status StopTask(uint32_t trackId); 129 Status StopAllTask(); 130 Status PauseAllTask(); 131 Status ResumeAllTask(); 132 133 bool IsDrmInfosUpdate(const std::multimap<std::string, std::vector<uint8_t>> &info); 134 Status ProcessDrmInfos(); 135 void HandleSourceDrmInfoEvent(const std::multimap<std::string, std::vector<uint8_t>> &info); 136 bool IsLocalDrmInfosExisted(); 137 Status ReportDrmInfos(const std::multimap<std::string, std::vector<uint8_t>> &info); 138 139 Plugins::Seekable seekable_; 140 std::string uri_; 141 uint64_t mediaDataSize_; 142 std::shared_ptr<TypeFinder> typeFinder_; 143 std::shared_ptr<DataPacker> dataPacker_ = nullptr; 144 145 std::string pluginName_; 146 std::shared_ptr<Plugins::DemuxerPlugin> plugin_; 147 std::atomic<DemuxerState> pluginState_{DemuxerState::DEMUXER_STATE_NULL}; 148 std::shared_ptr<DataSourceImpl> dataSource_; 149 std::shared_ptr<MediaSource> mediaSource_; 150 std::shared_ptr<Source> source_; 151 MediaMetaData mediaMetaData_; 152 153 std::function<bool(uint64_t, size_t)> checkRange_; 154 std::function<bool(uint64_t, size_t, std::shared_ptr<Buffer>&)> peekRange_; 155 std::function<bool(uint64_t, size_t, std::shared_ptr<Buffer>&)> getRange_; 156 157 bool PullDataWithCache(uint64_t offset, size_t size, std::shared_ptr<Buffer>& bufferPtr); 158 bool PullDataWithoutCache(uint64_t offset, size_t size, std::shared_ptr<Buffer>& bufferPtr); 159 void ReadLoop(uint32_t trackId); 160 Status CopyFrameToUserQueue(uint32_t trackId); 161 bool GetBufferFromUserQueue(uint32_t queueIndex, uint32_t size = 0); 162 Status InnerReadSample(uint32_t trackId, std::shared_ptr<AVBuffer>); 163 Status InnerSelectTrack(int32_t trackId); 164 165 Mutex mapMetaMutex_{}; 166 std::map<uint32_t, sptr<AVBufferQueueProducer>> bufferQueueMap_; 167 std::map<uint32_t, std::shared_ptr<AVBuffer>> bufferMap_; 168 std::map<uint32_t, bool> eosMap_; 169 std::atomic<bool> isThreadExit_ = true; 170 bool useBufferQueue_ = false; 171 172 std::shared_mutex drmMutex{}; 173 std::multimap<std::string, std::vector<uint8_t>> localDrmInfos_; 174 std::shared_ptr<OHOS::MediaAVCodec::AVDemuxerCallback> drmCallback_; 175 176 std::map<uint32_t, std::unique_ptr<Task>> taskMap_; 177 std::shared_ptr<Pipeline::EventReceiver> eventReceiver_; 178 int64_t lastSeekTime_{Plugins::HST_TIME_NONE}; 179 struct CacheData { 180 std::shared_ptr<Buffer> data = nullptr; 181 uint64_t offset = 0; 182 }; 183 184 CacheData cacheData_; 185 bool isSeeked_{false}; 186 uint32_t videoTrackId_{TRACK_ID_DUMMY}; 187 uint32_t audioTrackId_{TRACK_ID_DUMMY}; 188 bool firstAudio_{true}; 189 std::atomic<bool> isIgnoreParse_{false}; 190 }; 191 } // namespace Media 192 } // namespace OHOS 193 #endif // MEDIA_DEMUXER_H