• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 private:
52     void initBuffers();
53     std::shared_ptr<AudioBufferInfo> createNewBuffer();
54 
55 private:
56     std::atomic<bool> isRunning_;
57     std::mutex availableMutex_;
58     std::condition_variable availableCondition_;
59     std::queue<uint32_t> inBufIndexQue_;
60     std::vector<bool> inBufIndexExist;
61     mutable std::mutex stateMutex_;
62     const uint16_t bufferCount_;
63     uint32_t bufferSize_;
64     uint32_t metaSize_;
65     std::string_view name_;
66     std::vector<std::shared_ptr<AudioBufferInfo>> bufferInfo_;
67 };
68 } // namespace MediaAVCodec
69 } // namespace OHOS
70 
71 #endif