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