blob: cb810c840de97867394182f1cfe82258cea1c874 [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 }
659 if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) {
660 // no sonification on aux digital (e.g. HDMI)
661 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
662 }
663 if ((device2 == AUDIO_DEVICE_NONE) &&
Krishnankutty Kolathappilly4b7fdaa2013-12-16 00:40:11 -0800664 (mForceUse[AudioSystem::FOR_DOCK] == AudioSystem::FORCE_ANALOG_DOCK)
665 && (strategy != STRATEGY_SONIFICATION)) {
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700666 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
667 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700668#ifdef AUDIO_EXTN_FM_ENABLED
669 if ((strategy != STRATEGY_SONIFICATION) && (device2 == AUDIO_DEVICE_NONE)) {
670 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_FM_TX;
671 }
672#endif
673#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
674 if ((strategy != STRATEGY_SONIFICATION) && (device2 == AUDIO_DEVICE_NONE)) {
675 // no sonification on WFD sink
676 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_PROXY;
677 }
678#endif
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700679 if (device2 == AUDIO_DEVICE_NONE) {
680 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
681 }
682
683 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
684 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
685 device |= device2;
686 if (device) break;
687 device = mDefaultOutputDevice;
688 if (device == AUDIO_DEVICE_NONE) {
689 ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA");
690 }
691 } break;
692
693 default:
694 ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
695 break;
696 }
697
698 ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
699 return device;
700}
701
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700702audio_devices_t AudioPolicyManager::getDeviceForInputSource(int inputSource)
703{
704 uint32_t device = AUDIO_DEVICE_NONE;
705
706 switch (inputSource) {
707 case AUDIO_SOURCE_VOICE_UPLINK:
708 if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
709 device = AUDIO_DEVICE_IN_VOICE_CALL;
710 break;
711 }
712 // FALL THROUGH
713
714 case AUDIO_SOURCE_DEFAULT:
715 case AUDIO_SOURCE_MIC:
716 case AUDIO_SOURCE_VOICE_RECOGNITION:
717 case AUDIO_SOURCE_HOTWORD:
718 case AUDIO_SOURCE_VOICE_COMMUNICATION:
719 if (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO &&
720 mAvailableInputDevices & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
721 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
722 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_WIRED_HEADSET) {
723 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
724 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET) {
725 device = AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET;
726 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
727 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
728 }
729 break;
730 case AUDIO_SOURCE_CAMCORDER:
731 if (mAvailableInputDevices & AUDIO_DEVICE_IN_BACK_MIC) {
732 device = AUDIO_DEVICE_IN_BACK_MIC;
733 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
734 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
735 }
736 break;
737 case AUDIO_SOURCE_VOICE_DOWNLINK:
738 case AUDIO_SOURCE_VOICE_CALL:
739 if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
740 device = AUDIO_DEVICE_IN_VOICE_CALL;
741 }
742 break;
743 case AUDIO_SOURCE_REMOTE_SUBMIX:
744 if (mAvailableInputDevices & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
745 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
746 }
747 break;
748#ifdef AUDIO_EXTN_FM_ENABLED
749 case AUDIO_SOURCE_FM_RX:
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700750 device = AUDIO_DEVICE_IN_FM_RX;
751 break;
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700752 case AUDIO_SOURCE_FM_RX_A2DP:
753 device = AUDIO_DEVICE_IN_FM_RX_A2DP;
754 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700755#endif
756 default:
757 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
758 break;
759 }
760 ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
761 return device;
762}
763
764AudioPolicyManager::device_category AudioPolicyManager::getDeviceCategory(audio_devices_t device)
765{
766 switch(getDeviceForVolume(device)) {
767 case AUDIO_DEVICE_OUT_EARPIECE:
768 return DEVICE_CATEGORY_EARPIECE;
769 case AUDIO_DEVICE_OUT_WIRED_HEADSET:
770 case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
771 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
772 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
773 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
774 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
775#ifdef AUDIO_EXTN_FM_ENABLED
776 case AUDIO_DEVICE_OUT_FM:
777#endif
778 return DEVICE_CATEGORY_HEADSET;
779 case AUDIO_DEVICE_OUT_SPEAKER:
780 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
781 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
782 case AUDIO_DEVICE_OUT_AUX_DIGITAL:
783 case AUDIO_DEVICE_OUT_USB_ACCESSORY:
784 case AUDIO_DEVICE_OUT_USB_DEVICE:
785 case AUDIO_DEVICE_OUT_REMOTE_SUBMIX:
786#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
787 case AUDIO_DEVICE_OUT_PROXY:
788#endif
789 default:
790 return DEVICE_CATEGORY_SPEAKER;
791 }
792}
793
794status_t AudioPolicyManager::checkAndSetVolume(int stream,
795 int index,
796 audio_io_handle_t output,
797 audio_devices_t device,
798 int delayMs,
799 bool force)
800{
801 ALOGV("checkAndSetVolume: index %d output %d device %x", index, output, device);
802 // do not change actual stream volume if the stream is muted
803 if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
804 ALOGVV("checkAndSetVolume() stream %d muted count %d",
805 stream, mOutputs.valueFor(output)->mMuteCount[stream]);
806 return NO_ERROR;
807 }
808
809 // do not change in call volume if bluetooth is connected and vice versa
810 if ((stream == AudioSystem::VOICE_CALL && mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
811 (stream == AudioSystem::BLUETOOTH_SCO && mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO)) {
812 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
813 stream, mForceUse[AudioSystem::FOR_COMMUNICATION]);
814 return INVALID_OPERATION;
815 }
816
817 float volume = computeVolume(stream, index, output, device);
818 // We actually change the volume if:
819 // - the float value returned by computeVolume() changed
820 // - the force flag is set
821 if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
822 force) {
823 mOutputs.valueFor(output)->mCurVolume[stream] = volume;
824 ALOGV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
825 // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
826 // enabled
827 if (stream == AudioSystem::BLUETOOTH_SCO) {
828 mpClientInterface->setStreamVolume(AudioSystem::VOICE_CALL, volume, output, delayMs);
829#ifdef AUDIO_EXTN_FM_ENABLED
830 } else if (stream == AudioSystem::MUSIC &&
831 output == mPrimaryOutput) {
832 float fmVolume = -1.0;
833 fmVolume = computeVolume(stream, index, output, device);
834 if (fmVolume >= 0) {
835 AudioParameter param = AudioParameter();
836 param.addFloat(String8("fm_volume"), fmVolume);
837 ALOGV("checkAndSetVolume setParameters fm_volume, volume=:%f delay=:%d",fmVolume,delayMs*2);
838 //Double delayMs to avoid sound burst while device switch.
839 mpClientInterface->setParameters(mPrimaryOutput, param.toString(), delayMs*2);
840 }
841#endif
842 }
843 mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs);
844 }
845
846 if (stream == AudioSystem::VOICE_CALL ||
847 stream == AudioSystem::BLUETOOTH_SCO) {
848 float voiceVolume;
849
850 voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
851
852 // Force voice volume to max when Vgs is set for bluetooth SCO as volume is managed by the headset
853 if (stream == AudioSystem::BLUETOOTH_SCO) {
854 String8 key ("bt_headset_vgs");
855 mpClientInterface->getParameters(output,key);
856 AudioParameter result(mpClientInterface->getParameters(0,key));
857 int value;
858 if (result.getInt(String8("isVGS"),value) == NO_ERROR) {
859 ALOGV("Use BT-SCO Voice Volume");
860 voiceVolume = 1.0;
861 }
862 }
863
864 if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) {
865 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
866 mLastVoiceVolume = voiceVolume;
867 }
868 }
869
870 return NO_ERROR;
871}
872
873
874float AudioPolicyManager::computeVolume(int stream,
875 int index,
876 audio_io_handle_t output,
877 audio_devices_t device)
878{
879 float volume = 1.0;
880 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
881
882 if (device == AUDIO_DEVICE_NONE) {
883 device = outputDesc->device();
884 }
885
886 // if volume is not 0 (not muted), force media volume to max on digital output
887 if (stream == AudioSystem::MUSIC &&
888 index != mStreams[stream].mIndexMin &&
889 (device == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
890 device == AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET ||
891 device == AUDIO_DEVICE_OUT_USB_ACCESSORY ||
892#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
893 device == AUDIO_DEVICE_OUT_PROXY ||
894#endif
895 device == AUDIO_DEVICE_OUT_USB_DEVICE )) {
896 return 1.0;
897 }
898#ifdef AUDIO_EXTN_INCALL_MUSIC_ENABLED
899 if (stream == AudioSystem::INCALL_MUSIC) {
900 return 1.0;
901 }
902#endif
903 return AudioPolicyManagerBase::computeVolume(stream, index, output, device);
904}
Naresh Tanniru36c08932014-01-27 18:40:53 +0530905
906
907audio_io_handle_t AudioPolicyManager::getOutput(AudioSystem::stream_type stream,
908 uint32_t samplingRate,
909 uint32_t format,
910 uint32_t channelMask,
911 AudioSystem::output_flags flags,
912 const audio_offload_info_t *offloadInfo)
913{
914 audio_io_handle_t output = 0;
915 uint32_t latency = 0;
916 routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);
917 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
918 IOProfile *profile = NULL;
919
920#ifdef VOICE_CONCURRENCY
921 if (isInCall()) {
922 ALOGV(" IN call mode adding ULL flags .. flags: %x ", flags );
923 //For voip paths
924 if(flags & AudioSystem::OUTPUT_FLAG_DIRECT)
925 flags = AudioSystem::OUTPUT_FLAG_DIRECT;
926 else //route every thing else to ULL path
927 flags = (AudioSystem::output_flags)AUDIO_OUTPUT_FLAG_FAST;
928 }
929#endif
930
931#ifdef WFD_CONCURRENCY
932 if ((mAvailableOutputDevices & AUDIO_DEVICE_OUT_PROXY)
933 && (stream != AudioSystem::MUSIC)) {
934 ALOGV(" WFD mode adding ULL flags for non music stream.. flags: %x ", flags );
935 //For voip paths
936 if(flags & AudioSystem::OUTPUT_FLAG_DIRECT)
937 flags = AudioSystem::OUTPUT_FLAG_DIRECT;
938 else //route every thing else to ULL path
939 flags = (AudioSystem::output_flags)AUDIO_OUTPUT_FLAG_FAST;
940 }
941#endif
942
943 ALOGV(" getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x ",
944 device, stream, samplingRate, format, channelMask, flags);
945
946
947
948#ifdef AUDIO_POLICY_TEST
949 if (mCurOutput != 0) {
950 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
951 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
952
953 if (mTestOutputs[mCurOutput] == 0) {
954 ALOGV("getOutput() opening test output");
955 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL);
956 outputDesc->mDevice = mTestDevice;
957 outputDesc->mSamplingRate = mTestSamplingRate;
958 outputDesc->mFormat = mTestFormat;
959 outputDesc->mChannelMask = mTestChannels;
960 outputDesc->mLatency = mTestLatencyMs;
961 outputDesc->mFlags = (audio_output_flags_t)(mDirectOutput ? AudioSystem::OUTPUT_FLAG_DIRECT : 0);
962 outputDesc->mRefCount[stream] = 0;
963 mTestOutputs[mCurOutput] = mpClientInterface->openOutput(0, &outputDesc->mDevice,
964 &outputDesc->mSamplingRate,
965 &outputDesc->mFormat,
966 &outputDesc->mChannelMask,
967 &outputDesc->mLatency,
968 outputDesc->mFlags,
969 offloadInfo);
970 if (mTestOutputs[mCurOutput]) {
971 AudioParameter outputCmd = AudioParameter();
972 outputCmd.addInt(String8("set_id"),mCurOutput);
973 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
974 addOutput(mTestOutputs[mCurOutput], outputDesc);
975 }
976 }
977 return mTestOutputs[mCurOutput];
978 }
979#endif //AUDIO_POLICY_TEST
980
981 // open a direct output if required by specified parameters
982 //force direct flag if offload flag is set: offloading implies a direct output stream
983 // and all common behaviors are driven by checking only the direct flag
984 // this should normally be set appropriately in the policy configuration file
985 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
986 flags = (AudioSystem::output_flags)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
987 }
988
989 if ((format == AudioSystem::PCM_16_BIT) &&(AudioSystem::popCount(channelMask) > 2)) {
990 ALOGV("owerwrite flag(%x) for PCM16 multi-channel(CM:%x) playback", flags ,channelMask);
991 flags = (AudioSystem::output_flags)AUDIO_OUTPUT_FLAG_DIRECT;
992 }
993
994 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
995 // creating an offloaded track and tearing it down immediately after start when audioflinger
996 // detects there is an active non offloadable effect.
997 // FIXME: We should check the audio session here but we do not have it in this context.
998 // This may prevent offloading in rare situations where effects are left active by apps
999 // in the background.
1000 if ((((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
1001 !isNonOffloadableEffectEnabled()) &&
1002 flags & AUDIO_OUTPUT_FLAG_DIRECT) {
1003 profile = getProfileForDirectOutput(device,
1004 samplingRate,
1005 format,
1006 channelMask,
1007 (audio_output_flags_t)flags);
1008 }
1009
1010 if (profile != NULL) {
1011 AudioOutputDescriptor *outputDesc = NULL;
1012
1013 for (size_t i = 0; i < mOutputs.size(); i++) {
1014 AudioOutputDescriptor *desc = mOutputs.valueAt(i);
1015 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1016 outputDesc = desc;
1017 // reuse direct output if currently open and configured with same parameters
1018 if ((samplingRate == outputDesc->mSamplingRate) &&
1019 (format == outputDesc->mFormat) &&
1020 (channelMask == outputDesc->mChannelMask)) {
1021 outputDesc->mDirectOpenCount++;
1022 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1023 return mOutputs.keyAt(i);
1024 }
1025 }
1026 }
1027 // close direct output if currently open and configured with different parameters
1028 if (outputDesc != NULL) {
1029 closeOutput(outputDesc->mId);
1030 }
1031 outputDesc = new AudioOutputDescriptor(profile);
1032 outputDesc->mDevice = device;
1033 outputDesc->mSamplingRate = samplingRate;
1034 outputDesc->mFormat = (audio_format_t)format;
1035 outputDesc->mChannelMask = (audio_channel_mask_t)channelMask;
1036 outputDesc->mLatency = 0;
1037 outputDesc->mFlags =(audio_output_flags_t) (outputDesc->mFlags | flags);
1038 outputDesc->mRefCount[stream] = 0;
1039 outputDesc->mStopTime[stream] = 0;
1040 outputDesc->mDirectOpenCount = 1;
1041 output = mpClientInterface->openOutput(profile->mModule->mHandle,
1042 &outputDesc->mDevice,
1043 &outputDesc->mSamplingRate,
1044 &outputDesc->mFormat,
1045 &outputDesc->mChannelMask,
1046 &outputDesc->mLatency,
1047 outputDesc->mFlags,
1048 offloadInfo);
1049
1050 // only accept an output with the requested parameters
1051 if (output == 0 ||
1052 (samplingRate != 0 && samplingRate != outputDesc->mSamplingRate) ||
1053 (format != 0 && format != outputDesc->mFormat) ||
1054 (channelMask != 0 && channelMask != outputDesc->mChannelMask)) {
1055 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1056 "format %d %d, channelMask %04x %04x", output, samplingRate,
1057 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1058 outputDesc->mChannelMask);
1059 if (output != 0) {
1060 mpClientInterface->closeOutput(output);
1061 }
1062 delete outputDesc;
1063 return 0;
1064 }
1065 audio_io_handle_t srcOutput = getOutputForEffect();
1066 addOutput(output, outputDesc);
1067 audio_io_handle_t dstOutput = getOutputForEffect();
1068 if (dstOutput == output) {
1069 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1070 }
1071 mPreviousOutputs = mOutputs;
1072 ALOGV("getOutput() returns new direct output %d", output);
1073 return output;
1074 }
1075
1076 // ignoring channel mask due to downmix capability in mixer
1077
1078 // open a non direct output
1079
1080 // for non direct outputs, only PCM is supported
1081 if (audio_is_linear_pcm((audio_format_t)format)) {
1082 // get which output is suitable for the specified stream. The actual
1083 // routing change will happen when startOutput() will be called
1084 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1085
1086 output = selectOutput(outputs, flags);
1087 }
1088 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1089 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1090
1091 ALOGV("getOutput() returns output %d", output);
1092
1093 return output;
1094}
1095
1096
1097// This function checks for the parameters which can be offloaded.
1098// This can be enhanced depending on the capability of the DSP and policy
1099// of the system.
1100bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
1101{
1102 ALOGV(" isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
1103 " BitRate=%u, duration=%lld us, has_video=%d",
1104 offloadInfo.sample_rate, offloadInfo.channel_mask,
1105 offloadInfo.format,
1106 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
1107 offloadInfo.has_video);
1108
1109#ifdef VOICE_CONCURRENCY
1110 if(isInCall())
1111 {
1112 ALOGD("\n blocking compress offload on call mode\n");
1113 return false;
1114 }
1115#endif
1116
1117 // Check if offload has been disabled
1118 char propValue[PROPERTY_VALUE_MAX];
1119 if (property_get("audio.offload.disable", propValue, "0")) {
1120 if (atoi(propValue) != 0) {
1121 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
1122 return false;
1123 }
1124 }
1125
1126 // Check if stream type is music, then only allow offload as of now.
1127 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
1128 {
1129 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
1130 return false;
1131 }
1132
1133 //TODO: enable audio offloading with video when ready
1134 if (offloadInfo.has_video)
1135 {
1136 if(property_get("av.offload.enable", propValue, NULL)) {
1137 bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1138 if (!prop_enabled) {
1139 ALOGW("offload disabled by av.offload.enable = %s ", propValue );
1140 return false;
1141 }
1142 }
1143 if(offloadInfo.is_streaming &&
1144 property_get("av.streaming.offload.enable", propValue, NULL)) {
1145 bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1146 if (!prop_enabled) {
1147 ALOGW("offload disabled by av.streaming.offload.enable = %s ", propValue );
1148 return false;
1149 }
1150 }
1151 ALOGV("isOffloadSupported: has_video == true, property\
1152 set to enable offload");
1153 }
1154
1155 //If duration is less than minimum value defined in property, return false
1156 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
1157 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
1158 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
1159 return false;
1160 }
1161 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
1162 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
1163 //duration checks only valid for MP3/AAC formats,
1164 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
1165 if (offloadInfo.format == AUDIO_FORMAT_MP3 || offloadInfo.format == AUDIO_FORMAT_AAC)
1166 return false;
1167 }
1168
1169 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1170 // creating an offloaded track and tearing it down immediately after start when audioflinger
1171 // detects there is an active non offloadable effect.
1172 // FIXME: We should check the audio session here but we do not have it in this context.
1173 // This may prevent offloading in rare situations where effects are left active by apps
1174 // in the background.
1175 if (isNonOffloadableEffectEnabled()) {
1176 return false;
1177 }
1178
1179 // See if there is a profile to support this.
1180 // AUDIO_DEVICE_NONE
1181 IOProfile *profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
1182 offloadInfo.sample_rate,
1183 offloadInfo.format,
1184 offloadInfo.channel_mask,
1185 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1186 ALOGV("isOffloadSupported() profile %sfound", profile != NULL ? "" : "NOT ");
1187 return (profile != NULL);
1188}
1189
1190void AudioPolicyManager::setPhoneState(int state)
1191
1192{
1193 ALOGV("setPhoneState() state %d", state);
1194 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
1195 if (state < 0 || state >= AudioSystem::NUM_MODES) {
1196 ALOGW("setPhoneState() invalid state %d", state);
1197 return;
1198 }
1199
1200 if (state == mPhoneState ) {
1201 ALOGW("setPhoneState() setting same state %d", state);
1202 return;
1203 }
1204
1205 // if leaving call state, handle special case of active streams
1206 // pertaining to sonification strategy see handleIncallSonification()
1207 if (isInCall()) {
1208 ALOGV("setPhoneState() in call state management: new state is %d", state);
1209 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
1210 handleIncallSonification(stream, false, true);
1211 }
1212 }
1213
1214 // store previous phone state for management of sonification strategy below
1215 int oldState = mPhoneState;
1216 mPhoneState = state;
1217 bool force = false;
1218
1219 // are we entering or starting a call
1220 if (!isStateInCall(oldState) && isStateInCall(state)) {
1221 ALOGV(" Entering call in setPhoneState()");
1222 // force routing command to audio hardware when starting a call
1223 // even if no device change is needed
1224 force = true;
1225 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
1226 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
1227 sVolumeProfiles[AUDIO_STREAM_VOICE_CALL][j];
1228 }
1229 } else if (isStateInCall(oldState) && !isStateInCall(state)) {
1230 ALOGV(" Exiting call in setPhoneState()");
1231 // force routing command to audio hardware when exiting a call
1232 // even if no device change is needed
1233 force = true;
1234 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
1235 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
1236 sVolumeProfiles[AUDIO_STREAM_DTMF][j];
1237 }
1238 } else if (isStateInCall(state) && (state != oldState)) {
1239 ALOGV(" Switching between telephony and VoIP in setPhoneState()");
1240 // force routing command to audio hardware when switching between telephony and VoIP
1241 // even if no device change is needed
1242 force = true;
1243 }
1244
1245 // check for device and output changes triggered by new phone state
1246 newDevice = getNewDevice(mPrimaryOutput, false /*fromCache*/);
1247 checkA2dpSuspend();
1248 checkOutputForAllStrategies();
1249 updateDevicesAndOutputs();
1250
1251 AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
1252
1253 // force routing command to audio hardware when ending call
1254 // even if no device change is needed
1255 if (isStateInCall(oldState) && newDevice == AUDIO_DEVICE_NONE) {
1256 newDevice = hwOutputDesc->device();
1257 }
1258
1259 int delayMs = 0;
1260 if (isStateInCall(state)) {
1261 nsecs_t sysTime = systemTime();
1262 for (size_t i = 0; i < mOutputs.size(); i++) {
1263 AudioOutputDescriptor *desc = mOutputs.valueAt(i);
1264 // mute media and sonification strategies and delay device switch by the largest
1265 // latency of any output where either strategy is active.
1266 // This avoid sending the ring tone or music tail into the earpiece or headset.
1267 if ((desc->isStrategyActive(STRATEGY_MEDIA,
1268 SONIFICATION_HEADSET_MUSIC_DELAY,
1269 sysTime) ||
1270 desc->isStrategyActive(STRATEGY_SONIFICATION,
1271 SONIFICATION_HEADSET_MUSIC_DELAY,
1272 sysTime)) &&
1273 (delayMs < (int)desc->mLatency*2)) {
1274 delayMs = desc->mLatency*2;
1275 }
1276 setStrategyMute(STRATEGY_MEDIA, true, mOutputs.keyAt(i));
1277 setStrategyMute(STRATEGY_MEDIA, false, mOutputs.keyAt(i), MUTE_TIME_MS,
1278 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
1279 setStrategyMute(STRATEGY_SONIFICATION, true, mOutputs.keyAt(i));
1280 setStrategyMute(STRATEGY_SONIFICATION, false, mOutputs.keyAt(i), MUTE_TIME_MS,
1281 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
1282 }
1283 }
1284
1285 // change routing is necessary
1286 setOutputDevice(mPrimaryOutput, newDevice, force, delayMs);
1287
1288 // if entering in call state, handle special case of active streams
1289 // pertaining to sonification strategy see handleIncallSonification()
1290 if (isStateInCall(state)) {
1291 ALOGV("setPhoneState() in call state management: new state is %d", state);
1292 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
1293 handleIncallSonification(stream, true, true);
1294 }
1295 }
1296
1297 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
1298 if (state == AudioSystem::MODE_RINGTONE &&
1299 isStreamActive(AudioSystem::MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
1300 mLimitRingtoneVolume = true;
1301 } else {
1302 mLimitRingtoneVolume = false;
1303 }
1304
1305#ifdef VOICE_CONCURRENCY
1306 //Call invalidate to reset all opened non ULL audio tracks
1307 if(isInCall())
1308 {
1309 // Move tracks associated to this strategy from previous output to new output
1310 for (int i = AudioSystem::SYSTEM; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
1311 ALOGV("\n Invalidate on call mode for stream :: %d \n", i);
1312 //FIXME see fixme on name change
1313 mpClientInterface->setStreamOutput((AudioSystem::stream_type)i,
1314 0 /* ignored */);
1315 }
1316 }
1317#endif
1318
1319}
1320
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001321extern "C" AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001322{
1323 return new AudioPolicyManager(clientInterface);
1324}
1325
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001326extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001327{
1328 delete interface;
1329}
1330
1331}; // namespace android