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