• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 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 #include "meta/meta.h"
23 #include "buffer/avbuffer_queue_producer.h"
24 #include "common/event.h"
25 #include "common/interrupt_monitor.h"
26 #include "osal/task/mutex.h"
27 #include "osal/task/condition_variable.h"
28 #include "osal/task/task.h"
29 
30 namespace OHOS {
31 namespace Media {
32 namespace Pipeline {
33 
34 class Filter;
35 
36 enum class FilterType {
37     FILTERTYPE_SOURCE,
38     FILTERTYPE_DEMUXER,
39     FILTERTYPE_AENC,
40     FILTERTYPE_ADEC,
41     FILTERTYPE_VENC,
42     FILTERTYPE_VDEC,
43     FILTERTYPE_MUXER,
44     FILTERTYPE_VIDEODEC,
45     FILTERTYPE_ASINK,
46     FILTERTYPE_FSINK,
47     FILTERTYPE_SSINK,
48     AUDIO_CAPTURE,
49     AUDIO_DATA_SOURCE,
50     VIDEO_CAPTURE,
51     FILTERTYPE_VIDRESIZE,
52     FILTERTYPE_SEI,
53     TIMED_METADATA,
54     FILTERTYPE_MAX,
55 };
56 
57 enum class StreamType {
58     STREAMTYPE_PACKED,
59     STREAMTYPE_ENCODED_AUDIO,
60     STREAMTYPE_ENCODED_VIDEO,
61     STREAMTYPE_RAW_AUDIO,
62     STREAMTYPE_RAW_VIDEO,
63     STREAMTYPE_SUBTITLE,
64     STREAMTYPE_MAX,
65 };
66 
67 enum class FilterState {
68     CREATED,     // Filter created
69     INITIALIZED, // Init called
70     PREPARING,   // Prepare called
71     READY,       // Ready Event reported
72     RUNNING,     // Start called
73     PAUSED,      // Pause called
74     FROZEN,      // Freeze called
75     STOPPED,     // Stop called
76     RELEASED,    // Release called
77     ERROR,       // State fail
78 };
79 
80 enum class FilterCallBackCommand {
81     NEXT_FILTER_NEEDED,
82     NEXT_FILTER_REMOVED,
83     NEXT_FILTER_UPDATE,
84     FILTER_CALLBACK_COMMAND_MAX,
85 };
86 
87 enum class BufferQueueBufferAVailable : int {
88     BUFFER_AVAILABLE_IN_PORT = 0,
89     BUFFER_AVAILABLE_OUT_PORT = 1,
90 };
91 
92 class EventReceiver {
93 public:
94     virtual ~EventReceiver() = default;
95     virtual void OnEvent(const Event& event) = 0;
OnDfxEvent(const DfxEvent & event)96     virtual void OnDfxEvent(const DfxEvent& event)
97     {
98         (void)event;
99     }
NotifyRelease()100     virtual void NotifyRelease() {}
OnMemoryUsageEvent(const DfxEvent & event)101     virtual void OnMemoryUsageEvent(const DfxEvent& event)
102     {
103         (void)event;
104     }
105 };
106 
107 class FilterCallback {
108 public:
109     virtual ~FilterCallback() = default;
110     virtual Status OnCallback(const std::shared_ptr<Filter>& filter, FilterCallBackCommand cmd, StreamType outType) = 0;
NotifyRelease()111     virtual void NotifyRelease() {}
112 };
113 
114 class FilterLinkCallback {
115 public:
116     virtual ~FilterLinkCallback() = default;
117     virtual void OnLinkedResult(const sptr<AVBufferQueueProducer>& queue, std::shared_ptr<Meta>& meta) = 0;
118     virtual void OnUnlinkedResult(std::shared_ptr<Meta>& meta) = 0;
119     virtual void OnUpdatedResult(std::shared_ptr<Meta>& meta) = 0;
120 };
121 
122 class Filter {
123 public:
124     explicit Filter(std::string name, FilterType type, bool asyncMode = false);
125 
126     virtual ~Filter();
127 
128     virtual void Init(const std::shared_ptr<EventReceiver>& receiver, const std::shared_ptr<FilterCallback>& callback);
129 
130     // override this func if you need to use InterruptMonitor instance in your class
131     virtual void Init(const std::shared_ptr<EventReceiver>& receiver, const std::shared_ptr<FilterCallback>& callback,
132                       const std::shared_ptr<InterruptMonitor>& monitor);
133 
134     virtual void LinkPipeLine(const std::string &groupId, bool needTurbo = false, bool needInit = true) final;
135 
136     virtual Status Prepare() final;
137 
138     virtual Status Start() final;
139 
140     virtual Status Pause() final;
141 
142     virtual Status PauseDragging() final;
143 
144     virtual Status PauseAudioAlign() final;
145 
146     virtual Status Resume() final;
147 
148     virtual Status ResumeDragging() final;
149 
150     virtual Status ResumeAudioAlign() final;
151 
152     virtual Status Stop() final;
153 
154     virtual Status Flush() final;
155 
156     virtual Status Release() final;
157 
158     virtual Status Freeze() final;
159 
160     virtual Status UnFreeze() final;
161 
162     virtual Status Preroll() final;
163 
164     virtual Status WaitPrerollDone(bool render) final;
165 
166     virtual void StartFilterTask() final;
167 
168     virtual void PauseFilterTask() final;
169 
170     virtual void StopFilterTask() final;
171 
172     virtual Status SetPlayRange(int64_t start, int64_t end) final;
173 
174     virtual Status ProcessInputBuffer(int sendArg = 0, int64_t delayUs = 0) final;
175 
176     virtual Status ProcessOutputBuffer(int sendArg = 0, int64_t delayUs = 0, bool byIdx = false, uint32_t idx = 0,
177         int64_t renderTime = -1) final;
178 
179     virtual Status WaitAllState(FilterState state) final;
180 
181     virtual Status SetPerfRecEnabled(bool isPerfRecEnabled) final;
182 
HandleFormatChange(std::shared_ptr<Meta> & meta)183     virtual Status HandleFormatChange(std::shared_ptr<Meta>& meta)
184     {
185         (void)meta;
186         return Status::OK;
187     }
188 
189     virtual Status DoSetPerfRecEnabled(bool isPerfRecEnabled);
190 
191     virtual Status DoInitAfterLink();
192 
193     virtual Status DoPrepare();
194 
195     virtual Status DoStart();
196 
197     virtual Status DoPause();
198 
199     virtual Status DoPauseDragging();
200 
201     virtual Status DoPauseAudioAlign();
202 
203     virtual Status DoResume();
204 
205     virtual Status DoResumeDragging();
206 
207     virtual Status DoResumeAudioAlign();
208 
209     virtual Status DoStop();
210 
211     virtual Status DoFlush();
212 
213     virtual Status DoRelease();
214 
215     virtual Status DoFreeze();
216 
217     virtual Status DoUnFreeze();
218 
219     virtual Status DoPreroll();
220 
221     virtual Status DoWaitPrerollDone(bool render);
222 
223     virtual Status DoSetPlayRange(int64_t start, int64_t end);
224 
225     virtual Status DoProcessInputBuffer(int recvArg, bool dropFrame);
226 
227     virtual Status DoProcessOutputBuffer(int recvArg, bool dropFrame, bool byIdx, uint32_t idx, int64_t renderTime);
228 
229     virtual void SetParameter(const std::shared_ptr<Meta>& meta);
230 
231     virtual void GetParameter(std::shared_ptr<Meta>& meta);
232 
233     virtual Status LinkNext(const std::shared_ptr<Filter>& nextFilter, StreamType outType);
234 
235     virtual Status UpdateNext(const std::shared_ptr<Filter>& nextFilter, StreamType outType);
236 
237     virtual Status UnLinkNext(const std::shared_ptr<Filter>& nextFilter, StreamType outType);
238 
239     FilterType GetFilterType();
240 
241     virtual Status OnLinked(StreamType inType, const std::shared_ptr<Meta>& meta,
242                             const std::shared_ptr<FilterLinkCallback>& callback);
243 
244     virtual Status OnUpdated(StreamType inType, const std::shared_ptr<Meta>& meta,
245                              const std::shared_ptr<FilterLinkCallback>& callback);
246 
247     virtual Status OnUnLinked(StreamType inType, const std::shared_ptr<FilterLinkCallback>& callback);
248 
249     virtual Status ClearAllNextFilters();
250 
SetMuted(bool isMuted)251     virtual Status SetMuted(bool isMuted)
252     {
253         (void)isMuted;
254         return Status::OK;
255     }
256 
257     virtual Status ReleaseOnMuted(bool isNeedRelease) final;
258 
259     virtual Status ReInitAndStart() final;
260 
261     virtual Status DoReleaseOnMuted(bool isNeedRelease);
262 
263     virtual Status DoReInitAndStart();
264 
265     // When the AudioDecoderFilter runs in non async mode, we should call this function to notify AudioSink
266     // to cache the decoded audio frames temporarily to accelerate the Pause/Stop procedure, especially for APE/FLAC.
267     // Otherwise, it will take long time about 1.5 seconds in average to perform Pause/Stop procedure,
268     // as that one encoded audio buffer may produce many decoded audio frames to be consumed.
269     // Call this function in Pause/Stop/Start/Resume procedure of DemuxerFilter.
270     virtual void SetIsInPrePausing(bool isInPrePausing) final;
271 
272 protected:
DoSetIsInPrePausing(bool isInPrePausing)273     virtual void DoSetIsInPrePausing(bool isInPrePausing)
274     {
275         (void)isInPrePausing;
276     }
277 
SetIsAsyncMode(bool isAsyncMode)278     inline void SetIsAsyncMode(bool isAsyncMode)
279     {
280         isAsyncMode_ = isAsyncMode;
281     }
282 
IsAsyncMode()283     inline bool IsAsyncMode() const
284     {
285         return isAsyncMode_;
286     }
287 
288     std::string name_;
289 
290     std::shared_ptr<Meta> meta_;
291 
292     FilterType filterType_;
293 
294     std::vector<StreamType> supportedInStreams_;
295     std::vector<StreamType> supportedOutStreams_;
296 
297     FilterState state_ = FilterState::CREATED;
298 
299     std::map<StreamType, std::vector<std::shared_ptr<Filter>>> nextFiltersMap_;
300 
301     std::shared_ptr<EventReceiver> receiver_;
302 
303     std::shared_ptr<FilterCallback> callback_;
304 
305     std::shared_ptr<InterruptMonitor> interruptMonitor_;
306 
307     std::map<StreamType, std::vector<std::shared_ptr<FilterLinkCallback>>> linkCallbackMaps_;
308 
309     std::string groupId_;
310 
311     bool isPerfRecEnabled_ { false };
312 
313 private:
314     void ChangeState(FilterState state);
315 
316     void SetErrCode(Status errCode);
317 
318     Status GetErrCode();
319 
320     Status PrepareDone();
321 
322     Status StartDone();
323 
324     Status PauseDone();
325 
326     Status ResumeDone();
327 
328     Status StopDone();
329 
330     Status ReleaseDone();
331 
332     OHOS::Media::Mutex stateMutex_{};
333 
334     OHOS::Media::ConditionVariable cond_{};
335 
336     FilterState curState_{FilterState::CREATED};
337 
338     Status errCode_ = Status::OK;
339 
340     OHOS::Media::Mutex generationMutex_{};
341 
342     int64_t jobIdx_ = 0;
343 
344     int64_t jobIdxBase_ = 0;
345 
346     std::unique_ptr<Task> filterTask_;
347 
348     bool isAsyncMode_;
349 };
350 } // namespace Pipeline
351 } // namespace Media
352 } // namespace OHOS
353 #endif
354