Home | History | Annotate | Download | only in audio_utils
      1 /*
      2  * Copyright (C) 2015 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 #ifndef ANDROID_AUDIO_FIFO_H
     18 #define ANDROID_AUDIO_FIFO_H
     19 
     20 #include <stdlib.h>
     21 
     22 // FIXME use atomic_int_least32_t and new atomic operations instead of legacy Android ones
     23 // #include <stdatomic.h>
     24 
     25 #ifdef __cplusplus
     26 extern "C" {
     27 #endif
     28 
     29 // Single writer, single reader non-blocking FIFO.
     30 // Writer and reader must be in same process.
     31 
     32 // No user-serviceable parts within.
     33 struct audio_utils_fifo {
     34     // These fields are const after initialization
     35     size_t     mFrameCount;   // max number of significant frames to be stored in the FIFO > 0
     36     size_t     mFrameCountP2; // roundup(mFrameCount)
     37     size_t     mFudgeFactor;  // mFrameCountP2 - mFrameCount, the number of "wasted" frames after
     38                               // the end of mBuffer.  Only the indices are wasted, not any memory.
     39     size_t     mFrameSize;    // size of each frame in bytes
     40     void      *mBuffer;       // pointer to caller-allocated buffer of size mFrameCount frames
     41 
     42     volatile int32_t mFront; // frame index of first frame slot available to read, or read index
     43     volatile int32_t mRear;  // frame index of next frame slot available to write, or write index
     44 };
     45 
     46 // Initialize a FIFO object.
     47 // Input parameters:
     48 //  fifo        Pointer to the FIFO object.
     49 //  frameCount  Max number of significant frames to be stored in the FIFO > 0.
     50 //              If writes and reads always use the same count, and that count is a divisor of
     51 //              frameCount, then the writes and reads will never do a partial transfer.
     52 //  frameSize   Size of each frame in bytes.
     53 //  buffer      Pointer to a caller-allocated buffer of frameCount frames.
     54 void audio_utils_fifo_init(struct audio_utils_fifo *fifo, size_t frameCount, size_t frameSize,
     55         void *buffer);
     56 
     57 // De-initialize a FIFO object.
     58 // Input parameters:
     59 //  fifo        Pointer to the FIFO object.
     60 void audio_utils_fifo_deinit(struct audio_utils_fifo *fifo);
     61 
     62 // Write to FIFO.
     63 // Input parameters:
     64 //  fifo        Pointer to the FIFO object.
     65 //  buffer      Pointer to source buffer containing 'count' frames of data.
     66 // Returns actual number of frames written <= count.
     67 // The actual transfer count may be zero if the FIFO is full,
     68 // or partial if the FIFO was almost full.
     69 // A negative return value indicates an error.  Currently there are no errors defined.
     70 ssize_t audio_utils_fifo_write(struct audio_utils_fifo *fifo, const void *buffer, size_t count);
     71 
     72 // Read from FIFO.
     73 // Input parameters:
     74 //  fifo        Pointer to the FIFO object.
     75 //  buffer      Pointer to destination buffer to be filled with up to 'count' frames of data.
     76 // Returns actual number of frames read <= count.
     77 // The actual transfer count may be zero if the FIFO is empty,
     78 // or partial if the FIFO was almost empty.
     79 // A negative return value indicates an error.  Currently there are no errors defined.
     80 ssize_t audio_utils_fifo_read(struct audio_utils_fifo *fifo, void *buffer, size_t count);
     81 
     82 #ifdef __cplusplus
     83 }
     84 #endif
     85 
     86 #endif  // !ANDROID_AUDIO_FIFO_H
     87