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