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