blob: 0722aed2c8ce4044cba037728c38399ee031c339 [file] [log] [blame]
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001/*
Mingming Yin4a72d652014-01-03 18:54:18 -08002 * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07003 * Not a contribution.
4 *
5 * Copyright (C) 2009 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#define LOG_TAG "AudioPolicyManager"
21//#define LOG_NDEBUG 0
22
23//#define VERY_VERBOSE_LOGGING
24#ifdef VERY_VERBOSE_LOGGING
25#define ALOGVV ALOGV
26#else
27#define ALOGVV(a...) do { } while(0)
28#endif
29
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070030// A device mask for all audio output devices that are considered "remote" when evaluating
31// active output devices in isStreamActiveRemotely()
32#define APM_AUDIO_OUT_DEVICE_REMOTE_ALL AUDIO_DEVICE_OUT_REMOTE_SUBMIX
33
34#include <utils/Log.h>
35#include "AudioPolicyManager.h"
36#include <hardware/audio_effect.h>
37#include <hardware/audio.h>
38#include <math.h>
39#include <hardware_legacy/audio_policy_conf.h>
40#include <cutils/properties.h>
41
42namespace android_audio_legacy {
43
44// ----------------------------------------------------------------------------
45// AudioPolicyInterface implementation
46// ----------------------------------------------------------------------------
47
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070048status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
49 AudioSystem::device_connection_state state,
50 const char *device_address)
51{
52 SortedVector <audio_io_handle_t> outputs;
53
54 ALOGV("setDeviceConnectionState() device: %x, state %d, address %s", device, state, device_address);
55
56 // connect/disconnect only 1 device at a time
57 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
58
59 if (strlen(device_address) >= MAX_DEVICE_ADDRESS_LEN) {
60 ALOGE("setDeviceConnectionState() invalid address: %s", device_address);
61 return BAD_VALUE;
62 }
63
64 // handle output devices
65 if (audio_is_output_device(device)) {
66
67 if (!mHasA2dp && audio_is_a2dp_device(device)) {
68 ALOGE("setDeviceConnectionState() invalid A2DP device: %x", device);
69 return BAD_VALUE;
70 }
71 if (!mHasUsb && audio_is_usb_device(device)) {
72 ALOGE("setDeviceConnectionState() invalid USB audio device: %x", device);
73 return BAD_VALUE;
74 }
75 if (!mHasRemoteSubmix && audio_is_remote_submix_device((audio_devices_t)device)) {
76 ALOGE("setDeviceConnectionState() invalid remote submix audio device: %x", device);
77 return BAD_VALUE;
78 }
79
80 // save a copy of the opened output descriptors before any output is opened or closed
81 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
82 mPreviousOutputs = mOutputs;
83 switch (state)
84 {
85 // handle output device connection
86 case AudioSystem::DEVICE_STATE_AVAILABLE:
87 if (mAvailableOutputDevices & device) {
88 ALOGW("setDeviceConnectionState() device already connected: %x", device);
89 return INVALID_OPERATION;
90 }
91 ALOGV("setDeviceConnectionState() connecting device %x", device);
92
93 if (checkOutputsForDevice(device, state, outputs) != NO_ERROR) {
94 return INVALID_OPERATION;
95 }
96 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %d outputs",
97 outputs.size());
98 // register new device as available
99 mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices | device);
100
101 if (!outputs.isEmpty()) {
102 String8 paramStr;
103 if (mHasA2dp && audio_is_a2dp_device(device)) {
104 // handle A2DP device connection
105 AudioParameter param;
106 param.add(String8(AUDIO_PARAMETER_A2DP_SINK_ADDRESS), String8(device_address));
107 paramStr = param.toString();
108 mA2dpDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
109 mA2dpSuspended = false;
110 } else if (audio_is_bluetooth_sco_device(device)) {
111 // handle SCO device connection
112 mScoDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
113 } else if (mHasUsb && audio_is_usb_device(device)) {
114 // handle USB device connection
115 mUsbCardAndDevice = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
116 paramStr = mUsbCardAndDevice;
117 }
118 // not currently handling multiple simultaneous submixes: ignoring remote submix
119 // case and address
120 if (!paramStr.isEmpty()) {
121 for (size_t i = 0; i < outputs.size(); i++) {
122 mpClientInterface->setParameters(outputs[i], paramStr);
123 }
124 }
125 }
126 break;
127 // handle output device disconnection
128 case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
129 if (!(mAvailableOutputDevices & device)) {
130 ALOGW("setDeviceConnectionState() device not connected: %x", device);
131 return INVALID_OPERATION;
132 }
133
134 ALOGV("setDeviceConnectionState() disconnecting device %x", device);
135 // remove device from available output devices
136 mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices & ~device);
137
138 checkOutputsForDevice(device, state, outputs);
139 if (mHasA2dp && audio_is_a2dp_device(device)) {
140 // handle A2DP device disconnection
141 mA2dpDeviceAddress = "";
142 mA2dpSuspended = false;
143 } else if (audio_is_bluetooth_sco_device(device)) {
144 // handle SCO device disconnection
145 mScoDeviceAddress = "";
146 } else if (mHasUsb && audio_is_usb_device(device)) {
147 // handle USB device disconnection
148 mUsbCardAndDevice = "";
149 }
150 // not currently handling multiple simultaneous submixes: ignoring remote submix
151 // case and address
152 } break;
153
154 default:
155 ALOGE("setDeviceConnectionState() invalid state: %x", state);
156 return BAD_VALUE;
157 }
158
159 checkA2dpSuspend();
160 checkOutputForAllStrategies();
161 // outputs must be closed after checkOutputForAllStrategies() is executed
162 if (!outputs.isEmpty()) {
163 for (size_t i = 0; i < outputs.size(); i++) {
164 AudioOutputDescriptor *desc = mOutputs.valueFor(outputs[i]);
165 // close unused outputs after device disconnection or direct outputs that have been
166 // opened by checkOutputsForDevice() to query dynamic parameters
167 if ((state == AudioSystem::DEVICE_STATE_UNAVAILABLE) ||
168 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
169 (desc->mDirectOpenCount == 0))) {
170 closeOutput(outputs[i]);
171 }
172 }
173 }
174
175 updateDevicesAndOutputs();
176 audio_devices_t newDevice = getNewDevice(mPrimaryOutput, false /*fromCache*/);
177#ifdef AUDIO_EXTN_FM_ENABLED
178 if(device == AUDIO_DEVICE_OUT_FM) {
179 if (state == AudioSystem::DEVICE_STATE_AVAILABLE) {
180 mOutputs.valueFor(mPrimaryOutput)->changeRefCount(AudioSystem::MUSIC, 1);
181 newDevice = (audio_devices_t)(getNewDevice(mPrimaryOutput, false) | AUDIO_DEVICE_OUT_FM);
182 } else {
183 mOutputs.valueFor(mPrimaryOutput)->changeRefCount(AudioSystem::MUSIC, -1);
184 }
185
186 AudioParameter param = AudioParameter();
187 param.addInt(String8("handle_fm"), (int)newDevice);
188 ALOGV("setDeviceConnectionState() setParameters handle_fm");
189 mpClientInterface->setParameters(mPrimaryOutput, param.toString());
190 }
191#endif
192 for (size_t i = 0; i < mOutputs.size(); i++) {
193 // do not force device change on duplicated output because if device is 0, it will
194 // also force a device 0 for the two outputs it is duplicated to which may override
195 // a valid device selection on those outputs.
196 setOutputDevice(mOutputs.keyAt(i),
197 getNewDevice(mOutputs.keyAt(i), true /*fromCache*/),
198 !mOutputs.valueAt(i)->isDuplicated(),
199 0);
200 }
201
202 if (device == AUDIO_DEVICE_OUT_WIRED_HEADSET) {
203 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
204 } else if (device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO ||
205 device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET ||
206 device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT) {
207 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
208 } else if(device == AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET){
209 device = AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET;
210 } else {
211 return NO_ERROR;
212 }
213 }
214 // handle input devices
215 if (audio_is_input_device(device)) {
216
217 switch (state)
218 {
219 // handle input device connection
220 case AudioSystem::DEVICE_STATE_AVAILABLE: {
221 if (mAvailableInputDevices & device) {
222 ALOGW("setDeviceConnectionState() device already connected: %d", device);
223 return INVALID_OPERATION;
224 }
225 mAvailableInputDevices = mAvailableInputDevices | (device & ~AUDIO_DEVICE_BIT_IN);
226 }
227 break;
228
229 // handle input device disconnection
230 case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
231 if (!(mAvailableInputDevices & device)) {
232 ALOGW("setDeviceConnectionState() device not connected: %d", device);
233 return INVALID_OPERATION;
234 }
235 mAvailableInputDevices = (audio_devices_t) (mAvailableInputDevices & ~device);
236 } break;
237
238 default:
239 ALOGE("setDeviceConnectionState() invalid state: %x", state);
240 return BAD_VALUE;
241 }
242
243 audio_io_handle_t activeInput = getActiveInput();
244 if (activeInput != 0) {
245 AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
246 audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
247 if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) {
248 ALOGV("setDeviceConnectionState() changing device from %x to %x for input %d",
249 inputDesc->mDevice, newDevice, activeInput);
250 inputDesc->mDevice = newDevice;
251 AudioParameter param = AudioParameter();
252 param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
253 mpClientInterface->setParameters(activeInput, param.toString());
254 }
255 }
256
257 return NO_ERROR;
258 }
259
260 ALOGW("setDeviceConnectionState() invalid device: %x", device);
261 return BAD_VALUE;
262}
263
Pavan Chikkalae33427d2014-03-24 17:11:50 +0530264void AudioPolicyManager::setPhoneState(int state)
265{
266 ALOGV("setPhoneState() state %d", state);
267 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
268 if (state < 0 || state >= AudioSystem::NUM_MODES) {
269 ALOGW("setPhoneState() invalid state %d", state);
270 return;
271 }
272
273 if (state == mPhoneState ) {
274 ALOGW("setPhoneState() setting same state %d", state);
275 return;
276 }
277
278 // if leaving call state, handle special case of active streams
279 // pertaining to sonification strategy see handleIncallSonification()
280 if (isInCall()) {
281 ALOGV("setPhoneState() in call state management: new state is %d", state);
282 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
283 handleIncallSonification(stream, false, true);
284 }
285 }
286
287 // store previous phone state for management of sonification strategy below
288 int oldState = mPhoneState;
289 mPhoneState = state;
290 bool force = false;
291
292 // are we entering or starting a call
293 if (!isStateInCall(oldState) && isStateInCall(state)) {
294 ALOGV(" Entering call in setPhoneState()");
295 // force routing command to audio hardware when starting a call
296 // even if no device change is needed
297 force = true;
298 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
299 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
300 sVolumeProfiles[AUDIO_STREAM_VOICE_CALL][j];
301 }
302 } else if (isStateInCall(oldState) && !isStateInCall(state)) {
303 ALOGV(" Exiting call in setPhoneState()");
304 // force routing command to audio hardware when exiting a call
305 // even if no device change is needed
306 force = true;
307 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
308 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
309 sVolumeProfiles[AUDIO_STREAM_DTMF][j];
310 }
311 } else if (isStateInCall(state) && (state != oldState)) {
312 ALOGV(" Switching between telephony and VoIP in setPhoneState()");
313 // force routing command to audio hardware when switching between telephony and VoIP
314 // even if no device change is needed
315 force = true;
316 }
317
318 // check for device and output changes triggered by new phone state
319 newDevice = getNewDevice(mPrimaryOutput, false /*fromCache*/);
320 checkA2dpSuspend();
321 checkOutputForAllStrategies();
322 updateDevicesAndOutputs();
323
324 AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
325
326 // force routing command to audio hardware when ending call
327 // even if no device change is needed
328 if (isStateInCall(oldState) && newDevice == AUDIO_DEVICE_NONE) {
329 newDevice = hwOutputDesc->device();
330 }
331
332 int delayMs = 0;
333 if (isStateInCall(state)) {
334 nsecs_t sysTime = systemTime();
335 for (size_t i = 0; i < mOutputs.size(); i++) {
336 AudioOutputDescriptor *desc = mOutputs.valueAt(i);
337 // mute media and sonification strategies and delay device switch by the largest
338 // latency of any output where either strategy is active.
339 // This avoid sending the ring tone or music tail into the earpiece or headset.
340 if ((desc->isStrategyActive(STRATEGY_MEDIA,
341 SONIFICATION_HEADSET_MUSIC_DELAY,
342 sysTime) ||
343 desc->isStrategyActive(STRATEGY_SONIFICATION,
344 SONIFICATION_HEADSET_MUSIC_DELAY,
345 sysTime)) &&
346 (delayMs < (int)desc->mLatency*2)) {
347 delayMs = desc->mLatency*2;
348 }
349 setStrategyMute(STRATEGY_MEDIA, true, mOutputs.keyAt(i));
350 setStrategyMute(STRATEGY_MEDIA, false, mOutputs.keyAt(i), MUTE_TIME_MS,
351 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
352 setStrategyMute(STRATEGY_SONIFICATION, true, mOutputs.keyAt(i));
353 setStrategyMute(STRATEGY_SONIFICATION, false, mOutputs.keyAt(i), MUTE_TIME_MS,
354 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
355 }
356 }
357
358 // change routing is necessary
359 setOutputDevice(mPrimaryOutput, newDevice, force, delayMs);
360
361 //update device for all non-primary outputs
362 for (size_t i = 0; i < mOutputs.size(); i++) {
363 audio_io_handle_t output = mOutputs.keyAt(i);
364 if (output != mPrimaryOutput) {
365 newDevice = getNewDevice(output, false /*fromCache*/);
366 setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE));
367 }
368 }
369
370 // if entering in call state, handle special case of active streams
371 // pertaining to sonification strategy see handleIncallSonification()
372 if (isStateInCall(state)) {
373 ALOGV("setPhoneState() in call state management: new state is %d", state);
374 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
375 handleIncallSonification(stream, true, true);
376 }
377 }
378
379 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
380 if (state == AudioSystem::MODE_RINGTONE &&
381 isStreamActive(AudioSystem::MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
382 mLimitRingtoneVolume = true;
383 } else {
384 mLimitRingtoneVolume = false;
385 }
386}
387
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700388void AudioPolicyManager::setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config)
389{
390 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState);
391
392 bool forceVolumeReeval = false;
393 switch(usage) {
394 case AudioSystem::FOR_COMMUNICATION:
395 if (config != AudioSystem::FORCE_SPEAKER && config != AudioSystem::FORCE_BT_SCO &&
396 config != AudioSystem::FORCE_NONE) {
397 ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
398 return;
399 }
400 forceVolumeReeval = true;
401 mForceUse[usage] = config;
402 break;
403 case AudioSystem::FOR_MEDIA:
404 if (config != AudioSystem::FORCE_HEADPHONES && config != AudioSystem::FORCE_BT_A2DP &&
405#ifdef AUDIO_EXTN_FM_ENABLED
406 config != AudioSystem::FORCE_SPEAKER &&
407#endif
408 config != AudioSystem::FORCE_WIRED_ACCESSORY &&
409 config != AudioSystem::FORCE_ANALOG_DOCK &&
410 config != AudioSystem::FORCE_DIGITAL_DOCK && config != AudioSystem::FORCE_NONE &&
411 config != AudioSystem::FORCE_NO_BT_A2DP) {
412 ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
413 return;
414 }
415 mForceUse[usage] = config;
416 break;
417 case AudioSystem::FOR_RECORD:
418 if (config != AudioSystem::FORCE_BT_SCO && config != AudioSystem::FORCE_WIRED_ACCESSORY &&
419 config != AudioSystem::FORCE_NONE) {
420 ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
421 return;
422 }
423 mForceUse[usage] = config;
424 break;
425 case AudioSystem::FOR_DOCK:
426 if (config != AudioSystem::FORCE_NONE && config != AudioSystem::FORCE_BT_CAR_DOCK &&
427 config != AudioSystem::FORCE_BT_DESK_DOCK &&
428 config != AudioSystem::FORCE_WIRED_ACCESSORY &&
429 config != AudioSystem::FORCE_ANALOG_DOCK &&
430 config != AudioSystem::FORCE_DIGITAL_DOCK) {
431 ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
432 }
433 forceVolumeReeval = true;
434 mForceUse[usage] = config;
435 break;
436 case AudioSystem::FOR_SYSTEM:
437 if (config != AudioSystem::FORCE_NONE &&
438 config != AudioSystem::FORCE_SYSTEM_ENFORCED) {
439 ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
440 }
441 forceVolumeReeval = true;
442 mForceUse[usage] = config;
443 break;
444 default:
445 ALOGW("setForceUse() invalid usage %d", usage);
446 break;
447 }
448
449 // check for device and output changes triggered by new force usage
450 checkA2dpSuspend();
451 checkOutputForAllStrategies();
452 updateDevicesAndOutputs();
453 for (int i = mOutputs.size() -1; i >= 0; i--) {
454 audio_io_handle_t output = mOutputs.keyAt(i);
455 audio_devices_t newDevice = getNewDevice(output, true /*fromCache*/);
456 setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE));
457 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
458 applyStreamVolumes(output, newDevice, 0, true);
459 }
460 }
461
462 audio_io_handle_t activeInput = getActiveInput();
463 if (activeInput != 0) {
464 AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
465 audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
466 if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) {
467 ALOGV("setForceUse() changing device from %x to %x for input %d",
468 inputDesc->mDevice, newDevice, activeInput);
469 inputDesc->mDevice = newDevice;
470 AudioParameter param = AudioParameter();
471 param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
472 mpClientInterface->setParameters(activeInput, param.toString());
473 }
474 }
475
476}
477
Pavan Chikkalae33427d2014-03-24 17:11:50 +0530478status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
479 AudioSystem::stream_type stream,
480 int session)
481{
482 ALOGV("startOutput() output %d, stream %d, session %d", output, stream, session);
483 ssize_t index = mOutputs.indexOfKey(output);
484 if (index < 0) {
485 ALOGW("startOutput() unknow output %d", output);
486 return BAD_VALUE;
487 }
488
489 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
490
491 // increment usage count for this stream on the requested output:
492 // NOTE that the usage count is the same for duplicated output and hardware output which is
493 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
494 outputDesc->changeRefCount(stream, 1);
495
496 if (outputDesc->mRefCount[stream] == 1) {
497 audio_devices_t newDevice = getNewDevice(output, false /*fromCache*/);
498 routing_strategy strategy = getStrategy(stream);
499 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
500 (strategy == STRATEGY_SONIFICATION_RESPECTFUL);
501 uint32_t waitMs = 0;
502 bool force = false;
503 for (size_t i = 0; i < mOutputs.size(); i++) {
504 AudioOutputDescriptor *desc = mOutputs.valueAt(i);
505 if (desc != outputDesc) {
506 // force a device change if any other output is managed by the same hw
507 // module and has a current device selection that differs from selected device.
508 // In this case, the audio HAL must receive the new device selection so that it can
509 // change the device currently selected by the other active output.
510 if (outputDesc->sharesHwModuleWith(desc) &&
511 desc->device() != newDevice) {
512 force = true;
513 }
514 // wait for audio on other active outputs to be presented when starting
515 // a notification so that audio focus effect can propagate.
516 uint32_t latency = desc->latency();
517 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
518 waitMs = latency;
519 }
520 }
521 }
522 uint32_t muteWaitMs = setOutputDevice(output, newDevice, force);
523
524 // handle special case for sonification while in call
525 if (isInCall()) {
526 handleIncallSonification(stream, true, false);
527 }
528
529 // apply volume rules for current stream and device if necessary
530 checkAndSetVolume(stream,
531 mStreams[stream].getVolumeIndex(newDevice),
532 output,
533 newDevice);
534
535 // update the outputs if starting an output with a stream that can affect notification
536 // routing
537 handleNotificationRoutingForStream(stream);
538 if (waitMs > muteWaitMs) {
539 usleep((waitMs - muteWaitMs) * 2 * 1000);
540 }
541 }
542#ifdef DOLBY_UDC
543 // It is observed that in some use-cases where both outputs are present eg. bluetooth and headphone,
544 // the output for particular stream type is decided in this routine. Hence we must call
545 // getDeviceForStrategy in order to get the current active output for this stream type and update
546 // the dolby system property.
547 if (stream == AudioSystem::MUSIC)
548 {
549 audio_devices_t audioOutputDevice = getDeviceForStrategy(getStrategy(AudioSystem::MUSIC), true);
550 DolbySystemProperty::set(audioOutputDevice);
551 }
552#endif // DOLBY_END
553 return NO_ERROR;
554}
555
556
557status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
558 AudioSystem::stream_type stream,
559 int session)
560{
561 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
562 ssize_t index = mOutputs.indexOfKey(output);
563 if (index < 0) {
564 ALOGW("stopOutput() unknow output %d", output);
565 return BAD_VALUE;
566 }
567
568 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
569
570 // handle special case for sonification while in call
571 if (isInCall()) {
572 handleIncallSonification(stream, false, false);
573 }
574
575 if (outputDesc->mRefCount[stream] > 0) {
576 // decrement usage count of this stream on the output
577 outputDesc->changeRefCount(stream, -1);
578 // store time at which the stream was stopped - see isStreamActive()
579 if (outputDesc->mRefCount[stream] == 0) {
580 outputDesc->mStopTime[stream] = systemTime();
581 audio_devices_t newDevice = getNewDevice(output, false /*fromCache*/);
582 // delay the device switch by twice the latency because stopOutput() is executed when
583 // the track stop() command is received and at that time the audio track buffer can
584 // still contain data that needs to be drained. The latency only covers the audio HAL
585 // and kernel buffers. Also the latency does not always include additional delay in the
586 // audio path (audio DSP, CODEC ...)
587 setOutputDevice(output, newDevice, false, outputDesc->mLatency*2);
588
589 // force restoring the device selection on other active outputs if it differs from the
590 // one being selected for this output
591 for (size_t i = 0; i < mOutputs.size(); i++) {
592 audio_io_handle_t curOutput = mOutputs.keyAt(i);
593 AudioOutputDescriptor *desc = mOutputs.valueAt(i);
594 if (curOutput != output &&
595 desc->isActive() &&
596 outputDesc->sharesHwModuleWith(desc) &&
597 (newDevice != desc->device())) {
598 setOutputDevice(curOutput,
599 getNewDevice(curOutput, false /*fromCache*/),
600 true,
601 outputDesc->mLatency*2);
602 }
603 }
604 // update the outputs if stopping one with a stream that can affect notification routing
605 handleNotificationRoutingForStream(stream);
606 }
607 return NO_ERROR;
608 } else {
609 ALOGW("stopOutput() refcount is already 0 for output %d", output);
610 return INVALID_OPERATION;
611 }
612}
613
614audio_devices_t AudioPolicyManager::getNewDevice(audio_io_handle_t output, bool fromCache)
615{
616 audio_devices_t device = AUDIO_DEVICE_NONE;
617
618 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
619 AudioOutputDescriptor *primaryOutputDesc = mOutputs.valueFor(mPrimaryOutput);
620 // check the following by order of priority to request a routing change if necessary:
621 // 1: the strategy enforced audible is active on the output:
622 // use device for strategy enforced audible
623 // 2: we are in call or the strategy phone is active on the output:
624 // use device for strategy phone
625 // 3: the strategy sonification is active on the output:
626 // use device for strategy sonification
627 // 4: the strategy "respectful" sonification is active on the output:
628 // use device for strategy "respectful" sonification
629 // 5: the strategy media is active on the output:
630 // use device for strategy media
631 // 6: the strategy DTMF is active on the output:
632 // use device for strategy DTMF
633 if (outputDesc->isStrategyActive(STRATEGY_ENFORCED_AUDIBLE)) {
634 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
635 } else if (isInCall() ||
636 outputDesc->isStrategyActive(STRATEGY_PHONE)) {
637 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
638 } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION)||
639 (primaryOutputDesc->isStrategyActive(STRATEGY_SONIFICATION)&& !primaryOutputDesc->isStrategyActive(STRATEGY_MEDIA))){
640 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
641 } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION_RESPECTFUL)) {
642 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
643 } else if (outputDesc->isStrategyActive(STRATEGY_MEDIA)) {
644 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
645 } else if (outputDesc->isStrategyActive(STRATEGY_DTMF)) {
646 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
647 }
648
649 ALOGV("getNewDevice() selected device %x", device);
650 return device;
651}
652
653//private function, no changes from AudioPolicyManagerBase
654void AudioPolicyManager::handleNotificationRoutingForStream(AudioSystem::stream_type stream) {
655 switch(stream) {
656 case AudioSystem::MUSIC:
657 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
658 updateDevicesAndOutputs();
659 break;
660 default:
661 break;
662 }
663}
664
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700665audio_io_handle_t AudioPolicyManager::getInput(int inputSource,
666 uint32_t samplingRate,
667 uint32_t format,
668 uint32_t channelMask,
669 AudioSystem::audio_in_acoustics acoustics)
670{
671 audio_io_handle_t input = 0;
672 audio_devices_t device = getDeviceForInputSource(inputSource);
673
674 ALOGV("getInput() inputSource %d, samplingRate %d, format %d, channelMask %x, acoustics %x",
675 inputSource, samplingRate, format, channelMask, acoustics);
676
677 if (device == AUDIO_DEVICE_NONE) {
678 ALOGW("getInput() could not find device for inputSource %d", inputSource);
679 return 0;
680 }
681
682
683 IOProfile *profile = getInputProfile(device,
684 samplingRate,
685 format,
686 channelMask);
687 if (profile == NULL) {
688 ALOGW("getInput() could not find profile for device %04x, samplingRate %d, format %d,"
689 "channelMask %04x",
690 device, samplingRate, format, channelMask);
691 return 0;
692 }
693
694 if (profile->mModule->mHandle == 0) {
695 ALOGE("getInput(): HW module %s not opened", profile->mModule->mName);
696 return 0;
697 }
698
699 AudioInputDescriptor *inputDesc = new AudioInputDescriptor(profile);
700
701 inputDesc->mInputSource = inputSource;
702 inputDesc->mDevice = device;
703 inputDesc->mSamplingRate = samplingRate;
704 inputDesc->mFormat = (audio_format_t)format;
705 inputDesc->mChannelMask = (audio_channel_mask_t)channelMask;
706 inputDesc->mRefCount = 0;
707 input = mpClientInterface->openInput(profile->mModule->mHandle,
708 &inputDesc->mDevice,
709 &inputDesc->mSamplingRate,
710 &inputDesc->mFormat,
711 &inputDesc->mChannelMask);
712
713 // only accept input with the exact requested set of parameters
714 if (input == 0 ||
715 (samplingRate != inputDesc->mSamplingRate) ||
716 (format != inputDesc->mFormat) ||
717 (channelMask != inputDesc->mChannelMask)) {
718 ALOGV("getInput() failed opening input: samplingRate %d, format %d, channelMask %d",
719 samplingRate, format, channelMask);
720 if (input != 0) {
721 mpClientInterface->closeInput(input);
722 }
723 delete inputDesc;
724 return 0;
725 }
726 mInputs.add(input, inputDesc);
727 return input;
728}
729
730AudioPolicyManager::routing_strategy AudioPolicyManager::getStrategy(AudioSystem::stream_type stream)
731{
Pavan Chikkalae33427d2014-03-24 17:11:50 +0530732 // stream to strategy mapping
733 switch (stream) {
734 case AudioSystem::VOICE_CALL:
735 case AudioSystem::BLUETOOTH_SCO:
736 return STRATEGY_PHONE;
737 case AudioSystem::RING:
738 case AudioSystem::ALARM:
739 return STRATEGY_SONIFICATION;
740 case AudioSystem::NOTIFICATION:
741 return STRATEGY_SONIFICATION_RESPECTFUL;
742 case AudioSystem::DTMF:
743 return STRATEGY_DTMF;
744 default:
745 ALOGE("unknown stream type");
746 case AudioSystem::SYSTEM:
747 // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
748 // while key clicks are played produces a poor result
749 case AudioSystem::TTS:
750 case AudioSystem::MUSIC:
751#ifdef AUDIO_EXTN_INCALL_MUSIC_ENABLED
752 case AudioSystem::INCALL_MUSIC:
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700753#endif
Pavan Chikkalae33427d2014-03-24 17:11:50 +0530754 return STRATEGY_MEDIA;
755 case AudioSystem::ENFORCED_AUDIBLE:
756 return STRATEGY_ENFORCED_AUDIBLE;
757 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700758}
759
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700760audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
761 bool fromCache)
762{
763 uint32_t device = AUDIO_DEVICE_NONE;
764
765 if (fromCache) {
766 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
767 strategy, mDeviceForStrategy[strategy]);
768 return mDeviceForStrategy[strategy];
769 }
770
771 switch (strategy) {
772
773 case STRATEGY_SONIFICATION_RESPECTFUL:
774 if (isInCall()) {
775 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
776 } else if (isStreamActiveRemotely(AudioSystem::MUSIC,
777 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
778 // while media is playing on a remote device, use the the sonification behavior.
779 // Note that we test this usecase before testing if media is playing because
780 // the isStreamActive() method only informs about the activity of a stream, not
781 // if it's for local playback. Note also that we use the same delay between both tests
782 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
783 } else if (isStreamActive(AudioSystem::MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
784 // while media is playing (or has recently played), use the same device
785 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
786 } else {
787 // when media is not playing anymore, fall back on the sonification behavior
788 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
789 }
790
791 break;
792
793 case STRATEGY_DTMF:
794 if (!isInCall()) {
795 // when off call, DTMF strategy follows the same rules as MEDIA strategy
796 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
797 break;
798 }
799 // when in call, DTMF and PHONE strategies follow the same rules
800 // FALL THROUGH
801
802 case STRATEGY_PHONE:
803 // for phone strategy, we first consider the forced use and then the available devices by order
804 // of priority
805 switch (mForceUse[AudioSystem::FOR_COMMUNICATION]) {
806 case AudioSystem::FORCE_BT_SCO:
807 if (!isInCall() || strategy != STRATEGY_DTMF) {
808 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
809 if (device) break;
810 }
811 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
812 if (device) break;
813 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
814 if (device) break;
815 // if SCO device is requested but no SCO device is available, fall back to default case
816 // FALL THROUGH
817
818 default: // FORCE_NONE
819 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
820 if (mHasA2dp && !isInCall() &&
821 (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
822 (getA2dpOutput() != 0) && !mA2dpSuspended) {
823 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
824 if (device) break;
825 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
826 if (device) break;
827 }
828 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
829 if (device) break;
830 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
831 if (device) break;
832 if (mPhoneState != AudioSystem::MODE_IN_CALL) {
833 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
834 if (device) break;
835 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
836 if (device) break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700837 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
838 if (device) break;
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700839 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
840 if (device) break;
841 }
842
843 // Allow voice call on USB ANLG DOCK headset
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700844 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
845 if (device) break;
846
847 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_EARPIECE;
848 if (device) break;
849 device = mDefaultOutputDevice;
850 if (device == AUDIO_DEVICE_NONE) {
851 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE");
852 }
853 break;
854
855 case AudioSystem::FORCE_SPEAKER:
856 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
857 // A2DP speaker when forcing to speaker output
858 if (mHasA2dp && !isInCall() &&
859 (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
860 (getA2dpOutput() != 0) && !mA2dpSuspended) {
861 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
862 if (device) break;
863 }
864 if (mPhoneState != AudioSystem::MODE_IN_CALL) {
865 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
866 if (device) break;
867 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
868 if (device) break;
869 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
870 if (device) break;
871 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
872 if (device) break;
Helen Zengbbce4952013-12-16 20:26:46 -0800873 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
874 if (device) break;
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700875 }
876 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
877 if (device) break;
878 device = mDefaultOutputDevice;
879 if (device == AUDIO_DEVICE_NONE) {
880 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER");
881 }
882 break;
883 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700884 // FIXME: Why do need to replace with speaker? If voice call is active
885 // We should use device from STRATEGY_PHONE
886#ifdef AUDIO_EXTN_FM_ENABLED
887 if (mAvailableOutputDevices & AUDIO_DEVICE_OUT_FM) {
888 if (mForceUse[AudioSystem::FOR_MEDIA] == AudioSystem::FORCE_SPEAKER) {
889 device = AUDIO_DEVICE_OUT_SPEAKER;
890 }
891 }
892#endif
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700893 break;
894
895 case STRATEGY_SONIFICATION:
896
897 // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
898 // handleIncallSonification().
899 if (isInCall()) {
900 device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
901 break;
902 }
903 // FALL THROUGH
904
905 case STRATEGY_ENFORCED_AUDIBLE:
906 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
907 // except:
908 // - when in call where it doesn't default to STRATEGY_PHONE behavior
909 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
910
911 if ((strategy == STRATEGY_SONIFICATION) ||
912 (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_SYSTEM_ENFORCED)) {
913 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
914 if (device == AUDIO_DEVICE_NONE) {
915 ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION");
916 }
917 }
918 // The second device used for sonification is the same as the device used by media strategy
919 // FALL THROUGH
920
921 case STRATEGY_MEDIA: {
922 uint32_t device2 = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700923
Mingming Yin4a72d652014-01-03 18:54:18 -0800924 if (isInCall() && (device == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700925 // when in call, get the device for Phone strategy
926 device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
927 break;
928 }
929#ifdef AUDIO_EXTN_FM_ENABLED
930 if (mForceUse[AudioSystem::FOR_MEDIA] == AudioSystem::FORCE_SPEAKER) {
931 device = AUDIO_DEVICE_OUT_SPEAKER;
932 break;
933 }
934#endif
935
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700936 if (strategy != STRATEGY_SONIFICATION) {
937 // no sonification on remote submix (e.g. WFD)
938 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
939 }
940 if ((device2 == AUDIO_DEVICE_NONE) &&
941 mHasA2dp && (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
942 (getA2dpOutput() != 0) && !mA2dpSuspended) {
943 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
944 if (device2 == AUDIO_DEVICE_NONE) {
945 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
946 }
947 if (device2 == AUDIO_DEVICE_NONE) {
948 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
949 }
950 }
951 if (device2 == AUDIO_DEVICE_NONE) {
952 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
953 }
954 if (device2 == AUDIO_DEVICE_NONE) {
955 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
956 }
957 if (device2 == AUDIO_DEVICE_NONE) {
958 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
959 }
960 if (device2 == AUDIO_DEVICE_NONE) {
961 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
962 }
963 if (device2 == AUDIO_DEVICE_NONE) {
964 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
965 }
Apoorv Raghuvanshi8506a982014-01-17 13:10:09 -0800966 if ((strategy != STRATEGY_SONIFICATION) && (device == AUDIO_DEVICE_NONE)
967 && (device2 == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700968 // no sonification on aux digital (e.g. HDMI)
969 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
970 }
971 if ((device2 == AUDIO_DEVICE_NONE) &&
Krishnankutty Kolathappilly4b7fdaa2013-12-16 00:40:11 -0800972 (mForceUse[AudioSystem::FOR_DOCK] == AudioSystem::FORCE_ANALOG_DOCK)
973 && (strategy != STRATEGY_SONIFICATION)) {
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700974 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
975 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700976#ifdef AUDIO_EXTN_FM_ENABLED
Apoorv Raghuvanshi8506a982014-01-17 13:10:09 -0800977 if ((strategy != STRATEGY_SONIFICATION) && (device == AUDIO_DEVICE_NONE)
978 && (device2 == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700979 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_FM_TX;
980 }
981#endif
982#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Apoorv Raghuvanshi8506a982014-01-17 13:10:09 -0800983 if ((strategy != STRATEGY_SONIFICATION) && (device == AUDIO_DEVICE_NONE)
984 && (device2 == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700985 // no sonification on WFD sink
986 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_PROXY;
987 }
988#endif
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700989 if (device2 == AUDIO_DEVICE_NONE) {
990 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
991 }
992
993 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
994 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
995 device |= device2;
996 if (device) break;
997 device = mDefaultOutputDevice;
998 if (device == AUDIO_DEVICE_NONE) {
999 ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA");
1000 }
1001 } break;
1002
1003 default:
1004 ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
1005 break;
1006 }
1007
1008 ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
1009 return device;
1010}
1011
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001012audio_devices_t AudioPolicyManager::getDeviceForInputSource(int inputSource)
1013{
1014 uint32_t device = AUDIO_DEVICE_NONE;
1015
1016 switch (inputSource) {
1017 case AUDIO_SOURCE_VOICE_UPLINK:
1018 if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
1019 device = AUDIO_DEVICE_IN_VOICE_CALL;
1020 break;
1021 }
1022 // FALL THROUGH
1023
1024 case AUDIO_SOURCE_DEFAULT:
1025 case AUDIO_SOURCE_MIC:
1026 case AUDIO_SOURCE_VOICE_RECOGNITION:
1027 case AUDIO_SOURCE_HOTWORD:
1028 case AUDIO_SOURCE_VOICE_COMMUNICATION:
1029 if (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO &&
1030 mAvailableInputDevices & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
1031 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
1032 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_WIRED_HEADSET) {
1033 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
1034 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET) {
1035 device = AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET;
1036 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
1037 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
1038 }
1039 break;
1040 case AUDIO_SOURCE_CAMCORDER:
1041 if (mAvailableInputDevices & AUDIO_DEVICE_IN_BACK_MIC) {
1042 device = AUDIO_DEVICE_IN_BACK_MIC;
1043 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
1044 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
1045 }
1046 break;
1047 case AUDIO_SOURCE_VOICE_DOWNLINK:
1048 case AUDIO_SOURCE_VOICE_CALL:
1049 if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
1050 device = AUDIO_DEVICE_IN_VOICE_CALL;
1051 }
1052 break;
1053 case AUDIO_SOURCE_REMOTE_SUBMIX:
1054 if (mAvailableInputDevices & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
1055 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1056 }
1057 break;
1058#ifdef AUDIO_EXTN_FM_ENABLED
1059 case AUDIO_SOURCE_FM_RX:
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001060 device = AUDIO_DEVICE_IN_FM_RX;
1061 break;
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -07001062 case AUDIO_SOURCE_FM_RX_A2DP:
1063 device = AUDIO_DEVICE_IN_FM_RX_A2DP;
1064 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001065#endif
1066 default:
1067 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
1068 break;
1069 }
1070 ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
1071 return device;
1072}
1073
1074AudioPolicyManager::device_category AudioPolicyManager::getDeviceCategory(audio_devices_t device)
1075{
1076 switch(getDeviceForVolume(device)) {
1077 case AUDIO_DEVICE_OUT_EARPIECE:
1078 return DEVICE_CATEGORY_EARPIECE;
1079 case AUDIO_DEVICE_OUT_WIRED_HEADSET:
1080 case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
1081 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
1082 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
1083 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
1084 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
1085#ifdef AUDIO_EXTN_FM_ENABLED
1086 case AUDIO_DEVICE_OUT_FM:
1087#endif
1088 return DEVICE_CATEGORY_HEADSET;
1089 case AUDIO_DEVICE_OUT_SPEAKER:
1090 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
1091 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
1092 case AUDIO_DEVICE_OUT_AUX_DIGITAL:
1093 case AUDIO_DEVICE_OUT_USB_ACCESSORY:
1094 case AUDIO_DEVICE_OUT_USB_DEVICE:
1095 case AUDIO_DEVICE_OUT_REMOTE_SUBMIX:
1096#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
1097 case AUDIO_DEVICE_OUT_PROXY:
1098#endif
1099 default:
1100 return DEVICE_CATEGORY_SPEAKER;
1101 }
1102}
1103
1104status_t AudioPolicyManager::checkAndSetVolume(int stream,
1105 int index,
1106 audio_io_handle_t output,
1107 audio_devices_t device,
1108 int delayMs,
1109 bool force)
1110{
1111 ALOGV("checkAndSetVolume: index %d output %d device %x", index, output, device);
1112 // do not change actual stream volume if the stream is muted
1113 if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
1114 ALOGVV("checkAndSetVolume() stream %d muted count %d",
1115 stream, mOutputs.valueFor(output)->mMuteCount[stream]);
1116 return NO_ERROR;
1117 }
1118
1119 // do not change in call volume if bluetooth is connected and vice versa
1120 if ((stream == AudioSystem::VOICE_CALL && mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
1121 (stream == AudioSystem::BLUETOOTH_SCO && mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO)) {
1122 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
1123 stream, mForceUse[AudioSystem::FOR_COMMUNICATION]);
1124 return INVALID_OPERATION;
1125 }
1126
1127 float volume = computeVolume(stream, index, output, device);
1128 // We actually change the volume if:
1129 // - the float value returned by computeVolume() changed
1130 // - the force flag is set
1131 if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
1132 force) {
1133 mOutputs.valueFor(output)->mCurVolume[stream] = volume;
1134 ALOGV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
1135 // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
1136 // enabled
1137 if (stream == AudioSystem::BLUETOOTH_SCO) {
1138 mpClientInterface->setStreamVolume(AudioSystem::VOICE_CALL, volume, output, delayMs);
1139#ifdef AUDIO_EXTN_FM_ENABLED
1140 } else if (stream == AudioSystem::MUSIC &&
1141 output == mPrimaryOutput) {
1142 float fmVolume = -1.0;
1143 fmVolume = computeVolume(stream, index, output, device);
1144 if (fmVolume >= 0) {
1145 AudioParameter param = AudioParameter();
1146 param.addFloat(String8("fm_volume"), fmVolume);
1147 ALOGV("checkAndSetVolume setParameters fm_volume, volume=:%f delay=:%d",fmVolume,delayMs*2);
1148 //Double delayMs to avoid sound burst while device switch.
1149 mpClientInterface->setParameters(mPrimaryOutput, param.toString(), delayMs*2);
1150 }
1151#endif
1152 }
1153 mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs);
1154 }
1155
1156 if (stream == AudioSystem::VOICE_CALL ||
1157 stream == AudioSystem::BLUETOOTH_SCO) {
1158 float voiceVolume;
1159
1160 voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
1161
1162 // Force voice volume to max when Vgs is set for bluetooth SCO as volume is managed by the headset
1163 if (stream == AudioSystem::BLUETOOTH_SCO) {
1164 String8 key ("bt_headset_vgs");
1165 mpClientInterface->getParameters(output,key);
1166 AudioParameter result(mpClientInterface->getParameters(0,key));
1167 int value;
1168 if (result.getInt(String8("isVGS"),value) == NO_ERROR) {
1169 ALOGV("Use BT-SCO Voice Volume");
1170 voiceVolume = 1.0;
1171 }
1172 }
1173
1174 if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) {
1175 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
1176 mLastVoiceVolume = voiceVolume;
1177 }
1178 }
1179
1180 return NO_ERROR;
1181}
1182
1183
1184float AudioPolicyManager::computeVolume(int stream,
1185 int index,
1186 audio_io_handle_t output,
1187 audio_devices_t device)
1188{
1189 float volume = 1.0;
1190 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
1191
1192 if (device == AUDIO_DEVICE_NONE) {
1193 device = outputDesc->device();
1194 }
1195
1196 // if volume is not 0 (not muted), force media volume to max on digital output
1197 if (stream == AudioSystem::MUSIC &&
1198 index != mStreams[stream].mIndexMin &&
1199 (device == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
1200 device == AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET ||
1201 device == AUDIO_DEVICE_OUT_USB_ACCESSORY ||
1202#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
1203 device == AUDIO_DEVICE_OUT_PROXY ||
1204#endif
1205 device == AUDIO_DEVICE_OUT_USB_DEVICE )) {
1206 return 1.0;
1207 }
1208#ifdef AUDIO_EXTN_INCALL_MUSIC_ENABLED
1209 if (stream == AudioSystem::INCALL_MUSIC) {
1210 return 1.0;
1211 }
1212#endif
1213 return AudioPolicyManagerBase::computeVolume(stream, index, output, device);
1214}
ApurupaPattapu0c566872014-01-10 14:46:02 -08001215
1216// This function checks for the parameters which can be offloaded.
1217// This can be enhanced depending on the capability of the DSP and policy
1218// of the system.
1219bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
1220{
1221 ALOGV(" isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
1222 " BitRate=%u, duration=%lld us, has_video=%d",
1223 offloadInfo.sample_rate, offloadInfo.channel_mask,
1224 offloadInfo.format,
1225 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
1226 offloadInfo.has_video);
1227
1228#ifdef VOICE_CONCURRENCY
1229 if(isInCall())
1230 {
1231 ALOGD("\n blocking compress offload on call mode\n");
1232 return false;
1233 }
1234#endif
1235 // Check if stream type is music, then only allow offload as of now.
1236 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
1237 {
1238 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
1239 return false;
1240 }
1241
1242 char propValue[PROPERTY_VALUE_MAX];
1243 bool pcmOffload = false;
1244 if (audio_is_offload_pcm(offloadInfo.format)) {
1245 if(property_get("audio.offload.pcm.enable", propValue, NULL)) {
1246 bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1247 if (prop_enabled) {
1248 ALOGW("PCM offload property is enabled");
1249 pcmOffload = true;
1250 }
1251 }
1252 if (!pcmOffload) {
1253 ALOGV("PCM offload disabled by property audio.offload.pcm.enable");
1254 return false;
1255 }
1256 }
1257
1258 if (!pcmOffload) {
1259 // Check if offload has been disabled
1260 if (property_get("audio.offload.disable", propValue, "0")) {
1261 if (atoi(propValue) != 0) {
1262 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
1263 return false;
1264 }
1265 }
1266
1267 //check if it's multi-channel AAC format
1268 if (AudioSystem::popCount(offloadInfo.channel_mask) > 2
1269 && offloadInfo.format == AUDIO_FORMAT_AAC) {
1270 ALOGV("offload disabled for multi-channel AAC format");
1271 return false;
1272 }
1273
1274 if (offloadInfo.has_video)
1275 {
1276 if(property_get("av.offload.enable", propValue, NULL)) {
1277 bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1278 if (!prop_enabled) {
1279 ALOGW("offload disabled by av.offload.enable = %s ", propValue );
1280 return false;
1281 }
1282 } else {
1283 return false;
1284 }
1285
1286 if(offloadInfo.is_streaming) {
1287 if (property_get("av.streaming.offload.enable", propValue, NULL)) {
1288 bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1289 if (!prop_enabled) {
1290 ALOGW("offload disabled by av.streaming.offload.enable = %s ", propValue );
1291 return false;
1292 }
1293 } else {
1294 //Do not offload AV streamnig if the property is not defined
1295 return false;
1296 }
1297 }
1298 ALOGV("isOffloadSupported: has_video == true, property\
1299 set to enable offload");
1300 }
1301 }
1302
1303 //If duration is less than minimum value defined in property, return false
1304 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
1305 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
1306 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
1307 return false;
1308 }
1309 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
1310 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
1311 //duration checks only valid for MP3/AAC formats,
1312 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
1313 if (offloadInfo.format == AUDIO_FORMAT_MP3 || offloadInfo.format == AUDIO_FORMAT_AAC || pcmOffload)
1314 return false;
1315 }
1316
1317 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1318 // creating an offloaded track and tearing it down immediately after start when audioflinger
1319 // detects there is an active non offloadable effect.
1320 // FIXME: We should check the audio session here but we do not have it in this context.
1321 // This may prevent offloading in rare situations where effects are left active by apps
1322 // in the background.
1323 if (isNonOffloadableEffectEnabled()) {
1324 return false;
1325 }
1326
1327 // See if there is a profile to support this.
1328 // AUDIO_DEVICE_NONE
1329 IOProfile *profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
1330 offloadInfo.sample_rate,
1331 offloadInfo.format,
1332 offloadInfo.channel_mask,
1333 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1334 ALOGV("isOffloadSupported() profile %sfound", profile != NULL ? "" : "NOT ");
1335 return (profile != NULL);
1336}
1337
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001338extern "C" AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001339{
1340 return new AudioPolicyManager(clientInterface);
1341}
1342
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001343extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001344{
1345 delete interface;
1346}
1347
1348}; // namespace android