blob: 122ac147aea560036e9749c835b15738ad254f73 [file] [log] [blame]
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001/*
Weiyin Jiangd211ef52016-02-16 15:55:11 +08002 * Copyright (c) 2013-2016, 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.
Gao Jiedb057832015-11-12 08:51:22 +080018 *
19 * This file was modified by Dolby Laboratories, Inc. The portions of the
20 * code that are surrounded by "DOLBY..." are copyrighted and
21 * licensed separately, as follows:
22 *
23 * (C) 2015 Dolby Laboratories, Inc.
24 *
25 * Licensed under the Apache License, Version 2.0 (the "License");
26 * you may not use this file except in compliance with the License.
27 * You may obtain a copy of the License at
28 *
29 * http://www.apache.org/licenses/LICENSE-2.0
30 *
31 * Unless required by applicable law or agreed to in writing, software
32 * distributed under the License is distributed on an "AS IS" BASIS,
33 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34 * See the License for the specific language governing permissions and
35 * limitations under the License.
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070036 */
37
Sharad Sangle36781612015-05-28 16:15:16 +053038#define LOG_TAG "AudioPolicyManagerCustom"
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070039//#define LOG_NDEBUG 0
40
41//#define VERY_VERBOSE_LOGGING
42#ifdef VERY_VERBOSE_LOGGING
43#define ALOGVV ALOGV
44#else
45#define ALOGVV(a...) do { } while(0)
46#endif
Sharad Sangle36781612015-05-28 16:15:16 +053047
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070048// A device mask for all audio output devices that are considered "remote" when evaluating
49// active output devices in isStreamActiveRemotely()
50#define APM_AUDIO_OUT_DEVICE_REMOTE_ALL AUDIO_DEVICE_OUT_REMOTE_SUBMIX
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070051// A device mask for all audio input and output devices where matching inputs/outputs on device
52// type alone is not enough: the address must match too
53#define APM_AUDIO_DEVICE_MATCH_ADDRESS_ALL (AUDIO_DEVICE_IN_REMOTE_SUBMIX | \
54 AUDIO_DEVICE_OUT_REMOTE_SUBMIX)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070055
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070056#include <inttypes.h>
Mingming Yin0ae14ea2014-07-09 17:55:56 -070057#include <math.h>
Mingming Yin0670f162014-06-12 16:05:49 -070058
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070059#include <cutils/properties.h>
60#include <utils/Log.h>
61#include <hardware/audio.h>
62#include <hardware/audio_effect.h>
63#include <media/AudioParameter.h>
64#include <soundtrigger/SoundTrigger.h>
65#include "AudioPolicyManager.h"
Sharad Sangle36781612015-05-28 16:15:16 +053066#include <policy.h>
Gao Jiedb057832015-11-12 08:51:22 +080067#ifdef DOLBY_ENABLE
68#include "DolbyAudioPolicy_impl.h"
69#endif // DOLBY_END
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070070
71namespace android {
Sharad Sanglec5766ff2015-06-04 20:24:10 +053072#ifdef VOICE_CONCURRENCY
73audio_output_flags_t AudioPolicyManagerCustom::getFallBackPath()
74{
75 audio_output_flags_t flag = AUDIO_OUTPUT_FLAG_FAST;
76 char propValue[PROPERTY_VALUE_MAX];
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070077
Sharad Sanglec5766ff2015-06-04 20:24:10 +053078 if (property_get("voice.conc.fallbackpath", propValue, NULL)) {
79 if (!strncmp(propValue, "deep-buffer", 11)) {
80 flag = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
81 }
82 else if (!strncmp(propValue, "fast", 4)) {
83 flag = AUDIO_OUTPUT_FLAG_FAST;
84 }
85 else {
86 ALOGD("voice_conc:not a recognised path(%s) in prop voice.conc.fallbackpath",
87 propValue);
88 }
89 }
90 else {
91 ALOGD("voice_conc:prop voice.conc.fallbackpath not set");
92 }
93
94 ALOGD("voice_conc:picked up flag(0x%x) from prop voice.conc.fallbackpath",
95 flag);
96
97 return flag;
98}
99#endif /*VOICE_CONCURRENCY*/
Ashish Jaine4a22d52016-06-22 11:55:08 +0530100
101void AudioPolicyManagerCustom::moveGlobalEffect()
102{
103 audio_io_handle_t dstOutput = getOutputForEffect();
104 if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) {
105#ifdef DOLBY_ENABLE
106 status_t status =
107 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
108 mPrimaryOutput->mIoHandle,
109 dstOutput);
110 if (status == NO_ERROR) {
111 for (size_t i = 0; i < mEffects.size(); i++) {
112 sp<EffectDescriptor> desc = mEffects.valueAt(i);
113 if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX) {
114 // update the mIo member of EffectDescriptor
115 // for the global effect
116 ALOGV("%s updating mIo", __FUNCTION__);
117 desc->mIo = dstOutput;
118 }
119 }
120 } else {
121 ALOGW("%s moveEffects from %d to %d failed", __FUNCTION__,
122 mPrimaryOutput->mIoHandle, dstOutput);
123 }
124#else // DOLBY_END
125 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
126 mPrimaryOutput->mIoHandle, dstOutput);
127#endif
128 }
129}
130
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700131// ----------------------------------------------------------------------------
132// AudioPolicyInterface implementation
133// ----------------------------------------------------------------------------
Sharad Sangle36781612015-05-28 16:15:16 +0530134extern "C" AudioPolicyInterface* createAudioPolicyManager(
135 AudioPolicyClientInterface *clientInterface)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700136{
Sharad Sangle36781612015-05-28 16:15:16 +0530137 return new AudioPolicyManagerCustom(clientInterface);
138}
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700139
Sharad Sangle36781612015-05-28 16:15:16 +0530140extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface)
141{
142 delete interface;
143}
144
145status_t AudioPolicyManagerCustom::setDeviceConnectionStateInt(audio_devices_t device,
146 audio_policy_dev_state_t state,
147 const char *device_address,
148 const char *device_name)
149{
Ashish Jain40cab3a2016-03-23 11:14:14 +0530150 ALOGD("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
Sharad Sangle36781612015-05-28 16:15:16 +0530151 device, state, device_address, device_name);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700152
153 // connect/disconnect only 1 device at a time
154 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
155
Sharad Sangle36781612015-05-28 16:15:16 +0530156 sp<DeviceDescriptor> devDesc =
157 mHwModules.getDeviceDescriptor(device, device_address, device_name);
158
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700159 // handle output devices
160 if (audio_is_output_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700161 SortedVector <audio_io_handle_t> outputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700162
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700163 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700164
165 // save a copy of the opened output descriptors before any output is opened or closed
166 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
167 mPreviousOutputs = mOutputs;
168 switch (state)
169 {
170 // handle output device connection
Sharad Sangle36781612015-05-28 16:15:16 +0530171 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700172 if (index >= 0) {
Sharad Sangle4509cef2015-08-19 20:47:12 +0530173#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
174 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
175 if (!strncmp(device_address, "hdmi_spkr", 9)) {
176 mHdmiAudioDisabled = false;
177 } else {
178 mHdmiAudioEvent = true;
179 }
180 }
181#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700182 ALOGW("setDeviceConnectionState() device already connected: %x", device);
183 return INVALID_OPERATION;
184 }
185 ALOGV("setDeviceConnectionState() connecting device %x", device);
186
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700187 // register new device as available
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700188 index = mAvailableOutputDevices.add(devDesc);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530189#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
190 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
191 if (!strncmp(device_address, "hdmi_spkr", 9)) {
192 mHdmiAudioDisabled = false;
193 } else {
194 mHdmiAudioEvent = true;
195 }
196 if (mHdmiAudioDisabled || !mHdmiAudioEvent) {
197 mAvailableOutputDevices.remove(devDesc);
198 ALOGW("HDMI sink not connected, do not route audio to HDMI out");
199 return INVALID_OPERATION;
200 }
201 }
202#endif
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700203 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530204 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700205 if (module == 0) {
206 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
207 device);
208 mAvailableOutputDevices.remove(devDesc);
209 return INVALID_OPERATION;
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700210 }
Sharad Sangle36781612015-05-28 16:15:16 +0530211 mAvailableOutputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700212 } else {
213 return NO_MEMORY;
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700214 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700215
Sharad Sangle36781612015-05-28 16:15:16 +0530216 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700217 mAvailableOutputDevices.remove(devDesc);
218 return INVALID_OPERATION;
219 }
Sharad Sangle36781612015-05-28 16:15:16 +0530220 // Propagate device availability to Engine
221 mEngine->setDeviceConnectionState(devDesc, state);
222
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700223 // outputs should never be empty here
224 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
225 "checkOutputsForDevice() returned no outputs but status OK");
226 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
227 outputs.size());
Sharad Sangle36781612015-05-28 16:15:16 +0530228
229 // Send connect to HALs
230 AudioParameter param = AudioParameter(devDesc->mAddress);
231 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
232 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
233
234 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700235 // handle output device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700236 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
237 if (index < 0) {
Sharad Sangle4509cef2015-08-19 20:47:12 +0530238#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
239 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
240 if (!strncmp(device_address, "hdmi_spkr", 9)) {
241 mHdmiAudioDisabled = true;
242 } else {
243 mHdmiAudioEvent = false;
244 }
245 }
246#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700247 ALOGW("setDeviceConnectionState() device not connected: %x", device);
248 return INVALID_OPERATION;
249 }
250
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700251 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
252
Sharad Sangle36781612015-05-28 16:15:16 +0530253 // Send Disconnect to HALs
254 AudioParameter param = AudioParameter(devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700255 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
256 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
257
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700258 // remove device from available output devices
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700259 mAvailableOutputDevices.remove(devDesc);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530260#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
261 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
262 if (!strncmp(device_address, "hdmi_spkr", 9)) {
263 mHdmiAudioDisabled = true;
264 } else {
265 mHdmiAudioEvent = false;
266 }
267 }
268#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530269 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
270
271 // Propagate device availability to Engine
272 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700273 } break;
274
275 default:
276 ALOGE("setDeviceConnectionState() invalid state: %x", state);
277 return BAD_VALUE;
278 }
279
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700280 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
281 // output is suspended before any tracks are moved to it
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700282 checkA2dpSuspend();
283 checkOutputForAllStrategies();
284 // outputs must be closed after checkOutputForAllStrategies() is executed
285 if (!outputs.isEmpty()) {
286 for (size_t i = 0; i < outputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530287 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700288 // close unused outputs after device disconnection or direct outputs that have been
289 // opened by checkOutputsForDevice() to query dynamic parameters
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700290 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700291 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
292 (desc->mDirectOpenCount == 0))) {
293 closeOutput(outputs[i]);
294 }
295 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700296 // check again after closing A2DP output to reset mA2dpSuspended if needed
297 checkA2dpSuspend();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700298 }
299
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530300#ifdef FM_POWER_OPT
301 // handle FM device connection state to trigger FM AFE loopback
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800302 if (device == AUDIO_DEVICE_OUT_FM && hasPrimaryOutput()) {
Mingming Yin2dd77c22016-01-06 18:02:42 -0800303 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530304 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
305 mPrimaryOutput->changeRefCount(AUDIO_STREAM_MUSIC, 1);
Mingming Yin2dd77c22016-01-06 18:02:42 -0800306 newDevice = (audio_devices_t)(getNewOutputDevice(mPrimaryOutput, false)|AUDIO_DEVICE_OUT_FM);
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800307 mFMIsActive = true;
Zhou Songbd0264e2016-06-21 11:01:06 +0800308 mPrimaryOutput->mDevice = newDevice & ~AUDIO_DEVICE_OUT_FM;
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530309 } else {
Mingming Yin2dd77c22016-01-06 18:02:42 -0800310 newDevice = (audio_devices_t)(getNewOutputDevice(mPrimaryOutput, false));
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800311 mFMIsActive = false;
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530312 mPrimaryOutput->changeRefCount(AUDIO_STREAM_MUSIC, -1);
313 }
314 AudioParameter param = AudioParameter();
315 param.addInt(String8("handle_fm"), (int)newDevice);
316 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString());
317 }
318#endif /* FM_POWER_OPT end */
319
Zhou Songbd0264e2016-06-21 11:01:06 +0800320 updateDevicesAndOutputs();
321#ifdef DOLBY_ENABLE
322 // Before closing the opened outputs, update endpoint property with device capabilities
323 audio_devices_t audioOutputDevice = getDeviceForStrategy(getStrategy(AUDIO_STREAM_MUSIC), true);
324 mDolbyAudioPolicy.setEndpointSystemProperty(audioOutputDevice, mHwModules);
325#endif // DOLBY_END
326 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
327 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
328 updateCallRouting(newDevice);
329 }
330
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700331 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530332 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
333 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
334 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700335 // do not force device change on duplicated output because if device is 0, it will
336 // also force a device 0 for the two outputs it is duplicated to which may override
337 // a valid device selection on those outputs.
Sharad Sangle36781612015-05-28 16:15:16 +0530338 bool force = !desc->isDuplicated()
339 && (!device_distinguishes_on_address(device)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700340 // always force when disconnecting (a non-duplicated device)
341 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Sharad Sangle36781612015-05-28 16:15:16 +0530342 setOutputDevice(desc, newDevice, force, 0);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700343 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700344 }
345
Ashish Jain40cab3a2016-03-23 11:14:14 +0530346 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
347 cleanUpForDevice(devDesc);
348 }
349
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700350 mpClientInterface->onAudioPortListUpdate();
351 return NO_ERROR;
352 } // end if is output device
353
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700354 // handle input devices
355 if (audio_is_input_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700356 SortedVector <audio_io_handle_t> inputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700357
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700358 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700359 switch (state)
360 {
361 // handle input device connection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700362 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
363 if (index >= 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700364 ALOGW("setDeviceConnectionState() device already connected: %d", device);
365 return INVALID_OPERATION;
366 }
Sharad Sangle36781612015-05-28 16:15:16 +0530367 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700368 if (module == NULL) {
369 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
370 device);
371 return INVALID_OPERATION;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700372 }
Sharad Sangle36781612015-05-28 16:15:16 +0530373 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700374 return INVALID_OPERATION;
375 }
376
377 index = mAvailableInputDevices.add(devDesc);
378 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530379 mAvailableInputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700380 } else {
381 return NO_MEMORY;
382 }
Sharad Sangle36781612015-05-28 16:15:16 +0530383
384 // Set connect to HALs
385 AudioParameter param = AudioParameter(devDesc->mAddress);
386 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
387 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
388
389 // Propagate device availability to Engine
390 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700391 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700392
393 // handle input device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700394 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
395 if (index < 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700396 ALOGW("setDeviceConnectionState() device not connected: %d", device);
397 return INVALID_OPERATION;
398 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700399
400 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
401
402 // Set Disconnect to HALs
Sharad Sangle36781612015-05-28 16:15:16 +0530403 AudioParameter param = AudioParameter(devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700404 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
405 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
406
Sharad Sangle36781612015-05-28 16:15:16 +0530407 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700408 mAvailableInputDevices.remove(devDesc);
409
Sharad Sangle36781612015-05-28 16:15:16 +0530410 // Propagate device availability to Engine
411 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700412 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700413
414 default:
415 ALOGE("setDeviceConnectionState() invalid state: %x", state);
416 return BAD_VALUE;
417 }
418
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700419 closeAllInputs();
420
Sharad Sangle36781612015-05-28 16:15:16 +0530421 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700422 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
423 updateCallRouting(newDevice);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700424 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700425
Ashish Jain40cab3a2016-03-23 11:14:14 +0530426 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
427 cleanUpForDevice(devDesc);
428 }
429
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700430 mpClientInterface->onAudioPortListUpdate();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700431 return NO_ERROR;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700432 } // end if is input device
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700433
434 ALOGW("setDeviceConnectionState() invalid device: %x", device);
435 return BAD_VALUE;
436}
Weiyin Jiangd211ef52016-02-16 15:55:11 +0800437
Sharad Sangle36781612015-05-28 16:15:16 +0530438// This function checks for the parameters which can be offloaded.
439// This can be enhanced depending on the capability of the DSP and policy
440// of the system.
441bool AudioPolicyManagerCustom::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700442{
Sharad Sangle36781612015-05-28 16:15:16 +0530443 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
444 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
445 offloadInfo.sample_rate, offloadInfo.channel_mask,
446 offloadInfo.format,
447 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
448 offloadInfo.has_video);
Ashish Jain40cab3a2016-03-23 11:14:14 +0530449
450 if (mMasterMono) {
451 return false; // no offloading if mono is set.
452 }
453
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530454#ifdef VOICE_CONCURRENCY
455 char concpropValue[PROPERTY_VALUE_MAX];
456 if (property_get("voice.playback.conc.disabled", concpropValue, NULL)) {
457 bool propenabled = atoi(concpropValue) || !strncmp("true", concpropValue, 4);
458 if (propenabled) {
459 if (isInCall())
460 {
461 ALOGD("\n copl: blocking compress offload on call mode\n");
462 return false;
463 }
464 }
465 }
466#endif
467#ifdef RECORD_PLAY_CONCURRENCY
468 char recConcPropValue[PROPERTY_VALUE_MAX];
469 bool prop_rec_play_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700470
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530471 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
472 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
473 }
474
475 if ((prop_rec_play_enabled) &&
476 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
477 ALOGD("copl: blocking compress offload for record concurrency");
478 return false;
479 }
480#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530481 // Check if stream type is music, then only allow offload as of now.
482 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
483 {
484 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
485 return false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700486 }
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530487
Alexy Joseph0ac09032015-10-16 15:57:53 -0700488 // Check if offload has been disabled
489 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
490 if (offloadDisabled) {
491 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
492 return false;
493 }
494
Ashish Jainac510a72016-04-28 12:22:58 +0530495 //check if it's multi-channel AAC (includes sub formats) and FLAC format
496 if ((popcount(offloadInfo.channel_mask) > 2) &&
497 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
498 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS))) {
499 ALOGD("offload disabled for multi-channel AAC,FLAC and VORBIS format");
500 return false;
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530501 }
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530502
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530503#ifdef AUDIO_EXTN_FORMATS_ENABLED
Ashish Jainac510a72016-04-28 12:22:58 +0530504 //check if it's multi-channel FLAC/ALAC/WMA format with sample rate > 48k
505 if ((popcount(offloadInfo.channel_mask) > 2) &&
506 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
507 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) && (offloadInfo.sample_rate > 48000)) ||
508 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) && (offloadInfo.sample_rate > 48000)) ||
509 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) && (offloadInfo.sample_rate > 48000)) ||
510 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS))) {
511 ALOGD("offload disabled for multi-channel FLAC/ALAC/WMA/AAC_ADTS clips with sample rate > 48kHz");
512 return false;
513 }
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530514#endif
Ashish Jainac510a72016-04-28 12:22:58 +0530515 //TODO: enable audio offloading with video when ready
516 const bool allowOffloadWithVideo =
517 property_get_bool("audio.offload.video", false /* default_value */);
518 if (offloadInfo.has_video && !allowOffloadWithVideo) {
519 ALOGV("isOffloadSupported: has_video == true, returning false");
520 return false;
521 }
Manish Dewanganf3cd0f82015-10-13 14:04:36 +0530522
Ashish Jainac510a72016-04-28 12:22:58 +0530523 const bool allowOffloadStreamingWithVideo = property_get_bool("av.streaming.offload.enable",
524 false /*default value*/);
525 if (offloadInfo.has_video && offloadInfo.is_streaming && !allowOffloadStreamingWithVideo) {
526 ALOGW("offload disabled by av.streaming.offload.enable %d",allowOffloadStreamingWithVideo);
527 return false;
Sharad Sangle36781612015-05-28 16:15:16 +0530528 }
529
530 //If duration is less than minimum value defined in property, return false
Ashish Jainac510a72016-04-28 12:22:58 +0530531 char propValue[PROPERTY_VALUE_MAX];
Sharad Sangle36781612015-05-28 16:15:16 +0530532 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
533 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
534 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
535 return false;
536 }
537 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
538 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
539 //duration checks only valid for MP3/AAC/ formats,
540 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
541 if ((offloadInfo.format == AUDIO_FORMAT_MP3) ||
542 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
543 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
Ashish Jainac510a72016-04-28 12:22:58 +0530544 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS))
545 return false;
546
547#ifdef AUDIO_EXTN_FORMATS_ENABLED
548 if (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) ||
Sharad Sangle36781612015-05-28 16:15:16 +0530549 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) ||
550 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) ||
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530551 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_APE) ||
Ashish Jainac510a72016-04-28 12:22:58 +0530552 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS))
Sharad Sangle36781612015-05-28 16:15:16 +0530553 return false;
Ashish Jainac510a72016-04-28 12:22:58 +0530554#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530555 }
556
557 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
558 // creating an offloaded track and tearing it down immediately after start when audioflinger
559 // detects there is an active non offloadable effect.
560 // FIXME: We should check the audio session here but we do not have it in this context.
561 // This may prevent offloading in rare situations where effects are left active by apps
562 // in the background.
563 if (mEffects.isNonOffloadableEffectEnabled()) {
564 return false;
565 }
Dhananjay Kumarcf558ba2016-07-29 19:33:13 +0530566
Sharad Sangle36781612015-05-28 16:15:16 +0530567 // See if there is a profile to support this.
568 // AUDIO_DEVICE_NONE
569 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
570 offloadInfo.sample_rate,
571 offloadInfo.format,
572 offloadInfo.channel_mask,
573 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
574 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
575 return (profile != 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700576}
Weiyin Jiangd211ef52016-02-16 15:55:11 +0800577
Sharad Sangle36781612015-05-28 16:15:16 +0530578audio_devices_t AudioPolicyManagerCustom::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
579 bool fromCache)
580{
581 audio_devices_t device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700582
Ashish Jain40cab3a2016-03-23 11:14:14 +0530583 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Sharad Sangle36781612015-05-28 16:15:16 +0530584 if (index >= 0) {
585 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
586 if (patchDesc->mUid != mUidCached) {
587 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Ashish Jain40cab3a2016-03-23 11:14:14 +0530588 outputDesc->device(), outputDesc->getPatchHandle());
Sharad Sangle36781612015-05-28 16:15:16 +0530589 return outputDesc->device();
590 }
591 }
592
593 // check the following by order of priority to request a routing change if necessary:
594 // 1: the strategy enforced audible is active and enforced on the output:
595 // use device for strategy enforced audible
596 // 2: we are in call or the strategy phone is active on the output:
597 // use device for strategy phone
598 // 3: the strategy for enforced audible is active but not enforced on the output:
599 // use the device for strategy enforced audible
600 // 4: the strategy sonification is active on the output:
601 // use device for strategy sonification
602 // 5: the strategy "respectful" sonification is active on the output:
603 // use device for strategy "respectful" sonification
604 // 6: the strategy accessibility is active on the output:
605 // use device for strategy accessibility
606 // 7: the strategy media is active on the output:
607 // use device for strategy media
608 // 8: the strategy DTMF is active on the output:
609 // use device for strategy DTMF
610 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
611 // use device for strategy t-t-s
612 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
613 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
614 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
615 } else if (isInCall() ||
616 isStrategyActive(outputDesc, STRATEGY_PHONE)||
617 isStrategyActive(mPrimaryOutput, STRATEGY_PHONE)) {
618 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
619 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
620 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
621 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)||
622 (isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION)
623 && (!isStrategyActive(mPrimaryOutput,STRATEGY_MEDIA)))) {
624 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530625 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL) ||
626 isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION_RESPECTFUL)) {
Sharad Sangle36781612015-05-28 16:15:16 +0530627 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
628 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
629 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
630 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
631 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
632 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
633 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
634 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
635 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
636 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
637 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
638 }
639
640 ALOGV("getNewOutputDevice() selected device %x", device);
641 return device;
642}
Weiyin Jiangd211ef52016-02-16 15:55:11 +0800643
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700644void AudioPolicyManagerCustom::setPhoneState(audio_mode_t state)
645{
Ashish Jain40cab3a2016-03-23 11:14:14 +0530646 ALOGD("setPhoneState() state %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530647 // store previous phone state for management of sonification strategy below
Sharad Sangle4509cef2015-08-19 20:47:12 +0530648 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Sharad Sangle36781612015-05-28 16:15:16 +0530649 int oldState = mEngine->getPhoneState();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700650
Sharad Sangle36781612015-05-28 16:15:16 +0530651 if (mEngine->setPhoneState(state) != NO_ERROR) {
652 ALOGW("setPhoneState() invalid or same state %d", state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700653 return;
654 }
Sharad Sangle36781612015-05-28 16:15:16 +0530655 /// Opens: can these line be executed after the switch of volume curves???
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700656 // if leaving call state, handle special case of active streams
657 // pertaining to sonification strategy see handleIncallSonification()
Zhou Song58081b62016-02-24 13:10:55 +0800658 if (isStateInCall(oldState)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700659 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530660 for (size_t j = 0; j < mOutputs.size(); j++) {
661 audio_io_handle_t curOutput = mOutputs.keyAt(j);
662 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
663 if (stream == AUDIO_STREAM_PATCH) {
664 continue;
665 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530666 handleIncallSonification((audio_stream_type_t)stream, false, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +0530667 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700668 }
Sharad Sangle36781612015-05-28 16:15:16 +0530669
Zhou Song58081b62016-02-24 13:10:55 +0800670 // force reevaluating accessibility routing when call stops
Sharad Sangle36781612015-05-28 16:15:16 +0530671 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700672 }
673
Sharad Sangle36781612015-05-28 16:15:16 +0530674 /**
675 * Switching to or from incall state or switching between telephony and VoIP lead to force
676 * routing command.
677 */
678 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
679 || (is_state_in_call(state) && (state != oldState)));
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700680
681 // check for device and output changes triggered by new phone state
682 checkA2dpSuspend();
683 checkOutputForAllStrategies();
684 updateDevicesAndOutputs();
685
Sharad Sangle36781612015-05-28 16:15:16 +0530686 sp<SwAudioOutputDescriptor> hwOutputDesc = mPrimaryOutput;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530687#ifdef VOICE_CONCURRENCY
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530688 char propValue[PROPERTY_VALUE_MAX];
689 bool prop_playback_enabled = false, prop_rec_enabled=false, prop_voip_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700690
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530691 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
692 prop_playback_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
693 }
694
695 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
696 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
697 }
698
699 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
700 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
701 }
702
Dhananjay Kumar9125b2a2015-12-01 16:24:09 +0530703 if ((AUDIO_MODE_IN_CALL != oldState) && (AUDIO_MODE_IN_CALL == state)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530704 ALOGD("voice_conc:Entering to call mode oldState :: %d state::%d ",
705 oldState, state);
Dhananjay Kumar9125b2a2015-12-01 16:24:09 +0530706 mvoice_call_state = state;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530707 if (prop_rec_enabled) {
708 //Close all active inputs
709 audio_io_handle_t activeInput = mInputs.getActiveInput();
710 if (activeInput != 0) {
711 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
712 switch(activeDesc->mInputSource) {
713 case AUDIO_SOURCE_VOICE_UPLINK:
714 case AUDIO_SOURCE_VOICE_DOWNLINK:
715 case AUDIO_SOURCE_VOICE_CALL:
716 ALOGD("voice_conc:FOUND active input during call active: %d",activeDesc->mInputSource);
717 break;
718
719 case AUDIO_SOURCE_VOICE_COMMUNICATION:
720 if(prop_voip_enabled) {
721 ALOGD("voice_conc:CLOSING VoIP input source on call setup :%d ",activeDesc->mInputSource);
722 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
723 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
724 }
725 break;
726
727 default:
728 ALOGD("voice_conc:CLOSING input on call setup for inputSource: %d",activeDesc->mInputSource);
729 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
730 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
731 break;
732 }
733 }
734 } else if (prop_voip_enabled) {
735 audio_io_handle_t activeInput = mInputs.getActiveInput();
736 if (activeInput != 0) {
737 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
738 if (AUDIO_SOURCE_VOICE_COMMUNICATION == activeDesc->mInputSource) {
739 ALOGD("voice_conc:CLOSING VoIP on call setup : %d",activeDesc->mInputSource);
740 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
741 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
742 }
743 }
744 }
745 if (prop_playback_enabled) {
746 // Move tracks associated to this strategy from previous output to new output
747 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
748 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
749 if (i == AUDIO_STREAM_PATCH) {
750 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
751 continue;
752 }
753 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
754 if ((AUDIO_STREAM_MUSIC == i) ||
755 (AUDIO_STREAM_VOICE_CALL == i) ) {
756 ALOGD("voice_conc:Invalidate stream type %d", i);
757 mpClientInterface->invalidateStream((audio_stream_type_t)i);
758 }
759 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
760 ALOGD("voice_conc:Invalidate stream type %d", i);
761 mpClientInterface->invalidateStream((audio_stream_type_t)i);
762 }
763 }
764 }
765
766 for (size_t i = 0; i < mOutputs.size(); i++) {
767 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
768 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
769 ALOGD("voice_conc:ouput desc / profile is NULL");
770 continue;
771 }
772
773 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
774 if (((!outputDesc->isDuplicated() &&outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY))
775 && prop_playback_enabled) {
776 ALOGD("voice_conc:calling suspendOutput on call mode for primary output");
777 mpClientInterface->suspendOutput(mOutputs.keyAt(i));
778 } //Close compress all sessions
779 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
780 && prop_playback_enabled) {
781 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
782 closeOutput(mOutputs.keyAt(i));
783 }
784 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_VOIP_RX)
785 && prop_voip_enabled) {
786 ALOGD("voice_conc:calling closeOutput on call mode for DIRECT output");
787 closeOutput(mOutputs.keyAt(i));
788 }
789 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
790 if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)
791 && prop_playback_enabled) {
792 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
793 closeOutput(mOutputs.keyAt(i));
794 }
795 }
796 }
Ashish Jaine4a22d52016-06-22 11:55:08 +0530797 // If effects where present on any of the above closed outputs,
798 // audioflinger moved them to the primary output by default
799 // move them back to the appropriate output.
800 moveGlobalEffect();
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530801 }
802
803 if ((AUDIO_MODE_IN_CALL == oldState || AUDIO_MODE_IN_COMMUNICATION == oldState) &&
804 (AUDIO_MODE_NORMAL == state) && prop_playback_enabled && mvoice_call_state) {
805 ALOGD("voice_conc:EXITING from call mode oldState :: %d state::%d \n",oldState, state);
806 mvoice_call_state = 0;
807 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
808 //restore PCM (deep-buffer) output after call termination
809 for (size_t i = 0; i < mOutputs.size(); i++) {
810 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
811 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
812 ALOGD("voice_conc:ouput desc / profile is NULL");
813 continue;
814 }
815 if (!outputDesc->isDuplicated() && outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
816 ALOGD("voice_conc:calling restoreOutput after call mode for primary output");
817 mpClientInterface->restoreOutput(mOutputs.keyAt(i));
818 }
819 }
820 }
821 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
822 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
823 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
824 if (i == AUDIO_STREAM_PATCH) {
825 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
826 continue;
827 }
828 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
829 if ((AUDIO_STREAM_MUSIC == i) ||
830 (AUDIO_STREAM_VOICE_CALL == i) ) {
831 mpClientInterface->invalidateStream((audio_stream_type_t)i);
832 }
833 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
834 mpClientInterface->invalidateStream((audio_stream_type_t)i);
835 }
836 }
837 }
838
839#endif
840#ifdef RECORD_PLAY_CONCURRENCY
841 char recConcPropValue[PROPERTY_VALUE_MAX];
842 bool prop_rec_play_enabled = false;
843
844 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
845 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
846 }
847 if (prop_rec_play_enabled) {
848 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
849 ALOGD("phone state changed to MODE_IN_COMM invlaidating music and voice streams");
850 // call invalidate for voice streams, so that it can use deepbuffer with VoIP out device from HAL
851 mpClientInterface->invalidateStream(AUDIO_STREAM_VOICE_CALL);
852 // call invalidate for music, so that compress will fallback to deep-buffer with VoIP out device
853 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
854
855 // close compress output to make sure session will be closed before timeout(60sec)
856 for (size_t i = 0; i < mOutputs.size(); i++) {
857
858 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
859 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
860 ALOGD("ouput desc / profile is NULL");
861 continue;
862 }
863
864 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
865 ALOGD("calling closeOutput on call mode for COMPRESS output");
866 closeOutput(mOutputs.keyAt(i));
867 }
868 }
Ashish Jaine4a22d52016-06-22 11:55:08 +0530869 // If effects where present on any of the above closed outputs,
870 // audioflinger moved them to the primary output by default
871 // move them back to the appropriate output.
872 moveGlobalEffect();
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530873 } else if ((oldState == AUDIO_MODE_IN_COMMUNICATION) &&
874 (mEngine->getPhoneState() == AUDIO_MODE_NORMAL)) {
875 // call invalidate for music so that music can fallback to compress
876 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
877 }
878 }
879#endif
880 mPrevPhoneState = oldState;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700881 int delayMs = 0;
882 if (isStateInCall(state)) {
883 nsecs_t sysTime = systemTime();
884 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530885 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700886 // mute media and sonification strategies and delay device switch by the largest
887 // latency of any output where either strategy is active.
888 // This avoid sending the ring tone or music tail into the earpiece or headset.
Sharad Sangle36781612015-05-28 16:15:16 +0530889 if ((isStrategyActive(desc, STRATEGY_MEDIA,
890 SONIFICATION_HEADSET_MUSIC_DELAY,
891 sysTime) ||
892 isStrategyActive(desc, STRATEGY_SONIFICATION,
893 SONIFICATION_HEADSET_MUSIC_DELAY,
894 sysTime)) &&
895 (delayMs < (int)desc->latency()*2)) {
896 delayMs = desc->latency()*2;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700897 }
Sharad Sangle36781612015-05-28 16:15:16 +0530898 setStrategyMute(STRATEGY_MEDIA, true, desc);
899 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700900 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Sharad Sangle36781612015-05-28 16:15:16 +0530901 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
902 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700903 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
904 }
905 }
906
Sharad Sangle36781612015-05-28 16:15:16 +0530907 if (hasPrimaryOutput()) {
908 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
909 // the device returned is not necessarily reachable via this output
910 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
911 // force routing command to audio hardware when ending call
912 // even if no device change is needed
913 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
914 rxDevice = mPrimaryOutput->device();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700915 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700916
Sharad Sangle36781612015-05-28 16:15:16 +0530917 if (state == AUDIO_MODE_IN_CALL) {
918 updateCallRouting(rxDevice, delayMs);
919 } else if (oldState == AUDIO_MODE_IN_CALL) {
920 if (mCallRxPatch != 0) {
921 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
922 mCallRxPatch.clear();
923 }
924 if (mCallTxPatch != 0) {
925 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
926 mCallTxPatch.clear();
927 }
928 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
929 } else {
930 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700931 }
932 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530933 //update device for all non-primary outputs
934 for (size_t i = 0; i < mOutputs.size(); i++) {
935 audio_io_handle_t output = mOutputs.keyAt(i);
936 if (output != mPrimaryOutput->mIoHandle) {
937 newDevice = getNewOutputDevice(mOutputs.valueFor(output), false /*fromCache*/);
938 setOutputDevice(mOutputs.valueFor(output), newDevice, (newDevice != AUDIO_DEVICE_NONE));
939 }
940 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700941 // if entering in call state, handle special case of active streams
942 // pertaining to sonification strategy see handleIncallSonification()
943 if (isStateInCall(state)) {
944 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530945 for (size_t j = 0; j < mOutputs.size(); j++) {
946 audio_io_handle_t curOutput = mOutputs.keyAt(j);
947 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
948 if (stream == AUDIO_STREAM_PATCH) {
949 continue;
950 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530951 handleIncallSonification((audio_stream_type_t)stream, true, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +0530952 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700953 }
Zhou Song58081b62016-02-24 13:10:55 +0800954
955 // force reevaluating accessibility routing when call starts
956 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700957 }
958
959 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
960 if (state == AUDIO_MODE_RINGTONE &&
961 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
962 mLimitRingtoneVolume = true;
963 } else {
964 mLimitRingtoneVolume = false;
965 }
966}
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +0530967
968void AudioPolicyManagerCustom::setForceUse(audio_policy_force_use_t usage,
969 audio_policy_forced_cfg_t config)
970{
Ashish Jain40cab3a2016-03-23 11:14:14 +0530971 ALOGD("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +0530972
973 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
974 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
975 return;
976 }
977 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
978 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
979 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
980
981 // check for device and output changes triggered by new force usage
982 checkA2dpSuspend();
983 checkOutputForAllStrategies();
984 updateDevicesAndOutputs();
Ashish Jain40cab3a2016-03-23 11:14:14 +0530985
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +0530986 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
987 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
988 updateCallRouting(newDevice);
989 }
990 // Use reverse loop to make sure any low latency usecases (generally tones)
991 // are not routed before non LL usecases (generally music).
992 // We can safely assume that LL output would always have lower index,
993 // and use this work-around to avoid routing of output with music stream
994 // from the context of short lived LL output.
995 // Note: in case output's share backend(HAL sharing is implicit) all outputs
996 // gets routing update while processing first output itself.
997 for (size_t i = mOutputs.size(); i > 0; i--) {
998 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i-1);
999 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
1000 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || outputDesc != mPrimaryOutput) {
1001 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
1002 }
1003 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
1004 applyStreamVolumes(outputDesc, newDevice, 0, true);
1005 }
1006 }
1007
1008 audio_io_handle_t activeInput = mInputs.getActiveInput();
1009 if (activeInput != 0) {
Ashish Jain40cab3a2016-03-23 11:14:14 +05301010 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
1011 audio_devices_t newDevice = getNewInputDevice(activeInput);
1012 // Force new input selection if the new device can not be reached via current input
1013 if (activeDesc->mProfile->getSupportedDevices().types() & (newDevice & ~AUDIO_DEVICE_BIT_IN)) {
1014 setInputDevice(activeInput, newDevice);
1015 } else {
1016 closeInput(activeInput);
1017 }
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301018 }
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301019}
1020
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301021status_t AudioPolicyManagerCustom::stopSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301022 audio_stream_type_t stream,
1023 bool forceDeviceUpdate)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001024{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301025 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1026 ALOGW("stopSource() invalid stream %d", stream);
1027 return INVALID_OPERATION;
1028 }
Sharad Sangle36781612015-05-28 16:15:16 +05301029 // always handle stream stop, check which stream type is stopping
1030 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001031
Sharad Sangle36781612015-05-28 16:15:16 +05301032 // handle special case for sonification while in call
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001033 if (isInCall()) {
Sharad Sangle36781612015-05-28 16:15:16 +05301034 if (outputDesc->isDuplicated()) {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301035 handleIncallSonification(stream, false, false, outputDesc->subOutput1()->mIoHandle);
1036 handleIncallSonification(stream, false, false, outputDesc->subOutput2()->mIoHandle);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001037 }
Sharad Sangle36781612015-05-28 16:15:16 +05301038 handleIncallSonification(stream, false, false, outputDesc->mIoHandle);
1039 }
1040
1041 if (outputDesc->mRefCount[stream] > 0) {
1042 // decrement usage count of this stream on the output
1043 outputDesc->changeRefCount(stream, -1);
1044
1045 // store time at which the stream was stopped - see isStreamActive()
1046 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
1047 outputDesc->mStopTime[stream] = systemTime();
Zhou Song5dcddc92015-09-21 14:36:57 +08001048 audio_devices_t prevDevice = outputDesc->device();
Sharad Sangle36781612015-05-28 16:15:16 +05301049 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
1050 // delay the device switch by twice the latency because stopOutput() is executed when
1051 // the track stop() command is received and at that time the audio track buffer can
1052 // still contain data that needs to be drained. The latency only covers the audio HAL
1053 // and kernel buffers. Also the latency does not always include additional delay in the
1054 // audio path (audio DSP, CODEC ...)
1055 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
1056
1057 // force restoring the device selection on other active outputs if it differs from the
1058 // one being selected for this output
1059 for (size_t i = 0; i < mOutputs.size(); i++) {
1060 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1061 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1062 if (desc != outputDesc &&
1063 desc->isActive() &&
1064 outputDesc->sharesHwModuleWith(desc) &&
1065 (newDevice != desc->device())) {
Sharad Sangle4509cef2015-08-19 20:47:12 +05301066 audio_devices_t dev = getNewOutputDevice(mOutputs.valueFor(curOutput), false /*fromCache*/);
Zhou Song5dcddc92015-09-21 14:36:57 +08001067 uint32_t delayMs;
1068 if (dev == prevDevice) {
1069 delayMs = 0;
1070 } else {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301071 delayMs = outputDesc->latency()*2;
Zhou Song5dcddc92015-09-21 14:36:57 +08001072 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301073 setOutputDevice(desc,
1074 dev,
Sharad Sangle36781612015-05-28 16:15:16 +05301075 true,
Zhou Song5dcddc92015-09-21 14:36:57 +08001076 delayMs);
Sharad Sangle36781612015-05-28 16:15:16 +05301077 }
1078 }
1079 // update the outputs if stopping one with a stream that can affect notification routing
1080 handleNotificationRoutingForStream(stream);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001081 }
Sharad Sangle36781612015-05-28 16:15:16 +05301082 return NO_ERROR;
1083 } else {
1084 ALOGW("stopOutput() refcount is already 0");
1085 return INVALID_OPERATION;
1086 }
1087}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001088
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301089status_t AudioPolicyManagerCustom::startSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301090 audio_stream_type_t stream,
1091 audio_devices_t device,
Lalit Kansaraf803ac52016-05-09 19:12:01 +05301092 const char *address,
Sharad Sangle36781612015-05-28 16:15:16 +05301093 uint32_t *delayMs)
1094{
1095 // cannot start playback of STREAM_TTS if any other output is being used
1096 uint32_t beaconMuteLatency = 0;
1097
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301098 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1099 ALOGW("startSource() invalid stream %d", stream);
1100 return INVALID_OPERATION;
1101 }
1102
Sharad Sangle36781612015-05-28 16:15:16 +05301103 *delayMs = 0;
1104 if (stream == AUDIO_STREAM_TTS) {
1105 ALOGV("\t found BEACON stream");
Ashish Jain40cab3a2016-03-23 11:14:14 +05301106 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301107 return INVALID_OPERATION;
1108 } else {
1109 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001110 }
Sharad Sangle36781612015-05-28 16:15:16 +05301111 } else {
1112 // some playback other than beacon starts
1113 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1114 }
1115
Ashish Jain40cab3a2016-03-23 11:14:14 +05301116 // check active before incrementing usage count
1117 bool force = !outputDesc->isActive();
1118
Sharad Sangle36781612015-05-28 16:15:16 +05301119 // increment usage count for this stream on the requested output:
1120 // NOTE that the usage count is the same for duplicated output and hardware output which is
1121 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1122 outputDesc->changeRefCount(stream, 1);
1123
1124 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
1125 // starting an output being rerouted?
1126 if (device == AUDIO_DEVICE_NONE) {
1127 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001128 }
Sharad Sangle36781612015-05-28 16:15:16 +05301129 routing_strategy strategy = getStrategy(stream);
1130 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
1131 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1132 (beaconMuteLatency > 0);
1133 uint32_t waitMs = beaconMuteLatency;
Sharad Sangle36781612015-05-28 16:15:16 +05301134 for (size_t i = 0; i < mOutputs.size(); i++) {
1135 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1136 if (desc != outputDesc) {
1137 // force a device change if any other output is managed by the same hw
1138 // module and has a current device selection that differs from selected device.
1139 // In this case, the audio HAL must receive the new device selection so that it can
1140 // change the device currently selected by the other active output.
1141 if (outputDesc->sharesHwModuleWith(desc) &&
1142 desc->device() != device) {
1143 force = true;
1144 }
1145 // wait for audio on other active outputs to be presented when starting
1146 // a notification so that audio focus effect can propagate, or that a mute/unmute
1147 // event occurred for beacon
1148 uint32_t latency = desc->latency();
1149 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1150 waitMs = latency;
1151 }
1152 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001153 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05301154 uint32_t muteWaitMs;
Lalit Kansaraf803ac52016-05-09 19:12:01 +05301155 muteWaitMs = setOutputDevice(outputDesc, device, force, 0, NULL, address);
Sharad Sangle36781612015-05-28 16:15:16 +05301156
1157 // handle special case for sonification while in call
1158 if (isInCall()) {
1159 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001160 }
Sharad Sangle36781612015-05-28 16:15:16 +05301161
1162 // apply volume rules for current stream and device if necessary
1163 checkAndSetVolume(stream,
Ashish Jain40cab3a2016-03-23 11:14:14 +05301164 mVolumeCurves->getVolumeIndex(stream, device),
Sharad Sangle36781612015-05-28 16:15:16 +05301165 outputDesc,
1166 device);
1167
1168 // update the outputs if starting an output with a stream that can affect notification
1169 // routing
1170 handleNotificationRoutingForStream(stream);
1171
1172 // force reevaluating accessibility routing when ringtone or alarm starts
1173 if (strategy == STRATEGY_SONIFICATION) {
1174 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1175 }
1176 }
1177 else {
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001178 // handle special case for sonification while in call
1179 if (isInCall()) {
1180 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Sharad Sangle36781612015-05-28 16:15:16 +05301181 }
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001182 }
Sharad Sangle36781612015-05-28 16:15:16 +05301183 return NO_ERROR;
1184}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001185
Sharad Sangle36781612015-05-28 16:15:16 +05301186void AudioPolicyManagerCustom::handleIncallSonification(audio_stream_type_t stream,
1187 bool starting, bool stateChange,
1188 audio_io_handle_t output)
1189{
1190 if(!hasPrimaryOutput()) {
1191 return;
1192 }
1193 // no action needed for AUDIO_STREAM_PATCH stream type, it's for internal flinger tracks
1194 if (stream == AUDIO_STREAM_PATCH) {
1195 return;
1196 }
1197 // if the stream pertains to sonification strategy and we are in call we must
1198 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
1199 // in the device used for phone strategy and play the tone if the selected device does not
1200 // interfere with the device used for phone strategy
1201 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
1202 // many times as there are active tracks on the output
1203 const routing_strategy stream_strategy = getStrategy(stream);
1204 if ((stream_strategy == STRATEGY_SONIFICATION) ||
1205 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
1206 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
1207 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
1208 stream, starting, outputDesc->mDevice, stateChange);
1209 if (outputDesc->mRefCount[stream]) {
1210 int muteCount = 1;
1211 if (stateChange) {
1212 muteCount = outputDesc->mRefCount[stream];
1213 }
1214 if (audio_is_low_visibility(stream)) {
1215 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
1216 for (int i = 0; i < muteCount; i++) {
1217 setStreamMute(stream, starting, outputDesc);
1218 }
1219 } else {
1220 ALOGV("handleIncallSonification() high visibility");
1221 if (outputDesc->device() &
1222 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
1223 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
1224 for (int i = 0; i < muteCount; i++) {
1225 setStreamMute(stream, starting, outputDesc);
1226 }
1227 }
1228 if (starting) {
1229 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
1230 AUDIO_STREAM_VOICE_CALL);
1231 } else {
1232 mpClientInterface->stopTone();
1233 }
1234 }
1235 }
1236 }
1237}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001238
Sharad Sangle36781612015-05-28 16:15:16 +05301239void AudioPolicyManagerCustom::handleNotificationRoutingForStream(audio_stream_type_t stream) {
1240 switch(stream) {
1241 case AUDIO_STREAM_MUSIC:
1242 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
1243 updateDevicesAndOutputs();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001244 break;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001245 default:
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001246 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001247 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001248}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001249
Sharad Sangle36781612015-05-28 16:15:16 +05301250status_t AudioPolicyManagerCustom::checkAndSetVolume(audio_stream_type_t stream,
1251 int index,
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301252 const sp<AudioOutputDescriptor>& outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301253 audio_devices_t device,
1254 int delayMs, bool force)
1255{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301256 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1257 ALOGW("checkAndSetVolume() invalid stream %d", stream);
1258 return INVALID_OPERATION;
1259 }
Sharad Sangle36781612015-05-28 16:15:16 +05301260 // do not change actual stream volume if the stream is muted
1261 if (outputDesc->mMuteCount[stream] != 0) {
1262 ALOGVV("checkAndSetVolume() stream %d muted count %d",
1263 stream, outputDesc->mMuteCount[stream]);
1264 return NO_ERROR;
1265 }
1266 audio_policy_forced_cfg_t forceUseForComm =
1267 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
1268 // do not change in call volume if bluetooth is connected and vice versa
1269 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
1270 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
1271 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
1272 stream, forceUseForComm);
1273 return INVALID_OPERATION;
1274 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001275
Sharad Sangle36781612015-05-28 16:15:16 +05301276 if (device == AUDIO_DEVICE_NONE) {
1277 device = outputDesc->device();
1278 }
1279
1280 float volumeDb = computeVolume(stream, index, device);
1281 if (outputDesc->isFixedVolume(device)) {
1282 volumeDb = 0.0f;
1283 }
1284
1285 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
1286
1287 if (stream == AUDIO_STREAM_VOICE_CALL ||
1288 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
1289 float voiceVolume;
1290 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
1291 if (stream == AUDIO_STREAM_VOICE_CALL) {
Ashish Jain40cab3a2016-03-23 11:14:14 +05301292 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Sharad Sangle36781612015-05-28 16:15:16 +05301293 } else {
1294 voiceVolume = 1.0;
1295 }
1296
1297 if (voiceVolume != mLastVoiceVolume && ((outputDesc == mPrimaryOutput) ||
1298 isDirectOutput(outputDesc->mIoHandle) || device & AUDIO_DEVICE_OUT_ALL_USB)) {
1299 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
1300 mLastVoiceVolume = voiceVolume;
1301 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301302#ifdef FM_POWER_OPT
1303 } else if (stream == AUDIO_STREAM_MUSIC && hasPrimaryOutput() &&
Haynes Mathew George603ae9b2015-12-21 17:23:59 -08001304 outputDesc == mPrimaryOutput && mFMIsActive) {
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001305 /* Avoid unnecessary set_parameter calls as it puts the primary
1306 outputs FastMixer in HOT_IDLE leading to breaks in audio */
1307 if (volumeDb != mPrevFMVolumeDb) {
1308 mPrevFMVolumeDb = volumeDb;
1309 AudioParameter param = AudioParameter();
1310 param.addFloat(String8("fm_volume"), Volume::DbToAmpl(volumeDb));
Mingming Yin1433cc62016-01-04 14:58:00 -08001311 //Double delayMs to avoid sound burst while device switch.
1312 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString(), delayMs*2);
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001313 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301314#endif /* FM_POWER_OPT end */
Sharad Sangle36781612015-05-28 16:15:16 +05301315 }
1316
1317 return NO_ERROR;
1318}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001319
Sharad Sangle36781612015-05-28 16:15:16 +05301320bool AudioPolicyManagerCustom::isDirectOutput(audio_io_handle_t output) {
1321 for (size_t i = 0; i < mOutputs.size(); i++) {
1322 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1323 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1324 if ((curOutput == output) && (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
1325 return true;
1326 }
1327 }
1328 return false;
1329}
vivek mehta0ea887a2015-08-26 14:01:20 -07001330
Ashish Jainac510a72016-04-28 12:22:58 +05301331bool static isDirectPCMEnabled(int bitWidth)
1332{
1333 bool directPCMEnabled = false;
1334 if (bitWidth == 24 || bitWidth == 32)
1335 directPCMEnabled =
1336 property_get_bool("audio.offload.pcm.24bit.enable", false);
1337 else
1338 directPCMEnabled =
1339 property_get_bool("audio.offload.pcm.16bit.enable", false);
1340
1341 return directPCMEnabled;
1342}
1343
vivek mehta0ea887a2015-08-26 14:01:20 -07001344status_t AudioPolicyManagerCustom::getOutputForAttr(const audio_attributes_t *attr,
1345 audio_io_handle_t *output,
1346 audio_session_t session,
1347 audio_stream_type_t *stream,
1348 uid_t uid,
1349 uint32_t samplingRate,
1350 audio_format_t format,
1351 audio_channel_mask_t channelMask,
1352 audio_output_flags_t flags,
1353 audio_port_handle_t selectedDeviceId,
1354 const audio_offload_info_t *offloadInfo)
1355{
1356 audio_offload_info_t tOffloadInfo = AUDIO_INFO_INITIALIZER;
1357
Alexy Joseph0ac09032015-10-16 15:57:53 -07001358 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
Ashish Jainac510a72016-04-28 12:22:58 +05301359 uint32_t bitWidth = (audio_bytes_per_sample(format) * 8);
Alexy Joseph0ac09032015-10-16 15:57:53 -07001360
1361 if (offloadDisabled) {
1362 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
1363 }
1364
Ashish Jainac510a72016-04-28 12:22:58 +05301365 if (!offloadDisabled && (offloadInfo == NULL) &&
1366 isDirectPCMEnabled(bitWidth) &&
1367 (flags == AUDIO_OUTPUT_FLAG_NONE)) {
vivek mehta0ea887a2015-08-26 14:01:20 -07001368
vivek mehta0ea887a2015-08-26 14:01:20 -07001369 tOffloadInfo.sample_rate = samplingRate;
1370 tOffloadInfo.channel_mask = channelMask;
1371 tOffloadInfo.format = format;
1372 tOffloadInfo.stream_type = *stream;
Ashish Jainac510a72016-04-28 12:22:58 +05301373 tOffloadInfo.bit_width = bitWidth;
vivek mehta0ea887a2015-08-26 14:01:20 -07001374 if (attr != NULL) {
1375 ALOGV("found attribute .. setting usage %d ", attr->usage);
1376 tOffloadInfo.usage = attr->usage;
1377 } else {
Alexy Joseph0ac09032015-10-16 15:57:53 -07001378 ALOGI("%s:: attribute is NULL .. no usage set", __func__);
vivek mehta0ea887a2015-08-26 14:01:20 -07001379 }
1380 offloadInfo = &tOffloadInfo;
1381 }
1382
1383 return AudioPolicyManager::getOutputForAttr(attr, output, session, stream,
1384 (uid_t)uid, (uint32_t)samplingRate,
1385 format, (audio_channel_mask_t)channelMask,
1386 flags, (audio_port_handle_t)selectedDeviceId,
1387 offloadInfo);
1388}
1389
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001390audio_io_handle_t AudioPolicyManagerCustom::getOutputForDevice(
1391 audio_devices_t device,
Sharad Sangle36781612015-05-28 16:15:16 +05301392 audio_session_t session __unused,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001393 audio_stream_type_t stream,
1394 uint32_t samplingRate,
1395 audio_format_t format,
1396 audio_channel_mask_t channelMask,
1397 audio_output_flags_t flags,
1398 const audio_offload_info_t *offloadInfo)
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001399{
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001400 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001401 status_t status;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001402
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001403#ifdef AUDIO_POLICY_TEST
1404 if (mCurOutput != 0) {
1405 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1406 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1407
1408 if (mTestOutputs[mCurOutput] == 0) {
1409 ALOGV("getOutput() opening test output");
Sharad Sangle36781612015-05-28 16:15:16 +05301410 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
1411 mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001412 outputDesc->mDevice = mTestDevice;
1413 outputDesc->mLatency = mTestLatencyMs;
1414 outputDesc->mFlags =
1415 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
1416 outputDesc->mRefCount[stream] = 0;
1417 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1418 config.sample_rate = mTestSamplingRate;
1419 config.channel_mask = mTestChannels;
1420 config.format = mTestFormat;
1421 if (offloadInfo != NULL) {
1422 config.offload_info = *offloadInfo;
1423 }
1424 status = mpClientInterface->openOutput(0,
1425 &mTestOutputs[mCurOutput],
1426 &config,
1427 &outputDesc->mDevice,
1428 String8(""),
1429 &outputDesc->mLatency,
1430 outputDesc->mFlags);
1431 if (status == NO_ERROR) {
1432 outputDesc->mSamplingRate = config.sample_rate;
1433 outputDesc->mFormat = config.format;
1434 outputDesc->mChannelMask = config.channel_mask;
1435 AudioParameter outputCmd = AudioParameter();
1436 outputCmd.addInt(String8("set_id"),mCurOutput);
1437 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1438 addOutput(mTestOutputs[mCurOutput], outputDesc);
1439 }
1440 }
1441 return mTestOutputs[mCurOutput];
1442 }
1443#endif //AUDIO_POLICY_TEST
Sharad Sangle36781612015-05-28 16:15:16 +05301444 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1445 (stream != AUDIO_STREAM_MUSIC)) {
1446 // compress should not be used for non-music streams
1447 ALOGE("Offloading only allowed with music stream");
1448 return 0;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301449 }
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301450
1451 if ((stream == AUDIO_STREAM_VOICE_CALL) &&
1452 (channelMask == 1) &&
Shiv Maliyappanahallid77d3b32016-02-02 13:45:50 -08001453 (samplingRate == 8000 || samplingRate == 16000 ||
1454 samplingRate == 32000 || samplingRate == 48000)) {
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301455 // Allow Voip direct output only if:
1456 // audio mode is MODE_IN_COMMUNCATION; AND
1457 // voip output is not opened already; AND
1458 // requested sample rate matches with that of voip input stream (if opened already)
1459 int value = 0;
1460 uint32_t mode = 0, voipOutCount = 1, voipSampleRate = 1;
1461 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1462 String8("audio_mode"));
1463 AudioParameter result = AudioParameter(valueStr);
1464 if (result.getInt(String8("audio_mode"), value) == NO_ERROR) {
1465 mode = value;
1466 }
1467
1468 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1469 String8("voip_out_stream_count"));
1470 result = AudioParameter(valueStr);
1471 if (result.getInt(String8("voip_out_stream_count"), value) == NO_ERROR) {
1472 voipOutCount = value;
1473 }
1474
1475 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1476 String8("voip_sample_rate"));
1477 result = AudioParameter(valueStr);
1478 if (result.getInt(String8("voip_sample_rate"), value) == NO_ERROR) {
1479 voipSampleRate = value;
1480 }
1481
1482 if ((mode == AUDIO_MODE_IN_COMMUNICATION) && (voipOutCount == 0) &&
1483 ((voipSampleRate == 0) || (voipSampleRate == samplingRate))) {
1484 if (audio_is_linear_pcm(format)) {
1485 char propValue[PROPERTY_VALUE_MAX] = {0};
1486 property_get("use.voice.path.for.pcm.voip", propValue, "0");
1487 bool voipPcmSysPropEnabled = !strncmp("true", propValue, sizeof("true"));
1488 if (voipPcmSysPropEnabled && (format == AUDIO_FORMAT_PCM_16_BIT)) {
Karthik Reddy Katta3fb9be72015-11-25 11:18:27 +05301489 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX |
1490 AUDIO_OUTPUT_FLAG_DIRECT);
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301491 ALOGD("Set VoIP and Direct output flags for PCM format");
1492 }
1493 }
1494 }
1495 }
1496
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301497#ifdef VOICE_CONCURRENCY
1498 char propValue[PROPERTY_VALUE_MAX];
1499 bool prop_play_enabled=false, prop_voip_enabled = false;
1500
1501 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1502 prop_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001503 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301504
1505 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1506 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1507 }
1508
1509 if (prop_play_enabled && mvoice_call_state) {
1510 //check if voice call is active / running in background
1511 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1512 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1513 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1514 {
1515 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1516 if(prop_voip_enabled) {
1517 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1518 flags );
1519 return 0;
1520 }
1521 }
1522 else {
1523 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
1524 ALOGD("voice_conc:IN call mode adding ULL flags .. flags: %x ", flags );
1525 flags = AUDIO_OUTPUT_FLAG_FAST;
1526 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
1527 if (AUDIO_STREAM_MUSIC == stream) {
1528 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1529 ALOGD("voice_conc:IN call mode adding deep-buffer flags %x ", flags );
1530 }
1531 else {
1532 flags = AUDIO_OUTPUT_FLAG_FAST;
1533 ALOGD("voice_conc:IN call mode adding fast flags %x ", flags );
1534 }
1535 }
1536 }
1537 }
1538 } else if (prop_voip_enabled && mvoice_call_state) {
1539 //check if voice call is active / running in background
1540 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1541 //return only ULL ouput
1542 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1543 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1544 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1545 {
1546 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1547 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1548 flags );
1549 return 0;
1550 }
1551 }
1552 }
1553#endif
1554#ifdef RECORD_PLAY_CONCURRENCY
1555 char recConcPropValue[PROPERTY_VALUE_MAX];
1556 bool prop_rec_play_enabled = false;
1557
1558 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
1559 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
1560 }
1561 if ((prop_rec_play_enabled) &&
1562 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
1563 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
1564 if (AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1565 // allow VoIP using voice path
1566 // Do nothing
1567 } else if((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1568 ALOGD("voice_conc:MODE_IN_COMM is setforcing deep buffer output for non ULL... flags: %x", flags);
1569 // use deep buffer path for all non ULL outputs
1570 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1571 }
1572 } else if ((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1573 ALOGD("voice_conc:Record mode is on forcing deep buffer output for non ULL... flags: %x ", flags);
1574 // use deep buffer path for all non ULL outputs
1575 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1576 }
1577 }
1578 if (prop_rec_play_enabled &&
1579 (stream == AUDIO_STREAM_ENFORCED_AUDIBLE)) {
1580 ALOGD("Record conc is on forcing ULL output for ENFORCED_AUDIBLE");
1581 flags = AUDIO_OUTPUT_FLAG_FAST;
1582 }
1583#endif
1584
Sharad Sangle4509cef2015-08-19 20:47:12 +05301585#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Sharad Sangle36781612015-05-28 16:15:16 +05301586 /*
1587 * WFD audio routes back to target speaker when starting a ringtone playback.
1588 * This is because primary output is reused for ringtone, so output device is
1589 * updated based on SONIFICATION strategy for both ringtone and music playback.
1590 * The same issue is not seen on remoted_submix HAL based WFD audio because
1591 * primary output is not reused and a new output is created for ringtone playback.
1592 * Issue is fixed by updating output flag to AUDIO_OUTPUT_FLAG_FAST when there is
1593 * a non-music stream playback on WFD, so primary output is not reused for ringtone.
1594 */
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001595 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
1596 if ((availableOutputDeviceTypes & AUDIO_DEVICE_OUT_PROXY)
1597 && (stream != AUDIO_STREAM_MUSIC)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301598 ALOGD("WFD audio: use OUTPUT_FLAG_FAST for non music stream. flags:%x", flags );
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001599 //For voip paths
1600 if(flags & AUDIO_OUTPUT_FLAG_DIRECT)
1601 flags = AUDIO_OUTPUT_FLAG_DIRECT;
1602 else //route every thing else to ULL path
1603 flags = AUDIO_OUTPUT_FLAG_FAST;
1604 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301605#endif
1606
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001607 // open a direct output if required by specified parameters
vivek mehta0ea887a2015-08-26 14:01:20 -07001608 // force direct flag if offload flag is set: offloading implies a direct output stream
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001609 // and all common behaviors are driven by checking only the direct flag
1610 // this should normally be set appropriately in the policy configuration file
1611 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1612 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1613 }
1614 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1615 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1616 }
vivek mehta0ea887a2015-08-26 14:01:20 -07001617
vivek mehta4b0d8192015-10-16 00:25:59 -07001618 bool forced_deep = false;
Sharad Sangle36781612015-05-28 16:15:16 +05301619 // only allow deep buffering for music stream type
1620 if (stream != AUDIO_STREAM_MUSIC) {
1621 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Sharad Sangle497aef82015-08-03 17:55:48 +05301622 } else if (/* stream == AUDIO_STREAM_MUSIC && */
1623 flags == AUDIO_OUTPUT_FLAG_NONE &&
1624 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
vivek mehtac984b992015-11-25 13:28:55 -08001625 forced_deep = true;
Sharad Sangle36781612015-05-28 16:15:16 +05301626 }
Sharad Sangle497aef82015-08-03 17:55:48 +05301627
Sharad Sangle36781612015-05-28 16:15:16 +05301628 if (stream == AUDIO_STREAM_TTS) {
1629 flags = AUDIO_OUTPUT_FLAG_TTS;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001630 }
1631
Zhou Song2b3d1552016-03-21 17:21:04 +08001632 // check if direct output for track offload already exits
1633 bool is_track_offload_active = false;
1634 for (size_t i = 0; i < mOutputs.size(); i++) {
1635 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1636 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) {
1637 is_track_offload_active = true;
1638 ALOGD("Track offload already active");
1639 break;
1640 }
1641 }
1642
vivek mehta4b0d8192015-10-16 00:25:59 -07001643 // Do offload magic here
vivek mehtac984b992015-11-25 13:28:55 -08001644 if ((flags == AUDIO_OUTPUT_FLAG_NONE) &&
1645 (stream == AUDIO_STREAM_MUSIC) &&
Zhou Song2b3d1552016-03-21 17:21:04 +08001646 (offloadInfo != NULL) && !is_track_offload_active &&
vivek mehta4b0d8192015-10-16 00:25:59 -07001647 ((offloadInfo->usage == AUDIO_USAGE_MEDIA) || (offloadInfo->usage == AUDIO_USAGE_GAME))) {
vivek mehtac984b992015-11-25 13:28:55 -08001648 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DIRECT_PCM);
vivek mehta4b0d8192015-10-16 00:25:59 -07001649 ALOGD("AudioCustomHAL --> Force Direct Flag .. flag (0x%x)", flags);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301650 }
1651
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001652 sp<IOProfile> profile;
1653
1654 // skip direct output selection if the request can obviously be attached to a mixed output
1655 // and not explicitly requested
vivek mehtac984b992015-11-25 13:28:55 -08001656 if (((flags & (AUDIO_OUTPUT_FLAG_DIRECT|AUDIO_OUTPUT_FLAG_DIRECT_PCM)) == 0) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301657 audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX &&
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001658 audio_channel_count_from_out_mask(channelMask) <= 2) {
1659 goto non_direct_output;
1660 }
1661
1662 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1663 // creating an offloaded track and tearing it down immediately after start when audioflinger
1664 // detects there is an active non offloadable effect.
1665 // FIXME: We should check the audio session here but we do not have it in this context.
1666 // This may prevent offloading in rare situations where effects are left active by apps
1667 // in the background.
Weiyin Jiang6bba3202016-03-03 14:49:36 +08001668 //
1669 // Supplementary annotation:
1670 // For sake of track offload introduced, we need a rollback for both compress offload
1671 // and track offload use cases.
1672 if ((flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD|AUDIO_OUTPUT_FLAG_DIRECT_PCM)) &&
1673 (mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
1674 ALOGD("non offloadable effect is enabled, try with non direct output");
1675 goto non_direct_output;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001676 }
1677
Weiyin Jiang6bba3202016-03-03 14:49:36 +08001678 profile = getProfileForDirectOutput(device,
1679 samplingRate,
1680 format,
1681 channelMask,
1682 (audio_output_flags_t)flags);
1683
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001684 if (profile != 0) {
vivek mehtac984b992015-11-25 13:28:55 -08001685
1686 if (!(flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301687 (profile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT_PCM)) {
vivek mehtac984b992015-11-25 13:28:55 -08001688 ALOGI("got Direct_PCM without requesting ... reject ");
1689 profile = NULL;
1690 goto non_direct_output;
1691 }
1692
Sharad Sangle36781612015-05-28 16:15:16 +05301693 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001694
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001695 // if multiple concurrent offload decode is supported
1696 // do no check for reuse and also don't close previous output if its offload
1697 // previous output will be closed during track destruction
1698 if (!(property_get_bool("audio.offload.multiple.enabled", false) &&
1699 ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0))) {
1700 for (size_t i = 0; i < mOutputs.size(); i++) {
1701 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1702 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1703 outputDesc = desc;
1704 // reuse direct output if currently open and configured with same parameters
1705 if ((samplingRate == outputDesc->mSamplingRate) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301706 audio_formats_match(format, outputDesc->mFormat) &&
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001707 (channelMask == outputDesc->mChannelMask)) {
1708 outputDesc->mDirectOpenCount++;
1709 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1710 return mOutputs.keyAt(i);
1711 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001712 }
1713 }
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001714 // close direct output if currently open and configured with different parameters
1715 if (outputDesc != NULL) {
1716 closeOutput(outputDesc->mIoHandle);
1717 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001718 }
Sharad Sangle36781612015-05-28 16:15:16 +05301719
1720 // if the selected profile is offloaded and no offload info was specified,
1721 // create a default one
1722 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
Ashish Jain40cab3a2016-03-23 11:14:14 +05301723 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
Sharad Sangle36781612015-05-28 16:15:16 +05301724 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1725 defaultOffloadInfo.sample_rate = samplingRate;
1726 defaultOffloadInfo.channel_mask = channelMask;
1727 defaultOffloadInfo.format = format;
1728 defaultOffloadInfo.stream_type = stream;
1729 defaultOffloadInfo.bit_rate = 0;
1730 defaultOffloadInfo.duration_us = -1;
1731 defaultOffloadInfo.has_video = true; // conservative
1732 defaultOffloadInfo.is_streaming = true; // likely
1733 offloadInfo = &defaultOffloadInfo;
1734 }
1735
1736 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001737 outputDesc->mDevice = device;
1738 outputDesc->mLatency = 0;
Sharad Sangle36781612015-05-28 16:15:16 +05301739 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001740 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1741 config.sample_rate = samplingRate;
1742 config.channel_mask = channelMask;
1743 config.format = format;
1744 if (offloadInfo != NULL) {
1745 config.offload_info = *offloadInfo;
1746 }
Sharad Sangle36781612015-05-28 16:15:16 +05301747 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001748 &output,
1749 &config,
1750 &outputDesc->mDevice,
1751 String8(""),
1752 &outputDesc->mLatency,
1753 outputDesc->mFlags);
1754
1755 // only accept an output with the requested parameters
1756 if (status != NO_ERROR ||
1757 (samplingRate != 0 && samplingRate != config.sample_rate) ||
Ashish Jain40cab3a2016-03-23 11:14:14 +05301758 (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) ||
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001759 (channelMask != 0 && channelMask != config.channel_mask)) {
1760 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1761 "format %d %d, channelMask %04x %04x", output, samplingRate,
1762 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1763 outputDesc->mChannelMask);
1764 if (output != AUDIO_IO_HANDLE_NONE) {
1765 mpClientInterface->closeOutput(output);
1766 }
Sharad Sangle36781612015-05-28 16:15:16 +05301767 // fall back to mixer output if possible when the direct output could not be open
Ashish Jain40cab3a2016-03-23 11:14:14 +05301768 if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) {
Sharad Sangle36781612015-05-28 16:15:16 +05301769 goto non_direct_output;
1770 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001771 return AUDIO_IO_HANDLE_NONE;
1772 }
1773 outputDesc->mSamplingRate = config.sample_rate;
1774 outputDesc->mChannelMask = config.channel_mask;
1775 outputDesc->mFormat = config.format;
1776 outputDesc->mRefCount[stream] = 0;
1777 outputDesc->mStopTime[stream] = 0;
1778 outputDesc->mDirectOpenCount = 1;
1779
1780 audio_io_handle_t srcOutput = getOutputForEffect();
1781 addOutput(output, outputDesc);
1782 audio_io_handle_t dstOutput = getOutputForEffect();
1783 if (dstOutput == output) {
Gao Jiedb057832015-11-12 08:51:22 +08001784#ifdef DOLBY_ENABLE
1785 status_t status = mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1786 if (status == NO_ERROR) {
1787 for (size_t i = 0; i < mEffects.size(); i++) {
1788 sp<EffectDescriptor> desc = mEffects.valueAt(i);
1789 if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX) {
1790 // update the mIo member of EffectDescriptor for the global effect
1791 ALOGV("%s updating mIo", __FUNCTION__);
1792 desc->mIo = dstOutput;
1793 }
1794 }
1795 } else {
1796 ALOGW("%s moveEffects from %d to %d failed", __FUNCTION__, srcOutput, dstOutput);
1797 }
1798#else // DOLBY_END
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001799 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
Gao Jiedb057832015-11-12 08:51:22 +08001800#endif // LINE_ADDED_BY_DOLBY
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001801 }
1802 mPreviousOutputs = mOutputs;
1803 ALOGV("getOutput() returns new direct output %d", output);
1804 mpClientInterface->onAudioPortListUpdate();
1805 return output;
1806 }
1807
1808non_direct_output:
Ashish Jain5fe12412016-04-18 18:44:17 +05301809
1810 // A request for HW A/V sync cannot fallback to a mixed output because time
1811 // stamps are embedded in audio data
1812 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1813 return AUDIO_IO_HANDLE_NONE;
1814 }
1815
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001816 // ignoring channel mask due to downmix capability in mixer
1817
1818 // open a non direct output
1819
1820 // for non direct outputs, only PCM is supported
1821 if (audio_is_linear_pcm(format)) {
1822 // get which output is suitable for the specified stream. The actual
1823 // routing change will happen when startOutput() will be called
1824 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1825
1826 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1827 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
Ashish Jain660d3112016-06-20 10:16:51 +05301828
1829 if (forced_deep) {
1830 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
1831 ALOGI("setting force DEEP buffer now ");
1832 } else if(flags == AUDIO_OUTPUT_FLAG_NONE) {
1833 // no deep buffer playback is requested hence fallback to primary
1834 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_PRIMARY);
1835 ALOGI("FLAG None hence request for a primary output");
1836 }
1837
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001838 output = selectOutput(outputs, flags, format);
1839 }
1840 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1841 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1842
vivek mehta0ea887a2015-08-26 14:01:20 -07001843 ALOGV("getOutputForDevice() returns output %d", output);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001844
1845 return output;
1846}
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301847
1848status_t AudioPolicyManagerCustom::getInputForAttr(const audio_attributes_t *attr,
1849 audio_io_handle_t *input,
1850 audio_session_t session,
1851 uid_t uid,
1852 uint32_t samplingRate,
1853 audio_format_t format,
1854 audio_channel_mask_t channelMask,
1855 audio_input_flags_t flags,
1856 audio_port_handle_t selectedDeviceId,
1857 input_type_t *inputType)
1858{
Ashish Jain40cab3a2016-03-23 11:14:14 +05301859 audio_source_t inputSource;
1860 inputSource = attr->source;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301861#ifdef VOICE_CONCURRENCY
1862
1863 char propValue[PROPERTY_VALUE_MAX];
1864 bool prop_rec_enabled=false, prop_voip_enabled = false;
1865
1866 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
1867 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1868 }
1869
1870 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1871 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1872 }
1873
1874 if (prop_rec_enabled && mvoice_call_state) {
1875 //check if voice call is active / running in background
1876 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1877 //Need to block input request
1878 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1879 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1880 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1881 {
1882 switch(inputSource) {
1883 case AUDIO_SOURCE_VOICE_UPLINK:
1884 case AUDIO_SOURCE_VOICE_DOWNLINK:
1885 case AUDIO_SOURCE_VOICE_CALL:
1886 ALOGD("voice_conc:Creating input during incall mode for inputSource: %d",
1887 inputSource);
1888 break;
1889
1890 case AUDIO_SOURCE_VOICE_COMMUNICATION:
1891 if(prop_voip_enabled) {
1892 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1893 inputSource);
1894 return NO_INIT;
1895 }
1896 break;
1897 default:
1898 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1899 inputSource);
1900 return NO_INIT;
1901 }
1902 }
1903 }//check for VoIP flag
1904 else if(prop_voip_enabled && mvoice_call_state) {
1905 //check if voice call is active / running in background
1906 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1907 //Need to block input request
1908 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1909 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1910 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1911 {
1912 if(inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1913 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
1914 return NO_INIT;
1915 }
1916 }
1917 }
1918
1919#endif
1920
1921 return AudioPolicyManager::getInputForAttr(attr,
1922 input,
1923 session,
1924 uid,
1925 samplingRate,
1926 format,
1927 channelMask,
1928 flags,
1929 selectedDeviceId,
1930 inputType);
1931}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001932
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301933status_t AudioPolicyManagerCustom::startInput(audio_io_handle_t input,
1934 audio_session_t session)
1935{
1936 ALOGV("startInput() input %d", input);
1937 ssize_t index = mInputs.indexOfKey(input);
1938 if (index < 0) {
1939 ALOGW("startInput() unknown input %d", input);
1940 return BAD_VALUE;
1941 }
1942 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1943
Ashish Jain40cab3a2016-03-23 11:14:14 +05301944 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1945 if (audioSession == 0) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301946 ALOGW("startInput() unknown session %d on input %d", session, input);
1947 return BAD_VALUE;
1948 }
1949
1950 // virtual input devices are compatible with other input devices
1951 if (!is_virtual_input_device(inputDesc->mDevice)) {
1952
1953 // for a non-virtual input device, check if there is another (non-virtual) active input
1954 audio_io_handle_t activeInput = mInputs.getActiveInput();
1955 if (activeInput != 0 && activeInput != input) {
1956
1957 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1958 // otherwise the active input continues and the new input cannot be started.
1959 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Ashish Jain40cab3a2016-03-23 11:14:14 +05301960 if ((activeDesc->inputSource() == AUDIO_SOURCE_HOTWORD) &&
1961 !activeDesc->hasPreemptedSession(session)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301962 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
Ashish Jain40cab3a2016-03-23 11:14:14 +05301963 //FIXME: consider all active sessions
1964 AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions();
1965 audio_session_t activeSession = activeSessions.keyAt(0);
1966 SortedVector<audio_session_t> sessions =
1967 activeDesc->getPreemptedSessions();
1968 sessions.add(activeSession);
1969 inputDesc->setPreemptedSessions(sessions);
1970 stopInput(activeInput, activeSession);
1971 releaseInput(activeInput, activeSession);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301972 } else {
1973 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
1974 return INVALID_OPERATION;
1975 }
1976 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05301977 // Do not allow capture if an active voice call is using a software patch and
1978 // the call TX source device is on the same HW module.
1979 // FIXME: would be better to refine to only inputs whose profile connects to the
1980 // call TX device but this information is not in the audio patch
1981 if (mCallTxPatch != 0 &&
1982 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1983 return INVALID_OPERATION;
1984 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301985 }
1986
1987 // Routing?
1988 mInputRoutes.incRouteActivity(session);
1989#ifdef RECORD_PLAY_CONCURRENCY
1990 mIsInputRequestOnProgress = true;
1991
1992 char getPropValue[PROPERTY_VALUE_MAX];
1993 bool prop_rec_play_enabled = false;
1994
1995 if (property_get("rec.playback.conc.disabled", getPropValue, NULL)) {
1996 prop_rec_play_enabled = atoi(getPropValue) || !strncmp("true", getPropValue, 4);
1997 }
1998
1999 if ((prop_rec_play_enabled) &&(mInputs.activeInputsCount() == 0)){
2000 // send update to HAL on record playback concurrency
2001 AudioParameter param = AudioParameter();
2002 param.add(String8("rec_play_conc_on"), String8("true"));
2003 ALOGD("startInput() setParameters rec_play_conc is setting to ON ");
2004 mpClientInterface->setParameters(0, param.toString());
2005
2006 // Call invalidate to reset all opened non ULL audio tracks
2007 // Move tracks associated to this strategy from previous output to new output
2008 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
2009 // Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder)
Sharad Sangle4509cef2015-08-19 20:47:12 +05302010 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE && (i != AUDIO_STREAM_PATCH))) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302011 ALOGD("Invalidate on releaseInput for stream :: %d ", i);
2012 //FIXME see fixme on name change
2013 mpClientInterface->invalidateStream((audio_stream_type_t)i);
2014 }
2015 }
2016 // close compress tracks
2017 for (size_t i = 0; i < mOutputs.size(); i++) {
2018 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
2019 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
2020 ALOGD("ouput desc / profile is NULL");
2021 continue;
2022 }
2023 if (outputDesc->mProfile->mFlags
2024 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2025 // close compress sessions
2026 ALOGD("calling closeOutput on record conc for COMPRESS output");
2027 closeOutput(mOutputs.keyAt(i));
2028 }
2029 }
Ashish Jaine4a22d52016-06-22 11:55:08 +05302030 // If effects where present on any of the above closed outputs,
2031 // audioflinger moved them to the primary output by default
2032 // move them back to the appropriate output.
2033 moveGlobalEffect();
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302034 }
2035#endif
2036
Ashish Jain40cab3a2016-03-23 11:14:14 +05302037 if (!inputDesc->isActive() || mInputRoutes.hasRouteChanged(session)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302038 // if input maps to a dynamic policy with an activity listener, notify of state change
Ashish Jain630c5e12016-04-18 11:50:21 +05302039 if ((inputDesc->mPolicyMix != NULL)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302040 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Ashish Jain5fe12412016-04-18 18:44:17 +05302041 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302042 MIX_STATE_MIXING);
Ashish Jain630c5e12016-04-18 11:50:21 +05302043 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302044
2045 if (mInputs.activeInputsCount() == 0) {
2046 SoundTrigger::setCaptureState(true);
2047 }
2048 setInputDevice(input, getNewInputDevice(input), true /* force */);
2049
2050 // automatically enable the remote submix output when input is started if not
2051 // used by a policy mix of type MIX_TYPE_RECORDERS
2052 // For remote submix (a virtual device), we open only one input per capture request.
2053 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
2054 String8 address = String8("");
2055 if (inputDesc->mPolicyMix == NULL) {
2056 address = String8("0");
Ashish Jain630c5e12016-04-18 11:50:21 +05302057 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
Ashish Jain5fe12412016-04-18 18:44:17 +05302058 address = inputDesc->mPolicyMix->mDeviceAddress;
Ashish Jain630c5e12016-04-18 11:50:21 +05302059 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302060 if (address != "") {
2061 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2062 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2063 address, "remote-submix");
2064 }
2065 }
2066 }
2067
Ashish Jain40cab3a2016-03-23 11:14:14 +05302068 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302069
Ashish Jain40cab3a2016-03-23 11:14:14 +05302070 audioSession->changeActiveCount(1);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302071#ifdef RECORD_PLAY_CONCURRENCY
2072 mIsInputRequestOnProgress = false;
2073#endif
2074 return NO_ERROR;
2075}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08002076
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302077status_t AudioPolicyManagerCustom::stopInput(audio_io_handle_t input,
2078 audio_session_t session)
2079{
2080 status_t status;
2081 status = AudioPolicyManager::stopInput(input, session);
2082#ifdef RECORD_PLAY_CONCURRENCY
2083 char propValue[PROPERTY_VALUE_MAX];
2084 bool prop_rec_play_enabled = false;
2085
2086 if (property_get("rec.playback.conc.disabled", propValue, NULL)) {
2087 prop_rec_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
2088 }
2089
2090 if ((prop_rec_play_enabled) && (mInputs.activeInputsCount() == 0)) {
2091
2092 //send update to HAL on record playback concurrency
2093 AudioParameter param = AudioParameter();
2094 param.add(String8("rec_play_conc_on"), String8("false"));
2095 ALOGD("stopInput() setParameters rec_play_conc is setting to OFF ");
2096 mpClientInterface->setParameters(0, param.toString());
2097
2098 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
2099 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
2100 //Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder stop tone)
2101 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE) && (i != AUDIO_STREAM_PATCH)) {
2102 ALOGD(" Invalidate on stopInput for stream :: %d ", i);
2103 //FIXME see fixme on name change
2104 mpClientInterface->invalidateStream((audio_stream_type_t)i);
2105 }
2106 }
2107 }
2108#endif
2109 return status;
2110}
2111
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302112void AudioPolicyManagerCustom::closeAllInputs() {
2113 bool patchRemoved = false;
2114
Dhanalakshmi Siddanibfeca7f2015-10-08 15:17:11 +05302115 for(size_t input_index = mInputs.size(); input_index > 0; input_index--) {
2116 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index-1);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302117 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302118 if (patch_index >= 0) {
2119 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302120 status_t status;
2121 status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302122 mAudioPatches.removeItemsAt(patch_index);
2123 patchRemoved = true;
2124 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05302125 if ((inputDesc->getOpenRefCount() > 0) && inputDesc->isSoundTrigger()
2126 && (mInputs.size() == 1)) {
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302127 ALOGD("Do not close sound trigger input handle");
2128 } else {
Dhanalakshmi Siddanibfeca7f2015-10-08 15:17:11 +05302129 mpClientInterface->closeInput(mInputs.keyAt(input_index-1));
2130 mInputs.removeItem(mInputs.keyAt(input_index-1));
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302131 }
2132 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05302133 mInputs.clear();
2134 SoundTrigger::setCaptureState(false);
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302135 nextAudioPortGeneration();
2136
2137 if (patchRemoved) {
2138 mpClientInterface->onAudioPatchListUpdate();
2139 }
2140}
2141
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302142AudioPolicyManagerCustom::AudioPolicyManagerCustom(AudioPolicyClientInterface *clientInterface)
Sharad Sangle4509cef2015-08-19 20:47:12 +05302143 : AudioPolicyManager(clientInterface),
2144 mHdmiAudioDisabled(false),
2145 mHdmiAudioEvent(false),
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08002146 mPrevPhoneState(0),
Haynes Mathew George603ae9b2015-12-21 17:23:59 -08002147 mPrevFMVolumeDb(0.0f),
2148 mFMIsActive(false)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302149{
Ashish Jain40cab3a2016-03-23 11:14:14 +05302150
Satya Krishna Pindiproli8f83f102016-06-21 17:56:14 +05302151#ifdef USE_XML_AUDIO_POLICY_CONF
2152 ALOGD("USE_XML_AUDIO_POLICY_CONF is TRUE");
2153#else
2154 ALOGD("USE_XML_AUDIO_POLICY_CONF is FALSE");
2155#endif
2156
Ashish Jain40cab3a2016-03-23 11:14:14 +05302157 //TODO: Check the new logic to parse policy conf and update the below code
2158 // Need this when SSR encoding is enabled
Mingming Yin38ea08c2015-10-05 15:24:04 -07002159 char ssr_enabled[PROPERTY_VALUE_MAX] = {0};
2160 bool prop_ssr_enabled = false;
2161
2162 if (property_get("ro.qc.sdk.audio.ssr", ssr_enabled, NULL)) {
2163 prop_ssr_enabled = atoi(ssr_enabled) || !strncmp("true", ssr_enabled, 4);
2164 }
2165
2166 for (size_t i = 0; i < mHwModules.size(); i++) {
Ashish Jain40cab3a2016-03-23 11:14:14 +05302167 ALOGV("Hw module %zu", i);
Mingming Yin38ea08c2015-10-05 15:24:04 -07002168 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) {
2169 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Ashish Jain40cab3a2016-03-23 11:14:14 +05302170 AudioProfileVector profiles = inProfile->getAudioProfiles();
2171 for (size_t k = 0; k < profiles.size(); k++){
2172 ChannelsVector channels = profiles[k]->getChannels();
2173 for (size_t x = 0; x < channels.size(); x++) {
2174 audio_channel_mask_t channelMask = channels[x];
2175 ALOGV("Channel Mask %x size %zu", channelMask,
2176 channels.size());
2177 if (AUDIO_CHANNEL_IN_5POINT1 == channelMask) {
2178 if (!prop_ssr_enabled) {
2179 ALOGI("removing AUDIO_CHANNEL_IN_5POINT1 from"
2180 " input profile as SSR(surround sound record)"
2181 " is not supported on this chipset variant");
2182 channels.removeItemsAt(x, 1);
2183 ALOGV("Channel Mask size now %zu",
2184 channels.size());
2185 }
Mingming Yin38ea08c2015-10-05 15:24:04 -07002186 }
2187 }
2188 }
2189 }
2190 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302191#ifdef RECORD_PLAY_CONCURRENCY
2192 mIsInputRequestOnProgress = false;
2193#endif
2194
2195
2196#ifdef VOICE_CONCURRENCY
2197 mFallBackflag = getFallBackPath();
2198#endif
2199}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07002200}