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_3_1 HARDWARE_DEVICE_API_VERSION(3, 1)
60 #define AUDIO_DEVICE_API_VERSION_CURRENT AUDIO_DEVICE_API_VERSION_3_1
61 /* Minimal audio HAL version supported by the audio framework */
62 #define AUDIO_DEVICE_API_VERSION_MIN AUDIO_DEVICE_API_VERSION_2_0
63 
64 /**************************************/
65 
66 /**
67  *  standard audio parameters that the HAL may need to handle
68  */
69 
70 /**
71  *  audio device parameters
72  */
73 
74 /* TTY mode selection */
75 #define AUDIO_PARAMETER_KEY_TTY_MODE "tty_mode"
76 #define AUDIO_PARAMETER_VALUE_TTY_OFF "tty_off"
77 #define AUDIO_PARAMETER_VALUE_TTY_VCO "tty_vco"
78 #define AUDIO_PARAMETER_VALUE_TTY_HCO "tty_hco"
79 #define AUDIO_PARAMETER_VALUE_TTY_FULL "tty_full"
80 
81 /* Hearing Aid Compatibility - Telecoil (HAC-T) mode on/off */
82 #define AUDIO_PARAMETER_KEY_HAC "HACSetting"
83 #define AUDIO_PARAMETER_VALUE_HAC_ON "ON"
84 #define AUDIO_PARAMETER_VALUE_HAC_OFF "OFF"
85 
86 /* A2DP sink address set by framework */
87 #define AUDIO_PARAMETER_A2DP_SINK_ADDRESS "a2dp_sink_address"
88 
89 /* A2DP source address set by framework */
90 #define AUDIO_PARAMETER_A2DP_SOURCE_ADDRESS "a2dp_source_address"
91 
92 /* Bluetooth SCO wideband */
93 #define AUDIO_PARAMETER_KEY_BT_SCO_WB "bt_wbs"
94 
95 /* BT SCO headset name for debug */
96 #define AUDIO_PARAMETER_KEY_BT_SCO_HEADSET_NAME "bt_headset_name"
97 
98 /* BT SCO HFP control */
99 #define AUDIO_PARAMETER_KEY_HFP_ENABLE            "hfp_enable"
100 #define AUDIO_PARAMETER_KEY_HFP_SET_SAMPLING_RATE "hfp_set_sampling_rate"
101 #define AUDIO_PARAMETER_KEY_HFP_VOLUME            "hfp_volume"
102 
103 /* Set screen orientation */
104 #define AUDIO_PARAMETER_KEY_ROTATION "rotation"
105 
106 /**
107  *  audio stream parameters
108  */
109 
110 /* Enable AANC */
111 #define AUDIO_PARAMETER_KEY_AANC "aanc_enabled"
112 
113 /**************************************/
114 
115 /* common audio stream parameters and operations */
116 struct audio_stream {
117 
118     /**
119      * Return the sampling rate in Hz - eg. 44100.
120      */
121     uint32_t (*get_sample_rate)(const struct audio_stream *stream);
122 
123     /* currently unused - use set_parameters with key
124      *    AUDIO_PARAMETER_STREAM_SAMPLING_RATE
125      */
126     int (*set_sample_rate)(struct audio_stream *stream, uint32_t rate);
127 
128     /**
129      * Return size of input/output buffer in bytes for this stream - eg. 4800.
130      * It should be a multiple of the frame size.  See also get_input_buffer_size.
131      */
132     size_t (*get_buffer_size)(const struct audio_stream *stream);
133 
134     /**
135      * Return the channel mask -
136      *  e.g. AUDIO_CHANNEL_OUT_STEREO or AUDIO_CHANNEL_IN_STEREO
137      */
138     audio_channel_mask_t (*get_channels)(const struct audio_stream *stream);
139 
140     /**
141      * Return the audio format - e.g. AUDIO_FORMAT_PCM_16_BIT
142      */
143     audio_format_t (*get_format)(const struct audio_stream *stream);
144 
145     /* currently unused - use set_parameters with key
146      *     AUDIO_PARAMETER_STREAM_FORMAT
147      */
148     int (*set_format)(struct audio_stream *stream, audio_format_t format);
149 
150     /**
151      * Put the audio hardware input/output into standby mode.
152      * Driver should exit from standby mode at the next I/O operation.
153      * Returns 0 on success and <0 on failure.
154      */
155     int (*standby)(struct audio_stream *stream);
156 
157     /** dump the state of the audio input/output device */
158     int (*dump)(const struct audio_stream *stream, int fd);
159 
160     /** Return the set of device(s) which this stream is connected to */
161     audio_devices_t (*get_device)(const struct audio_stream *stream);
162 
163     /**
164      * Currently unused - set_device() corresponds to set_parameters() with key
165      * AUDIO_PARAMETER_STREAM_ROUTING for both input and output.
166      * AUDIO_PARAMETER_STREAM_INPUT_SOURCE is an additional information used by
167      * input streams only.
168      */
169     int (*set_device)(struct audio_stream *stream, audio_devices_t device);
170 
171     /**
172      * set/get audio stream parameters. The function accepts a list of
173      * parameter key value pairs in the form: key1=value1;key2=value2;...
174      *
175      * Some keys are reserved for standard parameters (See AudioParameter class)
176      *
177      * If the implementation does not accept a parameter change while
178      * the output is active but the parameter is acceptable otherwise, it must
179      * return -ENOSYS.
180      *
181      * The audio flinger will put the stream in standby and then change the
182      * parameter value.
183      */
184     int (*set_parameters)(struct audio_stream *stream, const char *kv_pairs);
185 
186     /*
187      * Returns a pointer to a heap allocated string. The caller is responsible
188      * for freeing the memory for it using free().
189      */
190     char * (*get_parameters)(const struct audio_stream *stream,
191                              const char *keys);
192     int (*add_audio_effect)(const struct audio_stream *stream,
193                              effect_handle_t effect);
194     int (*remove_audio_effect)(const struct audio_stream *stream,
195                              effect_handle_t effect);
196 };
197 typedef struct audio_stream audio_stream_t;
198 
199 /* type of asynchronous write callback events. Mutually exclusive */
200 typedef enum {
201     STREAM_CBK_EVENT_WRITE_READY, /* non blocking write completed */
202     STREAM_CBK_EVENT_DRAIN_READY,  /* drain completed */
203     STREAM_CBK_EVENT_ERROR, /* stream hit some error, let AF take action */
204 } stream_callback_event_t;
205 
206 typedef int (*stream_callback_t)(stream_callback_event_t event, void *param, void *cookie);
207 
208 /* type of drain requested to audio_stream_out->drain(). Mutually exclusive */
209 typedef enum {
210     AUDIO_DRAIN_ALL,            /* drain() returns when all data has been played */
211     AUDIO_DRAIN_EARLY_NOTIFY    /* drain() returns a short time before all data
212                                    from the current track has been played to
213                                    give time for gapless track switch */
214 } audio_drain_type_t;
215 
216 typedef struct source_metadata {
217     size_t track_count;
218     /** Array of metadata of each track connected to this source. */
219     struct playback_track_metadata* tracks;
220 } source_metadata_t;
221 
222 typedef struct sink_metadata {
223     size_t track_count;
224     /** Array of metadata of each track connected to this sink. */
225     struct record_track_metadata* tracks;
226 } sink_metadata_t;
227 
228 /**
229  * audio_stream_out is the abstraction interface for the audio output hardware.
230  *
231  * It provides information about various properties of the audio output
232  * hardware driver.
233  */
234 struct audio_stream_out {
235     /**
236      * Common methods of the audio stream out.  This *must* be the first member of audio_stream_out
237      * as users of this structure will cast a audio_stream to audio_stream_out pointer in contexts
238      * where it's known the audio_stream references an audio_stream_out.
239      */
240     struct audio_stream common;
241 
242     /**
243      * Return the audio hardware driver estimated latency in milliseconds.
244      */
245     uint32_t (*get_latency)(const struct audio_stream_out *stream);
246 
247     /**
248      * Use this method in situations where audio mixing is done in the
249      * hardware. This method serves as a direct interface with hardware,
250      * allowing you to directly set the volume as apposed to via the framework.
251      * This method might produce multiple PCM outputs or hardware accelerated
252      * codecs, such as MP3 or AAC.
253      */
254     int (*set_volume)(struct audio_stream_out *stream, float left, float right);
255 
256     /**
257      * Write audio buffer to driver. Returns number of bytes written, or a
258      * negative status_t. If at least one frame was written successfully prior to the error,
259      * it is suggested that the driver return that successful (short) byte count
260      * and then return an error in the subsequent call.
261      *
262      * If set_callback() has previously been called to enable non-blocking mode
263      * the write() is not allowed to block. It must write only the number of
264      * bytes that currently fit in the driver/hardware buffer and then return
265      * this byte count. If this is less than the requested write size the
266      * callback function must be called when more space is available in the
267      * driver/hardware buffer.
268      */
269     ssize_t (*write)(struct audio_stream_out *stream, const void* buffer,
270                      size_t bytes);
271 
272     /* return the number of audio frames written by the audio dsp to DAC since
273      * the output has exited standby
274      */
275     int (*get_render_position)(const struct audio_stream_out *stream,
276                                uint32_t *dsp_frames);
277 
278     /**
279      * get the local time at which the next write to the audio driver will be presented.
280      * The units are microseconds, where the epoch is decided by the local audio HAL.
281      */
282     int (*get_next_write_timestamp)(const struct audio_stream_out *stream,
283                                     int64_t *timestamp);
284 
285     /**
286      * set the callback function for notifying completion of non-blocking
287      * write and drain.
288      * Calling this function implies that all future write() and drain()
289      * must be non-blocking and use the callback to signal completion.
290      */
291     int (*set_callback)(struct audio_stream_out *stream,
292             stream_callback_t callback, void *cookie);
293 
294     /**
295      * Notifies to the audio driver to stop playback however the queued buffers are
296      * retained by the hardware. Useful for implementing pause/resume. Empty implementation
297      * if not supported however should be implemented for hardware with non-trivial
298      * latency. In the pause state audio hardware could still be using power. User may
299      * consider calling suspend after a timeout.
300      *
301      * Implementation of this function is mandatory for offloaded playback.
302      */
303     int (*pause)(struct audio_stream_out* stream);
304 
305     /**
306      * Notifies to the audio driver to resume playback following a pause.
307      * Returns error if called without matching pause.
308      *
309      * Implementation of this function is mandatory for offloaded playback.
310      */
311     int (*resume)(struct audio_stream_out* stream);
312 
313     /**
314      * Requests notification when data buffered by the driver/hardware has
315      * been played. If set_callback() has previously been called to enable
316      * non-blocking mode, the drain() must not block, instead it should return
317      * quickly and completion of the drain is notified through the callback.
318      * If set_callback() has not been called, the drain() must block until
319      * completion.
320      * If type==AUDIO_DRAIN_ALL, the drain completes when all previously written
321      * data has been played.
322      * If type==AUDIO_DRAIN_EARLY_NOTIFY, the drain completes shortly before all
323      * data for the current track has played to allow time for the framework
324      * to perform a gapless track switch.
325      *
326      * Drain must return immediately on stop() and flush() call
327      *
328      * Implementation of this function is mandatory for offloaded playback.
329      */
330     int (*drain)(struct audio_stream_out* stream, audio_drain_type_t type );
331 
332     /**
333      * Notifies to the audio driver to flush the queued data. Stream must already
334      * be paused before calling flush().
335      *
336      * Implementation of this function is mandatory for offloaded playback.
337      */
338    int (*flush)(struct audio_stream_out* stream);
339 
340     /**
341      * Return a recent count of the number of audio frames presented to an external observer.
342      * This excludes frames which have been written but are still in the pipeline.
343      * The count is not reset to zero when output enters standby.
344      * Also returns the value of CLOCK_MONOTONIC as of this presentation count.
345      * The returned count is expected to be 'recent',
346      * but does not need to be the most recent possible value.
347      * However, the associated time should correspond to whatever count is returned.
348      * Example:  assume that N+M frames have been presented, where M is a 'small' number.
349      * Then it is permissible to return N instead of N+M,
350      * and the timestamp should correspond to N rather than N+M.
351      * The terms 'recent' and 'small' are not defined.
352      * They reflect the quality of the implementation.
353      *
354      * 3.0 and higher only.
355      */
356     int (*get_presentation_position)(const struct audio_stream_out *stream,
357                                uint64_t *frames, struct timespec *timestamp);
358 
359     /**
360      * Called by the framework to start a stream operating in mmap mode.
361      * create_mmap_buffer must be called before calling start()
362      *
363      * \note Function only implemented by streams operating in mmap mode.
364      *
365      * \param[in] stream the stream object.
366      * \return 0 in case of success.
367      *         -ENOSYS if called out of sequence or on non mmap stream
368      */
369     int (*start)(const struct audio_stream_out* stream);
370 
371     /**
372      * Called by the framework to stop a stream operating in mmap mode.
373      * Must be called after start()
374      *
375      * \note Function only implemented by streams operating in mmap mode.
376      *
377      * \param[in] stream the stream object.
378      * \return 0 in case of success.
379      *         -ENOSYS if called out of sequence or on non mmap stream
380      */
381     int (*stop)(const struct audio_stream_out* stream);
382 
383     /**
384      * Called by the framework to retrieve information on the mmap buffer used for audio
385      * samples transfer.
386      *
387      * \note Function only implemented by streams operating in mmap mode.
388      *
389      * \param[in] stream the stream object.
390      * \param[in] min_size_frames minimum buffer size requested. The actual buffer
391      *        size returned in struct audio_mmap_buffer_info can be larger.
392      * \param[out] info address at which the mmap buffer information should be returned.
393      *
394      * \return 0 if the buffer was allocated.
395      *         -ENODEV in case of initialization error
396      *         -EINVAL if the requested buffer size is too large
397      *         -ENOSYS if called out of sequence (e.g. buffer already allocated)
398      */
399     int (*create_mmap_buffer)(const struct audio_stream_out *stream,
400                               int32_t min_size_frames,
401                               struct audio_mmap_buffer_info *info);
402 
403     /**
404      * Called by the framework to read current read/write position in the mmap buffer
405      * with associated time stamp.
406      *
407      * \note Function only implemented by streams operating in mmap mode.
408      *
409      * \param[in] stream the stream object.
410      * \param[out] position address at which the mmap read/write position should be returned.
411      *
412      * \return 0 if the position is successfully returned.
413      *         -ENODATA if the position cannot be retrieved
414      *         -ENOSYS if called before create_mmap_buffer()
415      */
416     int (*get_mmap_position)(const struct audio_stream_out *stream,
417                              struct audio_mmap_position *position);
418 
419     /**
420      * Called when the metadata of the stream's source has been changed.
421      * @param source_metadata Description of the audio that is played by the clients.
422      */
423     void (*update_source_metadata)(struct audio_stream_out *stream,
424                                    const struct source_metadata* source_metadata);
425 };
426 typedef struct audio_stream_out audio_stream_out_t;
427 
428 struct audio_stream_in {
429     /**
430      * Common methods of the audio stream in.  This *must* be the first member of audio_stream_in
431      * as users of this structure will cast a audio_stream to audio_stream_in pointer in contexts
432      * where it's known the audio_stream references an audio_stream_in.
433      */
434     struct audio_stream common;
435 
436     /** set the input gain for the audio driver. This method is for
437      *  for future use */
438     int (*set_gain)(struct audio_stream_in *stream, float gain);
439 
440     /** Read audio buffer in from audio driver. Returns number of bytes read, or a
441      *  negative status_t. If at least one frame was read prior to the error,
442      *  read should return that byte count and then return an error in the subsequent call.
443      */
444     ssize_t (*read)(struct audio_stream_in *stream, void* buffer,
445                     size_t bytes);
446 
447     /**
448      * Return the amount of input frames lost in the audio driver since the
449      * last call of this function.
450      * Audio driver is expected to reset the value to 0 and restart counting
451      * upon returning the current value by this function call.
452      * Such loss typically occurs when the user space process is blocked
453      * longer than the capacity of audio driver buffers.
454      *
455      * Unit: the number of input audio frames
456      */
457     uint32_t (*get_input_frames_lost)(struct audio_stream_in *stream);
458 
459     /**
460      * Return a recent count of the number of audio frames received and
461      * the clock time associated with that frame count.
462      *
463      * frames is the total frame count received. This should be as early in
464      *     the capture pipeline as possible. In general,
465      *     frames should be non-negative and should not go "backwards".
466      *
467      * time is the clock MONOTONIC time when frames was measured. In general,
468      *     time should be a positive quantity and should not go "backwards".
469      *
470      * The status returned is 0 on success, -ENOSYS if the device is not
471      * ready/available, or -EINVAL if the arguments are null or otherwise invalid.
472      */
473     int (*get_capture_position)(const struct audio_stream_in *stream,
474                                 int64_t *frames, int64_t *time);
475 
476     /**
477      * Called by the framework to start a stream operating in mmap mode.
478      * create_mmap_buffer must be called before calling start()
479      *
480      * \note Function only implemented by streams operating in mmap mode.
481      *
482      * \param[in] stream the stream object.
483      * \return 0 in case off success.
484      *         -ENOSYS if called out of sequence or on non mmap stream
485      */
486     int (*start)(const struct audio_stream_in* stream);
487 
488     /**
489      * Called by the framework to stop a stream operating in mmap mode.
490      *
491      * \note Function only implemented by streams operating in mmap mode.
492      *
493      * \param[in] stream the stream object.
494      * \return 0 in case of success.
495      *         -ENOSYS if called out of sequence or on non mmap stream
496      */
497     int (*stop)(const struct audio_stream_in* stream);
498 
499     /**
500      * Called by the framework to retrieve information on the mmap buffer used for audio
501      * samples transfer.
502      *
503      * \note Function only implemented by streams operating in mmap mode.
504      *
505      * \param[in] stream the stream object.
506      * \param[in] min_size_frames minimum buffer size requested. The actual buffer
507      *        size returned in struct audio_mmap_buffer_info can be larger.
508      * \param[out] info address at which the mmap buffer information should be returned.
509      *
510      * \return 0 if the buffer was allocated.
511      *         -ENODEV in case of initialization error
512      *         -EINVAL if the requested buffer size is too large
513      *         -ENOSYS if called out of sequence (e.g. buffer already allocated)
514      */
515     int (*create_mmap_buffer)(const struct audio_stream_in *stream,
516                               int32_t min_size_frames,
517                               struct audio_mmap_buffer_info *info);
518 
519     /**
520      * Called by the framework to read current read/write position in the mmap buffer
521      * with associated time stamp.
522      *
523      * \note Function only implemented by streams operating in mmap mode.
524      *
525      * \param[in] stream the stream object.
526      * \param[out] position address at which the mmap read/write position should be returned.
527      *
528      * \return 0 if the position is successfully returned.
529      *         -ENODATA if the position cannot be retreived
530      *         -ENOSYS if called before mmap_read_position()
531      */
532     int (*get_mmap_position)(const struct audio_stream_in *stream,
533                              struct audio_mmap_position *position);
534 
535     /**
536      * Called by the framework to read active microphones
537      *
538      * \param[in] stream the stream object.
539      * \param[out] mic_array Pointer to first element on array with microphone info
540      * \param[out] mic_count When called, this holds the value of the max number of elements
541      *                       allowed in the mic_array. The actual number of elements written
542      *                       is returned here.
543      *                       if mic_count is passed as zero, mic_array will not be populated,
544      *                       and mic_count will return the actual number of active microphones.
545      *
546      * \return 0 if the microphone array is successfully filled.
547      *         -ENOSYS if there is an error filling the data
548      */
549     int (*get_active_microphones)(const struct audio_stream_in *stream,
550                                   struct audio_microphone_characteristic_t *mic_array,
551                                   size_t *mic_count);
552 
553     /**
554      * Called by the framework to instruct the HAL to optimize the capture stream in the
555      * specified direction.
556      *
557      * \param[in] stream    the stream object.
558      * \param[in] direction The direction constant (from audio-base.h)
559      *   MIC_DIRECTION_UNSPECIFIED  Don't do any directionality processing of the
560      *      activated microphone(s).
561      *   MIC_DIRECTION_FRONT        Optimize capture for audio coming from the screen-side
562      *      of the device.
563      *   MIC_DIRECTION_BACK         Optimize capture for audio coming from the side of the
564      *      device opposite the screen.
565      *   MIC_DIRECTION_EXTERNAL     Optimize capture for audio coming from an off-device
566      *      microphone.
567      * \return OK if the call is successful, an error code otherwise.
568      */
569     int (*set_microphone_direction)(const struct audio_stream_in *stream,
570                                     audio_microphone_direction_t direction);
571 
572     /**
573      * Called by the framework to specify to the HAL the desired zoom factor for the selected
574      * microphone(s).
575      *
576      * \param[in] stream    the stream object.
577      * \param[in] zoom      the zoom factor.
578      * \return OK if the call is successful, an error code otherwise.
579      */
580     int (*set_microphone_field_dimension)(const struct audio_stream_in *stream,
581                                           float zoom);
582 
583     /**
584      * Called when the metadata of the stream's sink has been changed.
585      * @param sink_metadata Description of the audio that is recorded by the clients.
586      */
587     void (*update_sink_metadata)(struct audio_stream_in *stream,
588                                  const struct sink_metadata* sink_metadata);
589 };
590 typedef struct audio_stream_in audio_stream_in_t;
591 
592 /**
593  * return the frame size (number of bytes per sample).
594  *
595  * Deprecated: use audio_stream_out_frame_size() or audio_stream_in_frame_size() instead.
596  */
597 __attribute__((__deprecated__))
audio_stream_frame_size(const struct audio_stream * s)598 static inline size_t audio_stream_frame_size(const struct audio_stream *s)
599 {
600     size_t chan_samp_sz;
601     audio_format_t format = s->get_format(s);
602 
603     if (audio_has_proportional_frames(format)) {
604         chan_samp_sz = audio_bytes_per_sample(format);
605         return popcount(s->get_channels(s)) * chan_samp_sz;
606     }
607 
608     return sizeof(int8_t);
609 }
610 
611 /**
612  * return the frame size (number of bytes per sample) of an output stream.
613  */
audio_stream_out_frame_size(const struct audio_stream_out * s)614 static inline size_t audio_stream_out_frame_size(const struct audio_stream_out *s)
615 {
616     size_t chan_samp_sz;
617     audio_format_t format = s->common.get_format(&s->common);
618 
619     if (audio_has_proportional_frames(format)) {
620         chan_samp_sz = audio_bytes_per_sample(format);
621         return audio_channel_count_from_out_mask(s->common.get_channels(&s->common)) * chan_samp_sz;
622     }
623 
624     return sizeof(int8_t);
625 }
626 
627 /**
628  * return the frame size (number of bytes per sample) of an input stream.
629  */
audio_stream_in_frame_size(const struct audio_stream_in * s)630 static inline size_t audio_stream_in_frame_size(const struct audio_stream_in *s)
631 {
632     size_t chan_samp_sz;
633     audio_format_t format = s->common.get_format(&s->common);
634 
635     if (audio_has_proportional_frames(format)) {
636         chan_samp_sz = audio_bytes_per_sample(format);
637         return audio_channel_count_from_in_mask(s->common.get_channels(&s->common)) * chan_samp_sz;
638     }
639 
640     return sizeof(int8_t);
641 }
642 
643 /**********************************************************************/
644 
645 /**
646  * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
647  * and the fields of this data structure must begin with hw_module_t
648  * followed by module specific information.
649  */
650 struct audio_module {
651     struct hw_module_t common;
652 };
653 
654 struct audio_hw_device {
655     /**
656      * Common methods of the audio device.  This *must* be the first member of audio_hw_device
657      * as users of this structure will cast a hw_device_t to audio_hw_device pointer in contexts
658      * where it's known the hw_device_t references an audio_hw_device.
659      */
660     struct hw_device_t common;
661 
662     /**
663      * used by audio flinger to enumerate what devices are supported by
664      * each audio_hw_device implementation.
665      *
666      * Return value is a bitmask of 1 or more values of audio_devices_t
667      *
668      * NOTE: audio HAL implementations starting with
669      * AUDIO_DEVICE_API_VERSION_2_0 do not implement this function.
670      * All supported devices should be listed in audio_policy.conf
671      * file and the audio policy manager must choose the appropriate
672      * audio module based on information in this file.
673      */
674     uint32_t (*get_supported_devices)(const struct audio_hw_device *dev);
675 
676     /**
677      * check to see if the audio hardware interface has been initialized.
678      * returns 0 on success, -ENODEV on failure.
679      */
680     int (*init_check)(const struct audio_hw_device *dev);
681 
682     /** set the audio volume of a voice call. Range is between 0.0 and 1.0 */
683     int (*set_voice_volume)(struct audio_hw_device *dev, float volume);
684 
685     /**
686      * set the audio volume for all audio activities other than voice call.
687      * Range between 0.0 and 1.0. If any value other than 0 is returned,
688      * the software mixer will emulate this capability.
689      */
690     int (*set_master_volume)(struct audio_hw_device *dev, float volume);
691 
692     /**
693      * Get the current master volume value for the HAL, if the HAL supports
694      * master volume control.  AudioFlinger will query this value from the
695      * primary audio HAL when the service starts and use the value for setting
696      * the initial master volume across all HALs.  HALs which do not support
697      * this method may leave it set to NULL.
698      */
699     int (*get_master_volume)(struct audio_hw_device *dev, float *volume);
700 
701     /**
702      * set_mode is called when the audio mode changes. AUDIO_MODE_NORMAL mode
703      * is for standard audio playback, AUDIO_MODE_RINGTONE when a ringtone is
704      * playing, and AUDIO_MODE_IN_CALL when a call is in progress.
705      */
706     int (*set_mode)(struct audio_hw_device *dev, audio_mode_t mode);
707 
708     /* mic mute */
709     int (*set_mic_mute)(struct audio_hw_device *dev, bool state);
710     int (*get_mic_mute)(const struct audio_hw_device *dev, bool *state);
711 
712     /* set/get global audio parameters */
713     int (*set_parameters)(struct audio_hw_device *dev, const char *kv_pairs);
714 
715     /*
716      * Returns a pointer to a heap allocated string. The caller is responsible
717      * for freeing the memory for it using free().
718      */
719     char * (*get_parameters)(const struct audio_hw_device *dev,
720                              const char *keys);
721 
722     /* Returns audio input buffer size according to parameters passed or
723      * 0 if one of the parameters is not supported.
724      * See also get_buffer_size which is for a particular stream.
725      */
726     size_t (*get_input_buffer_size)(const struct audio_hw_device *dev,
727                                     const struct audio_config *config);
728 
729     /** This method creates and opens the audio hardware output stream.
730      * The "address" parameter qualifies the "devices" audio device type if needed.
731      * The format format depends on the device type:
732      * - Bluetooth devices use the MAC address of the device in the form "00:11:22:AA:BB:CC"
733      * - USB devices use the ALSA card and device numbers in the form  "card=X;device=Y"
734      * - Other devices may use a number or any other string.
735      */
736 
737     int (*open_output_stream)(struct audio_hw_device *dev,
738                               audio_io_handle_t handle,
739                               audio_devices_t devices,
740                               audio_output_flags_t flags,
741                               struct audio_config *config,
742                               struct audio_stream_out **stream_out,
743                               const char *address);
744 
745     void (*close_output_stream)(struct audio_hw_device *dev,
746                                 struct audio_stream_out* stream_out);
747 
748     /** This method creates and opens the audio hardware input stream */
749     int (*open_input_stream)(struct audio_hw_device *dev,
750                              audio_io_handle_t handle,
751                              audio_devices_t devices,
752                              struct audio_config *config,
753                              struct audio_stream_in **stream_in,
754                              audio_input_flags_t flags,
755                              const char *address,
756                              audio_source_t source);
757 
758     void (*close_input_stream)(struct audio_hw_device *dev,
759                                struct audio_stream_in *stream_in);
760 
761     /**
762      * Called by the framework to read available microphones characteristics.
763      *
764      * \param[in] dev the hw_device object.
765      * \param[out] mic_array Pointer to first element on array with microphone info
766      * \param[out] mic_count When called, this holds the value of the max number of elements
767      *                       allowed in the mic_array. The actual number of elements written
768      *                       is returned here.
769      *                       if mic_count is passed as zero, mic_array will not be populated,
770      *                       and mic_count will return the actual number of microphones in the
771      *                       system.
772      *
773      * \return 0 if the microphone array is successfully filled.
774      *         -ENOSYS if there is an error filling the data
775      */
776     int (*get_microphones)(const struct audio_hw_device *dev,
777                            struct audio_microphone_characteristic_t *mic_array,
778                            size_t *mic_count);
779 
780     /** This method dumps the state of the audio hardware */
781     int (*dump)(const struct audio_hw_device *dev, int fd);
782 
783     /**
784      * set the audio mute status for all audio activities.  If any value other
785      * than 0 is returned, the software mixer will emulate this capability.
786      */
787     int (*set_master_mute)(struct audio_hw_device *dev, bool mute);
788 
789     /**
790      * Get the current master mute status for the HAL, if the HAL supports
791      * master mute control.  AudioFlinger will query this value from the primary
792      * audio HAL when the service starts and use the value for setting the
793      * initial master mute across all HALs.  HALs which do not support this
794      * method may leave it set to NULL.
795      */
796     int (*get_master_mute)(struct audio_hw_device *dev, bool *mute);
797 
798     /**
799      * Routing control
800      */
801 
802     /* Creates an audio patch between several source and sink ports.
803      * The handle is allocated by the HAL and should be unique for this
804      * audio HAL module. */
805     int (*create_audio_patch)(struct audio_hw_device *dev,
806                                unsigned int num_sources,
807                                const struct audio_port_config *sources,
808                                unsigned int num_sinks,
809                                const struct audio_port_config *sinks,
810                                audio_patch_handle_t *handle);
811 
812     /* Release an audio patch */
813     int (*release_audio_patch)(struct audio_hw_device *dev,
814                                audio_patch_handle_t handle);
815 
816     /* Fills the list of supported attributes for a given audio port.
817      * As input, "port" contains the information (type, role, address etc...)
818      * needed by the HAL to identify the port.
819      * As output, "port" contains possible attributes (sampling rates, formats,
820      * channel masks, gain controllers...) for this port.
821      */
822     int (*get_audio_port)(struct audio_hw_device *dev,
823                           struct audio_port *port);
824 
825     /* Set audio port configuration */
826     int (*set_audio_port_config)(struct audio_hw_device *dev,
827                          const struct audio_port_config *config);
828 
829     /**
830      * Applies an audio effect to an audio device.
831      *
832      * @param dev the audio HAL device context.
833      * @param device identifies the sink or source device the effect must be applied to.
834      *               "device" is the audio_port_handle_t indicated for the device when
835      *               the audio patch connecting that device was created.
836      * @param effect effect interface handle corresponding to the effect being added.
837      * @return retval operation completion status.
838      */
839     int (*add_device_effect)(struct audio_hw_device *dev,
840                         audio_port_handle_t device, effect_handle_t effect);
841 
842     /**
843      * Stops applying an audio effect to an audio device.
844      *
845      * @param dev the audio HAL device context.
846      * @param device identifies the sink or source device this effect was applied to.
847      *               "device" is the audio_port_handle_t indicated for the device when
848      *               the audio patch is created.
849      * @param effect effect interface handle corresponding to the effect being removed.
850      * @return retval operation completion status.
851      */
852     int (*remove_device_effect)(struct audio_hw_device *dev,
853                         audio_port_handle_t device, effect_handle_t effect);
854 };
855 typedef struct audio_hw_device audio_hw_device_t;
856 
857 /** convenience API for opening and closing a supported device */
858 
audio_hw_device_open(const struct hw_module_t * module,struct audio_hw_device ** device)859 static inline int audio_hw_device_open(const struct hw_module_t* module,
860                                        struct audio_hw_device** device)
861 {
862     return module->methods->open(module, AUDIO_HARDWARE_INTERFACE,
863                                  TO_HW_DEVICE_T_OPEN(device));
864 }
865 
audio_hw_device_close(struct audio_hw_device * device)866 static inline int audio_hw_device_close(struct audio_hw_device* device)
867 {
868     return device->common.close(&device->common);
869 }
870 
871 
872 __END_DECLS
873 
874 #endif  // ANDROID_AUDIO_INTERFACE_H
875