blob: 87ba53c9d5baef2ce9dd3b5ebede18ba6694574a [file] [log] [blame]
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001/*
Sharad Sanglec66b9ca2016-12-21 18:40:54 +05302 * Copyright (c) 2013-2017 The Linux Foundation. All rights reserved.
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07003 * Not a contribution.
4 *
5 * Copyright (C) 2009 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Gao Jiedb057832015-11-12 08:51:22 +080018 *
19 * This file was modified by Dolby Laboratories, Inc. The portions of the
20 * code that are surrounded by "DOLBY..." are copyrighted and
21 * licensed separately, as follows:
22 *
23 * (C) 2015 Dolby Laboratories, Inc.
24 *
25 * Licensed under the Apache License, Version 2.0 (the "License");
26 * you may not use this file except in compliance with the License.
27 * You may obtain a copy of the License at
28 *
29 * http://www.apache.org/licenses/LICENSE-2.0
30 *
31 * Unless required by applicable law or agreed to in writing, software
32 * distributed under the License is distributed on an "AS IS" BASIS,
33 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34 * See the License for the specific language governing permissions and
35 * limitations under the License.
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070036 */
37
Sharad Sangle36781612015-05-28 16:15:16 +053038#define LOG_TAG "AudioPolicyManagerCustom"
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070039//#define LOG_NDEBUG 0
40
41//#define VERY_VERBOSE_LOGGING
42#ifdef VERY_VERBOSE_LOGGING
43#define ALOGVV ALOGV
44#else
45#define ALOGVV(a...) do { } while(0)
46#endif
Sharad Sangle36781612015-05-28 16:15:16 +053047
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070048// A device mask for all audio output devices that are considered "remote" when evaluating
49// active output devices in isStreamActiveRemotely()
50#define APM_AUDIO_OUT_DEVICE_REMOTE_ALL AUDIO_DEVICE_OUT_REMOTE_SUBMIX
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070051// A device mask for all audio input and output devices where matching inputs/outputs on device
52// type alone is not enough: the address must match too
53#define APM_AUDIO_DEVICE_MATCH_ADDRESS_ALL (AUDIO_DEVICE_IN_REMOTE_SUBMIX | \
54 AUDIO_DEVICE_OUT_REMOTE_SUBMIX)
Sharad Sangle75170722016-11-21 16:21:06 +053055#define SAMPLE_RATE_8000 8000
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070056#include <inttypes.h>
Mingming Yin0ae14ea2014-07-09 17:55:56 -070057#include <math.h>
Mingming Yin0670f162014-06-12 16:05:49 -070058
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070059#include <cutils/properties.h>
60#include <utils/Log.h>
61#include <hardware/audio.h>
62#include <hardware/audio_effect.h>
63#include <media/AudioParameter.h>
64#include <soundtrigger/SoundTrigger.h>
65#include "AudioPolicyManager.h"
Sharad Sangle36781612015-05-28 16:15:16 +053066#include <policy.h>
Gao Jiedb057832015-11-12 08:51:22 +080067#ifdef DOLBY_ENABLE
68#include "DolbyAudioPolicy_impl.h"
69#endif // DOLBY_END
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070070
71namespace android {
Sharad Sanglec66b9ca2016-12-21 18:40:54 +053072/*audio policy: workaround for truncated touch sounds*/
73//FIXME: workaround for truncated touch sounds
74// to be removed when the problem is handled by system UI
75#define TOUCH_SOUND_FIXED_DELAY_MS 100
Sharad Sanglec5766ff2015-06-04 20:24:10 +053076#ifdef VOICE_CONCURRENCY
77audio_output_flags_t AudioPolicyManagerCustom::getFallBackPath()
78{
79 audio_output_flags_t flag = AUDIO_OUTPUT_FLAG_FAST;
80 char propValue[PROPERTY_VALUE_MAX];
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070081
Sharad Sanglec5766ff2015-06-04 20:24:10 +053082 if (property_get("voice.conc.fallbackpath", propValue, NULL)) {
83 if (!strncmp(propValue, "deep-buffer", 11)) {
84 flag = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
85 }
86 else if (!strncmp(propValue, "fast", 4)) {
87 flag = AUDIO_OUTPUT_FLAG_FAST;
88 }
89 else {
90 ALOGD("voice_conc:not a recognised path(%s) in prop voice.conc.fallbackpath",
91 propValue);
92 }
93 }
94 else {
95 ALOGD("voice_conc:prop voice.conc.fallbackpath not set");
96 }
97
98 ALOGD("voice_conc:picked up flag(0x%x) from prop voice.conc.fallbackpath",
99 flag);
100
101 return flag;
102}
103#endif /*VOICE_CONCURRENCY*/
Ashish Jaine4a22d52016-06-22 11:55:08 +0530104
105void AudioPolicyManagerCustom::moveGlobalEffect()
106{
107 audio_io_handle_t dstOutput = getOutputForEffect();
108 if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) {
109#ifdef DOLBY_ENABLE
110 status_t status =
111 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
112 mPrimaryOutput->mIoHandle,
113 dstOutput);
114 if (status == NO_ERROR) {
115 for (size_t i = 0; i < mEffects.size(); i++) {
116 sp<EffectDescriptor> desc = mEffects.valueAt(i);
117 if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX) {
118 // update the mIo member of EffectDescriptor
119 // for the global effect
120 ALOGV("%s updating mIo", __FUNCTION__);
121 desc->mIo = dstOutput;
122 }
123 }
124 } else {
125 ALOGW("%s moveEffects from %d to %d failed", __FUNCTION__,
126 mPrimaryOutput->mIoHandle, dstOutput);
127 }
128#else // DOLBY_END
129 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
130 mPrimaryOutput->mIoHandle, dstOutput);
131#endif
132 }
133}
134
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700135// ----------------------------------------------------------------------------
136// AudioPolicyInterface implementation
137// ----------------------------------------------------------------------------
Sharad Sangle36781612015-05-28 16:15:16 +0530138extern "C" AudioPolicyInterface* createAudioPolicyManager(
139 AudioPolicyClientInterface *clientInterface)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700140{
Sharad Sangle36781612015-05-28 16:15:16 +0530141 return new AudioPolicyManagerCustom(clientInterface);
142}
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700143
Sharad Sangle36781612015-05-28 16:15:16 +0530144extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface)
145{
146 delete interface;
147}
148
149status_t AudioPolicyManagerCustom::setDeviceConnectionStateInt(audio_devices_t device,
150 audio_policy_dev_state_t state,
151 const char *device_address,
152 const char *device_name)
153{
Ashish Jain40cab3a2016-03-23 11:14:14 +0530154 ALOGD("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
Sharad Sangle36781612015-05-28 16:15:16 +0530155 device, state, device_address, device_name);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700156
157 // connect/disconnect only 1 device at a time
158 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
159
Sharad Sangle36781612015-05-28 16:15:16 +0530160 sp<DeviceDescriptor> devDesc =
161 mHwModules.getDeviceDescriptor(device, device_address, device_name);
162
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700163 // handle output devices
164 if (audio_is_output_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700165 SortedVector <audio_io_handle_t> outputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700166
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700167 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700168
169 // save a copy of the opened output descriptors before any output is opened or closed
170 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
171 mPreviousOutputs = mOutputs;
172 switch (state)
173 {
174 // handle output device connection
Sharad Sangle36781612015-05-28 16:15:16 +0530175 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700176 if (index >= 0) {
Sharad Sangle4509cef2015-08-19 20:47:12 +0530177#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
178 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
179 if (!strncmp(device_address, "hdmi_spkr", 9)) {
180 mHdmiAudioDisabled = false;
181 } else {
182 mHdmiAudioEvent = true;
183 }
184 }
185#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700186 ALOGW("setDeviceConnectionState() device already connected: %x", device);
187 return INVALID_OPERATION;
188 }
189 ALOGV("setDeviceConnectionState() connecting device %x", device);
190
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700191 // register new device as available
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700192 index = mAvailableOutputDevices.add(devDesc);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530193#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
194 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
195 if (!strncmp(device_address, "hdmi_spkr", 9)) {
196 mHdmiAudioDisabled = false;
197 } else {
198 mHdmiAudioEvent = true;
199 }
200 if (mHdmiAudioDisabled || !mHdmiAudioEvent) {
201 mAvailableOutputDevices.remove(devDesc);
202 ALOGW("HDMI sink not connected, do not route audio to HDMI out");
203 return INVALID_OPERATION;
204 }
205 }
206#endif
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700207 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530208 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700209 if (module == 0) {
210 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
211 device);
212 mAvailableOutputDevices.remove(devDesc);
213 return INVALID_OPERATION;
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700214 }
Sharad Sangle36781612015-05-28 16:15:16 +0530215 mAvailableOutputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700216 } else {
217 return NO_MEMORY;
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700218 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700219
Sharad Sangle36781612015-05-28 16:15:16 +0530220 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700221 mAvailableOutputDevices.remove(devDesc);
222 return INVALID_OPERATION;
223 }
Sharad Sangle36781612015-05-28 16:15:16 +0530224 // Propagate device availability to Engine
225 mEngine->setDeviceConnectionState(devDesc, state);
226
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700227 // outputs should never be empty here
228 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
229 "checkOutputsForDevice() returned no outputs but status OK");
230 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
231 outputs.size());
Sharad Sangle36781612015-05-28 16:15:16 +0530232
233 // Send connect to HALs
234 AudioParameter param = AudioParameter(devDesc->mAddress);
235 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
236 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
237
238 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700239 // handle output device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700240 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
241 if (index < 0) {
Sharad Sangle4509cef2015-08-19 20:47:12 +0530242#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
243 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
244 if (!strncmp(device_address, "hdmi_spkr", 9)) {
245 mHdmiAudioDisabled = true;
246 } else {
247 mHdmiAudioEvent = false;
248 }
249 }
250#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700251 ALOGW("setDeviceConnectionState() device not connected: %x", device);
252 return INVALID_OPERATION;
253 }
254
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700255 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
256
Sharad Sangle36781612015-05-28 16:15:16 +0530257 // Send Disconnect to HALs
258 AudioParameter param = AudioParameter(devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700259 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
260 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
261
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700262 // remove device from available output devices
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700263 mAvailableOutputDevices.remove(devDesc);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530264#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
265 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
266 if (!strncmp(device_address, "hdmi_spkr", 9)) {
267 mHdmiAudioDisabled = true;
268 } else {
269 mHdmiAudioEvent = false;
270 }
271 }
272#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530273 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
274
275 // Propagate device availability to Engine
276 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700277 } break;
278
279 default:
280 ALOGE("setDeviceConnectionState() invalid state: %x", state);
281 return BAD_VALUE;
282 }
283
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700284 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
285 // output is suspended before any tracks are moved to it
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700286 checkA2dpSuspend();
287 checkOutputForAllStrategies();
288 // outputs must be closed after checkOutputForAllStrategies() is executed
289 if (!outputs.isEmpty()) {
290 for (size_t i = 0; i < outputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530291 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700292 // close unused outputs after device disconnection or direct outputs that have been
293 // opened by checkOutputsForDevice() to query dynamic parameters
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700294 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700295 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
296 (desc->mDirectOpenCount == 0))) {
297 closeOutput(outputs[i]);
298 }
299 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700300 // check again after closing A2DP output to reset mA2dpSuspended if needed
301 checkA2dpSuspend();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700302 }
303
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530304#ifdef FM_POWER_OPT
305 // handle FM device connection state to trigger FM AFE loopback
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800306 if (device == AUDIO_DEVICE_OUT_FM && hasPrimaryOutput()) {
Mingming Yin2dd77c22016-01-06 18:02:42 -0800307 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530308 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
309 mPrimaryOutput->changeRefCount(AUDIO_STREAM_MUSIC, 1);
Mingming Yin2dd77c22016-01-06 18:02:42 -0800310 newDevice = (audio_devices_t)(getNewOutputDevice(mPrimaryOutput, false)|AUDIO_DEVICE_OUT_FM);
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800311 mFMIsActive = true;
Zhou Songbd0264e2016-06-21 11:01:06 +0800312 mPrimaryOutput->mDevice = newDevice & ~AUDIO_DEVICE_OUT_FM;
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530313 } else {
Mingming Yin2dd77c22016-01-06 18:02:42 -0800314 newDevice = (audio_devices_t)(getNewOutputDevice(mPrimaryOutput, false));
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800315 mFMIsActive = false;
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530316 mPrimaryOutput->changeRefCount(AUDIO_STREAM_MUSIC, -1);
317 }
318 AudioParameter param = AudioParameter();
319 param.addInt(String8("handle_fm"), (int)newDevice);
320 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString());
321 }
322#endif /* FM_POWER_OPT end */
323
Zhou Songbd0264e2016-06-21 11:01:06 +0800324 updateDevicesAndOutputs();
325#ifdef DOLBY_ENABLE
326 // Before closing the opened outputs, update endpoint property with device capabilities
327 audio_devices_t audioOutputDevice = getDeviceForStrategy(getStrategy(AUDIO_STREAM_MUSIC), true);
328 mDolbyAudioPolicy.setEndpointSystemProperty(audioOutputDevice, mHwModules);
329#endif // DOLBY_END
330 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
331 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
332 updateCallRouting(newDevice);
333 }
334
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700335 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530336 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
337 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
338 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700339 // do not force device change on duplicated output because if device is 0, it will
340 // also force a device 0 for the two outputs it is duplicated to which may override
341 // a valid device selection on those outputs.
Sharad Sangle36781612015-05-28 16:15:16 +0530342 bool force = !desc->isDuplicated()
343 && (!device_distinguishes_on_address(device)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700344 // always force when disconnecting (a non-duplicated device)
345 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Sharad Sangle36781612015-05-28 16:15:16 +0530346 setOutputDevice(desc, newDevice, force, 0);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700347 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700348 }
349
Ashish Jain40cab3a2016-03-23 11:14:14 +0530350 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
351 cleanUpForDevice(devDesc);
352 }
353
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700354 mpClientInterface->onAudioPortListUpdate();
355 return NO_ERROR;
356 } // end if is output device
357
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700358 // handle input devices
359 if (audio_is_input_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700360 SortedVector <audio_io_handle_t> inputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700361
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700362 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700363 switch (state)
364 {
365 // handle input device connection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700366 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
367 if (index >= 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700368 ALOGW("setDeviceConnectionState() device already connected: %d", device);
369 return INVALID_OPERATION;
370 }
Sharad Sangle36781612015-05-28 16:15:16 +0530371 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700372 if (module == NULL) {
373 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
374 device);
375 return INVALID_OPERATION;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700376 }
Sharad Sangle36781612015-05-28 16:15:16 +0530377 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700378 return INVALID_OPERATION;
379 }
380
381 index = mAvailableInputDevices.add(devDesc);
382 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530383 mAvailableInputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700384 } else {
385 return NO_MEMORY;
386 }
Sharad Sangle36781612015-05-28 16:15:16 +0530387
388 // Set connect to HALs
389 AudioParameter param = AudioParameter(devDesc->mAddress);
390 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
391 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
392
393 // Propagate device availability to Engine
394 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700395 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700396
397 // handle input device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700398 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
399 if (index < 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700400 ALOGW("setDeviceConnectionState() device not connected: %d", device);
401 return INVALID_OPERATION;
402 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700403
404 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
405
406 // Set Disconnect to HALs
Sharad Sangle36781612015-05-28 16:15:16 +0530407 AudioParameter param = AudioParameter(devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700408 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
409 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
410
Sharad Sangle36781612015-05-28 16:15:16 +0530411 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700412 mAvailableInputDevices.remove(devDesc);
413
Sharad Sangle36781612015-05-28 16:15:16 +0530414 // Propagate device availability to Engine
415 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700416 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700417
418 default:
419 ALOGE("setDeviceConnectionState() invalid state: %x", state);
420 return BAD_VALUE;
421 }
422
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700423 closeAllInputs();
Sharad Sanglec66b9ca2016-12-21 18:40:54 +0530424 /*audio policy: fix call volume over USB*/
425 // As the input device list can impact the output device selection, update
426 // getDeviceForStrategy() cache
427 updateDevicesAndOutputs();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700428
Sharad Sangle36781612015-05-28 16:15:16 +0530429 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700430 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
431 updateCallRouting(newDevice);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700432 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700433
Ashish Jain40cab3a2016-03-23 11:14:14 +0530434 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
435 cleanUpForDevice(devDesc);
436 }
437
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700438 mpClientInterface->onAudioPortListUpdate();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700439 return NO_ERROR;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700440 } // end if is input device
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700441
442 ALOGW("setDeviceConnectionState() invalid device: %x", device);
443 return BAD_VALUE;
444}
Weiyin Jiangd211ef52016-02-16 15:55:11 +0800445
Preetam Singh Ranawat3db94ec2016-10-13 18:35:12 +0530446bool AudioPolicyManagerCustom::isInvalidationOfMusicStreamNeeded(routing_strategy strategy)
447{
448 if (strategy == STRATEGY_MEDIA) {
449 for (size_t i = 0; i < mOutputs.size(); i++) {
450 sp<SwAudioOutputDescriptor> newOutputDesc = mOutputs.valueAt(i);
451 if (newOutputDesc->mFormat == AUDIO_FORMAT_DSD)
452 return false;
453 }
454 }
455 return true;
456}
457
458void AudioPolicyManagerCustom::checkOutputForStrategy(routing_strategy strategy)
459{
460 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
461 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
462 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mOutputs);
463 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
464
465 // also take into account external policy-related changes: add all outputs which are
466 // associated with policies in the "before" and "after" output vectors
467 ALOGV("checkOutputForStrategy(): policy related outputs");
468 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
469 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
470 if (desc != 0 && desc->mPolicyMix != NULL) {
471 srcOutputs.add(desc->mIoHandle);
472 ALOGV(" previous outputs: adding %d", desc->mIoHandle);
473 }
474 }
475 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
476 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
477 if (desc != 0 && desc->mPolicyMix != NULL) {
478 dstOutputs.add(desc->mIoHandle);
479 ALOGV(" new outputs: adding %d", desc->mIoHandle);
480 }
481 }
482
483 if (!vectorsEqual(srcOutputs,dstOutputs) && isInvalidationOfMusicStreamNeeded(strategy)) {
484 AudioPolicyManager::checkOutputForStrategy(strategy);
485 }
486}
487
Sharad Sangle36781612015-05-28 16:15:16 +0530488// This function checks for the parameters which can be offloaded.
489// This can be enhanced depending on the capability of the DSP and policy
490// of the system.
491bool AudioPolicyManagerCustom::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700492{
Sharad Sangle36781612015-05-28 16:15:16 +0530493 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
494 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
495 offloadInfo.sample_rate, offloadInfo.channel_mask,
496 offloadInfo.format,
497 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
498 offloadInfo.has_video);
Ashish Jain40cab3a2016-03-23 11:14:14 +0530499
500 if (mMasterMono) {
501 return false; // no offloading if mono is set.
502 }
503
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530504#ifdef VOICE_CONCURRENCY
505 char concpropValue[PROPERTY_VALUE_MAX];
506 if (property_get("voice.playback.conc.disabled", concpropValue, NULL)) {
507 bool propenabled = atoi(concpropValue) || !strncmp("true", concpropValue, 4);
508 if (propenabled) {
509 if (isInCall())
510 {
511 ALOGD("\n copl: blocking compress offload on call mode\n");
512 return false;
513 }
514 }
515 }
516#endif
Preetam Singh Ranawat96afc162016-08-25 14:35:02 +0530517 if (property_get_bool("voice.dsd.playback.conc.disabled", true) &&
518 isInCall() && (offloadInfo.format == AUDIO_FORMAT_DSD)) {
519 ALOGD("blocking DSD compress offload on call mode");
520 return false;
521 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530522#ifdef RECORD_PLAY_CONCURRENCY
523 char recConcPropValue[PROPERTY_VALUE_MAX];
524 bool prop_rec_play_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700525
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530526 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
527 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
528 }
529
530 if ((prop_rec_play_enabled) &&
Lalit Kansara33918092016-12-06 22:42:26 +0530531 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCountOnDevices() > 0))) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530532 ALOGD("copl: blocking compress offload for record concurrency");
533 return false;
534 }
535#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530536 // Check if stream type is music, then only allow offload as of now.
537 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
538 {
539 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
540 return false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700541 }
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530542
Alexy Joseph0ac09032015-10-16 15:57:53 -0700543 // Check if offload has been disabled
544 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
545 if (offloadDisabled) {
546 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
547 return false;
548 }
549
Ashish Jainac510a72016-04-28 12:22:58 +0530550 //check if it's multi-channel AAC (includes sub formats) and FLAC format
551 if ((popcount(offloadInfo.channel_mask) > 2) &&
552 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
553 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS))) {
554 ALOGD("offload disabled for multi-channel AAC,FLAC and VORBIS format");
555 return false;
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530556 }
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530557
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530558#ifdef AUDIO_EXTN_FORMATS_ENABLED
Ashish Jainac510a72016-04-28 12:22:58 +0530559 //check if it's multi-channel FLAC/ALAC/WMA format with sample rate > 48k
560 if ((popcount(offloadInfo.channel_mask) > 2) &&
561 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
562 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) && (offloadInfo.sample_rate > 48000)) ||
563 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) && (offloadInfo.sample_rate > 48000)) ||
564 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) && (offloadInfo.sample_rate > 48000)) ||
565 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS))) {
566 ALOGD("offload disabled for multi-channel FLAC/ALAC/WMA/AAC_ADTS clips with sample rate > 48kHz");
567 return false;
568 }
yidonghae28e3a2016-08-19 11:31:15 +0800569
570 if ((((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) && (offloadInfo.bit_rate > MAX_BITRATE_WMA)) ||
571 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) && (offloadInfo.bit_rate > MAX_BITRATE_WMA_PRO)) ||
572 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) && (offloadInfo.bit_rate > MAX_BITRATE_WMA_LOSSLESS))){
573 //Safely choose the min bitrate as threshold and leave the restriction to NT decoder as we can't distinguish wma pro and wma lossless here.
574 ALOGD("offload disabled for WMA/WMA_PRO/WMA_LOSSLESS clips with bit rate over maximum supported value");
575 return false;
576 }
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530577#endif
Ashish Jainac510a72016-04-28 12:22:58 +0530578 //TODO: enable audio offloading with video when ready
579 const bool allowOffloadWithVideo =
580 property_get_bool("audio.offload.video", false /* default_value */);
581 if (offloadInfo.has_video && !allowOffloadWithVideo) {
582 ALOGV("isOffloadSupported: has_video == true, returning false");
583 return false;
584 }
Manish Dewanganf3cd0f82015-10-13 14:04:36 +0530585
Ashish Jainac510a72016-04-28 12:22:58 +0530586 const bool allowOffloadStreamingWithVideo = property_get_bool("av.streaming.offload.enable",
587 false /*default value*/);
588 if (offloadInfo.has_video && offloadInfo.is_streaming && !allowOffloadStreamingWithVideo) {
589 ALOGW("offload disabled by av.streaming.offload.enable %d",allowOffloadStreamingWithVideo);
590 return false;
Sharad Sangle36781612015-05-28 16:15:16 +0530591 }
592
593 //If duration is less than minimum value defined in property, return false
Ashish Jainac510a72016-04-28 12:22:58 +0530594 char propValue[PROPERTY_VALUE_MAX];
Sharad Sangle36781612015-05-28 16:15:16 +0530595 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
596 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
597 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
598 return false;
599 }
600 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
601 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
602 //duration checks only valid for MP3/AAC/ formats,
603 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
604 if ((offloadInfo.format == AUDIO_FORMAT_MP3) ||
605 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
606 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
Ashish Jainac510a72016-04-28 12:22:58 +0530607 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS))
608 return false;
609
610#ifdef AUDIO_EXTN_FORMATS_ENABLED
611 if (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) ||
Sharad Sangle36781612015-05-28 16:15:16 +0530612 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) ||
613 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) ||
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530614 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_APE) ||
Preetam Singh Ranawat191eb772016-09-09 17:10:19 +0530615 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_DSD) ||
Ashish Jainac510a72016-04-28 12:22:58 +0530616 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS))
Sharad Sangle36781612015-05-28 16:15:16 +0530617 return false;
Ashish Jainac510a72016-04-28 12:22:58 +0530618#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530619 }
620
621 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
622 // creating an offloaded track and tearing it down immediately after start when audioflinger
623 // detects there is an active non offloadable effect.
624 // FIXME: We should check the audio session here but we do not have it in this context.
625 // This may prevent offloading in rare situations where effects are left active by apps
626 // in the background.
627 if (mEffects.isNonOffloadableEffectEnabled()) {
628 return false;
629 }
Dhananjay Kumarcf558ba2016-07-29 19:33:13 +0530630
Sharad Sangle36781612015-05-28 16:15:16 +0530631 // See if there is a profile to support this.
632 // AUDIO_DEVICE_NONE
633 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
634 offloadInfo.sample_rate,
635 offloadInfo.format,
636 offloadInfo.channel_mask,
637 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
638 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
639 return (profile != 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700640}
Weiyin Jiangd211ef52016-02-16 15:55:11 +0800641
Sharad Sangle36781612015-05-28 16:15:16 +0530642audio_devices_t AudioPolicyManagerCustom::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
643 bool fromCache)
644{
645 audio_devices_t device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700646
Ashish Jain40cab3a2016-03-23 11:14:14 +0530647 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Sharad Sangle36781612015-05-28 16:15:16 +0530648 if (index >= 0) {
649 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
650 if (patchDesc->mUid != mUidCached) {
651 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Ashish Jain40cab3a2016-03-23 11:14:14 +0530652 outputDesc->device(), outputDesc->getPatchHandle());
Sharad Sangle36781612015-05-28 16:15:16 +0530653 return outputDesc->device();
654 }
655 }
656
657 // check the following by order of priority to request a routing change if necessary:
658 // 1: the strategy enforced audible is active and enforced on the output:
659 // use device for strategy enforced audible
660 // 2: we are in call or the strategy phone is active on the output:
661 // use device for strategy phone
662 // 3: the strategy for enforced audible is active but not enforced on the output:
663 // use the device for strategy enforced audible
664 // 4: the strategy sonification is active on the output:
665 // use device for strategy sonification
666 // 5: the strategy "respectful" sonification is active on the output:
667 // use device for strategy "respectful" sonification
668 // 6: the strategy accessibility is active on the output:
669 // use device for strategy accessibility
670 // 7: the strategy media is active on the output:
671 // use device for strategy media
672 // 8: the strategy DTMF is active on the output:
673 // use device for strategy DTMF
674 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
675 // use device for strategy t-t-s
676 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
677 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
678 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
679 } else if (isInCall() ||
680 isStrategyActive(outputDesc, STRATEGY_PHONE)||
681 isStrategyActive(mPrimaryOutput, STRATEGY_PHONE)) {
682 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
683 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
684 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
685 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)||
686 (isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION)
687 && (!isStrategyActive(mPrimaryOutput,STRATEGY_MEDIA)))) {
688 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530689 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL) ||
690 isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION_RESPECTFUL)) {
Sharad Sangle36781612015-05-28 16:15:16 +0530691 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
692 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
693 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
694 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
695 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
696 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
697 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
698 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
699 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
700 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
701 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
702 }
703
704 ALOGV("getNewOutputDevice() selected device %x", device);
705 return device;
706}
Weiyin Jiangd211ef52016-02-16 15:55:11 +0800707
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700708void AudioPolicyManagerCustom::setPhoneState(audio_mode_t state)
709{
Ashish Jain40cab3a2016-03-23 11:14:14 +0530710 ALOGD("setPhoneState() state %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530711 // store previous phone state for management of sonification strategy below
Sharad Sangle4509cef2015-08-19 20:47:12 +0530712 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Sharad Sangle36781612015-05-28 16:15:16 +0530713 int oldState = mEngine->getPhoneState();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700714
Sharad Sangle36781612015-05-28 16:15:16 +0530715 if (mEngine->setPhoneState(state) != NO_ERROR) {
716 ALOGW("setPhoneState() invalid or same state %d", state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700717 return;
718 }
Sharad Sangle36781612015-05-28 16:15:16 +0530719 /// Opens: can these line be executed after the switch of volume curves???
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700720 // if leaving call state, handle special case of active streams
721 // pertaining to sonification strategy see handleIncallSonification()
Zhou Song58081b62016-02-24 13:10:55 +0800722 if (isStateInCall(oldState)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700723 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530724 for (size_t j = 0; j < mOutputs.size(); j++) {
725 audio_io_handle_t curOutput = mOutputs.keyAt(j);
726 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
727 if (stream == AUDIO_STREAM_PATCH) {
728 continue;
729 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530730 handleIncallSonification((audio_stream_type_t)stream, false, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +0530731 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700732 }
Sharad Sangle36781612015-05-28 16:15:16 +0530733
Zhou Song58081b62016-02-24 13:10:55 +0800734 // force reevaluating accessibility routing when call stops
Sharad Sangle36781612015-05-28 16:15:16 +0530735 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700736 }
737
Sharad Sangle36781612015-05-28 16:15:16 +0530738 /**
739 * Switching to or from incall state or switching between telephony and VoIP lead to force
740 * routing command.
741 */
742 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
743 || (is_state_in_call(state) && (state != oldState)));
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700744
745 // check for device and output changes triggered by new phone state
746 checkA2dpSuspend();
747 checkOutputForAllStrategies();
748 updateDevicesAndOutputs();
749
Sharad Sangle36781612015-05-28 16:15:16 +0530750 sp<SwAudioOutputDescriptor> hwOutputDesc = mPrimaryOutput;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530751#ifdef VOICE_CONCURRENCY
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530752 char propValue[PROPERTY_VALUE_MAX];
753 bool prop_playback_enabled = false, prop_rec_enabled=false, prop_voip_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700754
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530755 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
756 prop_playback_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
757 }
758
759 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
760 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
761 }
762
763 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
764 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
765 }
766
Dhananjay Kumar9125b2a2015-12-01 16:24:09 +0530767 if ((AUDIO_MODE_IN_CALL != oldState) && (AUDIO_MODE_IN_CALL == state)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530768 ALOGD("voice_conc:Entering to call mode oldState :: %d state::%d ",
769 oldState, state);
Dhananjay Kumar9125b2a2015-12-01 16:24:09 +0530770 mvoice_call_state = state;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530771 if (prop_rec_enabled) {
772 //Close all active inputs
773 audio_io_handle_t activeInput = mInputs.getActiveInput();
774 if (activeInput != 0) {
775 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
776 switch(activeDesc->mInputSource) {
777 case AUDIO_SOURCE_VOICE_UPLINK:
778 case AUDIO_SOURCE_VOICE_DOWNLINK:
779 case AUDIO_SOURCE_VOICE_CALL:
780 ALOGD("voice_conc:FOUND active input during call active: %d",activeDesc->mInputSource);
781 break;
782
783 case AUDIO_SOURCE_VOICE_COMMUNICATION:
784 if(prop_voip_enabled) {
785 ALOGD("voice_conc:CLOSING VoIP input source on call setup :%d ",activeDesc->mInputSource);
786 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
787 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
788 }
789 break;
790
791 default:
792 ALOGD("voice_conc:CLOSING input on call setup for inputSource: %d",activeDesc->mInputSource);
793 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
794 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
795 break;
796 }
797 }
798 } else if (prop_voip_enabled) {
799 audio_io_handle_t activeInput = mInputs.getActiveInput();
800 if (activeInput != 0) {
801 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
802 if (AUDIO_SOURCE_VOICE_COMMUNICATION == activeDesc->mInputSource) {
803 ALOGD("voice_conc:CLOSING VoIP on call setup : %d",activeDesc->mInputSource);
804 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
805 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
806 }
807 }
808 }
809 if (prop_playback_enabled) {
810 // Move tracks associated to this strategy from previous output to new output
811 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
812 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
813 if (i == AUDIO_STREAM_PATCH) {
814 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
815 continue;
816 }
817 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
818 if ((AUDIO_STREAM_MUSIC == i) ||
819 (AUDIO_STREAM_VOICE_CALL == i) ) {
820 ALOGD("voice_conc:Invalidate stream type %d", i);
821 mpClientInterface->invalidateStream((audio_stream_type_t)i);
822 }
823 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
824 ALOGD("voice_conc:Invalidate stream type %d", i);
825 mpClientInterface->invalidateStream((audio_stream_type_t)i);
826 }
827 }
828 }
829
830 for (size_t i = 0; i < mOutputs.size(); i++) {
831 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
832 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
833 ALOGD("voice_conc:ouput desc / profile is NULL");
834 continue;
835 }
836
Sidipotu Ashok738f19e2016-12-07 15:16:01 +0530837 bool isFastFallBackNeeded =
838 ((AUDIO_OUTPUT_FLAG_DEEP_BUFFER | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT_PCM) & outputDesc->mProfile->mFlags);
839
840 if ((AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) && isFastFallBackNeeded) {
841 if (((!outputDesc->isDuplicated() && outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY))
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530842 && prop_playback_enabled) {
843 ALOGD("voice_conc:calling suspendOutput on call mode for primary output");
844 mpClientInterface->suspendOutput(mOutputs.keyAt(i));
845 } //Close compress all sessions
846 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
847 && prop_playback_enabled) {
848 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
849 closeOutput(mOutputs.keyAt(i));
850 }
851 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_VOIP_RX)
852 && prop_voip_enabled) {
853 ALOGD("voice_conc:calling closeOutput on call mode for DIRECT output");
854 closeOutput(mOutputs.keyAt(i));
855 }
856 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
kunleiz0b55ec72016-11-10 16:23:58 +0800857 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_VOIP_RX) {
858 if (prop_voip_enabled) {
859 ALOGD("voice_conc:calling closeOutput on call mode for DIRECT output");
860 closeOutput(mOutputs.keyAt(i));
861 }
862 }
863 else if (prop_playback_enabled
864 && (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530865 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
866 closeOutput(mOutputs.keyAt(i));
867 }
868 }
869 }
Ashish Jaine4a22d52016-06-22 11:55:08 +0530870 // If effects where present on any of the above closed outputs,
871 // audioflinger moved them to the primary output by default
872 // move them back to the appropriate output.
873 moveGlobalEffect();
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530874 }
875
876 if ((AUDIO_MODE_IN_CALL == oldState || AUDIO_MODE_IN_COMMUNICATION == oldState) &&
877 (AUDIO_MODE_NORMAL == state) && prop_playback_enabled && mvoice_call_state) {
878 ALOGD("voice_conc:EXITING from call mode oldState :: %d state::%d \n",oldState, state);
879 mvoice_call_state = 0;
880 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
881 //restore PCM (deep-buffer) output after call termination
882 for (size_t i = 0; i < mOutputs.size(); i++) {
883 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
884 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
885 ALOGD("voice_conc:ouput desc / profile is NULL");
886 continue;
887 }
888 if (!outputDesc->isDuplicated() && outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
889 ALOGD("voice_conc:calling restoreOutput after call mode for primary output");
890 mpClientInterface->restoreOutput(mOutputs.keyAt(i));
891 }
892 }
893 }
894 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
895 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
896 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
897 if (i == AUDIO_STREAM_PATCH) {
898 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
899 continue;
900 }
901 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
902 if ((AUDIO_STREAM_MUSIC == i) ||
903 (AUDIO_STREAM_VOICE_CALL == i) ) {
904 mpClientInterface->invalidateStream((audio_stream_type_t)i);
905 }
906 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
907 mpClientInterface->invalidateStream((audio_stream_type_t)i);
908 }
909 }
910 }
911
912#endif
Preetam Singh Ranawat96afc162016-08-25 14:35:02 +0530913
914 sp<SwAudioOutputDescriptor> outputDesc = NULL;
915 for (size_t i = 0; i < mOutputs.size(); i++) {
916 outputDesc = mOutputs.valueAt(i);
917 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
918 ALOGD("voice_conc:ouput desc / profile is NULL");
919 continue;
920 }
921
922 if (property_get_bool("voice.dsd.playback.conc.disabled", true) &&
923 (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
924 (outputDesc->mFormat == AUDIO_FORMAT_DSD)) {
925 ALOGD("voice_conc:calling closeOutput on call mode for DSD COMPRESS output");
926 closeOutput(mOutputs.keyAt(i));
927 // call invalidate for music, so that DSD compress will fallback to deep-buffer.
928 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
929 }
930
931 }
932
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530933#ifdef RECORD_PLAY_CONCURRENCY
934 char recConcPropValue[PROPERTY_VALUE_MAX];
935 bool prop_rec_play_enabled = false;
936
937 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
938 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
939 }
940 if (prop_rec_play_enabled) {
941 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
942 ALOGD("phone state changed to MODE_IN_COMM invlaidating music and voice streams");
943 // call invalidate for voice streams, so that it can use deepbuffer with VoIP out device from HAL
944 mpClientInterface->invalidateStream(AUDIO_STREAM_VOICE_CALL);
945 // call invalidate for music, so that compress will fallback to deep-buffer with VoIP out device
946 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
947
948 // close compress output to make sure session will be closed before timeout(60sec)
949 for (size_t i = 0; i < mOutputs.size(); i++) {
950
951 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
952 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
953 ALOGD("ouput desc / profile is NULL");
954 continue;
955 }
956
957 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
958 ALOGD("calling closeOutput on call mode for COMPRESS output");
959 closeOutput(mOutputs.keyAt(i));
960 }
961 }
Ashish Jaine4a22d52016-06-22 11:55:08 +0530962 // If effects where present on any of the above closed outputs,
963 // audioflinger moved them to the primary output by default
964 // move them back to the appropriate output.
965 moveGlobalEffect();
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530966 } else if ((oldState == AUDIO_MODE_IN_COMMUNICATION) &&
967 (mEngine->getPhoneState() == AUDIO_MODE_NORMAL)) {
968 // call invalidate for music so that music can fallback to compress
969 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
970 }
971 }
972#endif
973 mPrevPhoneState = oldState;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700974 int delayMs = 0;
975 if (isStateInCall(state)) {
976 nsecs_t sysTime = systemTime();
977 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530978 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700979 // mute media and sonification strategies and delay device switch by the largest
980 // latency of any output where either strategy is active.
981 // This avoid sending the ring tone or music tail into the earpiece or headset.
Sharad Sangle36781612015-05-28 16:15:16 +0530982 if ((isStrategyActive(desc, STRATEGY_MEDIA,
983 SONIFICATION_HEADSET_MUSIC_DELAY,
984 sysTime) ||
985 isStrategyActive(desc, STRATEGY_SONIFICATION,
986 SONIFICATION_HEADSET_MUSIC_DELAY,
987 sysTime)) &&
988 (delayMs < (int)desc->latency()*2)) {
989 delayMs = desc->latency()*2;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700990 }
Sharad Sangle36781612015-05-28 16:15:16 +0530991 setStrategyMute(STRATEGY_MEDIA, true, desc);
992 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700993 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Sharad Sangle36781612015-05-28 16:15:16 +0530994 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
995 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700996 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
997 }
998 }
999
Sharad Sangle36781612015-05-28 16:15:16 +05301000 if (hasPrimaryOutput()) {
1001 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
1002 // the device returned is not necessarily reachable via this output
1003 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
1004 // force routing command to audio hardware when ending call
1005 // even if no device change is needed
1006 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
1007 rxDevice = mPrimaryOutput->device();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001008 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001009
Sharad Sangle36781612015-05-28 16:15:16 +05301010 if (state == AUDIO_MODE_IN_CALL) {
1011 updateCallRouting(rxDevice, delayMs);
1012 } else if (oldState == AUDIO_MODE_IN_CALL) {
1013 if (mCallRxPatch != 0) {
1014 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
1015 mCallRxPatch.clear();
1016 }
1017 if (mCallTxPatch != 0) {
1018 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
1019 mCallTxPatch.clear();
1020 }
1021 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
1022 } else {
1023 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001024 }
1025 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301026 //update device for all non-primary outputs
1027 for (size_t i = 0; i < mOutputs.size(); i++) {
1028 audio_io_handle_t output = mOutputs.keyAt(i);
1029 if (output != mPrimaryOutput->mIoHandle) {
1030 newDevice = getNewOutputDevice(mOutputs.valueFor(output), false /*fromCache*/);
1031 setOutputDevice(mOutputs.valueFor(output), newDevice, (newDevice != AUDIO_DEVICE_NONE));
1032 }
1033 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001034 // if entering in call state, handle special case of active streams
1035 // pertaining to sonification strategy see handleIncallSonification()
1036 if (isStateInCall(state)) {
1037 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +05301038 for (size_t j = 0; j < mOutputs.size(); j++) {
1039 audio_io_handle_t curOutput = mOutputs.keyAt(j);
1040 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
1041 if (stream == AUDIO_STREAM_PATCH) {
1042 continue;
1043 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301044 handleIncallSonification((audio_stream_type_t)stream, true, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +05301045 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001046 }
Zhou Song58081b62016-02-24 13:10:55 +08001047
1048 // force reevaluating accessibility routing when call starts
1049 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001050 }
1051
1052 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
1053 if (state == AUDIO_MODE_RINGTONE &&
1054 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
1055 mLimitRingtoneVolume = true;
1056 } else {
1057 mLimitRingtoneVolume = false;
1058 }
1059}
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301060
1061void AudioPolicyManagerCustom::setForceUse(audio_policy_force_use_t usage,
1062 audio_policy_forced_cfg_t config)
1063{
Ashish Jain40cab3a2016-03-23 11:14:14 +05301064 ALOGD("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301065
1066 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
1067 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
1068 return;
1069 }
1070 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
1071 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
1072 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
1073
1074 // check for device and output changes triggered by new force usage
1075 checkA2dpSuspend();
1076 checkOutputForAllStrategies();
1077 updateDevicesAndOutputs();
Ashish Jain40cab3a2016-03-23 11:14:14 +05301078
Sharad Sanglec66b9ca2016-12-21 18:40:54 +05301079 /*audio policy: workaround for truncated touch sounds*/
1080 //FIXME: workaround for truncated touch sounds
1081 // to be removed when the problem is handled by system UI
1082 uint32_t delayMs = 0;
1083 uint32_t waitMs = 0;
1084 if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) {
1085 delayMs = TOUCH_SOUND_FIXED_DELAY_MS;
1086 }
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301087 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
1088 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
Sharad Sanglec66b9ca2016-12-21 18:40:54 +05301089 waitMs = updateCallRouting(newDevice, delayMs);
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301090 }
1091 // Use reverse loop to make sure any low latency usecases (generally tones)
1092 // are not routed before non LL usecases (generally music).
1093 // We can safely assume that LL output would always have lower index,
1094 // and use this work-around to avoid routing of output with music stream
1095 // from the context of short lived LL output.
1096 // Note: in case output's share backend(HAL sharing is implicit) all outputs
1097 // gets routing update while processing first output itself.
1098 for (size_t i = mOutputs.size(); i > 0; i--) {
1099 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i-1);
1100 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
Sharad Sanglec66b9ca2016-12-21 18:40:54 +05301101 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
1102 waitMs = setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE),
1103 delayMs);
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301104 }
1105 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
Sharad Sanglec66b9ca2016-12-21 18:40:54 +05301106 applyStreamVolumes(outputDesc, newDevice, waitMs, true);
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301107 }
1108 }
1109
1110 audio_io_handle_t activeInput = mInputs.getActiveInput();
1111 if (activeInput != 0) {
Ashish Jain40cab3a2016-03-23 11:14:14 +05301112 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
1113 audio_devices_t newDevice = getNewInputDevice(activeInput);
1114 // Force new input selection if the new device can not be reached via current input
1115 if (activeDesc->mProfile->getSupportedDevices().types() & (newDevice & ~AUDIO_DEVICE_BIT_IN)) {
1116 setInputDevice(activeInput, newDevice);
1117 } else {
1118 closeInput(activeInput);
1119 }
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301120 }
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301121}
1122
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301123status_t AudioPolicyManagerCustom::stopSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301124 audio_stream_type_t stream,
1125 bool forceDeviceUpdate)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001126{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301127 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1128 ALOGW("stopSource() invalid stream %d", stream);
1129 return INVALID_OPERATION;
1130 }
Sharad Sangle36781612015-05-28 16:15:16 +05301131 // always handle stream stop, check which stream type is stopping
1132 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001133
Sharad Sangle36781612015-05-28 16:15:16 +05301134 // handle special case for sonification while in call
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001135 if (isInCall()) {
Sharad Sangle36781612015-05-28 16:15:16 +05301136 if (outputDesc->isDuplicated()) {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301137 handleIncallSonification(stream, false, false, outputDesc->subOutput1()->mIoHandle);
1138 handleIncallSonification(stream, false, false, outputDesc->subOutput2()->mIoHandle);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001139 }
Sharad Sangle36781612015-05-28 16:15:16 +05301140 handleIncallSonification(stream, false, false, outputDesc->mIoHandle);
1141 }
1142
1143 if (outputDesc->mRefCount[stream] > 0) {
1144 // decrement usage count of this stream on the output
1145 outputDesc->changeRefCount(stream, -1);
1146
1147 // store time at which the stream was stopped - see isStreamActive()
1148 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
1149 outputDesc->mStopTime[stream] = systemTime();
Zhou Song5dcddc92015-09-21 14:36:57 +08001150 audio_devices_t prevDevice = outputDesc->device();
Sharad Sangle36781612015-05-28 16:15:16 +05301151 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
1152 // delay the device switch by twice the latency because stopOutput() is executed when
1153 // the track stop() command is received and at that time the audio track buffer can
1154 // still contain data that needs to be drained. The latency only covers the audio HAL
1155 // and kernel buffers. Also the latency does not always include additional delay in the
1156 // audio path (audio DSP, CODEC ...)
1157 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
1158
1159 // force restoring the device selection on other active outputs if it differs from the
1160 // one being selected for this output
1161 for (size_t i = 0; i < mOutputs.size(); i++) {
1162 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1163 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1164 if (desc != outputDesc &&
1165 desc->isActive() &&
1166 outputDesc->sharesHwModuleWith(desc) &&
1167 (newDevice != desc->device())) {
Sharad Sangle4509cef2015-08-19 20:47:12 +05301168 audio_devices_t dev = getNewOutputDevice(mOutputs.valueFor(curOutput), false /*fromCache*/);
Naresh Tanniru11393702016-09-14 11:25:37 +05301169 bool force = desc->device() != dev;
Zhou Song5dcddc92015-09-21 14:36:57 +08001170 uint32_t delayMs;
1171 if (dev == prevDevice) {
1172 delayMs = 0;
1173 } else {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301174 delayMs = outputDesc->latency()*2;
Zhou Song5dcddc92015-09-21 14:36:57 +08001175 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301176 setOutputDevice(desc,
1177 dev,
Naresh Tanniru11393702016-09-14 11:25:37 +05301178 force,
Zhou Song5dcddc92015-09-21 14:36:57 +08001179 delayMs);
Sharad Sanglec66b9ca2016-12-21 18:40:54 +05301180 /*audio policy: fix media volume after ringtone*/
1181 // re-apply device specific volume if not done by setOutputDevice()
1182 if (!force) {
1183 applyStreamVolumes(desc, dev, delayMs);
1184 }
Sharad Sangle36781612015-05-28 16:15:16 +05301185 }
1186 }
1187 // update the outputs if stopping one with a stream that can affect notification routing
1188 handleNotificationRoutingForStream(stream);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001189 }
Sharad Sangle36781612015-05-28 16:15:16 +05301190 return NO_ERROR;
1191 } else {
1192 ALOGW("stopOutput() refcount is already 0");
1193 return INVALID_OPERATION;
1194 }
1195}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001196
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301197status_t AudioPolicyManagerCustom::startSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301198 audio_stream_type_t stream,
1199 audio_devices_t device,
Lalit Kansaraf803ac52016-05-09 19:12:01 +05301200 const char *address,
Sharad Sangle36781612015-05-28 16:15:16 +05301201 uint32_t *delayMs)
1202{
1203 // cannot start playback of STREAM_TTS if any other output is being used
1204 uint32_t beaconMuteLatency = 0;
1205
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301206 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1207 ALOGW("startSource() invalid stream %d", stream);
1208 return INVALID_OPERATION;
1209 }
1210
Sharad Sangle36781612015-05-28 16:15:16 +05301211 *delayMs = 0;
1212 if (stream == AUDIO_STREAM_TTS) {
1213 ALOGV("\t found BEACON stream");
Ashish Jain40cab3a2016-03-23 11:14:14 +05301214 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301215 return INVALID_OPERATION;
1216 } else {
1217 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001218 }
Sharad Sangle36781612015-05-28 16:15:16 +05301219 } else {
1220 // some playback other than beacon starts
1221 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1222 }
1223
Zhou Song63f0b852016-12-20 14:35:06 +08001224 // force device change if the output is inactive and no audio patch is already present.
Ashish Jain40cab3a2016-03-23 11:14:14 +05301225 // check active before incrementing usage count
Zhou Song63f0b852016-12-20 14:35:06 +08001226 bool force = !outputDesc->isActive() &&
1227 (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE);
Ashish Jain40cab3a2016-03-23 11:14:14 +05301228
Sharad Sangle36781612015-05-28 16:15:16 +05301229 // increment usage count for this stream on the requested output:
1230 // NOTE that the usage count is the same for duplicated output and hardware output which is
1231 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1232 outputDesc->changeRefCount(stream, 1);
1233
1234 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
1235 // starting an output being rerouted?
1236 if (device == AUDIO_DEVICE_NONE) {
1237 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001238 }
Sharad Sangle36781612015-05-28 16:15:16 +05301239 routing_strategy strategy = getStrategy(stream);
1240 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
1241 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1242 (beaconMuteLatency > 0);
1243 uint32_t waitMs = beaconMuteLatency;
Sharad Sangle36781612015-05-28 16:15:16 +05301244 for (size_t i = 0; i < mOutputs.size(); i++) {
1245 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1246 if (desc != outputDesc) {
Zhou Song63f0b852016-12-20 14:35:06 +08001247 // force a device change if any other output is:
1248 // - managed by the same hw module
1249 // - has a current device selection that differs from selected device.
1250 // - supports currently selected device
1251 // - has an active audio patch
Sharad Sangle36781612015-05-28 16:15:16 +05301252 // In this case, the audio HAL must receive the new device selection so that it can
1253 // change the device currently selected by the other active output.
1254 if (outputDesc->sharesHwModuleWith(desc) &&
Zhou Song63f0b852016-12-20 14:35:06 +08001255 desc->device() != device &&
1256 desc->supportedDevices() & device &&
1257 desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) {
Sharad Sangle36781612015-05-28 16:15:16 +05301258 force = true;
1259 }
1260 // wait for audio on other active outputs to be presented when starting
1261 // a notification so that audio focus effect can propagate, or that a mute/unmute
1262 // event occurred for beacon
1263 uint32_t latency = desc->latency();
1264 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1265 waitMs = latency;
1266 }
1267 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001268 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05301269 uint32_t muteWaitMs;
Lalit Kansaraf803ac52016-05-09 19:12:01 +05301270 muteWaitMs = setOutputDevice(outputDesc, device, force, 0, NULL, address);
Sharad Sangle36781612015-05-28 16:15:16 +05301271
1272 // handle special case for sonification while in call
1273 if (isInCall()) {
1274 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001275 }
Sharad Sangle36781612015-05-28 16:15:16 +05301276
1277 // apply volume rules for current stream and device if necessary
1278 checkAndSetVolume(stream,
Ashish Jain40cab3a2016-03-23 11:14:14 +05301279 mVolumeCurves->getVolumeIndex(stream, device),
Sharad Sangle36781612015-05-28 16:15:16 +05301280 outputDesc,
1281 device);
1282
1283 // update the outputs if starting an output with a stream that can affect notification
1284 // routing
1285 handleNotificationRoutingForStream(stream);
1286
1287 // force reevaluating accessibility routing when ringtone or alarm starts
1288 if (strategy == STRATEGY_SONIFICATION) {
1289 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1290 }
Sharad Sanglec66b9ca2016-12-21 18:40:54 +05301291 if (waitMs > muteWaitMs) {
1292 *delayMs = waitMs - muteWaitMs;
1293 }
1294
Sharad Sangle36781612015-05-28 16:15:16 +05301295 }
1296 else {
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001297 // handle special case for sonification while in call
1298 if (isInCall()) {
1299 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Sharad Sangle36781612015-05-28 16:15:16 +05301300 }
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001301 }
Sharad Sangle36781612015-05-28 16:15:16 +05301302 return NO_ERROR;
1303}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001304
Sharad Sangle36781612015-05-28 16:15:16 +05301305void AudioPolicyManagerCustom::handleIncallSonification(audio_stream_type_t stream,
1306 bool starting, bool stateChange,
1307 audio_io_handle_t output)
1308{
1309 if(!hasPrimaryOutput()) {
1310 return;
1311 }
1312 // no action needed for AUDIO_STREAM_PATCH stream type, it's for internal flinger tracks
1313 if (stream == AUDIO_STREAM_PATCH) {
1314 return;
1315 }
1316 // if the stream pertains to sonification strategy and we are in call we must
1317 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
1318 // in the device used for phone strategy and play the tone if the selected device does not
1319 // interfere with the device used for phone strategy
1320 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
1321 // many times as there are active tracks on the output
1322 const routing_strategy stream_strategy = getStrategy(stream);
1323 if ((stream_strategy == STRATEGY_SONIFICATION) ||
1324 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
1325 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
1326 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
1327 stream, starting, outputDesc->mDevice, stateChange);
1328 if (outputDesc->mRefCount[stream]) {
1329 int muteCount = 1;
1330 if (stateChange) {
1331 muteCount = outputDesc->mRefCount[stream];
1332 }
1333 if (audio_is_low_visibility(stream)) {
1334 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
1335 for (int i = 0; i < muteCount; i++) {
1336 setStreamMute(stream, starting, outputDesc);
1337 }
1338 } else {
1339 ALOGV("handleIncallSonification() high visibility");
1340 if (outputDesc->device() &
1341 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
1342 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
1343 for (int i = 0; i < muteCount; i++) {
1344 setStreamMute(stream, starting, outputDesc);
1345 }
1346 }
1347 if (starting) {
1348 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
1349 AUDIO_STREAM_VOICE_CALL);
1350 } else {
1351 mpClientInterface->stopTone();
1352 }
1353 }
1354 }
1355 }
1356}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001357
Sharad Sangle36781612015-05-28 16:15:16 +05301358void AudioPolicyManagerCustom::handleNotificationRoutingForStream(audio_stream_type_t stream) {
1359 switch(stream) {
1360 case AUDIO_STREAM_MUSIC:
1361 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
1362 updateDevicesAndOutputs();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001363 break;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001364 default:
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001365 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001366 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001367}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001368
Sharad Sangle36781612015-05-28 16:15:16 +05301369status_t AudioPolicyManagerCustom::checkAndSetVolume(audio_stream_type_t stream,
1370 int index,
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301371 const sp<AudioOutputDescriptor>& outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301372 audio_devices_t device,
1373 int delayMs, bool force)
1374{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301375 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1376 ALOGW("checkAndSetVolume() invalid stream %d", stream);
1377 return INVALID_OPERATION;
1378 }
Sharad Sangle36781612015-05-28 16:15:16 +05301379 // do not change actual stream volume if the stream is muted
1380 if (outputDesc->mMuteCount[stream] != 0) {
1381 ALOGVV("checkAndSetVolume() stream %d muted count %d",
1382 stream, outputDesc->mMuteCount[stream]);
1383 return NO_ERROR;
1384 }
1385 audio_policy_forced_cfg_t forceUseForComm =
1386 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
1387 // do not change in call volume if bluetooth is connected and vice versa
1388 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
1389 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
1390 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
1391 stream, forceUseForComm);
1392 return INVALID_OPERATION;
1393 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001394
Sharad Sangle36781612015-05-28 16:15:16 +05301395 if (device == AUDIO_DEVICE_NONE) {
1396 device = outputDesc->device();
1397 }
1398
1399 float volumeDb = computeVolume(stream, index, device);
1400 if (outputDesc->isFixedVolume(device)) {
1401 volumeDb = 0.0f;
1402 }
1403
1404 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
1405
1406 if (stream == AUDIO_STREAM_VOICE_CALL ||
1407 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
1408 float voiceVolume;
1409 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
1410 if (stream == AUDIO_STREAM_VOICE_CALL) {
Ashish Jain40cab3a2016-03-23 11:14:14 +05301411 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Sharad Sangle36781612015-05-28 16:15:16 +05301412 } else {
1413 voiceVolume = 1.0;
1414 }
1415
1416 if (voiceVolume != mLastVoiceVolume && ((outputDesc == mPrimaryOutput) ||
1417 isDirectOutput(outputDesc->mIoHandle) || device & AUDIO_DEVICE_OUT_ALL_USB)) {
1418 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
1419 mLastVoiceVolume = voiceVolume;
1420 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301421#ifdef FM_POWER_OPT
1422 } else if (stream == AUDIO_STREAM_MUSIC && hasPrimaryOutput() &&
Haynes Mathew George603ae9b2015-12-21 17:23:59 -08001423 outputDesc == mPrimaryOutput && mFMIsActive) {
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001424 /* Avoid unnecessary set_parameter calls as it puts the primary
1425 outputs FastMixer in HOT_IDLE leading to breaks in audio */
1426 if (volumeDb != mPrevFMVolumeDb) {
1427 mPrevFMVolumeDb = volumeDb;
1428 AudioParameter param = AudioParameter();
1429 param.addFloat(String8("fm_volume"), Volume::DbToAmpl(volumeDb));
Mingming Yin1433cc62016-01-04 14:58:00 -08001430 //Double delayMs to avoid sound burst while device switch.
1431 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString(), delayMs*2);
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001432 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301433#endif /* FM_POWER_OPT end */
Sharad Sangle36781612015-05-28 16:15:16 +05301434 }
1435
1436 return NO_ERROR;
1437}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001438
Sharad Sangle36781612015-05-28 16:15:16 +05301439bool AudioPolicyManagerCustom::isDirectOutput(audio_io_handle_t output) {
1440 for (size_t i = 0; i < mOutputs.size(); i++) {
1441 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1442 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1443 if ((curOutput == output) && (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
1444 return true;
1445 }
1446 }
1447 return false;
1448}
vivek mehta0ea887a2015-08-26 14:01:20 -07001449
Sharad Sangle75170722016-11-21 16:21:06 +05301450bool static tryForDirectPCM(int bitWidth, audio_output_flags_t *flags, uint32_t samplingRate)
Ashish Jainac510a72016-04-28 12:22:58 +05301451{
Ashish Jain771d02f2016-10-27 22:35:46 +05301452 bool playerDirectPCM = false; // Output request for Track created by mediaplayer
1453 bool trackDirectPCM = false; // Output request for track created by other apps
Ashish Jain27564312016-11-14 14:58:06 +05301454 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
Ashish Jainac510a72016-04-28 12:22:58 +05301455
Ashish Jain771d02f2016-10-27 22:35:46 +05301456 // Direct PCM is allowed only if
1457 // In case of mediaPlayer playback
1458 // 16 bit direct pcm or 24bit direct PCM property is set and
1459 // the FLAG requested is DIRECT_PCM ( NuPlayer case) or
1460 // In case of AudioTracks created by apps
1461 // track offload is enabled and FLAG requested is FLAG_NONE.
1462
Ashish Jain27564312016-11-14 14:58:06 +05301463 if (offloadDisabled) {
1464 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
1465 }
1466
Ashish Jain771d02f2016-10-27 22:35:46 +05301467 if (*flags == AUDIO_OUTPUT_FLAG_DIRECT_PCM) {
1468 if (bitWidth == 24 || bitWidth == 32)
1469 playerDirectPCM =
1470 property_get_bool("audio.offload.pcm.24bit.enable", false);
1471 else
1472 playerDirectPCM =
1473 property_get_bool("audio.offload.pcm.16bit.enable", false);
1474 // Reset flag to NONE so that we can still reuse direct pcm criteria check
1475 // in getOutputforDevice
1476 *flags = AUDIO_OUTPUT_FLAG_NONE;
Sharad Sangle75170722016-11-21 16:21:06 +05301477 } else if ((*flags == AUDIO_OUTPUT_FLAG_NONE) && (samplingRate % SAMPLE_RATE_8000 == 0)) {
Ashish Jain771d02f2016-10-27 22:35:46 +05301478 trackDirectPCM = property_get_bool("audio.offload.track.enable", true);
1479 }
1480
Ashish Jain27564312016-11-14 14:58:06 +05301481 ALOGI("Direct PCM %s for this request",
1482 (!offloadDisabled && (trackDirectPCM || playerDirectPCM))?"can be enabled":"is disabled");
1483
1484 return (!offloadDisabled && (trackDirectPCM || playerDirectPCM));
Ashish Jainac510a72016-04-28 12:22:58 +05301485}
1486
vivek mehta0ea887a2015-08-26 14:01:20 -07001487status_t AudioPolicyManagerCustom::getOutputForAttr(const audio_attributes_t *attr,
1488 audio_io_handle_t *output,
1489 audio_session_t session,
1490 audio_stream_type_t *stream,
1491 uid_t uid,
1492 uint32_t samplingRate,
1493 audio_format_t format,
1494 audio_channel_mask_t channelMask,
1495 audio_output_flags_t flags,
1496 audio_port_handle_t selectedDeviceId,
1497 const audio_offload_info_t *offloadInfo)
1498{
1499 audio_offload_info_t tOffloadInfo = AUDIO_INFO_INITIALIZER;
1500
Ashish Jainac510a72016-04-28 12:22:58 +05301501 uint32_t bitWidth = (audio_bytes_per_sample(format) * 8);
Alexy Joseph0ac09032015-10-16 15:57:53 -07001502
Alexy Joseph0ac09032015-10-16 15:57:53 -07001503
Sharad Sangle75170722016-11-21 16:21:06 +05301504 if (tryForDirectPCM(bitWidth, &flags, samplingRate) &&
Ashish Jain27564312016-11-14 14:58:06 +05301505 (offloadInfo == NULL)) {
vivek mehta0ea887a2015-08-26 14:01:20 -07001506
vivek mehta0ea887a2015-08-26 14:01:20 -07001507 tOffloadInfo.sample_rate = samplingRate;
1508 tOffloadInfo.channel_mask = channelMask;
1509 tOffloadInfo.format = format;
1510 tOffloadInfo.stream_type = *stream;
Ashish Jainac510a72016-04-28 12:22:58 +05301511 tOffloadInfo.bit_width = bitWidth;
vivek mehta0ea887a2015-08-26 14:01:20 -07001512 if (attr != NULL) {
1513 ALOGV("found attribute .. setting usage %d ", attr->usage);
1514 tOffloadInfo.usage = attr->usage;
1515 } else {
Alexy Joseph0ac09032015-10-16 15:57:53 -07001516 ALOGI("%s:: attribute is NULL .. no usage set", __func__);
vivek mehta0ea887a2015-08-26 14:01:20 -07001517 }
1518 offloadInfo = &tOffloadInfo;
1519 }
1520
1521 return AudioPolicyManager::getOutputForAttr(attr, output, session, stream,
1522 (uid_t)uid, (uint32_t)samplingRate,
1523 format, (audio_channel_mask_t)channelMask,
1524 flags, (audio_port_handle_t)selectedDeviceId,
1525 offloadInfo);
1526}
1527
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001528audio_io_handle_t AudioPolicyManagerCustom::getOutputForDevice(
1529 audio_devices_t device,
Sharad Sangle36781612015-05-28 16:15:16 +05301530 audio_session_t session __unused,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001531 audio_stream_type_t stream,
1532 uint32_t samplingRate,
1533 audio_format_t format,
1534 audio_channel_mask_t channelMask,
1535 audio_output_flags_t flags,
1536 const audio_offload_info_t *offloadInfo)
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001537{
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001538 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001539 status_t status;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001540
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001541#ifdef AUDIO_POLICY_TEST
1542 if (mCurOutput != 0) {
1543 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1544 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1545
1546 if (mTestOutputs[mCurOutput] == 0) {
1547 ALOGV("getOutput() opening test output");
Sharad Sangle36781612015-05-28 16:15:16 +05301548 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
1549 mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001550 outputDesc->mDevice = mTestDevice;
1551 outputDesc->mLatency = mTestLatencyMs;
1552 outputDesc->mFlags =
1553 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
1554 outputDesc->mRefCount[stream] = 0;
1555 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1556 config.sample_rate = mTestSamplingRate;
1557 config.channel_mask = mTestChannels;
1558 config.format = mTestFormat;
1559 if (offloadInfo != NULL) {
1560 config.offload_info = *offloadInfo;
1561 }
1562 status = mpClientInterface->openOutput(0,
1563 &mTestOutputs[mCurOutput],
1564 &config,
1565 &outputDesc->mDevice,
1566 String8(""),
1567 &outputDesc->mLatency,
1568 outputDesc->mFlags);
1569 if (status == NO_ERROR) {
1570 outputDesc->mSamplingRate = config.sample_rate;
1571 outputDesc->mFormat = config.format;
1572 outputDesc->mChannelMask = config.channel_mask;
1573 AudioParameter outputCmd = AudioParameter();
1574 outputCmd.addInt(String8("set_id"),mCurOutput);
1575 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1576 addOutput(mTestOutputs[mCurOutput], outputDesc);
1577 }
1578 }
1579 return mTestOutputs[mCurOutput];
1580 }
1581#endif //AUDIO_POLICY_TEST
Sharad Sangle36781612015-05-28 16:15:16 +05301582 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1583 (stream != AUDIO_STREAM_MUSIC)) {
1584 // compress should not be used for non-music streams
1585 ALOGE("Offloading only allowed with music stream");
1586 return 0;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301587 }
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301588
1589 if ((stream == AUDIO_STREAM_VOICE_CALL) &&
1590 (channelMask == 1) &&
Shiv Maliyappanahallid77d3b32016-02-02 13:45:50 -08001591 (samplingRate == 8000 || samplingRate == 16000 ||
1592 samplingRate == 32000 || samplingRate == 48000)) {
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301593 // Allow Voip direct output only if:
1594 // audio mode is MODE_IN_COMMUNCATION; AND
1595 // voip output is not opened already; AND
1596 // requested sample rate matches with that of voip input stream (if opened already)
1597 int value = 0;
1598 uint32_t mode = 0, voipOutCount = 1, voipSampleRate = 1;
1599 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1600 String8("audio_mode"));
1601 AudioParameter result = AudioParameter(valueStr);
1602 if (result.getInt(String8("audio_mode"), value) == NO_ERROR) {
1603 mode = value;
1604 }
1605
1606 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1607 String8("voip_out_stream_count"));
1608 result = AudioParameter(valueStr);
1609 if (result.getInt(String8("voip_out_stream_count"), value) == NO_ERROR) {
1610 voipOutCount = value;
1611 }
1612
1613 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1614 String8("voip_sample_rate"));
1615 result = AudioParameter(valueStr);
1616 if (result.getInt(String8("voip_sample_rate"), value) == NO_ERROR) {
1617 voipSampleRate = value;
1618 }
1619
1620 if ((mode == AUDIO_MODE_IN_COMMUNICATION) && (voipOutCount == 0) &&
1621 ((voipSampleRate == 0) || (voipSampleRate == samplingRate))) {
1622 if (audio_is_linear_pcm(format)) {
1623 char propValue[PROPERTY_VALUE_MAX] = {0};
1624 property_get("use.voice.path.for.pcm.voip", propValue, "0");
1625 bool voipPcmSysPropEnabled = !strncmp("true", propValue, sizeof("true"));
1626 if (voipPcmSysPropEnabled && (format == AUDIO_FORMAT_PCM_16_BIT)) {
Karthik Reddy Katta3fb9be72015-11-25 11:18:27 +05301627 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX |
1628 AUDIO_OUTPUT_FLAG_DIRECT);
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301629 ALOGD("Set VoIP and Direct output flags for PCM format");
1630 }
1631 }
1632 }
1633 }
1634
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301635#ifdef VOICE_CONCURRENCY
1636 char propValue[PROPERTY_VALUE_MAX];
1637 bool prop_play_enabled=false, prop_voip_enabled = false;
1638
1639 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1640 prop_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001641 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301642
1643 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1644 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1645 }
1646
Sidipotu Ashok738f19e2016-12-07 15:16:01 +05301647 bool isDeepBufferFallBackNeeded =
1648 ((AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT_PCM) & flags);
1649 bool isFastFallBackNeeded =
1650 ((AUDIO_OUTPUT_FLAG_DEEP_BUFFER | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT_PCM) & flags);
1651
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301652 if (prop_play_enabled && mvoice_call_state) {
1653 //check if voice call is active / running in background
1654 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1655 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1656 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1657 {
1658 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1659 if(prop_voip_enabled) {
1660 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1661 flags );
1662 return 0;
1663 }
1664 }
1665 else {
Sidipotu Ashok738f19e2016-12-07 15:16:01 +05301666 if (isFastFallBackNeeded &&
1667 (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301668 ALOGD("voice_conc:IN call mode adding ULL flags .. flags: %x ", flags );
1669 flags = AUDIO_OUTPUT_FLAG_FAST;
Sidipotu Ashok738f19e2016-12-07 15:16:01 +05301670 } else if (isDeepBufferFallBackNeeded &&
1671 (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301672 if (AUDIO_STREAM_MUSIC == stream) {
1673 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1674 ALOGD("voice_conc:IN call mode adding deep-buffer flags %x ", flags );
1675 }
1676 else {
1677 flags = AUDIO_OUTPUT_FLAG_FAST;
1678 ALOGD("voice_conc:IN call mode adding fast flags %x ", flags );
1679 }
1680 }
1681 }
1682 }
1683 } else if (prop_voip_enabled && mvoice_call_state) {
1684 //check if voice call is active / running in background
1685 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1686 //return only ULL ouput
1687 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1688 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1689 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1690 {
1691 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1692 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1693 flags );
1694 return 0;
1695 }
1696 }
1697 }
1698#endif
1699#ifdef RECORD_PLAY_CONCURRENCY
1700 char recConcPropValue[PROPERTY_VALUE_MAX];
1701 bool prop_rec_play_enabled = false;
1702
1703 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
1704 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
1705 }
1706 if ((prop_rec_play_enabled) &&
Lalit Kansara33918092016-12-06 22:42:26 +05301707 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCountOnDevices() > 0))) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301708 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
1709 if (AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1710 // allow VoIP using voice path
1711 // Do nothing
1712 } else if((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1713 ALOGD("voice_conc:MODE_IN_COMM is setforcing deep buffer output for non ULL... flags: %x", flags);
1714 // use deep buffer path for all non ULL outputs
1715 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1716 }
1717 } else if ((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1718 ALOGD("voice_conc:Record mode is on forcing deep buffer output for non ULL... flags: %x ", flags);
1719 // use deep buffer path for all non ULL outputs
1720 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1721 }
1722 }
1723 if (prop_rec_play_enabled &&
1724 (stream == AUDIO_STREAM_ENFORCED_AUDIBLE)) {
1725 ALOGD("Record conc is on forcing ULL output for ENFORCED_AUDIBLE");
1726 flags = AUDIO_OUTPUT_FLAG_FAST;
1727 }
1728#endif
1729
Sharad Sangle4509cef2015-08-19 20:47:12 +05301730#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Sharad Sangle36781612015-05-28 16:15:16 +05301731 /*
1732 * WFD audio routes back to target speaker when starting a ringtone playback.
1733 * This is because primary output is reused for ringtone, so output device is
1734 * updated based on SONIFICATION strategy for both ringtone and music playback.
1735 * The same issue is not seen on remoted_submix HAL based WFD audio because
1736 * primary output is not reused and a new output is created for ringtone playback.
1737 * Issue is fixed by updating output flag to AUDIO_OUTPUT_FLAG_FAST when there is
1738 * a non-music stream playback on WFD, so primary output is not reused for ringtone.
1739 */
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001740 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
1741 if ((availableOutputDeviceTypes & AUDIO_DEVICE_OUT_PROXY)
1742 && (stream != AUDIO_STREAM_MUSIC)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301743 ALOGD("WFD audio: use OUTPUT_FLAG_FAST for non music stream. flags:%x", flags );
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001744 //For voip paths
1745 if(flags & AUDIO_OUTPUT_FLAG_DIRECT)
1746 flags = AUDIO_OUTPUT_FLAG_DIRECT;
1747 else //route every thing else to ULL path
1748 flags = AUDIO_OUTPUT_FLAG_FAST;
1749 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301750#endif
1751
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001752 // open a direct output if required by specified parameters
vivek mehta0ea887a2015-08-26 14:01:20 -07001753 // force direct flag if offload flag is set: offloading implies a direct output stream
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001754 // and all common behaviors are driven by checking only the direct flag
1755 // this should normally be set appropriately in the policy configuration file
1756 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1757 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1758 }
1759 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1760 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1761 }
vivek mehta0ea887a2015-08-26 14:01:20 -07001762
vivek mehta4b0d8192015-10-16 00:25:59 -07001763 bool forced_deep = false;
Sharad Sangle36781612015-05-28 16:15:16 +05301764 // only allow deep buffering for music stream type
1765 if (stream != AUDIO_STREAM_MUSIC) {
1766 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Sharad Sangle497aef82015-08-03 17:55:48 +05301767 } else if (/* stream == AUDIO_STREAM_MUSIC && */
1768 flags == AUDIO_OUTPUT_FLAG_NONE &&
1769 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
vivek mehtac984b992015-11-25 13:28:55 -08001770 forced_deep = true;
Sharad Sangle36781612015-05-28 16:15:16 +05301771 }
Sharad Sangle497aef82015-08-03 17:55:48 +05301772
Sharad Sangle36781612015-05-28 16:15:16 +05301773 if (stream == AUDIO_STREAM_TTS) {
1774 flags = AUDIO_OUTPUT_FLAG_TTS;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001775 }
1776
Zhou Song2b3d1552016-03-21 17:21:04 +08001777 // check if direct output for track offload already exits
1778 bool is_track_offload_active = false;
1779 for (size_t i = 0; i < mOutputs.size(); i++) {
1780 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1781 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) {
1782 is_track_offload_active = true;
1783 ALOGD("Track offload already active");
1784 break;
1785 }
1786 }
1787
vivek mehta4b0d8192015-10-16 00:25:59 -07001788 // Do offload magic here
vivek mehtac984b992015-11-25 13:28:55 -08001789 if ((flags == AUDIO_OUTPUT_FLAG_NONE) &&
1790 (stream == AUDIO_STREAM_MUSIC) &&
Zhou Song2b3d1552016-03-21 17:21:04 +08001791 (offloadInfo != NULL) && !is_track_offload_active &&
vivek mehta4b0d8192015-10-16 00:25:59 -07001792 ((offloadInfo->usage == AUDIO_USAGE_MEDIA) || (offloadInfo->usage == AUDIO_USAGE_GAME))) {
vivek mehtac984b992015-11-25 13:28:55 -08001793 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DIRECT_PCM);
vivek mehta4b0d8192015-10-16 00:25:59 -07001794 ALOGD("AudioCustomHAL --> Force Direct Flag .. flag (0x%x)", flags);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301795 }
1796
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001797 sp<IOProfile> profile;
1798
1799 // skip direct output selection if the request can obviously be attached to a mixed output
1800 // and not explicitly requested
vivek mehtac984b992015-11-25 13:28:55 -08001801 if (((flags & (AUDIO_OUTPUT_FLAG_DIRECT|AUDIO_OUTPUT_FLAG_DIRECT_PCM)) == 0) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301802 audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX &&
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001803 audio_channel_count_from_out_mask(channelMask) <= 2) {
1804 goto non_direct_output;
1805 }
1806
1807 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1808 // creating an offloaded track and tearing it down immediately after start when audioflinger
1809 // detects there is an active non offloadable effect.
1810 // FIXME: We should check the audio session here but we do not have it in this context.
1811 // This may prevent offloading in rare situations where effects are left active by apps
1812 // in the background.
Weiyin Jiang6bba3202016-03-03 14:49:36 +08001813 //
1814 // Supplementary annotation:
1815 // For sake of track offload introduced, we need a rollback for both compress offload
1816 // and track offload use cases.
1817 if ((flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD|AUDIO_OUTPUT_FLAG_DIRECT_PCM)) &&
1818 (mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
1819 ALOGD("non offloadable effect is enabled, try with non direct output");
1820 goto non_direct_output;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001821 }
1822
Weiyin Jiang6bba3202016-03-03 14:49:36 +08001823 profile = getProfileForDirectOutput(device,
1824 samplingRate,
1825 format,
1826 channelMask,
1827 (audio_output_flags_t)flags);
1828
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001829 if (profile != 0) {
vivek mehtac984b992015-11-25 13:28:55 -08001830
1831 if (!(flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301832 (profile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT_PCM)) {
vivek mehtac984b992015-11-25 13:28:55 -08001833 ALOGI("got Direct_PCM without requesting ... reject ");
1834 profile = NULL;
1835 goto non_direct_output;
1836 }
1837
Sharad Sangle36781612015-05-28 16:15:16 +05301838 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001839
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001840 // if multiple concurrent offload decode is supported
1841 // do no check for reuse and also don't close previous output if its offload
1842 // previous output will be closed during track destruction
1843 if (!(property_get_bool("audio.offload.multiple.enabled", false) &&
1844 ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0))) {
1845 for (size_t i = 0; i < mOutputs.size(); i++) {
1846 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1847 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1848 outputDesc = desc;
1849 // reuse direct output if currently open and configured with same parameters
1850 if ((samplingRate == outputDesc->mSamplingRate) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301851 audio_formats_match(format, outputDesc->mFormat) &&
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001852 (channelMask == outputDesc->mChannelMask)) {
1853 outputDesc->mDirectOpenCount++;
1854 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1855 return mOutputs.keyAt(i);
1856 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001857 }
1858 }
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001859 // close direct output if currently open and configured with different parameters
1860 if (outputDesc != NULL) {
1861 closeOutput(outputDesc->mIoHandle);
1862 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001863 }
Sharad Sangle36781612015-05-28 16:15:16 +05301864
1865 // if the selected profile is offloaded and no offload info was specified,
1866 // create a default one
1867 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
Ashish Jain40cab3a2016-03-23 11:14:14 +05301868 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
Sharad Sangle36781612015-05-28 16:15:16 +05301869 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1870 defaultOffloadInfo.sample_rate = samplingRate;
1871 defaultOffloadInfo.channel_mask = channelMask;
1872 defaultOffloadInfo.format = format;
1873 defaultOffloadInfo.stream_type = stream;
1874 defaultOffloadInfo.bit_rate = 0;
1875 defaultOffloadInfo.duration_us = -1;
1876 defaultOffloadInfo.has_video = true; // conservative
1877 defaultOffloadInfo.is_streaming = true; // likely
1878 offloadInfo = &defaultOffloadInfo;
1879 }
1880
1881 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001882 outputDesc->mDevice = device;
1883 outputDesc->mLatency = 0;
Sharad Sangle36781612015-05-28 16:15:16 +05301884 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001885 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1886 config.sample_rate = samplingRate;
1887 config.channel_mask = channelMask;
1888 config.format = format;
1889 if (offloadInfo != NULL) {
1890 config.offload_info = *offloadInfo;
1891 }
Sharad Sangle36781612015-05-28 16:15:16 +05301892 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001893 &output,
1894 &config,
1895 &outputDesc->mDevice,
1896 String8(""),
1897 &outputDesc->mLatency,
1898 outputDesc->mFlags);
1899
1900 // only accept an output with the requested parameters
1901 if (status != NO_ERROR ||
1902 (samplingRate != 0 && samplingRate != config.sample_rate) ||
Ashish Jain40cab3a2016-03-23 11:14:14 +05301903 (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) ||
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001904 (channelMask != 0 && channelMask != config.channel_mask)) {
1905 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1906 "format %d %d, channelMask %04x %04x", output, samplingRate,
1907 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1908 outputDesc->mChannelMask);
1909 if (output != AUDIO_IO_HANDLE_NONE) {
1910 mpClientInterface->closeOutput(output);
1911 }
Sharad Sangle36781612015-05-28 16:15:16 +05301912 // fall back to mixer output if possible when the direct output could not be open
Ashish Jain40cab3a2016-03-23 11:14:14 +05301913 if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) {
Sharad Sangle36781612015-05-28 16:15:16 +05301914 goto non_direct_output;
1915 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001916 return AUDIO_IO_HANDLE_NONE;
1917 }
1918 outputDesc->mSamplingRate = config.sample_rate;
1919 outputDesc->mChannelMask = config.channel_mask;
1920 outputDesc->mFormat = config.format;
1921 outputDesc->mRefCount[stream] = 0;
1922 outputDesc->mStopTime[stream] = 0;
1923 outputDesc->mDirectOpenCount = 1;
1924
1925 audio_io_handle_t srcOutput = getOutputForEffect();
1926 addOutput(output, outputDesc);
1927 audio_io_handle_t dstOutput = getOutputForEffect();
1928 if (dstOutput == output) {
Gao Jiedb057832015-11-12 08:51:22 +08001929#ifdef DOLBY_ENABLE
1930 status_t status = mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1931 if (status == NO_ERROR) {
1932 for (size_t i = 0; i < mEffects.size(); i++) {
1933 sp<EffectDescriptor> desc = mEffects.valueAt(i);
1934 if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX) {
1935 // update the mIo member of EffectDescriptor for the global effect
1936 ALOGV("%s updating mIo", __FUNCTION__);
1937 desc->mIo = dstOutput;
1938 }
1939 }
1940 } else {
1941 ALOGW("%s moveEffects from %d to %d failed", __FUNCTION__, srcOutput, dstOutput);
1942 }
1943#else // DOLBY_END
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001944 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
Gao Jiedb057832015-11-12 08:51:22 +08001945#endif // LINE_ADDED_BY_DOLBY
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001946 }
1947 mPreviousOutputs = mOutputs;
1948 ALOGV("getOutput() returns new direct output %d", output);
1949 mpClientInterface->onAudioPortListUpdate();
1950 return output;
1951 }
1952
1953non_direct_output:
Ashish Jain5fe12412016-04-18 18:44:17 +05301954
1955 // A request for HW A/V sync cannot fallback to a mixed output because time
1956 // stamps are embedded in audio data
1957 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1958 return AUDIO_IO_HANDLE_NONE;
1959 }
1960
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001961 // ignoring channel mask due to downmix capability in mixer
1962
1963 // open a non direct output
1964
1965 // for non direct outputs, only PCM is supported
1966 if (audio_is_linear_pcm(format)) {
1967 // get which output is suitable for the specified stream. The actual
1968 // routing change will happen when startOutput() will be called
1969 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1970
1971 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1972 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
Ashish Jain660d3112016-06-20 10:16:51 +05301973
1974 if (forced_deep) {
1975 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
1976 ALOGI("setting force DEEP buffer now ");
1977 } else if(flags == AUDIO_OUTPUT_FLAG_NONE) {
1978 // no deep buffer playback is requested hence fallback to primary
1979 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_PRIMARY);
1980 ALOGI("FLAG None hence request for a primary output");
1981 }
1982
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001983 output = selectOutput(outputs, flags, format);
1984 }
1985 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1986 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1987
vivek mehta0ea887a2015-08-26 14:01:20 -07001988 ALOGV("getOutputForDevice() returns output %d", output);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001989
1990 return output;
1991}
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301992
1993status_t AudioPolicyManagerCustom::getInputForAttr(const audio_attributes_t *attr,
1994 audio_io_handle_t *input,
1995 audio_session_t session,
1996 uid_t uid,
1997 uint32_t samplingRate,
1998 audio_format_t format,
1999 audio_channel_mask_t channelMask,
2000 audio_input_flags_t flags,
2001 audio_port_handle_t selectedDeviceId,
2002 input_type_t *inputType)
2003{
Ashish Jain40cab3a2016-03-23 11:14:14 +05302004 audio_source_t inputSource;
2005 inputSource = attr->source;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302006#ifdef VOICE_CONCURRENCY
2007
2008 char propValue[PROPERTY_VALUE_MAX];
2009 bool prop_rec_enabled=false, prop_voip_enabled = false;
2010
2011 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
2012 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
2013 }
2014
2015 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
2016 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
2017 }
2018
2019 if (prop_rec_enabled && mvoice_call_state) {
2020 //check if voice call is active / running in background
2021 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
2022 //Need to block input request
2023 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
2024 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
2025 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
2026 {
2027 switch(inputSource) {
2028 case AUDIO_SOURCE_VOICE_UPLINK:
2029 case AUDIO_SOURCE_VOICE_DOWNLINK:
2030 case AUDIO_SOURCE_VOICE_CALL:
2031 ALOGD("voice_conc:Creating input during incall mode for inputSource: %d",
2032 inputSource);
2033 break;
2034
2035 case AUDIO_SOURCE_VOICE_COMMUNICATION:
2036 if(prop_voip_enabled) {
2037 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
2038 inputSource);
2039 return NO_INIT;
2040 }
2041 break;
2042 default:
2043 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
2044 inputSource);
2045 return NO_INIT;
2046 }
2047 }
2048 }//check for VoIP flag
2049 else if(prop_voip_enabled && mvoice_call_state) {
2050 //check if voice call is active / running in background
2051 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
2052 //Need to block input request
2053 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
2054 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
2055 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
2056 {
2057 if(inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION) {
2058 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
2059 return NO_INIT;
2060 }
2061 }
2062 }
2063
2064#endif
2065
2066 return AudioPolicyManager::getInputForAttr(attr,
2067 input,
2068 session,
2069 uid,
2070 samplingRate,
2071 format,
2072 channelMask,
2073 flags,
2074 selectedDeviceId,
2075 inputType);
2076}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08002077
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302078status_t AudioPolicyManagerCustom::startInput(audio_io_handle_t input,
2079 audio_session_t session)
2080{
2081 ALOGV("startInput() input %d", input);
2082 ssize_t index = mInputs.indexOfKey(input);
2083 if (index < 0) {
2084 ALOGW("startInput() unknown input %d", input);
2085 return BAD_VALUE;
2086 }
2087 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
2088
Ashish Jain40cab3a2016-03-23 11:14:14 +05302089 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
2090 if (audioSession == 0) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302091 ALOGW("startInput() unknown session %d on input %d", session, input);
2092 return BAD_VALUE;
2093 }
2094
2095 // virtual input devices are compatible with other input devices
2096 if (!is_virtual_input_device(inputDesc->mDevice)) {
2097
2098 // for a non-virtual input device, check if there is another (non-virtual) active input
2099 audio_io_handle_t activeInput = mInputs.getActiveInput();
2100 if (activeInput != 0 && activeInput != input) {
2101
2102 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
2103 // otherwise the active input continues and the new input cannot be started.
2104 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302105 if ((activeDesc->inputSource() == AUDIO_SOURCE_HOTWORD) &&
2106 !activeDesc->hasPreemptedSession(session)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302107 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302108 //FIXME: consider all active sessions
2109 AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions();
2110 audio_session_t activeSession = activeSessions.keyAt(0);
2111 SortedVector<audio_session_t> sessions =
2112 activeDesc->getPreemptedSessions();
2113 sessions.add(activeSession);
2114 inputDesc->setPreemptedSessions(sessions);
2115 stopInput(activeInput, activeSession);
2116 releaseInput(activeInput, activeSession);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302117 } else {
2118 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
2119 return INVALID_OPERATION;
2120 }
2121 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05302122 // Do not allow capture if an active voice call is using a software patch and
2123 // the call TX source device is on the same HW module.
2124 // FIXME: would be better to refine to only inputs whose profile connects to the
2125 // call TX device but this information is not in the audio patch
2126 if (mCallTxPatch != 0 &&
2127 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
2128 return INVALID_OPERATION;
2129 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302130 }
2131
2132 // Routing?
2133 mInputRoutes.incRouteActivity(session);
2134#ifdef RECORD_PLAY_CONCURRENCY
2135 mIsInputRequestOnProgress = true;
2136
2137 char getPropValue[PROPERTY_VALUE_MAX];
2138 bool prop_rec_play_enabled = false;
2139
2140 if (property_get("rec.playback.conc.disabled", getPropValue, NULL)) {
2141 prop_rec_play_enabled = atoi(getPropValue) || !strncmp("true", getPropValue, 4);
2142 }
2143
Lalit Kansara33918092016-12-06 22:42:26 +05302144 if ((prop_rec_play_enabled) &&(mInputs.activeInputsCountOnDevices() == 0)){
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302145 // send update to HAL on record playback concurrency
2146 AudioParameter param = AudioParameter();
2147 param.add(String8("rec_play_conc_on"), String8("true"));
2148 ALOGD("startInput() setParameters rec_play_conc is setting to ON ");
2149 mpClientInterface->setParameters(0, param.toString());
2150
2151 // Call invalidate to reset all opened non ULL audio tracks
2152 // Move tracks associated to this strategy from previous output to new output
2153 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
2154 // Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder)
Sharad Sangle4509cef2015-08-19 20:47:12 +05302155 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE && (i != AUDIO_STREAM_PATCH))) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302156 ALOGD("Invalidate on releaseInput for stream :: %d ", i);
2157 //FIXME see fixme on name change
2158 mpClientInterface->invalidateStream((audio_stream_type_t)i);
2159 }
2160 }
2161 // close compress tracks
2162 for (size_t i = 0; i < mOutputs.size(); i++) {
2163 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
2164 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
2165 ALOGD("ouput desc / profile is NULL");
2166 continue;
2167 }
2168 if (outputDesc->mProfile->mFlags
2169 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2170 // close compress sessions
2171 ALOGD("calling closeOutput on record conc for COMPRESS output");
2172 closeOutput(mOutputs.keyAt(i));
2173 }
2174 }
Ashish Jaine4a22d52016-06-22 11:55:08 +05302175 // If effects where present on any of the above closed outputs,
2176 // audioflinger moved them to the primary output by default
2177 // move them back to the appropriate output.
2178 moveGlobalEffect();
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302179 }
2180#endif
2181
Ashish Jain40cab3a2016-03-23 11:14:14 +05302182 if (!inputDesc->isActive() || mInputRoutes.hasRouteChanged(session)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302183 // if input maps to a dynamic policy with an activity listener, notify of state change
Ashish Jain630c5e12016-04-18 11:50:21 +05302184 if ((inputDesc->mPolicyMix != NULL)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302185 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Ashish Jain5fe12412016-04-18 18:44:17 +05302186 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302187 MIX_STATE_MIXING);
Ashish Jain630c5e12016-04-18 11:50:21 +05302188 }
Sharad Sanglec66b9ca2016-12-21 18:40:54 +05302189 /*audio policy: fix capture indication to sound trigger service*/
2190 // indicate active capture to sound trigger service if starting capture from a mic on
2191 // primary HW module
2192 audio_devices_t device = getNewInputDevice(input);
2193 audio_devices_t primaryInputDevices = availablePrimaryInputDevices();
2194 if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) &&
2195 mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302196 SoundTrigger::setCaptureState(true);
2197 }
Sharad Sanglec66b9ca2016-12-21 18:40:54 +05302198 setInputDevice(input, device, true /* force */);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302199
2200 // automatically enable the remote submix output when input is started if not
2201 // used by a policy mix of type MIX_TYPE_RECORDERS
2202 // For remote submix (a virtual device), we open only one input per capture request.
2203 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
2204 String8 address = String8("");
2205 if (inputDesc->mPolicyMix == NULL) {
2206 address = String8("0");
Ashish Jain630c5e12016-04-18 11:50:21 +05302207 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
Ashish Jain5fe12412016-04-18 18:44:17 +05302208 address = inputDesc->mPolicyMix->mDeviceAddress;
Ashish Jain630c5e12016-04-18 11:50:21 +05302209 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302210 if (address != "") {
2211 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2212 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2213 address, "remote-submix");
2214 }
2215 }
2216 }
2217
Ashish Jain40cab3a2016-03-23 11:14:14 +05302218 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302219
Ashish Jain40cab3a2016-03-23 11:14:14 +05302220 audioSession->changeActiveCount(1);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302221#ifdef RECORD_PLAY_CONCURRENCY
2222 mIsInputRequestOnProgress = false;
2223#endif
2224 return NO_ERROR;
2225}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08002226
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302227status_t AudioPolicyManagerCustom::stopInput(audio_io_handle_t input,
2228 audio_session_t session)
2229{
2230 status_t status;
2231 status = AudioPolicyManager::stopInput(input, session);
2232#ifdef RECORD_PLAY_CONCURRENCY
2233 char propValue[PROPERTY_VALUE_MAX];
2234 bool prop_rec_play_enabled = false;
2235
2236 if (property_get("rec.playback.conc.disabled", propValue, NULL)) {
2237 prop_rec_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
2238 }
2239
Lalit Kansara33918092016-12-06 22:42:26 +05302240 if ((prop_rec_play_enabled) && (mInputs.activeInputsCountOnDevices() == 0)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302241
2242 //send update to HAL on record playback concurrency
2243 AudioParameter param = AudioParameter();
2244 param.add(String8("rec_play_conc_on"), String8("false"));
2245 ALOGD("stopInput() setParameters rec_play_conc is setting to OFF ");
2246 mpClientInterface->setParameters(0, param.toString());
2247
2248 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
2249 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
2250 //Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder stop tone)
2251 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE) && (i != AUDIO_STREAM_PATCH)) {
2252 ALOGD(" Invalidate on stopInput for stream :: %d ", i);
2253 //FIXME see fixme on name change
2254 mpClientInterface->invalidateStream((audio_stream_type_t)i);
2255 }
2256 }
2257 }
2258#endif
2259 return status;
2260}
2261
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302262void AudioPolicyManagerCustom::closeAllInputs() {
2263 bool patchRemoved = false;
2264
Dhanalakshmi Siddanibfeca7f2015-10-08 15:17:11 +05302265 for(size_t input_index = mInputs.size(); input_index > 0; input_index--) {
2266 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index-1);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302267 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302268 if (patch_index >= 0) {
2269 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302270 status_t status;
2271 status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302272 mAudioPatches.removeItemsAt(patch_index);
2273 patchRemoved = true;
2274 }
Haynes Mathew George2e4655a2017-01-05 15:01:53 -08002275 mpClientInterface->closeInput(mInputs.keyAt(input_index-1));
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302276 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05302277 mInputs.clear();
2278 SoundTrigger::setCaptureState(false);
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302279 nextAudioPortGeneration();
2280
2281 if (patchRemoved) {
2282 mpClientInterface->onAudioPatchListUpdate();
2283 }
2284}
2285
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302286AudioPolicyManagerCustom::AudioPolicyManagerCustom(AudioPolicyClientInterface *clientInterface)
Sharad Sangle4509cef2015-08-19 20:47:12 +05302287 : AudioPolicyManager(clientInterface),
2288 mHdmiAudioDisabled(false),
2289 mHdmiAudioEvent(false),
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08002290 mPrevPhoneState(0),
Haynes Mathew George603ae9b2015-12-21 17:23:59 -08002291 mPrevFMVolumeDb(0.0f),
2292 mFMIsActive(false)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302293{
Ashish Jain40cab3a2016-03-23 11:14:14 +05302294
Satya Krishna Pindiproli8f83f102016-06-21 17:56:14 +05302295#ifdef USE_XML_AUDIO_POLICY_CONF
2296 ALOGD("USE_XML_AUDIO_POLICY_CONF is TRUE");
2297#else
2298 ALOGD("USE_XML_AUDIO_POLICY_CONF is FALSE");
2299#endif
2300
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302301#ifdef RECORD_PLAY_CONCURRENCY
2302 mIsInputRequestOnProgress = false;
2303#endif
2304
2305
2306#ifdef VOICE_CONCURRENCY
2307 mFallBackflag = getFallBackPath();
2308#endif
2309}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07002310}