blob: f64bbfe84b4aba2ea44218571fd3df0d1446e6de [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
264void AudioPolicyManager::setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config)
265{
266 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState);
267
268 bool forceVolumeReeval = false;
269 switch(usage) {
270 case AudioSystem::FOR_COMMUNICATION:
271 if (config != AudioSystem::FORCE_SPEAKER && config != AudioSystem::FORCE_BT_SCO &&
272 config != AudioSystem::FORCE_NONE) {
273 ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
274 return;
275 }
276 forceVolumeReeval = true;
277 mForceUse[usage] = config;
278 break;
279 case AudioSystem::FOR_MEDIA:
280 if (config != AudioSystem::FORCE_HEADPHONES && config != AudioSystem::FORCE_BT_A2DP &&
281#ifdef AUDIO_EXTN_FM_ENABLED
282 config != AudioSystem::FORCE_SPEAKER &&
283#endif
284 config != AudioSystem::FORCE_WIRED_ACCESSORY &&
285 config != AudioSystem::FORCE_ANALOG_DOCK &&
286 config != AudioSystem::FORCE_DIGITAL_DOCK && config != AudioSystem::FORCE_NONE &&
287 config != AudioSystem::FORCE_NO_BT_A2DP) {
288 ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
289 return;
290 }
291 mForceUse[usage] = config;
292 break;
293 case AudioSystem::FOR_RECORD:
294 if (config != AudioSystem::FORCE_BT_SCO && config != AudioSystem::FORCE_WIRED_ACCESSORY &&
295 config != AudioSystem::FORCE_NONE) {
296 ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
297 return;
298 }
299 mForceUse[usage] = config;
300 break;
301 case AudioSystem::FOR_DOCK:
302 if (config != AudioSystem::FORCE_NONE && config != AudioSystem::FORCE_BT_CAR_DOCK &&
303 config != AudioSystem::FORCE_BT_DESK_DOCK &&
304 config != AudioSystem::FORCE_WIRED_ACCESSORY &&
305 config != AudioSystem::FORCE_ANALOG_DOCK &&
306 config != AudioSystem::FORCE_DIGITAL_DOCK) {
307 ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
308 }
309 forceVolumeReeval = true;
310 mForceUse[usage] = config;
311 break;
312 case AudioSystem::FOR_SYSTEM:
313 if (config != AudioSystem::FORCE_NONE &&
314 config != AudioSystem::FORCE_SYSTEM_ENFORCED) {
315 ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
316 }
317 forceVolumeReeval = true;
318 mForceUse[usage] = config;
319 break;
320 default:
321 ALOGW("setForceUse() invalid usage %d", usage);
322 break;
323 }
324
325 // check for device and output changes triggered by new force usage
326 checkA2dpSuspend();
327 checkOutputForAllStrategies();
328 updateDevicesAndOutputs();
329 for (int i = mOutputs.size() -1; i >= 0; i--) {
330 audio_io_handle_t output = mOutputs.keyAt(i);
331 audio_devices_t newDevice = getNewDevice(output, true /*fromCache*/);
332 setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE));
333 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
334 applyStreamVolumes(output, newDevice, 0, true);
335 }
336 }
337
338 audio_io_handle_t activeInput = getActiveInput();
339 if (activeInput != 0) {
340 AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
341 audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
342 if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) {
343 ALOGV("setForceUse() changing device from %x to %x for input %d",
344 inputDesc->mDevice, newDevice, activeInput);
345 inputDesc->mDevice = newDevice;
346 AudioParameter param = AudioParameter();
347 param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
348 mpClientInterface->setParameters(activeInput, param.toString());
349 }
350 }
351
352}
353
354audio_io_handle_t AudioPolicyManager::getInput(int inputSource,
355 uint32_t samplingRate,
356 uint32_t format,
357 uint32_t channelMask,
358 AudioSystem::audio_in_acoustics acoustics)
359{
360 audio_io_handle_t input = 0;
361 audio_devices_t device = getDeviceForInputSource(inputSource);
362
363 ALOGV("getInput() inputSource %d, samplingRate %d, format %d, channelMask %x, acoustics %x",
364 inputSource, samplingRate, format, channelMask, acoustics);
365
366 if (device == AUDIO_DEVICE_NONE) {
367 ALOGW("getInput() could not find device for inputSource %d", inputSource);
368 return 0;
369 }
370
371
372 IOProfile *profile = getInputProfile(device,
373 samplingRate,
374 format,
375 channelMask);
376 if (profile == NULL) {
377 ALOGW("getInput() could not find profile for device %04x, samplingRate %d, format %d,"
378 "channelMask %04x",
379 device, samplingRate, format, channelMask);
380 return 0;
381 }
382
383 if (profile->mModule->mHandle == 0) {
384 ALOGE("getInput(): HW module %s not opened", profile->mModule->mName);
385 return 0;
386 }
387
388 AudioInputDescriptor *inputDesc = new AudioInputDescriptor(profile);
389
390 inputDesc->mInputSource = inputSource;
391 inputDesc->mDevice = device;
392 inputDesc->mSamplingRate = samplingRate;
393 inputDesc->mFormat = (audio_format_t)format;
394 inputDesc->mChannelMask = (audio_channel_mask_t)channelMask;
395 inputDesc->mRefCount = 0;
396 input = mpClientInterface->openInput(profile->mModule->mHandle,
397 &inputDesc->mDevice,
398 &inputDesc->mSamplingRate,
399 &inputDesc->mFormat,
400 &inputDesc->mChannelMask);
401
402 // only accept input with the exact requested set of parameters
403 if (input == 0 ||
404 (samplingRate != inputDesc->mSamplingRate) ||
405 (format != inputDesc->mFormat) ||
406 (channelMask != inputDesc->mChannelMask)) {
407 ALOGV("getInput() failed opening input: samplingRate %d, format %d, channelMask %d",
408 samplingRate, format, channelMask);
409 if (input != 0) {
410 mpClientInterface->closeInput(input);
411 }
412 delete inputDesc;
413 return 0;
414 }
415 mInputs.add(input, inputDesc);
416 return input;
417}
418
419AudioPolicyManager::routing_strategy AudioPolicyManager::getStrategy(AudioSystem::stream_type stream)
420{
421#ifdef QCOM_INCALL_MUSIC_ENABLED
422 if (stream == AudioSystem::INCALL_MUSIC)
423 return STRATEGY_MEDIA;
424#endif
425
426 return getStrategy(stream);
427}
428
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700429audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
430 bool fromCache)
431{
432 uint32_t device = AUDIO_DEVICE_NONE;
433
434 if (fromCache) {
435 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
436 strategy, mDeviceForStrategy[strategy]);
437 return mDeviceForStrategy[strategy];
438 }
439
440 switch (strategy) {
441
442 case STRATEGY_SONIFICATION_RESPECTFUL:
443 if (isInCall()) {
444 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
445 } else if (isStreamActiveRemotely(AudioSystem::MUSIC,
446 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
447 // while media is playing on a remote device, use the the sonification behavior.
448 // Note that we test this usecase before testing if media is playing because
449 // the isStreamActive() method only informs about the activity of a stream, not
450 // if it's for local playback. Note also that we use the same delay between both tests
451 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
452 } else if (isStreamActive(AudioSystem::MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
453 // while media is playing (or has recently played), use the same device
454 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
455 } else {
456 // when media is not playing anymore, fall back on the sonification behavior
457 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
458 }
459
460 break;
461
462 case STRATEGY_DTMF:
463 if (!isInCall()) {
464 // when off call, DTMF strategy follows the same rules as MEDIA strategy
465 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
466 break;
467 }
468 // when in call, DTMF and PHONE strategies follow the same rules
469 // FALL THROUGH
470
471 case STRATEGY_PHONE:
472 // for phone strategy, we first consider the forced use and then the available devices by order
473 // of priority
474 switch (mForceUse[AudioSystem::FOR_COMMUNICATION]) {
475 case AudioSystem::FORCE_BT_SCO:
476 if (!isInCall() || strategy != STRATEGY_DTMF) {
477 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
478 if (device) break;
479 }
480 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
481 if (device) break;
482 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
483 if (device) break;
484 // if SCO device is requested but no SCO device is available, fall back to default case
485 // FALL THROUGH
486
487 default: // FORCE_NONE
488 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
489 if (mHasA2dp && !isInCall() &&
490 (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
491 (getA2dpOutput() != 0) && !mA2dpSuspended) {
492 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
493 if (device) break;
494 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
495 if (device) break;
496 }
497 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
498 if (device) break;
499 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
500 if (device) break;
501 if (mPhoneState != AudioSystem::MODE_IN_CALL) {
502 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
503 if (device) break;
504 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
505 if (device) break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700506 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
507 if (device) break;
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700508 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
509 if (device) break;
510 }
511
512 // Allow voice call on USB ANLG DOCK headset
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700513 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
514 if (device) break;
515
516 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_EARPIECE;
517 if (device) break;
518 device = mDefaultOutputDevice;
519 if (device == AUDIO_DEVICE_NONE) {
520 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE");
521 }
522 break;
523
524 case AudioSystem::FORCE_SPEAKER:
525 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
526 // A2DP speaker when forcing to speaker output
527 if (mHasA2dp && !isInCall() &&
528 (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
529 (getA2dpOutput() != 0) && !mA2dpSuspended) {
530 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
531 if (device) break;
532 }
533 if (mPhoneState != AudioSystem::MODE_IN_CALL) {
534 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
535 if (device) break;
536 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
537 if (device) break;
538 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
539 if (device) break;
540 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
541 if (device) break;
Helen Zengbbce4952013-12-16 20:26:46 -0800542 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
543 if (device) break;
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700544 }
545 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
546 if (device) break;
547 device = mDefaultOutputDevice;
548 if (device == AUDIO_DEVICE_NONE) {
549 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER");
550 }
551 break;
552 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700553 // FIXME: Why do need to replace with speaker? If voice call is active
554 // We should use device from STRATEGY_PHONE
555#ifdef AUDIO_EXTN_FM_ENABLED
556 if (mAvailableOutputDevices & AUDIO_DEVICE_OUT_FM) {
557 if (mForceUse[AudioSystem::FOR_MEDIA] == AudioSystem::FORCE_SPEAKER) {
558 device = AUDIO_DEVICE_OUT_SPEAKER;
559 }
560 }
561#endif
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700562 break;
563
564 case STRATEGY_SONIFICATION:
565
566 // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
567 // handleIncallSonification().
568 if (isInCall()) {
569 device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
570 break;
571 }
572 // FALL THROUGH
573
574 case STRATEGY_ENFORCED_AUDIBLE:
575 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
576 // except:
577 // - when in call where it doesn't default to STRATEGY_PHONE behavior
578 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
579
580 if ((strategy == STRATEGY_SONIFICATION) ||
581 (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_SYSTEM_ENFORCED)) {
582 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
583 if (device == AUDIO_DEVICE_NONE) {
584 ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION");
585 }
586 }
587 // The second device used for sonification is the same as the device used by media strategy
588 // FALL THROUGH
589
590 case STRATEGY_MEDIA: {
591 uint32_t device2 = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700592
Mingming Yin4a72d652014-01-03 18:54:18 -0800593 if (isInCall() && (device == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700594 // when in call, get the device for Phone strategy
595 device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
596 break;
597 }
598#ifdef AUDIO_EXTN_FM_ENABLED
599 if (mForceUse[AudioSystem::FOR_MEDIA] == AudioSystem::FORCE_SPEAKER) {
600 device = AUDIO_DEVICE_OUT_SPEAKER;
601 break;
602 }
603#endif
604
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700605 if (strategy != STRATEGY_SONIFICATION) {
606 // no sonification on remote submix (e.g. WFD)
607 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
608 }
609 if ((device2 == AUDIO_DEVICE_NONE) &&
610 mHasA2dp && (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
611 (getA2dpOutput() != 0) && !mA2dpSuspended) {
612 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
613 if (device2 == AUDIO_DEVICE_NONE) {
614 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
615 }
616 if (device2 == AUDIO_DEVICE_NONE) {
617 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
618 }
619 }
620 if (device2 == AUDIO_DEVICE_NONE) {
621 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
622 }
623 if (device2 == AUDIO_DEVICE_NONE) {
624 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
625 }
626 if (device2 == AUDIO_DEVICE_NONE) {
627 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
628 }
629 if (device2 == AUDIO_DEVICE_NONE) {
630 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
631 }
632 if (device2 == AUDIO_DEVICE_NONE) {
633 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
634 }
635 if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) {
636 // no sonification on aux digital (e.g. HDMI)
637 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
638 }
639 if ((device2 == AUDIO_DEVICE_NONE) &&
Krishnankutty Kolathappilly4b7fdaa2013-12-16 00:40:11 -0800640 (mForceUse[AudioSystem::FOR_DOCK] == AudioSystem::FORCE_ANALOG_DOCK)
641 && (strategy != STRATEGY_SONIFICATION)) {
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700642 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
643 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700644#ifdef AUDIO_EXTN_FM_ENABLED
645 if ((strategy != STRATEGY_SONIFICATION) && (device2 == AUDIO_DEVICE_NONE)) {
646 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_FM_TX;
647 }
648#endif
649#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
650 if ((strategy != STRATEGY_SONIFICATION) && (device2 == AUDIO_DEVICE_NONE)) {
651 // no sonification on WFD sink
652 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_PROXY;
653 }
654#endif
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700655 if (device2 == AUDIO_DEVICE_NONE) {
656 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
657 }
658
659 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
660 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
661 device |= device2;
662 if (device) break;
663 device = mDefaultOutputDevice;
664 if (device == AUDIO_DEVICE_NONE) {
665 ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA");
666 }
667 } break;
668
669 default:
670 ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
671 break;
672 }
673
674 ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
675 return device;
676}
677
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700678audio_devices_t AudioPolicyManager::getDeviceForInputSource(int inputSource)
679{
680 uint32_t device = AUDIO_DEVICE_NONE;
681
682 switch (inputSource) {
683 case AUDIO_SOURCE_VOICE_UPLINK:
684 if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
685 device = AUDIO_DEVICE_IN_VOICE_CALL;
686 break;
687 }
688 // FALL THROUGH
689
690 case AUDIO_SOURCE_DEFAULT:
691 case AUDIO_SOURCE_MIC:
692 case AUDIO_SOURCE_VOICE_RECOGNITION:
693 case AUDIO_SOURCE_HOTWORD:
694 case AUDIO_SOURCE_VOICE_COMMUNICATION:
695 if (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO &&
696 mAvailableInputDevices & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
697 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
698 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_WIRED_HEADSET) {
699 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
700 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET) {
701 device = AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET;
702 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
703 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
704 }
705 break;
706 case AUDIO_SOURCE_CAMCORDER:
707 if (mAvailableInputDevices & AUDIO_DEVICE_IN_BACK_MIC) {
708 device = AUDIO_DEVICE_IN_BACK_MIC;
709 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
710 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
711 }
712 break;
713 case AUDIO_SOURCE_VOICE_DOWNLINK:
714 case AUDIO_SOURCE_VOICE_CALL:
715 if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
716 device = AUDIO_DEVICE_IN_VOICE_CALL;
717 }
718 break;
719 case AUDIO_SOURCE_REMOTE_SUBMIX:
720 if (mAvailableInputDevices & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
721 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
722 }
723 break;
724#ifdef AUDIO_EXTN_FM_ENABLED
725 case AUDIO_SOURCE_FM_RX:
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700726 device = AUDIO_DEVICE_IN_FM_RX;
727 break;
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700728 case AUDIO_SOURCE_FM_RX_A2DP:
729 device = AUDIO_DEVICE_IN_FM_RX_A2DP;
730 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700731#endif
732 default:
733 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
734 break;
735 }
736 ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
737 return device;
738}
739
740AudioPolicyManager::device_category AudioPolicyManager::getDeviceCategory(audio_devices_t device)
741{
742 switch(getDeviceForVolume(device)) {
743 case AUDIO_DEVICE_OUT_EARPIECE:
744 return DEVICE_CATEGORY_EARPIECE;
745 case AUDIO_DEVICE_OUT_WIRED_HEADSET:
746 case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
747 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
748 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
749 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
750 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
751#ifdef AUDIO_EXTN_FM_ENABLED
752 case AUDIO_DEVICE_OUT_FM:
753#endif
754 return DEVICE_CATEGORY_HEADSET;
755 case AUDIO_DEVICE_OUT_SPEAKER:
756 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
757 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
758 case AUDIO_DEVICE_OUT_AUX_DIGITAL:
759 case AUDIO_DEVICE_OUT_USB_ACCESSORY:
760 case AUDIO_DEVICE_OUT_USB_DEVICE:
761 case AUDIO_DEVICE_OUT_REMOTE_SUBMIX:
762#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
763 case AUDIO_DEVICE_OUT_PROXY:
764#endif
765 default:
766 return DEVICE_CATEGORY_SPEAKER;
767 }
768}
769
770status_t AudioPolicyManager::checkAndSetVolume(int stream,
771 int index,
772 audio_io_handle_t output,
773 audio_devices_t device,
774 int delayMs,
775 bool force)
776{
777 ALOGV("checkAndSetVolume: index %d output %d device %x", index, output, device);
778 // do not change actual stream volume if the stream is muted
779 if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
780 ALOGVV("checkAndSetVolume() stream %d muted count %d",
781 stream, mOutputs.valueFor(output)->mMuteCount[stream]);
782 return NO_ERROR;
783 }
784
785 // do not change in call volume if bluetooth is connected and vice versa
786 if ((stream == AudioSystem::VOICE_CALL && mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
787 (stream == AudioSystem::BLUETOOTH_SCO && mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO)) {
788 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
789 stream, mForceUse[AudioSystem::FOR_COMMUNICATION]);
790 return INVALID_OPERATION;
791 }
792
793 float volume = computeVolume(stream, index, output, device);
794 // We actually change the volume if:
795 // - the float value returned by computeVolume() changed
796 // - the force flag is set
797 if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
798 force) {
799 mOutputs.valueFor(output)->mCurVolume[stream] = volume;
800 ALOGV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
801 // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
802 // enabled
803 if (stream == AudioSystem::BLUETOOTH_SCO) {
804 mpClientInterface->setStreamVolume(AudioSystem::VOICE_CALL, volume, output, delayMs);
805#ifdef AUDIO_EXTN_FM_ENABLED
806 } else if (stream == AudioSystem::MUSIC &&
807 output == mPrimaryOutput) {
808 float fmVolume = -1.0;
809 fmVolume = computeVolume(stream, index, output, device);
810 if (fmVolume >= 0) {
811 AudioParameter param = AudioParameter();
812 param.addFloat(String8("fm_volume"), fmVolume);
813 ALOGV("checkAndSetVolume setParameters fm_volume, volume=:%f delay=:%d",fmVolume,delayMs*2);
814 //Double delayMs to avoid sound burst while device switch.
815 mpClientInterface->setParameters(mPrimaryOutput, param.toString(), delayMs*2);
816 }
817#endif
818 }
819 mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs);
820 }
821
822 if (stream == AudioSystem::VOICE_CALL ||
823 stream == AudioSystem::BLUETOOTH_SCO) {
824 float voiceVolume;
825
826 voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
827
828 // Force voice volume to max when Vgs is set for bluetooth SCO as volume is managed by the headset
829 if (stream == AudioSystem::BLUETOOTH_SCO) {
830 String8 key ("bt_headset_vgs");
831 mpClientInterface->getParameters(output,key);
832 AudioParameter result(mpClientInterface->getParameters(0,key));
833 int value;
834 if (result.getInt(String8("isVGS"),value) == NO_ERROR) {
835 ALOGV("Use BT-SCO Voice Volume");
836 voiceVolume = 1.0;
837 }
838 }
839
840 if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) {
841 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
842 mLastVoiceVolume = voiceVolume;
843 }
844 }
845
846 return NO_ERROR;
847}
848
849
850float AudioPolicyManager::computeVolume(int stream,
851 int index,
852 audio_io_handle_t output,
853 audio_devices_t device)
854{
855 float volume = 1.0;
856 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
857
858 if (device == AUDIO_DEVICE_NONE) {
859 device = outputDesc->device();
860 }
861
862 // if volume is not 0 (not muted), force media volume to max on digital output
863 if (stream == AudioSystem::MUSIC &&
864 index != mStreams[stream].mIndexMin &&
865 (device == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
866 device == AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET ||
867 device == AUDIO_DEVICE_OUT_USB_ACCESSORY ||
868#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
869 device == AUDIO_DEVICE_OUT_PROXY ||
870#endif
871 device == AUDIO_DEVICE_OUT_USB_DEVICE )) {
872 return 1.0;
873 }
874#ifdef AUDIO_EXTN_INCALL_MUSIC_ENABLED
875 if (stream == AudioSystem::INCALL_MUSIC) {
876 return 1.0;
877 }
878#endif
879 return AudioPolicyManagerBase::computeVolume(stream, index, output, device);
880}
881extern "C" AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700882{
883 return new AudioPolicyManager(clientInterface);
884}
885
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700886extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700887{
888 delete interface;
889}
890
891}; // namespace android