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