• 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_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 "error_code.h"
24 #include "utils/constants.h"
25 #include "i_media_sync_center.h"
26 #include "event.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,Plugin::TagMap & upstreamParams,Plugin::TagMap & downstreamParams)78     virtual bool Configure(const std::string &inPort, const std::shared_ptr<const Plugin::Meta> &upstreamMeta,
79                            Plugin::TagMap &upstreamParams, Plugin::TagMap &downstreamParams)
80     {
81         return false;
82     }
83 
84     /**
85      * push data to this filter
86      *
87      * @param inPort in port
88      * @param buffer in buffer
89      * @param offset means the offset from the start of the stream between Filter.Start and Filter.Stop. -1 means ignore
90      * this parameter.
91      * @return
92      */
93     virtual ErrorCode PushData(const std::string& inPort, const AVBufferPtr& buffer, int64_t offset) = 0; // InPort调用
94     virtual ErrorCode PullData(const std::string& outPort, uint64_t offset, size_t size,
95                                AVBufferPtr& data) = 0; // OutPort调用
96     virtual const EventReceiver* GetOwnerPipeline() const = 0;
97 };
98 
99 class Filter : public InfoTransfer {
100 public:
101     ~Filter() override = default;
102     virtual void Init(EventReceiver* receiver, FilterCallback* callback) = 0;
103     virtual PInPort GetInPort(const std::string& name) = 0;
104     virtual POutPort GetOutPort(const std::string& name) = 0;
105 
106     virtual ErrorCode Prepare() = 0;
107     virtual ErrorCode Start() = 0;
108     virtual ErrorCode Pause() = 0;
109     virtual ErrorCode Resume() = 0;
110     virtual ErrorCode Stop() = 0;
111 
112     // 清除缓存
113     virtual void FlushStart() = 0;
114     virtual void FlushEnd() = 0;
115 
116     // Filter的使用者可以直接调用Filter的SetParameter接口设置参数
117     virtual ErrorCode SetParameter(int32_t key, const Plugin::Any& value) = 0;
118     virtual ErrorCode GetParameter(int32_t key, Plugin::Any& value) = 0;
119 
120     virtual void UnlinkPrevFilters() = 0;
121     virtual std::vector<Filter*> GetNextFilters() = 0;
122     virtual std::vector<Filter*> GetPreFilters() = 0;
123     virtual void SetSyncCenter(std::weak_ptr<IMediaSyncCenter> mediaSyncCenter) = 0;
124 };
125 } // namespace Pipeline
126 } // namespace Media
127 } // namespace OHOS
128 #endif // HISTREAMER_PIPELINE_CORE_FILTER_H
129