blob: 1b113ed820d929ecaa6b821327edaa3ea48e851a [file] [log] [blame]
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001/*
Weiyin Jiangd211ef52016-02-16 15:55:11 +08002 * Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07003 * Not a contribution.
4 *
5 * Copyright (C) 2009 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Gao Jiedb057832015-11-12 08:51:22 +080018 *
19 * This file was modified by Dolby Laboratories, Inc. The portions of the
20 * code that are surrounded by "DOLBY..." are copyrighted and
21 * licensed separately, as follows:
22 *
23 * (C) 2015 Dolby Laboratories, Inc.
24 *
25 * Licensed under the Apache License, Version 2.0 (the "License");
26 * you may not use this file except in compliance with the License.
27 * You may obtain a copy of the License at
28 *
29 * http://www.apache.org/licenses/LICENSE-2.0
30 *
31 * Unless required by applicable law or agreed to in writing, software
32 * distributed under the License is distributed on an "AS IS" BASIS,
33 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34 * See the License for the specific language governing permissions and
35 * limitations under the License.
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070036 */
37
Sharad Sangle36781612015-05-28 16:15:16 +053038#define LOG_TAG "AudioPolicyManagerCustom"
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070039//#define LOG_NDEBUG 0
40
41//#define VERY_VERBOSE_LOGGING
42#ifdef VERY_VERBOSE_LOGGING
43#define ALOGVV ALOGV
44#else
45#define ALOGVV(a...) do { } while(0)
46#endif
Sharad Sangle36781612015-05-28 16:15:16 +053047
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070048// A device mask for all audio output devices that are considered "remote" when evaluating
49// active output devices in isStreamActiveRemotely()
50#define APM_AUDIO_OUT_DEVICE_REMOTE_ALL AUDIO_DEVICE_OUT_REMOTE_SUBMIX
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070051// A device mask for all audio input and output devices where matching inputs/outputs on device
52// type alone is not enough: the address must match too
53#define APM_AUDIO_DEVICE_MATCH_ADDRESS_ALL (AUDIO_DEVICE_IN_REMOTE_SUBMIX | \
54 AUDIO_DEVICE_OUT_REMOTE_SUBMIX)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070055
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070056#include <inttypes.h>
Mingming Yin0ae14ea2014-07-09 17:55:56 -070057#include <math.h>
Mingming Yin0670f162014-06-12 16:05:49 -070058
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070059#include <cutils/properties.h>
60#include <utils/Log.h>
61#include <hardware/audio.h>
62#include <hardware/audio_effect.h>
63#include <media/AudioParameter.h>
64#include <soundtrigger/SoundTrigger.h>
65#include "AudioPolicyManager.h"
Sharad Sangle36781612015-05-28 16:15:16 +053066#include <policy.h>
Gao Jiedb057832015-11-12 08:51:22 +080067#ifdef DOLBY_ENABLE
68#include "DolbyAudioPolicy_impl.h"
69#endif // DOLBY_END
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070070
71namespace android {
Sharad Sanglec5766ff2015-06-04 20:24:10 +053072#ifdef VOICE_CONCURRENCY
73audio_output_flags_t AudioPolicyManagerCustom::getFallBackPath()
74{
75 audio_output_flags_t flag = AUDIO_OUTPUT_FLAG_FAST;
76 char propValue[PROPERTY_VALUE_MAX];
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070077
Sharad Sanglec5766ff2015-06-04 20:24:10 +053078 if (property_get("voice.conc.fallbackpath", propValue, NULL)) {
79 if (!strncmp(propValue, "deep-buffer", 11)) {
80 flag = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
81 }
82 else if (!strncmp(propValue, "fast", 4)) {
83 flag = AUDIO_OUTPUT_FLAG_FAST;
84 }
85 else {
86 ALOGD("voice_conc:not a recognised path(%s) in prop voice.conc.fallbackpath",
87 propValue);
88 }
89 }
90 else {
91 ALOGD("voice_conc:prop voice.conc.fallbackpath not set");
92 }
93
94 ALOGD("voice_conc:picked up flag(0x%x) from prop voice.conc.fallbackpath",
95 flag);
96
97 return flag;
98}
99#endif /*VOICE_CONCURRENCY*/
Ashish Jaine4a22d52016-06-22 11:55:08 +0530100
101void AudioPolicyManagerCustom::moveGlobalEffect()
102{
103 audio_io_handle_t dstOutput = getOutputForEffect();
104 if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) {
105#ifdef DOLBY_ENABLE
106 status_t status =
107 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
108 mPrimaryOutput->mIoHandle,
109 dstOutput);
110 if (status == NO_ERROR) {
111 for (size_t i = 0; i < mEffects.size(); i++) {
112 sp<EffectDescriptor> desc = mEffects.valueAt(i);
113 if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX) {
114 // update the mIo member of EffectDescriptor
115 // for the global effect
116 ALOGV("%s updating mIo", __FUNCTION__);
117 desc->mIo = dstOutput;
118 }
119 }
120 } else {
121 ALOGW("%s moveEffects from %d to %d failed", __FUNCTION__,
122 mPrimaryOutput->mIoHandle, dstOutput);
123 }
124#else // DOLBY_END
125 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
126 mPrimaryOutput->mIoHandle, dstOutput);
127#endif
128 }
129}
130
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700131// ----------------------------------------------------------------------------
132// AudioPolicyInterface implementation
133// ----------------------------------------------------------------------------
Sharad Sangle36781612015-05-28 16:15:16 +0530134extern "C" AudioPolicyInterface* createAudioPolicyManager(
135 AudioPolicyClientInterface *clientInterface)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700136{
Sharad Sangle36781612015-05-28 16:15:16 +0530137 return new AudioPolicyManagerCustom(clientInterface);
138}
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700139
Sharad Sangle36781612015-05-28 16:15:16 +0530140extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface)
141{
142 delete interface;
143}
144
145status_t AudioPolicyManagerCustom::setDeviceConnectionStateInt(audio_devices_t device,
146 audio_policy_dev_state_t state,
147 const char *device_address,
148 const char *device_name)
149{
Ashish Jain40cab3a2016-03-23 11:14:14 +0530150 ALOGD("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
Sharad Sangle36781612015-05-28 16:15:16 +0530151 device, state, device_address, device_name);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700152
153 // connect/disconnect only 1 device at a time
154 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
155
Sharad Sangle36781612015-05-28 16:15:16 +0530156 sp<DeviceDescriptor> devDesc =
157 mHwModules.getDeviceDescriptor(device, device_address, device_name);
158
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700159 // handle output devices
160 if (audio_is_output_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700161 SortedVector <audio_io_handle_t> outputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700162
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700163 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700164
165 // save a copy of the opened output descriptors before any output is opened or closed
166 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
167 mPreviousOutputs = mOutputs;
168 switch (state)
169 {
170 // handle output device connection
Sharad Sangle36781612015-05-28 16:15:16 +0530171 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700172 if (index >= 0) {
Sharad Sangle4509cef2015-08-19 20:47:12 +0530173#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
174 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
175 if (!strncmp(device_address, "hdmi_spkr", 9)) {
176 mHdmiAudioDisabled = false;
177 } else {
178 mHdmiAudioEvent = true;
179 }
180 }
181#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700182 ALOGW("setDeviceConnectionState() device already connected: %x", device);
183 return INVALID_OPERATION;
184 }
185 ALOGV("setDeviceConnectionState() connecting device %x", device);
186
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700187 // register new device as available
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700188 index = mAvailableOutputDevices.add(devDesc);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530189#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
190 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
191 if (!strncmp(device_address, "hdmi_spkr", 9)) {
192 mHdmiAudioDisabled = false;
193 } else {
194 mHdmiAudioEvent = true;
195 }
196 if (mHdmiAudioDisabled || !mHdmiAudioEvent) {
197 mAvailableOutputDevices.remove(devDesc);
198 ALOGW("HDMI sink not connected, do not route audio to HDMI out");
199 return INVALID_OPERATION;
200 }
201 }
202#endif
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700203 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530204 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700205 if (module == 0) {
206 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
207 device);
208 mAvailableOutputDevices.remove(devDesc);
209 return INVALID_OPERATION;
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700210 }
Sharad Sangle36781612015-05-28 16:15:16 +0530211 mAvailableOutputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700212 } else {
213 return NO_MEMORY;
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700214 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700215
Sharad Sangle36781612015-05-28 16:15:16 +0530216 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700217 mAvailableOutputDevices.remove(devDesc);
218 return INVALID_OPERATION;
219 }
Sharad Sangle36781612015-05-28 16:15:16 +0530220 // Propagate device availability to Engine
221 mEngine->setDeviceConnectionState(devDesc, state);
222
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700223 // outputs should never be empty here
224 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
225 "checkOutputsForDevice() returned no outputs but status OK");
226 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
227 outputs.size());
Sharad Sangle36781612015-05-28 16:15:16 +0530228
229 // Send connect to HALs
230 AudioParameter param = AudioParameter(devDesc->mAddress);
231 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
232 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
233
234 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700235 // handle output device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700236 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
237 if (index < 0) {
Sharad Sangle4509cef2015-08-19 20:47:12 +0530238#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
239 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
240 if (!strncmp(device_address, "hdmi_spkr", 9)) {
241 mHdmiAudioDisabled = true;
242 } else {
243 mHdmiAudioEvent = false;
244 }
245 }
246#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700247 ALOGW("setDeviceConnectionState() device not connected: %x", device);
248 return INVALID_OPERATION;
249 }
250
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700251 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
252
Sharad Sangle36781612015-05-28 16:15:16 +0530253 // Send Disconnect to HALs
254 AudioParameter param = AudioParameter(devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700255 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
256 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
257
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700258 // remove device from available output devices
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700259 mAvailableOutputDevices.remove(devDesc);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530260#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
261 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
262 if (!strncmp(device_address, "hdmi_spkr", 9)) {
263 mHdmiAudioDisabled = true;
264 } else {
265 mHdmiAudioEvent = false;
266 }
267 }
268#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530269 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
270
271 // Propagate device availability to Engine
272 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700273 } break;
274
275 default:
276 ALOGE("setDeviceConnectionState() invalid state: %x", state);
277 return BAD_VALUE;
278 }
279
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700280 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
281 // output is suspended before any tracks are moved to it
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700282 checkA2dpSuspend();
283 checkOutputForAllStrategies();
284 // outputs must be closed after checkOutputForAllStrategies() is executed
285 if (!outputs.isEmpty()) {
286 for (size_t i = 0; i < outputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530287 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700288 // close unused outputs after device disconnection or direct outputs that have been
289 // opened by checkOutputsForDevice() to query dynamic parameters
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700290 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700291 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
292 (desc->mDirectOpenCount == 0))) {
293 closeOutput(outputs[i]);
294 }
295 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700296 // check again after closing A2DP output to reset mA2dpSuspended if needed
297 checkA2dpSuspend();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700298 }
299
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530300#ifdef FM_POWER_OPT
301 // handle FM device connection state to trigger FM AFE loopback
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800302 if (device == AUDIO_DEVICE_OUT_FM && hasPrimaryOutput()) {
Mingming Yin2dd77c22016-01-06 18:02:42 -0800303 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530304 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
305 mPrimaryOutput->changeRefCount(AUDIO_STREAM_MUSIC, 1);
Mingming Yin2dd77c22016-01-06 18:02:42 -0800306 newDevice = (audio_devices_t)(getNewOutputDevice(mPrimaryOutput, false)|AUDIO_DEVICE_OUT_FM);
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800307 mFMIsActive = true;
Zhou Songbd0264e2016-06-21 11:01:06 +0800308 mPrimaryOutput->mDevice = newDevice & ~AUDIO_DEVICE_OUT_FM;
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530309 } else {
Mingming Yin2dd77c22016-01-06 18:02:42 -0800310 newDevice = (audio_devices_t)(getNewOutputDevice(mPrimaryOutput, false));
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800311 mFMIsActive = false;
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530312 mPrimaryOutput->changeRefCount(AUDIO_STREAM_MUSIC, -1);
313 }
314 AudioParameter param = AudioParameter();
315 param.addInt(String8("handle_fm"), (int)newDevice);
316 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString());
317 }
318#endif /* FM_POWER_OPT end */
319
Zhou Songbd0264e2016-06-21 11:01:06 +0800320 updateDevicesAndOutputs();
321#ifdef DOLBY_ENABLE
322 // Before closing the opened outputs, update endpoint property with device capabilities
323 audio_devices_t audioOutputDevice = getDeviceForStrategy(getStrategy(AUDIO_STREAM_MUSIC), true);
324 mDolbyAudioPolicy.setEndpointSystemProperty(audioOutputDevice, mHwModules);
325#endif // DOLBY_END
326 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
327 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
328 updateCallRouting(newDevice);
329 }
330
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700331 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530332 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
333 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
334 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700335 // do not force device change on duplicated output because if device is 0, it will
336 // also force a device 0 for the two outputs it is duplicated to which may override
337 // a valid device selection on those outputs.
Sharad Sangle36781612015-05-28 16:15:16 +0530338 bool force = !desc->isDuplicated()
339 && (!device_distinguishes_on_address(device)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700340 // always force when disconnecting (a non-duplicated device)
341 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Sharad Sangle36781612015-05-28 16:15:16 +0530342 setOutputDevice(desc, newDevice, force, 0);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700343 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700344 }
345
Ashish Jain40cab3a2016-03-23 11:14:14 +0530346 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
347 cleanUpForDevice(devDesc);
348 }
349
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700350 mpClientInterface->onAudioPortListUpdate();
351 return NO_ERROR;
352 } // end if is output device
353
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700354 // handle input devices
355 if (audio_is_input_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700356 SortedVector <audio_io_handle_t> inputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700357
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700358 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700359 switch (state)
360 {
361 // handle input device connection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700362 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
363 if (index >= 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700364 ALOGW("setDeviceConnectionState() device already connected: %d", device);
365 return INVALID_OPERATION;
366 }
Sharad Sangle36781612015-05-28 16:15:16 +0530367 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700368 if (module == NULL) {
369 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
370 device);
371 return INVALID_OPERATION;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700372 }
Sharad Sangle36781612015-05-28 16:15:16 +0530373 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700374 return INVALID_OPERATION;
375 }
376
377 index = mAvailableInputDevices.add(devDesc);
378 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530379 mAvailableInputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700380 } else {
381 return NO_MEMORY;
382 }
Sharad Sangle36781612015-05-28 16:15:16 +0530383
384 // Set connect to HALs
385 AudioParameter param = AudioParameter(devDesc->mAddress);
386 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
387 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
388
389 // Propagate device availability to Engine
390 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700391 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700392
393 // handle input device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700394 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
395 if (index < 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700396 ALOGW("setDeviceConnectionState() device not connected: %d", device);
397 return INVALID_OPERATION;
398 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700399
400 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
401
402 // Set Disconnect to HALs
Sharad Sangle36781612015-05-28 16:15:16 +0530403 AudioParameter param = AudioParameter(devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700404 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
405 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
406
Sharad Sangle36781612015-05-28 16:15:16 +0530407 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700408 mAvailableInputDevices.remove(devDesc);
409
Sharad Sangle36781612015-05-28 16:15:16 +0530410 // Propagate device availability to Engine
411 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700412 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700413
414 default:
415 ALOGE("setDeviceConnectionState() invalid state: %x", state);
416 return BAD_VALUE;
417 }
418
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700419 closeAllInputs();
420
Sharad Sangle36781612015-05-28 16:15:16 +0530421 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700422 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
423 updateCallRouting(newDevice);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700424 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700425
Ashish Jain40cab3a2016-03-23 11:14:14 +0530426 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
427 cleanUpForDevice(devDesc);
428 }
429
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700430 mpClientInterface->onAudioPortListUpdate();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700431 return NO_ERROR;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700432 } // end if is input device
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700433
434 ALOGW("setDeviceConnectionState() invalid device: %x", device);
435 return BAD_VALUE;
436}
Weiyin Jiangd211ef52016-02-16 15:55:11 +0800437
Sharad Sangle36781612015-05-28 16:15:16 +0530438// This function checks for the parameters which can be offloaded.
439// This can be enhanced depending on the capability of the DSP and policy
440// of the system.
441bool AudioPolicyManagerCustom::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700442{
Sharad Sangle36781612015-05-28 16:15:16 +0530443 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
444 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
445 offloadInfo.sample_rate, offloadInfo.channel_mask,
446 offloadInfo.format,
447 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
448 offloadInfo.has_video);
Ashish Jain40cab3a2016-03-23 11:14:14 +0530449
450 if (mMasterMono) {
451 return false; // no offloading if mono is set.
452 }
453
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530454#ifdef VOICE_CONCURRENCY
455 char concpropValue[PROPERTY_VALUE_MAX];
456 if (property_get("voice.playback.conc.disabled", concpropValue, NULL)) {
457 bool propenabled = atoi(concpropValue) || !strncmp("true", concpropValue, 4);
458 if (propenabled) {
459 if (isInCall())
460 {
461 ALOGD("\n copl: blocking compress offload on call mode\n");
462 return false;
463 }
464 }
465 }
466#endif
467#ifdef RECORD_PLAY_CONCURRENCY
468 char recConcPropValue[PROPERTY_VALUE_MAX];
469 bool prop_rec_play_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700470
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530471 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
472 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
473 }
474
475 if ((prop_rec_play_enabled) &&
476 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
477 ALOGD("copl: blocking compress offload for record concurrency");
478 return false;
479 }
480#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530481 // Check if stream type is music, then only allow offload as of now.
482 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
483 {
484 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
485 return false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700486 }
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530487
Alexy Joseph0ac09032015-10-16 15:57:53 -0700488 // Check if offload has been disabled
489 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
490 if (offloadDisabled) {
491 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
492 return false;
493 }
494
Ashish Jainac510a72016-04-28 12:22:58 +0530495 //check if it's multi-channel AAC (includes sub formats) and FLAC format
496 if ((popcount(offloadInfo.channel_mask) > 2) &&
497 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
498 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS))) {
499 ALOGD("offload disabled for multi-channel AAC,FLAC and VORBIS format");
500 return false;
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530501 }
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530502
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530503#ifdef AUDIO_EXTN_FORMATS_ENABLED
Ashish Jainac510a72016-04-28 12:22:58 +0530504 //check if it's multi-channel FLAC/ALAC/WMA format with sample rate > 48k
505 if ((popcount(offloadInfo.channel_mask) > 2) &&
506 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
507 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) && (offloadInfo.sample_rate > 48000)) ||
508 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) && (offloadInfo.sample_rate > 48000)) ||
509 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) && (offloadInfo.sample_rate > 48000)) ||
510 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS))) {
511 ALOGD("offload disabled for multi-channel FLAC/ALAC/WMA/AAC_ADTS clips with sample rate > 48kHz");
512 return false;
513 }
yidonghae28e3a2016-08-19 11:31:15 +0800514
515 if ((((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) && (offloadInfo.bit_rate > MAX_BITRATE_WMA)) ||
516 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) && (offloadInfo.bit_rate > MAX_BITRATE_WMA_PRO)) ||
517 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) && (offloadInfo.bit_rate > MAX_BITRATE_WMA_LOSSLESS))){
518 //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.
519 ALOGD("offload disabled for WMA/WMA_PRO/WMA_LOSSLESS clips with bit rate over maximum supported value");
520 return false;
521 }
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530522#endif
Ashish Jainac510a72016-04-28 12:22:58 +0530523 //TODO: enable audio offloading with video when ready
524 const bool allowOffloadWithVideo =
525 property_get_bool("audio.offload.video", false /* default_value */);
526 if (offloadInfo.has_video && !allowOffloadWithVideo) {
527 ALOGV("isOffloadSupported: has_video == true, returning false");
528 return false;
529 }
Manish Dewanganf3cd0f82015-10-13 14:04:36 +0530530
Ashish Jainac510a72016-04-28 12:22:58 +0530531 const bool allowOffloadStreamingWithVideo = property_get_bool("av.streaming.offload.enable",
532 false /*default value*/);
533 if (offloadInfo.has_video && offloadInfo.is_streaming && !allowOffloadStreamingWithVideo) {
534 ALOGW("offload disabled by av.streaming.offload.enable %d",allowOffloadStreamingWithVideo);
535 return false;
Sharad Sangle36781612015-05-28 16:15:16 +0530536 }
537
538 //If duration is less than minimum value defined in property, return false
Ashish Jainac510a72016-04-28 12:22:58 +0530539 char propValue[PROPERTY_VALUE_MAX];
Sharad Sangle36781612015-05-28 16:15:16 +0530540 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
541 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
542 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
543 return false;
544 }
545 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
546 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
547 //duration checks only valid for MP3/AAC/ formats,
548 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
549 if ((offloadInfo.format == AUDIO_FORMAT_MP3) ||
550 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
551 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
Ashish Jainac510a72016-04-28 12:22:58 +0530552 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS))
553 return false;
554
555#ifdef AUDIO_EXTN_FORMATS_ENABLED
556 if (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) ||
Sharad Sangle36781612015-05-28 16:15:16 +0530557 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) ||
558 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) ||
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530559 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_APE) ||
Preetam Singh Ranawat191eb772016-09-09 17:10:19 +0530560 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_DSD) ||
Ashish Jainac510a72016-04-28 12:22:58 +0530561 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS))
Sharad Sangle36781612015-05-28 16:15:16 +0530562 return false;
Ashish Jainac510a72016-04-28 12:22:58 +0530563#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530564 }
565
566 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
567 // creating an offloaded track and tearing it down immediately after start when audioflinger
568 // detects there is an active non offloadable effect.
569 // FIXME: We should check the audio session here but we do not have it in this context.
570 // This may prevent offloading in rare situations where effects are left active by apps
571 // in the background.
572 if (mEffects.isNonOffloadableEffectEnabled()) {
573 return false;
574 }
Dhananjay Kumarcf558ba2016-07-29 19:33:13 +0530575
Sharad Sangle36781612015-05-28 16:15:16 +0530576 // See if there is a profile to support this.
577 // AUDIO_DEVICE_NONE
578 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
579 offloadInfo.sample_rate,
580 offloadInfo.format,
581 offloadInfo.channel_mask,
582 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
583 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
584 return (profile != 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700585}
Weiyin Jiangd211ef52016-02-16 15:55:11 +0800586
Sharad Sangle36781612015-05-28 16:15:16 +0530587audio_devices_t AudioPolicyManagerCustom::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
588 bool fromCache)
589{
590 audio_devices_t device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700591
Ashish Jain40cab3a2016-03-23 11:14:14 +0530592 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Sharad Sangle36781612015-05-28 16:15:16 +0530593 if (index >= 0) {
594 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
595 if (patchDesc->mUid != mUidCached) {
596 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Ashish Jain40cab3a2016-03-23 11:14:14 +0530597 outputDesc->device(), outputDesc->getPatchHandle());
Sharad Sangle36781612015-05-28 16:15:16 +0530598 return outputDesc->device();
599 }
600 }
601
602 // check the following by order of priority to request a routing change if necessary:
603 // 1: the strategy enforced audible is active and enforced on the output:
604 // use device for strategy enforced audible
605 // 2: we are in call or the strategy phone is active on the output:
606 // use device for strategy phone
607 // 3: the strategy for enforced audible is active but not enforced on the output:
608 // use the device for strategy enforced audible
609 // 4: the strategy sonification is active on the output:
610 // use device for strategy sonification
611 // 5: the strategy "respectful" sonification is active on the output:
612 // use device for strategy "respectful" sonification
613 // 6: the strategy accessibility is active on the output:
614 // use device for strategy accessibility
615 // 7: the strategy media is active on the output:
616 // use device for strategy media
617 // 8: the strategy DTMF is active on the output:
618 // use device for strategy DTMF
619 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
620 // use device for strategy t-t-s
621 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
622 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
623 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
624 } else if (isInCall() ||
625 isStrategyActive(outputDesc, STRATEGY_PHONE)||
626 isStrategyActive(mPrimaryOutput, STRATEGY_PHONE)) {
627 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
628 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
629 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
630 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)||
631 (isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION)
632 && (!isStrategyActive(mPrimaryOutput,STRATEGY_MEDIA)))) {
633 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530634 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL) ||
635 isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION_RESPECTFUL)) {
Sharad Sangle36781612015-05-28 16:15:16 +0530636 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
637 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
638 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
639 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
640 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
641 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
642 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
643 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
644 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
645 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
646 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
647 }
648
649 ALOGV("getNewOutputDevice() selected device %x", device);
650 return device;
651}
Weiyin Jiangd211ef52016-02-16 15:55:11 +0800652
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700653void AudioPolicyManagerCustom::setPhoneState(audio_mode_t state)
654{
Ashish Jain40cab3a2016-03-23 11:14:14 +0530655 ALOGD("setPhoneState() state %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530656 // store previous phone state for management of sonification strategy below
Sharad Sangle4509cef2015-08-19 20:47:12 +0530657 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Sharad Sangle36781612015-05-28 16:15:16 +0530658 int oldState = mEngine->getPhoneState();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700659
Sharad Sangle36781612015-05-28 16:15:16 +0530660 if (mEngine->setPhoneState(state) != NO_ERROR) {
661 ALOGW("setPhoneState() invalid or same state %d", state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700662 return;
663 }
Sharad Sangle36781612015-05-28 16:15:16 +0530664 /// Opens: can these line be executed after the switch of volume curves???
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700665 // if leaving call state, handle special case of active streams
666 // pertaining to sonification strategy see handleIncallSonification()
Zhou Song58081b62016-02-24 13:10:55 +0800667 if (isStateInCall(oldState)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700668 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530669 for (size_t j = 0; j < mOutputs.size(); j++) {
670 audio_io_handle_t curOutput = mOutputs.keyAt(j);
671 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
672 if (stream == AUDIO_STREAM_PATCH) {
673 continue;
674 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530675 handleIncallSonification((audio_stream_type_t)stream, false, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +0530676 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700677 }
Sharad Sangle36781612015-05-28 16:15:16 +0530678
Zhou Song58081b62016-02-24 13:10:55 +0800679 // force reevaluating accessibility routing when call stops
Sharad Sangle36781612015-05-28 16:15:16 +0530680 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700681 }
682
Sharad Sangle36781612015-05-28 16:15:16 +0530683 /**
684 * Switching to or from incall state or switching between telephony and VoIP lead to force
685 * routing command.
686 */
687 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
688 || (is_state_in_call(state) && (state != oldState)));
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700689
690 // check for device and output changes triggered by new phone state
691 checkA2dpSuspend();
692 checkOutputForAllStrategies();
693 updateDevicesAndOutputs();
694
Sharad Sangle36781612015-05-28 16:15:16 +0530695 sp<SwAudioOutputDescriptor> hwOutputDesc = mPrimaryOutput;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530696#ifdef VOICE_CONCURRENCY
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530697 char propValue[PROPERTY_VALUE_MAX];
698 bool prop_playback_enabled = false, prop_rec_enabled=false, prop_voip_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700699
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530700 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
701 prop_playback_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
702 }
703
704 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
705 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
706 }
707
708 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
709 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
710 }
711
Dhananjay Kumar9125b2a2015-12-01 16:24:09 +0530712 if ((AUDIO_MODE_IN_CALL != oldState) && (AUDIO_MODE_IN_CALL == state)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530713 ALOGD("voice_conc:Entering to call mode oldState :: %d state::%d ",
714 oldState, state);
Dhananjay Kumar9125b2a2015-12-01 16:24:09 +0530715 mvoice_call_state = state;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530716 if (prop_rec_enabled) {
717 //Close all active inputs
718 audio_io_handle_t activeInput = mInputs.getActiveInput();
719 if (activeInput != 0) {
720 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
721 switch(activeDesc->mInputSource) {
722 case AUDIO_SOURCE_VOICE_UPLINK:
723 case AUDIO_SOURCE_VOICE_DOWNLINK:
724 case AUDIO_SOURCE_VOICE_CALL:
725 ALOGD("voice_conc:FOUND active input during call active: %d",activeDesc->mInputSource);
726 break;
727
728 case AUDIO_SOURCE_VOICE_COMMUNICATION:
729 if(prop_voip_enabled) {
730 ALOGD("voice_conc:CLOSING VoIP input source on call setup :%d ",activeDesc->mInputSource);
731 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
732 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
733 }
734 break;
735
736 default:
737 ALOGD("voice_conc:CLOSING input on call setup for inputSource: %d",activeDesc->mInputSource);
738 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
739 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
740 break;
741 }
742 }
743 } else if (prop_voip_enabled) {
744 audio_io_handle_t activeInput = mInputs.getActiveInput();
745 if (activeInput != 0) {
746 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
747 if (AUDIO_SOURCE_VOICE_COMMUNICATION == activeDesc->mInputSource) {
748 ALOGD("voice_conc:CLOSING VoIP on call setup : %d",activeDesc->mInputSource);
749 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
750 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
751 }
752 }
753 }
754 if (prop_playback_enabled) {
755 // Move tracks associated to this strategy from previous output to new output
756 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
757 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
758 if (i == AUDIO_STREAM_PATCH) {
759 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
760 continue;
761 }
762 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
763 if ((AUDIO_STREAM_MUSIC == i) ||
764 (AUDIO_STREAM_VOICE_CALL == i) ) {
765 ALOGD("voice_conc:Invalidate stream type %d", i);
766 mpClientInterface->invalidateStream((audio_stream_type_t)i);
767 }
768 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
769 ALOGD("voice_conc:Invalidate stream type %d", i);
770 mpClientInterface->invalidateStream((audio_stream_type_t)i);
771 }
772 }
773 }
774
775 for (size_t i = 0; i < mOutputs.size(); i++) {
776 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
777 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
778 ALOGD("voice_conc:ouput desc / profile is NULL");
779 continue;
780 }
781
782 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
783 if (((!outputDesc->isDuplicated() &&outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY))
784 && prop_playback_enabled) {
785 ALOGD("voice_conc:calling suspendOutput on call mode for primary output");
786 mpClientInterface->suspendOutput(mOutputs.keyAt(i));
787 } //Close compress all sessions
788 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
789 && prop_playback_enabled) {
790 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
791 closeOutput(mOutputs.keyAt(i));
792 }
793 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_VOIP_RX)
794 && prop_voip_enabled) {
795 ALOGD("voice_conc:calling closeOutput on call mode for DIRECT output");
796 closeOutput(mOutputs.keyAt(i));
797 }
798 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
799 if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)
800 && prop_playback_enabled) {
801 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
802 closeOutput(mOutputs.keyAt(i));
803 }
804 }
805 }
Ashish Jaine4a22d52016-06-22 11:55:08 +0530806 // If effects where present on any of the above closed outputs,
807 // audioflinger moved them to the primary output by default
808 // move them back to the appropriate output.
809 moveGlobalEffect();
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530810 }
811
812 if ((AUDIO_MODE_IN_CALL == oldState || AUDIO_MODE_IN_COMMUNICATION == oldState) &&
813 (AUDIO_MODE_NORMAL == state) && prop_playback_enabled && mvoice_call_state) {
814 ALOGD("voice_conc:EXITING from call mode oldState :: %d state::%d \n",oldState, state);
815 mvoice_call_state = 0;
816 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
817 //restore PCM (deep-buffer) output after call termination
818 for (size_t i = 0; i < mOutputs.size(); i++) {
819 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
820 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
821 ALOGD("voice_conc:ouput desc / profile is NULL");
822 continue;
823 }
824 if (!outputDesc->isDuplicated() && outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
825 ALOGD("voice_conc:calling restoreOutput after call mode for primary output");
826 mpClientInterface->restoreOutput(mOutputs.keyAt(i));
827 }
828 }
829 }
830 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
831 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
832 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
833 if (i == AUDIO_STREAM_PATCH) {
834 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
835 continue;
836 }
837 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
838 if ((AUDIO_STREAM_MUSIC == i) ||
839 (AUDIO_STREAM_VOICE_CALL == i) ) {
840 mpClientInterface->invalidateStream((audio_stream_type_t)i);
841 }
842 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
843 mpClientInterface->invalidateStream((audio_stream_type_t)i);
844 }
845 }
846 }
847
848#endif
849#ifdef RECORD_PLAY_CONCURRENCY
850 char recConcPropValue[PROPERTY_VALUE_MAX];
851 bool prop_rec_play_enabled = false;
852
853 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
854 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
855 }
856 if (prop_rec_play_enabled) {
857 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
858 ALOGD("phone state changed to MODE_IN_COMM invlaidating music and voice streams");
859 // call invalidate for voice streams, so that it can use deepbuffer with VoIP out device from HAL
860 mpClientInterface->invalidateStream(AUDIO_STREAM_VOICE_CALL);
861 // call invalidate for music, so that compress will fallback to deep-buffer with VoIP out device
862 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
863
864 // close compress output to make sure session will be closed before timeout(60sec)
865 for (size_t i = 0; i < mOutputs.size(); i++) {
866
867 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
868 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
869 ALOGD("ouput desc / profile is NULL");
870 continue;
871 }
872
873 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
874 ALOGD("calling closeOutput on call mode for COMPRESS output");
875 closeOutput(mOutputs.keyAt(i));
876 }
877 }
Ashish Jaine4a22d52016-06-22 11:55:08 +0530878 // If effects where present on any of the above closed outputs,
879 // audioflinger moved them to the primary output by default
880 // move them back to the appropriate output.
881 moveGlobalEffect();
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530882 } else if ((oldState == AUDIO_MODE_IN_COMMUNICATION) &&
883 (mEngine->getPhoneState() == AUDIO_MODE_NORMAL)) {
884 // call invalidate for music so that music can fallback to compress
885 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
886 }
887 }
888#endif
889 mPrevPhoneState = oldState;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700890 int delayMs = 0;
891 if (isStateInCall(state)) {
892 nsecs_t sysTime = systemTime();
893 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530894 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700895 // mute media and sonification strategies and delay device switch by the largest
896 // latency of any output where either strategy is active.
897 // This avoid sending the ring tone or music tail into the earpiece or headset.
Sharad Sangle36781612015-05-28 16:15:16 +0530898 if ((isStrategyActive(desc, STRATEGY_MEDIA,
899 SONIFICATION_HEADSET_MUSIC_DELAY,
900 sysTime) ||
901 isStrategyActive(desc, STRATEGY_SONIFICATION,
902 SONIFICATION_HEADSET_MUSIC_DELAY,
903 sysTime)) &&
904 (delayMs < (int)desc->latency()*2)) {
905 delayMs = desc->latency()*2;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700906 }
Sharad Sangle36781612015-05-28 16:15:16 +0530907 setStrategyMute(STRATEGY_MEDIA, true, desc);
908 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700909 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Sharad Sangle36781612015-05-28 16:15:16 +0530910 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
911 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700912 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
913 }
914 }
915
Sharad Sangle36781612015-05-28 16:15:16 +0530916 if (hasPrimaryOutput()) {
917 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
918 // the device returned is not necessarily reachable via this output
919 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
920 // force routing command to audio hardware when ending call
921 // even if no device change is needed
922 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
923 rxDevice = mPrimaryOutput->device();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700924 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700925
Sharad Sangle36781612015-05-28 16:15:16 +0530926 if (state == AUDIO_MODE_IN_CALL) {
927 updateCallRouting(rxDevice, delayMs);
928 } else if (oldState == AUDIO_MODE_IN_CALL) {
929 if (mCallRxPatch != 0) {
930 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
931 mCallRxPatch.clear();
932 }
933 if (mCallTxPatch != 0) {
934 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
935 mCallTxPatch.clear();
936 }
937 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
938 } else {
939 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700940 }
941 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530942 //update device for all non-primary outputs
943 for (size_t i = 0; i < mOutputs.size(); i++) {
944 audio_io_handle_t output = mOutputs.keyAt(i);
945 if (output != mPrimaryOutput->mIoHandle) {
946 newDevice = getNewOutputDevice(mOutputs.valueFor(output), false /*fromCache*/);
947 setOutputDevice(mOutputs.valueFor(output), newDevice, (newDevice != AUDIO_DEVICE_NONE));
948 }
949 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700950 // if entering in call state, handle special case of active streams
951 // pertaining to sonification strategy see handleIncallSonification()
952 if (isStateInCall(state)) {
953 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530954 for (size_t j = 0; j < mOutputs.size(); j++) {
955 audio_io_handle_t curOutput = mOutputs.keyAt(j);
956 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
957 if (stream == AUDIO_STREAM_PATCH) {
958 continue;
959 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530960 handleIncallSonification((audio_stream_type_t)stream, true, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +0530961 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700962 }
Zhou Song58081b62016-02-24 13:10:55 +0800963
964 // force reevaluating accessibility routing when call starts
965 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700966 }
967
968 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
969 if (state == AUDIO_MODE_RINGTONE &&
970 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
971 mLimitRingtoneVolume = true;
972 } else {
973 mLimitRingtoneVolume = false;
974 }
975}
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +0530976
977void AudioPolicyManagerCustom::setForceUse(audio_policy_force_use_t usage,
978 audio_policy_forced_cfg_t config)
979{
Ashish Jain40cab3a2016-03-23 11:14:14 +0530980 ALOGD("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +0530981
982 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
983 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
984 return;
985 }
986 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
987 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
988 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
989
990 // check for device and output changes triggered by new force usage
991 checkA2dpSuspend();
992 checkOutputForAllStrategies();
993 updateDevicesAndOutputs();
Ashish Jain40cab3a2016-03-23 11:14:14 +0530994
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +0530995 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
996 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
997 updateCallRouting(newDevice);
998 }
999 // Use reverse loop to make sure any low latency usecases (generally tones)
1000 // are not routed before non LL usecases (generally music).
1001 // We can safely assume that LL output would always have lower index,
1002 // and use this work-around to avoid routing of output with music stream
1003 // from the context of short lived LL output.
1004 // Note: in case output's share backend(HAL sharing is implicit) all outputs
1005 // gets routing update while processing first output itself.
1006 for (size_t i = mOutputs.size(); i > 0; i--) {
1007 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i-1);
1008 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
1009 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || outputDesc != mPrimaryOutput) {
1010 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
1011 }
1012 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
1013 applyStreamVolumes(outputDesc, newDevice, 0, true);
1014 }
1015 }
1016
1017 audio_io_handle_t activeInput = mInputs.getActiveInput();
1018 if (activeInput != 0) {
Ashish Jain40cab3a2016-03-23 11:14:14 +05301019 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
1020 audio_devices_t newDevice = getNewInputDevice(activeInput);
1021 // Force new input selection if the new device can not be reached via current input
1022 if (activeDesc->mProfile->getSupportedDevices().types() & (newDevice & ~AUDIO_DEVICE_BIT_IN)) {
1023 setInputDevice(activeInput, newDevice);
1024 } else {
1025 closeInput(activeInput);
1026 }
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301027 }
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301028}
1029
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301030status_t AudioPolicyManagerCustom::stopSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301031 audio_stream_type_t stream,
1032 bool forceDeviceUpdate)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001033{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301034 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1035 ALOGW("stopSource() invalid stream %d", stream);
1036 return INVALID_OPERATION;
1037 }
Sharad Sangle36781612015-05-28 16:15:16 +05301038 // always handle stream stop, check which stream type is stopping
1039 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001040
Sharad Sangle36781612015-05-28 16:15:16 +05301041 // handle special case for sonification while in call
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001042 if (isInCall()) {
Sharad Sangle36781612015-05-28 16:15:16 +05301043 if (outputDesc->isDuplicated()) {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301044 handleIncallSonification(stream, false, false, outputDesc->subOutput1()->mIoHandle);
1045 handleIncallSonification(stream, false, false, outputDesc->subOutput2()->mIoHandle);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001046 }
Sharad Sangle36781612015-05-28 16:15:16 +05301047 handleIncallSonification(stream, false, false, outputDesc->mIoHandle);
1048 }
1049
1050 if (outputDesc->mRefCount[stream] > 0) {
1051 // decrement usage count of this stream on the output
1052 outputDesc->changeRefCount(stream, -1);
1053
1054 // store time at which the stream was stopped - see isStreamActive()
1055 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
1056 outputDesc->mStopTime[stream] = systemTime();
Zhou Song5dcddc92015-09-21 14:36:57 +08001057 audio_devices_t prevDevice = outputDesc->device();
Sharad Sangle36781612015-05-28 16:15:16 +05301058 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
1059 // delay the device switch by twice the latency because stopOutput() is executed when
1060 // the track stop() command is received and at that time the audio track buffer can
1061 // still contain data that needs to be drained. The latency only covers the audio HAL
1062 // and kernel buffers. Also the latency does not always include additional delay in the
1063 // audio path (audio DSP, CODEC ...)
1064 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
1065
1066 // force restoring the device selection on other active outputs if it differs from the
1067 // one being selected for this output
1068 for (size_t i = 0; i < mOutputs.size(); i++) {
1069 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1070 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1071 if (desc != outputDesc &&
1072 desc->isActive() &&
1073 outputDesc->sharesHwModuleWith(desc) &&
1074 (newDevice != desc->device())) {
Sharad Sangle4509cef2015-08-19 20:47:12 +05301075 audio_devices_t dev = getNewOutputDevice(mOutputs.valueFor(curOutput), false /*fromCache*/);
Zhou Song5dcddc92015-09-21 14:36:57 +08001076 uint32_t delayMs;
1077 if (dev == prevDevice) {
1078 delayMs = 0;
1079 } else {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301080 delayMs = outputDesc->latency()*2;
Zhou Song5dcddc92015-09-21 14:36:57 +08001081 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301082 setOutputDevice(desc,
1083 dev,
Sharad Sangle36781612015-05-28 16:15:16 +05301084 true,
Zhou Song5dcddc92015-09-21 14:36:57 +08001085 delayMs);
Sharad Sangle36781612015-05-28 16:15:16 +05301086 }
1087 }
1088 // update the outputs if stopping one with a stream that can affect notification routing
1089 handleNotificationRoutingForStream(stream);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001090 }
Sharad Sangle36781612015-05-28 16:15:16 +05301091 return NO_ERROR;
1092 } else {
1093 ALOGW("stopOutput() refcount is already 0");
1094 return INVALID_OPERATION;
1095 }
1096}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001097
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301098status_t AudioPolicyManagerCustom::startSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301099 audio_stream_type_t stream,
1100 audio_devices_t device,
Lalit Kansaraf803ac52016-05-09 19:12:01 +05301101 const char *address,
Sharad Sangle36781612015-05-28 16:15:16 +05301102 uint32_t *delayMs)
1103{
1104 // cannot start playback of STREAM_TTS if any other output is being used
1105 uint32_t beaconMuteLatency = 0;
1106
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301107 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1108 ALOGW("startSource() invalid stream %d", stream);
1109 return INVALID_OPERATION;
1110 }
1111
Sharad Sangle36781612015-05-28 16:15:16 +05301112 *delayMs = 0;
1113 if (stream == AUDIO_STREAM_TTS) {
1114 ALOGV("\t found BEACON stream");
Ashish Jain40cab3a2016-03-23 11:14:14 +05301115 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301116 return INVALID_OPERATION;
1117 } else {
1118 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001119 }
Sharad Sangle36781612015-05-28 16:15:16 +05301120 } else {
1121 // some playback other than beacon starts
1122 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1123 }
1124
Ashish Jain40cab3a2016-03-23 11:14:14 +05301125 // check active before incrementing usage count
1126 bool force = !outputDesc->isActive();
1127
Sharad Sangle36781612015-05-28 16:15:16 +05301128 // increment usage count for this stream on the requested output:
1129 // NOTE that the usage count is the same for duplicated output and hardware output which is
1130 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1131 outputDesc->changeRefCount(stream, 1);
1132
1133 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
1134 // starting an output being rerouted?
1135 if (device == AUDIO_DEVICE_NONE) {
1136 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001137 }
Sharad Sangle36781612015-05-28 16:15:16 +05301138 routing_strategy strategy = getStrategy(stream);
1139 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
1140 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1141 (beaconMuteLatency > 0);
1142 uint32_t waitMs = beaconMuteLatency;
Sharad Sangle36781612015-05-28 16:15:16 +05301143 for (size_t i = 0; i < mOutputs.size(); i++) {
1144 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1145 if (desc != outputDesc) {
1146 // force a device change if any other output is managed by the same hw
1147 // module and has a current device selection that differs from selected device.
1148 // In this case, the audio HAL must receive the new device selection so that it can
1149 // change the device currently selected by the other active output.
1150 if (outputDesc->sharesHwModuleWith(desc) &&
1151 desc->device() != device) {
1152 force = true;
1153 }
1154 // wait for audio on other active outputs to be presented when starting
1155 // a notification so that audio focus effect can propagate, or that a mute/unmute
1156 // event occurred for beacon
1157 uint32_t latency = desc->latency();
1158 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1159 waitMs = latency;
1160 }
1161 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001162 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05301163 uint32_t muteWaitMs;
Lalit Kansaraf803ac52016-05-09 19:12:01 +05301164 muteWaitMs = setOutputDevice(outputDesc, device, force, 0, NULL, address);
Sharad Sangle36781612015-05-28 16:15:16 +05301165
1166 // handle special case for sonification while in call
1167 if (isInCall()) {
1168 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001169 }
Sharad Sangle36781612015-05-28 16:15:16 +05301170
1171 // apply volume rules for current stream and device if necessary
1172 checkAndSetVolume(stream,
Ashish Jain40cab3a2016-03-23 11:14:14 +05301173 mVolumeCurves->getVolumeIndex(stream, device),
Sharad Sangle36781612015-05-28 16:15:16 +05301174 outputDesc,
1175 device);
1176
1177 // update the outputs if starting an output with a stream that can affect notification
1178 // routing
1179 handleNotificationRoutingForStream(stream);
1180
1181 // force reevaluating accessibility routing when ringtone or alarm starts
1182 if (strategy == STRATEGY_SONIFICATION) {
1183 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1184 }
1185 }
1186 else {
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001187 // handle special case for sonification while in call
1188 if (isInCall()) {
1189 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Sharad Sangle36781612015-05-28 16:15:16 +05301190 }
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001191 }
Sharad Sangle36781612015-05-28 16:15:16 +05301192 return NO_ERROR;
1193}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001194
Sharad Sangle36781612015-05-28 16:15:16 +05301195void AudioPolicyManagerCustom::handleIncallSonification(audio_stream_type_t stream,
1196 bool starting, bool stateChange,
1197 audio_io_handle_t output)
1198{
1199 if(!hasPrimaryOutput()) {
1200 return;
1201 }
1202 // no action needed for AUDIO_STREAM_PATCH stream type, it's for internal flinger tracks
1203 if (stream == AUDIO_STREAM_PATCH) {
1204 return;
1205 }
1206 // if the stream pertains to sonification strategy and we are in call we must
1207 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
1208 // in the device used for phone strategy and play the tone if the selected device does not
1209 // interfere with the device used for phone strategy
1210 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
1211 // many times as there are active tracks on the output
1212 const routing_strategy stream_strategy = getStrategy(stream);
1213 if ((stream_strategy == STRATEGY_SONIFICATION) ||
1214 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
1215 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
1216 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
1217 stream, starting, outputDesc->mDevice, stateChange);
1218 if (outputDesc->mRefCount[stream]) {
1219 int muteCount = 1;
1220 if (stateChange) {
1221 muteCount = outputDesc->mRefCount[stream];
1222 }
1223 if (audio_is_low_visibility(stream)) {
1224 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
1225 for (int i = 0; i < muteCount; i++) {
1226 setStreamMute(stream, starting, outputDesc);
1227 }
1228 } else {
1229 ALOGV("handleIncallSonification() high visibility");
1230 if (outputDesc->device() &
1231 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
1232 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
1233 for (int i = 0; i < muteCount; i++) {
1234 setStreamMute(stream, starting, outputDesc);
1235 }
1236 }
1237 if (starting) {
1238 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
1239 AUDIO_STREAM_VOICE_CALL);
1240 } else {
1241 mpClientInterface->stopTone();
1242 }
1243 }
1244 }
1245 }
1246}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001247
Sharad Sangle36781612015-05-28 16:15:16 +05301248void AudioPolicyManagerCustom::handleNotificationRoutingForStream(audio_stream_type_t stream) {
1249 switch(stream) {
1250 case AUDIO_STREAM_MUSIC:
1251 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
1252 updateDevicesAndOutputs();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001253 break;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001254 default:
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001255 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001256 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001257}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001258
Sharad Sangle36781612015-05-28 16:15:16 +05301259status_t AudioPolicyManagerCustom::checkAndSetVolume(audio_stream_type_t stream,
1260 int index,
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301261 const sp<AudioOutputDescriptor>& outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301262 audio_devices_t device,
1263 int delayMs, bool force)
1264{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301265 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1266 ALOGW("checkAndSetVolume() invalid stream %d", stream);
1267 return INVALID_OPERATION;
1268 }
Sharad Sangle36781612015-05-28 16:15:16 +05301269 // do not change actual stream volume if the stream is muted
1270 if (outputDesc->mMuteCount[stream] != 0) {
1271 ALOGVV("checkAndSetVolume() stream %d muted count %d",
1272 stream, outputDesc->mMuteCount[stream]);
1273 return NO_ERROR;
1274 }
1275 audio_policy_forced_cfg_t forceUseForComm =
1276 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
1277 // do not change in call volume if bluetooth is connected and vice versa
1278 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
1279 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
1280 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
1281 stream, forceUseForComm);
1282 return INVALID_OPERATION;
1283 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001284
Sharad Sangle36781612015-05-28 16:15:16 +05301285 if (device == AUDIO_DEVICE_NONE) {
1286 device = outputDesc->device();
1287 }
1288
1289 float volumeDb = computeVolume(stream, index, device);
1290 if (outputDesc->isFixedVolume(device)) {
1291 volumeDb = 0.0f;
1292 }
1293
1294 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
1295
1296 if (stream == AUDIO_STREAM_VOICE_CALL ||
1297 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
1298 float voiceVolume;
1299 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
1300 if (stream == AUDIO_STREAM_VOICE_CALL) {
Ashish Jain40cab3a2016-03-23 11:14:14 +05301301 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Sharad Sangle36781612015-05-28 16:15:16 +05301302 } else {
1303 voiceVolume = 1.0;
1304 }
1305
1306 if (voiceVolume != mLastVoiceVolume && ((outputDesc == mPrimaryOutput) ||
1307 isDirectOutput(outputDesc->mIoHandle) || device & AUDIO_DEVICE_OUT_ALL_USB)) {
1308 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
1309 mLastVoiceVolume = voiceVolume;
1310 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301311#ifdef FM_POWER_OPT
1312 } else if (stream == AUDIO_STREAM_MUSIC && hasPrimaryOutput() &&
Haynes Mathew George603ae9b2015-12-21 17:23:59 -08001313 outputDesc == mPrimaryOutput && mFMIsActive) {
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001314 /* Avoid unnecessary set_parameter calls as it puts the primary
1315 outputs FastMixer in HOT_IDLE leading to breaks in audio */
1316 if (volumeDb != mPrevFMVolumeDb) {
1317 mPrevFMVolumeDb = volumeDb;
1318 AudioParameter param = AudioParameter();
1319 param.addFloat(String8("fm_volume"), Volume::DbToAmpl(volumeDb));
Mingming Yin1433cc62016-01-04 14:58:00 -08001320 //Double delayMs to avoid sound burst while device switch.
1321 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString(), delayMs*2);
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001322 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301323#endif /* FM_POWER_OPT end */
Sharad Sangle36781612015-05-28 16:15:16 +05301324 }
1325
1326 return NO_ERROR;
1327}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001328
Sharad Sangle36781612015-05-28 16:15:16 +05301329bool AudioPolicyManagerCustom::isDirectOutput(audio_io_handle_t output) {
1330 for (size_t i = 0; i < mOutputs.size(); i++) {
1331 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1332 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1333 if ((curOutput == output) && (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
1334 return true;
1335 }
1336 }
1337 return false;
1338}
vivek mehta0ea887a2015-08-26 14:01:20 -07001339
Ashish Jainac510a72016-04-28 12:22:58 +05301340bool static isDirectPCMEnabled(int bitWidth)
1341{
1342 bool directPCMEnabled = false;
1343 if (bitWidth == 24 || bitWidth == 32)
1344 directPCMEnabled =
1345 property_get_bool("audio.offload.pcm.24bit.enable", false);
1346 else
1347 directPCMEnabled =
1348 property_get_bool("audio.offload.pcm.16bit.enable", false);
1349
1350 return directPCMEnabled;
1351}
1352
vivek mehta0ea887a2015-08-26 14:01:20 -07001353status_t AudioPolicyManagerCustom::getOutputForAttr(const audio_attributes_t *attr,
1354 audio_io_handle_t *output,
1355 audio_session_t session,
1356 audio_stream_type_t *stream,
1357 uid_t uid,
1358 uint32_t samplingRate,
1359 audio_format_t format,
1360 audio_channel_mask_t channelMask,
1361 audio_output_flags_t flags,
1362 audio_port_handle_t selectedDeviceId,
1363 const audio_offload_info_t *offloadInfo)
1364{
1365 audio_offload_info_t tOffloadInfo = AUDIO_INFO_INITIALIZER;
1366
Alexy Joseph0ac09032015-10-16 15:57:53 -07001367 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
Ashish Jainac510a72016-04-28 12:22:58 +05301368 uint32_t bitWidth = (audio_bytes_per_sample(format) * 8);
Alexy Joseph0ac09032015-10-16 15:57:53 -07001369
1370 if (offloadDisabled) {
1371 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
1372 }
1373
Ashish Jainac510a72016-04-28 12:22:58 +05301374 if (!offloadDisabled && (offloadInfo == NULL) &&
1375 isDirectPCMEnabled(bitWidth) &&
1376 (flags == AUDIO_OUTPUT_FLAG_NONE)) {
vivek mehta0ea887a2015-08-26 14:01:20 -07001377
vivek mehta0ea887a2015-08-26 14:01:20 -07001378 tOffloadInfo.sample_rate = samplingRate;
1379 tOffloadInfo.channel_mask = channelMask;
1380 tOffloadInfo.format = format;
1381 tOffloadInfo.stream_type = *stream;
Ashish Jainac510a72016-04-28 12:22:58 +05301382 tOffloadInfo.bit_width = bitWidth;
vivek mehta0ea887a2015-08-26 14:01:20 -07001383 if (attr != NULL) {
1384 ALOGV("found attribute .. setting usage %d ", attr->usage);
1385 tOffloadInfo.usage = attr->usage;
1386 } else {
Alexy Joseph0ac09032015-10-16 15:57:53 -07001387 ALOGI("%s:: attribute is NULL .. no usage set", __func__);
vivek mehta0ea887a2015-08-26 14:01:20 -07001388 }
1389 offloadInfo = &tOffloadInfo;
1390 }
1391
1392 return AudioPolicyManager::getOutputForAttr(attr, output, session, stream,
1393 (uid_t)uid, (uint32_t)samplingRate,
1394 format, (audio_channel_mask_t)channelMask,
1395 flags, (audio_port_handle_t)selectedDeviceId,
1396 offloadInfo);
1397}
1398
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001399audio_io_handle_t AudioPolicyManagerCustom::getOutputForDevice(
1400 audio_devices_t device,
Sharad Sangle36781612015-05-28 16:15:16 +05301401 audio_session_t session __unused,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001402 audio_stream_type_t stream,
1403 uint32_t samplingRate,
1404 audio_format_t format,
1405 audio_channel_mask_t channelMask,
1406 audio_output_flags_t flags,
1407 const audio_offload_info_t *offloadInfo)
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001408{
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001409 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001410 status_t status;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001411
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001412#ifdef AUDIO_POLICY_TEST
1413 if (mCurOutput != 0) {
1414 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1415 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1416
1417 if (mTestOutputs[mCurOutput] == 0) {
1418 ALOGV("getOutput() opening test output");
Sharad Sangle36781612015-05-28 16:15:16 +05301419 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
1420 mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001421 outputDesc->mDevice = mTestDevice;
1422 outputDesc->mLatency = mTestLatencyMs;
1423 outputDesc->mFlags =
1424 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
1425 outputDesc->mRefCount[stream] = 0;
1426 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1427 config.sample_rate = mTestSamplingRate;
1428 config.channel_mask = mTestChannels;
1429 config.format = mTestFormat;
1430 if (offloadInfo != NULL) {
1431 config.offload_info = *offloadInfo;
1432 }
1433 status = mpClientInterface->openOutput(0,
1434 &mTestOutputs[mCurOutput],
1435 &config,
1436 &outputDesc->mDevice,
1437 String8(""),
1438 &outputDesc->mLatency,
1439 outputDesc->mFlags);
1440 if (status == NO_ERROR) {
1441 outputDesc->mSamplingRate = config.sample_rate;
1442 outputDesc->mFormat = config.format;
1443 outputDesc->mChannelMask = config.channel_mask;
1444 AudioParameter outputCmd = AudioParameter();
1445 outputCmd.addInt(String8("set_id"),mCurOutput);
1446 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1447 addOutput(mTestOutputs[mCurOutput], outputDesc);
1448 }
1449 }
1450 return mTestOutputs[mCurOutput];
1451 }
1452#endif //AUDIO_POLICY_TEST
Sharad Sangle36781612015-05-28 16:15:16 +05301453 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1454 (stream != AUDIO_STREAM_MUSIC)) {
1455 // compress should not be used for non-music streams
1456 ALOGE("Offloading only allowed with music stream");
1457 return 0;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301458 }
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301459
1460 if ((stream == AUDIO_STREAM_VOICE_CALL) &&
1461 (channelMask == 1) &&
Shiv Maliyappanahallid77d3b32016-02-02 13:45:50 -08001462 (samplingRate == 8000 || samplingRate == 16000 ||
1463 samplingRate == 32000 || samplingRate == 48000)) {
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301464 // Allow Voip direct output only if:
1465 // audio mode is MODE_IN_COMMUNCATION; AND
1466 // voip output is not opened already; AND
1467 // requested sample rate matches with that of voip input stream (if opened already)
1468 int value = 0;
1469 uint32_t mode = 0, voipOutCount = 1, voipSampleRate = 1;
1470 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1471 String8("audio_mode"));
1472 AudioParameter result = AudioParameter(valueStr);
1473 if (result.getInt(String8("audio_mode"), value) == NO_ERROR) {
1474 mode = value;
1475 }
1476
1477 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1478 String8("voip_out_stream_count"));
1479 result = AudioParameter(valueStr);
1480 if (result.getInt(String8("voip_out_stream_count"), value) == NO_ERROR) {
1481 voipOutCount = value;
1482 }
1483
1484 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1485 String8("voip_sample_rate"));
1486 result = AudioParameter(valueStr);
1487 if (result.getInt(String8("voip_sample_rate"), value) == NO_ERROR) {
1488 voipSampleRate = value;
1489 }
1490
1491 if ((mode == AUDIO_MODE_IN_COMMUNICATION) && (voipOutCount == 0) &&
1492 ((voipSampleRate == 0) || (voipSampleRate == samplingRate))) {
1493 if (audio_is_linear_pcm(format)) {
1494 char propValue[PROPERTY_VALUE_MAX] = {0};
1495 property_get("use.voice.path.for.pcm.voip", propValue, "0");
1496 bool voipPcmSysPropEnabled = !strncmp("true", propValue, sizeof("true"));
1497 if (voipPcmSysPropEnabled && (format == AUDIO_FORMAT_PCM_16_BIT)) {
Karthik Reddy Katta3fb9be72015-11-25 11:18:27 +05301498 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX |
1499 AUDIO_OUTPUT_FLAG_DIRECT);
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301500 ALOGD("Set VoIP and Direct output flags for PCM format");
1501 }
1502 }
1503 }
1504 }
1505
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301506#ifdef VOICE_CONCURRENCY
1507 char propValue[PROPERTY_VALUE_MAX];
1508 bool prop_play_enabled=false, prop_voip_enabled = false;
1509
1510 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1511 prop_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001512 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301513
1514 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1515 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1516 }
1517
1518 if (prop_play_enabled && mvoice_call_state) {
1519 //check if voice call is active / running in background
1520 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1521 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1522 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1523 {
1524 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1525 if(prop_voip_enabled) {
1526 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1527 flags );
1528 return 0;
1529 }
1530 }
1531 else {
1532 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
1533 ALOGD("voice_conc:IN call mode adding ULL flags .. flags: %x ", flags );
1534 flags = AUDIO_OUTPUT_FLAG_FAST;
1535 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
1536 if (AUDIO_STREAM_MUSIC == stream) {
1537 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1538 ALOGD("voice_conc:IN call mode adding deep-buffer flags %x ", flags );
1539 }
1540 else {
1541 flags = AUDIO_OUTPUT_FLAG_FAST;
1542 ALOGD("voice_conc:IN call mode adding fast flags %x ", flags );
1543 }
1544 }
1545 }
1546 }
1547 } else if (prop_voip_enabled && mvoice_call_state) {
1548 //check if voice call is active / running in background
1549 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1550 //return only ULL ouput
1551 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1552 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1553 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1554 {
1555 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1556 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1557 flags );
1558 return 0;
1559 }
1560 }
1561 }
1562#endif
1563#ifdef RECORD_PLAY_CONCURRENCY
1564 char recConcPropValue[PROPERTY_VALUE_MAX];
1565 bool prop_rec_play_enabled = false;
1566
1567 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
1568 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
1569 }
1570 if ((prop_rec_play_enabled) &&
1571 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
1572 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
1573 if (AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1574 // allow VoIP using voice path
1575 // Do nothing
1576 } else if((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1577 ALOGD("voice_conc:MODE_IN_COMM is setforcing deep buffer output for non ULL... flags: %x", flags);
1578 // use deep buffer path for all non ULL outputs
1579 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1580 }
1581 } else if ((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1582 ALOGD("voice_conc:Record mode is on forcing deep buffer output for non ULL... flags: %x ", flags);
1583 // use deep buffer path for all non ULL outputs
1584 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1585 }
1586 }
1587 if (prop_rec_play_enabled &&
1588 (stream == AUDIO_STREAM_ENFORCED_AUDIBLE)) {
1589 ALOGD("Record conc is on forcing ULL output for ENFORCED_AUDIBLE");
1590 flags = AUDIO_OUTPUT_FLAG_FAST;
1591 }
1592#endif
1593
Sharad Sangle4509cef2015-08-19 20:47:12 +05301594#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Sharad Sangle36781612015-05-28 16:15:16 +05301595 /*
1596 * WFD audio routes back to target speaker when starting a ringtone playback.
1597 * This is because primary output is reused for ringtone, so output device is
1598 * updated based on SONIFICATION strategy for both ringtone and music playback.
1599 * The same issue is not seen on remoted_submix HAL based WFD audio because
1600 * primary output is not reused and a new output is created for ringtone playback.
1601 * Issue is fixed by updating output flag to AUDIO_OUTPUT_FLAG_FAST when there is
1602 * a non-music stream playback on WFD, so primary output is not reused for ringtone.
1603 */
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001604 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
1605 if ((availableOutputDeviceTypes & AUDIO_DEVICE_OUT_PROXY)
1606 && (stream != AUDIO_STREAM_MUSIC)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301607 ALOGD("WFD audio: use OUTPUT_FLAG_FAST for non music stream. flags:%x", flags );
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001608 //For voip paths
1609 if(flags & AUDIO_OUTPUT_FLAG_DIRECT)
1610 flags = AUDIO_OUTPUT_FLAG_DIRECT;
1611 else //route every thing else to ULL path
1612 flags = AUDIO_OUTPUT_FLAG_FAST;
1613 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301614#endif
1615
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001616 // open a direct output if required by specified parameters
vivek mehta0ea887a2015-08-26 14:01:20 -07001617 // force direct flag if offload flag is set: offloading implies a direct output stream
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001618 // and all common behaviors are driven by checking only the direct flag
1619 // this should normally be set appropriately in the policy configuration file
1620 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1621 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1622 }
1623 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1624 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1625 }
vivek mehta0ea887a2015-08-26 14:01:20 -07001626
vivek mehta4b0d8192015-10-16 00:25:59 -07001627 bool forced_deep = false;
Sharad Sangle36781612015-05-28 16:15:16 +05301628 // only allow deep buffering for music stream type
1629 if (stream != AUDIO_STREAM_MUSIC) {
1630 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Sharad Sangle497aef82015-08-03 17:55:48 +05301631 } else if (/* stream == AUDIO_STREAM_MUSIC && */
1632 flags == AUDIO_OUTPUT_FLAG_NONE &&
1633 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
vivek mehtac984b992015-11-25 13:28:55 -08001634 forced_deep = true;
Sharad Sangle36781612015-05-28 16:15:16 +05301635 }
Sharad Sangle497aef82015-08-03 17:55:48 +05301636
Sharad Sangle36781612015-05-28 16:15:16 +05301637 if (stream == AUDIO_STREAM_TTS) {
1638 flags = AUDIO_OUTPUT_FLAG_TTS;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001639 }
1640
Zhou Song2b3d1552016-03-21 17:21:04 +08001641 // check if direct output for track offload already exits
1642 bool is_track_offload_active = false;
1643 for (size_t i = 0; i < mOutputs.size(); i++) {
1644 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1645 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) {
1646 is_track_offload_active = true;
1647 ALOGD("Track offload already active");
1648 break;
1649 }
1650 }
1651
vivek mehta4b0d8192015-10-16 00:25:59 -07001652 // Do offload magic here
vivek mehtac984b992015-11-25 13:28:55 -08001653 if ((flags == AUDIO_OUTPUT_FLAG_NONE) &&
1654 (stream == AUDIO_STREAM_MUSIC) &&
Zhou Song2b3d1552016-03-21 17:21:04 +08001655 (offloadInfo != NULL) && !is_track_offload_active &&
vivek mehta4b0d8192015-10-16 00:25:59 -07001656 ((offloadInfo->usage == AUDIO_USAGE_MEDIA) || (offloadInfo->usage == AUDIO_USAGE_GAME))) {
vivek mehtac984b992015-11-25 13:28:55 -08001657 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DIRECT_PCM);
vivek mehta4b0d8192015-10-16 00:25:59 -07001658 ALOGD("AudioCustomHAL --> Force Direct Flag .. flag (0x%x)", flags);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301659 }
1660
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001661 sp<IOProfile> profile;
1662
1663 // skip direct output selection if the request can obviously be attached to a mixed output
1664 // and not explicitly requested
vivek mehtac984b992015-11-25 13:28:55 -08001665 if (((flags & (AUDIO_OUTPUT_FLAG_DIRECT|AUDIO_OUTPUT_FLAG_DIRECT_PCM)) == 0) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301666 audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX &&
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001667 audio_channel_count_from_out_mask(channelMask) <= 2) {
1668 goto non_direct_output;
1669 }
1670
1671 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1672 // creating an offloaded track and tearing it down immediately after start when audioflinger
1673 // detects there is an active non offloadable effect.
1674 // FIXME: We should check the audio session here but we do not have it in this context.
1675 // This may prevent offloading in rare situations where effects are left active by apps
1676 // in the background.
Weiyin Jiang6bba3202016-03-03 14:49:36 +08001677 //
1678 // Supplementary annotation:
1679 // For sake of track offload introduced, we need a rollback for both compress offload
1680 // and track offload use cases.
1681 if ((flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD|AUDIO_OUTPUT_FLAG_DIRECT_PCM)) &&
1682 (mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
1683 ALOGD("non offloadable effect is enabled, try with non direct output");
1684 goto non_direct_output;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001685 }
1686
Weiyin Jiang6bba3202016-03-03 14:49:36 +08001687 profile = getProfileForDirectOutput(device,
1688 samplingRate,
1689 format,
1690 channelMask,
1691 (audio_output_flags_t)flags);
1692
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001693 if (profile != 0) {
vivek mehtac984b992015-11-25 13:28:55 -08001694
1695 if (!(flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301696 (profile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT_PCM)) {
vivek mehtac984b992015-11-25 13:28:55 -08001697 ALOGI("got Direct_PCM without requesting ... reject ");
1698 profile = NULL;
1699 goto non_direct_output;
1700 }
1701
Sharad Sangle36781612015-05-28 16:15:16 +05301702 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001703
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001704 // if multiple concurrent offload decode is supported
1705 // do no check for reuse and also don't close previous output if its offload
1706 // previous output will be closed during track destruction
1707 if (!(property_get_bool("audio.offload.multiple.enabled", false) &&
1708 ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0))) {
1709 for (size_t i = 0; i < mOutputs.size(); i++) {
1710 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1711 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1712 outputDesc = desc;
1713 // reuse direct output if currently open and configured with same parameters
1714 if ((samplingRate == outputDesc->mSamplingRate) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301715 audio_formats_match(format, outputDesc->mFormat) &&
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001716 (channelMask == outputDesc->mChannelMask)) {
1717 outputDesc->mDirectOpenCount++;
1718 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1719 return mOutputs.keyAt(i);
1720 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001721 }
1722 }
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001723 // close direct output if currently open and configured with different parameters
1724 if (outputDesc != NULL) {
1725 closeOutput(outputDesc->mIoHandle);
1726 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001727 }
Sharad Sangle36781612015-05-28 16:15:16 +05301728
1729 // if the selected profile is offloaded and no offload info was specified,
1730 // create a default one
1731 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
Ashish Jain40cab3a2016-03-23 11:14:14 +05301732 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
Sharad Sangle36781612015-05-28 16:15:16 +05301733 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1734 defaultOffloadInfo.sample_rate = samplingRate;
1735 defaultOffloadInfo.channel_mask = channelMask;
1736 defaultOffloadInfo.format = format;
1737 defaultOffloadInfo.stream_type = stream;
1738 defaultOffloadInfo.bit_rate = 0;
1739 defaultOffloadInfo.duration_us = -1;
1740 defaultOffloadInfo.has_video = true; // conservative
1741 defaultOffloadInfo.is_streaming = true; // likely
1742 offloadInfo = &defaultOffloadInfo;
1743 }
1744
1745 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001746 outputDesc->mDevice = device;
1747 outputDesc->mLatency = 0;
Sharad Sangle36781612015-05-28 16:15:16 +05301748 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001749 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1750 config.sample_rate = samplingRate;
1751 config.channel_mask = channelMask;
1752 config.format = format;
1753 if (offloadInfo != NULL) {
1754 config.offload_info = *offloadInfo;
1755 }
Sharad Sangle36781612015-05-28 16:15:16 +05301756 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001757 &output,
1758 &config,
1759 &outputDesc->mDevice,
1760 String8(""),
1761 &outputDesc->mLatency,
1762 outputDesc->mFlags);
1763
1764 // only accept an output with the requested parameters
1765 if (status != NO_ERROR ||
1766 (samplingRate != 0 && samplingRate != config.sample_rate) ||
Ashish Jain40cab3a2016-03-23 11:14:14 +05301767 (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) ||
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001768 (channelMask != 0 && channelMask != config.channel_mask)) {
1769 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1770 "format %d %d, channelMask %04x %04x", output, samplingRate,
1771 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1772 outputDesc->mChannelMask);
1773 if (output != AUDIO_IO_HANDLE_NONE) {
1774 mpClientInterface->closeOutput(output);
1775 }
Sharad Sangle36781612015-05-28 16:15:16 +05301776 // fall back to mixer output if possible when the direct output could not be open
Ashish Jain40cab3a2016-03-23 11:14:14 +05301777 if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) {
Sharad Sangle36781612015-05-28 16:15:16 +05301778 goto non_direct_output;
1779 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001780 return AUDIO_IO_HANDLE_NONE;
1781 }
1782 outputDesc->mSamplingRate = config.sample_rate;
1783 outputDesc->mChannelMask = config.channel_mask;
1784 outputDesc->mFormat = config.format;
1785 outputDesc->mRefCount[stream] = 0;
1786 outputDesc->mStopTime[stream] = 0;
1787 outputDesc->mDirectOpenCount = 1;
1788
1789 audio_io_handle_t srcOutput = getOutputForEffect();
1790 addOutput(output, outputDesc);
1791 audio_io_handle_t dstOutput = getOutputForEffect();
1792 if (dstOutput == output) {
Gao Jiedb057832015-11-12 08:51:22 +08001793#ifdef DOLBY_ENABLE
1794 status_t status = mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1795 if (status == NO_ERROR) {
1796 for (size_t i = 0; i < mEffects.size(); i++) {
1797 sp<EffectDescriptor> desc = mEffects.valueAt(i);
1798 if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX) {
1799 // update the mIo member of EffectDescriptor for the global effect
1800 ALOGV("%s updating mIo", __FUNCTION__);
1801 desc->mIo = dstOutput;
1802 }
1803 }
1804 } else {
1805 ALOGW("%s moveEffects from %d to %d failed", __FUNCTION__, srcOutput, dstOutput);
1806 }
1807#else // DOLBY_END
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001808 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
Gao Jiedb057832015-11-12 08:51:22 +08001809#endif // LINE_ADDED_BY_DOLBY
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001810 }
1811 mPreviousOutputs = mOutputs;
1812 ALOGV("getOutput() returns new direct output %d", output);
1813 mpClientInterface->onAudioPortListUpdate();
1814 return output;
1815 }
1816
1817non_direct_output:
Ashish Jain5fe12412016-04-18 18:44:17 +05301818
1819 // A request for HW A/V sync cannot fallback to a mixed output because time
1820 // stamps are embedded in audio data
1821 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1822 return AUDIO_IO_HANDLE_NONE;
1823 }
1824
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001825 // ignoring channel mask due to downmix capability in mixer
1826
1827 // open a non direct output
1828
1829 // for non direct outputs, only PCM is supported
1830 if (audio_is_linear_pcm(format)) {
1831 // get which output is suitable for the specified stream. The actual
1832 // routing change will happen when startOutput() will be called
1833 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1834
1835 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1836 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
Ashish Jain660d3112016-06-20 10:16:51 +05301837
1838 if (forced_deep) {
1839 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
1840 ALOGI("setting force DEEP buffer now ");
1841 } else if(flags == AUDIO_OUTPUT_FLAG_NONE) {
1842 // no deep buffer playback is requested hence fallback to primary
1843 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_PRIMARY);
1844 ALOGI("FLAG None hence request for a primary output");
1845 }
1846
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001847 output = selectOutput(outputs, flags, format);
1848 }
1849 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1850 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1851
vivek mehta0ea887a2015-08-26 14:01:20 -07001852 ALOGV("getOutputForDevice() returns output %d", output);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001853
1854 return output;
1855}
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301856
1857status_t AudioPolicyManagerCustom::getInputForAttr(const audio_attributes_t *attr,
1858 audio_io_handle_t *input,
1859 audio_session_t session,
1860 uid_t uid,
1861 uint32_t samplingRate,
1862 audio_format_t format,
1863 audio_channel_mask_t channelMask,
1864 audio_input_flags_t flags,
1865 audio_port_handle_t selectedDeviceId,
1866 input_type_t *inputType)
1867{
Ashish Jain40cab3a2016-03-23 11:14:14 +05301868 audio_source_t inputSource;
1869 inputSource = attr->source;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301870#ifdef VOICE_CONCURRENCY
1871
1872 char propValue[PROPERTY_VALUE_MAX];
1873 bool prop_rec_enabled=false, prop_voip_enabled = false;
1874
1875 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
1876 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1877 }
1878
1879 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1880 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1881 }
1882
1883 if (prop_rec_enabled && mvoice_call_state) {
1884 //check if voice call is active / running in background
1885 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1886 //Need to block input request
1887 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1888 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1889 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1890 {
1891 switch(inputSource) {
1892 case AUDIO_SOURCE_VOICE_UPLINK:
1893 case AUDIO_SOURCE_VOICE_DOWNLINK:
1894 case AUDIO_SOURCE_VOICE_CALL:
1895 ALOGD("voice_conc:Creating input during incall mode for inputSource: %d",
1896 inputSource);
1897 break;
1898
1899 case AUDIO_SOURCE_VOICE_COMMUNICATION:
1900 if(prop_voip_enabled) {
1901 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1902 inputSource);
1903 return NO_INIT;
1904 }
1905 break;
1906 default:
1907 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1908 inputSource);
1909 return NO_INIT;
1910 }
1911 }
1912 }//check for VoIP flag
1913 else if(prop_voip_enabled && mvoice_call_state) {
1914 //check if voice call is active / running in background
1915 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1916 //Need to block input request
1917 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1918 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1919 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1920 {
1921 if(inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1922 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
1923 return NO_INIT;
1924 }
1925 }
1926 }
1927
1928#endif
1929
1930 return AudioPolicyManager::getInputForAttr(attr,
1931 input,
1932 session,
1933 uid,
1934 samplingRate,
1935 format,
1936 channelMask,
1937 flags,
1938 selectedDeviceId,
1939 inputType);
1940}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001941
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301942status_t AudioPolicyManagerCustom::startInput(audio_io_handle_t input,
1943 audio_session_t session)
1944{
1945 ALOGV("startInput() input %d", input);
1946 ssize_t index = mInputs.indexOfKey(input);
1947 if (index < 0) {
1948 ALOGW("startInput() unknown input %d", input);
1949 return BAD_VALUE;
1950 }
1951 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1952
Ashish Jain40cab3a2016-03-23 11:14:14 +05301953 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1954 if (audioSession == 0) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301955 ALOGW("startInput() unknown session %d on input %d", session, input);
1956 return BAD_VALUE;
1957 }
1958
1959 // virtual input devices are compatible with other input devices
1960 if (!is_virtual_input_device(inputDesc->mDevice)) {
1961
1962 // for a non-virtual input device, check if there is another (non-virtual) active input
1963 audio_io_handle_t activeInput = mInputs.getActiveInput();
1964 if (activeInput != 0 && activeInput != input) {
1965
1966 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1967 // otherwise the active input continues and the new input cannot be started.
1968 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Ashish Jain40cab3a2016-03-23 11:14:14 +05301969 if ((activeDesc->inputSource() == AUDIO_SOURCE_HOTWORD) &&
1970 !activeDesc->hasPreemptedSession(session)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301971 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
Ashish Jain40cab3a2016-03-23 11:14:14 +05301972 //FIXME: consider all active sessions
1973 AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions();
1974 audio_session_t activeSession = activeSessions.keyAt(0);
1975 SortedVector<audio_session_t> sessions =
1976 activeDesc->getPreemptedSessions();
1977 sessions.add(activeSession);
1978 inputDesc->setPreemptedSessions(sessions);
1979 stopInput(activeInput, activeSession);
1980 releaseInput(activeInput, activeSession);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301981 } else {
1982 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
1983 return INVALID_OPERATION;
1984 }
1985 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05301986 // Do not allow capture if an active voice call is using a software patch and
1987 // the call TX source device is on the same HW module.
1988 // FIXME: would be better to refine to only inputs whose profile connects to the
1989 // call TX device but this information is not in the audio patch
1990 if (mCallTxPatch != 0 &&
1991 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1992 return INVALID_OPERATION;
1993 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301994 }
1995
1996 // Routing?
1997 mInputRoutes.incRouteActivity(session);
1998#ifdef RECORD_PLAY_CONCURRENCY
1999 mIsInputRequestOnProgress = true;
2000
2001 char getPropValue[PROPERTY_VALUE_MAX];
2002 bool prop_rec_play_enabled = false;
2003
2004 if (property_get("rec.playback.conc.disabled", getPropValue, NULL)) {
2005 prop_rec_play_enabled = atoi(getPropValue) || !strncmp("true", getPropValue, 4);
2006 }
2007
2008 if ((prop_rec_play_enabled) &&(mInputs.activeInputsCount() == 0)){
2009 // send update to HAL on record playback concurrency
2010 AudioParameter param = AudioParameter();
2011 param.add(String8("rec_play_conc_on"), String8("true"));
2012 ALOGD("startInput() setParameters rec_play_conc is setting to ON ");
2013 mpClientInterface->setParameters(0, param.toString());
2014
2015 // Call invalidate to reset all opened non ULL audio tracks
2016 // Move tracks associated to this strategy from previous output to new output
2017 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
2018 // Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder)
Sharad Sangle4509cef2015-08-19 20:47:12 +05302019 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE && (i != AUDIO_STREAM_PATCH))) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302020 ALOGD("Invalidate on releaseInput for stream :: %d ", i);
2021 //FIXME see fixme on name change
2022 mpClientInterface->invalidateStream((audio_stream_type_t)i);
2023 }
2024 }
2025 // close compress tracks
2026 for (size_t i = 0; i < mOutputs.size(); i++) {
2027 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
2028 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
2029 ALOGD("ouput desc / profile is NULL");
2030 continue;
2031 }
2032 if (outputDesc->mProfile->mFlags
2033 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2034 // close compress sessions
2035 ALOGD("calling closeOutput on record conc for COMPRESS output");
2036 closeOutput(mOutputs.keyAt(i));
2037 }
2038 }
Ashish Jaine4a22d52016-06-22 11:55:08 +05302039 // If effects where present on any of the above closed outputs,
2040 // audioflinger moved them to the primary output by default
2041 // move them back to the appropriate output.
2042 moveGlobalEffect();
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302043 }
2044#endif
2045
Ashish Jain40cab3a2016-03-23 11:14:14 +05302046 if (!inputDesc->isActive() || mInputRoutes.hasRouteChanged(session)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302047 // if input maps to a dynamic policy with an activity listener, notify of state change
Ashish Jain630c5e12016-04-18 11:50:21 +05302048 if ((inputDesc->mPolicyMix != NULL)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302049 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Ashish Jain5fe12412016-04-18 18:44:17 +05302050 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302051 MIX_STATE_MIXING);
Ashish Jain630c5e12016-04-18 11:50:21 +05302052 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302053
2054 if (mInputs.activeInputsCount() == 0) {
2055 SoundTrigger::setCaptureState(true);
2056 }
2057 setInputDevice(input, getNewInputDevice(input), true /* force */);
2058
2059 // automatically enable the remote submix output when input is started if not
2060 // used by a policy mix of type MIX_TYPE_RECORDERS
2061 // For remote submix (a virtual device), we open only one input per capture request.
2062 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
2063 String8 address = String8("");
2064 if (inputDesc->mPolicyMix == NULL) {
2065 address = String8("0");
Ashish Jain630c5e12016-04-18 11:50:21 +05302066 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
Ashish Jain5fe12412016-04-18 18:44:17 +05302067 address = inputDesc->mPolicyMix->mDeviceAddress;
Ashish Jain630c5e12016-04-18 11:50:21 +05302068 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302069 if (address != "") {
2070 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2071 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2072 address, "remote-submix");
2073 }
2074 }
2075 }
2076
Ashish Jain40cab3a2016-03-23 11:14:14 +05302077 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302078
Ashish Jain40cab3a2016-03-23 11:14:14 +05302079 audioSession->changeActiveCount(1);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302080#ifdef RECORD_PLAY_CONCURRENCY
2081 mIsInputRequestOnProgress = false;
2082#endif
2083 return NO_ERROR;
2084}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08002085
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302086status_t AudioPolicyManagerCustom::stopInput(audio_io_handle_t input,
2087 audio_session_t session)
2088{
2089 status_t status;
2090 status = AudioPolicyManager::stopInput(input, session);
2091#ifdef RECORD_PLAY_CONCURRENCY
2092 char propValue[PROPERTY_VALUE_MAX];
2093 bool prop_rec_play_enabled = false;
2094
2095 if (property_get("rec.playback.conc.disabled", propValue, NULL)) {
2096 prop_rec_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
2097 }
2098
2099 if ((prop_rec_play_enabled) && (mInputs.activeInputsCount() == 0)) {
2100
2101 //send update to HAL on record playback concurrency
2102 AudioParameter param = AudioParameter();
2103 param.add(String8("rec_play_conc_on"), String8("false"));
2104 ALOGD("stopInput() setParameters rec_play_conc is setting to OFF ");
2105 mpClientInterface->setParameters(0, param.toString());
2106
2107 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
2108 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
2109 //Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder stop tone)
2110 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE) && (i != AUDIO_STREAM_PATCH)) {
2111 ALOGD(" Invalidate on stopInput for stream :: %d ", i);
2112 //FIXME see fixme on name change
2113 mpClientInterface->invalidateStream((audio_stream_type_t)i);
2114 }
2115 }
2116 }
2117#endif
2118 return status;
2119}
2120
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302121void AudioPolicyManagerCustom::closeAllInputs() {
2122 bool patchRemoved = false;
2123
Dhanalakshmi Siddanibfeca7f2015-10-08 15:17:11 +05302124 for(size_t input_index = mInputs.size(); input_index > 0; input_index--) {
2125 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index-1);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302126 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302127 if (patch_index >= 0) {
2128 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302129 status_t status;
2130 status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302131 mAudioPatches.removeItemsAt(patch_index);
2132 patchRemoved = true;
2133 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05302134 if ((inputDesc->getOpenRefCount() > 0) && inputDesc->isSoundTrigger()
2135 && (mInputs.size() == 1)) {
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302136 ALOGD("Do not close sound trigger input handle");
2137 } else {
Dhanalakshmi Siddanibfeca7f2015-10-08 15:17:11 +05302138 mpClientInterface->closeInput(mInputs.keyAt(input_index-1));
2139 mInputs.removeItem(mInputs.keyAt(input_index-1));
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302140 }
2141 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05302142 mInputs.clear();
2143 SoundTrigger::setCaptureState(false);
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302144 nextAudioPortGeneration();
2145
2146 if (patchRemoved) {
2147 mpClientInterface->onAudioPatchListUpdate();
2148 }
2149}
2150
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302151AudioPolicyManagerCustom::AudioPolicyManagerCustom(AudioPolicyClientInterface *clientInterface)
Sharad Sangle4509cef2015-08-19 20:47:12 +05302152 : AudioPolicyManager(clientInterface),
2153 mHdmiAudioDisabled(false),
2154 mHdmiAudioEvent(false),
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08002155 mPrevPhoneState(0),
Haynes Mathew George603ae9b2015-12-21 17:23:59 -08002156 mPrevFMVolumeDb(0.0f),
2157 mFMIsActive(false)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302158{
Ashish Jain40cab3a2016-03-23 11:14:14 +05302159
Satya Krishna Pindiproli8f83f102016-06-21 17:56:14 +05302160#ifdef USE_XML_AUDIO_POLICY_CONF
2161 ALOGD("USE_XML_AUDIO_POLICY_CONF is TRUE");
2162#else
2163 ALOGD("USE_XML_AUDIO_POLICY_CONF is FALSE");
2164#endif
2165
Ashish Jain40cab3a2016-03-23 11:14:14 +05302166 //TODO: Check the new logic to parse policy conf and update the below code
2167 // Need this when SSR encoding is enabled
Mingming Yin38ea08c2015-10-05 15:24:04 -07002168 char ssr_enabled[PROPERTY_VALUE_MAX] = {0};
2169 bool prop_ssr_enabled = false;
2170
2171 if (property_get("ro.qc.sdk.audio.ssr", ssr_enabled, NULL)) {
2172 prop_ssr_enabled = atoi(ssr_enabled) || !strncmp("true", ssr_enabled, 4);
2173 }
2174
2175 for (size_t i = 0; i < mHwModules.size(); i++) {
Ashish Jain40cab3a2016-03-23 11:14:14 +05302176 ALOGV("Hw module %zu", i);
Mingming Yin38ea08c2015-10-05 15:24:04 -07002177 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) {
2178 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Ashish Jain40cab3a2016-03-23 11:14:14 +05302179 AudioProfileVector profiles = inProfile->getAudioProfiles();
2180 for (size_t k = 0; k < profiles.size(); k++){
2181 ChannelsVector channels = profiles[k]->getChannels();
2182 for (size_t x = 0; x < channels.size(); x++) {
2183 audio_channel_mask_t channelMask = channels[x];
2184 ALOGV("Channel Mask %x size %zu", channelMask,
2185 channels.size());
2186 if (AUDIO_CHANNEL_IN_5POINT1 == channelMask) {
2187 if (!prop_ssr_enabled) {
2188 ALOGI("removing AUDIO_CHANNEL_IN_5POINT1 from"
2189 " input profile as SSR(surround sound record)"
2190 " is not supported on this chipset variant");
2191 channels.removeItemsAt(x, 1);
2192 ALOGV("Channel Mask size now %zu",
2193 channels.size());
2194 }
Mingming Yin38ea08c2015-10-05 15:24:04 -07002195 }
2196 }
2197 }
2198 }
2199 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302200#ifdef RECORD_PLAY_CONCURRENCY
2201 mIsInputRequestOnProgress = false;
2202#endif
2203
2204
2205#ifdef VOICE_CONCURRENCY
2206 mFallBackflag = getFallBackPath();
2207#endif
2208}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07002209}