blob: b49dce62ef406b8ce6b21ec67a7fc043425e15fe [file] [log] [blame]
Dima Zavinf1504db2011-03-11 11:20:49 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18#ifndef ANDROID_AUDIO_HAL_INTERFACE_H
19#define ANDROID_AUDIO_HAL_INTERFACE_H
20
21#include <stdint.h>
22#include <strings.h>
23#include <sys/cdefs.h>
24#include <sys/types.h>
25
26#include <cutils/bitops.h>
27
28#include <hardware/hardware.h>
Dima Zavinaa211722011-05-11 14:15:53 -070029#include <system/audio.h>
Eric Laurentf3008aa2011-06-17 16:53:12 -070030#include <hardware/audio_effect.h>
Dima Zavinf1504db2011-03-11 11:20:49 -080031
32__BEGIN_DECLS
33
34/**
35 * The id of this module
36 */
37#define AUDIO_HARDWARE_MODULE_ID "audio"
38
39/**
40 * Name of the audio devices to open
41 */
42#define AUDIO_HARDWARE_INTERFACE "audio_hw_if"
43
Eric Laurent55786bc2012-04-10 16:56:32 -070044
45/* Use version 0.1 to be compatible with first generation of audio hw module with version_major
46 * hardcoded to 1. No audio module API change.
47 */
48#define AUDIO_MODULE_API_VERSION_0_1 HARDWARE_MODULE_API_VERSION(0, 1)
49#define AUDIO_MODULE_API_VERSION_CURRENT AUDIO_MODULE_API_VERSION_0_1
50
51/* First generation of audio devices had version hardcoded to 0. all devices with versions < 1.0
52 * will be considered of first generation API.
53 */
54#define AUDIO_DEVICE_API_VERSION_0_0 HARDWARE_DEVICE_API_VERSION(0, 0)
55#define AUDIO_DEVICE_API_VERSION_1_0 HARDWARE_DEVICE_API_VERSION(1, 0)
Eric Laurent85e08e22012-08-28 14:30:35 -070056#define AUDIO_DEVICE_API_VERSION_2_0 HARDWARE_DEVICE_API_VERSION(2, 0)
Eric Laurent73b8a742014-05-22 14:02:38 -070057#define AUDIO_DEVICE_API_VERSION_3_0 HARDWARE_DEVICE_API_VERSION(3, 0)
58#define AUDIO_DEVICE_API_VERSION_CURRENT AUDIO_DEVICE_API_VERSION_3_0
Eric Laurent447cae72014-05-22 13:45:55 -070059/* Minimal audio HAL version supported by the audio framework */
60#define AUDIO_DEVICE_API_VERSION_MIN AUDIO_DEVICE_API_VERSION_2_0
Eric Laurent55786bc2012-04-10 16:56:32 -070061
Eric Laurent431fc782012-04-03 12:07:02 -070062/**
63 * List of known audio HAL modules. This is the base name of the audio HAL
64 * library composed of the "audio." prefix, one of the base names below and
65 * a suffix specific to the device.
66 * e.g: audio.primary.goldfish.so or audio.a2dp.default.so
67 */
68
69#define AUDIO_HARDWARE_MODULE_ID_PRIMARY "primary"
70#define AUDIO_HARDWARE_MODULE_ID_A2DP "a2dp"
71#define AUDIO_HARDWARE_MODULE_ID_USB "usb"
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070072#define AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX "r_submix"
Richard Fitzgeraldf37f1872013-03-25 16:11:44 +000073#define AUDIO_HARDWARE_MODULE_ID_CODEC_OFFLOAD "codec_offload"
Eric Laurent431fc782012-04-03 12:07:02 -070074
Dima Zavinf1504db2011-03-11 11:20:49 -080075/**************************************/
76
Eric Laurent70e81102011-08-07 10:05:40 -070077/**
78 * standard audio parameters that the HAL may need to handle
79 */
80
81/**
82 * audio device parameters
83 */
84
Eric Laurented9928c2011-08-02 17:12:00 -070085/* BT SCO Noise Reduction + Echo Cancellation parameters */
86#define AUDIO_PARAMETER_KEY_BT_NREC "bt_headset_nrec"
87#define AUDIO_PARAMETER_VALUE_ON "on"
88#define AUDIO_PARAMETER_VALUE_OFF "off"
89
Eric Laurent70e81102011-08-07 10:05:40 -070090/* TTY mode selection */
91#define AUDIO_PARAMETER_KEY_TTY_MODE "tty_mode"
92#define AUDIO_PARAMETER_VALUE_TTY_OFF "tty_off"
93#define AUDIO_PARAMETER_VALUE_TTY_VCO "tty_vco"
94#define AUDIO_PARAMETER_VALUE_TTY_HCO "tty_hco"
95#define AUDIO_PARAMETER_VALUE_TTY_FULL "tty_full"
96
Eric Laurentd1a1b1c2014-07-25 12:10:11 -050097/* Hearing Aid Compatibility - Telecoil (HAC-T) mode on/off
98 Strings must be in sync with CallFeaturesSetting.java */
99#define AUDIO_PARAMETER_KEY_HAC "HACSetting"
100#define AUDIO_PARAMETER_VALUE_HAC_ON "ON"
101#define AUDIO_PARAMETER_VALUE_HAC_OFF "OFF"
102
Eric Laurenta70c5d02012-03-07 18:59:47 -0800103/* A2DP sink address set by framework */
104#define AUDIO_PARAMETER_A2DP_SINK_ADDRESS "a2dp_sink_address"
105
Mike Lockwood2d4d9652014-05-28 11:09:54 -0700106/* A2DP source address set by framework */
107#define AUDIO_PARAMETER_A2DP_SOURCE_ADDRESS "a2dp_source_address"
108
Glenn Kasten34afb682012-06-08 10:49:34 -0700109/* Screen state */
110#define AUDIO_PARAMETER_KEY_SCREEN_STATE "screen_state"
111
Glenn Kastend930d922014-04-29 13:35:57 -0700112/* Bluetooth SCO wideband */
113#define AUDIO_PARAMETER_KEY_BT_SCO_WB "bt_wbs"
114
Eric Laurentbc19a3d2014-10-17 18:17:51 -0700115/* Get a new HW synchronization source identifier.
116 * Return a valid source (positive integer) or AUDIO_HW_SYNC_INVALID if an error occurs
117 * or no HW sync is available. */
118#define AUDIO_PARAMETER_HW_AV_SYNC "hw_av_sync"
Eric Laurent4ea9b952014-08-01 14:42:44 -0700119
Eric Laurent70e81102011-08-07 10:05:40 -0700120/**
121 * audio stream parameters
122 */
123
Eric Laurentf5e24692014-07-27 16:14:57 -0700124#define AUDIO_PARAMETER_STREAM_ROUTING "routing" /* audio_devices_t */
125#define AUDIO_PARAMETER_STREAM_FORMAT "format" /* audio_format_t */
126#define AUDIO_PARAMETER_STREAM_CHANNELS "channels" /* audio_channel_mask_t */
127#define AUDIO_PARAMETER_STREAM_FRAME_COUNT "frame_count" /* size_t */
128#define AUDIO_PARAMETER_STREAM_INPUT_SOURCE "input_source" /* audio_source_t */
129#define AUDIO_PARAMETER_STREAM_SAMPLING_RATE "sampling_rate" /* uint32_t */
Dima Zavin57dde282011-06-06 19:31:18 -0700130
Haynes Mathew Georged5f7dbe2014-09-24 19:10:12 -0700131#define AUDIO_PARAMETER_DEVICE_CONNECT "connect" /* audio_devices_t */
Paul McLean2c6196f2014-08-20 16:50:25 -0700132#define AUDIO_PARAMETER_DEVICE_DISCONNECT "disconnect" /* audio_devices_t */
133
Eric Laurent41eeb4f2012-05-17 18:54:49 -0700134/* Query supported formats. The response is a '|' separated list of strings from
135 * audio_format_t enum e.g: "sup_formats=AUDIO_FORMAT_PCM_16_BIT" */
136#define AUDIO_PARAMETER_STREAM_SUP_FORMATS "sup_formats"
137/* Query supported channel masks. The response is a '|' separated list of strings from
138 * audio_channel_mask_t enum e.g: "sup_channels=AUDIO_CHANNEL_OUT_STEREO|AUDIO_CHANNEL_OUT_MONO" */
139#define AUDIO_PARAMETER_STREAM_SUP_CHANNELS "sup_channels"
140/* Query supported sampling rates. The response is a '|' separated list of integer values e.g:
141 * "sup_sampling_rates=44100|48000" */
142#define AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES "sup_sampling_rates"
143
Eric Laurentbc19a3d2014-10-17 18:17:51 -0700144/* Set the HW synchronization source for an output stream. */
Eric Laurent4ea9b952014-08-01 14:42:44 -0700145#define AUDIO_PARAMETER_STREAM_HW_AV_SYNC "hw_av_sync"
146
Andy Hung89d567f2016-01-05 11:23:36 -0800147/* Enable mono audio playback if 1, else should be 0. */
148#define AUDIO_PARAMETER_MONO_OUTPUT "mono_output"
149
vivek mehta27258e52016-07-18 13:58:40 -0700150/* Enable AANC */
151#define AUDIO_PARAMETER_KEY_AANC "aanc_enabled"
152
Richard Fitzgeraldf37f1872013-03-25 16:11:44 +0000153/**
154 * audio codec parameters
155 */
156
157#define AUDIO_OFFLOAD_CODEC_PARAMS "music_offload_codec_param"
158#define AUDIO_OFFLOAD_CODEC_BIT_PER_SAMPLE "music_offload_bit_per_sample"
159#define AUDIO_OFFLOAD_CODEC_BIT_RATE "music_offload_bit_rate"
160#define AUDIO_OFFLOAD_CODEC_AVG_BIT_RATE "music_offload_avg_bit_rate"
161#define AUDIO_OFFLOAD_CODEC_ID "music_offload_codec_id"
162#define AUDIO_OFFLOAD_CODEC_BLOCK_ALIGN "music_offload_block_align"
163#define AUDIO_OFFLOAD_CODEC_SAMPLE_RATE "music_offload_sample_rate"
164#define AUDIO_OFFLOAD_CODEC_ENCODE_OPTION "music_offload_encode_option"
165#define AUDIO_OFFLOAD_CODEC_NUM_CHANNEL "music_offload_num_channels"
166#define AUDIO_OFFLOAD_CODEC_DOWN_SAMPLING "music_offload_down_sampling"
167#define AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES "delay_samples"
168#define AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES "padding_samples"
Eric Laurent55786bc2012-04-10 16:56:32 -0700169
Eric Laurent70e81102011-08-07 10:05:40 -0700170/**************************************/
171
Dima Zavinf1504db2011-03-11 11:20:49 -0800172/* common audio stream parameters and operations */
173struct audio_stream {
174
175 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800176 * Return the sampling rate in Hz - eg. 44100.
Dima Zavinf1504db2011-03-11 11:20:49 -0800177 */
178 uint32_t (*get_sample_rate)(const struct audio_stream *stream);
Dima Zavin57dde282011-06-06 19:31:18 -0700179
180 /* currently unused - use set_parameters with key
181 * AUDIO_PARAMETER_STREAM_SAMPLING_RATE
182 */
Dima Zavinf1504db2011-03-11 11:20:49 -0800183 int (*set_sample_rate)(struct audio_stream *stream, uint32_t rate);
184
185 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800186 * Return size of input/output buffer in bytes for this stream - eg. 4800.
187 * It should be a multiple of the frame size. See also get_input_buffer_size.
Dima Zavinf1504db2011-03-11 11:20:49 -0800188 */
189 size_t (*get_buffer_size)(const struct audio_stream *stream);
190
191 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800192 * Return the channel mask -
Dima Zavinf1504db2011-03-11 11:20:49 -0800193 * e.g. AUDIO_CHANNEL_OUT_STEREO or AUDIO_CHANNEL_IN_STEREO
194 */
Eric Laurent55786bc2012-04-10 16:56:32 -0700195 audio_channel_mask_t (*get_channels)(const struct audio_stream *stream);
Dima Zavinf1504db2011-03-11 11:20:49 -0800196
197 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800198 * Return the audio format - e.g. AUDIO_FORMAT_PCM_16_BIT
Dima Zavinf1504db2011-03-11 11:20:49 -0800199 */
Glenn Kastenfe79eb32012-01-12 14:55:57 -0800200 audio_format_t (*get_format)(const struct audio_stream *stream);
Dima Zavin57dde282011-06-06 19:31:18 -0700201
202 /* currently unused - use set_parameters with key
203 * AUDIO_PARAMETER_STREAM_FORMAT
204 */
Glenn Kastenfe79eb32012-01-12 14:55:57 -0800205 int (*set_format)(struct audio_stream *stream, audio_format_t format);
Dima Zavinf1504db2011-03-11 11:20:49 -0800206
207 /**
208 * Put the audio hardware input/output into standby mode.
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800209 * Driver should exit from standby mode at the next I/O operation.
Dima Zavinf1504db2011-03-11 11:20:49 -0800210 * Returns 0 on success and <0 on failure.
211 */
212 int (*standby)(struct audio_stream *stream);
213
214 /** dump the state of the audio input/output device */
215 int (*dump)(const struct audio_stream *stream, int fd);
216
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800217 /** Return the set of device(s) which this stream is connected to */
Dima Zavinf1504db2011-03-11 11:20:49 -0800218 audio_devices_t (*get_device)(const struct audio_stream *stream);
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800219
220 /**
221 * Currently unused - set_device() corresponds to set_parameters() with key
222 * AUDIO_PARAMETER_STREAM_ROUTING for both input and output.
223 * AUDIO_PARAMETER_STREAM_INPUT_SOURCE is an additional information used by
224 * input streams only.
225 */
Dima Zavinf1504db2011-03-11 11:20:49 -0800226 int (*set_device)(struct audio_stream *stream, audio_devices_t device);
227
228 /**
229 * set/get audio stream parameters. The function accepts a list of
230 * parameter key value pairs in the form: key1=value1;key2=value2;...
231 *
232 * Some keys are reserved for standard parameters (See AudioParameter class)
233 *
234 * If the implementation does not accept a parameter change while
235 * the output is active but the parameter is acceptable otherwise, it must
236 * return -ENOSYS.
237 *
238 * The audio flinger will put the stream in standby and then change the
239 * parameter value.
240 */
241 int (*set_parameters)(struct audio_stream *stream, const char *kv_pairs);
242
243 /*
244 * Returns a pointer to a heap allocated string. The caller is responsible
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800245 * for freeing the memory for it using free().
Dima Zavinf1504db2011-03-11 11:20:49 -0800246 */
247 char * (*get_parameters)(const struct audio_stream *stream,
248 const char *keys);
Eric Laurentf3008aa2011-06-17 16:53:12 -0700249 int (*add_audio_effect)(const struct audio_stream *stream,
250 effect_handle_t effect);
251 int (*remove_audio_effect)(const struct audio_stream *stream,
252 effect_handle_t effect);
Dima Zavinf1504db2011-03-11 11:20:49 -0800253};
254typedef struct audio_stream audio_stream_t;
255
Richard Fitzgeraldf37f1872013-03-25 16:11:44 +0000256/* type of asynchronous write callback events. Mutually exclusive */
257typedef enum {
258 STREAM_CBK_EVENT_WRITE_READY, /* non blocking write completed */
Haynes Mathew George0d468762016-07-07 20:05:39 -0700259 STREAM_CBK_EVENT_DRAIN_READY, /* drain completed */
260 STREAM_CBK_EVENT_ERROR, /* stream hit some error, let AF take action */
Richard Fitzgeraldf37f1872013-03-25 16:11:44 +0000261} stream_callback_event_t;
262
263typedef int (*stream_callback_t)(stream_callback_event_t event, void *param, void *cookie);
264
265/* type of drain requested to audio_stream_out->drain(). Mutually exclusive */
266typedef enum {
267 AUDIO_DRAIN_ALL, /* drain() returns when all data has been played */
268 AUDIO_DRAIN_EARLY_NOTIFY /* drain() returns a short time before all data
269 from the current track has been played to
270 give time for gapless track switch */
271} audio_drain_type_t;
272
Dima Zavinf1504db2011-03-11 11:20:49 -0800273/**
274 * audio_stream_out is the abstraction interface for the audio output hardware.
275 *
276 * It provides information about various properties of the audio output
277 * hardware driver.
278 */
279
280struct audio_stream_out {
Stewart Miles84d35492014-05-01 09:03:27 -0700281 /**
282 * Common methods of the audio stream out. This *must* be the first member of audio_stream_out
283 * as users of this structure will cast a audio_stream to audio_stream_out pointer in contexts
284 * where it's known the audio_stream references an audio_stream_out.
285 */
Dima Zavinf1504db2011-03-11 11:20:49 -0800286 struct audio_stream common;
287
288 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800289 * Return the audio hardware driver estimated latency in milliseconds.
Dima Zavinf1504db2011-03-11 11:20:49 -0800290 */
291 uint32_t (*get_latency)(const struct audio_stream_out *stream);
292
293 /**
294 * Use this method in situations where audio mixing is done in the
295 * hardware. This method serves as a direct interface with hardware,
296 * allowing you to directly set the volume as apposed to via the framework.
297 * This method might produce multiple PCM outputs or hardware accelerated
298 * codecs, such as MP3 or AAC.
299 */
300 int (*set_volume)(struct audio_stream_out *stream, float left, float right);
301
302 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800303 * Write audio buffer to driver. Returns number of bytes written, or a
304 * negative status_t. If at least one frame was written successfully prior to the error,
305 * it is suggested that the driver return that successful (short) byte count
306 * and then return an error in the subsequent call.
Richard Fitzgeraldf37f1872013-03-25 16:11:44 +0000307 *
308 * If set_callback() has previously been called to enable non-blocking mode
309 * the write() is not allowed to block. It must write only the number of
310 * bytes that currently fit in the driver/hardware buffer and then return
311 * this byte count. If this is less than the requested write size the
312 * callback function must be called when more space is available in the
313 * driver/hardware buffer.
Dima Zavinf1504db2011-03-11 11:20:49 -0800314 */
315 ssize_t (*write)(struct audio_stream_out *stream, const void* buffer,
316 size_t bytes);
317
318 /* return the number of audio frames written by the audio dsp to DAC since
319 * the output has exited standby
320 */
321 int (*get_render_position)(const struct audio_stream_out *stream,
322 uint32_t *dsp_frames);
Mike J. Chen5ad38a92011-08-15 12:05:00 -0700323
324 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800325 * get the local time at which the next write to the audio driver will be presented.
326 * The units are microseconds, where the epoch is decided by the local audio HAL.
Mike J. Chen5ad38a92011-08-15 12:05:00 -0700327 */
328 int (*get_next_write_timestamp)(const struct audio_stream_out *stream,
329 int64_t *timestamp);
330
Richard Fitzgeraldf37f1872013-03-25 16:11:44 +0000331 /**
332 * set the callback function for notifying completion of non-blocking
333 * write and drain.
334 * Calling this function implies that all future write() and drain()
335 * must be non-blocking and use the callback to signal completion.
336 */
337 int (*set_callback)(struct audio_stream_out *stream,
338 stream_callback_t callback, void *cookie);
339
340 /**
341 * Notifies to the audio driver to stop playback however the queued buffers are
342 * retained by the hardware. Useful for implementing pause/resume. Empty implementation
343 * if not supported however should be implemented for hardware with non-trivial
344 * latency. In the pause state audio hardware could still be using power. User may
345 * consider calling suspend after a timeout.
346 *
347 * Implementation of this function is mandatory for offloaded playback.
348 */
349 int (*pause)(struct audio_stream_out* stream);
350
351 /**
352 * Notifies to the audio driver to resume playback following a pause.
353 * Returns error if called without matching pause.
354 *
355 * Implementation of this function is mandatory for offloaded playback.
356 */
357 int (*resume)(struct audio_stream_out* stream);
358
359 /**
360 * Requests notification when data buffered by the driver/hardware has
361 * been played. If set_callback() has previously been called to enable
362 * non-blocking mode, the drain() must not block, instead it should return
363 * quickly and completion of the drain is notified through the callback.
364 * If set_callback() has not been called, the drain() must block until
365 * completion.
366 * If type==AUDIO_DRAIN_ALL, the drain completes when all previously written
367 * data has been played.
368 * If type==AUDIO_DRAIN_EARLY_NOTIFY, the drain completes shortly before all
369 * data for the current track has played to allow time for the framework
370 * to perform a gapless track switch.
371 *
372 * Drain must return immediately on stop() and flush() call
373 *
374 * Implementation of this function is mandatory for offloaded playback.
375 */
376 int (*drain)(struct audio_stream_out* stream, audio_drain_type_t type );
377
378 /**
379 * Notifies to the audio driver to flush the queued data. Stream must already
380 * be paused before calling flush().
381 *
382 * Implementation of this function is mandatory for offloaded playback.
383 */
384 int (*flush)(struct audio_stream_out* stream);
Glenn Kastene25f9ed2013-08-22 16:27:22 -0700385
386 /**
Glenn Kasten22a06b72013-09-10 09:23:07 -0700387 * Return a recent count of the number of audio frames presented to an external observer.
Glenn Kastene25f9ed2013-08-22 16:27:22 -0700388 * This excludes frames which have been written but are still in the pipeline.
389 * The count is not reset to zero when output enters standby.
390 * Also returns the value of CLOCK_MONOTONIC as of this presentation count.
Glenn Kasten22a06b72013-09-10 09:23:07 -0700391 * The returned count is expected to be 'recent',
392 * but does not need to be the most recent possible value.
393 * However, the associated time should correspond to whatever count is returned.
394 * Example: assume that N+M frames have been presented, where M is a 'small' number.
395 * Then it is permissible to return N instead of N+M,
396 * and the timestamp should correspond to N rather than N+M.
397 * The terms 'recent' and 'small' are not defined.
398 * They reflect the quality of the implementation.
Glenn Kastene25f9ed2013-08-22 16:27:22 -0700399 *
400 * 3.0 and higher only.
401 */
402 int (*get_presentation_position)(const struct audio_stream_out *stream,
403 uint64_t *frames, struct timespec *timestamp);
404
Dima Zavinf1504db2011-03-11 11:20:49 -0800405};
406typedef struct audio_stream_out audio_stream_out_t;
407
408struct audio_stream_in {
Stewart Miles84d35492014-05-01 09:03:27 -0700409 /**
410 * Common methods of the audio stream in. This *must* be the first member of audio_stream_in
411 * as users of this structure will cast a audio_stream to audio_stream_in pointer in contexts
412 * where it's known the audio_stream references an audio_stream_in.
413 */
Dima Zavinf1504db2011-03-11 11:20:49 -0800414 struct audio_stream common;
415
416 /** set the input gain for the audio driver. This method is for
417 * for future use */
418 int (*set_gain)(struct audio_stream_in *stream, float gain);
419
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800420 /** Read audio buffer in from audio driver. Returns number of bytes read, or a
421 * negative status_t. If at least one frame was read prior to the error,
422 * read should return that byte count and then return an error in the subsequent call.
423 */
Dima Zavinf1504db2011-03-11 11:20:49 -0800424 ssize_t (*read)(struct audio_stream_in *stream, void* buffer,
425 size_t bytes);
426
427 /**
428 * Return the amount of input frames lost in the audio driver since the
429 * last call of this function.
430 * Audio driver is expected to reset the value to 0 and restart counting
431 * upon returning the current value by this function call.
432 * Such loss typically occurs when the user space process is blocked
433 * longer than the capacity of audio driver buffers.
434 *
435 * Unit: the number of input audio frames
436 */
437 uint32_t (*get_input_frames_lost)(struct audio_stream_in *stream);
Andy Hung9904fab2016-01-15 17:42:36 -0800438
439 /**
440 * Return a recent count of the number of audio frames received and
441 * the clock time associated with that frame count.
442 *
443 * frames is the total frame count received. This should be as early in
444 * the capture pipeline as possible. In general,
445 * frames should be non-negative and should not go "backwards".
446 *
447 * time is the clock MONOTONIC time when frames was measured. In general,
448 * time should be a positive quantity and should not go "backwards".
449 *
450 * The status returned is 0 on success, -ENOSYS if the device is not
451 * ready/available, or -EINVAL if the arguments are null or otherwise invalid.
452 */
453 int (*get_capture_position)(const struct audio_stream_in *stream,
454 int64_t *frames, int64_t *time);
Dima Zavinf1504db2011-03-11 11:20:49 -0800455};
456typedef struct audio_stream_in audio_stream_in_t;
457
458/**
459 * return the frame size (number of bytes per sample).
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700460 *
461 * Deprecated: use audio_stream_out_frame_size() or audio_stream_in_frame_size() instead.
Dima Zavinf1504db2011-03-11 11:20:49 -0800462 */
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700463__attribute__((__deprecated__))
Glenn Kasten48915ac2012-02-20 12:08:57 -0800464static inline size_t audio_stream_frame_size(const struct audio_stream *s)
Dima Zavinf1504db2011-03-11 11:20:49 -0800465{
Glenn Kastena26cbac2012-01-13 14:53:35 -0800466 size_t chan_samp_sz;
Richard Fitzgeraldf37f1872013-03-25 16:11:44 +0000467 audio_format_t format = s->get_format(s);
Dima Zavinf1504db2011-03-11 11:20:49 -0800468
Phil Burkc3385fc2016-01-19 12:21:55 -0800469 if (audio_has_proportional_frames(format)) {
Richard Fitzgeraldf37f1872013-03-25 16:11:44 +0000470 chan_samp_sz = audio_bytes_per_sample(format);
471 return popcount(s->get_channels(s)) * chan_samp_sz;
Dima Zavinf1504db2011-03-11 11:20:49 -0800472 }
473
Richard Fitzgeraldf37f1872013-03-25 16:11:44 +0000474 return sizeof(int8_t);
Dima Zavinf1504db2011-03-11 11:20:49 -0800475}
476
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700477/**
478 * return the frame size (number of bytes per sample) of an output stream.
479 */
480static inline size_t audio_stream_out_frame_size(const struct audio_stream_out *s)
481{
482 size_t chan_samp_sz;
483 audio_format_t format = s->common.get_format(&s->common);
484
Phil Burkc3385fc2016-01-19 12:21:55 -0800485 if (audio_has_proportional_frames(format)) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700486 chan_samp_sz = audio_bytes_per_sample(format);
487 return audio_channel_count_from_out_mask(s->common.get_channels(&s->common)) * chan_samp_sz;
488 }
489
490 return sizeof(int8_t);
491}
492
493/**
494 * return the frame size (number of bytes per sample) of an input stream.
495 */
496static inline size_t audio_stream_in_frame_size(const struct audio_stream_in *s)
497{
498 size_t chan_samp_sz;
499 audio_format_t format = s->common.get_format(&s->common);
500
Phil Burkc3385fc2016-01-19 12:21:55 -0800501 if (audio_has_proportional_frames(format)) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700502 chan_samp_sz = audio_bytes_per_sample(format);
503 return audio_channel_count_from_in_mask(s->common.get_channels(&s->common)) * chan_samp_sz;
504 }
505
506 return sizeof(int8_t);
507}
Dima Zavinf1504db2011-03-11 11:20:49 -0800508
509/**********************************************************************/
510
511/**
512 * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
513 * and the fields of this data structure must begin with hw_module_t
514 * followed by module specific information.
515 */
516struct audio_module {
517 struct hw_module_t common;
518};
519
520struct audio_hw_device {
Stewart Miles84d35492014-05-01 09:03:27 -0700521 /**
522 * Common methods of the audio device. This *must* be the first member of audio_hw_device
523 * as users of this structure will cast a hw_device_t to audio_hw_device pointer in contexts
524 * where it's known the hw_device_t references an audio_hw_device.
525 */
Dima Zavinf1504db2011-03-11 11:20:49 -0800526 struct hw_device_t common;
527
528 /**
529 * used by audio flinger to enumerate what devices are supported by
530 * each audio_hw_device implementation.
531 *
532 * Return value is a bitmask of 1 or more values of audio_devices_t
Eric Laurent85e08e22012-08-28 14:30:35 -0700533 *
534 * NOTE: audio HAL implementations starting with
535 * AUDIO_DEVICE_API_VERSION_2_0 do not implement this function.
536 * All supported devices should be listed in audio_policy.conf
537 * file and the audio policy manager must choose the appropriate
538 * audio module based on information in this file.
Dima Zavinf1504db2011-03-11 11:20:49 -0800539 */
540 uint32_t (*get_supported_devices)(const struct audio_hw_device *dev);
541
542 /**
543 * check to see if the audio hardware interface has been initialized.
544 * returns 0 on success, -ENODEV on failure.
545 */
546 int (*init_check)(const struct audio_hw_device *dev);
547
548 /** set the audio volume of a voice call. Range is between 0.0 and 1.0 */
549 int (*set_voice_volume)(struct audio_hw_device *dev, float volume);
550
551 /**
552 * set the audio volume for all audio activities other than voice call.
553 * Range between 0.0 and 1.0. If any value other than 0 is returned,
554 * the software mixer will emulate this capability.
555 */
556 int (*set_master_volume)(struct audio_hw_device *dev, float volume);
557
558 /**
Mike J. Chen5ad38a92011-08-15 12:05:00 -0700559 * Get the current master volume value for the HAL, if the HAL supports
560 * master volume control. AudioFlinger will query this value from the
561 * primary audio HAL when the service starts and use the value for setting
562 * the initial master volume across all HALs. HALs which do not support
John Grossman47bf3d72012-07-17 11:54:04 -0700563 * this method may leave it set to NULL.
Mike J. Chen5ad38a92011-08-15 12:05:00 -0700564 */
565 int (*get_master_volume)(struct audio_hw_device *dev, float *volume);
566
567 /**
Glenn Kasten6df641e2012-01-09 10:41:30 -0800568 * set_mode is called when the audio mode changes. AUDIO_MODE_NORMAL mode
Dima Zavinf1504db2011-03-11 11:20:49 -0800569 * is for standard audio playback, AUDIO_MODE_RINGTONE when a ringtone is
570 * playing, and AUDIO_MODE_IN_CALL when a call is in progress.
Dima Zavinf1504db2011-03-11 11:20:49 -0800571 */
Glenn Kasten6df641e2012-01-09 10:41:30 -0800572 int (*set_mode)(struct audio_hw_device *dev, audio_mode_t mode);
Dima Zavinf1504db2011-03-11 11:20:49 -0800573
574 /* mic mute */
575 int (*set_mic_mute)(struct audio_hw_device *dev, bool state);
576 int (*get_mic_mute)(const struct audio_hw_device *dev, bool *state);
577
578 /* set/get global audio parameters */
579 int (*set_parameters)(struct audio_hw_device *dev, const char *kv_pairs);
580
581 /*
582 * Returns a pointer to a heap allocated string. The caller is responsible
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800583 * for freeing the memory for it using free().
Dima Zavinf1504db2011-03-11 11:20:49 -0800584 */
585 char * (*get_parameters)(const struct audio_hw_device *dev,
586 const char *keys);
587
588 /* Returns audio input buffer size according to parameters passed or
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800589 * 0 if one of the parameters is not supported.
590 * See also get_buffer_size which is for a particular stream.
Dima Zavinf1504db2011-03-11 11:20:49 -0800591 */
592 size_t (*get_input_buffer_size)(const struct audio_hw_device *dev,
Eric Laurent55786bc2012-04-10 16:56:32 -0700593 const struct audio_config *config);
Dima Zavinf1504db2011-03-11 11:20:49 -0800594
Eric Laurentf5e24692014-07-27 16:14:57 -0700595 /** This method creates and opens the audio hardware output stream.
596 * The "address" parameter qualifies the "devices" audio device type if needed.
597 * The format format depends on the device type:
598 * - Bluetooth devices use the MAC address of the device in the form "00:11:22:AA:BB:CC"
599 * - USB devices use the ALSA card and device numbers in the form "card=X;device=Y"
600 * - Other devices may use a number or any other string.
601 */
602
Eric Laurent55786bc2012-04-10 16:56:32 -0700603 int (*open_output_stream)(struct audio_hw_device *dev,
604 audio_io_handle_t handle,
605 audio_devices_t devices,
606 audio_output_flags_t flags,
607 struct audio_config *config,
Eric Laurentf5e24692014-07-27 16:14:57 -0700608 struct audio_stream_out **stream_out,
609 const char *address);
Dima Zavinf1504db2011-03-11 11:20:49 -0800610
611 void (*close_output_stream)(struct audio_hw_device *dev,
Eric Laurent55786bc2012-04-10 16:56:32 -0700612 struct audio_stream_out* stream_out);
Dima Zavinf1504db2011-03-11 11:20:49 -0800613
614 /** This method creates and opens the audio hardware input stream */
Eric Laurent55786bc2012-04-10 16:56:32 -0700615 int (*open_input_stream)(struct audio_hw_device *dev,
616 audio_io_handle_t handle,
617 audio_devices_t devices,
618 struct audio_config *config,
Glenn Kasten7d973ad2014-07-15 11:10:38 -0700619 struct audio_stream_in **stream_in,
Eric Laurentf5e24692014-07-27 16:14:57 -0700620 audio_input_flags_t flags,
621 const char *address,
622 audio_source_t source);
Dima Zavinf1504db2011-03-11 11:20:49 -0800623
624 void (*close_input_stream)(struct audio_hw_device *dev,
Eric Laurent55786bc2012-04-10 16:56:32 -0700625 struct audio_stream_in *stream_in);
Dima Zavinf1504db2011-03-11 11:20:49 -0800626
627 /** This method dumps the state of the audio hardware */
628 int (*dump)(const struct audio_hw_device *dev, int fd);
John Grossman47bf3d72012-07-17 11:54:04 -0700629
630 /**
631 * set the audio mute status for all audio activities. If any value other
632 * than 0 is returned, the software mixer will emulate this capability.
633 */
634 int (*set_master_mute)(struct audio_hw_device *dev, bool mute);
635
636 /**
637 * Get the current master mute status for the HAL, if the HAL supports
638 * master mute control. AudioFlinger will query this value from the primary
639 * audio HAL when the service starts and use the value for setting the
640 * initial master mute across all HALs. HALs which do not support this
641 * method may leave it set to NULL.
642 */
643 int (*get_master_mute)(struct audio_hw_device *dev, bool *mute);
Eric Laurent73b8a742014-05-22 14:02:38 -0700644
645 /**
646 * Routing control
647 */
648
649 /* Creates an audio patch between several source and sink ports.
650 * The handle is allocated by the HAL and should be unique for this
651 * audio HAL module. */
652 int (*create_audio_patch)(struct audio_hw_device *dev,
653 unsigned int num_sources,
654 const struct audio_port_config *sources,
655 unsigned int num_sinks,
656 const struct audio_port_config *sinks,
657 audio_patch_handle_t *handle);
658
659 /* Release an audio patch */
660 int (*release_audio_patch)(struct audio_hw_device *dev,
661 audio_patch_handle_t handle);
662
663 /* Fills the list of supported attributes for a given audio port.
664 * As input, "port" contains the information (type, role, address etc...)
665 * needed by the HAL to identify the port.
666 * As output, "port" contains possible attributes (sampling rates, formats,
667 * channel masks, gain controllers...) for this port.
668 */
669 int (*get_audio_port)(struct audio_hw_device *dev,
670 struct audio_port *port);
671
672 /* Set audio port configuration */
673 int (*set_audio_port_config)(struct audio_hw_device *dev,
674 const struct audio_port_config *config);
675
Dima Zavinf1504db2011-03-11 11:20:49 -0800676};
677typedef struct audio_hw_device audio_hw_device_t;
678
679/** convenience API for opening and closing a supported device */
680
681static inline int audio_hw_device_open(const struct hw_module_t* module,
682 struct audio_hw_device** device)
683{
684 return module->methods->open(module, AUDIO_HARDWARE_INTERFACE,
Colin Crosscc8d9f92016-10-06 16:44:23 -0700685 TO_HW_DEVICE_T_OPEN(device));
Dima Zavinf1504db2011-03-11 11:20:49 -0800686}
687
688static inline int audio_hw_device_close(struct audio_hw_device* device)
689{
690 return device->common.close(&device->common);
691}
692
693
694__END_DECLS
695
696#endif // ANDROID_AUDIO_INTERFACE_H