blob: 7bc90794e0e21cb9e587f6a8f1cebe3fdd9edb6f [file] [log] [blame]
Amit Pundire6732bb2020-09-28 12:43:59 +05301/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "audio_utils_fifo_wrapper"
18// #define LOG_NDEBUG 0
19
20#include <stdint.h>
21#include <errno.h>
22#include <log/log.h>
23#include <audio_utils/fifo.h>
24#include "fifo_wrapper.h"
25
26struct audio_fifo_itfe {
27 audio_utils_fifo *p_fifo;
28 audio_utils_fifo_reader *p_fifo_reader;
29 audio_utils_fifo_writer *p_fifo_writer;
30 int8_t *p_buffer;
31};
32
33void *fifo_init(uint32_t bytes, bool reader_throttles_writer) {
34 struct audio_fifo_itfe *interface = new struct audio_fifo_itfe;
35 interface->p_buffer = new int8_t[bytes];
36 if (interface->p_buffer == NULL) {
37 ALOGE("Failed to allocate fifo buffer!");
38 return NULL;
39 }
40 interface->p_fifo = new audio_utils_fifo(bytes, 1, interface->p_buffer, reader_throttles_writer);
41 interface->p_fifo_writer = new audio_utils_fifo_writer(*interface->p_fifo);
42 interface->p_fifo_reader = new audio_utils_fifo_reader(*interface->p_fifo);
43
44 return (void *)interface;
45}
46
47void fifo_release(void *fifo_itfe) {
48 struct audio_fifo_itfe *interface = static_cast<struct audio_fifo_itfe *>(fifo_itfe);
49 delete interface->p_fifo_writer;
50 delete interface->p_fifo_reader;
51 delete interface->p_fifo;
52 delete[] interface->p_buffer;
53 delete interface;
54}
55
56ssize_t fifo_read(void *fifo_itfe, void *buffer, size_t bytes) {
57 struct audio_fifo_itfe *interface = static_cast<struct audio_fifo_itfe *>(fifo_itfe);
58 return interface->p_fifo_reader->read(buffer, bytes);
59}
60
61ssize_t fifo_write(void *fifo_itfe, void *buffer, size_t bytes) {
62 struct audio_fifo_itfe *interface = static_cast<struct audio_fifo_itfe *>(fifo_itfe);
63 return interface->p_fifo_writer->write(buffer, bytes);
64}
65
66ssize_t fifo_available_to_read(void *fifo_itfe) {
67 struct audio_fifo_itfe *interface = static_cast<struct audio_fifo_itfe *>(fifo_itfe);
68 return interface->p_fifo_reader->available();
69}
70
71ssize_t fifo_available_to_write(void *fifo_itfe) {
72 struct audio_fifo_itfe *interface = static_cast<struct audio_fifo_itfe *>(fifo_itfe);
73 return interface->p_fifo_writer->available();
74}
75
76ssize_t fifo_flush(void *fifo_itfe) {
77 struct audio_fifo_itfe *interface = static_cast<struct audio_fifo_itfe *>(fifo_itfe);
78 return interface->p_fifo_reader->flush();
79}