blob: 4fa23561a77e1f5b95413ecd574772defc98b823 [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
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;
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700542 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700543 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
544 if (device) break;
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700545 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
593 if (isInCall()) {
594 // 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) &&
640 (mForceUse[AudioSystem::FOR_DOCK] == AudioSystem::FORCE_ANALOG_DOCK)) {
641 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
642 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700643#ifdef AUDIO_EXTN_FM_ENABLED
644 if ((strategy != STRATEGY_SONIFICATION) && (device2 == AUDIO_DEVICE_NONE)) {
645 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_FM_TX;
646 }
647#endif
648#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
649 if ((strategy != STRATEGY_SONIFICATION) && (device2 == AUDIO_DEVICE_NONE)) {
650 // no sonification on WFD sink
651 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_PROXY;
652 }
653#endif
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700654 if (device2 == AUDIO_DEVICE_NONE) {
655 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
656 }
657
658 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
659 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
660 device |= device2;
661 if (device) break;
662 device = mDefaultOutputDevice;
663 if (device == AUDIO_DEVICE_NONE) {
664 ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA");
665 }
666 } break;
667
668 default:
669 ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
670 break;
671 }
672
673 ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
674 return device;
675}
676
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700677audio_devices_t AudioPolicyManager::getDeviceForInputSource(int inputSource)
678{
679 uint32_t device = AUDIO_DEVICE_NONE;
680
681 switch (inputSource) {
682 case AUDIO_SOURCE_VOICE_UPLINK:
683 if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
684 device = AUDIO_DEVICE_IN_VOICE_CALL;
685 break;
686 }
687 // FALL THROUGH
688
689 case AUDIO_SOURCE_DEFAULT:
690 case AUDIO_SOURCE_MIC:
691 case AUDIO_SOURCE_VOICE_RECOGNITION:
692 case AUDIO_SOURCE_HOTWORD:
693 case AUDIO_SOURCE_VOICE_COMMUNICATION:
694 if (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO &&
695 mAvailableInputDevices & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
696 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
697 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_WIRED_HEADSET) {
698 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
699 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET) {
700 device = AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET;
701 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
702 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
703 }
704 break;
705 case AUDIO_SOURCE_CAMCORDER:
706 if (mAvailableInputDevices & AUDIO_DEVICE_IN_BACK_MIC) {
707 device = AUDIO_DEVICE_IN_BACK_MIC;
708 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
709 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
710 }
711 break;
712 case AUDIO_SOURCE_VOICE_DOWNLINK:
713 case AUDIO_SOURCE_VOICE_CALL:
714 if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
715 device = AUDIO_DEVICE_IN_VOICE_CALL;
716 }
717 break;
718 case AUDIO_SOURCE_REMOTE_SUBMIX:
719 if (mAvailableInputDevices & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
720 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
721 }
722 break;
723#ifdef AUDIO_EXTN_FM_ENABLED
724 case AUDIO_SOURCE_FM_RX:
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700725 device = AUDIO_DEVICE_IN_FM_RX;
726 break;
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700727 case AUDIO_SOURCE_FM_RX_A2DP:
728 device = AUDIO_DEVICE_IN_FM_RX_A2DP;
729 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700730#endif
731 default:
732 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
733 break;
734 }
735 ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
736 return device;
737}
738
739AudioPolicyManager::device_category AudioPolicyManager::getDeviceCategory(audio_devices_t device)
740{
741 switch(getDeviceForVolume(device)) {
742 case AUDIO_DEVICE_OUT_EARPIECE:
743 return DEVICE_CATEGORY_EARPIECE;
744 case AUDIO_DEVICE_OUT_WIRED_HEADSET:
745 case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
746 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
747 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
748 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
749 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
750#ifdef AUDIO_EXTN_FM_ENABLED
751 case AUDIO_DEVICE_OUT_FM:
752#endif
753 return DEVICE_CATEGORY_HEADSET;
754 case AUDIO_DEVICE_OUT_SPEAKER:
755 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
756 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
757 case AUDIO_DEVICE_OUT_AUX_DIGITAL:
758 case AUDIO_DEVICE_OUT_USB_ACCESSORY:
759 case AUDIO_DEVICE_OUT_USB_DEVICE:
760 case AUDIO_DEVICE_OUT_REMOTE_SUBMIX:
761#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
762 case AUDIO_DEVICE_OUT_PROXY:
763#endif
764 default:
765 return DEVICE_CATEGORY_SPEAKER;
766 }
767}
768
769status_t AudioPolicyManager::checkAndSetVolume(int stream,
770 int index,
771 audio_io_handle_t output,
772 audio_devices_t device,
773 int delayMs,
774 bool force)
775{
776 ALOGV("checkAndSetVolume: index %d output %d device %x", index, output, device);
777 // do not change actual stream volume if the stream is muted
778 if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
779 ALOGVV("checkAndSetVolume() stream %d muted count %d",
780 stream, mOutputs.valueFor(output)->mMuteCount[stream]);
781 return NO_ERROR;
782 }
783
784 // do not change in call volume if bluetooth is connected and vice versa
785 if ((stream == AudioSystem::VOICE_CALL && mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
786 (stream == AudioSystem::BLUETOOTH_SCO && mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO)) {
787 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
788 stream, mForceUse[AudioSystem::FOR_COMMUNICATION]);
789 return INVALID_OPERATION;
790 }
791
792 float volume = computeVolume(stream, index, output, device);
793 // We actually change the volume if:
794 // - the float value returned by computeVolume() changed
795 // - the force flag is set
796 if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
797 force) {
798 mOutputs.valueFor(output)->mCurVolume[stream] = volume;
799 ALOGV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
800 // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
801 // enabled
802 if (stream == AudioSystem::BLUETOOTH_SCO) {
803 mpClientInterface->setStreamVolume(AudioSystem::VOICE_CALL, volume, output, delayMs);
804#ifdef AUDIO_EXTN_FM_ENABLED
805 } else if (stream == AudioSystem::MUSIC &&
806 output == mPrimaryOutput) {
807 float fmVolume = -1.0;
808 fmVolume = computeVolume(stream, index, output, device);
809 if (fmVolume >= 0) {
810 AudioParameter param = AudioParameter();
811 param.addFloat(String8("fm_volume"), fmVolume);
812 ALOGV("checkAndSetVolume setParameters fm_volume, volume=:%f delay=:%d",fmVolume,delayMs*2);
813 //Double delayMs to avoid sound burst while device switch.
814 mpClientInterface->setParameters(mPrimaryOutput, param.toString(), delayMs*2);
815 }
816#endif
817 }
818 mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs);
819 }
820
821 if (stream == AudioSystem::VOICE_CALL ||
822 stream == AudioSystem::BLUETOOTH_SCO) {
823 float voiceVolume;
824
825 voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
826
827 // Force voice volume to max when Vgs is set for bluetooth SCO as volume is managed by the headset
828 if (stream == AudioSystem::BLUETOOTH_SCO) {
829 String8 key ("bt_headset_vgs");
830 mpClientInterface->getParameters(output,key);
831 AudioParameter result(mpClientInterface->getParameters(0,key));
832 int value;
833 if (result.getInt(String8("isVGS"),value) == NO_ERROR) {
834 ALOGV("Use BT-SCO Voice Volume");
835 voiceVolume = 1.0;
836 }
837 }
838
839 if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) {
840 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
841 mLastVoiceVolume = voiceVolume;
842 }
843 }
844
845 return NO_ERROR;
846}
847
848
849float AudioPolicyManager::computeVolume(int stream,
850 int index,
851 audio_io_handle_t output,
852 audio_devices_t device)
853{
854 float volume = 1.0;
855 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
856
857 if (device == AUDIO_DEVICE_NONE) {
858 device = outputDesc->device();
859 }
860
861 // if volume is not 0 (not muted), force media volume to max on digital output
862 if (stream == AudioSystem::MUSIC &&
863 index != mStreams[stream].mIndexMin &&
864 (device == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
865 device == AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET ||
866 device == AUDIO_DEVICE_OUT_USB_ACCESSORY ||
867#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
868 device == AUDIO_DEVICE_OUT_PROXY ||
869#endif
870 device == AUDIO_DEVICE_OUT_USB_DEVICE )) {
871 return 1.0;
872 }
873#ifdef AUDIO_EXTN_INCALL_MUSIC_ENABLED
874 if (stream == AudioSystem::INCALL_MUSIC) {
875 return 1.0;
876 }
877#endif
878 return AudioPolicyManagerBase::computeVolume(stream, index, output, device);
879}
880extern "C" AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700881{
882 return new AudioPolicyManager(clientInterface);
883}
884
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700885extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700886{
887 delete interface;
888}
889
890}; // namespace android