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_AUDIOPOLICY_INTERFACE_H
18 #define ANDROID_AUDIOPOLICY_INTERFACE_H
19 
20 #include <media/AudioSystem.h>
21 #include <media/AudioPolicy.h>
22 #include <utils/String8.h>
23 
24 namespace android {
25 
26 // ----------------------------------------------------------------------------
27 
28 // The AudioPolicyInterface and AudioPolicyClientInterface classes define the communication interfaces
29 // between the platform specific audio policy manager and Android generic audio policy manager.
30 // The platform specific audio policy manager must implement methods of the AudioPolicyInterface class.
31 // This implementation makes use of the AudioPolicyClientInterface to control the activity and
32 // configuration of audio input and output streams.
33 //
34 // The platform specific audio policy manager is in charge of the audio routing and volume control
35 // policies for a given platform.
36 // The main roles of this module are:
37 //   - keep track of current system state (removable device connections, phone state, user requests...).
38 //   System state changes and user actions are notified to audio policy manager with methods of the AudioPolicyInterface.
39 //   - process getOutput() queries received when AudioTrack objects are created: Those queries
40 //   return a handler on an output that has been selected, configured and opened by the audio policy manager and that
41 //   must be used by the AudioTrack when registering to the AudioFlinger with the createTrack() method.
42 //   When the AudioTrack object is released, a putOutput() query is received and the audio policy manager can decide
43 //   to close or reconfigure the output depending on other streams using this output and current system state.
44 //   - similarly process getInput() and putInput() queries received from AudioRecord objects and configure audio inputs.
45 //   - process volume control requests: the stream volume is converted from an index value (received from UI) to a float value
46 //   applicable to each output as a function of platform specific settings and current output route (destination device). It
47 //   also make sure that streams are not muted if not allowed (e.g. camera shutter sound in some countries).
48 //
49 // The platform specific audio policy manager is provided as a shared library by platform vendors (as for libaudio.so)
50 // and is linked with libaudioflinger.so
51 
52 
53 //    Audio Policy Manager Interface
54 class AudioPolicyInterface
55 {
56 
57 public:
58     typedef enum {
59         API_INPUT_INVALID = -1,
60         API_INPUT_LEGACY  = 0,// e.g. audio recording from a microphone
61         API_INPUT_MIX_CAPTURE,// used for "remote submix", capture of the media to play it remotely
62         API_INPUT_MIX_EXT_POLICY_REROUTE,// used for platform audio rerouting, where mixes are
63                                          // handled by external and dynamically installed
64                                          // policies which reroute audio mixes
65         API_INPUT_TELEPHONY_RX, // used for capture from telephony RX path
66     } input_type_t;
67 
68    enum {
69         API_INPUT_CONCURRENCY_NONE = 0,
70         API_INPUT_CONCURRENCY_CALL = (1 << 0),      // Concurrency with a call
71         API_INPUT_CONCURRENCY_CAPTURE = (1 << 1),   // Concurrency with another capture
72 
73         API_INPUT_CONCURRENCY_ALL = (API_INPUT_CONCURRENCY_CALL | API_INPUT_CONCURRENCY_CAPTURE),
74    };
75 
76    typedef uint32_t concurrency_type__mask_t;
77 
78 public:
~AudioPolicyInterface()79     virtual ~AudioPolicyInterface() {}
80     //
81     // configuration functions
82     //
83 
84     // indicate a change in device connection status
85     virtual status_t setDeviceConnectionState(audio_devices_t device,
86                                               audio_policy_dev_state_t state,
87                                               const char *device_address,
88                                               const char *device_name) = 0;
89     // retrieve a device connection status
90     virtual audio_policy_dev_state_t getDeviceConnectionState(audio_devices_t device,
91                                                                           const char *device_address) = 0;
92     // indicate a change in device configuration
93     virtual status_t handleDeviceConfigChange(audio_devices_t device,
94                                               const char *device_address,
95                                               const char *device_name) = 0;
96     // indicate a change in phone state. Valid phones states are defined by audio_mode_t
97     virtual void setPhoneState(audio_mode_t state) = 0;
98     // force using a specific device category for the specified usage
99     virtual void setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config) = 0;
100     // retrieve current device category forced for a given usage
101     virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage) = 0;
102     // set a system property (e.g. camera sound always audible)
103     virtual void setSystemProperty(const char* property, const char* value) = 0;
104     // check proper initialization
105     virtual status_t initCheck() = 0;
106 
107     //
108     // Audio routing query functions
109     //
110 
111     // request an output appropriate for playback of the supplied stream type and parameters
112     virtual audio_io_handle_t getOutput(audio_stream_type_t stream,
113                                         uint32_t samplingRate,
114                                         audio_format_t format,
115                                         audio_channel_mask_t channelMask,
116                                         audio_output_flags_t flags,
117                                         const audio_offload_info_t *offloadInfo) = 0;
118     virtual status_t getOutputForAttr(const audio_attributes_t *attr,
119                                         audio_io_handle_t *output,
120                                         audio_session_t session,
121                                         audio_stream_type_t *stream,
122                                         uid_t uid,
123                                         const audio_config_t *config,
124                                         audio_output_flags_t flags,
125                                         int selectedDeviceId,
126                                         audio_port_handle_t *portId) = 0;
127     // indicates to the audio policy manager that the output starts being used by corresponding stream.
128     virtual status_t startOutput(audio_io_handle_t output,
129                                  audio_stream_type_t stream,
130                                  audio_session_t session) = 0;
131     // indicates to the audio policy manager that the output stops being used by corresponding stream.
132     virtual status_t stopOutput(audio_io_handle_t output,
133                                 audio_stream_type_t stream,
134                                 audio_session_t session) = 0;
135     // releases the output.
136     virtual void releaseOutput(audio_io_handle_t output,
137                                audio_stream_type_t stream,
138                                audio_session_t session) = 0;
139 
140     // request an input appropriate for record from the supplied device with supplied parameters.
141     virtual status_t getInputForAttr(const audio_attributes_t *attr,
142                                      audio_io_handle_t *input,
143                                      audio_session_t session,
144                                      uid_t uid,
145                                      const audio_config_base_t *config,
146                                      audio_input_flags_t flags,
147                                      audio_port_handle_t selectedDeviceId,
148                                      input_type_t *inputType,
149                                      audio_port_handle_t *portId) = 0;
150     // indicates to the audio policy manager that the input starts being used.
151     virtual status_t startInput(audio_io_handle_t input,
152                                 audio_session_t session,
153                                 concurrency_type__mask_t *concurrency) = 0;
154     // indicates to the audio policy manager that the input stops being used.
155     virtual status_t stopInput(audio_io_handle_t input,
156                                audio_session_t session) = 0;
157     // releases the input.
158     virtual void releaseInput(audio_io_handle_t input,
159                               audio_session_t session) = 0;
160 
161     //
162     // volume control functions
163     //
164 
165     // initialises stream volume conversion parameters by specifying volume index range.
166     virtual void initStreamVolume(audio_stream_type_t stream,
167                                       int indexMin,
168                                       int indexMax) = 0;
169 
170     // sets the new stream volume at a level corresponding to the supplied index for the
171     // supplied device. By convention, specifying AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME means
172     // setting volume for all devices
173     virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
174                                           int index,
175                                           audio_devices_t device) = 0;
176 
177     // retrieve current volume index for the specified stream and the
178     // specified device. By convention, specifying AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME means
179     // querying the volume of the active device.
180     virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
181                                           int *index,
182                                           audio_devices_t device) = 0;
183 
184     // return the strategy corresponding to a given stream type
185     virtual uint32_t getStrategyForStream(audio_stream_type_t stream) = 0;
186 
187     // return the enabled output devices for the given stream type
188     virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream) = 0;
189 
190     // Audio effect management
191     virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc) = 0;
192     virtual status_t registerEffect(const effect_descriptor_t *desc,
193                                     audio_io_handle_t io,
194                                     uint32_t strategy,
195                                     int session,
196                                     int id) = 0;
197     virtual status_t unregisterEffect(int id) = 0;
198     virtual status_t setEffectEnabled(int id, bool enabled) = 0;
199 
200     virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const = 0;
201     virtual bool isStreamActiveRemotely(audio_stream_type_t stream,
202                                         uint32_t inPastMs = 0) const = 0;
203     virtual bool isSourceActive(audio_source_t source) const = 0;
204 
205     //dump state
206     virtual status_t    dump(int fd) = 0;
207 
208     virtual bool isOffloadSupported(const audio_offload_info_t& offloadInfo) = 0;
209 
210     virtual status_t listAudioPorts(audio_port_role_t role,
211                                     audio_port_type_t type,
212                                     unsigned int *num_ports,
213                                     struct audio_port *ports,
214                                     unsigned int *generation) = 0;
215     virtual status_t getAudioPort(struct audio_port *port) = 0;
216     virtual status_t createAudioPatch(const struct audio_patch *patch,
217                                        audio_patch_handle_t *handle,
218                                        uid_t uid) = 0;
219     virtual status_t releaseAudioPatch(audio_patch_handle_t handle,
220                                           uid_t uid) = 0;
221     virtual status_t listAudioPatches(unsigned int *num_patches,
222                                       struct audio_patch *patches,
223                                       unsigned int *generation) = 0;
224     virtual status_t setAudioPortConfig(const struct audio_port_config *config) = 0;
225     virtual void releaseResourcesForUid(uid_t uid) = 0;
226 
227     virtual status_t acquireSoundTriggerSession(audio_session_t *session,
228                                            audio_io_handle_t *ioHandle,
229                                            audio_devices_t *device) = 0;
230 
231     virtual status_t releaseSoundTriggerSession(audio_session_t session) = 0;
232 
233     virtual status_t registerPolicyMixes(const Vector<AudioMix>& mixes) = 0;
234     virtual status_t unregisterPolicyMixes(Vector<AudioMix> mixes) = 0;
235 
236     virtual status_t startAudioSource(const struct audio_port_config *source,
237                                       const audio_attributes_t *attributes,
238                                       audio_patch_handle_t *handle,
239                                       uid_t uid) = 0;
240     virtual status_t stopAudioSource(audio_patch_handle_t handle) = 0;
241 
242     virtual status_t setMasterMono(bool mono) = 0;
243     virtual status_t getMasterMono(bool *mono) = 0;
244 };
245 
246 
247 // Audio Policy client Interface
248 class AudioPolicyClientInterface
249 {
250 public:
~AudioPolicyClientInterface()251     virtual ~AudioPolicyClientInterface() {}
252 
253     //
254     // Audio HW module functions
255     //
256 
257     // loads a HW module.
258     virtual audio_module_handle_t loadHwModule(const char *name) = 0;
259 
260     //
261     // Audio output Control functions
262     //
263 
264     // opens an audio output with the requested parameters. The parameter values can indicate to use the default values
265     // in case the audio policy manager has no specific requirements for the output being opened.
266     // When the function returns, the parameter values reflect the actual values used by the audio hardware output stream.
267     // The audio policy manager can check if the proposed parameters are suitable or not and act accordingly.
268     virtual status_t openOutput(audio_module_handle_t module,
269                                 audio_io_handle_t *output,
270                                 audio_config_t *config,
271                                 audio_devices_t *devices,
272                                 const String8& address,
273                                 uint32_t *latencyMs,
274                                 audio_output_flags_t flags) = 0;
275     // creates a special output that is duplicated to the two outputs passed as arguments. The duplication is performed by
276     // a special mixer thread in the AudioFlinger.
277     virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1, audio_io_handle_t output2) = 0;
278     // closes the output stream
279     virtual status_t closeOutput(audio_io_handle_t output) = 0;
280     // suspends the output. When an output is suspended, the corresponding audio hardware output stream is placed in
281     // standby and the AudioTracks attached to the mixer thread are still processed but the output mix is discarded.
282     virtual status_t suspendOutput(audio_io_handle_t output) = 0;
283     // restores a suspended output.
284     virtual status_t restoreOutput(audio_io_handle_t output) = 0;
285 
286     //
287     // Audio input Control functions
288     //
289 
290     // opens an audio input
291     virtual status_t openInput(audio_module_handle_t module,
292                                audio_io_handle_t *input,
293                                audio_config_t *config,
294                                audio_devices_t *device,
295                                const String8& address,
296                                audio_source_t source,
297                                audio_input_flags_t flags) = 0;
298     // closes an audio input
299     virtual status_t closeInput(audio_io_handle_t input) = 0;
300     //
301     // misc control functions
302     //
303 
304     // set a stream volume for a particular output. For the same user setting, a given stream type can have different volumes
305     // for each output (destination device) it is attached to.
306     virtual status_t setStreamVolume(audio_stream_type_t stream, float volume, audio_io_handle_t output, int delayMs = 0) = 0;
307 
308     // invalidate a stream type, causing a reroute to an unspecified new output
309     virtual status_t invalidateStream(audio_stream_type_t stream) = 0;
310 
311     // function enabling to send proprietary informations directly from audio policy manager to audio hardware interface.
312     virtual void setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs, int delayMs = 0) = 0;
313     // function enabling to receive proprietary informations directly from audio hardware interface to audio policy manager.
314     virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys) = 0;
315 
316     // request the playback of a tone on the specified stream: used for instance to replace notification sounds when playing
317     // over a telephony device during a phone call.
318     virtual status_t startTone(audio_policy_tone_t tone, audio_stream_type_t stream) = 0;
319     virtual status_t stopTone() = 0;
320 
321     // set down link audio volume.
322     virtual status_t setVoiceVolume(float volume, int delayMs = 0) = 0;
323 
324     // move effect to the specified output
325     virtual status_t moveEffects(audio_session_t session,
326                                      audio_io_handle_t srcOutput,
327                                      audio_io_handle_t dstOutput) = 0;
328 
329     /* Create a patch between several source and sink ports */
330     virtual status_t createAudioPatch(const struct audio_patch *patch,
331                                        audio_patch_handle_t *handle,
332                                        int delayMs) = 0;
333 
334     /* Release a patch */
335     virtual status_t releaseAudioPatch(audio_patch_handle_t handle,
336                                        int delayMs) = 0;
337 
338     /* Set audio port configuration */
339     virtual status_t setAudioPortConfig(const struct audio_port_config *config, int delayMs) = 0;
340 
341     virtual void onAudioPortListUpdate() = 0;
342 
343     virtual void onAudioPatchListUpdate() = 0;
344 
345     virtual audio_unique_id_t newAudioUniqueId(audio_unique_id_use_t use) = 0;
346 
347     virtual void onDynamicPolicyMixStateUpdate(String8 regId, int32_t state) = 0;
348 
349     virtual void onRecordingConfigurationUpdate(int event, audio_session_t session,
350                     audio_source_t source,
351                     const struct audio_config_base *clientConfig,
352                     const struct audio_config_base *deviceConfig,
353                     audio_patch_handle_t patchHandle) = 0;
354 };
355 
356 extern "C" AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface);
357 extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface);
358 
359 
360 }; // namespace android
361 
362 #endif // ANDROID_AUDIOPOLICY_INTERFACE_H
363