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