blob: 2309730f26640ce462c91336d0ce495516639279 [file] [log] [blame]
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001/*
Mingming Yin4a72d652014-01-03 18:54:18 -08002 * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07003 * Not a contribution.
4 *
5 * Copyright (C) 2009 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#define LOG_TAG "AudioPolicyManager"
21//#define LOG_NDEBUG 0
22
23//#define VERY_VERBOSE_LOGGING
24#ifdef VERY_VERBOSE_LOGGING
25#define ALOGVV ALOGV
26#else
27#define ALOGVV(a...) do { } while(0)
28#endif
29
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070030// A device mask for all audio output devices that are considered "remote" when evaluating
31// active output devices in isStreamActiveRemotely()
32#define APM_AUDIO_OUT_DEVICE_REMOTE_ALL AUDIO_DEVICE_OUT_REMOTE_SUBMIX
33
34#include <utils/Log.h>
35#include "AudioPolicyManager.h"
36#include <hardware/audio_effect.h>
37#include <hardware/audio.h>
38#include <math.h>
39#include <hardware_legacy/audio_policy_conf.h>
40#include <cutils/properties.h>
41
42namespace android_audio_legacy {
43
44// ----------------------------------------------------------------------------
45// AudioPolicyInterface implementation
46// ----------------------------------------------------------------------------
Tanya Finkelde496d82014-03-05 23:59:45 +020047const char* AudioPolicyManager::HDMI_SPKR_STR = "hdmi_spkr";
Venkata Narendra Kumar Gutta5f64eea2014-05-28 17:42:10 +053048int AudioPolicyManager::mvoice_call_state = 0;
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070049
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070050status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
51 AudioSystem::device_connection_state state,
52 const char *device_address)
53{
54 SortedVector <audio_io_handle_t> outputs;
55
Sidipotu Ashokf43018c2014-05-02 16:21:50 +053056 ALOGD("setDeviceConnectionState() device: %x, state %d, address %s", device, state, device_address);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070057
58 // connect/disconnect only 1 device at a time
59 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
60
61 if (strlen(device_address) >= MAX_DEVICE_ADDRESS_LEN) {
62 ALOGE("setDeviceConnectionState() invalid address: %s", device_address);
63 return BAD_VALUE;
64 }
65
66 // handle output devices
67 if (audio_is_output_device(device)) {
68
69 if (!mHasA2dp && audio_is_a2dp_device(device)) {
70 ALOGE("setDeviceConnectionState() invalid A2DP device: %x", device);
71 return BAD_VALUE;
72 }
73 if (!mHasUsb && audio_is_usb_device(device)) {
74 ALOGE("setDeviceConnectionState() invalid USB audio device: %x", device);
75 return BAD_VALUE;
76 }
77 if (!mHasRemoteSubmix && audio_is_remote_submix_device((audio_devices_t)device)) {
78 ALOGE("setDeviceConnectionState() invalid remote submix audio device: %x", device);
79 return BAD_VALUE;
80 }
81
82 // save a copy of the opened output descriptors before any output is opened or closed
83 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
84 mPreviousOutputs = mOutputs;
85 switch (state)
86 {
87 // handle output device connection
88 case AudioSystem::DEVICE_STATE_AVAILABLE:
89 if (mAvailableOutputDevices & device) {
Tanya Finkelde496d82014-03-05 23:59:45 +020090#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
91 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
92 if (!strncmp(device_address, HDMI_SPKR_STR, MAX_DEVICE_ADDRESS_LEN)) {
93 mHdmiAudioDisabled = false;
94 } else {
95 mHdmiAudioEvent = true;
96 }
97 }
98#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070099 ALOGW("setDeviceConnectionState() device already connected: %x", device);
100 return INVALID_OPERATION;
101 }
102 ALOGV("setDeviceConnectionState() connecting device %x", device);
103
104 if (checkOutputsForDevice(device, state, outputs) != NO_ERROR) {
105 return INVALID_OPERATION;
106 }
107 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %d outputs",
108 outputs.size());
109 // register new device as available
110 mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices | device);
111
Tanya Finkelde496d82014-03-05 23:59:45 +0200112#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
113 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
114 if (!strncmp(device_address, HDMI_SPKR_STR, MAX_DEVICE_ADDRESS_LEN)) {
115 mHdmiAudioDisabled = false;
116 } else {
117 mHdmiAudioEvent = true;
118 }
119 if (mHdmiAudioDisabled || !mHdmiAudioEvent) {
120 mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices & ~device);
121 }
122 }
123#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700124 if (!outputs.isEmpty()) {
125 String8 paramStr;
126 if (mHasA2dp && audio_is_a2dp_device(device)) {
127 // handle A2DP device connection
128 AudioParameter param;
129 param.add(String8(AUDIO_PARAMETER_A2DP_SINK_ADDRESS), String8(device_address));
130 paramStr = param.toString();
131 mA2dpDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
132 mA2dpSuspended = false;
133 } else if (audio_is_bluetooth_sco_device(device)) {
134 // handle SCO device connection
135 mScoDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
136 } else if (mHasUsb && audio_is_usb_device(device)) {
137 // handle USB device connection
138 mUsbCardAndDevice = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
139 paramStr = mUsbCardAndDevice;
140 }
141 // not currently handling multiple simultaneous submixes: ignoring remote submix
142 // case and address
143 if (!paramStr.isEmpty()) {
144 for (size_t i = 0; i < outputs.size(); i++) {
145 mpClientInterface->setParameters(outputs[i], paramStr);
146 }
147 }
148 }
149 break;
150 // handle output device disconnection
151 case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
152 if (!(mAvailableOutputDevices & device)) {
Tanya Finkelde496d82014-03-05 23:59:45 +0200153#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
154 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
155 if (!strncmp(device_address, HDMI_SPKR_STR, MAX_DEVICE_ADDRESS_LEN)) {
156 mHdmiAudioDisabled = true;
157 } else {
158 mHdmiAudioEvent = false;
159 }
160 }
161#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700162 ALOGW("setDeviceConnectionState() device not connected: %x", device);
163 return INVALID_OPERATION;
164 }
165
166 ALOGV("setDeviceConnectionState() disconnecting device %x", device);
167 // remove device from available output devices
168 mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices & ~device);
169
Tanya Finkelde496d82014-03-05 23:59:45 +0200170#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
171 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
172 if (!strncmp(device_address, HDMI_SPKR_STR, MAX_DEVICE_ADDRESS_LEN)) {
173 mHdmiAudioDisabled = true;
174 } else {
175 mHdmiAudioEvent = false;
176 }
177 }
178#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700179 checkOutputsForDevice(device, state, outputs);
180 if (mHasA2dp && audio_is_a2dp_device(device)) {
181 // handle A2DP device disconnection
182 mA2dpDeviceAddress = "";
183 mA2dpSuspended = false;
184 } else if (audio_is_bluetooth_sco_device(device)) {
185 // handle SCO device disconnection
186 mScoDeviceAddress = "";
187 } else if (mHasUsb && audio_is_usb_device(device)) {
188 // handle USB device disconnection
189 mUsbCardAndDevice = "";
190 }
191 // not currently handling multiple simultaneous submixes: ignoring remote submix
192 // case and address
193 } break;
194
195 default:
196 ALOGE("setDeviceConnectionState() invalid state: %x", state);
197 return BAD_VALUE;
198 }
199
200 checkA2dpSuspend();
201 checkOutputForAllStrategies();
202 // outputs must be closed after checkOutputForAllStrategies() is executed
203 if (!outputs.isEmpty()) {
204 for (size_t i = 0; i < outputs.size(); i++) {
205 AudioOutputDescriptor *desc = mOutputs.valueFor(outputs[i]);
206 // close unused outputs after device disconnection or direct outputs that have been
207 // opened by checkOutputsForDevice() to query dynamic parameters
208 if ((state == AudioSystem::DEVICE_STATE_UNAVAILABLE) ||
209 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
210 (desc->mDirectOpenCount == 0))) {
211 closeOutput(outputs[i]);
212 }
213 }
214 }
215
216 updateDevicesAndOutputs();
217 audio_devices_t newDevice = getNewDevice(mPrimaryOutput, false /*fromCache*/);
218#ifdef AUDIO_EXTN_FM_ENABLED
219 if(device == AUDIO_DEVICE_OUT_FM) {
220 if (state == AudioSystem::DEVICE_STATE_AVAILABLE) {
221 mOutputs.valueFor(mPrimaryOutput)->changeRefCount(AudioSystem::MUSIC, 1);
222 newDevice = (audio_devices_t)(getNewDevice(mPrimaryOutput, false) | AUDIO_DEVICE_OUT_FM);
223 } else {
224 mOutputs.valueFor(mPrimaryOutput)->changeRefCount(AudioSystem::MUSIC, -1);
225 }
226
227 AudioParameter param = AudioParameter();
228 param.addInt(String8("handle_fm"), (int)newDevice);
229 ALOGV("setDeviceConnectionState() setParameters handle_fm");
230 mpClientInterface->setParameters(mPrimaryOutput, param.toString());
231 }
232#endif
233 for (size_t i = 0; i < mOutputs.size(); i++) {
234 // do not force device change on duplicated output because if device is 0, it will
235 // also force a device 0 for the two outputs it is duplicated to which may override
236 // a valid device selection on those outputs.
Satya Krishna Pindiproli7e522282014-05-08 12:22:16 +0530237 audio_devices_t cachedDevice = getNewDevice(mOutputs.keyAt(i), true /*fromCache*/);
238 AudioOutputDescriptor *desc = mOutputs.valueFor(mOutputs.keyAt(i));
239 if (cachedDevice == AUDIO_DEVICE_OUT_SPEAKER &&
240 device == AUDIO_DEVICE_OUT_PROXY &&
241 (desc->mFlags & AUDIO_OUTPUT_FLAG_FAST)) {
242 ALOGI("Avoid routing touch tone to spkr as proxy is being disconnected");
243 break;
244 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700245 setOutputDevice(mOutputs.keyAt(i),
Satya Krishna Pindiproli7e522282014-05-08 12:22:16 +0530246 cachedDevice,
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700247 !mOutputs.valueAt(i)->isDuplicated(),
248 0);
249 }
250
251 if (device == AUDIO_DEVICE_OUT_WIRED_HEADSET) {
252 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
253 } else if (device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO ||
254 device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET ||
255 device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT) {
256 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
257 } else if(device == AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET){
258 device = AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET;
259 } else {
260 return NO_ERROR;
261 }
262 }
263 // handle input devices
264 if (audio_is_input_device(device)) {
265
266 switch (state)
267 {
268 // handle input device connection
269 case AudioSystem::DEVICE_STATE_AVAILABLE: {
270 if (mAvailableInputDevices & device) {
271 ALOGW("setDeviceConnectionState() device already connected: %d", device);
272 return INVALID_OPERATION;
273 }
274 mAvailableInputDevices = mAvailableInputDevices | (device & ~AUDIO_DEVICE_BIT_IN);
275 }
276 break;
277
278 // handle input device disconnection
279 case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
280 if (!(mAvailableInputDevices & device)) {
281 ALOGW("setDeviceConnectionState() device not connected: %d", device);
282 return INVALID_OPERATION;
283 }
284 mAvailableInputDevices = (audio_devices_t) (mAvailableInputDevices & ~device);
285 } break;
286
287 default:
288 ALOGE("setDeviceConnectionState() invalid state: %x", state);
289 return BAD_VALUE;
290 }
291
292 audio_io_handle_t activeInput = getActiveInput();
293 if (activeInput != 0) {
294 AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
295 audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
296 if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) {
297 ALOGV("setDeviceConnectionState() changing device from %x to %x for input %d",
298 inputDesc->mDevice, newDevice, activeInput);
299 inputDesc->mDevice = newDevice;
300 AudioParameter param = AudioParameter();
301 param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
302 mpClientInterface->setParameters(activeInput, param.toString());
303 }
304 }
305
306 return NO_ERROR;
307 }
308
309 ALOGW("setDeviceConnectionState() invalid device: %x", device);
310 return BAD_VALUE;
311}
312
313void AudioPolicyManager::setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config)
314{
Sidipotu Ashokf43018c2014-05-02 16:21:50 +0530315 ALOGD("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700316
317 bool forceVolumeReeval = false;
318 switch(usage) {
319 case AudioSystem::FOR_COMMUNICATION:
320 if (config != AudioSystem::FORCE_SPEAKER && config != AudioSystem::FORCE_BT_SCO &&
321 config != AudioSystem::FORCE_NONE) {
322 ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
323 return;
324 }
325 forceVolumeReeval = true;
326 mForceUse[usage] = config;
327 break;
328 case AudioSystem::FOR_MEDIA:
329 if (config != AudioSystem::FORCE_HEADPHONES && config != AudioSystem::FORCE_BT_A2DP &&
330#ifdef AUDIO_EXTN_FM_ENABLED
331 config != AudioSystem::FORCE_SPEAKER &&
332#endif
333 config != AudioSystem::FORCE_WIRED_ACCESSORY &&
334 config != AudioSystem::FORCE_ANALOG_DOCK &&
335 config != AudioSystem::FORCE_DIGITAL_DOCK && config != AudioSystem::FORCE_NONE &&
336 config != AudioSystem::FORCE_NO_BT_A2DP) {
337 ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
338 return;
339 }
340 mForceUse[usage] = config;
341 break;
342 case AudioSystem::FOR_RECORD:
343 if (config != AudioSystem::FORCE_BT_SCO && config != AudioSystem::FORCE_WIRED_ACCESSORY &&
344 config != AudioSystem::FORCE_NONE) {
345 ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
346 return;
347 }
348 mForceUse[usage] = config;
349 break;
350 case AudioSystem::FOR_DOCK:
351 if (config != AudioSystem::FORCE_NONE && config != AudioSystem::FORCE_BT_CAR_DOCK &&
352 config != AudioSystem::FORCE_BT_DESK_DOCK &&
353 config != AudioSystem::FORCE_WIRED_ACCESSORY &&
354 config != AudioSystem::FORCE_ANALOG_DOCK &&
355 config != AudioSystem::FORCE_DIGITAL_DOCK) {
356 ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
357 }
358 forceVolumeReeval = true;
359 mForceUse[usage] = config;
360 break;
361 case AudioSystem::FOR_SYSTEM:
362 if (config != AudioSystem::FORCE_NONE &&
363 config != AudioSystem::FORCE_SYSTEM_ENFORCED) {
364 ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
365 }
366 forceVolumeReeval = true;
367 mForceUse[usage] = config;
368 break;
369 default:
370 ALOGW("setForceUse() invalid usage %d", usage);
371 break;
372 }
373
374 // check for device and output changes triggered by new force usage
375 checkA2dpSuspend();
376 checkOutputForAllStrategies();
377 updateDevicesAndOutputs();
378 for (int i = mOutputs.size() -1; i >= 0; i--) {
379 audio_io_handle_t output = mOutputs.keyAt(i);
380 audio_devices_t newDevice = getNewDevice(output, true /*fromCache*/);
381 setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE));
382 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
383 applyStreamVolumes(output, newDevice, 0, true);
384 }
385 }
386
387 audio_io_handle_t activeInput = getActiveInput();
388 if (activeInput != 0) {
389 AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
390 audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
391 if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) {
392 ALOGV("setForceUse() changing device from %x to %x for input %d",
393 inputDesc->mDevice, newDevice, activeInput);
394 inputDesc->mDevice = newDevice;
395 AudioParameter param = AudioParameter();
396 param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
397 mpClientInterface->setParameters(activeInput, param.toString());
398 }
399 }
400
401}
402
403audio_io_handle_t AudioPolicyManager::getInput(int inputSource,
404 uint32_t samplingRate,
405 uint32_t format,
406 uint32_t channelMask,
407 AudioSystem::audio_in_acoustics acoustics)
408{
409 audio_io_handle_t input = 0;
410 audio_devices_t device = getDeviceForInputSource(inputSource);
411
Sidipotu Ashokf43018c2014-05-02 16:21:50 +0530412 ALOGD("getInput() inputSource %d, samplingRate %d, format %d, channelMask %x, acoustics %x",
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700413 inputSource, samplingRate, format, channelMask, acoustics);
414
415 if (device == AUDIO_DEVICE_NONE) {
416 ALOGW("getInput() could not find device for inputSource %d", inputSource);
417 return 0;
418 }
419
Naresh Tanniru811f4982014-03-18 17:25:32 +0530420#ifdef VOICE_CONCURRENCY
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700421
Naresh Tanniru811f4982014-03-18 17:25:32 +0530422 char propValue[PROPERTY_VALUE_MAX];
423 bool prop_rec_enabled=false, prop_voip_enabled = false;
424
425 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
426 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
427 }
428
429 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
430 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
431 }
432
Venkata Narendra Kumar Gutta5f64eea2014-05-28 17:42:10 +0530433 if (prop_rec_enabled && mvoice_call_state) {
Naresh Tanniru811f4982014-03-18 17:25:32 +0530434 //check if voice call is active / running in background
435 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
436 //Need to block input request
437 if((AudioSystem::MODE_IN_CALL == mPhoneState) ||
438 ((AudioSystem::MODE_IN_CALL == mPrevPhoneState) &&
439 (AudioSystem::MODE_IN_COMMUNICATION == mPhoneState)))
440 {
441 switch(inputSource) {
442 case AUDIO_SOURCE_VOICE_UPLINK:
443 case AUDIO_SOURCE_VOICE_DOWNLINK:
444 case AUDIO_SOURCE_VOICE_CALL:
445 ALOGD("Creating input during incall mode for inputSource: %d ",inputSource);
446 break;
447
Venkata Narendra Kumar Gutta5f64eea2014-05-28 17:42:10 +0530448 case AUDIO_SOURCE_VOICE_COMMUNICATION:
Naresh Tanniru811f4982014-03-18 17:25:32 +0530449 if(prop_voip_enabled) {
Venkata Narendra Kumar Gutta5f64eea2014-05-28 17:42:10 +0530450 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
451 return 0;
Naresh Tanniru811f4982014-03-18 17:25:32 +0530452 }
453 break;
Venkata Narendra Kumar Gutta5f64eea2014-05-28 17:42:10 +0530454 default:
455 ALOGD("BLOCKING input during incall mode for inputSource: %d ",inputSource);
456 return 0;
Naresh Tanniru811f4982014-03-18 17:25:32 +0530457 }
458 }
459 }//check for VoIP flag
Venkata Narendra Kumar Gutta5f64eea2014-05-28 17:42:10 +0530460 else if(prop_voip_enabled && mvoice_call_state) {
Naresh Tanniru811f4982014-03-18 17:25:32 +0530461 //check if voice call is active / running in background
462 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
463 //Need to block input request
464 if((AudioSystem::MODE_IN_CALL == mPhoneState) ||
465 ((AudioSystem::MODE_IN_CALL == mPrevPhoneState) &&
466 (AudioSystem::MODE_IN_COMMUNICATION == mPhoneState)))
467 {
468 if(inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION) {
469 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
470 return 0;
471 }
472 }
473 }
474
475#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700476 IOProfile *profile = getInputProfile(device,
477 samplingRate,
478 format,
479 channelMask);
480 if (profile == NULL) {
481 ALOGW("getInput() could not find profile for device %04x, samplingRate %d, format %d,"
482 "channelMask %04x",
483 device, samplingRate, format, channelMask);
484 return 0;
485 }
486
487 if (profile->mModule->mHandle == 0) {
488 ALOGE("getInput(): HW module %s not opened", profile->mModule->mName);
489 return 0;
490 }
491
492 AudioInputDescriptor *inputDesc = new AudioInputDescriptor(profile);
493
494 inputDesc->mInputSource = inputSource;
495 inputDesc->mDevice = device;
496 inputDesc->mSamplingRate = samplingRate;
497 inputDesc->mFormat = (audio_format_t)format;
498 inputDesc->mChannelMask = (audio_channel_mask_t)channelMask;
499 inputDesc->mRefCount = 0;
500 input = mpClientInterface->openInput(profile->mModule->mHandle,
501 &inputDesc->mDevice,
502 &inputDesc->mSamplingRate,
503 &inputDesc->mFormat,
504 &inputDesc->mChannelMask);
505
506 // only accept input with the exact requested set of parameters
507 if (input == 0 ||
508 (samplingRate != inputDesc->mSamplingRate) ||
509 (format != inputDesc->mFormat) ||
510 (channelMask != inputDesc->mChannelMask)) {
511 ALOGV("getInput() failed opening input: samplingRate %d, format %d, channelMask %d",
512 samplingRate, format, channelMask);
513 if (input != 0) {
514 mpClientInterface->closeInput(input);
515 }
516 delete inputDesc;
517 return 0;
518 }
519 mInputs.add(input, inputDesc);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +0530520 ALOGD("getInput() returns input %d", input);
521
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700522 return input;
523}
524
525AudioPolicyManager::routing_strategy AudioPolicyManager::getStrategy(AudioSystem::stream_type stream)
526{
Naresh Tanniru36c08932014-01-27 18:40:53 +0530527 // stream to strategy mapping
528 switch (stream) {
529 case AudioSystem::VOICE_CALL:
530 case AudioSystem::BLUETOOTH_SCO:
531 return STRATEGY_PHONE;
532 case AudioSystem::RING:
533 case AudioSystem::ALARM:
534 return STRATEGY_SONIFICATION;
535 case AudioSystem::NOTIFICATION:
536 return STRATEGY_SONIFICATION_RESPECTFUL;
537 case AudioSystem::DTMF:
538 return STRATEGY_DTMF;
539 default:
540 ALOGE("unknown stream type");
541 case AudioSystem::SYSTEM:
542 // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
543 // while key clicks are played produces a poor result
544 case AudioSystem::TTS:
545 case AudioSystem::MUSIC:
546#ifdef AUDIO_EXTN_INCALL_MUSIC_ENABLED
547 case AudioSystem::INCALL_MUSIC:
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700548#endif
Naresh Tanniru36c08932014-01-27 18:40:53 +0530549#ifdef QCOM_INCALL_MUSIC_ENABLED
550 case AudioSystem::INCALL_MUSIC:
551#endif
552 return STRATEGY_MEDIA;
553 case AudioSystem::ENFORCED_AUDIBLE:
554 return STRATEGY_ENFORCED_AUDIBLE;
555 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700556
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700557}
558
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700559audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
560 bool fromCache)
561{
562 uint32_t device = AUDIO_DEVICE_NONE;
563
564 if (fromCache) {
565 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
566 strategy, mDeviceForStrategy[strategy]);
567 return mDeviceForStrategy[strategy];
568 }
569
570 switch (strategy) {
571
572 case STRATEGY_SONIFICATION_RESPECTFUL:
573 if (isInCall()) {
574 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
575 } else if (isStreamActiveRemotely(AudioSystem::MUSIC,
576 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
577 // while media is playing on a remote device, use the the sonification behavior.
578 // Note that we test this usecase before testing if media is playing because
579 // the isStreamActive() method only informs about the activity of a stream, not
580 // if it's for local playback. Note also that we use the same delay between both tests
581 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
582 } else if (isStreamActive(AudioSystem::MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
583 // while media is playing (or has recently played), use the same device
584 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
585 } else {
586 // when media is not playing anymore, fall back on the sonification behavior
587 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
588 }
589
590 break;
591
592 case STRATEGY_DTMF:
593 if (!isInCall()) {
594 // when off call, DTMF strategy follows the same rules as MEDIA strategy
595 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
596 break;
597 }
598 // when in call, DTMF and PHONE strategies follow the same rules
599 // FALL THROUGH
600
601 case STRATEGY_PHONE:
602 // for phone strategy, we first consider the forced use and then the available devices by order
603 // of priority
604 switch (mForceUse[AudioSystem::FOR_COMMUNICATION]) {
605 case AudioSystem::FORCE_BT_SCO:
606 if (!isInCall() || strategy != STRATEGY_DTMF) {
607 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
608 if (device) break;
609 }
610 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
611 if (device) break;
612 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
613 if (device) break;
614 // if SCO device is requested but no SCO device is available, fall back to default case
615 // FALL THROUGH
616
617 default: // FORCE_NONE
618 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
619 if (mHasA2dp && !isInCall() &&
620 (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
621 (getA2dpOutput() != 0) && !mA2dpSuspended) {
622 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
623 if (device) break;
624 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
625 if (device) break;
626 }
627 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
628 if (device) break;
629 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
630 if (device) break;
631 if (mPhoneState != AudioSystem::MODE_IN_CALL) {
632 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
633 if (device) break;
634 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
635 if (device) break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700636 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
637 if (device) break;
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700638 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
639 if (device) break;
640 }
641
642 // Allow voice call on USB ANLG DOCK headset
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700643 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
644 if (device) break;
645
646 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_EARPIECE;
647 if (device) break;
648 device = mDefaultOutputDevice;
649 if (device == AUDIO_DEVICE_NONE) {
650 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE");
651 }
652 break;
653
654 case AudioSystem::FORCE_SPEAKER:
655 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
656 // A2DP speaker when forcing to speaker output
657 if (mHasA2dp && !isInCall() &&
658 (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
659 (getA2dpOutput() != 0) && !mA2dpSuspended) {
660 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
661 if (device) break;
662 }
663 if (mPhoneState != AudioSystem::MODE_IN_CALL) {
664 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
665 if (device) break;
666 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
667 if (device) break;
668 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
669 if (device) break;
670 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
671 if (device) break;
Helen Zengbbce4952013-12-16 20:26:46 -0800672 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
673 if (device) break;
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700674 }
675 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
676 if (device) break;
677 device = mDefaultOutputDevice;
678 if (device == AUDIO_DEVICE_NONE) {
679 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER");
680 }
681 break;
682 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700683 // FIXME: Why do need to replace with speaker? If voice call is active
684 // We should use device from STRATEGY_PHONE
685#ifdef AUDIO_EXTN_FM_ENABLED
686 if (mAvailableOutputDevices & AUDIO_DEVICE_OUT_FM) {
687 if (mForceUse[AudioSystem::FOR_MEDIA] == AudioSystem::FORCE_SPEAKER) {
688 device = AUDIO_DEVICE_OUT_SPEAKER;
689 }
690 }
691#endif
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700692 break;
693
694 case STRATEGY_SONIFICATION:
695
696 // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
697 // handleIncallSonification().
698 if (isInCall()) {
699 device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
700 break;
701 }
702 // FALL THROUGH
703
704 case STRATEGY_ENFORCED_AUDIBLE:
705 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
706 // except:
707 // - when in call where it doesn't default to STRATEGY_PHONE behavior
708 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
709
710 if ((strategy == STRATEGY_SONIFICATION) ||
711 (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_SYSTEM_ENFORCED)) {
712 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
713 if (device == AUDIO_DEVICE_NONE) {
714 ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION");
715 }
716 }
717 // The second device used for sonification is the same as the device used by media strategy
718 // FALL THROUGH
719
720 case STRATEGY_MEDIA: {
721 uint32_t device2 = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700722
Mingming Yin4a72d652014-01-03 18:54:18 -0800723 if (isInCall() && (device == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700724 // when in call, get the device for Phone strategy
725 device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
726 break;
727 }
728#ifdef AUDIO_EXTN_FM_ENABLED
729 if (mForceUse[AudioSystem::FOR_MEDIA] == AudioSystem::FORCE_SPEAKER) {
730 device = AUDIO_DEVICE_OUT_SPEAKER;
731 break;
732 }
733#endif
734
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700735 if (strategy != STRATEGY_SONIFICATION) {
736 // no sonification on remote submix (e.g. WFD)
737 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
738 }
739 if ((device2 == AUDIO_DEVICE_NONE) &&
740 mHasA2dp && (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
741 (getA2dpOutput() != 0) && !mA2dpSuspended) {
742 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
743 if (device2 == AUDIO_DEVICE_NONE) {
744 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
745 }
746 if (device2 == AUDIO_DEVICE_NONE) {
747 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
748 }
749 }
750 if (device2 == AUDIO_DEVICE_NONE) {
751 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
752 }
753 if (device2 == AUDIO_DEVICE_NONE) {
754 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
755 }
756 if (device2 == AUDIO_DEVICE_NONE) {
757 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
758 }
759 if (device2 == AUDIO_DEVICE_NONE) {
760 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
761 }
762 if (device2 == AUDIO_DEVICE_NONE) {
763 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
764 }
Apoorv Raghuvanshi7566f8b2014-01-17 13:10:09 -0800765 if ((strategy != STRATEGY_SONIFICATION) && (device == AUDIO_DEVICE_NONE)
766 && (device2 == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700767 // no sonification on aux digital (e.g. HDMI)
768 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
769 }
770 if ((device2 == AUDIO_DEVICE_NONE) &&
Krishnankutty Kolathappilly4b7fdaa2013-12-16 00:40:11 -0800771 (mForceUse[AudioSystem::FOR_DOCK] == AudioSystem::FORCE_ANALOG_DOCK)
772 && (strategy != STRATEGY_SONIFICATION)) {
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700773 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
774 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700775#ifdef AUDIO_EXTN_FM_ENABLED
Apoorv Raghuvanshi7566f8b2014-01-17 13:10:09 -0800776 if ((strategy != STRATEGY_SONIFICATION) && (device == AUDIO_DEVICE_NONE)
777 && (device2 == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700778 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_FM_TX;
779 }
780#endif
781#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Apoorv Raghuvanshi7566f8b2014-01-17 13:10:09 -0800782 if ((strategy != STRATEGY_SONIFICATION) && (device == AUDIO_DEVICE_NONE)
783 && (device2 == AUDIO_DEVICE_NONE)) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700784 // no sonification on WFD sink
785 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_PROXY;
786 }
787#endif
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700788 if (device2 == AUDIO_DEVICE_NONE) {
789 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
790 }
791
792 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
793 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
794 device |= device2;
795 if (device) break;
796 device = mDefaultOutputDevice;
797 if (device == AUDIO_DEVICE_NONE) {
798 ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA");
799 }
800 } break;
801
802 default:
803 ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
804 break;
805 }
806
807 ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
808 return device;
809}
810
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700811audio_devices_t AudioPolicyManager::getDeviceForInputSource(int inputSource)
812{
813 uint32_t device = AUDIO_DEVICE_NONE;
814
815 switch (inputSource) {
816 case AUDIO_SOURCE_VOICE_UPLINK:
817 if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
818 device = AUDIO_DEVICE_IN_VOICE_CALL;
819 break;
820 }
821 // FALL THROUGH
822
823 case AUDIO_SOURCE_DEFAULT:
824 case AUDIO_SOURCE_MIC:
825 case AUDIO_SOURCE_VOICE_RECOGNITION:
826 case AUDIO_SOURCE_HOTWORD:
827 case AUDIO_SOURCE_VOICE_COMMUNICATION:
828 if (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO &&
829 mAvailableInputDevices & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
830 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
831 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_WIRED_HEADSET) {
832 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
833 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET) {
834 device = AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET;
835 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
836 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
837 }
838 break;
839 case AUDIO_SOURCE_CAMCORDER:
840 if (mAvailableInputDevices & AUDIO_DEVICE_IN_BACK_MIC) {
841 device = AUDIO_DEVICE_IN_BACK_MIC;
842 } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
843 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
844 }
845 break;
846 case AUDIO_SOURCE_VOICE_DOWNLINK:
847 case AUDIO_SOURCE_VOICE_CALL:
848 if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
849 device = AUDIO_DEVICE_IN_VOICE_CALL;
850 }
851 break;
852 case AUDIO_SOURCE_REMOTE_SUBMIX:
853 if (mAvailableInputDevices & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
854 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
855 }
856 break;
857#ifdef AUDIO_EXTN_FM_ENABLED
858 case AUDIO_SOURCE_FM_RX:
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700859 device = AUDIO_DEVICE_IN_FM_RX;
860 break;
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700861 case AUDIO_SOURCE_FM_RX_A2DP:
862 device = AUDIO_DEVICE_IN_FM_RX_A2DP;
863 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700864#endif
865 default:
866 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
867 break;
868 }
869 ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
870 return device;
871}
872
873AudioPolicyManager::device_category AudioPolicyManager::getDeviceCategory(audio_devices_t device)
874{
875 switch(getDeviceForVolume(device)) {
876 case AUDIO_DEVICE_OUT_EARPIECE:
877 return DEVICE_CATEGORY_EARPIECE;
878 case AUDIO_DEVICE_OUT_WIRED_HEADSET:
879 case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
880 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
881 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
882 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
883 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
884#ifdef AUDIO_EXTN_FM_ENABLED
885 case AUDIO_DEVICE_OUT_FM:
886#endif
887 return DEVICE_CATEGORY_HEADSET;
888 case AUDIO_DEVICE_OUT_SPEAKER:
889 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
890 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
891 case AUDIO_DEVICE_OUT_AUX_DIGITAL:
892 case AUDIO_DEVICE_OUT_USB_ACCESSORY:
893 case AUDIO_DEVICE_OUT_USB_DEVICE:
894 case AUDIO_DEVICE_OUT_REMOTE_SUBMIX:
895#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
896 case AUDIO_DEVICE_OUT_PROXY:
897#endif
898 default:
899 return DEVICE_CATEGORY_SPEAKER;
900 }
901}
902
903status_t AudioPolicyManager::checkAndSetVolume(int stream,
904 int index,
905 audio_io_handle_t output,
906 audio_devices_t device,
907 int delayMs,
908 bool force)
909{
910 ALOGV("checkAndSetVolume: index %d output %d device %x", index, output, device);
911 // do not change actual stream volume if the stream is muted
912 if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
913 ALOGVV("checkAndSetVolume() stream %d muted count %d",
914 stream, mOutputs.valueFor(output)->mMuteCount[stream]);
915 return NO_ERROR;
916 }
917
918 // do not change in call volume if bluetooth is connected and vice versa
919 if ((stream == AudioSystem::VOICE_CALL && mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
920 (stream == AudioSystem::BLUETOOTH_SCO && mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO)) {
921 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
922 stream, mForceUse[AudioSystem::FOR_COMMUNICATION]);
923 return INVALID_OPERATION;
924 }
925
926 float volume = computeVolume(stream, index, output, device);
927 // We actually change the volume if:
928 // - the float value returned by computeVolume() changed
929 // - the force flag is set
930 if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
931 force) {
932 mOutputs.valueFor(output)->mCurVolume[stream] = volume;
933 ALOGV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
934 // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
935 // enabled
936 if (stream == AudioSystem::BLUETOOTH_SCO) {
937 mpClientInterface->setStreamVolume(AudioSystem::VOICE_CALL, volume, output, delayMs);
938#ifdef AUDIO_EXTN_FM_ENABLED
939 } else if (stream == AudioSystem::MUSIC &&
940 output == mPrimaryOutput) {
941 float fmVolume = -1.0;
942 fmVolume = computeVolume(stream, index, output, device);
943 if (fmVolume >= 0) {
944 AudioParameter param = AudioParameter();
945 param.addFloat(String8("fm_volume"), fmVolume);
946 ALOGV("checkAndSetVolume setParameters fm_volume, volume=:%f delay=:%d",fmVolume,delayMs*2);
947 //Double delayMs to avoid sound burst while device switch.
948 mpClientInterface->setParameters(mPrimaryOutput, param.toString(), delayMs*2);
949 }
950#endif
951 }
952 mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs);
953 }
954
955 if (stream == AudioSystem::VOICE_CALL ||
956 stream == AudioSystem::BLUETOOTH_SCO) {
957 float voiceVolume;
Vidyakumar Athota35543452014-04-16 10:00:39 -0700958 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
959 if (stream == AudioSystem::VOICE_CALL) {
960 voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
961 } else {
962 voiceVolume = 1.0;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700963 }
964
965 if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) {
966 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
967 mLastVoiceVolume = voiceVolume;
968 }
969 }
970
971 return NO_ERROR;
972}
973
974
975float AudioPolicyManager::computeVolume(int stream,
976 int index,
977 audio_io_handle_t output,
978 audio_devices_t device)
979{
980 float volume = 1.0;
981 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
982
983 if (device == AUDIO_DEVICE_NONE) {
984 device = outputDesc->device();
985 }
986
987 // if volume is not 0 (not muted), force media volume to max on digital output
988 if (stream == AudioSystem::MUSIC &&
989 index != mStreams[stream].mIndexMin &&
990 (device == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
991 device == AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET ||
992 device == AUDIO_DEVICE_OUT_USB_ACCESSORY ||
993#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
994 device == AUDIO_DEVICE_OUT_PROXY ||
995#endif
996 device == AUDIO_DEVICE_OUT_USB_DEVICE )) {
997 return 1.0;
998 }
999#ifdef AUDIO_EXTN_INCALL_MUSIC_ENABLED
1000 if (stream == AudioSystem::INCALL_MUSIC) {
1001 return 1.0;
1002 }
1003#endif
1004 return AudioPolicyManagerBase::computeVolume(stream, index, output, device);
1005}
Naresh Tanniru36c08932014-01-27 18:40:53 +05301006
1007
1008audio_io_handle_t AudioPolicyManager::getOutput(AudioSystem::stream_type stream,
1009 uint32_t samplingRate,
1010 uint32_t format,
1011 uint32_t channelMask,
1012 AudioSystem::output_flags flags,
1013 const audio_offload_info_t *offloadInfo)
1014{
1015 audio_io_handle_t output = 0;
1016 uint32_t latency = 0;
1017 routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);
1018 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1019 IOProfile *profile = NULL;
1020
1021#ifdef VOICE_CONCURRENCY
Naresh Tanniru811f4982014-03-18 17:25:32 +05301022 char propValue[PROPERTY_VALUE_MAX];
1023 bool prop_play_enabled=false, prop_voip_enabled = false;
1024
1025 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1026 prop_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1027 }
1028
1029 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1030 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1031 }
1032
Venkata Narendra Kumar Gutta5f64eea2014-05-28 17:42:10 +05301033 if (prop_play_enabled && mvoice_call_state) {
Naresh Tanniru811f4982014-03-18 17:25:32 +05301034 //check if voice call is active / running in background
1035 if((AudioSystem::MODE_IN_CALL == mPhoneState) ||
1036 ((AudioSystem::MODE_IN_CALL == mPrevPhoneState)
1037 && (AudioSystem::MODE_IN_COMMUNICATION == mPhoneState)))
1038 {
1039 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1040 if(prop_voip_enabled) {
1041 ALOGD(" IN call mode returing no output .. for VoIP usecase flags: %x ", flags );
1042 // flags = (AudioSystem::output_flags)AUDIO_OUTPUT_FLAG_FAST;
1043 return 0;
1044 }
1045 }
1046 else {
1047 ALOGD(" IN call mode adding ULL flags .. flags: %x ", flags );
1048 flags = (AudioSystem::output_flags)AUDIO_OUTPUT_FLAG_FAST;
1049 }
1050 }
Venkata Narendra Kumar Gutta5f64eea2014-05-28 17:42:10 +05301051 } else if (prop_voip_enabled && mvoice_call_state) {
Naresh Tanniru811f4982014-03-18 17:25:32 +05301052 //check if voice call is active / running in background
1053 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1054 //return only ULL ouput
1055 if((AudioSystem::MODE_IN_CALL == mPhoneState) ||
1056 ((AudioSystem::MODE_IN_CALL == mPrevPhoneState)
1057 && (AudioSystem::MODE_IN_COMMUNICATION == mPhoneState)))
1058 {
1059 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1060 ALOGD(" IN call mode returing no output .. for VoIP usecase flags: %x ", flags );
1061 // flags = (AudioSystem::output_flags)AUDIO_OUTPUT_FLAG_FAST;
1062 return 0;
1063 }
1064 }
Naresh Tanniru36c08932014-01-27 18:40:53 +05301065 }
1066#endif
1067
1068#ifdef WFD_CONCURRENCY
1069 if ((mAvailableOutputDevices & AUDIO_DEVICE_OUT_PROXY)
1070 && (stream != AudioSystem::MUSIC)) {
1071 ALOGV(" WFD mode adding ULL flags for non music stream.. flags: %x ", flags );
1072 //For voip paths
1073 if(flags & AudioSystem::OUTPUT_FLAG_DIRECT)
1074 flags = AudioSystem::OUTPUT_FLAG_DIRECT;
1075 else //route every thing else to ULL path
1076 flags = (AudioSystem::output_flags)AUDIO_OUTPUT_FLAG_FAST;
1077 }
1078#endif
1079
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05301080 ALOGD(" getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x ",
Naresh Tanniru36c08932014-01-27 18:40:53 +05301081 device, stream, samplingRate, format, channelMask, flags);
1082
1083
1084
1085#ifdef AUDIO_POLICY_TEST
1086 if (mCurOutput != 0) {
1087 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1088 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1089
1090 if (mTestOutputs[mCurOutput] == 0) {
1091 ALOGV("getOutput() opening test output");
1092 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL);
1093 outputDesc->mDevice = mTestDevice;
1094 outputDesc->mSamplingRate = mTestSamplingRate;
1095 outputDesc->mFormat = mTestFormat;
1096 outputDesc->mChannelMask = mTestChannels;
1097 outputDesc->mLatency = mTestLatencyMs;
1098 outputDesc->mFlags = (audio_output_flags_t)(mDirectOutput ? AudioSystem::OUTPUT_FLAG_DIRECT : 0);
1099 outputDesc->mRefCount[stream] = 0;
1100 mTestOutputs[mCurOutput] = mpClientInterface->openOutput(0, &outputDesc->mDevice,
1101 &outputDesc->mSamplingRate,
1102 &outputDesc->mFormat,
1103 &outputDesc->mChannelMask,
1104 &outputDesc->mLatency,
1105 outputDesc->mFlags,
1106 offloadInfo);
1107 if (mTestOutputs[mCurOutput]) {
1108 AudioParameter outputCmd = AudioParameter();
1109 outputCmd.addInt(String8("set_id"),mCurOutput);
1110 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1111 addOutput(mTestOutputs[mCurOutput], outputDesc);
1112 }
1113 }
1114 return mTestOutputs[mCurOutput];
1115 }
1116#endif //AUDIO_POLICY_TEST
1117
1118 // open a direct output if required by specified parameters
1119 //force direct flag if offload flag is set: offloading implies a direct output stream
1120 // and all common behaviors are driven by checking only the direct flag
1121 // this should normally be set appropriately in the policy configuration file
1122 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1123 flags = (AudioSystem::output_flags)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1124 }
1125
1126 if ((format == AudioSystem::PCM_16_BIT) &&(AudioSystem::popCount(channelMask) > 2)) {
1127 ALOGV("owerwrite flag(%x) for PCM16 multi-channel(CM:%x) playback", flags ,channelMask);
1128 flags = (AudioSystem::output_flags)AUDIO_OUTPUT_FLAG_DIRECT;
1129 }
1130
1131 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1132 // creating an offloaded track and tearing it down immediately after start when audioflinger
1133 // detects there is an active non offloadable effect.
1134 // FIXME: We should check the audio session here but we do not have it in this context.
1135 // This may prevent offloading in rare situations where effects are left active by apps
1136 // in the background.
1137 if ((((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
1138 !isNonOffloadableEffectEnabled()) &&
1139 flags & AUDIO_OUTPUT_FLAG_DIRECT) {
1140 profile = getProfileForDirectOutput(device,
1141 samplingRate,
1142 format,
1143 channelMask,
1144 (audio_output_flags_t)flags);
1145 }
1146
1147 if (profile != NULL) {
1148 AudioOutputDescriptor *outputDesc = NULL;
1149
Subhash Chandra Bose Naripeddy5eb02f72014-04-11 17:54:09 -07001150#ifdef MULTIPLE_OFFLOAD_ENABLED
1151 bool multiOffloadEnabled = false;
1152 char value[PROPERTY_VALUE_MAX] = {0};
1153 property_get("audio.offload.multiple.enabled", value, NULL);
1154 if (atoi(value) || !strncmp("true", value, 4))
1155 multiOffloadEnabled = true;
1156 // if multiple concurrent offload decode is supported
1157 // do no check for reuse and also don't close previous output if its offload
1158 // previous output will be closed during track destruction
1159 if (multiOffloadEnabled)
1160 goto get_output__new_output_desc;
1161#endif
Naresh Tanniru36c08932014-01-27 18:40:53 +05301162 for (size_t i = 0; i < mOutputs.size(); i++) {
1163 AudioOutputDescriptor *desc = mOutputs.valueAt(i);
1164 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1165 outputDesc = desc;
1166 // reuse direct output if currently open and configured with same parameters
1167 if ((samplingRate == outputDesc->mSamplingRate) &&
1168 (format == outputDesc->mFormat) &&
1169 (channelMask == outputDesc->mChannelMask)) {
1170 outputDesc->mDirectOpenCount++;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05301171 ALOGD("getOutput() reusing direct output %d", mOutputs.keyAt(i));
Naresh Tanniru36c08932014-01-27 18:40:53 +05301172 return mOutputs.keyAt(i);
1173 }
1174 }
1175 }
1176 // close direct output if currently open and configured with different parameters
1177 if (outputDesc != NULL) {
1178 closeOutput(outputDesc->mId);
1179 }
Subhash Chandra Bose Naripeddy5eb02f72014-04-11 17:54:09 -07001180get_output__new_output_desc:
Naresh Tanniru36c08932014-01-27 18:40:53 +05301181 outputDesc = new AudioOutputDescriptor(profile);
1182 outputDesc->mDevice = device;
1183 outputDesc->mSamplingRate = samplingRate;
1184 outputDesc->mFormat = (audio_format_t)format;
1185 outputDesc->mChannelMask = (audio_channel_mask_t)channelMask;
1186 outputDesc->mLatency = 0;
1187 outputDesc->mFlags =(audio_output_flags_t) (outputDesc->mFlags | flags);
1188 outputDesc->mRefCount[stream] = 0;
1189 outputDesc->mStopTime[stream] = 0;
1190 outputDesc->mDirectOpenCount = 1;
1191 output = mpClientInterface->openOutput(profile->mModule->mHandle,
1192 &outputDesc->mDevice,
1193 &outputDesc->mSamplingRate,
1194 &outputDesc->mFormat,
1195 &outputDesc->mChannelMask,
1196 &outputDesc->mLatency,
1197 outputDesc->mFlags,
1198 offloadInfo);
1199
1200 // only accept an output with the requested parameters
1201 if (output == 0 ||
1202 (samplingRate != 0 && samplingRate != outputDesc->mSamplingRate) ||
1203 (format != 0 && format != outputDesc->mFormat) ||
1204 (channelMask != 0 && channelMask != outputDesc->mChannelMask)) {
1205 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1206 "format %d %d, channelMask %04x %04x", output, samplingRate,
1207 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1208 outputDesc->mChannelMask);
1209 if (output != 0) {
1210 mpClientInterface->closeOutput(output);
1211 }
1212 delete outputDesc;
1213 return 0;
1214 }
1215 audio_io_handle_t srcOutput = getOutputForEffect();
1216 addOutput(output, outputDesc);
1217 audio_io_handle_t dstOutput = getOutputForEffect();
1218 if (dstOutput == output) {
1219 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1220 }
1221 mPreviousOutputs = mOutputs;
1222 ALOGV("getOutput() returns new direct output %d", output);
1223 return output;
1224 }
1225
1226 // ignoring channel mask due to downmix capability in mixer
1227
1228 // open a non direct output
1229
1230 // for non direct outputs, only PCM is supported
1231 if (audio_is_linear_pcm((audio_format_t)format)) {
1232 // get which output is suitable for the specified stream. The actual
1233 // routing change will happen when startOutput() will be called
1234 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1235
1236 output = selectOutput(outputs, flags);
1237 }
1238 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1239 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1240
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05301241 ALOGD("getOutput() returns output %d", output);
Naresh Tanniru36c08932014-01-27 18:40:53 +05301242
1243 return output;
1244}
1245
1246
1247// This function checks for the parameters which can be offloaded.
1248// This can be enhanced depending on the capability of the DSP and policy
1249// of the system.
1250bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
1251{
Chaithanya Krishna Bacharaju7038d622014-03-27 15:57:27 +05301252 ALOGD("copl: isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Naresh Tanniru36c08932014-01-27 18:40:53 +05301253 " BitRate=%u, duration=%lld us, has_video=%d",
1254 offloadInfo.sample_rate, offloadInfo.channel_mask,
1255 offloadInfo.format,
1256 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
1257 offloadInfo.has_video);
1258
1259#ifdef VOICE_CONCURRENCY
Naresh Tanniru811f4982014-03-18 17:25:32 +05301260 char concpropValue[PROPERTY_VALUE_MAX];
1261 if(property_get("voice.playback.conc.disabled", concpropValue, NULL)) {
1262 bool propenabled = atoi(concpropValue) || !strncmp("true", concpropValue, 4);
1263 if (propenabled) {
1264 if(isInCall())
1265 {
Chaithanya Krishna Bacharaju7038d622014-03-27 15:57:27 +05301266 ALOGD("\n copl: blocking compress offload on call mode\n");
Naresh Tanniru811f4982014-03-18 17:25:32 +05301267 return false;
1268 }
1269 }
Naresh Tanniru36c08932014-01-27 18:40:53 +05301270 }
Naresh Tanniru811f4982014-03-18 17:25:32 +05301271
Naresh Tanniru36c08932014-01-27 18:40:53 +05301272#endif
Naresh Tanniru36c08932014-01-27 18:40:53 +05301273 // Check if stream type is music, then only allow offload as of now.
1274 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
1275 {
Chaithanya Krishna Bacharaju7038d622014-03-27 15:57:27 +05301276 ALOGD("copl: isOffloadSupported: stream_type != MUSIC, returning false");
Naresh Tanniru36c08932014-01-27 18:40:53 +05301277 return false;
1278 }
1279
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001280 char propValue[PROPERTY_VALUE_MAX];
1281 bool pcmOffload = false;
1282 if (audio_is_offload_pcm(offloadInfo.format)) {
1283 if(property_get("audio.offload.pcm.enable", propValue, NULL)) {
Naresh Tanniru36c08932014-01-27 18:40:53 +05301284 bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001285 if (prop_enabled) {
1286 ALOGW("PCM offload property is enabled");
1287 pcmOffload = true;
Naresh Tanniru36c08932014-01-27 18:40:53 +05301288 }
1289 }
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001290 if (!pcmOffload) {
Chaithanya Krishna Bacharaju7038d622014-03-27 15:57:27 +05301291 ALOGD("copl: PCM offload disabled by property audio.offload.pcm.enable");
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001292 return false;
1293 }
1294 }
1295
1296 if (!pcmOffload) {
1297 // Check if offload has been disabled
1298 if (property_get("audio.offload.disable", propValue, "0")) {
1299 if (atoi(propValue) != 0) {
Chaithanya Krishna Bacharaju7038d622014-03-27 15:57:27 +05301300 ALOGD("copl: offload disabled by audio.offload.disable=%s", propValue );
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001301 return false;
Naresh Tanniru36c08932014-01-27 18:40:53 +05301302 }
1303 }
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001304
1305 //check if it's multi-channel AAC format
1306 if (AudioSystem::popCount(offloadInfo.channel_mask) > 2
1307 && offloadInfo.format == AUDIO_FORMAT_AAC) {
Chaithanya Krishna Bacharaju7038d622014-03-27 15:57:27 +05301308 ALOGD("copl: offload disabled for multi-channel AAC format");
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001309 return false;
1310 }
1311
1312 if (offloadInfo.has_video)
1313 {
1314 if(property_get("av.offload.enable", propValue, NULL)) {
1315 bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1316 if (!prop_enabled) {
1317 ALOGW("offload disabled by av.offload.enable = %s ", propValue );
1318 return false;
1319 }
1320 } else {
1321 return false;
1322 }
1323
1324 if(offloadInfo.is_streaming) {
1325 if (property_get("av.streaming.offload.enable", propValue, NULL)) {
1326 bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1327 if (!prop_enabled) {
1328 ALOGW("offload disabled by av.streaming.offload.enable = %s ", propValue );
1329 return false;
1330 }
1331 } else {
1332 //Do not offload AV streamnig if the property is not defined
1333 return false;
1334 }
1335 }
Chaithanya Krishna Bacharaju7038d622014-03-27 15:57:27 +05301336 ALOGD("copl: isOffloadSupported: has_video == true, property\
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001337 set to enable offload");
1338 }
Naresh Tanniru36c08932014-01-27 18:40:53 +05301339 }
1340
1341 //If duration is less than minimum value defined in property, return false
1342 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
1343 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
Chaithanya Krishna Bacharaju7038d622014-03-27 15:57:27 +05301344 ALOGD("copl: Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
Naresh Tanniru36c08932014-01-27 18:40:53 +05301345 return false;
1346 }
1347 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
Chaithanya Krishna Bacharaju7038d622014-03-27 15:57:27 +05301348 ALOGD("copl: Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
Naresh Tanniru36c08932014-01-27 18:40:53 +05301349 //duration checks only valid for MP3/AAC formats,
1350 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001351 if (offloadInfo.format == AUDIO_FORMAT_MP3 || offloadInfo.format == AUDIO_FORMAT_AAC || pcmOffload)
Naresh Tanniru36c08932014-01-27 18:40:53 +05301352 return false;
1353 }
1354
1355 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1356 // creating an offloaded track and tearing it down immediately after start when audioflinger
1357 // detects there is an active non offloadable effect.
1358 // FIXME: We should check the audio session here but we do not have it in this context.
1359 // This may prevent offloading in rare situations where effects are left active by apps
1360 // in the background.
1361 if (isNonOffloadableEffectEnabled()) {
1362 return false;
1363 }
1364
1365 // See if there is a profile to support this.
1366 // AUDIO_DEVICE_NONE
1367 IOProfile *profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
1368 offloadInfo.sample_rate,
1369 offloadInfo.format,
1370 offloadInfo.channel_mask,
1371 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Chaithanya Krishna Bacharaju7038d622014-03-27 15:57:27 +05301372 ALOGD("copl: isOffloadSupported() profile %sfound", profile != NULL ? "" : "NOT ");
Naresh Tanniru36c08932014-01-27 18:40:53 +05301373 return (profile != NULL);
1374}
1375
1376void AudioPolicyManager::setPhoneState(int state)
1377
1378{
Naresh Tanniru811f4982014-03-18 17:25:32 +05301379 ALOGD("setPhoneState() state %d", state);
Naresh Tanniru36c08932014-01-27 18:40:53 +05301380 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
1381 if (state < 0 || state >= AudioSystem::NUM_MODES) {
1382 ALOGW("setPhoneState() invalid state %d", state);
1383 return;
1384 }
1385
1386 if (state == mPhoneState ) {
1387 ALOGW("setPhoneState() setting same state %d", state);
1388 return;
1389 }
1390
1391 // if leaving call state, handle special case of active streams
1392 // pertaining to sonification strategy see handleIncallSonification()
1393 if (isInCall()) {
1394 ALOGV("setPhoneState() in call state management: new state is %d", state);
1395 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
1396 handleIncallSonification(stream, false, true);
1397 }
1398 }
1399
1400 // store previous phone state for management of sonification strategy below
1401 int oldState = mPhoneState;
1402 mPhoneState = state;
1403 bool force = false;
Venkata Narendra Kumar Gutta5f64eea2014-05-28 17:42:10 +05301404 int voice_call_state = 0;
Naresh Tanniru36c08932014-01-27 18:40:53 +05301405
1406 // are we entering or starting a call
1407 if (!isStateInCall(oldState) && isStateInCall(state)) {
1408 ALOGV(" Entering call in setPhoneState()");
1409 // force routing command to audio hardware when starting a call
1410 // even if no device change is needed
1411 force = true;
1412 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
1413 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
1414 sVolumeProfiles[AUDIO_STREAM_VOICE_CALL][j];
1415 }
1416 } else if (isStateInCall(oldState) && !isStateInCall(state)) {
1417 ALOGV(" Exiting call in setPhoneState()");
1418 // force routing command to audio hardware when exiting a call
1419 // even if no device change is needed
1420 force = true;
1421 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
1422 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
1423 sVolumeProfiles[AUDIO_STREAM_DTMF][j];
1424 }
1425 } else if (isStateInCall(state) && (state != oldState)) {
1426 ALOGV(" Switching between telephony and VoIP in setPhoneState()");
1427 // force routing command to audio hardware when switching between telephony and VoIP
1428 // even if no device change is needed
1429 force = true;
1430 }
1431
1432 // check for device and output changes triggered by new phone state
1433 newDevice = getNewDevice(mPrimaryOutput, false /*fromCache*/);
1434 checkA2dpSuspend();
1435 checkOutputForAllStrategies();
1436 updateDevicesAndOutputs();
1437
1438 AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
1439
1440 // force routing command to audio hardware when ending call
1441 // even if no device change is needed
1442 if (isStateInCall(oldState) && newDevice == AUDIO_DEVICE_NONE) {
1443 newDevice = hwOutputDesc->device();
1444 }
Naresh Tanniru811f4982014-03-18 17:25:32 +05301445#ifdef VOICE_CONCURRENCY
1446 char propValue[PROPERTY_VALUE_MAX];
1447 bool prop_playback_enabled = false, prop_rec_enabled=false, prop_voip_enabled = false;
1448
1449 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1450 prop_playback_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1451 }
1452
1453 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
1454 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1455 }
1456
1457 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1458 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1459 }
1460
Venkata Narendra Kumar Gutta5f64eea2014-05-28 17:42:10 +05301461 bool mode_in_call = (AudioSystem::MODE_IN_CALL != oldState) && (AudioSystem::MODE_IN_CALL == state);
1462 //query if it is a actual voice call initiated by telephony
1463 if (mode_in_call) {
1464 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0, String8("in_call"));
1465 AudioParameter result = AudioParameter(valueStr);
1466 if (result.getInt(String8("in_call"), voice_call_state) == NO_ERROR)
1467 ALOGD("SetPhoneState: Voice call state = %d", voice_call_state);
1468 }
Naresh Tanniru811f4982014-03-18 17:25:32 +05301469
Venkata Narendra Kumar Gutta5f64eea2014-05-28 17:42:10 +05301470 if (mode_in_call && voice_call_state) {
1471 ALOGD("Entering to call mode oldState :: %d state::%d ",oldState, state);
1472 mvoice_call_state = voice_call_state;
1473 if (prop_playback_enabled) {
Naresh Tanniru811f4982014-03-18 17:25:32 +05301474 //Call invalidate to reset all opened non ULL audio tracks
1475 // Move tracks associated to this strategy from previous output to new output
1476 for (int i = AudioSystem::SYSTEM; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
1477 ALOGV(" Invalidate on call mode for stream :: %d ", i);
1478 //FIXME see fixme on name change
1479 mpClientInterface->setStreamOutput((AudioSystem::stream_type)i,
1480 0 /* ignored */);
1481 }
1482 }
1483
Venkata Narendra Kumar Gutta5f64eea2014-05-28 17:42:10 +05301484 if (prop_rec_enabled) {
Naresh Tanniru811f4982014-03-18 17:25:32 +05301485 //Close all active inputs
1486 audio_io_handle_t activeInput = getActiveInput();
1487 if (activeInput != 0) {
1488 AudioInputDescriptor *activeDesc = mInputs.valueFor(activeInput);
1489 switch(activeDesc->mInputSource) {
1490 case AUDIO_SOURCE_VOICE_UPLINK:
1491 case AUDIO_SOURCE_VOICE_DOWNLINK:
1492 case AUDIO_SOURCE_VOICE_CALL:
1493 ALOGD("FOUND active input during call active: %d",activeDesc->mInputSource);
1494 break;
1495
1496 case AUDIO_SOURCE_VOICE_COMMUNICATION:
1497 if(prop_voip_enabled) {
1498 ALOGD("CLOSING VoIP input source on call setup :%d ",activeDesc->mInputSource);
1499 stopInput(activeInput);
1500 releaseInput(activeInput);
1501 }
1502 break;
1503
1504 default:
1505 ALOGD("CLOSING input on call setup for inputSource: %d",activeDesc->mInputSource);
1506 stopInput(activeInput);
1507 releaseInput(activeInput);
1508 break;
1509 }
1510 }
Venkata Narendra Kumar Gutta5f64eea2014-05-28 17:42:10 +05301511 } else if (prop_voip_enabled) {
Naresh Tanniru811f4982014-03-18 17:25:32 +05301512 audio_io_handle_t activeInput = getActiveInput();
1513 if (activeInput != 0) {
1514 AudioInputDescriptor *activeDesc = mInputs.valueFor(activeInput);
Venkata Narendra Kumar Gutta5f64eea2014-05-28 17:42:10 +05301515 if (AUDIO_SOURCE_VOICE_COMMUNICATION == activeDesc->mInputSource) {
Naresh Tanniru811f4982014-03-18 17:25:32 +05301516 ALOGD("CLOSING VoIP on call setup : %d",activeDesc->mInputSource);
1517 stopInput(activeInput);
1518 releaseInput(activeInput);
1519 }
1520 }
1521 }
1522
1523 //suspend PCM (deep-buffer) output & close compress & direct tracks
1524 for (size_t i = 0; i < mOutputs.size(); i++) {
1525 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
Naresh Tanniru1fd15d32014-04-25 20:23:19 +05301526 if (!outputDesc || !outputDesc->mProfile) {
1527 ALOGD("ouput desc / profile is NULL");
1528 continue;
1529 }
Naresh Tanniru811f4982014-03-18 17:25:32 +05301530 if (((!outputDesc->isDuplicated() &&outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY))
1531 && prop_playback_enabled) {
Venkata Narendra Kumar Gutta5f64eea2014-05-28 17:42:10 +05301532 ALOGD(" calling suspendOutput on call mode for primary output");
Naresh Tanniru811f4982014-03-18 17:25:32 +05301533 mpClientInterface->suspendOutput(mOutputs.keyAt(i));
1534 } //Close compress all sessions
1535 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
1536 && prop_playback_enabled) {
Venkata Narendra Kumar Gutta5f64eea2014-05-28 17:42:10 +05301537 ALOGD(" calling closeOutput on call mode for COMPRESS output");
Naresh Tanniru811f4982014-03-18 17:25:32 +05301538 closeOutput(mOutputs.keyAt(i));
1539 }
1540 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_VOIP_RX)
1541 && prop_voip_enabled) {
Venkata Narendra Kumar Gutta5f64eea2014-05-28 17:42:10 +05301542 ALOGD(" calling closeOutput on call mode for DIRECT output");
Naresh Tanniru811f4982014-03-18 17:25:32 +05301543 closeOutput(mOutputs.keyAt(i));
1544 }
1545 }
1546 }
1547
Venkata Narendra Kumar Gutta5f64eea2014-05-28 17:42:10 +05301548 if ((AudioSystem::MODE_IN_CALL == oldState) && (AudioSystem::MODE_IN_CALL != state)
1549 && prop_playback_enabled && mvoice_call_state) {
Naresh Tanniru811f4982014-03-18 17:25:32 +05301550 ALOGD("EXITING from call mode oldState :: %d state::%d \n",oldState, state);
Venkata Narendra Kumar Gutta5f64eea2014-05-28 17:42:10 +05301551 mvoice_call_state = 0;
Naresh Tanniru811f4982014-03-18 17:25:32 +05301552 //restore PCM (deep-buffer) output after call termination
1553 for (size_t i = 0; i < mOutputs.size(); i++) {
1554 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
Naresh Tanniru1fd15d32014-04-25 20:23:19 +05301555 if (!outputDesc || !outputDesc->mProfile) {
1556 ALOGD("ouput desc / profile is NULL");
1557 continue;
1558 }
Naresh Tanniru811f4982014-03-18 17:25:32 +05301559 if (!outputDesc->isDuplicated() &&outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1560 ALOGD("calling restoreOutput after call mode for primary output");
1561 mpClientInterface->restoreOutput(mOutputs.keyAt(i));
1562 }
1563 }
1564 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
1565 for (int i = AudioSystem::SYSTEM; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
1566 ALOGD("Invalidate on call mode for stream :: %d ", i);
1567 //FIXME see fixme on name change
1568 mpClientInterface->setStreamOutput((AudioSystem::stream_type)i,
1569 0 /* ignored */);
1570 }
1571 }
Naresh Tanniru811f4982014-03-18 17:25:32 +05301572#endif
Karthik Reddy Katta060a6c42014-05-20 15:21:28 +05301573 mPrevPhoneState = oldState;
Naresh Tanniru36c08932014-01-27 18:40:53 +05301574
1575 int delayMs = 0;
1576 if (isStateInCall(state)) {
1577 nsecs_t sysTime = systemTime();
1578 for (size_t i = 0; i < mOutputs.size(); i++) {
1579 AudioOutputDescriptor *desc = mOutputs.valueAt(i);
1580 // mute media and sonification strategies and delay device switch by the largest
1581 // latency of any output where either strategy is active.
1582 // This avoid sending the ring tone or music tail into the earpiece or headset.
1583 if ((desc->isStrategyActive(STRATEGY_MEDIA,
1584 SONIFICATION_HEADSET_MUSIC_DELAY,
1585 sysTime) ||
1586 desc->isStrategyActive(STRATEGY_SONIFICATION,
1587 SONIFICATION_HEADSET_MUSIC_DELAY,
1588 sysTime)) &&
1589 (delayMs < (int)desc->mLatency*2)) {
1590 delayMs = desc->mLatency*2;
1591 }
1592 setStrategyMute(STRATEGY_MEDIA, true, mOutputs.keyAt(i));
1593 setStrategyMute(STRATEGY_MEDIA, false, mOutputs.keyAt(i), MUTE_TIME_MS,
1594 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
1595 setStrategyMute(STRATEGY_SONIFICATION, true, mOutputs.keyAt(i));
1596 setStrategyMute(STRATEGY_SONIFICATION, false, mOutputs.keyAt(i), MUTE_TIME_MS,
1597 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
1598 }
1599 }
1600
1601 // change routing is necessary
1602 setOutputDevice(mPrimaryOutput, newDevice, force, delayMs);
1603
1604 // if entering in call state, handle special case of active streams
1605 // pertaining to sonification strategy see handleIncallSonification()
1606 if (isStateInCall(state)) {
1607 ALOGV("setPhoneState() in call state management: new state is %d", state);
1608 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
1609 handleIncallSonification(stream, true, true);
1610 }
1611 }
1612
1613 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
1614 if (state == AudioSystem::MODE_RINGTONE &&
1615 isStreamActive(AudioSystem::MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
1616 mLimitRingtoneVolume = true;
1617 } else {
1618 mLimitRingtoneVolume = false;
1619 }
Naresh Tanniru811f4982014-03-18 17:25:32 +05301620 ALOGD(" End of setPhoneState ... mPhoneState: %d ",mPhoneState);
Naresh Tanniru36c08932014-01-27 18:40:53 +05301621}
1622
Karthik Reddy Katta060a6c42014-05-20 15:21:28 +05301623bool AudioPolicyManager::isStateInCall(int state)
1624{
1625 return ((state == AudioSystem::MODE_IN_CALL) || (state == AudioSystem::MODE_IN_COMMUNICATION) ||
1626 ((state == AudioSystem::MODE_RINGTONE) && (mPrevPhoneState == AudioSystem::MODE_IN_CALL)));
1627}
1628
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001629extern "C" AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001630{
1631 return new AudioPolicyManager(clientInterface);
1632}
1633
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001634extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001635{
1636 delete interface;
1637}
1638
1639}; // namespace android