Home | History | Annotate | Download | only in hardware
      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 when the metadata of the stream's sink has been changed.
    554      * @param sink_metadata Description of the audio that is recorded by the clients.
    555      */
    556     void (*update_sink_metadata)(struct audio_stream_in *stream,
    557                                  const struct sink_metadata* sink_metadata);
    558 };
    559 typedef struct audio_stream_in audio_stream_in_t;
    560 
    561 /**
    562  * return the frame size (number of bytes per sample).
    563  *
    564  * Deprecated: use audio_stream_out_frame_size() or audio_stream_in_frame_size() instead.
    565  */
    566 __attribute__((__deprecated__))
    567 static inline size_t audio_stream_frame_size(const struct audio_stream *s)
    568 {
    569     size_t chan_samp_sz;
    570     audio_format_t format = s->get_format(s);
    571 
    572     if (audio_has_proportional_frames(format)) {
    573         chan_samp_sz = audio_bytes_per_sample(format);
    574         return popcount(s->get_channels(s)) * chan_samp_sz;
    575     }
    576 
    577     return sizeof(int8_t);
    578 }
    579 
    580 /**
    581  * return the frame size (number of bytes per sample) of an output stream.
    582  */
    583 static inline size_t audio_stream_out_frame_size(const struct audio_stream_out *s)
    584 {
    585     size_t chan_samp_sz;
    586     audio_format_t format = s->common.get_format(&s->common);
    587 
    588     if (audio_has_proportional_frames(format)) {
    589         chan_samp_sz = audio_bytes_per_sample(format);
    590         return audio_channel_count_from_out_mask(s->common.get_channels(&s->common)) * chan_samp_sz;
    591     }
    592 
    593     return sizeof(int8_t);
    594 }
    595 
    596 /**
    597  * return the frame size (number of bytes per sample) of an input stream.
    598  */
    599 static inline size_t audio_stream_in_frame_size(const struct audio_stream_in *s)
    600 {
    601     size_t chan_samp_sz;
    602     audio_format_t format = s->common.get_format(&s->common);
    603 
    604     if (audio_has_proportional_frames(format)) {
    605         chan_samp_sz = audio_bytes_per_sample(format);
    606         return audio_channel_count_from_in_mask(s->common.get_channels(&s->common)) * chan_samp_sz;
    607     }
    608 
    609     return sizeof(int8_t);
    610 }
    611 
    612 /**********************************************************************/
    613 
    614 /**
    615  * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
    616  * and the fields of this data structure must begin with hw_module_t
    617  * followed by module specific information.
    618  */
    619 struct audio_module {
    620     struct hw_module_t common;
    621 };
    622 
    623 struct audio_hw_device {
    624     /**
    625      * Common methods of the audio device.  This *must* be the first member of audio_hw_device
    626      * as users of this structure will cast a hw_device_t to audio_hw_device pointer in contexts
    627      * where it's known the hw_device_t references an audio_hw_device.
    628      */
    629     struct hw_device_t common;
    630 
    631     /**
    632      * used by audio flinger to enumerate what devices are supported by
    633      * each audio_hw_device implementation.
    634      *
    635      * Return value is a bitmask of 1 or more values of audio_devices_t
    636      *
    637      * NOTE: audio HAL implementations starting with
    638      * AUDIO_DEVICE_API_VERSION_2_0 do not implement this function.
    639      * All supported devices should be listed in audio_policy.conf
    640      * file and the audio policy manager must choose the appropriate
    641      * audio module based on information in this file.
    642      */
    643     uint32_t (*get_supported_devices)(const struct audio_hw_device *dev);
    644 
    645     /**
    646      * check to see if the audio hardware interface has been initialized.
    647      * returns 0 on success, -ENODEV on failure.
    648      */
    649     int (*init_check)(const struct audio_hw_device *dev);
    650 
    651     /** set the audio volume of a voice call. Range is between 0.0 and 1.0 */
    652     int (*set_voice_volume)(struct audio_hw_device *dev, float volume);
    653 
    654     /**
    655      * set the audio volume for all audio activities other than voice call.
    656      * Range between 0.0 and 1.0. If any value other than 0 is returned,
    657      * the software mixer will emulate this capability.
    658      */
    659     int (*set_master_volume)(struct audio_hw_device *dev, float volume);
    660 
    661     /**
    662      * Get the current master volume value for the HAL, if the HAL supports
    663      * master volume control.  AudioFlinger will query this value from the
    664      * primary audio HAL when the service starts and use the value for setting
    665      * the initial master volume across all HALs.  HALs which do not support
    666      * this method may leave it set to NULL.
    667      */
    668     int (*get_master_volume)(struct audio_hw_device *dev, float *volume);
    669 
    670     /**
    671      * set_mode is called when the audio mode changes. AUDIO_MODE_NORMAL mode
    672      * is for standard audio playback, AUDIO_MODE_RINGTONE when a ringtone is
    673      * playing, and AUDIO_MODE_IN_CALL when a call is in progress.
    674      */
    675     int (*set_mode)(struct audio_hw_device *dev, audio_mode_t mode);
    676 
    677     /* mic mute */
    678     int (*set_mic_mute)(struct audio_hw_device *dev, bool state);
    679     int (*get_mic_mute)(const struct audio_hw_device *dev, bool *state);
    680 
    681     /* set/get global audio parameters */
    682     int (*set_parameters)(struct audio_hw_device *dev, const char *kv_pairs);
    683 
    684     /*
    685      * Returns a pointer to a heap allocated string. The caller is responsible
    686      * for freeing the memory for it using free().
    687      */
    688     char * (*get_parameters)(const struct audio_hw_device *dev,
    689                              const char *keys);
    690 
    691     /* Returns audio input buffer size according to parameters passed or
    692      * 0 if one of the parameters is not supported.
    693      * See also get_buffer_size which is for a particular stream.
    694      */
    695     size_t (*get_input_buffer_size)(const struct audio_hw_device *dev,
    696                                     const struct audio_config *config);
    697 
    698     /** This method creates and opens the audio hardware output stream.
    699      * The "address" parameter qualifies the "devices" audio device type if needed.
    700      * The format format depends on the device type:
    701      * - Bluetooth devices use the MAC address of the device in the form "00:11:22:AA:BB:CC"
    702      * - USB devices use the ALSA card and device numbers in the form  "card=X;device=Y"
    703      * - Other devices may use a number or any other string.
    704      */
    705 
    706     int (*open_output_stream)(struct audio_hw_device *dev,
    707                               audio_io_handle_t handle,
    708                               audio_devices_t devices,
    709                               audio_output_flags_t flags,
    710                               struct audio_config *config,
    711                               struct audio_stream_out **stream_out,
    712                               const char *address);
    713 
    714     void (*close_output_stream)(struct audio_hw_device *dev,
    715                                 struct audio_stream_out* stream_out);
    716 
    717     /** This method creates and opens the audio hardware input stream */
    718     int (*open_input_stream)(struct audio_hw_device *dev,
    719                              audio_io_handle_t handle,
    720                              audio_devices_t devices,
    721                              struct audio_config *config,
    722                              struct audio_stream_in **stream_in,
    723                              audio_input_flags_t flags,
    724                              const char *address,
    725                              audio_source_t source);
    726 
    727     void (*close_input_stream)(struct audio_hw_device *dev,
    728                                struct audio_stream_in *stream_in);
    729 
    730     /**
    731      * Called by the framework to read available microphones characteristics.
    732      *
    733      * \param[in] dev the hw_device object.
    734      * \param[out] mic_array Pointer to first element on array with microphone info
    735      * \param[out] mic_count When called, this holds the value of the max number of elements
    736      *                       allowed in the mic_array. The actual number of elements written
    737      *                       is returned here.
    738      *                       if mic_count is passed as zero, mic_array will not be populated,
    739      *                       and mic_count will return the actual number of microphones in the
    740      *                       system.
    741      *
    742      * \return 0 if the microphone array is successfully filled.
    743      *         -ENOSYS if there is an error filling the data
    744      */
    745     int (*get_microphones)(const struct audio_hw_device *dev,
    746                            struct audio_microphone_characteristic_t *mic_array,
    747                            size_t *mic_count);
    748 
    749     /** This method dumps the state of the audio hardware */
    750     int (*dump)(const struct audio_hw_device *dev, int fd);
    751 
    752     /**
    753      * set the audio mute status for all audio activities.  If any value other
    754      * than 0 is returned, the software mixer will emulate this capability.
    755      */
    756     int (*set_master_mute)(struct audio_hw_device *dev, bool mute);
    757 
    758     /**
    759      * Get the current master mute status for the HAL, if the HAL supports
    760      * master mute control.  AudioFlinger will query this value from the primary
    761      * audio HAL when the service starts and use the value for setting the
    762      * initial master mute across all HALs.  HALs which do not support this
    763      * method may leave it set to NULL.
    764      */
    765     int (*get_master_mute)(struct audio_hw_device *dev, bool *mute);
    766 
    767     /**
    768      * Routing control
    769      */
    770 
    771     /* Creates an audio patch between several source and sink ports.
    772      * The handle is allocated by the HAL and should be unique for this
    773      * audio HAL module. */
    774     int (*create_audio_patch)(struct audio_hw_device *dev,
    775                                unsigned int num_sources,
    776                                const struct audio_port_config *sources,
    777                                unsigned int num_sinks,
    778                                const struct audio_port_config *sinks,
    779                                audio_patch_handle_t *handle);
    780 
    781     /* Release an audio patch */
    782     int (*release_audio_patch)(struct audio_hw_device *dev,
    783                                audio_patch_handle_t handle);
    784 
    785     /* Fills the list of supported attributes for a given audio port.
    786      * As input, "port" contains the information (type, role, address etc...)
    787      * needed by the HAL to identify the port.
    788      * As output, "port" contains possible attributes (sampling rates, formats,
    789      * channel masks, gain controllers...) for this port.
    790      */
    791     int (*get_audio_port)(struct audio_hw_device *dev,
    792                           struct audio_port *port);
    793 
    794     /* Set audio port configuration */
    795     int (*set_audio_port_config)(struct audio_hw_device *dev,
    796                          const struct audio_port_config *config);
    797 
    798 };
    799 typedef struct audio_hw_device audio_hw_device_t;
    800 
    801 /** convenience API for opening and closing a supported device */
    802 
    803 static inline int audio_hw_device_open(const struct hw_module_t* module,
    804                                        struct audio_hw_device** device)
    805 {
    806     return module->methods->open(module, AUDIO_HARDWARE_INTERFACE,
    807                                  TO_HW_DEVICE_T_OPEN(device));
    808 }
    809 
    810 static inline int audio_hw_device_close(struct audio_hw_device* device)
    811 {
    812     return device->common.close(&device->common);
    813 }
    814 
    815 
    816 __END_DECLS
    817 
    818 #endif  // ANDROID_AUDIO_INTERFACE_H
    819