blob: fb1461608c342adfc7342538cbf980fa28522d5e [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
Garmond Leungfcb00d92017-01-31 11:12:47 -080071#ifndef AUDIO_OUTPUT_FLAG_VOIP_RX
72#define AUDIO_OUTPUT_FLAG_VOIP_RX 0x800
73#endif
74
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070075namespace android {
Sharad Sanglec66b9ca2016-12-21 18:40:54 +053076/*audio policy: workaround for truncated touch sounds*/
77//FIXME: workaround for truncated touch sounds
78// to be removed when the problem is handled by system UI
79#define TOUCH_SOUND_FIXED_DELAY_MS 100
Sharad Sanglec5766ff2015-06-04 20:24:10 +053080#ifdef VOICE_CONCURRENCY
81audio_output_flags_t AudioPolicyManagerCustom::getFallBackPath()
82{
83 audio_output_flags_t flag = AUDIO_OUTPUT_FLAG_FAST;
84 char propValue[PROPERTY_VALUE_MAX];
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070085
Sharad Sanglec5766ff2015-06-04 20:24:10 +053086 if (property_get("voice.conc.fallbackpath", propValue, NULL)) {
87 if (!strncmp(propValue, "deep-buffer", 11)) {
88 flag = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
89 }
90 else if (!strncmp(propValue, "fast", 4)) {
91 flag = AUDIO_OUTPUT_FLAG_FAST;
92 }
93 else {
94 ALOGD("voice_conc:not a recognised path(%s) in prop voice.conc.fallbackpath",
95 propValue);
96 }
97 }
98 else {
99 ALOGD("voice_conc:prop voice.conc.fallbackpath not set");
100 }
101
102 ALOGD("voice_conc:picked up flag(0x%x) from prop voice.conc.fallbackpath",
103 flag);
104
105 return flag;
106}
107#endif /*VOICE_CONCURRENCY*/
Ashish Jaine4a22d52016-06-22 11:55:08 +0530108
109void AudioPolicyManagerCustom::moveGlobalEffect()
110{
111 audio_io_handle_t dstOutput = getOutputForEffect();
112 if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) {
113#ifdef DOLBY_ENABLE
114 status_t status =
115 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
116 mPrimaryOutput->mIoHandle,
117 dstOutput);
118 if (status == NO_ERROR) {
119 for (size_t i = 0; i < mEffects.size(); i++) {
120 sp<EffectDescriptor> desc = mEffects.valueAt(i);
121 if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX) {
122 // update the mIo member of EffectDescriptor
123 // for the global effect
124 ALOGV("%s updating mIo", __FUNCTION__);
125 desc->mIo = dstOutput;
126 }
127 }
128 } else {
129 ALOGW("%s moveEffects from %d to %d failed", __FUNCTION__,
130 mPrimaryOutput->mIoHandle, dstOutput);
131 }
132#else // DOLBY_END
133 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
134 mPrimaryOutput->mIoHandle, dstOutput);
135#endif
136 }
137}
138
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700139// ----------------------------------------------------------------------------
140// AudioPolicyInterface implementation
141// ----------------------------------------------------------------------------
Sharad Sangle36781612015-05-28 16:15:16 +0530142extern "C" AudioPolicyInterface* createAudioPolicyManager(
143 AudioPolicyClientInterface *clientInterface)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700144{
Sharad Sangle36781612015-05-28 16:15:16 +0530145 return new AudioPolicyManagerCustom(clientInterface);
146}
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700147
Sharad Sangle36781612015-05-28 16:15:16 +0530148extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface)
149{
150 delete interface;
151}
152
153status_t AudioPolicyManagerCustom::setDeviceConnectionStateInt(audio_devices_t device,
154 audio_policy_dev_state_t state,
155 const char *device_address,
156 const char *device_name)
157{
Ashish Jain40cab3a2016-03-23 11:14:14 +0530158 ALOGD("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
Sharad Sangle36781612015-05-28 16:15:16 +0530159 device, state, device_address, device_name);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700160
161 // connect/disconnect only 1 device at a time
162 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
163
Sharad Sangle36781612015-05-28 16:15:16 +0530164 sp<DeviceDescriptor> devDesc =
165 mHwModules.getDeviceDescriptor(device, device_address, device_name);
166
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700167 // handle output devices
168 if (audio_is_output_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700169 SortedVector <audio_io_handle_t> outputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700170
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700171 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700172
173 // save a copy of the opened output descriptors before any output is opened or closed
174 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
175 mPreviousOutputs = mOutputs;
176 switch (state)
177 {
178 // handle output device connection
Sharad Sangle36781612015-05-28 16:15:16 +0530179 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700180 if (index >= 0) {
Sharad Sangle4509cef2015-08-19 20:47:12 +0530181#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
182 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
183 if (!strncmp(device_address, "hdmi_spkr", 9)) {
184 mHdmiAudioDisabled = false;
185 } else {
186 mHdmiAudioEvent = true;
187 }
188 }
189#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700190 ALOGW("setDeviceConnectionState() device already connected: %x", device);
191 return INVALID_OPERATION;
192 }
193 ALOGV("setDeviceConnectionState() connecting device %x", device);
194
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700195 // register new device as available
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700196 index = mAvailableOutputDevices.add(devDesc);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530197#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
198 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
199 if (!strncmp(device_address, "hdmi_spkr", 9)) {
200 mHdmiAudioDisabled = false;
201 } else {
202 mHdmiAudioEvent = true;
203 }
204 if (mHdmiAudioDisabled || !mHdmiAudioEvent) {
205 mAvailableOutputDevices.remove(devDesc);
206 ALOGW("HDMI sink not connected, do not route audio to HDMI out");
207 return INVALID_OPERATION;
208 }
209 }
210#endif
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700211 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530212 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700213 if (module == 0) {
214 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
215 device);
216 mAvailableOutputDevices.remove(devDesc);
217 return INVALID_OPERATION;
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700218 }
Sharad Sangle36781612015-05-28 16:15:16 +0530219 mAvailableOutputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700220 } else {
221 return NO_MEMORY;
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700222 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700223
Garmond Leungfcb00d92017-01-31 11:12:47 -0800224 // Before checking outputs, broadcast connect event to allow HAL to retrieve dynamic
225 // parameters on newly connected devices (instead of opening the outputs...)
226 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
227
Sharad Sangle36781612015-05-28 16:15:16 +0530228 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700229 mAvailableOutputDevices.remove(devDesc);
Garmond Leungfcb00d92017-01-31 11:12:47 -0800230
231 broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
232 devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700233 return INVALID_OPERATION;
234 }
Sharad Sangle36781612015-05-28 16:15:16 +0530235 // Propagate device availability to Engine
236 mEngine->setDeviceConnectionState(devDesc, state);
237
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700238 // outputs should never be empty here
239 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
240 "checkOutputsForDevice() returned no outputs but status OK");
241 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
242 outputs.size());
Sharad Sangle36781612015-05-28 16:15:16 +0530243
Sharad Sangle36781612015-05-28 16:15:16 +0530244 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700245 // handle output device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700246 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
247 if (index < 0) {
Sharad Sangle4509cef2015-08-19 20:47:12 +0530248#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
249 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
250 if (!strncmp(device_address, "hdmi_spkr", 9)) {
251 mHdmiAudioDisabled = true;
252 } else {
253 mHdmiAudioEvent = false;
254 }
255 }
256#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700257 ALOGW("setDeviceConnectionState() device not connected: %x", device);
258 return INVALID_OPERATION;
259 }
260
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700261 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
262
Sharad Sangle36781612015-05-28 16:15:16 +0530263 // Send Disconnect to HALs
Garmond Leungfcb00d92017-01-31 11:12:47 -0800264 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700265
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700266 // remove device from available output devices
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700267 mAvailableOutputDevices.remove(devDesc);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530268#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
269 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
270 if (!strncmp(device_address, "hdmi_spkr", 9)) {
271 mHdmiAudioDisabled = true;
272 } else {
273 mHdmiAudioEvent = false;
274 }
275 }
276#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530277 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
278
279 // Propagate device availability to Engine
280 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700281 } break;
282
283 default:
284 ALOGE("setDeviceConnectionState() invalid state: %x", state);
285 return BAD_VALUE;
286 }
287
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700288 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
289 // output is suspended before any tracks are moved to it
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700290 checkA2dpSuspend();
291 checkOutputForAllStrategies();
292 // outputs must be closed after checkOutputForAllStrategies() is executed
293 if (!outputs.isEmpty()) {
294 for (size_t i = 0; i < outputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530295 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700296 // close unused outputs after device disconnection or direct outputs that have been
297 // opened by checkOutputsForDevice() to query dynamic parameters
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700298 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700299 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
300 (desc->mDirectOpenCount == 0))) {
301 closeOutput(outputs[i]);
302 }
303 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700304 // check again after closing A2DP output to reset mA2dpSuspended if needed
305 checkA2dpSuspend();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700306 }
307
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530308#ifdef FM_POWER_OPT
309 // handle FM device connection state to trigger FM AFE loopback
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800310 if (device == AUDIO_DEVICE_OUT_FM && hasPrimaryOutput()) {
Mingming Yin2dd77c22016-01-06 18:02:42 -0800311 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530312 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
313 mPrimaryOutput->changeRefCount(AUDIO_STREAM_MUSIC, 1);
Mingming Yin2dd77c22016-01-06 18:02:42 -0800314 newDevice = (audio_devices_t)(getNewOutputDevice(mPrimaryOutput, false)|AUDIO_DEVICE_OUT_FM);
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800315 mFMIsActive = true;
Zhou Songbd0264e2016-06-21 11:01:06 +0800316 mPrimaryOutput->mDevice = newDevice & ~AUDIO_DEVICE_OUT_FM;
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530317 } else {
Mingming Yin2dd77c22016-01-06 18:02:42 -0800318 newDevice = (audio_devices_t)(getNewOutputDevice(mPrimaryOutput, false));
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800319 mFMIsActive = false;
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530320 mPrimaryOutput->changeRefCount(AUDIO_STREAM_MUSIC, -1);
321 }
322 AudioParameter param = AudioParameter();
323 param.addInt(String8("handle_fm"), (int)newDevice);
324 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString());
325 }
326#endif /* FM_POWER_OPT end */
327
Zhou Songbd0264e2016-06-21 11:01:06 +0800328 updateDevicesAndOutputs();
329#ifdef DOLBY_ENABLE
330 // Before closing the opened outputs, update endpoint property with device capabilities
331 audio_devices_t audioOutputDevice = getDeviceForStrategy(getStrategy(AUDIO_STREAM_MUSIC), true);
332 mDolbyAudioPolicy.setEndpointSystemProperty(audioOutputDevice, mHwModules);
333#endif // DOLBY_END
334 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
335 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
336 updateCallRouting(newDevice);
337 }
338
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700339 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530340 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
341 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
342 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700343 // do not force device change on duplicated output because if device is 0, it will
344 // also force a device 0 for the two outputs it is duplicated to which may override
345 // a valid device selection on those outputs.
Sharad Sangle36781612015-05-28 16:15:16 +0530346 bool force = !desc->isDuplicated()
347 && (!device_distinguishes_on_address(device)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700348 // always force when disconnecting (a non-duplicated device)
349 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Sharad Sangle36781612015-05-28 16:15:16 +0530350 setOutputDevice(desc, newDevice, force, 0);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700351 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700352 }
353
Ashish Jain40cab3a2016-03-23 11:14:14 +0530354 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
355 cleanUpForDevice(devDesc);
356 }
357
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700358 mpClientInterface->onAudioPortListUpdate();
359 return NO_ERROR;
360 } // end if is output device
361
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700362 // handle input devices
363 if (audio_is_input_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700364 SortedVector <audio_io_handle_t> inputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700365
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700366 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700367 switch (state)
368 {
369 // handle input device connection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700370 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
371 if (index >= 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700372 ALOGW("setDeviceConnectionState() device already connected: %d", device);
373 return INVALID_OPERATION;
374 }
Sharad Sangle36781612015-05-28 16:15:16 +0530375 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700376 if (module == NULL) {
377 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
378 device);
379 return INVALID_OPERATION;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700380 }
Garmond Leungfcb00d92017-01-31 11:12:47 -0800381
382 // Before checking intputs, broadcast connect event to allow HAL to retrieve dynamic
383 // parameters on newly connected devices (instead of opening the inputs...)
384 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
385
Sharad Sangle36781612015-05-28 16:15:16 +0530386 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
Garmond Leungfcb00d92017-01-31 11:12:47 -0800387 broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
388 devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700389 return INVALID_OPERATION;
390 }
391
392 index = mAvailableInputDevices.add(devDesc);
393 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530394 mAvailableInputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700395 } else {
396 return NO_MEMORY;
397 }
Sharad Sangle36781612015-05-28 16:15:16 +0530398
Sharad Sangle36781612015-05-28 16:15:16 +0530399 // Propagate device availability to Engine
400 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700401 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700402
403 // handle input device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700404 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
405 if (index < 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700406 ALOGW("setDeviceConnectionState() device not connected: %d", device);
407 return INVALID_OPERATION;
408 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700409
410 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
411
412 // Set Disconnect to HALs
Garmond Leungfcb00d92017-01-31 11:12:47 -0800413 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700414
Sharad Sangle36781612015-05-28 16:15:16 +0530415 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700416 mAvailableInputDevices.remove(devDesc);
417
Sharad Sangle36781612015-05-28 16:15:16 +0530418 // Propagate device availability to Engine
419 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700420 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700421
422 default:
423 ALOGE("setDeviceConnectionState() invalid state: %x", state);
424 return BAD_VALUE;
425 }
426
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700427 closeAllInputs();
Sharad Sanglec66b9ca2016-12-21 18:40:54 +0530428 /*audio policy: fix call volume over USB*/
429 // As the input device list can impact the output device selection, update
430 // getDeviceForStrategy() cache
431 updateDevicesAndOutputs();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700432
Sharad Sangle36781612015-05-28 16:15:16 +0530433 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700434 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
435 updateCallRouting(newDevice);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700436 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700437
Ashish Jain40cab3a2016-03-23 11:14:14 +0530438 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
439 cleanUpForDevice(devDesc);
440 }
441
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700442 mpClientInterface->onAudioPortListUpdate();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700443 return NO_ERROR;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700444 } // end if is input device
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700445
446 ALOGW("setDeviceConnectionState() invalid device: %x", device);
447 return BAD_VALUE;
448}
Weiyin Jiangd211ef52016-02-16 15:55:11 +0800449
Preetam Singh Ranawat3db94ec2016-10-13 18:35:12 +0530450bool AudioPolicyManagerCustom::isInvalidationOfMusicStreamNeeded(routing_strategy strategy)
451{
452 if (strategy == STRATEGY_MEDIA) {
453 for (size_t i = 0; i < mOutputs.size(); i++) {
454 sp<SwAudioOutputDescriptor> newOutputDesc = mOutputs.valueAt(i);
455 if (newOutputDesc->mFormat == AUDIO_FORMAT_DSD)
456 return false;
457 }
458 }
459 return true;
460}
461
462void AudioPolicyManagerCustom::checkOutputForStrategy(routing_strategy strategy)
463{
464 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
465 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
466 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mOutputs);
467 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
468
469 // also take into account external policy-related changes: add all outputs which are
470 // associated with policies in the "before" and "after" output vectors
471 ALOGV("checkOutputForStrategy(): policy related outputs");
472 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
473 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
474 if (desc != 0 && desc->mPolicyMix != NULL) {
475 srcOutputs.add(desc->mIoHandle);
476 ALOGV(" previous outputs: adding %d", desc->mIoHandle);
477 }
478 }
479 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
480 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
481 if (desc != 0 && desc->mPolicyMix != NULL) {
482 dstOutputs.add(desc->mIoHandle);
483 ALOGV(" new outputs: adding %d", desc->mIoHandle);
484 }
485 }
486
487 if (!vectorsEqual(srcOutputs,dstOutputs) && isInvalidationOfMusicStreamNeeded(strategy)) {
488 AudioPolicyManager::checkOutputForStrategy(strategy);
489 }
490}
491
Sharad Sangle36781612015-05-28 16:15:16 +0530492// This function checks for the parameters which can be offloaded.
493// This can be enhanced depending on the capability of the DSP and policy
494// of the system.
495bool AudioPolicyManagerCustom::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700496{
Sharad Sangle36781612015-05-28 16:15:16 +0530497 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
498 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
499 offloadInfo.sample_rate, offloadInfo.channel_mask,
500 offloadInfo.format,
501 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
502 offloadInfo.has_video);
Ashish Jain40cab3a2016-03-23 11:14:14 +0530503
504 if (mMasterMono) {
505 return false; // no offloading if mono is set.
506 }
507
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530508#ifdef VOICE_CONCURRENCY
509 char concpropValue[PROPERTY_VALUE_MAX];
510 if (property_get("voice.playback.conc.disabled", concpropValue, NULL)) {
511 bool propenabled = atoi(concpropValue) || !strncmp("true", concpropValue, 4);
512 if (propenabled) {
513 if (isInCall())
514 {
515 ALOGD("\n copl: blocking compress offload on call mode\n");
516 return false;
517 }
518 }
519 }
520#endif
Preetam Singh Ranawat96afc162016-08-25 14:35:02 +0530521 if (property_get_bool("voice.dsd.playback.conc.disabled", true) &&
522 isInCall() && (offloadInfo.format == AUDIO_FORMAT_DSD)) {
523 ALOGD("blocking DSD compress offload on call mode");
524 return false;
525 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530526#ifdef RECORD_PLAY_CONCURRENCY
527 char recConcPropValue[PROPERTY_VALUE_MAX];
528 bool prop_rec_play_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700529
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530530 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
531 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
532 }
533
534 if ((prop_rec_play_enabled) &&
Lalit Kansara33918092016-12-06 22:42:26 +0530535 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCountOnDevices() > 0))) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530536 ALOGD("copl: blocking compress offload for record concurrency");
537 return false;
538 }
539#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530540 // Check if stream type is music, then only allow offload as of now.
541 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
542 {
543 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
544 return false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700545 }
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530546
Alexy Joseph0ac09032015-10-16 15:57:53 -0700547 // Check if offload has been disabled
548 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
549 if (offloadDisabled) {
550 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
551 return false;
552 }
553
Ashish Jainac510a72016-04-28 12:22:58 +0530554 //check if it's multi-channel AAC (includes sub formats) and FLAC format
555 if ((popcount(offloadInfo.channel_mask) > 2) &&
556 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
557 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS))) {
558 ALOGD("offload disabled for multi-channel AAC,FLAC and VORBIS format");
559 return false;
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530560 }
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530561
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530562#ifdef AUDIO_EXTN_FORMATS_ENABLED
Ashish Jainac510a72016-04-28 12:22:58 +0530563 //check if it's multi-channel FLAC/ALAC/WMA format with sample rate > 48k
564 if ((popcount(offloadInfo.channel_mask) > 2) &&
565 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
566 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) && (offloadInfo.sample_rate > 48000)) ||
567 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) && (offloadInfo.sample_rate > 48000)) ||
568 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) && (offloadInfo.sample_rate > 48000)) ||
569 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS))) {
570 ALOGD("offload disabled for multi-channel FLAC/ALAC/WMA/AAC_ADTS clips with sample rate > 48kHz");
571 return false;
572 }
yidonghae28e3a2016-08-19 11:31:15 +0800573
Weiyin Jiang8790b1f2017-05-09 18:16:43 +0800574 // check against wma std bit rate restriction
575 if ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) {
576 int32_t sr_id = -1;
577 uint32_t min_bitrate, max_bitrate;
578 for (int i = 0; i < WMA_STD_NUM_FREQ; i++) {
579 if (offloadInfo.sample_rate == wmaStdSampleRateTbl[i]) {
580 sr_id = i;
581 break;
582 }
583 }
584 if ((sr_id < 0) || (popcount(offloadInfo.channel_mask) > 2)
585 || (popcount(offloadInfo.channel_mask) <= 0)) {
586 ALOGE("invalid sample rate or channel count");
587 return false;
588 }
589
590 min_bitrate = wmaStdMinAvgByteRateTbl[sr_id][popcount(offloadInfo.channel_mask) - 1];
591 max_bitrate = wmaStdMaxAvgByteRateTbl[sr_id][popcount(offloadInfo.channel_mask) - 1];
592 if ((offloadInfo.bit_rate > max_bitrate) || (offloadInfo.bit_rate < min_bitrate)) {
593 ALOGD("offload disabled for WMA clips with unsupported bit rate");
594 ALOGD("bit_rate %d, max_bitrate %d, min_bitrate %d", offloadInfo.bit_rate, max_bitrate, min_bitrate);
595 return false;
596 }
597 }
598
599 // 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.
600 if ((((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) && (offloadInfo.bit_rate > MAX_BITRATE_WMA_PRO)) ||
601 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) && (offloadInfo.bit_rate > MAX_BITRATE_WMA_LOSSLESS))) {
602 ALOGD("offload disabled for WMA_PRO/WMA_LOSSLESS clips with bit rate over maximum supported value");
yidonghae28e3a2016-08-19 11:31:15 +0800603 return false;
604 }
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530605#endif
Ashish Jainac510a72016-04-28 12:22:58 +0530606 //TODO: enable audio offloading with video when ready
607 const bool allowOffloadWithVideo =
608 property_get_bool("audio.offload.video", false /* default_value */);
609 if (offloadInfo.has_video && !allowOffloadWithVideo) {
610 ALOGV("isOffloadSupported: has_video == true, returning false");
611 return false;
612 }
Manish Dewanganf3cd0f82015-10-13 14:04:36 +0530613
Ashish Jainac510a72016-04-28 12:22:58 +0530614 const bool allowOffloadStreamingWithVideo = property_get_bool("av.streaming.offload.enable",
615 false /*default value*/);
616 if (offloadInfo.has_video && offloadInfo.is_streaming && !allowOffloadStreamingWithVideo) {
617 ALOGW("offload disabled by av.streaming.offload.enable %d",allowOffloadStreamingWithVideo);
618 return false;
Sharad Sangle36781612015-05-28 16:15:16 +0530619 }
620
621 //If duration is less than minimum value defined in property, return false
Ashish Jainac510a72016-04-28 12:22:58 +0530622 char propValue[PROPERTY_VALUE_MAX];
Sharad Sangle36781612015-05-28 16:15:16 +0530623 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
624 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
625 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
626 return false;
627 }
628 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
629 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
630 //duration checks only valid for MP3/AAC/ formats,
631 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
632 if ((offloadInfo.format == AUDIO_FORMAT_MP3) ||
633 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
634 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
Ashish Jainac510a72016-04-28 12:22:58 +0530635 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS))
636 return false;
637
638#ifdef AUDIO_EXTN_FORMATS_ENABLED
639 if (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) ||
Sharad Sangle36781612015-05-28 16:15:16 +0530640 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) ||
641 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) ||
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530642 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_APE) ||
Preetam Singh Ranawat191eb772016-09-09 17:10:19 +0530643 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_DSD) ||
Ashish Jainac510a72016-04-28 12:22:58 +0530644 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS))
Sharad Sangle36781612015-05-28 16:15:16 +0530645 return false;
Ashish Jainac510a72016-04-28 12:22:58 +0530646#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530647 }
648
649 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
650 // creating an offloaded track and tearing it down immediately after start when audioflinger
651 // detects there is an active non offloadable effect.
652 // FIXME: We should check the audio session here but we do not have it in this context.
653 // This may prevent offloading in rare situations where effects are left active by apps
654 // in the background.
655 if (mEffects.isNonOffloadableEffectEnabled()) {
656 return false;
657 }
Dhananjay Kumarcf558ba2016-07-29 19:33:13 +0530658
Sharad Sangle36781612015-05-28 16:15:16 +0530659 // See if there is a profile to support this.
660 // AUDIO_DEVICE_NONE
661 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
662 offloadInfo.sample_rate,
663 offloadInfo.format,
664 offloadInfo.channel_mask,
665 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
666 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
667 return (profile != 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700668}
Weiyin Jiangd211ef52016-02-16 15:55:11 +0800669
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700670void AudioPolicyManagerCustom::setPhoneState(audio_mode_t state)
671{
Ashish Jain40cab3a2016-03-23 11:14:14 +0530672 ALOGD("setPhoneState() state %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530673 // store previous phone state for management of sonification strategy below
Sharad Sangle4509cef2015-08-19 20:47:12 +0530674 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Sharad Sangle36781612015-05-28 16:15:16 +0530675 int oldState = mEngine->getPhoneState();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700676
Sharad Sangle36781612015-05-28 16:15:16 +0530677 if (mEngine->setPhoneState(state) != NO_ERROR) {
678 ALOGW("setPhoneState() invalid or same state %d", state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700679 return;
680 }
Sharad Sangle36781612015-05-28 16:15:16 +0530681 /// Opens: can these line be executed after the switch of volume curves???
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700682 // if leaving call state, handle special case of active streams
683 // pertaining to sonification strategy see handleIncallSonification()
Zhou Song58081b62016-02-24 13:10:55 +0800684 if (isStateInCall(oldState)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700685 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530686 for (size_t j = 0; j < mOutputs.size(); j++) {
687 audio_io_handle_t curOutput = mOutputs.keyAt(j);
Garmond Leungfcb00d92017-01-31 11:12:47 -0800688 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Sharad Sangle4509cef2015-08-19 20:47:12 +0530689 handleIncallSonification((audio_stream_type_t)stream, false, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +0530690 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700691 }
Sharad Sangle36781612015-05-28 16:15:16 +0530692
Zhou Song58081b62016-02-24 13:10:55 +0800693 // force reevaluating accessibility routing when call stops
Sharad Sangle36781612015-05-28 16:15:16 +0530694 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700695 }
696
Sharad Sangle36781612015-05-28 16:15:16 +0530697 /**
698 * Switching to or from incall state or switching between telephony and VoIP lead to force
699 * routing command.
700 */
701 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
702 || (is_state_in_call(state) && (state != oldState)));
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700703
704 // check for device and output changes triggered by new phone state
705 checkA2dpSuspend();
706 checkOutputForAllStrategies();
707 updateDevicesAndOutputs();
708
Sharad Sangle36781612015-05-28 16:15:16 +0530709 sp<SwAudioOutputDescriptor> hwOutputDesc = mPrimaryOutput;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530710#ifdef VOICE_CONCURRENCY
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530711 char propValue[PROPERTY_VALUE_MAX];
712 bool prop_playback_enabled = false, prop_rec_enabled=false, prop_voip_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700713
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530714 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
715 prop_playback_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
716 }
717
718 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
719 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
720 }
721
722 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
723 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
724 }
725
Dhananjay Kumar9125b2a2015-12-01 16:24:09 +0530726 if ((AUDIO_MODE_IN_CALL != oldState) && (AUDIO_MODE_IN_CALL == state)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530727 ALOGD("voice_conc:Entering to call mode oldState :: %d state::%d ",
728 oldState, state);
Dhananjay Kumar9125b2a2015-12-01 16:24:09 +0530729 mvoice_call_state = state;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530730 if (prop_rec_enabled) {
731 //Close all active inputs
Garmond Leungfcb00d92017-01-31 11:12:47 -0800732 Vector<sp <AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
733 if (activeInputs.size() != 0) {
734 for (size_t i = 0; i < activeInputs.size(); i++) {
735 sp<AudioInputDescriptor> activeInput = activeInputs[i];
736 switch(activeInput->inputSource()) {
737 case AUDIO_SOURCE_VOICE_UPLINK:
738 case AUDIO_SOURCE_VOICE_DOWNLINK:
739 case AUDIO_SOURCE_VOICE_CALL:
740 ALOGD("voice_conc:FOUND active input during call active: %d",activeInput->inputSource());
741 break;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530742
Garmond Leungfcb00d92017-01-31 11:12:47 -0800743 case AUDIO_SOURCE_VOICE_COMMUNICATION:
744 if(prop_voip_enabled) {
745 ALOGD("voice_conc:CLOSING VoIP input source on call setup :%d ",activeInput->inputSource());
746 stopInput(activeInput->mIoHandle, activeDesc->mSessions.itemAt(0));
747 releaseInput(activeInput->mIoHandle, activeDesc->mSessions.itemAt(0));
748 }
749 break;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530750
Garmond Leungfcb00d92017-01-31 11:12:47 -0800751 default:
752 ALOGD("voice_conc:CLOSING input on call setup for inputSource: %d",activeInput->inputSource());
753 stopInput(activeInput->mIoHandle, activeDesc->mSessions.itemAt(0));
754 releaseInput(activeInput->mIoHandle, activeDesc->mSessions.itemAt(0));
755 break;
756 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530757 }
758 }
759 } else if (prop_voip_enabled) {
Garmond Leungfcb00d92017-01-31 11:12:47 -0800760 Vector<sp <AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
761 if (activeInputs.size() != 0) {
762 for (size_t i = 0; i < activeInputs.size(); i++) {
763 sp<AudioInputDescriptor> activeInput = activeInputs[i];
764 if (AUDIO_SOURCE_VOICE_COMMUNICATION == activeInput->inputSource()) {
765 ALOGD("voice_conc:CLOSING VoIP on call setup : %d",activeInput->inputSource());
766 stopInput(activeInput->mIoHandle, activeInput->mSessions.itemAt(0));
767 releaseInput(activeInput->mIoHandle, activeInput->mSessions.itemAt(0));
768 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530769 }
770 }
771 }
772 if (prop_playback_enabled) {
773 // Move tracks associated to this strategy from previous output to new output
Garmond Leungfcb00d92017-01-31 11:12:47 -0800774 for (int i = AUDIO_STREAM_SYSTEM; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530775 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530776 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
777 if ((AUDIO_STREAM_MUSIC == i) ||
778 (AUDIO_STREAM_VOICE_CALL == i) ) {
779 ALOGD("voice_conc:Invalidate stream type %d", i);
780 mpClientInterface->invalidateStream((audio_stream_type_t)i);
781 }
782 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
783 ALOGD("voice_conc:Invalidate stream type %d", i);
784 mpClientInterface->invalidateStream((audio_stream_type_t)i);
785 }
786 }
787 }
788
789 for (size_t i = 0; i < mOutputs.size(); i++) {
790 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
791 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
792 ALOGD("voice_conc:ouput desc / profile is NULL");
793 continue;
794 }
795
Sidipotu Ashok738f19e2016-12-07 15:16:01 +0530796 bool isFastFallBackNeeded =
797 ((AUDIO_OUTPUT_FLAG_DEEP_BUFFER | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT_PCM) & outputDesc->mProfile->mFlags);
798
799 if ((AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) && isFastFallBackNeeded) {
800 if (((!outputDesc->isDuplicated() && outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY))
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530801 && prop_playback_enabled) {
802 ALOGD("voice_conc:calling suspendOutput on call mode for primary output");
803 mpClientInterface->suspendOutput(mOutputs.keyAt(i));
804 } //Close compress all sessions
805 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
806 && prop_playback_enabled) {
807 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
808 closeOutput(mOutputs.keyAt(i));
809 }
810 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_VOIP_RX)
811 && prop_voip_enabled) {
812 ALOGD("voice_conc:calling closeOutput on call mode for DIRECT output");
813 closeOutput(mOutputs.keyAt(i));
814 }
815 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
kunleiz0b55ec72016-11-10 16:23:58 +0800816 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_VOIP_RX) {
817 if (prop_voip_enabled) {
818 ALOGD("voice_conc:calling closeOutput on call mode for DIRECT output");
819 closeOutput(mOutputs.keyAt(i));
820 }
821 }
822 else if (prop_playback_enabled
823 && (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530824 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
825 closeOutput(mOutputs.keyAt(i));
826 }
827 }
828 }
Ashish Jaine4a22d52016-06-22 11:55:08 +0530829 // If effects where present on any of the above closed outputs,
830 // audioflinger moved them to the primary output by default
831 // move them back to the appropriate output.
832 moveGlobalEffect();
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530833 }
834
835 if ((AUDIO_MODE_IN_CALL == oldState || AUDIO_MODE_IN_COMMUNICATION == oldState) &&
836 (AUDIO_MODE_NORMAL == state) && prop_playback_enabled && mvoice_call_state) {
837 ALOGD("voice_conc:EXITING from call mode oldState :: %d state::%d \n",oldState, state);
838 mvoice_call_state = 0;
839 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
840 //restore PCM (deep-buffer) output after call termination
841 for (size_t i = 0; i < mOutputs.size(); i++) {
842 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
843 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
844 ALOGD("voice_conc:ouput desc / profile is NULL");
845 continue;
846 }
847 if (!outputDesc->isDuplicated() && outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
848 ALOGD("voice_conc:calling restoreOutput after call mode for primary output");
849 mpClientInterface->restoreOutput(mOutputs.keyAt(i));
850 }
851 }
852 }
853 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
Garmond Leungfcb00d92017-01-31 11:12:47 -0800854 for (int i = AUDIO_STREAM_SYSTEM; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530855 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530856 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
857 if ((AUDIO_STREAM_MUSIC == i) ||
858 (AUDIO_STREAM_VOICE_CALL == i) ) {
859 mpClientInterface->invalidateStream((audio_stream_type_t)i);
860 }
861 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
862 mpClientInterface->invalidateStream((audio_stream_type_t)i);
863 }
864 }
865 }
866
867#endif
Preetam Singh Ranawat96afc162016-08-25 14:35:02 +0530868
869 sp<SwAudioOutputDescriptor> outputDesc = NULL;
870 for (size_t i = 0; i < mOutputs.size(); i++) {
871 outputDesc = mOutputs.valueAt(i);
872 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
873 ALOGD("voice_conc:ouput desc / profile is NULL");
874 continue;
875 }
876
877 if (property_get_bool("voice.dsd.playback.conc.disabled", true) &&
878 (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
879 (outputDesc->mFormat == AUDIO_FORMAT_DSD)) {
880 ALOGD("voice_conc:calling closeOutput on call mode for DSD COMPRESS output");
881 closeOutput(mOutputs.keyAt(i));
882 // call invalidate for music, so that DSD compress will fallback to deep-buffer.
883 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
884 }
885
886 }
887
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530888#ifdef RECORD_PLAY_CONCURRENCY
889 char recConcPropValue[PROPERTY_VALUE_MAX];
890 bool prop_rec_play_enabled = false;
891
892 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
893 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
894 }
895 if (prop_rec_play_enabled) {
896 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
897 ALOGD("phone state changed to MODE_IN_COMM invlaidating music and voice streams");
898 // call invalidate for voice streams, so that it can use deepbuffer with VoIP out device from HAL
899 mpClientInterface->invalidateStream(AUDIO_STREAM_VOICE_CALL);
900 // call invalidate for music, so that compress will fallback to deep-buffer with VoIP out device
901 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
902
903 // close compress output to make sure session will be closed before timeout(60sec)
904 for (size_t i = 0; i < mOutputs.size(); i++) {
905
906 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
907 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
908 ALOGD("ouput desc / profile is NULL");
909 continue;
910 }
911
912 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
913 ALOGD("calling closeOutput on call mode for COMPRESS output");
914 closeOutput(mOutputs.keyAt(i));
915 }
916 }
Ashish Jaine4a22d52016-06-22 11:55:08 +0530917 // If effects where present on any of the above closed outputs,
918 // audioflinger moved them to the primary output by default
919 // move them back to the appropriate output.
920 moveGlobalEffect();
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530921 } else if ((oldState == AUDIO_MODE_IN_COMMUNICATION) &&
922 (mEngine->getPhoneState() == AUDIO_MODE_NORMAL)) {
923 // call invalidate for music so that music can fallback to compress
924 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
925 }
926 }
927#endif
928 mPrevPhoneState = oldState;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700929 int delayMs = 0;
930 if (isStateInCall(state)) {
931 nsecs_t sysTime = systemTime();
932 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530933 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700934 // mute media and sonification strategies and delay device switch by the largest
935 // latency of any output where either strategy is active.
936 // This avoid sending the ring tone or music tail into the earpiece or headset.
Sharad Sangle36781612015-05-28 16:15:16 +0530937 if ((isStrategyActive(desc, STRATEGY_MEDIA,
938 SONIFICATION_HEADSET_MUSIC_DELAY,
939 sysTime) ||
940 isStrategyActive(desc, STRATEGY_SONIFICATION,
941 SONIFICATION_HEADSET_MUSIC_DELAY,
942 sysTime)) &&
943 (delayMs < (int)desc->latency()*2)) {
944 delayMs = desc->latency()*2;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700945 }
Sharad Sangle36781612015-05-28 16:15:16 +0530946 setStrategyMute(STRATEGY_MEDIA, true, desc);
947 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700948 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Sharad Sangle36781612015-05-28 16:15:16 +0530949 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
950 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700951 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
952 }
953 }
954
Sharad Sangle36781612015-05-28 16:15:16 +0530955 if (hasPrimaryOutput()) {
956 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
957 // the device returned is not necessarily reachable via this output
958 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
959 // force routing command to audio hardware when ending call
960 // even if no device change is needed
961 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
962 rxDevice = mPrimaryOutput->device();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700963 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700964
Sharad Sangle36781612015-05-28 16:15:16 +0530965 if (state == AUDIO_MODE_IN_CALL) {
966 updateCallRouting(rxDevice, delayMs);
967 } else if (oldState == AUDIO_MODE_IN_CALL) {
968 if (mCallRxPatch != 0) {
969 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
970 mCallRxPatch.clear();
971 }
972 if (mCallTxPatch != 0) {
973 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
974 mCallTxPatch.clear();
975 }
976 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
977 } else {
978 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700979 }
980 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530981 //update device for all non-primary outputs
982 for (size_t i = 0; i < mOutputs.size(); i++) {
983 audio_io_handle_t output = mOutputs.keyAt(i);
984 if (output != mPrimaryOutput->mIoHandle) {
985 newDevice = getNewOutputDevice(mOutputs.valueFor(output), false /*fromCache*/);
986 setOutputDevice(mOutputs.valueFor(output), newDevice, (newDevice != AUDIO_DEVICE_NONE));
987 }
988 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700989 // if entering in call state, handle special case of active streams
990 // pertaining to sonification strategy see handleIncallSonification()
991 if (isStateInCall(state)) {
992 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530993 for (size_t j = 0; j < mOutputs.size(); j++) {
994 audio_io_handle_t curOutput = mOutputs.keyAt(j);
Garmond Leungfcb00d92017-01-31 11:12:47 -0800995 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Sharad Sangle4509cef2015-08-19 20:47:12 +0530996 handleIncallSonification((audio_stream_type_t)stream, true, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +0530997 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700998 }
Zhou Song58081b62016-02-24 13:10:55 +0800999
1000 // force reevaluating accessibility routing when call starts
1001 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001002 }
1003
1004 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
1005 if (state == AUDIO_MODE_RINGTONE &&
1006 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
1007 mLimitRingtoneVolume = true;
1008 } else {
1009 mLimitRingtoneVolume = false;
1010 }
1011}
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301012
1013void AudioPolicyManagerCustom::setForceUse(audio_policy_force_use_t usage,
1014 audio_policy_forced_cfg_t config)
1015{
Ashish Jain40cab3a2016-03-23 11:14:14 +05301016 ALOGD("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301017
1018 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
1019 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
1020 return;
1021 }
1022 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
1023 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
1024 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
1025
1026 // check for device and output changes triggered by new force usage
1027 checkA2dpSuspend();
1028 checkOutputForAllStrategies();
1029 updateDevicesAndOutputs();
Ashish Jain40cab3a2016-03-23 11:14:14 +05301030
Sharad Sanglec66b9ca2016-12-21 18:40:54 +05301031 /*audio policy: workaround for truncated touch sounds*/
1032 //FIXME: workaround for truncated touch sounds
1033 // to be removed when the problem is handled by system UI
1034 uint32_t delayMs = 0;
1035 uint32_t waitMs = 0;
1036 if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) {
1037 delayMs = TOUCH_SOUND_FIXED_DELAY_MS;
1038 }
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301039 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
1040 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
Sharad Sanglec66b9ca2016-12-21 18:40:54 +05301041 waitMs = updateCallRouting(newDevice, delayMs);
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301042 }
1043 // Use reverse loop to make sure any low latency usecases (generally tones)
1044 // are not routed before non LL usecases (generally music).
1045 // We can safely assume that LL output would always have lower index,
1046 // and use this work-around to avoid routing of output with music stream
1047 // from the context of short lived LL output.
1048 // Note: in case output's share backend(HAL sharing is implicit) all outputs
1049 // gets routing update while processing first output itself.
1050 for (size_t i = mOutputs.size(); i > 0; i--) {
1051 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i-1);
1052 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
Sharad Sanglec66b9ca2016-12-21 18:40:54 +05301053 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
1054 waitMs = setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE),
1055 delayMs);
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301056 }
1057 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
Sharad Sanglec66b9ca2016-12-21 18:40:54 +05301058 applyStreamVolumes(outputDesc, newDevice, waitMs, true);
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301059 }
1060 }
1061
Garmond Leungfcb00d92017-01-31 11:12:47 -08001062 Vector<sp <AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
1063 for (size_t i = 0; i < activeInputs.size(); i++) {
1064 sp<AudioInputDescriptor> activeDesc = activeInputs[i];
1065 audio_devices_t newDevice = getNewInputDevice(activeDesc);
Ashish Jain40cab3a2016-03-23 11:14:14 +05301066 // Force new input selection if the new device can not be reached via current input
Garmond Leungfcb00d92017-01-31 11:12:47 -08001067 if (activeDesc->mProfile->getSupportedDevices().types() &
1068 (newDevice & ~AUDIO_DEVICE_BIT_IN)) {
1069 setInputDevice(activeDesc->mIoHandle, newDevice);
Ashish Jain40cab3a2016-03-23 11:14:14 +05301070 } else {
Garmond Leungfcb00d92017-01-31 11:12:47 -08001071 closeInput(activeDesc->mIoHandle);
Ashish Jain40cab3a2016-03-23 11:14:14 +05301072 }
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301073 }
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301074}
1075
Garmond Leungfcb00d92017-01-31 11:12:47 -08001076status_t AudioPolicyManagerCustom::stopSource(const sp<AudioOutputDescriptor>& outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301077 audio_stream_type_t stream,
1078 bool forceDeviceUpdate)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001079{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301080 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1081 ALOGW("stopSource() invalid stream %d", stream);
1082 return INVALID_OPERATION;
1083 }
Sharad Sangle36781612015-05-28 16:15:16 +05301084 // always handle stream stop, check which stream type is stopping
1085 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001086
Sharad Sangle36781612015-05-28 16:15:16 +05301087 // handle special case for sonification while in call
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001088 if (isInCall()) {
Sharad Sangle36781612015-05-28 16:15:16 +05301089 if (outputDesc->isDuplicated()) {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301090 handleIncallSonification(stream, false, false, outputDesc->subOutput1()->mIoHandle);
1091 handleIncallSonification(stream, false, false, outputDesc->subOutput2()->mIoHandle);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001092 }
Sharad Sangle36781612015-05-28 16:15:16 +05301093 handleIncallSonification(stream, false, false, outputDesc->mIoHandle);
1094 }
1095
1096 if (outputDesc->mRefCount[stream] > 0) {
1097 // decrement usage count of this stream on the output
1098 outputDesc->changeRefCount(stream, -1);
1099
1100 // store time at which the stream was stopped - see isStreamActive()
1101 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
1102 outputDesc->mStopTime[stream] = systemTime();
Zhou Song5dcddc92015-09-21 14:36:57 +08001103 audio_devices_t prevDevice = outputDesc->device();
Sharad Sangle36781612015-05-28 16:15:16 +05301104 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
1105 // delay the device switch by twice the latency because stopOutput() is executed when
1106 // the track stop() command is received and at that time the audio track buffer can
1107 // still contain data that needs to be drained. The latency only covers the audio HAL
1108 // and kernel buffers. Also the latency does not always include additional delay in the
1109 // audio path (audio DSP, CODEC ...)
1110 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
1111
1112 // force restoring the device selection on other active outputs if it differs from the
1113 // one being selected for this output
1114 for (size_t i = 0; i < mOutputs.size(); i++) {
1115 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1116 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1117 if (desc != outputDesc &&
1118 desc->isActive() &&
1119 outputDesc->sharesHwModuleWith(desc) &&
1120 (newDevice != desc->device())) {
Sharad Sangle4509cef2015-08-19 20:47:12 +05301121 audio_devices_t dev = getNewOutputDevice(mOutputs.valueFor(curOutput), false /*fromCache*/);
Naresh Tanniru11393702016-09-14 11:25:37 +05301122 bool force = desc->device() != dev;
Zhou Song5dcddc92015-09-21 14:36:57 +08001123 uint32_t delayMs;
1124 if (dev == prevDevice) {
1125 delayMs = 0;
1126 } else {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301127 delayMs = outputDesc->latency()*2;
Zhou Song5dcddc92015-09-21 14:36:57 +08001128 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301129 setOutputDevice(desc,
1130 dev,
Naresh Tanniru11393702016-09-14 11:25:37 +05301131 force,
Zhou Song5dcddc92015-09-21 14:36:57 +08001132 delayMs);
Sharad Sanglec66b9ca2016-12-21 18:40:54 +05301133 /*audio policy: fix media volume after ringtone*/
1134 // re-apply device specific volume if not done by setOutputDevice()
1135 if (!force) {
1136 applyStreamVolumes(desc, dev, delayMs);
1137 }
Sharad Sangle36781612015-05-28 16:15:16 +05301138 }
1139 }
1140 // update the outputs if stopping one with a stream that can affect notification routing
1141 handleNotificationRoutingForStream(stream);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001142 }
Sharad Sangle36781612015-05-28 16:15:16 +05301143 return NO_ERROR;
1144 } else {
1145 ALOGW("stopOutput() refcount is already 0");
1146 return INVALID_OPERATION;
1147 }
1148}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001149
Garmond Leungfcb00d92017-01-31 11:12:47 -08001150status_t AudioPolicyManagerCustom::startSource(const sp<AudioOutputDescriptor>& outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301151 audio_stream_type_t stream,
1152 audio_devices_t device,
Lalit Kansaraf803ac52016-05-09 19:12:01 +05301153 const char *address,
Sharad Sangle36781612015-05-28 16:15:16 +05301154 uint32_t *delayMs)
1155{
1156 // cannot start playback of STREAM_TTS if any other output is being used
1157 uint32_t beaconMuteLatency = 0;
1158
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301159 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1160 ALOGW("startSource() invalid stream %d", stream);
1161 return INVALID_OPERATION;
1162 }
1163
Sharad Sangle36781612015-05-28 16:15:16 +05301164 *delayMs = 0;
1165 if (stream == AUDIO_STREAM_TTS) {
1166 ALOGV("\t found BEACON stream");
Ashish Jain40cab3a2016-03-23 11:14:14 +05301167 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301168 return INVALID_OPERATION;
1169 } else {
1170 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001171 }
Sharad Sangle36781612015-05-28 16:15:16 +05301172 } else {
1173 // some playback other than beacon starts
1174 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1175 }
1176
Zhou Song63f0b852016-12-20 14:35:06 +08001177 // force device change if the output is inactive and no audio patch is already present.
Ashish Jain40cab3a2016-03-23 11:14:14 +05301178 // check active before incrementing usage count
Zhou Song63f0b852016-12-20 14:35:06 +08001179 bool force = !outputDesc->isActive() &&
1180 (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE);
Ashish Jain40cab3a2016-03-23 11:14:14 +05301181
Sharad Sangle36781612015-05-28 16:15:16 +05301182 // increment usage count for this stream on the requested output:
1183 // NOTE that the usage count is the same for duplicated output and hardware output which is
1184 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1185 outputDesc->changeRefCount(stream, 1);
1186
1187 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
1188 // starting an output being rerouted?
1189 if (device == AUDIO_DEVICE_NONE) {
1190 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001191 }
Sharad Sangle36781612015-05-28 16:15:16 +05301192 routing_strategy strategy = getStrategy(stream);
1193 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
1194 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1195 (beaconMuteLatency > 0);
1196 uint32_t waitMs = beaconMuteLatency;
Sharad Sangle36781612015-05-28 16:15:16 +05301197 for (size_t i = 0; i < mOutputs.size(); i++) {
1198 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1199 if (desc != outputDesc) {
Zhou Song63f0b852016-12-20 14:35:06 +08001200 // force a device change if any other output is:
1201 // - managed by the same hw module
1202 // - has a current device selection that differs from selected device.
1203 // - supports currently selected device
1204 // - has an active audio patch
Sharad Sangle36781612015-05-28 16:15:16 +05301205 // In this case, the audio HAL must receive the new device selection so that it can
1206 // change the device currently selected by the other active output.
1207 if (outputDesc->sharesHwModuleWith(desc) &&
Zhou Song63f0b852016-12-20 14:35:06 +08001208 desc->device() != device &&
1209 desc->supportedDevices() & device &&
1210 desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) {
Sharad Sangle36781612015-05-28 16:15:16 +05301211 force = true;
1212 }
1213 // wait for audio on other active outputs to be presented when starting
1214 // a notification so that audio focus effect can propagate, or that a mute/unmute
1215 // event occurred for beacon
1216 uint32_t latency = desc->latency();
1217 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1218 waitMs = latency;
1219 }
1220 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001221 }
Garmond Leungfcb00d92017-01-31 11:12:47 -08001222 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force, 0, NULL, address);
Sharad Sangle36781612015-05-28 16:15:16 +05301223
1224 // handle special case for sonification while in call
1225 if (isInCall()) {
1226 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001227 }
Sharad Sangle36781612015-05-28 16:15:16 +05301228
1229 // apply volume rules for current stream and device if necessary
1230 checkAndSetVolume(stream,
Ashish Jain40cab3a2016-03-23 11:14:14 +05301231 mVolumeCurves->getVolumeIndex(stream, device),
Sharad Sangle36781612015-05-28 16:15:16 +05301232 outputDesc,
1233 device);
1234
1235 // update the outputs if starting an output with a stream that can affect notification
1236 // routing
1237 handleNotificationRoutingForStream(stream);
1238
1239 // force reevaluating accessibility routing when ringtone or alarm starts
1240 if (strategy == STRATEGY_SONIFICATION) {
1241 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1242 }
Sharad Sanglec66b9ca2016-12-21 18:40:54 +05301243 if (waitMs > muteWaitMs) {
1244 *delayMs = waitMs - muteWaitMs;
1245 }
1246
Garmond Leungfcb00d92017-01-31 11:12:47 -08001247 } else {
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001248 // handle special case for sonification while in call
1249 if (isInCall()) {
1250 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Sharad Sangle36781612015-05-28 16:15:16 +05301251 }
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001252 }
Sharad Sangle36781612015-05-28 16:15:16 +05301253 return NO_ERROR;
1254}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001255
Sharad Sangle36781612015-05-28 16:15:16 +05301256void AudioPolicyManagerCustom::handleIncallSonification(audio_stream_type_t stream,
1257 bool starting, bool stateChange,
1258 audio_io_handle_t output)
1259{
1260 if(!hasPrimaryOutput()) {
1261 return;
1262 }
1263 // no action needed for AUDIO_STREAM_PATCH stream type, it's for internal flinger tracks
1264 if (stream == AUDIO_STREAM_PATCH) {
1265 return;
1266 }
1267 // if the stream pertains to sonification strategy and we are in call we must
1268 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
1269 // in the device used for phone strategy and play the tone if the selected device does not
1270 // interfere with the device used for phone strategy
1271 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
1272 // many times as there are active tracks on the output
1273 const routing_strategy stream_strategy = getStrategy(stream);
1274 if ((stream_strategy == STRATEGY_SONIFICATION) ||
1275 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
1276 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
1277 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
1278 stream, starting, outputDesc->mDevice, stateChange);
1279 if (outputDesc->mRefCount[stream]) {
1280 int muteCount = 1;
1281 if (stateChange) {
1282 muteCount = outputDesc->mRefCount[stream];
1283 }
1284 if (audio_is_low_visibility(stream)) {
1285 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
1286 for (int i = 0; i < muteCount; i++) {
1287 setStreamMute(stream, starting, outputDesc);
1288 }
1289 } else {
1290 ALOGV("handleIncallSonification() high visibility");
1291 if (outputDesc->device() &
1292 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
1293 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
1294 for (int i = 0; i < muteCount; i++) {
1295 setStreamMute(stream, starting, outputDesc);
1296 }
1297 }
1298 if (starting) {
1299 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
1300 AUDIO_STREAM_VOICE_CALL);
1301 } else {
1302 mpClientInterface->stopTone();
1303 }
1304 }
1305 }
1306 }
1307}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001308
Sharad Sangle36781612015-05-28 16:15:16 +05301309void AudioPolicyManagerCustom::handleNotificationRoutingForStream(audio_stream_type_t stream) {
1310 switch(stream) {
1311 case AUDIO_STREAM_MUSIC:
1312 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
1313 updateDevicesAndOutputs();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001314 break;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001315 default:
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001316 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001317 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001318}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001319
Sharad Sangle36781612015-05-28 16:15:16 +05301320status_t AudioPolicyManagerCustom::checkAndSetVolume(audio_stream_type_t stream,
1321 int index,
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301322 const sp<AudioOutputDescriptor>& outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301323 audio_devices_t device,
Garmond Leungfcb00d92017-01-31 11:12:47 -08001324 int delayMs,
1325 bool force)
Sharad Sangle36781612015-05-28 16:15:16 +05301326{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301327 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1328 ALOGW("checkAndSetVolume() invalid stream %d", stream);
1329 return INVALID_OPERATION;
1330 }
Sharad Sangle36781612015-05-28 16:15:16 +05301331 // do not change actual stream volume if the stream is muted
1332 if (outputDesc->mMuteCount[stream] != 0) {
1333 ALOGVV("checkAndSetVolume() stream %d muted count %d",
1334 stream, outputDesc->mMuteCount[stream]);
1335 return NO_ERROR;
1336 }
1337 audio_policy_forced_cfg_t forceUseForComm =
1338 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
1339 // do not change in call volume if bluetooth is connected and vice versa
1340 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
1341 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
1342 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
1343 stream, forceUseForComm);
1344 return INVALID_OPERATION;
1345 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001346
Sharad Sangle36781612015-05-28 16:15:16 +05301347 if (device == AUDIO_DEVICE_NONE) {
1348 device = outputDesc->device();
1349 }
1350
1351 float volumeDb = computeVolume(stream, index, device);
1352 if (outputDesc->isFixedVolume(device)) {
1353 volumeDb = 0.0f;
1354 }
1355
1356 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
1357
1358 if (stream == AUDIO_STREAM_VOICE_CALL ||
1359 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
1360 float voiceVolume;
1361 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
1362 if (stream == AUDIO_STREAM_VOICE_CALL) {
Ashish Jain40cab3a2016-03-23 11:14:14 +05301363 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Sharad Sangle36781612015-05-28 16:15:16 +05301364 } else {
1365 voiceVolume = 1.0;
1366 }
1367
Garmond Leungfcb00d92017-01-31 11:12:47 -08001368 if (voiceVolume != mLastVoiceVolume) {
Sharad Sangle36781612015-05-28 16:15:16 +05301369 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
1370 mLastVoiceVolume = voiceVolume;
1371 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301372#ifdef FM_POWER_OPT
1373 } else if (stream == AUDIO_STREAM_MUSIC && hasPrimaryOutput() &&
Haynes Mathew George603ae9b2015-12-21 17:23:59 -08001374 outputDesc == mPrimaryOutput && mFMIsActive) {
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001375 /* Avoid unnecessary set_parameter calls as it puts the primary
1376 outputs FastMixer in HOT_IDLE leading to breaks in audio */
1377 if (volumeDb != mPrevFMVolumeDb) {
1378 mPrevFMVolumeDb = volumeDb;
1379 AudioParameter param = AudioParameter();
1380 param.addFloat(String8("fm_volume"), Volume::DbToAmpl(volumeDb));
Mingming Yin1433cc62016-01-04 14:58:00 -08001381 //Double delayMs to avoid sound burst while device switch.
1382 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString(), delayMs*2);
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001383 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301384#endif /* FM_POWER_OPT end */
Sharad Sangle36781612015-05-28 16:15:16 +05301385 }
1386
1387 return NO_ERROR;
1388}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001389
Sharad Sangle36781612015-05-28 16:15:16 +05301390bool AudioPolicyManagerCustom::isDirectOutput(audio_io_handle_t output) {
1391 for (size_t i = 0; i < mOutputs.size(); i++) {
1392 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1393 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1394 if ((curOutput == output) && (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
1395 return true;
1396 }
1397 }
1398 return false;
1399}
vivek mehta0ea887a2015-08-26 14:01:20 -07001400
Dhananjay Kumarac341582017-02-23 23:42:25 +05301401bool static tryForDirectPCM(audio_output_flags_t flags)
Ashish Jainac510a72016-04-28 12:22:58 +05301402{
Ashish Jain771d02f2016-10-27 22:35:46 +05301403 bool trackDirectPCM = false; // Output request for track created by other apps
Ashish Jainac510a72016-04-28 12:22:58 +05301404
Dhananjay Kumarac341582017-02-23 23:42:25 +05301405 if (flags == AUDIO_OUTPUT_FLAG_NONE) {
Ashish Jain771d02f2016-10-27 22:35:46 +05301406 trackDirectPCM = property_get_bool("audio.offload.track.enable", true);
1407 }
Dhananjay Kumarac341582017-02-23 23:42:25 +05301408 return trackDirectPCM;
Ashish Jainac510a72016-04-28 12:22:58 +05301409}
1410
vivek mehta0ea887a2015-08-26 14:01:20 -07001411status_t AudioPolicyManagerCustom::getOutputForAttr(const audio_attributes_t *attr,
Garmond Leungfcb00d92017-01-31 11:12:47 -08001412 audio_io_handle_t *output,
1413 audio_session_t session,
1414 audio_stream_type_t *stream,
1415 uid_t uid,
1416 const audio_config_t *config,
1417 audio_output_flags_t flags,
1418 audio_port_handle_t selectedDeviceId,
1419 audio_port_handle_t *portId)
vivek mehta0ea887a2015-08-26 14:01:20 -07001420{
1421 audio_offload_info_t tOffloadInfo = AUDIO_INFO_INITIALIZER;
Garmond Leungfcb00d92017-01-31 11:12:47 -08001422 audio_config_t tConfig;
vivek mehta0ea887a2015-08-26 14:01:20 -07001423
Garmond Leungfcb00d92017-01-31 11:12:47 -08001424 uint32_t bitWidth = (audio_bytes_per_sample(config->format) * 8);
Alexy Joseph0ac09032015-10-16 15:57:53 -07001425
Garmond Leungfcb00d92017-01-31 11:12:47 -08001426 memcpy(&tConfig, config, sizeof(audio_config_t));
Dhananjay Kumarac341582017-02-23 23:42:25 +05301427 if ((flags == AUDIO_OUTPUT_FLAG_DIRECT || tryForDirectPCM(flags)) &&
Garmond Leungfcb00d92017-01-31 11:12:47 -08001428 (!memcmp(&config->offload_info, &tOffloadInfo, sizeof(audio_offload_info_t)))) {
1429 tConfig.offload_info.sample_rate = config->sample_rate;
1430 tConfig.offload_info.channel_mask = config->channel_mask;
1431 tConfig.offload_info.format = config->format;
1432 tConfig.offload_info.stream_type = *stream;
1433 tConfig.offload_info.bit_width = bitWidth;
vivek mehta0ea887a2015-08-26 14:01:20 -07001434 if (attr != NULL) {
1435 ALOGV("found attribute .. setting usage %d ", attr->usage);
Garmond Leungfcb00d92017-01-31 11:12:47 -08001436 tConfig.offload_info.usage = attr->usage;
vivek mehta0ea887a2015-08-26 14:01:20 -07001437 } else {
Alexy Joseph0ac09032015-10-16 15:57:53 -07001438 ALOGI("%s:: attribute is NULL .. no usage set", __func__);
vivek mehta0ea887a2015-08-26 14:01:20 -07001439 }
vivek mehta0ea887a2015-08-26 14:01:20 -07001440 }
1441
1442 return AudioPolicyManager::getOutputForAttr(attr, output, session, stream,
Garmond Leungfcb00d92017-01-31 11:12:47 -08001443 (uid_t)uid, &tConfig,
vivek mehta0ea887a2015-08-26 14:01:20 -07001444 flags, (audio_port_handle_t)selectedDeviceId,
Garmond Leungfcb00d92017-01-31 11:12:47 -08001445 portId);
vivek mehta0ea887a2015-08-26 14:01:20 -07001446}
1447
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001448audio_io_handle_t AudioPolicyManagerCustom::getOutputForDevice(
1449 audio_devices_t device,
Sharad Sangle36781612015-05-28 16:15:16 +05301450 audio_session_t session __unused,
Garmond Leungfcb00d92017-01-31 11:12:47 -08001451 uid_t clientUid,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001452 audio_stream_type_t stream,
1453 uint32_t samplingRate,
1454 audio_format_t format,
1455 audio_channel_mask_t channelMask,
1456 audio_output_flags_t flags,
1457 const audio_offload_info_t *offloadInfo)
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001458{
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001459 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001460 status_t status;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001461
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001462#ifdef AUDIO_POLICY_TEST
1463 if (mCurOutput != 0) {
1464 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1465 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1466
1467 if (mTestOutputs[mCurOutput] == 0) {
1468 ALOGV("getOutput() opening test output");
Sharad Sangle36781612015-05-28 16:15:16 +05301469 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
1470 mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001471 outputDesc->mDevice = mTestDevice;
1472 outputDesc->mLatency = mTestLatencyMs;
1473 outputDesc->mFlags =
1474 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
1475 outputDesc->mRefCount[stream] = 0;
1476 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1477 config.sample_rate = mTestSamplingRate;
1478 config.channel_mask = mTestChannels;
1479 config.format = mTestFormat;
1480 if (offloadInfo != NULL) {
1481 config.offload_info = *offloadInfo;
1482 }
1483 status = mpClientInterface->openOutput(0,
1484 &mTestOutputs[mCurOutput],
1485 &config,
1486 &outputDesc->mDevice,
1487 String8(""),
1488 &outputDesc->mLatency,
1489 outputDesc->mFlags);
1490 if (status == NO_ERROR) {
1491 outputDesc->mSamplingRate = config.sample_rate;
1492 outputDesc->mFormat = config.format;
1493 outputDesc->mChannelMask = config.channel_mask;
1494 AudioParameter outputCmd = AudioParameter();
1495 outputCmd.addInt(String8("set_id"),mCurOutput);
1496 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1497 addOutput(mTestOutputs[mCurOutput], outputDesc);
1498 }
1499 }
1500 return mTestOutputs[mCurOutput];
1501 }
1502#endif //AUDIO_POLICY_TEST
Sharad Sangle36781612015-05-28 16:15:16 +05301503 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1504 (stream != AUDIO_STREAM_MUSIC)) {
1505 // compress should not be used for non-music streams
1506 ALOGE("Offloading only allowed with music stream");
1507 return 0;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301508 }
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301509
1510 if ((stream == AUDIO_STREAM_VOICE_CALL) &&
1511 (channelMask == 1) &&
Shiv Maliyappanahallid77d3b32016-02-02 13:45:50 -08001512 (samplingRate == 8000 || samplingRate == 16000 ||
1513 samplingRate == 32000 || samplingRate == 48000)) {
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301514 // Allow Voip direct output only if:
1515 // audio mode is MODE_IN_COMMUNCATION; AND
1516 // voip output is not opened already; AND
1517 // requested sample rate matches with that of voip input stream (if opened already)
1518 int value = 0;
1519 uint32_t mode = 0, voipOutCount = 1, voipSampleRate = 1;
Alexy Joseph9323d8b2017-02-17 10:30:07 -08001520 bool is_vr_mode_on = false;
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301521 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1522 String8("audio_mode"));
1523 AudioParameter result = AudioParameter(valueStr);
1524 if (result.getInt(String8("audio_mode"), value) == NO_ERROR) {
1525 mode = value;
1526 }
1527
1528 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1529 String8("voip_out_stream_count"));
1530 result = AudioParameter(valueStr);
1531 if (result.getInt(String8("voip_out_stream_count"), value) == NO_ERROR) {
1532 voipOutCount = value;
1533 }
1534
1535 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1536 String8("voip_sample_rate"));
1537 result = AudioParameter(valueStr);
1538 if (result.getInt(String8("voip_sample_rate"), value) == NO_ERROR) {
1539 voipSampleRate = value;
1540 }
1541
1542 if ((mode == AUDIO_MODE_IN_COMMUNICATION) && (voipOutCount == 0) &&
1543 ((voipSampleRate == 0) || (voipSampleRate == samplingRate))) {
1544 if (audio_is_linear_pcm(format)) {
1545 char propValue[PROPERTY_VALUE_MAX] = {0};
1546 property_get("use.voice.path.for.pcm.voip", propValue, "0");
1547 bool voipPcmSysPropEnabled = !strncmp("true", propValue, sizeof("true"));
1548 if (voipPcmSysPropEnabled && (format == AUDIO_FORMAT_PCM_16_BIT)) {
Karthik Reddy Katta3fb9be72015-11-25 11:18:27 +05301549 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX |
1550 AUDIO_OUTPUT_FLAG_DIRECT);
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301551 ALOGD("Set VoIP and Direct output flags for PCM format");
1552 }
1553 }
1554 }
Alexy Joseph9323d8b2017-02-17 10:30:07 -08001555 //IF VOIP is going to be started at the same time as when
1556 //vr is enabled, get VOIP to fallback to low latency
1557 String8 vr_value;
1558 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1559 String8("vr_audio_mode_on"));
1560 result = AudioParameter(valueStr);
1561 if (result.get(String8("vr_audio_mode_on"), vr_value) == NO_ERROR) {
1562 is_vr_mode_on = vr_value.contains("true");
1563 ALOGI("VR mode is %d, switch to primary output if request is for fast|raw",
1564 is_vr_mode_on);
1565 }
1566
1567 if (is_vr_mode_on) {
1568 //check the flags being requested for, and clear FAST|RAW
1569 flags = (audio_output_flags_t)(flags &
1570 (~(AUDIO_OUTPUT_FLAG_FAST|AUDIO_OUTPUT_FLAG_RAW)));
1571
1572 }
1573
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301574 }
1575
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301576#ifdef VOICE_CONCURRENCY
1577 char propValue[PROPERTY_VALUE_MAX];
1578 bool prop_play_enabled=false, prop_voip_enabled = false;
1579
1580 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1581 prop_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001582 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301583
1584 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1585 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1586 }
1587
Sidipotu Ashok738f19e2016-12-07 15:16:01 +05301588 bool isDeepBufferFallBackNeeded =
1589 ((AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT_PCM) & flags);
1590 bool isFastFallBackNeeded =
1591 ((AUDIO_OUTPUT_FLAG_DEEP_BUFFER | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT_PCM) & flags);
1592
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301593 if (prop_play_enabled && mvoice_call_state) {
1594 //check if voice call is active / running in background
1595 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1596 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1597 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1598 {
1599 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1600 if(prop_voip_enabled) {
1601 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1602 flags );
1603 return 0;
1604 }
1605 }
1606 else {
Sidipotu Ashok738f19e2016-12-07 15:16:01 +05301607 if (isFastFallBackNeeded &&
1608 (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301609 ALOGD("voice_conc:IN call mode adding ULL flags .. flags: %x ", flags );
1610 flags = AUDIO_OUTPUT_FLAG_FAST;
Sidipotu Ashok738f19e2016-12-07 15:16:01 +05301611 } else if (isDeepBufferFallBackNeeded &&
1612 (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301613 if (AUDIO_STREAM_MUSIC == stream) {
1614 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1615 ALOGD("voice_conc:IN call mode adding deep-buffer flags %x ", flags );
1616 }
1617 else {
1618 flags = AUDIO_OUTPUT_FLAG_FAST;
1619 ALOGD("voice_conc:IN call mode adding fast flags %x ", flags );
1620 }
1621 }
1622 }
1623 }
1624 } else if (prop_voip_enabled && mvoice_call_state) {
1625 //check if voice call is active / running in background
1626 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1627 //return only ULL ouput
1628 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1629 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1630 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1631 {
1632 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1633 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1634 flags );
1635 return 0;
1636 }
1637 }
1638 }
1639#endif
1640#ifdef RECORD_PLAY_CONCURRENCY
1641 char recConcPropValue[PROPERTY_VALUE_MAX];
1642 bool prop_rec_play_enabled = false;
1643
1644 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
1645 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
1646 }
1647 if ((prop_rec_play_enabled) &&
Lalit Kansara33918092016-12-06 22:42:26 +05301648 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCountOnDevices() > 0))) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301649 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
1650 if (AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1651 // allow VoIP using voice path
1652 // Do nothing
1653 } else if((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1654 ALOGD("voice_conc:MODE_IN_COMM is setforcing deep buffer output for non ULL... flags: %x", flags);
1655 // use deep buffer path for all non ULL outputs
1656 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1657 }
1658 } else if ((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1659 ALOGD("voice_conc:Record mode is on forcing deep buffer output for non ULL... flags: %x ", flags);
1660 // use deep buffer path for all non ULL outputs
1661 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1662 }
1663 }
1664 if (prop_rec_play_enabled &&
1665 (stream == AUDIO_STREAM_ENFORCED_AUDIBLE)) {
1666 ALOGD("Record conc is on forcing ULL output for ENFORCED_AUDIBLE");
1667 flags = AUDIO_OUTPUT_FLAG_FAST;
1668 }
1669#endif
1670
Sharad Sangle4509cef2015-08-19 20:47:12 +05301671#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Sharad Sangle36781612015-05-28 16:15:16 +05301672 /*
1673 * WFD audio routes back to target speaker when starting a ringtone playback.
1674 * This is because primary output is reused for ringtone, so output device is
1675 * updated based on SONIFICATION strategy for both ringtone and music playback.
1676 * The same issue is not seen on remoted_submix HAL based WFD audio because
1677 * primary output is not reused and a new output is created for ringtone playback.
1678 * Issue is fixed by updating output flag to AUDIO_OUTPUT_FLAG_FAST when there is
1679 * a non-music stream playback on WFD, so primary output is not reused for ringtone.
1680 */
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001681 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
1682 if ((availableOutputDeviceTypes & AUDIO_DEVICE_OUT_PROXY)
1683 && (stream != AUDIO_STREAM_MUSIC)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301684 ALOGD("WFD audio: use OUTPUT_FLAG_FAST for non music stream. flags:%x", flags );
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001685 //For voip paths
1686 if(flags & AUDIO_OUTPUT_FLAG_DIRECT)
1687 flags = AUDIO_OUTPUT_FLAG_DIRECT;
1688 else //route every thing else to ULL path
1689 flags = AUDIO_OUTPUT_FLAG_FAST;
1690 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301691#endif
1692
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001693 // open a direct output if required by specified parameters
vivek mehta0ea887a2015-08-26 14:01:20 -07001694 // force direct flag if offload flag is set: offloading implies a direct output stream
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001695 // and all common behaviors are driven by checking only the direct flag
1696 // this should normally be set appropriately in the policy configuration file
1697 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1698 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1699 }
1700 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1701 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1702 }
vivek mehta0ea887a2015-08-26 14:01:20 -07001703
Dhananjay Kumarac341582017-02-23 23:42:25 +05301704 // Do internal direct magic here
1705 bool offload_disabled = property_get_bool("audio.offload.disable", false);
1706 if ((flags == AUDIO_OUTPUT_FLAG_NONE) &&
1707 (stream == AUDIO_STREAM_MUSIC) &&
1708 (offloadInfo != NULL) && !offload_disabled &&
1709 ((offloadInfo->usage == AUDIO_USAGE_MEDIA) || (offloadInfo->usage == AUDIO_USAGE_GAME))) {
1710 audio_output_flags_t old_flags = flags;
1711 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DIRECT);
1712 ALOGD("AudioCustomHAL --> Force Direct Flag .. old flags(0x%x)", old_flags);
1713 } else if (flags == AUDIO_OUTPUT_FLAG_DIRECT && offload_disabled) {
1714 ALOGD("AudioCustomHAL --> offloading is disabled: Force Remove Direct Flag");
1715 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_NONE);
1716 }
1717
vivek mehta4b0d8192015-10-16 00:25:59 -07001718 bool forced_deep = false;
Sharad Sangle36781612015-05-28 16:15:16 +05301719 // only allow deep buffering for music stream type
1720 if (stream != AUDIO_STREAM_MUSIC) {
1721 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Sharad Sangle497aef82015-08-03 17:55:48 +05301722 } else if (/* stream == AUDIO_STREAM_MUSIC && */
1723 flags == AUDIO_OUTPUT_FLAG_NONE &&
1724 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
vivek mehtac984b992015-11-25 13:28:55 -08001725 forced_deep = true;
Sharad Sangle36781612015-05-28 16:15:16 +05301726 }
Sharad Sangle497aef82015-08-03 17:55:48 +05301727
Sharad Sangle36781612015-05-28 16:15:16 +05301728 if (stream == AUDIO_STREAM_TTS) {
1729 flags = AUDIO_OUTPUT_FLAG_TTS;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001730 }
1731
1732 sp<IOProfile> profile;
1733
1734 // skip direct output selection if the request can obviously be attached to a mixed output
1735 // and not explicitly requested
Dhananjay Kumarac341582017-02-23 23:42:25 +05301736 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301737 audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX &&
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001738 audio_channel_count_from_out_mask(channelMask) <= 2) {
1739 goto non_direct_output;
1740 }
1741
Garmond Leungfcb00d92017-01-31 11:12:47 -08001742 // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled.
1743 // This prevents creating an offloaded track and tearing it down immediately after start
1744 // when audioflinger detects there is an active non offloadable effect.
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001745 // FIXME: We should check the audio session here but we do not have it in this context.
1746 // This may prevent offloading in rare situations where effects are left active by apps
1747 // in the background.
Weiyin Jiang6bba3202016-03-03 14:49:36 +08001748 //
1749 // Supplementary annotation:
1750 // For sake of track offload introduced, we need a rollback for both compress offload
1751 // and track offload use cases.
Dhananjay Kumarac341582017-02-23 23:42:25 +05301752 if ((flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD|AUDIO_OUTPUT_FLAG_DIRECT)) &&
Weiyin Jiang6bba3202016-03-03 14:49:36 +08001753 (mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
1754 ALOGD("non offloadable effect is enabled, try with non direct output");
1755 goto non_direct_output;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001756 }
1757
Weiyin Jiang6bba3202016-03-03 14:49:36 +08001758 profile = getProfileForDirectOutput(device,
1759 samplingRate,
1760 format,
1761 channelMask,
1762 (audio_output_flags_t)flags);
1763
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001764 if (profile != 0) {
vivek mehtac984b992015-11-25 13:28:55 -08001765
Dhananjay Kumarac341582017-02-23 23:42:25 +05301766 if (!(flags & AUDIO_OUTPUT_FLAG_DIRECT) &&
1767 (profile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT)) {
1768 ALOGI("got Direct without requesting ... reject ");
vivek mehtac984b992015-11-25 13:28:55 -08001769 profile = NULL;
1770 goto non_direct_output;
1771 }
1772
Sharad Sangle36781612015-05-28 16:15:16 +05301773 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001774
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001775 // if multiple concurrent offload decode is supported
1776 // do no check for reuse and also don't close previous output if its offload
1777 // previous output will be closed during track destruction
1778 if (!(property_get_bool("audio.offload.multiple.enabled", false) &&
Dhananjay Kumarac341582017-02-23 23:42:25 +05301779 ((flags & AUDIO_OUTPUT_FLAG_DIRECT) != 0))) {
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001780 for (size_t i = 0; i < mOutputs.size(); i++) {
1781 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1782 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1783 outputDesc = desc;
Garmond Leungfcb00d92017-01-31 11:12:47 -08001784 // reuse direct output if currently open by the same client
1785 // and configured with same parameters
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001786 if ((samplingRate == outputDesc->mSamplingRate) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301787 audio_formats_match(format, outputDesc->mFormat) &&
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001788 (channelMask == outputDesc->mChannelMask)) {
Garmond Leungfcb00d92017-01-31 11:12:47 -08001789 if (clientUid == outputDesc->mDirectClientUid) {
1790 outputDesc->mDirectOpenCount++;
1791 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1792 return mOutputs.keyAt(i);
1793 } else {
1794 ALOGV("getOutput() do not reuse direct output because current client (%ld) "
1795 "is not the same as requesting client (%ld)",
1796 (long)outputDesc->mDirectClientUid, (long)clientUid);
1797 goto non_direct_output;
1798 }
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001799 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001800 }
1801 }
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001802 // close direct output if currently open and configured with different parameters
1803 if (outputDesc != NULL) {
1804 closeOutput(outputDesc->mIoHandle);
1805 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001806 }
Sharad Sangle36781612015-05-28 16:15:16 +05301807
1808 // if the selected profile is offloaded and no offload info was specified,
1809 // create a default one
1810 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
Ashish Jain40cab3a2016-03-23 11:14:14 +05301811 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
Sharad Sangle36781612015-05-28 16:15:16 +05301812 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1813 defaultOffloadInfo.sample_rate = samplingRate;
1814 defaultOffloadInfo.channel_mask = channelMask;
1815 defaultOffloadInfo.format = format;
1816 defaultOffloadInfo.stream_type = stream;
1817 defaultOffloadInfo.bit_rate = 0;
1818 defaultOffloadInfo.duration_us = -1;
1819 defaultOffloadInfo.has_video = true; // conservative
1820 defaultOffloadInfo.is_streaming = true; // likely
1821 offloadInfo = &defaultOffloadInfo;
1822 }
1823
1824 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001825 outputDesc->mDevice = device;
1826 outputDesc->mLatency = 0;
Sharad Sangle36781612015-05-28 16:15:16 +05301827 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001828 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1829 config.sample_rate = samplingRate;
1830 config.channel_mask = channelMask;
1831 config.format = format;
1832 if (offloadInfo != NULL) {
1833 config.offload_info = *offloadInfo;
1834 }
Sharad Sangle36781612015-05-28 16:15:16 +05301835 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001836 &output,
1837 &config,
1838 &outputDesc->mDevice,
1839 String8(""),
1840 &outputDesc->mLatency,
1841 outputDesc->mFlags);
1842
1843 // only accept an output with the requested parameters
1844 if (status != NO_ERROR ||
1845 (samplingRate != 0 && samplingRate != config.sample_rate) ||
Ashish Jain40cab3a2016-03-23 11:14:14 +05301846 (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) ||
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001847 (channelMask != 0 && channelMask != config.channel_mask)) {
1848 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1849 "format %d %d, channelMask %04x %04x", output, samplingRate,
1850 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1851 outputDesc->mChannelMask);
1852 if (output != AUDIO_IO_HANDLE_NONE) {
1853 mpClientInterface->closeOutput(output);
1854 }
Sharad Sangle36781612015-05-28 16:15:16 +05301855 // fall back to mixer output if possible when the direct output could not be open
Ashish Jain40cab3a2016-03-23 11:14:14 +05301856 if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) {
Sharad Sangle36781612015-05-28 16:15:16 +05301857 goto non_direct_output;
1858 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001859 return AUDIO_IO_HANDLE_NONE;
1860 }
1861 outputDesc->mSamplingRate = config.sample_rate;
1862 outputDesc->mChannelMask = config.channel_mask;
1863 outputDesc->mFormat = config.format;
1864 outputDesc->mRefCount[stream] = 0;
1865 outputDesc->mStopTime[stream] = 0;
1866 outputDesc->mDirectOpenCount = 1;
Garmond Leungfcb00d92017-01-31 11:12:47 -08001867 outputDesc->mDirectClientUid = clientUid;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001868
1869 audio_io_handle_t srcOutput = getOutputForEffect();
1870 addOutput(output, outputDesc);
1871 audio_io_handle_t dstOutput = getOutputForEffect();
1872 if (dstOutput == output) {
Gao Jiedb057832015-11-12 08:51:22 +08001873#ifdef DOLBY_ENABLE
1874 status_t status = mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1875 if (status == NO_ERROR) {
1876 for (size_t i = 0; i < mEffects.size(); i++) {
1877 sp<EffectDescriptor> desc = mEffects.valueAt(i);
1878 if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX) {
1879 // update the mIo member of EffectDescriptor for the global effect
1880 ALOGV("%s updating mIo", __FUNCTION__);
1881 desc->mIo = dstOutput;
1882 }
1883 }
1884 } else {
1885 ALOGW("%s moveEffects from %d to %d failed", __FUNCTION__, srcOutput, dstOutput);
1886 }
1887#else // DOLBY_END
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001888 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
Gao Jiedb057832015-11-12 08:51:22 +08001889#endif // LINE_ADDED_BY_DOLBY
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001890 }
1891 mPreviousOutputs = mOutputs;
1892 ALOGV("getOutput() returns new direct output %d", output);
1893 mpClientInterface->onAudioPortListUpdate();
1894 return output;
1895 }
1896
1897non_direct_output:
Ashish Jain5fe12412016-04-18 18:44:17 +05301898
1899 // A request for HW A/V sync cannot fallback to a mixed output because time
1900 // stamps are embedded in audio data
1901 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1902 return AUDIO_IO_HANDLE_NONE;
1903 }
1904
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001905 // ignoring channel mask due to downmix capability in mixer
1906
1907 // open a non direct output
1908
1909 // for non direct outputs, only PCM is supported
1910 if (audio_is_linear_pcm(format)) {
1911 // get which output is suitable for the specified stream. The actual
1912 // routing change will happen when startOutput() will be called
1913 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1914
1915 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1916 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
Ashish Jain660d3112016-06-20 10:16:51 +05301917
1918 if (forced_deep) {
1919 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
1920 ALOGI("setting force DEEP buffer now ");
1921 } else if(flags == AUDIO_OUTPUT_FLAG_NONE) {
1922 // no deep buffer playback is requested hence fallback to primary
1923 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_PRIMARY);
1924 ALOGI("FLAG None hence request for a primary output");
1925 }
1926
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001927 output = selectOutput(outputs, flags, format);
1928 }
1929 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1930 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1931
vivek mehta0ea887a2015-08-26 14:01:20 -07001932 ALOGV("getOutputForDevice() returns output %d", output);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001933
1934 return output;
1935}
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301936
1937status_t AudioPolicyManagerCustom::getInputForAttr(const audio_attributes_t *attr,
Garmond Leungfcb00d92017-01-31 11:12:47 -08001938 audio_io_handle_t *input,
1939 audio_session_t session,
1940 uid_t uid,
1941 const audio_config_base_t *config,
1942 audio_input_flags_t flags,
1943 audio_port_handle_t selectedDeviceId,
1944 input_type_t *inputType,
1945 audio_port_handle_t *portId)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301946{
Ashish Jain40cab3a2016-03-23 11:14:14 +05301947 audio_source_t inputSource;
1948 inputSource = attr->source;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301949#ifdef VOICE_CONCURRENCY
1950
1951 char propValue[PROPERTY_VALUE_MAX];
1952 bool prop_rec_enabled=false, prop_voip_enabled = false;
1953
1954 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
1955 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1956 }
1957
1958 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1959 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1960 }
1961
1962 if (prop_rec_enabled && mvoice_call_state) {
1963 //check if voice call is active / running in background
1964 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1965 //Need to block input request
1966 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1967 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1968 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1969 {
1970 switch(inputSource) {
1971 case AUDIO_SOURCE_VOICE_UPLINK:
1972 case AUDIO_SOURCE_VOICE_DOWNLINK:
1973 case AUDIO_SOURCE_VOICE_CALL:
1974 ALOGD("voice_conc:Creating input during incall mode for inputSource: %d",
1975 inputSource);
1976 break;
1977
1978 case AUDIO_SOURCE_VOICE_COMMUNICATION:
1979 if(prop_voip_enabled) {
1980 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1981 inputSource);
1982 return NO_INIT;
1983 }
1984 break;
1985 default:
1986 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1987 inputSource);
1988 return NO_INIT;
1989 }
1990 }
1991 }//check for VoIP flag
1992 else if(prop_voip_enabled && mvoice_call_state) {
1993 //check if voice call is active / running in background
1994 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1995 //Need to block input request
1996 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1997 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1998 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1999 {
2000 if(inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION) {
2001 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
2002 return NO_INIT;
2003 }
2004 }
2005 }
2006
2007#endif
2008
2009 return AudioPolicyManager::getInputForAttr(attr,
2010 input,
2011 session,
2012 uid,
Garmond Leungfcb00d92017-01-31 11:12:47 -08002013 config,
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302014 flags,
2015 selectedDeviceId,
Garmond Leungfcb00d92017-01-31 11:12:47 -08002016 inputType,
2017 portId);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302018}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08002019
Garmond Leungfcb00d92017-01-31 11:12:47 -08002020
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302021status_t AudioPolicyManagerCustom::startInput(audio_io_handle_t input,
Garmond Leungfcb00d92017-01-31 11:12:47 -08002022 audio_session_t session,
2023 concurrency_type__mask_t *concurrency)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302024{
2025 ALOGV("startInput() input %d", input);
Garmond Leungfcb00d92017-01-31 11:12:47 -08002026 *concurrency = API_INPUT_CONCURRENCY_NONE;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302027 ssize_t index = mInputs.indexOfKey(input);
2028 if (index < 0) {
2029 ALOGW("startInput() unknown input %d", input);
2030 return BAD_VALUE;
2031 }
2032 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
2033
Ashish Jain40cab3a2016-03-23 11:14:14 +05302034 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
2035 if (audioSession == 0) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302036 ALOGW("startInput() unknown session %d on input %d", session, input);
2037 return BAD_VALUE;
2038 }
2039
Garmond Leungfcb00d92017-01-31 11:12:47 -08002040 if (!isConcurentCaptureAllowed(inputDesc, audioSession)) {
2041 ALOGW("startInput(%d) failed: other input already started", input);
2042 return INVALID_OPERATION;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302043 }
2044
Garmond Leungfcb00d92017-01-31 11:12:47 -08002045 if (isInCall()) {
2046 *concurrency |= API_INPUT_CONCURRENCY_CALL;
2047 }
2048
2049 if (mInputs.activeInputsCountOnDevices() != 0) {
2050 *concurrency |= API_INPUT_CONCURRENCY_CAPTURE;
2051 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302052#ifdef RECORD_PLAY_CONCURRENCY
2053 mIsInputRequestOnProgress = true;
2054
2055 char getPropValue[PROPERTY_VALUE_MAX];
2056 bool prop_rec_play_enabled = false;
2057
2058 if (property_get("rec.playback.conc.disabled", getPropValue, NULL)) {
2059 prop_rec_play_enabled = atoi(getPropValue) || !strncmp("true", getPropValue, 4);
2060 }
2061
Garmond Leungfcb00d92017-01-31 11:12:47 -08002062 if ((prop_rec_play_enabled) && (mInputs.activeInputsCountOnDevices() == 0)){
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302063 // send update to HAL on record playback concurrency
2064 AudioParameter param = AudioParameter();
2065 param.add(String8("rec_play_conc_on"), String8("true"));
2066 ALOGD("startInput() setParameters rec_play_conc is setting to ON ");
2067 mpClientInterface->setParameters(0, param.toString());
2068
2069 // Call invalidate to reset all opened non ULL audio tracks
2070 // Move tracks associated to this strategy from previous output to new output
Garmond Leungfcb00d92017-01-31 11:12:47 -08002071 for (int i = AUDIO_STREAM_SYSTEM; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302072 // Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder)
Garmond Leungfcb00d92017-01-31 11:12:47 -08002073 if (i != AUDIO_STREAM_ENFORCED_AUDIBLE) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302074 ALOGD("Invalidate on releaseInput for stream :: %d ", i);
2075 //FIXME see fixme on name change
2076 mpClientInterface->invalidateStream((audio_stream_type_t)i);
2077 }
2078 }
2079 // close compress tracks
2080 for (size_t i = 0; i < mOutputs.size(); i++) {
2081 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
2082 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
2083 ALOGD("ouput desc / profile is NULL");
2084 continue;
2085 }
2086 if (outputDesc->mProfile->mFlags
2087 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2088 // close compress sessions
2089 ALOGD("calling closeOutput on record conc for COMPRESS output");
2090 closeOutput(mOutputs.keyAt(i));
2091 }
2092 }
Ashish Jaine4a22d52016-06-22 11:55:08 +05302093 // If effects where present on any of the above closed outputs,
2094 // audioflinger moved them to the primary output by default
2095 // move them back to the appropriate output.
2096 moveGlobalEffect();
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302097 }
2098#endif
2099
Garmond Leungfcb00d92017-01-31 11:12:47 -08002100 // increment activity count before calling getNewInputDevice() below as only active sessions
2101 // are considered for device selection
2102 audioSession->changeActiveCount(1);
2103
2104 // Routing?
2105 mInputRoutes.incRouteActivity(session);
2106
2107 if (audioSession->activeCount() == 1 || mInputRoutes.hasRouteChanged(session)) {
Sharad Sanglec66b9ca2016-12-21 18:40:54 +05302108 // indicate active capture to sound trigger service if starting capture from a mic on
2109 // primary HW module
Garmond Leungfcb00d92017-01-31 11:12:47 -08002110 audio_devices_t device = getNewInputDevice(inputDesc);
Sharad Sanglec66b9ca2016-12-21 18:40:54 +05302111 setInputDevice(input, device, true /* force */);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302112
Garmond Leungfcb00d92017-01-31 11:12:47 -08002113 if (inputDesc->getAudioSessionCount(true/*activeOnly*/) == 1) {
2114 // if input maps to a dynamic policy with an activity listener, notify of state change
2115 if ((inputDesc->mPolicyMix != NULL)
2116 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
2117 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
2118 MIX_STATE_MIXING);
Ashish Jain630c5e12016-04-18 11:50:21 +05302119 }
Garmond Leungfcb00d92017-01-31 11:12:47 -08002120
2121 audio_devices_t primaryInputDevices = availablePrimaryInputDevices();
2122 if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) &&
2123 mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) {
2124 SoundTrigger::setCaptureState(true);
2125 }
2126
2127 // automatically enable the remote submix output when input is started if not
2128 // used by a policy mix of type MIX_TYPE_RECORDERS
2129 // For remote submix (a virtual device), we open only one input per capture request.
2130 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
2131 String8 address = String8("");
2132 if (inputDesc->mPolicyMix == NULL) {
2133 address = String8("0");
2134 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
2135 address = inputDesc->mPolicyMix->mDeviceAddress;
2136 }
2137 if (address != "") {
2138 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2139 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2140 address, "remote-submix");
2141 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302142 }
2143 }
2144 }
2145
Ashish Jain40cab3a2016-03-23 11:14:14 +05302146 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302147#ifdef RECORD_PLAY_CONCURRENCY
2148 mIsInputRequestOnProgress = false;
2149#endif
2150 return NO_ERROR;
2151}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08002152
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302153status_t AudioPolicyManagerCustom::stopInput(audio_io_handle_t input,
2154 audio_session_t session)
2155{
2156 status_t status;
2157 status = AudioPolicyManager::stopInput(input, session);
2158#ifdef RECORD_PLAY_CONCURRENCY
2159 char propValue[PROPERTY_VALUE_MAX];
2160 bool prop_rec_play_enabled = false;
2161
2162 if (property_get("rec.playback.conc.disabled", propValue, NULL)) {
2163 prop_rec_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
2164 }
2165
Lalit Kansara33918092016-12-06 22:42:26 +05302166 if ((prop_rec_play_enabled) && (mInputs.activeInputsCountOnDevices() == 0)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302167
2168 //send update to HAL on record playback concurrency
2169 AudioParameter param = AudioParameter();
2170 param.add(String8("rec_play_conc_on"), String8("false"));
2171 ALOGD("stopInput() setParameters rec_play_conc is setting to OFF ");
2172 mpClientInterface->setParameters(0, param.toString());
2173
2174 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
2175 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
2176 //Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder stop tone)
2177 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE) && (i != AUDIO_STREAM_PATCH)) {
2178 ALOGD(" Invalidate on stopInput for stream :: %d ", i);
2179 //FIXME see fixme on name change
2180 mpClientInterface->invalidateStream((audio_stream_type_t)i);
2181 }
2182 }
2183 }
2184#endif
2185 return status;
2186}
2187
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302188void AudioPolicyManagerCustom::closeAllInputs() {
2189 bool patchRemoved = false;
2190
Dhanalakshmi Siddanibfeca7f2015-10-08 15:17:11 +05302191 for(size_t input_index = mInputs.size(); input_index > 0; input_index--) {
2192 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index-1);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302193 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302194 if (patch_index >= 0) {
2195 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
Garmond Leungfcb00d92017-01-31 11:12:47 -08002196 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302197 mAudioPatches.removeItemsAt(patch_index);
2198 patchRemoved = true;
2199 }
Haynes Mathew George2e4655a2017-01-05 15:01:53 -08002200 mpClientInterface->closeInput(mInputs.keyAt(input_index-1));
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302201 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05302202 mInputs.clear();
2203 SoundTrigger::setCaptureState(false);
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302204 nextAudioPortGeneration();
2205
2206 if (patchRemoved) {
2207 mpClientInterface->onAudioPatchListUpdate();
2208 }
2209}
2210
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302211AudioPolicyManagerCustom::AudioPolicyManagerCustom(AudioPolicyClientInterface *clientInterface)
Sharad Sangle4509cef2015-08-19 20:47:12 +05302212 : AudioPolicyManager(clientInterface),
2213 mHdmiAudioDisabled(false),
2214 mHdmiAudioEvent(false),
Garmond Leungfcb00d92017-01-31 11:12:47 -08002215#ifndef FM_POWER_OPT
2216 mPrevPhoneState(0)
2217#else
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08002218 mPrevPhoneState(0),
Haynes Mathew George603ae9b2015-12-21 17:23:59 -08002219 mPrevFMVolumeDb(0.0f),
2220 mFMIsActive(false)
Garmond Leungfcb00d92017-01-31 11:12:47 -08002221#endif
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302222{
Ashish Jain40cab3a2016-03-23 11:14:14 +05302223
Satya Krishna Pindiproli8f83f102016-06-21 17:56:14 +05302224#ifdef USE_XML_AUDIO_POLICY_CONF
2225 ALOGD("USE_XML_AUDIO_POLICY_CONF is TRUE");
2226#else
2227 ALOGD("USE_XML_AUDIO_POLICY_CONF is FALSE");
2228#endif
2229
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302230#ifdef RECORD_PLAY_CONCURRENCY
2231 mIsInputRequestOnProgress = false;
2232#endif
2233
2234
2235#ifdef VOICE_CONCURRENCY
2236 mFallBackflag = getFallBackPath();
2237#endif
2238}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07002239}