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