blob: 5992c876badb8a8254640207c73ab19b0e2c9214 [file] [log] [blame]
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001/*
2 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
3 * 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
30// A device mask for all audio input devices that are considered "virtual" when evaluating
31// active inputs in getActiveInput()
32#define APM_AUDIO_IN_DEVICE_VIRTUAL_ALL AUDIO_DEVICE_IN_REMOTE_SUBMIX
33// A device mask for all audio output devices that are considered "remote" when evaluating
34// active output devices in isStreamActiveRemotely()
35#define APM_AUDIO_OUT_DEVICE_REMOTE_ALL AUDIO_DEVICE_OUT_REMOTE_SUBMIX
36
37#include <utils/Log.h>
38#include "AudioPolicyManager.h"
39#include <hardware/audio_effect.h>
40#include <hardware/audio.h>
41#include <math.h>
42#include <hardware_legacy/audio_policy_conf.h>
43#include <cutils/properties.h>
44
45namespace android_audio_legacy {
46
47// ----------------------------------------------------------------------------
48// AudioPolicyInterface implementation
49// ----------------------------------------------------------------------------
50
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070051status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
52 AudioSystem::device_connection_state state,
53 const char *device_address)
54{
55 SortedVector <audio_io_handle_t> outputs;
56
57 ALOGV("setDeviceConnectionState() device: %x, state %d, address %s", device, state, device_address);
58
59 // connect/disconnect only 1 device at a time
60 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
61
62 if (strlen(device_address) >= MAX_DEVICE_ADDRESS_LEN) {
63 ALOGE("setDeviceConnectionState() invalid address: %s", device_address);
64 return BAD_VALUE;
65 }
66
67 // handle output devices
68 if (audio_is_output_device(device)) {
69
70 if (!mHasA2dp && audio_is_a2dp_device(device)) {
71 ALOGE("setDeviceConnectionState() invalid A2DP device: %x", device);
72 return BAD_VALUE;
73 }
74 if (!mHasUsb && audio_is_usb_device(device)) {
75 ALOGE("setDeviceConnectionState() invalid USB audio device: %x", device);
76 return BAD_VALUE;
77 }
78 if (!mHasRemoteSubmix && audio_is_remote_submix_device((audio_devices_t)device)) {
79 ALOGE("setDeviceConnectionState() invalid remote submix audio device: %x", device);
80 return BAD_VALUE;
81 }
82
83 // save a copy of the opened output descriptors before any output is opened or closed
84 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
85 mPreviousOutputs = mOutputs;
86 switch (state)
87 {
88 // handle output device connection
89 case AudioSystem::DEVICE_STATE_AVAILABLE:
90 if (mAvailableOutputDevices & device) {
91 ALOGW("setDeviceConnectionState() device already connected: %x", device);
92 return INVALID_OPERATION;
93 }
94 ALOGV("setDeviceConnectionState() connecting device %x", device);
95
96 if (checkOutputsForDevice(device, state, outputs) != NO_ERROR) {
97 return INVALID_OPERATION;
98 }
99 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %d outputs",
100 outputs.size());
101 // register new device as available
102 mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices | device);
103
104 if (!outputs.isEmpty()) {
105 String8 paramStr;
106 if (mHasA2dp && audio_is_a2dp_device(device)) {
107 // handle A2DP device connection
108 AudioParameter param;
109 param.add(String8(AUDIO_PARAMETER_A2DP_SINK_ADDRESS), String8(device_address));
110 paramStr = param.toString();
111 mA2dpDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
112 mA2dpSuspended = false;
113 } else if (audio_is_bluetooth_sco_device(device)) {
114 // handle SCO device connection
115 mScoDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
116 } else if (mHasUsb && audio_is_usb_device(device)) {
117 // handle USB device connection
118 mUsbCardAndDevice = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
119 paramStr = mUsbCardAndDevice;
120 }
121 // not currently handling multiple simultaneous submixes: ignoring remote submix
122 // case and address
123 if (!paramStr.isEmpty()) {
124 for (size_t i = 0; i < outputs.size(); i++) {
125 mpClientInterface->setParameters(outputs[i], paramStr);
126 }
127 }
128 }
129 break;
130 // handle output device disconnection
131 case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
132 if (!(mAvailableOutputDevices & device)) {
133 ALOGW("setDeviceConnectionState() device not connected: %x", device);
134 return INVALID_OPERATION;
135 }
136
137 ALOGV("setDeviceConnectionState() disconnecting device %x", device);
138 // remove device from available output devices
139 mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices & ~device);
140
141 checkOutputsForDevice(device, state, outputs);
142 if (mHasA2dp && audio_is_a2dp_device(device)) {
143 // handle A2DP device disconnection
144 mA2dpDeviceAddress = "";
145 mA2dpSuspended = false;
146 } else if (audio_is_bluetooth_sco_device(device)) {
147 // handle SCO device disconnection
148 mScoDeviceAddress = "";
149 } else if (mHasUsb && audio_is_usb_device(device)) {
150 // handle USB device disconnection
151 mUsbCardAndDevice = "";
152 }
153 // not currently handling multiple simultaneous submixes: ignoring remote submix
154 // case and address
155 } break;
156
157 default:
158 ALOGE("setDeviceConnectionState() invalid state: %x", state);
159 return BAD_VALUE;
160 }
161
162 checkA2dpSuspend();
163 checkOutputForAllStrategies();
164 // outputs must be closed after checkOutputForAllStrategies() is executed
165 if (!outputs.isEmpty()) {
166 for (size_t i = 0; i < outputs.size(); i++) {
167 AudioOutputDescriptor *desc = mOutputs.valueFor(outputs[i]);
168 // close unused outputs after device disconnection or direct outputs that have been
169 // opened by checkOutputsForDevice() to query dynamic parameters
170 if ((state == AudioSystem::DEVICE_STATE_UNAVAILABLE) ||
171 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
172 (desc->mDirectOpenCount == 0))) {
173 closeOutput(outputs[i]);
174 }
175 }
176 }
177
178 updateDevicesAndOutputs();
179 audio_devices_t newDevice = getNewDevice(mPrimaryOutput, false /*fromCache*/);
180#ifdef AUDIO_EXTN_FM_ENABLED
181 if(device == AUDIO_DEVICE_OUT_FM) {
182 if (state == AudioSystem::DEVICE_STATE_AVAILABLE) {
183 mOutputs.valueFor(mPrimaryOutput)->changeRefCount(AudioSystem::MUSIC, 1);
184 newDevice = (audio_devices_t)(getNewDevice(mPrimaryOutput, false) | AUDIO_DEVICE_OUT_FM);
185 } else {
186 mOutputs.valueFor(mPrimaryOutput)->changeRefCount(AudioSystem::MUSIC, -1);
187 }
188
189 AudioParameter param = AudioParameter();
190 param.addInt(String8("handle_fm"), (int)newDevice);
191 ALOGV("setDeviceConnectionState() setParameters handle_fm");
192 mpClientInterface->setParameters(mPrimaryOutput, param.toString());
193 }
194#endif
195 for (size_t i = 0; i < mOutputs.size(); i++) {
196 // do not force device change on duplicated output because if device is 0, it will
197 // also force a device 0 for the two outputs it is duplicated to which may override
198 // a valid device selection on those outputs.
199 setOutputDevice(mOutputs.keyAt(i),
200 getNewDevice(mOutputs.keyAt(i), true /*fromCache*/),
201 !mOutputs.valueAt(i)->isDuplicated(),
202 0);
203 }
204
205 if (device == AUDIO_DEVICE_OUT_WIRED_HEADSET) {
206 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
207 } else if (device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO ||
208 device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET ||
209 device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT) {
210 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
211 } else if(device == AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET){
212 device = AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET;
213 } else {
214 return NO_ERROR;
215 }
216 }
217 // handle input devices
218 if (audio_is_input_device(device)) {
219
220 switch (state)
221 {
222 // handle input device connection
223 case AudioSystem::DEVICE_STATE_AVAILABLE: {
224 if (mAvailableInputDevices & device) {
225 ALOGW("setDeviceConnectionState() device already connected: %d", device);
226 return INVALID_OPERATION;
227 }
228 mAvailableInputDevices = mAvailableInputDevices | (device & ~AUDIO_DEVICE_BIT_IN);
229 }
230 break;
231
232 // handle input device disconnection
233 case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
234 if (!(mAvailableInputDevices & device)) {
235 ALOGW("setDeviceConnectionState() device not connected: %d", device);
236 return INVALID_OPERATION;
237 }
238 mAvailableInputDevices = (audio_devices_t) (mAvailableInputDevices & ~device);
239 } break;
240
241 default:
242 ALOGE("setDeviceConnectionState() invalid state: %x", state);
243 return BAD_VALUE;
244 }
245
246 audio_io_handle_t activeInput = getActiveInput();
247 if (activeInput != 0) {
248 AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
249 audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
250 if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) {
251 ALOGV("setDeviceConnectionState() changing device from %x to %x for input %d",
252 inputDesc->mDevice, newDevice, activeInput);
253 inputDesc->mDevice = newDevice;
254 AudioParameter param = AudioParameter();
255 param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
256 mpClientInterface->setParameters(activeInput, param.toString());
257 }
258 }
259
260 return NO_ERROR;
261 }
262
263 ALOGW("setDeviceConnectionState() invalid device: %x", device);
264 return BAD_VALUE;
265}
266
267void AudioPolicyManager::setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config)
268{
269 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState);
270
271 bool forceVolumeReeval = false;
272 switch(usage) {
273 case AudioSystem::FOR_COMMUNICATION:
274 if (config != AudioSystem::FORCE_SPEAKER && config != AudioSystem::FORCE_BT_SCO &&
275 config != AudioSystem::FORCE_NONE) {
276 ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
277 return;
278 }
279 forceVolumeReeval = true;
280 mForceUse[usage] = config;
281 break;
282 case AudioSystem::FOR_MEDIA:
283 if (config != AudioSystem::FORCE_HEADPHONES && config != AudioSystem::FORCE_BT_A2DP &&
284#ifdef AUDIO_EXTN_FM_ENABLED
285 config != AudioSystem::FORCE_SPEAKER &&
286#endif
287 config != AudioSystem::FORCE_WIRED_ACCESSORY &&
288 config != AudioSystem::FORCE_ANALOG_DOCK &&
289 config != AudioSystem::FORCE_DIGITAL_DOCK && config != AudioSystem::FORCE_NONE &&
290 config != AudioSystem::FORCE_NO_BT_A2DP) {
291 ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
292 return;
293 }
294 mForceUse[usage] = config;
295 break;
296 case AudioSystem::FOR_RECORD:
297 if (config != AudioSystem::FORCE_BT_SCO && config != AudioSystem::FORCE_WIRED_ACCESSORY &&
298 config != AudioSystem::FORCE_NONE) {
299 ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
300 return;
301 }
302 mForceUse[usage] = config;
303 break;
304 case AudioSystem::FOR_DOCK:
305 if (config != AudioSystem::FORCE_NONE && config != AudioSystem::FORCE_BT_CAR_DOCK &&
306 config != AudioSystem::FORCE_BT_DESK_DOCK &&
307 config != AudioSystem::FORCE_WIRED_ACCESSORY &&
308 config != AudioSystem::FORCE_ANALOG_DOCK &&
309 config != AudioSystem::FORCE_DIGITAL_DOCK) {
310 ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
311 }
312 forceVolumeReeval = true;
313 mForceUse[usage] = config;
314 break;
315 case AudioSystem::FOR_SYSTEM:
316 if (config != AudioSystem::FORCE_NONE &&
317 config != AudioSystem::FORCE_SYSTEM_ENFORCED) {
318 ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
319 }
320 forceVolumeReeval = true;
321 mForceUse[usage] = config;
322 break;
323 default:
324 ALOGW("setForceUse() invalid usage %d", usage);
325 break;
326 }
327
328 // check for device and output changes triggered by new force usage
329 checkA2dpSuspend();
330 checkOutputForAllStrategies();
331 updateDevicesAndOutputs();
332 for (int i = mOutputs.size() -1; i >= 0; i--) {
333 audio_io_handle_t output = mOutputs.keyAt(i);
334 audio_devices_t newDevice = getNewDevice(output, true /*fromCache*/);
335 setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE));
336 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
337 applyStreamVolumes(output, newDevice, 0, true);
338 }
339 }
340
341 audio_io_handle_t activeInput = getActiveInput();
342 if (activeInput != 0) {
343 AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
344 audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
345 if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) {
346 ALOGV("setForceUse() changing device from %x to %x for input %d",
347 inputDesc->mDevice, newDevice, activeInput);
348 inputDesc->mDevice = newDevice;
349 AudioParameter param = AudioParameter();
350 param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
351 mpClientInterface->setParameters(activeInput, param.toString());
352 }
353 }
354
355}
356
357audio_io_handle_t AudioPolicyManager::getInput(int inputSource,
358 uint32_t samplingRate,
359 uint32_t format,
360 uint32_t channelMask,
361 AudioSystem::audio_in_acoustics acoustics)
362{
363 audio_io_handle_t input = 0;
364 audio_devices_t device = getDeviceForInputSource(inputSource);
365
366 ALOGV("getInput() inputSource %d, samplingRate %d, format %d, channelMask %x, acoustics %x",
367 inputSource, samplingRate, format, channelMask, acoustics);
368
369 if (device == AUDIO_DEVICE_NONE) {
370 ALOGW("getInput() could not find device for inputSource %d", inputSource);
371 return 0;
372 }
373
374
375 IOProfile *profile = getInputProfile(device,
376 samplingRate,
377 format,
378 channelMask);
379 if (profile == NULL) {
380 ALOGW("getInput() could not find profile for device %04x, samplingRate %d, format %d,"
381 "channelMask %04x",
382 device, samplingRate, format, channelMask);
383 return 0;
384 }
385
386 if (profile->mModule->mHandle == 0) {
387 ALOGE("getInput(): HW module %s not opened", profile->mModule->mName);
388 return 0;
389 }
390
391 AudioInputDescriptor *inputDesc = new AudioInputDescriptor(profile);
392
393 inputDesc->mInputSource = inputSource;
394 inputDesc->mDevice = device;
395 inputDesc->mSamplingRate = samplingRate;
396 inputDesc->mFormat = (audio_format_t)format;
397 inputDesc->mChannelMask = (audio_channel_mask_t)channelMask;
398 inputDesc->mRefCount = 0;
399 input = mpClientInterface->openInput(profile->mModule->mHandle,
400 &inputDesc->mDevice,
401 &inputDesc->mSamplingRate,
402 &inputDesc->mFormat,
403 &inputDesc->mChannelMask);
404
405 // only accept input with the exact requested set of parameters
406 if (input == 0 ||
407 (samplingRate != inputDesc->mSamplingRate) ||
408 (format != inputDesc->mFormat) ||
409 (channelMask != inputDesc->mChannelMask)) {
410 ALOGV("getInput() failed opening input: samplingRate %d, format %d, channelMask %d",
411 samplingRate, format, channelMask);
412 if (input != 0) {
413 mpClientInterface->closeInput(input);
414 }
415 delete inputDesc;
416 return 0;
417 }
418 mInputs.add(input, inputDesc);
419 return input;
420}
421
422AudioPolicyManager::routing_strategy AudioPolicyManager::getStrategy(AudioSystem::stream_type stream)
423{
424#ifdef QCOM_INCALL_MUSIC_ENABLED
425 if (stream == AudioSystem::INCALL_MUSIC)
426 return STRATEGY_MEDIA;
427#endif
428
429 return getStrategy(stream);
430}
431
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700432audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
433 bool fromCache)
434{
435 uint32_t device = AUDIO_DEVICE_NONE;
436
437 if (fromCache) {
438 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
439 strategy, mDeviceForStrategy[strategy]);
440 return mDeviceForStrategy[strategy];
441 }
442
443 switch (strategy) {
444
445 case STRATEGY_SONIFICATION_RESPECTFUL:
446 if (isInCall()) {
447 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
448 } else if (isStreamActiveRemotely(AudioSystem::MUSIC,
449 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
450 // while media is playing on a remote device, use the the sonification behavior.
451 // Note that we test this usecase before testing if media is playing because
452 // the isStreamActive() method only informs about the activity of a stream, not
453 // if it's for local playback. Note also that we use the same delay between both tests
454 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
455 } else if (isStreamActive(AudioSystem::MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
456 // while media is playing (or has recently played), use the same device
457 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
458 } else {
459 // when media is not playing anymore, fall back on the sonification behavior
460 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
461 }
462
463 break;
464
465 case STRATEGY_DTMF:
466 if (!isInCall()) {
467 // when off call, DTMF strategy follows the same rules as MEDIA strategy
468 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
469 break;
470 }
471 // when in call, DTMF and PHONE strategies follow the same rules
472 // FALL THROUGH
473
474 case STRATEGY_PHONE:
475 // for phone strategy, we first consider the forced use and then the available devices by order
476 // of priority
477 switch (mForceUse[AudioSystem::FOR_COMMUNICATION]) {
478 case AudioSystem::FORCE_BT_SCO:
479 if (!isInCall() || strategy != STRATEGY_DTMF) {
480 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
481 if (device) break;
482 }
483 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
484 if (device) break;
485 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
486 if (device) break;
487 // if SCO device is requested but no SCO device is available, fall back to default case
488 // FALL THROUGH
489
490 default: // FORCE_NONE
491 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
492 if (mHasA2dp && !isInCall() &&
493 (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
494 (getA2dpOutput() != 0) && !mA2dpSuspended) {
495 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
496 if (device) break;
497 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
498 if (device) break;
499 }
500 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
501 if (device) break;
502 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
503 if (device) break;
504 if (mPhoneState != AudioSystem::MODE_IN_CALL) {
505 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
506 if (device) break;
507 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
508 if (device) break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700509 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
510 if (device) break;
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700511 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
512 if (device) break;
513 }
514
515 // Allow voice call on USB ANLG DOCK headset
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700516 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
517 if (device) break;
518
519 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_EARPIECE;
520 if (device) break;
521 device = mDefaultOutputDevice;
522 if (device == AUDIO_DEVICE_NONE) {
523 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE");
524 }
525 break;
526
527 case AudioSystem::FORCE_SPEAKER:
528 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
529 // A2DP speaker when forcing to speaker output
530 if (mHasA2dp && !isInCall() &&
531 (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
532 (getA2dpOutput() != 0) && !mA2dpSuspended) {
533 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
534 if (device) break;
535 }
536 if (mPhoneState != AudioSystem::MODE_IN_CALL) {
537 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
538 if (device) break;
539 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
540 if (device) break;
541 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
542 if (device) break;
543 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
544 if (device) break;
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700545 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700546 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
547 if (device) break;
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700548 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
549 if (device) break;
550 device = mDefaultOutputDevice;
551 if (device == AUDIO_DEVICE_NONE) {
552 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER");
553 }
554 break;
555 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700556 // FIXME: Why do need to replace with speaker? If voice call is active
557 // We should use device from STRATEGY_PHONE
558#ifdef AUDIO_EXTN_FM_ENABLED
559 if (mAvailableOutputDevices & AUDIO_DEVICE_OUT_FM) {
560 if (mForceUse[AudioSystem::FOR_MEDIA] == AudioSystem::FORCE_SPEAKER) {
561 device = AUDIO_DEVICE_OUT_SPEAKER;
562 }
563 }
564#endif
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700565 break;
566
567 case STRATEGY_SONIFICATION:
568
569 // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
570 // handleIncallSonification().
571 if (isInCall()) {
572 device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
573 break;
574 }
575 // FALL THROUGH
576
577 case STRATEGY_ENFORCED_AUDIBLE:
578 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
579 // except:
580 // - when in call where it doesn't default to STRATEGY_PHONE behavior
581 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
582
583 if ((strategy == STRATEGY_SONIFICATION) ||
584 (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_SYSTEM_ENFORCED)) {
585 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
586 if (device == AUDIO_DEVICE_NONE) {
587 ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION");
588 }
589 }
590 // The second device used for sonification is the same as the device used by media strategy
591 // FALL THROUGH
592
593 case STRATEGY_MEDIA: {
594 uint32_t device2 = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700595
596 if (isInCall()) {
597 // when in call, get the device for Phone strategy
598 device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
599 break;
600 }
601#ifdef AUDIO_EXTN_FM_ENABLED
602 if (mForceUse[AudioSystem::FOR_MEDIA] == AudioSystem::FORCE_SPEAKER) {
603 device = AUDIO_DEVICE_OUT_SPEAKER;
604 break;
605 }
606#endif
607
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700608 if (strategy != STRATEGY_SONIFICATION) {
609 // no sonification on remote submix (e.g. WFD)
610 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
611 }
612 if ((device2 == AUDIO_DEVICE_NONE) &&
613 mHasA2dp && (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
614 (getA2dpOutput() != 0) && !mA2dpSuspended) {
615 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
616 if (device2 == AUDIO_DEVICE_NONE) {
617 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
618 }
619 if (device2 == AUDIO_DEVICE_NONE) {
620 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
621 }
622 }
623 if (device2 == AUDIO_DEVICE_NONE) {
624 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
625 }
626 if (device2 == AUDIO_DEVICE_NONE) {
627 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
628 }
629 if (device2 == AUDIO_DEVICE_NONE) {
630 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
631 }
632 if (device2 == AUDIO_DEVICE_NONE) {
633 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
634 }
635 if (device2 == AUDIO_DEVICE_NONE) {
636 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
637 }
638 if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) {
639 // no sonification on aux digital (e.g. HDMI)
640 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
641 }
642 if ((device2 == AUDIO_DEVICE_NONE) &&
643 (mForceUse[AudioSystem::FOR_DOCK] == AudioSystem::FORCE_ANALOG_DOCK)) {
644 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
645 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700646#ifdef AUDIO_EXTN_FM_ENABLED
647 if ((strategy != STRATEGY_SONIFICATION) && (device2 == AUDIO_DEVICE_NONE)) {
648 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_FM_TX;
649 }
650#endif
651#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
652 if ((strategy != STRATEGY_SONIFICATION) && (device2 == AUDIO_DEVICE_NONE)) {
653 // no sonification on WFD sink
654 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_PROXY;
655 }
656#endif
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700657 if (device2 == AUDIO_DEVICE_NONE) {
658 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
659 }
660
661 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
662 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
663 device |= device2;
664 if (device) break;
665 device = mDefaultOutputDevice;
666 if (device == AUDIO_DEVICE_NONE) {
667 ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA");
668 }
669 } break;
670
671 default:
672 ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
673 break;
674 }
675
676 ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
677 return device;
678}
679
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700680audio_devices_t AudioPolicyManager::getDeviceForInputSource(int inputSource)
681{
682 uint32_t device = AUDIO_DEVICE_NONE;
683
684 switch (inputSource) {
685 case AUDIO_SOURCE_VOICE_UPLINK:
686 if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
687 device = AUDIO_DEVICE_IN_VOICE_CALL;
688 break;
689 }
690 // FALL THROUGH
691
692 case AUDIO_SOURCE_DEFAULT:
693 case AUDIO_SOURCE_MIC:
694 case AUDIO_SOURCE_VOICE_RECOGNITION:
695 case AUDIO_SOURCE_HOTWORD:
696 case AUDIO_SOURCE_VOICE_COMMUNICATION:
697 if (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO &&
698 mAvailableInputDevices & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
699 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
700 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_WIRED_HEADSET) {
701 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
702 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET) {
703 device = AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET;
704 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
705 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
706 }
707 break;
708 case AUDIO_SOURCE_CAMCORDER:
709 if (mAvailableInputDevices & AUDIO_DEVICE_IN_BACK_MIC) {
710 device = AUDIO_DEVICE_IN_BACK_MIC;
711 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
712 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
713 }
714 break;
715 case AUDIO_SOURCE_VOICE_DOWNLINK:
716 case AUDIO_SOURCE_VOICE_CALL:
717 if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
718 device = AUDIO_DEVICE_IN_VOICE_CALL;
719 }
720 break;
721 case AUDIO_SOURCE_REMOTE_SUBMIX:
722 if (mAvailableInputDevices & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
723 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
724 }
725 break;
726#ifdef AUDIO_EXTN_FM_ENABLED
727 case AUDIO_SOURCE_FM_RX:
728 case AUDIO_SOURCE_FM_RX_A2DP:
729 device = AUDIO_DEVICE_IN_FM_RX;
730 break;
731#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