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