blob: 553eb46c3a5246e19649fe2cd6da17ff51377d55 [file] [log] [blame]
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001/*
Mingming Yin0ae14ea2014-07-09 17:55:56 -07002 * 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
Mingming Yin0670f162014-06-12 16:05:49 -070034#include <utils/Log.h>
Mingming Yin0670f162014-06-12 16:05:49 -070035#include "AudioPolicyManager.h"
Mingming Yin0ae14ea2014-07-09 17:55:56 -070036#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>
Mingming Yin0670f162014-06-12 16:05:49 -070041
Mingming Yin0ae14ea2014-07-09 17:55:56 -070042namespace android_audio_legacy {
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070043
44// ----------------------------------------------------------------------------
45// AudioPolicyInterface implementation
46// ----------------------------------------------------------------------------
Mingming Yin0ae14ea2014-07-09 17:55:56 -070047const char* AudioPolicyManager::HDMI_SPKR_STR = "hdmi_spkr";
48int 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,
Mingming Yin0ae14ea2014-07-09 17:55:56 -070051 AudioSystem::device_connection_state state,
52 const char *device_address)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070053{
Mingming Yin0ae14ea2014-07-09 17:55:56 -070054 SortedVector <audio_io_handle_t> outputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070055
Mingming Yin0ae14ea2014-07-09 17:55:56 -070056 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
Mingming Yin0ae14ea2014-07-09 17:55:56 -070061 if (strlen(device_address) >= MAX_DEVICE_ADDRESS_LEN) {
62 ALOGE("setDeviceConnectionState() invalid address: %s", device_address);
63 return BAD_VALUE;
64 }
65
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070066 // handle output devices
67 if (audio_is_output_device(device)) {
68
Mingming Yin0ae14ea2014-07-09 17:55:56 -070069 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 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070081
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
Mingming Yin0ae14ea2014-07-09 17:55:56 -070088 case AudioSystem::DEVICE_STATE_AVAILABLE:
89 if (mAvailableOutputDevices & device) {
90#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
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700104 if (checkOutputsForDevice(device, state, outputs) != NO_ERROR) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700105 return INVALID_OPERATION;
106 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700107 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %d outputs",
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700108 outputs.size());
109 // register new device as available
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700110 mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices | device);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700111
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700112#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
124 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 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700149 break;
150 // handle output device disconnection
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700151 case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
152 if (!(mAvailableOutputDevices & device)) {
153#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
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700168 mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices & ~device);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700169
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700170#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
179 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 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700191 // 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
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700208 if ((state == AudioSystem::DEVICE_STATE_UNAVAILABLE) ||
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700209 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
210 (desc->mDirectOpenCount == 0))) {
211 closeOutput(outputs[i]);
212 }
213 }
214 }
215
216 updateDevicesAndOutputs();
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700217 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
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700233 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.
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700237 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),
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700246 cachedDevice,
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700247 !mOutputs.valueAt(i)->isDuplicated(),
248 0);
249 }
250
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700251 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 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700263 // handle input devices
264 if (audio_is_input_device(device)) {
265
266 switch (state)
267 {
268 // handle input device connection
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700269 case AudioSystem::DEVICE_STATE_AVAILABLE: {
270 if (mAvailableInputDevices & device) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700271 ALOGW("setDeviceConnectionState() device already connected: %d", device);
272 return INVALID_OPERATION;
273 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700274 mAvailableInputDevices = mAvailableInputDevices | (device & ~AUDIO_DEVICE_BIT_IN);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700275 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700276 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700277
278 // handle input device disconnection
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700279 case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
280 if (!(mAvailableInputDevices & device)) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700281 ALOGW("setDeviceConnectionState() device not connected: %d", device);
282 return INVALID_OPERATION;
283 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700284 mAvailableInputDevices = (audio_devices_t) (mAvailableInputDevices & ~device);
285 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700286
287 default:
288 ALOGE("setDeviceConnectionState() invalid state: %x", state);
289 return BAD_VALUE;
290 }
291
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700292 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 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700305
306 return NO_ERROR;
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700307 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700308
309 ALOGW("setDeviceConnectionState() invalid device: %x", device);
310 return BAD_VALUE;
311}
312
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700313void AudioPolicyManager::setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700314{
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700315 ALOGD("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700316
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700317 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;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700372 }
373
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700374 // 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
412 ALOGD("getInput() inputSource %d, samplingRate %d, format %d, channelMask %x, acoustics %x",
413 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
420#ifdef VOICE_CONCURRENCY
421
422 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
433 if (prop_rec_enabled && mvoice_call_state) {
434 //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
448 case AUDIO_SOURCE_VOICE_COMMUNICATION:
449 if(prop_voip_enabled) {
450 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
451 return 0;
452 }
453 break;
454 default:
455 ALOGD("BLOCKING input during incall mode for inputSource: %d ",inputSource);
456 return 0;
457 }
458 }
459 }//check for VoIP flag
460 else if(prop_voip_enabled && mvoice_call_state) {
461 //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
476 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);
520 ALOGD("getInput() returns input %d", input);
521
522 return input;
523}
524
525AudioPolicyManager::routing_strategy AudioPolicyManager::getStrategy(AudioSystem::stream_type stream)
526{
527 // 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:
548#endif
549#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 }
556
557}
558
559audio_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;
636 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
637 if (device) break;
638 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
639 if (device) break;
640 }
641
642 // Allow voice call on USB ANLG DOCK headset
643 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;
672 device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
673 if (device) break;
674 }
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 }
683 // 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
692 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;
722
723 if (isInCall() && (device == AUDIO_DEVICE_NONE)) {
724 // 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
735 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 }
765 if ((strategy != STRATEGY_SONIFICATION) && (device == AUDIO_DEVICE_NONE)
766 && (device2 == AUDIO_DEVICE_NONE)) {
767 // no sonification on aux digital (e.g. HDMI)
768 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
769 }
770 if ((device2 == AUDIO_DEVICE_NONE) &&
771 (mForceUse[AudioSystem::FOR_DOCK] == AudioSystem::FORCE_ANALOG_DOCK)
772 && (strategy != STRATEGY_SONIFICATION)) {
773 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
774 }
775#ifdef AUDIO_EXTN_FM_ENABLED
776 if ((strategy != STRATEGY_SONIFICATION) && (device == AUDIO_DEVICE_NONE)
777 && (device2 == AUDIO_DEVICE_NONE)) {
778 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_FM_TX;
779 }
780#endif
781#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
782 if ((strategy != STRATEGY_SONIFICATION) && (device == AUDIO_DEVICE_NONE)
783 && (device2 == AUDIO_DEVICE_NONE)) {
784 // no sonification on WFD sink
785 device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_PROXY;
786 }
787#endif
788 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
811audio_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:
859 device = AUDIO_DEVICE_IN_FM_RX;
860 break;
861 case AUDIO_SOURCE_FM_RX_A2DP:
862 device = AUDIO_DEVICE_IN_FM_RX_A2DP;
863 break;
864#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;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700900 }
901}
902
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700903status_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)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700909{
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700910 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;
958 // 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;
963 }
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}
1006
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
1022 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
1033 if (prop_play_enabled && mvoice_call_state) {
1034 //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 }
1051 } else if (prop_voip_enabled && mvoice_call_state) {
1052 //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 }
1065 }
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
1080 ALOGD(" getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x ",
1081 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
1150#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
1162 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++;
1171 ALOGD("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1172 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 }
1180get_output__new_output_desc:
1181 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
1241 ALOGD("getOutput() returns output %d", output);
1242
1243 return output;
1244}
1245
Zhou Songc0d78c22014-06-16 10:48:22 +08001246status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
1247 AudioSystem::stream_type stream,
1248 int session)
1249{
1250 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1251 ssize_t index = mOutputs.indexOfKey(output);
1252 if (index < 0) {
1253 ALOGW("stopOutput() unknow output %d", output);
1254 return BAD_VALUE;
1255 }
1256
1257 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
1258
1259 // handle special case for sonification while in call
1260 if ((isInCall()) && (outputDesc->mRefCount[stream] == 1)) {
1261 handleIncallSonification(stream, false, false);
1262 }
1263
1264 if (outputDesc->mRefCount[stream] > 0) {
1265 // decrement usage count of this stream on the output
1266 outputDesc->changeRefCount(stream, -1);
1267 // store time at which the stream was stopped - see isStreamActive()
1268 if (outputDesc->mRefCount[stream] == 0) {
1269 outputDesc->mStopTime[stream] = systemTime();
1270 audio_devices_t newDevice = getNewDevice(output, false /*fromCache*/);
1271 // delay the device switch by twice the latency because stopOutput() is executed when
1272 // the track stop() command is received and at that time the audio track buffer can
1273 // still contain data that needs to be drained. The latency only covers the audio HAL
1274 // and kernel buffers. Also the latency does not always include additional delay in the
1275 // audio path (audio DSP, CODEC ...)
1276#ifdef VOICE_CONCURRENCY
1277 //if newDevice is invalid for voice stream, cancel the unexecuted device routing
1278 //command(if existed)which could be handled later in command queue for current output
1279 if (newDevice == AUDIO_DEVICE_NONE && stream == AudioSystem::VOICE_CALL)
1280 setOutputDevice(output, newDevice, true);
1281 else
1282#endif
1283 setOutputDevice(output, newDevice, false, outputDesc->mLatency*2);
1284
1285 // force restoring the device selection on other active outputs if it differs from the
1286 // one being selected for this output
1287 for (size_t i = 0; i < mOutputs.size(); i++) {
1288 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1289 AudioOutputDescriptor *desc = mOutputs.valueAt(i);
1290 if (curOutput != output &&
1291 desc->isActive() &&
1292 outputDesc->sharesHwModuleWith(desc) &&
1293 (newDevice != desc->device())) {
1294 setOutputDevice(curOutput,
1295 getNewDevice(curOutput, false /*fromCache*/),
1296 true,
1297 outputDesc->mLatency*2);
1298 }
1299 }
1300 // update the outputs if stopping one with a stream that can affect notification routing
1301 handleNotificationRoutingForStream(stream);
1302 }
1303 return NO_ERROR;
1304 } else {
1305 ALOGW("stopOutput() refcount is already 0 for output %d", output);
1306 return INVALID_OPERATION;
1307 }
1308}
1309
1310//private function, no changes from AudioPolicyManagerBase
1311void AudioPolicyManager::handleNotificationRoutingForStream(AudioSystem::stream_type stream) {
1312 switch(stream) {
1313 case AudioSystem::MUSIC:
1314 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
1315 updateDevicesAndOutputs();
1316 break;
1317 default:
1318 break;
1319 }
1320}
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001321
1322// This function checks for the parameters which can be offloaded.
1323// This can be enhanced depending on the capability of the DSP and policy
1324// of the system.
1325bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
1326{
1327 ALOGD("copl: isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
1328 " BitRate=%u, duration=%lld us, has_video=%d",
1329 offloadInfo.sample_rate, offloadInfo.channel_mask,
1330 offloadInfo.format,
1331 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
1332 offloadInfo.has_video);
1333
1334#ifdef VOICE_CONCURRENCY
1335 char concpropValue[PROPERTY_VALUE_MAX];
1336 if(property_get("voice.playback.conc.disabled", concpropValue, NULL)) {
1337 bool propenabled = atoi(concpropValue) || !strncmp("true", concpropValue, 4);
1338 if (propenabled) {
1339 if(isInCall())
1340 {
1341 ALOGD("\n copl: blocking compress offload on call mode\n");
1342 return false;
1343 }
1344 }
1345 }
1346
1347#endif
1348 // Check if stream type is music, then only allow offload as of now.
1349 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
1350 {
1351 ALOGD("copl: isOffloadSupported: stream_type != MUSIC, returning false");
1352 return false;
1353 }
1354
1355 char propValue[PROPERTY_VALUE_MAX];
1356 bool pcmOffload = false;
1357 if (audio_is_offload_pcm(offloadInfo.format)) {
1358 if(property_get("audio.offload.pcm.enable", propValue, NULL)) {
1359 bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1360 if (prop_enabled) {
1361 ALOGW("PCM offload property is enabled");
1362 pcmOffload = true;
1363 }
1364 }
1365 if (!pcmOffload) {
1366 ALOGD("copl: PCM offload disabled by property audio.offload.pcm.enable");
1367 return false;
1368 }
1369 }
1370
1371 if (!pcmOffload) {
1372 // Check if offload has been disabled
1373 if (property_get("audio.offload.disable", propValue, "0")) {
1374 if (atoi(propValue) != 0) {
1375 ALOGD("copl: offload disabled by audio.offload.disable=%s", propValue );
1376 return false;
1377 }
1378 }
1379
1380 //check if it's multi-channel AAC format
1381 if (AudioSystem::popCount(offloadInfo.channel_mask) > 2
1382 && offloadInfo.format == AUDIO_FORMAT_AAC) {
1383 ALOGD("copl: offload disabled for multi-channel AAC format");
1384 return false;
1385 }
1386
1387 if (offloadInfo.has_video)
1388 {
1389 if(property_get("av.offload.enable", propValue, NULL)) {
1390 bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1391 if (!prop_enabled) {
1392 ALOGW("offload disabled by av.offload.enable = %s ", propValue );
1393 return false;
1394 }
1395 } else {
1396 return false;
1397 }
1398
1399 if(offloadInfo.is_streaming) {
1400 if (property_get("av.streaming.offload.enable", propValue, NULL)) {
1401 bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1402 if (!prop_enabled) {
1403 ALOGW("offload disabled by av.streaming.offload.enable = %s ", propValue );
1404 return false;
1405 }
1406 } else {
1407 //Do not offload AV streamnig if the property is not defined
1408 return false;
1409 }
1410 }
1411 ALOGD("copl: isOffloadSupported: has_video == true, property\
1412 set to enable offload");
1413 }
1414 }
1415
1416 //If duration is less than minimum value defined in property, return false
1417 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
1418 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
1419 ALOGD("copl: Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
1420 return false;
1421 }
1422 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
1423 ALOGD("copl: Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
1424 //duration checks only valid for MP3/AAC formats,
1425 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
1426 if (offloadInfo.format == AUDIO_FORMAT_MP3 || offloadInfo.format == AUDIO_FORMAT_AAC || pcmOffload)
1427 return false;
1428 }
1429
1430 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1431 // creating an offloaded track and tearing it down immediately after start when audioflinger
1432 // detects there is an active non offloadable effect.
1433 // FIXME: We should check the audio session here but we do not have it in this context.
1434 // This may prevent offloading in rare situations where effects are left active by apps
1435 // in the background.
1436 if (isNonOffloadableEffectEnabled()) {
1437 return false;
1438 }
1439
1440 // See if there is a profile to support this.
1441 // AUDIO_DEVICE_NONE
1442 IOProfile *profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
1443 offloadInfo.sample_rate,
1444 offloadInfo.format,
1445 offloadInfo.channel_mask,
1446 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1447 ALOGD("copl: isOffloadSupported() profile %sfound", profile != NULL ? "" : "NOT ");
1448 return (profile != NULL);
1449}
1450
1451void AudioPolicyManager::setPhoneState(int state)
1452
1453{
1454 ALOGD("setPhoneState() state %d", state);
Naresh Tanniru36c08932014-01-27 18:40:53 +05301455 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001456 if (state < 0 || state >= AudioSystem::NUM_MODES) {
Naresh Tanniru36c08932014-01-27 18:40:53 +05301457 ALOGW("setPhoneState() invalid state %d", state);
1458 return;
1459 }
1460
1461 if (state == mPhoneState ) {
1462 ALOGW("setPhoneState() setting same state %d", state);
1463 return;
1464 }
1465
1466 // if leaving call state, handle special case of active streams
1467 // pertaining to sonification strategy see handleIncallSonification()
1468 if (isInCall()) {
1469 ALOGV("setPhoneState() in call state management: new state is %d", state);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001470 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
1471 handleIncallSonification(stream, false, true);
Naresh Tanniru36c08932014-01-27 18:40:53 +05301472 }
1473 }
1474
1475 // store previous phone state for management of sonification strategy below
1476 int oldState = mPhoneState;
1477 mPhoneState = state;
1478 bool force = false;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001479 int voice_call_state = 0;
Naresh Tanniru36c08932014-01-27 18:40:53 +05301480
1481 // are we entering or starting a call
1482 if (!isStateInCall(oldState) && isStateInCall(state)) {
1483 ALOGV(" Entering call in setPhoneState()");
1484 // force routing command to audio hardware when starting a call
1485 // even if no device change is needed
1486 force = true;
1487 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
1488 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
1489 sVolumeProfiles[AUDIO_STREAM_VOICE_CALL][j];
1490 }
1491 } else if (isStateInCall(oldState) && !isStateInCall(state)) {
1492 ALOGV(" Exiting call in setPhoneState()");
1493 // force routing command to audio hardware when exiting a call
1494 // even if no device change is needed
1495 force = true;
1496 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
1497 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
1498 sVolumeProfiles[AUDIO_STREAM_DTMF][j];
1499 }
1500 } else if (isStateInCall(state) && (state != oldState)) {
1501 ALOGV(" Switching between telephony and VoIP in setPhoneState()");
1502 // force routing command to audio hardware when switching between telephony and VoIP
1503 // even if no device change is needed
1504 force = true;
1505 }
1506
1507 // check for device and output changes triggered by new phone state
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001508 newDevice = getNewDevice(mPrimaryOutput, false /*fromCache*/);
Naresh Tanniru36c08932014-01-27 18:40:53 +05301509 checkA2dpSuspend();
1510 checkOutputForAllStrategies();
1511 updateDevicesAndOutputs();
1512
1513 AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
1514
1515 // force routing command to audio hardware when ending call
1516 // even if no device change is needed
1517 if (isStateInCall(oldState) && newDevice == AUDIO_DEVICE_NONE) {
1518 newDevice = hwOutputDesc->device();
1519 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001520#ifdef VOICE_CONCURRENCY
1521 char propValue[PROPERTY_VALUE_MAX];
1522 bool prop_playback_enabled = false, prop_rec_enabled=false, prop_voip_enabled = false;
1523
1524 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1525 prop_playback_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1526 }
1527
1528 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
1529 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1530 }
1531
1532 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1533 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1534 }
1535
1536 bool mode_in_call = (AudioSystem::MODE_IN_CALL != oldState) && (AudioSystem::MODE_IN_CALL == state);
1537 //query if it is a actual voice call initiated by telephony
1538 if (mode_in_call) {
1539 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0, String8("in_call"));
1540 AudioParameter result = AudioParameter(valueStr);
1541 if (result.getInt(String8("in_call"), voice_call_state) == NO_ERROR)
1542 ALOGD("SetPhoneState: Voice call state = %d", voice_call_state);
1543 }
1544
1545 if (mode_in_call && voice_call_state) {
1546 ALOGD("Entering to call mode oldState :: %d state::%d ",oldState, state);
1547 mvoice_call_state = voice_call_state;
1548 if (prop_playback_enabled) {
1549 //Call invalidate to reset all opened non ULL audio tracks
1550 // Move tracks associated to this strategy from previous output to new output
1551 for (int i = AudioSystem::SYSTEM; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
1552 ALOGV(" Invalidate on call mode for stream :: %d ", i);
1553 //FIXME see fixme on name change
1554 mpClientInterface->setStreamOutput((AudioSystem::stream_type)i,
1555 0 /* ignored */);
1556 }
1557 }
1558
1559 if (prop_rec_enabled) {
1560 //Close all active inputs
1561 audio_io_handle_t activeInput = getActiveInput();
1562 if (activeInput != 0) {
1563 AudioInputDescriptor *activeDesc = mInputs.valueFor(activeInput);
1564 switch(activeDesc->mInputSource) {
1565 case AUDIO_SOURCE_VOICE_UPLINK:
1566 case AUDIO_SOURCE_VOICE_DOWNLINK:
1567 case AUDIO_SOURCE_VOICE_CALL:
1568 ALOGD("FOUND active input during call active: %d",activeDesc->mInputSource);
1569 break;
1570
1571 case AUDIO_SOURCE_VOICE_COMMUNICATION:
1572 if(prop_voip_enabled) {
1573 ALOGD("CLOSING VoIP input source on call setup :%d ",activeDesc->mInputSource);
1574 stopInput(activeInput);
1575 releaseInput(activeInput);
1576 }
1577 break;
1578
1579 default:
1580 ALOGD("CLOSING input on call setup for inputSource: %d",activeDesc->mInputSource);
1581 stopInput(activeInput);
1582 releaseInput(activeInput);
1583 break;
1584 }
1585 }
1586 } else if (prop_voip_enabled) {
1587 audio_io_handle_t activeInput = getActiveInput();
1588 if (activeInput != 0) {
1589 AudioInputDescriptor *activeDesc = mInputs.valueFor(activeInput);
1590 if (AUDIO_SOURCE_VOICE_COMMUNICATION == activeDesc->mInputSource) {
1591 ALOGD("CLOSING VoIP on call setup : %d",activeDesc->mInputSource);
1592 stopInput(activeInput);
1593 releaseInput(activeInput);
1594 }
1595 }
1596 }
1597
1598 //suspend PCM (deep-buffer) output & close compress & direct tracks
1599 for (size_t i = 0; i < mOutputs.size(); i++) {
1600 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
1601 if (!outputDesc || !outputDesc->mProfile) {
1602 ALOGD("ouput desc / profile is NULL");
1603 continue;
1604 }
1605 if (((!outputDesc->isDuplicated() &&outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY))
1606 && prop_playback_enabled) {
1607 ALOGD(" calling suspendOutput on call mode for primary output");
1608 mpClientInterface->suspendOutput(mOutputs.keyAt(i));
1609 } //Close compress all sessions
1610 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
1611 && prop_playback_enabled) {
1612 ALOGD(" calling closeOutput on call mode for COMPRESS output");
1613 closeOutput(mOutputs.keyAt(i));
1614 }
1615 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_VOIP_RX)
1616 && prop_voip_enabled) {
1617 ALOGD(" calling closeOutput on call mode for DIRECT output");
1618 closeOutput(mOutputs.keyAt(i));
1619 }
1620 }
1621 }
1622
1623 if ((AudioSystem::MODE_IN_CALL == oldState) && (AudioSystem::MODE_IN_CALL != state)
1624 && prop_playback_enabled && mvoice_call_state) {
1625 ALOGD("EXITING from call mode oldState :: %d state::%d \n",oldState, state);
1626 mvoice_call_state = 0;
1627 //restore PCM (deep-buffer) output after call termination
1628 for (size_t i = 0; i < mOutputs.size(); i++) {
1629 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
1630 if (!outputDesc || !outputDesc->mProfile) {
1631 ALOGD("ouput desc / profile is NULL");
1632 continue;
1633 }
1634 if (!outputDesc->isDuplicated() &&outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1635 ALOGD("calling restoreOutput after call mode for primary output");
1636 mpClientInterface->restoreOutput(mOutputs.keyAt(i));
1637 }
1638 }
1639 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
1640 for (int i = AudioSystem::SYSTEM; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
1641 ALOGD("Invalidate on call mode for stream :: %d ", i);
1642 //FIXME see fixme on name change
1643 mpClientInterface->setStreamOutput((AudioSystem::stream_type)i,
1644 0 /* ignored */);
1645 }
1646 }
1647#endif
1648 mPrevPhoneState = oldState;
Naresh Tanniru36c08932014-01-27 18:40:53 +05301649
1650 int delayMs = 0;
1651 if (isStateInCall(state)) {
1652 nsecs_t sysTime = systemTime();
1653 for (size_t i = 0; i < mOutputs.size(); i++) {
1654 AudioOutputDescriptor *desc = mOutputs.valueAt(i);
1655 // mute media and sonification strategies and delay device switch by the largest
1656 // latency of any output where either strategy is active.
1657 // This avoid sending the ring tone or music tail into the earpiece or headset.
1658 if ((desc->isStrategyActive(STRATEGY_MEDIA,
1659 SONIFICATION_HEADSET_MUSIC_DELAY,
1660 sysTime) ||
1661 desc->isStrategyActive(STRATEGY_SONIFICATION,
1662 SONIFICATION_HEADSET_MUSIC_DELAY,
1663 sysTime)) &&
1664 (delayMs < (int)desc->mLatency*2)) {
1665 delayMs = desc->mLatency*2;
1666 }
1667 setStrategyMute(STRATEGY_MEDIA, true, mOutputs.keyAt(i));
1668 setStrategyMute(STRATEGY_MEDIA, false, mOutputs.keyAt(i), MUTE_TIME_MS,
1669 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
1670 setStrategyMute(STRATEGY_SONIFICATION, true, mOutputs.keyAt(i));
1671 setStrategyMute(STRATEGY_SONIFICATION, false, mOutputs.keyAt(i), MUTE_TIME_MS,
1672 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
1673 }
1674 }
1675
1676 // change routing is necessary
1677 setOutputDevice(mPrimaryOutput, newDevice, force, delayMs);
1678
1679 // if entering in call state, handle special case of active streams
1680 // pertaining to sonification strategy see handleIncallSonification()
1681 if (isStateInCall(state)) {
1682 ALOGV("setPhoneState() in call state management: new state is %d", state);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001683 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
1684 handleIncallSonification(stream, true, true);
Naresh Tanniru36c08932014-01-27 18:40:53 +05301685 }
1686 }
1687
1688 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001689 if (state == AudioSystem::MODE_RINGTONE &&
1690 isStreamActive(AudioSystem::MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Naresh Tanniru36c08932014-01-27 18:40:53 +05301691 mLimitRingtoneVolume = true;
1692 } else {
1693 mLimitRingtoneVolume = false;
1694 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001695 ALOGD(" End of setPhoneState ... mPhoneState: %d ",mPhoneState);
Naresh Tanniru36c08932014-01-27 18:40:53 +05301696}
1697
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001698bool AudioPolicyManager::isStateInCall(int state)
Karthik Reddy Katta060a6c42014-05-20 15:21:28 +05301699{
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001700 return ((state == AudioSystem::MODE_IN_CALL) || (state == AudioSystem::MODE_IN_COMMUNICATION) ||
1701 ((state == AudioSystem::MODE_RINGTONE) && (mPrevPhoneState == AudioSystem::MODE_IN_CALL)));
Karthik Reddy Katta060a6c42014-05-20 15:21:28 +05301702}
1703
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001704extern "C" AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001705{
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001706 return new AudioPolicyManager(clientInterface);
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001707}
1708
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001709extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001710{
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001711 delete interface;
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001712}
1713
1714}; // namespace android