Kevin Rocard | c6ec948 | 2018-01-24 06:04:27 +0000 | [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 | #include <time.h> |
| 26 | |
| 27 | #include <cutils/bitops.h> |
| 28 | |
| 29 | #include <hardware/hardware.h> |
| 30 | #include <system/audio.h> |
| 31 | #include <hardware/audio_effect.h> |
| 32 | |
| 33 | __BEGIN_DECLS |
| 34 | |
| 35 | /** |
| 36 | * The id of this module |
| 37 | */ |
| 38 | #define AUDIO_HARDWARE_MODULE_ID "audio" |
| 39 | |
| 40 | /** |
| 41 | * Name of the audio devices to open |
| 42 | */ |
| 43 | #define AUDIO_HARDWARE_INTERFACE "audio_hw_if" |
| 44 | |
| 45 | |
| 46 | /* Use version 0.1 to be compatible with first generation of audio hw module with version_major |
| 47 | * hardcoded to 1. No audio module API change. |
| 48 | */ |
| 49 | #define AUDIO_MODULE_API_VERSION_0_1 HARDWARE_MODULE_API_VERSION(0, 1) |
| 50 | #define AUDIO_MODULE_API_VERSION_CURRENT AUDIO_MODULE_API_VERSION_0_1 |
| 51 | |
| 52 | /* First generation of audio devices had version hardcoded to 0. all devices with versions < 1.0 |
| 53 | * will be considered of first generation API. |
| 54 | */ |
| 55 | #define AUDIO_DEVICE_API_VERSION_0_0 HARDWARE_DEVICE_API_VERSION(0, 0) |
| 56 | #define AUDIO_DEVICE_API_VERSION_1_0 HARDWARE_DEVICE_API_VERSION(1, 0) |
| 57 | #define AUDIO_DEVICE_API_VERSION_2_0 HARDWARE_DEVICE_API_VERSION(2, 0) |
| 58 | #define AUDIO_DEVICE_API_VERSION_3_0 HARDWARE_DEVICE_API_VERSION(3, 0) |
| 59 | #define AUDIO_DEVICE_API_VERSION_CURRENT AUDIO_DEVICE_API_VERSION_3_0 |
| 60 | /* Minimal audio HAL version supported by the audio framework */ |
| 61 | #define AUDIO_DEVICE_API_VERSION_MIN AUDIO_DEVICE_API_VERSION_2_0 |
| 62 | |
| 63 | /**************************************/ |
| 64 | |
| 65 | /** |
| 66 | * standard audio parameters that the HAL may need to handle |
| 67 | */ |
| 68 | |
| 69 | /** |
| 70 | * audio device parameters |
| 71 | */ |
| 72 | |
| 73 | /* TTY mode selection */ |
| 74 | #define AUDIO_PARAMETER_KEY_TTY_MODE "tty_mode" |
| 75 | #define AUDIO_PARAMETER_VALUE_TTY_OFF "tty_off" |
| 76 | #define AUDIO_PARAMETER_VALUE_TTY_VCO "tty_vco" |
| 77 | #define AUDIO_PARAMETER_VALUE_TTY_HCO "tty_hco" |
| 78 | #define AUDIO_PARAMETER_VALUE_TTY_FULL "tty_full" |
| 79 | |
| 80 | /* Hearing Aid Compatibility - Telecoil (HAC-T) mode on/off */ |
| 81 | #define AUDIO_PARAMETER_KEY_HAC "HACSetting" |
| 82 | #define AUDIO_PARAMETER_VALUE_HAC_ON "ON" |
| 83 | #define AUDIO_PARAMETER_VALUE_HAC_OFF "OFF" |
| 84 | |
| 85 | /* A2DP sink address set by framework */ |
| 86 | #define AUDIO_PARAMETER_A2DP_SINK_ADDRESS "a2dp_sink_address" |
| 87 | |
| 88 | /* A2DP source address set by framework */ |
| 89 | #define AUDIO_PARAMETER_A2DP_SOURCE_ADDRESS "a2dp_source_address" |
| 90 | |
| 91 | /* Bluetooth SCO wideband */ |
| 92 | #define AUDIO_PARAMETER_KEY_BT_SCO_WB "bt_wbs" |
| 93 | |
Kevin Rocard | d55a49a | 2018-03-02 12:46:57 -0800 | [diff] [blame] | 94 | /* BT SCO headset name for debug */ |
| 95 | #define AUDIO_PARAMETER_KEY_BT_SCO_HEADSET_NAME "bt_headset_name" |
| 96 | |
| 97 | /* BT SCO HFP control */ |
| 98 | #define AUDIO_PARAMETER_KEY_HFP_ENABLE "hfp_enable" |
| 99 | #define AUDIO_PARAMETER_KEY_HFP_SET_SAMPLING_RATE "hfp_set_sampling_rate" |
| 100 | #define AUDIO_PARAMETER_KEY_HFP_VOLUME "hfp_volume" |
| 101 | |
| 102 | /* Set screen orientation */ |
| 103 | #define AUDIO_PARAMETER_KEY_ROTATION "rotation" |
| 104 | |
Kevin Rocard | c6ec948 | 2018-01-24 06:04:27 +0000 | [diff] [blame] | 105 | /** |
| 106 | * audio stream parameters |
| 107 | */ |
| 108 | |
| 109 | /* Enable AANC */ |
| 110 | #define AUDIO_PARAMETER_KEY_AANC "aanc_enabled" |
| 111 | |
| 112 | /**************************************/ |
| 113 | |
| 114 | /* common audio stream parameters and operations */ |
| 115 | struct audio_stream { |
| 116 | |
| 117 | /** |
| 118 | * Return the sampling rate in Hz - eg. 44100. |
| 119 | */ |
| 120 | uint32_t (*get_sample_rate)(const struct audio_stream *stream); |
| 121 | |
| 122 | /* currently unused - use set_parameters with key |
| 123 | * AUDIO_PARAMETER_STREAM_SAMPLING_RATE |
| 124 | */ |
| 125 | int (*set_sample_rate)(struct audio_stream *stream, uint32_t rate); |
| 126 | |
| 127 | /** |
| 128 | * Return size of input/output buffer in bytes for this stream - eg. 4800. |
| 129 | * It should be a multiple of the frame size. See also get_input_buffer_size. |
| 130 | */ |
| 131 | size_t (*get_buffer_size)(const struct audio_stream *stream); |
| 132 | |
| 133 | /** |
| 134 | * Return the channel mask - |
| 135 | * e.g. AUDIO_CHANNEL_OUT_STEREO or AUDIO_CHANNEL_IN_STEREO |
| 136 | */ |
| 137 | audio_channel_mask_t (*get_channels)(const struct audio_stream *stream); |
| 138 | |
| 139 | /** |
| 140 | * Return the audio format - e.g. AUDIO_FORMAT_PCM_16_BIT |
| 141 | */ |
| 142 | audio_format_t (*get_format)(const struct audio_stream *stream); |
| 143 | |
| 144 | /* currently unused - use set_parameters with key |
| 145 | * AUDIO_PARAMETER_STREAM_FORMAT |
| 146 | */ |
| 147 | int (*set_format)(struct audio_stream *stream, audio_format_t format); |
| 148 | |
| 149 | /** |
| 150 | * Put the audio hardware input/output into standby mode. |
| 151 | * Driver should exit from standby mode at the next I/O operation. |
| 152 | * Returns 0 on success and <0 on failure. |
| 153 | */ |
| 154 | int (*standby)(struct audio_stream *stream); |
| 155 | |
| 156 | /** dump the state of the audio input/output device */ |
| 157 | int (*dump)(const struct audio_stream *stream, int fd); |
| 158 | |
| 159 | /** Return the set of device(s) which this stream is connected to */ |
| 160 | audio_devices_t (*get_device)(const struct audio_stream *stream); |
| 161 | |
| 162 | /** |
| 163 | * Currently unused - set_device() corresponds to set_parameters() with key |
| 164 | * AUDIO_PARAMETER_STREAM_ROUTING for both input and output. |
| 165 | * AUDIO_PARAMETER_STREAM_INPUT_SOURCE is an additional information used by |
| 166 | * input streams only. |
| 167 | */ |
| 168 | int (*set_device)(struct audio_stream *stream, audio_devices_t device); |
| 169 | |
| 170 | /** |
| 171 | * set/get audio stream parameters. The function accepts a list of |
| 172 | * parameter key value pairs in the form: key1=value1;key2=value2;... |
| 173 | * |
| 174 | * Some keys are reserved for standard parameters (See AudioParameter class) |
| 175 | * |
| 176 | * If the implementation does not accept a parameter change while |
| 177 | * the output is active but the parameter is acceptable otherwise, it must |
| 178 | * return -ENOSYS. |
| 179 | * |
| 180 | * The audio flinger will put the stream in standby and then change the |
| 181 | * parameter value. |
| 182 | */ |
| 183 | int (*set_parameters)(struct audio_stream *stream, const char *kv_pairs); |
| 184 | |
| 185 | /* |
| 186 | * Returns a pointer to a heap allocated string. The caller is responsible |
| 187 | * for freeing the memory for it using free(). |
| 188 | */ |
| 189 | char * (*get_parameters)(const struct audio_stream *stream, |
| 190 | const char *keys); |
| 191 | int (*add_audio_effect)(const struct audio_stream *stream, |
| 192 | effect_handle_t effect); |
| 193 | int (*remove_audio_effect)(const struct audio_stream *stream, |
| 194 | effect_handle_t effect); |
| 195 | }; |
| 196 | typedef struct audio_stream audio_stream_t; |
| 197 | |
| 198 | /* type of asynchronous write callback events. Mutually exclusive */ |
| 199 | typedef enum { |
| 200 | STREAM_CBK_EVENT_WRITE_READY, /* non blocking write completed */ |
| 201 | STREAM_CBK_EVENT_DRAIN_READY, /* drain completed */ |
| 202 | STREAM_CBK_EVENT_ERROR, /* stream hit some error, let AF take action */ |
| 203 | } stream_callback_event_t; |
| 204 | |
| 205 | typedef int (*stream_callback_t)(stream_callback_event_t event, void *param, void *cookie); |
| 206 | |
| 207 | /* type of drain requested to audio_stream_out->drain(). Mutually exclusive */ |
| 208 | typedef enum { |
| 209 | AUDIO_DRAIN_ALL, /* drain() returns when all data has been played */ |
| 210 | AUDIO_DRAIN_EARLY_NOTIFY /* drain() returns a short time before all data |
| 211 | from the current track has been played to |
| 212 | give time for gapless track switch */ |
| 213 | } audio_drain_type_t; |
| 214 | |
| 215 | /** |
| 216 | * audio_stream_out is the abstraction interface for the audio output hardware. |
| 217 | * |
| 218 | * It provides information about various properties of the audio output |
| 219 | * hardware driver. |
| 220 | */ |
| 221 | |
| 222 | struct audio_stream_out { |
| 223 | /** |
| 224 | * Common methods of the audio stream out. This *must* be the first member of audio_stream_out |
| 225 | * as users of this structure will cast a audio_stream to audio_stream_out pointer in contexts |
| 226 | * where it's known the audio_stream references an audio_stream_out. |
| 227 | */ |
| 228 | struct audio_stream common; |
| 229 | |
| 230 | /** |
| 231 | * Return the audio hardware driver estimated latency in milliseconds. |
| 232 | */ |
| 233 | uint32_t (*get_latency)(const struct audio_stream_out *stream); |
| 234 | |
| 235 | /** |
| 236 | * Use this method in situations where audio mixing is done in the |
| 237 | * hardware. This method serves as a direct interface with hardware, |
| 238 | * allowing you to directly set the volume as apposed to via the framework. |
| 239 | * This method might produce multiple PCM outputs or hardware accelerated |
| 240 | * codecs, such as MP3 or AAC. |
| 241 | */ |
| 242 | int (*set_volume)(struct audio_stream_out *stream, float left, float right); |
| 243 | |
| 244 | /** |
| 245 | * Write audio buffer to driver. Returns number of bytes written, or a |
| 246 | * negative status_t. If at least one frame was written successfully prior to the error, |
| 247 | * it is suggested that the driver return that successful (short) byte count |
| 248 | * and then return an error in the subsequent call. |
| 249 | * |
| 250 | * If set_callback() has previously been called to enable non-blocking mode |
| 251 | * the write() is not allowed to block. It must write only the number of |
| 252 | * bytes that currently fit in the driver/hardware buffer and then return |
| 253 | * this byte count. If this is less than the requested write size the |
| 254 | * callback function must be called when more space is available in the |
| 255 | * driver/hardware buffer. |
| 256 | */ |
| 257 | ssize_t (*write)(struct audio_stream_out *stream, const void* buffer, |
| 258 | size_t bytes); |
| 259 | |
| 260 | /* return the number of audio frames written by the audio dsp to DAC since |
| 261 | * the output has exited standby |
| 262 | */ |
| 263 | int (*get_render_position)(const struct audio_stream_out *stream, |
| 264 | uint32_t *dsp_frames); |
| 265 | |
| 266 | /** |
| 267 | * get the local time at which the next write to the audio driver will be presented. |
| 268 | * The units are microseconds, where the epoch is decided by the local audio HAL. |
| 269 | */ |
| 270 | int (*get_next_write_timestamp)(const struct audio_stream_out *stream, |
| 271 | int64_t *timestamp); |
| 272 | |
| 273 | /** |
| 274 | * set the callback function for notifying completion of non-blocking |
| 275 | * write and drain. |
| 276 | * Calling this function implies that all future write() and drain() |
| 277 | * must be non-blocking and use the callback to signal completion. |
| 278 | */ |
| 279 | int (*set_callback)(struct audio_stream_out *stream, |
| 280 | stream_callback_t callback, void *cookie); |
| 281 | |
| 282 | /** |
| 283 | * Notifies to the audio driver to stop playback however the queued buffers are |
| 284 | * retained by the hardware. Useful for implementing pause/resume. Empty implementation |
| 285 | * if not supported however should be implemented for hardware with non-trivial |
| 286 | * latency. In the pause state audio hardware could still be using power. User may |
| 287 | * consider calling suspend after a timeout. |
| 288 | * |
| 289 | * Implementation of this function is mandatory for offloaded playback. |
| 290 | */ |
| 291 | int (*pause)(struct audio_stream_out* stream); |
| 292 | |
| 293 | /** |
| 294 | * Notifies to the audio driver to resume playback following a pause. |
| 295 | * Returns error if called without matching pause. |
| 296 | * |
| 297 | * Implementation of this function is mandatory for offloaded playback. |
| 298 | */ |
| 299 | int (*resume)(struct audio_stream_out* stream); |
| 300 | |
| 301 | /** |
| 302 | * Requests notification when data buffered by the driver/hardware has |
| 303 | * been played. If set_callback() has previously been called to enable |
| 304 | * non-blocking mode, the drain() must not block, instead it should return |
| 305 | * quickly and completion of the drain is notified through the callback. |
| 306 | * If set_callback() has not been called, the drain() must block until |
| 307 | * completion. |
| 308 | * If type==AUDIO_DRAIN_ALL, the drain completes when all previously written |
| 309 | * data has been played. |
| 310 | * If type==AUDIO_DRAIN_EARLY_NOTIFY, the drain completes shortly before all |
| 311 | * data for the current track has played to allow time for the framework |
| 312 | * to perform a gapless track switch. |
| 313 | * |
| 314 | * Drain must return immediately on stop() and flush() call |
| 315 | * |
| 316 | * Implementation of this function is mandatory for offloaded playback. |
| 317 | */ |
| 318 | int (*drain)(struct audio_stream_out* stream, audio_drain_type_t type ); |
| 319 | |
| 320 | /** |
| 321 | * Notifies to the audio driver to flush the queued data. Stream must already |
| 322 | * be paused before calling flush(). |
| 323 | * |
| 324 | * Implementation of this function is mandatory for offloaded playback. |
| 325 | */ |
| 326 | int (*flush)(struct audio_stream_out* stream); |
| 327 | |
| 328 | /** |
| 329 | * Return a recent count of the number of audio frames presented to an external observer. |
| 330 | * This excludes frames which have been written but are still in the pipeline. |
| 331 | * The count is not reset to zero when output enters standby. |
| 332 | * Also returns the value of CLOCK_MONOTONIC as of this presentation count. |
| 333 | * The returned count is expected to be 'recent', |
| 334 | * but does not need to be the most recent possible value. |
| 335 | * However, the associated time should correspond to whatever count is returned. |
| 336 | * Example: assume that N+M frames have been presented, where M is a 'small' number. |
| 337 | * Then it is permissible to return N instead of N+M, |
| 338 | * and the timestamp should correspond to N rather than N+M. |
| 339 | * The terms 'recent' and 'small' are not defined. |
| 340 | * They reflect the quality of the implementation. |
| 341 | * |
| 342 | * 3.0 and higher only. |
| 343 | */ |
| 344 | int (*get_presentation_position)(const struct audio_stream_out *stream, |
| 345 | uint64_t *frames, struct timespec *timestamp); |
| 346 | |
| 347 | /** |
| 348 | * Called by the framework to start a stream operating in mmap mode. |
| 349 | * create_mmap_buffer must be called before calling start() |
| 350 | * |
| 351 | * \note Function only implemented by streams operating in mmap mode. |
| 352 | * |
| 353 | * \param[in] stream the stream object. |
| 354 | * \return 0 in case of success. |
| 355 | * -ENOSYS if called out of sequence or on non mmap stream |
| 356 | */ |
| 357 | int (*start)(const struct audio_stream_out* stream); |
| 358 | |
| 359 | /** |
| 360 | * Called by the framework to stop a stream operating in mmap mode. |
| 361 | * Must be called after start() |
| 362 | * |
| 363 | * \note Function only implemented by streams operating in mmap mode. |
| 364 | * |
| 365 | * \param[in] stream the stream object. |
| 366 | * \return 0 in case of success. |
| 367 | * -ENOSYS if called out of sequence or on non mmap stream |
| 368 | */ |
| 369 | int (*stop)(const struct audio_stream_out* stream); |
| 370 | |
| 371 | /** |
| 372 | * Called by the framework to retrieve information on the mmap buffer used for audio |
| 373 | * samples transfer. |
| 374 | * |
| 375 | * \note Function only implemented by streams operating in mmap mode. |
| 376 | * |
| 377 | * \param[in] stream the stream object. |
| 378 | * \param[in] min_size_frames minimum buffer size requested. The actual buffer |
| 379 | * size returned in struct audio_mmap_buffer_info can be larger. |
| 380 | * \param[out] info address at which the mmap buffer information should be returned. |
| 381 | * |
| 382 | * \return 0 if the buffer was allocated. |
| 383 | * -ENODEV in case of initialization error |
| 384 | * -EINVAL if the requested buffer size is too large |
| 385 | * -ENOSYS if called out of sequence (e.g. buffer already allocated) |
| 386 | */ |
| 387 | int (*create_mmap_buffer)(const struct audio_stream_out *stream, |
| 388 | int32_t min_size_frames, |
| 389 | struct audio_mmap_buffer_info *info); |
| 390 | |
| 391 | /** |
| 392 | * Called by the framework to read current read/write position in the mmap buffer |
| 393 | * with associated time stamp. |
| 394 | * |
| 395 | * \note Function only implemented by streams operating in mmap mode. |
| 396 | * |
| 397 | * \param[in] stream the stream object. |
| 398 | * \param[out] position address at which the mmap read/write position should be returned. |
| 399 | * |
| 400 | * \return 0 if the position is successfully returned. |
| 401 | * -ENODATA if the position cannot be retrieved |
| 402 | * -ENOSYS if called before create_mmap_buffer() |
| 403 | */ |
| 404 | int (*get_mmap_position)(const struct audio_stream_out *stream, |
| 405 | struct audio_mmap_position *position); |
| 406 | }; |
| 407 | typedef struct audio_stream_out audio_stream_out_t; |
| 408 | |
| 409 | struct audio_stream_in { |
| 410 | /** |
| 411 | * Common methods of the audio stream in. This *must* be the first member of audio_stream_in |
| 412 | * as users of this structure will cast a audio_stream to audio_stream_in pointer in contexts |
| 413 | * where it's known the audio_stream references an audio_stream_in. |
| 414 | */ |
| 415 | struct audio_stream common; |
| 416 | |
| 417 | /** set the input gain for the audio driver. This method is for |
| 418 | * for future use */ |
| 419 | int (*set_gain)(struct audio_stream_in *stream, float gain); |
| 420 | |
| 421 | /** Read audio buffer in from audio driver. Returns number of bytes read, or a |
| 422 | * negative status_t. If at least one frame was read prior to the error, |
| 423 | * read should return that byte count and then return an error in the subsequent call. |
| 424 | */ |
| 425 | ssize_t (*read)(struct audio_stream_in *stream, void* buffer, |
| 426 | size_t bytes); |
| 427 | |
| 428 | /** |
| 429 | * Return the amount of input frames lost in the audio driver since the |
| 430 | * last call of this function. |
| 431 | * Audio driver is expected to reset the value to 0 and restart counting |
| 432 | * upon returning the current value by this function call. |
| 433 | * Such loss typically occurs when the user space process is blocked |
| 434 | * longer than the capacity of audio driver buffers. |
| 435 | * |
| 436 | * Unit: the number of input audio frames |
| 437 | */ |
| 438 | uint32_t (*get_input_frames_lost)(struct audio_stream_in *stream); |
| 439 | |
| 440 | /** |
| 441 | * Return a recent count of the number of audio frames received and |
| 442 | * the clock time associated with that frame count. |
| 443 | * |
| 444 | * frames is the total frame count received. This should be as early in |
| 445 | * the capture pipeline as possible. In general, |
| 446 | * frames should be non-negative and should not go "backwards". |
| 447 | * |
| 448 | * time is the clock MONOTONIC time when frames was measured. In general, |
| 449 | * time should be a positive quantity and should not go "backwards". |
| 450 | * |
| 451 | * The status returned is 0 on success, -ENOSYS if the device is not |
| 452 | * ready/available, or -EINVAL if the arguments are null or otherwise invalid. |
| 453 | */ |
| 454 | int (*get_capture_position)(const struct audio_stream_in *stream, |
| 455 | int64_t *frames, int64_t *time); |
| 456 | |
| 457 | /** |
| 458 | * Called by the framework to start a stream operating in mmap mode. |
| 459 | * create_mmap_buffer must be called before calling start() |
| 460 | * |
| 461 | * \note Function only implemented by streams operating in mmap mode. |
| 462 | * |
| 463 | * \param[in] stream the stream object. |
| 464 | * \return 0 in case off success. |
| 465 | * -ENOSYS if called out of sequence or on non mmap stream |
| 466 | */ |
| 467 | int (*start)(const struct audio_stream_in* stream); |
| 468 | |
| 469 | /** |
| 470 | * Called by the framework to stop a stream operating in mmap mode. |
| 471 | * |
| 472 | * \note Function only implemented by streams operating in mmap mode. |
| 473 | * |
| 474 | * \param[in] stream the stream object. |
| 475 | * \return 0 in case of success. |
| 476 | * -ENOSYS if called out of sequence or on non mmap stream |
| 477 | */ |
| 478 | int (*stop)(const struct audio_stream_in* stream); |
| 479 | |
| 480 | /** |
| 481 | * Called by the framework to retrieve information on the mmap buffer used for audio |
| 482 | * samples transfer. |
| 483 | * |
| 484 | * \note Function only implemented by streams operating in mmap mode. |
| 485 | * |
| 486 | * \param[in] stream the stream object. |
| 487 | * \param[in] min_size_frames minimum buffer size requested. The actual buffer |
| 488 | * size returned in struct audio_mmap_buffer_info can be larger. |
| 489 | * \param[out] info address at which the mmap buffer information should be returned. |
| 490 | * |
| 491 | * \return 0 if the buffer was allocated. |
| 492 | * -ENODEV in case of initialization error |
| 493 | * -EINVAL if the requested buffer size is too large |
| 494 | * -ENOSYS if called out of sequence (e.g. buffer already allocated) |
| 495 | */ |
| 496 | int (*create_mmap_buffer)(const struct audio_stream_in *stream, |
| 497 | int32_t min_size_frames, |
| 498 | struct audio_mmap_buffer_info *info); |
| 499 | |
| 500 | /** |
| 501 | * Called by the framework to read current read/write position in the mmap buffer |
| 502 | * with associated time stamp. |
| 503 | * |
| 504 | * \note Function only implemented by streams operating in mmap mode. |
| 505 | * |
| 506 | * \param[in] stream the stream object. |
| 507 | * \param[out] position address at which the mmap read/write position should be returned. |
| 508 | * |
| 509 | * \return 0 if the position is successfully returned. |
| 510 | * -ENODATA if the position cannot be retreived |
| 511 | * -ENOSYS if called before mmap_read_position() |
| 512 | */ |
| 513 | int (*get_mmap_position)(const struct audio_stream_in *stream, |
| 514 | struct audio_mmap_position *position); |
rago | 909a8f9 | 2018-01-22 16:00:30 -0800 | [diff] [blame^] | 515 | |
| 516 | /** |
| 517 | * Called by the framework to read active microphones |
| 518 | * |
| 519 | * \param[in] stream the stream object. |
| 520 | * \param[out] mic_array Pointer to first element on array with microphone info |
| 521 | * \param[out] mic_count When called, this holds the value of the max number of elements |
| 522 | * allowed in the mic_array. The actual number of elements written |
| 523 | * is returned here. |
| 524 | * if mic_count is passed as zero, mic_array will not be populated, |
| 525 | * and mic_count will return the actual number of active microphones. |
| 526 | * |
| 527 | * \return 0 if the microphone array is successfully filled. |
| 528 | * -ENOSYS if there is an error filling the data |
| 529 | */ |
| 530 | int (*get_active_microphones)(const struct audio_stream_in *stream, |
| 531 | struct audio_microphone_characteristic_t *mic_array, |
| 532 | size_t *mic_count); |
Kevin Rocard | c6ec948 | 2018-01-24 06:04:27 +0000 | [diff] [blame] | 533 | }; |
| 534 | typedef struct audio_stream_in audio_stream_in_t; |
| 535 | |
| 536 | /** |
| 537 | * return the frame size (number of bytes per sample). |
| 538 | * |
| 539 | * Deprecated: use audio_stream_out_frame_size() or audio_stream_in_frame_size() instead. |
| 540 | */ |
| 541 | __attribute__((__deprecated__)) |
| 542 | static inline size_t audio_stream_frame_size(const struct audio_stream *s) |
| 543 | { |
| 544 | size_t chan_samp_sz; |
| 545 | audio_format_t format = s->get_format(s); |
| 546 | |
| 547 | if (audio_has_proportional_frames(format)) { |
| 548 | chan_samp_sz = audio_bytes_per_sample(format); |
| 549 | return popcount(s->get_channels(s)) * chan_samp_sz; |
| 550 | } |
| 551 | |
| 552 | return sizeof(int8_t); |
| 553 | } |
| 554 | |
| 555 | /** |
| 556 | * return the frame size (number of bytes per sample) of an output stream. |
| 557 | */ |
| 558 | static inline size_t audio_stream_out_frame_size(const struct audio_stream_out *s) |
| 559 | { |
| 560 | size_t chan_samp_sz; |
| 561 | audio_format_t format = s->common.get_format(&s->common); |
| 562 | |
| 563 | if (audio_has_proportional_frames(format)) { |
| 564 | chan_samp_sz = audio_bytes_per_sample(format); |
| 565 | return audio_channel_count_from_out_mask(s->common.get_channels(&s->common)) * chan_samp_sz; |
| 566 | } |
| 567 | |
| 568 | return sizeof(int8_t); |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * return the frame size (number of bytes per sample) of an input stream. |
| 573 | */ |
| 574 | static inline size_t audio_stream_in_frame_size(const struct audio_stream_in *s) |
| 575 | { |
| 576 | size_t chan_samp_sz; |
| 577 | audio_format_t format = s->common.get_format(&s->common); |
| 578 | |
| 579 | if (audio_has_proportional_frames(format)) { |
| 580 | chan_samp_sz = audio_bytes_per_sample(format); |
| 581 | return audio_channel_count_from_in_mask(s->common.get_channels(&s->common)) * chan_samp_sz; |
| 582 | } |
| 583 | |
| 584 | return sizeof(int8_t); |
| 585 | } |
| 586 | |
| 587 | /**********************************************************************/ |
| 588 | |
| 589 | /** |
| 590 | * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM |
| 591 | * and the fields of this data structure must begin with hw_module_t |
| 592 | * followed by module specific information. |
| 593 | */ |
| 594 | struct audio_module { |
| 595 | struct hw_module_t common; |
| 596 | }; |
| 597 | |
| 598 | struct audio_hw_device { |
| 599 | /** |
| 600 | * Common methods of the audio device. This *must* be the first member of audio_hw_device |
| 601 | * as users of this structure will cast a hw_device_t to audio_hw_device pointer in contexts |
| 602 | * where it's known the hw_device_t references an audio_hw_device. |
| 603 | */ |
| 604 | struct hw_device_t common; |
| 605 | |
| 606 | /** |
| 607 | * used by audio flinger to enumerate what devices are supported by |
| 608 | * each audio_hw_device implementation. |
| 609 | * |
| 610 | * Return value is a bitmask of 1 or more values of audio_devices_t |
| 611 | * |
| 612 | * NOTE: audio HAL implementations starting with |
| 613 | * AUDIO_DEVICE_API_VERSION_2_0 do not implement this function. |
| 614 | * All supported devices should be listed in audio_policy.conf |
| 615 | * file and the audio policy manager must choose the appropriate |
| 616 | * audio module based on information in this file. |
| 617 | */ |
| 618 | uint32_t (*get_supported_devices)(const struct audio_hw_device *dev); |
| 619 | |
| 620 | /** |
| 621 | * check to see if the audio hardware interface has been initialized. |
| 622 | * returns 0 on success, -ENODEV on failure. |
| 623 | */ |
| 624 | int (*init_check)(const struct audio_hw_device *dev); |
| 625 | |
| 626 | /** set the audio volume of a voice call. Range is between 0.0 and 1.0 */ |
| 627 | int (*set_voice_volume)(struct audio_hw_device *dev, float volume); |
| 628 | |
| 629 | /** |
| 630 | * set the audio volume for all audio activities other than voice call. |
| 631 | * Range between 0.0 and 1.0. If any value other than 0 is returned, |
| 632 | * the software mixer will emulate this capability. |
| 633 | */ |
| 634 | int (*set_master_volume)(struct audio_hw_device *dev, float volume); |
| 635 | |
| 636 | /** |
| 637 | * Get the current master volume value for the HAL, if the HAL supports |
| 638 | * master volume control. AudioFlinger will query this value from the |
| 639 | * primary audio HAL when the service starts and use the value for setting |
| 640 | * the initial master volume across all HALs. HALs which do not support |
| 641 | * this method may leave it set to NULL. |
| 642 | */ |
| 643 | int (*get_master_volume)(struct audio_hw_device *dev, float *volume); |
| 644 | |
| 645 | /** |
| 646 | * set_mode is called when the audio mode changes. AUDIO_MODE_NORMAL mode |
| 647 | * is for standard audio playback, AUDIO_MODE_RINGTONE when a ringtone is |
| 648 | * playing, and AUDIO_MODE_IN_CALL when a call is in progress. |
| 649 | */ |
| 650 | int (*set_mode)(struct audio_hw_device *dev, audio_mode_t mode); |
| 651 | |
| 652 | /* mic mute */ |
| 653 | int (*set_mic_mute)(struct audio_hw_device *dev, bool state); |
| 654 | int (*get_mic_mute)(const struct audio_hw_device *dev, bool *state); |
| 655 | |
| 656 | /* set/get global audio parameters */ |
| 657 | int (*set_parameters)(struct audio_hw_device *dev, const char *kv_pairs); |
| 658 | |
| 659 | /* |
| 660 | * Returns a pointer to a heap allocated string. The caller is responsible |
| 661 | * for freeing the memory for it using free(). |
| 662 | */ |
| 663 | char * (*get_parameters)(const struct audio_hw_device *dev, |
| 664 | const char *keys); |
| 665 | |
| 666 | /* Returns audio input buffer size according to parameters passed or |
| 667 | * 0 if one of the parameters is not supported. |
| 668 | * See also get_buffer_size which is for a particular stream. |
| 669 | */ |
| 670 | size_t (*get_input_buffer_size)(const struct audio_hw_device *dev, |
| 671 | const struct audio_config *config); |
| 672 | |
| 673 | /** This method creates and opens the audio hardware output stream. |
| 674 | * The "address" parameter qualifies the "devices" audio device type if needed. |
| 675 | * The format format depends on the device type: |
| 676 | * - Bluetooth devices use the MAC address of the device in the form "00:11:22:AA:BB:CC" |
| 677 | * - USB devices use the ALSA card and device numbers in the form "card=X;device=Y" |
| 678 | * - Other devices may use a number or any other string. |
| 679 | */ |
| 680 | |
| 681 | int (*open_output_stream)(struct audio_hw_device *dev, |
| 682 | audio_io_handle_t handle, |
| 683 | audio_devices_t devices, |
| 684 | audio_output_flags_t flags, |
| 685 | struct audio_config *config, |
| 686 | struct audio_stream_out **stream_out, |
| 687 | const char *address); |
| 688 | |
| 689 | void (*close_output_stream)(struct audio_hw_device *dev, |
| 690 | struct audio_stream_out* stream_out); |
| 691 | |
| 692 | /** This method creates and opens the audio hardware input stream */ |
| 693 | int (*open_input_stream)(struct audio_hw_device *dev, |
| 694 | audio_io_handle_t handle, |
| 695 | audio_devices_t devices, |
| 696 | struct audio_config *config, |
| 697 | struct audio_stream_in **stream_in, |
| 698 | audio_input_flags_t flags, |
| 699 | const char *address, |
| 700 | audio_source_t source); |
| 701 | |
| 702 | void (*close_input_stream)(struct audio_hw_device *dev, |
| 703 | struct audio_stream_in *stream_in); |
| 704 | |
rago | 909a8f9 | 2018-01-22 16:00:30 -0800 | [diff] [blame^] | 705 | /** |
| 706 | * Called by the framework to read available microphones characteristics. |
| 707 | * |
| 708 | * \param[in] dev the hw_device object. |
| 709 | * \param[out] mic_array Pointer to first element on array with microphone info |
| 710 | * \param[out] mic_count When called, this holds the value of the max number of elements |
| 711 | * allowed in the mic_array. The actual number of elements written |
| 712 | * is returned here. |
| 713 | * if mic_count is passed as zero, mic_array will not be populated, |
| 714 | * and mic_count will return the actual number of microphones in the |
| 715 | * system. |
| 716 | * |
| 717 | * \return 0 if the microphone array is successfully filled. |
| 718 | * -ENOSYS if there is an error filling the data |
| 719 | */ |
| 720 | int (*get_microphones)(const struct audio_hw_device *dev, |
| 721 | struct audio_microphone_characteristic_t *mic_array, |
| 722 | size_t *mic_count); |
| 723 | |
Kevin Rocard | c6ec948 | 2018-01-24 06:04:27 +0000 | [diff] [blame] | 724 | /** This method dumps the state of the audio hardware */ |
| 725 | int (*dump)(const struct audio_hw_device *dev, int fd); |
| 726 | |
| 727 | /** |
| 728 | * set the audio mute status for all audio activities. If any value other |
| 729 | * than 0 is returned, the software mixer will emulate this capability. |
| 730 | */ |
| 731 | int (*set_master_mute)(struct audio_hw_device *dev, bool mute); |
| 732 | |
| 733 | /** |
| 734 | * Get the current master mute status for the HAL, if the HAL supports |
| 735 | * master mute control. AudioFlinger will query this value from the primary |
| 736 | * audio HAL when the service starts and use the value for setting the |
| 737 | * initial master mute across all HALs. HALs which do not support this |
| 738 | * method may leave it set to NULL. |
| 739 | */ |
| 740 | int (*get_master_mute)(struct audio_hw_device *dev, bool *mute); |
| 741 | |
| 742 | /** |
| 743 | * Routing control |
| 744 | */ |
| 745 | |
| 746 | /* Creates an audio patch between several source and sink ports. |
| 747 | * The handle is allocated by the HAL and should be unique for this |
| 748 | * audio HAL module. */ |
| 749 | int (*create_audio_patch)(struct audio_hw_device *dev, |
| 750 | unsigned int num_sources, |
| 751 | const struct audio_port_config *sources, |
| 752 | unsigned int num_sinks, |
| 753 | const struct audio_port_config *sinks, |
| 754 | audio_patch_handle_t *handle); |
| 755 | |
| 756 | /* Release an audio patch */ |
| 757 | int (*release_audio_patch)(struct audio_hw_device *dev, |
| 758 | audio_patch_handle_t handle); |
| 759 | |
| 760 | /* Fills the list of supported attributes for a given audio port. |
| 761 | * As input, "port" contains the information (type, role, address etc...) |
| 762 | * needed by the HAL to identify the port. |
| 763 | * As output, "port" contains possible attributes (sampling rates, formats, |
| 764 | * channel masks, gain controllers...) for this port. |
| 765 | */ |
| 766 | int (*get_audio_port)(struct audio_hw_device *dev, |
| 767 | struct audio_port *port); |
| 768 | |
| 769 | /* Set audio port configuration */ |
| 770 | int (*set_audio_port_config)(struct audio_hw_device *dev, |
| 771 | const struct audio_port_config *config); |
| 772 | |
| 773 | }; |
| 774 | typedef struct audio_hw_device audio_hw_device_t; |
| 775 | |
| 776 | /** convenience API for opening and closing a supported device */ |
| 777 | |
| 778 | static inline int audio_hw_device_open(const struct hw_module_t* module, |
| 779 | struct audio_hw_device** device) |
| 780 | { |
| 781 | return module->methods->open(module, AUDIO_HARDWARE_INTERFACE, |
| 782 | TO_HW_DEVICE_T_OPEN(device)); |
| 783 | } |
| 784 | |
| 785 | static inline int audio_hw_device_close(struct audio_hw_device* device) |
| 786 | { |
| 787 | return device->common.close(&device->common); |
| 788 | } |
| 789 | |
| 790 | |
| 791 | __END_DECLS |
| 792 | |
| 793 | #endif // ANDROID_AUDIO_INTERFACE_H |