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