blob: 64842a164c07d03e616dc8f68b52bb6638a50edb [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// ----------------------------------------------------------------------------
47
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070048status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
49 AudioSystem::device_connection_state state,
50 const char *device_address)
51{
52 SortedVector <audio_io_handle_t> outputs;
53
54 ALOGV("setDeviceConnectionState() device: %x, state %d, address %s", device, state, device_address);
55
56 // connect/disconnect only 1 device at a time
57 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
58
59 if (strlen(device_address) >= MAX_DEVICE_ADDRESS_LEN) {
60 ALOGE("setDeviceConnectionState() invalid address: %s", device_address);
61 return BAD_VALUE;
62 }
63
64 // handle output devices
65 if (audio_is_output_device(device)) {
66
67 if (!mHasA2dp && audio_is_a2dp_device(device)) {
68 ALOGE("setDeviceConnectionState() invalid A2DP device: %x", device);
69 return BAD_VALUE;
70 }
71 if (!mHasUsb && audio_is_usb_device(device)) {
72 ALOGE("setDeviceConnectionState() invalid USB audio device: %x", device);
73 return BAD_VALUE;
74 }
75 if (!mHasRemoteSubmix && audio_is_remote_submix_device((audio_devices_t)device)) {
76 ALOGE("setDeviceConnectionState() invalid remote submix audio device: %x", device);
77 return BAD_VALUE;
78 }
79
80 // save a copy of the opened output descriptors before any output is opened or closed
81 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
82 mPreviousOutputs = mOutputs;
83 switch (state)
84 {
85 // handle output device connection
86 case AudioSystem::DEVICE_STATE_AVAILABLE:
87 if (mAvailableOutputDevices & device) {
88 ALOGW("setDeviceConnectionState() device already connected: %x", device);
89 return INVALID_OPERATION;
90 }
91 ALOGV("setDeviceConnectionState() connecting device %x", device);
92
93 if (checkOutputsForDevice(device, state, outputs) != NO_ERROR) {
94 return INVALID_OPERATION;
95 }
96 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %d outputs",
97 outputs.size());
98 // register new device as available
99 mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices | device);
100
101 if (!outputs.isEmpty()) {
102 String8 paramStr;
103 if (mHasA2dp && audio_is_a2dp_device(device)) {
104 // handle A2DP device connection
105 AudioParameter param;
106 param.add(String8(AUDIO_PARAMETER_A2DP_SINK_ADDRESS), String8(device_address));
107 paramStr = param.toString();
108 mA2dpDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
109 mA2dpSuspended = false;
110 } else if (audio_is_bluetooth_sco_device(device)) {
111 // handle SCO device connection
112 mScoDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
113 } else if (mHasUsb && audio_is_usb_device(device)) {
114 // handle USB device connection
115 mUsbCardAndDevice = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
116 paramStr = mUsbCardAndDevice;
117 }
118 // not currently handling multiple simultaneous submixes: ignoring remote submix
119 // case and address
120 if (!paramStr.isEmpty()) {
121 for (size_t i = 0; i < outputs.size(); i++) {
122 mpClientInterface->setParameters(outputs[i], paramStr);
123 }
124 }
125 }
126 break;
127 // handle output device disconnection
128 case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
129 if (!(mAvailableOutputDevices & device)) {
130 ALOGW("setDeviceConnectionState() device not connected: %x", device);
131 return INVALID_OPERATION;
132 }
133
134 ALOGV("setDeviceConnectionState() disconnecting device %x", device);
135 // remove device from available output devices
136 mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices & ~device);
137
138 checkOutputsForDevice(device, state, outputs);
139 if (mHasA2dp && audio_is_a2dp_device(device)) {
140 // handle A2DP device disconnection
141 mA2dpDeviceAddress = "";
142 mA2dpSuspended = false;
143 } else if (audio_is_bluetooth_sco_device(device)) {
144 // handle SCO device disconnection
145 mScoDeviceAddress = "";
146 } else if (mHasUsb && audio_is_usb_device(device)) {
147 // handle USB device disconnection
148 mUsbCardAndDevice = "";
149 }
150 // not currently handling multiple simultaneous submixes: ignoring remote submix
151 // case and address
152 } break;
153
154 default:
155 ALOGE("setDeviceConnectionState() invalid state: %x", state);
156 return BAD_VALUE;
157 }
158
159 checkA2dpSuspend();
160 checkOutputForAllStrategies();
161 // outputs must be closed after checkOutputForAllStrategies() is executed
162 if (!outputs.isEmpty()) {
163 for (size_t i = 0; i < outputs.size(); i++) {
164 AudioOutputDescriptor *desc = mOutputs.valueFor(outputs[i]);
165 // close unused outputs after device disconnection or direct outputs that have been
166 // opened by checkOutputsForDevice() to query dynamic parameters
167 if ((state == AudioSystem::DEVICE_STATE_UNAVAILABLE) ||
168 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
169 (desc->mDirectOpenCount == 0))) {
170 closeOutput(outputs[i]);
171 }
172 }
173 }
174
175 updateDevicesAndOutputs();
176 audio_devices_t newDevice = getNewDevice(mPrimaryOutput, false /*fromCache*/);
177#ifdef AUDIO_EXTN_FM_ENABLED
178 if(device == AUDIO_DEVICE_OUT_FM) {
179 if (state == AudioSystem::DEVICE_STATE_AVAILABLE) {
180 mOutputs.valueFor(mPrimaryOutput)->changeRefCount(AudioSystem::MUSIC, 1);
181 newDevice = (audio_devices_t)(getNewDevice(mPrimaryOutput, false) | AUDIO_DEVICE_OUT_FM);
182 } else {
183 mOutputs.valueFor(mPrimaryOutput)->changeRefCount(AudioSystem::MUSIC, -1);
184 }
185
186 AudioParameter param = AudioParameter();
187 param.addInt(String8("handle_fm"), (int)newDevice);
188 ALOGV("setDeviceConnectionState() setParameters handle_fm");
189 mpClientInterface->setParameters(mPrimaryOutput, param.toString());
190 }
191#endif
192 for (size_t i = 0; i < mOutputs.size(); i++) {
193 // do not force device change on duplicated output because if device is 0, it will
194 // also force a device 0 for the two outputs it is duplicated to which may override
195 // a valid device selection on those outputs.
196 setOutputDevice(mOutputs.keyAt(i),
197 getNewDevice(mOutputs.keyAt(i), true /*fromCache*/),
198 !mOutputs.valueAt(i)->isDuplicated(),
199 0);
200 }
201
202 if (device == AUDIO_DEVICE_OUT_WIRED_HEADSET) {
203 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
204 } else if (device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO ||
205 device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET ||
206 device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT) {
207 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
208 } else if(device == AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET){
209 device = AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET;
210 } else {
211 return NO_ERROR;
212 }
213 }
214 // handle input devices
215 if (audio_is_input_device(device)) {
216
217 switch (state)
218 {
219 // handle input device connection
220 case AudioSystem::DEVICE_STATE_AVAILABLE: {
221 if (mAvailableInputDevices & device) {
222 ALOGW("setDeviceConnectionState() device already connected: %d", device);
223 return INVALID_OPERATION;
224 }
225 mAvailableInputDevices = mAvailableInputDevices | (device & ~AUDIO_DEVICE_BIT_IN);
226 }
227 break;
228
229 // handle input device disconnection
230 case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
231 if (!(mAvailableInputDevices & device)) {
232 ALOGW("setDeviceConnectionState() device not connected: %d", device);
233 return INVALID_OPERATION;
234 }
235 mAvailableInputDevices = (audio_devices_t) (mAvailableInputDevices & ~device);
236 } break;
237
238 default:
239 ALOGE("setDeviceConnectionState() invalid state: %x", state);
240 return BAD_VALUE;
241 }
242
243 audio_io_handle_t activeInput = getActiveInput();
244 if (activeInput != 0) {
245 AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
246 audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
247 if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) {
248 ALOGV("setDeviceConnectionState() changing device from %x to %x for input %d",
249 inputDesc->mDevice, newDevice, activeInput);
250 inputDesc->mDevice = newDevice;
251 AudioParameter param = AudioParameter();
252 param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
253 mpClientInterface->setParameters(activeInput, param.toString());
254 }
255 }
256
257 return NO_ERROR;
258 }
259
260 ALOGW("setDeviceConnectionState() invalid device: %x", device);
261 return BAD_VALUE;
262}
263
Pavan Chikkalae33427d2014-03-24 17:11:50 +0530264void AudioPolicyManager::setPhoneState(int state)
265{
266 ALOGV("setPhoneState() state %d", state);
267 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
268 if (state < 0 || state >= AudioSystem::NUM_MODES) {
269 ALOGW("setPhoneState() invalid state %d", state);
270 return;
271 }
272
273 if (state == mPhoneState ) {
274 ALOGW("setPhoneState() setting same state %d", state);
275 return;
276 }
277
278 // if leaving call state, handle special case of active streams
279 // pertaining to sonification strategy see handleIncallSonification()
280 if (isInCall()) {
281 ALOGV("setPhoneState() in call state management: new state is %d", state);
282 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
283 handleIncallSonification(stream, false, true);
284 }
285 }
286
287 // store previous phone state for management of sonification strategy below
288 int oldState = mPhoneState;
289 mPhoneState = state;
290 bool force = false;
291
292 // are we entering or starting a call
293 if (!isStateInCall(oldState) && isStateInCall(state)) {
294 ALOGV(" Entering call in setPhoneState()");
295 // force routing command to audio hardware when starting a call
296 // even if no device change is needed
297 force = true;
298 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
299 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
300 sVolumeProfiles[AUDIO_STREAM_VOICE_CALL][j];
301 }
302 } else if (isStateInCall(oldState) && !isStateInCall(state)) {
303 ALOGV(" Exiting call in setPhoneState()");
304 // force routing command to audio hardware when exiting a call
305 // even if no device change is needed
306 force = true;
307 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
308 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
309 sVolumeProfiles[AUDIO_STREAM_DTMF][j];
310 }
311 } else if (isStateInCall(state) && (state != oldState)) {
312 ALOGV(" Switching between telephony and VoIP in setPhoneState()");
313 // force routing command to audio hardware when switching between telephony and VoIP
314 // even if no device change is needed
315 force = true;
316 }
317
318 // check for device and output changes triggered by new phone state
319 newDevice = getNewDevice(mPrimaryOutput, false /*fromCache*/);
320 checkA2dpSuspend();
321 checkOutputForAllStrategies();
322 updateDevicesAndOutputs();
323
324 AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
325
326 // force routing command to audio hardware when ending call
327 // even if no device change is needed
328 if (isStateInCall(oldState) && newDevice == AUDIO_DEVICE_NONE) {
329 newDevice = hwOutputDesc->device();
330 }
331
332 int delayMs = 0;
333 if (isStateInCall(state)) {
334 nsecs_t sysTime = systemTime();
335 for (size_t i = 0; i < mOutputs.size(); i++) {
336 AudioOutputDescriptor *desc = mOutputs.valueAt(i);
337 // mute media and sonification strategies and delay device switch by the largest
338 // latency of any output where either strategy is active.
339 // This avoid sending the ring tone or music tail into the earpiece or headset.
340 if ((desc->isStrategyActive(STRATEGY_MEDIA,
341 SONIFICATION_HEADSET_MUSIC_DELAY,
342 sysTime) ||
343 desc->isStrategyActive(STRATEGY_SONIFICATION,
344 SONIFICATION_HEADSET_MUSIC_DELAY,
345 sysTime)) &&
346 (delayMs < (int)desc->mLatency*2)) {
347 delayMs = desc->mLatency*2;
348 }
349 setStrategyMute(STRATEGY_MEDIA, true, mOutputs.keyAt(i));
350 setStrategyMute(STRATEGY_MEDIA, false, mOutputs.keyAt(i), MUTE_TIME_MS,
351 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
352 setStrategyMute(STRATEGY_SONIFICATION, true, mOutputs.keyAt(i));
353 setStrategyMute(STRATEGY_SONIFICATION, false, mOutputs.keyAt(i), MUTE_TIME_MS,
354 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
355 }
356 }
357
358 // change routing is necessary
359 setOutputDevice(mPrimaryOutput, newDevice, force, delayMs);
360
361 //update device for all non-primary outputs
362 for (size_t i = 0; i < mOutputs.size(); i++) {
363 audio_io_handle_t output = mOutputs.keyAt(i);
364 if (output != mPrimaryOutput) {
365 newDevice = getNewDevice(output, false /*fromCache*/);
366 setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE));
367 }
368 }
369
370 // if entering in call state, handle special case of active streams
371 // pertaining to sonification strategy see handleIncallSonification()
372 if (isStateInCall(state)) {
373 ALOGV("setPhoneState() in call state management: new state is %d", state);
374 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
375 handleIncallSonification(stream, true, true);
376 }
377 }
378
379 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
380 if (state == AudioSystem::MODE_RINGTONE &&
381 isStreamActive(AudioSystem::MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
382 mLimitRingtoneVolume = true;
383 } else {
384 mLimitRingtoneVolume = false;
385 }
386}
387
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700388void AudioPolicyManager::setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config)
389{
390 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState);
391
392 bool forceVolumeReeval = false;
393 switch(usage) {
394 case AudioSystem::FOR_COMMUNICATION:
395 if (config != AudioSystem::FORCE_SPEAKER && config != AudioSystem::FORCE_BT_SCO &&
396 config != AudioSystem::FORCE_NONE) {
397 ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
398 return;
399 }
400 forceVolumeReeval = true;
401 mForceUse[usage] = config;
402 break;
403 case AudioSystem::FOR_MEDIA:
404 if (config != AudioSystem::FORCE_HEADPHONES && config != AudioSystem::FORCE_BT_A2DP &&
405#ifdef AUDIO_EXTN_FM_ENABLED
406 config != AudioSystem::FORCE_SPEAKER &&
407#endif
408 config != AudioSystem::FORCE_WIRED_ACCESSORY &&
409 config != AudioSystem::FORCE_ANALOG_DOCK &&
410 config != AudioSystem::FORCE_DIGITAL_DOCK && config != AudioSystem::FORCE_NONE &&
411 config != AudioSystem::FORCE_NO_BT_A2DP) {
412 ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
413 return;
414 }
415 mForceUse[usage] = config;
416 break;
417 case AudioSystem::FOR_RECORD:
418 if (config != AudioSystem::FORCE_BT_SCO && config != AudioSystem::FORCE_WIRED_ACCESSORY &&
419 config != AudioSystem::FORCE_NONE) {
420 ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
421 return;
422 }
423 mForceUse[usage] = config;
424 break;
425 case AudioSystem::FOR_DOCK:
426 if (config != AudioSystem::FORCE_NONE && config != AudioSystem::FORCE_BT_CAR_DOCK &&
427 config != AudioSystem::FORCE_BT_DESK_DOCK &&
428 config != AudioSystem::FORCE_WIRED_ACCESSORY &&
429 config != AudioSystem::FORCE_ANALOG_DOCK &&
430 config != AudioSystem::FORCE_DIGITAL_DOCK) {
431 ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
432 }
433 forceVolumeReeval = true;
434 mForceUse[usage] = config;
435 break;
436 case AudioSystem::FOR_SYSTEM:
437 if (config != AudioSystem::FORCE_NONE &&
438 config != AudioSystem::FORCE_SYSTEM_ENFORCED) {
439 ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
440 }
441 forceVolumeReeval = true;
442 mForceUse[usage] = config;
443 break;
444 default:
445 ALOGW("setForceUse() invalid usage %d", usage);
446 break;
447 }
448
449 // check for device and output changes triggered by new force usage
450 checkA2dpSuspend();
451 checkOutputForAllStrategies();
452 updateDevicesAndOutputs();
453 for (int i = mOutputs.size() -1; i >= 0; i--) {
454 audio_io_handle_t output = mOutputs.keyAt(i);
455 audio_devices_t newDevice = getNewDevice(output, true /*fromCache*/);
456 setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE));
457 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
458 applyStreamVolumes(output, newDevice, 0, true);
459 }
460 }
461
462 audio_io_handle_t activeInput = getActiveInput();
463 if (activeInput != 0) {
464 AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
465 audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
466 if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) {
467 ALOGV("setForceUse() changing device from %x to %x for input %d",
468 inputDesc->mDevice, newDevice, activeInput);
469 inputDesc->mDevice = newDevice;
470 AudioParameter param = AudioParameter();
471 param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
472 mpClientInterface->setParameters(activeInput, param.toString());
473 }
474 }
475
476}
477
478audio_io_handle_t AudioPolicyManager::getInput(int inputSource,
479 uint32_t samplingRate,
480 uint32_t format,
481 uint32_t channelMask,
482 AudioSystem::audio_in_acoustics acoustics)
483{
484 audio_io_handle_t input = 0;
485 audio_devices_t device = getDeviceForInputSource(inputSource);
486
487 ALOGV("getInput() inputSource %d, samplingRate %d, format %d, channelMask %x, acoustics %x",
488 inputSource, samplingRate, format, channelMask, acoustics);
489
490 if (device == AUDIO_DEVICE_NONE) {
491 ALOGW("getInput() could not find device for inputSource %d", inputSource);
492 return 0;
493 }
494
495
496 IOProfile *profile = getInputProfile(device,
497 samplingRate,
498 format,
499 channelMask);
500 if (profile == NULL) {
501 ALOGW("getInput() could not find profile for device %04x, samplingRate %d, format %d,"
502 "channelMask %04x",
503 device, samplingRate, format, channelMask);
504 return 0;
505 }
506
507 if (profile->mModule->mHandle == 0) {
508 ALOGE("getInput(): HW module %s not opened", profile->mModule->mName);
509 return 0;
510 }
511
512 AudioInputDescriptor *inputDesc = new AudioInputDescriptor(profile);
513
514 inputDesc->mInputSource = inputSource;
515 inputDesc->mDevice = device;
516 inputDesc->mSamplingRate = samplingRate;
517 inputDesc->mFormat = (audio_format_t)format;
518 inputDesc->mChannelMask = (audio_channel_mask_t)channelMask;
519 inputDesc->mRefCount = 0;
520 input = mpClientInterface->openInput(profile->mModule->mHandle,
521 &inputDesc->mDevice,
522 &inputDesc->mSamplingRate,
523 &inputDesc->mFormat,
524 &inputDesc->mChannelMask);
525
526 // only accept input with the exact requested set of parameters
527 if (input == 0 ||
528 (samplingRate != inputDesc->mSamplingRate) ||
529 (format != inputDesc->mFormat) ||
530 (channelMask != inputDesc->mChannelMask)) {
531 ALOGV("getInput() failed opening input: samplingRate %d, format %d, channelMask %d",
532 samplingRate, format, channelMask);
533 if (input != 0) {
534 mpClientInterface->closeInput(input);
535 }
536 delete inputDesc;
537 return 0;
538 }
539 mInputs.add(input, inputDesc);
540 return input;
541}
542
543AudioPolicyManager::routing_strategy AudioPolicyManager::getStrategy(AudioSystem::stream_type stream)
544{
Pavan Chikkalae33427d2014-03-24 17:11:50 +0530545 // stream to strategy mapping
546 switch (stream) {
547 case AudioSystem::VOICE_CALL:
548 case AudioSystem::BLUETOOTH_SCO:
549 return STRATEGY_PHONE;
550 case AudioSystem::RING:
551 case AudioSystem::ALARM:
552 return STRATEGY_SONIFICATION;
553 case AudioSystem::NOTIFICATION:
554 return STRATEGY_SONIFICATION_RESPECTFUL;
555 case AudioSystem::DTMF:
556 return STRATEGY_DTMF;
557 default:
558 ALOGE("unknown stream type");
559 case AudioSystem::SYSTEM:
560 // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
561 // while key clicks are played produces a poor result
562 case AudioSystem::TTS:
563 case AudioSystem::MUSIC:
564#ifdef AUDIO_EXTN_INCALL_MUSIC_ENABLED
565 case AudioSystem::INCALL_MUSIC:
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700566#endif
Pavan Chikkalae33427d2014-03-24 17:11:50 +0530567 return STRATEGY_MEDIA;
568 case AudioSystem::ENFORCED_AUDIBLE:
569 return STRATEGY_ENFORCED_AUDIBLE;
570 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700571}
572
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700573audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
574 bool fromCache)
575{
576 uint32_t device = AUDIO_DEVICE_NONE;
577
578 if (fromCache) {
579 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
580 strategy, mDeviceForStrategy[strategy]);
581 return mDeviceForStrategy[strategy];
582 }
583
584 switch (strategy) {
585
586 case STRATEGY_SONIFICATION_RESPECTFUL:
587 if (isInCall()) {
588 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
589 } else if (isStreamActiveRemotely(AudioSystem::MUSIC,
590 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
591 // while media is playing on a remote device, use the the sonification behavior.
592 // Note that we test this usecase before testing if media is playing because
593 // the isStreamActive() method only informs about the activity of a stream, not
594 // if it's for local playback. Note also that we use the same delay between both tests
595 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
596 } else if (isStreamActive(AudioSystem::MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
597 // while media is playing (or has recently played), use the same device
598 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
599 } else {
600 // when media is not playing anymore, fall back on the sonification behavior
601 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
602 }
603
604 break;
605
606 case STRATEGY_DTMF:
607 if (!isInCall()) {
608 // when off call, DTMF strategy follows the same rules as MEDIA strategy
609 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
610 break;
611 }
612 // when in call, DTMF and PHONE strategies follow the same rules
613 // FALL THROUGH
614
615 case STRATEGY_PHONE:
616 // for phone strategy, we first consider the forced use and then the available devices by order
617 // of priority
618 switch (mForceUse[AudioSystem::FOR_COMMUNICATION]) {
619 case AudioSystem::FORCE_BT_SCO:
620 if (!isInCall() || strategy != STRATEGY_DTMF) {
621 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
622 if (device) break;
623 }
624 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
625 if (device) break;
626 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
627 if (device) break;
628 // if SCO device is requested but no SCO device is available, fall back to default case
629 // FALL THROUGH
630
631 default: // FORCE_NONE
632 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
633 if (mHasA2dp && !isInCall() &&
634 (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
635 (getA2dpOutput() != 0) && !mA2dpSuspended) {
636 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
637 if (device) break;
638 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
639 if (device) break;
640 }
641 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
642 if (device) break;
643 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
644 if (device) break;
645 if (mPhoneState != AudioSystem::MODE_IN_CALL) {
646 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
647 if (device) break;
648 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
649 if (device) break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700650 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
651 if (device) break;
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700652 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
653 if (device) break;
654 }
655
656 // Allow voice call on USB ANLG DOCK headset
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700657 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
658 if (device) break;
659
660 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_EARPIECE;
661 if (device) break;
662 device = mDefaultOutputDevice;
663 if (device == AUDIO_DEVICE_NONE) {
664 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE");
665 }
666 break;
667
668 case AudioSystem::FORCE_SPEAKER:
669 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
670 // A2DP speaker when forcing to speaker output
671 if (mHasA2dp && !isInCall() &&
672 (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
673 (getA2dpOutput() != 0) && !mA2dpSuspended) {
674 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
675 if (device) break;
676 }
677 if (mPhoneState != AudioSystem::MODE_IN_CALL) {
678 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
679 if (device) break;
680 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
681 if (device) break;
682 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
683 if (device) break;
684 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
685 if (device) break;
Helen Zengbbce4952013-12-16 20:26:46 -0800686 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
687 if (device) break;
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700688 }
689 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
690 if (device) break;
691 device = mDefaultOutputDevice;
692 if (device == AUDIO_DEVICE_NONE) {
693 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER");
694 }
695 break;
696 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700697 // FIXME: Why do need to replace with speaker? If voice call is active
698 // We should use device from STRATEGY_PHONE
699#ifdef AUDIO_EXTN_FM_ENABLED
700 if (mAvailableOutputDevices & AUDIO_DEVICE_OUT_FM) {
701 if (mForceUse[AudioSystem::FOR_MEDIA] == AudioSystem::FORCE_SPEAKER) {
702 device = AUDIO_DEVICE_OUT_SPEAKER;
703 }
704 }
705#endif
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700706 break;
707
708 case STRATEGY_SONIFICATION:
709
710 // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
711 // handleIncallSonification().
712 if (isInCall()) {
713 device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
714 break;
715 }
716 // FALL THROUGH
717
718 case STRATEGY_ENFORCED_AUDIBLE:
719 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
720 // except:
721 // - when in call where it doesn't default to STRATEGY_PHONE behavior
722 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
723
724 if ((strategy == STRATEGY_SONIFICATION) ||
725 (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_SYSTEM_ENFORCED)) {
726 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
727 if (device == AUDIO_DEVICE_NONE) {
728 ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION");
729 }
730 }
731 // The second device used for sonification is the same as the device used by media strategy
732 // FALL THROUGH
733
734 case STRATEGY_MEDIA: {
735 uint32_t device2 = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700736
Mingming Yin4a72d652014-01-03 18:54:18 -0800737 if (isInCall() && (device == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700738 // when in call, get the device for Phone strategy
739 device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
740 break;
741 }
742#ifdef AUDIO_EXTN_FM_ENABLED
743 if (mForceUse[AudioSystem::FOR_MEDIA] == AudioSystem::FORCE_SPEAKER) {
744 device = AUDIO_DEVICE_OUT_SPEAKER;
745 break;
746 }
747#endif
748
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700749 if (strategy != STRATEGY_SONIFICATION) {
750 // no sonification on remote submix (e.g. WFD)
751 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
752 }
753 if ((device2 == AUDIO_DEVICE_NONE) &&
754 mHasA2dp && (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
755 (getA2dpOutput() != 0) && !mA2dpSuspended) {
756 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
757 if (device2 == AUDIO_DEVICE_NONE) {
758 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
759 }
760 if (device2 == AUDIO_DEVICE_NONE) {
761 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
762 }
763 }
764 if (device2 == AUDIO_DEVICE_NONE) {
765 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
766 }
767 if (device2 == AUDIO_DEVICE_NONE) {
768 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
769 }
770 if (device2 == AUDIO_DEVICE_NONE) {
771 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
772 }
773 if (device2 == AUDIO_DEVICE_NONE) {
774 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
775 }
776 if (device2 == AUDIO_DEVICE_NONE) {
777 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
778 }
Apoorv Raghuvanshi8506a982014-01-17 13:10:09 -0800779 if ((strategy != STRATEGY_SONIFICATION) && (device == AUDIO_DEVICE_NONE)
780 && (device2 == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700781 // no sonification on aux digital (e.g. HDMI)
782 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
783 }
784 if ((device2 == AUDIO_DEVICE_NONE) &&
Krishnankutty Kolathappilly4b7fdaa2013-12-16 00:40:11 -0800785 (mForceUse[AudioSystem::FOR_DOCK] == AudioSystem::FORCE_ANALOG_DOCK)
786 && (strategy != STRATEGY_SONIFICATION)) {
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700787 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
788 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700789#ifdef AUDIO_EXTN_FM_ENABLED
Apoorv Raghuvanshi8506a982014-01-17 13:10:09 -0800790 if ((strategy != STRATEGY_SONIFICATION) && (device == AUDIO_DEVICE_NONE)
791 && (device2 == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700792 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_FM_TX;
793 }
794#endif
795#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Apoorv Raghuvanshi8506a982014-01-17 13:10:09 -0800796 if ((strategy != STRATEGY_SONIFICATION) && (device == AUDIO_DEVICE_NONE)
797 && (device2 == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700798 // no sonification on WFD sink
799 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_PROXY;
800 }
801#endif
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700802 if (device2 == AUDIO_DEVICE_NONE) {
803 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
804 }
805
806 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
807 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
808 device |= device2;
809 if (device) break;
810 device = mDefaultOutputDevice;
811 if (device == AUDIO_DEVICE_NONE) {
812 ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA");
813 }
814 } break;
815
816 default:
817 ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
818 break;
819 }
820
821 ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
822 return device;
823}
824
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700825audio_devices_t AudioPolicyManager::getDeviceForInputSource(int inputSource)
826{
827 uint32_t device = AUDIO_DEVICE_NONE;
828
829 switch (inputSource) {
830 case AUDIO_SOURCE_VOICE_UPLINK:
831 if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
832 device = AUDIO_DEVICE_IN_VOICE_CALL;
833 break;
834 }
835 // FALL THROUGH
836
837 case AUDIO_SOURCE_DEFAULT:
838 case AUDIO_SOURCE_MIC:
839 case AUDIO_SOURCE_VOICE_RECOGNITION:
840 case AUDIO_SOURCE_HOTWORD:
841 case AUDIO_SOURCE_VOICE_COMMUNICATION:
842 if (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO &&
843 mAvailableInputDevices & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
844 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
845 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_WIRED_HEADSET) {
846 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
847 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET) {
848 device = AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET;
849 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
850 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
851 }
852 break;
853 case AUDIO_SOURCE_CAMCORDER:
854 if (mAvailableInputDevices & AUDIO_DEVICE_IN_BACK_MIC) {
855 device = AUDIO_DEVICE_IN_BACK_MIC;
856 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
857 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
858 }
859 break;
860 case AUDIO_SOURCE_VOICE_DOWNLINK:
861 case AUDIO_SOURCE_VOICE_CALL:
862 if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
863 device = AUDIO_DEVICE_IN_VOICE_CALL;
864 }
865 break;
866 case AUDIO_SOURCE_REMOTE_SUBMIX:
867 if (mAvailableInputDevices & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
868 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
869 }
870 break;
871#ifdef AUDIO_EXTN_FM_ENABLED
872 case AUDIO_SOURCE_FM_RX:
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700873 device = AUDIO_DEVICE_IN_FM_RX;
874 break;
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700875 case AUDIO_SOURCE_FM_RX_A2DP:
876 device = AUDIO_DEVICE_IN_FM_RX_A2DP;
877 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700878#endif
879 default:
880 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
881 break;
882 }
883 ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
884 return device;
885}
886
887AudioPolicyManager::device_category AudioPolicyManager::getDeviceCategory(audio_devices_t device)
888{
889 switch(getDeviceForVolume(device)) {
890 case AUDIO_DEVICE_OUT_EARPIECE:
891 return DEVICE_CATEGORY_EARPIECE;
892 case AUDIO_DEVICE_OUT_WIRED_HEADSET:
893 case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
894 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
895 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
896 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
897 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
898#ifdef AUDIO_EXTN_FM_ENABLED
899 case AUDIO_DEVICE_OUT_FM:
900#endif
901 return DEVICE_CATEGORY_HEADSET;
902 case AUDIO_DEVICE_OUT_SPEAKER:
903 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
904 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
905 case AUDIO_DEVICE_OUT_AUX_DIGITAL:
906 case AUDIO_DEVICE_OUT_USB_ACCESSORY:
907 case AUDIO_DEVICE_OUT_USB_DEVICE:
908 case AUDIO_DEVICE_OUT_REMOTE_SUBMIX:
909#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
910 case AUDIO_DEVICE_OUT_PROXY:
911#endif
912 default:
913 return DEVICE_CATEGORY_SPEAKER;
914 }
915}
916
917status_t AudioPolicyManager::checkAndSetVolume(int stream,
918 int index,
919 audio_io_handle_t output,
920 audio_devices_t device,
921 int delayMs,
922 bool force)
923{
924 ALOGV("checkAndSetVolume: index %d output %d device %x", index, output, device);
925 // do not change actual stream volume if the stream is muted
926 if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
927 ALOGVV("checkAndSetVolume() stream %d muted count %d",
928 stream, mOutputs.valueFor(output)->mMuteCount[stream]);
929 return NO_ERROR;
930 }
931
932 // do not change in call volume if bluetooth is connected and vice versa
933 if ((stream == AudioSystem::VOICE_CALL && mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
934 (stream == AudioSystem::BLUETOOTH_SCO && mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO)) {
935 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
936 stream, mForceUse[AudioSystem::FOR_COMMUNICATION]);
937 return INVALID_OPERATION;
938 }
939
940 float volume = computeVolume(stream, index, output, device);
941 // We actually change the volume if:
942 // - the float value returned by computeVolume() changed
943 // - the force flag is set
944 if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
945 force) {
946 mOutputs.valueFor(output)->mCurVolume[stream] = volume;
947 ALOGV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
948 // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
949 // enabled
950 if (stream == AudioSystem::BLUETOOTH_SCO) {
951 mpClientInterface->setStreamVolume(AudioSystem::VOICE_CALL, volume, output, delayMs);
952#ifdef AUDIO_EXTN_FM_ENABLED
953 } else if (stream == AudioSystem::MUSIC &&
954 output == mPrimaryOutput) {
955 float fmVolume = -1.0;
956 fmVolume = computeVolume(stream, index, output, device);
957 if (fmVolume >= 0) {
958 AudioParameter param = AudioParameter();
959 param.addFloat(String8("fm_volume"), fmVolume);
960 ALOGV("checkAndSetVolume setParameters fm_volume, volume=:%f delay=:%d",fmVolume,delayMs*2);
961 //Double delayMs to avoid sound burst while device switch.
962 mpClientInterface->setParameters(mPrimaryOutput, param.toString(), delayMs*2);
963 }
964#endif
965 }
966 mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs);
967 }
968
969 if (stream == AudioSystem::VOICE_CALL ||
970 stream == AudioSystem::BLUETOOTH_SCO) {
971 float voiceVolume;
972
Avinash Vaishbd1440c2014-04-21 18:46:48 +0530973 if (stream == AudioSystem::VOICE_CALL)
974 voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
975 else
976 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
977 voiceVolume = 1.0;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700978
979 if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) {
980 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
981 mLastVoiceVolume = voiceVolume;
982 }
983 }
984
985 return NO_ERROR;
986}
987
988
989float AudioPolicyManager::computeVolume(int stream,
990 int index,
991 audio_io_handle_t output,
992 audio_devices_t device)
993{
994 float volume = 1.0;
995 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
996
997 if (device == AUDIO_DEVICE_NONE) {
998 device = outputDesc->device();
999 }
1000
1001 // if volume is not 0 (not muted), force media volume to max on digital output
1002 if (stream == AudioSystem::MUSIC &&
1003 index != mStreams[stream].mIndexMin &&
1004 (device == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
1005 device == AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET ||
1006 device == AUDIO_DEVICE_OUT_USB_ACCESSORY ||
1007#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
1008 device == AUDIO_DEVICE_OUT_PROXY ||
1009#endif
1010 device == AUDIO_DEVICE_OUT_USB_DEVICE )) {
1011 return 1.0;
1012 }
1013#ifdef AUDIO_EXTN_INCALL_MUSIC_ENABLED
1014 if (stream == AudioSystem::INCALL_MUSIC) {
1015 return 1.0;
1016 }
1017#endif
1018 return AudioPolicyManagerBase::computeVolume(stream, index, output, device);
1019}
ApurupaPattapu0c566872014-01-10 14:46:02 -08001020
1021// This function checks for the parameters which can be offloaded.
1022// This can be enhanced depending on the capability of the DSP and policy
1023// of the system.
1024bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
1025{
1026 ALOGV(" isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
1027 " BitRate=%u, duration=%lld us, has_video=%d",
1028 offloadInfo.sample_rate, offloadInfo.channel_mask,
1029 offloadInfo.format,
1030 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
1031 offloadInfo.has_video);
1032
1033#ifdef VOICE_CONCURRENCY
1034 if(isInCall())
1035 {
1036 ALOGD("\n blocking compress offload on call mode\n");
1037 return false;
1038 }
1039#endif
1040 // Check if stream type is music, then only allow offload as of now.
1041 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
1042 {
1043 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
1044 return false;
1045 }
1046
1047 char propValue[PROPERTY_VALUE_MAX];
1048 bool pcmOffload = false;
1049 if (audio_is_offload_pcm(offloadInfo.format)) {
1050 if(property_get("audio.offload.pcm.enable", propValue, NULL)) {
1051 bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1052 if (prop_enabled) {
1053 ALOGW("PCM offload property is enabled");
1054 pcmOffload = true;
1055 }
1056 }
1057 if (!pcmOffload) {
1058 ALOGV("PCM offload disabled by property audio.offload.pcm.enable");
1059 return false;
1060 }
1061 }
1062
1063 if (!pcmOffload) {
1064 // Check if offload has been disabled
1065 if (property_get("audio.offload.disable", propValue, "0")) {
1066 if (atoi(propValue) != 0) {
1067 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
1068 return false;
1069 }
1070 }
1071
1072 //check if it's multi-channel AAC format
1073 if (AudioSystem::popCount(offloadInfo.channel_mask) > 2
1074 && offloadInfo.format == AUDIO_FORMAT_AAC) {
1075 ALOGV("offload disabled for multi-channel AAC format");
1076 return false;
1077 }
1078
1079 if (offloadInfo.has_video)
1080 {
1081 if(property_get("av.offload.enable", propValue, NULL)) {
1082 bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1083 if (!prop_enabled) {
1084 ALOGW("offload disabled by av.offload.enable = %s ", propValue );
1085 return false;
1086 }
1087 } else {
1088 return false;
1089 }
1090
1091 if(offloadInfo.is_streaming) {
1092 if (property_get("av.streaming.offload.enable", propValue, NULL)) {
1093 bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1094 if (!prop_enabled) {
1095 ALOGW("offload disabled by av.streaming.offload.enable = %s ", propValue );
1096 return false;
1097 }
1098 } else {
1099 //Do not offload AV streamnig if the property is not defined
1100 return false;
1101 }
1102 }
1103 ALOGV("isOffloadSupported: has_video == true, property\
1104 set to enable offload");
1105 }
1106 }
1107
1108 //If duration is less than minimum value defined in property, return false
1109 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
1110 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
1111 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
1112 return false;
1113 }
1114 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
1115 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
1116 //duration checks only valid for MP3/AAC formats,
1117 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
1118 if (offloadInfo.format == AUDIO_FORMAT_MP3 || offloadInfo.format == AUDIO_FORMAT_AAC || pcmOffload)
1119 return false;
1120 }
1121
1122 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1123 // creating an offloaded track and tearing it down immediately after start when audioflinger
1124 // detects there is an active non offloadable effect.
1125 // FIXME: We should check the audio session here but we do not have it in this context.
1126 // This may prevent offloading in rare situations where effects are left active by apps
1127 // in the background.
1128 if (isNonOffloadableEffectEnabled()) {
1129 return false;
1130 }
1131
1132 // See if there is a profile to support this.
1133 // AUDIO_DEVICE_NONE
1134 IOProfile *profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
1135 offloadInfo.sample_rate,
1136 offloadInfo.format,
1137 offloadInfo.channel_mask,
1138 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1139 ALOGV("isOffloadSupported() profile %sfound", profile != NULL ? "" : "NOT ");
1140 return (profile != NULL);
1141}
1142
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001143extern "C" AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001144{
1145 return new AudioPolicyManager(clientInterface);
1146}
1147
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001148extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001149{
1150 delete interface;
1151}
1152
1153}; // namespace android