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