1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #ifndef ANDROID_AUDIO_HAL_INTERFACE_H
19 #define ANDROID_AUDIO_HAL_INTERFACE_H
20 
21 #include <stdint.h>
22 #include <strings.h>
23 #include <sys/cdefs.h>
24 #include <sys/types.h>
25 #include <time.h>
26 
27 #include <cutils/bitops.h>
28 
29 #include <hardware/hardware.h>
30 #include <system/audio.h>
31 #include <hardware/audio_effect.h>
32 
33 __BEGIN_DECLS
34 
35 /**
36  * The id of this module
37  */
38 #define AUDIO_HARDWARE_MODULE_ID "audio"
39 
40 /**
41  * Name of the audio devices to open
42  */
43 #define AUDIO_HARDWARE_INTERFACE "audio_hw_if"
44 
45 
46 /* Use version 0.1 to be compatible with first generation of audio hw module with version_major
47  * hardcoded to 1. No audio module API change.
48  */
49 #define AUDIO_MODULE_API_VERSION_0_1 HARDWARE_MODULE_API_VERSION(0, 1)
50 #define AUDIO_MODULE_API_VERSION_CURRENT AUDIO_MODULE_API_VERSION_0_1
51 
52 /* First generation of audio devices had version hardcoded to 0. all devices with versions < 1.0
53  * will be considered of first generation API.
54  */
55 #define AUDIO_DEVICE_API_VERSION_0_0 HARDWARE_DEVICE_API_VERSION(0, 0)
56 #define AUDIO_DEVICE_API_VERSION_1_0 HARDWARE_DEVICE_API_VERSION(1, 0)
57 #define AUDIO_DEVICE_API_VERSION_2_0 HARDWARE_DEVICE_API_VERSION(2, 0)
58 #define AUDIO_DEVICE_API_VERSION_3_0 HARDWARE_DEVICE_API_VERSION(3, 0)
59 #define AUDIO_DEVICE_API_VERSION_CURRENT AUDIO_DEVICE_API_VERSION_3_0
60 /* Minimal audio HAL version supported by the audio framework */
61 #define AUDIO_DEVICE_API_VERSION_MIN AUDIO_DEVICE_API_VERSION_2_0
62 
63 /**************************************/
64 
65 /**
66  *  standard audio parameters that the HAL may need to handle
67  */
68 
69 /**
70  *  audio device parameters
71  */
72 
73 /* TTY mode selection */
74 #define AUDIO_PARAMETER_KEY_TTY_MODE "tty_mode"
75 #define AUDIO_PARAMETER_VALUE_TTY_OFF "tty_off"
76 #define AUDIO_PARAMETER_VALUE_TTY_VCO "tty_vco"
77 #define AUDIO_PARAMETER_VALUE_TTY_HCO "tty_hco"
78 #define AUDIO_PARAMETER_VALUE_TTY_FULL "tty_full"
79 
80 /* Hearing Aid Compatibility - Telecoil (HAC-T) mode on/off */
81 #define AUDIO_PARAMETER_KEY_HAC "HACSetting"
82 #define AUDIO_PARAMETER_VALUE_HAC_ON "ON"
83 #define AUDIO_PARAMETER_VALUE_HAC_OFF "OFF"
84 
85 /* A2DP sink address set by framework */
86 #define AUDIO_PARAMETER_A2DP_SINK_ADDRESS "a2dp_sink_address"
87 
88 /* A2DP source address set by framework */
89 #define AUDIO_PARAMETER_A2DP_SOURCE_ADDRESS "a2dp_source_address"
90 
91 /* Bluetooth SCO wideband */
92 #define AUDIO_PARAMETER_KEY_BT_SCO_WB "bt_wbs"
93 
94 /**
95  *  audio stream parameters
96  */
97 
98 /* Enable AANC */
99 #define AUDIO_PARAMETER_KEY_AANC "aanc_enabled"
100 
101 /**************************************/
102 
103 /* common audio stream parameters and operations */
104 struct audio_stream {
105 
106     /**
107      * Return the sampling rate in Hz - eg. 44100.
108      */
109     uint32_t (*get_sample_rate)(const struct audio_stream *stream);
110 
111     /* currently unused - use set_parameters with key
112      *    AUDIO_PARAMETER_STREAM_SAMPLING_RATE
113      */
114     int (*set_sample_rate)(struct audio_stream *stream, uint32_t rate);
115 
116     /**
117      * Return size of input/output buffer in bytes for this stream - eg. 4800.
118      * It should be a multiple of the frame size.  See also get_input_buffer_size.
119      */
120     size_t (*get_buffer_size)(const struct audio_stream *stream);
121 
122     /**
123      * Return the channel mask -
124      *  e.g. AUDIO_CHANNEL_OUT_STEREO or AUDIO_CHANNEL_IN_STEREO
125      */
126     audio_channel_mask_t (*get_channels)(const struct audio_stream *stream);
127 
128     /**
129      * Return the audio format - e.g. AUDIO_FORMAT_PCM_16_BIT
130      */
131     audio_format_t (*get_format)(const struct audio_stream *stream);
132 
133     /* currently unused - use set_parameters with key
134      *     AUDIO_PARAMETER_STREAM_FORMAT
135      */
136     int (*set_format)(struct audio_stream *stream, audio_format_t format);
137 
138     /**
139      * Put the audio hardware input/output into standby mode.
140      * Driver should exit from standby mode at the next I/O operation.
141      * Returns 0 on success and <0 on failure.
142      */
143     int (*standby)(struct audio_stream *stream);
144 
145     /** dump the state of the audio input/output device */
146     int (*dump)(const struct audio_stream *stream, int fd);
147 
148     /** Return the set of device(s) which this stream is connected to */
149     audio_devices_t (*get_device)(const struct audio_stream *stream);
150 
151     /**
152      * Currently unused - set_device() corresponds to set_parameters() with key
153      * AUDIO_PARAMETER_STREAM_ROUTING for both input and output.
154      * AUDIO_PARAMETER_STREAM_INPUT_SOURCE is an additional information used by
155      * input streams only.
156      */
157     int (*set_device)(struct audio_stream *stream, audio_devices_t device);
158 
159     /**
160      * set/get audio stream parameters. The function accepts a list of
161      * parameter key value pairs in the form: key1=value1;key2=value2;...
162      *
163      * Some keys are reserved for standard parameters (See AudioParameter class)
164      *
165      * If the implementation does not accept a parameter change while
166      * the output is active but the parameter is acceptable otherwise, it must
167      * return -ENOSYS.
168      *
169      * The audio flinger will put the stream in standby and then change the
170      * parameter value.
171      */
172     int (*set_parameters)(struct audio_stream *stream, const char *kv_pairs);
173 
174     /*
175      * Returns a pointer to a heap allocated string. The caller is responsible
176      * for freeing the memory for it using free().
177      */
178     char * (*get_parameters)(const struct audio_stream *stream,
179                              const char *keys);
180     int (*add_audio_effect)(const struct audio_stream *stream,
181                              effect_handle_t effect);
182     int (*remove_audio_effect)(const struct audio_stream *stream,
183                              effect_handle_t effect);
184 };
185 typedef struct audio_stream audio_stream_t;
186 
187 /* type of asynchronous write callback events. Mutually exclusive */
188 typedef enum {
189     STREAM_CBK_EVENT_WRITE_READY, /* non blocking write completed */
190     STREAM_CBK_EVENT_DRAIN_READY,  /* drain completed */
191     STREAM_CBK_EVENT_ERROR, /* stream hit some error, let AF take action */
192 } stream_callback_event_t;
193 
194 typedef int (*stream_callback_t)(stream_callback_event_t event, void *param, void *cookie);
195 
196 /* type of drain requested to audio_stream_out->drain(). Mutually exclusive */
197 typedef enum {
198     AUDIO_DRAIN_ALL,            /* drain() returns when all data has been played */
199     AUDIO_DRAIN_EARLY_NOTIFY    /* drain() returns a short time before all data
200                                    from the current track has been played to
201                                    give time for gapless track switch */
202 } audio_drain_type_t;
203 
204 /**
205  * audio_stream_out is the abstraction interface for the audio output hardware.
206  *
207  * It provides information about various properties of the audio output
208  * hardware driver.
209  */
210 
211 struct audio_stream_out {
212     /**
213      * Common methods of the audio stream out.  This *must* be the first member of audio_stream_out
214      * as users of this structure will cast a audio_stream to audio_stream_out pointer in contexts
215      * where it's known the audio_stream references an audio_stream_out.
216      */
217     struct audio_stream common;
218 
219     /**
220      * Return the audio hardware driver estimated latency in milliseconds.
221      */
222     uint32_t (*get_latency)(const struct audio_stream_out *stream);
223 
224     /**
225      * Use this method in situations where audio mixing is done in the
226      * hardware. This method serves as a direct interface with hardware,
227      * allowing you to directly set the volume as apposed to via the framework.
228      * This method might produce multiple PCM outputs or hardware accelerated
229      * codecs, such as MP3 or AAC.
230      */
231     int (*set_volume)(struct audio_stream_out *stream, float left, float right);
232 
233     /**
234      * Write audio buffer to driver. Returns number of bytes written, or a
235      * negative status_t. If at least one frame was written successfully prior to the error,
236      * it is suggested that the driver return that successful (short) byte count
237      * and then return an error in the subsequent call.
238      *
239      * If set_callback() has previously been called to enable non-blocking mode
240      * the write() is not allowed to block. It must write only the number of
241      * bytes that currently fit in the driver/hardware buffer and then return
242      * this byte count. If this is less than the requested write size the
243      * callback function must be called when more space is available in the
244      * driver/hardware buffer.
245      */
246     ssize_t (*write)(struct audio_stream_out *stream, const void* buffer,
247                      size_t bytes);
248 
249     /* return the number of audio frames written by the audio dsp to DAC since
250      * the output has exited standby
251      */
252     int (*get_render_position)(const struct audio_stream_out *stream,
253                                uint32_t *dsp_frames);
254 
255     /**
256      * get the local time at which the next write to the audio driver will be presented.
257      * The units are microseconds, where the epoch is decided by the local audio HAL.
258      */
259     int (*get_next_write_timestamp)(const struct audio_stream_out *stream,
260                                     int64_t *timestamp);
261 
262     /**
263      * set the callback function for notifying completion of non-blocking
264      * write and drain.
265      * Calling this function implies that all future write() and drain()
266      * must be non-blocking and use the callback to signal completion.
267      */
268     int (*set_callback)(struct audio_stream_out *stream,
269             stream_callback_t callback, void *cookie);
270 
271     /**
272      * Notifies to the audio driver to stop playback however the queued buffers are
273      * retained by the hardware. Useful for implementing pause/resume. Empty implementation
274      * if not supported however should be implemented for hardware with non-trivial
275      * latency. In the pause state audio hardware could still be using power. User may
276      * consider calling suspend after a timeout.
277      *
278      * Implementation of this function is mandatory for offloaded playback.
279      */
280     int (*pause)(struct audio_stream_out* stream);
281 
282     /**
283      * Notifies to the audio driver to resume playback following a pause.
284      * Returns error if called without matching pause.
285      *
286      * Implementation of this function is mandatory for offloaded playback.
287      */
288     int (*resume)(struct audio_stream_out* stream);
289 
290     /**
291      * Requests notification when data buffered by the driver/hardware has
292      * been played. If set_callback() has previously been called to enable
293      * non-blocking mode, the drain() must not block, instead it should return
294      * quickly and completion of the drain is notified through the callback.
295      * If set_callback() has not been called, the drain() must block until
296      * completion.
297      * If type==AUDIO_DRAIN_ALL, the drain completes when all previously written
298      * data has been played.
299      * If type==AUDIO_DRAIN_EARLY_NOTIFY, the drain completes shortly before all
300      * data for the current track has played to allow time for the framework
301      * to perform a gapless track switch.
302      *
303      * Drain must return immediately on stop() and flush() call
304      *
305      * Implementation of this function is mandatory for offloaded playback.
306      */
307     int (*drain)(struct audio_stream_out* stream, audio_drain_type_t type );
308 
309     /**
310      * Notifies to the audio driver to flush the queued data. Stream must already
311      * be paused before calling flush().
312      *
313      * Implementation of this function is mandatory for offloaded playback.
314      */
315    int (*flush)(struct audio_stream_out* stream);
316 
317     /**
318      * Return a recent count of the number of audio frames presented to an external observer.
319      * This excludes frames which have been written but are still in the pipeline.
320      * The count is not reset to zero when output enters standby.
321      * Also returns the value of CLOCK_MONOTONIC as of this presentation count.
322      * The returned count is expected to be 'recent',
323      * but does not need to be the most recent possible value.
324      * However, the associated time should correspond to whatever count is returned.
325      * Example:  assume that N+M frames have been presented, where M is a 'small' number.
326      * Then it is permissible to return N instead of N+M,
327      * and the timestamp should correspond to N rather than N+M.
328      * The terms 'recent' and 'small' are not defined.
329      * They reflect the quality of the implementation.
330      *
331      * 3.0 and higher only.
332      */
333     int (*get_presentation_position)(const struct audio_stream_out *stream,
334                                uint64_t *frames, struct timespec *timestamp);
335 
336     /**
337      * Called by the framework to start a stream operating in mmap mode.
338      * create_mmap_buffer must be called before calling start()
339      *
340      * \note Function only implemented by streams operating in mmap mode.
341      *
342      * \param[in] stream the stream object.
343      * \return 0 in case of success.
344      *         -ENOSYS if called out of sequence or on non mmap stream
345      */
346     int (*start)(const struct audio_stream_out* stream);
347 
348     /**
349      * Called by the framework to stop a stream operating in mmap mode.
350      * Must be called after start()
351      *
352      * \note Function only implemented by streams operating in mmap mode.
353      *
354      * \param[in] stream the stream object.
355      * \return 0 in case of success.
356      *         -ENOSYS if called out of sequence or on non mmap stream
357      */
358     int (*stop)(const struct audio_stream_out* stream);
359 
360     /**
361      * Called by the framework to retrieve information on the mmap buffer used for audio
362      * samples transfer.
363      *
364      * \note Function only implemented by streams operating in mmap mode.
365      *
366      * \param[in] stream the stream object.
367      * \param[in] min_size_frames minimum buffer size requested. The actual buffer
368      *        size returned in struct audio_mmap_buffer_info can be larger.
369      * \param[out] info address at which the mmap buffer information should be returned.
370      *
371      * \return 0 if the buffer was allocated.
372      *         -ENODEV in case of initialization error
373      *         -EINVAL if the requested buffer size is too large
374      *         -ENOSYS if called out of sequence (e.g. buffer already allocated)
375      */
376     int (*create_mmap_buffer)(const struct audio_stream_out *stream,
377                               int32_t min_size_frames,
378                               struct audio_mmap_buffer_info *info);
379 
380     /**
381      * Called by the framework to read current read/write position in the mmap buffer
382      * with associated time stamp.
383      *
384      * \note Function only implemented by streams operating in mmap mode.
385      *
386      * \param[in] stream the stream object.
387      * \param[out] position address at which the mmap read/write position should be returned.
388      *
389      * \return 0 if the position is successfully returned.
390      *         -ENODATA if the position cannot be retrieved
391      *         -ENOSYS if called before create_mmap_buffer()
392      */
393     int (*get_mmap_position)(const struct audio_stream_out *stream,
394                              struct audio_mmap_position *position);
395 };
396 typedef struct audio_stream_out audio_stream_out_t;
397 
398 struct audio_stream_in {
399     /**
400      * Common methods of the audio stream in.  This *must* be the first member of audio_stream_in
401      * as users of this structure will cast a audio_stream to audio_stream_in pointer in contexts
402      * where it's known the audio_stream references an audio_stream_in.
403      */
404     struct audio_stream common;
405 
406     /** set the input gain for the audio driver. This method is for
407      *  for future use */
408     int (*set_gain)(struct audio_stream_in *stream, float gain);
409 
410     /** Read audio buffer in from audio driver. Returns number of bytes read, or a
411      *  negative status_t. If at least one frame was read prior to the error,
412      *  read should return that byte count and then return an error in the subsequent call.
413      */
414     ssize_t (*read)(struct audio_stream_in *stream, void* buffer,
415                     size_t bytes);
416 
417     /**
418      * Return the amount of input frames lost in the audio driver since the
419      * last call of this function.
420      * Audio driver is expected to reset the value to 0 and restart counting
421      * upon returning the current value by this function call.
422      * Such loss typically occurs when the user space process is blocked
423      * longer than the capacity of audio driver buffers.
424      *
425      * Unit: the number of input audio frames
426      */
427     uint32_t (*get_input_frames_lost)(struct audio_stream_in *stream);
428 
429     /**
430      * Return a recent count of the number of audio frames received and
431      * the clock time associated with that frame count.
432      *
433      * frames is the total frame count received. This should be as early in
434      *     the capture pipeline as possible. In general,
435      *     frames should be non-negative and should not go "backwards".
436      *
437      * time is the clock MONOTONIC time when frames was measured. In general,
438      *     time should be a positive quantity and should not go "backwards".
439      *
440      * The status returned is 0 on success, -ENOSYS if the device is not
441      * ready/available, or -EINVAL if the arguments are null or otherwise invalid.
442      */
443     int (*get_capture_position)(const struct audio_stream_in *stream,
444                                 int64_t *frames, int64_t *time);
445 
446     /**
447      * Called by the framework to start a stream operating in mmap mode.
448      * create_mmap_buffer must be called before calling start()
449      *
450      * \note Function only implemented by streams operating in mmap mode.
451      *
452      * \param[in] stream the stream object.
453      * \return 0 in case off success.
454      *         -ENOSYS if called out of sequence or on non mmap stream
455      */
456     int (*start)(const struct audio_stream_in* stream);
457 
458     /**
459      * Called by the framework to stop a stream operating in mmap mode.
460      *
461      * \note Function only implemented by streams operating in mmap mode.
462      *
463      * \param[in] stream the stream object.
464      * \return 0 in case of success.
465      *         -ENOSYS if called out of sequence or on non mmap stream
466      */
467     int (*stop)(const struct audio_stream_in* stream);
468 
469     /**
470      * Called by the framework to retrieve information on the mmap buffer used for audio
471      * samples transfer.
472      *
473      * \note Function only implemented by streams operating in mmap mode.
474      *
475      * \param[in] stream the stream object.
476      * \param[in] min_size_frames minimum buffer size requested. The actual buffer
477      *        size returned in struct audio_mmap_buffer_info can be larger.
478      * \param[out] info address at which the mmap buffer information should be returned.
479      *
480      * \return 0 if the buffer was allocated.
481      *         -ENODEV in case of initialization error
482      *         -EINVAL if the requested buffer size is too large
483      *         -ENOSYS if called out of sequence (e.g. buffer already allocated)
484      */
485     int (*create_mmap_buffer)(const struct audio_stream_in *stream,
486                               int32_t min_size_frames,
487                               struct audio_mmap_buffer_info *info);
488 
489     /**
490      * Called by the framework to read current read/write position in the mmap buffer
491      * with associated time stamp.
492      *
493      * \note Function only implemented by streams operating in mmap mode.
494      *
495      * \param[in] stream the stream object.
496      * \param[out] position address at which the mmap read/write position should be returned.
497      *
498      * \return 0 if the position is successfully returned.
499      *         -ENODATA if the position cannot be retreived
500      *         -ENOSYS if called before mmap_read_position()
501      */
502     int (*get_mmap_position)(const struct audio_stream_in *stream,
503                              struct audio_mmap_position *position);
504 };
505 typedef struct audio_stream_in audio_stream_in_t;
506 
507 /**
508  * return the frame size (number of bytes per sample).
509  *
510  * Deprecated: use audio_stream_out_frame_size() or audio_stream_in_frame_size() instead.
511  */
512 __attribute__((__deprecated__))
audio_stream_frame_size(const struct audio_stream * s)513 static inline size_t audio_stream_frame_size(const struct audio_stream *s)
514 {
515     size_t chan_samp_sz;
516     audio_format_t format = s->get_format(s);
517 
518     if (audio_has_proportional_frames(format)) {
519         chan_samp_sz = audio_bytes_per_sample(format);
520         return popcount(s->get_channels(s)) * chan_samp_sz;
521     }
522 
523     return sizeof(int8_t);
524 }
525 
526 /**
527  * return the frame size (number of bytes per sample) of an output stream.
528  */
audio_stream_out_frame_size(const struct audio_stream_out * s)529 static inline size_t audio_stream_out_frame_size(const struct audio_stream_out *s)
530 {
531     size_t chan_samp_sz;
532     audio_format_t format = s->common.get_format(&s->common);
533 
534     if (audio_has_proportional_frames(format)) {
535         chan_samp_sz = audio_bytes_per_sample(format);
536         return audio_channel_count_from_out_mask(s->common.get_channels(&s->common)) * chan_samp_sz;
537     }
538 
539     return sizeof(int8_t);
540 }
541 
542 /**
543  * return the frame size (number of bytes per sample) of an input stream.
544  */
audio_stream_in_frame_size(const struct audio_stream_in * s)545 static inline size_t audio_stream_in_frame_size(const struct audio_stream_in *s)
546 {
547     size_t chan_samp_sz;
548     audio_format_t format = s->common.get_format(&s->common);
549 
550     if (audio_has_proportional_frames(format)) {
551         chan_samp_sz = audio_bytes_per_sample(format);
552         return audio_channel_count_from_in_mask(s->common.get_channels(&s->common)) * chan_samp_sz;
553     }
554 
555     return sizeof(int8_t);
556 }
557 
558 /**********************************************************************/
559 
560 /**
561  * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
562  * and the fields of this data structure must begin with hw_module_t
563  * followed by module specific information.
564  */
565 struct audio_module {
566     struct hw_module_t common;
567 };
568 
569 struct audio_hw_device {
570     /**
571      * Common methods of the audio device.  This *must* be the first member of audio_hw_device
572      * as users of this structure will cast a hw_device_t to audio_hw_device pointer in contexts
573      * where it's known the hw_device_t references an audio_hw_device.
574      */
575     struct hw_device_t common;
576 
577     /**
578      * used by audio flinger to enumerate what devices are supported by
579      * each audio_hw_device implementation.
580      *
581      * Return value is a bitmask of 1 or more values of audio_devices_t
582      *
583      * NOTE: audio HAL implementations starting with
584      * AUDIO_DEVICE_API_VERSION_2_0 do not implement this function.
585      * All supported devices should be listed in audio_policy.conf
586      * file and the audio policy manager must choose the appropriate
587      * audio module based on information in this file.
588      */
589     uint32_t (*get_supported_devices)(const struct audio_hw_device *dev);
590 
591     /**
592      * check to see if the audio hardware interface has been initialized.
593      * returns 0 on success, -ENODEV on failure.
594      */
595     int (*init_check)(const struct audio_hw_device *dev);
596 
597     /** set the audio volume of a voice call. Range is between 0.0 and 1.0 */
598     int (*set_voice_volume)(struct audio_hw_device *dev, float volume);
599 
600     /**
601      * set the audio volume for all audio activities other than voice call.
602      * Range between 0.0 and 1.0. If any value other than 0 is returned,
603      * the software mixer will emulate this capability.
604      */
605     int (*set_master_volume)(struct audio_hw_device *dev, float volume);
606 
607     /**
608      * Get the current master volume value for the HAL, if the HAL supports
609      * master volume control.  AudioFlinger will query this value from the
610      * primary audio HAL when the service starts and use the value for setting
611      * the initial master volume across all HALs.  HALs which do not support
612      * this method may leave it set to NULL.
613      */
614     int (*get_master_volume)(struct audio_hw_device *dev, float *volume);
615 
616     /**
617      * set_mode is called when the audio mode changes. AUDIO_MODE_NORMAL mode
618      * is for standard audio playback, AUDIO_MODE_RINGTONE when a ringtone is
619      * playing, and AUDIO_MODE_IN_CALL when a call is in progress.
620      */
621     int (*set_mode)(struct audio_hw_device *dev, audio_mode_t mode);
622 
623     /* mic mute */
624     int (*set_mic_mute)(struct audio_hw_device *dev, bool state);
625     int (*get_mic_mute)(const struct audio_hw_device *dev, bool *state);
626 
627     /* set/get global audio parameters */
628     int (*set_parameters)(struct audio_hw_device *dev, const char *kv_pairs);
629 
630     /*
631      * Returns a pointer to a heap allocated string. The caller is responsible
632      * for freeing the memory for it using free().
633      */
634     char * (*get_parameters)(const struct audio_hw_device *dev,
635                              const char *keys);
636 
637     /* Returns audio input buffer size according to parameters passed or
638      * 0 if one of the parameters is not supported.
639      * See also get_buffer_size which is for a particular stream.
640      */
641     size_t (*get_input_buffer_size)(const struct audio_hw_device *dev,
642                                     const struct audio_config *config);
643 
644     /** This method creates and opens the audio hardware output stream.
645      * The "address" parameter qualifies the "devices" audio device type if needed.
646      * The format format depends on the device type:
647      * - Bluetooth devices use the MAC address of the device in the form "00:11:22:AA:BB:CC"
648      * - USB devices use the ALSA card and device numbers in the form  "card=X;device=Y"
649      * - Other devices may use a number or any other string.
650      */
651 
652     int (*open_output_stream)(struct audio_hw_device *dev,
653                               audio_io_handle_t handle,
654                               audio_devices_t devices,
655                               audio_output_flags_t flags,
656                               struct audio_config *config,
657                               struct audio_stream_out **stream_out,
658                               const char *address);
659 
660     void (*close_output_stream)(struct audio_hw_device *dev,
661                                 struct audio_stream_out* stream_out);
662 
663     /** This method creates and opens the audio hardware input stream */
664     int (*open_input_stream)(struct audio_hw_device *dev,
665                              audio_io_handle_t handle,
666                              audio_devices_t devices,
667                              struct audio_config *config,
668                              struct audio_stream_in **stream_in,
669                              audio_input_flags_t flags,
670                              const char *address,
671                              audio_source_t source);
672 
673     void (*close_input_stream)(struct audio_hw_device *dev,
674                                struct audio_stream_in *stream_in);
675 
676     /** This method dumps the state of the audio hardware */
677     int (*dump)(const struct audio_hw_device *dev, int fd);
678 
679     /**
680      * set the audio mute status for all audio activities.  If any value other
681      * than 0 is returned, the software mixer will emulate this capability.
682      */
683     int (*set_master_mute)(struct audio_hw_device *dev, bool mute);
684 
685     /**
686      * Get the current master mute status for the HAL, if the HAL supports
687      * master mute control.  AudioFlinger will query this value from the primary
688      * audio HAL when the service starts and use the value for setting the
689      * initial master mute across all HALs.  HALs which do not support this
690      * method may leave it set to NULL.
691      */
692     int (*get_master_mute)(struct audio_hw_device *dev, bool *mute);
693 
694     /**
695      * Routing control
696      */
697 
698     /* Creates an audio patch between several source and sink ports.
699      * The handle is allocated by the HAL and should be unique for this
700      * audio HAL module. */
701     int (*create_audio_patch)(struct audio_hw_device *dev,
702                                unsigned int num_sources,
703                                const struct audio_port_config *sources,
704                                unsigned int num_sinks,
705                                const struct audio_port_config *sinks,
706                                audio_patch_handle_t *handle);
707 
708     /* Release an audio patch */
709     int (*release_audio_patch)(struct audio_hw_device *dev,
710                                audio_patch_handle_t handle);
711 
712     /* Fills the list of supported attributes for a given audio port.
713      * As input, "port" contains the information (type, role, address etc...)
714      * needed by the HAL to identify the port.
715      * As output, "port" contains possible attributes (sampling rates, formats,
716      * channel masks, gain controllers...) for this port.
717      */
718     int (*get_audio_port)(struct audio_hw_device *dev,
719                           struct audio_port *port);
720 
721     /* Set audio port configuration */
722     int (*set_audio_port_config)(struct audio_hw_device *dev,
723                          const struct audio_port_config *config);
724 
725 };
726 typedef struct audio_hw_device audio_hw_device_t;
727 
728 /** convenience API for opening and closing a supported device */
729 
audio_hw_device_open(const struct hw_module_t * module,struct audio_hw_device ** device)730 static inline int audio_hw_device_open(const struct hw_module_t* module,
731                                        struct audio_hw_device** device)
732 {
733     return module->methods->open(module, AUDIO_HARDWARE_INTERFACE,
734                                  TO_HW_DEVICE_T_OPEN(device));
735 }
736 
audio_hw_device_close(struct audio_hw_device * device)737 static inline int audio_hw_device_close(struct audio_hw_device* device)
738 {
739     return device->common.close(&device->common);
740 }
741 
742 
743 __END_DECLS
744 
745 #endif  // ANDROID_AUDIO_INTERFACE_H
746