blob: 4501239db00ea5b31c47e5b98a2d8a9e61690e14 [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
598
599 IOProfile *profile = getInputProfile(device,
600 samplingRate,
601 format,
602 channelMask);
603 if (profile == NULL) {
604 ALOGW("getInput() could not find profile for device %04x, samplingRate %d, format %d,"
605 "channelMask %04x",
606 device, samplingRate, format, channelMask);
607 return 0;
608 }
609
610 if (profile->mModule->mHandle == 0) {
611 ALOGE("getInput(): HW module %s not opened", profile->mModule->mName);
612 return 0;
613 }
614
615 AudioInputDescriptor *inputDesc = new AudioInputDescriptor(profile);
616
617 inputDesc->mInputSource = inputSource;
618 inputDesc->mDevice = device;
619 inputDesc->mSamplingRate = samplingRate;
620 inputDesc->mFormat = (audio_format_t)format;
621 inputDesc->mChannelMask = (audio_channel_mask_t)channelMask;
622 inputDesc->mRefCount = 0;
623 input = mpClientInterface->openInput(profile->mModule->mHandle,
624 &inputDesc->mDevice,
625 &inputDesc->mSamplingRate,
626 &inputDesc->mFormat,
627 &inputDesc->mChannelMask);
628
629 // only accept input with the exact requested set of parameters
630 if (input == 0 ||
631 (samplingRate != inputDesc->mSamplingRate) ||
632 (format != inputDesc->mFormat) ||
633 (channelMask != inputDesc->mChannelMask)) {
634 ALOGV("getInput() failed opening input: samplingRate %d, format %d, channelMask %d",
635 samplingRate, format, channelMask);
636 if (input != 0) {
637 mpClientInterface->closeInput(input);
638 }
639 delete inputDesc;
640 return 0;
641 }
642 mInputs.add(input, inputDesc);
643 return input;
644}
645
646AudioPolicyManager::routing_strategy AudioPolicyManager::getStrategy(AudioSystem::stream_type stream)
647{
Naresh Tanniru36c08932014-01-27 18:40:53 +0530648 // stream to strategy mapping
649 switch (stream) {
650 case AudioSystem::VOICE_CALL:
651 case AudioSystem::BLUETOOTH_SCO:
652 return STRATEGY_PHONE;
653 case AudioSystem::RING:
654 case AudioSystem::ALARM:
655 return STRATEGY_SONIFICATION;
656 case AudioSystem::NOTIFICATION:
657 return STRATEGY_SONIFICATION_RESPECTFUL;
658 case AudioSystem::DTMF:
659 return STRATEGY_DTMF;
660 default:
661 ALOGE("unknown stream type");
662 case AudioSystem::SYSTEM:
663 // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
664 // while key clicks are played produces a poor result
665 case AudioSystem::TTS:
666 case AudioSystem::MUSIC:
667#ifdef AUDIO_EXTN_INCALL_MUSIC_ENABLED
668 case AudioSystem::INCALL_MUSIC:
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700669#endif
Naresh Tanniru36c08932014-01-27 18:40:53 +0530670#ifdef QCOM_INCALL_MUSIC_ENABLED
671 case AudioSystem::INCALL_MUSIC:
672#endif
673 return STRATEGY_MEDIA;
674 case AudioSystem::ENFORCED_AUDIBLE:
675 return STRATEGY_ENFORCED_AUDIBLE;
676 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700677
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700678}
679
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700680audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
681 bool fromCache)
682{
683 uint32_t device = AUDIO_DEVICE_NONE;
684
685 if (fromCache) {
686 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
687 strategy, mDeviceForStrategy[strategy]);
688 return mDeviceForStrategy[strategy];
689 }
690
691 switch (strategy) {
692
693 case STRATEGY_SONIFICATION_RESPECTFUL:
694 if (isInCall()) {
695 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
696 } else if (isStreamActiveRemotely(AudioSystem::MUSIC,
697 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
698 // while media is playing on a remote device, use the the sonification behavior.
699 // Note that we test this usecase before testing if media is playing because
700 // the isStreamActive() method only informs about the activity of a stream, not
701 // if it's for local playback. Note also that we use the same delay between both tests
702 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
703 } else if (isStreamActive(AudioSystem::MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
704 // while media is playing (or has recently played), use the same device
705 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
706 } else {
707 // when media is not playing anymore, fall back on the sonification behavior
708 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
709 }
710
711 break;
712
713 case STRATEGY_DTMF:
714 if (!isInCall()) {
715 // when off call, DTMF strategy follows the same rules as MEDIA strategy
716 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
717 break;
718 }
719 // when in call, DTMF and PHONE strategies follow the same rules
720 // FALL THROUGH
721
722 case STRATEGY_PHONE:
723 // for phone strategy, we first consider the forced use and then the available devices by order
724 // of priority
725 switch (mForceUse[AudioSystem::FOR_COMMUNICATION]) {
726 case AudioSystem::FORCE_BT_SCO:
727 if (!isInCall() || strategy != STRATEGY_DTMF) {
728 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
729 if (device) break;
730 }
731 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
732 if (device) break;
733 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
734 if (device) break;
735 // if SCO device is requested but no SCO device is available, fall back to default case
736 // FALL THROUGH
737
738 default: // FORCE_NONE
739 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
740 if (mHasA2dp && !isInCall() &&
741 (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
742 (getA2dpOutput() != 0) && !mA2dpSuspended) {
743 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
744 if (device) break;
745 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
746 if (device) break;
747 }
748 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
749 if (device) break;
750 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
751 if (device) break;
752 if (mPhoneState != AudioSystem::MODE_IN_CALL) {
753 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
754 if (device) break;
755 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
756 if (device) break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700757 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
758 if (device) break;
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700759 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
760 if (device) break;
761 }
762
763 // Allow voice call on USB ANLG DOCK headset
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700764 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
765 if (device) break;
766
767 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_EARPIECE;
768 if (device) break;
769 device = mDefaultOutputDevice;
770 if (device == AUDIO_DEVICE_NONE) {
771 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE");
772 }
773 break;
774
775 case AudioSystem::FORCE_SPEAKER:
776 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
777 // A2DP speaker when forcing to speaker output
778 if (mHasA2dp && !isInCall() &&
779 (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
780 (getA2dpOutput() != 0) && !mA2dpSuspended) {
781 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
782 if (device) break;
783 }
784 if (mPhoneState != AudioSystem::MODE_IN_CALL) {
785 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
786 if (device) break;
787 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
788 if (device) break;
789 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
790 if (device) break;
791 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
792 if (device) break;
Helen Zengbbce4952013-12-16 20:26:46 -0800793 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
794 if (device) break;
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700795 }
796 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
797 if (device) break;
798 device = mDefaultOutputDevice;
799 if (device == AUDIO_DEVICE_NONE) {
800 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER");
801 }
802 break;
803 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700804 // FIXME: Why do need to replace with speaker? If voice call is active
805 // We should use device from STRATEGY_PHONE
806#ifdef AUDIO_EXTN_FM_ENABLED
807 if (mAvailableOutputDevices & AUDIO_DEVICE_OUT_FM) {
808 if (mForceUse[AudioSystem::FOR_MEDIA] == AudioSystem::FORCE_SPEAKER) {
809 device = AUDIO_DEVICE_OUT_SPEAKER;
810 }
811 }
812#endif
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700813 break;
814
815 case STRATEGY_SONIFICATION:
816
817 // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
818 // handleIncallSonification().
819 if (isInCall()) {
820 device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
821 break;
822 }
823 // FALL THROUGH
824
825 case STRATEGY_ENFORCED_AUDIBLE:
826 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
827 // except:
828 // - when in call where it doesn't default to STRATEGY_PHONE behavior
829 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
830
831 if ((strategy == STRATEGY_SONIFICATION) ||
832 (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_SYSTEM_ENFORCED)) {
833 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
834 if (device == AUDIO_DEVICE_NONE) {
835 ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION");
836 }
837 }
838 // The second device used for sonification is the same as the device used by media strategy
839 // FALL THROUGH
840
841 case STRATEGY_MEDIA: {
842 uint32_t device2 = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700843
Mingming Yin4a72d652014-01-03 18:54:18 -0800844 if (isInCall() && (device == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700845 // when in call, get the device for Phone strategy
846 device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
847 break;
848 }
849#ifdef AUDIO_EXTN_FM_ENABLED
850 if (mForceUse[AudioSystem::FOR_MEDIA] == AudioSystem::FORCE_SPEAKER) {
851 device = AUDIO_DEVICE_OUT_SPEAKER;
852 break;
853 }
854#endif
855
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700856 if (strategy != STRATEGY_SONIFICATION) {
857 // no sonification on remote submix (e.g. WFD)
858 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
859 }
860 if ((device2 == AUDIO_DEVICE_NONE) &&
861 mHasA2dp && (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
862 (getA2dpOutput() != 0) && !mA2dpSuspended) {
863 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
864 if (device2 == AUDIO_DEVICE_NONE) {
865 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
866 }
867 if (device2 == AUDIO_DEVICE_NONE) {
868 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
869 }
870 }
871 if (device2 == AUDIO_DEVICE_NONE) {
872 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
873 }
874 if (device2 == AUDIO_DEVICE_NONE) {
875 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
876 }
877 if (device2 == AUDIO_DEVICE_NONE) {
878 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
879 }
880 if (device2 == AUDIO_DEVICE_NONE) {
881 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
882 }
883 if (device2 == AUDIO_DEVICE_NONE) {
884 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
885 }
Apoorv Raghuvanshi7566f8b2014-01-17 13:10:09 -0800886 if ((strategy != STRATEGY_SONIFICATION) && (device == AUDIO_DEVICE_NONE)
887 && (device2 == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700888 // no sonification on aux digital (e.g. HDMI)
889 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
890 }
891 if ((device2 == AUDIO_DEVICE_NONE) &&
Krishnankutty Kolathappilly4b7fdaa2013-12-16 00:40:11 -0800892 (mForceUse[AudioSystem::FOR_DOCK] == AudioSystem::FORCE_ANALOG_DOCK)
893 && (strategy != STRATEGY_SONIFICATION)) {
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700894 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
895 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700896#ifdef AUDIO_EXTN_FM_ENABLED
Apoorv Raghuvanshi7566f8b2014-01-17 13:10:09 -0800897 if ((strategy != STRATEGY_SONIFICATION) && (device == AUDIO_DEVICE_NONE)
898 && (device2 == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700899 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_FM_TX;
900 }
901#endif
902#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Apoorv Raghuvanshi7566f8b2014-01-17 13:10:09 -0800903 if ((strategy != STRATEGY_SONIFICATION) && (device == AUDIO_DEVICE_NONE)
904 && (device2 == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700905 // no sonification on WFD sink
906 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_PROXY;
907 }
908#endif
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700909 if (device2 == AUDIO_DEVICE_NONE) {
910 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
911 }
912
913 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
914 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
915 device |= device2;
916 if (device) break;
917 device = mDefaultOutputDevice;
918 if (device == AUDIO_DEVICE_NONE) {
919 ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA");
920 }
921 } break;
922
923 default:
924 ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
925 break;
926 }
927
928 ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
929 return device;
930}
931
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700932audio_devices_t AudioPolicyManager::getDeviceForInputSource(int inputSource)
933{
934 uint32_t device = AUDIO_DEVICE_NONE;
935
936 switch (inputSource) {
937 case AUDIO_SOURCE_VOICE_UPLINK:
938 if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
939 device = AUDIO_DEVICE_IN_VOICE_CALL;
940 break;
941 }
942 // FALL THROUGH
943
944 case AUDIO_SOURCE_DEFAULT:
945 case AUDIO_SOURCE_MIC:
946 case AUDIO_SOURCE_VOICE_RECOGNITION:
947 case AUDIO_SOURCE_HOTWORD:
948 case AUDIO_SOURCE_VOICE_COMMUNICATION:
949 if (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO &&
950 mAvailableInputDevices & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
951 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
952 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_WIRED_HEADSET) {
953 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
954 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET) {
955 device = AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET;
956 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
957 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
958 }
959 break;
960 case AUDIO_SOURCE_CAMCORDER:
961 if (mAvailableInputDevices & AUDIO_DEVICE_IN_BACK_MIC) {
962 device = AUDIO_DEVICE_IN_BACK_MIC;
963 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
964 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
965 }
966 break;
967 case AUDIO_SOURCE_VOICE_DOWNLINK:
968 case AUDIO_SOURCE_VOICE_CALL:
969 if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
970 device = AUDIO_DEVICE_IN_VOICE_CALL;
971 }
972 break;
973 case AUDIO_SOURCE_REMOTE_SUBMIX:
974 if (mAvailableInputDevices & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
975 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
976 }
977 break;
978#ifdef AUDIO_EXTN_FM_ENABLED
979 case AUDIO_SOURCE_FM_RX:
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700980 device = AUDIO_DEVICE_IN_FM_RX;
981 break;
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700982 case AUDIO_SOURCE_FM_RX_A2DP:
983 device = AUDIO_DEVICE_IN_FM_RX_A2DP;
984 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700985#endif
986 default:
987 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
988 break;
989 }
990 ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
991 return device;
992}
993
994AudioPolicyManager::device_category AudioPolicyManager::getDeviceCategory(audio_devices_t device)
995{
996 switch(getDeviceForVolume(device)) {
997 case AUDIO_DEVICE_OUT_EARPIECE:
998 return DEVICE_CATEGORY_EARPIECE;
999 case AUDIO_DEVICE_OUT_WIRED_HEADSET:
1000 case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
1001 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
1002 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
1003 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
1004 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
1005#ifdef AUDIO_EXTN_FM_ENABLED
1006 case AUDIO_DEVICE_OUT_FM:
1007#endif
1008 return DEVICE_CATEGORY_HEADSET;
1009 case AUDIO_DEVICE_OUT_SPEAKER:
1010 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
1011 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
1012 case AUDIO_DEVICE_OUT_AUX_DIGITAL:
1013 case AUDIO_DEVICE_OUT_USB_ACCESSORY:
1014 case AUDIO_DEVICE_OUT_USB_DEVICE:
1015 case AUDIO_DEVICE_OUT_REMOTE_SUBMIX:
1016#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
1017 case AUDIO_DEVICE_OUT_PROXY:
1018#endif
1019 default:
1020 return DEVICE_CATEGORY_SPEAKER;
1021 }
1022}
1023
1024status_t AudioPolicyManager::checkAndSetVolume(int stream,
1025 int index,
1026 audio_io_handle_t output,
1027 audio_devices_t device,
1028 int delayMs,
1029 bool force)
1030{
1031 ALOGV("checkAndSetVolume: index %d output %d device %x", index, output, device);
1032 // do not change actual stream volume if the stream is muted
1033 if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
1034 ALOGVV("checkAndSetVolume() stream %d muted count %d",
1035 stream, mOutputs.valueFor(output)->mMuteCount[stream]);
1036 return NO_ERROR;
1037 }
1038
1039 // do not change in call volume if bluetooth is connected and vice versa
1040 if ((stream == AudioSystem::VOICE_CALL && mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
1041 (stream == AudioSystem::BLUETOOTH_SCO && mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO)) {
1042 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
1043 stream, mForceUse[AudioSystem::FOR_COMMUNICATION]);
1044 return INVALID_OPERATION;
1045 }
1046
1047 float volume = computeVolume(stream, index, output, device);
1048 // We actually change the volume if:
1049 // - the float value returned by computeVolume() changed
1050 // - the force flag is set
1051 if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
1052 force) {
1053 mOutputs.valueFor(output)->mCurVolume[stream] = volume;
1054 ALOGV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
1055 // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
1056 // enabled
1057 if (stream == AudioSystem::BLUETOOTH_SCO) {
1058 mpClientInterface->setStreamVolume(AudioSystem::VOICE_CALL, volume, output, delayMs);
1059#ifdef AUDIO_EXTN_FM_ENABLED
1060 } else if (stream == AudioSystem::MUSIC &&
1061 output == mPrimaryOutput) {
1062 float fmVolume = -1.0;
1063 fmVolume = computeVolume(stream, index, output, device);
1064 if (fmVolume >= 0) {
1065 AudioParameter param = AudioParameter();
1066 param.addFloat(String8("fm_volume"), fmVolume);
1067 ALOGV("checkAndSetVolume setParameters fm_volume, volume=:%f delay=:%d",fmVolume,delayMs*2);
1068 //Double delayMs to avoid sound burst while device switch.
1069 mpClientInterface->setParameters(mPrimaryOutput, param.toString(), delayMs*2);
1070 }
1071#endif
1072 }
1073 mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs);
1074 }
1075
1076 if (stream == AudioSystem::VOICE_CALL ||
1077 stream == AudioSystem::BLUETOOTH_SCO) {
1078 float voiceVolume;
Vidyakumar Athota35543452014-04-16 10:00:39 -07001079 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
1080 if (stream == AudioSystem::VOICE_CALL) {
1081 voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
1082 } else {
1083 voiceVolume = 1.0;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001084 }
1085
1086 if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) {
1087 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
1088 mLastVoiceVolume = voiceVolume;
1089 }
1090 }
1091
1092 return NO_ERROR;
1093}
1094
1095
1096float AudioPolicyManager::computeVolume(int stream,
1097 int index,
1098 audio_io_handle_t output,
1099 audio_devices_t device)
1100{
1101 float volume = 1.0;
1102 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
1103
1104 if (device == AUDIO_DEVICE_NONE) {
1105 device = outputDesc->device();
1106 }
1107
1108 // if volume is not 0 (not muted), force media volume to max on digital output
1109 if (stream == AudioSystem::MUSIC &&
1110 index != mStreams[stream].mIndexMin &&
1111 (device == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
1112 device == AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET ||
1113 device == AUDIO_DEVICE_OUT_USB_ACCESSORY ||
1114#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
1115 device == AUDIO_DEVICE_OUT_PROXY ||
1116#endif
1117 device == AUDIO_DEVICE_OUT_USB_DEVICE )) {
1118 return 1.0;
1119 }
1120#ifdef AUDIO_EXTN_INCALL_MUSIC_ENABLED
1121 if (stream == AudioSystem::INCALL_MUSIC) {
1122 return 1.0;
1123 }
1124#endif
1125 return AudioPolicyManagerBase::computeVolume(stream, index, output, device);
1126}
Naresh Tanniru36c08932014-01-27 18:40:53 +05301127
1128
1129audio_io_handle_t AudioPolicyManager::getOutput(AudioSystem::stream_type stream,
1130 uint32_t samplingRate,
1131 uint32_t format,
1132 uint32_t channelMask,
1133 AudioSystem::output_flags flags,
1134 const audio_offload_info_t *offloadInfo)
1135{
1136 audio_io_handle_t output = 0;
1137 uint32_t latency = 0;
1138 routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);
1139 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1140 IOProfile *profile = NULL;
1141
1142#ifdef VOICE_CONCURRENCY
1143 if (isInCall()) {
1144 ALOGV(" IN call mode adding ULL flags .. flags: %x ", flags );
1145 //For voip paths
1146 if(flags & AudioSystem::OUTPUT_FLAG_DIRECT)
1147 flags = AudioSystem::OUTPUT_FLAG_DIRECT;
1148 else //route every thing else to ULL path
1149 flags = (AudioSystem::output_flags)AUDIO_OUTPUT_FLAG_FAST;
1150 }
1151#endif
1152
1153#ifdef WFD_CONCURRENCY
1154 if ((mAvailableOutputDevices & AUDIO_DEVICE_OUT_PROXY)
1155 && (stream != AudioSystem::MUSIC)) {
1156 ALOGV(" WFD mode adding ULL flags for non music stream.. flags: %x ", flags );
1157 //For voip paths
1158 if(flags & AudioSystem::OUTPUT_FLAG_DIRECT)
1159 flags = AudioSystem::OUTPUT_FLAG_DIRECT;
1160 else //route every thing else to ULL path
1161 flags = (AudioSystem::output_flags)AUDIO_OUTPUT_FLAG_FAST;
1162 }
1163#endif
1164
1165 ALOGV(" getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x ",
1166 device, stream, samplingRate, format, channelMask, flags);
1167
1168
1169
1170#ifdef AUDIO_POLICY_TEST
1171 if (mCurOutput != 0) {
1172 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1173 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1174
1175 if (mTestOutputs[mCurOutput] == 0) {
1176 ALOGV("getOutput() opening test output");
1177 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL);
1178 outputDesc->mDevice = mTestDevice;
1179 outputDesc->mSamplingRate = mTestSamplingRate;
1180 outputDesc->mFormat = mTestFormat;
1181 outputDesc->mChannelMask = mTestChannels;
1182 outputDesc->mLatency = mTestLatencyMs;
1183 outputDesc->mFlags = (audio_output_flags_t)(mDirectOutput ? AudioSystem::OUTPUT_FLAG_DIRECT : 0);
1184 outputDesc->mRefCount[stream] = 0;
1185 mTestOutputs[mCurOutput] = mpClientInterface->openOutput(0, &outputDesc->mDevice,
1186 &outputDesc->mSamplingRate,
1187 &outputDesc->mFormat,
1188 &outputDesc->mChannelMask,
1189 &outputDesc->mLatency,
1190 outputDesc->mFlags,
1191 offloadInfo);
1192 if (mTestOutputs[mCurOutput]) {
1193 AudioParameter outputCmd = AudioParameter();
1194 outputCmd.addInt(String8("set_id"),mCurOutput);
1195 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1196 addOutput(mTestOutputs[mCurOutput], outputDesc);
1197 }
1198 }
1199 return mTestOutputs[mCurOutput];
1200 }
1201#endif //AUDIO_POLICY_TEST
1202
1203 // open a direct output if required by specified parameters
1204 //force direct flag if offload flag is set: offloading implies a direct output stream
1205 // and all common behaviors are driven by checking only the direct flag
1206 // this should normally be set appropriately in the policy configuration file
1207 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1208 flags = (AudioSystem::output_flags)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1209 }
1210
1211 if ((format == AudioSystem::PCM_16_BIT) &&(AudioSystem::popCount(channelMask) > 2)) {
1212 ALOGV("owerwrite flag(%x) for PCM16 multi-channel(CM:%x) playback", flags ,channelMask);
1213 flags = (AudioSystem::output_flags)AUDIO_OUTPUT_FLAG_DIRECT;
1214 }
1215
1216 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1217 // creating an offloaded track and tearing it down immediately after start when audioflinger
1218 // detects there is an active non offloadable effect.
1219 // FIXME: We should check the audio session here but we do not have it in this context.
1220 // This may prevent offloading in rare situations where effects are left active by apps
1221 // in the background.
1222 if ((((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
1223 !isNonOffloadableEffectEnabled()) &&
1224 flags & AUDIO_OUTPUT_FLAG_DIRECT) {
1225 profile = getProfileForDirectOutput(device,
1226 samplingRate,
1227 format,
1228 channelMask,
1229 (audio_output_flags_t)flags);
1230 }
1231
1232 if (profile != NULL) {
1233 AudioOutputDescriptor *outputDesc = NULL;
1234
1235 for (size_t i = 0; i < mOutputs.size(); i++) {
1236 AudioOutputDescriptor *desc = mOutputs.valueAt(i);
1237 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1238 outputDesc = desc;
1239 // reuse direct output if currently open and configured with same parameters
1240 if ((samplingRate == outputDesc->mSamplingRate) &&
1241 (format == outputDesc->mFormat) &&
1242 (channelMask == outputDesc->mChannelMask)) {
1243 outputDesc->mDirectOpenCount++;
1244 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1245 return mOutputs.keyAt(i);
1246 }
1247 }
1248 }
1249 // close direct output if currently open and configured with different parameters
1250 if (outputDesc != NULL) {
1251 closeOutput(outputDesc->mId);
1252 }
1253 outputDesc = new AudioOutputDescriptor(profile);
1254 outputDesc->mDevice = device;
1255 outputDesc->mSamplingRate = samplingRate;
1256 outputDesc->mFormat = (audio_format_t)format;
1257 outputDesc->mChannelMask = (audio_channel_mask_t)channelMask;
1258 outputDesc->mLatency = 0;
1259 outputDesc->mFlags =(audio_output_flags_t) (outputDesc->mFlags | flags);
1260 outputDesc->mRefCount[stream] = 0;
1261 outputDesc->mStopTime[stream] = 0;
1262 outputDesc->mDirectOpenCount = 1;
1263 output = mpClientInterface->openOutput(profile->mModule->mHandle,
1264 &outputDesc->mDevice,
1265 &outputDesc->mSamplingRate,
1266 &outputDesc->mFormat,
1267 &outputDesc->mChannelMask,
1268 &outputDesc->mLatency,
1269 outputDesc->mFlags,
1270 offloadInfo);
1271
1272 // only accept an output with the requested parameters
1273 if (output == 0 ||
1274 (samplingRate != 0 && samplingRate != outputDesc->mSamplingRate) ||
1275 (format != 0 && format != outputDesc->mFormat) ||
1276 (channelMask != 0 && channelMask != outputDesc->mChannelMask)) {
1277 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1278 "format %d %d, channelMask %04x %04x", output, samplingRate,
1279 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1280 outputDesc->mChannelMask);
1281 if (output != 0) {
1282 mpClientInterface->closeOutput(output);
1283 }
1284 delete outputDesc;
1285 return 0;
1286 }
1287 audio_io_handle_t srcOutput = getOutputForEffect();
1288 addOutput(output, outputDesc);
1289 audio_io_handle_t dstOutput = getOutputForEffect();
1290 if (dstOutput == output) {
1291 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1292 }
1293 mPreviousOutputs = mOutputs;
1294 ALOGV("getOutput() returns new direct output %d", output);
1295 return output;
1296 }
1297
1298 // ignoring channel mask due to downmix capability in mixer
1299
1300 // open a non direct output
1301
1302 // for non direct outputs, only PCM is supported
1303 if (audio_is_linear_pcm((audio_format_t)format)) {
1304 // get which output is suitable for the specified stream. The actual
1305 // routing change will happen when startOutput() will be called
1306 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1307
1308 output = selectOutput(outputs, flags);
1309 }
1310 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1311 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1312
1313 ALOGV("getOutput() returns output %d", output);
1314
1315 return output;
1316}
1317
1318
1319// This function checks for the parameters which can be offloaded.
1320// This can be enhanced depending on the capability of the DSP and policy
1321// of the system.
1322bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
1323{
1324 ALOGV(" isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
1325 " BitRate=%u, duration=%lld us, has_video=%d",
1326 offloadInfo.sample_rate, offloadInfo.channel_mask,
1327 offloadInfo.format,
1328 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
1329 offloadInfo.has_video);
1330
1331#ifdef VOICE_CONCURRENCY
1332 if(isInCall())
1333 {
1334 ALOGD("\n blocking compress offload on call mode\n");
1335 return false;
1336 }
1337#endif
Naresh Tanniru36c08932014-01-27 18:40:53 +05301338 // Check if stream type is music, then only allow offload as of now.
1339 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
1340 {
1341 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
1342 return false;
1343 }
1344
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001345 char propValue[PROPERTY_VALUE_MAX];
1346 bool pcmOffload = false;
1347 if (audio_is_offload_pcm(offloadInfo.format)) {
1348 if(property_get("audio.offload.pcm.enable", propValue, NULL)) {
Naresh Tanniru36c08932014-01-27 18:40:53 +05301349 bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001350 if (prop_enabled) {
1351 ALOGW("PCM offload property is enabled");
1352 pcmOffload = true;
Naresh Tanniru36c08932014-01-27 18:40:53 +05301353 }
1354 }
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001355 if (!pcmOffload) {
1356 ALOGV("PCM offload disabled by property audio.offload.pcm.enable");
1357 return false;
1358 }
1359 }
1360
1361 if (!pcmOffload) {
1362 // Check if offload has been disabled
1363 if (property_get("audio.offload.disable", propValue, "0")) {
1364 if (atoi(propValue) != 0) {
1365 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
1366 return false;
Naresh Tanniru36c08932014-01-27 18:40:53 +05301367 }
1368 }
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001369
1370 //check if it's multi-channel AAC format
1371 if (AudioSystem::popCount(offloadInfo.channel_mask) > 2
1372 && offloadInfo.format == AUDIO_FORMAT_AAC) {
1373 ALOGV("offload disabled for multi-channel AAC format");
1374 return false;
1375 }
1376
1377 if (offloadInfo.has_video)
1378 {
1379 if(property_get("av.offload.enable", propValue, NULL)) {
1380 bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1381 if (!prop_enabled) {
1382 ALOGW("offload disabled by av.offload.enable = %s ", propValue );
1383 return false;
1384 }
1385 } else {
1386 return false;
1387 }
1388
1389 if(offloadInfo.is_streaming) {
1390 if (property_get("av.streaming.offload.enable", propValue, NULL)) {
1391 bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1392 if (!prop_enabled) {
1393 ALOGW("offload disabled by av.streaming.offload.enable = %s ", propValue );
1394 return false;
1395 }
1396 } else {
1397 //Do not offload AV streamnig if the property is not defined
1398 return false;
1399 }
1400 }
1401 ALOGV("isOffloadSupported: has_video == true, property\
1402 set to enable offload");
1403 }
Naresh Tanniru36c08932014-01-27 18:40:53 +05301404 }
1405
1406 //If duration is less than minimum value defined in property, return false
1407 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
1408 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
1409 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
1410 return false;
1411 }
1412 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
1413 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
1414 //duration checks only valid for MP3/AAC formats,
1415 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001416 if (offloadInfo.format == AUDIO_FORMAT_MP3 || offloadInfo.format == AUDIO_FORMAT_AAC || pcmOffload)
Naresh Tanniru36c08932014-01-27 18:40:53 +05301417 return false;
1418 }
1419
1420 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1421 // creating an offloaded track and tearing it down immediately after start when audioflinger
1422 // detects there is an active non offloadable effect.
1423 // FIXME: We should check the audio session here but we do not have it in this context.
1424 // This may prevent offloading in rare situations where effects are left active by apps
1425 // in the background.
1426 if (isNonOffloadableEffectEnabled()) {
1427 return false;
1428 }
1429
1430 // See if there is a profile to support this.
1431 // AUDIO_DEVICE_NONE
1432 IOProfile *profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
1433 offloadInfo.sample_rate,
1434 offloadInfo.format,
1435 offloadInfo.channel_mask,
1436 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1437 ALOGV("isOffloadSupported() profile %sfound", profile != NULL ? "" : "NOT ");
1438 return (profile != NULL);
1439}
1440
1441void AudioPolicyManager::setPhoneState(int state)
1442
1443{
1444 ALOGV("setPhoneState() state %d", state);
1445 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
1446 if (state < 0 || state >= AudioSystem::NUM_MODES) {
1447 ALOGW("setPhoneState() invalid state %d", state);
1448 return;
1449 }
1450
1451 if (state == mPhoneState ) {
1452 ALOGW("setPhoneState() setting same state %d", state);
1453 return;
1454 }
1455
1456 // if leaving call state, handle special case of active streams
1457 // pertaining to sonification strategy see handleIncallSonification()
1458 if (isInCall()) {
1459 ALOGV("setPhoneState() in call state management: new state is %d", state);
1460 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
1461 handleIncallSonification(stream, false, true);
1462 }
1463 }
1464
1465 // store previous phone state for management of sonification strategy below
1466 int oldState = mPhoneState;
1467 mPhoneState = state;
1468 bool force = false;
1469
1470 // are we entering or starting a call
1471 if (!isStateInCall(oldState) && isStateInCall(state)) {
1472 ALOGV(" Entering call in setPhoneState()");
1473 // force routing command to audio hardware when starting a call
1474 // even if no device change is needed
1475 force = true;
1476 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
1477 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
1478 sVolumeProfiles[AUDIO_STREAM_VOICE_CALL][j];
1479 }
1480 } else if (isStateInCall(oldState) && !isStateInCall(state)) {
1481 ALOGV(" Exiting call in setPhoneState()");
1482 // force routing command to audio hardware when exiting a call
1483 // even if no device change is needed
1484 force = true;
1485 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
1486 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
1487 sVolumeProfiles[AUDIO_STREAM_DTMF][j];
1488 }
1489 } else if (isStateInCall(state) && (state != oldState)) {
1490 ALOGV(" Switching between telephony and VoIP in setPhoneState()");
1491 // force routing command to audio hardware when switching between telephony and VoIP
1492 // even if no device change is needed
1493 force = true;
1494 }
1495
1496 // check for device and output changes triggered by new phone state
1497 newDevice = getNewDevice(mPrimaryOutput, false /*fromCache*/);
1498 checkA2dpSuspend();
1499 checkOutputForAllStrategies();
1500 updateDevicesAndOutputs();
1501
1502 AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
1503
1504 // force routing command to audio hardware when ending call
1505 // even if no device change is needed
1506 if (isStateInCall(oldState) && newDevice == AUDIO_DEVICE_NONE) {
1507 newDevice = hwOutputDesc->device();
1508 }
1509
1510 int delayMs = 0;
1511 if (isStateInCall(state)) {
1512 nsecs_t sysTime = systemTime();
1513 for (size_t i = 0; i < mOutputs.size(); i++) {
1514 AudioOutputDescriptor *desc = mOutputs.valueAt(i);
1515 // mute media and sonification strategies and delay device switch by the largest
1516 // latency of any output where either strategy is active.
1517 // This avoid sending the ring tone or music tail into the earpiece or headset.
1518 if ((desc->isStrategyActive(STRATEGY_MEDIA,
1519 SONIFICATION_HEADSET_MUSIC_DELAY,
1520 sysTime) ||
1521 desc->isStrategyActive(STRATEGY_SONIFICATION,
1522 SONIFICATION_HEADSET_MUSIC_DELAY,
1523 sysTime)) &&
1524 (delayMs < (int)desc->mLatency*2)) {
1525 delayMs = desc->mLatency*2;
1526 }
1527 setStrategyMute(STRATEGY_MEDIA, true, mOutputs.keyAt(i));
1528 setStrategyMute(STRATEGY_MEDIA, false, mOutputs.keyAt(i), MUTE_TIME_MS,
1529 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
1530 setStrategyMute(STRATEGY_SONIFICATION, true, mOutputs.keyAt(i));
1531 setStrategyMute(STRATEGY_SONIFICATION, false, mOutputs.keyAt(i), MUTE_TIME_MS,
1532 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
1533 }
1534 }
1535
1536 // change routing is necessary
1537 setOutputDevice(mPrimaryOutput, newDevice, force, delayMs);
1538
1539 // if entering in call state, handle special case of active streams
1540 // pertaining to sonification strategy see handleIncallSonification()
1541 if (isStateInCall(state)) {
1542 ALOGV("setPhoneState() in call state management: new state is %d", state);
1543 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
1544 handleIncallSonification(stream, true, true);
1545 }
1546 }
1547
1548 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
1549 if (state == AudioSystem::MODE_RINGTONE &&
1550 isStreamActive(AudioSystem::MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
1551 mLimitRingtoneVolume = true;
1552 } else {
1553 mLimitRingtoneVolume = false;
1554 }
1555
1556#ifdef VOICE_CONCURRENCY
1557 //Call invalidate to reset all opened non ULL audio tracks
1558 if(isInCall())
1559 {
1560 // Move tracks associated to this strategy from previous output to new output
1561 for (int i = AudioSystem::SYSTEM; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
1562 ALOGV("\n Invalidate on call mode for stream :: %d \n", i);
1563 //FIXME see fixme on name change
1564 mpClientInterface->setStreamOutput((AudioSystem::stream_type)i,
1565 0 /* ignored */);
1566 }
1567 }
1568#endif
1569
1570}
1571
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001572extern "C" AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001573{
1574 return new AudioPolicyManager(clientInterface);
1575}
1576
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001577extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001578{
1579 delete interface;
1580}
1581
1582}; // namespace android