blob: 212f8e8853a4abcbd91ba3afa208d7fcb22e5bf1 [file] [log] [blame]
Glenn Kasten01066232012-02-27 11:50:44 -08001/*
2 * Copyright (C) 2012 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_NBAIO_H
18#define ANDROID_AUDIO_NBAIO_H
19
20// Non-blocking audio I/O interface
21//
22// This header file has the abstract interfaces only. Concrete implementation classes are declared
23// elsewhere. Implementations _should_ be non-blocking for all methods, especially read() and
24// write(), but this is not enforced. In general, implementations do not need to be multi-thread
25// safe, and any exceptions are noted in the particular implementation.
26
27#include <limits.h>
28#include <stdlib.h>
John Grossman2c3b2da2012-08-02 17:08:54 -070029#include <utils/Errors.h>
Glenn Kasten01066232012-02-27 11:50:44 -080030#include <utils/RefBase.h>
Glenn Kasten767094d2013-08-23 13:51:43 -070031#include <media/AudioTimestamp.h>
Glenn Kastenf95a3c42014-03-06 07:59:49 -080032#include <system/audio.h>
Glenn Kasten01066232012-02-27 11:50:44 -080033
34namespace android {
35
36// In addition to the usual status_t
37enum {
38 NEGOTIATE = 0x80000010, // Must (re-)negotiate format. For negotiate() only, the offeree
39 // doesn't accept offers, and proposes counter-offers
40 OVERRUN = 0x80000011, // availableToRead(), read(), or readVia() detected lost input due
41 // to overrun; an event is counted and the caller should re-try
42 UNDERRUN = 0x80000012, // availableToWrite(), write(), or writeVia() detected a gap in
43 // output due to underrun (not being called often enough, or with
44 // enough data); an event is counted and the caller should re-try
45};
46
47// Negotiation of format is based on the data provider and data sink, or the data consumer and
48// data source, exchanging prioritized arrays of offers and counter-offers until a single offer is
49// mutually agreed upon. Each offer is an NBAIO_Format. For simplicity and performance,
Glenn Kastenb64497e2012-10-01 09:47:30 -070050// NBAIO_Format is a typedef that ties together the most important combinations of the various
Glenn Kasten01066232012-02-27 11:50:44 -080051// attributes, rather than a struct with separate fields for format, sample rate, channel count,
52// interleave, packing, alignment, etc. The reason is that NBAIO_Format tries to abstract out only
Glenn Kastenb64497e2012-10-01 09:47:30 -070053// the combinations that are actually needed within AudioFlinger. If the list of combinations grows
Glenn Kasten01066232012-02-27 11:50:44 -080054// too large, then this decision should be re-visited.
Glenn Kastenb64497e2012-10-01 09:47:30 -070055// Sample rate and channel count are explicit, PCM interleaved 16-bit is assumed.
Glenn Kastenc4b8b322014-01-31 09:39:01 -080056struct NBAIO_Format {
Glenn Kasten55e599d2014-03-06 10:54:55 -080057// FIXME make this a class, and change Format_... global methods to class methods
Glenn Kastenc4b8b322014-01-31 09:39:01 -080058//private:
Glenn Kasten2b7b9102014-03-06 08:02:51 -080059 unsigned mSampleRate;
60 unsigned mChannelCount;
61 audio_format_t mFormat;
Glenn Kasten55e599d2014-03-06 10:54:55 -080062 size_t mFrameSize;
Glenn Kastenc4b8b322014-01-31 09:39:01 -080063};
Glenn Kasten51d53cd2014-01-31 09:38:33 -080064
65extern const NBAIO_Format Format_Invalid;
Glenn Kasten01066232012-02-27 11:50:44 -080066
67// Return the frame size of an NBAIO_Format in bytes
Glenn Kasten72e54af2014-01-31 09:37:35 -080068size_t Format_frameSize(const NBAIO_Format& format);
Glenn Kasten01066232012-02-27 11:50:44 -080069
Glenn Kasten01066232012-02-27 11:50:44 -080070// Convert a sample rate in Hz and channel count to an NBAIO_Format
Glenn Kastenf69f9862014-03-07 08:37:57 -080071// FIXME rename
72NBAIO_Format Format_from_SR_C(unsigned sampleRate, unsigned channelCount, audio_format_t format);
Glenn Kasten01066232012-02-27 11:50:44 -080073
74// Return the sample rate in Hz of an NBAIO_Format
Glenn Kasten72e54af2014-01-31 09:37:35 -080075unsigned Format_sampleRate(const NBAIO_Format& format);
Glenn Kasten01066232012-02-27 11:50:44 -080076
77// Return the channel count of an NBAIO_Format
Glenn Kasten72e54af2014-01-31 09:37:35 -080078unsigned Format_channelCount(const NBAIO_Format& format);
Glenn Kasten01066232012-02-27 11:50:44 -080079
80// Callbacks used by NBAIO_Sink::writeVia() and NBAIO_Source::readVia() below.
81typedef ssize_t (*writeVia_t)(void *user, void *buffer, size_t count);
Glenn Kastend79072e2016-01-06 08:41:20 -080082typedef ssize_t (*readVia_t)(void *user, const void *buffer, size_t count);
Glenn Kasten01066232012-02-27 11:50:44 -080083
Glenn Kastencc1e0e82014-01-31 09:48:42 -080084// Check whether an NBAIO_Format is valid
85bool Format_isValid(const NBAIO_Format& format);
86
87// Compare two NBAIO_Format values
88bool Format_isEqual(const NBAIO_Format& format1, const NBAIO_Format& format2);
89
Glenn Kasten01066232012-02-27 11:50:44 -080090// Abstract class (interface) representing a data port.
91class NBAIO_Port : public RefBase {
92
93public:
94
95 // negotiate() must called first. The purpose of negotiate() is to check compatibility of
96 // formats, not to automatically adapt if they are incompatible. It's the responsibility of
97 // whoever sets up the graph connections to make sure formats are compatible, and this method
98 // just verifies that. The edges are "dumb" and don't attempt to adapt to bad connections.
99 // How it works: offerer proposes an array of formats, in descending order of preference from
100 // offers[0] to offers[numOffers - 1]. If offeree accepts one of these formats, it returns
101 // the index of that offer. Otherwise, offeree sets numCounterOffers to the number of
102 // counter-offers (up to a maximumum of the entry value of numCounterOffers), fills in the
103 // provided array counterOffers[] with its counter-offers, in descending order of preference
104 // from counterOffers[0] to counterOffers[numCounterOffers - 1], and returns NEGOTIATE.
105 // Note that since the offerer allocates space for counter-offers, but only the offeree knows
106 // how many counter-offers it has, there may be insufficient space for all counter-offers.
107 // In that case, the offeree sets numCounterOffers to the requested number of counter-offers
108 // (which is greater than the entry value of numCounterOffers), fills in as many of the most
109 // important counterOffers as will fit, and returns NEGOTIATE. As this implies a re-allocation,
110 // it should be used as a last resort. It is preferable for the offerer to simply allocate a
111 // larger space to begin with, and/or for the offeree to tolerate a smaller space than desired.
112 // Alternatively, the offerer can pass NULL for offers and counterOffers, and zero for
113 // numOffers. This indicates that it has not allocated space for any counter-offers yet.
114 // In this case, the offerree should set numCounterOffers appropriately and return NEGOTIATE.
115 // Then the offerer will allocate the correct amount of memory and retry.
116 // Format_Invalid is not allowed as either an offer or counter-offer.
117 // Returns:
118 // >= 0 Offer accepted.
119 // NEGOTIATE No offer accepted, and counter-offer(s) optionally made. See above for details.
120 virtual ssize_t negotiate(const NBAIO_Format offers[], size_t numOffers,
121 NBAIO_Format counterOffers[], size_t& numCounterOffers);
122
123 // Return the current negotiated format, or Format_Invalid if negotiation has not been done,
124 // or if re-negotiation is required.
125 virtual NBAIO_Format format() const { return mNegotiated ? mFormat : Format_Invalid; }
126
127protected:
Glenn Kasten72e54af2014-01-31 09:37:35 -0800128 NBAIO_Port(const NBAIO_Format& format) : mNegotiated(false), mFormat(format),
Glenn Kastenac3e9db2014-03-06 08:00:31 -0800129 mFrameSize(Format_frameSize(format)) { }
Glenn Kasten01066232012-02-27 11:50:44 -0800130 virtual ~NBAIO_Port() { }
131
132 // Implementations are free to ignore these if they don't need them
133
134 bool mNegotiated; // mNegotiated implies (mFormat != Format_Invalid)
135 NBAIO_Format mFormat; // (mFormat != Format_Invalid) does not imply mNegotiated
Glenn Kastenac3e9db2014-03-06 08:00:31 -0800136 size_t mFrameSize; // assign in parallel with any assignment to mFormat
Glenn Kasten01066232012-02-27 11:50:44 -0800137};
138
139// Abstract class (interface) representing a non-blocking data sink, for use by a data provider.
140class NBAIO_Sink : public NBAIO_Port {
141
142public:
143
144 // For the next two APIs:
145 // 32 bits rolls over after 27 hours at 44.1 kHz; if that concerns you then poll periodically.
146
147 // Return the number of frames written successfully since construction.
Andy Hung818e7a32016-02-16 18:08:07 -0800148 virtual int64_t framesWritten() const { return mFramesWritten; }
Glenn Kasten01066232012-02-27 11:50:44 -0800149
150 // Number of frames lost due to underrun since construction.
Andy Hung818e7a32016-02-16 18:08:07 -0800151 virtual int64_t framesUnderrun() const { return 0; }
Glenn Kasten01066232012-02-27 11:50:44 -0800152
153 // Number of underruns since construction, where a set of contiguous lost frames is one event.
Andy Hung818e7a32016-02-16 18:08:07 -0800154 virtual int64_t underruns() const { return 0; }
Glenn Kasten01066232012-02-27 11:50:44 -0800155
156 // Estimate of number of frames that could be written successfully now without blocking.
157 // When a write() is actually attempted, the implementation is permitted to return a smaller or
158 // larger transfer count, however it will make a good faith effort to give an accurate estimate.
159 // Errors:
160 // NEGOTIATE (Re-)negotiation is needed.
161 // UNDERRUN write() has not been called frequently enough, or with enough frames to keep up.
162 // An underrun event is counted, and the caller should re-try this operation.
163 // WOULD_BLOCK Determining how many frames can be written without blocking would itself block.
164 virtual ssize_t availableToWrite() const { return SSIZE_MAX; }
165
166 // Transfer data to sink from single input buffer. Implies a copy.
167 // Inputs:
168 // buffer Non-NULL buffer owned by provider.
169 // count Maximum number of frames to transfer.
170 // Return value:
171 // > 0 Number of frames successfully transferred prior to first error.
172 // = 0 Count was zero.
173 // < 0 status_t error occurred prior to the first frame transfer.
174 // Errors:
175 // NEGOTIATE (Re-)negotiation is needed.
176 // WOULD_BLOCK No frames can be transferred without blocking.
177 // UNDERRUN write() has not been called frequently enough, or with enough frames to keep up.
178 // An underrun event is counted, and the caller should re-try this operation.
179 virtual ssize_t write(const void *buffer, size_t count) = 0;
180
181 // Transfer data to sink using a series of callbacks. More suitable for zero-fill, synthesis,
182 // and non-contiguous transfers (e.g. circular buffer or writev).
183 // Inputs:
184 // via Callback function that the sink will call as many times as needed to consume data.
185 // total Estimate of the number of frames the provider has available. This is an estimate,
186 // and it can provide a different number of frames during the series of callbacks.
187 // user Arbitrary void * reserved for data provider.
188 // block Number of frames per block, that is a suggested value for 'count' in each callback.
189 // Zero means no preference. This parameter is a hint only, and may be ignored.
190 // Return value:
191 // > 0 Total number of frames successfully transferred prior to first error.
192 // = 0 Count was zero.
193 // < 0 status_t error occurred prior to the first frame transfer.
194 // Errors:
195 // NEGOTIATE (Re-)negotiation is needed.
196 // WOULD_BLOCK No frames can be transferred without blocking.
197 // UNDERRUN write() has not been called frequently enough, or with enough frames to keep up.
198 // An underrun event is counted, and the caller should re-try this operation.
199 //
200 // The 'via' callback is called by the data sink as follows:
201 // Inputs:
202 // user Arbitrary void * reserved for data provider.
203 // buffer Non-NULL buffer owned by sink that callback should fill in with data,
204 // up to a maximum of 'count' frames.
205 // count Maximum number of frames to transfer during this callback.
206 // Return value:
207 // > 0 Number of frames successfully transferred during this callback prior to first error.
208 // = 0 Count was zero.
209 // < 0 status_t error occurred prior to the first frame transfer during this callback.
210 virtual ssize_t writeVia(writeVia_t via, size_t total, void *user, size_t block = 0);
211
Glenn Kasten767094d2013-08-23 13:51:43 -0700212 // Returns NO_ERROR if a timestamp is available. The timestamp includes the total number
213 // of frames presented to an external observer, together with the value of CLOCK_MONOTONIC
Glenn Kasten200092b2014-08-15 15:13:30 -0700214 // as of this presentation count. The timestamp parameter is undefined if error is returned.
Dan Willemsen7e7a4692016-12-13 20:34:11 -0800215 virtual status_t getTimestamp(ExtendedTimestamp& /*timestamp*/) { return INVALID_OPERATION; }
Glenn Kasten767094d2013-08-23 13:51:43 -0700216
Glenn Kasten01066232012-02-27 11:50:44 -0800217protected:
Glenn Kastenb187de12014-12-30 08:18:15 -0800218 NBAIO_Sink(const NBAIO_Format& format = Format_Invalid) : NBAIO_Port(format), mFramesWritten(0)
219 { }
Glenn Kasten01066232012-02-27 11:50:44 -0800220 virtual ~NBAIO_Sink() { }
221
222 // Implementations are free to ignore these if they don't need them
Andy Hung818e7a32016-02-16 18:08:07 -0800223 int64_t mFramesWritten;
Glenn Kasten01066232012-02-27 11:50:44 -0800224};
225
226// Abstract class (interface) representing a non-blocking data source, for use by a data consumer.
227class NBAIO_Source : public NBAIO_Port {
228
229public:
230
231 // For the next two APIs:
232 // 32 bits rolls over after 27 hours at 44.1 kHz; if that concerns you then poll periodically.
233
234 // Number of frames read successfully since construction.
Andy Hung818e7a32016-02-16 18:08:07 -0800235 virtual int64_t framesRead() const { return mFramesRead; }
Glenn Kasten01066232012-02-27 11:50:44 -0800236
237 // Number of frames lost due to overrun since construction.
238 // Not const because implementations may need to do I/O.
Andy Hung818e7a32016-02-16 18:08:07 -0800239 virtual int64_t framesOverrun() /*const*/ { return 0; }
Glenn Kasten01066232012-02-27 11:50:44 -0800240
241 // Number of overruns since construction, where a set of contiguous lost frames is one event.
242 // Not const because implementations may need to do I/O.
Andy Hung818e7a32016-02-16 18:08:07 -0800243 virtual int64_t overruns() /*const*/ { return 0; }
Glenn Kasten01066232012-02-27 11:50:44 -0800244
245 // Estimate of number of frames that could be read successfully now.
246 // When a read() is actually attempted, the implementation is permitted to return a smaller or
247 // larger transfer count, however it will make a good faith effort to give an accurate estimate.
248 // Errors:
249 // NEGOTIATE (Re-)negotiation is needed.
250 // OVERRUN One or more frames were lost due to overrun, try again to read more recent data.
251 // WOULD_BLOCK Determining how many frames can be read without blocking would itself block.
252 virtual ssize_t availableToRead() { return SSIZE_MAX; }
253
254 // Transfer data from source into single destination buffer. Implies a copy.
255 // Inputs:
256 // buffer Non-NULL destination buffer owned by consumer.
257 // count Maximum number of frames to transfer.
258 // Return value:
259 // > 0 Number of frames successfully transferred prior to first error.
260 // = 0 Count was zero.
261 // < 0 status_t error occurred prior to the first frame transfer.
262 // Errors:
263 // NEGOTIATE (Re-)negotiation is needed.
264 // WOULD_BLOCK No frames can be transferred without blocking.
265 // OVERRUN read() has not been called frequently enough, or with enough frames to keep up.
266 // One or more frames were lost due to overrun, try again to read more recent data.
Glenn Kastend79072e2016-01-06 08:41:20 -0800267 virtual ssize_t read(void *buffer, size_t count) = 0;
Glenn Kasten01066232012-02-27 11:50:44 -0800268
269 // Transfer data from source using a series of callbacks. More suitable for zero-fill,
270 // synthesis, and non-contiguous transfers (e.g. circular buffer or readv).
271 // Inputs:
272 // via Callback function that the source will call as many times as needed to provide data.
273 // total Estimate of the number of frames the consumer desires. This is an estimate,
274 // and it can consume a different number of frames during the series of callbacks.
275 // user Arbitrary void * reserved for data consumer.
276 // block Number of frames per block, that is a suggested value for 'count' in each callback.
277 // Zero means no preference. This parameter is a hint only, and may be ignored.
278 // Return value:
279 // > 0 Total number of frames successfully transferred prior to first error.
280 // = 0 Count was zero.
281 // < 0 status_t error occurred prior to the first frame transfer.
282 // Errors:
283 // NEGOTIATE (Re-)negotiation is needed.
284 // WOULD_BLOCK No frames can be transferred without blocking.
285 // OVERRUN read() has not been called frequently enough, or with enough frames to keep up.
286 // One or more frames were lost due to overrun, try again to read more recent data.
287 //
288 // The 'via' callback is called by the data source as follows:
289 // Inputs:
290 // user Arbitrary void * reserved for data consumer.
291 // dest Non-NULL buffer owned by source that callback should consume data from,
292 // up to a maximum of 'count' frames.
293 // count Maximum number of frames to transfer during this callback.
294 // Return value:
295 // > 0 Number of frames successfully transferred during this callback prior to first error.
296 // = 0 Count was zero.
297 // < 0 status_t error occurred prior to the first frame transfer during this callback.
Glenn Kastend79072e2016-01-06 08:41:20 -0800298 virtual ssize_t readVia(readVia_t via, size_t total, void *user, size_t block = 0);
Glenn Kasten01066232012-02-27 11:50:44 -0800299
Glenn Kasten894d6be2013-08-26 10:29:28 -0700300 // Invoked asynchronously by corresponding sink when a new timestamp is available.
301 // Default implementation ignores the timestamp.
Dan Willemsen7e7a4692016-12-13 20:34:11 -0800302 virtual void onTimestamp(const ExtendedTimestamp& /*timestamp*/) { }
Glenn Kasten894d6be2013-08-26 10:29:28 -0700303
Glenn Kasten01066232012-02-27 11:50:44 -0800304protected:
Glenn Kastenb187de12014-12-30 08:18:15 -0800305 NBAIO_Source(const NBAIO_Format& format = Format_Invalid) : NBAIO_Port(format), mFramesRead(0)
306 { }
Glenn Kasten01066232012-02-27 11:50:44 -0800307 virtual ~NBAIO_Source() { }
308
309 // Implementations are free to ignore these if they don't need them
Andy Hung818e7a32016-02-16 18:08:07 -0800310 int64_t mFramesRead;
Glenn Kasten01066232012-02-27 11:50:44 -0800311};
312
313} // namespace android
314
315#endif // ANDROID_AUDIO_NBAIO_H