blob: ae82c3b3e638f0af0ebf0872fee89a60f5003739 [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
66/**
67 * audio stream parameters
68 */
69
Dima Zavin57dde282011-06-06 19:31:18 -070070#define AUDIO_PARAMETER_STREAM_ROUTING "routing"
71#define AUDIO_PARAMETER_STREAM_FORMAT "format"
72#define AUDIO_PARAMETER_STREAM_CHANNELS "channels"
73#define AUDIO_PARAMETER_STREAM_FRAME_COUNT "frame_count"
74#define AUDIO_PARAMETER_STREAM_INPUT_SOURCE "input_source"
75
Eric Laurent70e81102011-08-07 10:05:40 -070076/**************************************/
77
Dima Zavinf1504db2011-03-11 11:20:49 -080078/* common audio stream parameters and operations */
79struct audio_stream {
80
81 /**
82 * sampling rate is in Hz - eg. 44100
83 */
84 uint32_t (*get_sample_rate)(const struct audio_stream *stream);
Dima Zavin57dde282011-06-06 19:31:18 -070085
86 /* currently unused - use set_parameters with key
87 * AUDIO_PARAMETER_STREAM_SAMPLING_RATE
88 */
Dima Zavinf1504db2011-03-11 11:20:49 -080089 int (*set_sample_rate)(struct audio_stream *stream, uint32_t rate);
90
91 /**
92 * size of output buffer in bytes - eg. 4800
93 */
94 size_t (*get_buffer_size)(const struct audio_stream *stream);
95
96 /**
97 * the channel mask -
98 * e.g. AUDIO_CHANNEL_OUT_STEREO or AUDIO_CHANNEL_IN_STEREO
99 */
100 uint32_t (*get_channels)(const struct audio_stream *stream);
101
102 /**
103 * audio format - eg. AUDIO_FORMAT_PCM_16_BIT
104 */
Glenn Kastenfe79eb32012-01-12 14:55:57 -0800105 audio_format_t (*get_format)(const struct audio_stream *stream);
Dima Zavin57dde282011-06-06 19:31:18 -0700106
107 /* currently unused - use set_parameters with key
108 * AUDIO_PARAMETER_STREAM_FORMAT
109 */
Glenn Kastenfe79eb32012-01-12 14:55:57 -0800110 int (*set_format)(struct audio_stream *stream, audio_format_t format);
Dima Zavinf1504db2011-03-11 11:20:49 -0800111
112 /**
113 * Put the audio hardware input/output into standby mode.
114 * Returns 0 on success and <0 on failure.
115 */
116 int (*standby)(struct audio_stream *stream);
117
118 /** dump the state of the audio input/output device */
119 int (*dump)(const struct audio_stream *stream, int fd);
120
121 audio_devices_t (*get_device)(const struct audio_stream *stream);
122 int (*set_device)(struct audio_stream *stream, audio_devices_t device);
123
124 /**
125 * set/get audio stream parameters. The function accepts a list of
126 * parameter key value pairs in the form: key1=value1;key2=value2;...
127 *
128 * Some keys are reserved for standard parameters (See AudioParameter class)
129 *
130 * If the implementation does not accept a parameter change while
131 * the output is active but the parameter is acceptable otherwise, it must
132 * return -ENOSYS.
133 *
134 * The audio flinger will put the stream in standby and then change the
135 * parameter value.
136 */
137 int (*set_parameters)(struct audio_stream *stream, const char *kv_pairs);
138
139 /*
140 * Returns a pointer to a heap allocated string. The caller is responsible
141 * for freeing the memory for it.
142 */
143 char * (*get_parameters)(const struct audio_stream *stream,
144 const char *keys);
Eric Laurentf3008aa2011-06-17 16:53:12 -0700145 int (*add_audio_effect)(const struct audio_stream *stream,
146 effect_handle_t effect);
147 int (*remove_audio_effect)(const struct audio_stream *stream,
148 effect_handle_t effect);
Dima Zavinf1504db2011-03-11 11:20:49 -0800149};
150typedef struct audio_stream audio_stream_t;
151
152/**
153 * audio_stream_out is the abstraction interface for the audio output hardware.
154 *
155 * It provides information about various properties of the audio output
156 * hardware driver.
157 */
158
159struct audio_stream_out {
160 struct audio_stream common;
161
162 /**
163 * return the audio hardware driver latency in milli seconds.
164 */
165 uint32_t (*get_latency)(const struct audio_stream_out *stream);
166
167 /**
168 * Use this method in situations where audio mixing is done in the
169 * hardware. This method serves as a direct interface with hardware,
170 * allowing you to directly set the volume as apposed to via the framework.
171 * This method might produce multiple PCM outputs or hardware accelerated
172 * codecs, such as MP3 or AAC.
173 */
174 int (*set_volume)(struct audio_stream_out *stream, float left, float right);
175
176 /**
177 * write audio buffer to driver. Returns number of bytes written
178 */
179 ssize_t (*write)(struct audio_stream_out *stream, const void* buffer,
180 size_t bytes);
181
182 /* return the number of audio frames written by the audio dsp to DAC since
183 * the output has exited standby
184 */
185 int (*get_render_position)(const struct audio_stream_out *stream,
186 uint32_t *dsp_frames);
187};
188typedef struct audio_stream_out audio_stream_out_t;
189
190struct audio_stream_in {
191 struct audio_stream common;
192
193 /** set the input gain for the audio driver. This method is for
194 * for future use */
195 int (*set_gain)(struct audio_stream_in *stream, float gain);
196
197 /** read audio buffer in from audio driver */
198 ssize_t (*read)(struct audio_stream_in *stream, void* buffer,
199 size_t bytes);
200
201 /**
202 * Return the amount of input frames lost in the audio driver since the
203 * last call of this function.
204 * Audio driver is expected to reset the value to 0 and restart counting
205 * upon returning the current value by this function call.
206 * Such loss typically occurs when the user space process is blocked
207 * longer than the capacity of audio driver buffers.
208 *
209 * Unit: the number of input audio frames
210 */
211 uint32_t (*get_input_frames_lost)(struct audio_stream_in *stream);
212};
213typedef struct audio_stream_in audio_stream_in_t;
214
215/**
216 * return the frame size (number of bytes per sample).
217 */
218static inline uint32_t audio_stream_frame_size(struct audio_stream *s)
219{
220 int chan_samp_sz;
221
222 switch (s->get_format(s)) {
223 case AUDIO_FORMAT_PCM_16_BIT:
224 chan_samp_sz = sizeof(int16_t);
225 break;
226 case AUDIO_FORMAT_PCM_8_BIT:
227 default:
228 chan_samp_sz = sizeof(int8_t);
229 break;
230 }
231
232 return popcount(s->get_channels(s)) * chan_samp_sz;
233}
234
235
236/**********************************************************************/
237
238/**
239 * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
240 * and the fields of this data structure must begin with hw_module_t
241 * followed by module specific information.
242 */
243struct audio_module {
244 struct hw_module_t common;
245};
246
247struct audio_hw_device {
248 struct hw_device_t common;
249
250 /**
251 * used by audio flinger to enumerate what devices are supported by
252 * each audio_hw_device implementation.
253 *
254 * Return value is a bitmask of 1 or more values of audio_devices_t
255 */
256 uint32_t (*get_supported_devices)(const struct audio_hw_device *dev);
257
258 /**
259 * check to see if the audio hardware interface has been initialized.
260 * returns 0 on success, -ENODEV on failure.
261 */
262 int (*init_check)(const struct audio_hw_device *dev);
263
264 /** set the audio volume of a voice call. Range is between 0.0 and 1.0 */
265 int (*set_voice_volume)(struct audio_hw_device *dev, float volume);
266
267 /**
268 * set the audio volume for all audio activities other than voice call.
269 * Range between 0.0 and 1.0. If any value other than 0 is returned,
270 * the software mixer will emulate this capability.
271 */
272 int (*set_master_volume)(struct audio_hw_device *dev, float volume);
273
274 /**
275 * setMode is called when the audio mode changes. AUDIO_MODE_NORMAL mode
276 * is for standard audio playback, AUDIO_MODE_RINGTONE when a ringtone is
277 * playing, and AUDIO_MODE_IN_CALL when a call is in progress.
Glenn Kastenaf320d72011-12-19 17:19:34 -0800278 * Actual type of mode is enum audio_mode_t defined in <system/audio.h>.
Dima Zavinf1504db2011-03-11 11:20:49 -0800279 */
280 int (*set_mode)(struct audio_hw_device *dev, int mode);
281
282 /* mic mute */
283 int (*set_mic_mute)(struct audio_hw_device *dev, bool state);
284 int (*get_mic_mute)(const struct audio_hw_device *dev, bool *state);
285
286 /* set/get global audio parameters */
287 int (*set_parameters)(struct audio_hw_device *dev, const char *kv_pairs);
288
289 /*
290 * Returns a pointer to a heap allocated string. The caller is responsible
291 * for freeing the memory for it.
292 */
293 char * (*get_parameters)(const struct audio_hw_device *dev,
294 const char *keys);
295
296 /* Returns audio input buffer size according to parameters passed or
297 * 0 if one of the parameters is not supported
298 */
299 size_t (*get_input_buffer_size)(const struct audio_hw_device *dev,
Glenn Kastenfe79eb32012-01-12 14:55:57 -0800300 uint32_t sample_rate, audio_format_t format,
Dima Zavinf1504db2011-03-11 11:20:49 -0800301 int channel_count);
302
303 /** This method creates and opens the audio hardware output stream */
304 int (*open_output_stream)(struct audio_hw_device *dev, uint32_t devices,
Glenn Kastenfe79eb32012-01-12 14:55:57 -0800305 audio_format_t *format, uint32_t *channels,
Dima Zavinf1504db2011-03-11 11:20:49 -0800306 uint32_t *sample_rate,
307 struct audio_stream_out **out);
308
309 void (*close_output_stream)(struct audio_hw_device *dev,
310 struct audio_stream_out* out);
311
312 /** This method creates and opens the audio hardware input stream */
313 int (*open_input_stream)(struct audio_hw_device *dev, uint32_t devices,
Glenn Kastenfe79eb32012-01-12 14:55:57 -0800314 audio_format_t *format, uint32_t *channels,
Dima Zavinf1504db2011-03-11 11:20:49 -0800315 uint32_t *sample_rate,
316 audio_in_acoustics_t acoustics,
317 struct audio_stream_in **stream_in);
318
319 void (*close_input_stream)(struct audio_hw_device *dev,
320 struct audio_stream_in *in);
321
322 /** This method dumps the state of the audio hardware */
323 int (*dump)(const struct audio_hw_device *dev, int fd);
324};
325typedef struct audio_hw_device audio_hw_device_t;
326
327/** convenience API for opening and closing a supported device */
328
329static inline int audio_hw_device_open(const struct hw_module_t* module,
330 struct audio_hw_device** device)
331{
332 return module->methods->open(module, AUDIO_HARDWARE_INTERFACE,
333 (struct hw_device_t**)device);
334}
335
336static inline int audio_hw_device_close(struct audio_hw_device* device)
337{
338 return device->common.close(&device->common);
339}
340
341
342__END_DECLS
343
344#endif // ANDROID_AUDIO_INTERFACE_H