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>
27 // ----------------------------------------------------------------------------
29 // The AudioPolicyInterface and AudioPolicyClientInterface classes define the communication interfaces
30 // between the platform specific audio policy manager and Android generic audio policy manager.
31 // The platform specific audio policy manager must implement methods of the AudioPolicyInterface class.
32 // This implementation makes use of the AudioPolicyClientInterface to control the activity and
33 // configuration of audio input and output streams.
35 // The platform specific audio policy manager is in charge of the audio routing and volume control
36 // policies for a given platform.
37 // The main roles of this module are:
38 // - keep track of current system state (removable device connections, phone state, user requests...).
39 // System state changes and user actions are notified to audio policy manager with methods of the AudioPolicyInterface.
40 // - process getOutput() queries received when AudioTrack objects are created: Those queries
41 // return a handler on an output that has been selected, configured and opened by the audio policy manager and that
42 // must be used by the AudioTrack when registering to the AudioFlinger with the createTrack() method.
43 // When the AudioTrack object is released, a putOutput() query is received and the audio policy manager can decide
44 // to close or reconfigure the output depending on other streams using this output and current system state.
45 // - similarly process getInput() and putInput() queries received from AudioRecord objects and configure audio inputs.
46 // - process volume control requests: the stream volume is converted from an index value (received from UI) to a float value
47 // applicable to each output as a function of platform specific settings and current output route (destination device). It
48 // also make sure that streams are not muted if not allowed (e.g. camera shutter sound in some countries).
50 // The platform specific audio policy manager is provided as a shared library by platform vendors (as for libaudio.so)
51 // and is linked with libaudioflinger.so
54 // Audio Policy Manager Interface
55 class AudioPolicyInterface
59 virtual ~AudioPolicyInterface() {}
61 // configuration functions
64 // indicate a change in device connection status
65 virtual status_t setDeviceConnectionState(AudioSystem::audio_devices device,
66 AudioSystem::device_connection_state state,
67 const char *device_address) = 0;
68 // retreive a device connection status
69 virtual AudioSystem::device_connection_state getDeviceConnectionState(AudioSystem::audio_devices device,
70 const char *device_address) = 0;
71 // indicate a change in phone state. Valid phones states are defined by AudioSystem::audio_mode
72 virtual void setPhoneState(int state) = 0;
73 // indicate a change in ringer mode
74 virtual void setRingerMode(uint32_t mode, uint32_t mask) = 0;
75 // force using a specific device category for the specified usage
76 virtual void setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config) = 0;
77 // retreive current device category forced for a given usage
78 virtual AudioSystem::forced_config getForceUse(AudioSystem::force_use usage) = 0;
79 // set a system property (e.g. camera sound always audible)
80 virtual void setSystemProperty(const char* property, const char* value) = 0;
84 // Audio routing query functions
87 // request an output appriate for playback of the supplied stream type and parameters
88 virtual audio_io_handle_t getOutput(AudioSystem::stream_type stream,
89 uint32_t samplingRate = 0,
90 uint32_t format = AudioSystem::FORMAT_DEFAULT,
91 uint32_t channels = 0,
92 AudioSystem::output_flags flags = AudioSystem::OUTPUT_FLAG_INDIRECT) = 0;
93 // indicates to the audio policy manager that the output starts being used by corresponding stream.
94 virtual status_t startOutput(audio_io_handle_t output, AudioSystem::stream_type stream) = 0;
95 // indicates to the audio policy manager that the output stops being used by corresponding stream.
96 virtual status_t stopOutput(audio_io_handle_t output, AudioSystem::stream_type stream) = 0;
97 // releases the output.
98 virtual void releaseOutput(audio_io_handle_t output) = 0;
100 // request an input appriate for record from the supplied device with supplied parameters.
101 virtual audio_io_handle_t getInput(int inputSource,
102 uint32_t samplingRate = 0,
103 uint32_t Format = AudioSystem::FORMAT_DEFAULT,
104 uint32_t channels = 0,
105 AudioSystem::audio_in_acoustics acoustics = (AudioSystem::audio_in_acoustics)0) = 0;
106 // indicates to the audio policy manager that the input starts being used.
107 virtual status_t startInput(audio_io_handle_t input) = 0;
108 // indicates to the audio policy manager that the input stops being used.
109 virtual status_t stopInput(audio_io_handle_t input) = 0;
110 // releases the input.
111 virtual void releaseInput(audio_io_handle_t input) = 0;
114 // volume control functions
117 // initialises stream volume conversion parameters by specifying volume index range.
118 virtual void initStreamVolume(AudioSystem::stream_type stream,
122 // sets the new stream volume at a level corresponding to the supplied index
123 virtual status_t setStreamVolumeIndex(AudioSystem::stream_type stream, int index) = 0;
124 // retreive current volume index for the specified stream
125 virtual status_t getStreamVolumeIndex(AudioSystem::stream_type stream, int *index) = 0;
129 // Audio Policy client Interface
130 class AudioPolicyClientInterface
133 virtual ~AudioPolicyClientInterface() {}
136 // Audio output Control functions
139 // opens an audio output with the requested parameters. The parameter values can indicate to use the default values
140 // in case the audio policy manager has no specific requirements for the output being opened.
141 // When the function returns, the parameter values reflect the actual values used by the audio hardware output stream.
142 // The audio policy manager can check if the proposed parameters are suitable or not and act accordingly.
143 virtual audio_io_handle_t openOutput(uint32_t *pDevices,
144 uint32_t *pSamplingRate,
147 uint32_t *pLatencyMs,
148 AudioSystem::output_flags flags) = 0;
149 // creates a special output that is duplicated to the two outputs passed as arguments. The duplication is performed by
150 // a special mixer thread in the AudioFlinger.
151 virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1, audio_io_handle_t output2) = 0;
152 // closes the output stream
153 virtual status_t closeOutput(audio_io_handle_t output) = 0;
154 // suspends the output. When an output is suspended, the corresponding audio hardware output stream is placed in
155 // standby and the AudioTracks attached to the mixer thread are still processed but the output mix is discarded.
156 virtual status_t suspendOutput(audio_io_handle_t output) = 0;
157 // restores a suspended output.
158 virtual status_t restoreOutput(audio_io_handle_t output) = 0;
161 // Audio input Control functions
164 // opens an audio input
165 virtual audio_io_handle_t openInput(uint32_t *pDevices,
166 uint32_t *pSamplingRate,
169 uint32_t acoustics) = 0;
170 // closes an audio input
171 virtual status_t closeInput(audio_io_handle_t input) = 0;
173 // misc control functions
176 // set a stream volume for a particular output. For the same user setting, a given stream type can have different volumes
177 // for each output (destination device) it is attached to.
178 virtual status_t setStreamVolume(AudioSystem::stream_type stream, float volume, audio_io_handle_t output, int delayMs = 0) = 0;
180 // reroute a given stream type to the specified output
181 virtual status_t setStreamOutput(AudioSystem::stream_type stream, audio_io_handle_t output) = 0;
183 // function enabling to send proprietary informations directly from audio policy manager to audio hardware interface.
184 virtual void setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs, int delayMs = 0) = 0;
185 // function enabling to receive proprietary informations directly from audio hardware interface to audio policy manager.
186 virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys) = 0;
188 // request the playback of a tone on the specified stream: used for instance to replace notification sounds when playing
189 // over a telephony device during a phone call.
190 virtual status_t startTone(ToneGenerator::tone_type tone, AudioSystem::stream_type stream) = 0;
191 virtual status_t stopTone() = 0;
194 extern "C" AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface);
195 extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface);
198 }; // namespace android
200 #endif // ANDROID_AUDIOPOLICYINTERFACE_H