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