blob: 58f08c48b62252c06aa07eaf898faf348a7c4811 [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// ----------------------------------------------------------------------------
Tanya Finkelde496d82014-03-05 23:59:45 +020047const char* AudioPolicyManager::HDMI_SPKR_STR = "hdmi_spkr";
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070048
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070049status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
50 AudioSystem::device_connection_state state,
51 const char *device_address)
52{
53 SortedVector <audio_io_handle_t> outputs;
54
55 ALOGV("setDeviceConnectionState() device: %x, state %d, address %s", device, state, device_address);
56
57 // connect/disconnect only 1 device at a time
58 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
59
60 if (strlen(device_address) >= MAX_DEVICE_ADDRESS_LEN) {
61 ALOGE("setDeviceConnectionState() invalid address: %s", device_address);
62 return BAD_VALUE;
63 }
64
65 // handle output devices
66 if (audio_is_output_device(device)) {
67
68 if (!mHasA2dp && audio_is_a2dp_device(device)) {
69 ALOGE("setDeviceConnectionState() invalid A2DP device: %x", device);
70 return BAD_VALUE;
71 }
72 if (!mHasUsb && audio_is_usb_device(device)) {
73 ALOGE("setDeviceConnectionState() invalid USB audio device: %x", device);
74 return BAD_VALUE;
75 }
76 if (!mHasRemoteSubmix && audio_is_remote_submix_device((audio_devices_t)device)) {
77 ALOGE("setDeviceConnectionState() invalid remote submix audio device: %x", device);
78 return BAD_VALUE;
79 }
80
81 // save a copy of the opened output descriptors before any output is opened or closed
82 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
83 mPreviousOutputs = mOutputs;
84 switch (state)
85 {
86 // handle output device connection
87 case AudioSystem::DEVICE_STATE_AVAILABLE:
88 if (mAvailableOutputDevices & device) {
Tanya Finkelde496d82014-03-05 23:59:45 +020089#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
90 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
91 if (!strncmp(device_address, HDMI_SPKR_STR, MAX_DEVICE_ADDRESS_LEN)) {
92 mHdmiAudioDisabled = false;
93 } else {
94 mHdmiAudioEvent = true;
95 }
96 }
97#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070098 ALOGW("setDeviceConnectionState() device already connected: %x", device);
99 return INVALID_OPERATION;
100 }
101 ALOGV("setDeviceConnectionState() connecting device %x", device);
102
103 if (checkOutputsForDevice(device, state, outputs) != NO_ERROR) {
104 return INVALID_OPERATION;
105 }
106 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %d outputs",
107 outputs.size());
108 // register new device as available
109 mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices | device);
110
Tanya Finkelde496d82014-03-05 23:59:45 +0200111#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
112 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
113 if (!strncmp(device_address, HDMI_SPKR_STR, MAX_DEVICE_ADDRESS_LEN)) {
114 mHdmiAudioDisabled = false;
115 } else {
116 mHdmiAudioEvent = true;
117 }
118 if (mHdmiAudioDisabled || !mHdmiAudioEvent) {
119 mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices & ~device);
120 }
121 }
122#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700123 if (!outputs.isEmpty()) {
124 String8 paramStr;
125 if (mHasA2dp && audio_is_a2dp_device(device)) {
126 // handle A2DP device connection
127 AudioParameter param;
128 param.add(String8(AUDIO_PARAMETER_A2DP_SINK_ADDRESS), String8(device_address));
129 paramStr = param.toString();
130 mA2dpDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
131 mA2dpSuspended = false;
132 } else if (audio_is_bluetooth_sco_device(device)) {
133 // handle SCO device connection
134 mScoDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
135 } else if (mHasUsb && audio_is_usb_device(device)) {
136 // handle USB device connection
137 mUsbCardAndDevice = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
138 paramStr = mUsbCardAndDevice;
139 }
140 // not currently handling multiple simultaneous submixes: ignoring remote submix
141 // case and address
142 if (!paramStr.isEmpty()) {
143 for (size_t i = 0; i < outputs.size(); i++) {
144 mpClientInterface->setParameters(outputs[i], paramStr);
145 }
146 }
147 }
148 break;
149 // handle output device disconnection
150 case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
151 if (!(mAvailableOutputDevices & device)) {
Tanya Finkelde496d82014-03-05 23:59:45 +0200152#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
153 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
154 if (!strncmp(device_address, HDMI_SPKR_STR, MAX_DEVICE_ADDRESS_LEN)) {
155 mHdmiAudioDisabled = true;
156 } else {
157 mHdmiAudioEvent = false;
158 }
159 }
160#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700161 ALOGW("setDeviceConnectionState() device not connected: %x", device);
162 return INVALID_OPERATION;
163 }
164
165 ALOGV("setDeviceConnectionState() disconnecting device %x", device);
166 // remove device from available output devices
167 mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices & ~device);
168
Tanya Finkelde496d82014-03-05 23:59:45 +0200169#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
170 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
171 if (!strncmp(device_address, HDMI_SPKR_STR, MAX_DEVICE_ADDRESS_LEN)) {
172 mHdmiAudioDisabled = true;
173 } else {
174 mHdmiAudioEvent = false;
175 }
176 }
177#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700178 checkOutputsForDevice(device, state, outputs);
179 if (mHasA2dp && audio_is_a2dp_device(device)) {
180 // handle A2DP device disconnection
181 mA2dpDeviceAddress = "";
182 mA2dpSuspended = false;
183 } else if (audio_is_bluetooth_sco_device(device)) {
184 // handle SCO device disconnection
185 mScoDeviceAddress = "";
186 } else if (mHasUsb && audio_is_usb_device(device)) {
187 // handle USB device disconnection
188 mUsbCardAndDevice = "";
189 }
190 // not currently handling multiple simultaneous submixes: ignoring remote submix
191 // case and address
192 } break;
193
194 default:
195 ALOGE("setDeviceConnectionState() invalid state: %x", state);
196 return BAD_VALUE;
197 }
198
199 checkA2dpSuspend();
200 checkOutputForAllStrategies();
201 // outputs must be closed after checkOutputForAllStrategies() is executed
202 if (!outputs.isEmpty()) {
203 for (size_t i = 0; i < outputs.size(); i++) {
204 AudioOutputDescriptor *desc = mOutputs.valueFor(outputs[i]);
205 // close unused outputs after device disconnection or direct outputs that have been
206 // opened by checkOutputsForDevice() to query dynamic parameters
207 if ((state == AudioSystem::DEVICE_STATE_UNAVAILABLE) ||
208 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
209 (desc->mDirectOpenCount == 0))) {
210 closeOutput(outputs[i]);
211 }
212 }
213 }
214
215 updateDevicesAndOutputs();
216 audio_devices_t newDevice = getNewDevice(mPrimaryOutput, false /*fromCache*/);
217#ifdef AUDIO_EXTN_FM_ENABLED
218 if(device == AUDIO_DEVICE_OUT_FM) {
219 if (state == AudioSystem::DEVICE_STATE_AVAILABLE) {
220 mOutputs.valueFor(mPrimaryOutput)->changeRefCount(AudioSystem::MUSIC, 1);
221 newDevice = (audio_devices_t)(getNewDevice(mPrimaryOutput, false) | AUDIO_DEVICE_OUT_FM);
222 } else {
223 mOutputs.valueFor(mPrimaryOutput)->changeRefCount(AudioSystem::MUSIC, -1);
224 }
225
226 AudioParameter param = AudioParameter();
227 param.addInt(String8("handle_fm"), (int)newDevice);
228 ALOGV("setDeviceConnectionState() setParameters handle_fm");
229 mpClientInterface->setParameters(mPrimaryOutput, param.toString());
230 }
231#endif
232 for (size_t i = 0; i < mOutputs.size(); i++) {
233 // do not force device change on duplicated output because if device is 0, it will
234 // also force a device 0 for the two outputs it is duplicated to which may override
235 // a valid device selection on those outputs.
236 setOutputDevice(mOutputs.keyAt(i),
237 getNewDevice(mOutputs.keyAt(i), true /*fromCache*/),
238 !mOutputs.valueAt(i)->isDuplicated(),
239 0);
240 }
241
242 if (device == AUDIO_DEVICE_OUT_WIRED_HEADSET) {
243 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
244 } else if (device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO ||
245 device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET ||
246 device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT) {
247 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
248 } else if(device == AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET){
249 device = AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET;
250 } else {
251 return NO_ERROR;
252 }
253 }
254 // handle input devices
255 if (audio_is_input_device(device)) {
256
257 switch (state)
258 {
259 // handle input device connection
260 case AudioSystem::DEVICE_STATE_AVAILABLE: {
261 if (mAvailableInputDevices & device) {
262 ALOGW("setDeviceConnectionState() device already connected: %d", device);
263 return INVALID_OPERATION;
264 }
265 mAvailableInputDevices = mAvailableInputDevices | (device & ~AUDIO_DEVICE_BIT_IN);
266 }
267 break;
268
269 // handle input device disconnection
270 case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
271 if (!(mAvailableInputDevices & device)) {
272 ALOGW("setDeviceConnectionState() device not connected: %d", device);
273 return INVALID_OPERATION;
274 }
275 mAvailableInputDevices = (audio_devices_t) (mAvailableInputDevices & ~device);
276 } break;
277
278 default:
279 ALOGE("setDeviceConnectionState() invalid state: %x", state);
280 return BAD_VALUE;
281 }
282
283 audio_io_handle_t activeInput = getActiveInput();
284 if (activeInput != 0) {
285 AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
286 audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
287 if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) {
288 ALOGV("setDeviceConnectionState() changing device from %x to %x for input %d",
289 inputDesc->mDevice, newDevice, activeInput);
290 inputDesc->mDevice = newDevice;
291 AudioParameter param = AudioParameter();
292 param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
293 mpClientInterface->setParameters(activeInput, param.toString());
294 }
295 }
296
297 return NO_ERROR;
298 }
299
300 ALOGW("setDeviceConnectionState() invalid device: %x", device);
301 return BAD_VALUE;
302}
303
304void AudioPolicyManager::setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config)
305{
306 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState);
307
308 bool forceVolumeReeval = false;
309 switch(usage) {
310 case AudioSystem::FOR_COMMUNICATION:
311 if (config != AudioSystem::FORCE_SPEAKER && config != AudioSystem::FORCE_BT_SCO &&
312 config != AudioSystem::FORCE_NONE) {
313 ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
314 return;
315 }
316 forceVolumeReeval = true;
317 mForceUse[usage] = config;
318 break;
319 case AudioSystem::FOR_MEDIA:
320 if (config != AudioSystem::FORCE_HEADPHONES && config != AudioSystem::FORCE_BT_A2DP &&
321#ifdef AUDIO_EXTN_FM_ENABLED
322 config != AudioSystem::FORCE_SPEAKER &&
323#endif
324 config != AudioSystem::FORCE_WIRED_ACCESSORY &&
325 config != AudioSystem::FORCE_ANALOG_DOCK &&
326 config != AudioSystem::FORCE_DIGITAL_DOCK && config != AudioSystem::FORCE_NONE &&
327 config != AudioSystem::FORCE_NO_BT_A2DP) {
328 ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
329 return;
330 }
331 mForceUse[usage] = config;
332 break;
333 case AudioSystem::FOR_RECORD:
334 if (config != AudioSystem::FORCE_BT_SCO && config != AudioSystem::FORCE_WIRED_ACCESSORY &&
335 config != AudioSystem::FORCE_NONE) {
336 ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
337 return;
338 }
339 mForceUse[usage] = config;
340 break;
341 case AudioSystem::FOR_DOCK:
342 if (config != AudioSystem::FORCE_NONE && config != AudioSystem::FORCE_BT_CAR_DOCK &&
343 config != AudioSystem::FORCE_BT_DESK_DOCK &&
344 config != AudioSystem::FORCE_WIRED_ACCESSORY &&
345 config != AudioSystem::FORCE_ANALOG_DOCK &&
346 config != AudioSystem::FORCE_DIGITAL_DOCK) {
347 ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
348 }
349 forceVolumeReeval = true;
350 mForceUse[usage] = config;
351 break;
352 case AudioSystem::FOR_SYSTEM:
353 if (config != AudioSystem::FORCE_NONE &&
354 config != AudioSystem::FORCE_SYSTEM_ENFORCED) {
355 ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
356 }
357 forceVolumeReeval = true;
358 mForceUse[usage] = config;
359 break;
360 default:
361 ALOGW("setForceUse() invalid usage %d", usage);
362 break;
363 }
364
365 // check for device and output changes triggered by new force usage
366 checkA2dpSuspend();
367 checkOutputForAllStrategies();
368 updateDevicesAndOutputs();
369 for (int i = mOutputs.size() -1; i >= 0; i--) {
370 audio_io_handle_t output = mOutputs.keyAt(i);
371 audio_devices_t newDevice = getNewDevice(output, true /*fromCache*/);
372 setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE));
373 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
374 applyStreamVolumes(output, newDevice, 0, true);
375 }
376 }
377
378 audio_io_handle_t activeInput = getActiveInput();
379 if (activeInput != 0) {
380 AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
381 audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
382 if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) {
383 ALOGV("setForceUse() changing device from %x to %x for input %d",
384 inputDesc->mDevice, newDevice, activeInput);
385 inputDesc->mDevice = newDevice;
386 AudioParameter param = AudioParameter();
387 param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
388 mpClientInterface->setParameters(activeInput, param.toString());
389 }
390 }
391
392}
393
394audio_io_handle_t AudioPolicyManager::getInput(int inputSource,
395 uint32_t samplingRate,
396 uint32_t format,
397 uint32_t channelMask,
398 AudioSystem::audio_in_acoustics acoustics)
399{
400 audio_io_handle_t input = 0;
401 audio_devices_t device = getDeviceForInputSource(inputSource);
402
403 ALOGV("getInput() inputSource %d, samplingRate %d, format %d, channelMask %x, acoustics %x",
404 inputSource, samplingRate, format, channelMask, acoustics);
405
406 if (device == AUDIO_DEVICE_NONE) {
407 ALOGW("getInput() could not find device for inputSource %d", inputSource);
408 return 0;
409 }
410
Naresh Tanniru811f4982014-03-18 17:25:32 +0530411#ifdef VOICE_CONCURRENCY
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700412
Naresh Tanniru811f4982014-03-18 17:25:32 +0530413 char propValue[PROPERTY_VALUE_MAX];
414 bool prop_rec_enabled=false, prop_voip_enabled = false;
415
416 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
417 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
418 }
419
420 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
421 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
422 }
423
424 if (prop_rec_enabled) {
425 //check if voice call is active / running in background
426 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
427 //Need to block input request
428 if((AudioSystem::MODE_IN_CALL == mPhoneState) ||
429 ((AudioSystem::MODE_IN_CALL == mPrevPhoneState) &&
430 (AudioSystem::MODE_IN_COMMUNICATION == mPhoneState)))
431 {
432 switch(inputSource) {
433 case AUDIO_SOURCE_VOICE_UPLINK:
434 case AUDIO_SOURCE_VOICE_DOWNLINK:
435 case AUDIO_SOURCE_VOICE_CALL:
436 ALOGD("Creating input during incall mode for inputSource: %d ",inputSource);
437 break;
438
439 case AUDIO_SOURCE_VOICE_COMMUNICATION:
440 if(prop_voip_enabled) {
441 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
442 return 0;
443 }
444 break;
445
446 default:
447 ALOGD("BLOCKING input during incall mode for inputSource: %d ",inputSource);
448 return 0;
449 }
450 }
451 }//check for VoIP flag
452 else if(prop_voip_enabled) {
453 //check if voice call is active / running in background
454 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
455 //Need to block input request
456 if((AudioSystem::MODE_IN_CALL == mPhoneState) ||
457 ((AudioSystem::MODE_IN_CALL == mPrevPhoneState) &&
458 (AudioSystem::MODE_IN_COMMUNICATION == mPhoneState)))
459 {
460 if(inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION) {
461 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
462 return 0;
463 }
464 }
465 }
466
467#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700468 IOProfile *profile = getInputProfile(device,
469 samplingRate,
470 format,
471 channelMask);
472 if (profile == NULL) {
473 ALOGW("getInput() could not find profile for device %04x, samplingRate %d, format %d,"
474 "channelMask %04x",
475 device, samplingRate, format, channelMask);
476 return 0;
477 }
478
479 if (profile->mModule->mHandle == 0) {
480 ALOGE("getInput(): HW module %s not opened", profile->mModule->mName);
481 return 0;
482 }
483
484 AudioInputDescriptor *inputDesc = new AudioInputDescriptor(profile);
485
486 inputDesc->mInputSource = inputSource;
487 inputDesc->mDevice = device;
488 inputDesc->mSamplingRate = samplingRate;
489 inputDesc->mFormat = (audio_format_t)format;
490 inputDesc->mChannelMask = (audio_channel_mask_t)channelMask;
491 inputDesc->mRefCount = 0;
492 input = mpClientInterface->openInput(profile->mModule->mHandle,
493 &inputDesc->mDevice,
494 &inputDesc->mSamplingRate,
495 &inputDesc->mFormat,
496 &inputDesc->mChannelMask);
497
498 // only accept input with the exact requested set of parameters
499 if (input == 0 ||
500 (samplingRate != inputDesc->mSamplingRate) ||
501 (format != inputDesc->mFormat) ||
502 (channelMask != inputDesc->mChannelMask)) {
503 ALOGV("getInput() failed opening input: samplingRate %d, format %d, channelMask %d",
504 samplingRate, format, channelMask);
505 if (input != 0) {
506 mpClientInterface->closeInput(input);
507 }
508 delete inputDesc;
509 return 0;
510 }
511 mInputs.add(input, inputDesc);
512 return input;
513}
514
515AudioPolicyManager::routing_strategy AudioPolicyManager::getStrategy(AudioSystem::stream_type stream)
516{
Naresh Tanniru36c08932014-01-27 18:40:53 +0530517 // stream to strategy mapping
518 switch (stream) {
519 case AudioSystem::VOICE_CALL:
520 case AudioSystem::BLUETOOTH_SCO:
521 return STRATEGY_PHONE;
522 case AudioSystem::RING:
523 case AudioSystem::ALARM:
524 return STRATEGY_SONIFICATION;
525 case AudioSystem::NOTIFICATION:
526 return STRATEGY_SONIFICATION_RESPECTFUL;
527 case AudioSystem::DTMF:
528 return STRATEGY_DTMF;
529 default:
530 ALOGE("unknown stream type");
531 case AudioSystem::SYSTEM:
532 // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
533 // while key clicks are played produces a poor result
534 case AudioSystem::TTS:
535 case AudioSystem::MUSIC:
536#ifdef AUDIO_EXTN_INCALL_MUSIC_ENABLED
537 case AudioSystem::INCALL_MUSIC:
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700538#endif
Naresh Tanniru36c08932014-01-27 18:40:53 +0530539#ifdef QCOM_INCALL_MUSIC_ENABLED
540 case AudioSystem::INCALL_MUSIC:
541#endif
542 return STRATEGY_MEDIA;
543 case AudioSystem::ENFORCED_AUDIBLE:
544 return STRATEGY_ENFORCED_AUDIBLE;
545 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700546
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700547}
548
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700549audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
550 bool fromCache)
551{
552 uint32_t device = AUDIO_DEVICE_NONE;
553
554 if (fromCache) {
555 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
556 strategy, mDeviceForStrategy[strategy]);
557 return mDeviceForStrategy[strategy];
558 }
559
560 switch (strategy) {
561
562 case STRATEGY_SONIFICATION_RESPECTFUL:
563 if (isInCall()) {
564 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
565 } else if (isStreamActiveRemotely(AudioSystem::MUSIC,
566 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
567 // while media is playing on a remote device, use the the sonification behavior.
568 // Note that we test this usecase before testing if media is playing because
569 // the isStreamActive() method only informs about the activity of a stream, not
570 // if it's for local playback. Note also that we use the same delay between both tests
571 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
572 } else if (isStreamActive(AudioSystem::MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
573 // while media is playing (or has recently played), use the same device
574 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
575 } else {
576 // when media is not playing anymore, fall back on the sonification behavior
577 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
578 }
579
580 break;
581
582 case STRATEGY_DTMF:
583 if (!isInCall()) {
584 // when off call, DTMF strategy follows the same rules as MEDIA strategy
585 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
586 break;
587 }
588 // when in call, DTMF and PHONE strategies follow the same rules
589 // FALL THROUGH
590
591 case STRATEGY_PHONE:
592 // for phone strategy, we first consider the forced use and then the available devices by order
593 // of priority
594 switch (mForceUse[AudioSystem::FOR_COMMUNICATION]) {
595 case AudioSystem::FORCE_BT_SCO:
596 if (!isInCall() || strategy != STRATEGY_DTMF) {
597 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
598 if (device) break;
599 }
600 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
601 if (device) break;
602 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
603 if (device) break;
604 // if SCO device is requested but no SCO device is available, fall back to default case
605 // FALL THROUGH
606
607 default: // FORCE_NONE
608 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
609 if (mHasA2dp && !isInCall() &&
610 (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
611 (getA2dpOutput() != 0) && !mA2dpSuspended) {
612 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
613 if (device) break;
614 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
615 if (device) break;
616 }
617 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
618 if (device) break;
619 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
620 if (device) break;
621 if (mPhoneState != AudioSystem::MODE_IN_CALL) {
622 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
623 if (device) break;
624 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
625 if (device) break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700626 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
627 if (device) break;
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700628 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
629 if (device) break;
630 }
631
632 // Allow voice call on USB ANLG DOCK headset
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700633 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
634 if (device) break;
635
636 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_EARPIECE;
637 if (device) break;
638 device = mDefaultOutputDevice;
639 if (device == AUDIO_DEVICE_NONE) {
640 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE");
641 }
642 break;
643
644 case AudioSystem::FORCE_SPEAKER:
645 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
646 // A2DP speaker when forcing to speaker output
647 if (mHasA2dp && !isInCall() &&
648 (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
649 (getA2dpOutput() != 0) && !mA2dpSuspended) {
650 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
651 if (device) break;
652 }
653 if (mPhoneState != AudioSystem::MODE_IN_CALL) {
654 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
655 if (device) break;
656 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
657 if (device) break;
658 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
659 if (device) break;
660 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
661 if (device) break;
Helen Zengbbce4952013-12-16 20:26:46 -0800662 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
663 if (device) break;
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700664 }
665 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
666 if (device) break;
667 device = mDefaultOutputDevice;
668 if (device == AUDIO_DEVICE_NONE) {
669 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER");
670 }
671 break;
672 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700673 // FIXME: Why do need to replace with speaker? If voice call is active
674 // We should use device from STRATEGY_PHONE
675#ifdef AUDIO_EXTN_FM_ENABLED
676 if (mAvailableOutputDevices & AUDIO_DEVICE_OUT_FM) {
677 if (mForceUse[AudioSystem::FOR_MEDIA] == AudioSystem::FORCE_SPEAKER) {
678 device = AUDIO_DEVICE_OUT_SPEAKER;
679 }
680 }
681#endif
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700682 break;
683
684 case STRATEGY_SONIFICATION:
685
686 // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
687 // handleIncallSonification().
688 if (isInCall()) {
689 device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
690 break;
691 }
692 // FALL THROUGH
693
694 case STRATEGY_ENFORCED_AUDIBLE:
695 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
696 // except:
697 // - when in call where it doesn't default to STRATEGY_PHONE behavior
698 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
699
700 if ((strategy == STRATEGY_SONIFICATION) ||
701 (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_SYSTEM_ENFORCED)) {
702 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
703 if (device == AUDIO_DEVICE_NONE) {
704 ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION");
705 }
706 }
707 // The second device used for sonification is the same as the device used by media strategy
708 // FALL THROUGH
709
710 case STRATEGY_MEDIA: {
711 uint32_t device2 = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700712
Mingming Yin4a72d652014-01-03 18:54:18 -0800713 if (isInCall() && (device == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700714 // when in call, get the device for Phone strategy
715 device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
716 break;
717 }
718#ifdef AUDIO_EXTN_FM_ENABLED
719 if (mForceUse[AudioSystem::FOR_MEDIA] == AudioSystem::FORCE_SPEAKER) {
720 device = AUDIO_DEVICE_OUT_SPEAKER;
721 break;
722 }
723#endif
724
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700725 if (strategy != STRATEGY_SONIFICATION) {
726 // no sonification on remote submix (e.g. WFD)
727 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
728 }
729 if ((device2 == AUDIO_DEVICE_NONE) &&
730 mHasA2dp && (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
731 (getA2dpOutput() != 0) && !mA2dpSuspended) {
732 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
733 if (device2 == AUDIO_DEVICE_NONE) {
734 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
735 }
736 if (device2 == AUDIO_DEVICE_NONE) {
737 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
738 }
739 }
740 if (device2 == AUDIO_DEVICE_NONE) {
741 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
742 }
743 if (device2 == AUDIO_DEVICE_NONE) {
744 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
745 }
746 if (device2 == AUDIO_DEVICE_NONE) {
747 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
748 }
749 if (device2 == AUDIO_DEVICE_NONE) {
750 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
751 }
752 if (device2 == AUDIO_DEVICE_NONE) {
753 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
754 }
Apoorv Raghuvanshi7566f8b2014-01-17 13:10:09 -0800755 if ((strategy != STRATEGY_SONIFICATION) && (device == AUDIO_DEVICE_NONE)
756 && (device2 == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700757 // no sonification on aux digital (e.g. HDMI)
758 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
759 }
760 if ((device2 == AUDIO_DEVICE_NONE) &&
Krishnankutty Kolathappilly4b7fdaa2013-12-16 00:40:11 -0800761 (mForceUse[AudioSystem::FOR_DOCK] == AudioSystem::FORCE_ANALOG_DOCK)
762 && (strategy != STRATEGY_SONIFICATION)) {
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700763 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
764 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700765#ifdef AUDIO_EXTN_FM_ENABLED
Apoorv Raghuvanshi7566f8b2014-01-17 13:10:09 -0800766 if ((strategy != STRATEGY_SONIFICATION) && (device == AUDIO_DEVICE_NONE)
767 && (device2 == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700768 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_FM_TX;
769 }
770#endif
771#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Apoorv Raghuvanshi7566f8b2014-01-17 13:10:09 -0800772 if ((strategy != STRATEGY_SONIFICATION) && (device == AUDIO_DEVICE_NONE)
773 && (device2 == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700774 // no sonification on WFD sink
775 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_PROXY;
776 }
777#endif
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700778 if (device2 == AUDIO_DEVICE_NONE) {
779 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
780 }
781
782 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
783 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
784 device |= device2;
785 if (device) break;
786 device = mDefaultOutputDevice;
787 if (device == AUDIO_DEVICE_NONE) {
788 ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA");
789 }
790 } break;
791
792 default:
793 ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
794 break;
795 }
796
797 ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
798 return device;
799}
800
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700801audio_devices_t AudioPolicyManager::getDeviceForInputSource(int inputSource)
802{
803 uint32_t device = AUDIO_DEVICE_NONE;
804
805 switch (inputSource) {
806 case AUDIO_SOURCE_VOICE_UPLINK:
807 if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
808 device = AUDIO_DEVICE_IN_VOICE_CALL;
809 break;
810 }
811 // FALL THROUGH
812
813 case AUDIO_SOURCE_DEFAULT:
814 case AUDIO_SOURCE_MIC:
815 case AUDIO_SOURCE_VOICE_RECOGNITION:
816 case AUDIO_SOURCE_HOTWORD:
817 case AUDIO_SOURCE_VOICE_COMMUNICATION:
818 if (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO &&
819 mAvailableInputDevices & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
820 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
821 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_WIRED_HEADSET) {
822 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
823 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET) {
824 device = AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET;
825 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
826 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
827 }
828 break;
829 case AUDIO_SOURCE_CAMCORDER:
830 if (mAvailableInputDevices & AUDIO_DEVICE_IN_BACK_MIC) {
831 device = AUDIO_DEVICE_IN_BACK_MIC;
832 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
833 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
834 }
835 break;
836 case AUDIO_SOURCE_VOICE_DOWNLINK:
837 case AUDIO_SOURCE_VOICE_CALL:
838 if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
839 device = AUDIO_DEVICE_IN_VOICE_CALL;
840 }
841 break;
842 case AUDIO_SOURCE_REMOTE_SUBMIX:
843 if (mAvailableInputDevices & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
844 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
845 }
846 break;
847#ifdef AUDIO_EXTN_FM_ENABLED
848 case AUDIO_SOURCE_FM_RX:
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700849 device = AUDIO_DEVICE_IN_FM_RX;
850 break;
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700851 case AUDIO_SOURCE_FM_RX_A2DP:
852 device = AUDIO_DEVICE_IN_FM_RX_A2DP;
853 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700854#endif
855 default:
856 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
857 break;
858 }
859 ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
860 return device;
861}
862
863AudioPolicyManager::device_category AudioPolicyManager::getDeviceCategory(audio_devices_t device)
864{
865 switch(getDeviceForVolume(device)) {
866 case AUDIO_DEVICE_OUT_EARPIECE:
867 return DEVICE_CATEGORY_EARPIECE;
868 case AUDIO_DEVICE_OUT_WIRED_HEADSET:
869 case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
870 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
871 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
872 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
873 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
874#ifdef AUDIO_EXTN_FM_ENABLED
875 case AUDIO_DEVICE_OUT_FM:
876#endif
877 return DEVICE_CATEGORY_HEADSET;
878 case AUDIO_DEVICE_OUT_SPEAKER:
879 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
880 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
881 case AUDIO_DEVICE_OUT_AUX_DIGITAL:
882 case AUDIO_DEVICE_OUT_USB_ACCESSORY:
883 case AUDIO_DEVICE_OUT_USB_DEVICE:
884 case AUDIO_DEVICE_OUT_REMOTE_SUBMIX:
885#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
886 case AUDIO_DEVICE_OUT_PROXY:
887#endif
888 default:
889 return DEVICE_CATEGORY_SPEAKER;
890 }
891}
892
893status_t AudioPolicyManager::checkAndSetVolume(int stream,
894 int index,
895 audio_io_handle_t output,
896 audio_devices_t device,
897 int delayMs,
898 bool force)
899{
900 ALOGV("checkAndSetVolume: index %d output %d device %x", index, output, device);
901 // do not change actual stream volume if the stream is muted
902 if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
903 ALOGVV("checkAndSetVolume() stream %d muted count %d",
904 stream, mOutputs.valueFor(output)->mMuteCount[stream]);
905 return NO_ERROR;
906 }
907
908 // do not change in call volume if bluetooth is connected and vice versa
909 if ((stream == AudioSystem::VOICE_CALL && mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
910 (stream == AudioSystem::BLUETOOTH_SCO && mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO)) {
911 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
912 stream, mForceUse[AudioSystem::FOR_COMMUNICATION]);
913 return INVALID_OPERATION;
914 }
915
916 float volume = computeVolume(stream, index, output, device);
917 // We actually change the volume if:
918 // - the float value returned by computeVolume() changed
919 // - the force flag is set
920 if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
921 force) {
922 mOutputs.valueFor(output)->mCurVolume[stream] = volume;
923 ALOGV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
924 // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
925 // enabled
926 if (stream == AudioSystem::BLUETOOTH_SCO) {
927 mpClientInterface->setStreamVolume(AudioSystem::VOICE_CALL, volume, output, delayMs);
928#ifdef AUDIO_EXTN_FM_ENABLED
929 } else if (stream == AudioSystem::MUSIC &&
930 output == mPrimaryOutput) {
931 float fmVolume = -1.0;
932 fmVolume = computeVolume(stream, index, output, device);
933 if (fmVolume >= 0) {
934 AudioParameter param = AudioParameter();
935 param.addFloat(String8("fm_volume"), fmVolume);
936 ALOGV("checkAndSetVolume setParameters fm_volume, volume=:%f delay=:%d",fmVolume,delayMs*2);
937 //Double delayMs to avoid sound burst while device switch.
938 mpClientInterface->setParameters(mPrimaryOutput, param.toString(), delayMs*2);
939 }
940#endif
941 }
942 mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs);
943 }
944
945 if (stream == AudioSystem::VOICE_CALL ||
946 stream == AudioSystem::BLUETOOTH_SCO) {
947 float voiceVolume;
Vidyakumar Athota35543452014-04-16 10:00:39 -0700948 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
949 if (stream == AudioSystem::VOICE_CALL) {
950 voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
951 } else {
952 voiceVolume = 1.0;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700953 }
954
955 if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) {
956 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
957 mLastVoiceVolume = voiceVolume;
958 }
959 }
960
961 return NO_ERROR;
962}
963
964
965float AudioPolicyManager::computeVolume(int stream,
966 int index,
967 audio_io_handle_t output,
968 audio_devices_t device)
969{
970 float volume = 1.0;
971 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
972
973 if (device == AUDIO_DEVICE_NONE) {
974 device = outputDesc->device();
975 }
976
977 // if volume is not 0 (not muted), force media volume to max on digital output
978 if (stream == AudioSystem::MUSIC &&
979 index != mStreams[stream].mIndexMin &&
980 (device == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
981 device == AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET ||
982 device == AUDIO_DEVICE_OUT_USB_ACCESSORY ||
983#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
984 device == AUDIO_DEVICE_OUT_PROXY ||
985#endif
986 device == AUDIO_DEVICE_OUT_USB_DEVICE )) {
987 return 1.0;
988 }
989#ifdef AUDIO_EXTN_INCALL_MUSIC_ENABLED
990 if (stream == AudioSystem::INCALL_MUSIC) {
991 return 1.0;
992 }
993#endif
994 return AudioPolicyManagerBase::computeVolume(stream, index, output, device);
995}
Naresh Tanniru36c08932014-01-27 18:40:53 +0530996
997
998audio_io_handle_t AudioPolicyManager::getOutput(AudioSystem::stream_type stream,
999 uint32_t samplingRate,
1000 uint32_t format,
1001 uint32_t channelMask,
1002 AudioSystem::output_flags flags,
1003 const audio_offload_info_t *offloadInfo)
1004{
1005 audio_io_handle_t output = 0;
1006 uint32_t latency = 0;
1007 routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);
1008 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1009 IOProfile *profile = NULL;
1010
1011#ifdef VOICE_CONCURRENCY
Naresh Tanniru811f4982014-03-18 17:25:32 +05301012 char propValue[PROPERTY_VALUE_MAX];
1013 bool prop_play_enabled=false, prop_voip_enabled = false;
1014
1015 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1016 prop_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1017 }
1018
1019 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1020 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1021 }
1022
1023 if (prop_play_enabled) {
1024 //check if voice call is active / running in background
1025 if((AudioSystem::MODE_IN_CALL == mPhoneState) ||
1026 ((AudioSystem::MODE_IN_CALL == mPrevPhoneState)
1027 && (AudioSystem::MODE_IN_COMMUNICATION == mPhoneState)))
1028 {
1029 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1030 if(prop_voip_enabled) {
1031 ALOGD(" IN call mode returing no output .. for VoIP usecase flags: %x ", flags );
1032 // flags = (AudioSystem::output_flags)AUDIO_OUTPUT_FLAG_FAST;
1033 return 0;
1034 }
1035 }
1036 else {
1037 ALOGD(" IN call mode adding ULL flags .. flags: %x ", flags );
1038 flags = (AudioSystem::output_flags)AUDIO_OUTPUT_FLAG_FAST;
1039 }
1040 }
1041 } else if (prop_voip_enabled) {
1042 //check if voice call is active / running in background
1043 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1044 //return only ULL ouput
1045 if((AudioSystem::MODE_IN_CALL == mPhoneState) ||
1046 ((AudioSystem::MODE_IN_CALL == mPrevPhoneState)
1047 && (AudioSystem::MODE_IN_COMMUNICATION == mPhoneState)))
1048 {
1049 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1050 ALOGD(" IN call mode returing no output .. for VoIP usecase flags: %x ", flags );
1051 // flags = (AudioSystem::output_flags)AUDIO_OUTPUT_FLAG_FAST;
1052 return 0;
1053 }
1054 }
Naresh Tanniru36c08932014-01-27 18:40:53 +05301055 }
1056#endif
1057
1058#ifdef WFD_CONCURRENCY
1059 if ((mAvailableOutputDevices & AUDIO_DEVICE_OUT_PROXY)
1060 && (stream != AudioSystem::MUSIC)) {
1061 ALOGV(" WFD mode adding ULL flags for non music stream.. flags: %x ", flags );
1062 //For voip paths
1063 if(flags & AudioSystem::OUTPUT_FLAG_DIRECT)
1064 flags = AudioSystem::OUTPUT_FLAG_DIRECT;
1065 else //route every thing else to ULL path
1066 flags = (AudioSystem::output_flags)AUDIO_OUTPUT_FLAG_FAST;
1067 }
1068#endif
1069
1070 ALOGV(" getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x ",
1071 device, stream, samplingRate, format, channelMask, flags);
1072
1073
1074
1075#ifdef AUDIO_POLICY_TEST
1076 if (mCurOutput != 0) {
1077 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1078 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1079
1080 if (mTestOutputs[mCurOutput] == 0) {
1081 ALOGV("getOutput() opening test output");
1082 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL);
1083 outputDesc->mDevice = mTestDevice;
1084 outputDesc->mSamplingRate = mTestSamplingRate;
1085 outputDesc->mFormat = mTestFormat;
1086 outputDesc->mChannelMask = mTestChannels;
1087 outputDesc->mLatency = mTestLatencyMs;
1088 outputDesc->mFlags = (audio_output_flags_t)(mDirectOutput ? AudioSystem::OUTPUT_FLAG_DIRECT : 0);
1089 outputDesc->mRefCount[stream] = 0;
1090 mTestOutputs[mCurOutput] = mpClientInterface->openOutput(0, &outputDesc->mDevice,
1091 &outputDesc->mSamplingRate,
1092 &outputDesc->mFormat,
1093 &outputDesc->mChannelMask,
1094 &outputDesc->mLatency,
1095 outputDesc->mFlags,
1096 offloadInfo);
1097 if (mTestOutputs[mCurOutput]) {
1098 AudioParameter outputCmd = AudioParameter();
1099 outputCmd.addInt(String8("set_id"),mCurOutput);
1100 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1101 addOutput(mTestOutputs[mCurOutput], outputDesc);
1102 }
1103 }
1104 return mTestOutputs[mCurOutput];
1105 }
1106#endif //AUDIO_POLICY_TEST
1107
1108 // open a direct output if required by specified parameters
1109 //force direct flag if offload flag is set: offloading implies a direct output stream
1110 // and all common behaviors are driven by checking only the direct flag
1111 // this should normally be set appropriately in the policy configuration file
1112 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1113 flags = (AudioSystem::output_flags)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1114 }
1115
1116 if ((format == AudioSystem::PCM_16_BIT) &&(AudioSystem::popCount(channelMask) > 2)) {
1117 ALOGV("owerwrite flag(%x) for PCM16 multi-channel(CM:%x) playback", flags ,channelMask);
1118 flags = (AudioSystem::output_flags)AUDIO_OUTPUT_FLAG_DIRECT;
1119 }
1120
1121 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1122 // creating an offloaded track and tearing it down immediately after start when audioflinger
1123 // detects there is an active non offloadable effect.
1124 // FIXME: We should check the audio session here but we do not have it in this context.
1125 // This may prevent offloading in rare situations where effects are left active by apps
1126 // in the background.
1127 if ((((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
1128 !isNonOffloadableEffectEnabled()) &&
1129 flags & AUDIO_OUTPUT_FLAG_DIRECT) {
1130 profile = getProfileForDirectOutput(device,
1131 samplingRate,
1132 format,
1133 channelMask,
1134 (audio_output_flags_t)flags);
1135 }
1136
1137 if (profile != NULL) {
1138 AudioOutputDescriptor *outputDesc = NULL;
1139
Subhash Chandra Bose Naripeddy5eb02f72014-04-11 17:54:09 -07001140#ifdef MULTIPLE_OFFLOAD_ENABLED
1141 bool multiOffloadEnabled = false;
1142 char value[PROPERTY_VALUE_MAX] = {0};
1143 property_get("audio.offload.multiple.enabled", value, NULL);
1144 if (atoi(value) || !strncmp("true", value, 4))
1145 multiOffloadEnabled = true;
1146 // if multiple concurrent offload decode is supported
1147 // do no check for reuse and also don't close previous output if its offload
1148 // previous output will be closed during track destruction
1149 if (multiOffloadEnabled)
1150 goto get_output__new_output_desc;
1151#endif
Naresh Tanniru36c08932014-01-27 18:40:53 +05301152 for (size_t i = 0; i < mOutputs.size(); i++) {
1153 AudioOutputDescriptor *desc = mOutputs.valueAt(i);
1154 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1155 outputDesc = desc;
1156 // reuse direct output if currently open and configured with same parameters
1157 if ((samplingRate == outputDesc->mSamplingRate) &&
1158 (format == outputDesc->mFormat) &&
1159 (channelMask == outputDesc->mChannelMask)) {
1160 outputDesc->mDirectOpenCount++;
1161 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1162 return mOutputs.keyAt(i);
1163 }
1164 }
1165 }
1166 // close direct output if currently open and configured with different parameters
1167 if (outputDesc != NULL) {
1168 closeOutput(outputDesc->mId);
1169 }
Subhash Chandra Bose Naripeddy5eb02f72014-04-11 17:54:09 -07001170get_output__new_output_desc:
Naresh Tanniru36c08932014-01-27 18:40:53 +05301171 outputDesc = new AudioOutputDescriptor(profile);
1172 outputDesc->mDevice = device;
1173 outputDesc->mSamplingRate = samplingRate;
1174 outputDesc->mFormat = (audio_format_t)format;
1175 outputDesc->mChannelMask = (audio_channel_mask_t)channelMask;
1176 outputDesc->mLatency = 0;
1177 outputDesc->mFlags =(audio_output_flags_t) (outputDesc->mFlags | flags);
1178 outputDesc->mRefCount[stream] = 0;
1179 outputDesc->mStopTime[stream] = 0;
1180 outputDesc->mDirectOpenCount = 1;
1181 output = mpClientInterface->openOutput(profile->mModule->mHandle,
1182 &outputDesc->mDevice,
1183 &outputDesc->mSamplingRate,
1184 &outputDesc->mFormat,
1185 &outputDesc->mChannelMask,
1186 &outputDesc->mLatency,
1187 outputDesc->mFlags,
1188 offloadInfo);
1189
1190 // only accept an output with the requested parameters
1191 if (output == 0 ||
1192 (samplingRate != 0 && samplingRate != outputDesc->mSamplingRate) ||
1193 (format != 0 && format != outputDesc->mFormat) ||
1194 (channelMask != 0 && channelMask != outputDesc->mChannelMask)) {
1195 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1196 "format %d %d, channelMask %04x %04x", output, samplingRate,
1197 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1198 outputDesc->mChannelMask);
1199 if (output != 0) {
1200 mpClientInterface->closeOutput(output);
1201 }
1202 delete outputDesc;
1203 return 0;
1204 }
1205 audio_io_handle_t srcOutput = getOutputForEffect();
1206 addOutput(output, outputDesc);
1207 audio_io_handle_t dstOutput = getOutputForEffect();
1208 if (dstOutput == output) {
1209 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1210 }
1211 mPreviousOutputs = mOutputs;
1212 ALOGV("getOutput() returns new direct output %d", output);
1213 return output;
1214 }
1215
1216 // ignoring channel mask due to downmix capability in mixer
1217
1218 // open a non direct output
1219
1220 // for non direct outputs, only PCM is supported
1221 if (audio_is_linear_pcm((audio_format_t)format)) {
1222 // get which output is suitable for the specified stream. The actual
1223 // routing change will happen when startOutput() will be called
1224 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1225
1226 output = selectOutput(outputs, flags);
1227 }
1228 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1229 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1230
1231 ALOGV("getOutput() returns output %d", output);
1232
1233 return output;
1234}
1235
1236
1237// This function checks for the parameters which can be offloaded.
1238// This can be enhanced depending on the capability of the DSP and policy
1239// of the system.
1240bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
1241{
1242 ALOGV(" isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
1243 " BitRate=%u, duration=%lld us, has_video=%d",
1244 offloadInfo.sample_rate, offloadInfo.channel_mask,
1245 offloadInfo.format,
1246 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
1247 offloadInfo.has_video);
1248
1249#ifdef VOICE_CONCURRENCY
Naresh Tanniru811f4982014-03-18 17:25:32 +05301250 char concpropValue[PROPERTY_VALUE_MAX];
1251 if(property_get("voice.playback.conc.disabled", concpropValue, NULL)) {
1252 bool propenabled = atoi(concpropValue) || !strncmp("true", concpropValue, 4);
1253 if (propenabled) {
1254 if(isInCall())
1255 {
1256 ALOGD("\n blocking compress offload on call mode\n");
1257 return false;
1258 }
1259 }
Naresh Tanniru36c08932014-01-27 18:40:53 +05301260 }
Naresh Tanniru811f4982014-03-18 17:25:32 +05301261
Naresh Tanniru36c08932014-01-27 18:40:53 +05301262#endif
Naresh Tanniru36c08932014-01-27 18:40:53 +05301263 // Check if stream type is music, then only allow offload as of now.
1264 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
1265 {
1266 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
1267 return false;
1268 }
1269
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001270 char propValue[PROPERTY_VALUE_MAX];
1271 bool pcmOffload = false;
1272 if (audio_is_offload_pcm(offloadInfo.format)) {
1273 if(property_get("audio.offload.pcm.enable", propValue, NULL)) {
Naresh Tanniru36c08932014-01-27 18:40:53 +05301274 bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001275 if (prop_enabled) {
1276 ALOGW("PCM offload property is enabled");
1277 pcmOffload = true;
Naresh Tanniru36c08932014-01-27 18:40:53 +05301278 }
1279 }
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001280 if (!pcmOffload) {
1281 ALOGV("PCM offload disabled by property audio.offload.pcm.enable");
1282 return false;
1283 }
1284 }
1285
1286 if (!pcmOffload) {
1287 // Check if offload has been disabled
1288 if (property_get("audio.offload.disable", propValue, "0")) {
1289 if (atoi(propValue) != 0) {
1290 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
1291 return false;
Naresh Tanniru36c08932014-01-27 18:40:53 +05301292 }
1293 }
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001294
1295 //check if it's multi-channel AAC format
1296 if (AudioSystem::popCount(offloadInfo.channel_mask) > 2
1297 && offloadInfo.format == AUDIO_FORMAT_AAC) {
1298 ALOGV("offload disabled for multi-channel AAC format");
1299 return false;
1300 }
1301
1302 if (offloadInfo.has_video)
1303 {
1304 if(property_get("av.offload.enable", propValue, NULL)) {
1305 bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1306 if (!prop_enabled) {
1307 ALOGW("offload disabled by av.offload.enable = %s ", propValue );
1308 return false;
1309 }
1310 } else {
1311 return false;
1312 }
1313
1314 if(offloadInfo.is_streaming) {
1315 if (property_get("av.streaming.offload.enable", propValue, NULL)) {
1316 bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1317 if (!prop_enabled) {
1318 ALOGW("offload disabled by av.streaming.offload.enable = %s ", propValue );
1319 return false;
1320 }
1321 } else {
1322 //Do not offload AV streamnig if the property is not defined
1323 return false;
1324 }
1325 }
1326 ALOGV("isOffloadSupported: has_video == true, property\
1327 set to enable offload");
1328 }
Naresh Tanniru36c08932014-01-27 18:40:53 +05301329 }
1330
1331 //If duration is less than minimum value defined in property, return false
1332 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
1333 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
1334 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
1335 return false;
1336 }
1337 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
1338 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
1339 //duration checks only valid for MP3/AAC formats,
1340 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001341 if (offloadInfo.format == AUDIO_FORMAT_MP3 || offloadInfo.format == AUDIO_FORMAT_AAC || pcmOffload)
Naresh Tanniru36c08932014-01-27 18:40:53 +05301342 return false;
1343 }
1344
1345 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1346 // creating an offloaded track and tearing it down immediately after start when audioflinger
1347 // detects there is an active non offloadable effect.
1348 // FIXME: We should check the audio session here but we do not have it in this context.
1349 // This may prevent offloading in rare situations where effects are left active by apps
1350 // in the background.
1351 if (isNonOffloadableEffectEnabled()) {
1352 return false;
1353 }
1354
1355 // See if there is a profile to support this.
1356 // AUDIO_DEVICE_NONE
1357 IOProfile *profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
1358 offloadInfo.sample_rate,
1359 offloadInfo.format,
1360 offloadInfo.channel_mask,
1361 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1362 ALOGV("isOffloadSupported() profile %sfound", profile != NULL ? "" : "NOT ");
1363 return (profile != NULL);
1364}
1365
1366void AudioPolicyManager::setPhoneState(int state)
1367
1368{
Naresh Tanniru811f4982014-03-18 17:25:32 +05301369 ALOGD("setPhoneState() state %d", state);
Naresh Tanniru36c08932014-01-27 18:40:53 +05301370 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
1371 if (state < 0 || state >= AudioSystem::NUM_MODES) {
1372 ALOGW("setPhoneState() invalid state %d", state);
1373 return;
1374 }
1375
1376 if (state == mPhoneState ) {
1377 ALOGW("setPhoneState() setting same state %d", state);
1378 return;
1379 }
1380
1381 // if leaving call state, handle special case of active streams
1382 // pertaining to sonification strategy see handleIncallSonification()
1383 if (isInCall()) {
1384 ALOGV("setPhoneState() in call state management: new state is %d", state);
1385 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
1386 handleIncallSonification(stream, false, true);
1387 }
1388 }
1389
1390 // store previous phone state for management of sonification strategy below
1391 int oldState = mPhoneState;
1392 mPhoneState = state;
1393 bool force = false;
1394
1395 // are we entering or starting a call
1396 if (!isStateInCall(oldState) && isStateInCall(state)) {
1397 ALOGV(" Entering call in setPhoneState()");
1398 // force routing command to audio hardware when starting a call
1399 // even if no device change is needed
1400 force = true;
1401 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
1402 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
1403 sVolumeProfiles[AUDIO_STREAM_VOICE_CALL][j];
1404 }
1405 } else if (isStateInCall(oldState) && !isStateInCall(state)) {
1406 ALOGV(" Exiting call in setPhoneState()");
1407 // force routing command to audio hardware when exiting a call
1408 // even if no device change is needed
1409 force = true;
1410 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
1411 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
1412 sVolumeProfiles[AUDIO_STREAM_DTMF][j];
1413 }
1414 } else if (isStateInCall(state) && (state != oldState)) {
1415 ALOGV(" Switching between telephony and VoIP in setPhoneState()");
1416 // force routing command to audio hardware when switching between telephony and VoIP
1417 // even if no device change is needed
1418 force = true;
1419 }
1420
1421 // check for device and output changes triggered by new phone state
1422 newDevice = getNewDevice(mPrimaryOutput, false /*fromCache*/);
1423 checkA2dpSuspend();
1424 checkOutputForAllStrategies();
1425 updateDevicesAndOutputs();
1426
1427 AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
1428
1429 // force routing command to audio hardware when ending call
1430 // even if no device change is needed
1431 if (isStateInCall(oldState) && newDevice == AUDIO_DEVICE_NONE) {
1432 newDevice = hwOutputDesc->device();
1433 }
Naresh Tanniru811f4982014-03-18 17:25:32 +05301434#ifdef VOICE_CONCURRENCY
1435 char propValue[PROPERTY_VALUE_MAX];
1436 bool prop_playback_enabled = false, prop_rec_enabled=false, prop_voip_enabled = false;
1437
1438 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1439 prop_playback_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1440 }
1441
1442 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
1443 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1444 }
1445
1446 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1447 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1448 }
1449
1450 if((AudioSystem::MODE_IN_CALL != oldState) && (AudioSystem::MODE_IN_CALL == state)) {
1451 ALOGD("Entering to call mode oldState :: %d state::%d ",oldState, state);
1452
1453 if(prop_playback_enabled) {
1454 //Call invalidate to reset all opened non ULL audio tracks
1455 // Move tracks associated to this strategy from previous output to new output
1456 for (int i = AudioSystem::SYSTEM; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
1457 ALOGV(" Invalidate on call mode for stream :: %d ", i);
1458 //FIXME see fixme on name change
1459 mpClientInterface->setStreamOutput((AudioSystem::stream_type)i,
1460 0 /* ignored */);
1461 }
1462 }
1463
1464 if(prop_rec_enabled) {
1465 //Close all active inputs
1466 audio_io_handle_t activeInput = getActiveInput();
1467 if (activeInput != 0) {
1468 AudioInputDescriptor *activeDesc = mInputs.valueFor(activeInput);
1469 switch(activeDesc->mInputSource) {
1470 case AUDIO_SOURCE_VOICE_UPLINK:
1471 case AUDIO_SOURCE_VOICE_DOWNLINK:
1472 case AUDIO_SOURCE_VOICE_CALL:
1473 ALOGD("FOUND active input during call active: %d",activeDesc->mInputSource);
1474 break;
1475
1476 case AUDIO_SOURCE_VOICE_COMMUNICATION:
1477 if(prop_voip_enabled) {
1478 ALOGD("CLOSING VoIP input source on call setup :%d ",activeDesc->mInputSource);
1479 stopInput(activeInput);
1480 releaseInput(activeInput);
1481 }
1482 break;
1483
1484 default:
1485 ALOGD("CLOSING input on call setup for inputSource: %d",activeDesc->mInputSource);
1486 stopInput(activeInput);
1487 releaseInput(activeInput);
1488 break;
1489 }
1490 }
1491 } else if(prop_voip_enabled) {
1492 audio_io_handle_t activeInput = getActiveInput();
1493 if (activeInput != 0) {
1494 AudioInputDescriptor *activeDesc = mInputs.valueFor(activeInput);
1495 if(AUDIO_SOURCE_VOICE_COMMUNICATION == activeDesc->mInputSource) {
1496 ALOGD("CLOSING VoIP on call setup : %d",activeDesc->mInputSource);
1497 stopInput(activeInput);
1498 releaseInput(activeInput);
1499 }
1500 }
1501 }
1502
1503 //suspend PCM (deep-buffer) output & close compress & direct tracks
1504 for (size_t i = 0; i < mOutputs.size(); i++) {
1505 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
1506 if (((!outputDesc->isDuplicated() &&outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY))
1507 && prop_playback_enabled) {
1508 ALOGD(" calling suspendOutput on call mdoe for primary output");
1509 mpClientInterface->suspendOutput(mOutputs.keyAt(i));
1510 } //Close compress all sessions
1511 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
1512 && prop_playback_enabled) {
1513 ALOGD(" calling closeOutput on call mdoe for COMPRESS output");
1514 closeOutput(mOutputs.keyAt(i));
1515 }
1516 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_VOIP_RX)
1517 && prop_voip_enabled) {
1518 ALOGD(" calling closeOutput on call mdoe for DIRECT output");
1519 closeOutput(mOutputs.keyAt(i));
1520 }
1521 }
1522 }
1523
1524 if((AudioSystem::MODE_IN_CALL == oldState) && (AudioSystem::MODE_IN_CALL != state)
1525 && prop_playback_enabled) {
1526 ALOGD("EXITING from call mode oldState :: %d state::%d \n",oldState, state);
1527 //restore PCM (deep-buffer) output after call termination
1528 for (size_t i = 0; i < mOutputs.size(); i++) {
1529 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
1530 if (!outputDesc->isDuplicated() &&outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1531 ALOGD("calling restoreOutput after call mode for primary output");
1532 mpClientInterface->restoreOutput(mOutputs.keyAt(i));
1533 }
1534 }
1535 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
1536 for (int i = AudioSystem::SYSTEM; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
1537 ALOGD("Invalidate on call mode for stream :: %d ", i);
1538 //FIXME see fixme on name change
1539 mpClientInterface->setStreamOutput((AudioSystem::stream_type)i,
1540 0 /* ignored */);
1541 }
1542 }
1543 mPrevPhoneState = oldState;
1544#endif
Naresh Tanniru36c08932014-01-27 18:40:53 +05301545
1546 int delayMs = 0;
1547 if (isStateInCall(state)) {
1548 nsecs_t sysTime = systemTime();
1549 for (size_t i = 0; i < mOutputs.size(); i++) {
1550 AudioOutputDescriptor *desc = mOutputs.valueAt(i);
1551 // mute media and sonification strategies and delay device switch by the largest
1552 // latency of any output where either strategy is active.
1553 // This avoid sending the ring tone or music tail into the earpiece or headset.
1554 if ((desc->isStrategyActive(STRATEGY_MEDIA,
1555 SONIFICATION_HEADSET_MUSIC_DELAY,
1556 sysTime) ||
1557 desc->isStrategyActive(STRATEGY_SONIFICATION,
1558 SONIFICATION_HEADSET_MUSIC_DELAY,
1559 sysTime)) &&
1560 (delayMs < (int)desc->mLatency*2)) {
1561 delayMs = desc->mLatency*2;
1562 }
1563 setStrategyMute(STRATEGY_MEDIA, true, mOutputs.keyAt(i));
1564 setStrategyMute(STRATEGY_MEDIA, false, mOutputs.keyAt(i), MUTE_TIME_MS,
1565 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
1566 setStrategyMute(STRATEGY_SONIFICATION, true, mOutputs.keyAt(i));
1567 setStrategyMute(STRATEGY_SONIFICATION, false, mOutputs.keyAt(i), MUTE_TIME_MS,
1568 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
1569 }
1570 }
1571
1572 // change routing is necessary
1573 setOutputDevice(mPrimaryOutput, newDevice, force, delayMs);
1574
1575 // if entering in call state, handle special case of active streams
1576 // pertaining to sonification strategy see handleIncallSonification()
1577 if (isStateInCall(state)) {
1578 ALOGV("setPhoneState() in call state management: new state is %d", state);
1579 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
1580 handleIncallSonification(stream, true, true);
1581 }
1582 }
1583
1584 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
1585 if (state == AudioSystem::MODE_RINGTONE &&
1586 isStreamActive(AudioSystem::MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
1587 mLimitRingtoneVolume = true;
1588 } else {
1589 mLimitRingtoneVolume = false;
1590 }
Naresh Tanniru811f4982014-03-18 17:25:32 +05301591 ALOGD(" End of setPhoneState ... mPhoneState: %d ",mPhoneState);
Naresh Tanniru36c08932014-01-27 18:40:53 +05301592}
1593
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001594extern "C" AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001595{
1596 return new AudioPolicyManager(clientInterface);
1597}
1598
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001599extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001600{
1601 delete interface;
1602}
1603
1604}; // namespace android