• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 RING_BUFFER_HANDLER_H
17 #define RING_BUFFER_HANDLER_H
18 
19 #include <string>
20 #include <mutex>
21 #include "common/hdi_adapter_info.h"
22 
23 namespace OHOS {
24 namespace AudioStandard {
25 class RingBufferHandler {
26 public:
27     RingBufferHandler() = default;
28     ~RingBufferHandler();
29 
30     void Init(const uint32_t sampleRate, const uint32_t channelCount, const uint32_t formatBytes,
31         const uint32_t onceFrameNum = DEFAULT_ONCE_FRAME_NUM, const uint32_t maxFrameNum = DEFAULT_MAX_FRAME_NUM);
32     int32_t WriteDataToRingBuffer(uint8_t *data, uint32_t dataLen);
33     int32_t ReadDataFromRingBuffer(uint8_t *data, uint32_t dataLen);
34 
35 private:
36     void AddWriteIndex(void);
37     void AddReadIndex(void);
38 
39 private:
40     static constexpr int32_t DEFAULT_ONCE_FRAME_NUM = 2;
41     static constexpr int32_t DEFAULT_MAX_FRAME_NUM = 5;
42     static constexpr int32_t PER_FRAME_LENGTH_RATE = 100;
43 
44     uint8_t *buffer_ = nullptr;
45     uint32_t maxBufferSize_ = 0;
46     uint32_t perFrameLength_ = 0;
47     uint32_t maxFrameNum_ = 0;
48     uint64_t readIdx_ = 0;
49     uint64_t writeIdx_ = 0;
50     std::mutex mutex_;
51 };
52 
53 } // namespace AudioStandard
54 } // namespace OHOS
55 
56 #endif // RING_BUFFER_HANDLER_H
57