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