blob: 878b1b23b716baa57f929dcd69ff7585efd77d84 [file] [log] [blame]
Dima Zavinf1504db2011-03-11 11:20:49 -08001/*
2 * Copyright (C) 2011 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
18#ifndef ANDROID_AUDIO_HAL_INTERFACE_H
19#define ANDROID_AUDIO_HAL_INTERFACE_H
20
21#include <stdint.h>
22#include <strings.h>
23#include <sys/cdefs.h>
24#include <sys/types.h>
25
26#include <cutils/bitops.h>
27
28#include <hardware/hardware.h>
Dima Zavinaa211722011-05-11 14:15:53 -070029#include <system/audio.h>
Eric Laurentf3008aa2011-06-17 16:53:12 -070030#include <hardware/audio_effect.h>
Dima Zavinf1504db2011-03-11 11:20:49 -080031
32__BEGIN_DECLS
33
34/**
35 * The id of this module
36 */
37#define AUDIO_HARDWARE_MODULE_ID "audio"
38
39/**
40 * Name of the audio devices to open
41 */
42#define AUDIO_HARDWARE_INTERFACE "audio_hw_if"
43
Eric Laurent431fc782012-04-03 12:07:02 -070044/**
45 * List of known audio HAL modules. This is the base name of the audio HAL
46 * library composed of the "audio." prefix, one of the base names below and
47 * a suffix specific to the device.
48 * e.g: audio.primary.goldfish.so or audio.a2dp.default.so
49 */
50
51#define AUDIO_HARDWARE_MODULE_ID_PRIMARY "primary"
52#define AUDIO_HARDWARE_MODULE_ID_A2DP "a2dp"
53#define AUDIO_HARDWARE_MODULE_ID_USB "usb"
54
Dima Zavinf1504db2011-03-11 11:20:49 -080055/**************************************/
56
Eric Laurent70e81102011-08-07 10:05:40 -070057/**
58 * standard audio parameters that the HAL may need to handle
59 */
60
61/**
62 * audio device parameters
63 */
64
Eric Laurented9928c2011-08-02 17:12:00 -070065/* BT SCO Noise Reduction + Echo Cancellation parameters */
66#define AUDIO_PARAMETER_KEY_BT_NREC "bt_headset_nrec"
67#define AUDIO_PARAMETER_VALUE_ON "on"
68#define AUDIO_PARAMETER_VALUE_OFF "off"
69
Eric Laurent70e81102011-08-07 10:05:40 -070070/* TTY mode selection */
71#define AUDIO_PARAMETER_KEY_TTY_MODE "tty_mode"
72#define AUDIO_PARAMETER_VALUE_TTY_OFF "tty_off"
73#define AUDIO_PARAMETER_VALUE_TTY_VCO "tty_vco"
74#define AUDIO_PARAMETER_VALUE_TTY_HCO "tty_hco"
75#define AUDIO_PARAMETER_VALUE_TTY_FULL "tty_full"
76
Eric Laurenta70c5d02012-03-07 18:59:47 -080077/* A2DP sink address set by framework */
78#define AUDIO_PARAMETER_A2DP_SINK_ADDRESS "a2dp_sink_address"
79
Eric Laurent70e81102011-08-07 10:05:40 -070080/**
81 * audio stream parameters
82 */
83
Glenn Kasten0cacd8d2012-02-10 13:42:44 -080084#define AUDIO_PARAMETER_STREAM_ROUTING "routing" // audio_devices_t
85#define AUDIO_PARAMETER_STREAM_FORMAT "format" // audio_format_t
86#define AUDIO_PARAMETER_STREAM_CHANNELS "channels" // audio_channel_mask_t
87#define AUDIO_PARAMETER_STREAM_FRAME_COUNT "frame_count" // size_t
88#define AUDIO_PARAMETER_STREAM_INPUT_SOURCE "input_source" // audio_source_t
89#define AUDIO_PARAMETER_STREAM_SAMPLING_RATE "sampling_rate" // uint32_t
Dima Zavin57dde282011-06-06 19:31:18 -070090
Eric Laurent70e81102011-08-07 10:05:40 -070091/**************************************/
92
Dima Zavinf1504db2011-03-11 11:20:49 -080093/* common audio stream parameters and operations */
94struct audio_stream {
95
96 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -080097 * Return the sampling rate in Hz - eg. 44100.
Dima Zavinf1504db2011-03-11 11:20:49 -080098 */
99 uint32_t (*get_sample_rate)(const struct audio_stream *stream);
Dima Zavin57dde282011-06-06 19:31:18 -0700100
101 /* currently unused - use set_parameters with key
102 * AUDIO_PARAMETER_STREAM_SAMPLING_RATE
103 */
Dima Zavinf1504db2011-03-11 11:20:49 -0800104 int (*set_sample_rate)(struct audio_stream *stream, uint32_t rate);
105
106 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800107 * Return size of input/output buffer in bytes for this stream - eg. 4800.
108 * It should be a multiple of the frame size. See also get_input_buffer_size.
Dima Zavinf1504db2011-03-11 11:20:49 -0800109 */
110 size_t (*get_buffer_size)(const struct audio_stream *stream);
111
112 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800113 * Return the channel mask -
Dima Zavinf1504db2011-03-11 11:20:49 -0800114 * e.g. AUDIO_CHANNEL_OUT_STEREO or AUDIO_CHANNEL_IN_STEREO
115 */
116 uint32_t (*get_channels)(const struct audio_stream *stream);
117
118 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800119 * Return the audio format - e.g. AUDIO_FORMAT_PCM_16_BIT
Dima Zavinf1504db2011-03-11 11:20:49 -0800120 */
Glenn Kastenfe79eb32012-01-12 14:55:57 -0800121 audio_format_t (*get_format)(const struct audio_stream *stream);
Dima Zavin57dde282011-06-06 19:31:18 -0700122
123 /* currently unused - use set_parameters with key
124 * AUDIO_PARAMETER_STREAM_FORMAT
125 */
Glenn Kastenfe79eb32012-01-12 14:55:57 -0800126 int (*set_format)(struct audio_stream *stream, audio_format_t format);
Dima Zavinf1504db2011-03-11 11:20:49 -0800127
128 /**
129 * Put the audio hardware input/output into standby mode.
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800130 * Driver should exit from standby mode at the next I/O operation.
Dima Zavinf1504db2011-03-11 11:20:49 -0800131 * Returns 0 on success and <0 on failure.
132 */
133 int (*standby)(struct audio_stream *stream);
134
135 /** dump the state of the audio input/output device */
136 int (*dump)(const struct audio_stream *stream, int fd);
137
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800138 /** Return the set of device(s) which this stream is connected to */
Dima Zavinf1504db2011-03-11 11:20:49 -0800139 audio_devices_t (*get_device)(const struct audio_stream *stream);
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800140
141 /**
142 * Currently unused - set_device() corresponds to set_parameters() with key
143 * AUDIO_PARAMETER_STREAM_ROUTING for both input and output.
144 * AUDIO_PARAMETER_STREAM_INPUT_SOURCE is an additional information used by
145 * input streams only.
146 */
Dima Zavinf1504db2011-03-11 11:20:49 -0800147 int (*set_device)(struct audio_stream *stream, audio_devices_t device);
148
149 /**
150 * set/get audio stream parameters. The function accepts a list of
151 * parameter key value pairs in the form: key1=value1;key2=value2;...
152 *
153 * Some keys are reserved for standard parameters (See AudioParameter class)
154 *
155 * If the implementation does not accept a parameter change while
156 * the output is active but the parameter is acceptable otherwise, it must
157 * return -ENOSYS.
158 *
159 * The audio flinger will put the stream in standby and then change the
160 * parameter value.
161 */
162 int (*set_parameters)(struct audio_stream *stream, const char *kv_pairs);
163
164 /*
165 * Returns a pointer to a heap allocated string. The caller is responsible
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800166 * for freeing the memory for it using free().
Dima Zavinf1504db2011-03-11 11:20:49 -0800167 */
168 char * (*get_parameters)(const struct audio_stream *stream,
169 const char *keys);
Eric Laurentf3008aa2011-06-17 16:53:12 -0700170 int (*add_audio_effect)(const struct audio_stream *stream,
171 effect_handle_t effect);
172 int (*remove_audio_effect)(const struct audio_stream *stream,
173 effect_handle_t effect);
Dima Zavinf1504db2011-03-11 11:20:49 -0800174};
175typedef struct audio_stream audio_stream_t;
176
177/**
178 * audio_stream_out is the abstraction interface for the audio output hardware.
179 *
180 * It provides information about various properties of the audio output
181 * hardware driver.
182 */
183
184struct audio_stream_out {
185 struct audio_stream common;
186
187 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800188 * Return the audio hardware driver estimated latency in milliseconds.
Dima Zavinf1504db2011-03-11 11:20:49 -0800189 */
190 uint32_t (*get_latency)(const struct audio_stream_out *stream);
191
192 /**
193 * Use this method in situations where audio mixing is done in the
194 * hardware. This method serves as a direct interface with hardware,
195 * allowing you to directly set the volume as apposed to via the framework.
196 * This method might produce multiple PCM outputs or hardware accelerated
197 * codecs, such as MP3 or AAC.
198 */
199 int (*set_volume)(struct audio_stream_out *stream, float left, float right);
200
201 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800202 * Write audio buffer to driver. Returns number of bytes written, or a
203 * negative status_t. If at least one frame was written successfully prior to the error,
204 * it is suggested that the driver return that successful (short) byte count
205 * and then return an error in the subsequent call.
Dima Zavinf1504db2011-03-11 11:20:49 -0800206 */
207 ssize_t (*write)(struct audio_stream_out *stream, const void* buffer,
208 size_t bytes);
209
210 /* return the number of audio frames written by the audio dsp to DAC since
211 * the output has exited standby
212 */
213 int (*get_render_position)(const struct audio_stream_out *stream,
214 uint32_t *dsp_frames);
Mike J. Chen5ad38a92011-08-15 12:05:00 -0700215
216 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800217 * get the local time at which the next write to the audio driver will be presented.
218 * The units are microseconds, where the epoch is decided by the local audio HAL.
Mike J. Chen5ad38a92011-08-15 12:05:00 -0700219 */
220 int (*get_next_write_timestamp)(const struct audio_stream_out *stream,
221 int64_t *timestamp);
222
Dima Zavinf1504db2011-03-11 11:20:49 -0800223};
224typedef struct audio_stream_out audio_stream_out_t;
225
226struct audio_stream_in {
227 struct audio_stream common;
228
229 /** set the input gain for the audio driver. This method is for
230 * for future use */
231 int (*set_gain)(struct audio_stream_in *stream, float gain);
232
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800233 /** Read audio buffer in from audio driver. Returns number of bytes read, or a
234 * negative status_t. If at least one frame was read prior to the error,
235 * read should return that byte count and then return an error in the subsequent call.
236 */
Dima Zavinf1504db2011-03-11 11:20:49 -0800237 ssize_t (*read)(struct audio_stream_in *stream, void* buffer,
238 size_t bytes);
239
240 /**
241 * Return the amount of input frames lost in the audio driver since the
242 * last call of this function.
243 * Audio driver is expected to reset the value to 0 and restart counting
244 * upon returning the current value by this function call.
245 * Such loss typically occurs when the user space process is blocked
246 * longer than the capacity of audio driver buffers.
247 *
248 * Unit: the number of input audio frames
249 */
250 uint32_t (*get_input_frames_lost)(struct audio_stream_in *stream);
251};
252typedef struct audio_stream_in audio_stream_in_t;
253
254/**
255 * return the frame size (number of bytes per sample).
256 */
Glenn Kastena26cbac2012-01-13 14:53:35 -0800257static inline size_t audio_stream_frame_size(struct audio_stream *s)
Dima Zavinf1504db2011-03-11 11:20:49 -0800258{
Glenn Kastena26cbac2012-01-13 14:53:35 -0800259 size_t chan_samp_sz;
Dima Zavinf1504db2011-03-11 11:20:49 -0800260
261 switch (s->get_format(s)) {
262 case AUDIO_FORMAT_PCM_16_BIT:
263 chan_samp_sz = sizeof(int16_t);
264 break;
265 case AUDIO_FORMAT_PCM_8_BIT:
266 default:
267 chan_samp_sz = sizeof(int8_t);
268 break;
269 }
270
271 return popcount(s->get_channels(s)) * chan_samp_sz;
272}
273
274
275/**********************************************************************/
276
277/**
278 * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
279 * and the fields of this data structure must begin with hw_module_t
280 * followed by module specific information.
281 */
282struct audio_module {
283 struct hw_module_t common;
284};
285
286struct audio_hw_device {
287 struct hw_device_t common;
288
289 /**
290 * used by audio flinger to enumerate what devices are supported by
291 * each audio_hw_device implementation.
292 *
293 * Return value is a bitmask of 1 or more values of audio_devices_t
294 */
295 uint32_t (*get_supported_devices)(const struct audio_hw_device *dev);
296
297 /**
298 * check to see if the audio hardware interface has been initialized.
299 * returns 0 on success, -ENODEV on failure.
300 */
301 int (*init_check)(const struct audio_hw_device *dev);
302
303 /** set the audio volume of a voice call. Range is between 0.0 and 1.0 */
304 int (*set_voice_volume)(struct audio_hw_device *dev, float volume);
305
306 /**
307 * set the audio volume for all audio activities other than voice call.
308 * Range between 0.0 and 1.0. If any value other than 0 is returned,
309 * the software mixer will emulate this capability.
310 */
311 int (*set_master_volume)(struct audio_hw_device *dev, float volume);
312
313 /**
Mike J. Chen5ad38a92011-08-15 12:05:00 -0700314 * Get the current master volume value for the HAL, if the HAL supports
315 * master volume control. AudioFlinger will query this value from the
316 * primary audio HAL when the service starts and use the value for setting
317 * the initial master volume across all HALs. HALs which do not support
318 * this method should may leave it set to NULL.
319 */
320 int (*get_master_volume)(struct audio_hw_device *dev, float *volume);
321
322 /**
Glenn Kasten6df641e2012-01-09 10:41:30 -0800323 * set_mode is called when the audio mode changes. AUDIO_MODE_NORMAL mode
Dima Zavinf1504db2011-03-11 11:20:49 -0800324 * is for standard audio playback, AUDIO_MODE_RINGTONE when a ringtone is
325 * playing, and AUDIO_MODE_IN_CALL when a call is in progress.
Dima Zavinf1504db2011-03-11 11:20:49 -0800326 */
Glenn Kasten6df641e2012-01-09 10:41:30 -0800327 int (*set_mode)(struct audio_hw_device *dev, audio_mode_t mode);
Dima Zavinf1504db2011-03-11 11:20:49 -0800328
329 /* mic mute */
330 int (*set_mic_mute)(struct audio_hw_device *dev, bool state);
331 int (*get_mic_mute)(const struct audio_hw_device *dev, bool *state);
332
333 /* set/get global audio parameters */
334 int (*set_parameters)(struct audio_hw_device *dev, const char *kv_pairs);
335
336 /*
337 * Returns a pointer to a heap allocated string. The caller is responsible
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800338 * for freeing the memory for it using free().
Dima Zavinf1504db2011-03-11 11:20:49 -0800339 */
340 char * (*get_parameters)(const struct audio_hw_device *dev,
341 const char *keys);
342
343 /* Returns audio input buffer size according to parameters passed or
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800344 * 0 if one of the parameters is not supported.
345 * See also get_buffer_size which is for a particular stream.
Dima Zavinf1504db2011-03-11 11:20:49 -0800346 */
347 size_t (*get_input_buffer_size)(const struct audio_hw_device *dev,
Glenn Kastenfe79eb32012-01-12 14:55:57 -0800348 uint32_t sample_rate, audio_format_t format,
Dima Zavinf1504db2011-03-11 11:20:49 -0800349 int channel_count);
350
351 /** This method creates and opens the audio hardware output stream */
352 int (*open_output_stream)(struct audio_hw_device *dev, uint32_t devices,
Glenn Kastenfe79eb32012-01-12 14:55:57 -0800353 audio_format_t *format, uint32_t *channels,
Dima Zavinf1504db2011-03-11 11:20:49 -0800354 uint32_t *sample_rate,
355 struct audio_stream_out **out);
356
357 void (*close_output_stream)(struct audio_hw_device *dev,
358 struct audio_stream_out* out);
359
360 /** This method creates and opens the audio hardware input stream */
361 int (*open_input_stream)(struct audio_hw_device *dev, uint32_t devices,
Glenn Kastenfe79eb32012-01-12 14:55:57 -0800362 audio_format_t *format, uint32_t *channels,
Dima Zavinf1504db2011-03-11 11:20:49 -0800363 uint32_t *sample_rate,
364 audio_in_acoustics_t acoustics,
365 struct audio_stream_in **stream_in);
366
367 void (*close_input_stream)(struct audio_hw_device *dev,
368 struct audio_stream_in *in);
369
370 /** This method dumps the state of the audio hardware */
371 int (*dump)(const struct audio_hw_device *dev, int fd);
372};
373typedef struct audio_hw_device audio_hw_device_t;
374
375/** convenience API for opening and closing a supported device */
376
377static inline int audio_hw_device_open(const struct hw_module_t* module,
378 struct audio_hw_device** device)
379{
380 return module->methods->open(module, AUDIO_HARDWARE_INTERFACE,
381 (struct hw_device_t**)device);
382}
383
384static inline int audio_hw_device_close(struct audio_hw_device* device)
385{
386 return device->common.close(&device->common);
387}
388
389
390__END_DECLS
391
392#endif // ANDROID_AUDIO_INTERFACE_H