blob: 69b0b9c09f81bf641bd09068ae21ff7fc8bed24a [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
Kevin Rocardc6ec9482018-01-24 06:04:27 +0000236/**
237 * audio_stream_out is the abstraction interface for the audio output hardware.
238 *
239 * It provides information about various properties of the audio output
240 * hardware driver.
241 */
Kevin Rocardc6ec9482018-01-24 06:04:27 +0000242struct audio_stream_out {
243 /**
244 * Common methods of the audio stream out. This *must* be the first member of audio_stream_out
245 * as users of this structure will cast a audio_stream to audio_stream_out pointer in contexts
246 * where it's known the audio_stream references an audio_stream_out.
247 */
248 struct audio_stream common;
249
250 /**
251 * Return the audio hardware driver estimated latency in milliseconds.
252 */
253 uint32_t (*get_latency)(const struct audio_stream_out *stream);
254
255 /**
256 * Use this method in situations where audio mixing is done in the
257 * hardware. This method serves as a direct interface with hardware,
258 * allowing you to directly set the volume as apposed to via the framework.
259 * This method might produce multiple PCM outputs or hardware accelerated
260 * codecs, such as MP3 or AAC.
261 */
262 int (*set_volume)(struct audio_stream_out *stream, float left, float right);
263
264 /**
265 * Write audio buffer to driver. Returns number of bytes written, or a
266 * negative status_t. If at least one frame was written successfully prior to the error,
267 * it is suggested that the driver return that successful (short) byte count
268 * and then return an error in the subsequent call.
269 *
270 * If set_callback() has previously been called to enable non-blocking mode
271 * the write() is not allowed to block. It must write only the number of
272 * bytes that currently fit in the driver/hardware buffer and then return
273 * this byte count. If this is less than the requested write size the
274 * callback function must be called when more space is available in the
275 * driver/hardware buffer.
276 */
277 ssize_t (*write)(struct audio_stream_out *stream, const void* buffer,
278 size_t bytes);
279
280 /* return the number of audio frames written by the audio dsp to DAC since
281 * the output has exited standby
282 */
283 int (*get_render_position)(const struct audio_stream_out *stream,
284 uint32_t *dsp_frames);
285
286 /**
287 * get the local time at which the next write to the audio driver will be presented.
288 * The units are microseconds, where the epoch is decided by the local audio HAL.
289 */
290 int (*get_next_write_timestamp)(const struct audio_stream_out *stream,
291 int64_t *timestamp);
292
293 /**
294 * set the callback function for notifying completion of non-blocking
295 * write and drain.
296 * Calling this function implies that all future write() and drain()
297 * must be non-blocking and use the callback to signal completion.
298 */
299 int (*set_callback)(struct audio_stream_out *stream,
300 stream_callback_t callback, void *cookie);
301
302 /**
303 * Notifies to the audio driver to stop playback however the queued buffers are
304 * retained by the hardware. Useful for implementing pause/resume. Empty implementation
305 * if not supported however should be implemented for hardware with non-trivial
306 * latency. In the pause state audio hardware could still be using power. User may
307 * consider calling suspend after a timeout.
308 *
309 * Implementation of this function is mandatory for offloaded playback.
310 */
311 int (*pause)(struct audio_stream_out* stream);
312
313 /**
314 * Notifies to the audio driver to resume playback following a pause.
315 * Returns error if called without matching pause.
316 *
317 * Implementation of this function is mandatory for offloaded playback.
318 */
319 int (*resume)(struct audio_stream_out* stream);
320
321 /**
322 * Requests notification when data buffered by the driver/hardware has
323 * been played. If set_callback() has previously been called to enable
324 * non-blocking mode, the drain() must not block, instead it should return
325 * quickly and completion of the drain is notified through the callback.
326 * If set_callback() has not been called, the drain() must block until
327 * completion.
328 * If type==AUDIO_DRAIN_ALL, the drain completes when all previously written
329 * data has been played.
330 * If type==AUDIO_DRAIN_EARLY_NOTIFY, the drain completes shortly before all
331 * data for the current track has played to allow time for the framework
332 * to perform a gapless track switch.
333 *
334 * Drain must return immediately on stop() and flush() call
335 *
336 * Implementation of this function is mandatory for offloaded playback.
337 */
338 int (*drain)(struct audio_stream_out* stream, audio_drain_type_t type );
339
340 /**
341 * Notifies to the audio driver to flush the queued data. Stream must already
342 * be paused before calling flush().
343 *
344 * Implementation of this function is mandatory for offloaded playback.
345 */
346 int (*flush)(struct audio_stream_out* stream);
347
348 /**
349 * Return a recent count of the number of audio frames presented to an external observer.
350 * This excludes frames which have been written but are still in the pipeline.
351 * The count is not reset to zero when output enters standby.
352 * Also returns the value of CLOCK_MONOTONIC as of this presentation count.
353 * The returned count is expected to be 'recent',
354 * but does not need to be the most recent possible value.
355 * However, the associated time should correspond to whatever count is returned.
356 * Example: assume that N+M frames have been presented, where M is a 'small' number.
357 * Then it is permissible to return N instead of N+M,
358 * and the timestamp should correspond to N rather than N+M.
359 * The terms 'recent' and 'small' are not defined.
360 * They reflect the quality of the implementation.
361 *
362 * 3.0 and higher only.
363 */
364 int (*get_presentation_position)(const struct audio_stream_out *stream,
365 uint64_t *frames, struct timespec *timestamp);
366
367 /**
368 * Called by the framework to start a stream operating in mmap mode.
369 * create_mmap_buffer must be called before calling start()
370 *
371 * \note Function only implemented by streams operating in mmap mode.
372 *
373 * \param[in] stream the stream object.
374 * \return 0 in case of success.
375 * -ENOSYS if called out of sequence or on non mmap stream
376 */
377 int (*start)(const struct audio_stream_out* stream);
378
379 /**
380 * Called by the framework to stop a stream operating in mmap mode.
381 * Must be called after start()
382 *
383 * \note Function only implemented by streams operating in mmap mode.
384 *
385 * \param[in] stream the stream object.
386 * \return 0 in case of success.
387 * -ENOSYS if called out of sequence or on non mmap stream
388 */
389 int (*stop)(const struct audio_stream_out* stream);
390
391 /**
392 * Called by the framework to retrieve information on the mmap buffer used for audio
393 * samples transfer.
394 *
395 * \note Function only implemented by streams operating in mmap mode.
396 *
397 * \param[in] stream the stream object.
398 * \param[in] min_size_frames minimum buffer size requested. The actual buffer
399 * size returned in struct audio_mmap_buffer_info can be larger.
400 * \param[out] info address at which the mmap buffer information should be returned.
401 *
402 * \return 0 if the buffer was allocated.
403 * -ENODEV in case of initialization error
404 * -EINVAL if the requested buffer size is too large
405 * -ENOSYS if called out of sequence (e.g. buffer already allocated)
406 */
407 int (*create_mmap_buffer)(const struct audio_stream_out *stream,
408 int32_t min_size_frames,
409 struct audio_mmap_buffer_info *info);
410
411 /**
412 * Called by the framework to read current read/write position in the mmap buffer
413 * with associated time stamp.
414 *
415 * \note Function only implemented by streams operating in mmap mode.
416 *
417 * \param[in] stream the stream object.
418 * \param[out] position address at which the mmap read/write position should be returned.
419 *
420 * \return 0 if the position is successfully returned.
421 * -ENODATA if the position cannot be retrieved
422 * -ENOSYS if called before create_mmap_buffer()
423 */
424 int (*get_mmap_position)(const struct audio_stream_out *stream,
425 struct audio_mmap_position *position);
Kevin Rocard0360e252018-03-26 17:13:12 -0700426
427 /**
428 * Called when the metadata of the stream's source has been changed.
429 * @param source_metadata Description of the audio that is played by the clients.
430 */
431 void (*update_source_metadata)(struct audio_stream_out *stream,
432 const struct source_metadata* source_metadata);
jiabin3b4b33f2020-02-12 12:59:18 -0800433
434 /**
435 * Set the callback function for notifying events for an output stream.
436 */
437 int (*set_event_callback)(struct audio_stream_out *stream,
438 stream_event_callback_t callback,
439 void *cookie);
Kevin Rocardc6ec9482018-01-24 06:04:27 +0000440};
441typedef struct audio_stream_out audio_stream_out_t;
442
443struct audio_stream_in {
444 /**
445 * Common methods of the audio stream in. This *must* be the first member of audio_stream_in
446 * as users of this structure will cast a audio_stream to audio_stream_in pointer in contexts
447 * where it's known the audio_stream references an audio_stream_in.
448 */
449 struct audio_stream common;
450
451 /** set the input gain for the audio driver. This method is for
452 * for future use */
453 int (*set_gain)(struct audio_stream_in *stream, float gain);
454
455 /** Read audio buffer in from audio driver. Returns number of bytes read, or a
456 * negative status_t. If at least one frame was read prior to the error,
457 * read should return that byte count and then return an error in the subsequent call.
458 */
459 ssize_t (*read)(struct audio_stream_in *stream, void* buffer,
460 size_t bytes);
461
462 /**
463 * Return the amount of input frames lost in the audio driver since the
464 * last call of this function.
465 * Audio driver is expected to reset the value to 0 and restart counting
466 * upon returning the current value by this function call.
467 * Such loss typically occurs when the user space process is blocked
468 * longer than the capacity of audio driver buffers.
469 *
470 * Unit: the number of input audio frames
471 */
472 uint32_t (*get_input_frames_lost)(struct audio_stream_in *stream);
473
474 /**
475 * Return a recent count of the number of audio frames received and
476 * the clock time associated with that frame count.
477 *
478 * frames is the total frame count received. This should be as early in
479 * the capture pipeline as possible. In general,
480 * frames should be non-negative and should not go "backwards".
481 *
482 * time is the clock MONOTONIC time when frames was measured. In general,
483 * time should be a positive quantity and should not go "backwards".
484 *
485 * The status returned is 0 on success, -ENOSYS if the device is not
486 * ready/available, or -EINVAL if the arguments are null or otherwise invalid.
487 */
488 int (*get_capture_position)(const struct audio_stream_in *stream,
489 int64_t *frames, int64_t *time);
490
491 /**
492 * Called by the framework to start a stream operating in mmap mode.
493 * create_mmap_buffer must be called before calling start()
494 *
495 * \note Function only implemented by streams operating in mmap mode.
496 *
497 * \param[in] stream the stream object.
498 * \return 0 in case off success.
499 * -ENOSYS if called out of sequence or on non mmap stream
500 */
501 int (*start)(const struct audio_stream_in* stream);
502
503 /**
504 * Called by the framework to stop a stream operating in mmap mode.
505 *
506 * \note Function only implemented by streams operating in mmap mode.
507 *
508 * \param[in] stream the stream object.
509 * \return 0 in case of success.
510 * -ENOSYS if called out of sequence or on non mmap stream
511 */
512 int (*stop)(const struct audio_stream_in* stream);
513
514 /**
515 * Called by the framework to retrieve information on the mmap buffer used for audio
516 * samples transfer.
517 *
518 * \note Function only implemented by streams operating in mmap mode.
519 *
520 * \param[in] stream the stream object.
521 * \param[in] min_size_frames minimum buffer size requested. The actual buffer
522 * size returned in struct audio_mmap_buffer_info can be larger.
523 * \param[out] info address at which the mmap buffer information should be returned.
524 *
525 * \return 0 if the buffer was allocated.
526 * -ENODEV in case of initialization error
527 * -EINVAL if the requested buffer size is too large
528 * -ENOSYS if called out of sequence (e.g. buffer already allocated)
529 */
530 int (*create_mmap_buffer)(const struct audio_stream_in *stream,
531 int32_t min_size_frames,
532 struct audio_mmap_buffer_info *info);
533
534 /**
535 * Called by the framework to read current read/write position in the mmap buffer
536 * with associated time stamp.
537 *
538 * \note Function only implemented by streams operating in mmap mode.
539 *
540 * \param[in] stream the stream object.
541 * \param[out] position address at which the mmap read/write position should be returned.
542 *
543 * \return 0 if the position is successfully returned.
544 * -ENODATA if the position cannot be retreived
545 * -ENOSYS if called before mmap_read_position()
546 */
547 int (*get_mmap_position)(const struct audio_stream_in *stream,
548 struct audio_mmap_position *position);
rago909a8f92018-01-22 16:00:30 -0800549
550 /**
551 * Called by the framework to read active microphones
552 *
553 * \param[in] stream the stream object.
554 * \param[out] mic_array Pointer to first element on array with microphone info
555 * \param[out] mic_count When called, this holds the value of the max number of elements
556 * allowed in the mic_array. The actual number of elements written
557 * is returned here.
558 * if mic_count is passed as zero, mic_array will not be populated,
559 * and mic_count will return the actual number of active microphones.
560 *
561 * \return 0 if the microphone array is successfully filled.
562 * -ENOSYS if there is an error filling the data
563 */
564 int (*get_active_microphones)(const struct audio_stream_in *stream,
565 struct audio_microphone_characteristic_t *mic_array,
566 size_t *mic_count);
Kevin Rocard0360e252018-03-26 17:13:12 -0700567
568 /**
Paul McLeanfa3ae3e2018-12-12 09:57:02 -0800569 * Called by the framework to instruct the HAL to optimize the capture stream in the
570 * specified direction.
571 *
572 * \param[in] stream the stream object.
573 * \param[in] direction The direction constant (from audio-base.h)
574 * MIC_DIRECTION_UNSPECIFIED Don't do any directionality processing of the
575 * activated microphone(s).
576 * MIC_DIRECTION_FRONT Optimize capture for audio coming from the screen-side
577 * of the device.
578 * MIC_DIRECTION_BACK Optimize capture for audio coming from the side of the
579 * device opposite the screen.
580 * MIC_DIRECTION_EXTERNAL Optimize capture for audio coming from an off-device
581 * microphone.
582 * \return OK if the call is successful, an error code otherwise.
583 */
584 int (*set_microphone_direction)(const struct audio_stream_in *stream,
585 audio_microphone_direction_t direction);
586
587 /**
588 * Called by the framework to specify to the HAL the desired zoom factor for the selected
589 * microphone(s).
590 *
591 * \param[in] stream the stream object.
592 * \param[in] zoom the zoom factor.
593 * \return OK if the call is successful, an error code otherwise.
594 */
595 int (*set_microphone_field_dimension)(const struct audio_stream_in *stream,
596 float zoom);
597
598 /**
Kevin Rocard0360e252018-03-26 17:13:12 -0700599 * Called when the metadata of the stream's sink has been changed.
600 * @param sink_metadata Description of the audio that is recorded by the clients.
601 */
602 void (*update_sink_metadata)(struct audio_stream_in *stream,
603 const struct sink_metadata* sink_metadata);
Kevin Rocardc6ec9482018-01-24 06:04:27 +0000604};
605typedef struct audio_stream_in audio_stream_in_t;
606
607/**
608 * return the frame size (number of bytes per sample).
609 *
610 * Deprecated: use audio_stream_out_frame_size() or audio_stream_in_frame_size() instead.
611 */
612__attribute__((__deprecated__))
613static inline size_t audio_stream_frame_size(const struct audio_stream *s)
614{
615 size_t chan_samp_sz;
616 audio_format_t format = s->get_format(s);
617
618 if (audio_has_proportional_frames(format)) {
619 chan_samp_sz = audio_bytes_per_sample(format);
620 return popcount(s->get_channels(s)) * chan_samp_sz;
621 }
622
623 return sizeof(int8_t);
624}
625
626/**
627 * return the frame size (number of bytes per sample) of an output stream.
628 */
629static inline size_t audio_stream_out_frame_size(const struct audio_stream_out *s)
630{
631 size_t chan_samp_sz;
632 audio_format_t format = s->common.get_format(&s->common);
633
634 if (audio_has_proportional_frames(format)) {
635 chan_samp_sz = audio_bytes_per_sample(format);
636 return audio_channel_count_from_out_mask(s->common.get_channels(&s->common)) * chan_samp_sz;
637 }
638
639 return sizeof(int8_t);
640}
641
642/**
643 * return the frame size (number of bytes per sample) of an input stream.
644 */
645static inline size_t audio_stream_in_frame_size(const struct audio_stream_in *s)
646{
647 size_t chan_samp_sz;
648 audio_format_t format = s->common.get_format(&s->common);
649
650 if (audio_has_proportional_frames(format)) {
651 chan_samp_sz = audio_bytes_per_sample(format);
652 return audio_channel_count_from_in_mask(s->common.get_channels(&s->common)) * chan_samp_sz;
653 }
654
655 return sizeof(int8_t);
656}
657
658/**********************************************************************/
659
660/**
661 * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
662 * and the fields of this data structure must begin with hw_module_t
663 * followed by module specific information.
664 */
665struct audio_module {
666 struct hw_module_t common;
667};
668
669struct audio_hw_device {
670 /**
671 * Common methods of the audio device. This *must* be the first member of audio_hw_device
672 * as users of this structure will cast a hw_device_t to audio_hw_device pointer in contexts
673 * where it's known the hw_device_t references an audio_hw_device.
674 */
675 struct hw_device_t common;
676
677 /**
678 * used by audio flinger to enumerate what devices are supported by
679 * each audio_hw_device implementation.
680 *
681 * Return value is a bitmask of 1 or more values of audio_devices_t
682 *
683 * NOTE: audio HAL implementations starting with
684 * AUDIO_DEVICE_API_VERSION_2_0 do not implement this function.
685 * All supported devices should be listed in audio_policy.conf
686 * file and the audio policy manager must choose the appropriate
687 * audio module based on information in this file.
688 */
689 uint32_t (*get_supported_devices)(const struct audio_hw_device *dev);
690
691 /**
692 * check to see if the audio hardware interface has been initialized.
693 * returns 0 on success, -ENODEV on failure.
694 */
695 int (*init_check)(const struct audio_hw_device *dev);
696
697 /** set the audio volume of a voice call. Range is between 0.0 and 1.0 */
698 int (*set_voice_volume)(struct audio_hw_device *dev, float volume);
699
700 /**
701 * set the audio volume for all audio activities other than voice call.
702 * Range between 0.0 and 1.0. If any value other than 0 is returned,
703 * the software mixer will emulate this capability.
704 */
705 int (*set_master_volume)(struct audio_hw_device *dev, float volume);
706
707 /**
708 * Get the current master volume value for the HAL, if the HAL supports
709 * master volume control. AudioFlinger will query this value from the
710 * primary audio HAL when the service starts and use the value for setting
711 * the initial master volume across all HALs. HALs which do not support
712 * this method may leave it set to NULL.
713 */
714 int (*get_master_volume)(struct audio_hw_device *dev, float *volume);
715
716 /**
717 * set_mode is called when the audio mode changes. AUDIO_MODE_NORMAL mode
718 * is for standard audio playback, AUDIO_MODE_RINGTONE when a ringtone is
719 * playing, and AUDIO_MODE_IN_CALL when a call is in progress.
720 */
721 int (*set_mode)(struct audio_hw_device *dev, audio_mode_t mode);
722
723 /* mic mute */
724 int (*set_mic_mute)(struct audio_hw_device *dev, bool state);
725 int (*get_mic_mute)(const struct audio_hw_device *dev, bool *state);
726
727 /* set/get global audio parameters */
728 int (*set_parameters)(struct audio_hw_device *dev, const char *kv_pairs);
729
730 /*
731 * Returns a pointer to a heap allocated string. The caller is responsible
732 * for freeing the memory for it using free().
733 */
734 char * (*get_parameters)(const struct audio_hw_device *dev,
735 const char *keys);
736
737 /* Returns audio input buffer size according to parameters passed or
738 * 0 if one of the parameters is not supported.
739 * See also get_buffer_size which is for a particular stream.
740 */
741 size_t (*get_input_buffer_size)(const struct audio_hw_device *dev,
742 const struct audio_config *config);
743
744 /** This method creates and opens the audio hardware output stream.
745 * The "address" parameter qualifies the "devices" audio device type if needed.
746 * The format format depends on the device type:
747 * - Bluetooth devices use the MAC address of the device in the form "00:11:22:AA:BB:CC"
748 * - USB devices use the ALSA card and device numbers in the form "card=X;device=Y"
749 * - Other devices may use a number or any other string.
750 */
751
752 int (*open_output_stream)(struct audio_hw_device *dev,
753 audio_io_handle_t handle,
754 audio_devices_t devices,
755 audio_output_flags_t flags,
756 struct audio_config *config,
757 struct audio_stream_out **stream_out,
758 const char *address);
759
760 void (*close_output_stream)(struct audio_hw_device *dev,
761 struct audio_stream_out* stream_out);
762
763 /** This method creates and opens the audio hardware input stream */
764 int (*open_input_stream)(struct audio_hw_device *dev,
765 audio_io_handle_t handle,
766 audio_devices_t devices,
767 struct audio_config *config,
768 struct audio_stream_in **stream_in,
769 audio_input_flags_t flags,
770 const char *address,
771 audio_source_t source);
772
773 void (*close_input_stream)(struct audio_hw_device *dev,
774 struct audio_stream_in *stream_in);
775
rago909a8f92018-01-22 16:00:30 -0800776 /**
777 * Called by the framework to read available microphones characteristics.
778 *
779 * \param[in] dev the hw_device object.
780 * \param[out] mic_array Pointer to first element on array with microphone info
781 * \param[out] mic_count When called, this holds the value of the max number of elements
782 * allowed in the mic_array. The actual number of elements written
783 * is returned here.
784 * if mic_count is passed as zero, mic_array will not be populated,
785 * and mic_count will return the actual number of microphones in the
786 * system.
787 *
788 * \return 0 if the microphone array is successfully filled.
789 * -ENOSYS if there is an error filling the data
790 */
791 int (*get_microphones)(const struct audio_hw_device *dev,
792 struct audio_microphone_characteristic_t *mic_array,
793 size_t *mic_count);
794
Kevin Rocardc6ec9482018-01-24 06:04:27 +0000795 /** This method dumps the state of the audio hardware */
796 int (*dump)(const struct audio_hw_device *dev, int fd);
797
798 /**
799 * set the audio mute status for all audio activities. If any value other
800 * than 0 is returned, the software mixer will emulate this capability.
801 */
802 int (*set_master_mute)(struct audio_hw_device *dev, bool mute);
803
804 /**
805 * Get the current master mute status for the HAL, if the HAL supports
806 * master mute control. AudioFlinger will query this value from the primary
807 * audio HAL when the service starts and use the value for setting the
808 * initial master mute across all HALs. HALs which do not support this
809 * method may leave it set to NULL.
810 */
811 int (*get_master_mute)(struct audio_hw_device *dev, bool *mute);
812
813 /**
814 * Routing control
815 */
816
817 /* Creates an audio patch between several source and sink ports.
818 * The handle is allocated by the HAL and should be unique for this
819 * audio HAL module. */
820 int (*create_audio_patch)(struct audio_hw_device *dev,
821 unsigned int num_sources,
822 const struct audio_port_config *sources,
823 unsigned int num_sinks,
824 const struct audio_port_config *sinks,
825 audio_patch_handle_t *handle);
826
827 /* Release an audio patch */
828 int (*release_audio_patch)(struct audio_hw_device *dev,
829 audio_patch_handle_t handle);
830
831 /* Fills the list of supported attributes for a given audio port.
832 * As input, "port" contains the information (type, role, address etc...)
833 * needed by the HAL to identify the port.
834 * As output, "port" contains possible attributes (sampling rates, formats,
835 * channel masks, gain controllers...) for this port.
836 */
837 int (*get_audio_port)(struct audio_hw_device *dev,
838 struct audio_port *port);
839
840 /* Set audio port configuration */
841 int (*set_audio_port_config)(struct audio_hw_device *dev,
842 const struct audio_port_config *config);
843
Eric Laurent26f0adf2019-12-11 10:41:10 -0800844 /**
845 * Applies an audio effect to an audio device.
846 *
847 * @param dev the audio HAL device context.
848 * @param device identifies the sink or source device the effect must be applied to.
849 * "device" is the audio_port_handle_t indicated for the device when
850 * the audio patch connecting that device was created.
851 * @param effect effect interface handle corresponding to the effect being added.
852 * @return retval operation completion status.
853 */
854 int (*add_device_effect)(struct audio_hw_device *dev,
855 audio_port_handle_t device, effect_handle_t effect);
856
857 /**
858 * Stops applying an audio effect to an audio device.
859 *
860 * @param dev the audio HAL device context.
861 * @param device identifies the sink or source device this effect was applied to.
862 * "device" is the audio_port_handle_t indicated for the device when
863 * the audio patch is created.
864 * @param effect effect interface handle corresponding to the effect being removed.
865 * @return retval operation completion status.
866 */
867 int (*remove_device_effect)(struct audio_hw_device *dev,
868 audio_port_handle_t device, effect_handle_t effect);
jiabind6510512020-10-14 15:01:58 -0700869
870 /**
871 * Fills the list of supported attributes for a given audio port.
872 * As input, "port" contains the information (type, role, address etc...)
873 * needed by the HAL to identify the port.
874 * As output, "port" contains possible attributes (sampling rates, formats,
875 * channel masks, gain controllers...) for this port. The possible attributes
876 * are saved as audio profiles, which contains audio format and the supported
877 * sampling rates and channel masks.
878 */
879 int (*get_audio_port_v7)(struct audio_hw_device *dev,
880 struct audio_port_v7 *port);
Kevin Rocardc6ec9482018-01-24 06:04:27 +0000881};
882typedef struct audio_hw_device audio_hw_device_t;
883
884/** convenience API for opening and closing a supported device */
885
886static inline int audio_hw_device_open(const struct hw_module_t* module,
887 struct audio_hw_device** device)
888{
889 return module->methods->open(module, AUDIO_HARDWARE_INTERFACE,
890 TO_HW_DEVICE_T_OPEN(device));
891}
892
893static inline int audio_hw_device_close(struct audio_hw_device* device)
894{
895 return device->common.close(&device->common);
896}
897
898
899__END_DECLS
900
901#endif // ANDROID_AUDIO_INTERFACE_H