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