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