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_PIPELINE_CORE_FILTER_H 17 #define HISTREAMER_PIPELINE_CORE_FILTER_H 18 #include <functional> 19 #include <list> 20 #include <memory> 21 22 #include "filter_callback.h" 23 #include "foundation/error_code.h" 24 #include "utils/constants.h" 25 #include "event.h" 26 #include "utils/utils.h" 27 #include "parameter.h" 28 #include "port.h" 29 #include "plugin/core/plugin_meta.h" 30 31 namespace OHOS { 32 namespace Media { 33 namespace Pipeline { 34 class Filter; 35 36 using PFilter = std::shared_ptr<Filter>; 37 using PPort = std::shared_ptr<Port>; 38 using PInPort = std::shared_ptr<InPort>; 39 using POutPort = std::shared_ptr<OutPort>; 40 using PairPort = std::pair<std::string, std::string>; 41 42 enum class FilterState { 43 CREATED, // Filter created 44 INITIALIZED, // Init called 45 PREPARING, // Prepare called 46 READY, // Ready Event reported 47 RUNNING, // Start called 48 PAUSED, // Pause called 49 }; 50 51 // EventReceiver: 52 // 1. Port使用此接口传递事件给Filter 53 // 2. Filter使用此接口传递事件给Pipeline 54 // 3. Sub Filter使用此接口传递事件给 Parent Filter 55 class EventReceiver { 56 public: 57 virtual ~EventReceiver() = default; 58 virtual void OnEvent(const Event& event) = 0; 59 }; 60 61 // InfoTransfer: 62 // Port使用此接口从Filter获取信息 或 传递信息给Filter 63 class InfoTransfer : public EventReceiver { 64 public: 65 virtual const std::string& GetName() = 0; 66 virtual std::vector<WorkMode> GetWorkModes() = 0; // OutPort调用 67 68 // InPort调用此接口确定是否要继续往后协商 Negotiate(const std::string & inPort,const std::shared_ptr<const Plugin::Capability> & upstreamCap,Plugin::Capability & negotiatedCap,const Plugin::TagMap & upstreamParams,Plugin::TagMap & downstreamParams)69 virtual bool Negotiate(const std::string& inPort, 70 const std::shared_ptr<const Plugin::Capability>& upstreamCap, 71 Plugin::Capability& negotiatedCap, 72 const Plugin::TagMap& upstreamParams, 73 Plugin::TagMap& downstreamParams) 74 { 75 return false; 76 } 77 Configure(const std::string & inPort,const std::shared_ptr<const Plugin::Meta> & upstreamMeta)78 virtual bool Configure(const std::string& inPort, const std::shared_ptr<const Plugin::Meta>& upstreamMeta) 79 { 80 return false; 81 } 82 83 /** 84 * push data to this filter 85 * 86 * @param inPort in port 87 * @param buffer in buffer 88 * @param offset means the offset from the start of the stream between Filter.Start and Filter.Stop. -1 means ignore 89 * this parameter. 90 * @return 91 */ 92 virtual ErrorCode PushData(const std::string& inPort, AVBufferPtr buffer, int64_t offset) = 0; // InPort调用 93 virtual ErrorCode PullData(const std::string& outPort, uint64_t offset, size_t size, 94 AVBufferPtr& data) = 0; // OutPort调用 95 virtual const EventReceiver* GetOwnerPipeline() const = 0; 96 }; 97 98 class Filter : public InfoTransfer { 99 public: 100 ~Filter() override = default; 101 virtual void Init(EventReceiver* receiver, FilterCallback* callback) = 0; 102 virtual PInPort GetInPort(const std::string& name) = 0; 103 virtual POutPort GetOutPort(const std::string& name) = 0; 104 105 virtual ErrorCode Prepare() = 0; 106 virtual ErrorCode Start() = 0; 107 virtual ErrorCode Pause() = 0; 108 virtual ErrorCode Resume() = 0; 109 virtual ErrorCode Stop() = 0; 110 111 // 清除缓存 112 virtual void FlushStart() = 0; 113 virtual void FlushEnd() = 0; 114 115 // Filter的使用者可以直接调用Filter的SetParameter接口设置参数 116 virtual ErrorCode SetParameter(int32_t key, const Plugin::Any& value) = 0; 117 virtual ErrorCode GetParameter(int32_t key, Plugin::Any& value) = 0; 118 119 virtual void UnlinkPrevFilters() = 0; 120 virtual std::vector<Filter*> GetNextFilters() = 0; 121 virtual std::vector<Filter*> GetPreFilters() = 0; 122 }; 123 } // namespace Pipeline 124 } // namespace Media 125 } // namespace OHOS 126 #endif // HISTREAMER_PIPELINE_CORE_FILTER_H 127