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 16 #ifndef HDI_UTILS_RINGBUFFER_H 17 #define HDI_UTILS_RINGBUFFER_H 18 19 #include <cstdint> 20 #include <mutex> 21 #include <string> 22 23 namespace OHOS { 24 namespace AudioStandard { 25 26 class HdiRingBuffer { 27 public: 28 HdiRingBuffer() = default; 29 ~HdiRingBuffer(); 30 31 void Init(const uint32_t sampleRate, const uint32_t channelCount, const uint32_t formatBytes, 32 const uint32_t onceFrameNum = 2, const uint32_t maxFrameNum = 5); 33 34 int32_t WriteDataToRingBuffer(uint8_t *data, uint32_t dataLen); 35 int32_t ReadDataFromRingBuffer(uint8_t *data, uint32_t dataLen); 36 37 private: 38 void AddWriteIndex(void); 39 void AddReadIndex(void); 40 uint8_t *buffer_ { nullptr }; 41 uint32_t maxBufferSize_ { 0 }; 42 uint32_t perFrameLength_ { 0 }; 43 uint32_t maxFrameNum_ { 0 }; 44 uint64_t readIdx_ { 0 }; 45 uint64_t writeIdx_ { 0 }; 46 std::mutex mtx_; 47 }; 48 49 } // namespace AudioStandard 50 } // namespace OHOS 51 #endif // HDI_UTILS_RINGBUFFER_H