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 LOG_TAG
17 #define LOG_TAG "RingBufferHandler"
18 #endif
19
20 #include "util/ring_buffer_handler.h"
21 #include <cstdint>
22 #include "securec.h"
23 #include "audio_hdi_log.h"
24 #include "audio_errors.h"
25
26 namespace OHOS {
27 namespace AudioStandard {
~RingBufferHandler()28 RingBufferHandler::~RingBufferHandler()
29 {
30 if (buffer_ != nullptr) {
31 delete[] buffer_;
32 buffer_ = nullptr;
33 }
34 }
35
Init(const uint32_t sampleRate,const uint32_t channelCount,const uint32_t formatBytes,const uint32_t onceFrameNum,const uint32_t maxFrameNum)36 void RingBufferHandler::Init(const uint32_t sampleRate, const uint32_t channelCount, const uint32_t formatBytes,
37 const uint32_t onceFrameNum, const uint32_t maxFrameNum)
38 {
39 std::lock_guard<std::mutex> lock(mutex_);
40 perFrameLength_ = ((sampleRate * onceFrameNum) / PER_FRAME_LENGTH_RATE) * channelCount * formatBytes;
41 maxBufferSize_ = perFrameLength_ * maxFrameNum;
42 CHECK_AND_RETURN_LOG(maxBufferSize_ > 0 && maxBufferSize_ <= UINT32_MAX, "invalid param, maxBufferSize: %{public}u",
43 maxBufferSize_);
44 maxFrameNum_ = maxFrameNum;
45
46 buffer_ = new uint8_t[maxBufferSize_];
47 CHECK_AND_RETURN_LOG(buffer_ != nullptr, "alloc buffer fail");
48 memset_s(static_cast<void *>(buffer_), maxBufferSize_, 0, maxBufferSize_);
49 }
50
WriteDataToRingBuffer(uint8_t * data,uint32_t dataLen)51 int32_t RingBufferHandler::WriteDataToRingBuffer(uint8_t *data, uint32_t dataLen)
52 {
53 CHECK_AND_RETURN_RET_LOG(data != nullptr, ERR_INVALID_PARAM, "data is nullptr");
54 CHECK_AND_RETURN_RET_LOG(dataLen == perFrameLength_, ERR_INVALID_PARAM, "dataLen not equal perFrameLength");
55
56 std::lock_guard<std::mutex> lock(mutex_);
57 uint32_t offset = (writeIdx_ % maxFrameNum_) * perFrameLength_;
58 CHECK_AND_RETURN_RET_LOG(buffer_ != nullptr, ERR_INVALID_PARAM, "buffer is nullptr");
59 auto ret = memcpy_s(buffer_ + offset, maxBufferSize_ - offset, data, dataLen);
60 CHECK_AND_RETURN_RET_LOG(ret == EOK, ERR_WRITE_BUFFER, "write ring buffer fail");
61 AddWriteIndex();
62
63 return SUCCESS;
64 }
65
ReadDataFromRingBuffer(uint8_t * data,uint32_t dataLen)66 int32_t RingBufferHandler::ReadDataFromRingBuffer(uint8_t *data, uint32_t dataLen)
67 {
68 CHECK_AND_RETURN_RET_LOG(data != nullptr, ERR_INVALID_PARAM, "data is nullptr");
69 CHECK_AND_RETURN_RET_LOG(dataLen == perFrameLength_, ERR_INVALID_PARAM, "dataLen not equal perFrameLength");
70
71 std::lock_guard<std::mutex> lock(mutex_);
72 if (readIdx_ >= writeIdx_) {
73 static_cast<void>(memset_s(data, dataLen, 0, dataLen));
74 return SUCCESS;
75 }
76 uint32_t offset = (readIdx_ % maxFrameNum_) * perFrameLength_;
77 CHECK_AND_RETURN_RET_LOG(buffer_ != nullptr, ERR_INVALID_PARAM, "buffer is nullptr");
78 auto ret = memcpy_s(data, dataLen, buffer_ + offset, perFrameLength_);
79 CHECK_AND_RETURN_RET_LOG(ret == EOK, ERR_READ_BUFFER, "read ring buffer fail");
80 AddReadIndex();
81
82 return SUCCESS;
83 }
84
AddWriteIndex(void)85 void RingBufferHandler::AddWriteIndex(void)
86 {
87 if (writeIdx_ < UINT64_MAX) {
88 ++writeIdx_;
89 return;
90 }
91
92 uint64_t diff = (writeIdx_ - readIdx_) % maxFrameNum_;
93 writeIdx_ = writeIdx_ % maxFrameNum_;
94 readIdx_ = writeIdx_ - diff;
95 ++writeIdx_;
96 }
97
AddReadIndex(void)98 void RingBufferHandler::AddReadIndex(void)
99 {
100 if (readIdx_ < UINT64_MAX) {
101 ++readIdx_;
102 return;
103 }
104
105 readIdx_ = 0;
106 }
107
108 } // namespace AudioStandard
109 } // namespace OHOS
110