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_BASE_H 17 #define HISTREAMER_PIPELINE_CORE_FILTER_BASE_H 18 #include <atomic> 19 #include <functional> 20 #include <list> 21 #include <memory> 22 23 #include "foundation/pre_defines.h" 24 #include "foundation/utils/constants.h" 25 #include "pipeline/core/error_code.h" 26 #include "pipeline/core/event.h" 27 #include "pipeline/core/filter.h" 28 #include "pipeline/core/filter_type.h" 29 #include "pipeline/core/port.h" 30 #include "plugin/core/plugin_info.h" 31 #include "plugin/core/base.h" 32 33 namespace OHOS { 34 namespace Media { 35 namespace Pipeline { 36 class FilterBase : public Filter, public Plugin::CallbackWrap { 37 public: 38 explicit FilterBase(std::string name); 39 ~FilterBase() override = default; 40 void Init(EventReceiver* receiver, FilterCallback* callback) override; 41 PInPort GetInPort(const std::string& name) override; 42 POutPort GetOutPort(const std::string& name) override; GetName()43 const std::string& GetName() override 44 { 45 return name_; 46 } GetOwnerPipeline()47 const EventReceiver* GetOwnerPipeline() const override 48 { 49 return eventReceiver_; 50 } 51 52 ErrorCode Prepare() override; 53 ErrorCode Start() override; 54 ErrorCode Pause() override; 55 ErrorCode Stop() override; Resume()56 ErrorCode Resume() override 57 { 58 return Start(); 59 } FlushStart()60 void FlushStart() override 61 { 62 } FlushEnd()63 void FlushEnd() override 64 { 65 } SetParameter(int32_t key,const Plugin::Any & value)66 ErrorCode SetParameter(int32_t key, const Plugin::Any& value) override 67 { 68 UNUSED_VARIABLE(key); 69 UNUSED_VARIABLE(value); 70 return ErrorCode::ERROR_UNIMPLEMENTED; 71 } GetParameter(int32_t key,Plugin::Any & value)72 ErrorCode GetParameter(int32_t key, Plugin::Any& value) override 73 { 74 UNUSED_VARIABLE(key); 75 UNUSED_VARIABLE(value); 76 return ErrorCode::ERROR_UNIMPLEMENTED; 77 } 78 79 void UnlinkPrevFilters() override; 80 81 std::vector<Filter*> GetNextFilters() override; 82 83 std::vector<Filter*> GetPreFilters() override; 84 85 ErrorCode PushData(const std::string& inPort, const AVBufferPtr& buffer, int64_t offset) override; 86 ErrorCode PullData(const std::string& outPort, uint64_t offset, size_t size, AVBufferPtr& data) override; GetWorkModes()87 std::vector<WorkMode> GetWorkModes() override 88 { 89 return {WorkMode::PUSH}; 90 } 91 92 // Port调用此方法向Filter报告事件 93 void OnEvent(const Event& event) override; 94 95 void SetSyncCenter(std::weak_ptr<IMediaSyncCenter> syncCenter) final; 96 97 template <typename T> 98 static T FindPort(const std::vector<T>& list, const std::string& name); 99 protected: 100 virtual void InitPorts(); 101 102 ErrorCode ConfigPluginWithMeta(Plugin::Base& plugin, const Plugin::Meta& meta); 103 104 std::string NamePort(const std::string& mime); 105 106 /** 107 * 获取routemap中 outPortName对应的inport 108 * 109 * @param outPortName outport name 110 * @return null if not exists 111 */ 112 PInPort GetRouteInPort(const std::string& outPortName); 113 114 /** 115 * 获取routemap中 inPortName对应的outport 116 * 117 * @param inPortName inport name 118 * @return null if not exists 119 */ 120 POutPort GetRouteOutPort(const std::string& inPortName); 121 122 template<typename T> 123 static bool UpdateAndInitPluginByInfo(std::shared_ptr<T>& plugin, std::shared_ptr<Plugin::PluginInfo>& pluginInfo, 124 const std::shared_ptr<Plugin::PluginInfo>& selectedPluginInfo, 125 const std::function<std::shared_ptr<T>(const std::string&)>& pluginCreator); 126 127 protected: 128 void OnEvent(const Plugin::PluginEvent &event) override; 129 std::string name_; 130 std::atomic<FilterState> state_; 131 EventReceiver* eventReceiver_; 132 FilterCallback* callback_; 133 std::vector<PFilter> children_ {}; 134 std::vector<PInPort> inPorts_ {}; 135 std::vector<POutPort> outPorts_ {}; 136 std::vector<PairPort> routeMap_ {}; // inport -> outport 137 138 std::map<std::string, uint32_t> mediaTypeCntMap_ {}; 139 140 FilterType filterType_ {FilterType::NONE}; 141 142 std::shared_ptr<Plugin::PluginInfo> pluginInfo_{}; 143 144 std::weak_ptr<IMediaSyncCenter> syncCenter_ {}; 145 }; 146 } // namespace Pipeline 147 } // namespace Media 148 } // namespace OHOS 149 #endif 150