• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "filter.h"
25 #include "error_code.h"
26 #include "utils/constants.h"
27 #include "event.h"
28 #include "filter_type.h"
29 #include "plugin/core/plugin_info.h"
30 #include "plugin/core/base.h"
31 #include "port.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 protected:
97     virtual void InitPorts();
98 
99     ErrorCode ConfigPluginWithMeta(Plugin::Base& plugin, const Plugin::Meta& meta);
100 
101     std::string NamePort(const std::string& mime);
102 
103     /**
104      * 获取routemap中 outPortName对应的inport
105      *
106      * @param outPortName outport name
107      * @return null if not exists
108      */
109     PInPort GetRouteInPort(const std::string& outPortName);
110 
111     /**
112      * 获取routemap中 inPortName对应的outport
113      *
114      * @param inPortName inport name
115      * @return null if not exists
116      */
117     POutPort GetRouteOutPort(const std::string& inPortName);
118 
119     template<typename T>
120     static bool UpdateAndInitPluginByInfo(std::shared_ptr<T>& plugin, std::shared_ptr<Plugin::PluginInfo>& pluginInfo,
121         const std::shared_ptr<Plugin::PluginInfo>& selectedPluginInfo,
122         const std::function<std::shared_ptr<T>(const std::string&)>& pluginCreator);
123 
124 protected:
125     void OnEvent(const Plugin::PluginEvent &event) override;
126     std::string name_;
127     std::atomic<FilterState> state_;
128     EventReceiver* eventReceiver_;
129     FilterCallback* callback_;
130     std::vector<PFilter> children_ {};
131     std::vector<PInPort> inPorts_ {};
132     std::vector<POutPort> outPorts_ {};
133     std::vector<PairPort> routeMap_ {}; // inport -> outport
134 
135     std::map<std::string, uint32_t> mediaTypeCntMap_ {};
136 
137     FilterType filterType_ {FilterType::NONE};
138 
139     std::shared_ptr<Plugin::PluginInfo> pluginInfo_{};
140 
141     std::weak_ptr<IMediaSyncCenter> syncCenter_ {};
142 private:
143     template <typename T>
144     static T FindPort(const std::vector<T>& list, const std::string& name);
145 };
146 } // namespace Pipeline
147 } // namespace Media
148 } // namespace OHOS
149 #endif
150