blob: 4fd73a2f97e18d0d17e013b2a663d60442d95e54 [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 Laurent55786bc2012-04-10 16:56:32 -070044
45/* Use version 0.1 to be compatible with first generation of audio hw module with version_major
46 * hardcoded to 1. No audio module API change.
47 */
48#define AUDIO_MODULE_API_VERSION_0_1 HARDWARE_MODULE_API_VERSION(0, 1)
49#define AUDIO_MODULE_API_VERSION_CURRENT AUDIO_MODULE_API_VERSION_0_1
50
51/* First generation of audio devices had version hardcoded to 0. all devices with versions < 1.0
52 * will be considered of first generation API.
53 */
54#define AUDIO_DEVICE_API_VERSION_0_0 HARDWARE_DEVICE_API_VERSION(0, 0)
55#define AUDIO_DEVICE_API_VERSION_1_0 HARDWARE_DEVICE_API_VERSION(1, 0)
56#define AUDIO_DEVICE_API_VERSION_CURRENT AUDIO_DEVICE_API_VERSION_1_0
57
Eric Laurent431fc782012-04-03 12:07:02 -070058/**
59 * List of known audio HAL modules. This is the base name of the audio HAL
60 * library composed of the "audio." prefix, one of the base names below and
61 * a suffix specific to the device.
62 * e.g: audio.primary.goldfish.so or audio.a2dp.default.so
63 */
64
65#define AUDIO_HARDWARE_MODULE_ID_PRIMARY "primary"
66#define AUDIO_HARDWARE_MODULE_ID_A2DP "a2dp"
67#define AUDIO_HARDWARE_MODULE_ID_USB "usb"
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070068#define AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX "r_submix"
Eric Laurent431fc782012-04-03 12:07:02 -070069
Dima Zavinf1504db2011-03-11 11:20:49 -080070/**************************************/
71
Eric Laurent70e81102011-08-07 10:05:40 -070072/**
73 * standard audio parameters that the HAL may need to handle
74 */
75
76/**
77 * audio device parameters
78 */
79
Eric Laurented9928c2011-08-02 17:12:00 -070080/* BT SCO Noise Reduction + Echo Cancellation parameters */
81#define AUDIO_PARAMETER_KEY_BT_NREC "bt_headset_nrec"
82#define AUDIO_PARAMETER_VALUE_ON "on"
83#define AUDIO_PARAMETER_VALUE_OFF "off"
84
Eric Laurent70e81102011-08-07 10:05:40 -070085/* TTY mode selection */
86#define AUDIO_PARAMETER_KEY_TTY_MODE "tty_mode"
87#define AUDIO_PARAMETER_VALUE_TTY_OFF "tty_off"
88#define AUDIO_PARAMETER_VALUE_TTY_VCO "tty_vco"
89#define AUDIO_PARAMETER_VALUE_TTY_HCO "tty_hco"
90#define AUDIO_PARAMETER_VALUE_TTY_FULL "tty_full"
91
Eric Laurenta70c5d02012-03-07 18:59:47 -080092/* A2DP sink address set by framework */
93#define AUDIO_PARAMETER_A2DP_SINK_ADDRESS "a2dp_sink_address"
94
Glenn Kasten34afb682012-06-08 10:49:34 -070095/* Screen state */
96#define AUDIO_PARAMETER_KEY_SCREEN_STATE "screen_state"
97
Eric Laurent70e81102011-08-07 10:05:40 -070098/**
99 * audio stream parameters
100 */
101
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800102#define AUDIO_PARAMETER_STREAM_ROUTING "routing" // audio_devices_t
103#define AUDIO_PARAMETER_STREAM_FORMAT "format" // audio_format_t
104#define AUDIO_PARAMETER_STREAM_CHANNELS "channels" // audio_channel_mask_t
105#define AUDIO_PARAMETER_STREAM_FRAME_COUNT "frame_count" // size_t
106#define AUDIO_PARAMETER_STREAM_INPUT_SOURCE "input_source" // audio_source_t
107#define AUDIO_PARAMETER_STREAM_SAMPLING_RATE "sampling_rate" // uint32_t
Dima Zavin57dde282011-06-06 19:31:18 -0700108
Eric Laurent41eeb4f2012-05-17 18:54:49 -0700109/* Query supported formats. The response is a '|' separated list of strings from
110 * audio_format_t enum e.g: "sup_formats=AUDIO_FORMAT_PCM_16_BIT" */
111#define AUDIO_PARAMETER_STREAM_SUP_FORMATS "sup_formats"
112/* Query supported channel masks. The response is a '|' separated list of strings from
113 * audio_channel_mask_t enum e.g: "sup_channels=AUDIO_CHANNEL_OUT_STEREO|AUDIO_CHANNEL_OUT_MONO" */
114#define AUDIO_PARAMETER_STREAM_SUP_CHANNELS "sup_channels"
115/* Query supported sampling rates. The response is a '|' separated list of integer values e.g:
116 * "sup_sampling_rates=44100|48000" */
117#define AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES "sup_sampling_rates"
118
Eric Laurent55786bc2012-04-10 16:56:32 -0700119
Eric Laurent70e81102011-08-07 10:05:40 -0700120/**************************************/
121
Eric Laurent55786bc2012-04-10 16:56:32 -0700122/* common audio stream configuration parameters */
123struct audio_config {
124 uint32_t sample_rate;
125 audio_channel_mask_t channel_mask;
126 audio_format_t format;
127};
128
129typedef struct audio_config audio_config_t;
130
Dima Zavinf1504db2011-03-11 11:20:49 -0800131/* common audio stream parameters and operations */
132struct audio_stream {
133
134 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800135 * Return the sampling rate in Hz - eg. 44100.
Dima Zavinf1504db2011-03-11 11:20:49 -0800136 */
137 uint32_t (*get_sample_rate)(const struct audio_stream *stream);
Dima Zavin57dde282011-06-06 19:31:18 -0700138
139 /* currently unused - use set_parameters with key
140 * AUDIO_PARAMETER_STREAM_SAMPLING_RATE
141 */
Dima Zavinf1504db2011-03-11 11:20:49 -0800142 int (*set_sample_rate)(struct audio_stream *stream, uint32_t rate);
143
144 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800145 * Return size of input/output buffer in bytes for this stream - eg. 4800.
146 * It should be a multiple of the frame size. See also get_input_buffer_size.
Dima Zavinf1504db2011-03-11 11:20:49 -0800147 */
148 size_t (*get_buffer_size)(const struct audio_stream *stream);
149
150 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800151 * Return the channel mask -
Dima Zavinf1504db2011-03-11 11:20:49 -0800152 * e.g. AUDIO_CHANNEL_OUT_STEREO or AUDIO_CHANNEL_IN_STEREO
153 */
Eric Laurent55786bc2012-04-10 16:56:32 -0700154 audio_channel_mask_t (*get_channels)(const struct audio_stream *stream);
Dima Zavinf1504db2011-03-11 11:20:49 -0800155
156 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800157 * Return the audio format - e.g. AUDIO_FORMAT_PCM_16_BIT
Dima Zavinf1504db2011-03-11 11:20:49 -0800158 */
Glenn Kastenfe79eb32012-01-12 14:55:57 -0800159 audio_format_t (*get_format)(const struct audio_stream *stream);
Dima Zavin57dde282011-06-06 19:31:18 -0700160
161 /* currently unused - use set_parameters with key
162 * AUDIO_PARAMETER_STREAM_FORMAT
163 */
Glenn Kastenfe79eb32012-01-12 14:55:57 -0800164 int (*set_format)(struct audio_stream *stream, audio_format_t format);
Dima Zavinf1504db2011-03-11 11:20:49 -0800165
166 /**
167 * Put the audio hardware input/output into standby mode.
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800168 * Driver should exit from standby mode at the next I/O operation.
Dima Zavinf1504db2011-03-11 11:20:49 -0800169 * Returns 0 on success and <0 on failure.
170 */
171 int (*standby)(struct audio_stream *stream);
172
173 /** dump the state of the audio input/output device */
174 int (*dump)(const struct audio_stream *stream, int fd);
175
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800176 /** Return the set of device(s) which this stream is connected to */
Dima Zavinf1504db2011-03-11 11:20:49 -0800177 audio_devices_t (*get_device)(const struct audio_stream *stream);
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800178
179 /**
180 * Currently unused - set_device() corresponds to set_parameters() with key
181 * AUDIO_PARAMETER_STREAM_ROUTING for both input and output.
182 * AUDIO_PARAMETER_STREAM_INPUT_SOURCE is an additional information used by
183 * input streams only.
184 */
Dima Zavinf1504db2011-03-11 11:20:49 -0800185 int (*set_device)(struct audio_stream *stream, audio_devices_t device);
186
187 /**
188 * set/get audio stream parameters. The function accepts a list of
189 * parameter key value pairs in the form: key1=value1;key2=value2;...
190 *
191 * Some keys are reserved for standard parameters (See AudioParameter class)
192 *
193 * If the implementation does not accept a parameter change while
194 * the output is active but the parameter is acceptable otherwise, it must
195 * return -ENOSYS.
196 *
197 * The audio flinger will put the stream in standby and then change the
198 * parameter value.
199 */
200 int (*set_parameters)(struct audio_stream *stream, const char *kv_pairs);
201
202 /*
203 * Returns a pointer to a heap allocated string. The caller is responsible
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800204 * for freeing the memory for it using free().
Dima Zavinf1504db2011-03-11 11:20:49 -0800205 */
206 char * (*get_parameters)(const struct audio_stream *stream,
207 const char *keys);
Eric Laurentf3008aa2011-06-17 16:53:12 -0700208 int (*add_audio_effect)(const struct audio_stream *stream,
209 effect_handle_t effect);
210 int (*remove_audio_effect)(const struct audio_stream *stream,
211 effect_handle_t effect);
Dima Zavinf1504db2011-03-11 11:20:49 -0800212};
213typedef struct audio_stream audio_stream_t;
214
215/**
216 * audio_stream_out is the abstraction interface for the audio output hardware.
217 *
218 * It provides information about various properties of the audio output
219 * hardware driver.
220 */
221
222struct audio_stream_out {
223 struct audio_stream common;
224
225 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800226 * Return the audio hardware driver estimated latency in milliseconds.
Dima Zavinf1504db2011-03-11 11:20:49 -0800227 */
228 uint32_t (*get_latency)(const struct audio_stream_out *stream);
229
230 /**
231 * Use this method in situations where audio mixing is done in the
232 * hardware. This method serves as a direct interface with hardware,
233 * allowing you to directly set the volume as apposed to via the framework.
234 * This method might produce multiple PCM outputs or hardware accelerated
235 * codecs, such as MP3 or AAC.
236 */
237 int (*set_volume)(struct audio_stream_out *stream, float left, float right);
238
239 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800240 * Write audio buffer to driver. Returns number of bytes written, or a
241 * negative status_t. If at least one frame was written successfully prior to the error,
242 * it is suggested that the driver return that successful (short) byte count
243 * and then return an error in the subsequent call.
Dima Zavinf1504db2011-03-11 11:20:49 -0800244 */
245 ssize_t (*write)(struct audio_stream_out *stream, const void* buffer,
246 size_t bytes);
247
248 /* return the number of audio frames written by the audio dsp to DAC since
249 * the output has exited standby
250 */
251 int (*get_render_position)(const struct audio_stream_out *stream,
252 uint32_t *dsp_frames);
Mike J. Chen5ad38a92011-08-15 12:05:00 -0700253
254 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800255 * get the local time at which the next write to the audio driver will be presented.
256 * The units are microseconds, where the epoch is decided by the local audio HAL.
Mike J. Chen5ad38a92011-08-15 12:05:00 -0700257 */
258 int (*get_next_write_timestamp)(const struct audio_stream_out *stream,
259 int64_t *timestamp);
260
Dima Zavinf1504db2011-03-11 11:20:49 -0800261};
262typedef struct audio_stream_out audio_stream_out_t;
263
264struct audio_stream_in {
265 struct audio_stream common;
266
267 /** set the input gain for the audio driver. This method is for
268 * for future use */
269 int (*set_gain)(struct audio_stream_in *stream, float gain);
270
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800271 /** Read audio buffer in from audio driver. Returns number of bytes read, or a
272 * negative status_t. If at least one frame was read prior to the error,
273 * read should return that byte count and then return an error in the subsequent call.
274 */
Dima Zavinf1504db2011-03-11 11:20:49 -0800275 ssize_t (*read)(struct audio_stream_in *stream, void* buffer,
276 size_t bytes);
277
278 /**
279 * Return the amount of input frames lost in the audio driver since the
280 * last call of this function.
281 * Audio driver is expected to reset the value to 0 and restart counting
282 * upon returning the current value by this function call.
283 * Such loss typically occurs when the user space process is blocked
284 * longer than the capacity of audio driver buffers.
285 *
286 * Unit: the number of input audio frames
287 */
288 uint32_t (*get_input_frames_lost)(struct audio_stream_in *stream);
289};
290typedef struct audio_stream_in audio_stream_in_t;
291
292/**
293 * return the frame size (number of bytes per sample).
294 */
Glenn Kasten48915ac2012-02-20 12:08:57 -0800295static inline size_t audio_stream_frame_size(const struct audio_stream *s)
Dima Zavinf1504db2011-03-11 11:20:49 -0800296{
Glenn Kastena26cbac2012-01-13 14:53:35 -0800297 size_t chan_samp_sz;
Dima Zavinf1504db2011-03-11 11:20:49 -0800298
299 switch (s->get_format(s)) {
300 case AUDIO_FORMAT_PCM_16_BIT:
301 chan_samp_sz = sizeof(int16_t);
302 break;
303 case AUDIO_FORMAT_PCM_8_BIT:
304 default:
305 chan_samp_sz = sizeof(int8_t);
306 break;
307 }
308
309 return popcount(s->get_channels(s)) * chan_samp_sz;
310}
311
312
313/**********************************************************************/
314
315/**
316 * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
317 * and the fields of this data structure must begin with hw_module_t
318 * followed by module specific information.
319 */
320struct audio_module {
321 struct hw_module_t common;
322};
323
324struct audio_hw_device {
325 struct hw_device_t common;
326
327 /**
328 * used by audio flinger to enumerate what devices are supported by
329 * each audio_hw_device implementation.
330 *
331 * Return value is a bitmask of 1 or more values of audio_devices_t
332 */
333 uint32_t (*get_supported_devices)(const struct audio_hw_device *dev);
334
335 /**
336 * check to see if the audio hardware interface has been initialized.
337 * returns 0 on success, -ENODEV on failure.
338 */
339 int (*init_check)(const struct audio_hw_device *dev);
340
341 /** set the audio volume of a voice call. Range is between 0.0 and 1.0 */
342 int (*set_voice_volume)(struct audio_hw_device *dev, float volume);
343
344 /**
345 * set the audio volume for all audio activities other than voice call.
346 * Range between 0.0 and 1.0. If any value other than 0 is returned,
347 * the software mixer will emulate this capability.
348 */
349 int (*set_master_volume)(struct audio_hw_device *dev, float volume);
350
351 /**
Mike J. Chen5ad38a92011-08-15 12:05:00 -0700352 * Get the current master volume value for the HAL, if the HAL supports
353 * master volume control. AudioFlinger will query this value from the
354 * primary audio HAL when the service starts and use the value for setting
355 * the initial master volume across all HALs. HALs which do not support
John Grossman47bf3d72012-07-17 11:54:04 -0700356 * this method may leave it set to NULL.
Mike J. Chen5ad38a92011-08-15 12:05:00 -0700357 */
358 int (*get_master_volume)(struct audio_hw_device *dev, float *volume);
359
360 /**
Glenn Kasten6df641e2012-01-09 10:41:30 -0800361 * set_mode is called when the audio mode changes. AUDIO_MODE_NORMAL mode
Dima Zavinf1504db2011-03-11 11:20:49 -0800362 * is for standard audio playback, AUDIO_MODE_RINGTONE when a ringtone is
363 * playing, and AUDIO_MODE_IN_CALL when a call is in progress.
Dima Zavinf1504db2011-03-11 11:20:49 -0800364 */
Glenn Kasten6df641e2012-01-09 10:41:30 -0800365 int (*set_mode)(struct audio_hw_device *dev, audio_mode_t mode);
Dima Zavinf1504db2011-03-11 11:20:49 -0800366
367 /* mic mute */
368 int (*set_mic_mute)(struct audio_hw_device *dev, bool state);
369 int (*get_mic_mute)(const struct audio_hw_device *dev, bool *state);
370
371 /* set/get global audio parameters */
372 int (*set_parameters)(struct audio_hw_device *dev, const char *kv_pairs);
373
374 /*
375 * Returns a pointer to a heap allocated string. The caller is responsible
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800376 * for freeing the memory for it using free().
Dima Zavinf1504db2011-03-11 11:20:49 -0800377 */
378 char * (*get_parameters)(const struct audio_hw_device *dev,
379 const char *keys);
380
381 /* Returns audio input buffer size according to parameters passed or
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800382 * 0 if one of the parameters is not supported.
383 * See also get_buffer_size which is for a particular stream.
Dima Zavinf1504db2011-03-11 11:20:49 -0800384 */
385 size_t (*get_input_buffer_size)(const struct audio_hw_device *dev,
Eric Laurent55786bc2012-04-10 16:56:32 -0700386 const struct audio_config *config);
Dima Zavinf1504db2011-03-11 11:20:49 -0800387
388 /** This method creates and opens the audio hardware output stream */
Eric Laurent55786bc2012-04-10 16:56:32 -0700389 int (*open_output_stream)(struct audio_hw_device *dev,
390 audio_io_handle_t handle,
391 audio_devices_t devices,
392 audio_output_flags_t flags,
393 struct audio_config *config,
394 struct audio_stream_out **stream_out);
Dima Zavinf1504db2011-03-11 11:20:49 -0800395
396 void (*close_output_stream)(struct audio_hw_device *dev,
Eric Laurent55786bc2012-04-10 16:56:32 -0700397 struct audio_stream_out* stream_out);
Dima Zavinf1504db2011-03-11 11:20:49 -0800398
399 /** This method creates and opens the audio hardware input stream */
Eric Laurent55786bc2012-04-10 16:56:32 -0700400 int (*open_input_stream)(struct audio_hw_device *dev,
401 audio_io_handle_t handle,
402 audio_devices_t devices,
403 struct audio_config *config,
Dima Zavinf1504db2011-03-11 11:20:49 -0800404 struct audio_stream_in **stream_in);
405
406 void (*close_input_stream)(struct audio_hw_device *dev,
Eric Laurent55786bc2012-04-10 16:56:32 -0700407 struct audio_stream_in *stream_in);
Dima Zavinf1504db2011-03-11 11:20:49 -0800408
409 /** This method dumps the state of the audio hardware */
410 int (*dump)(const struct audio_hw_device *dev, int fd);
John Grossman47bf3d72012-07-17 11:54:04 -0700411
412 /**
413 * set the audio mute status for all audio activities. If any value other
414 * than 0 is returned, the software mixer will emulate this capability.
415 */
416 int (*set_master_mute)(struct audio_hw_device *dev, bool mute);
417
418 /**
419 * Get the current master mute status for the HAL, if the HAL supports
420 * master mute control. AudioFlinger will query this value from the primary
421 * audio HAL when the service starts and use the value for setting the
422 * initial master mute across all HALs. HALs which do not support this
423 * method may leave it set to NULL.
424 */
425 int (*get_master_mute)(struct audio_hw_device *dev, bool *mute);
Dima Zavinf1504db2011-03-11 11:20:49 -0800426};
427typedef struct audio_hw_device audio_hw_device_t;
428
429/** convenience API for opening and closing a supported device */
430
431static inline int audio_hw_device_open(const struct hw_module_t* module,
432 struct audio_hw_device** device)
433{
434 return module->methods->open(module, AUDIO_HARDWARE_INTERFACE,
435 (struct hw_device_t**)device);
436}
437
438static inline int audio_hw_device_close(struct audio_hw_device* device)
439{
440 return device->common.close(&device->common);
441}
442
443
444__END_DECLS
445
446#endif // ANDROID_AUDIO_INTERFACE_H