2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ANDROID_AUDIO_FIFO_H
18 #define ANDROID_AUDIO_FIFO_H
22 // FIXME use atomic_int_least32_t and new atomic operations instead of legacy Android ones
23 // #include <stdatomic.h>
29 // Single writer, single reader non-blocking FIFO.
30 // Writer and reader must be in same process.
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
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
47 * Initialize a FIFO object.
49 * \param fifo Pointer to the FIFO object.
50 * \param frameCount Max number of significant frames to be stored in the FIFO > 0.
51 * If writes and reads always use the same count, and that count is a divisor of
52 * frameCount, then the writes and reads will never do a partial transfer.
53 * \param frameSize Size of each frame in bytes.
54 * \param buffer Pointer to a caller-allocated buffer of frameCount frames.
56 void audio_utils_fifo_init(struct audio_utils_fifo *fifo, size_t frameCount, size_t frameSize,
60 * De-initialize a FIFO object.
62 * \param fifo Pointer to the FIFO object.
64 void audio_utils_fifo_deinit(struct audio_utils_fifo *fifo);
69 * \param fifo Pointer to the FIFO object.
70 * \param buffer Pointer to source buffer containing 'count' frames of data.
71 * \param count Desired number of frames to write.
73 * \return actual number of frames written <= count.
75 * The actual transfer count may be zero if the FIFO is full,
76 * or partial if the FIFO was almost full.
77 * A negative return value indicates an error. Currently there are no errors defined.
79 ssize_t audio_utils_fifo_write(struct audio_utils_fifo *fifo, const void *buffer, size_t count);
83 * \param fifo Pointer to the FIFO object.
84 * \param buffer Pointer to destination buffer to be filled with up to 'count' frames of data.
85 * \param count Desired number of frames to read.
87 * \return actual number of frames read <= count.
89 * The actual transfer count may be zero if the FIFO is empty,
90 * or partial if the FIFO was almost empty.
91 * A negative return value indicates an error. Currently there are no errors defined.
93 ssize_t audio_utils_fifo_read(struct audio_utils_fifo *fifo, void *buffer, size_t count);
99 #endif // !ANDROID_AUDIO_FIFO_H