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 MEDIA_PIPELINE_DEMUXER_FILTER_H 17 #define MEDIA_PIPELINE_DEMUXER_FILTER_H 18 19 #include <atomic> 20 #include <string> 21 #include "core/filter_base.h" 22 #include "data_packer.h" 23 #include "osal/thread/mutex.h" 24 #include "osal/thread/task.h" 25 #include "plugin/common/plugin_types.h" 26 #include "plugin/core/demuxer.h" 27 #include "plugin/core/plugin_meta.h" 28 #include "type_finder.h" 29 #include "pipeline/core/type_define.h" 30 31 namespace OHOS { 32 namespace Media { 33 namespace Pipeline { 34 class DemuxerFilter : public FilterBase { 35 public: 36 explicit DemuxerFilter(std::string name); 37 38 ~DemuxerFilter() override; 39 40 void Init(EventReceiver* receiver, FilterCallback* callback) override; 41 42 ErrorCode Prepare() override; 43 44 ErrorCode Start() override; 45 46 ErrorCode Stop() override; 47 48 ErrorCode Pause() override; 49 50 void FlushStart() override; 51 52 void FlushEnd() override; 53 54 ErrorCode SetParameter(int32_t key, const Plugin::Any& value) override; 55 56 ErrorCode GetParameter(int32_t key, Plugin::Any& value) override; 57 58 /** 59 * 60 * @param inPort 61 * @param buffer 62 * @param offset always ignore this parameter 63 * @return 64 */ 65 ErrorCode PushData(const std::string& inPort, const AVBufferPtr& buffer, int64_t offset) override; 66 67 bool Negotiate(const std::string& inPort, 68 const std::shared_ptr<const Plugin::Capability>& upstreamCap, 69 Plugin::Capability& negotiatedCap, 70 const Plugin::TagMap& upstreamParams, 71 Plugin::TagMap& downstreamParams) override; 72 73 bool Configure(const std::string &inPort, const std::shared_ptr<const Plugin::Meta> &upstreamMeta, 74 Plugin::TagMap &upstreamParams, Plugin::TagMap &downstreamParams) override; 75 76 ErrorCode SeekTo(int64_t pos, Plugin::SeekMode mode); 77 78 std::vector<std::shared_ptr<Plugin::Meta>> GetStreamMetaInfo() const; 79 80 std::shared_ptr<Plugin::Meta> GetGlobalMetaInfo() const; 81 82 private: 83 class DataSourceImpl; 84 85 enum class DemuxerState { DEMUXER_STATE_NULL, DEMUXER_STATE_PARSE_HEADER, DEMUXER_STATE_PARSE_FRAME }; 86 87 struct StreamTrackInfo { 88 uint32_t trackId = 0; 89 std::shared_ptr<OutPort> port = nullptr; 90 bool needNegoCaps = false; 91 StreamTrackInfoStreamTrackInfo92 StreamTrackInfo(uint32_t trackId, std::shared_ptr<OutPort> port, bool needNegoCaps) 93 : trackId(trackId), port(std::move(port)), needNegoCaps(needNegoCaps) 94 { 95 } 96 }; 97 98 struct MediaMetaData { 99 std::vector<StreamTrackInfo> trackInfos; 100 std::vector<std::shared_ptr<Plugin::Meta>> trackMetas; 101 std::shared_ptr<Plugin::Meta> globalMeta; 102 }; 103 104 void Reset(); 105 106 void InitTypeFinder(); 107 108 bool CreatePlugin(std::string pluginName); 109 110 bool InitPlugin(std::string pluginName); 111 112 void ActivatePullMode(); 113 114 void ActivatePushMode(); 115 116 void MediaTypeFound(std::string pluginName); 117 118 void InitMediaMetaData(const Plugin::MediaInfoHelper& mediaInfo); 119 120 bool IsOffsetValid(int64_t offset) const; 121 122 bool PrepareStreams(const Plugin::MediaInfoHelper& mediaInfo); 123 124 ErrorCode ReadFrame(AVBuffer& buffer, uint32_t& trackId); 125 126 std::shared_ptr<Plugin::Meta> GetTrackMeta(uint32_t trackId); 127 128 void SendEventEos(); 129 130 void HandleFrame(const AVBufferPtr& bufferPtr, uint32_t trackId); 131 132 void NegotiateDownstream(); 133 134 void UpdateStreamMeta(std::shared_ptr<Plugin::Meta>& streamMeta, 135 Plugin::Capability& negotiatedCap, Plugin::TagMap& downstreamParams); 136 137 void DemuxerLoop(); 138 139 Plugin::Seekable seekable_; 140 std::string uriSuffix_; 141 uint64_t mediaDataSize_; 142 std::shared_ptr<OSAL::Task> task_; 143 std::shared_ptr<TypeFinder> typeFinder_; 144 std::shared_ptr<DataPacker> dataPacker_; 145 146 std::string pluginName_; 147 std::shared_ptr<Plugin::Demuxer> plugin_; 148 std::atomic<DemuxerState> pluginState_; 149 std::shared_ptr<Plugin::AllocatorHelper> pluginAllocator_; 150 std::shared_ptr<DataSourceImpl> dataSource_; 151 MediaMetaData mediaMetaData_; 152 153 std::function<bool(uint64_t, size_t)> checkRange_; 154 std::function<bool(uint64_t, size_t, AVBufferPtr&)> peekRange_; 155 std::function<bool(uint64_t, size_t, AVBufferPtr&)> getRange_; 156 }; 157 } // namespace Pipeline 158 } // namespace Media 159 } // namespace OHOS 160 161 #endif // MEDIA_PIPELINE_DEMUXER_FILTER_H 162