blob: c400c74396ba2f4c77e82482b37e9a34baa8754b [file] [log] [blame]
Dima Zavinf1504db2011-03-11 11:20:49 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
Bharath Ramachandramurthye41db452014-01-14 07:28:09 -08003 * Copyright (c) 2011-2014, The Linux Foundation. All rights reserved.
4 * Not a Contribution.
Dima Zavinf1504db2011-03-11 11:20:49 -08005 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19
20#ifndef ANDROID_AUDIO_HAL_INTERFACE_H
21#define ANDROID_AUDIO_HAL_INTERFACE_H
22
23#include <stdint.h>
24#include <strings.h>
25#include <sys/cdefs.h>
26#include <sys/types.h>
27
28#include <cutils/bitops.h>
29
30#include <hardware/hardware.h>
Dima Zavinaa211722011-05-11 14:15:53 -070031#include <system/audio.h>
Eric Laurentf3008aa2011-06-17 16:53:12 -070032#include <hardware/audio_effect.h>
Kiran Kandi807edfa2013-10-17 15:49:54 -070033#ifdef AUDIO_LISTEN_ENABLED
34#include <listen_types.h>
35#endif
Dima Zavinf1504db2011-03-11 11:20:49 -080036
37__BEGIN_DECLS
38
39/**
40 * The id of this module
41 */
42#define AUDIO_HARDWARE_MODULE_ID "audio"
43
44/**
45 * Name of the audio devices to open
46 */
47#define AUDIO_HARDWARE_INTERFACE "audio_hw_if"
48
Eric Laurent55786bc2012-04-10 16:56:32 -070049
50/* Use version 0.1 to be compatible with first generation of audio hw module with version_major
51 * hardcoded to 1. No audio module API change.
52 */
53#define AUDIO_MODULE_API_VERSION_0_1 HARDWARE_MODULE_API_VERSION(0, 1)
54#define AUDIO_MODULE_API_VERSION_CURRENT AUDIO_MODULE_API_VERSION_0_1
55
56/* First generation of audio devices had version hardcoded to 0. all devices with versions < 1.0
57 * will be considered of first generation API.
58 */
59#define AUDIO_DEVICE_API_VERSION_0_0 HARDWARE_DEVICE_API_VERSION(0, 0)
60#define AUDIO_DEVICE_API_VERSION_1_0 HARDWARE_DEVICE_API_VERSION(1, 0)
Eric Laurent85e08e22012-08-28 14:30:35 -070061#define AUDIO_DEVICE_API_VERSION_2_0 HARDWARE_DEVICE_API_VERSION(2, 0)
Eric Laurent73b8a742014-05-22 14:02:38 -070062#define AUDIO_DEVICE_API_VERSION_3_0 HARDWARE_DEVICE_API_VERSION(3, 0)
63#define AUDIO_DEVICE_API_VERSION_CURRENT AUDIO_DEVICE_API_VERSION_3_0
Eric Laurent447cae72014-05-22 13:45:55 -070064/* Minimal audio HAL version supported by the audio framework */
65#define AUDIO_DEVICE_API_VERSION_MIN AUDIO_DEVICE_API_VERSION_2_0
Eric Laurent55786bc2012-04-10 16:56:32 -070066
Eric Laurent431fc782012-04-03 12:07:02 -070067/**
68 * List of known audio HAL modules. This is the base name of the audio HAL
69 * library composed of the "audio." prefix, one of the base names below and
70 * a suffix specific to the device.
71 * e.g: audio.primary.goldfish.so or audio.a2dp.default.so
72 */
73
74#define AUDIO_HARDWARE_MODULE_ID_PRIMARY "primary"
75#define AUDIO_HARDWARE_MODULE_ID_A2DP "a2dp"
76#define AUDIO_HARDWARE_MODULE_ID_USB "usb"
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070077#define AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX "r_submix"
Richard Fitzgeraldf37f1872013-03-25 16:11:44 +000078#define AUDIO_HARDWARE_MODULE_ID_CODEC_OFFLOAD "codec_offload"
Eric Laurent431fc782012-04-03 12:07:02 -070079
Dima Zavinf1504db2011-03-11 11:20:49 -080080/**************************************/
81
Eric Laurent70e81102011-08-07 10:05:40 -070082/**
83 * standard audio parameters that the HAL may need to handle
84 */
85
86/**
87 * audio device parameters
88 */
89
Eric Laurented9928c2011-08-02 17:12:00 -070090/* BT SCO Noise Reduction + Echo Cancellation parameters */
91#define AUDIO_PARAMETER_KEY_BT_NREC "bt_headset_nrec"
92#define AUDIO_PARAMETER_VALUE_ON "on"
93#define AUDIO_PARAMETER_VALUE_OFF "off"
94
Eric Laurent70e81102011-08-07 10:05:40 -070095/* TTY mode selection */
96#define AUDIO_PARAMETER_KEY_TTY_MODE "tty_mode"
97#define AUDIO_PARAMETER_VALUE_TTY_OFF "tty_off"
98#define AUDIO_PARAMETER_VALUE_TTY_VCO "tty_vco"
99#define AUDIO_PARAMETER_VALUE_TTY_HCO "tty_hco"
100#define AUDIO_PARAMETER_VALUE_TTY_FULL "tty_full"
101
Eric Laurentd1a1b1c2014-07-25 12:10:11 -0500102/* Hearing Aid Compatibility - Telecoil (HAC-T) mode on/off
103 Strings must be in sync with CallFeaturesSetting.java */
104#define AUDIO_PARAMETER_KEY_HAC "HACSetting"
105#define AUDIO_PARAMETER_VALUE_HAC_ON "ON"
106#define AUDIO_PARAMETER_VALUE_HAC_OFF "OFF"
107
Eric Laurenta70c5d02012-03-07 18:59:47 -0800108/* A2DP sink address set by framework */
109#define AUDIO_PARAMETER_A2DP_SINK_ADDRESS "a2dp_sink_address"
110
Mike Lockwood2d4d9652014-05-28 11:09:54 -0700111/* A2DP source address set by framework */
112#define AUDIO_PARAMETER_A2DP_SOURCE_ADDRESS "a2dp_source_address"
113
Glenn Kasten34afb682012-06-08 10:49:34 -0700114/* Screen state */
115#define AUDIO_PARAMETER_KEY_SCREEN_STATE "screen_state"
116
Glenn Kastend930d922014-04-29 13:35:57 -0700117/* Bluetooth SCO wideband */
118#define AUDIO_PARAMETER_KEY_BT_SCO_WB "bt_wbs"
119
Eric Laurentbc19a3d2014-10-17 18:17:51 -0700120/* Get a new HW synchronization source identifier.
121 * Return a valid source (positive integer) or AUDIO_HW_SYNC_INVALID if an error occurs
122 * or no HW sync is available. */
123#define AUDIO_PARAMETER_HW_AV_SYNC "hw_av_sync"
Eric Laurent4ea9b952014-08-01 14:42:44 -0700124
Eric Laurent70e81102011-08-07 10:05:40 -0700125/**
126 * audio stream parameters
127 */
128
Eric Laurentf5e24692014-07-27 16:14:57 -0700129#define AUDIO_PARAMETER_STREAM_ROUTING "routing" /* audio_devices_t */
130#define AUDIO_PARAMETER_STREAM_FORMAT "format" /* audio_format_t */
131#define AUDIO_PARAMETER_STREAM_CHANNELS "channels" /* audio_channel_mask_t */
132#define AUDIO_PARAMETER_STREAM_FRAME_COUNT "frame_count" /* size_t */
133#define AUDIO_PARAMETER_STREAM_INPUT_SOURCE "input_source" /* audio_source_t */
134#define AUDIO_PARAMETER_STREAM_SAMPLING_RATE "sampling_rate" /* uint32_t */
Dima Zavin57dde282011-06-06 19:31:18 -0700135
Haynes Mathew George845569f2014-09-24 19:10:12 -0700136#define AUDIO_PARAMETER_DEVICE_CONNECT "connect" /* audio_devices_t */
Paul McLean2c6196f2014-08-20 16:50:25 -0700137#define AUDIO_PARAMETER_DEVICE_DISCONNECT "disconnect" /* audio_devices_t */
138
Eric Laurent41eeb4f2012-05-17 18:54:49 -0700139/* Query supported formats. The response is a '|' separated list of strings from
140 * audio_format_t enum e.g: "sup_formats=AUDIO_FORMAT_PCM_16_BIT" */
141#define AUDIO_PARAMETER_STREAM_SUP_FORMATS "sup_formats"
142/* Query supported channel masks. The response is a '|' separated list of strings from
143 * audio_channel_mask_t enum e.g: "sup_channels=AUDIO_CHANNEL_OUT_STEREO|AUDIO_CHANNEL_OUT_MONO" */
144#define AUDIO_PARAMETER_STREAM_SUP_CHANNELS "sup_channels"
145/* Query supported sampling rates. The response is a '|' separated list of integer values e.g:
146 * "sup_sampling_rates=44100|48000" */
147#define AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES "sup_sampling_rates"
148
Eric Laurentbc19a3d2014-10-17 18:17:51 -0700149/* Set the HW synchronization source for an output stream. */
Eric Laurent4ea9b952014-08-01 14:42:44 -0700150#define AUDIO_PARAMETER_STREAM_HW_AV_SYNC "hw_av_sync"
151
Richard Fitzgeraldf37f1872013-03-25 16:11:44 +0000152/**
153 * audio codec parameters
154 */
155
156#define AUDIO_OFFLOAD_CODEC_PARAMS "music_offload_codec_param"
157#define AUDIO_OFFLOAD_CODEC_BIT_PER_SAMPLE "music_offload_bit_per_sample"
158#define AUDIO_OFFLOAD_CODEC_BIT_RATE "music_offload_bit_rate"
159#define AUDIO_OFFLOAD_CODEC_AVG_BIT_RATE "music_offload_avg_bit_rate"
160#define AUDIO_OFFLOAD_CODEC_ID "music_offload_codec_id"
161#define AUDIO_OFFLOAD_CODEC_BLOCK_ALIGN "music_offload_block_align"
162#define AUDIO_OFFLOAD_CODEC_SAMPLE_RATE "music_offload_sample_rate"
163#define AUDIO_OFFLOAD_CODEC_ENCODE_OPTION "music_offload_encode_option"
164#define AUDIO_OFFLOAD_CODEC_NUM_CHANNEL "music_offload_num_channels"
165#define AUDIO_OFFLOAD_CODEC_DOWN_SAMPLING "music_offload_down_sampling"
166#define AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES "delay_samples"
167#define AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES "padding_samples"
Eric Laurent55786bc2012-04-10 16:56:32 -0700168
Eric Laurent70e81102011-08-07 10:05:40 -0700169/**************************************/
170
Dima Zavinf1504db2011-03-11 11:20:49 -0800171/* common audio stream parameters and operations */
172struct audio_stream {
173
174 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800175 * Return the sampling rate in Hz - eg. 44100.
Dima Zavinf1504db2011-03-11 11:20:49 -0800176 */
177 uint32_t (*get_sample_rate)(const struct audio_stream *stream);
Dima Zavin57dde282011-06-06 19:31:18 -0700178
179 /* currently unused - use set_parameters with key
180 * AUDIO_PARAMETER_STREAM_SAMPLING_RATE
181 */
Dima Zavinf1504db2011-03-11 11:20:49 -0800182 int (*set_sample_rate)(struct audio_stream *stream, uint32_t rate);
183
184 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800185 * Return size of input/output buffer in bytes for this stream - eg. 4800.
186 * It should be a multiple of the frame size. See also get_input_buffer_size.
Dima Zavinf1504db2011-03-11 11:20:49 -0800187 */
188 size_t (*get_buffer_size)(const struct audio_stream *stream);
189
190 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800191 * Return the channel mask -
Dima Zavinf1504db2011-03-11 11:20:49 -0800192 * e.g. AUDIO_CHANNEL_OUT_STEREO or AUDIO_CHANNEL_IN_STEREO
193 */
Eric Laurent55786bc2012-04-10 16:56:32 -0700194 audio_channel_mask_t (*get_channels)(const struct audio_stream *stream);
Dima Zavinf1504db2011-03-11 11:20:49 -0800195
196 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800197 * Return the audio format - e.g. AUDIO_FORMAT_PCM_16_BIT
Dima Zavinf1504db2011-03-11 11:20:49 -0800198 */
Glenn Kastenfe79eb32012-01-12 14:55:57 -0800199 audio_format_t (*get_format)(const struct audio_stream *stream);
Dima Zavin57dde282011-06-06 19:31:18 -0700200
201 /* currently unused - use set_parameters with key
202 * AUDIO_PARAMETER_STREAM_FORMAT
203 */
Glenn Kastenfe79eb32012-01-12 14:55:57 -0800204 int (*set_format)(struct audio_stream *stream, audio_format_t format);
Dima Zavinf1504db2011-03-11 11:20:49 -0800205
206 /**
207 * Put the audio hardware input/output into standby mode.
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800208 * Driver should exit from standby mode at the next I/O operation.
Dima Zavinf1504db2011-03-11 11:20:49 -0800209 * Returns 0 on success and <0 on failure.
210 */
211 int (*standby)(struct audio_stream *stream);
212
213 /** dump the state of the audio input/output device */
214 int (*dump)(const struct audio_stream *stream, int fd);
215
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800216 /** Return the set of device(s) which this stream is connected to */
Dima Zavinf1504db2011-03-11 11:20:49 -0800217 audio_devices_t (*get_device)(const struct audio_stream *stream);
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800218
219 /**
220 * Currently unused - set_device() corresponds to set_parameters() with key
221 * AUDIO_PARAMETER_STREAM_ROUTING for both input and output.
222 * AUDIO_PARAMETER_STREAM_INPUT_SOURCE is an additional information used by
223 * input streams only.
224 */
Dima Zavinf1504db2011-03-11 11:20:49 -0800225 int (*set_device)(struct audio_stream *stream, audio_devices_t device);
226
227 /**
228 * set/get audio stream parameters. The function accepts a list of
229 * parameter key value pairs in the form: key1=value1;key2=value2;...
230 *
231 * Some keys are reserved for standard parameters (See AudioParameter class)
232 *
233 * If the implementation does not accept a parameter change while
234 * the output is active but the parameter is acceptable otherwise, it must
235 * return -ENOSYS.
236 *
237 * The audio flinger will put the stream in standby and then change the
238 * parameter value.
239 */
240 int (*set_parameters)(struct audio_stream *stream, const char *kv_pairs);
241
242 /*
243 * Returns a pointer to a heap allocated string. The caller is responsible
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800244 * for freeing the memory for it using free().
Dima Zavinf1504db2011-03-11 11:20:49 -0800245 */
246 char * (*get_parameters)(const struct audio_stream *stream,
247 const char *keys);
Eric Laurentf3008aa2011-06-17 16:53:12 -0700248 int (*add_audio_effect)(const struct audio_stream *stream,
249 effect_handle_t effect);
250 int (*remove_audio_effect)(const struct audio_stream *stream,
251 effect_handle_t effect);
Dima Zavinf1504db2011-03-11 11:20:49 -0800252};
253typedef struct audio_stream audio_stream_t;
254
Richard Fitzgeraldf37f1872013-03-25 16:11:44 +0000255/* type of asynchronous write callback events. Mutually exclusive */
256typedef enum {
257 STREAM_CBK_EVENT_WRITE_READY, /* non blocking write completed */
258 STREAM_CBK_EVENT_DRAIN_READY /* drain completed */
259} stream_callback_event_t;
260
261typedef int (*stream_callback_t)(stream_callback_event_t event, void *param, void *cookie);
262
263/* type of drain requested to audio_stream_out->drain(). Mutually exclusive */
264typedef enum {
265 AUDIO_DRAIN_ALL, /* drain() returns when all data has been played */
266 AUDIO_DRAIN_EARLY_NOTIFY /* drain() returns a short time before all data
267 from the current track has been played to
268 give time for gapless track switch */
269} audio_drain_type_t;
270
Dima Zavinf1504db2011-03-11 11:20:49 -0800271/**
272 * audio_stream_out is the abstraction interface for the audio output hardware.
273 *
274 * It provides information about various properties of the audio output
275 * hardware driver.
276 */
277
278struct audio_stream_out {
Stewart Miles84d35492014-05-01 09:03:27 -0700279 /**
280 * Common methods of the audio stream out. This *must* be the first member of audio_stream_out
281 * as users of this structure will cast a audio_stream to audio_stream_out pointer in contexts
282 * where it's known the audio_stream references an audio_stream_out.
283 */
Dima Zavinf1504db2011-03-11 11:20:49 -0800284 struct audio_stream common;
285
286 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800287 * Return the audio hardware driver estimated latency in milliseconds.
Dima Zavinf1504db2011-03-11 11:20:49 -0800288 */
289 uint32_t (*get_latency)(const struct audio_stream_out *stream);
290
291 /**
292 * Use this method in situations where audio mixing is done in the
293 * hardware. This method serves as a direct interface with hardware,
294 * allowing you to directly set the volume as apposed to via the framework.
295 * This method might produce multiple PCM outputs or hardware accelerated
296 * codecs, such as MP3 or AAC.
297 */
298 int (*set_volume)(struct audio_stream_out *stream, float left, float right);
299
300 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800301 * Write audio buffer to driver. Returns number of bytes written, or a
302 * negative status_t. If at least one frame was written successfully prior to the error,
303 * it is suggested that the driver return that successful (short) byte count
304 * and then return an error in the subsequent call.
Richard Fitzgeraldf37f1872013-03-25 16:11:44 +0000305 *
306 * If set_callback() has previously been called to enable non-blocking mode
307 * the write() is not allowed to block. It must write only the number of
308 * bytes that currently fit in the driver/hardware buffer and then return
309 * this byte count. If this is less than the requested write size the
310 * callback function must be called when more space is available in the
311 * driver/hardware buffer.
Dima Zavinf1504db2011-03-11 11:20:49 -0800312 */
313 ssize_t (*write)(struct audio_stream_out *stream, const void* buffer,
314 size_t bytes);
315
316 /* return the number of audio frames written by the audio dsp to DAC since
317 * the output has exited standby
318 */
319 int (*get_render_position)(const struct audio_stream_out *stream,
320 uint32_t *dsp_frames);
Mike J. Chen5ad38a92011-08-15 12:05:00 -0700321
322 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800323 * get the local time at which the next write to the audio driver will be presented.
324 * The units are microseconds, where the epoch is decided by the local audio HAL.
Mike J. Chen5ad38a92011-08-15 12:05:00 -0700325 */
326 int (*get_next_write_timestamp)(const struct audio_stream_out *stream,
327 int64_t *timestamp);
328
Richard Fitzgeraldf37f1872013-03-25 16:11:44 +0000329 /**
330 * set the callback function for notifying completion of non-blocking
331 * write and drain.
332 * Calling this function implies that all future write() and drain()
333 * must be non-blocking and use the callback to signal completion.
334 */
335 int (*set_callback)(struct audio_stream_out *stream,
336 stream_callback_t callback, void *cookie);
337
338 /**
339 * Notifies to the audio driver to stop playback however the queued buffers are
340 * retained by the hardware. Useful for implementing pause/resume. Empty implementation
341 * if not supported however should be implemented for hardware with non-trivial
342 * latency. In the pause state audio hardware could still be using power. User may
343 * consider calling suspend after a timeout.
344 *
345 * Implementation of this function is mandatory for offloaded playback.
346 */
347 int (*pause)(struct audio_stream_out* stream);
348
349 /**
350 * Notifies to the audio driver to resume playback following a pause.
351 * Returns error if called without matching pause.
352 *
353 * Implementation of this function is mandatory for offloaded playback.
354 */
355 int (*resume)(struct audio_stream_out* stream);
356
357 /**
358 * Requests notification when data buffered by the driver/hardware has
359 * been played. If set_callback() has previously been called to enable
360 * non-blocking mode, the drain() must not block, instead it should return
361 * quickly and completion of the drain is notified through the callback.
362 * If set_callback() has not been called, the drain() must block until
363 * completion.
364 * If type==AUDIO_DRAIN_ALL, the drain completes when all previously written
365 * data has been played.
366 * If type==AUDIO_DRAIN_EARLY_NOTIFY, the drain completes shortly before all
367 * data for the current track has played to allow time for the framework
368 * to perform a gapless track switch.
369 *
370 * Drain must return immediately on stop() and flush() call
371 *
372 * Implementation of this function is mandatory for offloaded playback.
373 */
374 int (*drain)(struct audio_stream_out* stream, audio_drain_type_t type );
375
376 /**
377 * Notifies to the audio driver to flush the queued data. Stream must already
378 * be paused before calling flush().
379 *
380 * Implementation of this function is mandatory for offloaded playback.
381 */
382 int (*flush)(struct audio_stream_out* stream);
Glenn Kastene25f9ed2013-08-22 16:27:22 -0700383
384 /**
Glenn Kasten22a06b72013-09-10 09:23:07 -0700385 * Return a recent count of the number of audio frames presented to an external observer.
Glenn Kastene25f9ed2013-08-22 16:27:22 -0700386 * This excludes frames which have been written but are still in the pipeline.
387 * The count is not reset to zero when output enters standby.
388 * Also returns the value of CLOCK_MONOTONIC as of this presentation count.
Glenn Kasten22a06b72013-09-10 09:23:07 -0700389 * The returned count is expected to be 'recent',
390 * but does not need to be the most recent possible value.
391 * However, the associated time should correspond to whatever count is returned.
392 * Example: assume that N+M frames have been presented, where M is a 'small' number.
393 * Then it is permissible to return N instead of N+M,
394 * and the timestamp should correspond to N rather than N+M.
395 * The terms 'recent' and 'small' are not defined.
396 * They reflect the quality of the implementation.
Glenn Kastene25f9ed2013-08-22 16:27:22 -0700397 *
398 * 3.0 and higher only.
399 */
400 int (*get_presentation_position)(const struct audio_stream_out *stream,
401 uint64_t *frames, struct timespec *timestamp);
402
Dima Zavinf1504db2011-03-11 11:20:49 -0800403};
404typedef struct audio_stream_out audio_stream_out_t;
405
406struct audio_stream_in {
Stewart Miles84d35492014-05-01 09:03:27 -0700407 /**
408 * Common methods of the audio stream in. This *must* be the first member of audio_stream_in
409 * as users of this structure will cast a audio_stream to audio_stream_in pointer in contexts
410 * where it's known the audio_stream references an audio_stream_in.
411 */
Dima Zavinf1504db2011-03-11 11:20:49 -0800412 struct audio_stream common;
413
414 /** set the input gain for the audio driver. This method is for
415 * for future use */
416 int (*set_gain)(struct audio_stream_in *stream, float gain);
417
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800418 /** Read audio buffer in from audio driver. Returns number of bytes read, or a
419 * negative status_t. If at least one frame was read prior to the error,
420 * read should return that byte count and then return an error in the subsequent call.
421 */
Dima Zavinf1504db2011-03-11 11:20:49 -0800422 ssize_t (*read)(struct audio_stream_in *stream, void* buffer,
423 size_t bytes);
424
425 /**
426 * Return the amount of input frames lost in the audio driver since the
427 * last call of this function.
428 * Audio driver is expected to reset the value to 0 and restart counting
429 * upon returning the current value by this function call.
430 * Such loss typically occurs when the user space process is blocked
431 * longer than the capacity of audio driver buffers.
432 *
433 * Unit: the number of input audio frames
434 */
435 uint32_t (*get_input_frames_lost)(struct audio_stream_in *stream);
436};
437typedef struct audio_stream_in audio_stream_in_t;
438
439/**
440 * return the frame size (number of bytes per sample).
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700441 *
442 * Deprecated: use audio_stream_out_frame_size() or audio_stream_in_frame_size() instead.
Dima Zavinf1504db2011-03-11 11:20:49 -0800443 */
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700444__attribute__((__deprecated__))
Glenn Kasten48915ac2012-02-20 12:08:57 -0800445static inline size_t audio_stream_frame_size(const struct audio_stream *s)
Dima Zavinf1504db2011-03-11 11:20:49 -0800446{
Glenn Kastena26cbac2012-01-13 14:53:35 -0800447 size_t chan_samp_sz;
Richard Fitzgeraldf37f1872013-03-25 16:11:44 +0000448 audio_format_t format = s->get_format(s);
Dima Zavinf1504db2011-03-11 11:20:49 -0800449
Mingming Yinb9966742014-05-22 15:37:22 -0700450 if (audio_is_linear_pcm(format) &&
451 format != AUDIO_FORMAT_PCM_8_24_BIT) {
Richard Fitzgeraldf37f1872013-03-25 16:11:44 +0000452 chan_samp_sz = audio_bytes_per_sample(format);
453 return popcount(s->get_channels(s)) * chan_samp_sz;
Dima Zavinf1504db2011-03-11 11:20:49 -0800454 }
455
Richard Fitzgeraldf37f1872013-03-25 16:11:44 +0000456 return sizeof(int8_t);
Dima Zavinf1504db2011-03-11 11:20:49 -0800457}
458
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700459/**
460 * return the frame size (number of bytes per sample) of an output stream.
461 */
462static inline size_t audio_stream_out_frame_size(const struct audio_stream_out *s)
463{
464 size_t chan_samp_sz;
465 audio_format_t format = s->common.get_format(&s->common);
466
467 if (audio_is_linear_pcm(format)) {
468 chan_samp_sz = audio_bytes_per_sample(format);
469 return audio_channel_count_from_out_mask(s->common.get_channels(&s->common)) * chan_samp_sz;
470 }
471
472 return sizeof(int8_t);
473}
474
475/**
476 * return the frame size (number of bytes per sample) of an input stream.
477 */
478static inline size_t audio_stream_in_frame_size(const struct audio_stream_in *s)
479{
480 size_t chan_samp_sz;
481 audio_format_t format = s->common.get_format(&s->common);
482
483 if (audio_is_linear_pcm(format)) {
484 chan_samp_sz = audio_bytes_per_sample(format);
485 return audio_channel_count_from_in_mask(s->common.get_channels(&s->common)) * chan_samp_sz;
486 }
487
488 return sizeof(int8_t);
489}
Dima Zavinf1504db2011-03-11 11:20:49 -0800490
491/**********************************************************************/
492
493/**
494 * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
495 * and the fields of this data structure must begin with hw_module_t
496 * followed by module specific information.
497 */
498struct audio_module {
499 struct hw_module_t common;
500};
501
502struct audio_hw_device {
Stewart Miles84d35492014-05-01 09:03:27 -0700503 /**
504 * Common methods of the audio device. This *must* be the first member of audio_hw_device
505 * as users of this structure will cast a hw_device_t to audio_hw_device pointer in contexts
506 * where it's known the hw_device_t references an audio_hw_device.
507 */
Dima Zavinf1504db2011-03-11 11:20:49 -0800508 struct hw_device_t common;
509
510 /**
511 * used by audio flinger to enumerate what devices are supported by
512 * each audio_hw_device implementation.
513 *
514 * Return value is a bitmask of 1 or more values of audio_devices_t
Eric Laurent85e08e22012-08-28 14:30:35 -0700515 *
516 * NOTE: audio HAL implementations starting with
517 * AUDIO_DEVICE_API_VERSION_2_0 do not implement this function.
518 * All supported devices should be listed in audio_policy.conf
519 * file and the audio policy manager must choose the appropriate
520 * audio module based on information in this file.
Dima Zavinf1504db2011-03-11 11:20:49 -0800521 */
522 uint32_t (*get_supported_devices)(const struct audio_hw_device *dev);
523
524 /**
525 * check to see if the audio hardware interface has been initialized.
526 * returns 0 on success, -ENODEV on failure.
527 */
528 int (*init_check)(const struct audio_hw_device *dev);
529
530 /** set the audio volume of a voice call. Range is between 0.0 and 1.0 */
531 int (*set_voice_volume)(struct audio_hw_device *dev, float volume);
532
533 /**
534 * set the audio volume for all audio activities other than voice call.
535 * Range between 0.0 and 1.0. If any value other than 0 is returned,
536 * the software mixer will emulate this capability.
537 */
538 int (*set_master_volume)(struct audio_hw_device *dev, float volume);
539
540 /**
Mike J. Chen5ad38a92011-08-15 12:05:00 -0700541 * Get the current master volume value for the HAL, if the HAL supports
542 * master volume control. AudioFlinger will query this value from the
543 * primary audio HAL when the service starts and use the value for setting
544 * the initial master volume across all HALs. HALs which do not support
John Grossman47bf3d72012-07-17 11:54:04 -0700545 * this method may leave it set to NULL.
Mike J. Chen5ad38a92011-08-15 12:05:00 -0700546 */
547 int (*get_master_volume)(struct audio_hw_device *dev, float *volume);
548
549 /**
Glenn Kasten6df641e2012-01-09 10:41:30 -0800550 * set_mode is called when the audio mode changes. AUDIO_MODE_NORMAL mode
Dima Zavinf1504db2011-03-11 11:20:49 -0800551 * is for standard audio playback, AUDIO_MODE_RINGTONE when a ringtone is
552 * playing, and AUDIO_MODE_IN_CALL when a call is in progress.
Dima Zavinf1504db2011-03-11 11:20:49 -0800553 */
Glenn Kasten6df641e2012-01-09 10:41:30 -0800554 int (*set_mode)(struct audio_hw_device *dev, audio_mode_t mode);
Dima Zavinf1504db2011-03-11 11:20:49 -0800555
556 /* mic mute */
557 int (*set_mic_mute)(struct audio_hw_device *dev, bool state);
558 int (*get_mic_mute)(const struct audio_hw_device *dev, bool *state);
559
560 /* set/get global audio parameters */
561 int (*set_parameters)(struct audio_hw_device *dev, const char *kv_pairs);
562
563 /*
564 * Returns a pointer to a heap allocated string. The caller is responsible
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800565 * for freeing the memory for it using free().
Dima Zavinf1504db2011-03-11 11:20:49 -0800566 */
567 char * (*get_parameters)(const struct audio_hw_device *dev,
568 const char *keys);
569
570 /* Returns audio input buffer size according to parameters passed or
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800571 * 0 if one of the parameters is not supported.
572 * See also get_buffer_size which is for a particular stream.
Dima Zavinf1504db2011-03-11 11:20:49 -0800573 */
574 size_t (*get_input_buffer_size)(const struct audio_hw_device *dev,
Eric Laurent55786bc2012-04-10 16:56:32 -0700575 const struct audio_config *config);
Dima Zavinf1504db2011-03-11 11:20:49 -0800576
Eric Laurentf5e24692014-07-27 16:14:57 -0700577 /** This method creates and opens the audio hardware output stream.
578 * The "address" parameter qualifies the "devices" audio device type if needed.
579 * The format format depends on the device type:
580 * - Bluetooth devices use the MAC address of the device in the form "00:11:22:AA:BB:CC"
581 * - USB devices use the ALSA card and device numbers in the form "card=X;device=Y"
582 * - Other devices may use a number or any other string.
583 */
584
Eric Laurent55786bc2012-04-10 16:56:32 -0700585 int (*open_output_stream)(struct audio_hw_device *dev,
586 audio_io_handle_t handle,
587 audio_devices_t devices,
588 audio_output_flags_t flags,
589 struct audio_config *config,
Eric Laurentf5e24692014-07-27 16:14:57 -0700590 struct audio_stream_out **stream_out,
591 const char *address);
Dima Zavinf1504db2011-03-11 11:20:49 -0800592
593 void (*close_output_stream)(struct audio_hw_device *dev,
Eric Laurent55786bc2012-04-10 16:56:32 -0700594 struct audio_stream_out* stream_out);
Dima Zavinf1504db2011-03-11 11:20:49 -0800595
596 /** This method creates and opens the audio hardware input stream */
Eric Laurent55786bc2012-04-10 16:56:32 -0700597 int (*open_input_stream)(struct audio_hw_device *dev,
598 audio_io_handle_t handle,
599 audio_devices_t devices,
600 struct audio_config *config,
Glenn Kasten7d973ad2014-07-15 11:10:38 -0700601 struct audio_stream_in **stream_in,
Eric Laurentf5e24692014-07-27 16:14:57 -0700602 audio_input_flags_t flags,
603 const char *address,
604 audio_source_t source);
Dima Zavinf1504db2011-03-11 11:20:49 -0800605
606 void (*close_input_stream)(struct audio_hw_device *dev,
Eric Laurent55786bc2012-04-10 16:56:32 -0700607 struct audio_stream_in *stream_in);
Dima Zavinf1504db2011-03-11 11:20:49 -0800608
609 /** This method dumps the state of the audio hardware */
610 int (*dump)(const struct audio_hw_device *dev, int fd);
John Grossman47bf3d72012-07-17 11:54:04 -0700611
612 /**
613 * set the audio mute status for all audio activities. If any value other
614 * than 0 is returned, the software mixer will emulate this capability.
615 */
616 int (*set_master_mute)(struct audio_hw_device *dev, bool mute);
617
618 /**
619 * Get the current master mute status for the HAL, if the HAL supports
620 * master mute control. AudioFlinger will query this value from the primary
621 * audio HAL when the service starts and use the value for setting the
622 * initial master mute across all HALs. HALs which do not support this
623 * method may leave it set to NULL.
624 */
625 int (*get_master_mute)(struct audio_hw_device *dev, bool *mute);
Eric Laurent73b8a742014-05-22 14:02:38 -0700626
627 /**
628 * Routing control
629 */
630
631 /* Creates an audio patch between several source and sink ports.
632 * The handle is allocated by the HAL and should be unique for this
633 * audio HAL module. */
634 int (*create_audio_patch)(struct audio_hw_device *dev,
635 unsigned int num_sources,
636 const struct audio_port_config *sources,
637 unsigned int num_sinks,
638 const struct audio_port_config *sinks,
639 audio_patch_handle_t *handle);
640
641 /* Release an audio patch */
642 int (*release_audio_patch)(struct audio_hw_device *dev,
643 audio_patch_handle_t handle);
644
645 /* Fills the list of supported attributes for a given audio port.
646 * As input, "port" contains the information (type, role, address etc...)
647 * needed by the HAL to identify the port.
648 * As output, "port" contains possible attributes (sampling rates, formats,
649 * channel masks, gain controllers...) for this port.
650 */
651 int (*get_audio_port)(struct audio_hw_device *dev,
652 struct audio_port *port);
653
654 /* Set audio port configuration */
655 int (*set_audio_port_config)(struct audio_hw_device *dev,
656 const struct audio_port_config *config);
657
Kiran Kandi807edfa2013-10-17 15:49:54 -0700658#ifdef AUDIO_LISTEN_ENABLED
Bharath Ramachandramurthye41db452014-01-14 07:28:09 -0800659 /** This method creates the listen session and returns handle */
Kiran Kandi807edfa2013-10-17 15:49:54 -0700660 int (*open_listen_session)(struct audio_hw_device *dev,
Bharath Ramachandramurthye41db452014-01-14 07:28:09 -0800661 listen_open_params_t *params,
662 struct listen_session** handle);
Kiran Kandi807edfa2013-10-17 15:49:54 -0700663
664 /** This method closes the listen session */
665 int (*close_listen_session)(struct audio_hw_device *dev,
666 struct listen_session* handle);
667
668 /** This method sets the mad observer callback */
669 int (*set_mad_observer)(struct audio_hw_device *dev,
670 listen_callback_t cb_func);
671
Bharath Ramachandramurthye41db452014-01-14 07:28:09 -0800672 /**
673 * This method is used for setting listen hal specfic parameters.
Kiran Kandi807edfa2013-10-17 15:49:54 -0700674 * If multiple paramets are set in one call and setting any one of them
675 * fails it will return failure.
676 */
677 int (*listen_set_parameters)(struct audio_hw_device *dev,
678 const char *kv_pairs);
679#endif
Dima Zavinf1504db2011-03-11 11:20:49 -0800680};
681typedef struct audio_hw_device audio_hw_device_t;
682
683/** convenience API for opening and closing a supported device */
684
685static inline int audio_hw_device_open(const struct hw_module_t* module,
686 struct audio_hw_device** device)
687{
688 return module->methods->open(module, AUDIO_HARDWARE_INTERFACE,
689 (struct hw_device_t**)device);
690}
691
692static inline int audio_hw_device_close(struct audio_hw_device* device)
693{
694 return device->common.close(&device->common);
695}
696
697
698__END_DECLS
699
700#endif // ANDROID_AUDIO_INTERFACE_H