2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ANDROID_AUDIOPOLICYINTERFACE_H
18 #define ANDROID_AUDIOPOLICYINTERFACE_H
20 #include <media/AudioSystem.h>
21 #include <media/ToneGenerator.h>
22 #include <utils/String8.h>
24 #include <hardware_legacy/AudioSystemLegacy.h>
26 namespace android_audio_legacy {
27 using android::Vector;
28 using android::String8;
29 using android::ToneGenerator;
31 // ----------------------------------------------------------------------------
33 // The AudioPolicyInterface and AudioPolicyClientInterface classes define the communication interfaces
34 // between the platform specific audio policy manager and Android generic audio policy manager.
35 // The platform specific audio policy manager must implement methods of the AudioPolicyInterface class.
36 // This implementation makes use of the AudioPolicyClientInterface to control the activity and
37 // configuration of audio input and output streams.
39 // The platform specific audio policy manager is in charge of the audio routing and volume control
40 // policies for a given platform.
41 // The main roles of this module are:
42 // - keep track of current system state (removable device connections, phone state, user requests...).
43 // System state changes and user actions are notified to audio policy manager with methods of the AudioPolicyInterface.
44 // - process getOutput() queries received when AudioTrack objects are created: Those queries
45 // return a handler on an output that has been selected, configured and opened by the audio policy manager and that
46 // must be used by the AudioTrack when registering to the AudioFlinger with the createTrack() method.
47 // When the AudioTrack object is released, a putOutput() query is received and the audio policy manager can decide
48 // to close or reconfigure the output depending on other streams using this output and current system state.
49 // - similarly process getInput() and putInput() queries received from AudioRecord objects and configure audio inputs.
50 // - process volume control requests: the stream volume is converted from an index value (received from UI) to a float value
51 // applicable to each output as a function of platform specific settings and current output route (destination device). It
52 // also make sure that streams are not muted if not allowed (e.g. camera shutter sound in some countries).
54 // The platform specific audio policy manager is provided as a shared library by platform vendors (as for libaudio.so)
55 // and is linked with libaudioflinger.so
58 // Audio Policy Manager Interface
59 class AudioPolicyInterface
63 virtual ~AudioPolicyInterface() {}
65 // configuration functions
68 // indicate a change in device connection status
69 virtual status_t setDeviceConnectionState(AudioSystem::audio_devices device,
70 AudioSystem::device_connection_state state,
71 const char *device_address) = 0;
72 // retrieve a device connection status
73 virtual AudioSystem::device_connection_state getDeviceConnectionState(AudioSystem::audio_devices device,
74 const char *device_address) = 0;
75 // indicate a change in phone state. Valid phones states are defined by AudioSystem::audio_mode
76 virtual void setPhoneState(int state) = 0;
77 // force using a specific device category for the specified usage
78 virtual void setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config) = 0;
79 // retrieve current device category forced for a given usage
80 virtual AudioSystem::forced_config getForceUse(AudioSystem::force_use usage) = 0;
81 // set a system property (e.g. camera sound always audible)
82 virtual void setSystemProperty(const char* property, const char* value) = 0;
83 // check proper initialization
84 virtual status_t initCheck() = 0;
87 // Audio routing query functions
90 // request an output appropriate for playback of the supplied stream type and parameters
91 virtual audio_io_handle_t getOutput(AudioSystem::stream_type stream,
92 uint32_t samplingRate = 0,
93 uint32_t format = AudioSystem::FORMAT_DEFAULT,
94 uint32_t channels = 0,
95 AudioSystem::output_flags flags = AudioSystem::OUTPUT_FLAG_INDIRECT) = 0;
96 // indicates to the audio policy manager that the output starts being used by corresponding stream.
97 virtual status_t startOutput(audio_io_handle_t output,
98 AudioSystem::stream_type stream,
100 // indicates to the audio policy manager that the output stops being used by corresponding stream.
101 virtual status_t stopOutput(audio_io_handle_t output,
102 AudioSystem::stream_type stream,
103 int session = 0) = 0;
104 // releases the output.
105 virtual void releaseOutput(audio_io_handle_t output) = 0;
107 // request an input appropriate for record from the supplied device with supplied parameters.
108 virtual audio_io_handle_t getInput(int inputSource,
109 uint32_t samplingRate = 0,
110 uint32_t Format = AudioSystem::FORMAT_DEFAULT,
111 uint32_t channels = 0,
112 AudioSystem::audio_in_acoustics acoustics = (AudioSystem::audio_in_acoustics)0) = 0;
113 // indicates to the audio policy manager that the input starts being used.
114 virtual status_t startInput(audio_io_handle_t input) = 0;
115 // indicates to the audio policy manager that the input stops being used.
116 virtual status_t stopInput(audio_io_handle_t input) = 0;
117 // releases the input.
118 virtual void releaseInput(audio_io_handle_t input) = 0;
121 // volume control functions
124 // initialises stream volume conversion parameters by specifying volume index range.
125 virtual void initStreamVolume(AudioSystem::stream_type stream,
129 // sets the new stream volume at a level corresponding to the supplied index for the
130 // supplied device. By convention, specifying AUDIO_DEVICE_OUT_DEFAULT means
131 // setting volume for all devices
132 virtual status_t setStreamVolumeIndex(AudioSystem::stream_type stream,
134 audio_devices_t device) = 0;
136 // retrieve current volume index for the specified stream and the
137 // specified device. By convention, specifying AUDIO_DEVICE_OUT_DEFAULT means
138 // querying the volume of the active device.
139 virtual status_t getStreamVolumeIndex(AudioSystem::stream_type stream,
141 audio_devices_t device) = 0;
143 // return the strategy corresponding to a given stream type
144 virtual uint32_t getStrategyForStream(AudioSystem::stream_type stream) = 0;
146 // return the enabled output devices for the given stream type
147 virtual audio_devices_t getDevicesForStream(AudioSystem::stream_type stream) = 0;
149 // Audio effect management
150 virtual audio_io_handle_t getOutputForEffect(effect_descriptor_t *desc) = 0;
151 virtual status_t registerEffect(effect_descriptor_t *desc,
152 audio_io_handle_t io,
156 virtual status_t unregisterEffect(int id) = 0;
157 virtual status_t setEffectEnabled(int id, bool enabled) = 0;
159 virtual bool isStreamActive(int stream, uint32_t inPastMs = 0) const = 0;
162 virtual status_t dump(int fd) = 0;
166 // Audio Policy client Interface
167 class AudioPolicyClientInterface
170 virtual ~AudioPolicyClientInterface() {}
173 // Audio output Control functions
176 // opens an audio output with the requested parameters. The parameter values can indicate to use the default values
177 // in case the audio policy manager has no specific requirements for the output being opened.
178 // When the function returns, the parameter values reflect the actual values used by the audio hardware output stream.
179 // The audio policy manager can check if the proposed parameters are suitable or not and act accordingly.
180 virtual audio_io_handle_t openOutput(uint32_t *pDevices,
181 uint32_t *pSamplingRate,
184 uint32_t *pLatencyMs,
185 AudioSystem::output_flags flags) = 0;
186 // creates a special output that is duplicated to the two outputs passed as arguments. The duplication is performed by
187 // a special mixer thread in the AudioFlinger.
188 virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1, audio_io_handle_t output2) = 0;
189 // closes the output stream
190 virtual status_t closeOutput(audio_io_handle_t output) = 0;
191 // suspends the output. When an output is suspended, the corresponding audio hardware output stream is placed in
192 // standby and the AudioTracks attached to the mixer thread are still processed but the output mix is discarded.
193 virtual status_t suspendOutput(audio_io_handle_t output) = 0;
194 // restores a suspended output.
195 virtual status_t restoreOutput(audio_io_handle_t output) = 0;
198 // Audio input Control functions
201 // opens an audio input
202 virtual audio_io_handle_t openInput(uint32_t *pDevices,
203 uint32_t *pSamplingRate,
206 audio_in_acoustics_t acoustics) = 0;
207 // closes an audio input
208 virtual status_t closeInput(audio_io_handle_t input) = 0;
210 // misc control functions
213 // set a stream volume for a particular output. For the same user setting, a given stream type can have different volumes
214 // for each output (destination device) it is attached to.
215 virtual status_t setStreamVolume(AudioSystem::stream_type stream, float volume, audio_io_handle_t output, int delayMs = 0) = 0;
217 // FIXME ignores output, should be renamed to invalidateStreamOuput(stream)
218 // reroute a given stream type to the specified output
219 virtual status_t setStreamOutput(AudioSystem::stream_type stream, audio_io_handle_t output) = 0;
221 // function enabling to send proprietary informations directly from audio policy manager to audio hardware interface.
222 virtual void setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs, int delayMs = 0) = 0;
223 // function enabling to receive proprietary informations directly from audio hardware interface to audio policy manager.
224 virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys) = 0;
226 // request the playback of a tone on the specified stream: used for instance to replace notification sounds when playing
227 // over a telephony device during a phone call.
228 virtual status_t startTone(ToneGenerator::tone_type tone, AudioSystem::stream_type stream) = 0;
229 virtual status_t stopTone() = 0;
231 // set down link audio volume.
232 virtual status_t setVoiceVolume(float volume, int delayMs = 0) = 0;
234 // move effect to the specified output
235 virtual status_t moveEffects(int session,
236 audio_io_handle_t srcOutput,
237 audio_io_handle_t dstOutput) = 0;
241 extern "C" AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface);
242 extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface);
245 }; // namespace android
247 #endif // ANDROID_AUDIOPOLICYINTERFACE_H