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