blob: a8cbcda59c83d33b8d8ccd94007d64c1c0956d9f [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.
Satya Krishna Pindiproli7e522282014-05-08 12:22:16 +0530236 audio_devices_t cachedDevice = getNewDevice(mOutputs.keyAt(i), true /*fromCache*/);
237 AudioOutputDescriptor *desc = mOutputs.valueFor(mOutputs.keyAt(i));
238 if (cachedDevice == AUDIO_DEVICE_OUT_SPEAKER &&
239 device == AUDIO_DEVICE_OUT_PROXY &&
240 (desc->mFlags & AUDIO_OUTPUT_FLAG_FAST)) {
241 ALOGI("Avoid routing touch tone to spkr as proxy is being disconnected");
242 break;
243 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700244 setOutputDevice(mOutputs.keyAt(i),
Satya Krishna Pindiproli7e522282014-05-08 12:22:16 +0530245 cachedDevice,
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700246 !mOutputs.valueAt(i)->isDuplicated(),
247 0);
248 }
249
250 if (device == AUDIO_DEVICE_OUT_WIRED_HEADSET) {
251 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
252 } else if (device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO ||
253 device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET ||
254 device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT) {
255 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
256 } else if(device == AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET){
257 device = AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET;
258 } else {
259 return NO_ERROR;
260 }
261 }
262 // handle input devices
263 if (audio_is_input_device(device)) {
264
265 switch (state)
266 {
267 // handle input device connection
268 case AudioSystem::DEVICE_STATE_AVAILABLE: {
269 if (mAvailableInputDevices & device) {
270 ALOGW("setDeviceConnectionState() device already connected: %d", device);
271 return INVALID_OPERATION;
272 }
273 mAvailableInputDevices = mAvailableInputDevices | (device & ~AUDIO_DEVICE_BIT_IN);
274 }
275 break;
276
277 // handle input device disconnection
278 case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
279 if (!(mAvailableInputDevices & device)) {
280 ALOGW("setDeviceConnectionState() device not connected: %d", device);
281 return INVALID_OPERATION;
282 }
283 mAvailableInputDevices = (audio_devices_t) (mAvailableInputDevices & ~device);
284 } break;
285
286 default:
287 ALOGE("setDeviceConnectionState() invalid state: %x", state);
288 return BAD_VALUE;
289 }
290
291 audio_io_handle_t activeInput = getActiveInput();
292 if (activeInput != 0) {
293 AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
294 audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
295 if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) {
296 ALOGV("setDeviceConnectionState() changing device from %x to %x for input %d",
297 inputDesc->mDevice, newDevice, activeInput);
298 inputDesc->mDevice = newDevice;
299 AudioParameter param = AudioParameter();
300 param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
301 mpClientInterface->setParameters(activeInput, param.toString());
302 }
303 }
304
305 return NO_ERROR;
306 }
307
308 ALOGW("setDeviceConnectionState() invalid device: %x", device);
309 return BAD_VALUE;
310}
311
312void AudioPolicyManager::setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config)
313{
Sidipotu Ashokf43018c2014-05-02 16:21:50 +0530314 ALOGD("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700315
316 bool forceVolumeReeval = false;
317 switch(usage) {
318 case AudioSystem::FOR_COMMUNICATION:
319 if (config != AudioSystem::FORCE_SPEAKER && config != AudioSystem::FORCE_BT_SCO &&
320 config != AudioSystem::FORCE_NONE) {
321 ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
322 return;
323 }
324 forceVolumeReeval = true;
325 mForceUse[usage] = config;
326 break;
327 case AudioSystem::FOR_MEDIA:
328 if (config != AudioSystem::FORCE_HEADPHONES && config != AudioSystem::FORCE_BT_A2DP &&
329#ifdef AUDIO_EXTN_FM_ENABLED
330 config != AudioSystem::FORCE_SPEAKER &&
331#endif
332 config != AudioSystem::FORCE_WIRED_ACCESSORY &&
333 config != AudioSystem::FORCE_ANALOG_DOCK &&
334 config != AudioSystem::FORCE_DIGITAL_DOCK && config != AudioSystem::FORCE_NONE &&
335 config != AudioSystem::FORCE_NO_BT_A2DP) {
336 ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
337 return;
338 }
339 mForceUse[usage] = config;
340 break;
341 case AudioSystem::FOR_RECORD:
342 if (config != AudioSystem::FORCE_BT_SCO && config != AudioSystem::FORCE_WIRED_ACCESSORY &&
343 config != AudioSystem::FORCE_NONE) {
344 ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
345 return;
346 }
347 mForceUse[usage] = config;
348 break;
349 case AudioSystem::FOR_DOCK:
350 if (config != AudioSystem::FORCE_NONE && config != AudioSystem::FORCE_BT_CAR_DOCK &&
351 config != AudioSystem::FORCE_BT_DESK_DOCK &&
352 config != AudioSystem::FORCE_WIRED_ACCESSORY &&
353 config != AudioSystem::FORCE_ANALOG_DOCK &&
354 config != AudioSystem::FORCE_DIGITAL_DOCK) {
355 ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
356 }
357 forceVolumeReeval = true;
358 mForceUse[usage] = config;
359 break;
360 case AudioSystem::FOR_SYSTEM:
361 if (config != AudioSystem::FORCE_NONE &&
362 config != AudioSystem::FORCE_SYSTEM_ENFORCED) {
363 ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
364 }
365 forceVolumeReeval = true;
366 mForceUse[usage] = config;
367 break;
368 default:
369 ALOGW("setForceUse() invalid usage %d", usage);
370 break;
371 }
372
373 // check for device and output changes triggered by new force usage
374 checkA2dpSuspend();
375 checkOutputForAllStrategies();
376 updateDevicesAndOutputs();
377 for (int i = mOutputs.size() -1; i >= 0; i--) {
378 audio_io_handle_t output = mOutputs.keyAt(i);
379 audio_devices_t newDevice = getNewDevice(output, true /*fromCache*/);
380 setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE));
381 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
382 applyStreamVolumes(output, newDevice, 0, true);
383 }
384 }
385
386 audio_io_handle_t activeInput = getActiveInput();
387 if (activeInput != 0) {
388 AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
389 audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
390 if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) {
391 ALOGV("setForceUse() changing device from %x to %x for input %d",
392 inputDesc->mDevice, newDevice, activeInput);
393 inputDesc->mDevice = newDevice;
394 AudioParameter param = AudioParameter();
395 param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
396 mpClientInterface->setParameters(activeInput, param.toString());
397 }
398 }
399
400}
401
402audio_io_handle_t AudioPolicyManager::getInput(int inputSource,
403 uint32_t samplingRate,
404 uint32_t format,
405 uint32_t channelMask,
406 AudioSystem::audio_in_acoustics acoustics)
407{
408 audio_io_handle_t input = 0;
409 audio_devices_t device = getDeviceForInputSource(inputSource);
410
Sidipotu Ashokf43018c2014-05-02 16:21:50 +0530411 ALOGD("getInput() inputSource %d, samplingRate %d, format %d, channelMask %x, acoustics %x",
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700412 inputSource, samplingRate, format, channelMask, acoustics);
413
414 if (device == AUDIO_DEVICE_NONE) {
415 ALOGW("getInput() could not find device for inputSource %d", inputSource);
416 return 0;
417 }
418
Naresh Tanniru811f4982014-03-18 17:25:32 +0530419#ifdef VOICE_CONCURRENCY
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700420
Naresh Tanniru811f4982014-03-18 17:25:32 +0530421 char propValue[PROPERTY_VALUE_MAX];
422 bool prop_rec_enabled=false, prop_voip_enabled = false;
423
424 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
425 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
426 }
427
428 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
429 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
430 }
431
432 if (prop_rec_enabled) {
433 //check if voice call is active / running in background
434 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
435 //Need to block input request
436 if((AudioSystem::MODE_IN_CALL == mPhoneState) ||
437 ((AudioSystem::MODE_IN_CALL == mPrevPhoneState) &&
438 (AudioSystem::MODE_IN_COMMUNICATION == mPhoneState)))
439 {
440 switch(inputSource) {
441 case AUDIO_SOURCE_VOICE_UPLINK:
442 case AUDIO_SOURCE_VOICE_DOWNLINK:
443 case AUDIO_SOURCE_VOICE_CALL:
444 ALOGD("Creating input during incall mode for inputSource: %d ",inputSource);
445 break;
446
447 case AUDIO_SOURCE_VOICE_COMMUNICATION:
448 if(prop_voip_enabled) {
449 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
450 return 0;
451 }
452 break;
453
454 default:
455 ALOGD("BLOCKING input during incall mode for inputSource: %d ",inputSource);
456 return 0;
457 }
458 }
459 }//check for VoIP flag
460 else if(prop_voip_enabled) {
461 //check if voice call is active / running in background
462 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
463 //Need to block input request
464 if((AudioSystem::MODE_IN_CALL == mPhoneState) ||
465 ((AudioSystem::MODE_IN_CALL == mPrevPhoneState) &&
466 (AudioSystem::MODE_IN_COMMUNICATION == mPhoneState)))
467 {
468 if(inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION) {
469 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
470 return 0;
471 }
472 }
473 }
474
475#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700476 IOProfile *profile = getInputProfile(device,
477 samplingRate,
478 format,
479 channelMask);
480 if (profile == NULL) {
481 ALOGW("getInput() could not find profile for device %04x, samplingRate %d, format %d,"
482 "channelMask %04x",
483 device, samplingRate, format, channelMask);
484 return 0;
485 }
486
487 if (profile->mModule->mHandle == 0) {
488 ALOGE("getInput(): HW module %s not opened", profile->mModule->mName);
489 return 0;
490 }
491
492 AudioInputDescriptor *inputDesc = new AudioInputDescriptor(profile);
493
494 inputDesc->mInputSource = inputSource;
495 inputDesc->mDevice = device;
496 inputDesc->mSamplingRate = samplingRate;
497 inputDesc->mFormat = (audio_format_t)format;
498 inputDesc->mChannelMask = (audio_channel_mask_t)channelMask;
499 inputDesc->mRefCount = 0;
500 input = mpClientInterface->openInput(profile->mModule->mHandle,
501 &inputDesc->mDevice,
502 &inputDesc->mSamplingRate,
503 &inputDesc->mFormat,
504 &inputDesc->mChannelMask);
505
506 // only accept input with the exact requested set of parameters
507 if (input == 0 ||
508 (samplingRate != inputDesc->mSamplingRate) ||
509 (format != inputDesc->mFormat) ||
510 (channelMask != inputDesc->mChannelMask)) {
511 ALOGV("getInput() failed opening input: samplingRate %d, format %d, channelMask %d",
512 samplingRate, format, channelMask);
513 if (input != 0) {
514 mpClientInterface->closeInput(input);
515 }
516 delete inputDesc;
517 return 0;
518 }
519 mInputs.add(input, inputDesc);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +0530520 ALOGD("getInput() returns input %d", input);
521
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700522 return input;
523}
524
525AudioPolicyManager::routing_strategy AudioPolicyManager::getStrategy(AudioSystem::stream_type stream)
526{
Naresh Tanniru36c08932014-01-27 18:40:53 +0530527 // stream to strategy mapping
528 switch (stream) {
529 case AudioSystem::VOICE_CALL:
530 case AudioSystem::BLUETOOTH_SCO:
531 return STRATEGY_PHONE;
532 case AudioSystem::RING:
533 case AudioSystem::ALARM:
534 return STRATEGY_SONIFICATION;
535 case AudioSystem::NOTIFICATION:
536 return STRATEGY_SONIFICATION_RESPECTFUL;
537 case AudioSystem::DTMF:
538 return STRATEGY_DTMF;
539 default:
540 ALOGE("unknown stream type");
541 case AudioSystem::SYSTEM:
542 // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
543 // while key clicks are played produces a poor result
544 case AudioSystem::TTS:
545 case AudioSystem::MUSIC:
546#ifdef AUDIO_EXTN_INCALL_MUSIC_ENABLED
547 case AudioSystem::INCALL_MUSIC:
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700548#endif
Naresh Tanniru36c08932014-01-27 18:40:53 +0530549#ifdef QCOM_INCALL_MUSIC_ENABLED
550 case AudioSystem::INCALL_MUSIC:
551#endif
552 return STRATEGY_MEDIA;
553 case AudioSystem::ENFORCED_AUDIBLE:
554 return STRATEGY_ENFORCED_AUDIBLE;
555 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700556
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700557}
558
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700559audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
560 bool fromCache)
561{
562 uint32_t device = AUDIO_DEVICE_NONE;
563
564 if (fromCache) {
565 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
566 strategy, mDeviceForStrategy[strategy]);
567 return mDeviceForStrategy[strategy];
568 }
569
570 switch (strategy) {
571
572 case STRATEGY_SONIFICATION_RESPECTFUL:
573 if (isInCall()) {
574 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
575 } else if (isStreamActiveRemotely(AudioSystem::MUSIC,
576 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
577 // while media is playing on a remote device, use the the sonification behavior.
578 // Note that we test this usecase before testing if media is playing because
579 // the isStreamActive() method only informs about the activity of a stream, not
580 // if it's for local playback. Note also that we use the same delay between both tests
581 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
582 } else if (isStreamActive(AudioSystem::MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
583 // while media is playing (or has recently played), use the same device
584 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
585 } else {
586 // when media is not playing anymore, fall back on the sonification behavior
587 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
588 }
589
590 break;
591
592 case STRATEGY_DTMF:
593 if (!isInCall()) {
594 // when off call, DTMF strategy follows the same rules as MEDIA strategy
595 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
596 break;
597 }
598 // when in call, DTMF and PHONE strategies follow the same rules
599 // FALL THROUGH
600
601 case STRATEGY_PHONE:
602 // for phone strategy, we first consider the forced use and then the available devices by order
603 // of priority
604 switch (mForceUse[AudioSystem::FOR_COMMUNICATION]) {
605 case AudioSystem::FORCE_BT_SCO:
606 if (!isInCall() || strategy != STRATEGY_DTMF) {
607 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
608 if (device) break;
609 }
610 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
611 if (device) break;
612 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
613 if (device) break;
614 // if SCO device is requested but no SCO device is available, fall back to default case
615 // FALL THROUGH
616
617 default: // FORCE_NONE
618 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
619 if (mHasA2dp && !isInCall() &&
620 (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
621 (getA2dpOutput() != 0) && !mA2dpSuspended) {
622 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
623 if (device) break;
624 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
625 if (device) break;
626 }
627 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
628 if (device) break;
629 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
630 if (device) break;
631 if (mPhoneState != AudioSystem::MODE_IN_CALL) {
632 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
633 if (device) break;
634 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
635 if (device) break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700636 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
637 if (device) break;
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700638 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
639 if (device) break;
640 }
641
642 // Allow voice call on USB ANLG DOCK headset
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700643 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
644 if (device) break;
645
646 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_EARPIECE;
647 if (device) break;
648 device = mDefaultOutputDevice;
649 if (device == AUDIO_DEVICE_NONE) {
650 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE");
651 }
652 break;
653
654 case AudioSystem::FORCE_SPEAKER:
655 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
656 // A2DP speaker when forcing to speaker output
657 if (mHasA2dp && !isInCall() &&
658 (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
659 (getA2dpOutput() != 0) && !mA2dpSuspended) {
660 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
661 if (device) break;
662 }
663 if (mPhoneState != AudioSystem::MODE_IN_CALL) {
664 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
665 if (device) break;
666 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
667 if (device) break;
668 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
669 if (device) break;
670 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
671 if (device) break;
Helen Zengbbce4952013-12-16 20:26:46 -0800672 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
673 if (device) break;
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700674 }
675 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
676 if (device) break;
677 device = mDefaultOutputDevice;
678 if (device == AUDIO_DEVICE_NONE) {
679 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER");
680 }
681 break;
682 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700683 // FIXME: Why do need to replace with speaker? If voice call is active
684 // We should use device from STRATEGY_PHONE
685#ifdef AUDIO_EXTN_FM_ENABLED
686 if (mAvailableOutputDevices & AUDIO_DEVICE_OUT_FM) {
687 if (mForceUse[AudioSystem::FOR_MEDIA] == AudioSystem::FORCE_SPEAKER) {
688 device = AUDIO_DEVICE_OUT_SPEAKER;
689 }
690 }
691#endif
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700692 break;
693
694 case STRATEGY_SONIFICATION:
695
696 // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
697 // handleIncallSonification().
698 if (isInCall()) {
699 device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
700 break;
701 }
702 // FALL THROUGH
703
704 case STRATEGY_ENFORCED_AUDIBLE:
705 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
706 // except:
707 // - when in call where it doesn't default to STRATEGY_PHONE behavior
708 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
709
710 if ((strategy == STRATEGY_SONIFICATION) ||
711 (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_SYSTEM_ENFORCED)) {
712 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
713 if (device == AUDIO_DEVICE_NONE) {
714 ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION");
715 }
716 }
717 // The second device used for sonification is the same as the device used by media strategy
718 // FALL THROUGH
719
720 case STRATEGY_MEDIA: {
721 uint32_t device2 = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700722
Mingming Yin4a72d652014-01-03 18:54:18 -0800723 if (isInCall() && (device == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700724 // when in call, get the device for Phone strategy
725 device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
726 break;
727 }
728#ifdef AUDIO_EXTN_FM_ENABLED
729 if (mForceUse[AudioSystem::FOR_MEDIA] == AudioSystem::FORCE_SPEAKER) {
730 device = AUDIO_DEVICE_OUT_SPEAKER;
731 break;
732 }
733#endif
734
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700735 if (strategy != STRATEGY_SONIFICATION) {
736 // no sonification on remote submix (e.g. WFD)
737 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
738 }
739 if ((device2 == AUDIO_DEVICE_NONE) &&
740 mHasA2dp && (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
741 (getA2dpOutput() != 0) && !mA2dpSuspended) {
742 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
743 if (device2 == AUDIO_DEVICE_NONE) {
744 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
745 }
746 if (device2 == AUDIO_DEVICE_NONE) {
747 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
748 }
749 }
750 if (device2 == AUDIO_DEVICE_NONE) {
751 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
752 }
753 if (device2 == AUDIO_DEVICE_NONE) {
754 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
755 }
756 if (device2 == AUDIO_DEVICE_NONE) {
757 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
758 }
759 if (device2 == AUDIO_DEVICE_NONE) {
760 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
761 }
762 if (device2 == AUDIO_DEVICE_NONE) {
763 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
764 }
Apoorv Raghuvanshi7566f8b2014-01-17 13:10:09 -0800765 if ((strategy != STRATEGY_SONIFICATION) && (device == AUDIO_DEVICE_NONE)
766 && (device2 == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700767 // no sonification on aux digital (e.g. HDMI)
768 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
769 }
770 if ((device2 == AUDIO_DEVICE_NONE) &&
Krishnankutty Kolathappilly4b7fdaa2013-12-16 00:40:11 -0800771 (mForceUse[AudioSystem::FOR_DOCK] == AudioSystem::FORCE_ANALOG_DOCK)
772 && (strategy != STRATEGY_SONIFICATION)) {
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700773 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
774 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700775#ifdef AUDIO_EXTN_FM_ENABLED
Apoorv Raghuvanshi7566f8b2014-01-17 13:10:09 -0800776 if ((strategy != STRATEGY_SONIFICATION) && (device == AUDIO_DEVICE_NONE)
777 && (device2 == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700778 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_FM_TX;
779 }
780#endif
781#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Apoorv Raghuvanshi7566f8b2014-01-17 13:10:09 -0800782 if ((strategy != STRATEGY_SONIFICATION) && (device == AUDIO_DEVICE_NONE)
783 && (device2 == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700784 // no sonification on WFD sink
785 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_PROXY;
786 }
787#endif
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700788 if (device2 == AUDIO_DEVICE_NONE) {
789 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
790 }
791
792 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
793 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
794 device |= device2;
795 if (device) break;
796 device = mDefaultOutputDevice;
797 if (device == AUDIO_DEVICE_NONE) {
798 ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA");
799 }
800 } break;
801
802 default:
803 ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
804 break;
805 }
806
807 ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
808 return device;
809}
810
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700811audio_devices_t AudioPolicyManager::getDeviceForInputSource(int inputSource)
812{
813 uint32_t device = AUDIO_DEVICE_NONE;
814
815 switch (inputSource) {
816 case AUDIO_SOURCE_VOICE_UPLINK:
817 if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
818 device = AUDIO_DEVICE_IN_VOICE_CALL;
819 break;
820 }
821 // FALL THROUGH
822
823 case AUDIO_SOURCE_DEFAULT:
824 case AUDIO_SOURCE_MIC:
825 case AUDIO_SOURCE_VOICE_RECOGNITION:
826 case AUDIO_SOURCE_HOTWORD:
827 case AUDIO_SOURCE_VOICE_COMMUNICATION:
828 if (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO &&
829 mAvailableInputDevices & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
830 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
831 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_WIRED_HEADSET) {
832 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
833 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET) {
834 device = AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET;
835 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
836 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
837 }
838 break;
839 case AUDIO_SOURCE_CAMCORDER:
840 if (mAvailableInputDevices & AUDIO_DEVICE_IN_BACK_MIC) {
841 device = AUDIO_DEVICE_IN_BACK_MIC;
842 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
843 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
844 }
845 break;
846 case AUDIO_SOURCE_VOICE_DOWNLINK:
847 case AUDIO_SOURCE_VOICE_CALL:
848 if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
849 device = AUDIO_DEVICE_IN_VOICE_CALL;
850 }
851 break;
852 case AUDIO_SOURCE_REMOTE_SUBMIX:
853 if (mAvailableInputDevices & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
854 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
855 }
856 break;
857#ifdef AUDIO_EXTN_FM_ENABLED
858 case AUDIO_SOURCE_FM_RX:
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700859 device = AUDIO_DEVICE_IN_FM_RX;
860 break;
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700861 case AUDIO_SOURCE_FM_RX_A2DP:
862 device = AUDIO_DEVICE_IN_FM_RX_A2DP;
863 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700864#endif
865 default:
866 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
867 break;
868 }
869 ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
870 return device;
871}
872
873AudioPolicyManager::device_category AudioPolicyManager::getDeviceCategory(audio_devices_t device)
874{
875 switch(getDeviceForVolume(device)) {
876 case AUDIO_DEVICE_OUT_EARPIECE:
877 return DEVICE_CATEGORY_EARPIECE;
878 case AUDIO_DEVICE_OUT_WIRED_HEADSET:
879 case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
880 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
881 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
882 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
883 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
884#ifdef AUDIO_EXTN_FM_ENABLED
885 case AUDIO_DEVICE_OUT_FM:
886#endif
887 return DEVICE_CATEGORY_HEADSET;
888 case AUDIO_DEVICE_OUT_SPEAKER:
889 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
890 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
891 case AUDIO_DEVICE_OUT_AUX_DIGITAL:
892 case AUDIO_DEVICE_OUT_USB_ACCESSORY:
893 case AUDIO_DEVICE_OUT_USB_DEVICE:
894 case AUDIO_DEVICE_OUT_REMOTE_SUBMIX:
895#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
896 case AUDIO_DEVICE_OUT_PROXY:
897#endif
898 default:
899 return DEVICE_CATEGORY_SPEAKER;
900 }
901}
902
903status_t AudioPolicyManager::checkAndSetVolume(int stream,
904 int index,
905 audio_io_handle_t output,
906 audio_devices_t device,
907 int delayMs,
908 bool force)
909{
910 ALOGV("checkAndSetVolume: index %d output %d device %x", index, output, device);
911 // do not change actual stream volume if the stream is muted
912 if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
913 ALOGVV("checkAndSetVolume() stream %d muted count %d",
914 stream, mOutputs.valueFor(output)->mMuteCount[stream]);
915 return NO_ERROR;
916 }
917
918 // do not change in call volume if bluetooth is connected and vice versa
919 if ((stream == AudioSystem::VOICE_CALL && mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
920 (stream == AudioSystem::BLUETOOTH_SCO && mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO)) {
921 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
922 stream, mForceUse[AudioSystem::FOR_COMMUNICATION]);
923 return INVALID_OPERATION;
924 }
925
926 float volume = computeVolume(stream, index, output, device);
927 // We actually change the volume if:
928 // - the float value returned by computeVolume() changed
929 // - the force flag is set
930 if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
931 force) {
932 mOutputs.valueFor(output)->mCurVolume[stream] = volume;
933 ALOGV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
934 // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
935 // enabled
936 if (stream == AudioSystem::BLUETOOTH_SCO) {
937 mpClientInterface->setStreamVolume(AudioSystem::VOICE_CALL, volume, output, delayMs);
938#ifdef AUDIO_EXTN_FM_ENABLED
939 } else if (stream == AudioSystem::MUSIC &&
940 output == mPrimaryOutput) {
941 float fmVolume = -1.0;
942 fmVolume = computeVolume(stream, index, output, device);
943 if (fmVolume >= 0) {
944 AudioParameter param = AudioParameter();
945 param.addFloat(String8("fm_volume"), fmVolume);
946 ALOGV("checkAndSetVolume setParameters fm_volume, volume=:%f delay=:%d",fmVolume,delayMs*2);
947 //Double delayMs to avoid sound burst while device switch.
948 mpClientInterface->setParameters(mPrimaryOutput, param.toString(), delayMs*2);
949 }
950#endif
951 }
952 mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs);
953 }
954
955 if (stream == AudioSystem::VOICE_CALL ||
956 stream == AudioSystem::BLUETOOTH_SCO) {
957 float voiceVolume;
Vidyakumar Athota35543452014-04-16 10:00:39 -0700958 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
959 if (stream == AudioSystem::VOICE_CALL) {
960 voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
961 } else {
962 voiceVolume = 1.0;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700963 }
964
965 if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) {
966 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
967 mLastVoiceVolume = voiceVolume;
968 }
969 }
970
971 return NO_ERROR;
972}
973
974
975float AudioPolicyManager::computeVolume(int stream,
976 int index,
977 audio_io_handle_t output,
978 audio_devices_t device)
979{
980 float volume = 1.0;
981 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
982
983 if (device == AUDIO_DEVICE_NONE) {
984 device = outputDesc->device();
985 }
986
987 // if volume is not 0 (not muted), force media volume to max on digital output
988 if (stream == AudioSystem::MUSIC &&
989 index != mStreams[stream].mIndexMin &&
990 (device == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
991 device == AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET ||
992 device == AUDIO_DEVICE_OUT_USB_ACCESSORY ||
993#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
994 device == AUDIO_DEVICE_OUT_PROXY ||
995#endif
996 device == AUDIO_DEVICE_OUT_USB_DEVICE )) {
997 return 1.0;
998 }
999#ifdef AUDIO_EXTN_INCALL_MUSIC_ENABLED
1000 if (stream == AudioSystem::INCALL_MUSIC) {
1001 return 1.0;
1002 }
1003#endif
1004 return AudioPolicyManagerBase::computeVolume(stream, index, output, device);
1005}
Naresh Tanniru36c08932014-01-27 18:40:53 +05301006
1007
1008audio_io_handle_t AudioPolicyManager::getOutput(AudioSystem::stream_type stream,
1009 uint32_t samplingRate,
1010 uint32_t format,
1011 uint32_t channelMask,
1012 AudioSystem::output_flags flags,
1013 const audio_offload_info_t *offloadInfo)
1014{
1015 audio_io_handle_t output = 0;
1016 uint32_t latency = 0;
1017 routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);
1018 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1019 IOProfile *profile = NULL;
1020
1021#ifdef VOICE_CONCURRENCY
Naresh Tanniru811f4982014-03-18 17:25:32 +05301022 char propValue[PROPERTY_VALUE_MAX];
1023 bool prop_play_enabled=false, prop_voip_enabled = false;
1024
1025 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1026 prop_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1027 }
1028
1029 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1030 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1031 }
1032
1033 if (prop_play_enabled) {
1034 //check if voice call is active / running in background
1035 if((AudioSystem::MODE_IN_CALL == mPhoneState) ||
1036 ((AudioSystem::MODE_IN_CALL == mPrevPhoneState)
1037 && (AudioSystem::MODE_IN_COMMUNICATION == mPhoneState)))
1038 {
1039 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1040 if(prop_voip_enabled) {
1041 ALOGD(" IN call mode returing no output .. for VoIP usecase flags: %x ", flags );
1042 // flags = (AudioSystem::output_flags)AUDIO_OUTPUT_FLAG_FAST;
1043 return 0;
1044 }
1045 }
1046 else {
1047 ALOGD(" IN call mode adding ULL flags .. flags: %x ", flags );
1048 flags = (AudioSystem::output_flags)AUDIO_OUTPUT_FLAG_FAST;
1049 }
1050 }
1051 } else if (prop_voip_enabled) {
1052 //check if voice call is active / running in background
1053 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1054 //return only ULL ouput
1055 if((AudioSystem::MODE_IN_CALL == mPhoneState) ||
1056 ((AudioSystem::MODE_IN_CALL == mPrevPhoneState)
1057 && (AudioSystem::MODE_IN_COMMUNICATION == mPhoneState)))
1058 {
1059 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1060 ALOGD(" IN call mode returing no output .. for VoIP usecase flags: %x ", flags );
1061 // flags = (AudioSystem::output_flags)AUDIO_OUTPUT_FLAG_FAST;
1062 return 0;
1063 }
1064 }
Naresh Tanniru36c08932014-01-27 18:40:53 +05301065 }
1066#endif
1067
1068#ifdef WFD_CONCURRENCY
1069 if ((mAvailableOutputDevices & AUDIO_DEVICE_OUT_PROXY)
1070 && (stream != AudioSystem::MUSIC)) {
1071 ALOGV(" WFD mode adding ULL flags for non music stream.. flags: %x ", flags );
1072 //For voip paths
1073 if(flags & AudioSystem::OUTPUT_FLAG_DIRECT)
1074 flags = AudioSystem::OUTPUT_FLAG_DIRECT;
1075 else //route every thing else to ULL path
1076 flags = (AudioSystem::output_flags)AUDIO_OUTPUT_FLAG_FAST;
1077 }
1078#endif
1079
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05301080 ALOGD(" getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x ",
Naresh Tanniru36c08932014-01-27 18:40:53 +05301081 device, stream, samplingRate, format, channelMask, flags);
1082
1083
1084
1085#ifdef AUDIO_POLICY_TEST
1086 if (mCurOutput != 0) {
1087 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1088 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1089
1090 if (mTestOutputs[mCurOutput] == 0) {
1091 ALOGV("getOutput() opening test output");
1092 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL);
1093 outputDesc->mDevice = mTestDevice;
1094 outputDesc->mSamplingRate = mTestSamplingRate;
1095 outputDesc->mFormat = mTestFormat;
1096 outputDesc->mChannelMask = mTestChannels;
1097 outputDesc->mLatency = mTestLatencyMs;
1098 outputDesc->mFlags = (audio_output_flags_t)(mDirectOutput ? AudioSystem::OUTPUT_FLAG_DIRECT : 0);
1099 outputDesc->mRefCount[stream] = 0;
1100 mTestOutputs[mCurOutput] = mpClientInterface->openOutput(0, &outputDesc->mDevice,
1101 &outputDesc->mSamplingRate,
1102 &outputDesc->mFormat,
1103 &outputDesc->mChannelMask,
1104 &outputDesc->mLatency,
1105 outputDesc->mFlags,
1106 offloadInfo);
1107 if (mTestOutputs[mCurOutput]) {
1108 AudioParameter outputCmd = AudioParameter();
1109 outputCmd.addInt(String8("set_id"),mCurOutput);
1110 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1111 addOutput(mTestOutputs[mCurOutput], outputDesc);
1112 }
1113 }
1114 return mTestOutputs[mCurOutput];
1115 }
1116#endif //AUDIO_POLICY_TEST
1117
1118 // open a direct output if required by specified parameters
1119 //force direct flag if offload flag is set: offloading implies a direct output stream
1120 // and all common behaviors are driven by checking only the direct flag
1121 // this should normally be set appropriately in the policy configuration file
1122 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1123 flags = (AudioSystem::output_flags)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1124 }
1125
1126 if ((format == AudioSystem::PCM_16_BIT) &&(AudioSystem::popCount(channelMask) > 2)) {
1127 ALOGV("owerwrite flag(%x) for PCM16 multi-channel(CM:%x) playback", flags ,channelMask);
1128 flags = (AudioSystem::output_flags)AUDIO_OUTPUT_FLAG_DIRECT;
1129 }
1130
1131 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1132 // creating an offloaded track and tearing it down immediately after start when audioflinger
1133 // detects there is an active non offloadable effect.
1134 // FIXME: We should check the audio session here but we do not have it in this context.
1135 // This may prevent offloading in rare situations where effects are left active by apps
1136 // in the background.
1137 if ((((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
1138 !isNonOffloadableEffectEnabled()) &&
1139 flags & AUDIO_OUTPUT_FLAG_DIRECT) {
1140 profile = getProfileForDirectOutput(device,
1141 samplingRate,
1142 format,
1143 channelMask,
1144 (audio_output_flags_t)flags);
1145 }
1146
1147 if (profile != NULL) {
1148 AudioOutputDescriptor *outputDesc = NULL;
1149
Subhash Chandra Bose Naripeddy5eb02f72014-04-11 17:54:09 -07001150#ifdef MULTIPLE_OFFLOAD_ENABLED
1151 bool multiOffloadEnabled = false;
1152 char value[PROPERTY_VALUE_MAX] = {0};
1153 property_get("audio.offload.multiple.enabled", value, NULL);
1154 if (atoi(value) || !strncmp("true", value, 4))
1155 multiOffloadEnabled = true;
1156 // if multiple concurrent offload decode is supported
1157 // do no check for reuse and also don't close previous output if its offload
1158 // previous output will be closed during track destruction
1159 if (multiOffloadEnabled)
1160 goto get_output__new_output_desc;
1161#endif
Naresh Tanniru36c08932014-01-27 18:40:53 +05301162 for (size_t i = 0; i < mOutputs.size(); i++) {
1163 AudioOutputDescriptor *desc = mOutputs.valueAt(i);
1164 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1165 outputDesc = desc;
1166 // reuse direct output if currently open and configured with same parameters
1167 if ((samplingRate == outputDesc->mSamplingRate) &&
1168 (format == outputDesc->mFormat) &&
1169 (channelMask == outputDesc->mChannelMask)) {
1170 outputDesc->mDirectOpenCount++;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05301171 ALOGD("getOutput() reusing direct output %d", mOutputs.keyAt(i));
Naresh Tanniru36c08932014-01-27 18:40:53 +05301172 return mOutputs.keyAt(i);
1173 }
1174 }
1175 }
1176 // close direct output if currently open and configured with different parameters
1177 if (outputDesc != NULL) {
1178 closeOutput(outputDesc->mId);
1179 }
Subhash Chandra Bose Naripeddy5eb02f72014-04-11 17:54:09 -07001180get_output__new_output_desc:
Naresh Tanniru36c08932014-01-27 18:40:53 +05301181 outputDesc = new AudioOutputDescriptor(profile);
1182 outputDesc->mDevice = device;
1183 outputDesc->mSamplingRate = samplingRate;
1184 outputDesc->mFormat = (audio_format_t)format;
1185 outputDesc->mChannelMask = (audio_channel_mask_t)channelMask;
1186 outputDesc->mLatency = 0;
1187 outputDesc->mFlags =(audio_output_flags_t) (outputDesc->mFlags | flags);
1188 outputDesc->mRefCount[stream] = 0;
1189 outputDesc->mStopTime[stream] = 0;
1190 outputDesc->mDirectOpenCount = 1;
1191 output = mpClientInterface->openOutput(profile->mModule->mHandle,
1192 &outputDesc->mDevice,
1193 &outputDesc->mSamplingRate,
1194 &outputDesc->mFormat,
1195 &outputDesc->mChannelMask,
1196 &outputDesc->mLatency,
1197 outputDesc->mFlags,
1198 offloadInfo);
1199
1200 // only accept an output with the requested parameters
1201 if (output == 0 ||
1202 (samplingRate != 0 && samplingRate != outputDesc->mSamplingRate) ||
1203 (format != 0 && format != outputDesc->mFormat) ||
1204 (channelMask != 0 && channelMask != outputDesc->mChannelMask)) {
1205 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1206 "format %d %d, channelMask %04x %04x", output, samplingRate,
1207 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1208 outputDesc->mChannelMask);
1209 if (output != 0) {
1210 mpClientInterface->closeOutput(output);
1211 }
1212 delete outputDesc;
1213 return 0;
1214 }
1215 audio_io_handle_t srcOutput = getOutputForEffect();
1216 addOutput(output, outputDesc);
1217 audio_io_handle_t dstOutput = getOutputForEffect();
1218 if (dstOutput == output) {
1219 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1220 }
1221 mPreviousOutputs = mOutputs;
1222 ALOGV("getOutput() returns new direct output %d", output);
1223 return output;
1224 }
1225
1226 // ignoring channel mask due to downmix capability in mixer
1227
1228 // open a non direct output
1229
1230 // for non direct outputs, only PCM is supported
1231 if (audio_is_linear_pcm((audio_format_t)format)) {
1232 // get which output is suitable for the specified stream. The actual
1233 // routing change will happen when startOutput() will be called
1234 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1235
1236 output = selectOutput(outputs, flags);
1237 }
1238 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1239 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1240
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05301241 ALOGD("getOutput() returns output %d", output);
Naresh Tanniru36c08932014-01-27 18:40:53 +05301242
1243 return output;
1244}
1245
1246
1247// This function checks for the parameters which can be offloaded.
1248// This can be enhanced depending on the capability of the DSP and policy
1249// of the system.
1250bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
1251{
Chaithanya Krishna Bacharaju7038d622014-03-27 15:57:27 +05301252 ALOGD("copl: isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Naresh Tanniru36c08932014-01-27 18:40:53 +05301253 " BitRate=%u, duration=%lld us, has_video=%d",
1254 offloadInfo.sample_rate, offloadInfo.channel_mask,
1255 offloadInfo.format,
1256 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
1257 offloadInfo.has_video);
1258
1259#ifdef VOICE_CONCURRENCY
Naresh Tanniru811f4982014-03-18 17:25:32 +05301260 char concpropValue[PROPERTY_VALUE_MAX];
1261 if(property_get("voice.playback.conc.disabled", concpropValue, NULL)) {
1262 bool propenabled = atoi(concpropValue) || !strncmp("true", concpropValue, 4);
1263 if (propenabled) {
1264 if(isInCall())
1265 {
Chaithanya Krishna Bacharaju7038d622014-03-27 15:57:27 +05301266 ALOGD("\n copl: blocking compress offload on call mode\n");
Naresh Tanniru811f4982014-03-18 17:25:32 +05301267 return false;
1268 }
1269 }
Naresh Tanniru36c08932014-01-27 18:40:53 +05301270 }
Naresh Tanniru811f4982014-03-18 17:25:32 +05301271
Naresh Tanniru36c08932014-01-27 18:40:53 +05301272#endif
Naresh Tanniru36c08932014-01-27 18:40:53 +05301273 // Check if stream type is music, then only allow offload as of now.
1274 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
1275 {
Chaithanya Krishna Bacharaju7038d622014-03-27 15:57:27 +05301276 ALOGD("copl: isOffloadSupported: stream_type != MUSIC, returning false");
Naresh Tanniru36c08932014-01-27 18:40:53 +05301277 return false;
1278 }
1279
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001280 char propValue[PROPERTY_VALUE_MAX];
1281 bool pcmOffload = false;
1282 if (audio_is_offload_pcm(offloadInfo.format)) {
1283 if(property_get("audio.offload.pcm.enable", propValue, NULL)) {
Naresh Tanniru36c08932014-01-27 18:40:53 +05301284 bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001285 if (prop_enabled) {
1286 ALOGW("PCM offload property is enabled");
1287 pcmOffload = true;
Naresh Tanniru36c08932014-01-27 18:40:53 +05301288 }
1289 }
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001290 if (!pcmOffload) {
Chaithanya Krishna Bacharaju7038d622014-03-27 15:57:27 +05301291 ALOGD("copl: PCM offload disabled by property audio.offload.pcm.enable");
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001292 return false;
1293 }
1294 }
1295
1296 if (!pcmOffload) {
1297 // Check if offload has been disabled
1298 if (property_get("audio.offload.disable", propValue, "0")) {
1299 if (atoi(propValue) != 0) {
Chaithanya Krishna Bacharaju7038d622014-03-27 15:57:27 +05301300 ALOGD("copl: offload disabled by audio.offload.disable=%s", propValue );
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001301 return false;
Naresh Tanniru36c08932014-01-27 18:40:53 +05301302 }
1303 }
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001304
1305 //check if it's multi-channel AAC format
1306 if (AudioSystem::popCount(offloadInfo.channel_mask) > 2
1307 && offloadInfo.format == AUDIO_FORMAT_AAC) {
Chaithanya Krishna Bacharaju7038d622014-03-27 15:57:27 +05301308 ALOGD("copl: offload disabled for multi-channel AAC format");
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001309 return false;
1310 }
1311
1312 if (offloadInfo.has_video)
1313 {
1314 if(property_get("av.offload.enable", propValue, NULL)) {
1315 bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1316 if (!prop_enabled) {
1317 ALOGW("offload disabled by av.offload.enable = %s ", propValue );
1318 return false;
1319 }
1320 } else {
1321 return false;
1322 }
1323
1324 if(offloadInfo.is_streaming) {
1325 if (property_get("av.streaming.offload.enable", propValue, NULL)) {
1326 bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1327 if (!prop_enabled) {
1328 ALOGW("offload disabled by av.streaming.offload.enable = %s ", propValue );
1329 return false;
1330 }
1331 } else {
1332 //Do not offload AV streamnig if the property is not defined
1333 return false;
1334 }
1335 }
Chaithanya Krishna Bacharaju7038d622014-03-27 15:57:27 +05301336 ALOGD("copl: isOffloadSupported: has_video == true, property\
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001337 set to enable offload");
1338 }
Naresh Tanniru36c08932014-01-27 18:40:53 +05301339 }
1340
1341 //If duration is less than minimum value defined in property, return false
1342 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
1343 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
Chaithanya Krishna Bacharaju7038d622014-03-27 15:57:27 +05301344 ALOGD("copl: Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
Naresh Tanniru36c08932014-01-27 18:40:53 +05301345 return false;
1346 }
1347 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
Chaithanya Krishna Bacharaju7038d622014-03-27 15:57:27 +05301348 ALOGD("copl: Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
Naresh Tanniru36c08932014-01-27 18:40:53 +05301349 //duration checks only valid for MP3/AAC formats,
1350 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001351 if (offloadInfo.format == AUDIO_FORMAT_MP3 || offloadInfo.format == AUDIO_FORMAT_AAC || pcmOffload)
Naresh Tanniru36c08932014-01-27 18:40:53 +05301352 return false;
1353 }
1354
1355 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1356 // creating an offloaded track and tearing it down immediately after start when audioflinger
1357 // detects there is an active non offloadable effect.
1358 // FIXME: We should check the audio session here but we do not have it in this context.
1359 // This may prevent offloading in rare situations where effects are left active by apps
1360 // in the background.
1361 if (isNonOffloadableEffectEnabled()) {
1362 return false;
1363 }
1364
1365 // See if there is a profile to support this.
1366 // AUDIO_DEVICE_NONE
1367 IOProfile *profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
1368 offloadInfo.sample_rate,
1369 offloadInfo.format,
1370 offloadInfo.channel_mask,
1371 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Chaithanya Krishna Bacharaju7038d622014-03-27 15:57:27 +05301372 ALOGD("copl: isOffloadSupported() profile %sfound", profile != NULL ? "" : "NOT ");
Naresh Tanniru36c08932014-01-27 18:40:53 +05301373 return (profile != NULL);
1374}
1375
1376void AudioPolicyManager::setPhoneState(int state)
1377
1378{
Naresh Tanniru811f4982014-03-18 17:25:32 +05301379 ALOGD("setPhoneState() state %d", state);
Naresh Tanniru36c08932014-01-27 18:40:53 +05301380 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
1381 if (state < 0 || state >= AudioSystem::NUM_MODES) {
1382 ALOGW("setPhoneState() invalid state %d", state);
1383 return;
1384 }
1385
1386 if (state == mPhoneState ) {
1387 ALOGW("setPhoneState() setting same state %d", state);
1388 return;
1389 }
1390
1391 // if leaving call state, handle special case of active streams
1392 // pertaining to sonification strategy see handleIncallSonification()
1393 if (isInCall()) {
1394 ALOGV("setPhoneState() in call state management: new state is %d", state);
1395 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
1396 handleIncallSonification(stream, false, true);
1397 }
1398 }
1399
1400 // store previous phone state for management of sonification strategy below
1401 int oldState = mPhoneState;
1402 mPhoneState = state;
1403 bool force = false;
1404
1405 // are we entering or starting a call
1406 if (!isStateInCall(oldState) && isStateInCall(state)) {
1407 ALOGV(" Entering call in setPhoneState()");
1408 // force routing command to audio hardware when starting a call
1409 // even if no device change is needed
1410 force = true;
1411 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
1412 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
1413 sVolumeProfiles[AUDIO_STREAM_VOICE_CALL][j];
1414 }
1415 } else if (isStateInCall(oldState) && !isStateInCall(state)) {
1416 ALOGV(" Exiting call in setPhoneState()");
1417 // force routing command to audio hardware when exiting a call
1418 // even if no device change is needed
1419 force = true;
1420 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
1421 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
1422 sVolumeProfiles[AUDIO_STREAM_DTMF][j];
1423 }
1424 } else if (isStateInCall(state) && (state != oldState)) {
1425 ALOGV(" Switching between telephony and VoIP in setPhoneState()");
1426 // force routing command to audio hardware when switching between telephony and VoIP
1427 // even if no device change is needed
1428 force = true;
1429 }
1430
1431 // check for device and output changes triggered by new phone state
1432 newDevice = getNewDevice(mPrimaryOutput, false /*fromCache*/);
1433 checkA2dpSuspend();
1434 checkOutputForAllStrategies();
1435 updateDevicesAndOutputs();
1436
1437 AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
1438
1439 // force routing command to audio hardware when ending call
1440 // even if no device change is needed
1441 if (isStateInCall(oldState) && newDevice == AUDIO_DEVICE_NONE) {
1442 newDevice = hwOutputDesc->device();
1443 }
Naresh Tanniru811f4982014-03-18 17:25:32 +05301444#ifdef VOICE_CONCURRENCY
1445 char propValue[PROPERTY_VALUE_MAX];
1446 bool prop_playback_enabled = false, prop_rec_enabled=false, prop_voip_enabled = false;
1447
1448 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1449 prop_playback_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1450 }
1451
1452 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
1453 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1454 }
1455
1456 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1457 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1458 }
1459
1460 if((AudioSystem::MODE_IN_CALL != oldState) && (AudioSystem::MODE_IN_CALL == state)) {
1461 ALOGD("Entering to call mode oldState :: %d state::%d ",oldState, state);
1462
1463 if(prop_playback_enabled) {
1464 //Call invalidate to reset all opened non ULL audio tracks
1465 // Move tracks associated to this strategy from previous output to new output
1466 for (int i = AudioSystem::SYSTEM; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
1467 ALOGV(" Invalidate on call mode for stream :: %d ", i);
1468 //FIXME see fixme on name change
1469 mpClientInterface->setStreamOutput((AudioSystem::stream_type)i,
1470 0 /* ignored */);
1471 }
1472 }
1473
1474 if(prop_rec_enabled) {
1475 //Close all active inputs
1476 audio_io_handle_t activeInput = getActiveInput();
1477 if (activeInput != 0) {
1478 AudioInputDescriptor *activeDesc = mInputs.valueFor(activeInput);
1479 switch(activeDesc->mInputSource) {
1480 case AUDIO_SOURCE_VOICE_UPLINK:
1481 case AUDIO_SOURCE_VOICE_DOWNLINK:
1482 case AUDIO_SOURCE_VOICE_CALL:
1483 ALOGD("FOUND active input during call active: %d",activeDesc->mInputSource);
1484 break;
1485
1486 case AUDIO_SOURCE_VOICE_COMMUNICATION:
1487 if(prop_voip_enabled) {
1488 ALOGD("CLOSING VoIP input source on call setup :%d ",activeDesc->mInputSource);
1489 stopInput(activeInput);
1490 releaseInput(activeInput);
1491 }
1492 break;
1493
1494 default:
1495 ALOGD("CLOSING input on call setup for inputSource: %d",activeDesc->mInputSource);
1496 stopInput(activeInput);
1497 releaseInput(activeInput);
1498 break;
1499 }
1500 }
1501 } else if(prop_voip_enabled) {
1502 audio_io_handle_t activeInput = getActiveInput();
1503 if (activeInput != 0) {
1504 AudioInputDescriptor *activeDesc = mInputs.valueFor(activeInput);
1505 if(AUDIO_SOURCE_VOICE_COMMUNICATION == activeDesc->mInputSource) {
1506 ALOGD("CLOSING VoIP on call setup : %d",activeDesc->mInputSource);
1507 stopInput(activeInput);
1508 releaseInput(activeInput);
1509 }
1510 }
1511 }
1512
1513 //suspend PCM (deep-buffer) output & close compress & direct tracks
1514 for (size_t i = 0; i < mOutputs.size(); i++) {
1515 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
Naresh Tanniru1fd15d32014-04-25 20:23:19 +05301516 if (!outputDesc || !outputDesc->mProfile) {
1517 ALOGD("ouput desc / profile is NULL");
1518 continue;
1519 }
Naresh Tanniru811f4982014-03-18 17:25:32 +05301520 if (((!outputDesc->isDuplicated() &&outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY))
1521 && prop_playback_enabled) {
1522 ALOGD(" calling suspendOutput on call mdoe for primary output");
1523 mpClientInterface->suspendOutput(mOutputs.keyAt(i));
1524 } //Close compress all sessions
1525 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
1526 && prop_playback_enabled) {
1527 ALOGD(" calling closeOutput on call mdoe for COMPRESS output");
1528 closeOutput(mOutputs.keyAt(i));
1529 }
1530 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_VOIP_RX)
1531 && prop_voip_enabled) {
1532 ALOGD(" calling closeOutput on call mdoe for DIRECT output");
1533 closeOutput(mOutputs.keyAt(i));
1534 }
1535 }
1536 }
1537
1538 if((AudioSystem::MODE_IN_CALL == oldState) && (AudioSystem::MODE_IN_CALL != state)
1539 && prop_playback_enabled) {
1540 ALOGD("EXITING from call mode oldState :: %d state::%d \n",oldState, state);
1541 //restore PCM (deep-buffer) output after call termination
1542 for (size_t i = 0; i < mOutputs.size(); i++) {
1543 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
Naresh Tanniru1fd15d32014-04-25 20:23:19 +05301544 if (!outputDesc || !outputDesc->mProfile) {
1545 ALOGD("ouput desc / profile is NULL");
1546 continue;
1547 }
Naresh Tanniru811f4982014-03-18 17:25:32 +05301548 if (!outputDesc->isDuplicated() &&outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1549 ALOGD("calling restoreOutput after call mode for primary output");
1550 mpClientInterface->restoreOutput(mOutputs.keyAt(i));
1551 }
1552 }
1553 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
1554 for (int i = AudioSystem::SYSTEM; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
1555 ALOGD("Invalidate on call mode for stream :: %d ", i);
1556 //FIXME see fixme on name change
1557 mpClientInterface->setStreamOutput((AudioSystem::stream_type)i,
1558 0 /* ignored */);
1559 }
1560 }
Naresh Tanniru811f4982014-03-18 17:25:32 +05301561#endif
Karthik Reddy Katta060a6c42014-05-20 15:21:28 +05301562 mPrevPhoneState = oldState;
Naresh Tanniru36c08932014-01-27 18:40:53 +05301563
1564 int delayMs = 0;
1565 if (isStateInCall(state)) {
1566 nsecs_t sysTime = systemTime();
1567 for (size_t i = 0; i < mOutputs.size(); i++) {
1568 AudioOutputDescriptor *desc = mOutputs.valueAt(i);
1569 // mute media and sonification strategies and delay device switch by the largest
1570 // latency of any output where either strategy is active.
1571 // This avoid sending the ring tone or music tail into the earpiece or headset.
1572 if ((desc->isStrategyActive(STRATEGY_MEDIA,
1573 SONIFICATION_HEADSET_MUSIC_DELAY,
1574 sysTime) ||
1575 desc->isStrategyActive(STRATEGY_SONIFICATION,
1576 SONIFICATION_HEADSET_MUSIC_DELAY,
1577 sysTime)) &&
1578 (delayMs < (int)desc->mLatency*2)) {
1579 delayMs = desc->mLatency*2;
1580 }
1581 setStrategyMute(STRATEGY_MEDIA, true, mOutputs.keyAt(i));
1582 setStrategyMute(STRATEGY_MEDIA, false, mOutputs.keyAt(i), MUTE_TIME_MS,
1583 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
1584 setStrategyMute(STRATEGY_SONIFICATION, true, mOutputs.keyAt(i));
1585 setStrategyMute(STRATEGY_SONIFICATION, false, mOutputs.keyAt(i), MUTE_TIME_MS,
1586 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
1587 }
1588 }
1589
1590 // change routing is necessary
1591 setOutputDevice(mPrimaryOutput, newDevice, force, delayMs);
1592
1593 // if entering in call state, handle special case of active streams
1594 // pertaining to sonification strategy see handleIncallSonification()
1595 if (isStateInCall(state)) {
1596 ALOGV("setPhoneState() in call state management: new state is %d", state);
1597 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
1598 handleIncallSonification(stream, true, true);
1599 }
1600 }
1601
1602 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
1603 if (state == AudioSystem::MODE_RINGTONE &&
1604 isStreamActive(AudioSystem::MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
1605 mLimitRingtoneVolume = true;
1606 } else {
1607 mLimitRingtoneVolume = false;
1608 }
Naresh Tanniru811f4982014-03-18 17:25:32 +05301609 ALOGD(" End of setPhoneState ... mPhoneState: %d ",mPhoneState);
Naresh Tanniru36c08932014-01-27 18:40:53 +05301610}
1611
Karthik Reddy Katta060a6c42014-05-20 15:21:28 +05301612bool AudioPolicyManager::isStateInCall(int state)
1613{
1614 return ((state == AudioSystem::MODE_IN_CALL) || (state == AudioSystem::MODE_IN_COMMUNICATION) ||
1615 ((state == AudioSystem::MODE_RINGTONE) && (mPrevPhoneState == AudioSystem::MODE_IN_CALL)));
1616}
1617
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001618extern "C" AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001619{
1620 return new AudioPolicyManager(clientInterface);
1621}
1622
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001623extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001624{
1625 delete interface;
1626}
1627
1628}; // namespace android