1 /*
2  * Copyright (c) 2022-2022 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_FILTER_ASYNC_MODE_H
17 #define HISTREAMER_PIPELINE_FILTER_ASYNC_MODE_H
18 #include <atomic>
19 
20 #include "foundation/osal/thread/condition_variable.h"
21 #include "foundation/osal/thread/mutex.h"
22 #include "foundation/osal/thread/scoped_lock.h"
23 #include "foundation/osal/thread/task.h"
24 #include "foundation/utils/blocking_queue.h"
25 #include "pipeline/filters/codec/codec_mode.h"
26 
27 namespace OHOS {
28 namespace Media {
29 namespace Pipeline {
30 class AsyncMode : public CodecMode {
31 public:
32     explicit AsyncMode(std::string name);
33     ~AsyncMode() override;
34 
35     ErrorCode Configure() override;
36 
37     ErrorCode PushData(const std::string &inPort, const AVBufferPtr& buffer, int64_t offset) override;
38 
39     ErrorCode Stop() override;
40 
41     void FlushStart() override;
42 
43     void FlushEnd() override;
44 
45     void OnOutputBufferDone(const std::shared_ptr<Plugin::Buffer>& buffer) override;
46 
47     ErrorCode Prepare() override;
48 
49     ErrorCode Release() override;
50 
51 protected:
52     ErrorCode HandleFrame();
53 
54     ErrorCode DecodeFrame();
55 
56     ErrorCode FinishFrame();
57 
58     ErrorCode QueueAllBufferInPoolToPluginLocked();
59 
60     ErrorCode CheckBufferValidity(std::shared_ptr<AVBuffer>& buffer);
61 
62 private:
63     // dequeue from es bufferQ then enqueue to plugin
64     std::shared_ptr<OHOS::Media::OSAL::Task> handleFrameTask_ {};
65 
66     // this task will queue output buffer and decode
67     std::shared_ptr<OHOS::Media::OSAL::Task> decodeFrameTask_ {};
68 
69     // this task will dequeue from the plugin and then push to downstream
70     std::shared_ptr<OHOS::Media::OSAL::Task> pushTask_ {nullptr};
71 
72     std::shared_ptr<OHOS::Media::BlockingQueue<OHOS::Media::AVBufferPtr>> inBufQue_ {nullptr};
73     std::queue<AVBufferPtr> outBufQue_;  // PCM data
74     mutable OSAL::Mutex renderMutex_ {};
75     bool stopped_ {false};
76 
77     mutable OSAL::ConditionVariable cv_;
78     std::atomic<bool> isNeedQueueInputBuffer_;
79     mutable OSAL::Mutex mutex_;
80 };
81 } // namespace Pipeline
82 } // namespace Media
83 } // namespace OHOS
84 #endif // HISTREAMER_PIPELINE_FILTER_ASYNC_MODE_H