1 /*
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 
26 struct 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 
fifo_init(uint32_t bytes,bool reader_throttles_writer)33 void *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 
fifo_release(void * fifo_itfe)47 void 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 
fifo_read(void * fifo_itfe,void * buffer,size_t bytes)56 ssize_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 
fifo_write(void * fifo_itfe,void * buffer,size_t bytes)61 ssize_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 
fifo_available_to_read(void * fifo_itfe)66 ssize_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 
fifo_available_to_write(void * fifo_itfe)71 ssize_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 
fifo_flush(void * fifo_itfe)76 ssize_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 }
80