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> |
Eric Laurent | f3008aa | 2011-06-17 16:53:12 -0700 | [diff] [blame] | 30 | #include <hardware/audio_effect.h> |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 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 | |
Eric Laurent | 55786bc | 2012-04-10 16:56:32 -0700 | [diff] [blame] | 44 | |
| 45 | /* Use version 0.1 to be compatible with first generation of audio hw module with version_major |
| 46 | * hardcoded to 1. No audio module API change. |
| 47 | */ |
| 48 | #define AUDIO_MODULE_API_VERSION_0_1 HARDWARE_MODULE_API_VERSION(0, 1) |
| 49 | #define AUDIO_MODULE_API_VERSION_CURRENT AUDIO_MODULE_API_VERSION_0_1 |
| 50 | |
| 51 | /* First generation of audio devices had version hardcoded to 0. all devices with versions < 1.0 |
| 52 | * will be considered of first generation API. |
| 53 | */ |
| 54 | #define AUDIO_DEVICE_API_VERSION_0_0 HARDWARE_DEVICE_API_VERSION(0, 0) |
| 55 | #define AUDIO_DEVICE_API_VERSION_1_0 HARDWARE_DEVICE_API_VERSION(1, 0) |
Eric Laurent | 85e08e2 | 2012-08-28 14:30:35 -0700 | [diff] [blame] | 56 | #define AUDIO_DEVICE_API_VERSION_2_0 HARDWARE_DEVICE_API_VERSION(2, 0) |
Eric Laurent | 73b8a74 | 2014-05-22 14:02:38 -0700 | [diff] [blame] | 57 | #define AUDIO_DEVICE_API_VERSION_3_0 HARDWARE_DEVICE_API_VERSION(3, 0) |
| 58 | #define AUDIO_DEVICE_API_VERSION_CURRENT AUDIO_DEVICE_API_VERSION_3_0 |
Eric Laurent | 447cae7 | 2014-05-22 13:45:55 -0700 | [diff] [blame] | 59 | /* Minimal audio HAL version supported by the audio framework */ |
| 60 | #define AUDIO_DEVICE_API_VERSION_MIN AUDIO_DEVICE_API_VERSION_2_0 |
Eric Laurent | 55786bc | 2012-04-10 16:56:32 -0700 | [diff] [blame] | 61 | |
Eric Laurent | 431fc78 | 2012-04-03 12:07:02 -0700 | [diff] [blame] | 62 | /** |
| 63 | * List of known audio HAL modules. This is the base name of the audio HAL |
| 64 | * library composed of the "audio." prefix, one of the base names below and |
| 65 | * a suffix specific to the device. |
| 66 | * e.g: audio.primary.goldfish.so or audio.a2dp.default.so |
| 67 | */ |
| 68 | |
| 69 | #define AUDIO_HARDWARE_MODULE_ID_PRIMARY "primary" |
| 70 | #define AUDIO_HARDWARE_MODULE_ID_A2DP "a2dp" |
| 71 | #define AUDIO_HARDWARE_MODULE_ID_USB "usb" |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 72 | #define AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX "r_submix" |
Richard Fitzgerald | f37f187 | 2013-03-25 16:11:44 +0000 | [diff] [blame] | 73 | #define AUDIO_HARDWARE_MODULE_ID_CODEC_OFFLOAD "codec_offload" |
Eric Laurent | 431fc78 | 2012-04-03 12:07:02 -0700 | [diff] [blame] | 74 | |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 75 | /**************************************/ |
| 76 | |
Eric Laurent | 70e8110 | 2011-08-07 10:05:40 -0700 | [diff] [blame] | 77 | /** |
| 78 | * standard audio parameters that the HAL may need to handle |
| 79 | */ |
| 80 | |
| 81 | /** |
| 82 | * audio device parameters |
| 83 | */ |
| 84 | |
Eric Laurent | 70e8110 | 2011-08-07 10:05:40 -0700 | [diff] [blame] | 85 | /* TTY mode selection */ |
| 86 | #define AUDIO_PARAMETER_KEY_TTY_MODE "tty_mode" |
| 87 | #define AUDIO_PARAMETER_VALUE_TTY_OFF "tty_off" |
| 88 | #define AUDIO_PARAMETER_VALUE_TTY_VCO "tty_vco" |
| 89 | #define AUDIO_PARAMETER_VALUE_TTY_HCO "tty_hco" |
| 90 | #define AUDIO_PARAMETER_VALUE_TTY_FULL "tty_full" |
| 91 | |
Mikhail Naganov | f7d1dff | 2016-10-17 17:13:10 -0700 | [diff] [blame^] | 92 | /* Hearing Aid Compatibility - Telecoil (HAC-T) mode on/off */ |
Eric Laurent | d1a1b1c | 2014-07-25 12:10:11 -0500 | [diff] [blame] | 93 | #define AUDIO_PARAMETER_KEY_HAC "HACSetting" |
| 94 | #define AUDIO_PARAMETER_VALUE_HAC_ON "ON" |
| 95 | #define AUDIO_PARAMETER_VALUE_HAC_OFF "OFF" |
| 96 | |
Eric Laurent | a70c5d0 | 2012-03-07 18:59:47 -0800 | [diff] [blame] | 97 | /* A2DP sink address set by framework */ |
| 98 | #define AUDIO_PARAMETER_A2DP_SINK_ADDRESS "a2dp_sink_address" |
| 99 | |
Mike Lockwood | 2d4d965 | 2014-05-28 11:09:54 -0700 | [diff] [blame] | 100 | /* A2DP source address set by framework */ |
| 101 | #define AUDIO_PARAMETER_A2DP_SOURCE_ADDRESS "a2dp_source_address" |
| 102 | |
Glenn Kasten | d930d92 | 2014-04-29 13:35:57 -0700 | [diff] [blame] | 103 | /* Bluetooth SCO wideband */ |
| 104 | #define AUDIO_PARAMETER_KEY_BT_SCO_WB "bt_wbs" |
| 105 | |
Eric Laurent | 70e8110 | 2011-08-07 10:05:40 -0700 | [diff] [blame] | 106 | /** |
| 107 | * audio stream parameters |
| 108 | */ |
| 109 | |
vivek mehta | 27258e5 | 2016-07-18 13:58:40 -0700 | [diff] [blame] | 110 | /* Enable AANC */ |
| 111 | #define AUDIO_PARAMETER_KEY_AANC "aanc_enabled" |
| 112 | |
Eric Laurent | 70e8110 | 2011-08-07 10:05:40 -0700 | [diff] [blame] | 113 | /**************************************/ |
| 114 | |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 115 | /* common audio stream parameters and operations */ |
| 116 | struct audio_stream { |
| 117 | |
| 118 | /** |
Glenn Kasten | 0cacd8d | 2012-02-10 13:42:44 -0800 | [diff] [blame] | 119 | * Return the sampling rate in Hz - eg. 44100. |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 120 | */ |
| 121 | uint32_t (*get_sample_rate)(const struct audio_stream *stream); |
Dima Zavin | 57dde28 | 2011-06-06 19:31:18 -0700 | [diff] [blame] | 122 | |
| 123 | /* currently unused - use set_parameters with key |
| 124 | * AUDIO_PARAMETER_STREAM_SAMPLING_RATE |
| 125 | */ |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 126 | int (*set_sample_rate)(struct audio_stream *stream, uint32_t rate); |
| 127 | |
| 128 | /** |
Glenn Kasten | 0cacd8d | 2012-02-10 13:42:44 -0800 | [diff] [blame] | 129 | * Return size of input/output buffer in bytes for this stream - eg. 4800. |
| 130 | * It should be a multiple of the frame size. See also get_input_buffer_size. |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 131 | */ |
| 132 | size_t (*get_buffer_size)(const struct audio_stream *stream); |
| 133 | |
| 134 | /** |
Glenn Kasten | 0cacd8d | 2012-02-10 13:42:44 -0800 | [diff] [blame] | 135 | * Return the channel mask - |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 136 | * e.g. AUDIO_CHANNEL_OUT_STEREO or AUDIO_CHANNEL_IN_STEREO |
| 137 | */ |
Eric Laurent | 55786bc | 2012-04-10 16:56:32 -0700 | [diff] [blame] | 138 | audio_channel_mask_t (*get_channels)(const struct audio_stream *stream); |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 139 | |
| 140 | /** |
Glenn Kasten | 0cacd8d | 2012-02-10 13:42:44 -0800 | [diff] [blame] | 141 | * Return the audio format - e.g. AUDIO_FORMAT_PCM_16_BIT |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 142 | */ |
Glenn Kasten | fe79eb3 | 2012-01-12 14:55:57 -0800 | [diff] [blame] | 143 | audio_format_t (*get_format)(const struct audio_stream *stream); |
Dima Zavin | 57dde28 | 2011-06-06 19:31:18 -0700 | [diff] [blame] | 144 | |
| 145 | /* currently unused - use set_parameters with key |
| 146 | * AUDIO_PARAMETER_STREAM_FORMAT |
| 147 | */ |
Glenn Kasten | fe79eb3 | 2012-01-12 14:55:57 -0800 | [diff] [blame] | 148 | int (*set_format)(struct audio_stream *stream, audio_format_t format); |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 149 | |
| 150 | /** |
| 151 | * Put the audio hardware input/output into standby mode. |
Glenn Kasten | 0cacd8d | 2012-02-10 13:42:44 -0800 | [diff] [blame] | 152 | * Driver should exit from standby mode at the next I/O operation. |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 153 | * Returns 0 on success and <0 on failure. |
| 154 | */ |
| 155 | int (*standby)(struct audio_stream *stream); |
| 156 | |
| 157 | /** dump the state of the audio input/output device */ |
| 158 | int (*dump)(const struct audio_stream *stream, int fd); |
| 159 | |
Glenn Kasten | 0cacd8d | 2012-02-10 13:42:44 -0800 | [diff] [blame] | 160 | /** Return the set of device(s) which this stream is connected to */ |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 161 | audio_devices_t (*get_device)(const struct audio_stream *stream); |
Glenn Kasten | 0cacd8d | 2012-02-10 13:42:44 -0800 | [diff] [blame] | 162 | |
| 163 | /** |
| 164 | * Currently unused - set_device() corresponds to set_parameters() with key |
| 165 | * AUDIO_PARAMETER_STREAM_ROUTING for both input and output. |
| 166 | * AUDIO_PARAMETER_STREAM_INPUT_SOURCE is an additional information used by |
| 167 | * input streams only. |
| 168 | */ |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 169 | int (*set_device)(struct audio_stream *stream, audio_devices_t device); |
| 170 | |
| 171 | /** |
| 172 | * set/get audio stream parameters. The function accepts a list of |
| 173 | * parameter key value pairs in the form: key1=value1;key2=value2;... |
| 174 | * |
| 175 | * Some keys are reserved for standard parameters (See AudioParameter class) |
| 176 | * |
| 177 | * If the implementation does not accept a parameter change while |
| 178 | * the output is active but the parameter is acceptable otherwise, it must |
| 179 | * return -ENOSYS. |
| 180 | * |
| 181 | * The audio flinger will put the stream in standby and then change the |
| 182 | * parameter value. |
| 183 | */ |
| 184 | int (*set_parameters)(struct audio_stream *stream, const char *kv_pairs); |
| 185 | |
| 186 | /* |
| 187 | * Returns a pointer to a heap allocated string. The caller is responsible |
Glenn Kasten | 0cacd8d | 2012-02-10 13:42:44 -0800 | [diff] [blame] | 188 | * for freeing the memory for it using free(). |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 189 | */ |
| 190 | char * (*get_parameters)(const struct audio_stream *stream, |
| 191 | const char *keys); |
Eric Laurent | f3008aa | 2011-06-17 16:53:12 -0700 | [diff] [blame] | 192 | int (*add_audio_effect)(const struct audio_stream *stream, |
| 193 | effect_handle_t effect); |
| 194 | int (*remove_audio_effect)(const struct audio_stream *stream, |
| 195 | effect_handle_t effect); |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 196 | }; |
| 197 | typedef struct audio_stream audio_stream_t; |
| 198 | |
Richard Fitzgerald | f37f187 | 2013-03-25 16:11:44 +0000 | [diff] [blame] | 199 | /* type of asynchronous write callback events. Mutually exclusive */ |
| 200 | typedef enum { |
| 201 | STREAM_CBK_EVENT_WRITE_READY, /* non blocking write completed */ |
Haynes Mathew George | 0d46876 | 2016-07-07 20:05:39 -0700 | [diff] [blame] | 202 | STREAM_CBK_EVENT_DRAIN_READY, /* drain completed */ |
| 203 | STREAM_CBK_EVENT_ERROR, /* stream hit some error, let AF take action */ |
Richard Fitzgerald | f37f187 | 2013-03-25 16:11:44 +0000 | [diff] [blame] | 204 | } stream_callback_event_t; |
| 205 | |
| 206 | typedef int (*stream_callback_t)(stream_callback_event_t event, void *param, void *cookie); |
| 207 | |
| 208 | /* type of drain requested to audio_stream_out->drain(). Mutually exclusive */ |
| 209 | typedef enum { |
| 210 | AUDIO_DRAIN_ALL, /* drain() returns when all data has been played */ |
| 211 | AUDIO_DRAIN_EARLY_NOTIFY /* drain() returns a short time before all data |
| 212 | from the current track has been played to |
| 213 | give time for gapless track switch */ |
| 214 | } audio_drain_type_t; |
| 215 | |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 216 | /** |
| 217 | * audio_stream_out is the abstraction interface for the audio output hardware. |
| 218 | * |
| 219 | * It provides information about various properties of the audio output |
| 220 | * hardware driver. |
| 221 | */ |
| 222 | |
| 223 | struct audio_stream_out { |
Stewart Miles | 84d3549 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 224 | /** |
| 225 | * Common methods of the audio stream out. This *must* be the first member of audio_stream_out |
| 226 | * as users of this structure will cast a audio_stream to audio_stream_out pointer in contexts |
| 227 | * where it's known the audio_stream references an audio_stream_out. |
| 228 | */ |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 229 | struct audio_stream common; |
| 230 | |
| 231 | /** |
Glenn Kasten | 0cacd8d | 2012-02-10 13:42:44 -0800 | [diff] [blame] | 232 | * Return the audio hardware driver estimated latency in milliseconds. |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 233 | */ |
| 234 | uint32_t (*get_latency)(const struct audio_stream_out *stream); |
| 235 | |
| 236 | /** |
| 237 | * Use this method in situations where audio mixing is done in the |
| 238 | * hardware. This method serves as a direct interface with hardware, |
| 239 | * allowing you to directly set the volume as apposed to via the framework. |
| 240 | * This method might produce multiple PCM outputs or hardware accelerated |
| 241 | * codecs, such as MP3 or AAC. |
| 242 | */ |
| 243 | int (*set_volume)(struct audio_stream_out *stream, float left, float right); |
| 244 | |
| 245 | /** |
Glenn Kasten | 0cacd8d | 2012-02-10 13:42:44 -0800 | [diff] [blame] | 246 | * Write audio buffer to driver. Returns number of bytes written, or a |
| 247 | * negative status_t. If at least one frame was written successfully prior to the error, |
| 248 | * it is suggested that the driver return that successful (short) byte count |
| 249 | * and then return an error in the subsequent call. |
Richard Fitzgerald | f37f187 | 2013-03-25 16:11:44 +0000 | [diff] [blame] | 250 | * |
| 251 | * If set_callback() has previously been called to enable non-blocking mode |
| 252 | * the write() is not allowed to block. It must write only the number of |
| 253 | * bytes that currently fit in the driver/hardware buffer and then return |
| 254 | * this byte count. If this is less than the requested write size the |
| 255 | * callback function must be called when more space is available in the |
| 256 | * driver/hardware buffer. |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 257 | */ |
| 258 | ssize_t (*write)(struct audio_stream_out *stream, const void* buffer, |
| 259 | size_t bytes); |
| 260 | |
| 261 | /* return the number of audio frames written by the audio dsp to DAC since |
| 262 | * the output has exited standby |
| 263 | */ |
| 264 | int (*get_render_position)(const struct audio_stream_out *stream, |
| 265 | uint32_t *dsp_frames); |
Mike J. Chen | 5ad38a9 | 2011-08-15 12:05:00 -0700 | [diff] [blame] | 266 | |
| 267 | /** |
Glenn Kasten | 0cacd8d | 2012-02-10 13:42:44 -0800 | [diff] [blame] | 268 | * get the local time at which the next write to the audio driver will be presented. |
| 269 | * The units are microseconds, where the epoch is decided by the local audio HAL. |
Mike J. Chen | 5ad38a9 | 2011-08-15 12:05:00 -0700 | [diff] [blame] | 270 | */ |
| 271 | int (*get_next_write_timestamp)(const struct audio_stream_out *stream, |
| 272 | int64_t *timestamp); |
| 273 | |
Richard Fitzgerald | f37f187 | 2013-03-25 16:11:44 +0000 | [diff] [blame] | 274 | /** |
| 275 | * set the callback function for notifying completion of non-blocking |
| 276 | * write and drain. |
| 277 | * Calling this function implies that all future write() and drain() |
| 278 | * must be non-blocking and use the callback to signal completion. |
| 279 | */ |
| 280 | int (*set_callback)(struct audio_stream_out *stream, |
| 281 | stream_callback_t callback, void *cookie); |
| 282 | |
| 283 | /** |
| 284 | * Notifies to the audio driver to stop playback however the queued buffers are |
| 285 | * retained by the hardware. Useful for implementing pause/resume. Empty implementation |
| 286 | * if not supported however should be implemented for hardware with non-trivial |
| 287 | * latency. In the pause state audio hardware could still be using power. User may |
| 288 | * consider calling suspend after a timeout. |
| 289 | * |
| 290 | * Implementation of this function is mandatory for offloaded playback. |
| 291 | */ |
| 292 | int (*pause)(struct audio_stream_out* stream); |
| 293 | |
| 294 | /** |
| 295 | * Notifies to the audio driver to resume playback following a pause. |
| 296 | * Returns error if called without matching pause. |
| 297 | * |
| 298 | * Implementation of this function is mandatory for offloaded playback. |
| 299 | */ |
| 300 | int (*resume)(struct audio_stream_out* stream); |
| 301 | |
| 302 | /** |
| 303 | * Requests notification when data buffered by the driver/hardware has |
| 304 | * been played. If set_callback() has previously been called to enable |
| 305 | * non-blocking mode, the drain() must not block, instead it should return |
| 306 | * quickly and completion of the drain is notified through the callback. |
| 307 | * If set_callback() has not been called, the drain() must block until |
| 308 | * completion. |
| 309 | * If type==AUDIO_DRAIN_ALL, the drain completes when all previously written |
| 310 | * data has been played. |
| 311 | * If type==AUDIO_DRAIN_EARLY_NOTIFY, the drain completes shortly before all |
| 312 | * data for the current track has played to allow time for the framework |
| 313 | * to perform a gapless track switch. |
| 314 | * |
| 315 | * Drain must return immediately on stop() and flush() call |
| 316 | * |
| 317 | * Implementation of this function is mandatory for offloaded playback. |
| 318 | */ |
| 319 | int (*drain)(struct audio_stream_out* stream, audio_drain_type_t type ); |
| 320 | |
| 321 | /** |
| 322 | * Notifies to the audio driver to flush the queued data. Stream must already |
| 323 | * be paused before calling flush(). |
| 324 | * |
| 325 | * Implementation of this function is mandatory for offloaded playback. |
| 326 | */ |
| 327 | int (*flush)(struct audio_stream_out* stream); |
Glenn Kasten | e25f9ed | 2013-08-22 16:27:22 -0700 | [diff] [blame] | 328 | |
| 329 | /** |
Glenn Kasten | 22a06b7 | 2013-09-10 09:23:07 -0700 | [diff] [blame] | 330 | * Return a recent count of the number of audio frames presented to an external observer. |
Glenn Kasten | e25f9ed | 2013-08-22 16:27:22 -0700 | [diff] [blame] | 331 | * This excludes frames which have been written but are still in the pipeline. |
| 332 | * The count is not reset to zero when output enters standby. |
| 333 | * Also returns the value of CLOCK_MONOTONIC as of this presentation count. |
Glenn Kasten | 22a06b7 | 2013-09-10 09:23:07 -0700 | [diff] [blame] | 334 | * The returned count is expected to be 'recent', |
| 335 | * but does not need to be the most recent possible value. |
| 336 | * However, the associated time should correspond to whatever count is returned. |
| 337 | * Example: assume that N+M frames have been presented, where M is a 'small' number. |
| 338 | * Then it is permissible to return N instead of N+M, |
| 339 | * and the timestamp should correspond to N rather than N+M. |
| 340 | * The terms 'recent' and 'small' are not defined. |
| 341 | * They reflect the quality of the implementation. |
Glenn Kasten | e25f9ed | 2013-08-22 16:27:22 -0700 | [diff] [blame] | 342 | * |
| 343 | * 3.0 and higher only. |
| 344 | */ |
| 345 | int (*get_presentation_position)(const struct audio_stream_out *stream, |
| 346 | uint64_t *frames, struct timespec *timestamp); |
| 347 | |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 348 | }; |
| 349 | typedef struct audio_stream_out audio_stream_out_t; |
| 350 | |
| 351 | struct audio_stream_in { |
Stewart Miles | 84d3549 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 352 | /** |
| 353 | * Common methods of the audio stream in. This *must* be the first member of audio_stream_in |
| 354 | * as users of this structure will cast a audio_stream to audio_stream_in pointer in contexts |
| 355 | * where it's known the audio_stream references an audio_stream_in. |
| 356 | */ |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 357 | struct audio_stream common; |
| 358 | |
| 359 | /** set the input gain for the audio driver. This method is for |
| 360 | * for future use */ |
| 361 | int (*set_gain)(struct audio_stream_in *stream, float gain); |
| 362 | |
Glenn Kasten | 0cacd8d | 2012-02-10 13:42:44 -0800 | [diff] [blame] | 363 | /** Read audio buffer in from audio driver. Returns number of bytes read, or a |
| 364 | * negative status_t. If at least one frame was read prior to the error, |
| 365 | * read should return that byte count and then return an error in the subsequent call. |
| 366 | */ |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 367 | ssize_t (*read)(struct audio_stream_in *stream, void* buffer, |
| 368 | size_t bytes); |
| 369 | |
| 370 | /** |
| 371 | * Return the amount of input frames lost in the audio driver since the |
| 372 | * last call of this function. |
| 373 | * Audio driver is expected to reset the value to 0 and restart counting |
| 374 | * upon returning the current value by this function call. |
| 375 | * Such loss typically occurs when the user space process is blocked |
| 376 | * longer than the capacity of audio driver buffers. |
| 377 | * |
| 378 | * Unit: the number of input audio frames |
| 379 | */ |
| 380 | uint32_t (*get_input_frames_lost)(struct audio_stream_in *stream); |
Andy Hung | 9904fab | 2016-01-15 17:42:36 -0800 | [diff] [blame] | 381 | |
| 382 | /** |
| 383 | * Return a recent count of the number of audio frames received and |
| 384 | * the clock time associated with that frame count. |
| 385 | * |
| 386 | * frames is the total frame count received. This should be as early in |
| 387 | * the capture pipeline as possible. In general, |
| 388 | * frames should be non-negative and should not go "backwards". |
| 389 | * |
| 390 | * time is the clock MONOTONIC time when frames was measured. In general, |
| 391 | * time should be a positive quantity and should not go "backwards". |
| 392 | * |
| 393 | * The status returned is 0 on success, -ENOSYS if the device is not |
| 394 | * ready/available, or -EINVAL if the arguments are null or otherwise invalid. |
| 395 | */ |
| 396 | int (*get_capture_position)(const struct audio_stream_in *stream, |
| 397 | int64_t *frames, int64_t *time); |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 398 | }; |
| 399 | typedef struct audio_stream_in audio_stream_in_t; |
| 400 | |
| 401 | /** |
| 402 | * return the frame size (number of bytes per sample). |
Eric Laurent | c5ae6a0 | 2014-07-02 13:45:32 -0700 | [diff] [blame] | 403 | * |
| 404 | * Deprecated: use audio_stream_out_frame_size() or audio_stream_in_frame_size() instead. |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 405 | */ |
Eric Laurent | c5ae6a0 | 2014-07-02 13:45:32 -0700 | [diff] [blame] | 406 | __attribute__((__deprecated__)) |
Glenn Kasten | 48915ac | 2012-02-20 12:08:57 -0800 | [diff] [blame] | 407 | static inline size_t audio_stream_frame_size(const struct audio_stream *s) |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 408 | { |
Glenn Kasten | a26cbac | 2012-01-13 14:53:35 -0800 | [diff] [blame] | 409 | size_t chan_samp_sz; |
Richard Fitzgerald | f37f187 | 2013-03-25 16:11:44 +0000 | [diff] [blame] | 410 | audio_format_t format = s->get_format(s); |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 411 | |
Phil Burk | c3385fc | 2016-01-19 12:21:55 -0800 | [diff] [blame] | 412 | if (audio_has_proportional_frames(format)) { |
Richard Fitzgerald | f37f187 | 2013-03-25 16:11:44 +0000 | [diff] [blame] | 413 | chan_samp_sz = audio_bytes_per_sample(format); |
| 414 | return popcount(s->get_channels(s)) * chan_samp_sz; |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 415 | } |
| 416 | |
Richard Fitzgerald | f37f187 | 2013-03-25 16:11:44 +0000 | [diff] [blame] | 417 | return sizeof(int8_t); |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 418 | } |
| 419 | |
Eric Laurent | c5ae6a0 | 2014-07-02 13:45:32 -0700 | [diff] [blame] | 420 | /** |
| 421 | * return the frame size (number of bytes per sample) of an output stream. |
| 422 | */ |
| 423 | static inline size_t audio_stream_out_frame_size(const struct audio_stream_out *s) |
| 424 | { |
| 425 | size_t chan_samp_sz; |
| 426 | audio_format_t format = s->common.get_format(&s->common); |
| 427 | |
Phil Burk | c3385fc | 2016-01-19 12:21:55 -0800 | [diff] [blame] | 428 | if (audio_has_proportional_frames(format)) { |
Eric Laurent | c5ae6a0 | 2014-07-02 13:45:32 -0700 | [diff] [blame] | 429 | chan_samp_sz = audio_bytes_per_sample(format); |
| 430 | return audio_channel_count_from_out_mask(s->common.get_channels(&s->common)) * chan_samp_sz; |
| 431 | } |
| 432 | |
| 433 | return sizeof(int8_t); |
| 434 | } |
| 435 | |
| 436 | /** |
| 437 | * return the frame size (number of bytes per sample) of an input stream. |
| 438 | */ |
| 439 | static inline size_t audio_stream_in_frame_size(const struct audio_stream_in *s) |
| 440 | { |
| 441 | size_t chan_samp_sz; |
| 442 | audio_format_t format = s->common.get_format(&s->common); |
| 443 | |
Phil Burk | c3385fc | 2016-01-19 12:21:55 -0800 | [diff] [blame] | 444 | if (audio_has_proportional_frames(format)) { |
Eric Laurent | c5ae6a0 | 2014-07-02 13:45:32 -0700 | [diff] [blame] | 445 | chan_samp_sz = audio_bytes_per_sample(format); |
| 446 | return audio_channel_count_from_in_mask(s->common.get_channels(&s->common)) * chan_samp_sz; |
| 447 | } |
| 448 | |
| 449 | return sizeof(int8_t); |
| 450 | } |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 451 | |
| 452 | /**********************************************************************/ |
| 453 | |
| 454 | /** |
| 455 | * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM |
| 456 | * and the fields of this data structure must begin with hw_module_t |
| 457 | * followed by module specific information. |
| 458 | */ |
| 459 | struct audio_module { |
| 460 | struct hw_module_t common; |
| 461 | }; |
| 462 | |
| 463 | struct audio_hw_device { |
Stewart Miles | 84d3549 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 464 | /** |
| 465 | * Common methods of the audio device. This *must* be the first member of audio_hw_device |
| 466 | * as users of this structure will cast a hw_device_t to audio_hw_device pointer in contexts |
| 467 | * where it's known the hw_device_t references an audio_hw_device. |
| 468 | */ |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 469 | struct hw_device_t common; |
| 470 | |
| 471 | /** |
| 472 | * used by audio flinger to enumerate what devices are supported by |
| 473 | * each audio_hw_device implementation. |
| 474 | * |
| 475 | * Return value is a bitmask of 1 or more values of audio_devices_t |
Eric Laurent | 85e08e2 | 2012-08-28 14:30:35 -0700 | [diff] [blame] | 476 | * |
| 477 | * NOTE: audio HAL implementations starting with |
| 478 | * AUDIO_DEVICE_API_VERSION_2_0 do not implement this function. |
| 479 | * All supported devices should be listed in audio_policy.conf |
| 480 | * file and the audio policy manager must choose the appropriate |
| 481 | * audio module based on information in this file. |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 482 | */ |
| 483 | uint32_t (*get_supported_devices)(const struct audio_hw_device *dev); |
| 484 | |
| 485 | /** |
| 486 | * check to see if the audio hardware interface has been initialized. |
| 487 | * returns 0 on success, -ENODEV on failure. |
| 488 | */ |
| 489 | int (*init_check)(const struct audio_hw_device *dev); |
| 490 | |
| 491 | /** set the audio volume of a voice call. Range is between 0.0 and 1.0 */ |
| 492 | int (*set_voice_volume)(struct audio_hw_device *dev, float volume); |
| 493 | |
| 494 | /** |
| 495 | * set the audio volume for all audio activities other than voice call. |
| 496 | * Range between 0.0 and 1.0. If any value other than 0 is returned, |
| 497 | * the software mixer will emulate this capability. |
| 498 | */ |
| 499 | int (*set_master_volume)(struct audio_hw_device *dev, float volume); |
| 500 | |
| 501 | /** |
Mike J. Chen | 5ad38a9 | 2011-08-15 12:05:00 -0700 | [diff] [blame] | 502 | * Get the current master volume value for the HAL, if the HAL supports |
| 503 | * master volume control. AudioFlinger will query this value from the |
| 504 | * primary audio HAL when the service starts and use the value for setting |
| 505 | * the initial master volume across all HALs. HALs which do not support |
John Grossman | 47bf3d7 | 2012-07-17 11:54:04 -0700 | [diff] [blame] | 506 | * this method may leave it set to NULL. |
Mike J. Chen | 5ad38a9 | 2011-08-15 12:05:00 -0700 | [diff] [blame] | 507 | */ |
| 508 | int (*get_master_volume)(struct audio_hw_device *dev, float *volume); |
| 509 | |
| 510 | /** |
Glenn Kasten | 6df641e | 2012-01-09 10:41:30 -0800 | [diff] [blame] | 511 | * set_mode is called when the audio mode changes. AUDIO_MODE_NORMAL mode |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 512 | * is for standard audio playback, AUDIO_MODE_RINGTONE when a ringtone is |
| 513 | * playing, and AUDIO_MODE_IN_CALL when a call is in progress. |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 514 | */ |
Glenn Kasten | 6df641e | 2012-01-09 10:41:30 -0800 | [diff] [blame] | 515 | int (*set_mode)(struct audio_hw_device *dev, audio_mode_t mode); |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 516 | |
| 517 | /* mic mute */ |
| 518 | int (*set_mic_mute)(struct audio_hw_device *dev, bool state); |
| 519 | int (*get_mic_mute)(const struct audio_hw_device *dev, bool *state); |
| 520 | |
| 521 | /* set/get global audio parameters */ |
| 522 | int (*set_parameters)(struct audio_hw_device *dev, const char *kv_pairs); |
| 523 | |
| 524 | /* |
| 525 | * Returns a pointer to a heap allocated string. The caller is responsible |
Glenn Kasten | 0cacd8d | 2012-02-10 13:42:44 -0800 | [diff] [blame] | 526 | * for freeing the memory for it using free(). |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 527 | */ |
| 528 | char * (*get_parameters)(const struct audio_hw_device *dev, |
| 529 | const char *keys); |
| 530 | |
| 531 | /* Returns audio input buffer size according to parameters passed or |
Glenn Kasten | 0cacd8d | 2012-02-10 13:42:44 -0800 | [diff] [blame] | 532 | * 0 if one of the parameters is not supported. |
| 533 | * See also get_buffer_size which is for a particular stream. |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 534 | */ |
| 535 | size_t (*get_input_buffer_size)(const struct audio_hw_device *dev, |
Eric Laurent | 55786bc | 2012-04-10 16:56:32 -0700 | [diff] [blame] | 536 | const struct audio_config *config); |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 537 | |
Eric Laurent | f5e2469 | 2014-07-27 16:14:57 -0700 | [diff] [blame] | 538 | /** This method creates and opens the audio hardware output stream. |
| 539 | * The "address" parameter qualifies the "devices" audio device type if needed. |
| 540 | * The format format depends on the device type: |
| 541 | * - Bluetooth devices use the MAC address of the device in the form "00:11:22:AA:BB:CC" |
| 542 | * - USB devices use the ALSA card and device numbers in the form "card=X;device=Y" |
| 543 | * - Other devices may use a number or any other string. |
| 544 | */ |
| 545 | |
Eric Laurent | 55786bc | 2012-04-10 16:56:32 -0700 | [diff] [blame] | 546 | int (*open_output_stream)(struct audio_hw_device *dev, |
| 547 | audio_io_handle_t handle, |
| 548 | audio_devices_t devices, |
| 549 | audio_output_flags_t flags, |
| 550 | struct audio_config *config, |
Eric Laurent | f5e2469 | 2014-07-27 16:14:57 -0700 | [diff] [blame] | 551 | struct audio_stream_out **stream_out, |
| 552 | const char *address); |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 553 | |
| 554 | void (*close_output_stream)(struct audio_hw_device *dev, |
Eric Laurent | 55786bc | 2012-04-10 16:56:32 -0700 | [diff] [blame] | 555 | struct audio_stream_out* stream_out); |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 556 | |
| 557 | /** This method creates and opens the audio hardware input stream */ |
Eric Laurent | 55786bc | 2012-04-10 16:56:32 -0700 | [diff] [blame] | 558 | int (*open_input_stream)(struct audio_hw_device *dev, |
| 559 | audio_io_handle_t handle, |
| 560 | audio_devices_t devices, |
| 561 | struct audio_config *config, |
Glenn Kasten | 7d973ad | 2014-07-15 11:10:38 -0700 | [diff] [blame] | 562 | struct audio_stream_in **stream_in, |
Eric Laurent | f5e2469 | 2014-07-27 16:14:57 -0700 | [diff] [blame] | 563 | audio_input_flags_t flags, |
| 564 | const char *address, |
| 565 | audio_source_t source); |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 566 | |
| 567 | void (*close_input_stream)(struct audio_hw_device *dev, |
Eric Laurent | 55786bc | 2012-04-10 16:56:32 -0700 | [diff] [blame] | 568 | struct audio_stream_in *stream_in); |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 569 | |
| 570 | /** This method dumps the state of the audio hardware */ |
| 571 | int (*dump)(const struct audio_hw_device *dev, int fd); |
John Grossman | 47bf3d7 | 2012-07-17 11:54:04 -0700 | [diff] [blame] | 572 | |
| 573 | /** |
| 574 | * set the audio mute status for all audio activities. If any value other |
| 575 | * than 0 is returned, the software mixer will emulate this capability. |
| 576 | */ |
| 577 | int (*set_master_mute)(struct audio_hw_device *dev, bool mute); |
| 578 | |
| 579 | /** |
| 580 | * Get the current master mute status for the HAL, if the HAL supports |
| 581 | * master mute control. AudioFlinger will query this value from the primary |
| 582 | * audio HAL when the service starts and use the value for setting the |
| 583 | * initial master mute across all HALs. HALs which do not support this |
| 584 | * method may leave it set to NULL. |
| 585 | */ |
| 586 | int (*get_master_mute)(struct audio_hw_device *dev, bool *mute); |
Eric Laurent | 73b8a74 | 2014-05-22 14:02:38 -0700 | [diff] [blame] | 587 | |
| 588 | /** |
| 589 | * Routing control |
| 590 | */ |
| 591 | |
| 592 | /* Creates an audio patch between several source and sink ports. |
| 593 | * The handle is allocated by the HAL and should be unique for this |
| 594 | * audio HAL module. */ |
| 595 | int (*create_audio_patch)(struct audio_hw_device *dev, |
| 596 | unsigned int num_sources, |
| 597 | const struct audio_port_config *sources, |
| 598 | unsigned int num_sinks, |
| 599 | const struct audio_port_config *sinks, |
| 600 | audio_patch_handle_t *handle); |
| 601 | |
| 602 | /* Release an audio patch */ |
| 603 | int (*release_audio_patch)(struct audio_hw_device *dev, |
| 604 | audio_patch_handle_t handle); |
| 605 | |
| 606 | /* Fills the list of supported attributes for a given audio port. |
| 607 | * As input, "port" contains the information (type, role, address etc...) |
| 608 | * needed by the HAL to identify the port. |
| 609 | * As output, "port" contains possible attributes (sampling rates, formats, |
| 610 | * channel masks, gain controllers...) for this port. |
| 611 | */ |
| 612 | int (*get_audio_port)(struct audio_hw_device *dev, |
| 613 | struct audio_port *port); |
| 614 | |
| 615 | /* Set audio port configuration */ |
| 616 | int (*set_audio_port_config)(struct audio_hw_device *dev, |
| 617 | const struct audio_port_config *config); |
| 618 | |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 619 | }; |
| 620 | typedef struct audio_hw_device audio_hw_device_t; |
| 621 | |
| 622 | /** convenience API for opening and closing a supported device */ |
| 623 | |
| 624 | static inline int audio_hw_device_open(const struct hw_module_t* module, |
| 625 | struct audio_hw_device** device) |
| 626 | { |
| 627 | return module->methods->open(module, AUDIO_HARDWARE_INTERFACE, |
Colin Cross | cc8d9f9 | 2016-10-06 16:44:23 -0700 | [diff] [blame] | 628 | TO_HW_DEVICE_T_OPEN(device)); |
Dima Zavin | f1504db | 2011-03-11 11:20:49 -0800 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | static inline int audio_hw_device_close(struct audio_hw_device* device) |
| 632 | { |
| 633 | return device->common.close(&device->common); |
| 634 | } |
| 635 | |
| 636 | |
| 637 | __END_DECLS |
| 638 | |
| 639 | #endif // ANDROID_AUDIO_INTERFACE_H |