1 /*
2  * Copyright (C) 2023 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 AV_CODEC_ENGIN_BFFERS_H
17 #define AV_CODEC_ENGIN_BFFERS_H
18 
19 #include "audio_buffer_info.h"
20 #include "nocopyable.h"
21 
22 #include <atomic>
23 #include <condition_variable>
24 #include <mutex>
25 #include <queue>
26 #include <string_view>
27 
28 namespace OHOS {
29 namespace MediaAVCodec {
30 class AudioBuffersManager : public NoCopyable {
31 public:
32     AudioBuffersManager(const uint32_t bufferSize, const std::string_view &name, const uint16_t count,
33                         const uint32_t metaSize = 0);
34 
35     ~AudioBuffersManager();
36 
37     std::shared_ptr<AudioBufferInfo> getMemory(const uint32_t &index) const noexcept;
38 
39     bool ReleaseBuffer(const uint32_t &index);
40 
41     bool SetBufferBusy(const uint32_t &index);
42 
43     bool RequestNewBuffer(uint32_t &index, std::shared_ptr<AudioBufferInfo> &buffer);
44 
45     bool RequestAvailableIndex(uint32_t &index);
46 
47     void ReleaseAll();
48 
49     void SetRunning();
50 
51     void DisableRunning();
52 
53 private:
54     void initBuffers();
55     std::shared_ptr<AudioBufferInfo> createNewBuffer();
56 
57 private:
58     std::atomic<bool> isRunning_;
59     std::mutex availableMutex_;
60     std::condition_variable availableCondition_;
61     std::queue<uint32_t> inBufIndexQue_;
62     std::vector<bool> inBufIndexExist;
63     mutable std::mutex stateMutex_;
64     const uint16_t bufferCount_;
65     uint32_t bufferSize_;
66     uint32_t metaSize_;
67     std::string_view name_;
68     std::vector<std::shared_ptr<AudioBufferInfo>> bufferInfo_;
69 };
70 } // namespace MediaAVCodec
71 } // namespace OHOS
72 
73 #endif