blob: bf6e90aa6e4b6a7ae2feaf9c31c261bbb892a67b [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
44/**************************************/
45
Eric Laurent70e81102011-08-07 10:05:40 -070046/**
47 * standard audio parameters that the HAL may need to handle
48 */
49
50/**
51 * audio device parameters
52 */
53
Eric Laurented9928c2011-08-02 17:12:00 -070054/* BT SCO Noise Reduction + Echo Cancellation parameters */
55#define AUDIO_PARAMETER_KEY_BT_NREC "bt_headset_nrec"
56#define AUDIO_PARAMETER_VALUE_ON "on"
57#define AUDIO_PARAMETER_VALUE_OFF "off"
58
Eric Laurent70e81102011-08-07 10:05:40 -070059/* TTY mode selection */
60#define AUDIO_PARAMETER_KEY_TTY_MODE "tty_mode"
61#define AUDIO_PARAMETER_VALUE_TTY_OFF "tty_off"
62#define AUDIO_PARAMETER_VALUE_TTY_VCO "tty_vco"
63#define AUDIO_PARAMETER_VALUE_TTY_HCO "tty_hco"
64#define AUDIO_PARAMETER_VALUE_TTY_FULL "tty_full"
65
Eric Laurenta70c5d02012-03-07 18:59:47 -080066/* A2DP sink address set by framework */
67#define AUDIO_PARAMETER_A2DP_SINK_ADDRESS "a2dp_sink_address"
68
Eric Laurent70e81102011-08-07 10:05:40 -070069/**
70 * audio stream parameters
71 */
72
Glenn Kasten0cacd8d2012-02-10 13:42:44 -080073#define AUDIO_PARAMETER_STREAM_ROUTING "routing" // audio_devices_t
74#define AUDIO_PARAMETER_STREAM_FORMAT "format" // audio_format_t
75#define AUDIO_PARAMETER_STREAM_CHANNELS "channels" // audio_channel_mask_t
76#define AUDIO_PARAMETER_STREAM_FRAME_COUNT "frame_count" // size_t
77#define AUDIO_PARAMETER_STREAM_INPUT_SOURCE "input_source" // audio_source_t
78#define AUDIO_PARAMETER_STREAM_SAMPLING_RATE "sampling_rate" // uint32_t
Dima Zavin57dde282011-06-06 19:31:18 -070079
Eric Laurent70e81102011-08-07 10:05:40 -070080/**************************************/
81
Dima Zavinf1504db2011-03-11 11:20:49 -080082/* common audio stream parameters and operations */
83struct audio_stream {
84
85 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -080086 * Return the sampling rate in Hz - eg. 44100.
Dima Zavinf1504db2011-03-11 11:20:49 -080087 */
88 uint32_t (*get_sample_rate)(const struct audio_stream *stream);
Dima Zavin57dde282011-06-06 19:31:18 -070089
90 /* currently unused - use set_parameters with key
91 * AUDIO_PARAMETER_STREAM_SAMPLING_RATE
92 */
Dima Zavinf1504db2011-03-11 11:20:49 -080093 int (*set_sample_rate)(struct audio_stream *stream, uint32_t rate);
94
95 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -080096 * Return size of input/output buffer in bytes for this stream - eg. 4800.
97 * It should be a multiple of the frame size. See also get_input_buffer_size.
Dima Zavinf1504db2011-03-11 11:20:49 -080098 */
99 size_t (*get_buffer_size)(const struct audio_stream *stream);
100
101 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800102 * Return the channel mask -
Dima Zavinf1504db2011-03-11 11:20:49 -0800103 * e.g. AUDIO_CHANNEL_OUT_STEREO or AUDIO_CHANNEL_IN_STEREO
104 */
105 uint32_t (*get_channels)(const struct audio_stream *stream);
106
107 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800108 * Return the audio format - e.g. AUDIO_FORMAT_PCM_16_BIT
Dima Zavinf1504db2011-03-11 11:20:49 -0800109 */
Glenn Kastenfe79eb32012-01-12 14:55:57 -0800110 audio_format_t (*get_format)(const struct audio_stream *stream);
Dima Zavin57dde282011-06-06 19:31:18 -0700111
112 /* currently unused - use set_parameters with key
113 * AUDIO_PARAMETER_STREAM_FORMAT
114 */
Glenn Kastenfe79eb32012-01-12 14:55:57 -0800115 int (*set_format)(struct audio_stream *stream, audio_format_t format);
Dima Zavinf1504db2011-03-11 11:20:49 -0800116
117 /**
118 * Put the audio hardware input/output into standby mode.
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800119 * Driver should exit from standby mode at the next I/O operation.
Dima Zavinf1504db2011-03-11 11:20:49 -0800120 * Returns 0 on success and <0 on failure.
121 */
122 int (*standby)(struct audio_stream *stream);
123
124 /** dump the state of the audio input/output device */
125 int (*dump)(const struct audio_stream *stream, int fd);
126
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800127 /** Return the set of device(s) which this stream is connected to */
Dima Zavinf1504db2011-03-11 11:20:49 -0800128 audio_devices_t (*get_device)(const struct audio_stream *stream);
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800129
130 /**
131 * Currently unused - set_device() corresponds to set_parameters() with key
132 * AUDIO_PARAMETER_STREAM_ROUTING for both input and output.
133 * AUDIO_PARAMETER_STREAM_INPUT_SOURCE is an additional information used by
134 * input streams only.
135 */
Dima Zavinf1504db2011-03-11 11:20:49 -0800136 int (*set_device)(struct audio_stream *stream, audio_devices_t device);
137
138 /**
139 * set/get audio stream parameters. The function accepts a list of
140 * parameter key value pairs in the form: key1=value1;key2=value2;...
141 *
142 * Some keys are reserved for standard parameters (See AudioParameter class)
143 *
144 * If the implementation does not accept a parameter change while
145 * the output is active but the parameter is acceptable otherwise, it must
146 * return -ENOSYS.
147 *
148 * The audio flinger will put the stream in standby and then change the
149 * parameter value.
150 */
151 int (*set_parameters)(struct audio_stream *stream, const char *kv_pairs);
152
153 /*
154 * Returns a pointer to a heap allocated string. The caller is responsible
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800155 * for freeing the memory for it using free().
Dima Zavinf1504db2011-03-11 11:20:49 -0800156 */
157 char * (*get_parameters)(const struct audio_stream *stream,
158 const char *keys);
Eric Laurentf3008aa2011-06-17 16:53:12 -0700159 int (*add_audio_effect)(const struct audio_stream *stream,
160 effect_handle_t effect);
161 int (*remove_audio_effect)(const struct audio_stream *stream,
162 effect_handle_t effect);
Dima Zavinf1504db2011-03-11 11:20:49 -0800163};
164typedef struct audio_stream audio_stream_t;
165
166/**
167 * audio_stream_out is the abstraction interface for the audio output hardware.
168 *
169 * It provides information about various properties of the audio output
170 * hardware driver.
171 */
172
173struct audio_stream_out {
174 struct audio_stream common;
175
176 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800177 * Return the audio hardware driver estimated latency in milliseconds.
Dima Zavinf1504db2011-03-11 11:20:49 -0800178 */
179 uint32_t (*get_latency)(const struct audio_stream_out *stream);
180
181 /**
182 * Use this method in situations where audio mixing is done in the
183 * hardware. This method serves as a direct interface with hardware,
184 * allowing you to directly set the volume as apposed to via the framework.
185 * This method might produce multiple PCM outputs or hardware accelerated
186 * codecs, such as MP3 or AAC.
187 */
188 int (*set_volume)(struct audio_stream_out *stream, float left, float right);
189
190 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800191 * Write audio buffer to driver. Returns number of bytes written, or a
192 * negative status_t. If at least one frame was written successfully prior to the error,
193 * it is suggested that the driver return that successful (short) byte count
194 * and then return an error in the subsequent call.
Dima Zavinf1504db2011-03-11 11:20:49 -0800195 */
196 ssize_t (*write)(struct audio_stream_out *stream, const void* buffer,
197 size_t bytes);
198
199 /* return the number of audio frames written by the audio dsp to DAC since
200 * the output has exited standby
201 */
202 int (*get_render_position)(const struct audio_stream_out *stream,
203 uint32_t *dsp_frames);
Mike J. Chen5ad38a92011-08-15 12:05:00 -0700204
205 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800206 * get the local time at which the next write to the audio driver will be presented.
207 * The units are microseconds, where the epoch is decided by the local audio HAL.
Mike J. Chen5ad38a92011-08-15 12:05:00 -0700208 */
209 int (*get_next_write_timestamp)(const struct audio_stream_out *stream,
210 int64_t *timestamp);
211
Dima Zavinf1504db2011-03-11 11:20:49 -0800212};
213typedef struct audio_stream_out audio_stream_out_t;
214
215struct audio_stream_in {
216 struct audio_stream common;
217
218 /** set the input gain for the audio driver. This method is for
219 * for future use */
220 int (*set_gain)(struct audio_stream_in *stream, float gain);
221
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800222 /** Read audio buffer in from audio driver. Returns number of bytes read, or a
223 * negative status_t. If at least one frame was read prior to the error,
224 * read should return that byte count and then return an error in the subsequent call.
225 */
Dima Zavinf1504db2011-03-11 11:20:49 -0800226 ssize_t (*read)(struct audio_stream_in *stream, void* buffer,
227 size_t bytes);
228
229 /**
230 * Return the amount of input frames lost in the audio driver since the
231 * last call of this function.
232 * Audio driver is expected to reset the value to 0 and restart counting
233 * upon returning the current value by this function call.
234 * Such loss typically occurs when the user space process is blocked
235 * longer than the capacity of audio driver buffers.
236 *
237 * Unit: the number of input audio frames
238 */
239 uint32_t (*get_input_frames_lost)(struct audio_stream_in *stream);
240};
241typedef struct audio_stream_in audio_stream_in_t;
242
243/**
244 * return the frame size (number of bytes per sample).
245 */
Glenn Kastena26cbac2012-01-13 14:53:35 -0800246static inline size_t audio_stream_frame_size(struct audio_stream *s)
Dima Zavinf1504db2011-03-11 11:20:49 -0800247{
Glenn Kastena26cbac2012-01-13 14:53:35 -0800248 size_t chan_samp_sz;
Dima Zavinf1504db2011-03-11 11:20:49 -0800249
250 switch (s->get_format(s)) {
251 case AUDIO_FORMAT_PCM_16_BIT:
252 chan_samp_sz = sizeof(int16_t);
253 break;
254 case AUDIO_FORMAT_PCM_8_BIT:
255 default:
256 chan_samp_sz = sizeof(int8_t);
257 break;
258 }
259
260 return popcount(s->get_channels(s)) * chan_samp_sz;
261}
262
263
264/**********************************************************************/
265
266/**
267 * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
268 * and the fields of this data structure must begin with hw_module_t
269 * followed by module specific information.
270 */
271struct audio_module {
272 struct hw_module_t common;
273};
274
275struct audio_hw_device {
276 struct hw_device_t common;
277
278 /**
279 * used by audio flinger to enumerate what devices are supported by
280 * each audio_hw_device implementation.
281 *
282 * Return value is a bitmask of 1 or more values of audio_devices_t
283 */
284 uint32_t (*get_supported_devices)(const struct audio_hw_device *dev);
285
286 /**
287 * check to see if the audio hardware interface has been initialized.
288 * returns 0 on success, -ENODEV on failure.
289 */
290 int (*init_check)(const struct audio_hw_device *dev);
291
292 /** set the audio volume of a voice call. Range is between 0.0 and 1.0 */
293 int (*set_voice_volume)(struct audio_hw_device *dev, float volume);
294
295 /**
296 * set the audio volume for all audio activities other than voice call.
297 * Range between 0.0 and 1.0. If any value other than 0 is returned,
298 * the software mixer will emulate this capability.
299 */
300 int (*set_master_volume)(struct audio_hw_device *dev, float volume);
301
302 /**
Mike J. Chen5ad38a92011-08-15 12:05:00 -0700303 * Get the current master volume value for the HAL, if the HAL supports
304 * master volume control. AudioFlinger will query this value from the
305 * primary audio HAL when the service starts and use the value for setting
306 * the initial master volume across all HALs. HALs which do not support
307 * this method should may leave it set to NULL.
308 */
309 int (*get_master_volume)(struct audio_hw_device *dev, float *volume);
310
311 /**
Glenn Kasten6df641e2012-01-09 10:41:30 -0800312 * set_mode is called when the audio mode changes. AUDIO_MODE_NORMAL mode
Dima Zavinf1504db2011-03-11 11:20:49 -0800313 * is for standard audio playback, AUDIO_MODE_RINGTONE when a ringtone is
314 * playing, and AUDIO_MODE_IN_CALL when a call is in progress.
Dima Zavinf1504db2011-03-11 11:20:49 -0800315 */
Glenn Kasten6df641e2012-01-09 10:41:30 -0800316 int (*set_mode)(struct audio_hw_device *dev, audio_mode_t mode);
Dima Zavinf1504db2011-03-11 11:20:49 -0800317
318 /* mic mute */
319 int (*set_mic_mute)(struct audio_hw_device *dev, bool state);
320 int (*get_mic_mute)(const struct audio_hw_device *dev, bool *state);
321
322 /* set/get global audio parameters */
323 int (*set_parameters)(struct audio_hw_device *dev, const char *kv_pairs);
324
325 /*
326 * Returns a pointer to a heap allocated string. The caller is responsible
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800327 * for freeing the memory for it using free().
Dima Zavinf1504db2011-03-11 11:20:49 -0800328 */
329 char * (*get_parameters)(const struct audio_hw_device *dev,
330 const char *keys);
331
332 /* Returns audio input buffer size according to parameters passed or
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800333 * 0 if one of the parameters is not supported.
334 * See also get_buffer_size which is for a particular stream.
Dima Zavinf1504db2011-03-11 11:20:49 -0800335 */
336 size_t (*get_input_buffer_size)(const struct audio_hw_device *dev,
Glenn Kastenfe79eb32012-01-12 14:55:57 -0800337 uint32_t sample_rate, audio_format_t format,
Dima Zavinf1504db2011-03-11 11:20:49 -0800338 int channel_count);
339
340 /** This method creates and opens the audio hardware output stream */
341 int (*open_output_stream)(struct audio_hw_device *dev, uint32_t devices,
Glenn Kastenfe79eb32012-01-12 14:55:57 -0800342 audio_format_t *format, uint32_t *channels,
Dima Zavinf1504db2011-03-11 11:20:49 -0800343 uint32_t *sample_rate,
344 struct audio_stream_out **out);
345
346 void (*close_output_stream)(struct audio_hw_device *dev,
347 struct audio_stream_out* out);
348
349 /** This method creates and opens the audio hardware input stream */
350 int (*open_input_stream)(struct audio_hw_device *dev, uint32_t devices,
Glenn Kastenfe79eb32012-01-12 14:55:57 -0800351 audio_format_t *format, uint32_t *channels,
Dima Zavinf1504db2011-03-11 11:20:49 -0800352 uint32_t *sample_rate,
353 audio_in_acoustics_t acoustics,
354 struct audio_stream_in **stream_in);
355
356 void (*close_input_stream)(struct audio_hw_device *dev,
357 struct audio_stream_in *in);
358
359 /** This method dumps the state of the audio hardware */
360 int (*dump)(const struct audio_hw_device *dev, int fd);
361};
362typedef struct audio_hw_device audio_hw_device_t;
363
364/** convenience API for opening and closing a supported device */
365
366static inline int audio_hw_device_open(const struct hw_module_t* module,
367 struct audio_hw_device** device)
368{
369 return module->methods->open(module, AUDIO_HARDWARE_INTERFACE,
370 (struct hw_device_t**)device);
371}
372
373static inline int audio_hw_device_close(struct audio_hw_device* device)
374{
375 return device->common.close(&device->common);
376}
377
378
379__END_DECLS
380
381#endif // ANDROID_AUDIO_INTERFACE_H