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