blob: cb22dc7586d93566c875c7bf30be53b82296703d [file] [log] [blame]
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001/*
Sharad Sanglec66b9ca2016-12-21 18:40:54 +05302 * Copyright (c) 2013-2017 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)
Sharad Sangle75170722016-11-21 16:21:06 +053055#define SAMPLE_RATE_8000 8000
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 Sanglec66b9ca2016-12-21 18:40:54 +053072/*audio policy: workaround for truncated touch sounds*/
73//FIXME: workaround for truncated touch sounds
74// to be removed when the problem is handled by system UI
75#define TOUCH_SOUND_FIXED_DELAY_MS 100
Sharad Sanglec5766ff2015-06-04 20:24:10 +053076#ifdef VOICE_CONCURRENCY
77audio_output_flags_t AudioPolicyManagerCustom::getFallBackPath()
78{
79 audio_output_flags_t flag = AUDIO_OUTPUT_FLAG_FAST;
80 char propValue[PROPERTY_VALUE_MAX];
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070081
Sharad Sanglec5766ff2015-06-04 20:24:10 +053082 if (property_get("voice.conc.fallbackpath", propValue, NULL)) {
83 if (!strncmp(propValue, "deep-buffer", 11)) {
84 flag = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
85 }
86 else if (!strncmp(propValue, "fast", 4)) {
87 flag = AUDIO_OUTPUT_FLAG_FAST;
88 }
89 else {
90 ALOGD("voice_conc:not a recognised path(%s) in prop voice.conc.fallbackpath",
91 propValue);
92 }
93 }
94 else {
95 ALOGD("voice_conc:prop voice.conc.fallbackpath not set");
96 }
97
98 ALOGD("voice_conc:picked up flag(0x%x) from prop voice.conc.fallbackpath",
99 flag);
100
101 return flag;
102}
103#endif /*VOICE_CONCURRENCY*/
Ashish Jaine4a22d52016-06-22 11:55:08 +0530104
105void AudioPolicyManagerCustom::moveGlobalEffect()
106{
107 audio_io_handle_t dstOutput = getOutputForEffect();
108 if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) {
109#ifdef DOLBY_ENABLE
110 status_t status =
111 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
112 mPrimaryOutput->mIoHandle,
113 dstOutput);
114 if (status == NO_ERROR) {
115 for (size_t i = 0; i < mEffects.size(); i++) {
116 sp<EffectDescriptor> desc = mEffects.valueAt(i);
117 if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX) {
118 // update the mIo member of EffectDescriptor
119 // for the global effect
120 ALOGV("%s updating mIo", __FUNCTION__);
121 desc->mIo = dstOutput;
122 }
123 }
124 } else {
125 ALOGW("%s moveEffects from %d to %d failed", __FUNCTION__,
126 mPrimaryOutput->mIoHandle, dstOutput);
127 }
128#else // DOLBY_END
129 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
130 mPrimaryOutput->mIoHandle, dstOutput);
131#endif
132 }
133}
134
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700135// ----------------------------------------------------------------------------
136// AudioPolicyInterface implementation
137// ----------------------------------------------------------------------------
Sharad Sangle36781612015-05-28 16:15:16 +0530138extern "C" AudioPolicyInterface* createAudioPolicyManager(
139 AudioPolicyClientInterface *clientInterface)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700140{
Sharad Sangle36781612015-05-28 16:15:16 +0530141 return new AudioPolicyManagerCustom(clientInterface);
142}
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700143
Sharad Sangle36781612015-05-28 16:15:16 +0530144extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface)
145{
146 delete interface;
147}
148
149status_t AudioPolicyManagerCustom::setDeviceConnectionStateInt(audio_devices_t device,
150 audio_policy_dev_state_t state,
151 const char *device_address,
152 const char *device_name)
153{
Ashish Jain40cab3a2016-03-23 11:14:14 +0530154 ALOGD("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
Sharad Sangle36781612015-05-28 16:15:16 +0530155 device, state, device_address, device_name);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700156
157 // connect/disconnect only 1 device at a time
158 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
159
Sharad Sangle36781612015-05-28 16:15:16 +0530160 sp<DeviceDescriptor> devDesc =
161 mHwModules.getDeviceDescriptor(device, device_address, device_name);
162
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700163 // handle output devices
164 if (audio_is_output_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700165 SortedVector <audio_io_handle_t> outputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700166
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700167 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700168
169 // save a copy of the opened output descriptors before any output is opened or closed
170 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
171 mPreviousOutputs = mOutputs;
172 switch (state)
173 {
174 // handle output device connection
Sharad Sangle36781612015-05-28 16:15:16 +0530175 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700176 if (index >= 0) {
Sharad Sangle4509cef2015-08-19 20:47:12 +0530177#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
178 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
179 if (!strncmp(device_address, "hdmi_spkr", 9)) {
180 mHdmiAudioDisabled = false;
181 } else {
182 mHdmiAudioEvent = true;
183 }
184 }
185#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700186 ALOGW("setDeviceConnectionState() device already connected: %x", device);
187 return INVALID_OPERATION;
188 }
189 ALOGV("setDeviceConnectionState() connecting device %x", device);
190
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700191 // register new device as available
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700192 index = mAvailableOutputDevices.add(devDesc);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530193#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
194 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
195 if (!strncmp(device_address, "hdmi_spkr", 9)) {
196 mHdmiAudioDisabled = false;
197 } else {
198 mHdmiAudioEvent = true;
199 }
200 if (mHdmiAudioDisabled || !mHdmiAudioEvent) {
201 mAvailableOutputDevices.remove(devDesc);
202 ALOGW("HDMI sink not connected, do not route audio to HDMI out");
203 return INVALID_OPERATION;
204 }
205 }
206#endif
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700207 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530208 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700209 if (module == 0) {
210 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
211 device);
212 mAvailableOutputDevices.remove(devDesc);
213 return INVALID_OPERATION;
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700214 }
Sharad Sangle36781612015-05-28 16:15:16 +0530215 mAvailableOutputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700216 } else {
217 return NO_MEMORY;
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700218 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700219
Sharad Sangle36781612015-05-28 16:15:16 +0530220 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700221 mAvailableOutputDevices.remove(devDesc);
222 return INVALID_OPERATION;
223 }
Sharad Sangle36781612015-05-28 16:15:16 +0530224 // Propagate device availability to Engine
225 mEngine->setDeviceConnectionState(devDesc, state);
226
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700227 // outputs should never be empty here
228 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
229 "checkOutputsForDevice() returned no outputs but status OK");
230 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
231 outputs.size());
Sharad Sangle36781612015-05-28 16:15:16 +0530232
233 // Send connect to HALs
234 AudioParameter param = AudioParameter(devDesc->mAddress);
235 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
236 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
237
238 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700239 // handle output device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700240 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
241 if (index < 0) {
Sharad Sangle4509cef2015-08-19 20:47:12 +0530242#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
243 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
244 if (!strncmp(device_address, "hdmi_spkr", 9)) {
245 mHdmiAudioDisabled = true;
246 } else {
247 mHdmiAudioEvent = false;
248 }
249 }
250#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700251 ALOGW("setDeviceConnectionState() device not connected: %x", device);
252 return INVALID_OPERATION;
253 }
254
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700255 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
256
Sharad Sangle36781612015-05-28 16:15:16 +0530257 // Send Disconnect to HALs
258 AudioParameter param = AudioParameter(devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700259 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
260 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
261
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700262 // remove device from available output devices
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700263 mAvailableOutputDevices.remove(devDesc);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530264#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
265 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
266 if (!strncmp(device_address, "hdmi_spkr", 9)) {
267 mHdmiAudioDisabled = true;
268 } else {
269 mHdmiAudioEvent = false;
270 }
271 }
272#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530273 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
274
275 // Propagate device availability to Engine
276 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700277 } break;
278
279 default:
280 ALOGE("setDeviceConnectionState() invalid state: %x", state);
281 return BAD_VALUE;
282 }
283
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700284 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
285 // output is suspended before any tracks are moved to it
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700286 checkA2dpSuspend();
287 checkOutputForAllStrategies();
288 // outputs must be closed after checkOutputForAllStrategies() is executed
289 if (!outputs.isEmpty()) {
290 for (size_t i = 0; i < outputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530291 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700292 // close unused outputs after device disconnection or direct outputs that have been
293 // opened by checkOutputsForDevice() to query dynamic parameters
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700294 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700295 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
296 (desc->mDirectOpenCount == 0))) {
297 closeOutput(outputs[i]);
298 }
299 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700300 // check again after closing A2DP output to reset mA2dpSuspended if needed
301 checkA2dpSuspend();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700302 }
303
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530304#ifdef FM_POWER_OPT
305 // handle FM device connection state to trigger FM AFE loopback
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800306 if (device == AUDIO_DEVICE_OUT_FM && hasPrimaryOutput()) {
Mingming Yin2dd77c22016-01-06 18:02:42 -0800307 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530308 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
309 mPrimaryOutput->changeRefCount(AUDIO_STREAM_MUSIC, 1);
Mingming Yin2dd77c22016-01-06 18:02:42 -0800310 newDevice = (audio_devices_t)(getNewOutputDevice(mPrimaryOutput, false)|AUDIO_DEVICE_OUT_FM);
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800311 mFMIsActive = true;
Zhou Songbd0264e2016-06-21 11:01:06 +0800312 mPrimaryOutput->mDevice = newDevice & ~AUDIO_DEVICE_OUT_FM;
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530313 } else {
Mingming Yin2dd77c22016-01-06 18:02:42 -0800314 newDevice = (audio_devices_t)(getNewOutputDevice(mPrimaryOutput, false));
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800315 mFMIsActive = false;
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530316 mPrimaryOutput->changeRefCount(AUDIO_STREAM_MUSIC, -1);
317 }
318 AudioParameter param = AudioParameter();
319 param.addInt(String8("handle_fm"), (int)newDevice);
320 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString());
321 }
322#endif /* FM_POWER_OPT end */
323
Zhou Songbd0264e2016-06-21 11:01:06 +0800324 updateDevicesAndOutputs();
325#ifdef DOLBY_ENABLE
326 // Before closing the opened outputs, update endpoint property with device capabilities
327 audio_devices_t audioOutputDevice = getDeviceForStrategy(getStrategy(AUDIO_STREAM_MUSIC), true);
328 mDolbyAudioPolicy.setEndpointSystemProperty(audioOutputDevice, mHwModules);
329#endif // DOLBY_END
330 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
331 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
332 updateCallRouting(newDevice);
333 }
334
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700335 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530336 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
337 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
338 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700339 // do not force device change on duplicated output because if device is 0, it will
340 // also force a device 0 for the two outputs it is duplicated to which may override
341 // a valid device selection on those outputs.
Sharad Sangle36781612015-05-28 16:15:16 +0530342 bool force = !desc->isDuplicated()
343 && (!device_distinguishes_on_address(device)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700344 // always force when disconnecting (a non-duplicated device)
345 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Sharad Sangle36781612015-05-28 16:15:16 +0530346 setOutputDevice(desc, newDevice, force, 0);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700347 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700348 }
349
Ashish Jain40cab3a2016-03-23 11:14:14 +0530350 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
351 cleanUpForDevice(devDesc);
352 }
353
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700354 mpClientInterface->onAudioPortListUpdate();
355 return NO_ERROR;
356 } // end if is output device
357
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700358 // handle input devices
359 if (audio_is_input_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700360 SortedVector <audio_io_handle_t> inputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700361
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700362 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700363 switch (state)
364 {
365 // handle input device connection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700366 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
367 if (index >= 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700368 ALOGW("setDeviceConnectionState() device already connected: %d", device);
369 return INVALID_OPERATION;
370 }
Sharad Sangle36781612015-05-28 16:15:16 +0530371 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700372 if (module == NULL) {
373 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
374 device);
375 return INVALID_OPERATION;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700376 }
Sharad Sangle36781612015-05-28 16:15:16 +0530377 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700378 return INVALID_OPERATION;
379 }
380
381 index = mAvailableInputDevices.add(devDesc);
382 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530383 mAvailableInputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700384 } else {
385 return NO_MEMORY;
386 }
Sharad Sangle36781612015-05-28 16:15:16 +0530387
388 // Set connect to HALs
389 AudioParameter param = AudioParameter(devDesc->mAddress);
390 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
391 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
392
393 // Propagate device availability to Engine
394 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700395 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700396
397 // handle input device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700398 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
399 if (index < 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700400 ALOGW("setDeviceConnectionState() device not connected: %d", device);
401 return INVALID_OPERATION;
402 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700403
404 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
405
406 // Set Disconnect to HALs
Sharad Sangle36781612015-05-28 16:15:16 +0530407 AudioParameter param = AudioParameter(devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700408 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
409 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
410
Sharad Sangle36781612015-05-28 16:15:16 +0530411 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700412 mAvailableInputDevices.remove(devDesc);
413
Sharad Sangle36781612015-05-28 16:15:16 +0530414 // Propagate device availability to Engine
415 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700416 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700417
418 default:
419 ALOGE("setDeviceConnectionState() invalid state: %x", state);
420 return BAD_VALUE;
421 }
422
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700423 closeAllInputs();
Sharad Sanglec66b9ca2016-12-21 18:40:54 +0530424 /*audio policy: fix call volume over USB*/
425 // As the input device list can impact the output device selection, update
426 // getDeviceForStrategy() cache
427 updateDevicesAndOutputs();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700428
Sharad Sangle36781612015-05-28 16:15:16 +0530429 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700430 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
431 updateCallRouting(newDevice);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700432 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700433
Ashish Jain40cab3a2016-03-23 11:14:14 +0530434 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
435 cleanUpForDevice(devDesc);
436 }
437
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700438 mpClientInterface->onAudioPortListUpdate();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700439 return NO_ERROR;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700440 } // end if is input device
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700441
442 ALOGW("setDeviceConnectionState() invalid device: %x", device);
443 return BAD_VALUE;
444}
Weiyin Jiangd211ef52016-02-16 15:55:11 +0800445
Preetam Singh Ranawat3db94ec2016-10-13 18:35:12 +0530446bool AudioPolicyManagerCustom::isInvalidationOfMusicStreamNeeded(routing_strategy strategy)
447{
448 if (strategy == STRATEGY_MEDIA) {
449 for (size_t i = 0; i < mOutputs.size(); i++) {
450 sp<SwAudioOutputDescriptor> newOutputDesc = mOutputs.valueAt(i);
451 if (newOutputDesc->mFormat == AUDIO_FORMAT_DSD)
452 return false;
453 }
454 }
455 return true;
456}
457
458void AudioPolicyManagerCustom::checkOutputForStrategy(routing_strategy strategy)
459{
460 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
461 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
462 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mOutputs);
463 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
464
465 // also take into account external policy-related changes: add all outputs which are
466 // associated with policies in the "before" and "after" output vectors
467 ALOGV("checkOutputForStrategy(): policy related outputs");
468 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
469 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
470 if (desc != 0 && desc->mPolicyMix != NULL) {
471 srcOutputs.add(desc->mIoHandle);
472 ALOGV(" previous outputs: adding %d", desc->mIoHandle);
473 }
474 }
475 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
476 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
477 if (desc != 0 && desc->mPolicyMix != NULL) {
478 dstOutputs.add(desc->mIoHandle);
479 ALOGV(" new outputs: adding %d", desc->mIoHandle);
480 }
481 }
482
483 if (!vectorsEqual(srcOutputs,dstOutputs) && isInvalidationOfMusicStreamNeeded(strategy)) {
484 AudioPolicyManager::checkOutputForStrategy(strategy);
485 }
486}
487
Sharad Sangle36781612015-05-28 16:15:16 +0530488// This function checks for the parameters which can be offloaded.
489// This can be enhanced depending on the capability of the DSP and policy
490// of the system.
491bool AudioPolicyManagerCustom::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700492{
Sharad Sangle36781612015-05-28 16:15:16 +0530493 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
494 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
495 offloadInfo.sample_rate, offloadInfo.channel_mask,
496 offloadInfo.format,
497 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
498 offloadInfo.has_video);
Ashish Jain40cab3a2016-03-23 11:14:14 +0530499
500 if (mMasterMono) {
501 return false; // no offloading if mono is set.
502 }
503
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530504#ifdef VOICE_CONCURRENCY
505 char concpropValue[PROPERTY_VALUE_MAX];
506 if (property_get("voice.playback.conc.disabled", concpropValue, NULL)) {
507 bool propenabled = atoi(concpropValue) || !strncmp("true", concpropValue, 4);
508 if (propenabled) {
509 if (isInCall())
510 {
511 ALOGD("\n copl: blocking compress offload on call mode\n");
512 return false;
513 }
514 }
515 }
516#endif
Preetam Singh Ranawat96afc162016-08-25 14:35:02 +0530517 if (property_get_bool("voice.dsd.playback.conc.disabled", true) &&
518 isInCall() && (offloadInfo.format == AUDIO_FORMAT_DSD)) {
519 ALOGD("blocking DSD compress offload on call mode");
520 return false;
521 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530522#ifdef RECORD_PLAY_CONCURRENCY
523 char recConcPropValue[PROPERTY_VALUE_MAX];
524 bool prop_rec_play_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700525
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530526 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
527 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
528 }
529
530 if ((prop_rec_play_enabled) &&
Lalit Kansara33918092016-12-06 22:42:26 +0530531 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCountOnDevices() > 0))) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530532 ALOGD("copl: blocking compress offload for record concurrency");
533 return false;
534 }
535#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530536 // Check if stream type is music, then only allow offload as of now.
537 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
538 {
539 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
540 return false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700541 }
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530542
Alexy Joseph0ac09032015-10-16 15:57:53 -0700543 // Check if offload has been disabled
544 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
545 if (offloadDisabled) {
546 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
547 return false;
548 }
549
Ashish Jainac510a72016-04-28 12:22:58 +0530550 //check if it's multi-channel AAC (includes sub formats) and FLAC format
551 if ((popcount(offloadInfo.channel_mask) > 2) &&
552 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
553 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS))) {
554 ALOGD("offload disabled for multi-channel AAC,FLAC and VORBIS format");
555 return false;
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530556 }
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530557
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530558#ifdef AUDIO_EXTN_FORMATS_ENABLED
Ashish Jainac510a72016-04-28 12:22:58 +0530559 //check if it's multi-channel FLAC/ALAC/WMA format with sample rate > 48k
560 if ((popcount(offloadInfo.channel_mask) > 2) &&
561 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
562 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) && (offloadInfo.sample_rate > 48000)) ||
563 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) && (offloadInfo.sample_rate > 48000)) ||
564 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) && (offloadInfo.sample_rate > 48000)) ||
565 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS))) {
566 ALOGD("offload disabled for multi-channel FLAC/ALAC/WMA/AAC_ADTS clips with sample rate > 48kHz");
567 return false;
568 }
yidonghae28e3a2016-08-19 11:31:15 +0800569
570 if ((((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) && (offloadInfo.bit_rate > MAX_BITRATE_WMA)) ||
571 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) && (offloadInfo.bit_rate > MAX_BITRATE_WMA_PRO)) ||
572 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) && (offloadInfo.bit_rate > MAX_BITRATE_WMA_LOSSLESS))){
573 //Safely choose the min bitrate as threshold and leave the restriction to NT decoder as we can't distinguish wma pro and wma lossless here.
574 ALOGD("offload disabled for WMA/WMA_PRO/WMA_LOSSLESS clips with bit rate over maximum supported value");
575 return false;
576 }
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530577#endif
Ashish Jainac510a72016-04-28 12:22:58 +0530578 //TODO: enable audio offloading with video when ready
579 const bool allowOffloadWithVideo =
580 property_get_bool("audio.offload.video", false /* default_value */);
581 if (offloadInfo.has_video && !allowOffloadWithVideo) {
582 ALOGV("isOffloadSupported: has_video == true, returning false");
583 return false;
584 }
Manish Dewanganf3cd0f82015-10-13 14:04:36 +0530585
Ashish Jainac510a72016-04-28 12:22:58 +0530586 const bool allowOffloadStreamingWithVideo = property_get_bool("av.streaming.offload.enable",
587 false /*default value*/);
588 if (offloadInfo.has_video && offloadInfo.is_streaming && !allowOffloadStreamingWithVideo) {
589 ALOGW("offload disabled by av.streaming.offload.enable %d",allowOffloadStreamingWithVideo);
590 return false;
Sharad Sangle36781612015-05-28 16:15:16 +0530591 }
592
593 //If duration is less than minimum value defined in property, return false
Ashish Jainac510a72016-04-28 12:22:58 +0530594 char propValue[PROPERTY_VALUE_MAX];
Sharad Sangle36781612015-05-28 16:15:16 +0530595 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
596 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
597 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
598 return false;
599 }
600 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
601 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
602 //duration checks only valid for MP3/AAC/ formats,
603 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
604 if ((offloadInfo.format == AUDIO_FORMAT_MP3) ||
605 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
606 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
Ashish Jainac510a72016-04-28 12:22:58 +0530607 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS))
608 return false;
609
610#ifdef AUDIO_EXTN_FORMATS_ENABLED
611 if (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) ||
Sharad Sangle36781612015-05-28 16:15:16 +0530612 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) ||
613 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) ||
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530614 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_APE) ||
Preetam Singh Ranawat191eb772016-09-09 17:10:19 +0530615 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_DSD) ||
Ashish Jainac510a72016-04-28 12:22:58 +0530616 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS))
Sharad Sangle36781612015-05-28 16:15:16 +0530617 return false;
Ashish Jainac510a72016-04-28 12:22:58 +0530618#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530619 }
620
621 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
622 // creating an offloaded track and tearing it down immediately after start when audioflinger
623 // detects there is an active non offloadable effect.
624 // FIXME: We should check the audio session here but we do not have it in this context.
625 // This may prevent offloading in rare situations where effects are left active by apps
626 // in the background.
627 if (mEffects.isNonOffloadableEffectEnabled()) {
628 return false;
629 }
Dhananjay Kumarcf558ba2016-07-29 19:33:13 +0530630
Sharad Sangle36781612015-05-28 16:15:16 +0530631 // See if there is a profile to support this.
632 // AUDIO_DEVICE_NONE
633 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
634 offloadInfo.sample_rate,
635 offloadInfo.format,
636 offloadInfo.channel_mask,
637 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
638 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
639 return (profile != 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700640}
Weiyin Jiangd211ef52016-02-16 15:55:11 +0800641
Sharad Sangle36781612015-05-28 16:15:16 +0530642audio_devices_t AudioPolicyManagerCustom::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
643 bool fromCache)
644{
645 audio_devices_t device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700646
Ashish Jain40cab3a2016-03-23 11:14:14 +0530647 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Sharad Sangle36781612015-05-28 16:15:16 +0530648 if (index >= 0) {
649 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
650 if (patchDesc->mUid != mUidCached) {
651 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Ashish Jain40cab3a2016-03-23 11:14:14 +0530652 outputDesc->device(), outputDesc->getPatchHandle());
Sharad Sangle36781612015-05-28 16:15:16 +0530653 return outputDesc->device();
654 }
655 }
656
657 // check the following by order of priority to request a routing change if necessary:
658 // 1: the strategy enforced audible is active and enforced on the output:
659 // use device for strategy enforced audible
660 // 2: we are in call or the strategy phone is active on the output:
661 // use device for strategy phone
662 // 3: the strategy for enforced audible is active but not enforced on the output:
663 // use the device for strategy enforced audible
664 // 4: the strategy sonification is active on the output:
665 // use device for strategy sonification
666 // 5: the strategy "respectful" sonification is active on the output:
667 // use device for strategy "respectful" sonification
668 // 6: the strategy accessibility is active on the output:
669 // use device for strategy accessibility
670 // 7: the strategy media is active on the output:
671 // use device for strategy media
672 // 8: the strategy DTMF is active on the output:
673 // use device for strategy DTMF
674 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
675 // use device for strategy t-t-s
676 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
677 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
678 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
679 } else if (isInCall() ||
680 isStrategyActive(outputDesc, STRATEGY_PHONE)||
681 isStrategyActive(mPrimaryOutput, STRATEGY_PHONE)) {
682 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
683 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
684 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
Garmond Leung20e9ad52017-02-03 10:28:32 -0800685 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Sharad Sangle36781612015-05-28 16:15:16 +0530686 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Garmond Leung20e9ad52017-02-03 10:28:32 -0800687 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Sharad Sangle36781612015-05-28 16:15:16 +0530688 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
689 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
690 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
691 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
692 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
693 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
694 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
695 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
696 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
697 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
698 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
699 }
700
701 ALOGV("getNewOutputDevice() selected device %x", device);
702 return device;
703}
Weiyin Jiangd211ef52016-02-16 15:55:11 +0800704
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700705void AudioPolicyManagerCustom::setPhoneState(audio_mode_t state)
706{
Ashish Jain40cab3a2016-03-23 11:14:14 +0530707 ALOGD("setPhoneState() state %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530708 // store previous phone state for management of sonification strategy below
Sharad Sangle4509cef2015-08-19 20:47:12 +0530709 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Sharad Sangle36781612015-05-28 16:15:16 +0530710 int oldState = mEngine->getPhoneState();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700711
Sharad Sangle36781612015-05-28 16:15:16 +0530712 if (mEngine->setPhoneState(state) != NO_ERROR) {
713 ALOGW("setPhoneState() invalid or same state %d", state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700714 return;
715 }
Sharad Sangle36781612015-05-28 16:15:16 +0530716 /// Opens: can these line be executed after the switch of volume curves???
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700717 // if leaving call state, handle special case of active streams
718 // pertaining to sonification strategy see handleIncallSonification()
Zhou Song58081b62016-02-24 13:10:55 +0800719 if (isStateInCall(oldState)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700720 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530721 for (size_t j = 0; j < mOutputs.size(); j++) {
722 audio_io_handle_t curOutput = mOutputs.keyAt(j);
723 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
724 if (stream == AUDIO_STREAM_PATCH) {
725 continue;
726 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530727 handleIncallSonification((audio_stream_type_t)stream, false, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +0530728 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700729 }
Sharad Sangle36781612015-05-28 16:15:16 +0530730
Zhou Song58081b62016-02-24 13:10:55 +0800731 // force reevaluating accessibility routing when call stops
Sharad Sangle36781612015-05-28 16:15:16 +0530732 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700733 }
734
Sharad Sangle36781612015-05-28 16:15:16 +0530735 /**
736 * Switching to or from incall state or switching between telephony and VoIP lead to force
737 * routing command.
738 */
739 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
740 || (is_state_in_call(state) && (state != oldState)));
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700741
742 // check for device and output changes triggered by new phone state
743 checkA2dpSuspend();
744 checkOutputForAllStrategies();
745 updateDevicesAndOutputs();
746
Sharad Sangle36781612015-05-28 16:15:16 +0530747 sp<SwAudioOutputDescriptor> hwOutputDesc = mPrimaryOutput;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530748#ifdef VOICE_CONCURRENCY
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530749 char propValue[PROPERTY_VALUE_MAX];
750 bool prop_playback_enabled = false, prop_rec_enabled=false, prop_voip_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700751
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530752 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
753 prop_playback_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
754 }
755
756 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
757 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
758 }
759
760 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
761 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
762 }
763
Dhananjay Kumar9125b2a2015-12-01 16:24:09 +0530764 if ((AUDIO_MODE_IN_CALL != oldState) && (AUDIO_MODE_IN_CALL == state)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530765 ALOGD("voice_conc:Entering to call mode oldState :: %d state::%d ",
766 oldState, state);
Dhananjay Kumar9125b2a2015-12-01 16:24:09 +0530767 mvoice_call_state = state;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530768 if (prop_rec_enabled) {
769 //Close all active inputs
770 audio_io_handle_t activeInput = mInputs.getActiveInput();
771 if (activeInput != 0) {
772 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
773 switch(activeDesc->mInputSource) {
774 case AUDIO_SOURCE_VOICE_UPLINK:
775 case AUDIO_SOURCE_VOICE_DOWNLINK:
776 case AUDIO_SOURCE_VOICE_CALL:
777 ALOGD("voice_conc:FOUND active input during call active: %d",activeDesc->mInputSource);
778 break;
779
780 case AUDIO_SOURCE_VOICE_COMMUNICATION:
781 if(prop_voip_enabled) {
782 ALOGD("voice_conc:CLOSING VoIP input source on call setup :%d ",activeDesc->mInputSource);
783 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
784 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
785 }
786 break;
787
788 default:
789 ALOGD("voice_conc:CLOSING input on call setup for inputSource: %d",activeDesc->mInputSource);
790 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
791 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
792 break;
793 }
794 }
795 } else if (prop_voip_enabled) {
796 audio_io_handle_t activeInput = mInputs.getActiveInput();
797 if (activeInput != 0) {
798 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
799 if (AUDIO_SOURCE_VOICE_COMMUNICATION == activeDesc->mInputSource) {
800 ALOGD("voice_conc:CLOSING VoIP on call setup : %d",activeDesc->mInputSource);
801 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
802 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
803 }
804 }
805 }
806 if (prop_playback_enabled) {
807 // Move tracks associated to this strategy from previous output to new output
808 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
809 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
810 if (i == AUDIO_STREAM_PATCH) {
811 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
812 continue;
813 }
814 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
815 if ((AUDIO_STREAM_MUSIC == i) ||
816 (AUDIO_STREAM_VOICE_CALL == i) ) {
817 ALOGD("voice_conc:Invalidate stream type %d", i);
818 mpClientInterface->invalidateStream((audio_stream_type_t)i);
819 }
820 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
821 ALOGD("voice_conc:Invalidate stream type %d", i);
822 mpClientInterface->invalidateStream((audio_stream_type_t)i);
823 }
824 }
825 }
826
827 for (size_t i = 0; i < mOutputs.size(); i++) {
828 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
829 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
830 ALOGD("voice_conc:ouput desc / profile is NULL");
831 continue;
832 }
833
Sidipotu Ashok738f19e2016-12-07 15:16:01 +0530834 bool isFastFallBackNeeded =
835 ((AUDIO_OUTPUT_FLAG_DEEP_BUFFER | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT_PCM) & outputDesc->mProfile->mFlags);
836
837 if ((AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) && isFastFallBackNeeded) {
838 if (((!outputDesc->isDuplicated() && outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY))
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530839 && prop_playback_enabled) {
840 ALOGD("voice_conc:calling suspendOutput on call mode for primary output");
841 mpClientInterface->suspendOutput(mOutputs.keyAt(i));
842 } //Close compress all sessions
843 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
844 && prop_playback_enabled) {
845 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
846 closeOutput(mOutputs.keyAt(i));
847 }
848 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_VOIP_RX)
849 && prop_voip_enabled) {
850 ALOGD("voice_conc:calling closeOutput on call mode for DIRECT output");
851 closeOutput(mOutputs.keyAt(i));
852 }
853 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
kunleiz0b55ec72016-11-10 16:23:58 +0800854 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_VOIP_RX) {
855 if (prop_voip_enabled) {
856 ALOGD("voice_conc:calling closeOutput on call mode for DIRECT output");
857 closeOutput(mOutputs.keyAt(i));
858 }
859 }
860 else if (prop_playback_enabled
861 && (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530862 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
863 closeOutput(mOutputs.keyAt(i));
864 }
865 }
866 }
Ashish Jaine4a22d52016-06-22 11:55:08 +0530867 // If effects where present on any of the above closed outputs,
868 // audioflinger moved them to the primary output by default
869 // move them back to the appropriate output.
870 moveGlobalEffect();
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530871 }
872
873 if ((AUDIO_MODE_IN_CALL == oldState || AUDIO_MODE_IN_COMMUNICATION == oldState) &&
874 (AUDIO_MODE_NORMAL == state) && prop_playback_enabled && mvoice_call_state) {
875 ALOGD("voice_conc:EXITING from call mode oldState :: %d state::%d \n",oldState, state);
876 mvoice_call_state = 0;
877 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
878 //restore PCM (deep-buffer) output after call termination
879 for (size_t i = 0; i < mOutputs.size(); i++) {
880 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
881 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
882 ALOGD("voice_conc:ouput desc / profile is NULL");
883 continue;
884 }
885 if (!outputDesc->isDuplicated() && outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
886 ALOGD("voice_conc:calling restoreOutput after call mode for primary output");
887 mpClientInterface->restoreOutput(mOutputs.keyAt(i));
888 }
889 }
890 }
891 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
892 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
893 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
894 if (i == AUDIO_STREAM_PATCH) {
895 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
896 continue;
897 }
898 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
899 if ((AUDIO_STREAM_MUSIC == i) ||
900 (AUDIO_STREAM_VOICE_CALL == i) ) {
901 mpClientInterface->invalidateStream((audio_stream_type_t)i);
902 }
903 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
904 mpClientInterface->invalidateStream((audio_stream_type_t)i);
905 }
906 }
907 }
908
909#endif
Preetam Singh Ranawat96afc162016-08-25 14:35:02 +0530910
911 sp<SwAudioOutputDescriptor> outputDesc = NULL;
912 for (size_t i = 0; i < mOutputs.size(); i++) {
913 outputDesc = mOutputs.valueAt(i);
914 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
915 ALOGD("voice_conc:ouput desc / profile is NULL");
916 continue;
917 }
918
919 if (property_get_bool("voice.dsd.playback.conc.disabled", true) &&
920 (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
921 (outputDesc->mFormat == AUDIO_FORMAT_DSD)) {
922 ALOGD("voice_conc:calling closeOutput on call mode for DSD COMPRESS output");
923 closeOutput(mOutputs.keyAt(i));
924 // call invalidate for music, so that DSD compress will fallback to deep-buffer.
925 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
926 }
927
928 }
929
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530930#ifdef RECORD_PLAY_CONCURRENCY
931 char recConcPropValue[PROPERTY_VALUE_MAX];
932 bool prop_rec_play_enabled = false;
933
934 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
935 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
936 }
937 if (prop_rec_play_enabled) {
938 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
939 ALOGD("phone state changed to MODE_IN_COMM invlaidating music and voice streams");
940 // call invalidate for voice streams, so that it can use deepbuffer with VoIP out device from HAL
941 mpClientInterface->invalidateStream(AUDIO_STREAM_VOICE_CALL);
942 // call invalidate for music, so that compress will fallback to deep-buffer with VoIP out device
943 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
944
945 // close compress output to make sure session will be closed before timeout(60sec)
946 for (size_t i = 0; i < mOutputs.size(); i++) {
947
948 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
949 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
950 ALOGD("ouput desc / profile is NULL");
951 continue;
952 }
953
954 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
955 ALOGD("calling closeOutput on call mode for COMPRESS output");
956 closeOutput(mOutputs.keyAt(i));
957 }
958 }
Ashish Jaine4a22d52016-06-22 11:55:08 +0530959 // If effects where present on any of the above closed outputs,
960 // audioflinger moved them to the primary output by default
961 // move them back to the appropriate output.
962 moveGlobalEffect();
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530963 } else if ((oldState == AUDIO_MODE_IN_COMMUNICATION) &&
964 (mEngine->getPhoneState() == AUDIO_MODE_NORMAL)) {
965 // call invalidate for music so that music can fallback to compress
966 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
967 }
968 }
969#endif
970 mPrevPhoneState = oldState;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700971 int delayMs = 0;
972 if (isStateInCall(state)) {
973 nsecs_t sysTime = systemTime();
974 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530975 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700976 // mute media and sonification strategies and delay device switch by the largest
977 // latency of any output where either strategy is active.
978 // This avoid sending the ring tone or music tail into the earpiece or headset.
Sharad Sangle36781612015-05-28 16:15:16 +0530979 if ((isStrategyActive(desc, STRATEGY_MEDIA,
980 SONIFICATION_HEADSET_MUSIC_DELAY,
981 sysTime) ||
982 isStrategyActive(desc, STRATEGY_SONIFICATION,
983 SONIFICATION_HEADSET_MUSIC_DELAY,
984 sysTime)) &&
985 (delayMs < (int)desc->latency()*2)) {
986 delayMs = desc->latency()*2;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700987 }
Sharad Sangle36781612015-05-28 16:15:16 +0530988 setStrategyMute(STRATEGY_MEDIA, true, desc);
989 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700990 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Sharad Sangle36781612015-05-28 16:15:16 +0530991 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
992 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700993 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
994 }
995 }
996
Sharad Sangle36781612015-05-28 16:15:16 +0530997 if (hasPrimaryOutput()) {
998 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
999 // the device returned is not necessarily reachable via this output
1000 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
1001 // force routing command to audio hardware when ending call
1002 // even if no device change is needed
1003 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
1004 rxDevice = mPrimaryOutput->device();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001005 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001006
Sharad Sangle36781612015-05-28 16:15:16 +05301007 if (state == AUDIO_MODE_IN_CALL) {
1008 updateCallRouting(rxDevice, delayMs);
1009 } else if (oldState == AUDIO_MODE_IN_CALL) {
1010 if (mCallRxPatch != 0) {
1011 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
1012 mCallRxPatch.clear();
1013 }
1014 if (mCallTxPatch != 0) {
1015 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
1016 mCallTxPatch.clear();
1017 }
1018 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
1019 } else {
1020 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001021 }
1022 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301023 //update device for all non-primary outputs
1024 for (size_t i = 0; i < mOutputs.size(); i++) {
1025 audio_io_handle_t output = mOutputs.keyAt(i);
1026 if (output != mPrimaryOutput->mIoHandle) {
1027 newDevice = getNewOutputDevice(mOutputs.valueFor(output), false /*fromCache*/);
1028 setOutputDevice(mOutputs.valueFor(output), newDevice, (newDevice != AUDIO_DEVICE_NONE));
1029 }
1030 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001031 // if entering in call state, handle special case of active streams
1032 // pertaining to sonification strategy see handleIncallSonification()
1033 if (isStateInCall(state)) {
1034 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +05301035 for (size_t j = 0; j < mOutputs.size(); j++) {
1036 audio_io_handle_t curOutput = mOutputs.keyAt(j);
1037 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
1038 if (stream == AUDIO_STREAM_PATCH) {
1039 continue;
1040 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301041 handleIncallSonification((audio_stream_type_t)stream, true, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +05301042 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001043 }
Zhou Song58081b62016-02-24 13:10:55 +08001044
1045 // force reevaluating accessibility routing when call starts
1046 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001047 }
1048
1049 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
1050 if (state == AUDIO_MODE_RINGTONE &&
1051 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
1052 mLimitRingtoneVolume = true;
1053 } else {
1054 mLimitRingtoneVolume = false;
1055 }
1056}
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301057
1058void AudioPolicyManagerCustom::setForceUse(audio_policy_force_use_t usage,
1059 audio_policy_forced_cfg_t config)
1060{
Ashish Jain40cab3a2016-03-23 11:14:14 +05301061 ALOGD("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301062
1063 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
1064 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
1065 return;
1066 }
1067 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
1068 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
1069 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
1070
1071 // check for device and output changes triggered by new force usage
1072 checkA2dpSuspend();
1073 checkOutputForAllStrategies();
1074 updateDevicesAndOutputs();
Ashish Jain40cab3a2016-03-23 11:14:14 +05301075
Sharad Sanglec66b9ca2016-12-21 18:40:54 +05301076 /*audio policy: workaround for truncated touch sounds*/
1077 //FIXME: workaround for truncated touch sounds
1078 // to be removed when the problem is handled by system UI
1079 uint32_t delayMs = 0;
1080 uint32_t waitMs = 0;
1081 if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) {
1082 delayMs = TOUCH_SOUND_FIXED_DELAY_MS;
1083 }
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301084 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
1085 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
Sharad Sanglec66b9ca2016-12-21 18:40:54 +05301086 waitMs = updateCallRouting(newDevice, delayMs);
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301087 }
1088 // Use reverse loop to make sure any low latency usecases (generally tones)
1089 // are not routed before non LL usecases (generally music).
1090 // We can safely assume that LL output would always have lower index,
1091 // and use this work-around to avoid routing of output with music stream
1092 // from the context of short lived LL output.
1093 // Note: in case output's share backend(HAL sharing is implicit) all outputs
1094 // gets routing update while processing first output itself.
1095 for (size_t i = mOutputs.size(); i > 0; i--) {
1096 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i-1);
1097 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
Sharad Sanglec66b9ca2016-12-21 18:40:54 +05301098 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
1099 waitMs = setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE),
1100 delayMs);
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301101 }
1102 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
Sharad Sanglec66b9ca2016-12-21 18:40:54 +05301103 applyStreamVolumes(outputDesc, newDevice, waitMs, true);
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301104 }
1105 }
1106
1107 audio_io_handle_t activeInput = mInputs.getActiveInput();
1108 if (activeInput != 0) {
Ashish Jain40cab3a2016-03-23 11:14:14 +05301109 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
1110 audio_devices_t newDevice = getNewInputDevice(activeInput);
1111 // Force new input selection if the new device can not be reached via current input
1112 if (activeDesc->mProfile->getSupportedDevices().types() & (newDevice & ~AUDIO_DEVICE_BIT_IN)) {
1113 setInputDevice(activeInput, newDevice);
1114 } else {
1115 closeInput(activeInput);
1116 }
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301117 }
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301118}
1119
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301120status_t AudioPolicyManagerCustom::stopSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301121 audio_stream_type_t stream,
1122 bool forceDeviceUpdate)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001123{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301124 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1125 ALOGW("stopSource() invalid stream %d", stream);
1126 return INVALID_OPERATION;
1127 }
Sharad Sangle36781612015-05-28 16:15:16 +05301128 // always handle stream stop, check which stream type is stopping
1129 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001130
Sharad Sangle36781612015-05-28 16:15:16 +05301131 // handle special case for sonification while in call
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001132 if (isInCall()) {
Sharad Sangle36781612015-05-28 16:15:16 +05301133 if (outputDesc->isDuplicated()) {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301134 handleIncallSonification(stream, false, false, outputDesc->subOutput1()->mIoHandle);
1135 handleIncallSonification(stream, false, false, outputDesc->subOutput2()->mIoHandle);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001136 }
Sharad Sangle36781612015-05-28 16:15:16 +05301137 handleIncallSonification(stream, false, false, outputDesc->mIoHandle);
1138 }
1139
1140 if (outputDesc->mRefCount[stream] > 0) {
1141 // decrement usage count of this stream on the output
1142 outputDesc->changeRefCount(stream, -1);
1143
1144 // store time at which the stream was stopped - see isStreamActive()
1145 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
1146 outputDesc->mStopTime[stream] = systemTime();
Zhou Song5dcddc92015-09-21 14:36:57 +08001147 audio_devices_t prevDevice = outputDesc->device();
Sharad Sangle36781612015-05-28 16:15:16 +05301148 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
1149 // delay the device switch by twice the latency because stopOutput() is executed when
1150 // the track stop() command is received and at that time the audio track buffer can
1151 // still contain data that needs to be drained. The latency only covers the audio HAL
1152 // and kernel buffers. Also the latency does not always include additional delay in the
1153 // audio path (audio DSP, CODEC ...)
1154 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
1155
1156 // force restoring the device selection on other active outputs if it differs from the
1157 // one being selected for this output
1158 for (size_t i = 0; i < mOutputs.size(); i++) {
1159 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1160 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1161 if (desc != outputDesc &&
1162 desc->isActive() &&
1163 outputDesc->sharesHwModuleWith(desc) &&
1164 (newDevice != desc->device())) {
Sharad Sangle4509cef2015-08-19 20:47:12 +05301165 audio_devices_t dev = getNewOutputDevice(mOutputs.valueFor(curOutput), false /*fromCache*/);
Naresh Tanniru11393702016-09-14 11:25:37 +05301166 bool force = desc->device() != dev;
Zhou Song5dcddc92015-09-21 14:36:57 +08001167 uint32_t delayMs;
1168 if (dev == prevDevice) {
1169 delayMs = 0;
1170 } else {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301171 delayMs = outputDesc->latency()*2;
Zhou Song5dcddc92015-09-21 14:36:57 +08001172 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301173 setOutputDevice(desc,
1174 dev,
Naresh Tanniru11393702016-09-14 11:25:37 +05301175 force,
Zhou Song5dcddc92015-09-21 14:36:57 +08001176 delayMs);
Sharad Sanglec66b9ca2016-12-21 18:40:54 +05301177 /*audio policy: fix media volume after ringtone*/
1178 // re-apply device specific volume if not done by setOutputDevice()
1179 if (!force) {
1180 applyStreamVolumes(desc, dev, delayMs);
1181 }
Sharad Sangle36781612015-05-28 16:15:16 +05301182 }
1183 }
1184 // update the outputs if stopping one with a stream that can affect notification routing
1185 handleNotificationRoutingForStream(stream);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001186 }
Sharad Sangle36781612015-05-28 16:15:16 +05301187 return NO_ERROR;
1188 } else {
1189 ALOGW("stopOutput() refcount is already 0");
1190 return INVALID_OPERATION;
1191 }
1192}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001193
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301194status_t AudioPolicyManagerCustom::startSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301195 audio_stream_type_t stream,
1196 audio_devices_t device,
Lalit Kansaraf803ac52016-05-09 19:12:01 +05301197 const char *address,
Sharad Sangle36781612015-05-28 16:15:16 +05301198 uint32_t *delayMs)
1199{
1200 // cannot start playback of STREAM_TTS if any other output is being used
1201 uint32_t beaconMuteLatency = 0;
1202
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301203 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1204 ALOGW("startSource() invalid stream %d", stream);
1205 return INVALID_OPERATION;
1206 }
1207
Sharad Sangle36781612015-05-28 16:15:16 +05301208 *delayMs = 0;
1209 if (stream == AUDIO_STREAM_TTS) {
1210 ALOGV("\t found BEACON stream");
Ashish Jain40cab3a2016-03-23 11:14:14 +05301211 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301212 return INVALID_OPERATION;
1213 } else {
1214 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001215 }
Sharad Sangle36781612015-05-28 16:15:16 +05301216 } else {
1217 // some playback other than beacon starts
1218 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1219 }
1220
Zhou Song63f0b852016-12-20 14:35:06 +08001221 // force device change if the output is inactive and no audio patch is already present.
Ashish Jain40cab3a2016-03-23 11:14:14 +05301222 // check active before incrementing usage count
Zhou Song63f0b852016-12-20 14:35:06 +08001223 bool force = !outputDesc->isActive() &&
1224 (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE);
Ashish Jain40cab3a2016-03-23 11:14:14 +05301225
Sharad Sangle36781612015-05-28 16:15:16 +05301226 // increment usage count for this stream on the requested output:
1227 // NOTE that the usage count is the same for duplicated output and hardware output which is
1228 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1229 outputDesc->changeRefCount(stream, 1);
1230
1231 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
1232 // starting an output being rerouted?
1233 if (device == AUDIO_DEVICE_NONE) {
1234 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001235 }
Sharad Sangle36781612015-05-28 16:15:16 +05301236 routing_strategy strategy = getStrategy(stream);
1237 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
1238 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1239 (beaconMuteLatency > 0);
1240 uint32_t waitMs = beaconMuteLatency;
Sharad Sangle36781612015-05-28 16:15:16 +05301241 for (size_t i = 0; i < mOutputs.size(); i++) {
1242 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1243 if (desc != outputDesc) {
Zhou Song63f0b852016-12-20 14:35:06 +08001244 // force a device change if any other output is:
1245 // - managed by the same hw module
1246 // - has a current device selection that differs from selected device.
1247 // - supports currently selected device
1248 // - has an active audio patch
Sharad Sangle36781612015-05-28 16:15:16 +05301249 // In this case, the audio HAL must receive the new device selection so that it can
1250 // change the device currently selected by the other active output.
1251 if (outputDesc->sharesHwModuleWith(desc) &&
Zhou Song63f0b852016-12-20 14:35:06 +08001252 desc->device() != device &&
1253 desc->supportedDevices() & device &&
1254 desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) {
Sharad Sangle36781612015-05-28 16:15:16 +05301255 force = true;
1256 }
1257 // wait for audio on other active outputs to be presented when starting
1258 // a notification so that audio focus effect can propagate, or that a mute/unmute
1259 // event occurred for beacon
1260 uint32_t latency = desc->latency();
1261 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1262 waitMs = latency;
1263 }
1264 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001265 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05301266 uint32_t muteWaitMs;
Lalit Kansaraf803ac52016-05-09 19:12:01 +05301267 muteWaitMs = setOutputDevice(outputDesc, device, force, 0, NULL, address);
Sharad Sangle36781612015-05-28 16:15:16 +05301268
1269 // handle special case for sonification while in call
1270 if (isInCall()) {
1271 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001272 }
Sharad Sangle36781612015-05-28 16:15:16 +05301273
1274 // apply volume rules for current stream and device if necessary
1275 checkAndSetVolume(stream,
Ashish Jain40cab3a2016-03-23 11:14:14 +05301276 mVolumeCurves->getVolumeIndex(stream, device),
Sharad Sangle36781612015-05-28 16:15:16 +05301277 outputDesc,
1278 device);
1279
1280 // update the outputs if starting an output with a stream that can affect notification
1281 // routing
1282 handleNotificationRoutingForStream(stream);
1283
1284 // force reevaluating accessibility routing when ringtone or alarm starts
1285 if (strategy == STRATEGY_SONIFICATION) {
1286 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1287 }
Sharad Sanglec66b9ca2016-12-21 18:40:54 +05301288 if (waitMs > muteWaitMs) {
1289 *delayMs = waitMs - muteWaitMs;
1290 }
1291
Sharad Sangle36781612015-05-28 16:15:16 +05301292 }
1293 else {
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001294 // handle special case for sonification while in call
1295 if (isInCall()) {
1296 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Sharad Sangle36781612015-05-28 16:15:16 +05301297 }
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001298 }
Sharad Sangle36781612015-05-28 16:15:16 +05301299 return NO_ERROR;
1300}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001301
Sharad Sangle36781612015-05-28 16:15:16 +05301302void AudioPolicyManagerCustom::handleIncallSonification(audio_stream_type_t stream,
1303 bool starting, bool stateChange,
1304 audio_io_handle_t output)
1305{
1306 if(!hasPrimaryOutput()) {
1307 return;
1308 }
1309 // no action needed for AUDIO_STREAM_PATCH stream type, it's for internal flinger tracks
1310 if (stream == AUDIO_STREAM_PATCH) {
1311 return;
1312 }
1313 // if the stream pertains to sonification strategy and we are in call we must
1314 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
1315 // in the device used for phone strategy and play the tone if the selected device does not
1316 // interfere with the device used for phone strategy
1317 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
1318 // many times as there are active tracks on the output
1319 const routing_strategy stream_strategy = getStrategy(stream);
1320 if ((stream_strategy == STRATEGY_SONIFICATION) ||
1321 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
1322 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
1323 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
1324 stream, starting, outputDesc->mDevice, stateChange);
1325 if (outputDesc->mRefCount[stream]) {
1326 int muteCount = 1;
1327 if (stateChange) {
1328 muteCount = outputDesc->mRefCount[stream];
1329 }
1330 if (audio_is_low_visibility(stream)) {
1331 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
1332 for (int i = 0; i < muteCount; i++) {
1333 setStreamMute(stream, starting, outputDesc);
1334 }
1335 } else {
1336 ALOGV("handleIncallSonification() high visibility");
1337 if (outputDesc->device() &
1338 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
1339 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
1340 for (int i = 0; i < muteCount; i++) {
1341 setStreamMute(stream, starting, outputDesc);
1342 }
1343 }
1344 if (starting) {
1345 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
1346 AUDIO_STREAM_VOICE_CALL);
1347 } else {
1348 mpClientInterface->stopTone();
1349 }
1350 }
1351 }
1352 }
1353}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001354
Sharad Sangle36781612015-05-28 16:15:16 +05301355void AudioPolicyManagerCustom::handleNotificationRoutingForStream(audio_stream_type_t stream) {
1356 switch(stream) {
1357 case AUDIO_STREAM_MUSIC:
1358 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
1359 updateDevicesAndOutputs();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001360 break;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001361 default:
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001362 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001363 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001364}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001365
Sharad Sangle36781612015-05-28 16:15:16 +05301366status_t AudioPolicyManagerCustom::checkAndSetVolume(audio_stream_type_t stream,
1367 int index,
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301368 const sp<AudioOutputDescriptor>& outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301369 audio_devices_t device,
1370 int delayMs, bool force)
1371{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301372 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1373 ALOGW("checkAndSetVolume() invalid stream %d", stream);
1374 return INVALID_OPERATION;
1375 }
Sharad Sangle36781612015-05-28 16:15:16 +05301376 // do not change actual stream volume if the stream is muted
1377 if (outputDesc->mMuteCount[stream] != 0) {
1378 ALOGVV("checkAndSetVolume() stream %d muted count %d",
1379 stream, outputDesc->mMuteCount[stream]);
1380 return NO_ERROR;
1381 }
1382 audio_policy_forced_cfg_t forceUseForComm =
1383 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
1384 // do not change in call volume if bluetooth is connected and vice versa
1385 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
1386 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
1387 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
1388 stream, forceUseForComm);
1389 return INVALID_OPERATION;
1390 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001391
Sharad Sangle36781612015-05-28 16:15:16 +05301392 if (device == AUDIO_DEVICE_NONE) {
1393 device = outputDesc->device();
1394 }
1395
1396 float volumeDb = computeVolume(stream, index, device);
1397 if (outputDesc->isFixedVolume(device)) {
1398 volumeDb = 0.0f;
1399 }
1400
1401 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
1402
1403 if (stream == AUDIO_STREAM_VOICE_CALL ||
1404 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
1405 float voiceVolume;
1406 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
1407 if (stream == AUDIO_STREAM_VOICE_CALL) {
Ashish Jain40cab3a2016-03-23 11:14:14 +05301408 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Sharad Sangle36781612015-05-28 16:15:16 +05301409 } else {
1410 voiceVolume = 1.0;
1411 }
1412
1413 if (voiceVolume != mLastVoiceVolume && ((outputDesc == mPrimaryOutput) ||
1414 isDirectOutput(outputDesc->mIoHandle) || device & AUDIO_DEVICE_OUT_ALL_USB)) {
1415 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
1416 mLastVoiceVolume = voiceVolume;
1417 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301418#ifdef FM_POWER_OPT
1419 } else if (stream == AUDIO_STREAM_MUSIC && hasPrimaryOutput() &&
Haynes Mathew George603ae9b2015-12-21 17:23:59 -08001420 outputDesc == mPrimaryOutput && mFMIsActive) {
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001421 /* Avoid unnecessary set_parameter calls as it puts the primary
1422 outputs FastMixer in HOT_IDLE leading to breaks in audio */
1423 if (volumeDb != mPrevFMVolumeDb) {
1424 mPrevFMVolumeDb = volumeDb;
1425 AudioParameter param = AudioParameter();
1426 param.addFloat(String8("fm_volume"), Volume::DbToAmpl(volumeDb));
Mingming Yin1433cc62016-01-04 14:58:00 -08001427 //Double delayMs to avoid sound burst while device switch.
1428 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString(), delayMs*2);
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001429 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301430#endif /* FM_POWER_OPT end */
Sharad Sangle36781612015-05-28 16:15:16 +05301431 }
1432
1433 return NO_ERROR;
1434}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001435
Sharad Sangle36781612015-05-28 16:15:16 +05301436bool AudioPolicyManagerCustom::isDirectOutput(audio_io_handle_t output) {
1437 for (size_t i = 0; i < mOutputs.size(); i++) {
1438 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1439 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1440 if ((curOutput == output) && (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
1441 return true;
1442 }
1443 }
1444 return false;
1445}
vivek mehta0ea887a2015-08-26 14:01:20 -07001446
Sharad Sangle75170722016-11-21 16:21:06 +05301447bool static tryForDirectPCM(int bitWidth, audio_output_flags_t *flags, uint32_t samplingRate)
Ashish Jainac510a72016-04-28 12:22:58 +05301448{
Ashish Jain771d02f2016-10-27 22:35:46 +05301449 bool playerDirectPCM = false; // Output request for Track created by mediaplayer
1450 bool trackDirectPCM = false; // Output request for track created by other apps
Ashish Jain27564312016-11-14 14:58:06 +05301451 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
Ashish Jainac510a72016-04-28 12:22:58 +05301452
Ashish Jain771d02f2016-10-27 22:35:46 +05301453 // Direct PCM is allowed only if
1454 // In case of mediaPlayer playback
1455 // 16 bit direct pcm or 24bit direct PCM property is set and
1456 // the FLAG requested is DIRECT_PCM ( NuPlayer case) or
1457 // In case of AudioTracks created by apps
1458 // track offload is enabled and FLAG requested is FLAG_NONE.
1459
Ashish Jain27564312016-11-14 14:58:06 +05301460 if (offloadDisabled) {
1461 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
1462 }
1463
Ashish Jain771d02f2016-10-27 22:35:46 +05301464 if (*flags == AUDIO_OUTPUT_FLAG_DIRECT_PCM) {
1465 if (bitWidth == 24 || bitWidth == 32)
1466 playerDirectPCM =
1467 property_get_bool("audio.offload.pcm.24bit.enable", false);
1468 else
1469 playerDirectPCM =
1470 property_get_bool("audio.offload.pcm.16bit.enable", false);
1471 // Reset flag to NONE so that we can still reuse direct pcm criteria check
1472 // in getOutputforDevice
1473 *flags = AUDIO_OUTPUT_FLAG_NONE;
Sharad Sangle75170722016-11-21 16:21:06 +05301474 } else if ((*flags == AUDIO_OUTPUT_FLAG_NONE) && (samplingRate % SAMPLE_RATE_8000 == 0)) {
Ashish Jain771d02f2016-10-27 22:35:46 +05301475 trackDirectPCM = property_get_bool("audio.offload.track.enable", true);
1476 }
1477
Ashish Jain27564312016-11-14 14:58:06 +05301478 ALOGI("Direct PCM %s for this request",
1479 (!offloadDisabled && (trackDirectPCM || playerDirectPCM))?"can be enabled":"is disabled");
1480
1481 return (!offloadDisabled && (trackDirectPCM || playerDirectPCM));
Ashish Jainac510a72016-04-28 12:22:58 +05301482}
1483
vivek mehta0ea887a2015-08-26 14:01:20 -07001484status_t AudioPolicyManagerCustom::getOutputForAttr(const audio_attributes_t *attr,
1485 audio_io_handle_t *output,
1486 audio_session_t session,
1487 audio_stream_type_t *stream,
1488 uid_t uid,
1489 uint32_t samplingRate,
1490 audio_format_t format,
1491 audio_channel_mask_t channelMask,
1492 audio_output_flags_t flags,
1493 audio_port_handle_t selectedDeviceId,
1494 const audio_offload_info_t *offloadInfo)
1495{
1496 audio_offload_info_t tOffloadInfo = AUDIO_INFO_INITIALIZER;
1497
Ashish Jainac510a72016-04-28 12:22:58 +05301498 uint32_t bitWidth = (audio_bytes_per_sample(format) * 8);
Alexy Joseph0ac09032015-10-16 15:57:53 -07001499
Alexy Joseph0ac09032015-10-16 15:57:53 -07001500
Sharad Sangle75170722016-11-21 16:21:06 +05301501 if (tryForDirectPCM(bitWidth, &flags, samplingRate) &&
Ashish Jain27564312016-11-14 14:58:06 +05301502 (offloadInfo == NULL)) {
vivek mehta0ea887a2015-08-26 14:01:20 -07001503
vivek mehta0ea887a2015-08-26 14:01:20 -07001504 tOffloadInfo.sample_rate = samplingRate;
1505 tOffloadInfo.channel_mask = channelMask;
1506 tOffloadInfo.format = format;
1507 tOffloadInfo.stream_type = *stream;
Ashish Jainac510a72016-04-28 12:22:58 +05301508 tOffloadInfo.bit_width = bitWidth;
vivek mehta0ea887a2015-08-26 14:01:20 -07001509 if (attr != NULL) {
1510 ALOGV("found attribute .. setting usage %d ", attr->usage);
1511 tOffloadInfo.usage = attr->usage;
1512 } else {
Alexy Joseph0ac09032015-10-16 15:57:53 -07001513 ALOGI("%s:: attribute is NULL .. no usage set", __func__);
vivek mehta0ea887a2015-08-26 14:01:20 -07001514 }
1515 offloadInfo = &tOffloadInfo;
1516 }
1517
1518 return AudioPolicyManager::getOutputForAttr(attr, output, session, stream,
1519 (uid_t)uid, (uint32_t)samplingRate,
1520 format, (audio_channel_mask_t)channelMask,
1521 flags, (audio_port_handle_t)selectedDeviceId,
1522 offloadInfo);
1523}
1524
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001525audio_io_handle_t AudioPolicyManagerCustom::getOutputForDevice(
1526 audio_devices_t device,
Sharad Sangle36781612015-05-28 16:15:16 +05301527 audio_session_t session __unused,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001528 audio_stream_type_t stream,
1529 uint32_t samplingRate,
1530 audio_format_t format,
1531 audio_channel_mask_t channelMask,
1532 audio_output_flags_t flags,
1533 const audio_offload_info_t *offloadInfo)
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001534{
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001535 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001536 status_t status;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001537
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001538#ifdef AUDIO_POLICY_TEST
1539 if (mCurOutput != 0) {
1540 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1541 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1542
1543 if (mTestOutputs[mCurOutput] == 0) {
1544 ALOGV("getOutput() opening test output");
Sharad Sangle36781612015-05-28 16:15:16 +05301545 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
1546 mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001547 outputDesc->mDevice = mTestDevice;
1548 outputDesc->mLatency = mTestLatencyMs;
1549 outputDesc->mFlags =
1550 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
1551 outputDesc->mRefCount[stream] = 0;
1552 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1553 config.sample_rate = mTestSamplingRate;
1554 config.channel_mask = mTestChannels;
1555 config.format = mTestFormat;
1556 if (offloadInfo != NULL) {
1557 config.offload_info = *offloadInfo;
1558 }
1559 status = mpClientInterface->openOutput(0,
1560 &mTestOutputs[mCurOutput],
1561 &config,
1562 &outputDesc->mDevice,
1563 String8(""),
1564 &outputDesc->mLatency,
1565 outputDesc->mFlags);
1566 if (status == NO_ERROR) {
1567 outputDesc->mSamplingRate = config.sample_rate;
1568 outputDesc->mFormat = config.format;
1569 outputDesc->mChannelMask = config.channel_mask;
1570 AudioParameter outputCmd = AudioParameter();
1571 outputCmd.addInt(String8("set_id"),mCurOutput);
1572 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1573 addOutput(mTestOutputs[mCurOutput], outputDesc);
1574 }
1575 }
1576 return mTestOutputs[mCurOutput];
1577 }
1578#endif //AUDIO_POLICY_TEST
Sharad Sangle36781612015-05-28 16:15:16 +05301579 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1580 (stream != AUDIO_STREAM_MUSIC)) {
1581 // compress should not be used for non-music streams
1582 ALOGE("Offloading only allowed with music stream");
1583 return 0;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301584 }
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301585
1586 if ((stream == AUDIO_STREAM_VOICE_CALL) &&
1587 (channelMask == 1) &&
Shiv Maliyappanahallid77d3b32016-02-02 13:45:50 -08001588 (samplingRate == 8000 || samplingRate == 16000 ||
1589 samplingRate == 32000 || samplingRate == 48000)) {
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301590 // Allow Voip direct output only if:
1591 // audio mode is MODE_IN_COMMUNCATION; AND
1592 // voip output is not opened already; AND
1593 // requested sample rate matches with that of voip input stream (if opened already)
1594 int value = 0;
1595 uint32_t mode = 0, voipOutCount = 1, voipSampleRate = 1;
Alexy Joseph9323d8b2017-02-17 10:30:07 -08001596 bool is_vr_mode_on = false;
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301597 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1598 String8("audio_mode"));
1599 AudioParameter result = AudioParameter(valueStr);
1600 if (result.getInt(String8("audio_mode"), value) == NO_ERROR) {
1601 mode = value;
1602 }
1603
1604 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1605 String8("voip_out_stream_count"));
1606 result = AudioParameter(valueStr);
1607 if (result.getInt(String8("voip_out_stream_count"), value) == NO_ERROR) {
1608 voipOutCount = value;
1609 }
1610
1611 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1612 String8("voip_sample_rate"));
1613 result = AudioParameter(valueStr);
1614 if (result.getInt(String8("voip_sample_rate"), value) == NO_ERROR) {
1615 voipSampleRate = value;
1616 }
1617
1618 if ((mode == AUDIO_MODE_IN_COMMUNICATION) && (voipOutCount == 0) &&
1619 ((voipSampleRate == 0) || (voipSampleRate == samplingRate))) {
1620 if (audio_is_linear_pcm(format)) {
1621 char propValue[PROPERTY_VALUE_MAX] = {0};
1622 property_get("use.voice.path.for.pcm.voip", propValue, "0");
1623 bool voipPcmSysPropEnabled = !strncmp("true", propValue, sizeof("true"));
1624 if (voipPcmSysPropEnabled && (format == AUDIO_FORMAT_PCM_16_BIT)) {
Karthik Reddy Katta3fb9be72015-11-25 11:18:27 +05301625 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX |
1626 AUDIO_OUTPUT_FLAG_DIRECT);
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301627 ALOGD("Set VoIP and Direct output flags for PCM format");
1628 }
1629 }
1630 }
Alexy Joseph9323d8b2017-02-17 10:30:07 -08001631 //IF VOIP is going to be started at the same time as when
1632 //vr is enabled, get VOIP to fallback to low latency
1633 String8 vr_value;
1634 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1635 String8("vr_audio_mode_on"));
1636 result = AudioParameter(valueStr);
1637 if (result.get(String8("vr_audio_mode_on"), vr_value) == NO_ERROR) {
1638 is_vr_mode_on = vr_value.contains("true");
1639 ALOGI("VR mode is %d, switch to primary output if request is for fast|raw",
1640 is_vr_mode_on);
1641 }
1642
1643 if (is_vr_mode_on) {
1644 //check the flags being requested for, and clear FAST|RAW
1645 flags = (audio_output_flags_t)(flags &
1646 (~(AUDIO_OUTPUT_FLAG_FAST|AUDIO_OUTPUT_FLAG_RAW)));
1647
1648 }
1649
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301650 }
1651
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301652#ifdef VOICE_CONCURRENCY
1653 char propValue[PROPERTY_VALUE_MAX];
1654 bool prop_play_enabled=false, prop_voip_enabled = false;
1655
1656 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1657 prop_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001658 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301659
1660 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1661 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1662 }
1663
Sidipotu Ashok738f19e2016-12-07 15:16:01 +05301664 bool isDeepBufferFallBackNeeded =
1665 ((AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT_PCM) & flags);
1666 bool isFastFallBackNeeded =
1667 ((AUDIO_OUTPUT_FLAG_DEEP_BUFFER | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT_PCM) & flags);
1668
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301669 if (prop_play_enabled && mvoice_call_state) {
1670 //check if voice call is active / running in background
1671 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1672 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1673 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1674 {
1675 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1676 if(prop_voip_enabled) {
1677 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1678 flags );
1679 return 0;
1680 }
1681 }
1682 else {
Sidipotu Ashok738f19e2016-12-07 15:16:01 +05301683 if (isFastFallBackNeeded &&
1684 (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301685 ALOGD("voice_conc:IN call mode adding ULL flags .. flags: %x ", flags );
1686 flags = AUDIO_OUTPUT_FLAG_FAST;
Sidipotu Ashok738f19e2016-12-07 15:16:01 +05301687 } else if (isDeepBufferFallBackNeeded &&
1688 (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301689 if (AUDIO_STREAM_MUSIC == stream) {
1690 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1691 ALOGD("voice_conc:IN call mode adding deep-buffer flags %x ", flags );
1692 }
1693 else {
1694 flags = AUDIO_OUTPUT_FLAG_FAST;
1695 ALOGD("voice_conc:IN call mode adding fast flags %x ", flags );
1696 }
1697 }
1698 }
1699 }
1700 } else if (prop_voip_enabled && mvoice_call_state) {
1701 //check if voice call is active / running in background
1702 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1703 //return only ULL ouput
1704 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1705 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1706 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1707 {
1708 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1709 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1710 flags );
1711 return 0;
1712 }
1713 }
1714 }
1715#endif
1716#ifdef RECORD_PLAY_CONCURRENCY
1717 char recConcPropValue[PROPERTY_VALUE_MAX];
1718 bool prop_rec_play_enabled = false;
1719
1720 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
1721 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
1722 }
1723 if ((prop_rec_play_enabled) &&
Lalit Kansara33918092016-12-06 22:42:26 +05301724 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCountOnDevices() > 0))) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301725 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
1726 if (AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1727 // allow VoIP using voice path
1728 // Do nothing
1729 } else if((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1730 ALOGD("voice_conc:MODE_IN_COMM is setforcing deep buffer output for non ULL... flags: %x", flags);
1731 // use deep buffer path for all non ULL outputs
1732 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1733 }
1734 } else if ((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1735 ALOGD("voice_conc:Record mode is on forcing deep buffer output for non ULL... flags: %x ", flags);
1736 // use deep buffer path for all non ULL outputs
1737 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1738 }
1739 }
1740 if (prop_rec_play_enabled &&
1741 (stream == AUDIO_STREAM_ENFORCED_AUDIBLE)) {
1742 ALOGD("Record conc is on forcing ULL output for ENFORCED_AUDIBLE");
1743 flags = AUDIO_OUTPUT_FLAG_FAST;
1744 }
1745#endif
1746
Sharad Sangle4509cef2015-08-19 20:47:12 +05301747#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Sharad Sangle36781612015-05-28 16:15:16 +05301748 /*
1749 * WFD audio routes back to target speaker when starting a ringtone playback.
1750 * This is because primary output is reused for ringtone, so output device is
1751 * updated based on SONIFICATION strategy for both ringtone and music playback.
1752 * The same issue is not seen on remoted_submix HAL based WFD audio because
1753 * primary output is not reused and a new output is created for ringtone playback.
1754 * Issue is fixed by updating output flag to AUDIO_OUTPUT_FLAG_FAST when there is
1755 * a non-music stream playback on WFD, so primary output is not reused for ringtone.
1756 */
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001757 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
1758 if ((availableOutputDeviceTypes & AUDIO_DEVICE_OUT_PROXY)
1759 && (stream != AUDIO_STREAM_MUSIC)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301760 ALOGD("WFD audio: use OUTPUT_FLAG_FAST for non music stream. flags:%x", flags );
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001761 //For voip paths
1762 if(flags & AUDIO_OUTPUT_FLAG_DIRECT)
1763 flags = AUDIO_OUTPUT_FLAG_DIRECT;
1764 else //route every thing else to ULL path
1765 flags = AUDIO_OUTPUT_FLAG_FAST;
1766 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301767#endif
1768
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001769 // open a direct output if required by specified parameters
vivek mehta0ea887a2015-08-26 14:01:20 -07001770 // force direct flag if offload flag is set: offloading implies a direct output stream
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001771 // and all common behaviors are driven by checking only the direct flag
1772 // this should normally be set appropriately in the policy configuration file
1773 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1774 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1775 }
1776 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1777 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1778 }
vivek mehta0ea887a2015-08-26 14:01:20 -07001779
vivek mehta4b0d8192015-10-16 00:25:59 -07001780 bool forced_deep = false;
Sharad Sangle36781612015-05-28 16:15:16 +05301781 // only allow deep buffering for music stream type
1782 if (stream != AUDIO_STREAM_MUSIC) {
1783 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Sharad Sangle497aef82015-08-03 17:55:48 +05301784 } else if (/* stream == AUDIO_STREAM_MUSIC && */
1785 flags == AUDIO_OUTPUT_FLAG_NONE &&
1786 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
vivek mehtac984b992015-11-25 13:28:55 -08001787 forced_deep = true;
Sharad Sangle36781612015-05-28 16:15:16 +05301788 }
Sharad Sangle497aef82015-08-03 17:55:48 +05301789
Sharad Sangle36781612015-05-28 16:15:16 +05301790 if (stream == AUDIO_STREAM_TTS) {
1791 flags = AUDIO_OUTPUT_FLAG_TTS;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001792 }
1793
Zhou Song2b3d1552016-03-21 17:21:04 +08001794 // check if direct output for track offload already exits
1795 bool is_track_offload_active = false;
1796 for (size_t i = 0; i < mOutputs.size(); i++) {
1797 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1798 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) {
1799 is_track_offload_active = true;
1800 ALOGD("Track offload already active");
1801 break;
1802 }
1803 }
1804
vivek mehta4b0d8192015-10-16 00:25:59 -07001805 // Do offload magic here
vivek mehtac984b992015-11-25 13:28:55 -08001806 if ((flags == AUDIO_OUTPUT_FLAG_NONE) &&
1807 (stream == AUDIO_STREAM_MUSIC) &&
Zhou Song2b3d1552016-03-21 17:21:04 +08001808 (offloadInfo != NULL) && !is_track_offload_active &&
vivek mehta4b0d8192015-10-16 00:25:59 -07001809 ((offloadInfo->usage == AUDIO_USAGE_MEDIA) || (offloadInfo->usage == AUDIO_USAGE_GAME))) {
vivek mehtac984b992015-11-25 13:28:55 -08001810 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DIRECT_PCM);
vivek mehta4b0d8192015-10-16 00:25:59 -07001811 ALOGD("AudioCustomHAL --> Force Direct Flag .. flag (0x%x)", flags);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301812 }
1813
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001814 sp<IOProfile> profile;
1815
1816 // skip direct output selection if the request can obviously be attached to a mixed output
1817 // and not explicitly requested
vivek mehtac984b992015-11-25 13:28:55 -08001818 if (((flags & (AUDIO_OUTPUT_FLAG_DIRECT|AUDIO_OUTPUT_FLAG_DIRECT_PCM)) == 0) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301819 audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX &&
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001820 audio_channel_count_from_out_mask(channelMask) <= 2) {
1821 goto non_direct_output;
1822 }
1823
1824 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1825 // creating an offloaded track and tearing it down immediately after start when audioflinger
1826 // detects there is an active non offloadable effect.
1827 // FIXME: We should check the audio session here but we do not have it in this context.
1828 // This may prevent offloading in rare situations where effects are left active by apps
1829 // in the background.
Weiyin Jiang6bba3202016-03-03 14:49:36 +08001830 //
1831 // Supplementary annotation:
1832 // For sake of track offload introduced, we need a rollback for both compress offload
1833 // and track offload use cases.
1834 if ((flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD|AUDIO_OUTPUT_FLAG_DIRECT_PCM)) &&
1835 (mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
1836 ALOGD("non offloadable effect is enabled, try with non direct output");
1837 goto non_direct_output;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001838 }
1839
Weiyin Jiang6bba3202016-03-03 14:49:36 +08001840 profile = getProfileForDirectOutput(device,
1841 samplingRate,
1842 format,
1843 channelMask,
1844 (audio_output_flags_t)flags);
1845
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001846 if (profile != 0) {
vivek mehtac984b992015-11-25 13:28:55 -08001847
1848 if (!(flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301849 (profile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT_PCM)) {
vivek mehtac984b992015-11-25 13:28:55 -08001850 ALOGI("got Direct_PCM without requesting ... reject ");
1851 profile = NULL;
1852 goto non_direct_output;
1853 }
1854
Sharad Sangle36781612015-05-28 16:15:16 +05301855 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001856
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001857 // if multiple concurrent offload decode is supported
1858 // do no check for reuse and also don't close previous output if its offload
1859 // previous output will be closed during track destruction
1860 if (!(property_get_bool("audio.offload.multiple.enabled", false) &&
1861 ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0))) {
1862 for (size_t i = 0; i < mOutputs.size(); i++) {
1863 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1864 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1865 outputDesc = desc;
1866 // reuse direct output if currently open and configured with same parameters
1867 if ((samplingRate == outputDesc->mSamplingRate) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301868 audio_formats_match(format, outputDesc->mFormat) &&
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001869 (channelMask == outputDesc->mChannelMask)) {
1870 outputDesc->mDirectOpenCount++;
1871 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1872 return mOutputs.keyAt(i);
1873 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001874 }
1875 }
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001876 // close direct output if currently open and configured with different parameters
1877 if (outputDesc != NULL) {
1878 closeOutput(outputDesc->mIoHandle);
1879 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001880 }
Sharad Sangle36781612015-05-28 16:15:16 +05301881
1882 // if the selected profile is offloaded and no offload info was specified,
1883 // create a default one
1884 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
Ashish Jain40cab3a2016-03-23 11:14:14 +05301885 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
Sharad Sangle36781612015-05-28 16:15:16 +05301886 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1887 defaultOffloadInfo.sample_rate = samplingRate;
1888 defaultOffloadInfo.channel_mask = channelMask;
1889 defaultOffloadInfo.format = format;
1890 defaultOffloadInfo.stream_type = stream;
1891 defaultOffloadInfo.bit_rate = 0;
1892 defaultOffloadInfo.duration_us = -1;
1893 defaultOffloadInfo.has_video = true; // conservative
1894 defaultOffloadInfo.is_streaming = true; // likely
1895 offloadInfo = &defaultOffloadInfo;
1896 }
1897
1898 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001899 outputDesc->mDevice = device;
1900 outputDesc->mLatency = 0;
Sharad Sangle36781612015-05-28 16:15:16 +05301901 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001902 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1903 config.sample_rate = samplingRate;
1904 config.channel_mask = channelMask;
1905 config.format = format;
1906 if (offloadInfo != NULL) {
1907 config.offload_info = *offloadInfo;
1908 }
Sharad Sangle36781612015-05-28 16:15:16 +05301909 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001910 &output,
1911 &config,
1912 &outputDesc->mDevice,
1913 String8(""),
1914 &outputDesc->mLatency,
1915 outputDesc->mFlags);
1916
1917 // only accept an output with the requested parameters
1918 if (status != NO_ERROR ||
1919 (samplingRate != 0 && samplingRate != config.sample_rate) ||
Ashish Jain40cab3a2016-03-23 11:14:14 +05301920 (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) ||
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001921 (channelMask != 0 && channelMask != config.channel_mask)) {
1922 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1923 "format %d %d, channelMask %04x %04x", output, samplingRate,
1924 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1925 outputDesc->mChannelMask);
1926 if (output != AUDIO_IO_HANDLE_NONE) {
1927 mpClientInterface->closeOutput(output);
1928 }
Sharad Sangle36781612015-05-28 16:15:16 +05301929 // fall back to mixer output if possible when the direct output could not be open
Ashish Jain40cab3a2016-03-23 11:14:14 +05301930 if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) {
Sharad Sangle36781612015-05-28 16:15:16 +05301931 goto non_direct_output;
1932 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001933 return AUDIO_IO_HANDLE_NONE;
1934 }
1935 outputDesc->mSamplingRate = config.sample_rate;
1936 outputDesc->mChannelMask = config.channel_mask;
1937 outputDesc->mFormat = config.format;
1938 outputDesc->mRefCount[stream] = 0;
1939 outputDesc->mStopTime[stream] = 0;
1940 outputDesc->mDirectOpenCount = 1;
1941
1942 audio_io_handle_t srcOutput = getOutputForEffect();
1943 addOutput(output, outputDesc);
1944 audio_io_handle_t dstOutput = getOutputForEffect();
1945 if (dstOutput == output) {
Gao Jiedb057832015-11-12 08:51:22 +08001946#ifdef DOLBY_ENABLE
1947 status_t status = mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1948 if (status == NO_ERROR) {
1949 for (size_t i = 0; i < mEffects.size(); i++) {
1950 sp<EffectDescriptor> desc = mEffects.valueAt(i);
1951 if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX) {
1952 // update the mIo member of EffectDescriptor for the global effect
1953 ALOGV("%s updating mIo", __FUNCTION__);
1954 desc->mIo = dstOutput;
1955 }
1956 }
1957 } else {
1958 ALOGW("%s moveEffects from %d to %d failed", __FUNCTION__, srcOutput, dstOutput);
1959 }
1960#else // DOLBY_END
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001961 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
Gao Jiedb057832015-11-12 08:51:22 +08001962#endif // LINE_ADDED_BY_DOLBY
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001963 }
1964 mPreviousOutputs = mOutputs;
1965 ALOGV("getOutput() returns new direct output %d", output);
1966 mpClientInterface->onAudioPortListUpdate();
1967 return output;
1968 }
1969
1970non_direct_output:
Ashish Jain5fe12412016-04-18 18:44:17 +05301971
1972 // A request for HW A/V sync cannot fallback to a mixed output because time
1973 // stamps are embedded in audio data
1974 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1975 return AUDIO_IO_HANDLE_NONE;
1976 }
1977
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001978 // ignoring channel mask due to downmix capability in mixer
1979
1980 // open a non direct output
1981
1982 // for non direct outputs, only PCM is supported
1983 if (audio_is_linear_pcm(format)) {
1984 // get which output is suitable for the specified stream. The actual
1985 // routing change will happen when startOutput() will be called
1986 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1987
1988 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1989 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
Ashish Jain660d3112016-06-20 10:16:51 +05301990
1991 if (forced_deep) {
1992 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
1993 ALOGI("setting force DEEP buffer now ");
1994 } else if(flags == AUDIO_OUTPUT_FLAG_NONE) {
1995 // no deep buffer playback is requested hence fallback to primary
1996 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_PRIMARY);
1997 ALOGI("FLAG None hence request for a primary output");
1998 }
1999
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07002000 output = selectOutput(outputs, flags, format);
2001 }
2002 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
2003 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
2004
vivek mehta0ea887a2015-08-26 14:01:20 -07002005 ALOGV("getOutputForDevice() returns output %d", output);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07002006
2007 return output;
2008}
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302009
2010status_t AudioPolicyManagerCustom::getInputForAttr(const audio_attributes_t *attr,
2011 audio_io_handle_t *input,
2012 audio_session_t session,
2013 uid_t uid,
2014 uint32_t samplingRate,
2015 audio_format_t format,
2016 audio_channel_mask_t channelMask,
2017 audio_input_flags_t flags,
2018 audio_port_handle_t selectedDeviceId,
2019 input_type_t *inputType)
2020{
Ashish Jain40cab3a2016-03-23 11:14:14 +05302021 audio_source_t inputSource;
2022 inputSource = attr->source;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302023#ifdef VOICE_CONCURRENCY
2024
2025 char propValue[PROPERTY_VALUE_MAX];
2026 bool prop_rec_enabled=false, prop_voip_enabled = false;
2027
2028 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
2029 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
2030 }
2031
2032 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
2033 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
2034 }
2035
2036 if (prop_rec_enabled && mvoice_call_state) {
2037 //check if voice call is active / running in background
2038 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
2039 //Need to block input request
2040 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
2041 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
2042 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
2043 {
2044 switch(inputSource) {
2045 case AUDIO_SOURCE_VOICE_UPLINK:
2046 case AUDIO_SOURCE_VOICE_DOWNLINK:
2047 case AUDIO_SOURCE_VOICE_CALL:
2048 ALOGD("voice_conc:Creating input during incall mode for inputSource: %d",
2049 inputSource);
2050 break;
2051
2052 case AUDIO_SOURCE_VOICE_COMMUNICATION:
2053 if(prop_voip_enabled) {
2054 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
2055 inputSource);
2056 return NO_INIT;
2057 }
2058 break;
2059 default:
2060 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
2061 inputSource);
2062 return NO_INIT;
2063 }
2064 }
2065 }//check for VoIP flag
2066 else if(prop_voip_enabled && mvoice_call_state) {
2067 //check if voice call is active / running in background
2068 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
2069 //Need to block input request
2070 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
2071 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
2072 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
2073 {
2074 if(inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION) {
2075 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
2076 return NO_INIT;
2077 }
2078 }
2079 }
2080
2081#endif
2082
2083 return AudioPolicyManager::getInputForAttr(attr,
2084 input,
2085 session,
2086 uid,
2087 samplingRate,
2088 format,
2089 channelMask,
2090 flags,
2091 selectedDeviceId,
2092 inputType);
2093}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08002094
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302095status_t AudioPolicyManagerCustom::startInput(audio_io_handle_t input,
2096 audio_session_t session)
2097{
2098 ALOGV("startInput() input %d", input);
2099 ssize_t index = mInputs.indexOfKey(input);
2100 if (index < 0) {
2101 ALOGW("startInput() unknown input %d", input);
2102 return BAD_VALUE;
2103 }
2104 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
2105
Ashish Jain40cab3a2016-03-23 11:14:14 +05302106 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
2107 if (audioSession == 0) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302108 ALOGW("startInput() unknown session %d on input %d", session, input);
2109 return BAD_VALUE;
2110 }
2111
2112 // virtual input devices are compatible with other input devices
2113 if (!is_virtual_input_device(inputDesc->mDevice)) {
2114
2115 // for a non-virtual input device, check if there is another (non-virtual) active input
2116 audio_io_handle_t activeInput = mInputs.getActiveInput();
2117 if (activeInput != 0 && activeInput != input) {
2118
2119 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
2120 // otherwise the active input continues and the new input cannot be started.
2121 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302122 if ((activeDesc->inputSource() == AUDIO_SOURCE_HOTWORD) &&
2123 !activeDesc->hasPreemptedSession(session)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302124 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302125 //FIXME: consider all active sessions
2126 AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions();
2127 audio_session_t activeSession = activeSessions.keyAt(0);
2128 SortedVector<audio_session_t> sessions =
2129 activeDesc->getPreemptedSessions();
2130 sessions.add(activeSession);
2131 inputDesc->setPreemptedSessions(sessions);
2132 stopInput(activeInput, activeSession);
2133 releaseInput(activeInput, activeSession);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302134 } else {
2135 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
2136 return INVALID_OPERATION;
2137 }
2138 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05302139 // Do not allow capture if an active voice call is using a software patch and
2140 // the call TX source device is on the same HW module.
2141 // FIXME: would be better to refine to only inputs whose profile connects to the
2142 // call TX device but this information is not in the audio patch
2143 if (mCallTxPatch != 0 &&
2144 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
2145 return INVALID_OPERATION;
2146 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302147 }
2148
2149 // Routing?
2150 mInputRoutes.incRouteActivity(session);
2151#ifdef RECORD_PLAY_CONCURRENCY
2152 mIsInputRequestOnProgress = true;
2153
2154 char getPropValue[PROPERTY_VALUE_MAX];
2155 bool prop_rec_play_enabled = false;
2156
2157 if (property_get("rec.playback.conc.disabled", getPropValue, NULL)) {
2158 prop_rec_play_enabled = atoi(getPropValue) || !strncmp("true", getPropValue, 4);
2159 }
2160
Lalit Kansara33918092016-12-06 22:42:26 +05302161 if ((prop_rec_play_enabled) &&(mInputs.activeInputsCountOnDevices() == 0)){
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302162 // send update to HAL on record playback concurrency
2163 AudioParameter param = AudioParameter();
2164 param.add(String8("rec_play_conc_on"), String8("true"));
2165 ALOGD("startInput() setParameters rec_play_conc is setting to ON ");
2166 mpClientInterface->setParameters(0, param.toString());
2167
2168 // Call invalidate to reset all opened non ULL audio tracks
2169 // Move tracks associated to this strategy from previous output to new output
2170 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
2171 // Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder)
Sharad Sangle4509cef2015-08-19 20:47:12 +05302172 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE && (i != AUDIO_STREAM_PATCH))) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302173 ALOGD("Invalidate on releaseInput for stream :: %d ", i);
2174 //FIXME see fixme on name change
2175 mpClientInterface->invalidateStream((audio_stream_type_t)i);
2176 }
2177 }
2178 // close compress tracks
2179 for (size_t i = 0; i < mOutputs.size(); i++) {
2180 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
2181 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
2182 ALOGD("ouput desc / profile is NULL");
2183 continue;
2184 }
2185 if (outputDesc->mProfile->mFlags
2186 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2187 // close compress sessions
2188 ALOGD("calling closeOutput on record conc for COMPRESS output");
2189 closeOutput(mOutputs.keyAt(i));
2190 }
2191 }
Ashish Jaine4a22d52016-06-22 11:55:08 +05302192 // If effects where present on any of the above closed outputs,
2193 // audioflinger moved them to the primary output by default
2194 // move them back to the appropriate output.
2195 moveGlobalEffect();
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302196 }
2197#endif
2198
Ashish Jain40cab3a2016-03-23 11:14:14 +05302199 if (!inputDesc->isActive() || mInputRoutes.hasRouteChanged(session)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302200 // if input maps to a dynamic policy with an activity listener, notify of state change
Ashish Jain630c5e12016-04-18 11:50:21 +05302201 if ((inputDesc->mPolicyMix != NULL)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302202 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Ashish Jain5fe12412016-04-18 18:44:17 +05302203 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302204 MIX_STATE_MIXING);
Ashish Jain630c5e12016-04-18 11:50:21 +05302205 }
Sharad Sanglec66b9ca2016-12-21 18:40:54 +05302206 /*audio policy: fix capture indication to sound trigger service*/
2207 // indicate active capture to sound trigger service if starting capture from a mic on
2208 // primary HW module
2209 audio_devices_t device = getNewInputDevice(input);
2210 audio_devices_t primaryInputDevices = availablePrimaryInputDevices();
2211 if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) &&
2212 mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302213 SoundTrigger::setCaptureState(true);
2214 }
Sharad Sanglec66b9ca2016-12-21 18:40:54 +05302215 setInputDevice(input, device, true /* force */);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302216
2217 // automatically enable the remote submix output when input is started if not
2218 // used by a policy mix of type MIX_TYPE_RECORDERS
2219 // For remote submix (a virtual device), we open only one input per capture request.
2220 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
2221 String8 address = String8("");
2222 if (inputDesc->mPolicyMix == NULL) {
2223 address = String8("0");
Ashish Jain630c5e12016-04-18 11:50:21 +05302224 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
Ashish Jain5fe12412016-04-18 18:44:17 +05302225 address = inputDesc->mPolicyMix->mDeviceAddress;
Ashish Jain630c5e12016-04-18 11:50:21 +05302226 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302227 if (address != "") {
2228 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2229 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2230 address, "remote-submix");
2231 }
2232 }
2233 }
2234
Ashish Jain40cab3a2016-03-23 11:14:14 +05302235 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302236
Ashish Jain40cab3a2016-03-23 11:14:14 +05302237 audioSession->changeActiveCount(1);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302238#ifdef RECORD_PLAY_CONCURRENCY
2239 mIsInputRequestOnProgress = false;
2240#endif
2241 return NO_ERROR;
2242}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08002243
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302244status_t AudioPolicyManagerCustom::stopInput(audio_io_handle_t input,
2245 audio_session_t session)
2246{
2247 status_t status;
2248 status = AudioPolicyManager::stopInput(input, session);
2249#ifdef RECORD_PLAY_CONCURRENCY
2250 char propValue[PROPERTY_VALUE_MAX];
2251 bool prop_rec_play_enabled = false;
2252
2253 if (property_get("rec.playback.conc.disabled", propValue, NULL)) {
2254 prop_rec_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
2255 }
2256
Lalit Kansara33918092016-12-06 22:42:26 +05302257 if ((prop_rec_play_enabled) && (mInputs.activeInputsCountOnDevices() == 0)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302258
2259 //send update to HAL on record playback concurrency
2260 AudioParameter param = AudioParameter();
2261 param.add(String8("rec_play_conc_on"), String8("false"));
2262 ALOGD("stopInput() setParameters rec_play_conc is setting to OFF ");
2263 mpClientInterface->setParameters(0, param.toString());
2264
2265 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
2266 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
2267 //Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder stop tone)
2268 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE) && (i != AUDIO_STREAM_PATCH)) {
2269 ALOGD(" Invalidate on stopInput for stream :: %d ", i);
2270 //FIXME see fixme on name change
2271 mpClientInterface->invalidateStream((audio_stream_type_t)i);
2272 }
2273 }
2274 }
2275#endif
2276 return status;
2277}
2278
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302279void AudioPolicyManagerCustom::closeAllInputs() {
2280 bool patchRemoved = false;
2281
Dhanalakshmi Siddanibfeca7f2015-10-08 15:17:11 +05302282 for(size_t input_index = mInputs.size(); input_index > 0; input_index--) {
2283 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index-1);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302284 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302285 if (patch_index >= 0) {
2286 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302287 status_t status;
2288 status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302289 mAudioPatches.removeItemsAt(patch_index);
2290 patchRemoved = true;
2291 }
Haynes Mathew George2e4655a2017-01-05 15:01:53 -08002292 mpClientInterface->closeInput(mInputs.keyAt(input_index-1));
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302293 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05302294 mInputs.clear();
2295 SoundTrigger::setCaptureState(false);
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302296 nextAudioPortGeneration();
2297
2298 if (patchRemoved) {
2299 mpClientInterface->onAudioPatchListUpdate();
2300 }
2301}
2302
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302303AudioPolicyManagerCustom::AudioPolicyManagerCustom(AudioPolicyClientInterface *clientInterface)
Sharad Sangle4509cef2015-08-19 20:47:12 +05302304 : AudioPolicyManager(clientInterface),
2305 mHdmiAudioDisabled(false),
2306 mHdmiAudioEvent(false),
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08002307 mPrevPhoneState(0),
Haynes Mathew George603ae9b2015-12-21 17:23:59 -08002308 mPrevFMVolumeDb(0.0f),
2309 mFMIsActive(false)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302310{
Ashish Jain40cab3a2016-03-23 11:14:14 +05302311
Satya Krishna Pindiproli8f83f102016-06-21 17:56:14 +05302312#ifdef USE_XML_AUDIO_POLICY_CONF
2313 ALOGD("USE_XML_AUDIO_POLICY_CONF is TRUE");
2314#else
2315 ALOGD("USE_XML_AUDIO_POLICY_CONF is FALSE");
2316#endif
2317
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302318#ifdef RECORD_PLAY_CONCURRENCY
2319 mIsInputRequestOnProgress = false;
2320#endif
2321
2322
2323#ifdef VOICE_CONCURRENCY
2324 mFallBackflag = getFallBackPath();
2325#endif
2326}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07002327}