Home | History | Annotate | Download | only in hardware_legacy
      1 /*
      2  * Copyright (C) 2007 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 #ifndef ANDROID_AUDIO_HARDWARE_INTERFACE_H
     18 #define ANDROID_AUDIO_HARDWARE_INTERFACE_H
     19 
     20 #include <stdint.h>
     21 #include <sys/types.h>
     22 
     23 #include <utils/Errors.h>
     24 #include <utils/Vector.h>
     25 #include <utils/String16.h>
     26 #include <utils/String8.h>
     27 
     28 #include <hardware_legacy/AudioSystemLegacy.h>
     29 
     30 #include <system/audio.h>
     31 #include <hardware/audio.h>
     32 
     33 #include <cutils/bitops.h>
     34 
     35 namespace android_audio_legacy {
     36     using android::Vector;
     37     using android::String16;
     38     using android::String8;
     39 
     40 // ----------------------------------------------------------------------------
     41 
     42 /**
     43  * AudioStreamOut is the abstraction interface for the audio output hardware.
     44  *
     45  * It provides information about various properties of the audio output hardware driver.
     46  */
     47 class AudioStreamOut {
     48 public:
     49     virtual             ~AudioStreamOut() = 0;
     50 
     51     /** return audio sampling rate in hz - eg. 44100 */
     52     virtual uint32_t    sampleRate() const = 0;
     53 
     54     /** returns size of output buffer - eg. 4800 */
     55     virtual size_t      bufferSize() const = 0;
     56 
     57     /**
     58      * returns the output channel mask
     59      */
     60     virtual uint32_t    channels() const = 0;
     61 
     62     /**
     63      * return audio format in 8bit or 16bit PCM format -
     64      * eg. AudioSystem:PCM_16_BIT
     65      */
     66     virtual int         format() const = 0;
     67 
     68     /**
     69      * return the frame size (number of bytes per sample).
     70      */
     71     uint32_t    frameSize() const { return audio_channel_count_from_out_mask(channels())*
     72                             ((format()==AUDIO_FORMAT_PCM_16_BIT)?sizeof(int16_t):sizeof(int8_t)); }
     73 
     74     /**
     75      * return the audio hardware driver latency in milli seconds.
     76      */
     77     virtual uint32_t    latency() const = 0;
     78 
     79     /**
     80      * Use this method in situations where audio mixing is done in the
     81      * hardware. This method serves as a direct interface with hardware,
     82      * allowing you to directly set the volume as apposed to via the framework.
     83      * This method might produce multiple PCM outputs or hardware accelerated
     84      * codecs, such as MP3 or AAC.
     85      */
     86     virtual status_t    setVolume(float left, float right) = 0;
     87 
     88     /** write audio buffer to driver. Returns number of bytes written */
     89     virtual ssize_t     write(const void* buffer, size_t bytes) = 0;
     90 
     91     /**
     92      * Put the audio hardware output into standby mode. Returns
     93      * status based on include/utils/Errors.h
     94      */
     95     virtual status_t    standby() = 0;
     96 
     97     /** dump the state of the audio output device */
     98     virtual status_t dump(int fd, const Vector<String16>& args) = 0;
     99 
    100     // set/get audio output parameters. The function accepts a list of parameters
    101     // key value pairs in the form: key1=value1;key2=value2;...
    102     // Some keys are reserved for standard parameters (See AudioParameter class).
    103     // If the implementation does not accept a parameter change while the output is
    104     // active but the parameter is acceptable otherwise, it must return INVALID_OPERATION.
    105     // The audio flinger will put the output in standby and then change the parameter value.
    106     virtual status_t    setParameters(const String8& keyValuePairs) = 0;
    107     virtual String8     getParameters(const String8& keys) = 0;
    108 
    109     // return the number of audio frames written by the audio dsp to DAC since
    110     // the output has exited standby
    111     virtual status_t    getRenderPosition(uint32_t *dspFrames) = 0;
    112 
    113     /**
    114      * get the local time at which the next write to the audio driver will be
    115      * presented
    116      */
    117     virtual status_t    getNextWriteTimestamp(int64_t *timestamp);
    118 
    119     /**
    120      * Return a recent count of the number of audio frames presented to an external observer.
    121      */
    122     virtual status_t    getPresentationPosition(uint64_t *frames, struct timespec *timestamp);
    123 
    124 };
    125 
    126 /**
    127  * AudioStreamIn is the abstraction interface for the audio input hardware.
    128  *
    129  * It defines the various properties of the audio hardware input driver.
    130  */
    131 class AudioStreamIn {
    132 public:
    133     virtual             ~AudioStreamIn() = 0;
    134 
    135     /** return audio sampling rate in hz - eg. 44100 */
    136     virtual uint32_t    sampleRate() const = 0;
    137 
    138     /** return the input buffer size allowed by audio driver */
    139     virtual size_t      bufferSize() const = 0;
    140 
    141     /** return input channel mask */
    142     virtual uint32_t    channels() const = 0;
    143 
    144     /**
    145      * return audio format in 8bit or 16bit PCM format -
    146      * eg. AudioSystem:PCM_16_BIT
    147      */
    148     virtual int         format() const = 0;
    149 
    150     /**
    151      * return the frame size (number of bytes per sample).
    152      */
    153     uint32_t    frameSize() const { return audio_channel_count_from_in_mask(channels())*
    154                             ((format()==AudioSystem::PCM_16_BIT)?sizeof(int16_t):sizeof(int8_t)); }
    155 
    156     /** set the input gain for the audio driver. This method is for
    157      *  for future use */
    158     virtual status_t    setGain(float gain) = 0;
    159 
    160     /** read audio buffer in from audio driver */
    161     virtual ssize_t     read(void* buffer, ssize_t bytes) = 0;
    162 
    163     /** dump the state of the audio input device */
    164     virtual status_t dump(int fd, const Vector<String16>& args) = 0;
    165 
    166     /**
    167      * Put the audio hardware input into standby mode. Returns
    168      * status based on include/utils/Errors.h
    169      */
    170     virtual status_t    standby() = 0;
    171 
    172     // set/get audio input parameters. The function accepts a list of parameters
    173     // key value pairs in the form: key1=value1;key2=value2;...
    174     // Some keys are reserved for standard parameters (See AudioParameter class).
    175     // If the implementation does not accept a parameter change while the output is
    176     // active but the parameter is acceptable otherwise, it must return INVALID_OPERATION.
    177     // The audio flinger will put the input in standby and then change the parameter value.
    178     virtual status_t    setParameters(const String8& keyValuePairs) = 0;
    179     virtual String8     getParameters(const String8& keys) = 0;
    180 
    181 
    182     // Return the number of input frames lost in the audio driver since the last call of this function.
    183     // Audio driver is expected to reset the value to 0 and restart counting upon returning the current value by this function call.
    184     // Such loss typically occurs when the user space process is blocked longer than the capacity of audio driver buffers.
    185     // Unit: the number of input audio frames
    186     virtual unsigned int  getInputFramesLost() const = 0;
    187 
    188     virtual status_t addAudioEffect(effect_handle_t effect) = 0;
    189     virtual status_t removeAudioEffect(effect_handle_t effect) = 0;
    190 };
    191 
    192 /**
    193  * AudioHardwareInterface.h defines the interface to the audio hardware abstraction layer.
    194  *
    195  * The interface supports setting and getting parameters, selecting audio routing
    196  * paths, and defining input and output streams.
    197  *
    198  * AudioFlinger initializes the audio hardware and immediately opens an output stream.
    199  * You can set Audio routing to output to handset, speaker, Bluetooth, or a headset.
    200  *
    201  * The audio input stream is initialized when AudioFlinger is called to carry out
    202  * a record operation.
    203  */
    204 class AudioHardwareInterface
    205 {
    206 public:
    207     virtual ~AudioHardwareInterface() {}
    208 
    209     /**
    210      * check to see if the audio hardware interface has been initialized.
    211      * return status based on values defined in include/utils/Errors.h
    212      */
    213     virtual status_t    initCheck() = 0;
    214 
    215     /** set the audio volume of a voice call. Range is between 0.0 and 1.0 */
    216     virtual status_t    setVoiceVolume(float volume) = 0;
    217 
    218     /**
    219      * set the audio volume for all audio activities other than voice call.
    220      * Range between 0.0 and 1.0. If any value other than NO_ERROR is returned,
    221      * the software mixer will emulate this capability.
    222      */
    223     virtual status_t    setMasterVolume(float volume) = 0;
    224 
    225     /**
    226      * Get the current master volume value for the HAL, if the HAL supports
    227      * master volume control.  AudioFlinger will query this value from the
    228      * primary audio HAL when the service starts and use the value for setting
    229      * the initial master volume across all HALs.
    230      */
    231     virtual status_t    getMasterVolume(float *volume) = 0;
    232 
    233     /**
    234      * setMode is called when the audio mode changes. NORMAL mode is for
    235      * standard audio playback, RINGTONE when a ringtone is playing, and IN_CALL
    236      * when a call is in progress.
    237      */
    238     virtual status_t    setMode(int mode) = 0;
    239 
    240     // mic mute
    241     virtual status_t    setMicMute(bool state) = 0;
    242     virtual status_t    getMicMute(bool* state) = 0;
    243 
    244     // set/get global audio parameters
    245     virtual status_t    setParameters(const String8& keyValuePairs) = 0;
    246     virtual String8     getParameters(const String8& keys) = 0;
    247 
    248     // Returns audio input buffer size according to parameters passed or 0 if one of the
    249     // parameters is not supported
    250     virtual size_t    getInputBufferSize(uint32_t sampleRate, int format, int channelCount) = 0;
    251 
    252     /** This method creates and opens the audio hardware output stream */
    253     virtual AudioStreamOut* openOutputStream(
    254                                 uint32_t devices,
    255                                 int *format=0,
    256                                 uint32_t *channels=0,
    257                                 uint32_t *sampleRate=0,
    258                                 status_t *status=0) = 0;
    259     virtual AudioStreamOut* openOutputStreamWithFlags(
    260                                 uint32_t devices,
    261                                 audio_output_flags_t flags=(audio_output_flags_t)0,
    262                                 int *format=0,
    263                                 uint32_t *channels=0,
    264                                 uint32_t *sampleRate=0,
    265                                 status_t *status=0) = 0;
    266     virtual    void        closeOutputStream(AudioStreamOut* out) = 0;
    267 
    268     /** This method creates and opens the audio hardware input stream */
    269     virtual AudioStreamIn* openInputStream(
    270                                 uint32_t devices,
    271                                 int *format,
    272                                 uint32_t *channels,
    273                                 uint32_t *sampleRate,
    274                                 status_t *status,
    275                                 AudioSystem::audio_in_acoustics acoustics) = 0;
    276     virtual    void        closeInputStream(AudioStreamIn* in) = 0;
    277 
    278     /**This method dumps the state of the audio hardware */
    279     virtual status_t dumpState(int fd, const Vector<String16>& args) = 0;
    280 
    281     virtual status_t setMasterMute(bool muted) = 0;
    282 
    283     static AudioHardwareInterface* create();
    284 
    285     virtual int createAudioPatch(unsigned int num_sources,
    286                                const struct audio_port_config *sources,
    287                                unsigned int num_sinks,
    288                                const struct audio_port_config *sinks,
    289                                audio_patch_handle_t *handle) = 0;
    290 
    291     virtual int releaseAudioPatch(audio_patch_handle_t handle) = 0;
    292 
    293     virtual int getAudioPort(struct audio_port *port) = 0;
    294 
    295     virtual int setAudioPortConfig(const struct audio_port_config *config) = 0;
    296 
    297 protected:
    298 
    299     virtual status_t dump(int fd, const Vector<String16>& args) = 0;
    300 };
    301 
    302 // ----------------------------------------------------------------------------
    303 
    304 extern "C" AudioHardwareInterface* createAudioHardware(void);
    305 
    306 }; // namespace android
    307 
    308 #endif // ANDROID_AUDIO_HARDWARE_INTERFACE_H
    309