blob: 275d0c568f38789685ace38c71e557e1288e98e4 [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
Sidipotu Ashokf43018c2014-05-02 16:21:50 +053055 ALOGD("setDeviceConnectionState() device: %x, state %d, address %s", device, state, device_address);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070056
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{
Sidipotu Ashokf43018c2014-05-02 16:21:50 +0530306 ALOGD("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700307
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
Sidipotu Ashokf43018c2014-05-02 16:21:50 +0530403 ALOGD("getInput() inputSource %d, samplingRate %d, format %d, channelMask %x, acoustics %x",
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700404 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);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +0530512 ALOGD("getInput() returns input %d", input);
513
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700514 return input;
515}
516
517AudioPolicyManager::routing_strategy AudioPolicyManager::getStrategy(AudioSystem::stream_type stream)
518{
Naresh Tanniru36c08932014-01-27 18:40:53 +0530519 // stream to strategy mapping
520 switch (stream) {
521 case AudioSystem::VOICE_CALL:
522 case AudioSystem::BLUETOOTH_SCO:
523 return STRATEGY_PHONE;
524 case AudioSystem::RING:
525 case AudioSystem::ALARM:
526 return STRATEGY_SONIFICATION;
527 case AudioSystem::NOTIFICATION:
528 return STRATEGY_SONIFICATION_RESPECTFUL;
529 case AudioSystem::DTMF:
530 return STRATEGY_DTMF;
531 default:
532 ALOGE("unknown stream type");
533 case AudioSystem::SYSTEM:
534 // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
535 // while key clicks are played produces a poor result
536 case AudioSystem::TTS:
537 case AudioSystem::MUSIC:
538#ifdef AUDIO_EXTN_INCALL_MUSIC_ENABLED
539 case AudioSystem::INCALL_MUSIC:
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700540#endif
Naresh Tanniru36c08932014-01-27 18:40:53 +0530541#ifdef QCOM_INCALL_MUSIC_ENABLED
542 case AudioSystem::INCALL_MUSIC:
543#endif
544 return STRATEGY_MEDIA;
545 case AudioSystem::ENFORCED_AUDIBLE:
546 return STRATEGY_ENFORCED_AUDIBLE;
547 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700548
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700549}
550
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700551audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
552 bool fromCache)
553{
554 uint32_t device = AUDIO_DEVICE_NONE;
555
556 if (fromCache) {
557 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
558 strategy, mDeviceForStrategy[strategy]);
559 return mDeviceForStrategy[strategy];
560 }
561
562 switch (strategy) {
563
564 case STRATEGY_SONIFICATION_RESPECTFUL:
565 if (isInCall()) {
566 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
567 } else if (isStreamActiveRemotely(AudioSystem::MUSIC,
568 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
569 // while media is playing on a remote device, use the the sonification behavior.
570 // Note that we test this usecase before testing if media is playing because
571 // the isStreamActive() method only informs about the activity of a stream, not
572 // if it's for local playback. Note also that we use the same delay between both tests
573 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
574 } else if (isStreamActive(AudioSystem::MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
575 // while media is playing (or has recently played), use the same device
576 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
577 } else {
578 // when media is not playing anymore, fall back on the sonification behavior
579 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
580 }
581
582 break;
583
584 case STRATEGY_DTMF:
585 if (!isInCall()) {
586 // when off call, DTMF strategy follows the same rules as MEDIA strategy
587 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
588 break;
589 }
590 // when in call, DTMF and PHONE strategies follow the same rules
591 // FALL THROUGH
592
593 case STRATEGY_PHONE:
594 // for phone strategy, we first consider the forced use and then the available devices by order
595 // of priority
596 switch (mForceUse[AudioSystem::FOR_COMMUNICATION]) {
597 case AudioSystem::FORCE_BT_SCO:
598 if (!isInCall() || strategy != STRATEGY_DTMF) {
599 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
600 if (device) break;
601 }
602 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
603 if (device) break;
604 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
605 if (device) break;
606 // if SCO device is requested but no SCO device is available, fall back to default case
607 // FALL THROUGH
608
609 default: // FORCE_NONE
610 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
611 if (mHasA2dp && !isInCall() &&
612 (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
613 (getA2dpOutput() != 0) && !mA2dpSuspended) {
614 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
615 if (device) break;
616 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
617 if (device) break;
618 }
619 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
620 if (device) break;
621 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
622 if (device) break;
623 if (mPhoneState != AudioSystem::MODE_IN_CALL) {
624 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
625 if (device) break;
626 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
627 if (device) break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700628 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
629 if (device) break;
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700630 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
631 if (device) break;
632 }
633
634 // Allow voice call on USB ANLG DOCK headset
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700635 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
636 if (device) break;
637
638 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_EARPIECE;
639 if (device) break;
640 device = mDefaultOutputDevice;
641 if (device == AUDIO_DEVICE_NONE) {
642 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE");
643 }
644 break;
645
646 case AudioSystem::FORCE_SPEAKER:
647 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
648 // A2DP speaker when forcing to speaker output
649 if (mHasA2dp && !isInCall() &&
650 (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
651 (getA2dpOutput() != 0) && !mA2dpSuspended) {
652 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
653 if (device) break;
654 }
655 if (mPhoneState != AudioSystem::MODE_IN_CALL) {
656 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
657 if (device) break;
658 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
659 if (device) break;
660 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
661 if (device) break;
662 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
663 if (device) break;
Helen Zengbbce4952013-12-16 20:26:46 -0800664 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
665 if (device) break;
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700666 }
667 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
668 if (device) break;
669 device = mDefaultOutputDevice;
670 if (device == AUDIO_DEVICE_NONE) {
671 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER");
672 }
673 break;
674 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700675 // FIXME: Why do need to replace with speaker? If voice call is active
676 // We should use device from STRATEGY_PHONE
677#ifdef AUDIO_EXTN_FM_ENABLED
678 if (mAvailableOutputDevices & AUDIO_DEVICE_OUT_FM) {
679 if (mForceUse[AudioSystem::FOR_MEDIA] == AudioSystem::FORCE_SPEAKER) {
680 device = AUDIO_DEVICE_OUT_SPEAKER;
681 }
682 }
683#endif
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700684 break;
685
686 case STRATEGY_SONIFICATION:
687
688 // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
689 // handleIncallSonification().
690 if (isInCall()) {
691 device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
692 break;
693 }
694 // FALL THROUGH
695
696 case STRATEGY_ENFORCED_AUDIBLE:
697 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
698 // except:
699 // - when in call where it doesn't default to STRATEGY_PHONE behavior
700 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
701
702 if ((strategy == STRATEGY_SONIFICATION) ||
703 (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_SYSTEM_ENFORCED)) {
704 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
705 if (device == AUDIO_DEVICE_NONE) {
706 ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION");
707 }
708 }
709 // The second device used for sonification is the same as the device used by media strategy
710 // FALL THROUGH
711
712 case STRATEGY_MEDIA: {
713 uint32_t device2 = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700714
Mingming Yin4a72d652014-01-03 18:54:18 -0800715 if (isInCall() && (device == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700716 // when in call, get the device for Phone strategy
717 device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
718 break;
719 }
720#ifdef AUDIO_EXTN_FM_ENABLED
721 if (mForceUse[AudioSystem::FOR_MEDIA] == AudioSystem::FORCE_SPEAKER) {
722 device = AUDIO_DEVICE_OUT_SPEAKER;
723 break;
724 }
725#endif
726
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700727 if (strategy != STRATEGY_SONIFICATION) {
728 // no sonification on remote submix (e.g. WFD)
729 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
730 }
731 if ((device2 == AUDIO_DEVICE_NONE) &&
732 mHasA2dp && (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
733 (getA2dpOutput() != 0) && !mA2dpSuspended) {
734 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
735 if (device2 == AUDIO_DEVICE_NONE) {
736 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
737 }
738 if (device2 == AUDIO_DEVICE_NONE) {
739 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
740 }
741 }
742 if (device2 == AUDIO_DEVICE_NONE) {
743 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
744 }
745 if (device2 == AUDIO_DEVICE_NONE) {
746 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
747 }
748 if (device2 == AUDIO_DEVICE_NONE) {
749 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
750 }
751 if (device2 == AUDIO_DEVICE_NONE) {
752 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
753 }
754 if (device2 == AUDIO_DEVICE_NONE) {
755 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
756 }
Apoorv Raghuvanshi7566f8b2014-01-17 13:10:09 -0800757 if ((strategy != STRATEGY_SONIFICATION) && (device == AUDIO_DEVICE_NONE)
758 && (device2 == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700759 // no sonification on aux digital (e.g. HDMI)
760 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
761 }
762 if ((device2 == AUDIO_DEVICE_NONE) &&
Krishnankutty Kolathappilly4b7fdaa2013-12-16 00:40:11 -0800763 (mForceUse[AudioSystem::FOR_DOCK] == AudioSystem::FORCE_ANALOG_DOCK)
764 && (strategy != STRATEGY_SONIFICATION)) {
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700765 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
766 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700767#ifdef AUDIO_EXTN_FM_ENABLED
Apoorv Raghuvanshi7566f8b2014-01-17 13:10:09 -0800768 if ((strategy != STRATEGY_SONIFICATION) && (device == AUDIO_DEVICE_NONE)
769 && (device2 == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700770 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_FM_TX;
771 }
772#endif
773#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Apoorv Raghuvanshi7566f8b2014-01-17 13:10:09 -0800774 if ((strategy != STRATEGY_SONIFICATION) && (device == AUDIO_DEVICE_NONE)
775 && (device2 == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700776 // no sonification on WFD sink
777 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_PROXY;
778 }
779#endif
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700780 if (device2 == AUDIO_DEVICE_NONE) {
781 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
782 }
783
784 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
785 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
786 device |= device2;
787 if (device) break;
788 device = mDefaultOutputDevice;
789 if (device == AUDIO_DEVICE_NONE) {
790 ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA");
791 }
792 } break;
793
794 default:
795 ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
796 break;
797 }
798
799 ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
800 return device;
801}
802
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700803audio_devices_t AudioPolicyManager::getDeviceForInputSource(int inputSource)
804{
805 uint32_t device = AUDIO_DEVICE_NONE;
806
807 switch (inputSource) {
808 case AUDIO_SOURCE_VOICE_UPLINK:
809 if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
810 device = AUDIO_DEVICE_IN_VOICE_CALL;
811 break;
812 }
813 // FALL THROUGH
814
815 case AUDIO_SOURCE_DEFAULT:
816 case AUDIO_SOURCE_MIC:
817 case AUDIO_SOURCE_VOICE_RECOGNITION:
818 case AUDIO_SOURCE_HOTWORD:
819 case AUDIO_SOURCE_VOICE_COMMUNICATION:
820 if (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO &&
821 mAvailableInputDevices & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
822 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
823 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_WIRED_HEADSET) {
824 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
825 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET) {
826 device = AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET;
827 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
828 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
829 }
830 break;
831 case AUDIO_SOURCE_CAMCORDER:
832 if (mAvailableInputDevices & AUDIO_DEVICE_IN_BACK_MIC) {
833 device = AUDIO_DEVICE_IN_BACK_MIC;
834 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
835 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
836 }
837 break;
838 case AUDIO_SOURCE_VOICE_DOWNLINK:
839 case AUDIO_SOURCE_VOICE_CALL:
840 if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
841 device = AUDIO_DEVICE_IN_VOICE_CALL;
842 }
843 break;
844 case AUDIO_SOURCE_REMOTE_SUBMIX:
845 if (mAvailableInputDevices & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
846 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
847 }
848 break;
849#ifdef AUDIO_EXTN_FM_ENABLED
850 case AUDIO_SOURCE_FM_RX:
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700851 device = AUDIO_DEVICE_IN_FM_RX;
852 break;
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700853 case AUDIO_SOURCE_FM_RX_A2DP:
854 device = AUDIO_DEVICE_IN_FM_RX_A2DP;
855 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700856#endif
857 default:
858 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
859 break;
860 }
861 ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
862 return device;
863}
864
865AudioPolicyManager::device_category AudioPolicyManager::getDeviceCategory(audio_devices_t device)
866{
867 switch(getDeviceForVolume(device)) {
868 case AUDIO_DEVICE_OUT_EARPIECE:
869 return DEVICE_CATEGORY_EARPIECE;
870 case AUDIO_DEVICE_OUT_WIRED_HEADSET:
871 case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
872 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
873 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
874 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
875 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
876#ifdef AUDIO_EXTN_FM_ENABLED
877 case AUDIO_DEVICE_OUT_FM:
878#endif
879 return DEVICE_CATEGORY_HEADSET;
880 case AUDIO_DEVICE_OUT_SPEAKER:
881 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
882 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
883 case AUDIO_DEVICE_OUT_AUX_DIGITAL:
884 case AUDIO_DEVICE_OUT_USB_ACCESSORY:
885 case AUDIO_DEVICE_OUT_USB_DEVICE:
886 case AUDIO_DEVICE_OUT_REMOTE_SUBMIX:
887#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
888 case AUDIO_DEVICE_OUT_PROXY:
889#endif
890 default:
891 return DEVICE_CATEGORY_SPEAKER;
892 }
893}
894
895status_t AudioPolicyManager::checkAndSetVolume(int stream,
896 int index,
897 audio_io_handle_t output,
898 audio_devices_t device,
899 int delayMs,
900 bool force)
901{
902 ALOGV("checkAndSetVolume: index %d output %d device %x", index, output, device);
903 // do not change actual stream volume if the stream is muted
904 if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
905 ALOGVV("checkAndSetVolume() stream %d muted count %d",
906 stream, mOutputs.valueFor(output)->mMuteCount[stream]);
907 return NO_ERROR;
908 }
909
910 // do not change in call volume if bluetooth is connected and vice versa
911 if ((stream == AudioSystem::VOICE_CALL && mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
912 (stream == AudioSystem::BLUETOOTH_SCO && mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO)) {
913 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
914 stream, mForceUse[AudioSystem::FOR_COMMUNICATION]);
915 return INVALID_OPERATION;
916 }
917
918 float volume = computeVolume(stream, index, output, device);
919 // We actually change the volume if:
920 // - the float value returned by computeVolume() changed
921 // - the force flag is set
922 if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
923 force) {
924 mOutputs.valueFor(output)->mCurVolume[stream] = volume;
925 ALOGV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
926 // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
927 // enabled
928 if (stream == AudioSystem::BLUETOOTH_SCO) {
929 mpClientInterface->setStreamVolume(AudioSystem::VOICE_CALL, volume, output, delayMs);
930#ifdef AUDIO_EXTN_FM_ENABLED
931 } else if (stream == AudioSystem::MUSIC &&
932 output == mPrimaryOutput) {
933 float fmVolume = -1.0;
934 fmVolume = computeVolume(stream, index, output, device);
935 if (fmVolume >= 0) {
936 AudioParameter param = AudioParameter();
937 param.addFloat(String8("fm_volume"), fmVolume);
938 ALOGV("checkAndSetVolume setParameters fm_volume, volume=:%f delay=:%d",fmVolume,delayMs*2);
939 //Double delayMs to avoid sound burst while device switch.
940 mpClientInterface->setParameters(mPrimaryOutput, param.toString(), delayMs*2);
941 }
942#endif
943 }
944 mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs);
945 }
946
947 if (stream == AudioSystem::VOICE_CALL ||
948 stream == AudioSystem::BLUETOOTH_SCO) {
949 float voiceVolume;
Vidyakumar Athota35543452014-04-16 10:00:39 -0700950 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
951 if (stream == AudioSystem::VOICE_CALL) {
952 voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
953 } else {
954 voiceVolume = 1.0;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700955 }
956
957 if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) {
958 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
959 mLastVoiceVolume = voiceVolume;
960 }
961 }
962
963 return NO_ERROR;
964}
965
966
967float AudioPolicyManager::computeVolume(int stream,
968 int index,
969 audio_io_handle_t output,
970 audio_devices_t device)
971{
972 float volume = 1.0;
973 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
974
975 if (device == AUDIO_DEVICE_NONE) {
976 device = outputDesc->device();
977 }
978
979 // if volume is not 0 (not muted), force media volume to max on digital output
980 if (stream == AudioSystem::MUSIC &&
981 index != mStreams[stream].mIndexMin &&
982 (device == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
983 device == AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET ||
984 device == AUDIO_DEVICE_OUT_USB_ACCESSORY ||
985#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
986 device == AUDIO_DEVICE_OUT_PROXY ||
987#endif
988 device == AUDIO_DEVICE_OUT_USB_DEVICE )) {
989 return 1.0;
990 }
991#ifdef AUDIO_EXTN_INCALL_MUSIC_ENABLED
992 if (stream == AudioSystem::INCALL_MUSIC) {
993 return 1.0;
994 }
995#endif
996 return AudioPolicyManagerBase::computeVolume(stream, index, output, device);
997}
Naresh Tanniru36c08932014-01-27 18:40:53 +0530998
999
1000audio_io_handle_t AudioPolicyManager::getOutput(AudioSystem::stream_type stream,
1001 uint32_t samplingRate,
1002 uint32_t format,
1003 uint32_t channelMask,
1004 AudioSystem::output_flags flags,
1005 const audio_offload_info_t *offloadInfo)
1006{
1007 audio_io_handle_t output = 0;
1008 uint32_t latency = 0;
1009 routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);
1010 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1011 IOProfile *profile = NULL;
1012
1013#ifdef VOICE_CONCURRENCY
Naresh Tanniru811f4982014-03-18 17:25:32 +05301014 char propValue[PROPERTY_VALUE_MAX];
1015 bool prop_play_enabled=false, prop_voip_enabled = false;
1016
1017 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1018 prop_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1019 }
1020
1021 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1022 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1023 }
1024
1025 if (prop_play_enabled) {
1026 //check if voice call is active / running in background
1027 if((AudioSystem::MODE_IN_CALL == mPhoneState) ||
1028 ((AudioSystem::MODE_IN_CALL == mPrevPhoneState)
1029 && (AudioSystem::MODE_IN_COMMUNICATION == mPhoneState)))
1030 {
1031 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1032 if(prop_voip_enabled) {
1033 ALOGD(" IN call mode returing no output .. for VoIP usecase flags: %x ", flags );
1034 // flags = (AudioSystem::output_flags)AUDIO_OUTPUT_FLAG_FAST;
1035 return 0;
1036 }
1037 }
1038 else {
1039 ALOGD(" IN call mode adding ULL flags .. flags: %x ", flags );
1040 flags = (AudioSystem::output_flags)AUDIO_OUTPUT_FLAG_FAST;
1041 }
1042 }
1043 } else if (prop_voip_enabled) {
1044 //check if voice call is active / running in background
1045 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1046 //return only ULL ouput
1047 if((AudioSystem::MODE_IN_CALL == mPhoneState) ||
1048 ((AudioSystem::MODE_IN_CALL == mPrevPhoneState)
1049 && (AudioSystem::MODE_IN_COMMUNICATION == mPhoneState)))
1050 {
1051 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1052 ALOGD(" IN call mode returing no output .. for VoIP usecase flags: %x ", flags );
1053 // flags = (AudioSystem::output_flags)AUDIO_OUTPUT_FLAG_FAST;
1054 return 0;
1055 }
1056 }
Naresh Tanniru36c08932014-01-27 18:40:53 +05301057 }
1058#endif
1059
1060#ifdef WFD_CONCURRENCY
1061 if ((mAvailableOutputDevices & AUDIO_DEVICE_OUT_PROXY)
1062 && (stream != AudioSystem::MUSIC)) {
1063 ALOGV(" WFD mode adding ULL flags for non music stream.. flags: %x ", flags );
1064 //For voip paths
1065 if(flags & AudioSystem::OUTPUT_FLAG_DIRECT)
1066 flags = AudioSystem::OUTPUT_FLAG_DIRECT;
1067 else //route every thing else to ULL path
1068 flags = (AudioSystem::output_flags)AUDIO_OUTPUT_FLAG_FAST;
1069 }
1070#endif
1071
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05301072 ALOGD(" getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x ",
Naresh Tanniru36c08932014-01-27 18:40:53 +05301073 device, stream, samplingRate, format, channelMask, flags);
1074
1075
1076
1077#ifdef AUDIO_POLICY_TEST
1078 if (mCurOutput != 0) {
1079 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1080 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1081
1082 if (mTestOutputs[mCurOutput] == 0) {
1083 ALOGV("getOutput() opening test output");
1084 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL);
1085 outputDesc->mDevice = mTestDevice;
1086 outputDesc->mSamplingRate = mTestSamplingRate;
1087 outputDesc->mFormat = mTestFormat;
1088 outputDesc->mChannelMask = mTestChannels;
1089 outputDesc->mLatency = mTestLatencyMs;
1090 outputDesc->mFlags = (audio_output_flags_t)(mDirectOutput ? AudioSystem::OUTPUT_FLAG_DIRECT : 0);
1091 outputDesc->mRefCount[stream] = 0;
1092 mTestOutputs[mCurOutput] = mpClientInterface->openOutput(0, &outputDesc->mDevice,
1093 &outputDesc->mSamplingRate,
1094 &outputDesc->mFormat,
1095 &outputDesc->mChannelMask,
1096 &outputDesc->mLatency,
1097 outputDesc->mFlags,
1098 offloadInfo);
1099 if (mTestOutputs[mCurOutput]) {
1100 AudioParameter outputCmd = AudioParameter();
1101 outputCmd.addInt(String8("set_id"),mCurOutput);
1102 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1103 addOutput(mTestOutputs[mCurOutput], outputDesc);
1104 }
1105 }
1106 return mTestOutputs[mCurOutput];
1107 }
1108#endif //AUDIO_POLICY_TEST
1109
1110 // open a direct output if required by specified parameters
1111 //force direct flag if offload flag is set: offloading implies a direct output stream
1112 // and all common behaviors are driven by checking only the direct flag
1113 // this should normally be set appropriately in the policy configuration file
1114 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1115 flags = (AudioSystem::output_flags)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1116 }
1117
1118 if ((format == AudioSystem::PCM_16_BIT) &&(AudioSystem::popCount(channelMask) > 2)) {
1119 ALOGV("owerwrite flag(%x) for PCM16 multi-channel(CM:%x) playback", flags ,channelMask);
1120 flags = (AudioSystem::output_flags)AUDIO_OUTPUT_FLAG_DIRECT;
1121 }
1122
1123 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1124 // creating an offloaded track and tearing it down immediately after start when audioflinger
1125 // detects there is an active non offloadable effect.
1126 // FIXME: We should check the audio session here but we do not have it in this context.
1127 // This may prevent offloading in rare situations where effects are left active by apps
1128 // in the background.
1129 if ((((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
1130 !isNonOffloadableEffectEnabled()) &&
1131 flags & AUDIO_OUTPUT_FLAG_DIRECT) {
1132 profile = getProfileForDirectOutput(device,
1133 samplingRate,
1134 format,
1135 channelMask,
1136 (audio_output_flags_t)flags);
1137 }
1138
1139 if (profile != NULL) {
1140 AudioOutputDescriptor *outputDesc = NULL;
1141
Subhash Chandra Bose Naripeddy5eb02f72014-04-11 17:54:09 -07001142#ifdef MULTIPLE_OFFLOAD_ENABLED
1143 bool multiOffloadEnabled = false;
1144 char value[PROPERTY_VALUE_MAX] = {0};
1145 property_get("audio.offload.multiple.enabled", value, NULL);
1146 if (atoi(value) || !strncmp("true", value, 4))
1147 multiOffloadEnabled = true;
1148 // if multiple concurrent offload decode is supported
1149 // do no check for reuse and also don't close previous output if its offload
1150 // previous output will be closed during track destruction
1151 if (multiOffloadEnabled)
1152 goto get_output__new_output_desc;
1153#endif
Naresh Tanniru36c08932014-01-27 18:40:53 +05301154 for (size_t i = 0; i < mOutputs.size(); i++) {
1155 AudioOutputDescriptor *desc = mOutputs.valueAt(i);
1156 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1157 outputDesc = desc;
1158 // reuse direct output if currently open and configured with same parameters
1159 if ((samplingRate == outputDesc->mSamplingRate) &&
1160 (format == outputDesc->mFormat) &&
1161 (channelMask == outputDesc->mChannelMask)) {
1162 outputDesc->mDirectOpenCount++;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05301163 ALOGD("getOutput() reusing direct output %d", mOutputs.keyAt(i));
Naresh Tanniru36c08932014-01-27 18:40:53 +05301164 return mOutputs.keyAt(i);
1165 }
1166 }
1167 }
1168 // close direct output if currently open and configured with different parameters
1169 if (outputDesc != NULL) {
1170 closeOutput(outputDesc->mId);
1171 }
Subhash Chandra Bose Naripeddy5eb02f72014-04-11 17:54:09 -07001172get_output__new_output_desc:
Naresh Tanniru36c08932014-01-27 18:40:53 +05301173 outputDesc = new AudioOutputDescriptor(profile);
1174 outputDesc->mDevice = device;
1175 outputDesc->mSamplingRate = samplingRate;
1176 outputDesc->mFormat = (audio_format_t)format;
1177 outputDesc->mChannelMask = (audio_channel_mask_t)channelMask;
1178 outputDesc->mLatency = 0;
1179 outputDesc->mFlags =(audio_output_flags_t) (outputDesc->mFlags | flags);
1180 outputDesc->mRefCount[stream] = 0;
1181 outputDesc->mStopTime[stream] = 0;
1182 outputDesc->mDirectOpenCount = 1;
1183 output = mpClientInterface->openOutput(profile->mModule->mHandle,
1184 &outputDesc->mDevice,
1185 &outputDesc->mSamplingRate,
1186 &outputDesc->mFormat,
1187 &outputDesc->mChannelMask,
1188 &outputDesc->mLatency,
1189 outputDesc->mFlags,
1190 offloadInfo);
1191
1192 // only accept an output with the requested parameters
1193 if (output == 0 ||
1194 (samplingRate != 0 && samplingRate != outputDesc->mSamplingRate) ||
1195 (format != 0 && format != outputDesc->mFormat) ||
1196 (channelMask != 0 && channelMask != outputDesc->mChannelMask)) {
1197 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1198 "format %d %d, channelMask %04x %04x", output, samplingRate,
1199 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1200 outputDesc->mChannelMask);
1201 if (output != 0) {
1202 mpClientInterface->closeOutput(output);
1203 }
1204 delete outputDesc;
1205 return 0;
1206 }
1207 audio_io_handle_t srcOutput = getOutputForEffect();
1208 addOutput(output, outputDesc);
1209 audio_io_handle_t dstOutput = getOutputForEffect();
1210 if (dstOutput == output) {
1211 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1212 }
1213 mPreviousOutputs = mOutputs;
1214 ALOGV("getOutput() returns new direct output %d", output);
1215 return output;
1216 }
1217
1218 // ignoring channel mask due to downmix capability in mixer
1219
1220 // open a non direct output
1221
1222 // for non direct outputs, only PCM is supported
1223 if (audio_is_linear_pcm((audio_format_t)format)) {
1224 // get which output is suitable for the specified stream. The actual
1225 // routing change will happen when startOutput() will be called
1226 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1227
1228 output = selectOutput(outputs, flags);
1229 }
1230 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1231 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1232
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05301233 ALOGD("getOutput() returns output %d", output);
Naresh Tanniru36c08932014-01-27 18:40:53 +05301234
1235 return output;
1236}
1237
1238
1239// This function checks for the parameters which can be offloaded.
1240// This can be enhanced depending on the capability of the DSP and policy
1241// of the system.
1242bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
1243{
1244 ALOGV(" isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
1245 " BitRate=%u, duration=%lld us, has_video=%d",
1246 offloadInfo.sample_rate, offloadInfo.channel_mask,
1247 offloadInfo.format,
1248 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
1249 offloadInfo.has_video);
1250
1251#ifdef VOICE_CONCURRENCY
Naresh Tanniru811f4982014-03-18 17:25:32 +05301252 char concpropValue[PROPERTY_VALUE_MAX];
1253 if(property_get("voice.playback.conc.disabled", concpropValue, NULL)) {
1254 bool propenabled = atoi(concpropValue) || !strncmp("true", concpropValue, 4);
1255 if (propenabled) {
1256 if(isInCall())
1257 {
1258 ALOGD("\n blocking compress offload on call mode\n");
1259 return false;
1260 }
1261 }
Naresh Tanniru36c08932014-01-27 18:40:53 +05301262 }
Naresh Tanniru811f4982014-03-18 17:25:32 +05301263
Naresh Tanniru36c08932014-01-27 18:40:53 +05301264#endif
Naresh Tanniru36c08932014-01-27 18:40:53 +05301265 // Check if stream type is music, then only allow offload as of now.
1266 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
1267 {
1268 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
1269 return false;
1270 }
1271
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001272 char propValue[PROPERTY_VALUE_MAX];
1273 bool pcmOffload = false;
1274 if (audio_is_offload_pcm(offloadInfo.format)) {
1275 if(property_get("audio.offload.pcm.enable", propValue, NULL)) {
Naresh Tanniru36c08932014-01-27 18:40:53 +05301276 bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001277 if (prop_enabled) {
1278 ALOGW("PCM offload property is enabled");
1279 pcmOffload = true;
Naresh Tanniru36c08932014-01-27 18:40:53 +05301280 }
1281 }
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001282 if (!pcmOffload) {
1283 ALOGV("PCM offload disabled by property audio.offload.pcm.enable");
1284 return false;
1285 }
1286 }
1287
1288 if (!pcmOffload) {
1289 // Check if offload has been disabled
1290 if (property_get("audio.offload.disable", propValue, "0")) {
1291 if (atoi(propValue) != 0) {
1292 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
1293 return false;
Naresh Tanniru36c08932014-01-27 18:40:53 +05301294 }
1295 }
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001296
1297 //check if it's multi-channel AAC format
1298 if (AudioSystem::popCount(offloadInfo.channel_mask) > 2
1299 && offloadInfo.format == AUDIO_FORMAT_AAC) {
1300 ALOGV("offload disabled for multi-channel AAC format");
1301 return false;
1302 }
1303
1304 if (offloadInfo.has_video)
1305 {
1306 if(property_get("av.offload.enable", propValue, NULL)) {
1307 bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1308 if (!prop_enabled) {
1309 ALOGW("offload disabled by av.offload.enable = %s ", propValue );
1310 return false;
1311 }
1312 } else {
1313 return false;
1314 }
1315
1316 if(offloadInfo.is_streaming) {
1317 if (property_get("av.streaming.offload.enable", propValue, NULL)) {
1318 bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1319 if (!prop_enabled) {
1320 ALOGW("offload disabled by av.streaming.offload.enable = %s ", propValue );
1321 return false;
1322 }
1323 } else {
1324 //Do not offload AV streamnig if the property is not defined
1325 return false;
1326 }
1327 }
1328 ALOGV("isOffloadSupported: has_video == true, property\
1329 set to enable offload");
1330 }
Naresh Tanniru36c08932014-01-27 18:40:53 +05301331 }
1332
1333 //If duration is less than minimum value defined in property, return false
1334 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
1335 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
1336 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
1337 return false;
1338 }
1339 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
1340 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
1341 //duration checks only valid for MP3/AAC formats,
1342 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001343 if (offloadInfo.format == AUDIO_FORMAT_MP3 || offloadInfo.format == AUDIO_FORMAT_AAC || pcmOffload)
Naresh Tanniru36c08932014-01-27 18:40:53 +05301344 return false;
1345 }
1346
1347 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1348 // creating an offloaded track and tearing it down immediately after start when audioflinger
1349 // detects there is an active non offloadable effect.
1350 // FIXME: We should check the audio session here but we do not have it in this context.
1351 // This may prevent offloading in rare situations where effects are left active by apps
1352 // in the background.
1353 if (isNonOffloadableEffectEnabled()) {
1354 return false;
1355 }
1356
1357 // See if there is a profile to support this.
1358 // AUDIO_DEVICE_NONE
1359 IOProfile *profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
1360 offloadInfo.sample_rate,
1361 offloadInfo.format,
1362 offloadInfo.channel_mask,
1363 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1364 ALOGV("isOffloadSupported() profile %sfound", profile != NULL ? "" : "NOT ");
1365 return (profile != NULL);
1366}
1367
1368void AudioPolicyManager::setPhoneState(int state)
1369
1370{
Naresh Tanniru811f4982014-03-18 17:25:32 +05301371 ALOGD("setPhoneState() state %d", state);
Naresh Tanniru36c08932014-01-27 18:40:53 +05301372 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
1373 if (state < 0 || state >= AudioSystem::NUM_MODES) {
1374 ALOGW("setPhoneState() invalid state %d", state);
1375 return;
1376 }
1377
1378 if (state == mPhoneState ) {
1379 ALOGW("setPhoneState() setting same state %d", state);
1380 return;
1381 }
1382
1383 // if leaving call state, handle special case of active streams
1384 // pertaining to sonification strategy see handleIncallSonification()
1385 if (isInCall()) {
1386 ALOGV("setPhoneState() in call state management: new state is %d", state);
1387 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
1388 handleIncallSonification(stream, false, true);
1389 }
1390 }
1391
1392 // store previous phone state for management of sonification strategy below
1393 int oldState = mPhoneState;
1394 mPhoneState = state;
1395 bool force = false;
1396
1397 // are we entering or starting a call
1398 if (!isStateInCall(oldState) && isStateInCall(state)) {
1399 ALOGV(" Entering call in setPhoneState()");
1400 // force routing command to audio hardware when starting a call
1401 // even if no device change is needed
1402 force = true;
1403 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
1404 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
1405 sVolumeProfiles[AUDIO_STREAM_VOICE_CALL][j];
1406 }
1407 } else if (isStateInCall(oldState) && !isStateInCall(state)) {
1408 ALOGV(" Exiting call in setPhoneState()");
1409 // force routing command to audio hardware when exiting a call
1410 // even if no device change is needed
1411 force = true;
1412 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
1413 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
1414 sVolumeProfiles[AUDIO_STREAM_DTMF][j];
1415 }
1416 } else if (isStateInCall(state) && (state != oldState)) {
1417 ALOGV(" Switching between telephony and VoIP in setPhoneState()");
1418 // force routing command to audio hardware when switching between telephony and VoIP
1419 // even if no device change is needed
1420 force = true;
1421 }
1422
1423 // check for device and output changes triggered by new phone state
1424 newDevice = getNewDevice(mPrimaryOutput, false /*fromCache*/);
1425 checkA2dpSuspend();
1426 checkOutputForAllStrategies();
1427 updateDevicesAndOutputs();
1428
1429 AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
1430
1431 // force routing command to audio hardware when ending call
1432 // even if no device change is needed
1433 if (isStateInCall(oldState) && newDevice == AUDIO_DEVICE_NONE) {
1434 newDevice = hwOutputDesc->device();
1435 }
Naresh Tanniru811f4982014-03-18 17:25:32 +05301436#ifdef VOICE_CONCURRENCY
1437 char propValue[PROPERTY_VALUE_MAX];
1438 bool prop_playback_enabled = false, prop_rec_enabled=false, prop_voip_enabled = false;
1439
1440 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1441 prop_playback_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1442 }
1443
1444 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
1445 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1446 }
1447
1448 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1449 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1450 }
1451
1452 if((AudioSystem::MODE_IN_CALL != oldState) && (AudioSystem::MODE_IN_CALL == state)) {
1453 ALOGD("Entering to call mode oldState :: %d state::%d ",oldState, state);
1454
1455 if(prop_playback_enabled) {
1456 //Call invalidate to reset all opened non ULL audio tracks
1457 // Move tracks associated to this strategy from previous output to new output
1458 for (int i = AudioSystem::SYSTEM; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
1459 ALOGV(" Invalidate on call mode for stream :: %d ", i);
1460 //FIXME see fixme on name change
1461 mpClientInterface->setStreamOutput((AudioSystem::stream_type)i,
1462 0 /* ignored */);
1463 }
1464 }
1465
1466 if(prop_rec_enabled) {
1467 //Close all active inputs
1468 audio_io_handle_t activeInput = getActiveInput();
1469 if (activeInput != 0) {
1470 AudioInputDescriptor *activeDesc = mInputs.valueFor(activeInput);
1471 switch(activeDesc->mInputSource) {
1472 case AUDIO_SOURCE_VOICE_UPLINK:
1473 case AUDIO_SOURCE_VOICE_DOWNLINK:
1474 case AUDIO_SOURCE_VOICE_CALL:
1475 ALOGD("FOUND active input during call active: %d",activeDesc->mInputSource);
1476 break;
1477
1478 case AUDIO_SOURCE_VOICE_COMMUNICATION:
1479 if(prop_voip_enabled) {
1480 ALOGD("CLOSING VoIP input source on call setup :%d ",activeDesc->mInputSource);
1481 stopInput(activeInput);
1482 releaseInput(activeInput);
1483 }
1484 break;
1485
1486 default:
1487 ALOGD("CLOSING input on call setup for inputSource: %d",activeDesc->mInputSource);
1488 stopInput(activeInput);
1489 releaseInput(activeInput);
1490 break;
1491 }
1492 }
1493 } else if(prop_voip_enabled) {
1494 audio_io_handle_t activeInput = getActiveInput();
1495 if (activeInput != 0) {
1496 AudioInputDescriptor *activeDesc = mInputs.valueFor(activeInput);
1497 if(AUDIO_SOURCE_VOICE_COMMUNICATION == activeDesc->mInputSource) {
1498 ALOGD("CLOSING VoIP on call setup : %d",activeDesc->mInputSource);
1499 stopInput(activeInput);
1500 releaseInput(activeInput);
1501 }
1502 }
1503 }
1504
1505 //suspend PCM (deep-buffer) output & close compress & direct tracks
1506 for (size_t i = 0; i < mOutputs.size(); i++) {
1507 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
1508 if (((!outputDesc->isDuplicated() &&outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY))
1509 && prop_playback_enabled) {
1510 ALOGD(" calling suspendOutput on call mdoe for primary output");
1511 mpClientInterface->suspendOutput(mOutputs.keyAt(i));
1512 } //Close compress all sessions
1513 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
1514 && prop_playback_enabled) {
1515 ALOGD(" calling closeOutput on call mdoe for COMPRESS output");
1516 closeOutput(mOutputs.keyAt(i));
1517 }
1518 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_VOIP_RX)
1519 && prop_voip_enabled) {
1520 ALOGD(" calling closeOutput on call mdoe for DIRECT output");
1521 closeOutput(mOutputs.keyAt(i));
1522 }
1523 }
1524 }
1525
1526 if((AudioSystem::MODE_IN_CALL == oldState) && (AudioSystem::MODE_IN_CALL != state)
1527 && prop_playback_enabled) {
1528 ALOGD("EXITING from call mode oldState :: %d state::%d \n",oldState, state);
1529 //restore PCM (deep-buffer) output after call termination
1530 for (size_t i = 0; i < mOutputs.size(); i++) {
1531 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
1532 if (!outputDesc->isDuplicated() &&outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1533 ALOGD("calling restoreOutput after call mode for primary output");
1534 mpClientInterface->restoreOutput(mOutputs.keyAt(i));
1535 }
1536 }
1537 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
1538 for (int i = AudioSystem::SYSTEM; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
1539 ALOGD("Invalidate on call mode for stream :: %d ", i);
1540 //FIXME see fixme on name change
1541 mpClientInterface->setStreamOutput((AudioSystem::stream_type)i,
1542 0 /* ignored */);
1543 }
1544 }
1545 mPrevPhoneState = oldState;
1546#endif
Naresh Tanniru36c08932014-01-27 18:40:53 +05301547
1548 int delayMs = 0;
1549 if (isStateInCall(state)) {
1550 nsecs_t sysTime = systemTime();
1551 for (size_t i = 0; i < mOutputs.size(); i++) {
1552 AudioOutputDescriptor *desc = mOutputs.valueAt(i);
1553 // mute media and sonification strategies and delay device switch by the largest
1554 // latency of any output where either strategy is active.
1555 // This avoid sending the ring tone or music tail into the earpiece or headset.
1556 if ((desc->isStrategyActive(STRATEGY_MEDIA,
1557 SONIFICATION_HEADSET_MUSIC_DELAY,
1558 sysTime) ||
1559 desc->isStrategyActive(STRATEGY_SONIFICATION,
1560 SONIFICATION_HEADSET_MUSIC_DELAY,
1561 sysTime)) &&
1562 (delayMs < (int)desc->mLatency*2)) {
1563 delayMs = desc->mLatency*2;
1564 }
1565 setStrategyMute(STRATEGY_MEDIA, true, mOutputs.keyAt(i));
1566 setStrategyMute(STRATEGY_MEDIA, false, mOutputs.keyAt(i), MUTE_TIME_MS,
1567 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
1568 setStrategyMute(STRATEGY_SONIFICATION, true, mOutputs.keyAt(i));
1569 setStrategyMute(STRATEGY_SONIFICATION, false, mOutputs.keyAt(i), MUTE_TIME_MS,
1570 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
1571 }
1572 }
1573
1574 // change routing is necessary
1575 setOutputDevice(mPrimaryOutput, newDevice, force, delayMs);
1576
1577 // if entering in call state, handle special case of active streams
1578 // pertaining to sonification strategy see handleIncallSonification()
1579 if (isStateInCall(state)) {
1580 ALOGV("setPhoneState() in call state management: new state is %d", state);
1581 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
1582 handleIncallSonification(stream, true, true);
1583 }
1584 }
1585
1586 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
1587 if (state == AudioSystem::MODE_RINGTONE &&
1588 isStreamActive(AudioSystem::MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
1589 mLimitRingtoneVolume = true;
1590 } else {
1591 mLimitRingtoneVolume = false;
1592 }
Naresh Tanniru811f4982014-03-18 17:25:32 +05301593 ALOGD(" End of setPhoneState ... mPhoneState: %d ",mPhoneState);
Naresh Tanniru36c08932014-01-27 18:40:53 +05301594}
1595
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001596extern "C" AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001597{
1598 return new AudioPolicyManager(clientInterface);
1599}
1600
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001601extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001602{
1603 delete interface;
1604}
1605
1606}; // namespace android