blob: f9d119859be19eca413d363edfcbbea88d565dd5 [file] [log] [blame]
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08001/*
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
François Gaffiead3183e2015-03-18 16:55:35 +010017#pragma once
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080018
19#include <stdint.h>
20#include <sys/types.h>
21#include <cutils/config_utils.h>
22#include <cutils/misc.h>
23#include <utils/Timers.h>
24#include <utils/Errors.h>
25#include <utils/KeyedVector.h>
26#include <utils/SortedVector.h>
27#include <media/AudioPolicy.h>
28#include "AudioPolicyInterface.h"
29
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -070030#include <AudioPolicyManagerInterface.h>
François Gaffie2110e042015-03-24 08:41:51 +010031#include <AudioPolicyManagerObserver.h>
François Gaffie98cc1912015-03-18 17:52:40 +010032#include <AudioGain.h>
33#include <AudioPort.h>
34#include <AudioPatch.h>
35#include <ConfigParsingUtils.h>
36#include <DeviceDescriptor.h>
37#include <IOProfile.h>
38#include <HwModule.h>
39#include <AudioInputDescriptor.h>
40#include <AudioOutputDescriptor.h>
François Gaffie036e1e92015-03-19 10:16:24 +010041#include <AudioPolicyMix.h>
François Gaffie45ed3b02015-03-19 10:35:14 +010042#include <EffectDescriptor.h>
François Gaffiedf372692015-03-19 10:43:27 +010043#include <SoundTriggerSession.h>
François Gaffiedfd74092015-03-19 12:10:59 +010044#include <StreamDescriptor.h>
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080045
46namespace android {
47
48// ----------------------------------------------------------------------------
49
50// Attenuation applied to STRATEGY_SONIFICATION streams when a headset is connected: 6dB
51#define SONIFICATION_HEADSET_VOLUME_FACTOR 0.5
Eric Laurentffbc80f2015-03-18 18:30:19 -070052#define SONIFICATION_HEADSET_VOLUME_FACTOR_DB (-6)
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080053// Min volume for STRATEGY_SONIFICATION streams when limited by music volume: -36dB
54#define SONIFICATION_HEADSET_VOLUME_MIN 0.016
Eric Laurentffbc80f2015-03-18 18:30:19 -070055#define SONIFICATION_HEADSET_VOLUME_MIN_DB (-36)
56
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080057// Time in milliseconds during which we consider that music is still active after a music
58// track was stopped - see computeVolume()
59#define SONIFICATION_HEADSET_MUSIC_DELAY 5000
François Gaffie2110e042015-03-24 08:41:51 +010060
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080061// Time in milliseconds during witch some streams are muted while the audio path
62// is switched
63#define MUTE_TIME_MS 2000
64
65#define NUM_TEST_OUTPUTS 5
66
67#define NUM_VOL_CURVE_KNEES 2
68
69// Default minimum length allowed for offloading a compressed track
70// Can be overridden by the audio.offload.min.duration.secs property
71#define OFFLOAD_DEFAULT_MIN_DURATION_SECS 60
72
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080073// ----------------------------------------------------------------------------
74// AudioPolicyManager implements audio policy manager behavior common to all platforms.
75// ----------------------------------------------------------------------------
76
François Gaffie2110e042015-03-24 08:41:51 +010077class AudioPolicyManager : public AudioPolicyInterface, public AudioPolicyManagerObserver
78
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080079#ifdef AUDIO_POLICY_TEST
80 , public Thread
81#endif //AUDIO_POLICY_TEST
82{
83
84public:
85 AudioPolicyManager(AudioPolicyClientInterface *clientInterface);
86 virtual ~AudioPolicyManager();
87
88 // AudioPolicyInterface
89 virtual status_t setDeviceConnectionState(audio_devices_t device,
90 audio_policy_dev_state_t state,
91 const char *device_address,
92 const char *device_name);
93 virtual audio_policy_dev_state_t getDeviceConnectionState(audio_devices_t device,
94 const char *device_address);
95 virtual void setPhoneState(audio_mode_t state);
96 virtual void setForceUse(audio_policy_force_use_t usage,
97 audio_policy_forced_cfg_t config);
98 virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage);
François Gaffie2110e042015-03-24 08:41:51 +010099
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800100 virtual void setSystemProperty(const char* property, const char* value);
101 virtual status_t initCheck();
102 virtual audio_io_handle_t getOutput(audio_stream_type_t stream,
103 uint32_t samplingRate,
104 audio_format_t format,
105 audio_channel_mask_t channelMask,
106 audio_output_flags_t flags,
107 const audio_offload_info_t *offloadInfo);
108 virtual status_t getOutputForAttr(const audio_attributes_t *attr,
109 audio_io_handle_t *output,
110 audio_session_t session,
111 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700112 uid_t uid,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800113 uint32_t samplingRate,
114 audio_format_t format,
115 audio_channel_mask_t channelMask,
116 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700117 audio_port_handle_t selectedDeviceId,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800118 const audio_offload_info_t *offloadInfo);
119 virtual status_t startOutput(audio_io_handle_t output,
120 audio_stream_type_t stream,
121 audio_session_t session);
122 virtual status_t stopOutput(audio_io_handle_t output,
123 audio_stream_type_t stream,
124 audio_session_t session);
125 virtual void releaseOutput(audio_io_handle_t output,
126 audio_stream_type_t stream,
127 audio_session_t session);
128 virtual status_t getInputForAttr(const audio_attributes_t *attr,
129 audio_io_handle_t *input,
130 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700131 uid_t uid,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800132 uint32_t samplingRate,
133 audio_format_t format,
134 audio_channel_mask_t channelMask,
135 audio_input_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -0600136 audio_port_handle_t selectedDeviceId,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800137 input_type_t *inputType);
138
139 // indicates to the audio policy manager that the input starts being used.
140 virtual status_t startInput(audio_io_handle_t input,
141 audio_session_t session);
142
143 // indicates to the audio policy manager that the input stops being used.
144 virtual status_t stopInput(audio_io_handle_t input,
145 audio_session_t session);
146 virtual void releaseInput(audio_io_handle_t input,
147 audio_session_t session);
148 virtual void closeAllInputs();
149 virtual void initStreamVolume(audio_stream_type_t stream,
150 int indexMin,
151 int indexMax);
152 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
153 int index,
154 audio_devices_t device);
155 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
156 int *index,
157 audio_devices_t device);
158
159 // return the strategy corresponding to a given stream type
160 virtual uint32_t getStrategyForStream(audio_stream_type_t stream);
161 // return the strategy corresponding to the given audio attributes
162 virtual uint32_t getStrategyForAttr(const audio_attributes_t *attr);
163
164 // return the enabled output devices for the given stream type
165 virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream);
166
167 virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc = NULL);
168 virtual status_t registerEffect(const effect_descriptor_t *desc,
169 audio_io_handle_t io,
170 uint32_t strategy,
171 int session,
172 int id);
François Gaffie45ed3b02015-03-19 10:35:14 +0100173 virtual status_t unregisterEffect(int id)
174 {
175 return mEffects.unregisterEffect(id);
176 }
177 virtual status_t setEffectEnabled(int id, bool enabled)
178 {
179 return mEffects.setEffectEnabled(id, enabled);
180 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800181
Eric Laurentc75307b2015-03-17 15:29:32 -0700182 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800183 // return whether a stream is playing remotely, override to change the definition of
184 // local/remote playback, used for instance by notification manager to not make
185 // media players lose audio focus when not playing locally
186 // For the base implementation, "remotely" means playing during screen mirroring which
187 // uses an output for playback with a non-empty, non "0" address.
Eric Laurentc75307b2015-03-17 15:29:32 -0700188 virtual bool isStreamActiveRemotely(audio_stream_type_t stream,
189 uint32_t inPastMs = 0) const;
190
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800191 virtual bool isSourceActive(audio_source_t source) const;
192
193 virtual status_t dump(int fd);
194
195 virtual bool isOffloadSupported(const audio_offload_info_t& offloadInfo);
196
197 virtual status_t listAudioPorts(audio_port_role_t role,
198 audio_port_type_t type,
199 unsigned int *num_ports,
200 struct audio_port *ports,
201 unsigned int *generation);
202 virtual status_t getAudioPort(struct audio_port *port);
203 virtual status_t createAudioPatch(const struct audio_patch *patch,
204 audio_patch_handle_t *handle,
205 uid_t uid);
206 virtual status_t releaseAudioPatch(audio_patch_handle_t handle,
207 uid_t uid);
208 virtual status_t listAudioPatches(unsigned int *num_patches,
209 struct audio_patch *patches,
210 unsigned int *generation);
211 virtual status_t setAudioPortConfig(const struct audio_port_config *config);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800212
213 virtual status_t acquireSoundTriggerSession(audio_session_t *session,
214 audio_io_handle_t *ioHandle,
215 audio_devices_t *device);
216
François Gaffiedf372692015-03-19 10:43:27 +0100217 virtual status_t releaseSoundTriggerSession(audio_session_t session)
218 {
219 return mSoundTriggerSessions.releaseSession(session);
220 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800221
222 virtual status_t registerPolicyMixes(Vector<AudioMix> mixes);
223 virtual status_t unregisterPolicyMixes(Vector<AudioMix> mixes);
224
Eric Laurent554a2772015-04-10 11:29:24 -0700225 virtual status_t startAudioSource(const struct audio_port_config *source,
226 const audio_attributes_t *attributes,
227 audio_io_handle_t *handle);
228 virtual status_t stopAudioSource(audio_io_handle_t handle);
229
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700230 virtual void releaseResourcesForUid(uid_t uid);
231
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800232 // Audio policy configuration file parsing (audio_policy.conf)
233 // TODO candidates to be moved to ConfigParsingUtils
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800234 void defaultAudioPolicyConfig(void);
235
François Gaffie2110e042015-03-24 08:41:51 +0100236 // return the strategy corresponding to a given stream type
237 routing_strategy getStrategy(audio_stream_type_t stream) const;
238
Paul McLeanaa981192015-03-21 09:55:15 -0700239protected:
Paul McLean466dc8e2015-04-17 13:15:36 -0600240 class SessionRoute : public RefBase {
Paul McLeanaa981192015-03-21 09:55:15 -0700241 public:
Paul McLean466dc8e2015-04-17 13:15:36 -0600242 // For Input (Source) routes, use STREAM_TYPE_NA ("NA" = "not applicable)for the
243 // streamType argument
244 static const audio_stream_type_t STREAM_TYPE_NA = AUDIO_STREAM_DEFAULT;
245
246 // For Output (Sink) routes, use SOURCE_TYPE_NA ("NA" = "not applicable") for the
247 // source argument
248
249 static const audio_source_t SOURCE_TYPE_NA = AUDIO_SOURCE_DEFAULT;
250
Paul McLeanaa981192015-03-21 09:55:15 -0700251 SessionRoute(audio_session_t session,
252 audio_stream_type_t streamType,
Paul McLean466dc8e2015-04-17 13:15:36 -0600253 audio_source_t source,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700254 sp<DeviceDescriptor> deviceDescriptor,
255 uid_t uid)
256 : mUid(uid),
257 mSession(session),
Paul McLean466dc8e2015-04-17 13:15:36 -0600258 mDeviceDescriptor(deviceDescriptor),
259 mRefCount(0),
260 mActivityCount(0),
261 mChanged(false),
262 mStreamType(streamType),
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700263 mSource(source)
264 {}
Paul McLeanaa981192015-03-21 09:55:15 -0700265
Paul McLean466dc8e2015-04-17 13:15:36 -0600266 void log(const char* prefix);
267
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700268 bool isActive() {
269 return (mDeviceDescriptor != 0) && (mChanged || (mActivityCount > 0));
270 }
271
272 uid_t mUid;
273 audio_session_t mSession;
274 sp<DeviceDescriptor> mDeviceDescriptor;
275
Paul McLeanaa981192015-03-21 09:55:15 -0700276 // "reference" counting
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700277 int mRefCount; // +/- on references
278 int mActivityCount; // +/- on start/stop
279 bool mChanged;
Paul McLean466dc8e2015-04-17 13:15:36 -0600280 // for outputs
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700281 const audio_stream_type_t mStreamType;
Paul McLean466dc8e2015-04-17 13:15:36 -0600282 // for inputs
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700283 const audio_source_t mSource;
Paul McLeanaa981192015-03-21 09:55:15 -0700284 };
285
Paul McLean466dc8e2015-04-17 13:15:36 -0600286 class SessionRouteMap: public KeyedVector<audio_session_t, sp<SessionRoute>> {
287 public:
288 // These constants identify the SessionRoutMap as holding EITHER input routes,
289 // or output routes. An error will occur if an attempt is made to add a SessionRoute
290 // object with mStreamType == STREAM_TYPE_NA (i.e. an input SessionRoute) to a
291 // SessionRoutMap that is marked for output (i.e. mMapType == SESSION_ROUTE_MAP_OUTPUT)
292 // and similarly for output SessionRoutes and Input SessionRouteMaps.
293 typedef enum {
294 MAPTYPE_INPUT = 0,
295 MAPTYPE_OUTPUT = 1
296 } session_route_map_type_t;
297
298 SessionRouteMap(session_route_map_type_t mapType) :
299 mMapType(mapType) {
300 }
301
Paul McLeanaa981192015-03-21 09:55:15 -0700302 bool hasRoute(audio_session_t session);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700303
Paul McLeanaa981192015-03-21 09:55:15 -0700304 void removeRoute(audio_session_t session);
305
306 int incRouteActivity(audio_session_t session);
307 int decRouteActivity(audio_session_t session);
Eric Laurent493404d2015-04-21 15:07:36 -0700308 bool hasRouteChanged(audio_session_t session); // also clears the changed flag
Paul McLeanaa981192015-03-21 09:55:15 -0700309 void log(const char* caption);
Paul McLean466dc8e2015-04-17 13:15:36 -0600310
311 // Specify an Output(Sink) route by passing SessionRoute::SOURCE_TYPE_NA in the
312 // source argument.
313 // Specify an Input(Source) rout by passing SessionRoute::AUDIO_STREAM_DEFAULT
314 // in the streamType argument.
315 void addRoute(audio_session_t session,
316 audio_stream_type_t streamType,
317 audio_source_t source,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700318 sp<DeviceDescriptor> deviceDescriptor,
319 uid_t uid);
Paul McLean466dc8e2015-04-17 13:15:36 -0600320
321 private:
322 // Used to mark a SessionRoute as for either inputs (mMapType == kSessionRouteMap_Input)
323 // or outputs (mMapType == kSessionRouteMap_Output)
324 const session_route_map_type_t mMapType;
Paul McLeanaa981192015-03-21 09:55:15 -0700325 };
326
François Gaffie2110e042015-03-24 08:41:51 +0100327 // From AudioPolicyManagerObserver
328 virtual const AudioPatchCollection &getAudioPatches() const
329 {
330 return mAudioPatches;
331 }
332 virtual const SoundTriggerSessionCollection &getSoundTriggerSessionCollection() const
333 {
334 return mSoundTriggerSessions;
335 }
336 virtual const AudioPolicyMixCollection &getAudioPolicyMixCollection() const
337 {
338 return mPolicyMixes;
339 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700340 virtual const SwAudioOutputCollection &getOutputs() const
François Gaffie2110e042015-03-24 08:41:51 +0100341 {
342 return mOutputs;
343 }
344 virtual const AudioInputCollection &getInputs() const
345 {
346 return mInputs;
347 }
348 virtual const DeviceVector &getAvailableOutputDevices() const
349 {
350 return mAvailableOutputDevices;
351 }
352 virtual const DeviceVector &getAvailableInputDevices() const
353 {
354 return mAvailableInputDevices;
355 }
356 virtual StreamDescriptorCollection &getStreamDescriptors()
357 {
358 return mStreams;
359 }
360 virtual const sp<DeviceDescriptor> &getDefaultOutputDevice() const
361 {
362 return mDefaultOutputDevice;
363 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800364protected:
Eric Laurentc75307b2015-03-17 15:29:32 -0700365 void addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc);
François Gaffie53615e22015-03-19 09:24:12 +0100366 void removeOutput(audio_io_handle_t output);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800367 void addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc);
368
369 // return appropriate device for streams handled by the specified strategy according to current
370 // phone state, connected devices...
371 // if fromCache is true, the device is returned from mDeviceForStrategy[],
372 // otherwise it is determine by current state
373 // (device connected,phone state, force use, a2dp output...)
374 // This allows to:
375 // 1 speed up process when the state is stable (when starting or stopping an output)
376 // 2 access to either current device selection (fromCache == true) or
377 // "future" device selection (fromCache == false) when called from a context
378 // where conditions are changing (setDeviceConnectionState(), setPhoneState()...) AND
379 // before updateDevicesAndOutputs() is called.
380 virtual audio_devices_t getDeviceForStrategy(routing_strategy strategy,
381 bool fromCache);
382
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700383 bool isStrategyActive(const sp<AudioOutputDescriptor> outputDesc, routing_strategy strategy,
384 uint32_t inPastMs = 0, nsecs_t sysTime = 0) const;
385
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800386 // change the route of the specified output. Returns the number of ms we have slept to
387 // allow new routing to take effect in certain cases.
Eric Laurentc75307b2015-03-17 15:29:32 -0700388 virtual uint32_t setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800389 audio_devices_t device,
390 bool force = false,
391 int delayMs = 0,
392 audio_patch_handle_t *patchHandle = NULL,
393 const char* address = NULL);
Eric Laurentc75307b2015-03-17 15:29:32 -0700394 status_t resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800395 int delayMs = 0,
396 audio_patch_handle_t *patchHandle = NULL);
397 status_t setInputDevice(audio_io_handle_t input,
398 audio_devices_t device,
399 bool force = false,
400 audio_patch_handle_t *patchHandle = NULL);
401 status_t resetInputDevice(audio_io_handle_t input,
402 audio_patch_handle_t *patchHandle = NULL);
403
404 // select input device corresponding to requested audio source
405 virtual audio_devices_t getDeviceForInputSource(audio_source_t inputSource);
406
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800407 // compute the actual volume for a given stream according to the requested index and a particular
408 // device
Eric Laurentc75307b2015-03-17 15:29:32 -0700409 virtual float computeVolume(audio_stream_type_t stream,
410 int index,
411 audio_devices_t device);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800412
413 // check that volume change is permitted, compute and send new volume to audio hardware
414 virtual status_t checkAndSetVolume(audio_stream_type_t stream, int index,
Eric Laurentc75307b2015-03-17 15:29:32 -0700415 const sp<AudioOutputDescriptor>& outputDesc,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800416 audio_devices_t device,
417 int delayMs = 0, bool force = false);
418
419 // apply all stream volumes to the specified output and device
Eric Laurentc75307b2015-03-17 15:29:32 -0700420 void applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
421 audio_devices_t device, int delayMs = 0, bool force = false);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800422
423 // Mute or unmute all streams handled by the specified strategy on the specified output
424 void setStrategyMute(routing_strategy strategy,
425 bool on,
Eric Laurentc75307b2015-03-17 15:29:32 -0700426 const sp<AudioOutputDescriptor>& outputDesc,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800427 int delayMs = 0,
428 audio_devices_t device = (audio_devices_t)0);
429
430 // Mute or unmute the stream on the specified output
431 void setStreamMute(audio_stream_type_t stream,
432 bool on,
Eric Laurentc75307b2015-03-17 15:29:32 -0700433 const sp<AudioOutputDescriptor>& outputDesc,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800434 int delayMs = 0,
435 audio_devices_t device = (audio_devices_t)0);
436
437 // handle special cases for sonification strategy while in call: mute streams or replace by
438 // a special tone in the device used for communication
439 void handleIncallSonification(audio_stream_type_t stream, bool starting, bool stateChange);
440
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700441 audio_mode_t getPhoneState();
442
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800443 // true if device is in a telephony or VoIP call
444 virtual bool isInCall();
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800445 // true if given state represents a device in a telephony or VoIP call
446 virtual bool isStateInCall(int state);
447
448 // when a device is connected, checks if an open output can be routed
449 // to this device. If none is open, tries to open one of the available outputs.
450 // Returns an output suitable to this device or 0.
451 // when a device is disconnected, checks if an output is not used any more and
452 // returns its handle if any.
453 // transfers the audio tracks and effects from one output thread to another accordingly.
454 status_t checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
455 audio_policy_dev_state_t state,
456 SortedVector<audio_io_handle_t>& outputs,
457 const String8 address);
458
459 status_t checkInputsForDevice(audio_devices_t device,
460 audio_policy_dev_state_t state,
461 SortedVector<audio_io_handle_t>& inputs,
462 const String8 address);
463
464 // close an output and its companion duplicating output.
465 void closeOutput(audio_io_handle_t output);
466
467 // close an input.
468 void closeInput(audio_io_handle_t input);
469
470 // checks and if necessary changes outputs used for all strategies.
471 // must be called every time a condition that affects the output choice for a given strategy
472 // changes: connected device, phone state, force use...
473 // Must be called before updateDevicesAndOutputs()
474 void checkOutputForStrategy(routing_strategy strategy);
475
476 // Same as checkOutputForStrategy() but for a all strategies in order of priority
477 void checkOutputForAllStrategies();
478
479 // manages A2DP output suspend/restore according to phone state and BT SCO usage
480 void checkA2dpSuspend();
481
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800482 // selects the most appropriate device on output for current state
483 // must be called every time a condition that affects the device choice for a given output is
484 // changed: connected device, phone state, force use, output start, output stop..
485 // see getDeviceForStrategy() for the use of fromCache parameter
Eric Laurentc75307b2015-03-17 15:29:32 -0700486 audio_devices_t getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
487 bool fromCache);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800488
489 // updates cache of device used by all strategies (mDeviceForStrategy[])
490 // must be called every time a condition that affects the device choice for a given strategy is
491 // changed: connected device, phone state, force use...
492 // cached values are used by getDeviceForStrategy() if parameter fromCache is true.
493 // Must be called after checkOutputForAllStrategies()
494 void updateDevicesAndOutputs();
495
496 // selects the most appropriate device on input for current state
497 audio_devices_t getNewInputDevice(audio_io_handle_t input);
498
François Gaffie45ed3b02015-03-19 10:35:14 +0100499 virtual uint32_t getMaxEffectsCpuLoad()
500 {
501 return mEffects.getMaxEffectsCpuLoad();
502 }
503
504 virtual uint32_t getMaxEffectsMemory()
505 {
506 return mEffects.getMaxEffectsMemory();
507 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800508#ifdef AUDIO_POLICY_TEST
509 virtual bool threadLoop();
510 void exit();
511 int testOutputIndex(audio_io_handle_t output);
512#endif //AUDIO_POLICY_TEST
513
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800514 SortedVector<audio_io_handle_t> getOutputsForDevice(audio_devices_t device,
Eric Laurentc75307b2015-03-17 15:29:32 -0700515 SwAudioOutputCollection openOutputs);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800516 bool vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
517 SortedVector<audio_io_handle_t>& outputs2);
518
519 // mute/unmute strategies using an incompatible device combination
520 // if muting, wait for the audio in pcm buffer to be drained before proceeding
521 // if unmuting, unmute only after the specified delay
522 // Returns the number of ms waited
523 virtual uint32_t checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
524 audio_devices_t prevDevice,
525 uint32_t delayMs);
526
527 audio_io_handle_t selectOutput(const SortedVector<audio_io_handle_t>& outputs,
528 audio_output_flags_t flags,
529 audio_format_t format);
Andy Hungf129b032015-04-07 13:45:50 -0700530 // samplingRate, format, channelMask are in/out and so may be modified
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800531 sp<IOProfile> getInputProfile(audio_devices_t device,
532 String8 address,
533 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -0700534 audio_format_t& format,
535 audio_channel_mask_t& channelMask,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800536 audio_input_flags_t flags);
537 sp<IOProfile> getProfileForDirectOutput(audio_devices_t device,
538 uint32_t samplingRate,
539 audio_format_t format,
540 audio_channel_mask_t channelMask,
541 audio_output_flags_t flags);
542
543 audio_io_handle_t selectOutputForEffects(const SortedVector<audio_io_handle_t>& outputs);
544
François Gaffie53615e22015-03-19 09:24:12 +0100545 virtual status_t addAudioPatch(audio_patch_handle_t handle, const sp<AudioPatch>& patch)
546 {
547 return mAudioPatches.addAudioPatch(handle, patch);
548 }
549 virtual status_t removeAudioPatch(audio_patch_handle_t handle)
550 {
551 return mAudioPatches.removeAudioPatch(handle);
552 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800553
François Gaffie53615e22015-03-19 09:24:12 +0100554 audio_devices_t availablePrimaryOutputDevices() const
555 {
Eric Laurent87ffa392015-05-22 10:32:38 -0700556 if (!hasPrimaryOutput()) {
557 return AUDIO_DEVICE_NONE;
558 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700559 return mPrimaryOutput->supportedDevices() & mAvailableOutputDevices.types();
François Gaffie53615e22015-03-19 09:24:12 +0100560 }
561 audio_devices_t availablePrimaryInputDevices() const
562 {
Eric Laurent87ffa392015-05-22 10:32:38 -0700563 if (!hasPrimaryOutput()) {
564 return AUDIO_DEVICE_NONE;
565 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700566 return mAvailableInputDevices.getDevicesFromHwModule(mPrimaryOutput->getModuleHandle());
François Gaffie53615e22015-03-19 09:24:12 +0100567 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800568
569 void updateCallRouting(audio_devices_t rxDevice, int delayMs = 0);
570
Eric Laurent493404d2015-04-21 15:07:36 -0700571 // if argument "device" is different from AUDIO_DEVICE_NONE, startSource() will force
572 // the re-evaluation of the output device.
Eric Laurentc75307b2015-03-17 15:29:32 -0700573 status_t startSource(sp<AudioOutputDescriptor> outputDesc,
574 audio_stream_type_t stream,
575 audio_devices_t device,
576 uint32_t *delayMs);
577 status_t stopSource(sp<AudioOutputDescriptor> outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700578 audio_stream_type_t stream,
579 bool forceDeviceUpdate);
580
581 void clearAudioPatches(uid_t uid);
582 void clearSessionRoutes(uid_t uid);
583 void checkStrategyRoute(routing_strategy strategy, audio_io_handle_t ouptutToSkip);
Eric Laurentc75307b2015-03-17 15:29:32 -0700584
Eric Laurent87ffa392015-05-22 10:32:38 -0700585 status_t hasPrimaryOutput() const { return mPrimaryOutput != 0; }
586
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800587 uid_t mUidCached;
588 AudioPolicyClientInterface *mpClientInterface; // audio policy client interface
Eric Laurentc75307b2015-03-17 15:29:32 -0700589 sp<SwAudioOutputDescriptor> mPrimaryOutput; // primary output descriptor
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800590 // list of descriptors for outputs currently opened
Eric Laurentc75307b2015-03-17 15:29:32 -0700591
592 SwAudioOutputCollection mOutputs;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800593 // copy of mOutputs before setDeviceConnectionState() opens new outputs
594 // reset to mOutputs when updateDevicesAndOutputs() is called.
Eric Laurentc75307b2015-03-17 15:29:32 -0700595 SwAudioOutputCollection mPreviousOutputs;
François Gaffie53615e22015-03-19 09:24:12 +0100596 AudioInputCollection mInputs; // list of input descriptors
Eric Laurentc75307b2015-03-17 15:29:32 -0700597
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800598 DeviceVector mAvailableOutputDevices; // all available output devices
599 DeviceVector mAvailableInputDevices; // all available input devices
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800600
Paul McLean466dc8e2015-04-17 13:15:36 -0600601 SessionRouteMap mOutputRoutes = SessionRouteMap(SessionRouteMap::MAPTYPE_OUTPUT);
602 SessionRouteMap mInputRoutes = SessionRouteMap(SessionRouteMap::MAPTYPE_INPUT);
Paul McLeanaa981192015-03-21 09:55:15 -0700603
François Gaffiedfd74092015-03-19 12:10:59 +0100604 StreamDescriptorCollection mStreams; // stream descriptors for volume control
605 bool mLimitRingtoneVolume; // limit ringtone volume to music volume if headset connected
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800606 audio_devices_t mDeviceForStrategy[NUM_STRATEGIES];
François Gaffiedfd74092015-03-19 12:10:59 +0100607 float mLastVoiceVolume; // last voice volume value sent to audio HAL
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800608
François Gaffie45ed3b02015-03-19 10:35:14 +0100609 EffectDescriptorCollection mEffects; // list of registered audio effects
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800610 bool mA2dpSuspended; // true if A2DP output is suspended
611 sp<DeviceDescriptor> mDefaultOutputDevice; // output device selected by default at boot time
612 bool mSpeakerDrcEnabled;// true on devices that use DRC on the DEVICE_CATEGORY_SPEAKER path
613 // to boost soft sounds, used to adjust volume curves accordingly
614
François Gaffie53615e22015-03-19 09:24:12 +0100615 HwModuleCollection mHwModules;
616
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800617 volatile int32_t mAudioPortGeneration;
618
François Gaffie53615e22015-03-19 09:24:12 +0100619 AudioPatchCollection mAudioPatches;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800620
François Gaffiedf372692015-03-19 10:43:27 +0100621 SoundTriggerSessionCollection mSoundTriggerSessions;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800622
623 sp<AudioPatch> mCallTxPatch;
624 sp<AudioPatch> mCallRxPatch;
625
626 // for supporting "beacon" streams, i.e. streams that only play on speaker, and never
627 // when something other than STREAM_TTS (a.k.a. "Transmitted Through Speaker") is playing
628 enum {
629 STARTING_OUTPUT,
630 STARTING_BEACON,
631 STOPPING_OUTPUT,
632 STOPPING_BEACON
633 };
634 uint32_t mBeaconMuteRefCount; // ref count for stream that would mute beacon
635 uint32_t mBeaconPlayingRefCount;// ref count for the playing beacon streams
636 bool mBeaconMuted; // has STREAM_TTS been muted
637
François Gaffie036e1e92015-03-19 10:16:24 +0100638 AudioPolicyMixCollection mPolicyMixes; // list of registered mixes
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800639
640#ifdef AUDIO_POLICY_TEST
641 Mutex mLock;
642 Condition mWaitWorkCV;
643
644 int mCurOutput;
645 bool mDirectOutput;
646 audio_io_handle_t mTestOutputs[NUM_TEST_OUTPUTS];
647 int mTestInput;
648 uint32_t mTestDevice;
649 uint32_t mTestSamplingRate;
650 uint32_t mTestFormat;
651 uint32_t mTestChannels;
652 uint32_t mTestLatencyMs;
653#endif //AUDIO_POLICY_TEST
654
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800655 uint32_t nextAudioPortGeneration();
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700656
657 // Audio Policy Engine Interface.
658 AudioPolicyManagerInterface *mEngine;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800659private:
660 // updates device caching and output for streams that can influence the
661 // routing of notifications
662 void handleNotificationRoutingForStream(audio_stream_type_t stream);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800663 // find the outputs on a given output descriptor that have the given address.
664 // to be called on an AudioOutputDescriptor whose supported devices (as defined
665 // in mProfile->mSupportedDevices) matches the device whose address is to be matched.
666 // see deviceDistinguishesOnAddress(audio_devices_t) for whether the device type is one
667 // where addresses are used to distinguish between one connected device and another.
Eric Laurentc75307b2015-03-17 15:29:32 -0700668 void findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800669 const audio_devices_t device /*in*/,
670 const String8 address /*in*/,
671 SortedVector<audio_io_handle_t>& outputs /*out*/);
672 uint32_t curAudioPortGeneration() const { return mAudioPortGeneration; }
673 // internal method to return the output handle for the given device and format
674 audio_io_handle_t getOutputForDevice(
675 audio_devices_t device,
676 audio_session_t session,
677 audio_stream_type_t stream,
678 uint32_t samplingRate,
679 audio_format_t format,
680 audio_channel_mask_t channelMask,
681 audio_output_flags_t flags,
682 const audio_offload_info_t *offloadInfo);
683 // internal function to derive a stream type value from audio attributes
684 audio_stream_type_t streamTypefromAttributesInt(const audio_attributes_t *attr);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800685 // event is one of STARTING_OUTPUT, STARTING_BEACON, STOPPING_OUTPUT, STOPPING_BEACON
686 // returns 0 if no mute/unmute event happened, the largest latency of the device where
687 // the mute/unmute happened
688 uint32_t handleEventForBeacon(int event);
689 uint32_t setBeaconMute(bool mute);
690 bool isValidAttributes(const audio_attributes_t *paa);
691
692 // select input device corresponding to requested audio source and return associated policy
693 // mix if any. Calls getDeviceForInputSource().
694 audio_devices_t getDeviceAndMixForInputSource(audio_source_t inputSource,
695 AudioMix **policyMix = NULL);
696
697 // Called by setDeviceConnectionState().
698 status_t setDeviceConnectionStateInt(audio_devices_t device,
699 audio_policy_dev_state_t state,
700 const char *device_address,
701 const char *device_name);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800702};
703
704};