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 
     26 #include <cutils/bitops.h>
     27 
     28 #include <hardware/hardware.h>
     29 #include <system/audio.h>
     30 #include <hardware/audio_effect.h>
     31 
     32 __BEGIN_DECLS
     33 
     34 /**
     35  * The id of this module
     36  */
     37 #define AUDIO_HARDWARE_MODULE_ID "audio"
     38 
     39 /**
     40  * Name of the audio devices to open
     41  */
     42 #define AUDIO_HARDWARE_INTERFACE "audio_hw_if"
     43 
     44 /**************************************/
     45 
     46 /**
     47  *  standard audio parameters that the HAL may need to handle
     48  */
     49 
     50 /**
     51  *  audio device parameters
     52  */
     53 
     54 /* BT SCO Noise Reduction + Echo Cancellation parameters */
     55 #define AUDIO_PARAMETER_KEY_BT_NREC "bt_headset_nrec"
     56 #define AUDIO_PARAMETER_VALUE_ON "on"
     57 #define AUDIO_PARAMETER_VALUE_OFF "off"
     58 
     59 /* TTY mode selection */
     60 #define AUDIO_PARAMETER_KEY_TTY_MODE "tty_mode"
     61 #define AUDIO_PARAMETER_VALUE_TTY_OFF "tty_off"
     62 #define AUDIO_PARAMETER_VALUE_TTY_VCO "tty_vco"
     63 #define AUDIO_PARAMETER_VALUE_TTY_HCO "tty_hco"
     64 #define AUDIO_PARAMETER_VALUE_TTY_FULL "tty_full"
     65 
     66 /**
     67  *  audio stream parameters
     68  */
     69 
     70 #define AUDIO_PARAMETER_STREAM_ROUTING "routing"
     71 #define AUDIO_PARAMETER_STREAM_FORMAT "format"
     72 #define AUDIO_PARAMETER_STREAM_CHANNELS "channels"
     73 #define AUDIO_PARAMETER_STREAM_FRAME_COUNT "frame_count"
     74 #define AUDIO_PARAMETER_STREAM_INPUT_SOURCE "input_source"
     75 
     76 /**************************************/
     77 
     78 /* common audio stream parameters and operations */
     79 struct audio_stream {
     80 
     81     /**
     82      * sampling rate is in Hz - eg. 44100
     83      */
     84     uint32_t (*get_sample_rate)(const struct audio_stream *stream);
     85 
     86     /* currently unused - use set_parameters with key
     87      *    AUDIO_PARAMETER_STREAM_SAMPLING_RATE
     88      */
     89     int (*set_sample_rate)(struct audio_stream *stream, uint32_t rate);
     90 
     91     /**
     92      * size of output buffer in bytes - eg. 4800
     93      */
     94     size_t (*get_buffer_size)(const struct audio_stream *stream);
     95 
     96     /**
     97      * the channel mask -
     98      *  e.g. AUDIO_CHANNEL_OUT_STEREO or AUDIO_CHANNEL_IN_STEREO
     99      */
    100     uint32_t (*get_channels)(const struct audio_stream *stream);
    101 
    102     /**
    103      * audio format - eg. AUDIO_FORMAT_PCM_16_BIT
    104      */
    105     int (*get_format)(const struct audio_stream *stream);
    106 
    107     /* currently unused - use set_parameters with key
    108      *     AUDIO_PARAMETER_STREAM_FORMAT
    109      */
    110     int (*set_format)(struct audio_stream *stream, int format);
    111 
    112     /**
    113      * Put the audio hardware input/output into standby mode.
    114      * Returns 0 on success and <0 on failure.
    115      */
    116     int (*standby)(struct audio_stream *stream);
    117 
    118     /** dump the state of the audio input/output device */
    119     int (*dump)(const struct audio_stream *stream, int fd);
    120 
    121     audio_devices_t (*get_device)(const struct audio_stream *stream);
    122     int (*set_device)(struct audio_stream *stream, audio_devices_t device);
    123 
    124     /**
    125      * set/get audio stream parameters. The function accepts a list of
    126      * parameter key value pairs in the form: key1=value1;key2=value2;...
    127      *
    128      * Some keys are reserved for standard parameters (See AudioParameter class)
    129      *
    130      * If the implementation does not accept a parameter change while
    131      * the output is active but the parameter is acceptable otherwise, it must
    132      * return -ENOSYS.
    133      *
    134      * The audio flinger will put the stream in standby and then change the
    135      * parameter value.
    136      */
    137     int (*set_parameters)(struct audio_stream *stream, const char *kv_pairs);
    138 
    139     /*
    140      * Returns a pointer to a heap allocated string. The caller is responsible
    141      * for freeing the memory for it.
    142      */
    143     char * (*get_parameters)(const struct audio_stream *stream,
    144                              const char *keys);
    145     int (*add_audio_effect)(const struct audio_stream *stream,
    146                              effect_handle_t effect);
    147     int (*remove_audio_effect)(const struct audio_stream *stream,
    148                              effect_handle_t effect);
    149 };
    150 typedef struct audio_stream audio_stream_t;
    151 
    152 /**
    153  * audio_stream_out is the abstraction interface for the audio output hardware.
    154  *
    155  * It provides information about various properties of the audio output
    156  * hardware driver.
    157  */
    158 
    159 struct audio_stream_out {
    160     struct audio_stream common;
    161 
    162     /**
    163      * return the audio hardware driver latency in milli seconds.
    164      */
    165     uint32_t (*get_latency)(const struct audio_stream_out *stream);
    166 
    167     /**
    168      * Use this method in situations where audio mixing is done in the
    169      * hardware. This method serves as a direct interface with hardware,
    170      * allowing you to directly set the volume as apposed to via the framework.
    171      * This method might produce multiple PCM outputs or hardware accelerated
    172      * codecs, such as MP3 or AAC.
    173      */
    174     int (*set_volume)(struct audio_stream_out *stream, float left, float right);
    175 
    176     /**
    177      * write audio buffer to driver. Returns number of bytes written
    178      */
    179     ssize_t (*write)(struct audio_stream_out *stream, const void* buffer,
    180                      size_t bytes);
    181 
    182     /* return the number of audio frames written by the audio dsp to DAC since
    183      * the output has exited standby
    184      */
    185     int (*get_render_position)(const struct audio_stream_out *stream,
    186                                uint32_t *dsp_frames);
    187 };
    188 typedef struct audio_stream_out audio_stream_out_t;
    189 
    190 struct audio_stream_in {
    191     struct audio_stream common;
    192 
    193     /** set the input gain for the audio driver. This method is for
    194      *  for future use */
    195     int (*set_gain)(struct audio_stream_in *stream, float gain);
    196 
    197     /** read audio buffer in from audio driver */
    198     ssize_t (*read)(struct audio_stream_in *stream, void* buffer,
    199                     size_t bytes);
    200 
    201     /**
    202      * Return the amount of input frames lost in the audio driver since the
    203      * last call of this function.
    204      * Audio driver is expected to reset the value to 0 and restart counting
    205      * upon returning the current value by this function call.
    206      * Such loss typically occurs when the user space process is blocked
    207      * longer than the capacity of audio driver buffers.
    208      *
    209      * Unit: the number of input audio frames
    210      */
    211     uint32_t (*get_input_frames_lost)(struct audio_stream_in *stream);
    212 };
    213 typedef struct audio_stream_in audio_stream_in_t;
    214 
    215 /**
    216  * return the frame size (number of bytes per sample).
    217  */
    218 static inline uint32_t audio_stream_frame_size(struct audio_stream *s)
    219 {
    220     int chan_samp_sz;
    221 
    222     switch (s->get_format(s)) {
    223     case AUDIO_FORMAT_PCM_16_BIT:
    224         chan_samp_sz = sizeof(int16_t);
    225         break;
    226     case AUDIO_FORMAT_PCM_8_BIT:
    227     default:
    228         chan_samp_sz = sizeof(int8_t);
    229         break;
    230     }
    231 
    232     return popcount(s->get_channels(s)) * chan_samp_sz;
    233 }
    234 
    235 
    236 /**********************************************************************/
    237 
    238 /**
    239  * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
    240  * and the fields of this data structure must begin with hw_module_t
    241  * followed by module specific information.
    242  */
    243 struct audio_module {
    244     struct hw_module_t common;
    245 };
    246 
    247 struct audio_hw_device {
    248     struct hw_device_t common;
    249 
    250     /**
    251      * used by audio flinger to enumerate what devices are supported by
    252      * each audio_hw_device implementation.
    253      *
    254      * Return value is a bitmask of 1 or more values of audio_devices_t
    255      */
    256     uint32_t (*get_supported_devices)(const struct audio_hw_device *dev);
    257 
    258     /**
    259      * check to see if the audio hardware interface has been initialized.
    260      * returns 0 on success, -ENODEV on failure.
    261      */
    262     int (*init_check)(const struct audio_hw_device *dev);
    263 
    264     /** set the audio volume of a voice call. Range is between 0.0 and 1.0 */
    265     int (*set_voice_volume)(struct audio_hw_device *dev, float volume);
    266 
    267     /**
    268      * set the audio volume for all audio activities other than voice call.
    269      * Range between 0.0 and 1.0. If any value other than 0 is returned,
    270      * the software mixer will emulate this capability.
    271      */
    272     int (*set_master_volume)(struct audio_hw_device *dev, float volume);
    273 
    274     /**
    275      * setMode is called when the audio mode changes. AUDIO_MODE_NORMAL mode
    276      * is for standard audio playback, AUDIO_MODE_RINGTONE when a ringtone is
    277      * playing, and AUDIO_MODE_IN_CALL when a call is in progress.
    278      */
    279     int (*set_mode)(struct audio_hw_device *dev, int mode);
    280 
    281     /* mic mute */
    282     int (*set_mic_mute)(struct audio_hw_device *dev, bool state);
    283     int (*get_mic_mute)(const struct audio_hw_device *dev, bool *state);
    284 
    285     /* set/get global audio parameters */
    286     int (*set_parameters)(struct audio_hw_device *dev, const char *kv_pairs);
    287 
    288     /*
    289      * Returns a pointer to a heap allocated string. The caller is responsible
    290      * for freeing the memory for it.
    291      */
    292     char * (*get_parameters)(const struct audio_hw_device *dev,
    293                              const char *keys);
    294 
    295     /* Returns audio input buffer size according to parameters passed or
    296      * 0 if one of the parameters is not supported
    297      */
    298     size_t (*get_input_buffer_size)(const struct audio_hw_device *dev,
    299                                     uint32_t sample_rate, int format,
    300                                     int channel_count);
    301 
    302     /** This method creates and opens the audio hardware output stream */
    303     int (*open_output_stream)(struct audio_hw_device *dev, uint32_t devices,
    304                               int *format, uint32_t *channels,
    305                               uint32_t *sample_rate,
    306                               struct audio_stream_out **out);
    307 
    308     void (*close_output_stream)(struct audio_hw_device *dev,
    309                                 struct audio_stream_out* out);
    310 
    311     /** This method creates and opens the audio hardware input stream */
    312     int (*open_input_stream)(struct audio_hw_device *dev, uint32_t devices,
    313                              int *format, uint32_t *channels,
    314                              uint32_t *sample_rate,
    315                              audio_in_acoustics_t acoustics,
    316                              struct audio_stream_in **stream_in);
    317 
    318     void (*close_input_stream)(struct audio_hw_device *dev,
    319                                struct audio_stream_in *in);
    320 
    321     /** This method dumps the state of the audio hardware */
    322     int (*dump)(const struct audio_hw_device *dev, int fd);
    323 };
    324 typedef struct audio_hw_device audio_hw_device_t;
    325 
    326 /** convenience API for opening and closing a supported device */
    327 
    328 static inline int audio_hw_device_open(const struct hw_module_t* module,
    329                                        struct audio_hw_device** device)
    330 {
    331     return module->methods->open(module, AUDIO_HARDWARE_INTERFACE,
    332                                  (struct hw_device_t**)device);
    333 }
    334 
    335 static inline int audio_hw_device_close(struct audio_hw_device* device)
    336 {
    337     return device->common.close(&device->common);
    338 }
    339 
    340 
    341 __END_DECLS
    342 
    343 #endif  // ANDROID_AUDIO_INTERFACE_H
    344