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