blob: 4ff479368c8c07ae9c80c9e97c9c76c6b76651eb [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 Laurenta70c5d02012-03-07 18:59:47 -080097/* A2DP sink address set by framework */
98#define AUDIO_PARAMETER_A2DP_SINK_ADDRESS "a2dp_sink_address"
99
Mike Lockwood2d4d9652014-05-28 11:09:54 -0700100/* A2DP source address set by framework */
101#define AUDIO_PARAMETER_A2DP_SOURCE_ADDRESS "a2dp_source_address"
102
Glenn Kasten34afb682012-06-08 10:49:34 -0700103/* Screen state */
104#define AUDIO_PARAMETER_KEY_SCREEN_STATE "screen_state"
105
Glenn Kastend930d922014-04-29 13:35:57 -0700106/* Bluetooth SCO wideband */
107#define AUDIO_PARAMETER_KEY_BT_SCO_WB "bt_wbs"
108
Eric Laurent70e81102011-08-07 10:05:40 -0700109/**
110 * audio stream parameters
111 */
112
Eric Laurentf5e24692014-07-27 16:14:57 -0700113#define AUDIO_PARAMETER_STREAM_ROUTING "routing" /* audio_devices_t */
114#define AUDIO_PARAMETER_STREAM_FORMAT "format" /* audio_format_t */
115#define AUDIO_PARAMETER_STREAM_CHANNELS "channels" /* audio_channel_mask_t */
116#define AUDIO_PARAMETER_STREAM_FRAME_COUNT "frame_count" /* size_t */
117#define AUDIO_PARAMETER_STREAM_INPUT_SOURCE "input_source" /* audio_source_t */
118#define AUDIO_PARAMETER_STREAM_SAMPLING_RATE "sampling_rate" /* uint32_t */
Dima Zavin57dde282011-06-06 19:31:18 -0700119
Eric Laurent41eeb4f2012-05-17 18:54:49 -0700120/* Query supported formats. The response is a '|' separated list of strings from
121 * audio_format_t enum e.g: "sup_formats=AUDIO_FORMAT_PCM_16_BIT" */
122#define AUDIO_PARAMETER_STREAM_SUP_FORMATS "sup_formats"
123/* Query supported channel masks. The response is a '|' separated list of strings from
124 * audio_channel_mask_t enum e.g: "sup_channels=AUDIO_CHANNEL_OUT_STEREO|AUDIO_CHANNEL_OUT_MONO" */
125#define AUDIO_PARAMETER_STREAM_SUP_CHANNELS "sup_channels"
126/* Query supported sampling rates. The response is a '|' separated list of integer values e.g:
127 * "sup_sampling_rates=44100|48000" */
128#define AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES "sup_sampling_rates"
129
Richard Fitzgeraldf37f1872013-03-25 16:11:44 +0000130/**
131 * audio codec parameters
132 */
133
134#define AUDIO_OFFLOAD_CODEC_PARAMS "music_offload_codec_param"
135#define AUDIO_OFFLOAD_CODEC_BIT_PER_SAMPLE "music_offload_bit_per_sample"
136#define AUDIO_OFFLOAD_CODEC_BIT_RATE "music_offload_bit_rate"
137#define AUDIO_OFFLOAD_CODEC_AVG_BIT_RATE "music_offload_avg_bit_rate"
138#define AUDIO_OFFLOAD_CODEC_ID "music_offload_codec_id"
139#define AUDIO_OFFLOAD_CODEC_BLOCK_ALIGN "music_offload_block_align"
140#define AUDIO_OFFLOAD_CODEC_SAMPLE_RATE "music_offload_sample_rate"
141#define AUDIO_OFFLOAD_CODEC_ENCODE_OPTION "music_offload_encode_option"
142#define AUDIO_OFFLOAD_CODEC_NUM_CHANNEL "music_offload_num_channels"
143#define AUDIO_OFFLOAD_CODEC_DOWN_SAMPLING "music_offload_down_sampling"
144#define AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES "delay_samples"
145#define AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES "padding_samples"
Eric Laurent55786bc2012-04-10 16:56:32 -0700146
Eric Laurent70e81102011-08-07 10:05:40 -0700147/**************************************/
148
Dima Zavinf1504db2011-03-11 11:20:49 -0800149/* common audio stream parameters and operations */
150struct audio_stream {
151
152 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800153 * Return the sampling rate in Hz - eg. 44100.
Dima Zavinf1504db2011-03-11 11:20:49 -0800154 */
155 uint32_t (*get_sample_rate)(const struct audio_stream *stream);
Dima Zavin57dde282011-06-06 19:31:18 -0700156
157 /* currently unused - use set_parameters with key
158 * AUDIO_PARAMETER_STREAM_SAMPLING_RATE
159 */
Dima Zavinf1504db2011-03-11 11:20:49 -0800160 int (*set_sample_rate)(struct audio_stream *stream, uint32_t rate);
161
162 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800163 * Return size of input/output buffer in bytes for this stream - eg. 4800.
164 * It should be a multiple of the frame size. See also get_input_buffer_size.
Dima Zavinf1504db2011-03-11 11:20:49 -0800165 */
166 size_t (*get_buffer_size)(const struct audio_stream *stream);
167
168 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800169 * Return the channel mask -
Dima Zavinf1504db2011-03-11 11:20:49 -0800170 * e.g. AUDIO_CHANNEL_OUT_STEREO or AUDIO_CHANNEL_IN_STEREO
171 */
Eric Laurent55786bc2012-04-10 16:56:32 -0700172 audio_channel_mask_t (*get_channels)(const struct audio_stream *stream);
Dima Zavinf1504db2011-03-11 11:20:49 -0800173
174 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800175 * Return the audio format - e.g. AUDIO_FORMAT_PCM_16_BIT
Dima Zavinf1504db2011-03-11 11:20:49 -0800176 */
Glenn Kastenfe79eb32012-01-12 14:55:57 -0800177 audio_format_t (*get_format)(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_FORMAT
181 */
Glenn Kastenfe79eb32012-01-12 14:55:57 -0800182 int (*set_format)(struct audio_stream *stream, audio_format_t format);
Dima Zavinf1504db2011-03-11 11:20:49 -0800183
184 /**
185 * Put the audio hardware input/output into standby mode.
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800186 * Driver should exit from standby mode at the next I/O operation.
Dima Zavinf1504db2011-03-11 11:20:49 -0800187 * Returns 0 on success and <0 on failure.
188 */
189 int (*standby)(struct audio_stream *stream);
190
191 /** dump the state of the audio input/output device */
192 int (*dump)(const struct audio_stream *stream, int fd);
193
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800194 /** Return the set of device(s) which this stream is connected to */
Dima Zavinf1504db2011-03-11 11:20:49 -0800195 audio_devices_t (*get_device)(const struct audio_stream *stream);
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800196
197 /**
198 * Currently unused - set_device() corresponds to set_parameters() with key
199 * AUDIO_PARAMETER_STREAM_ROUTING for both input and output.
200 * AUDIO_PARAMETER_STREAM_INPUT_SOURCE is an additional information used by
201 * input streams only.
202 */
Dima Zavinf1504db2011-03-11 11:20:49 -0800203 int (*set_device)(struct audio_stream *stream, audio_devices_t device);
204
205 /**
206 * set/get audio stream parameters. The function accepts a list of
207 * parameter key value pairs in the form: key1=value1;key2=value2;...
208 *
209 * Some keys are reserved for standard parameters (See AudioParameter class)
210 *
211 * If the implementation does not accept a parameter change while
212 * the output is active but the parameter is acceptable otherwise, it must
213 * return -ENOSYS.
214 *
215 * The audio flinger will put the stream in standby and then change the
216 * parameter value.
217 */
218 int (*set_parameters)(struct audio_stream *stream, const char *kv_pairs);
219
220 /*
221 * Returns a pointer to a heap allocated string. The caller is responsible
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800222 * for freeing the memory for it using free().
Dima Zavinf1504db2011-03-11 11:20:49 -0800223 */
224 char * (*get_parameters)(const struct audio_stream *stream,
225 const char *keys);
Eric Laurentf3008aa2011-06-17 16:53:12 -0700226 int (*add_audio_effect)(const struct audio_stream *stream,
227 effect_handle_t effect);
228 int (*remove_audio_effect)(const struct audio_stream *stream,
229 effect_handle_t effect);
Dima Zavinf1504db2011-03-11 11:20:49 -0800230};
231typedef struct audio_stream audio_stream_t;
232
Richard Fitzgeraldf37f1872013-03-25 16:11:44 +0000233/* type of asynchronous write callback events. Mutually exclusive */
234typedef enum {
235 STREAM_CBK_EVENT_WRITE_READY, /* non blocking write completed */
236 STREAM_CBK_EVENT_DRAIN_READY /* drain completed */
237} stream_callback_event_t;
238
239typedef int (*stream_callback_t)(stream_callback_event_t event, void *param, void *cookie);
240
241/* type of drain requested to audio_stream_out->drain(). Mutually exclusive */
242typedef enum {
243 AUDIO_DRAIN_ALL, /* drain() returns when all data has been played */
244 AUDIO_DRAIN_EARLY_NOTIFY /* drain() returns a short time before all data
245 from the current track has been played to
246 give time for gapless track switch */
247} audio_drain_type_t;
248
Dima Zavinf1504db2011-03-11 11:20:49 -0800249/**
250 * audio_stream_out is the abstraction interface for the audio output hardware.
251 *
252 * It provides information about various properties of the audio output
253 * hardware driver.
254 */
255
256struct audio_stream_out {
Stewart Miles84d35492014-05-01 09:03:27 -0700257 /**
258 * Common methods of the audio stream out. This *must* be the first member of audio_stream_out
259 * as users of this structure will cast a audio_stream to audio_stream_out pointer in contexts
260 * where it's known the audio_stream references an audio_stream_out.
261 */
Dima Zavinf1504db2011-03-11 11:20:49 -0800262 struct audio_stream common;
263
264 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800265 * Return the audio hardware driver estimated latency in milliseconds.
Dima Zavinf1504db2011-03-11 11:20:49 -0800266 */
267 uint32_t (*get_latency)(const struct audio_stream_out *stream);
268
269 /**
270 * Use this method in situations where audio mixing is done in the
271 * hardware. This method serves as a direct interface with hardware,
272 * allowing you to directly set the volume as apposed to via the framework.
273 * This method might produce multiple PCM outputs or hardware accelerated
274 * codecs, such as MP3 or AAC.
275 */
276 int (*set_volume)(struct audio_stream_out *stream, float left, float right);
277
278 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800279 * Write audio buffer to driver. Returns number of bytes written, or a
280 * negative status_t. If at least one frame was written successfully prior to the error,
281 * it is suggested that the driver return that successful (short) byte count
282 * and then return an error in the subsequent call.
Richard Fitzgeraldf37f1872013-03-25 16:11:44 +0000283 *
284 * If set_callback() has previously been called to enable non-blocking mode
285 * the write() is not allowed to block. It must write only the number of
286 * bytes that currently fit in the driver/hardware buffer and then return
287 * this byte count. If this is less than the requested write size the
288 * callback function must be called when more space is available in the
289 * driver/hardware buffer.
Dima Zavinf1504db2011-03-11 11:20:49 -0800290 */
291 ssize_t (*write)(struct audio_stream_out *stream, const void* buffer,
292 size_t bytes);
293
294 /* return the number of audio frames written by the audio dsp to DAC since
295 * the output has exited standby
296 */
297 int (*get_render_position)(const struct audio_stream_out *stream,
298 uint32_t *dsp_frames);
Mike J. Chen5ad38a92011-08-15 12:05:00 -0700299
300 /**
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800301 * get the local time at which the next write to the audio driver will be presented.
302 * The units are microseconds, where the epoch is decided by the local audio HAL.
Mike J. Chen5ad38a92011-08-15 12:05:00 -0700303 */
304 int (*get_next_write_timestamp)(const struct audio_stream_out *stream,
305 int64_t *timestamp);
306
Richard Fitzgeraldf37f1872013-03-25 16:11:44 +0000307 /**
308 * set the callback function for notifying completion of non-blocking
309 * write and drain.
310 * Calling this function implies that all future write() and drain()
311 * must be non-blocking and use the callback to signal completion.
312 */
313 int (*set_callback)(struct audio_stream_out *stream,
314 stream_callback_t callback, void *cookie);
315
316 /**
317 * Notifies to the audio driver to stop playback however the queued buffers are
318 * retained by the hardware. Useful for implementing pause/resume. Empty implementation
319 * if not supported however should be implemented for hardware with non-trivial
320 * latency. In the pause state audio hardware could still be using power. User may
321 * consider calling suspend after a timeout.
322 *
323 * Implementation of this function is mandatory for offloaded playback.
324 */
325 int (*pause)(struct audio_stream_out* stream);
326
327 /**
328 * Notifies to the audio driver to resume playback following a pause.
329 * Returns error if called without matching pause.
330 *
331 * Implementation of this function is mandatory for offloaded playback.
332 */
333 int (*resume)(struct audio_stream_out* stream);
334
335 /**
336 * Requests notification when data buffered by the driver/hardware has
337 * been played. If set_callback() has previously been called to enable
338 * non-blocking mode, the drain() must not block, instead it should return
339 * quickly and completion of the drain is notified through the callback.
340 * If set_callback() has not been called, the drain() must block until
341 * completion.
342 * If type==AUDIO_DRAIN_ALL, the drain completes when all previously written
343 * data has been played.
344 * If type==AUDIO_DRAIN_EARLY_NOTIFY, the drain completes shortly before all
345 * data for the current track has played to allow time for the framework
346 * to perform a gapless track switch.
347 *
348 * Drain must return immediately on stop() and flush() call
349 *
350 * Implementation of this function is mandatory for offloaded playback.
351 */
352 int (*drain)(struct audio_stream_out* stream, audio_drain_type_t type );
353
354 /**
355 * Notifies to the audio driver to flush the queued data. Stream must already
356 * be paused before calling flush().
357 *
358 * Implementation of this function is mandatory for offloaded playback.
359 */
360 int (*flush)(struct audio_stream_out* stream);
Glenn Kastene25f9ed2013-08-22 16:27:22 -0700361
362 /**
Glenn Kasten22a06b72013-09-10 09:23:07 -0700363 * Return a recent count of the number of audio frames presented to an external observer.
Glenn Kastene25f9ed2013-08-22 16:27:22 -0700364 * This excludes frames which have been written but are still in the pipeline.
365 * The count is not reset to zero when output enters standby.
366 * Also returns the value of CLOCK_MONOTONIC as of this presentation count.
Glenn Kasten22a06b72013-09-10 09:23:07 -0700367 * The returned count is expected to be 'recent',
368 * but does not need to be the most recent possible value.
369 * However, the associated time should correspond to whatever count is returned.
370 * Example: assume that N+M frames have been presented, where M is a 'small' number.
371 * Then it is permissible to return N instead of N+M,
372 * and the timestamp should correspond to N rather than N+M.
373 * The terms 'recent' and 'small' are not defined.
374 * They reflect the quality of the implementation.
Glenn Kastene25f9ed2013-08-22 16:27:22 -0700375 *
376 * 3.0 and higher only.
377 */
378 int (*get_presentation_position)(const struct audio_stream_out *stream,
379 uint64_t *frames, struct timespec *timestamp);
380
Dima Zavinf1504db2011-03-11 11:20:49 -0800381};
382typedef struct audio_stream_out audio_stream_out_t;
383
384struct audio_stream_in {
Stewart Miles84d35492014-05-01 09:03:27 -0700385 /**
386 * Common methods of the audio stream in. This *must* be the first member of audio_stream_in
387 * as users of this structure will cast a audio_stream to audio_stream_in pointer in contexts
388 * where it's known the audio_stream references an audio_stream_in.
389 */
Dima Zavinf1504db2011-03-11 11:20:49 -0800390 struct audio_stream common;
391
392 /** set the input gain for the audio driver. This method is for
393 * for future use */
394 int (*set_gain)(struct audio_stream_in *stream, float gain);
395
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800396 /** Read audio buffer in from audio driver. Returns number of bytes read, or a
397 * negative status_t. If at least one frame was read prior to the error,
398 * read should return that byte count and then return an error in the subsequent call.
399 */
Dima Zavinf1504db2011-03-11 11:20:49 -0800400 ssize_t (*read)(struct audio_stream_in *stream, void* buffer,
401 size_t bytes);
402
403 /**
404 * Return the amount of input frames lost in the audio driver since the
405 * last call of this function.
406 * Audio driver is expected to reset the value to 0 and restart counting
407 * upon returning the current value by this function call.
408 * Such loss typically occurs when the user space process is blocked
409 * longer than the capacity of audio driver buffers.
410 *
411 * Unit: the number of input audio frames
412 */
413 uint32_t (*get_input_frames_lost)(struct audio_stream_in *stream);
414};
415typedef struct audio_stream_in audio_stream_in_t;
416
417/**
418 * return the frame size (number of bytes per sample).
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700419 *
420 * Deprecated: use audio_stream_out_frame_size() or audio_stream_in_frame_size() instead.
Dima Zavinf1504db2011-03-11 11:20:49 -0800421 */
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700422__attribute__((__deprecated__))
Glenn Kasten48915ac2012-02-20 12:08:57 -0800423static inline size_t audio_stream_frame_size(const struct audio_stream *s)
Dima Zavinf1504db2011-03-11 11:20:49 -0800424{
Glenn Kastena26cbac2012-01-13 14:53:35 -0800425 size_t chan_samp_sz;
Richard Fitzgeraldf37f1872013-03-25 16:11:44 +0000426 audio_format_t format = s->get_format(s);
Dima Zavinf1504db2011-03-11 11:20:49 -0800427
Richard Fitzgeraldf37f1872013-03-25 16:11:44 +0000428 if (audio_is_linear_pcm(format)) {
429 chan_samp_sz = audio_bytes_per_sample(format);
430 return popcount(s->get_channels(s)) * chan_samp_sz;
Dima Zavinf1504db2011-03-11 11:20:49 -0800431 }
432
Richard Fitzgeraldf37f1872013-03-25 16:11:44 +0000433 return sizeof(int8_t);
Dima Zavinf1504db2011-03-11 11:20:49 -0800434}
435
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700436/**
437 * return the frame size (number of bytes per sample) of an output stream.
438 */
439static inline size_t audio_stream_out_frame_size(const struct audio_stream_out *s)
440{
441 size_t chan_samp_sz;
442 audio_format_t format = s->common.get_format(&s->common);
443
444 if (audio_is_linear_pcm(format)) {
445 chan_samp_sz = audio_bytes_per_sample(format);
446 return audio_channel_count_from_out_mask(s->common.get_channels(&s->common)) * chan_samp_sz;
447 }
448
449 return sizeof(int8_t);
450}
451
452/**
453 * return the frame size (number of bytes per sample) of an input stream.
454 */
455static inline size_t audio_stream_in_frame_size(const struct audio_stream_in *s)
456{
457 size_t chan_samp_sz;
458 audio_format_t format = s->common.get_format(&s->common);
459
460 if (audio_is_linear_pcm(format)) {
461 chan_samp_sz = audio_bytes_per_sample(format);
462 return audio_channel_count_from_in_mask(s->common.get_channels(&s->common)) * chan_samp_sz;
463 }
464
465 return sizeof(int8_t);
466}
Dima Zavinf1504db2011-03-11 11:20:49 -0800467
468/**********************************************************************/
469
470/**
471 * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
472 * and the fields of this data structure must begin with hw_module_t
473 * followed by module specific information.
474 */
475struct audio_module {
476 struct hw_module_t common;
477};
478
479struct audio_hw_device {
Stewart Miles84d35492014-05-01 09:03:27 -0700480 /**
481 * Common methods of the audio device. This *must* be the first member of audio_hw_device
482 * as users of this structure will cast a hw_device_t to audio_hw_device pointer in contexts
483 * where it's known the hw_device_t references an audio_hw_device.
484 */
Dima Zavinf1504db2011-03-11 11:20:49 -0800485 struct hw_device_t common;
486
487 /**
488 * used by audio flinger to enumerate what devices are supported by
489 * each audio_hw_device implementation.
490 *
491 * Return value is a bitmask of 1 or more values of audio_devices_t
Eric Laurent85e08e22012-08-28 14:30:35 -0700492 *
493 * NOTE: audio HAL implementations starting with
494 * AUDIO_DEVICE_API_VERSION_2_0 do not implement this function.
495 * All supported devices should be listed in audio_policy.conf
496 * file and the audio policy manager must choose the appropriate
497 * audio module based on information in this file.
Dima Zavinf1504db2011-03-11 11:20:49 -0800498 */
499 uint32_t (*get_supported_devices)(const struct audio_hw_device *dev);
500
501 /**
502 * check to see if the audio hardware interface has been initialized.
503 * returns 0 on success, -ENODEV on failure.
504 */
505 int (*init_check)(const struct audio_hw_device *dev);
506
507 /** set the audio volume of a voice call. Range is between 0.0 and 1.0 */
508 int (*set_voice_volume)(struct audio_hw_device *dev, float volume);
509
510 /**
511 * set the audio volume for all audio activities other than voice call.
512 * Range between 0.0 and 1.0. If any value other than 0 is returned,
513 * the software mixer will emulate this capability.
514 */
515 int (*set_master_volume)(struct audio_hw_device *dev, float volume);
516
517 /**
Mike J. Chen5ad38a92011-08-15 12:05:00 -0700518 * Get the current master volume value for the HAL, if the HAL supports
519 * master volume control. AudioFlinger will query this value from the
520 * primary audio HAL when the service starts and use the value for setting
521 * the initial master volume across all HALs. HALs which do not support
John Grossman47bf3d72012-07-17 11:54:04 -0700522 * this method may leave it set to NULL.
Mike J. Chen5ad38a92011-08-15 12:05:00 -0700523 */
524 int (*get_master_volume)(struct audio_hw_device *dev, float *volume);
525
526 /**
Glenn Kasten6df641e2012-01-09 10:41:30 -0800527 * set_mode is called when the audio mode changes. AUDIO_MODE_NORMAL mode
Dima Zavinf1504db2011-03-11 11:20:49 -0800528 * is for standard audio playback, AUDIO_MODE_RINGTONE when a ringtone is
529 * playing, and AUDIO_MODE_IN_CALL when a call is in progress.
Dima Zavinf1504db2011-03-11 11:20:49 -0800530 */
Glenn Kasten6df641e2012-01-09 10:41:30 -0800531 int (*set_mode)(struct audio_hw_device *dev, audio_mode_t mode);
Dima Zavinf1504db2011-03-11 11:20:49 -0800532
533 /* mic mute */
534 int (*set_mic_mute)(struct audio_hw_device *dev, bool state);
535 int (*get_mic_mute)(const struct audio_hw_device *dev, bool *state);
536
537 /* set/get global audio parameters */
538 int (*set_parameters)(struct audio_hw_device *dev, const char *kv_pairs);
539
540 /*
541 * Returns a pointer to a heap allocated string. The caller is responsible
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800542 * for freeing the memory for it using free().
Dima Zavinf1504db2011-03-11 11:20:49 -0800543 */
544 char * (*get_parameters)(const struct audio_hw_device *dev,
545 const char *keys);
546
547 /* Returns audio input buffer size according to parameters passed or
Glenn Kasten0cacd8d2012-02-10 13:42:44 -0800548 * 0 if one of the parameters is not supported.
549 * See also get_buffer_size which is for a particular stream.
Dima Zavinf1504db2011-03-11 11:20:49 -0800550 */
551 size_t (*get_input_buffer_size)(const struct audio_hw_device *dev,
Eric Laurent55786bc2012-04-10 16:56:32 -0700552 const struct audio_config *config);
Dima Zavinf1504db2011-03-11 11:20:49 -0800553
Eric Laurentf5e24692014-07-27 16:14:57 -0700554 /** This method creates and opens the audio hardware output stream.
555 * The "address" parameter qualifies the "devices" audio device type if needed.
556 * The format format depends on the device type:
557 * - Bluetooth devices use the MAC address of the device in the form "00:11:22:AA:BB:CC"
558 * - USB devices use the ALSA card and device numbers in the form "card=X;device=Y"
559 * - Other devices may use a number or any other string.
560 */
561
Eric Laurent55786bc2012-04-10 16:56:32 -0700562 int (*open_output_stream)(struct audio_hw_device *dev,
563 audio_io_handle_t handle,
564 audio_devices_t devices,
565 audio_output_flags_t flags,
566 struct audio_config *config,
Eric Laurentf5e24692014-07-27 16:14:57 -0700567 struct audio_stream_out **stream_out,
568 const char *address);
Dima Zavinf1504db2011-03-11 11:20:49 -0800569
570 void (*close_output_stream)(struct audio_hw_device *dev,
Eric Laurent55786bc2012-04-10 16:56:32 -0700571 struct audio_stream_out* stream_out);
Dima Zavinf1504db2011-03-11 11:20:49 -0800572
573 /** This method creates and opens the audio hardware input stream */
Eric Laurent55786bc2012-04-10 16:56:32 -0700574 int (*open_input_stream)(struct audio_hw_device *dev,
575 audio_io_handle_t handle,
576 audio_devices_t devices,
577 struct audio_config *config,
Glenn Kasten7d973ad2014-07-15 11:10:38 -0700578 struct audio_stream_in **stream_in,
Eric Laurentf5e24692014-07-27 16:14:57 -0700579 audio_input_flags_t flags,
580 const char *address,
581 audio_source_t source);
Dima Zavinf1504db2011-03-11 11:20:49 -0800582
583 void (*close_input_stream)(struct audio_hw_device *dev,
Eric Laurent55786bc2012-04-10 16:56:32 -0700584 struct audio_stream_in *stream_in);
Dima Zavinf1504db2011-03-11 11:20:49 -0800585
586 /** This method dumps the state of the audio hardware */
587 int (*dump)(const struct audio_hw_device *dev, int fd);
John Grossman47bf3d72012-07-17 11:54:04 -0700588
589 /**
590 * set the audio mute status for all audio activities. If any value other
591 * than 0 is returned, the software mixer will emulate this capability.
592 */
593 int (*set_master_mute)(struct audio_hw_device *dev, bool mute);
594
595 /**
596 * Get the current master mute status for the HAL, if the HAL supports
597 * master mute control. AudioFlinger will query this value from the primary
598 * audio HAL when the service starts and use the value for setting the
599 * initial master mute across all HALs. HALs which do not support this
600 * method may leave it set to NULL.
601 */
602 int (*get_master_mute)(struct audio_hw_device *dev, bool *mute);
Eric Laurent73b8a742014-05-22 14:02:38 -0700603
604 /**
605 * Routing control
606 */
607
608 /* Creates an audio patch between several source and sink ports.
609 * The handle is allocated by the HAL and should be unique for this
610 * audio HAL module. */
611 int (*create_audio_patch)(struct audio_hw_device *dev,
612 unsigned int num_sources,
613 const struct audio_port_config *sources,
614 unsigned int num_sinks,
615 const struct audio_port_config *sinks,
616 audio_patch_handle_t *handle);
617
618 /* Release an audio patch */
619 int (*release_audio_patch)(struct audio_hw_device *dev,
620 audio_patch_handle_t handle);
621
622 /* Fills the list of supported attributes for a given audio port.
623 * As input, "port" contains the information (type, role, address etc...)
624 * needed by the HAL to identify the port.
625 * As output, "port" contains possible attributes (sampling rates, formats,
626 * channel masks, gain controllers...) for this port.
627 */
628 int (*get_audio_port)(struct audio_hw_device *dev,
629 struct audio_port *port);
630
631 /* Set audio port configuration */
632 int (*set_audio_port_config)(struct audio_hw_device *dev,
633 const struct audio_port_config *config);
634
Dima Zavinf1504db2011-03-11 11:20:49 -0800635};
636typedef struct audio_hw_device audio_hw_device_t;
637
638/** convenience API for opening and closing a supported device */
639
640static inline int audio_hw_device_open(const struct hw_module_t* module,
641 struct audio_hw_device** device)
642{
643 return module->methods->open(module, AUDIO_HARDWARE_INTERFACE,
644 (struct hw_device_t**)device);
645}
646
647static inline int audio_hw_device_close(struct audio_hw_device* device)
648{
649 return device->common.close(&device->common);
650}
651
652
653__END_DECLS
654
655#endif // ANDROID_AUDIO_INTERFACE_H