blob: a774c9e2169636f26a38dc619a5a4b857a43e2ed [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
264void AudioPolicyManager::setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config)
265{
266 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState);
267
268 bool forceVolumeReeval = false;
269 switch(usage) {
270 case AudioSystem::FOR_COMMUNICATION:
271 if (config != AudioSystem::FORCE_SPEAKER && config != AudioSystem::FORCE_BT_SCO &&
272 config != AudioSystem::FORCE_NONE) {
273 ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
274 return;
275 }
276 forceVolumeReeval = true;
277 mForceUse[usage] = config;
278 break;
279 case AudioSystem::FOR_MEDIA:
280 if (config != AudioSystem::FORCE_HEADPHONES && config != AudioSystem::FORCE_BT_A2DP &&
281#ifdef AUDIO_EXTN_FM_ENABLED
282 config != AudioSystem::FORCE_SPEAKER &&
283#endif
284 config != AudioSystem::FORCE_WIRED_ACCESSORY &&
285 config != AudioSystem::FORCE_ANALOG_DOCK &&
286 config != AudioSystem::FORCE_DIGITAL_DOCK && config != AudioSystem::FORCE_NONE &&
287 config != AudioSystem::FORCE_NO_BT_A2DP) {
288 ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
289 return;
290 }
291 mForceUse[usage] = config;
292 break;
293 case AudioSystem::FOR_RECORD:
294 if (config != AudioSystem::FORCE_BT_SCO && config != AudioSystem::FORCE_WIRED_ACCESSORY &&
295 config != AudioSystem::FORCE_NONE) {
296 ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
297 return;
298 }
299 mForceUse[usage] = config;
300 break;
301 case AudioSystem::FOR_DOCK:
302 if (config != AudioSystem::FORCE_NONE && config != AudioSystem::FORCE_BT_CAR_DOCK &&
303 config != AudioSystem::FORCE_BT_DESK_DOCK &&
304 config != AudioSystem::FORCE_WIRED_ACCESSORY &&
305 config != AudioSystem::FORCE_ANALOG_DOCK &&
306 config != AudioSystem::FORCE_DIGITAL_DOCK) {
307 ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
308 }
309 forceVolumeReeval = true;
310 mForceUse[usage] = config;
311 break;
312 case AudioSystem::FOR_SYSTEM:
313 if (config != AudioSystem::FORCE_NONE &&
314 config != AudioSystem::FORCE_SYSTEM_ENFORCED) {
315 ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
316 }
317 forceVolumeReeval = true;
318 mForceUse[usage] = config;
319 break;
320 default:
321 ALOGW("setForceUse() invalid usage %d", usage);
322 break;
323 }
324
325 // check for device and output changes triggered by new force usage
326 checkA2dpSuspend();
327 checkOutputForAllStrategies();
328 updateDevicesAndOutputs();
329 for (int i = mOutputs.size() -1; i >= 0; i--) {
330 audio_io_handle_t output = mOutputs.keyAt(i);
331 audio_devices_t newDevice = getNewDevice(output, true /*fromCache*/);
332 setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE));
333 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
334 applyStreamVolumes(output, newDevice, 0, true);
335 }
336 }
337
338 audio_io_handle_t activeInput = getActiveInput();
339 if (activeInput != 0) {
340 AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
341 audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
342 if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) {
343 ALOGV("setForceUse() changing device from %x to %x for input %d",
344 inputDesc->mDevice, newDevice, activeInput);
345 inputDesc->mDevice = newDevice;
346 AudioParameter param = AudioParameter();
347 param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
348 mpClientInterface->setParameters(activeInput, param.toString());
349 }
350 }
351
352}
353
354audio_io_handle_t AudioPolicyManager::getInput(int inputSource,
355 uint32_t samplingRate,
356 uint32_t format,
357 uint32_t channelMask,
358 AudioSystem::audio_in_acoustics acoustics)
359{
360 audio_io_handle_t input = 0;
361 audio_devices_t device = getDeviceForInputSource(inputSource);
362
363 ALOGV("getInput() inputSource %d, samplingRate %d, format %d, channelMask %x, acoustics %x",
364 inputSource, samplingRate, format, channelMask, acoustics);
365
366 if (device == AUDIO_DEVICE_NONE) {
367 ALOGW("getInput() could not find device for inputSource %d", inputSource);
368 return 0;
369 }
370
371
372 IOProfile *profile = getInputProfile(device,
373 samplingRate,
374 format,
375 channelMask);
376 if (profile == NULL) {
377 ALOGW("getInput() could not find profile for device %04x, samplingRate %d, format %d,"
378 "channelMask %04x",
379 device, samplingRate, format, channelMask);
380 return 0;
381 }
382
383 if (profile->mModule->mHandle == 0) {
384 ALOGE("getInput(): HW module %s not opened", profile->mModule->mName);
385 return 0;
386 }
387
388 AudioInputDescriptor *inputDesc = new AudioInputDescriptor(profile);
389
390 inputDesc->mInputSource = inputSource;
391 inputDesc->mDevice = device;
392 inputDesc->mSamplingRate = samplingRate;
393 inputDesc->mFormat = (audio_format_t)format;
394 inputDesc->mChannelMask = (audio_channel_mask_t)channelMask;
395 inputDesc->mRefCount = 0;
396 input = mpClientInterface->openInput(profile->mModule->mHandle,
397 &inputDesc->mDevice,
398 &inputDesc->mSamplingRate,
399 &inputDesc->mFormat,
400 &inputDesc->mChannelMask);
401
402 // only accept input with the exact requested set of parameters
403 if (input == 0 ||
404 (samplingRate != inputDesc->mSamplingRate) ||
405 (format != inputDesc->mFormat) ||
406 (channelMask != inputDesc->mChannelMask)) {
407 ALOGV("getInput() failed opening input: samplingRate %d, format %d, channelMask %d",
408 samplingRate, format, channelMask);
409 if (input != 0) {
410 mpClientInterface->closeInput(input);
411 }
412 delete inputDesc;
413 return 0;
414 }
415 mInputs.add(input, inputDesc);
416 return input;
417}
418
419AudioPolicyManager::routing_strategy AudioPolicyManager::getStrategy(AudioSystem::stream_type stream)
420{
Naresh Tanniru36c08932014-01-27 18:40:53 +0530421 // stream to strategy mapping
422 switch (stream) {
423 case AudioSystem::VOICE_CALL:
424 case AudioSystem::BLUETOOTH_SCO:
425 return STRATEGY_PHONE;
426 case AudioSystem::RING:
427 case AudioSystem::ALARM:
428 return STRATEGY_SONIFICATION;
429 case AudioSystem::NOTIFICATION:
430 return STRATEGY_SONIFICATION_RESPECTFUL;
431 case AudioSystem::DTMF:
432 return STRATEGY_DTMF;
433 default:
434 ALOGE("unknown stream type");
435 case AudioSystem::SYSTEM:
436 // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
437 // while key clicks are played produces a poor result
438 case AudioSystem::TTS:
439 case AudioSystem::MUSIC:
440#ifdef AUDIO_EXTN_INCALL_MUSIC_ENABLED
441 case AudioSystem::INCALL_MUSIC:
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700442#endif
Naresh Tanniru36c08932014-01-27 18:40:53 +0530443#ifdef QCOM_INCALL_MUSIC_ENABLED
444 case AudioSystem::INCALL_MUSIC:
445#endif
446 return STRATEGY_MEDIA;
447 case AudioSystem::ENFORCED_AUDIBLE:
448 return STRATEGY_ENFORCED_AUDIBLE;
449 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700450
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700451}
452
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700453audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
454 bool fromCache)
455{
456 uint32_t device = AUDIO_DEVICE_NONE;
457
458 if (fromCache) {
459 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
460 strategy, mDeviceForStrategy[strategy]);
461 return mDeviceForStrategy[strategy];
462 }
463
464 switch (strategy) {
465
466 case STRATEGY_SONIFICATION_RESPECTFUL:
467 if (isInCall()) {
468 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
469 } else if (isStreamActiveRemotely(AudioSystem::MUSIC,
470 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
471 // while media is playing on a remote device, use the the sonification behavior.
472 // Note that we test this usecase before testing if media is playing because
473 // the isStreamActive() method only informs about the activity of a stream, not
474 // if it's for local playback. Note also that we use the same delay between both tests
475 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
476 } else if (isStreamActive(AudioSystem::MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
477 // while media is playing (or has recently played), use the same device
478 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
479 } else {
480 // when media is not playing anymore, fall back on the sonification behavior
481 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
482 }
483
484 break;
485
486 case STRATEGY_DTMF:
487 if (!isInCall()) {
488 // when off call, DTMF strategy follows the same rules as MEDIA strategy
489 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
490 break;
491 }
492 // when in call, DTMF and PHONE strategies follow the same rules
493 // FALL THROUGH
494
495 case STRATEGY_PHONE:
496 // for phone strategy, we first consider the forced use and then the available devices by order
497 // of priority
498 switch (mForceUse[AudioSystem::FOR_COMMUNICATION]) {
499 case AudioSystem::FORCE_BT_SCO:
500 if (!isInCall() || strategy != STRATEGY_DTMF) {
501 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
502 if (device) break;
503 }
504 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
505 if (device) break;
506 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
507 if (device) break;
508 // if SCO device is requested but no SCO device is available, fall back to default case
509 // FALL THROUGH
510
511 default: // FORCE_NONE
512 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
513 if (mHasA2dp && !isInCall() &&
514 (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
515 (getA2dpOutput() != 0) && !mA2dpSuspended) {
516 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
517 if (device) break;
518 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
519 if (device) break;
520 }
521 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
522 if (device) break;
523 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
524 if (device) break;
525 if (mPhoneState != AudioSystem::MODE_IN_CALL) {
526 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
527 if (device) break;
528 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
529 if (device) break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700530 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
531 if (device) break;
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700532 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
533 if (device) break;
534 }
535
536 // Allow voice call on USB ANLG DOCK headset
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700537 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
538 if (device) break;
539
540 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_EARPIECE;
541 if (device) break;
542 device = mDefaultOutputDevice;
543 if (device == AUDIO_DEVICE_NONE) {
544 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE");
545 }
546 break;
547
548 case AudioSystem::FORCE_SPEAKER:
549 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
550 // A2DP speaker when forcing to speaker output
551 if (mHasA2dp && !isInCall() &&
552 (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
553 (getA2dpOutput() != 0) && !mA2dpSuspended) {
554 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
555 if (device) break;
556 }
557 if (mPhoneState != AudioSystem::MODE_IN_CALL) {
558 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
559 if (device) break;
560 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
561 if (device) break;
562 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
563 if (device) break;
564 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
565 if (device) break;
Helen Zengbbce4952013-12-16 20:26:46 -0800566 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
567 if (device) break;
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700568 }
569 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
570 if (device) break;
571 device = mDefaultOutputDevice;
572 if (device == AUDIO_DEVICE_NONE) {
573 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER");
574 }
575 break;
576 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700577 // FIXME: Why do need to replace with speaker? If voice call is active
578 // We should use device from STRATEGY_PHONE
579#ifdef AUDIO_EXTN_FM_ENABLED
580 if (mAvailableOutputDevices & AUDIO_DEVICE_OUT_FM) {
581 if (mForceUse[AudioSystem::FOR_MEDIA] == AudioSystem::FORCE_SPEAKER) {
582 device = AUDIO_DEVICE_OUT_SPEAKER;
583 }
584 }
585#endif
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700586 break;
587
588 case STRATEGY_SONIFICATION:
589
590 // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
591 // handleIncallSonification().
592 if (isInCall()) {
593 device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
594 break;
595 }
596 // FALL THROUGH
597
598 case STRATEGY_ENFORCED_AUDIBLE:
599 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
600 // except:
601 // - when in call where it doesn't default to STRATEGY_PHONE behavior
602 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
603
604 if ((strategy == STRATEGY_SONIFICATION) ||
605 (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_SYSTEM_ENFORCED)) {
606 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
607 if (device == AUDIO_DEVICE_NONE) {
608 ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION");
609 }
610 }
611 // The second device used for sonification is the same as the device used by media strategy
612 // FALL THROUGH
613
614 case STRATEGY_MEDIA: {
615 uint32_t device2 = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700616
Mingming Yin4a72d652014-01-03 18:54:18 -0800617 if (isInCall() && (device == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700618 // when in call, get the device for Phone strategy
619 device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
620 break;
621 }
622#ifdef AUDIO_EXTN_FM_ENABLED
623 if (mForceUse[AudioSystem::FOR_MEDIA] == AudioSystem::FORCE_SPEAKER) {
624 device = AUDIO_DEVICE_OUT_SPEAKER;
625 break;
626 }
627#endif
628
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700629 if (strategy != STRATEGY_SONIFICATION) {
630 // no sonification on remote submix (e.g. WFD)
631 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
632 }
633 if ((device2 == AUDIO_DEVICE_NONE) &&
634 mHasA2dp && (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
635 (getA2dpOutput() != 0) && !mA2dpSuspended) {
636 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
637 if (device2 == AUDIO_DEVICE_NONE) {
638 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
639 }
640 if (device2 == AUDIO_DEVICE_NONE) {
641 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
642 }
643 }
644 if (device2 == AUDIO_DEVICE_NONE) {
645 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
646 }
647 if (device2 == AUDIO_DEVICE_NONE) {
648 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
649 }
650 if (device2 == AUDIO_DEVICE_NONE) {
651 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
652 }
653 if (device2 == AUDIO_DEVICE_NONE) {
654 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
655 }
656 if (device2 == AUDIO_DEVICE_NONE) {
657 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
658 }
Apoorv Raghuvanshi7566f8b2014-01-17 13:10:09 -0800659 if ((strategy != STRATEGY_SONIFICATION) && (device == AUDIO_DEVICE_NONE)
660 && (device2 == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700661 // no sonification on aux digital (e.g. HDMI)
662 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
663 }
664 if ((device2 == AUDIO_DEVICE_NONE) &&
Krishnankutty Kolathappilly4b7fdaa2013-12-16 00:40:11 -0800665 (mForceUse[AudioSystem::FOR_DOCK] == AudioSystem::FORCE_ANALOG_DOCK)
666 && (strategy != STRATEGY_SONIFICATION)) {
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700667 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
668 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700669#ifdef AUDIO_EXTN_FM_ENABLED
Apoorv Raghuvanshi7566f8b2014-01-17 13:10:09 -0800670 if ((strategy != STRATEGY_SONIFICATION) && (device == AUDIO_DEVICE_NONE)
671 && (device2 == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700672 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_FM_TX;
673 }
674#endif
675#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Apoorv Raghuvanshi7566f8b2014-01-17 13:10:09 -0800676 if ((strategy != STRATEGY_SONIFICATION) && (device == AUDIO_DEVICE_NONE)
677 && (device2 == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700678 // no sonification on WFD sink
679 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_PROXY;
680 }
681#endif
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700682 if (device2 == AUDIO_DEVICE_NONE) {
683 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
684 }
685
686 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
687 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
688 device |= device2;
689 if (device) break;
690 device = mDefaultOutputDevice;
691 if (device == AUDIO_DEVICE_NONE) {
692 ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA");
693 }
694 } break;
695
696 default:
697 ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
698 break;
699 }
700
701 ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
702 return device;
703}
704
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700705audio_devices_t AudioPolicyManager::getDeviceForInputSource(int inputSource)
706{
707 uint32_t device = AUDIO_DEVICE_NONE;
708
709 switch (inputSource) {
710 case AUDIO_SOURCE_VOICE_UPLINK:
711 if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
712 device = AUDIO_DEVICE_IN_VOICE_CALL;
713 break;
714 }
715 // FALL THROUGH
716
717 case AUDIO_SOURCE_DEFAULT:
718 case AUDIO_SOURCE_MIC:
719 case AUDIO_SOURCE_VOICE_RECOGNITION:
720 case AUDIO_SOURCE_HOTWORD:
721 case AUDIO_SOURCE_VOICE_COMMUNICATION:
722 if (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO &&
723 mAvailableInputDevices & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
724 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
725 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_WIRED_HEADSET) {
726 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
727 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET) {
728 device = AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET;
729 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
730 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
731 }
732 break;
733 case AUDIO_SOURCE_CAMCORDER:
734 if (mAvailableInputDevices & AUDIO_DEVICE_IN_BACK_MIC) {
735 device = AUDIO_DEVICE_IN_BACK_MIC;
736 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
737 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
738 }
739 break;
740 case AUDIO_SOURCE_VOICE_DOWNLINK:
741 case AUDIO_SOURCE_VOICE_CALL:
742 if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
743 device = AUDIO_DEVICE_IN_VOICE_CALL;
744 }
745 break;
746 case AUDIO_SOURCE_REMOTE_SUBMIX:
747 if (mAvailableInputDevices & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
748 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
749 }
750 break;
751#ifdef AUDIO_EXTN_FM_ENABLED
752 case AUDIO_SOURCE_FM_RX:
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700753 device = AUDIO_DEVICE_IN_FM_RX;
754 break;
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700755 case AUDIO_SOURCE_FM_RX_A2DP:
756 device = AUDIO_DEVICE_IN_FM_RX_A2DP;
757 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700758#endif
759 default:
760 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
761 break;
762 }
763 ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
764 return device;
765}
766
767AudioPolicyManager::device_category AudioPolicyManager::getDeviceCategory(audio_devices_t device)
768{
769 switch(getDeviceForVolume(device)) {
770 case AUDIO_DEVICE_OUT_EARPIECE:
771 return DEVICE_CATEGORY_EARPIECE;
772 case AUDIO_DEVICE_OUT_WIRED_HEADSET:
773 case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
774 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
775 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
776 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
777 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
778#ifdef AUDIO_EXTN_FM_ENABLED
779 case AUDIO_DEVICE_OUT_FM:
780#endif
781 return DEVICE_CATEGORY_HEADSET;
782 case AUDIO_DEVICE_OUT_SPEAKER:
783 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
784 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
785 case AUDIO_DEVICE_OUT_AUX_DIGITAL:
786 case AUDIO_DEVICE_OUT_USB_ACCESSORY:
787 case AUDIO_DEVICE_OUT_USB_DEVICE:
788 case AUDIO_DEVICE_OUT_REMOTE_SUBMIX:
789#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
790 case AUDIO_DEVICE_OUT_PROXY:
791#endif
792 default:
793 return DEVICE_CATEGORY_SPEAKER;
794 }
795}
796
797status_t AudioPolicyManager::checkAndSetVolume(int stream,
798 int index,
799 audio_io_handle_t output,
800 audio_devices_t device,
801 int delayMs,
802 bool force)
803{
804 ALOGV("checkAndSetVolume: index %d output %d device %x", index, output, device);
805 // do not change actual stream volume if the stream is muted
806 if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
807 ALOGVV("checkAndSetVolume() stream %d muted count %d",
808 stream, mOutputs.valueFor(output)->mMuteCount[stream]);
809 return NO_ERROR;
810 }
811
812 // do not change in call volume if bluetooth is connected and vice versa
813 if ((stream == AudioSystem::VOICE_CALL && mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
814 (stream == AudioSystem::BLUETOOTH_SCO && mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO)) {
815 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
816 stream, mForceUse[AudioSystem::FOR_COMMUNICATION]);
817 return INVALID_OPERATION;
818 }
819
820 float volume = computeVolume(stream, index, output, device);
821 // We actually change the volume if:
822 // - the float value returned by computeVolume() changed
823 // - the force flag is set
824 if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
825 force) {
826 mOutputs.valueFor(output)->mCurVolume[stream] = volume;
827 ALOGV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
828 // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
829 // enabled
830 if (stream == AudioSystem::BLUETOOTH_SCO) {
831 mpClientInterface->setStreamVolume(AudioSystem::VOICE_CALL, volume, output, delayMs);
832#ifdef AUDIO_EXTN_FM_ENABLED
833 } else if (stream == AudioSystem::MUSIC &&
834 output == mPrimaryOutput) {
835 float fmVolume = -1.0;
836 fmVolume = computeVolume(stream, index, output, device);
837 if (fmVolume >= 0) {
838 AudioParameter param = AudioParameter();
839 param.addFloat(String8("fm_volume"), fmVolume);
840 ALOGV("checkAndSetVolume setParameters fm_volume, volume=:%f delay=:%d",fmVolume,delayMs*2);
841 //Double delayMs to avoid sound burst while device switch.
842 mpClientInterface->setParameters(mPrimaryOutput, param.toString(), delayMs*2);
843 }
844#endif
845 }
846 mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs);
847 }
848
849 if (stream == AudioSystem::VOICE_CALL ||
850 stream == AudioSystem::BLUETOOTH_SCO) {
851 float voiceVolume;
852
853 voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
854
855 // Force voice volume to max when Vgs is set for bluetooth SCO as volume is managed by the headset
856 if (stream == AudioSystem::BLUETOOTH_SCO) {
857 String8 key ("bt_headset_vgs");
858 mpClientInterface->getParameters(output,key);
859 AudioParameter result(mpClientInterface->getParameters(0,key));
860 int value;
861 if (result.getInt(String8("isVGS"),value) == NO_ERROR) {
862 ALOGV("Use BT-SCO Voice Volume");
863 voiceVolume = 1.0;
864 }
865 }
866
867 if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) {
868 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
869 mLastVoiceVolume = voiceVolume;
870 }
871 }
872
873 return NO_ERROR;
874}
875
876
877float AudioPolicyManager::computeVolume(int stream,
878 int index,
879 audio_io_handle_t output,
880 audio_devices_t device)
881{
882 float volume = 1.0;
883 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
884
885 if (device == AUDIO_DEVICE_NONE) {
886 device = outputDesc->device();
887 }
888
889 // if volume is not 0 (not muted), force media volume to max on digital output
890 if (stream == AudioSystem::MUSIC &&
891 index != mStreams[stream].mIndexMin &&
892 (device == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
893 device == AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET ||
894 device == AUDIO_DEVICE_OUT_USB_ACCESSORY ||
895#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
896 device == AUDIO_DEVICE_OUT_PROXY ||
897#endif
898 device == AUDIO_DEVICE_OUT_USB_DEVICE )) {
899 return 1.0;
900 }
901#ifdef AUDIO_EXTN_INCALL_MUSIC_ENABLED
902 if (stream == AudioSystem::INCALL_MUSIC) {
903 return 1.0;
904 }
905#endif
906 return AudioPolicyManagerBase::computeVolume(stream, index, output, device);
907}
Naresh Tanniru36c08932014-01-27 18:40:53 +0530908
909
910audio_io_handle_t AudioPolicyManager::getOutput(AudioSystem::stream_type stream,
911 uint32_t samplingRate,
912 uint32_t format,
913 uint32_t channelMask,
914 AudioSystem::output_flags flags,
915 const audio_offload_info_t *offloadInfo)
916{
917 audio_io_handle_t output = 0;
918 uint32_t latency = 0;
919 routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);
920 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
921 IOProfile *profile = NULL;
922
923#ifdef VOICE_CONCURRENCY
924 if (isInCall()) {
925 ALOGV(" IN call mode adding ULL flags .. flags: %x ", flags );
926 //For voip paths
927 if(flags & AudioSystem::OUTPUT_FLAG_DIRECT)
928 flags = AudioSystem::OUTPUT_FLAG_DIRECT;
929 else //route every thing else to ULL path
930 flags = (AudioSystem::output_flags)AUDIO_OUTPUT_FLAG_FAST;
931 }
932#endif
933
934#ifdef WFD_CONCURRENCY
935 if ((mAvailableOutputDevices & AUDIO_DEVICE_OUT_PROXY)
936 && (stream != AudioSystem::MUSIC)) {
937 ALOGV(" WFD mode adding ULL flags for non music stream.. flags: %x ", flags );
938 //For voip paths
939 if(flags & AudioSystem::OUTPUT_FLAG_DIRECT)
940 flags = AudioSystem::OUTPUT_FLAG_DIRECT;
941 else //route every thing else to ULL path
942 flags = (AudioSystem::output_flags)AUDIO_OUTPUT_FLAG_FAST;
943 }
944#endif
945
946 ALOGV(" getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x ",
947 device, stream, samplingRate, format, channelMask, flags);
948
949
950
951#ifdef AUDIO_POLICY_TEST
952 if (mCurOutput != 0) {
953 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
954 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
955
956 if (mTestOutputs[mCurOutput] == 0) {
957 ALOGV("getOutput() opening test output");
958 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL);
959 outputDesc->mDevice = mTestDevice;
960 outputDesc->mSamplingRate = mTestSamplingRate;
961 outputDesc->mFormat = mTestFormat;
962 outputDesc->mChannelMask = mTestChannels;
963 outputDesc->mLatency = mTestLatencyMs;
964 outputDesc->mFlags = (audio_output_flags_t)(mDirectOutput ? AudioSystem::OUTPUT_FLAG_DIRECT : 0);
965 outputDesc->mRefCount[stream] = 0;
966 mTestOutputs[mCurOutput] = mpClientInterface->openOutput(0, &outputDesc->mDevice,
967 &outputDesc->mSamplingRate,
968 &outputDesc->mFormat,
969 &outputDesc->mChannelMask,
970 &outputDesc->mLatency,
971 outputDesc->mFlags,
972 offloadInfo);
973 if (mTestOutputs[mCurOutput]) {
974 AudioParameter outputCmd = AudioParameter();
975 outputCmd.addInt(String8("set_id"),mCurOutput);
976 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
977 addOutput(mTestOutputs[mCurOutput], outputDesc);
978 }
979 }
980 return mTestOutputs[mCurOutput];
981 }
982#endif //AUDIO_POLICY_TEST
983
984 // open a direct output if required by specified parameters
985 //force direct flag if offload flag is set: offloading implies a direct output stream
986 // and all common behaviors are driven by checking only the direct flag
987 // this should normally be set appropriately in the policy configuration file
988 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
989 flags = (AudioSystem::output_flags)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
990 }
991
992 if ((format == AudioSystem::PCM_16_BIT) &&(AudioSystem::popCount(channelMask) > 2)) {
993 ALOGV("owerwrite flag(%x) for PCM16 multi-channel(CM:%x) playback", flags ,channelMask);
994 flags = (AudioSystem::output_flags)AUDIO_OUTPUT_FLAG_DIRECT;
995 }
996
997 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
998 // creating an offloaded track and tearing it down immediately after start when audioflinger
999 // detects there is an active non offloadable effect.
1000 // FIXME: We should check the audio session here but we do not have it in this context.
1001 // This may prevent offloading in rare situations where effects are left active by apps
1002 // in the background.
1003 if ((((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
1004 !isNonOffloadableEffectEnabled()) &&
1005 flags & AUDIO_OUTPUT_FLAG_DIRECT) {
1006 profile = getProfileForDirectOutput(device,
1007 samplingRate,
1008 format,
1009 channelMask,
1010 (audio_output_flags_t)flags);
1011 }
1012
1013 if (profile != NULL) {
1014 AudioOutputDescriptor *outputDesc = NULL;
1015
1016 for (size_t i = 0; i < mOutputs.size(); i++) {
1017 AudioOutputDescriptor *desc = mOutputs.valueAt(i);
1018 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1019 outputDesc = desc;
1020 // reuse direct output if currently open and configured with same parameters
1021 if ((samplingRate == outputDesc->mSamplingRate) &&
1022 (format == outputDesc->mFormat) &&
1023 (channelMask == outputDesc->mChannelMask)) {
1024 outputDesc->mDirectOpenCount++;
1025 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1026 return mOutputs.keyAt(i);
1027 }
1028 }
1029 }
1030 // close direct output if currently open and configured with different parameters
1031 if (outputDesc != NULL) {
1032 closeOutput(outputDesc->mId);
1033 }
1034 outputDesc = new AudioOutputDescriptor(profile);
1035 outputDesc->mDevice = device;
1036 outputDesc->mSamplingRate = samplingRate;
1037 outputDesc->mFormat = (audio_format_t)format;
1038 outputDesc->mChannelMask = (audio_channel_mask_t)channelMask;
1039 outputDesc->mLatency = 0;
1040 outputDesc->mFlags =(audio_output_flags_t) (outputDesc->mFlags | flags);
1041 outputDesc->mRefCount[stream] = 0;
1042 outputDesc->mStopTime[stream] = 0;
1043 outputDesc->mDirectOpenCount = 1;
1044 output = mpClientInterface->openOutput(profile->mModule->mHandle,
1045 &outputDesc->mDevice,
1046 &outputDesc->mSamplingRate,
1047 &outputDesc->mFormat,
1048 &outputDesc->mChannelMask,
1049 &outputDesc->mLatency,
1050 outputDesc->mFlags,
1051 offloadInfo);
1052
1053 // only accept an output with the requested parameters
1054 if (output == 0 ||
1055 (samplingRate != 0 && samplingRate != outputDesc->mSamplingRate) ||
1056 (format != 0 && format != outputDesc->mFormat) ||
1057 (channelMask != 0 && channelMask != outputDesc->mChannelMask)) {
1058 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1059 "format %d %d, channelMask %04x %04x", output, samplingRate,
1060 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1061 outputDesc->mChannelMask);
1062 if (output != 0) {
1063 mpClientInterface->closeOutput(output);
1064 }
1065 delete outputDesc;
1066 return 0;
1067 }
1068 audio_io_handle_t srcOutput = getOutputForEffect();
1069 addOutput(output, outputDesc);
1070 audio_io_handle_t dstOutput = getOutputForEffect();
1071 if (dstOutput == output) {
1072 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1073 }
1074 mPreviousOutputs = mOutputs;
1075 ALOGV("getOutput() returns new direct output %d", output);
1076 return output;
1077 }
1078
1079 // ignoring channel mask due to downmix capability in mixer
1080
1081 // open a non direct output
1082
1083 // for non direct outputs, only PCM is supported
1084 if (audio_is_linear_pcm((audio_format_t)format)) {
1085 // get which output is suitable for the specified stream. The actual
1086 // routing change will happen when startOutput() will be called
1087 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1088
1089 output = selectOutput(outputs, flags);
1090 }
1091 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1092 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1093
1094 ALOGV("getOutput() returns output %d", output);
1095
1096 return output;
1097}
1098
1099
1100// This function checks for the parameters which can be offloaded.
1101// This can be enhanced depending on the capability of the DSP and policy
1102// of the system.
1103bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
1104{
1105 ALOGV(" isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
1106 " BitRate=%u, duration=%lld us, has_video=%d",
1107 offloadInfo.sample_rate, offloadInfo.channel_mask,
1108 offloadInfo.format,
1109 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
1110 offloadInfo.has_video);
1111
1112#ifdef VOICE_CONCURRENCY
1113 if(isInCall())
1114 {
1115 ALOGD("\n blocking compress offload on call mode\n");
1116 return false;
1117 }
1118#endif
Naresh Tanniru36c08932014-01-27 18:40:53 +05301119 // Check if stream type is music, then only allow offload as of now.
1120 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
1121 {
1122 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
1123 return false;
1124 }
1125
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001126 char propValue[PROPERTY_VALUE_MAX];
1127 bool pcmOffload = false;
1128 if (audio_is_offload_pcm(offloadInfo.format)) {
1129 if(property_get("audio.offload.pcm.enable", propValue, NULL)) {
Naresh Tanniru36c08932014-01-27 18:40:53 +05301130 bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001131 if (prop_enabled) {
1132 ALOGW("PCM offload property is enabled");
1133 pcmOffload = true;
Naresh Tanniru36c08932014-01-27 18:40:53 +05301134 }
1135 }
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001136 if (!pcmOffload) {
1137 ALOGV("PCM offload disabled by property audio.offload.pcm.enable");
1138 return false;
1139 }
1140 }
1141
1142 if (!pcmOffload) {
1143 // Check if offload has been disabled
1144 if (property_get("audio.offload.disable", propValue, "0")) {
1145 if (atoi(propValue) != 0) {
1146 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
1147 return false;
Naresh Tanniru36c08932014-01-27 18:40:53 +05301148 }
1149 }
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001150
1151 //check if it's multi-channel AAC format
1152 if (AudioSystem::popCount(offloadInfo.channel_mask) > 2
1153 && offloadInfo.format == AUDIO_FORMAT_AAC) {
1154 ALOGV("offload disabled for multi-channel AAC format");
1155 return false;
1156 }
1157
1158 if (offloadInfo.has_video)
1159 {
1160 if(property_get("av.offload.enable", propValue, NULL)) {
1161 bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1162 if (!prop_enabled) {
1163 ALOGW("offload disabled by av.offload.enable = %s ", propValue );
1164 return false;
1165 }
1166 } else {
1167 return false;
1168 }
1169
1170 if(offloadInfo.is_streaming) {
1171 if (property_get("av.streaming.offload.enable", propValue, NULL)) {
1172 bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1173 if (!prop_enabled) {
1174 ALOGW("offload disabled by av.streaming.offload.enable = %s ", propValue );
1175 return false;
1176 }
1177 } else {
1178 //Do not offload AV streamnig if the property is not defined
1179 return false;
1180 }
1181 }
1182 ALOGV("isOffloadSupported: has_video == true, property\
1183 set to enable offload");
1184 }
Naresh Tanniru36c08932014-01-27 18:40:53 +05301185 }
1186
1187 //If duration is less than minimum value defined in property, return false
1188 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
1189 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
1190 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
1191 return false;
1192 }
1193 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
1194 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
1195 //duration checks only valid for MP3/AAC formats,
1196 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001197 if (offloadInfo.format == AUDIO_FORMAT_MP3 || offloadInfo.format == AUDIO_FORMAT_AAC || pcmOffload)
Naresh Tanniru36c08932014-01-27 18:40:53 +05301198 return false;
1199 }
1200
1201 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1202 // creating an offloaded track and tearing it down immediately after start when audioflinger
1203 // detects there is an active non offloadable effect.
1204 // FIXME: We should check the audio session here but we do not have it in this context.
1205 // This may prevent offloading in rare situations where effects are left active by apps
1206 // in the background.
1207 if (isNonOffloadableEffectEnabled()) {
1208 return false;
1209 }
1210
1211 // See if there is a profile to support this.
1212 // AUDIO_DEVICE_NONE
1213 IOProfile *profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
1214 offloadInfo.sample_rate,
1215 offloadInfo.format,
1216 offloadInfo.channel_mask,
1217 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1218 ALOGV("isOffloadSupported() profile %sfound", profile != NULL ? "" : "NOT ");
1219 return (profile != NULL);
1220}
1221
1222void AudioPolicyManager::setPhoneState(int state)
1223
1224{
1225 ALOGV("setPhoneState() state %d", state);
1226 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
1227 if (state < 0 || state >= AudioSystem::NUM_MODES) {
1228 ALOGW("setPhoneState() invalid state %d", state);
1229 return;
1230 }
1231
1232 if (state == mPhoneState ) {
1233 ALOGW("setPhoneState() setting same state %d", state);
1234 return;
1235 }
1236
1237 // if leaving call state, handle special case of active streams
1238 // pertaining to sonification strategy see handleIncallSonification()
1239 if (isInCall()) {
1240 ALOGV("setPhoneState() in call state management: new state is %d", state);
1241 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
1242 handleIncallSonification(stream, false, true);
1243 }
1244 }
1245
1246 // store previous phone state for management of sonification strategy below
1247 int oldState = mPhoneState;
1248 mPhoneState = state;
1249 bool force = false;
1250
1251 // are we entering or starting a call
1252 if (!isStateInCall(oldState) && isStateInCall(state)) {
1253 ALOGV(" Entering call in setPhoneState()");
1254 // force routing command to audio hardware when starting a call
1255 // even if no device change is needed
1256 force = true;
1257 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
1258 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
1259 sVolumeProfiles[AUDIO_STREAM_VOICE_CALL][j];
1260 }
1261 } else if (isStateInCall(oldState) && !isStateInCall(state)) {
1262 ALOGV(" Exiting call in setPhoneState()");
1263 // force routing command to audio hardware when exiting a call
1264 // even if no device change is needed
1265 force = true;
1266 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
1267 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
1268 sVolumeProfiles[AUDIO_STREAM_DTMF][j];
1269 }
1270 } else if (isStateInCall(state) && (state != oldState)) {
1271 ALOGV(" Switching between telephony and VoIP in setPhoneState()");
1272 // force routing command to audio hardware when switching between telephony and VoIP
1273 // even if no device change is needed
1274 force = true;
1275 }
1276
1277 // check for device and output changes triggered by new phone state
1278 newDevice = getNewDevice(mPrimaryOutput, false /*fromCache*/);
1279 checkA2dpSuspend();
1280 checkOutputForAllStrategies();
1281 updateDevicesAndOutputs();
1282
1283 AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
1284
1285 // force routing command to audio hardware when ending call
1286 // even if no device change is needed
1287 if (isStateInCall(oldState) && newDevice == AUDIO_DEVICE_NONE) {
1288 newDevice = hwOutputDesc->device();
1289 }
1290
1291 int delayMs = 0;
1292 if (isStateInCall(state)) {
1293 nsecs_t sysTime = systemTime();
1294 for (size_t i = 0; i < mOutputs.size(); i++) {
1295 AudioOutputDescriptor *desc = mOutputs.valueAt(i);
1296 // mute media and sonification strategies and delay device switch by the largest
1297 // latency of any output where either strategy is active.
1298 // This avoid sending the ring tone or music tail into the earpiece or headset.
1299 if ((desc->isStrategyActive(STRATEGY_MEDIA,
1300 SONIFICATION_HEADSET_MUSIC_DELAY,
1301 sysTime) ||
1302 desc->isStrategyActive(STRATEGY_SONIFICATION,
1303 SONIFICATION_HEADSET_MUSIC_DELAY,
1304 sysTime)) &&
1305 (delayMs < (int)desc->mLatency*2)) {
1306 delayMs = desc->mLatency*2;
1307 }
1308 setStrategyMute(STRATEGY_MEDIA, true, mOutputs.keyAt(i));
1309 setStrategyMute(STRATEGY_MEDIA, false, mOutputs.keyAt(i), MUTE_TIME_MS,
1310 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
1311 setStrategyMute(STRATEGY_SONIFICATION, true, mOutputs.keyAt(i));
1312 setStrategyMute(STRATEGY_SONIFICATION, false, mOutputs.keyAt(i), MUTE_TIME_MS,
1313 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
1314 }
1315 }
1316
1317 // change routing is necessary
1318 setOutputDevice(mPrimaryOutput, newDevice, force, delayMs);
1319
1320 // if entering in call state, handle special case of active streams
1321 // pertaining to sonification strategy see handleIncallSonification()
1322 if (isStateInCall(state)) {
1323 ALOGV("setPhoneState() in call state management: new state is %d", state);
1324 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
1325 handleIncallSonification(stream, true, true);
1326 }
1327 }
1328
1329 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
1330 if (state == AudioSystem::MODE_RINGTONE &&
1331 isStreamActive(AudioSystem::MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
1332 mLimitRingtoneVolume = true;
1333 } else {
1334 mLimitRingtoneVolume = false;
1335 }
1336
1337#ifdef VOICE_CONCURRENCY
1338 //Call invalidate to reset all opened non ULL audio tracks
1339 if(isInCall())
1340 {
1341 // Move tracks associated to this strategy from previous output to new output
1342 for (int i = AudioSystem::SYSTEM; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
1343 ALOGV("\n Invalidate on call mode for stream :: %d \n", i);
1344 //FIXME see fixme on name change
1345 mpClientInterface->setStreamOutput((AudioSystem::stream_type)i,
1346 0 /* ignored */);
1347 }
1348 }
1349#endif
1350
1351}
1352
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001353extern "C" AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001354{
1355 return new AudioPolicyManager(clientInterface);
1356}
1357
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001358extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001359{
1360 delete interface;
1361}
1362
1363}; // namespace android