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