blob: 03f6c41e34ce14cf6996643408b365152ad6251c [file] [log] [blame]
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001/*
Mingming Yin4a72d652014-01-03 18:54:18 -08002 * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07003 * Not a contribution.
4 *
5 * Copyright (C) 2009 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#define LOG_TAG "AudioPolicyManager"
21//#define LOG_NDEBUG 0
22
23//#define VERY_VERBOSE_LOGGING
24#ifdef VERY_VERBOSE_LOGGING
25#define ALOGVV ALOGV
26#else
27#define ALOGVV(a...) do { } while(0)
28#endif
29
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070030// A device mask for all audio output devices that are considered "remote" when evaluating
31// active output devices in isStreamActiveRemotely()
32#define APM_AUDIO_OUT_DEVICE_REMOTE_ALL AUDIO_DEVICE_OUT_REMOTE_SUBMIX
33
34#include <utils/Log.h>
35#include "AudioPolicyManager.h"
36#include <hardware/audio_effect.h>
37#include <hardware/audio.h>
38#include <math.h>
39#include <hardware_legacy/audio_policy_conf.h>
40#include <cutils/properties.h>
41
42namespace android_audio_legacy {
43
44// ----------------------------------------------------------------------------
45// AudioPolicyInterface implementation
46// ----------------------------------------------------------------------------
Tanya Finkelde496d82014-03-05 23:59:45 +020047const char* AudioPolicyManager::HDMI_SPKR_STR = "hdmi_spkr";
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070048
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070049status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
50 AudioSystem::device_connection_state state,
51 const char *device_address)
52{
53 SortedVector <audio_io_handle_t> outputs;
54
55 ALOGV("setDeviceConnectionState() device: %x, state %d, address %s", device, state, device_address);
56
57 // connect/disconnect only 1 device at a time
58 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
59
60 if (strlen(device_address) >= MAX_DEVICE_ADDRESS_LEN) {
61 ALOGE("setDeviceConnectionState() invalid address: %s", device_address);
62 return BAD_VALUE;
63 }
64
65 // handle output devices
66 if (audio_is_output_device(device)) {
67
68 if (!mHasA2dp && audio_is_a2dp_device(device)) {
69 ALOGE("setDeviceConnectionState() invalid A2DP device: %x", device);
70 return BAD_VALUE;
71 }
72 if (!mHasUsb && audio_is_usb_device(device)) {
73 ALOGE("setDeviceConnectionState() invalid USB audio device: %x", device);
74 return BAD_VALUE;
75 }
76 if (!mHasRemoteSubmix && audio_is_remote_submix_device((audio_devices_t)device)) {
77 ALOGE("setDeviceConnectionState() invalid remote submix audio device: %x", device);
78 return BAD_VALUE;
79 }
80
81 // save a copy of the opened output descriptors before any output is opened or closed
82 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
83 mPreviousOutputs = mOutputs;
84 switch (state)
85 {
86 // handle output device connection
87 case AudioSystem::DEVICE_STATE_AVAILABLE:
88 if (mAvailableOutputDevices & device) {
Tanya Finkelde496d82014-03-05 23:59:45 +020089#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
90 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
91 if (!strncmp(device_address, HDMI_SPKR_STR, MAX_DEVICE_ADDRESS_LEN)) {
92 mHdmiAudioDisabled = false;
93 } else {
94 mHdmiAudioEvent = true;
95 }
96 }
97#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070098 ALOGW("setDeviceConnectionState() device already connected: %x", device);
99 return INVALID_OPERATION;
100 }
101 ALOGV("setDeviceConnectionState() connecting device %x", device);
102
103 if (checkOutputsForDevice(device, state, outputs) != NO_ERROR) {
104 return INVALID_OPERATION;
105 }
106 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %d outputs",
107 outputs.size());
108 // register new device as available
109 mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices | device);
110
Tanya Finkelde496d82014-03-05 23:59:45 +0200111#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
112 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
113 if (!strncmp(device_address, HDMI_SPKR_STR, MAX_DEVICE_ADDRESS_LEN)) {
114 mHdmiAudioDisabled = false;
115 } else {
116 mHdmiAudioEvent = true;
117 }
118 if (mHdmiAudioDisabled || !mHdmiAudioEvent) {
119 mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices & ~device);
120 }
121 }
122#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700123 if (!outputs.isEmpty()) {
124 String8 paramStr;
125 if (mHasA2dp && audio_is_a2dp_device(device)) {
126 // handle A2DP device connection
127 AudioParameter param;
128 param.add(String8(AUDIO_PARAMETER_A2DP_SINK_ADDRESS), String8(device_address));
129 paramStr = param.toString();
130 mA2dpDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
131 mA2dpSuspended = false;
132 } else if (audio_is_bluetooth_sco_device(device)) {
133 // handle SCO device connection
134 mScoDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
135 } else if (mHasUsb && audio_is_usb_device(device)) {
136 // handle USB device connection
137 mUsbCardAndDevice = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
138 paramStr = mUsbCardAndDevice;
139 }
140 // not currently handling multiple simultaneous submixes: ignoring remote submix
141 // case and address
142 if (!paramStr.isEmpty()) {
143 for (size_t i = 0; i < outputs.size(); i++) {
144 mpClientInterface->setParameters(outputs[i], paramStr);
145 }
146 }
147 }
148 break;
149 // handle output device disconnection
150 case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
151 if (!(mAvailableOutputDevices & device)) {
Tanya Finkelde496d82014-03-05 23:59:45 +0200152#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
153 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
154 if (!strncmp(device_address, HDMI_SPKR_STR, MAX_DEVICE_ADDRESS_LEN)) {
155 mHdmiAudioDisabled = true;
156 } else {
157 mHdmiAudioEvent = false;
158 }
159 }
160#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700161 ALOGW("setDeviceConnectionState() device not connected: %x", device);
162 return INVALID_OPERATION;
163 }
164
165 ALOGV("setDeviceConnectionState() disconnecting device %x", device);
166 // remove device from available output devices
167 mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices & ~device);
168
Tanya Finkelde496d82014-03-05 23:59:45 +0200169#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
170 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
171 if (!strncmp(device_address, HDMI_SPKR_STR, MAX_DEVICE_ADDRESS_LEN)) {
172 mHdmiAudioDisabled = true;
173 } else {
174 mHdmiAudioEvent = false;
175 }
176 }
177#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700178 checkOutputsForDevice(device, state, outputs);
179 if (mHasA2dp && audio_is_a2dp_device(device)) {
180 // handle A2DP device disconnection
181 mA2dpDeviceAddress = "";
182 mA2dpSuspended = false;
183 } else if (audio_is_bluetooth_sco_device(device)) {
184 // handle SCO device disconnection
185 mScoDeviceAddress = "";
186 } else if (mHasUsb && audio_is_usb_device(device)) {
187 // handle USB device disconnection
188 mUsbCardAndDevice = "";
189 }
190 // not currently handling multiple simultaneous submixes: ignoring remote submix
191 // case and address
192 } break;
193
194 default:
195 ALOGE("setDeviceConnectionState() invalid state: %x", state);
196 return BAD_VALUE;
197 }
198
199 checkA2dpSuspend();
200 checkOutputForAllStrategies();
201 // outputs must be closed after checkOutputForAllStrategies() is executed
202 if (!outputs.isEmpty()) {
203 for (size_t i = 0; i < outputs.size(); i++) {
204 AudioOutputDescriptor *desc = mOutputs.valueFor(outputs[i]);
205 // close unused outputs after device disconnection or direct outputs that have been
206 // opened by checkOutputsForDevice() to query dynamic parameters
207 if ((state == AudioSystem::DEVICE_STATE_UNAVAILABLE) ||
208 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
209 (desc->mDirectOpenCount == 0))) {
210 closeOutput(outputs[i]);
211 }
212 }
213 }
214
215 updateDevicesAndOutputs();
216 audio_devices_t newDevice = getNewDevice(mPrimaryOutput, false /*fromCache*/);
217#ifdef AUDIO_EXTN_FM_ENABLED
218 if(device == AUDIO_DEVICE_OUT_FM) {
219 if (state == AudioSystem::DEVICE_STATE_AVAILABLE) {
220 mOutputs.valueFor(mPrimaryOutput)->changeRefCount(AudioSystem::MUSIC, 1);
221 newDevice = (audio_devices_t)(getNewDevice(mPrimaryOutput, false) | AUDIO_DEVICE_OUT_FM);
222 } else {
223 mOutputs.valueFor(mPrimaryOutput)->changeRefCount(AudioSystem::MUSIC, -1);
224 }
225
226 AudioParameter param = AudioParameter();
227 param.addInt(String8("handle_fm"), (int)newDevice);
228 ALOGV("setDeviceConnectionState() setParameters handle_fm");
229 mpClientInterface->setParameters(mPrimaryOutput, param.toString());
230 }
231#endif
232 for (size_t i = 0; i < mOutputs.size(); i++) {
233 // do not force device change on duplicated output because if device is 0, it will
234 // also force a device 0 for the two outputs it is duplicated to which may override
235 // a valid device selection on those outputs.
236 setOutputDevice(mOutputs.keyAt(i),
237 getNewDevice(mOutputs.keyAt(i), true /*fromCache*/),
238 !mOutputs.valueAt(i)->isDuplicated(),
239 0);
240 }
241
242 if (device == AUDIO_DEVICE_OUT_WIRED_HEADSET) {
243 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
244 } else if (device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO ||
245 device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET ||
246 device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT) {
247 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
248 } else if(device == AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET){
249 device = AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET;
250 } else {
251 return NO_ERROR;
252 }
253 }
254 // handle input devices
255 if (audio_is_input_device(device)) {
256
257 switch (state)
258 {
259 // handle input device connection
260 case AudioSystem::DEVICE_STATE_AVAILABLE: {
261 if (mAvailableInputDevices & device) {
262 ALOGW("setDeviceConnectionState() device already connected: %d", device);
263 return INVALID_OPERATION;
264 }
265 mAvailableInputDevices = mAvailableInputDevices | (device & ~AUDIO_DEVICE_BIT_IN);
266 }
267 break;
268
269 // handle input device disconnection
270 case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
271 if (!(mAvailableInputDevices & device)) {
272 ALOGW("setDeviceConnectionState() device not connected: %d", device);
273 return INVALID_OPERATION;
274 }
275 mAvailableInputDevices = (audio_devices_t) (mAvailableInputDevices & ~device);
276 } break;
277
278 default:
279 ALOGE("setDeviceConnectionState() invalid state: %x", state);
280 return BAD_VALUE;
281 }
282
283 audio_io_handle_t activeInput = getActiveInput();
284 if (activeInput != 0) {
285 AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
286 audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
287 if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) {
288 ALOGV("setDeviceConnectionState() changing device from %x to %x for input %d",
289 inputDesc->mDevice, newDevice, activeInput);
290 inputDesc->mDevice = newDevice;
291 AudioParameter param = AudioParameter();
292 param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
293 mpClientInterface->setParameters(activeInput, param.toString());
294 }
295 }
296
297 return NO_ERROR;
298 }
299
300 ALOGW("setDeviceConnectionState() invalid device: %x", device);
301 return BAD_VALUE;
302}
303
304void AudioPolicyManager::setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config)
305{
306 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState);
307
308 bool forceVolumeReeval = false;
309 switch(usage) {
310 case AudioSystem::FOR_COMMUNICATION:
311 if (config != AudioSystem::FORCE_SPEAKER && config != AudioSystem::FORCE_BT_SCO &&
312 config != AudioSystem::FORCE_NONE) {
313 ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
314 return;
315 }
316 forceVolumeReeval = true;
317 mForceUse[usage] = config;
318 break;
319 case AudioSystem::FOR_MEDIA:
320 if (config != AudioSystem::FORCE_HEADPHONES && config != AudioSystem::FORCE_BT_A2DP &&
321#ifdef AUDIO_EXTN_FM_ENABLED
322 config != AudioSystem::FORCE_SPEAKER &&
323#endif
324 config != AudioSystem::FORCE_WIRED_ACCESSORY &&
325 config != AudioSystem::FORCE_ANALOG_DOCK &&
326 config != AudioSystem::FORCE_DIGITAL_DOCK && config != AudioSystem::FORCE_NONE &&
327 config != AudioSystem::FORCE_NO_BT_A2DP) {
328 ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
329 return;
330 }
331 mForceUse[usage] = config;
332 break;
333 case AudioSystem::FOR_RECORD:
334 if (config != AudioSystem::FORCE_BT_SCO && config != AudioSystem::FORCE_WIRED_ACCESSORY &&
335 config != AudioSystem::FORCE_NONE) {
336 ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
337 return;
338 }
339 mForceUse[usage] = config;
340 break;
341 case AudioSystem::FOR_DOCK:
342 if (config != AudioSystem::FORCE_NONE && config != AudioSystem::FORCE_BT_CAR_DOCK &&
343 config != AudioSystem::FORCE_BT_DESK_DOCK &&
344 config != AudioSystem::FORCE_WIRED_ACCESSORY &&
345 config != AudioSystem::FORCE_ANALOG_DOCK &&
346 config != AudioSystem::FORCE_DIGITAL_DOCK) {
347 ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
348 }
349 forceVolumeReeval = true;
350 mForceUse[usage] = config;
351 break;
352 case AudioSystem::FOR_SYSTEM:
353 if (config != AudioSystem::FORCE_NONE &&
354 config != AudioSystem::FORCE_SYSTEM_ENFORCED) {
355 ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
356 }
357 forceVolumeReeval = true;
358 mForceUse[usage] = config;
359 break;
360 default:
361 ALOGW("setForceUse() invalid usage %d", usage);
362 break;
363 }
364
365 // check for device and output changes triggered by new force usage
366 checkA2dpSuspend();
367 checkOutputForAllStrategies();
368 updateDevicesAndOutputs();
369 for (int i = mOutputs.size() -1; i >= 0; i--) {
370 audio_io_handle_t output = mOutputs.keyAt(i);
371 audio_devices_t newDevice = getNewDevice(output, true /*fromCache*/);
372 setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE));
373 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
374 applyStreamVolumes(output, newDevice, 0, true);
375 }
376 }
377
378 audio_io_handle_t activeInput = getActiveInput();
379 if (activeInput != 0) {
380 AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
381 audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
382 if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) {
383 ALOGV("setForceUse() changing device from %x to %x for input %d",
384 inputDesc->mDevice, newDevice, activeInput);
385 inputDesc->mDevice = newDevice;
386 AudioParameter param = AudioParameter();
387 param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
388 mpClientInterface->setParameters(activeInput, param.toString());
389 }
390 }
391
392}
393
Pavan Chikkala785b6932014-03-24 18:58:11 +0530394status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
395 AudioSystem::stream_type stream,
396 int session)
397{
398 ALOGV("startOutput() output %d, stream %d, session %d", output, stream, session);
399 ssize_t index = mOutputs.indexOfKey(output);
400 if (index < 0) {
401 ALOGW("startOutput() unknow output %d", output);
402 return BAD_VALUE;
403 }
404
405 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
406
407 // increment usage count for this stream on the requested output:
408 // NOTE that the usage count is the same for duplicated output and hardware output which is
409 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
410 outputDesc->changeRefCount(stream, 1);
411
412 if (outputDesc->mRefCount[stream] == 1) {
413 audio_devices_t newDevice = getNewDevice(output, false /*fromCache*/);
414 routing_strategy strategy = getStrategy(stream);
415 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
416 (strategy == STRATEGY_SONIFICATION_RESPECTFUL);
417 uint32_t waitMs = 0;
418 bool force = false;
419 for (size_t i = 0; i < mOutputs.size(); i++) {
420 AudioOutputDescriptor *desc = mOutputs.valueAt(i);
421 if (desc != outputDesc) {
422 // force a device change if any other output is managed by the same hw
423 // module and has a current device selection that differs from selected device.
424 // In this case, the audio HAL must receive the new device selection so that it can
425 // change the device currently selected by the other active output.
426 if (outputDesc->sharesHwModuleWith(desc) &&
427 desc->device() != newDevice) {
428 force = true;
429 }
430 // wait for audio on other active outputs to be presented when starting
431 // a notification so that audio focus effect can propagate.
432 uint32_t latency = desc->latency();
433 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
434 waitMs = latency;
435 }
436 }
437 }
438 uint32_t muteWaitMs = setOutputDevice(output, newDevice, force);
439
440 // handle special case for sonification while in call
441 if (isInCall()) {
442 handleIncallSonification(stream, true, false);
443 }
444
445 // apply volume rules for current stream and device if necessary
446 checkAndSetVolume(stream,
447 mStreams[stream].getVolumeIndex(newDevice),
448 output,
449 newDevice);
450
451 // update the outputs if starting an output with a stream that can affect notification
452 // routing
453 handleNotificationRoutingForStream(stream);
454 if (waitMs > muteWaitMs) {
455 usleep((waitMs - muteWaitMs) * 2 * 1000);
456 }
457 }
458#ifdef DOLBY_UDC
459 // It is observed that in some use-cases where both outputs are present eg. bluetooth and headphone,
460 // the output for particular stream type is decided in this routine. Hence we must call
461 // getDeviceForStrategy in order to get the current active output for this stream type and update
462 // the dolby system property.
463 if (stream == AudioSystem::MUSIC)
464 {
465 audio_devices_t audioOutputDevice = getDeviceForStrategy(getStrategy(AudioSystem::MUSIC), true);
466 DolbySystemProperty::set(audioOutputDevice);
467 }
468#endif // DOLBY_END
469 return NO_ERROR;
470}
471
472
473status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
474 AudioSystem::stream_type stream,
475 int session)
476{
477 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
478 ssize_t index = mOutputs.indexOfKey(output);
479 if (index < 0) {
480 ALOGW("stopOutput() unknow output %d", output);
481 return BAD_VALUE;
482 }
483
484 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
485
486 // handle special case for sonification while in call
487 if (isInCall()) {
488 handleIncallSonification(stream, false, false);
489 }
490
491 if (outputDesc->mRefCount[stream] > 0) {
492 // decrement usage count of this stream on the output
493 outputDesc->changeRefCount(stream, -1);
494 // store time at which the stream was stopped - see isStreamActive()
495 if (outputDesc->mRefCount[stream] == 0) {
496 outputDesc->mStopTime[stream] = systemTime();
497 audio_devices_t newDevice = getNewDevice(output, false /*fromCache*/);
498 // delay the device switch by twice the latency because stopOutput() is executed when
499 // the track stop() command is received and at that time the audio track buffer can
500 // still contain data that needs to be drained. The latency only covers the audio HAL
501 // and kernel buffers. Also the latency does not always include additional delay in the
502 // audio path (audio DSP, CODEC ...)
503 setOutputDevice(output, newDevice, false, outputDesc->mLatency*2);
504
505 // force restoring the device selection on other active outputs if it differs from the
506 // one being selected for this output
507 for (size_t i = 0; i < mOutputs.size(); i++) {
508 audio_io_handle_t curOutput = mOutputs.keyAt(i);
509 AudioOutputDescriptor *desc = mOutputs.valueAt(i);
510 if (curOutput != output &&
511 desc->isActive() &&
512 outputDesc->sharesHwModuleWith(desc) &&
513 (newDevice != desc->device())) {
514 setOutputDevice(curOutput,
515 getNewDevice(curOutput, false /*fromCache*/),
516 true,
517 outputDesc->mLatency*2);
518 }
519 }
520 // update the outputs if stopping one with a stream that can affect notification routing
521 handleNotificationRoutingForStream(stream);
522 }
523 return NO_ERROR;
524 } else {
525 ALOGW("stopOutput() refcount is already 0 for output %d", output);
526 return INVALID_OPERATION;
527 }
528}
529
530audio_devices_t AudioPolicyManager::getNewDevice(audio_io_handle_t output, bool fromCache)
531{
532 audio_devices_t device = AUDIO_DEVICE_NONE;
533
534 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
535 AudioOutputDescriptor *primaryOutputDesc = mOutputs.valueFor(mPrimaryOutput);
536 // check the following by order of priority to request a routing change if necessary:
537 // 1: the strategy enforced audible is active on the output:
538 // use device for strategy enforced audible
539 // 2: we are in call or the strategy phone is active on the output:
540 // use device for strategy phone
541 // 3: the strategy sonification is active on the output:
542 // use device for strategy sonification
543 // 4: the strategy "respectful" sonification is active on the output:
544 // use device for strategy "respectful" sonification
545 // 5: the strategy media is active on the output:
546 // use device for strategy media
547 // 6: the strategy DTMF is active on the output:
548 // use device for strategy DTMF
549 if (outputDesc->isStrategyActive(STRATEGY_ENFORCED_AUDIBLE)) {
550 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
551 } else if (isInCall() ||
552 outputDesc->isStrategyActive(STRATEGY_PHONE)) {
553 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
554 } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION)||
555 (primaryOutputDesc->isStrategyActive(STRATEGY_SONIFICATION)&& !primaryOutputDesc->isStrategyActive(STRATEGY_MEDIA))){
556 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
557 } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION_RESPECTFUL)) {
558 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
559 } else if (outputDesc->isStrategyActive(STRATEGY_MEDIA)) {
560 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
561 } else if (outputDesc->isStrategyActive(STRATEGY_DTMF)) {
562 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
563 }
564
565 ALOGV("getNewDevice() selected device %x", device);
566 return device;
567}
568
569//private function, no changes from AudioPolicyManagerBase
570void AudioPolicyManager::handleNotificationRoutingForStream(AudioSystem::stream_type stream) {
571 switch(stream) {
572 case AudioSystem::MUSIC:
573 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
574 updateDevicesAndOutputs();
575 break;
576 default:
577 break;
578 }
579}
580
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700581audio_io_handle_t AudioPolicyManager::getInput(int inputSource,
582 uint32_t samplingRate,
583 uint32_t format,
584 uint32_t channelMask,
585 AudioSystem::audio_in_acoustics acoustics)
586{
587 audio_io_handle_t input = 0;
588 audio_devices_t device = getDeviceForInputSource(inputSource);
589
590 ALOGV("getInput() inputSource %d, samplingRate %d, format %d, channelMask %x, acoustics %x",
591 inputSource, samplingRate, format, channelMask, acoustics);
592
593 if (device == AUDIO_DEVICE_NONE) {
594 ALOGW("getInput() could not find device for inputSource %d", inputSource);
595 return 0;
596 }
597
Naresh Tanniru811f4982014-03-18 17:25:32 +0530598#ifdef VOICE_CONCURRENCY
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700599
Naresh Tanniru811f4982014-03-18 17:25:32 +0530600 char propValue[PROPERTY_VALUE_MAX];
601 bool prop_rec_enabled=false, prop_voip_enabled = false;
602
603 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
604 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
605 }
606
607 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
608 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
609 }
610
611 if (prop_rec_enabled) {
612 //check if voice call is active / running in background
613 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
614 //Need to block input request
615 if((AudioSystem::MODE_IN_CALL == mPhoneState) ||
616 ((AudioSystem::MODE_IN_CALL == mPrevPhoneState) &&
617 (AudioSystem::MODE_IN_COMMUNICATION == mPhoneState)))
618 {
619 switch(inputSource) {
620 case AUDIO_SOURCE_VOICE_UPLINK:
621 case AUDIO_SOURCE_VOICE_DOWNLINK:
622 case AUDIO_SOURCE_VOICE_CALL:
623 ALOGD("Creating input during incall mode for inputSource: %d ",inputSource);
624 break;
625
626 case AUDIO_SOURCE_VOICE_COMMUNICATION:
627 if(prop_voip_enabled) {
628 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
629 return 0;
630 }
631 break;
632
633 default:
634 ALOGD("BLOCKING input during incall mode for inputSource: %d ",inputSource);
635 return 0;
636 }
637 }
638 }//check for VoIP flag
639 else if(prop_voip_enabled) {
640 //check if voice call is active / running in background
641 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
642 //Need to block input request
643 if((AudioSystem::MODE_IN_CALL == mPhoneState) ||
644 ((AudioSystem::MODE_IN_CALL == mPrevPhoneState) &&
645 (AudioSystem::MODE_IN_COMMUNICATION == mPhoneState)))
646 {
647 if(inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION) {
648 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
649 return 0;
650 }
651 }
652 }
653
654#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700655 IOProfile *profile = getInputProfile(device,
656 samplingRate,
657 format,
658 channelMask);
659 if (profile == NULL) {
660 ALOGW("getInput() could not find profile for device %04x, samplingRate %d, format %d,"
661 "channelMask %04x",
662 device, samplingRate, format, channelMask);
663 return 0;
664 }
665
666 if (profile->mModule->mHandle == 0) {
667 ALOGE("getInput(): HW module %s not opened", profile->mModule->mName);
668 return 0;
669 }
670
671 AudioInputDescriptor *inputDesc = new AudioInputDescriptor(profile);
672
673 inputDesc->mInputSource = inputSource;
674 inputDesc->mDevice = device;
675 inputDesc->mSamplingRate = samplingRate;
676 inputDesc->mFormat = (audio_format_t)format;
677 inputDesc->mChannelMask = (audio_channel_mask_t)channelMask;
678 inputDesc->mRefCount = 0;
679 input = mpClientInterface->openInput(profile->mModule->mHandle,
680 &inputDesc->mDevice,
681 &inputDesc->mSamplingRate,
682 &inputDesc->mFormat,
683 &inputDesc->mChannelMask);
684
685 // only accept input with the exact requested set of parameters
686 if (input == 0 ||
687 (samplingRate != inputDesc->mSamplingRate) ||
688 (format != inputDesc->mFormat) ||
689 (channelMask != inputDesc->mChannelMask)) {
690 ALOGV("getInput() failed opening input: samplingRate %d, format %d, channelMask %d",
691 samplingRate, format, channelMask);
692 if (input != 0) {
693 mpClientInterface->closeInput(input);
694 }
695 delete inputDesc;
696 return 0;
697 }
698 mInputs.add(input, inputDesc);
699 return input;
700}
701
702AudioPolicyManager::routing_strategy AudioPolicyManager::getStrategy(AudioSystem::stream_type stream)
703{
Naresh Tanniru36c08932014-01-27 18:40:53 +0530704 // stream to strategy mapping
705 switch (stream) {
706 case AudioSystem::VOICE_CALL:
707 case AudioSystem::BLUETOOTH_SCO:
708 return STRATEGY_PHONE;
709 case AudioSystem::RING:
710 case AudioSystem::ALARM:
711 return STRATEGY_SONIFICATION;
712 case AudioSystem::NOTIFICATION:
713 return STRATEGY_SONIFICATION_RESPECTFUL;
714 case AudioSystem::DTMF:
715 return STRATEGY_DTMF;
716 default:
717 ALOGE("unknown stream type");
718 case AudioSystem::SYSTEM:
719 // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
720 // while key clicks are played produces a poor result
721 case AudioSystem::TTS:
722 case AudioSystem::MUSIC:
723#ifdef AUDIO_EXTN_INCALL_MUSIC_ENABLED
724 case AudioSystem::INCALL_MUSIC:
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700725#endif
Naresh Tanniru36c08932014-01-27 18:40:53 +0530726#ifdef QCOM_INCALL_MUSIC_ENABLED
727 case AudioSystem::INCALL_MUSIC:
728#endif
729 return STRATEGY_MEDIA;
730 case AudioSystem::ENFORCED_AUDIBLE:
731 return STRATEGY_ENFORCED_AUDIBLE;
732 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700733
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700734}
735
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700736audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
737 bool fromCache)
738{
739 uint32_t device = AUDIO_DEVICE_NONE;
740
741 if (fromCache) {
742 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
743 strategy, mDeviceForStrategy[strategy]);
744 return mDeviceForStrategy[strategy];
745 }
746
747 switch (strategy) {
748
749 case STRATEGY_SONIFICATION_RESPECTFUL:
750 if (isInCall()) {
751 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
752 } else if (isStreamActiveRemotely(AudioSystem::MUSIC,
753 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
754 // while media is playing on a remote device, use the the sonification behavior.
755 // Note that we test this usecase before testing if media is playing because
756 // the isStreamActive() method only informs about the activity of a stream, not
757 // if it's for local playback. Note also that we use the same delay between both tests
758 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
759 } else if (isStreamActive(AudioSystem::MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
760 // while media is playing (or has recently played), use the same device
761 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
762 } else {
763 // when media is not playing anymore, fall back on the sonification behavior
764 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
765 }
766
767 break;
768
769 case STRATEGY_DTMF:
770 if (!isInCall()) {
771 // when off call, DTMF strategy follows the same rules as MEDIA strategy
772 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
773 break;
774 }
775 // when in call, DTMF and PHONE strategies follow the same rules
776 // FALL THROUGH
777
778 case STRATEGY_PHONE:
779 // for phone strategy, we first consider the forced use and then the available devices by order
780 // of priority
781 switch (mForceUse[AudioSystem::FOR_COMMUNICATION]) {
782 case AudioSystem::FORCE_BT_SCO:
783 if (!isInCall() || strategy != STRATEGY_DTMF) {
784 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
785 if (device) break;
786 }
787 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
788 if (device) break;
789 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
790 if (device) break;
791 // if SCO device is requested but no SCO device is available, fall back to default case
792 // FALL THROUGH
793
794 default: // FORCE_NONE
795 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
796 if (mHasA2dp && !isInCall() &&
797 (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
798 (getA2dpOutput() != 0) && !mA2dpSuspended) {
799 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
800 if (device) break;
801 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
802 if (device) break;
803 }
804 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
805 if (device) break;
806 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
807 if (device) break;
808 if (mPhoneState != AudioSystem::MODE_IN_CALL) {
809 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
810 if (device) break;
811 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
812 if (device) break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700813 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
814 if (device) break;
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700815 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
816 if (device) break;
817 }
818
819 // Allow voice call on USB ANLG DOCK headset
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700820 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
821 if (device) break;
822
823 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_EARPIECE;
824 if (device) break;
825 device = mDefaultOutputDevice;
826 if (device == AUDIO_DEVICE_NONE) {
827 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE");
828 }
829 break;
830
831 case AudioSystem::FORCE_SPEAKER:
832 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
833 // A2DP speaker when forcing to speaker output
834 if (mHasA2dp && !isInCall() &&
835 (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
836 (getA2dpOutput() != 0) && !mA2dpSuspended) {
837 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
838 if (device) break;
839 }
840 if (mPhoneState != AudioSystem::MODE_IN_CALL) {
841 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
842 if (device) break;
843 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
844 if (device) break;
845 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
846 if (device) break;
847 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
848 if (device) break;
Helen Zengbbce4952013-12-16 20:26:46 -0800849 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
850 if (device) break;
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700851 }
852 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
853 if (device) break;
854 device = mDefaultOutputDevice;
855 if (device == AUDIO_DEVICE_NONE) {
856 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER");
857 }
858 break;
859 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700860 // FIXME: Why do need to replace with speaker? If voice call is active
861 // We should use device from STRATEGY_PHONE
862#ifdef AUDIO_EXTN_FM_ENABLED
863 if (mAvailableOutputDevices & AUDIO_DEVICE_OUT_FM) {
864 if (mForceUse[AudioSystem::FOR_MEDIA] == AudioSystem::FORCE_SPEAKER) {
865 device = AUDIO_DEVICE_OUT_SPEAKER;
866 }
867 }
868#endif
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700869 break;
870
871 case STRATEGY_SONIFICATION:
872
873 // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
874 // handleIncallSonification().
875 if (isInCall()) {
876 device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
877 break;
878 }
879 // FALL THROUGH
880
881 case STRATEGY_ENFORCED_AUDIBLE:
882 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
883 // except:
884 // - when in call where it doesn't default to STRATEGY_PHONE behavior
885 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
886
887 if ((strategy == STRATEGY_SONIFICATION) ||
888 (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_SYSTEM_ENFORCED)) {
889 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
890 if (device == AUDIO_DEVICE_NONE) {
891 ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION");
892 }
893 }
894 // The second device used for sonification is the same as the device used by media strategy
895 // FALL THROUGH
896
897 case STRATEGY_MEDIA: {
898 uint32_t device2 = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700899
Mingming Yin4a72d652014-01-03 18:54:18 -0800900 if (isInCall() && (device == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700901 // when in call, get the device for Phone strategy
902 device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
903 break;
904 }
905#ifdef AUDIO_EXTN_FM_ENABLED
906 if (mForceUse[AudioSystem::FOR_MEDIA] == AudioSystem::FORCE_SPEAKER) {
907 device = AUDIO_DEVICE_OUT_SPEAKER;
908 break;
909 }
910#endif
911
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700912 if (strategy != STRATEGY_SONIFICATION) {
913 // no sonification on remote submix (e.g. WFD)
914 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
915 }
916 if ((device2 == AUDIO_DEVICE_NONE) &&
917 mHasA2dp && (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
918 (getA2dpOutput() != 0) && !mA2dpSuspended) {
919 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
920 if (device2 == AUDIO_DEVICE_NONE) {
921 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
922 }
923 if (device2 == AUDIO_DEVICE_NONE) {
924 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
925 }
926 }
927 if (device2 == AUDIO_DEVICE_NONE) {
928 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
929 }
930 if (device2 == AUDIO_DEVICE_NONE) {
931 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
932 }
933 if (device2 == AUDIO_DEVICE_NONE) {
934 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
935 }
936 if (device2 == AUDIO_DEVICE_NONE) {
937 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
938 }
939 if (device2 == AUDIO_DEVICE_NONE) {
940 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
941 }
Apoorv Raghuvanshi7566f8b2014-01-17 13:10:09 -0800942 if ((strategy != STRATEGY_SONIFICATION) && (device == AUDIO_DEVICE_NONE)
943 && (device2 == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700944 // no sonification on aux digital (e.g. HDMI)
945 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
946 }
947 if ((device2 == AUDIO_DEVICE_NONE) &&
Krishnankutty Kolathappilly4b7fdaa2013-12-16 00:40:11 -0800948 (mForceUse[AudioSystem::FOR_DOCK] == AudioSystem::FORCE_ANALOG_DOCK)
949 && (strategy != STRATEGY_SONIFICATION)) {
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700950 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
951 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700952#ifdef AUDIO_EXTN_FM_ENABLED
Apoorv Raghuvanshi7566f8b2014-01-17 13:10:09 -0800953 if ((strategy != STRATEGY_SONIFICATION) && (device == AUDIO_DEVICE_NONE)
954 && (device2 == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700955 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_FM_TX;
956 }
957#endif
958#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Apoorv Raghuvanshi7566f8b2014-01-17 13:10:09 -0800959 if ((strategy != STRATEGY_SONIFICATION) && (device == AUDIO_DEVICE_NONE)
960 && (device2 == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700961 // no sonification on WFD sink
962 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_PROXY;
963 }
964#endif
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700965 if (device2 == AUDIO_DEVICE_NONE) {
966 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
967 }
968
969 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
970 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
971 device |= device2;
972 if (device) break;
973 device = mDefaultOutputDevice;
974 if (device == AUDIO_DEVICE_NONE) {
975 ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA");
976 }
977 } break;
978
979 default:
980 ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
981 break;
982 }
983
984 ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
985 return device;
986}
987
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700988audio_devices_t AudioPolicyManager::getDeviceForInputSource(int inputSource)
989{
990 uint32_t device = AUDIO_DEVICE_NONE;
991
992 switch (inputSource) {
993 case AUDIO_SOURCE_VOICE_UPLINK:
994 if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
995 device = AUDIO_DEVICE_IN_VOICE_CALL;
996 break;
997 }
998 // FALL THROUGH
999
1000 case AUDIO_SOURCE_DEFAULT:
1001 case AUDIO_SOURCE_MIC:
1002 case AUDIO_SOURCE_VOICE_RECOGNITION:
1003 case AUDIO_SOURCE_HOTWORD:
1004 case AUDIO_SOURCE_VOICE_COMMUNICATION:
1005 if (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO &&
1006 mAvailableInputDevices & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
1007 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
1008 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_WIRED_HEADSET) {
1009 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
1010 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET) {
1011 device = AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET;
1012 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
1013 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
1014 }
1015 break;
1016 case AUDIO_SOURCE_CAMCORDER:
1017 if (mAvailableInputDevices & AUDIO_DEVICE_IN_BACK_MIC) {
1018 device = AUDIO_DEVICE_IN_BACK_MIC;
1019 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
1020 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
1021 }
1022 break;
1023 case AUDIO_SOURCE_VOICE_DOWNLINK:
1024 case AUDIO_SOURCE_VOICE_CALL:
1025 if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
1026 device = AUDIO_DEVICE_IN_VOICE_CALL;
1027 }
1028 break;
1029 case AUDIO_SOURCE_REMOTE_SUBMIX:
1030 if (mAvailableInputDevices & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
1031 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1032 }
1033 break;
1034#ifdef AUDIO_EXTN_FM_ENABLED
1035 case AUDIO_SOURCE_FM_RX:
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001036 device = AUDIO_DEVICE_IN_FM_RX;
1037 break;
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -07001038 case AUDIO_SOURCE_FM_RX_A2DP:
1039 device = AUDIO_DEVICE_IN_FM_RX_A2DP;
1040 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001041#endif
1042 default:
1043 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
1044 break;
1045 }
1046 ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
1047 return device;
1048}
1049
1050AudioPolicyManager::device_category AudioPolicyManager::getDeviceCategory(audio_devices_t device)
1051{
1052 switch(getDeviceForVolume(device)) {
1053 case AUDIO_DEVICE_OUT_EARPIECE:
1054 return DEVICE_CATEGORY_EARPIECE;
1055 case AUDIO_DEVICE_OUT_WIRED_HEADSET:
1056 case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
1057 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
1058 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
1059 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
1060 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
1061#ifdef AUDIO_EXTN_FM_ENABLED
1062 case AUDIO_DEVICE_OUT_FM:
1063#endif
1064 return DEVICE_CATEGORY_HEADSET;
1065 case AUDIO_DEVICE_OUT_SPEAKER:
1066 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
1067 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
1068 case AUDIO_DEVICE_OUT_AUX_DIGITAL:
1069 case AUDIO_DEVICE_OUT_USB_ACCESSORY:
1070 case AUDIO_DEVICE_OUT_USB_DEVICE:
1071 case AUDIO_DEVICE_OUT_REMOTE_SUBMIX:
1072#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
1073 case AUDIO_DEVICE_OUT_PROXY:
1074#endif
1075 default:
1076 return DEVICE_CATEGORY_SPEAKER;
1077 }
1078}
1079
1080status_t AudioPolicyManager::checkAndSetVolume(int stream,
1081 int index,
1082 audio_io_handle_t output,
1083 audio_devices_t device,
1084 int delayMs,
1085 bool force)
1086{
1087 ALOGV("checkAndSetVolume: index %d output %d device %x", index, output, device);
1088 // do not change actual stream volume if the stream is muted
1089 if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
1090 ALOGVV("checkAndSetVolume() stream %d muted count %d",
1091 stream, mOutputs.valueFor(output)->mMuteCount[stream]);
1092 return NO_ERROR;
1093 }
1094
1095 // do not change in call volume if bluetooth is connected and vice versa
1096 if ((stream == AudioSystem::VOICE_CALL && mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
1097 (stream == AudioSystem::BLUETOOTH_SCO && mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO)) {
1098 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
1099 stream, mForceUse[AudioSystem::FOR_COMMUNICATION]);
1100 return INVALID_OPERATION;
1101 }
1102
1103 float volume = computeVolume(stream, index, output, device);
1104 // We actually change the volume if:
1105 // - the float value returned by computeVolume() changed
1106 // - the force flag is set
1107 if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
1108 force) {
1109 mOutputs.valueFor(output)->mCurVolume[stream] = volume;
1110 ALOGV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
1111 // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
1112 // enabled
1113 if (stream == AudioSystem::BLUETOOTH_SCO) {
1114 mpClientInterface->setStreamVolume(AudioSystem::VOICE_CALL, volume, output, delayMs);
1115#ifdef AUDIO_EXTN_FM_ENABLED
1116 } else if (stream == AudioSystem::MUSIC &&
1117 output == mPrimaryOutput) {
1118 float fmVolume = -1.0;
1119 fmVolume = computeVolume(stream, index, output, device);
1120 if (fmVolume >= 0) {
1121 AudioParameter param = AudioParameter();
1122 param.addFloat(String8("fm_volume"), fmVolume);
1123 ALOGV("checkAndSetVolume setParameters fm_volume, volume=:%f delay=:%d",fmVolume,delayMs*2);
1124 //Double delayMs to avoid sound burst while device switch.
1125 mpClientInterface->setParameters(mPrimaryOutput, param.toString(), delayMs*2);
1126 }
1127#endif
1128 }
1129 mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs);
1130 }
1131
1132 if (stream == AudioSystem::VOICE_CALL ||
1133 stream == AudioSystem::BLUETOOTH_SCO) {
1134 float voiceVolume;
Vidyakumar Athota35543452014-04-16 10:00:39 -07001135 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
1136 if (stream == AudioSystem::VOICE_CALL) {
1137 voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
1138 } else {
1139 voiceVolume = 1.0;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001140 }
1141
1142 if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) {
1143 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
1144 mLastVoiceVolume = voiceVolume;
1145 }
1146 }
1147
1148 return NO_ERROR;
1149}
1150
1151
1152float AudioPolicyManager::computeVolume(int stream,
1153 int index,
1154 audio_io_handle_t output,
1155 audio_devices_t device)
1156{
1157 float volume = 1.0;
1158 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
1159
1160 if (device == AUDIO_DEVICE_NONE) {
1161 device = outputDesc->device();
1162 }
1163
1164 // if volume is not 0 (not muted), force media volume to max on digital output
1165 if (stream == AudioSystem::MUSIC &&
1166 index != mStreams[stream].mIndexMin &&
1167 (device == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
1168 device == AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET ||
1169 device == AUDIO_DEVICE_OUT_USB_ACCESSORY ||
1170#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
1171 device == AUDIO_DEVICE_OUT_PROXY ||
1172#endif
1173 device == AUDIO_DEVICE_OUT_USB_DEVICE )) {
1174 return 1.0;
1175 }
1176#ifdef AUDIO_EXTN_INCALL_MUSIC_ENABLED
1177 if (stream == AudioSystem::INCALL_MUSIC) {
1178 return 1.0;
1179 }
1180#endif
1181 return AudioPolicyManagerBase::computeVolume(stream, index, output, device);
1182}
Naresh Tanniru36c08932014-01-27 18:40:53 +05301183
1184
1185audio_io_handle_t AudioPolicyManager::getOutput(AudioSystem::stream_type stream,
1186 uint32_t samplingRate,
1187 uint32_t format,
1188 uint32_t channelMask,
1189 AudioSystem::output_flags flags,
1190 const audio_offload_info_t *offloadInfo)
1191{
1192 audio_io_handle_t output = 0;
1193 uint32_t latency = 0;
1194 routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);
1195 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1196 IOProfile *profile = NULL;
1197
1198#ifdef VOICE_CONCURRENCY
Naresh Tanniru811f4982014-03-18 17:25:32 +05301199 char propValue[PROPERTY_VALUE_MAX];
1200 bool prop_play_enabled=false, prop_voip_enabled = false;
1201
1202 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1203 prop_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1204 }
1205
1206 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1207 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1208 }
1209
1210 if (prop_play_enabled) {
1211 //check if voice call is active / running in background
1212 if((AudioSystem::MODE_IN_CALL == mPhoneState) ||
1213 ((AudioSystem::MODE_IN_CALL == mPrevPhoneState)
1214 && (AudioSystem::MODE_IN_COMMUNICATION == mPhoneState)))
1215 {
1216 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1217 if(prop_voip_enabled) {
1218 ALOGD(" IN call mode returing no output .. for VoIP usecase flags: %x ", flags );
1219 // flags = (AudioSystem::output_flags)AUDIO_OUTPUT_FLAG_FAST;
1220 return 0;
1221 }
1222 }
1223 else {
1224 ALOGD(" IN call mode adding ULL flags .. flags: %x ", flags );
1225 flags = (AudioSystem::output_flags)AUDIO_OUTPUT_FLAG_FAST;
1226 }
1227 }
1228 } else if (prop_voip_enabled) {
1229 //check if voice call is active / running in background
1230 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1231 //return only ULL ouput
1232 if((AudioSystem::MODE_IN_CALL == mPhoneState) ||
1233 ((AudioSystem::MODE_IN_CALL == mPrevPhoneState)
1234 && (AudioSystem::MODE_IN_COMMUNICATION == mPhoneState)))
1235 {
1236 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1237 ALOGD(" IN call mode returing no output .. for VoIP usecase flags: %x ", flags );
1238 // flags = (AudioSystem::output_flags)AUDIO_OUTPUT_FLAG_FAST;
1239 return 0;
1240 }
1241 }
Naresh Tanniru36c08932014-01-27 18:40:53 +05301242 }
1243#endif
1244
1245#ifdef WFD_CONCURRENCY
1246 if ((mAvailableOutputDevices & AUDIO_DEVICE_OUT_PROXY)
1247 && (stream != AudioSystem::MUSIC)) {
1248 ALOGV(" WFD mode adding ULL flags for non music stream.. flags: %x ", flags );
1249 //For voip paths
1250 if(flags & AudioSystem::OUTPUT_FLAG_DIRECT)
1251 flags = AudioSystem::OUTPUT_FLAG_DIRECT;
1252 else //route every thing else to ULL path
1253 flags = (AudioSystem::output_flags)AUDIO_OUTPUT_FLAG_FAST;
1254 }
1255#endif
1256
1257 ALOGV(" getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x ",
1258 device, stream, samplingRate, format, channelMask, flags);
1259
1260
1261
1262#ifdef AUDIO_POLICY_TEST
1263 if (mCurOutput != 0) {
1264 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1265 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1266
1267 if (mTestOutputs[mCurOutput] == 0) {
1268 ALOGV("getOutput() opening test output");
1269 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL);
1270 outputDesc->mDevice = mTestDevice;
1271 outputDesc->mSamplingRate = mTestSamplingRate;
1272 outputDesc->mFormat = mTestFormat;
1273 outputDesc->mChannelMask = mTestChannels;
1274 outputDesc->mLatency = mTestLatencyMs;
1275 outputDesc->mFlags = (audio_output_flags_t)(mDirectOutput ? AudioSystem::OUTPUT_FLAG_DIRECT : 0);
1276 outputDesc->mRefCount[stream] = 0;
1277 mTestOutputs[mCurOutput] = mpClientInterface->openOutput(0, &outputDesc->mDevice,
1278 &outputDesc->mSamplingRate,
1279 &outputDesc->mFormat,
1280 &outputDesc->mChannelMask,
1281 &outputDesc->mLatency,
1282 outputDesc->mFlags,
1283 offloadInfo);
1284 if (mTestOutputs[mCurOutput]) {
1285 AudioParameter outputCmd = AudioParameter();
1286 outputCmd.addInt(String8("set_id"),mCurOutput);
1287 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1288 addOutput(mTestOutputs[mCurOutput], outputDesc);
1289 }
1290 }
1291 return mTestOutputs[mCurOutput];
1292 }
1293#endif //AUDIO_POLICY_TEST
1294
1295 // open a direct output if required by specified parameters
1296 //force direct flag if offload flag is set: offloading implies a direct output stream
1297 // and all common behaviors are driven by checking only the direct flag
1298 // this should normally be set appropriately in the policy configuration file
1299 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1300 flags = (AudioSystem::output_flags)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1301 }
1302
1303 if ((format == AudioSystem::PCM_16_BIT) &&(AudioSystem::popCount(channelMask) > 2)) {
1304 ALOGV("owerwrite flag(%x) for PCM16 multi-channel(CM:%x) playback", flags ,channelMask);
1305 flags = (AudioSystem::output_flags)AUDIO_OUTPUT_FLAG_DIRECT;
1306 }
1307
1308 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1309 // creating an offloaded track and tearing it down immediately after start when audioflinger
1310 // detects there is an active non offloadable effect.
1311 // FIXME: We should check the audio session here but we do not have it in this context.
1312 // This may prevent offloading in rare situations where effects are left active by apps
1313 // in the background.
1314 if ((((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
1315 !isNonOffloadableEffectEnabled()) &&
1316 flags & AUDIO_OUTPUT_FLAG_DIRECT) {
1317 profile = getProfileForDirectOutput(device,
1318 samplingRate,
1319 format,
1320 channelMask,
1321 (audio_output_flags_t)flags);
1322 }
1323
1324 if (profile != NULL) {
1325 AudioOutputDescriptor *outputDesc = NULL;
1326
1327 for (size_t i = 0; i < mOutputs.size(); i++) {
1328 AudioOutputDescriptor *desc = mOutputs.valueAt(i);
1329 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1330 outputDesc = desc;
1331 // reuse direct output if currently open and configured with same parameters
1332 if ((samplingRate == outputDesc->mSamplingRate) &&
1333 (format == outputDesc->mFormat) &&
1334 (channelMask == outputDesc->mChannelMask)) {
1335 outputDesc->mDirectOpenCount++;
1336 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1337 return mOutputs.keyAt(i);
1338 }
1339 }
1340 }
1341 // close direct output if currently open and configured with different parameters
1342 if (outputDesc != NULL) {
1343 closeOutput(outputDesc->mId);
1344 }
1345 outputDesc = new AudioOutputDescriptor(profile);
1346 outputDesc->mDevice = device;
1347 outputDesc->mSamplingRate = samplingRate;
1348 outputDesc->mFormat = (audio_format_t)format;
1349 outputDesc->mChannelMask = (audio_channel_mask_t)channelMask;
1350 outputDesc->mLatency = 0;
1351 outputDesc->mFlags =(audio_output_flags_t) (outputDesc->mFlags | flags);
1352 outputDesc->mRefCount[stream] = 0;
1353 outputDesc->mStopTime[stream] = 0;
1354 outputDesc->mDirectOpenCount = 1;
1355 output = mpClientInterface->openOutput(profile->mModule->mHandle,
1356 &outputDesc->mDevice,
1357 &outputDesc->mSamplingRate,
1358 &outputDesc->mFormat,
1359 &outputDesc->mChannelMask,
1360 &outputDesc->mLatency,
1361 outputDesc->mFlags,
1362 offloadInfo);
1363
1364 // only accept an output with the requested parameters
1365 if (output == 0 ||
1366 (samplingRate != 0 && samplingRate != outputDesc->mSamplingRate) ||
1367 (format != 0 && format != outputDesc->mFormat) ||
1368 (channelMask != 0 && channelMask != outputDesc->mChannelMask)) {
1369 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1370 "format %d %d, channelMask %04x %04x", output, samplingRate,
1371 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1372 outputDesc->mChannelMask);
1373 if (output != 0) {
1374 mpClientInterface->closeOutput(output);
1375 }
1376 delete outputDesc;
1377 return 0;
1378 }
1379 audio_io_handle_t srcOutput = getOutputForEffect();
1380 addOutput(output, outputDesc);
1381 audio_io_handle_t dstOutput = getOutputForEffect();
1382 if (dstOutput == output) {
1383 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1384 }
1385 mPreviousOutputs = mOutputs;
1386 ALOGV("getOutput() returns new direct output %d", output);
1387 return output;
1388 }
1389
1390 // ignoring channel mask due to downmix capability in mixer
1391
1392 // open a non direct output
1393
1394 // for non direct outputs, only PCM is supported
1395 if (audio_is_linear_pcm((audio_format_t)format)) {
1396 // get which output is suitable for the specified stream. The actual
1397 // routing change will happen when startOutput() will be called
1398 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1399
1400 output = selectOutput(outputs, flags);
1401 }
1402 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1403 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1404
1405 ALOGV("getOutput() returns output %d", output);
1406
1407 return output;
1408}
1409
1410
1411// This function checks for the parameters which can be offloaded.
1412// This can be enhanced depending on the capability of the DSP and policy
1413// of the system.
1414bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
1415{
1416 ALOGV(" isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
1417 " BitRate=%u, duration=%lld us, has_video=%d",
1418 offloadInfo.sample_rate, offloadInfo.channel_mask,
1419 offloadInfo.format,
1420 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
1421 offloadInfo.has_video);
1422
1423#ifdef VOICE_CONCURRENCY
Naresh Tanniru811f4982014-03-18 17:25:32 +05301424 char concpropValue[PROPERTY_VALUE_MAX];
1425 if(property_get("voice.playback.conc.disabled", concpropValue, NULL)) {
1426 bool propenabled = atoi(concpropValue) || !strncmp("true", concpropValue, 4);
1427 if (propenabled) {
1428 if(isInCall())
1429 {
1430 ALOGD("\n blocking compress offload on call mode\n");
1431 return false;
1432 }
1433 }
Naresh Tanniru36c08932014-01-27 18:40:53 +05301434 }
Naresh Tanniru811f4982014-03-18 17:25:32 +05301435
Naresh Tanniru36c08932014-01-27 18:40:53 +05301436#endif
Naresh Tanniru36c08932014-01-27 18:40:53 +05301437 // Check if stream type is music, then only allow offload as of now.
1438 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
1439 {
1440 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
1441 return false;
1442 }
1443
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001444 char propValue[PROPERTY_VALUE_MAX];
1445 bool pcmOffload = false;
1446 if (audio_is_offload_pcm(offloadInfo.format)) {
1447 if(property_get("audio.offload.pcm.enable", propValue, NULL)) {
Naresh Tanniru36c08932014-01-27 18:40:53 +05301448 bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001449 if (prop_enabled) {
1450 ALOGW("PCM offload property is enabled");
1451 pcmOffload = true;
Naresh Tanniru36c08932014-01-27 18:40:53 +05301452 }
1453 }
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001454 if (!pcmOffload) {
1455 ALOGV("PCM offload disabled by property audio.offload.pcm.enable");
1456 return false;
1457 }
1458 }
1459
1460 if (!pcmOffload) {
1461 // Check if offload has been disabled
1462 if (property_get("audio.offload.disable", propValue, "0")) {
1463 if (atoi(propValue) != 0) {
1464 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
1465 return false;
Naresh Tanniru36c08932014-01-27 18:40:53 +05301466 }
1467 }
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001468
1469 //check if it's multi-channel AAC format
1470 if (AudioSystem::popCount(offloadInfo.channel_mask) > 2
1471 && offloadInfo.format == AUDIO_FORMAT_AAC) {
1472 ALOGV("offload disabled for multi-channel AAC format");
1473 return false;
1474 }
1475
1476 if (offloadInfo.has_video)
1477 {
1478 if(property_get("av.offload.enable", propValue, NULL)) {
1479 bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1480 if (!prop_enabled) {
1481 ALOGW("offload disabled by av.offload.enable = %s ", propValue );
1482 return false;
1483 }
1484 } else {
1485 return false;
1486 }
1487
1488 if(offloadInfo.is_streaming) {
1489 if (property_get("av.streaming.offload.enable", propValue, NULL)) {
1490 bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1491 if (!prop_enabled) {
1492 ALOGW("offload disabled by av.streaming.offload.enable = %s ", propValue );
1493 return false;
1494 }
1495 } else {
1496 //Do not offload AV streamnig if the property is not defined
1497 return false;
1498 }
1499 }
1500 ALOGV("isOffloadSupported: has_video == true, property\
1501 set to enable offload");
1502 }
Naresh Tanniru36c08932014-01-27 18:40:53 +05301503 }
1504
1505 //If duration is less than minimum value defined in property, return false
1506 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
1507 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
1508 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
1509 return false;
1510 }
1511 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
1512 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
1513 //duration checks only valid for MP3/AAC formats,
1514 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001515 if (offloadInfo.format == AUDIO_FORMAT_MP3 || offloadInfo.format == AUDIO_FORMAT_AAC || pcmOffload)
Naresh Tanniru36c08932014-01-27 18:40:53 +05301516 return false;
1517 }
1518
1519 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1520 // creating an offloaded track and tearing it down immediately after start when audioflinger
1521 // detects there is an active non offloadable effect.
1522 // FIXME: We should check the audio session here but we do not have it in this context.
1523 // This may prevent offloading in rare situations where effects are left active by apps
1524 // in the background.
1525 if (isNonOffloadableEffectEnabled()) {
1526 return false;
1527 }
1528
1529 // See if there is a profile to support this.
1530 // AUDIO_DEVICE_NONE
1531 IOProfile *profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
1532 offloadInfo.sample_rate,
1533 offloadInfo.format,
1534 offloadInfo.channel_mask,
1535 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1536 ALOGV("isOffloadSupported() profile %sfound", profile != NULL ? "" : "NOT ");
1537 return (profile != NULL);
1538}
1539
1540void AudioPolicyManager::setPhoneState(int state)
1541
1542{
Naresh Tanniru811f4982014-03-18 17:25:32 +05301543 ALOGD("setPhoneState() state %d", state);
Naresh Tanniru36c08932014-01-27 18:40:53 +05301544 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
1545 if (state < 0 || state >= AudioSystem::NUM_MODES) {
1546 ALOGW("setPhoneState() invalid state %d", state);
1547 return;
1548 }
1549
1550 if (state == mPhoneState ) {
1551 ALOGW("setPhoneState() setting same state %d", state);
1552 return;
1553 }
1554
1555 // if leaving call state, handle special case of active streams
1556 // pertaining to sonification strategy see handleIncallSonification()
1557 if (isInCall()) {
1558 ALOGV("setPhoneState() in call state management: new state is %d", state);
1559 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
1560 handleIncallSonification(stream, false, true);
1561 }
1562 }
1563
1564 // store previous phone state for management of sonification strategy below
1565 int oldState = mPhoneState;
1566 mPhoneState = state;
1567 bool force = false;
1568
1569 // are we entering or starting a call
1570 if (!isStateInCall(oldState) && isStateInCall(state)) {
1571 ALOGV(" Entering call in setPhoneState()");
1572 // force routing command to audio hardware when starting a call
1573 // even if no device change is needed
1574 force = true;
1575 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
1576 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
1577 sVolumeProfiles[AUDIO_STREAM_VOICE_CALL][j];
1578 }
1579 } else if (isStateInCall(oldState) && !isStateInCall(state)) {
1580 ALOGV(" Exiting call in setPhoneState()");
1581 // force routing command to audio hardware when exiting a call
1582 // even if no device change is needed
1583 force = true;
1584 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
1585 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
1586 sVolumeProfiles[AUDIO_STREAM_DTMF][j];
1587 }
1588 } else if (isStateInCall(state) && (state != oldState)) {
1589 ALOGV(" Switching between telephony and VoIP in setPhoneState()");
1590 // force routing command to audio hardware when switching between telephony and VoIP
1591 // even if no device change is needed
1592 force = true;
1593 }
1594
1595 // check for device and output changes triggered by new phone state
1596 newDevice = getNewDevice(mPrimaryOutput, false /*fromCache*/);
1597 checkA2dpSuspend();
1598 checkOutputForAllStrategies();
1599 updateDevicesAndOutputs();
1600
1601 AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
1602
1603 // force routing command to audio hardware when ending call
1604 // even if no device change is needed
1605 if (isStateInCall(oldState) && newDevice == AUDIO_DEVICE_NONE) {
1606 newDevice = hwOutputDesc->device();
1607 }
Naresh Tanniru811f4982014-03-18 17:25:32 +05301608#ifdef VOICE_CONCURRENCY
1609 char propValue[PROPERTY_VALUE_MAX];
1610 bool prop_playback_enabled = false, prop_rec_enabled=false, prop_voip_enabled = false;
1611
1612 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1613 prop_playback_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1614 }
1615
1616 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
1617 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1618 }
1619
1620 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1621 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1622 }
1623
1624 if((AudioSystem::MODE_IN_CALL != oldState) && (AudioSystem::MODE_IN_CALL == state)) {
1625 ALOGD("Entering to call mode oldState :: %d state::%d ",oldState, state);
1626
1627 if(prop_playback_enabled) {
1628 //Call invalidate to reset all opened non ULL audio tracks
1629 // Move tracks associated to this strategy from previous output to new output
1630 for (int i = AudioSystem::SYSTEM; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
1631 ALOGV(" Invalidate on call mode for stream :: %d ", i);
1632 //FIXME see fixme on name change
1633 mpClientInterface->setStreamOutput((AudioSystem::stream_type)i,
1634 0 /* ignored */);
1635 }
1636 }
1637
1638 if(prop_rec_enabled) {
1639 //Close all active inputs
1640 audio_io_handle_t activeInput = getActiveInput();
1641 if (activeInput != 0) {
1642 AudioInputDescriptor *activeDesc = mInputs.valueFor(activeInput);
1643 switch(activeDesc->mInputSource) {
1644 case AUDIO_SOURCE_VOICE_UPLINK:
1645 case AUDIO_SOURCE_VOICE_DOWNLINK:
1646 case AUDIO_SOURCE_VOICE_CALL:
1647 ALOGD("FOUND active input during call active: %d",activeDesc->mInputSource);
1648 break;
1649
1650 case AUDIO_SOURCE_VOICE_COMMUNICATION:
1651 if(prop_voip_enabled) {
1652 ALOGD("CLOSING VoIP input source on call setup :%d ",activeDesc->mInputSource);
1653 stopInput(activeInput);
1654 releaseInput(activeInput);
1655 }
1656 break;
1657
1658 default:
1659 ALOGD("CLOSING input on call setup for inputSource: %d",activeDesc->mInputSource);
1660 stopInput(activeInput);
1661 releaseInput(activeInput);
1662 break;
1663 }
1664 }
1665 } else if(prop_voip_enabled) {
1666 audio_io_handle_t activeInput = getActiveInput();
1667 if (activeInput != 0) {
1668 AudioInputDescriptor *activeDesc = mInputs.valueFor(activeInput);
1669 if(AUDIO_SOURCE_VOICE_COMMUNICATION == activeDesc->mInputSource) {
1670 ALOGD("CLOSING VoIP on call setup : %d",activeDesc->mInputSource);
1671 stopInput(activeInput);
1672 releaseInput(activeInput);
1673 }
1674 }
1675 }
1676
1677 //suspend PCM (deep-buffer) output & close compress & direct tracks
1678 for (size_t i = 0; i < mOutputs.size(); i++) {
1679 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
1680 if (((!outputDesc->isDuplicated() &&outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY))
1681 && prop_playback_enabled) {
1682 ALOGD(" calling suspendOutput on call mdoe for primary output");
1683 mpClientInterface->suspendOutput(mOutputs.keyAt(i));
1684 } //Close compress all sessions
1685 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
1686 && prop_playback_enabled) {
1687 ALOGD(" calling closeOutput on call mdoe for COMPRESS output");
1688 closeOutput(mOutputs.keyAt(i));
1689 }
1690 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_VOIP_RX)
1691 && prop_voip_enabled) {
1692 ALOGD(" calling closeOutput on call mdoe for DIRECT output");
1693 closeOutput(mOutputs.keyAt(i));
1694 }
1695 }
1696 }
1697
1698 if((AudioSystem::MODE_IN_CALL == oldState) && (AudioSystem::MODE_IN_CALL != state)
1699 && prop_playback_enabled) {
1700 ALOGD("EXITING from call mode oldState :: %d state::%d \n",oldState, state);
1701 //restore PCM (deep-buffer) output after call termination
1702 for (size_t i = 0; i < mOutputs.size(); i++) {
1703 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
1704 if (!outputDesc->isDuplicated() &&outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1705 ALOGD("calling restoreOutput after call mode for primary output");
1706 mpClientInterface->restoreOutput(mOutputs.keyAt(i));
1707 }
1708 }
1709 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
1710 for (int i = AudioSystem::SYSTEM; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
1711 ALOGD("Invalidate on call mode for stream :: %d ", i);
1712 //FIXME see fixme on name change
1713 mpClientInterface->setStreamOutput((AudioSystem::stream_type)i,
1714 0 /* ignored */);
1715 }
1716 }
1717 mPrevPhoneState = oldState;
1718#endif
Naresh Tanniru36c08932014-01-27 18:40:53 +05301719
1720 int delayMs = 0;
1721 if (isStateInCall(state)) {
1722 nsecs_t sysTime = systemTime();
1723 for (size_t i = 0; i < mOutputs.size(); i++) {
1724 AudioOutputDescriptor *desc = mOutputs.valueAt(i);
1725 // mute media and sonification strategies and delay device switch by the largest
1726 // latency of any output where either strategy is active.
1727 // This avoid sending the ring tone or music tail into the earpiece or headset.
1728 if ((desc->isStrategyActive(STRATEGY_MEDIA,
1729 SONIFICATION_HEADSET_MUSIC_DELAY,
1730 sysTime) ||
1731 desc->isStrategyActive(STRATEGY_SONIFICATION,
1732 SONIFICATION_HEADSET_MUSIC_DELAY,
1733 sysTime)) &&
1734 (delayMs < (int)desc->mLatency*2)) {
1735 delayMs = desc->mLatency*2;
1736 }
1737 setStrategyMute(STRATEGY_MEDIA, true, mOutputs.keyAt(i));
1738 setStrategyMute(STRATEGY_MEDIA, false, mOutputs.keyAt(i), MUTE_TIME_MS,
1739 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
1740 setStrategyMute(STRATEGY_SONIFICATION, true, mOutputs.keyAt(i));
1741 setStrategyMute(STRATEGY_SONIFICATION, false, mOutputs.keyAt(i), MUTE_TIME_MS,
1742 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
1743 }
1744 }
1745
1746 // change routing is necessary
1747 setOutputDevice(mPrimaryOutput, newDevice, force, delayMs);
1748
1749 // if entering in call state, handle special case of active streams
1750 // pertaining to sonification strategy see handleIncallSonification()
1751 if (isStateInCall(state)) {
1752 ALOGV("setPhoneState() in call state management: new state is %d", state);
1753 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
1754 handleIncallSonification(stream, true, true);
1755 }
1756 }
1757
1758 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
1759 if (state == AudioSystem::MODE_RINGTONE &&
1760 isStreamActive(AudioSystem::MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
1761 mLimitRingtoneVolume = true;
1762 } else {
1763 mLimitRingtoneVolume = false;
1764 }
Naresh Tanniru811f4982014-03-18 17:25:32 +05301765 ALOGD(" End of setPhoneState ... mPhoneState: %d ",mPhoneState);
Naresh Tanniru36c08932014-01-27 18:40:53 +05301766}
1767
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001768extern "C" AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001769{
1770 return new AudioPolicyManager(clientInterface);
1771}
1772
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001773extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001774{
1775 delete interface;
1776}
1777
1778}; // namespace android