blob: 9b950d9451e51c56f27a55d46ef26e3e8fcf49fe [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*/);
Naresh Tanniru11393702016-09-14 11:25:37 +05301076 bool force = desc->device() != dev;
Zhou Song5dcddc92015-09-21 14:36:57 +08001077 uint32_t delayMs;
1078 if (dev == prevDevice) {
1079 delayMs = 0;
1080 } else {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301081 delayMs = outputDesc->latency()*2;
Zhou Song5dcddc92015-09-21 14:36:57 +08001082 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301083 setOutputDevice(desc,
1084 dev,
Naresh Tanniru11393702016-09-14 11:25:37 +05301085 force,
Zhou Song5dcddc92015-09-21 14:36:57 +08001086 delayMs);
Sharad Sangle36781612015-05-28 16:15:16 +05301087 }
1088 }
1089 // update the outputs if stopping one with a stream that can affect notification routing
1090 handleNotificationRoutingForStream(stream);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001091 }
Sharad Sangle36781612015-05-28 16:15:16 +05301092 return NO_ERROR;
1093 } else {
1094 ALOGW("stopOutput() refcount is already 0");
1095 return INVALID_OPERATION;
1096 }
1097}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001098
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301099status_t AudioPolicyManagerCustom::startSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301100 audio_stream_type_t stream,
1101 audio_devices_t device,
Lalit Kansaraf803ac52016-05-09 19:12:01 +05301102 const char *address,
Sharad Sangle36781612015-05-28 16:15:16 +05301103 uint32_t *delayMs)
1104{
1105 // cannot start playback of STREAM_TTS if any other output is being used
1106 uint32_t beaconMuteLatency = 0;
1107
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301108 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1109 ALOGW("startSource() invalid stream %d", stream);
1110 return INVALID_OPERATION;
1111 }
1112
Sharad Sangle36781612015-05-28 16:15:16 +05301113 *delayMs = 0;
1114 if (stream == AUDIO_STREAM_TTS) {
1115 ALOGV("\t found BEACON stream");
Ashish Jain40cab3a2016-03-23 11:14:14 +05301116 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301117 return INVALID_OPERATION;
1118 } else {
1119 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001120 }
Sharad Sangle36781612015-05-28 16:15:16 +05301121 } else {
1122 // some playback other than beacon starts
1123 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1124 }
1125
Ashish Jain40cab3a2016-03-23 11:14:14 +05301126 // check active before incrementing usage count
1127 bool force = !outputDesc->isActive();
1128
Sharad Sangle36781612015-05-28 16:15:16 +05301129 // increment usage count for this stream on the requested output:
1130 // NOTE that the usage count is the same for duplicated output and hardware output which is
1131 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1132 outputDesc->changeRefCount(stream, 1);
1133
1134 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
1135 // starting an output being rerouted?
1136 if (device == AUDIO_DEVICE_NONE) {
1137 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001138 }
Sharad Sangle36781612015-05-28 16:15:16 +05301139 routing_strategy strategy = getStrategy(stream);
1140 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
1141 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1142 (beaconMuteLatency > 0);
1143 uint32_t waitMs = beaconMuteLatency;
Sharad Sangle36781612015-05-28 16:15:16 +05301144 for (size_t i = 0; i < mOutputs.size(); i++) {
1145 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1146 if (desc != outputDesc) {
1147 // force a device change if any other output is managed by the same hw
1148 // module and has a current device selection that differs from selected device.
1149 // In this case, the audio HAL must receive the new device selection so that it can
1150 // change the device currently selected by the other active output.
1151 if (outputDesc->sharesHwModuleWith(desc) &&
1152 desc->device() != device) {
1153 force = true;
1154 }
1155 // wait for audio on other active outputs to be presented when starting
1156 // a notification so that audio focus effect can propagate, or that a mute/unmute
1157 // event occurred for beacon
1158 uint32_t latency = desc->latency();
1159 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1160 waitMs = latency;
1161 }
1162 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001163 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05301164 uint32_t muteWaitMs;
Lalit Kansaraf803ac52016-05-09 19:12:01 +05301165 muteWaitMs = setOutputDevice(outputDesc, device, force, 0, NULL, address);
Sharad Sangle36781612015-05-28 16:15:16 +05301166
1167 // handle special case for sonification while in call
1168 if (isInCall()) {
1169 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001170 }
Sharad Sangle36781612015-05-28 16:15:16 +05301171
1172 // apply volume rules for current stream and device if necessary
1173 checkAndSetVolume(stream,
Ashish Jain40cab3a2016-03-23 11:14:14 +05301174 mVolumeCurves->getVolumeIndex(stream, device),
Sharad Sangle36781612015-05-28 16:15:16 +05301175 outputDesc,
1176 device);
1177
1178 // update the outputs if starting an output with a stream that can affect notification
1179 // routing
1180 handleNotificationRoutingForStream(stream);
1181
1182 // force reevaluating accessibility routing when ringtone or alarm starts
1183 if (strategy == STRATEGY_SONIFICATION) {
1184 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1185 }
1186 }
1187 else {
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001188 // handle special case for sonification while in call
1189 if (isInCall()) {
1190 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Sharad Sangle36781612015-05-28 16:15:16 +05301191 }
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001192 }
Sharad Sangle36781612015-05-28 16:15:16 +05301193 return NO_ERROR;
1194}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001195
Sharad Sangle36781612015-05-28 16:15:16 +05301196void AudioPolicyManagerCustom::handleIncallSonification(audio_stream_type_t stream,
1197 bool starting, bool stateChange,
1198 audio_io_handle_t output)
1199{
1200 if(!hasPrimaryOutput()) {
1201 return;
1202 }
1203 // no action needed for AUDIO_STREAM_PATCH stream type, it's for internal flinger tracks
1204 if (stream == AUDIO_STREAM_PATCH) {
1205 return;
1206 }
1207 // if the stream pertains to sonification strategy and we are in call we must
1208 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
1209 // in the device used for phone strategy and play the tone if the selected device does not
1210 // interfere with the device used for phone strategy
1211 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
1212 // many times as there are active tracks on the output
1213 const routing_strategy stream_strategy = getStrategy(stream);
1214 if ((stream_strategy == STRATEGY_SONIFICATION) ||
1215 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
1216 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
1217 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
1218 stream, starting, outputDesc->mDevice, stateChange);
1219 if (outputDesc->mRefCount[stream]) {
1220 int muteCount = 1;
1221 if (stateChange) {
1222 muteCount = outputDesc->mRefCount[stream];
1223 }
1224 if (audio_is_low_visibility(stream)) {
1225 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
1226 for (int i = 0; i < muteCount; i++) {
1227 setStreamMute(stream, starting, outputDesc);
1228 }
1229 } else {
1230 ALOGV("handleIncallSonification() high visibility");
1231 if (outputDesc->device() &
1232 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
1233 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
1234 for (int i = 0; i < muteCount; i++) {
1235 setStreamMute(stream, starting, outputDesc);
1236 }
1237 }
1238 if (starting) {
1239 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
1240 AUDIO_STREAM_VOICE_CALL);
1241 } else {
1242 mpClientInterface->stopTone();
1243 }
1244 }
1245 }
1246 }
1247}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001248
Sharad Sangle36781612015-05-28 16:15:16 +05301249void AudioPolicyManagerCustom::handleNotificationRoutingForStream(audio_stream_type_t stream) {
1250 switch(stream) {
1251 case AUDIO_STREAM_MUSIC:
1252 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
1253 updateDevicesAndOutputs();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001254 break;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001255 default:
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001256 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001257 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001258}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001259
Sharad Sangle36781612015-05-28 16:15:16 +05301260status_t AudioPolicyManagerCustom::checkAndSetVolume(audio_stream_type_t stream,
1261 int index,
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301262 const sp<AudioOutputDescriptor>& outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301263 audio_devices_t device,
1264 int delayMs, bool force)
1265{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301266 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1267 ALOGW("checkAndSetVolume() invalid stream %d", stream);
1268 return INVALID_OPERATION;
1269 }
Sharad Sangle36781612015-05-28 16:15:16 +05301270 // do not change actual stream volume if the stream is muted
1271 if (outputDesc->mMuteCount[stream] != 0) {
1272 ALOGVV("checkAndSetVolume() stream %d muted count %d",
1273 stream, outputDesc->mMuteCount[stream]);
1274 return NO_ERROR;
1275 }
1276 audio_policy_forced_cfg_t forceUseForComm =
1277 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
1278 // do not change in call volume if bluetooth is connected and vice versa
1279 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
1280 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
1281 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
1282 stream, forceUseForComm);
1283 return INVALID_OPERATION;
1284 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001285
Sharad Sangle36781612015-05-28 16:15:16 +05301286 if (device == AUDIO_DEVICE_NONE) {
1287 device = outputDesc->device();
1288 }
1289
1290 float volumeDb = computeVolume(stream, index, device);
1291 if (outputDesc->isFixedVolume(device)) {
1292 volumeDb = 0.0f;
1293 }
1294
1295 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
1296
1297 if (stream == AUDIO_STREAM_VOICE_CALL ||
1298 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
1299 float voiceVolume;
1300 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
1301 if (stream == AUDIO_STREAM_VOICE_CALL) {
Ashish Jain40cab3a2016-03-23 11:14:14 +05301302 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Sharad Sangle36781612015-05-28 16:15:16 +05301303 } else {
1304 voiceVolume = 1.0;
1305 }
1306
1307 if (voiceVolume != mLastVoiceVolume && ((outputDesc == mPrimaryOutput) ||
1308 isDirectOutput(outputDesc->mIoHandle) || device & AUDIO_DEVICE_OUT_ALL_USB)) {
1309 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
1310 mLastVoiceVolume = voiceVolume;
1311 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301312#ifdef FM_POWER_OPT
1313 } else if (stream == AUDIO_STREAM_MUSIC && hasPrimaryOutput() &&
Haynes Mathew George603ae9b2015-12-21 17:23:59 -08001314 outputDesc == mPrimaryOutput && mFMIsActive) {
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001315 /* Avoid unnecessary set_parameter calls as it puts the primary
1316 outputs FastMixer in HOT_IDLE leading to breaks in audio */
1317 if (volumeDb != mPrevFMVolumeDb) {
1318 mPrevFMVolumeDb = volumeDb;
1319 AudioParameter param = AudioParameter();
1320 param.addFloat(String8("fm_volume"), Volume::DbToAmpl(volumeDb));
Mingming Yin1433cc62016-01-04 14:58:00 -08001321 //Double delayMs to avoid sound burst while device switch.
1322 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString(), delayMs*2);
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001323 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301324#endif /* FM_POWER_OPT end */
Sharad Sangle36781612015-05-28 16:15:16 +05301325 }
1326
1327 return NO_ERROR;
1328}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001329
Sharad Sangle36781612015-05-28 16:15:16 +05301330bool AudioPolicyManagerCustom::isDirectOutput(audio_io_handle_t output) {
1331 for (size_t i = 0; i < mOutputs.size(); i++) {
1332 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1333 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1334 if ((curOutput == output) && (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
1335 return true;
1336 }
1337 }
1338 return false;
1339}
vivek mehta0ea887a2015-08-26 14:01:20 -07001340
Ashish Jainac510a72016-04-28 12:22:58 +05301341bool static isDirectPCMEnabled(int bitWidth)
1342{
1343 bool directPCMEnabled = false;
1344 if (bitWidth == 24 || bitWidth == 32)
1345 directPCMEnabled =
1346 property_get_bool("audio.offload.pcm.24bit.enable", false);
1347 else
1348 directPCMEnabled =
1349 property_get_bool("audio.offload.pcm.16bit.enable", false);
1350
1351 return directPCMEnabled;
1352}
1353
vivek mehta0ea887a2015-08-26 14:01:20 -07001354status_t AudioPolicyManagerCustom::getOutputForAttr(const audio_attributes_t *attr,
1355 audio_io_handle_t *output,
1356 audio_session_t session,
1357 audio_stream_type_t *stream,
1358 uid_t uid,
1359 uint32_t samplingRate,
1360 audio_format_t format,
1361 audio_channel_mask_t channelMask,
1362 audio_output_flags_t flags,
1363 audio_port_handle_t selectedDeviceId,
1364 const audio_offload_info_t *offloadInfo)
1365{
1366 audio_offload_info_t tOffloadInfo = AUDIO_INFO_INITIALIZER;
1367
Alexy Joseph0ac09032015-10-16 15:57:53 -07001368 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
Ashish Jainac510a72016-04-28 12:22:58 +05301369 uint32_t bitWidth = (audio_bytes_per_sample(format) * 8);
Alexy Joseph0ac09032015-10-16 15:57:53 -07001370
1371 if (offloadDisabled) {
1372 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
1373 }
1374
Ashish Jainac510a72016-04-28 12:22:58 +05301375 if (!offloadDisabled && (offloadInfo == NULL) &&
1376 isDirectPCMEnabled(bitWidth) &&
1377 (flags == AUDIO_OUTPUT_FLAG_NONE)) {
vivek mehta0ea887a2015-08-26 14:01:20 -07001378
vivek mehta0ea887a2015-08-26 14:01:20 -07001379 tOffloadInfo.sample_rate = samplingRate;
1380 tOffloadInfo.channel_mask = channelMask;
1381 tOffloadInfo.format = format;
1382 tOffloadInfo.stream_type = *stream;
Ashish Jainac510a72016-04-28 12:22:58 +05301383 tOffloadInfo.bit_width = bitWidth;
vivek mehta0ea887a2015-08-26 14:01:20 -07001384 if (attr != NULL) {
1385 ALOGV("found attribute .. setting usage %d ", attr->usage);
1386 tOffloadInfo.usage = attr->usage;
1387 } else {
Alexy Joseph0ac09032015-10-16 15:57:53 -07001388 ALOGI("%s:: attribute is NULL .. no usage set", __func__);
vivek mehta0ea887a2015-08-26 14:01:20 -07001389 }
1390 offloadInfo = &tOffloadInfo;
1391 }
1392
1393 return AudioPolicyManager::getOutputForAttr(attr, output, session, stream,
1394 (uid_t)uid, (uint32_t)samplingRate,
1395 format, (audio_channel_mask_t)channelMask,
1396 flags, (audio_port_handle_t)selectedDeviceId,
1397 offloadInfo);
1398}
1399
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001400audio_io_handle_t AudioPolicyManagerCustom::getOutputForDevice(
1401 audio_devices_t device,
Sharad Sangle36781612015-05-28 16:15:16 +05301402 audio_session_t session __unused,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001403 audio_stream_type_t stream,
1404 uint32_t samplingRate,
1405 audio_format_t format,
1406 audio_channel_mask_t channelMask,
1407 audio_output_flags_t flags,
1408 const audio_offload_info_t *offloadInfo)
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001409{
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001410 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001411 status_t status;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001412
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001413#ifdef AUDIO_POLICY_TEST
1414 if (mCurOutput != 0) {
1415 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1416 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1417
1418 if (mTestOutputs[mCurOutput] == 0) {
1419 ALOGV("getOutput() opening test output");
Sharad Sangle36781612015-05-28 16:15:16 +05301420 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
1421 mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001422 outputDesc->mDevice = mTestDevice;
1423 outputDesc->mLatency = mTestLatencyMs;
1424 outputDesc->mFlags =
1425 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
1426 outputDesc->mRefCount[stream] = 0;
1427 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1428 config.sample_rate = mTestSamplingRate;
1429 config.channel_mask = mTestChannels;
1430 config.format = mTestFormat;
1431 if (offloadInfo != NULL) {
1432 config.offload_info = *offloadInfo;
1433 }
1434 status = mpClientInterface->openOutput(0,
1435 &mTestOutputs[mCurOutput],
1436 &config,
1437 &outputDesc->mDevice,
1438 String8(""),
1439 &outputDesc->mLatency,
1440 outputDesc->mFlags);
1441 if (status == NO_ERROR) {
1442 outputDesc->mSamplingRate = config.sample_rate;
1443 outputDesc->mFormat = config.format;
1444 outputDesc->mChannelMask = config.channel_mask;
1445 AudioParameter outputCmd = AudioParameter();
1446 outputCmd.addInt(String8("set_id"),mCurOutput);
1447 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1448 addOutput(mTestOutputs[mCurOutput], outputDesc);
1449 }
1450 }
1451 return mTestOutputs[mCurOutput];
1452 }
1453#endif //AUDIO_POLICY_TEST
Sharad Sangle36781612015-05-28 16:15:16 +05301454 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1455 (stream != AUDIO_STREAM_MUSIC)) {
1456 // compress should not be used for non-music streams
1457 ALOGE("Offloading only allowed with music stream");
1458 return 0;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301459 }
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301460
1461 if ((stream == AUDIO_STREAM_VOICE_CALL) &&
1462 (channelMask == 1) &&
Shiv Maliyappanahallid77d3b32016-02-02 13:45:50 -08001463 (samplingRate == 8000 || samplingRate == 16000 ||
1464 samplingRate == 32000 || samplingRate == 48000)) {
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301465 // Allow Voip direct output only if:
1466 // audio mode is MODE_IN_COMMUNCATION; AND
1467 // voip output is not opened already; AND
1468 // requested sample rate matches with that of voip input stream (if opened already)
1469 int value = 0;
1470 uint32_t mode = 0, voipOutCount = 1, voipSampleRate = 1;
1471 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1472 String8("audio_mode"));
1473 AudioParameter result = AudioParameter(valueStr);
1474 if (result.getInt(String8("audio_mode"), value) == NO_ERROR) {
1475 mode = value;
1476 }
1477
1478 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1479 String8("voip_out_stream_count"));
1480 result = AudioParameter(valueStr);
1481 if (result.getInt(String8("voip_out_stream_count"), value) == NO_ERROR) {
1482 voipOutCount = value;
1483 }
1484
1485 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1486 String8("voip_sample_rate"));
1487 result = AudioParameter(valueStr);
1488 if (result.getInt(String8("voip_sample_rate"), value) == NO_ERROR) {
1489 voipSampleRate = value;
1490 }
1491
1492 if ((mode == AUDIO_MODE_IN_COMMUNICATION) && (voipOutCount == 0) &&
1493 ((voipSampleRate == 0) || (voipSampleRate == samplingRate))) {
1494 if (audio_is_linear_pcm(format)) {
1495 char propValue[PROPERTY_VALUE_MAX] = {0};
1496 property_get("use.voice.path.for.pcm.voip", propValue, "0");
1497 bool voipPcmSysPropEnabled = !strncmp("true", propValue, sizeof("true"));
1498 if (voipPcmSysPropEnabled && (format == AUDIO_FORMAT_PCM_16_BIT)) {
Karthik Reddy Katta3fb9be72015-11-25 11:18:27 +05301499 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX |
1500 AUDIO_OUTPUT_FLAG_DIRECT);
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301501 ALOGD("Set VoIP and Direct output flags for PCM format");
1502 }
1503 }
1504 }
1505 }
1506
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301507#ifdef VOICE_CONCURRENCY
1508 char propValue[PROPERTY_VALUE_MAX];
1509 bool prop_play_enabled=false, prop_voip_enabled = false;
1510
1511 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1512 prop_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001513 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301514
1515 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1516 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1517 }
1518
1519 if (prop_play_enabled && mvoice_call_state) {
1520 //check if voice call is active / running in background
1521 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1522 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1523 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1524 {
1525 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1526 if(prop_voip_enabled) {
1527 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1528 flags );
1529 return 0;
1530 }
1531 }
1532 else {
1533 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
1534 ALOGD("voice_conc:IN call mode adding ULL flags .. flags: %x ", flags );
1535 flags = AUDIO_OUTPUT_FLAG_FAST;
1536 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
1537 if (AUDIO_STREAM_MUSIC == stream) {
1538 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1539 ALOGD("voice_conc:IN call mode adding deep-buffer flags %x ", flags );
1540 }
1541 else {
1542 flags = AUDIO_OUTPUT_FLAG_FAST;
1543 ALOGD("voice_conc:IN call mode adding fast flags %x ", flags );
1544 }
1545 }
1546 }
1547 }
1548 } else if (prop_voip_enabled && mvoice_call_state) {
1549 //check if voice call is active / running in background
1550 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1551 //return only ULL ouput
1552 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1553 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1554 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1555 {
1556 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1557 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1558 flags );
1559 return 0;
1560 }
1561 }
1562 }
1563#endif
1564#ifdef RECORD_PLAY_CONCURRENCY
1565 char recConcPropValue[PROPERTY_VALUE_MAX];
1566 bool prop_rec_play_enabled = false;
1567
1568 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
1569 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
1570 }
1571 if ((prop_rec_play_enabled) &&
1572 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
1573 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
1574 if (AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1575 // allow VoIP using voice path
1576 // Do nothing
1577 } else if((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1578 ALOGD("voice_conc:MODE_IN_COMM is setforcing deep buffer output for non ULL... flags: %x", flags);
1579 // use deep buffer path for all non ULL outputs
1580 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1581 }
1582 } else if ((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1583 ALOGD("voice_conc:Record mode is on forcing deep buffer output for non ULL... flags: %x ", flags);
1584 // use deep buffer path for all non ULL outputs
1585 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1586 }
1587 }
1588 if (prop_rec_play_enabled &&
1589 (stream == AUDIO_STREAM_ENFORCED_AUDIBLE)) {
1590 ALOGD("Record conc is on forcing ULL output for ENFORCED_AUDIBLE");
1591 flags = AUDIO_OUTPUT_FLAG_FAST;
1592 }
1593#endif
1594
Sharad Sangle4509cef2015-08-19 20:47:12 +05301595#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Sharad Sangle36781612015-05-28 16:15:16 +05301596 /*
1597 * WFD audio routes back to target speaker when starting a ringtone playback.
1598 * This is because primary output is reused for ringtone, so output device is
1599 * updated based on SONIFICATION strategy for both ringtone and music playback.
1600 * The same issue is not seen on remoted_submix HAL based WFD audio because
1601 * primary output is not reused and a new output is created for ringtone playback.
1602 * Issue is fixed by updating output flag to AUDIO_OUTPUT_FLAG_FAST when there is
1603 * a non-music stream playback on WFD, so primary output is not reused for ringtone.
1604 */
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001605 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
1606 if ((availableOutputDeviceTypes & AUDIO_DEVICE_OUT_PROXY)
1607 && (stream != AUDIO_STREAM_MUSIC)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301608 ALOGD("WFD audio: use OUTPUT_FLAG_FAST for non music stream. flags:%x", flags );
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001609 //For voip paths
1610 if(flags & AUDIO_OUTPUT_FLAG_DIRECT)
1611 flags = AUDIO_OUTPUT_FLAG_DIRECT;
1612 else //route every thing else to ULL path
1613 flags = AUDIO_OUTPUT_FLAG_FAST;
1614 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301615#endif
1616
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001617 // open a direct output if required by specified parameters
vivek mehta0ea887a2015-08-26 14:01:20 -07001618 // force direct flag if offload flag is set: offloading implies a direct output stream
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001619 // and all common behaviors are driven by checking only the direct flag
1620 // this should normally be set appropriately in the policy configuration file
1621 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1622 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1623 }
1624 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1625 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1626 }
vivek mehta0ea887a2015-08-26 14:01:20 -07001627
vivek mehta4b0d8192015-10-16 00:25:59 -07001628 bool forced_deep = false;
Sharad Sangle36781612015-05-28 16:15:16 +05301629 // only allow deep buffering for music stream type
1630 if (stream != AUDIO_STREAM_MUSIC) {
1631 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Sharad Sangle497aef82015-08-03 17:55:48 +05301632 } else if (/* stream == AUDIO_STREAM_MUSIC && */
1633 flags == AUDIO_OUTPUT_FLAG_NONE &&
1634 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
vivek mehtac984b992015-11-25 13:28:55 -08001635 forced_deep = true;
Sharad Sangle36781612015-05-28 16:15:16 +05301636 }
Sharad Sangle497aef82015-08-03 17:55:48 +05301637
Sharad Sangle36781612015-05-28 16:15:16 +05301638 if (stream == AUDIO_STREAM_TTS) {
1639 flags = AUDIO_OUTPUT_FLAG_TTS;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001640 }
1641
Zhou Song2b3d1552016-03-21 17:21:04 +08001642 // check if direct output for track offload already exits
1643 bool is_track_offload_active = false;
1644 for (size_t i = 0; i < mOutputs.size(); i++) {
1645 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1646 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) {
1647 is_track_offload_active = true;
1648 ALOGD("Track offload already active");
1649 break;
1650 }
1651 }
1652
vivek mehta4b0d8192015-10-16 00:25:59 -07001653 // Do offload magic here
vivek mehtac984b992015-11-25 13:28:55 -08001654 if ((flags == AUDIO_OUTPUT_FLAG_NONE) &&
1655 (stream == AUDIO_STREAM_MUSIC) &&
Zhou Song2b3d1552016-03-21 17:21:04 +08001656 (offloadInfo != NULL) && !is_track_offload_active &&
vivek mehta4b0d8192015-10-16 00:25:59 -07001657 ((offloadInfo->usage == AUDIO_USAGE_MEDIA) || (offloadInfo->usage == AUDIO_USAGE_GAME))) {
vivek mehtac984b992015-11-25 13:28:55 -08001658 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DIRECT_PCM);
vivek mehta4b0d8192015-10-16 00:25:59 -07001659 ALOGD("AudioCustomHAL --> Force Direct Flag .. flag (0x%x)", flags);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301660 }
1661
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001662 sp<IOProfile> profile;
1663
1664 // skip direct output selection if the request can obviously be attached to a mixed output
1665 // and not explicitly requested
vivek mehtac984b992015-11-25 13:28:55 -08001666 if (((flags & (AUDIO_OUTPUT_FLAG_DIRECT|AUDIO_OUTPUT_FLAG_DIRECT_PCM)) == 0) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301667 audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX &&
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001668 audio_channel_count_from_out_mask(channelMask) <= 2) {
1669 goto non_direct_output;
1670 }
1671
1672 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1673 // creating an offloaded track and tearing it down immediately after start when audioflinger
1674 // detects there is an active non offloadable effect.
1675 // FIXME: We should check the audio session here but we do not have it in this context.
1676 // This may prevent offloading in rare situations where effects are left active by apps
1677 // in the background.
Weiyin Jiang6bba3202016-03-03 14:49:36 +08001678 //
1679 // Supplementary annotation:
1680 // For sake of track offload introduced, we need a rollback for both compress offload
1681 // and track offload use cases.
1682 if ((flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD|AUDIO_OUTPUT_FLAG_DIRECT_PCM)) &&
1683 (mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
1684 ALOGD("non offloadable effect is enabled, try with non direct output");
1685 goto non_direct_output;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001686 }
1687
Weiyin Jiang6bba3202016-03-03 14:49:36 +08001688 profile = getProfileForDirectOutput(device,
1689 samplingRate,
1690 format,
1691 channelMask,
1692 (audio_output_flags_t)flags);
1693
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001694 if (profile != 0) {
vivek mehtac984b992015-11-25 13:28:55 -08001695
1696 if (!(flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301697 (profile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT_PCM)) {
vivek mehtac984b992015-11-25 13:28:55 -08001698 ALOGI("got Direct_PCM without requesting ... reject ");
1699 profile = NULL;
1700 goto non_direct_output;
1701 }
1702
Sharad Sangle36781612015-05-28 16:15:16 +05301703 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001704
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001705 // if multiple concurrent offload decode is supported
1706 // do no check for reuse and also don't close previous output if its offload
1707 // previous output will be closed during track destruction
1708 if (!(property_get_bool("audio.offload.multiple.enabled", false) &&
1709 ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0))) {
1710 for (size_t i = 0; i < mOutputs.size(); i++) {
1711 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1712 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1713 outputDesc = desc;
1714 // reuse direct output if currently open and configured with same parameters
1715 if ((samplingRate == outputDesc->mSamplingRate) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301716 audio_formats_match(format, outputDesc->mFormat) &&
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001717 (channelMask == outputDesc->mChannelMask)) {
1718 outputDesc->mDirectOpenCount++;
1719 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1720 return mOutputs.keyAt(i);
1721 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001722 }
1723 }
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001724 // close direct output if currently open and configured with different parameters
1725 if (outputDesc != NULL) {
1726 closeOutput(outputDesc->mIoHandle);
1727 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001728 }
Sharad Sangle36781612015-05-28 16:15:16 +05301729
1730 // if the selected profile is offloaded and no offload info was specified,
1731 // create a default one
1732 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
Ashish Jain40cab3a2016-03-23 11:14:14 +05301733 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
Sharad Sangle36781612015-05-28 16:15:16 +05301734 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1735 defaultOffloadInfo.sample_rate = samplingRate;
1736 defaultOffloadInfo.channel_mask = channelMask;
1737 defaultOffloadInfo.format = format;
1738 defaultOffloadInfo.stream_type = stream;
1739 defaultOffloadInfo.bit_rate = 0;
1740 defaultOffloadInfo.duration_us = -1;
1741 defaultOffloadInfo.has_video = true; // conservative
1742 defaultOffloadInfo.is_streaming = true; // likely
1743 offloadInfo = &defaultOffloadInfo;
1744 }
1745
1746 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001747 outputDesc->mDevice = device;
1748 outputDesc->mLatency = 0;
Sharad Sangle36781612015-05-28 16:15:16 +05301749 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001750 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1751 config.sample_rate = samplingRate;
1752 config.channel_mask = channelMask;
1753 config.format = format;
1754 if (offloadInfo != NULL) {
1755 config.offload_info = *offloadInfo;
1756 }
Sharad Sangle36781612015-05-28 16:15:16 +05301757 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001758 &output,
1759 &config,
1760 &outputDesc->mDevice,
1761 String8(""),
1762 &outputDesc->mLatency,
1763 outputDesc->mFlags);
1764
1765 // only accept an output with the requested parameters
1766 if (status != NO_ERROR ||
1767 (samplingRate != 0 && samplingRate != config.sample_rate) ||
Ashish Jain40cab3a2016-03-23 11:14:14 +05301768 (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) ||
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001769 (channelMask != 0 && channelMask != config.channel_mask)) {
1770 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1771 "format %d %d, channelMask %04x %04x", output, samplingRate,
1772 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1773 outputDesc->mChannelMask);
1774 if (output != AUDIO_IO_HANDLE_NONE) {
1775 mpClientInterface->closeOutput(output);
1776 }
Sharad Sangle36781612015-05-28 16:15:16 +05301777 // fall back to mixer output if possible when the direct output could not be open
Ashish Jain40cab3a2016-03-23 11:14:14 +05301778 if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) {
Sharad Sangle36781612015-05-28 16:15:16 +05301779 goto non_direct_output;
1780 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001781 return AUDIO_IO_HANDLE_NONE;
1782 }
1783 outputDesc->mSamplingRate = config.sample_rate;
1784 outputDesc->mChannelMask = config.channel_mask;
1785 outputDesc->mFormat = config.format;
1786 outputDesc->mRefCount[stream] = 0;
1787 outputDesc->mStopTime[stream] = 0;
1788 outputDesc->mDirectOpenCount = 1;
1789
1790 audio_io_handle_t srcOutput = getOutputForEffect();
1791 addOutput(output, outputDesc);
1792 audio_io_handle_t dstOutput = getOutputForEffect();
1793 if (dstOutput == output) {
Gao Jiedb057832015-11-12 08:51:22 +08001794#ifdef DOLBY_ENABLE
1795 status_t status = mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1796 if (status == NO_ERROR) {
1797 for (size_t i = 0; i < mEffects.size(); i++) {
1798 sp<EffectDescriptor> desc = mEffects.valueAt(i);
1799 if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX) {
1800 // update the mIo member of EffectDescriptor for the global effect
1801 ALOGV("%s updating mIo", __FUNCTION__);
1802 desc->mIo = dstOutput;
1803 }
1804 }
1805 } else {
1806 ALOGW("%s moveEffects from %d to %d failed", __FUNCTION__, srcOutput, dstOutput);
1807 }
1808#else // DOLBY_END
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001809 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
Gao Jiedb057832015-11-12 08:51:22 +08001810#endif // LINE_ADDED_BY_DOLBY
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001811 }
1812 mPreviousOutputs = mOutputs;
1813 ALOGV("getOutput() returns new direct output %d", output);
1814 mpClientInterface->onAudioPortListUpdate();
1815 return output;
1816 }
1817
1818non_direct_output:
Ashish Jain5fe12412016-04-18 18:44:17 +05301819
1820 // A request for HW A/V sync cannot fallback to a mixed output because time
1821 // stamps are embedded in audio data
1822 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1823 return AUDIO_IO_HANDLE_NONE;
1824 }
1825
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001826 // ignoring channel mask due to downmix capability in mixer
1827
1828 // open a non direct output
1829
1830 // for non direct outputs, only PCM is supported
1831 if (audio_is_linear_pcm(format)) {
1832 // get which output is suitable for the specified stream. The actual
1833 // routing change will happen when startOutput() will be called
1834 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1835
1836 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1837 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
Ashish Jain660d3112016-06-20 10:16:51 +05301838
1839 if (forced_deep) {
1840 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
1841 ALOGI("setting force DEEP buffer now ");
1842 } else if(flags == AUDIO_OUTPUT_FLAG_NONE) {
1843 // no deep buffer playback is requested hence fallback to primary
1844 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_PRIMARY);
1845 ALOGI("FLAG None hence request for a primary output");
1846 }
1847
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001848 output = selectOutput(outputs, flags, format);
1849 }
1850 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1851 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1852
vivek mehta0ea887a2015-08-26 14:01:20 -07001853 ALOGV("getOutputForDevice() returns output %d", output);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001854
1855 return output;
1856}
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301857
1858status_t AudioPolicyManagerCustom::getInputForAttr(const audio_attributes_t *attr,
1859 audio_io_handle_t *input,
1860 audio_session_t session,
1861 uid_t uid,
1862 uint32_t samplingRate,
1863 audio_format_t format,
1864 audio_channel_mask_t channelMask,
1865 audio_input_flags_t flags,
1866 audio_port_handle_t selectedDeviceId,
1867 input_type_t *inputType)
1868{
Ashish Jain40cab3a2016-03-23 11:14:14 +05301869 audio_source_t inputSource;
1870 inputSource = attr->source;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301871#ifdef VOICE_CONCURRENCY
1872
1873 char propValue[PROPERTY_VALUE_MAX];
1874 bool prop_rec_enabled=false, prop_voip_enabled = false;
1875
1876 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
1877 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1878 }
1879
1880 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1881 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1882 }
1883
1884 if (prop_rec_enabled && mvoice_call_state) {
1885 //check if voice call is active / running in background
1886 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1887 //Need to block input request
1888 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1889 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1890 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1891 {
1892 switch(inputSource) {
1893 case AUDIO_SOURCE_VOICE_UPLINK:
1894 case AUDIO_SOURCE_VOICE_DOWNLINK:
1895 case AUDIO_SOURCE_VOICE_CALL:
1896 ALOGD("voice_conc:Creating input during incall mode for inputSource: %d",
1897 inputSource);
1898 break;
1899
1900 case AUDIO_SOURCE_VOICE_COMMUNICATION:
1901 if(prop_voip_enabled) {
1902 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1903 inputSource);
1904 return NO_INIT;
1905 }
1906 break;
1907 default:
1908 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1909 inputSource);
1910 return NO_INIT;
1911 }
1912 }
1913 }//check for VoIP flag
1914 else if(prop_voip_enabled && mvoice_call_state) {
1915 //check if voice call is active / running in background
1916 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1917 //Need to block input request
1918 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1919 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1920 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1921 {
1922 if(inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1923 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
1924 return NO_INIT;
1925 }
1926 }
1927 }
1928
1929#endif
1930
1931 return AudioPolicyManager::getInputForAttr(attr,
1932 input,
1933 session,
1934 uid,
1935 samplingRate,
1936 format,
1937 channelMask,
1938 flags,
1939 selectedDeviceId,
1940 inputType);
1941}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001942
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301943status_t AudioPolicyManagerCustom::startInput(audio_io_handle_t input,
1944 audio_session_t session)
1945{
1946 ALOGV("startInput() input %d", input);
1947 ssize_t index = mInputs.indexOfKey(input);
1948 if (index < 0) {
1949 ALOGW("startInput() unknown input %d", input);
1950 return BAD_VALUE;
1951 }
1952 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1953
Ashish Jain40cab3a2016-03-23 11:14:14 +05301954 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1955 if (audioSession == 0) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301956 ALOGW("startInput() unknown session %d on input %d", session, input);
1957 return BAD_VALUE;
1958 }
1959
1960 // virtual input devices are compatible with other input devices
1961 if (!is_virtual_input_device(inputDesc->mDevice)) {
1962
1963 // for a non-virtual input device, check if there is another (non-virtual) active input
1964 audio_io_handle_t activeInput = mInputs.getActiveInput();
1965 if (activeInput != 0 && activeInput != input) {
1966
1967 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1968 // otherwise the active input continues and the new input cannot be started.
1969 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Ashish Jain40cab3a2016-03-23 11:14:14 +05301970 if ((activeDesc->inputSource() == AUDIO_SOURCE_HOTWORD) &&
1971 !activeDesc->hasPreemptedSession(session)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301972 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
Ashish Jain40cab3a2016-03-23 11:14:14 +05301973 //FIXME: consider all active sessions
1974 AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions();
1975 audio_session_t activeSession = activeSessions.keyAt(0);
1976 SortedVector<audio_session_t> sessions =
1977 activeDesc->getPreemptedSessions();
1978 sessions.add(activeSession);
1979 inputDesc->setPreemptedSessions(sessions);
1980 stopInput(activeInput, activeSession);
1981 releaseInput(activeInput, activeSession);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301982 } else {
1983 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
1984 return INVALID_OPERATION;
1985 }
1986 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05301987 // Do not allow capture if an active voice call is using a software patch and
1988 // the call TX source device is on the same HW module.
1989 // FIXME: would be better to refine to only inputs whose profile connects to the
1990 // call TX device but this information is not in the audio patch
1991 if (mCallTxPatch != 0 &&
1992 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1993 return INVALID_OPERATION;
1994 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301995 }
1996
1997 // Routing?
1998 mInputRoutes.incRouteActivity(session);
1999#ifdef RECORD_PLAY_CONCURRENCY
2000 mIsInputRequestOnProgress = true;
2001
2002 char getPropValue[PROPERTY_VALUE_MAX];
2003 bool prop_rec_play_enabled = false;
2004
2005 if (property_get("rec.playback.conc.disabled", getPropValue, NULL)) {
2006 prop_rec_play_enabled = atoi(getPropValue) || !strncmp("true", getPropValue, 4);
2007 }
2008
2009 if ((prop_rec_play_enabled) &&(mInputs.activeInputsCount() == 0)){
2010 // send update to HAL on record playback concurrency
2011 AudioParameter param = AudioParameter();
2012 param.add(String8("rec_play_conc_on"), String8("true"));
2013 ALOGD("startInput() setParameters rec_play_conc is setting to ON ");
2014 mpClientInterface->setParameters(0, param.toString());
2015
2016 // Call invalidate to reset all opened non ULL audio tracks
2017 // Move tracks associated to this strategy from previous output to new output
2018 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
2019 // Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder)
Sharad Sangle4509cef2015-08-19 20:47:12 +05302020 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE && (i != AUDIO_STREAM_PATCH))) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302021 ALOGD("Invalidate on releaseInput for stream :: %d ", i);
2022 //FIXME see fixme on name change
2023 mpClientInterface->invalidateStream((audio_stream_type_t)i);
2024 }
2025 }
2026 // close compress tracks
2027 for (size_t i = 0; i < mOutputs.size(); i++) {
2028 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
2029 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
2030 ALOGD("ouput desc / profile is NULL");
2031 continue;
2032 }
2033 if (outputDesc->mProfile->mFlags
2034 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2035 // close compress sessions
2036 ALOGD("calling closeOutput on record conc for COMPRESS output");
2037 closeOutput(mOutputs.keyAt(i));
2038 }
2039 }
Ashish Jaine4a22d52016-06-22 11:55:08 +05302040 // If effects where present on any of the above closed outputs,
2041 // audioflinger moved them to the primary output by default
2042 // move them back to the appropriate output.
2043 moveGlobalEffect();
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302044 }
2045#endif
2046
Ashish Jain40cab3a2016-03-23 11:14:14 +05302047 if (!inputDesc->isActive() || mInputRoutes.hasRouteChanged(session)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302048 // if input maps to a dynamic policy with an activity listener, notify of state change
Ashish Jain630c5e12016-04-18 11:50:21 +05302049 if ((inputDesc->mPolicyMix != NULL)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302050 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Ashish Jain5fe12412016-04-18 18:44:17 +05302051 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302052 MIX_STATE_MIXING);
Ashish Jain630c5e12016-04-18 11:50:21 +05302053 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302054
2055 if (mInputs.activeInputsCount() == 0) {
2056 SoundTrigger::setCaptureState(true);
2057 }
2058 setInputDevice(input, getNewInputDevice(input), true /* force */);
2059
2060 // automatically enable the remote submix output when input is started if not
2061 // used by a policy mix of type MIX_TYPE_RECORDERS
2062 // For remote submix (a virtual device), we open only one input per capture request.
2063 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
2064 String8 address = String8("");
2065 if (inputDesc->mPolicyMix == NULL) {
2066 address = String8("0");
Ashish Jain630c5e12016-04-18 11:50:21 +05302067 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
Ashish Jain5fe12412016-04-18 18:44:17 +05302068 address = inputDesc->mPolicyMix->mDeviceAddress;
Ashish Jain630c5e12016-04-18 11:50:21 +05302069 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302070 if (address != "") {
2071 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2072 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2073 address, "remote-submix");
2074 }
2075 }
2076 }
2077
Ashish Jain40cab3a2016-03-23 11:14:14 +05302078 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302079
Ashish Jain40cab3a2016-03-23 11:14:14 +05302080 audioSession->changeActiveCount(1);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302081#ifdef RECORD_PLAY_CONCURRENCY
2082 mIsInputRequestOnProgress = false;
2083#endif
2084 return NO_ERROR;
2085}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08002086
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302087status_t AudioPolicyManagerCustom::stopInput(audio_io_handle_t input,
2088 audio_session_t session)
2089{
2090 status_t status;
2091 status = AudioPolicyManager::stopInput(input, session);
2092#ifdef RECORD_PLAY_CONCURRENCY
2093 char propValue[PROPERTY_VALUE_MAX];
2094 bool prop_rec_play_enabled = false;
2095
2096 if (property_get("rec.playback.conc.disabled", propValue, NULL)) {
2097 prop_rec_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
2098 }
2099
2100 if ((prop_rec_play_enabled) && (mInputs.activeInputsCount() == 0)) {
2101
2102 //send update to HAL on record playback concurrency
2103 AudioParameter param = AudioParameter();
2104 param.add(String8("rec_play_conc_on"), String8("false"));
2105 ALOGD("stopInput() setParameters rec_play_conc is setting to OFF ");
2106 mpClientInterface->setParameters(0, param.toString());
2107
2108 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
2109 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
2110 //Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder stop tone)
2111 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE) && (i != AUDIO_STREAM_PATCH)) {
2112 ALOGD(" Invalidate on stopInput for stream :: %d ", i);
2113 //FIXME see fixme on name change
2114 mpClientInterface->invalidateStream((audio_stream_type_t)i);
2115 }
2116 }
2117 }
2118#endif
2119 return status;
2120}
2121
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302122void AudioPolicyManagerCustom::closeAllInputs() {
2123 bool patchRemoved = false;
2124
Dhanalakshmi Siddanibfeca7f2015-10-08 15:17:11 +05302125 for(size_t input_index = mInputs.size(); input_index > 0; input_index--) {
2126 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index-1);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302127 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302128 if (patch_index >= 0) {
2129 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302130 status_t status;
2131 status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302132 mAudioPatches.removeItemsAt(patch_index);
2133 patchRemoved = true;
2134 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05302135 if ((inputDesc->getOpenRefCount() > 0) && inputDesc->isSoundTrigger()
2136 && (mInputs.size() == 1)) {
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302137 ALOGD("Do not close sound trigger input handle");
2138 } else {
Dhanalakshmi Siddanibfeca7f2015-10-08 15:17:11 +05302139 mpClientInterface->closeInput(mInputs.keyAt(input_index-1));
2140 mInputs.removeItem(mInputs.keyAt(input_index-1));
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302141 }
2142 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05302143 mInputs.clear();
2144 SoundTrigger::setCaptureState(false);
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302145 nextAudioPortGeneration();
2146
2147 if (patchRemoved) {
2148 mpClientInterface->onAudioPatchListUpdate();
2149 }
2150}
2151
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302152AudioPolicyManagerCustom::AudioPolicyManagerCustom(AudioPolicyClientInterface *clientInterface)
Sharad Sangle4509cef2015-08-19 20:47:12 +05302153 : AudioPolicyManager(clientInterface),
2154 mHdmiAudioDisabled(false),
2155 mHdmiAudioEvent(false),
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08002156 mPrevPhoneState(0),
Haynes Mathew George603ae9b2015-12-21 17:23:59 -08002157 mPrevFMVolumeDb(0.0f),
2158 mFMIsActive(false)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302159{
Ashish Jain40cab3a2016-03-23 11:14:14 +05302160
Satya Krishna Pindiproli8f83f102016-06-21 17:56:14 +05302161#ifdef USE_XML_AUDIO_POLICY_CONF
2162 ALOGD("USE_XML_AUDIO_POLICY_CONF is TRUE");
2163#else
2164 ALOGD("USE_XML_AUDIO_POLICY_CONF is FALSE");
2165#endif
2166
Ashish Jain40cab3a2016-03-23 11:14:14 +05302167 //TODO: Check the new logic to parse policy conf and update the below code
2168 // Need this when SSR encoding is enabled
Mingming Yin38ea08c2015-10-05 15:24:04 -07002169 char ssr_enabled[PROPERTY_VALUE_MAX] = {0};
2170 bool prop_ssr_enabled = false;
2171
2172 if (property_get("ro.qc.sdk.audio.ssr", ssr_enabled, NULL)) {
2173 prop_ssr_enabled = atoi(ssr_enabled) || !strncmp("true", ssr_enabled, 4);
2174 }
2175
2176 for (size_t i = 0; i < mHwModules.size(); i++) {
Ashish Jain40cab3a2016-03-23 11:14:14 +05302177 ALOGV("Hw module %zu", i);
Mingming Yin38ea08c2015-10-05 15:24:04 -07002178 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) {
2179 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Ashish Jain40cab3a2016-03-23 11:14:14 +05302180 AudioProfileVector profiles = inProfile->getAudioProfiles();
2181 for (size_t k = 0; k < profiles.size(); k++){
2182 ChannelsVector channels = profiles[k]->getChannels();
2183 for (size_t x = 0; x < channels.size(); x++) {
2184 audio_channel_mask_t channelMask = channels[x];
2185 ALOGV("Channel Mask %x size %zu", channelMask,
2186 channels.size());
2187 if (AUDIO_CHANNEL_IN_5POINT1 == channelMask) {
2188 if (!prop_ssr_enabled) {
2189 ALOGI("removing AUDIO_CHANNEL_IN_5POINT1 from"
2190 " input profile as SSR(surround sound record)"
2191 " is not supported on this chipset variant");
2192 channels.removeItemsAt(x, 1);
2193 ALOGV("Channel Mask size now %zu",
2194 channels.size());
2195 }
Mingming Yin38ea08c2015-10-05 15:24:04 -07002196 }
2197 }
2198 }
2199 }
2200 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302201#ifdef RECORD_PLAY_CONCURRENCY
2202 mIsInputRequestOnProgress = false;
2203#endif
2204
2205
2206#ifdef VOICE_CONCURRENCY
2207 mFallBackflag = getFallBackPath();
2208#endif
2209}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07002210}