blob: 2299e3ffce1c84214ddd5947003fffe14754ab60 [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
300 updateDevicesAndOutputs();
Gao Jiedb057832015-11-12 08:51:22 +0800301#ifdef DOLBY_ENABLE
302 // Before closing the opened outputs, update endpoint property with device capabilities
303 audio_devices_t audioOutputDevice = getDeviceForStrategy(getStrategy(AUDIO_STREAM_MUSIC), true);
304 mDolbyAudioPolicy.setEndpointSystemProperty(audioOutputDevice, mHwModules);
305#endif // DOLBY_END
Sharad Sangle36781612015-05-28 16:15:16 +0530306 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
307 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700308 updateCallRouting(newDevice);
309 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530310
311#ifdef FM_POWER_OPT
312 // handle FM device connection state to trigger FM AFE loopback
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800313 if (device == AUDIO_DEVICE_OUT_FM && hasPrimaryOutput()) {
Mingming Yin2dd77c22016-01-06 18:02:42 -0800314 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530315 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
316 mPrimaryOutput->changeRefCount(AUDIO_STREAM_MUSIC, 1);
Mingming Yin2dd77c22016-01-06 18:02:42 -0800317 newDevice = (audio_devices_t)(getNewOutputDevice(mPrimaryOutput, false)|AUDIO_DEVICE_OUT_FM);
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800318 mFMIsActive = true;
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530319 } else {
Mingming Yin2dd77c22016-01-06 18:02:42 -0800320 newDevice = (audio_devices_t)(getNewOutputDevice(mPrimaryOutput, false));
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800321 mFMIsActive = false;
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530322 mPrimaryOutput->changeRefCount(AUDIO_STREAM_MUSIC, -1);
323 }
324 AudioParameter param = AudioParameter();
325 param.addInt(String8("handle_fm"), (int)newDevice);
326 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString());
327 }
328#endif /* FM_POWER_OPT end */
329
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700330 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530331 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
332 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
333 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700334 // do not force device change on duplicated output because if device is 0, it will
335 // also force a device 0 for the two outputs it is duplicated to which may override
336 // a valid device selection on those outputs.
Sharad Sangle36781612015-05-28 16:15:16 +0530337 bool force = !desc->isDuplicated()
338 && (!device_distinguishes_on_address(device)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700339 // always force when disconnecting (a non-duplicated device)
340 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Sharad Sangle36781612015-05-28 16:15:16 +0530341 setOutputDevice(desc, newDevice, force, 0);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700342 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700343 }
344
Ashish Jain40cab3a2016-03-23 11:14:14 +0530345 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
346 cleanUpForDevice(devDesc);
347 }
348
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700349 mpClientInterface->onAudioPortListUpdate();
350 return NO_ERROR;
351 } // end if is output device
352
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700353 // handle input devices
354 if (audio_is_input_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700355 SortedVector <audio_io_handle_t> inputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700356
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700357 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700358 switch (state)
359 {
360 // handle input device connection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700361 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
362 if (index >= 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700363 ALOGW("setDeviceConnectionState() device already connected: %d", device);
364 return INVALID_OPERATION;
365 }
Sharad Sangle36781612015-05-28 16:15:16 +0530366 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700367 if (module == NULL) {
368 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
369 device);
370 return INVALID_OPERATION;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700371 }
Sharad Sangle36781612015-05-28 16:15:16 +0530372 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700373 return INVALID_OPERATION;
374 }
375
376 index = mAvailableInputDevices.add(devDesc);
377 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530378 mAvailableInputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700379 } else {
380 return NO_MEMORY;
381 }
Sharad Sangle36781612015-05-28 16:15:16 +0530382
383 // Set connect to HALs
384 AudioParameter param = AudioParameter(devDesc->mAddress);
385 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
386 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
387
388 // Propagate device availability to Engine
389 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700390 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700391
392 // handle input device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700393 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
394 if (index < 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700395 ALOGW("setDeviceConnectionState() device not connected: %d", device);
396 return INVALID_OPERATION;
397 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700398
399 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
400
401 // Set Disconnect to HALs
Sharad Sangle36781612015-05-28 16:15:16 +0530402 AudioParameter param = AudioParameter(devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700403 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
404 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
405
Sharad Sangle36781612015-05-28 16:15:16 +0530406 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700407 mAvailableInputDevices.remove(devDesc);
408
Sharad Sangle36781612015-05-28 16:15:16 +0530409 // Propagate device availability to Engine
410 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700411 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700412
413 default:
414 ALOGE("setDeviceConnectionState() invalid state: %x", state);
415 return BAD_VALUE;
416 }
417
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700418 closeAllInputs();
419
Sharad Sangle36781612015-05-28 16:15:16 +0530420 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700421 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
422 updateCallRouting(newDevice);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700423 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700424
Ashish Jain40cab3a2016-03-23 11:14:14 +0530425 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
426 cleanUpForDevice(devDesc);
427 }
428
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700429 mpClientInterface->onAudioPortListUpdate();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700430 return NO_ERROR;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700431 } // end if is input device
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700432
433 ALOGW("setDeviceConnectionState() invalid device: %x", device);
434 return BAD_VALUE;
435}
Weiyin Jiangd211ef52016-02-16 15:55:11 +0800436
Sharad Sangle36781612015-05-28 16:15:16 +0530437// This function checks for the parameters which can be offloaded.
438// This can be enhanced depending on the capability of the DSP and policy
439// of the system.
440bool AudioPolicyManagerCustom::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700441{
Sharad Sangle36781612015-05-28 16:15:16 +0530442 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
443 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
444 offloadInfo.sample_rate, offloadInfo.channel_mask,
445 offloadInfo.format,
446 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
447 offloadInfo.has_video);
Ashish Jain40cab3a2016-03-23 11:14:14 +0530448
449 if (mMasterMono) {
450 return false; // no offloading if mono is set.
451 }
452
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530453#ifdef VOICE_CONCURRENCY
454 char concpropValue[PROPERTY_VALUE_MAX];
455 if (property_get("voice.playback.conc.disabled", concpropValue, NULL)) {
456 bool propenabled = atoi(concpropValue) || !strncmp("true", concpropValue, 4);
457 if (propenabled) {
458 if (isInCall())
459 {
460 ALOGD("\n copl: blocking compress offload on call mode\n");
461 return false;
462 }
463 }
464 }
465#endif
466#ifdef RECORD_PLAY_CONCURRENCY
467 char recConcPropValue[PROPERTY_VALUE_MAX];
468 bool prop_rec_play_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700469
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530470 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
471 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
472 }
473
474 if ((prop_rec_play_enabled) &&
475 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
476 ALOGD("copl: blocking compress offload for record concurrency");
477 return false;
478 }
479#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530480 // Check if stream type is music, then only allow offload as of now.
481 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
482 {
483 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
484 return false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700485 }
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530486
Alexy Joseph0ac09032015-10-16 15:57:53 -0700487 // Check if offload has been disabled
488 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
489 if (offloadDisabled) {
490 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
491 return false;
492 }
493
Ashish Jainac510a72016-04-28 12:22:58 +0530494 //check if it's multi-channel AAC (includes sub formats) and FLAC format
495 if ((popcount(offloadInfo.channel_mask) > 2) &&
496 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
497 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS))) {
498 ALOGD("offload disabled for multi-channel AAC,FLAC and VORBIS format");
499 return false;
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530500 }
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530501
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530502#ifdef AUDIO_EXTN_FORMATS_ENABLED
Ashish Jainac510a72016-04-28 12:22:58 +0530503 //check if it's multi-channel FLAC/ALAC/WMA format with sample rate > 48k
504 if ((popcount(offloadInfo.channel_mask) > 2) &&
505 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
506 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) && (offloadInfo.sample_rate > 48000)) ||
507 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) && (offloadInfo.sample_rate > 48000)) ||
508 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) && (offloadInfo.sample_rate > 48000)) ||
509 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS))) {
510 ALOGD("offload disabled for multi-channel FLAC/ALAC/WMA/AAC_ADTS clips with sample rate > 48kHz");
511 return false;
512 }
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530513#endif
Ashish Jainac510a72016-04-28 12:22:58 +0530514 //TODO: enable audio offloading with video when ready
515 const bool allowOffloadWithVideo =
516 property_get_bool("audio.offload.video", false /* default_value */);
517 if (offloadInfo.has_video && !allowOffloadWithVideo) {
518 ALOGV("isOffloadSupported: has_video == true, returning false");
519 return false;
520 }
Manish Dewanganf3cd0f82015-10-13 14:04:36 +0530521
Ashish Jainac510a72016-04-28 12:22:58 +0530522 const bool allowOffloadStreamingWithVideo = property_get_bool("av.streaming.offload.enable",
523 false /*default value*/);
524 if (offloadInfo.has_video && offloadInfo.is_streaming && !allowOffloadStreamingWithVideo) {
525 ALOGW("offload disabled by av.streaming.offload.enable %d",allowOffloadStreamingWithVideo);
526 return false;
Sharad Sangle36781612015-05-28 16:15:16 +0530527 }
528
529 //If duration is less than minimum value defined in property, return false
Ashish Jainac510a72016-04-28 12:22:58 +0530530 char propValue[PROPERTY_VALUE_MAX];
Sharad Sangle36781612015-05-28 16:15:16 +0530531 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
532 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
533 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
534 return false;
535 }
536 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
537 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
538 //duration checks only valid for MP3/AAC/ formats,
539 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
540 if ((offloadInfo.format == AUDIO_FORMAT_MP3) ||
541 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
542 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
Ashish Jainac510a72016-04-28 12:22:58 +0530543 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS))
544 return false;
545
546#ifdef AUDIO_EXTN_FORMATS_ENABLED
547 if (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) ||
Sharad Sangle36781612015-05-28 16:15:16 +0530548 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) ||
549 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) ||
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530550 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_APE) ||
Ashish Jainac510a72016-04-28 12:22:58 +0530551 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS))
Sharad Sangle36781612015-05-28 16:15:16 +0530552 return false;
Ashish Jainac510a72016-04-28 12:22:58 +0530553#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530554 }
555
556 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
557 // creating an offloaded track and tearing it down immediately after start when audioflinger
558 // detects there is an active non offloadable effect.
559 // FIXME: We should check the audio session here but we do not have it in this context.
560 // This may prevent offloading in rare situations where effects are left active by apps
561 // in the background.
562 if (mEffects.isNonOffloadableEffectEnabled()) {
563 return false;
564 }
565 // Check for soundcard status
566 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
567 String8("SND_CARD_STATUS"));
568 AudioParameter result = AudioParameter(valueStr);
569 int isonline = 0;
570 if ((result.getInt(String8("SND_CARD_STATUS"), isonline) == NO_ERROR)
571 && !isonline) {
572 ALOGD("copl: soundcard is offline rejecting offload request");
573 return false;
574 }
575 // See if there is a profile to support this.
576 // AUDIO_DEVICE_NONE
577 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
578 offloadInfo.sample_rate,
579 offloadInfo.format,
580 offloadInfo.channel_mask,
581 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
582 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
583 return (profile != 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700584}
Weiyin Jiangd211ef52016-02-16 15:55:11 +0800585
Sharad Sangle36781612015-05-28 16:15:16 +0530586audio_devices_t AudioPolicyManagerCustom::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
587 bool fromCache)
588{
589 audio_devices_t device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700590
Ashish Jain40cab3a2016-03-23 11:14:14 +0530591 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Sharad Sangle36781612015-05-28 16:15:16 +0530592 if (index >= 0) {
593 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
594 if (patchDesc->mUid != mUidCached) {
595 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Ashish Jain40cab3a2016-03-23 11:14:14 +0530596 outputDesc->device(), outputDesc->getPatchHandle());
Sharad Sangle36781612015-05-28 16:15:16 +0530597 return outputDesc->device();
598 }
599 }
600
601 // check the following by order of priority to request a routing change if necessary:
602 // 1: the strategy enforced audible is active and enforced on the output:
603 // use device for strategy enforced audible
604 // 2: we are in call or the strategy phone is active on the output:
605 // use device for strategy phone
606 // 3: the strategy for enforced audible is active but not enforced on the output:
607 // use the device for strategy enforced audible
608 // 4: the strategy sonification is active on the output:
609 // use device for strategy sonification
610 // 5: the strategy "respectful" sonification is active on the output:
611 // use device for strategy "respectful" sonification
612 // 6: the strategy accessibility is active on the output:
613 // use device for strategy accessibility
614 // 7: the strategy media is active on the output:
615 // use device for strategy media
616 // 8: the strategy DTMF is active on the output:
617 // use device for strategy DTMF
618 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
619 // use device for strategy t-t-s
620 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
621 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
622 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
623 } else if (isInCall() ||
624 isStrategyActive(outputDesc, STRATEGY_PHONE)||
625 isStrategyActive(mPrimaryOutput, STRATEGY_PHONE)) {
626 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
627 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
628 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
629 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)||
630 (isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION)
631 && (!isStrategyActive(mPrimaryOutput,STRATEGY_MEDIA)))) {
632 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530633 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL) ||
634 isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION_RESPECTFUL)) {
Sharad Sangle36781612015-05-28 16:15:16 +0530635 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
636 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
637 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
638 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
639 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
640 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
641 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
642 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
643 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
644 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
645 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
646 }
647
648 ALOGV("getNewOutputDevice() selected device %x", device);
649 return device;
650}
Weiyin Jiangd211ef52016-02-16 15:55:11 +0800651
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700652void AudioPolicyManagerCustom::setPhoneState(audio_mode_t state)
653{
Ashish Jain40cab3a2016-03-23 11:14:14 +0530654 ALOGD("setPhoneState() state %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530655 // store previous phone state for management of sonification strategy below
Sharad Sangle4509cef2015-08-19 20:47:12 +0530656 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Sharad Sangle36781612015-05-28 16:15:16 +0530657 int oldState = mEngine->getPhoneState();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700658
Sharad Sangle36781612015-05-28 16:15:16 +0530659 if (mEngine->setPhoneState(state) != NO_ERROR) {
660 ALOGW("setPhoneState() invalid or same state %d", state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700661 return;
662 }
Sharad Sangle36781612015-05-28 16:15:16 +0530663 /// Opens: can these line be executed after the switch of volume curves???
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700664 // if leaving call state, handle special case of active streams
665 // pertaining to sonification strategy see handleIncallSonification()
Zhou Song58081b62016-02-24 13:10:55 +0800666 if (isStateInCall(oldState)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700667 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530668 for (size_t j = 0; j < mOutputs.size(); j++) {
669 audio_io_handle_t curOutput = mOutputs.keyAt(j);
670 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
671 if (stream == AUDIO_STREAM_PATCH) {
672 continue;
673 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530674 handleIncallSonification((audio_stream_type_t)stream, false, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +0530675 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700676 }
Sharad Sangle36781612015-05-28 16:15:16 +0530677
Zhou Song58081b62016-02-24 13:10:55 +0800678 // force reevaluating accessibility routing when call stops
Sharad Sangle36781612015-05-28 16:15:16 +0530679 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700680 }
681
Sharad Sangle36781612015-05-28 16:15:16 +0530682 /**
683 * Switching to or from incall state or switching between telephony and VoIP lead to force
684 * routing command.
685 */
686 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
687 || (is_state_in_call(state) && (state != oldState)));
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700688
689 // check for device and output changes triggered by new phone state
690 checkA2dpSuspend();
691 checkOutputForAllStrategies();
692 updateDevicesAndOutputs();
693
Sharad Sangle36781612015-05-28 16:15:16 +0530694 sp<SwAudioOutputDescriptor> hwOutputDesc = mPrimaryOutput;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530695#ifdef VOICE_CONCURRENCY
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530696 char propValue[PROPERTY_VALUE_MAX];
697 bool prop_playback_enabled = false, prop_rec_enabled=false, prop_voip_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700698
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530699 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
700 prop_playback_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
701 }
702
703 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
704 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
705 }
706
707 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
708 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
709 }
710
Dhananjay Kumar9125b2a2015-12-01 16:24:09 +0530711 if ((AUDIO_MODE_IN_CALL != oldState) && (AUDIO_MODE_IN_CALL == state)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530712 ALOGD("voice_conc:Entering to call mode oldState :: %d state::%d ",
713 oldState, state);
Dhananjay Kumar9125b2a2015-12-01 16:24:09 +0530714 mvoice_call_state = state;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530715 if (prop_rec_enabled) {
716 //Close all active inputs
717 audio_io_handle_t activeInput = mInputs.getActiveInput();
718 if (activeInput != 0) {
719 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
720 switch(activeDesc->mInputSource) {
721 case AUDIO_SOURCE_VOICE_UPLINK:
722 case AUDIO_SOURCE_VOICE_DOWNLINK:
723 case AUDIO_SOURCE_VOICE_CALL:
724 ALOGD("voice_conc:FOUND active input during call active: %d",activeDesc->mInputSource);
725 break;
726
727 case AUDIO_SOURCE_VOICE_COMMUNICATION:
728 if(prop_voip_enabled) {
729 ALOGD("voice_conc:CLOSING VoIP input source on call setup :%d ",activeDesc->mInputSource);
730 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
731 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
732 }
733 break;
734
735 default:
736 ALOGD("voice_conc:CLOSING input on call setup for inputSource: %d",activeDesc->mInputSource);
737 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
738 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
739 break;
740 }
741 }
742 } else if (prop_voip_enabled) {
743 audio_io_handle_t activeInput = mInputs.getActiveInput();
744 if (activeInput != 0) {
745 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
746 if (AUDIO_SOURCE_VOICE_COMMUNICATION == activeDesc->mInputSource) {
747 ALOGD("voice_conc:CLOSING VoIP on call setup : %d",activeDesc->mInputSource);
748 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
749 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
750 }
751 }
752 }
753 if (prop_playback_enabled) {
754 // Move tracks associated to this strategy from previous output to new output
755 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
756 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
757 if (i == AUDIO_STREAM_PATCH) {
758 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
759 continue;
760 }
761 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
762 if ((AUDIO_STREAM_MUSIC == i) ||
763 (AUDIO_STREAM_VOICE_CALL == i) ) {
764 ALOGD("voice_conc:Invalidate stream type %d", i);
765 mpClientInterface->invalidateStream((audio_stream_type_t)i);
766 }
767 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
768 ALOGD("voice_conc:Invalidate stream type %d", i);
769 mpClientInterface->invalidateStream((audio_stream_type_t)i);
770 }
771 }
772 }
773
774 for (size_t i = 0; i < mOutputs.size(); i++) {
775 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
776 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
777 ALOGD("voice_conc:ouput desc / profile is NULL");
778 continue;
779 }
780
781 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
782 if (((!outputDesc->isDuplicated() &&outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY))
783 && prop_playback_enabled) {
784 ALOGD("voice_conc:calling suspendOutput on call mode for primary output");
785 mpClientInterface->suspendOutput(mOutputs.keyAt(i));
786 } //Close compress all sessions
787 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
788 && prop_playback_enabled) {
789 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
790 closeOutput(mOutputs.keyAt(i));
791 }
792 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_VOIP_RX)
793 && prop_voip_enabled) {
794 ALOGD("voice_conc:calling closeOutput on call mode for DIRECT output");
795 closeOutput(mOutputs.keyAt(i));
796 }
797 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
798 if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)
799 && prop_playback_enabled) {
800 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
801 closeOutput(mOutputs.keyAt(i));
802 }
803 }
804 }
Ashish Jaine4a22d52016-06-22 11:55:08 +0530805 // If effects where present on any of the above closed outputs,
806 // audioflinger moved them to the primary output by default
807 // move them back to the appropriate output.
808 moveGlobalEffect();
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530809 }
810
811 if ((AUDIO_MODE_IN_CALL == oldState || AUDIO_MODE_IN_COMMUNICATION == oldState) &&
812 (AUDIO_MODE_NORMAL == state) && prop_playback_enabled && mvoice_call_state) {
813 ALOGD("voice_conc:EXITING from call mode oldState :: %d state::%d \n",oldState, state);
814 mvoice_call_state = 0;
815 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
816 //restore PCM (deep-buffer) output after call termination
817 for (size_t i = 0; i < mOutputs.size(); i++) {
818 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
819 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
820 ALOGD("voice_conc:ouput desc / profile is NULL");
821 continue;
822 }
823 if (!outputDesc->isDuplicated() && outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
824 ALOGD("voice_conc:calling restoreOutput after call mode for primary output");
825 mpClientInterface->restoreOutput(mOutputs.keyAt(i));
826 }
827 }
828 }
829 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
830 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
831 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
832 if (i == AUDIO_STREAM_PATCH) {
833 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
834 continue;
835 }
836 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
837 if ((AUDIO_STREAM_MUSIC == i) ||
838 (AUDIO_STREAM_VOICE_CALL == i) ) {
839 mpClientInterface->invalidateStream((audio_stream_type_t)i);
840 }
841 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
842 mpClientInterface->invalidateStream((audio_stream_type_t)i);
843 }
844 }
845 }
846
847#endif
848#ifdef RECORD_PLAY_CONCURRENCY
849 char recConcPropValue[PROPERTY_VALUE_MAX];
850 bool prop_rec_play_enabled = false;
851
852 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
853 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
854 }
855 if (prop_rec_play_enabled) {
856 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
857 ALOGD("phone state changed to MODE_IN_COMM invlaidating music and voice streams");
858 // call invalidate for voice streams, so that it can use deepbuffer with VoIP out device from HAL
859 mpClientInterface->invalidateStream(AUDIO_STREAM_VOICE_CALL);
860 // call invalidate for music, so that compress will fallback to deep-buffer with VoIP out device
861 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
862
863 // close compress output to make sure session will be closed before timeout(60sec)
864 for (size_t i = 0; i < mOutputs.size(); i++) {
865
866 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
867 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
868 ALOGD("ouput desc / profile is NULL");
869 continue;
870 }
871
872 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
873 ALOGD("calling closeOutput on call mode for COMPRESS output");
874 closeOutput(mOutputs.keyAt(i));
875 }
876 }
Ashish Jaine4a22d52016-06-22 11:55:08 +0530877 // If effects where present on any of the above closed outputs,
878 // audioflinger moved them to the primary output by default
879 // move them back to the appropriate output.
880 moveGlobalEffect();
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530881 } else if ((oldState == AUDIO_MODE_IN_COMMUNICATION) &&
882 (mEngine->getPhoneState() == AUDIO_MODE_NORMAL)) {
883 // call invalidate for music so that music can fallback to compress
884 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
885 }
886 }
887#endif
888 mPrevPhoneState = oldState;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700889 int delayMs = 0;
890 if (isStateInCall(state)) {
891 nsecs_t sysTime = systemTime();
892 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530893 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700894 // mute media and sonification strategies and delay device switch by the largest
895 // latency of any output where either strategy is active.
896 // This avoid sending the ring tone or music tail into the earpiece or headset.
Sharad Sangle36781612015-05-28 16:15:16 +0530897 if ((isStrategyActive(desc, STRATEGY_MEDIA,
898 SONIFICATION_HEADSET_MUSIC_DELAY,
899 sysTime) ||
900 isStrategyActive(desc, STRATEGY_SONIFICATION,
901 SONIFICATION_HEADSET_MUSIC_DELAY,
902 sysTime)) &&
903 (delayMs < (int)desc->latency()*2)) {
904 delayMs = desc->latency()*2;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700905 }
Sharad Sangle36781612015-05-28 16:15:16 +0530906 setStrategyMute(STRATEGY_MEDIA, true, desc);
907 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700908 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Sharad Sangle36781612015-05-28 16:15:16 +0530909 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
910 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700911 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
912 }
913 }
914
Sharad Sangle36781612015-05-28 16:15:16 +0530915 if (hasPrimaryOutput()) {
916 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
917 // the device returned is not necessarily reachable via this output
918 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
919 // force routing command to audio hardware when ending call
920 // even if no device change is needed
921 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
922 rxDevice = mPrimaryOutput->device();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700923 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700924
Sharad Sangle36781612015-05-28 16:15:16 +0530925 if (state == AUDIO_MODE_IN_CALL) {
926 updateCallRouting(rxDevice, delayMs);
927 } else if (oldState == AUDIO_MODE_IN_CALL) {
928 if (mCallRxPatch != 0) {
929 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
930 mCallRxPatch.clear();
931 }
932 if (mCallTxPatch != 0) {
933 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
934 mCallTxPatch.clear();
935 }
936 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
937 } else {
938 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700939 }
940 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530941 //update device for all non-primary outputs
942 for (size_t i = 0; i < mOutputs.size(); i++) {
943 audio_io_handle_t output = mOutputs.keyAt(i);
944 if (output != mPrimaryOutput->mIoHandle) {
945 newDevice = getNewOutputDevice(mOutputs.valueFor(output), false /*fromCache*/);
946 setOutputDevice(mOutputs.valueFor(output), newDevice, (newDevice != AUDIO_DEVICE_NONE));
947 }
948 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700949 // if entering in call state, handle special case of active streams
950 // pertaining to sonification strategy see handleIncallSonification()
951 if (isStateInCall(state)) {
952 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530953 for (size_t j = 0; j < mOutputs.size(); j++) {
954 audio_io_handle_t curOutput = mOutputs.keyAt(j);
955 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
956 if (stream == AUDIO_STREAM_PATCH) {
957 continue;
958 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530959 handleIncallSonification((audio_stream_type_t)stream, true, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +0530960 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700961 }
Zhou Song58081b62016-02-24 13:10:55 +0800962
963 // force reevaluating accessibility routing when call starts
964 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700965 }
966
967 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
968 if (state == AUDIO_MODE_RINGTONE &&
969 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
970 mLimitRingtoneVolume = true;
971 } else {
972 mLimitRingtoneVolume = false;
973 }
974}
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +0530975
976void AudioPolicyManagerCustom::setForceUse(audio_policy_force_use_t usage,
977 audio_policy_forced_cfg_t config)
978{
Ashish Jain40cab3a2016-03-23 11:14:14 +0530979 ALOGD("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +0530980
981 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
982 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
983 return;
984 }
985 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
986 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
987 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
988
989 // check for device and output changes triggered by new force usage
990 checkA2dpSuspend();
991 checkOutputForAllStrategies();
992 updateDevicesAndOutputs();
Ashish Jain40cab3a2016-03-23 11:14:14 +0530993
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +0530994 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
995 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
996 updateCallRouting(newDevice);
997 }
998 // Use reverse loop to make sure any low latency usecases (generally tones)
999 // are not routed before non LL usecases (generally music).
1000 // We can safely assume that LL output would always have lower index,
1001 // and use this work-around to avoid routing of output with music stream
1002 // from the context of short lived LL output.
1003 // Note: in case output's share backend(HAL sharing is implicit) all outputs
1004 // gets routing update while processing first output itself.
1005 for (size_t i = mOutputs.size(); i > 0; i--) {
1006 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i-1);
1007 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
1008 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || outputDesc != mPrimaryOutput) {
1009 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
1010 }
1011 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
1012 applyStreamVolumes(outputDesc, newDevice, 0, true);
1013 }
1014 }
1015
1016 audio_io_handle_t activeInput = mInputs.getActiveInput();
1017 if (activeInput != 0) {
Ashish Jain40cab3a2016-03-23 11:14:14 +05301018 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
1019 audio_devices_t newDevice = getNewInputDevice(activeInput);
1020 // Force new input selection if the new device can not be reached via current input
1021 if (activeDesc->mProfile->getSupportedDevices().types() & (newDevice & ~AUDIO_DEVICE_BIT_IN)) {
1022 setInputDevice(activeInput, newDevice);
1023 } else {
1024 closeInput(activeInput);
1025 }
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301026 }
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301027}
1028
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301029status_t AudioPolicyManagerCustom::stopSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301030 audio_stream_type_t stream,
1031 bool forceDeviceUpdate)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001032{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301033 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1034 ALOGW("stopSource() invalid stream %d", stream);
1035 return INVALID_OPERATION;
1036 }
Sharad Sangle36781612015-05-28 16:15:16 +05301037 // always handle stream stop, check which stream type is stopping
1038 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001039
Sharad Sangle36781612015-05-28 16:15:16 +05301040 // handle special case for sonification while in call
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001041 if (isInCall()) {
Sharad Sangle36781612015-05-28 16:15:16 +05301042 if (outputDesc->isDuplicated()) {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301043 handleIncallSonification(stream, false, false, outputDesc->subOutput1()->mIoHandle);
1044 handleIncallSonification(stream, false, false, outputDesc->subOutput2()->mIoHandle);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001045 }
Sharad Sangle36781612015-05-28 16:15:16 +05301046 handleIncallSonification(stream, false, false, outputDesc->mIoHandle);
1047 }
1048
1049 if (outputDesc->mRefCount[stream] > 0) {
1050 // decrement usage count of this stream on the output
1051 outputDesc->changeRefCount(stream, -1);
1052
1053 // store time at which the stream was stopped - see isStreamActive()
1054 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
1055 outputDesc->mStopTime[stream] = systemTime();
Zhou Song5dcddc92015-09-21 14:36:57 +08001056 audio_devices_t prevDevice = outputDesc->device();
Sharad Sangle36781612015-05-28 16:15:16 +05301057 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
1058 // delay the device switch by twice the latency because stopOutput() is executed when
1059 // the track stop() command is received and at that time the audio track buffer can
1060 // still contain data that needs to be drained. The latency only covers the audio HAL
1061 // and kernel buffers. Also the latency does not always include additional delay in the
1062 // audio path (audio DSP, CODEC ...)
1063 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
1064
1065 // force restoring the device selection on other active outputs if it differs from the
1066 // one being selected for this output
1067 for (size_t i = 0; i < mOutputs.size(); i++) {
1068 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1069 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1070 if (desc != outputDesc &&
1071 desc->isActive() &&
1072 outputDesc->sharesHwModuleWith(desc) &&
1073 (newDevice != desc->device())) {
Sharad Sangle4509cef2015-08-19 20:47:12 +05301074 audio_devices_t dev = getNewOutputDevice(mOutputs.valueFor(curOutput), false /*fromCache*/);
Zhou Song5dcddc92015-09-21 14:36:57 +08001075 uint32_t delayMs;
1076 if (dev == prevDevice) {
1077 delayMs = 0;
1078 } else {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301079 delayMs = outputDesc->latency()*2;
Zhou Song5dcddc92015-09-21 14:36:57 +08001080 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301081 setOutputDevice(desc,
1082 dev,
Sharad Sangle36781612015-05-28 16:15:16 +05301083 true,
Zhou Song5dcddc92015-09-21 14:36:57 +08001084 delayMs);
Sharad Sangle36781612015-05-28 16:15:16 +05301085 }
1086 }
1087 // update the outputs if stopping one with a stream that can affect notification routing
1088 handleNotificationRoutingForStream(stream);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001089 }
Sharad Sangle36781612015-05-28 16:15:16 +05301090 return NO_ERROR;
1091 } else {
1092 ALOGW("stopOutput() refcount is already 0");
1093 return INVALID_OPERATION;
1094 }
1095}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001096
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301097status_t AudioPolicyManagerCustom::startSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301098 audio_stream_type_t stream,
1099 audio_devices_t device,
Lalit Kansaraf803ac52016-05-09 19:12:01 +05301100 const char *address,
Sharad Sangle36781612015-05-28 16:15:16 +05301101 uint32_t *delayMs)
1102{
1103 // cannot start playback of STREAM_TTS if any other output is being used
1104 uint32_t beaconMuteLatency = 0;
1105
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301106 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1107 ALOGW("startSource() invalid stream %d", stream);
1108 return INVALID_OPERATION;
1109 }
1110
Sharad Sangle36781612015-05-28 16:15:16 +05301111 *delayMs = 0;
1112 if (stream == AUDIO_STREAM_TTS) {
1113 ALOGV("\t found BEACON stream");
Ashish Jain40cab3a2016-03-23 11:14:14 +05301114 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301115 return INVALID_OPERATION;
1116 } else {
1117 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001118 }
Sharad Sangle36781612015-05-28 16:15:16 +05301119 } else {
1120 // some playback other than beacon starts
1121 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1122 }
1123
Ashish Jain40cab3a2016-03-23 11:14:14 +05301124 // check active before incrementing usage count
1125 bool force = !outputDesc->isActive();
1126
Sharad Sangle36781612015-05-28 16:15:16 +05301127 // increment usage count for this stream on the requested output:
1128 // NOTE that the usage count is the same for duplicated output and hardware output which is
1129 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1130 outputDesc->changeRefCount(stream, 1);
1131
1132 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
1133 // starting an output being rerouted?
1134 if (device == AUDIO_DEVICE_NONE) {
1135 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001136 }
Sharad Sangle36781612015-05-28 16:15:16 +05301137 routing_strategy strategy = getStrategy(stream);
1138 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
1139 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1140 (beaconMuteLatency > 0);
1141 uint32_t waitMs = beaconMuteLatency;
Sharad Sangle36781612015-05-28 16:15:16 +05301142 for (size_t i = 0; i < mOutputs.size(); i++) {
1143 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1144 if (desc != outputDesc) {
1145 // force a device change if any other output is managed by the same hw
1146 // module and has a current device selection that differs from selected device.
1147 // In this case, the audio HAL must receive the new device selection so that it can
1148 // change the device currently selected by the other active output.
1149 if (outputDesc->sharesHwModuleWith(desc) &&
1150 desc->device() != device) {
1151 force = true;
1152 }
1153 // wait for audio on other active outputs to be presented when starting
1154 // a notification so that audio focus effect can propagate, or that a mute/unmute
1155 // event occurred for beacon
1156 uint32_t latency = desc->latency();
1157 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1158 waitMs = latency;
1159 }
1160 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001161 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05301162 uint32_t muteWaitMs;
Lalit Kansaraf803ac52016-05-09 19:12:01 +05301163 muteWaitMs = setOutputDevice(outputDesc, device, force, 0, NULL, address);
Sharad Sangle36781612015-05-28 16:15:16 +05301164
1165 // handle special case for sonification while in call
1166 if (isInCall()) {
1167 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001168 }
Sharad Sangle36781612015-05-28 16:15:16 +05301169
1170 // apply volume rules for current stream and device if necessary
1171 checkAndSetVolume(stream,
Ashish Jain40cab3a2016-03-23 11:14:14 +05301172 mVolumeCurves->getVolumeIndex(stream, device),
Sharad Sangle36781612015-05-28 16:15:16 +05301173 outputDesc,
1174 device);
1175
1176 // update the outputs if starting an output with a stream that can affect notification
1177 // routing
1178 handleNotificationRoutingForStream(stream);
1179
1180 // force reevaluating accessibility routing when ringtone or alarm starts
1181 if (strategy == STRATEGY_SONIFICATION) {
1182 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1183 }
1184 }
1185 else {
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001186 // handle special case for sonification while in call
1187 if (isInCall()) {
1188 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Sharad Sangle36781612015-05-28 16:15:16 +05301189 }
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001190 }
Sharad Sangle36781612015-05-28 16:15:16 +05301191 return NO_ERROR;
1192}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001193
Sharad Sangle36781612015-05-28 16:15:16 +05301194void AudioPolicyManagerCustom::handleIncallSonification(audio_stream_type_t stream,
1195 bool starting, bool stateChange,
1196 audio_io_handle_t output)
1197{
1198 if(!hasPrimaryOutput()) {
1199 return;
1200 }
1201 // no action needed for AUDIO_STREAM_PATCH stream type, it's for internal flinger tracks
1202 if (stream == AUDIO_STREAM_PATCH) {
1203 return;
1204 }
1205 // if the stream pertains to sonification strategy and we are in call we must
1206 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
1207 // in the device used for phone strategy and play the tone if the selected device does not
1208 // interfere with the device used for phone strategy
1209 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
1210 // many times as there are active tracks on the output
1211 const routing_strategy stream_strategy = getStrategy(stream);
1212 if ((stream_strategy == STRATEGY_SONIFICATION) ||
1213 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
1214 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
1215 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
1216 stream, starting, outputDesc->mDevice, stateChange);
1217 if (outputDesc->mRefCount[stream]) {
1218 int muteCount = 1;
1219 if (stateChange) {
1220 muteCount = outputDesc->mRefCount[stream];
1221 }
1222 if (audio_is_low_visibility(stream)) {
1223 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
1224 for (int i = 0; i < muteCount; i++) {
1225 setStreamMute(stream, starting, outputDesc);
1226 }
1227 } else {
1228 ALOGV("handleIncallSonification() high visibility");
1229 if (outputDesc->device() &
1230 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
1231 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
1232 for (int i = 0; i < muteCount; i++) {
1233 setStreamMute(stream, starting, outputDesc);
1234 }
1235 }
1236 if (starting) {
1237 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
1238 AUDIO_STREAM_VOICE_CALL);
1239 } else {
1240 mpClientInterface->stopTone();
1241 }
1242 }
1243 }
1244 }
1245}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001246
Sharad Sangle36781612015-05-28 16:15:16 +05301247void AudioPolicyManagerCustom::handleNotificationRoutingForStream(audio_stream_type_t stream) {
1248 switch(stream) {
1249 case AUDIO_STREAM_MUSIC:
1250 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
1251 updateDevicesAndOutputs();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001252 break;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001253 default:
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001254 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001255 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001256}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001257
Sharad Sangle36781612015-05-28 16:15:16 +05301258status_t AudioPolicyManagerCustom::checkAndSetVolume(audio_stream_type_t stream,
1259 int index,
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301260 const sp<AudioOutputDescriptor>& outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301261 audio_devices_t device,
1262 int delayMs, bool force)
1263{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301264 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1265 ALOGW("checkAndSetVolume() invalid stream %d", stream);
1266 return INVALID_OPERATION;
1267 }
Sharad Sangle36781612015-05-28 16:15:16 +05301268 // do not change actual stream volume if the stream is muted
1269 if (outputDesc->mMuteCount[stream] != 0) {
1270 ALOGVV("checkAndSetVolume() stream %d muted count %d",
1271 stream, outputDesc->mMuteCount[stream]);
1272 return NO_ERROR;
1273 }
1274 audio_policy_forced_cfg_t forceUseForComm =
1275 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
1276 // do not change in call volume if bluetooth is connected and vice versa
1277 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
1278 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
1279 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
1280 stream, forceUseForComm);
1281 return INVALID_OPERATION;
1282 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001283
Sharad Sangle36781612015-05-28 16:15:16 +05301284 if (device == AUDIO_DEVICE_NONE) {
1285 device = outputDesc->device();
1286 }
1287
1288 float volumeDb = computeVolume(stream, index, device);
1289 if (outputDesc->isFixedVolume(device)) {
1290 volumeDb = 0.0f;
1291 }
1292
1293 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
1294
1295 if (stream == AUDIO_STREAM_VOICE_CALL ||
1296 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
1297 float voiceVolume;
1298 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
1299 if (stream == AUDIO_STREAM_VOICE_CALL) {
Ashish Jain40cab3a2016-03-23 11:14:14 +05301300 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Sharad Sangle36781612015-05-28 16:15:16 +05301301 } else {
1302 voiceVolume = 1.0;
1303 }
1304
1305 if (voiceVolume != mLastVoiceVolume && ((outputDesc == mPrimaryOutput) ||
1306 isDirectOutput(outputDesc->mIoHandle) || device & AUDIO_DEVICE_OUT_ALL_USB)) {
1307 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
1308 mLastVoiceVolume = voiceVolume;
1309 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301310#ifdef FM_POWER_OPT
1311 } else if (stream == AUDIO_STREAM_MUSIC && hasPrimaryOutput() &&
Haynes Mathew George603ae9b2015-12-21 17:23:59 -08001312 outputDesc == mPrimaryOutput && mFMIsActive) {
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001313 /* Avoid unnecessary set_parameter calls as it puts the primary
1314 outputs FastMixer in HOT_IDLE leading to breaks in audio */
1315 if (volumeDb != mPrevFMVolumeDb) {
1316 mPrevFMVolumeDb = volumeDb;
1317 AudioParameter param = AudioParameter();
1318 param.addFloat(String8("fm_volume"), Volume::DbToAmpl(volumeDb));
Mingming Yin1433cc62016-01-04 14:58:00 -08001319 //Double delayMs to avoid sound burst while device switch.
1320 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString(), delayMs*2);
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001321 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301322#endif /* FM_POWER_OPT end */
Sharad Sangle36781612015-05-28 16:15:16 +05301323 }
1324
1325 return NO_ERROR;
1326}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001327
Sharad Sangle36781612015-05-28 16:15:16 +05301328bool AudioPolicyManagerCustom::isDirectOutput(audio_io_handle_t output) {
1329 for (size_t i = 0; i < mOutputs.size(); i++) {
1330 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1331 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1332 if ((curOutput == output) && (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
1333 return true;
1334 }
1335 }
1336 return false;
1337}
vivek mehta0ea887a2015-08-26 14:01:20 -07001338
Ashish Jainac510a72016-04-28 12:22:58 +05301339bool static isDirectPCMEnabled(int bitWidth)
1340{
1341 bool directPCMEnabled = false;
1342 if (bitWidth == 24 || bitWidth == 32)
1343 directPCMEnabled =
1344 property_get_bool("audio.offload.pcm.24bit.enable", false);
1345 else
1346 directPCMEnabled =
1347 property_get_bool("audio.offload.pcm.16bit.enable", false);
1348
1349 return directPCMEnabled;
1350}
1351
vivek mehta0ea887a2015-08-26 14:01:20 -07001352status_t AudioPolicyManagerCustom::getOutputForAttr(const audio_attributes_t *attr,
1353 audio_io_handle_t *output,
1354 audio_session_t session,
1355 audio_stream_type_t *stream,
1356 uid_t uid,
1357 uint32_t samplingRate,
1358 audio_format_t format,
1359 audio_channel_mask_t channelMask,
1360 audio_output_flags_t flags,
1361 audio_port_handle_t selectedDeviceId,
1362 const audio_offload_info_t *offloadInfo)
1363{
1364 audio_offload_info_t tOffloadInfo = AUDIO_INFO_INITIALIZER;
1365
Alexy Joseph0ac09032015-10-16 15:57:53 -07001366 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
Ashish Jainac510a72016-04-28 12:22:58 +05301367 uint32_t bitWidth = (audio_bytes_per_sample(format) * 8);
Alexy Joseph0ac09032015-10-16 15:57:53 -07001368
1369 if (offloadDisabled) {
1370 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
1371 }
1372
Ashish Jainac510a72016-04-28 12:22:58 +05301373 if (!offloadDisabled && (offloadInfo == NULL) &&
1374 isDirectPCMEnabled(bitWidth) &&
1375 (flags == AUDIO_OUTPUT_FLAG_NONE)) {
vivek mehta0ea887a2015-08-26 14:01:20 -07001376
vivek mehta0ea887a2015-08-26 14:01:20 -07001377 tOffloadInfo.sample_rate = samplingRate;
1378 tOffloadInfo.channel_mask = channelMask;
1379 tOffloadInfo.format = format;
1380 tOffloadInfo.stream_type = *stream;
Ashish Jainac510a72016-04-28 12:22:58 +05301381 tOffloadInfo.bit_width = bitWidth;
vivek mehta0ea887a2015-08-26 14:01:20 -07001382 if (attr != NULL) {
1383 ALOGV("found attribute .. setting usage %d ", attr->usage);
1384 tOffloadInfo.usage = attr->usage;
1385 } else {
Alexy Joseph0ac09032015-10-16 15:57:53 -07001386 ALOGI("%s:: attribute is NULL .. no usage set", __func__);
vivek mehta0ea887a2015-08-26 14:01:20 -07001387 }
1388 offloadInfo = &tOffloadInfo;
1389 }
1390
1391 return AudioPolicyManager::getOutputForAttr(attr, output, session, stream,
1392 (uid_t)uid, (uint32_t)samplingRate,
1393 format, (audio_channel_mask_t)channelMask,
1394 flags, (audio_port_handle_t)selectedDeviceId,
1395 offloadInfo);
1396}
1397
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001398audio_io_handle_t AudioPolicyManagerCustom::getOutputForDevice(
1399 audio_devices_t device,
Sharad Sangle36781612015-05-28 16:15:16 +05301400 audio_session_t session __unused,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001401 audio_stream_type_t stream,
1402 uint32_t samplingRate,
1403 audio_format_t format,
1404 audio_channel_mask_t channelMask,
1405 audio_output_flags_t flags,
1406 const audio_offload_info_t *offloadInfo)
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001407{
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001408 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001409 status_t status;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001410
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001411#ifdef AUDIO_POLICY_TEST
1412 if (mCurOutput != 0) {
1413 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1414 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1415
1416 if (mTestOutputs[mCurOutput] == 0) {
1417 ALOGV("getOutput() opening test output");
Sharad Sangle36781612015-05-28 16:15:16 +05301418 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
1419 mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001420 outputDesc->mDevice = mTestDevice;
1421 outputDesc->mLatency = mTestLatencyMs;
1422 outputDesc->mFlags =
1423 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
1424 outputDesc->mRefCount[stream] = 0;
1425 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1426 config.sample_rate = mTestSamplingRate;
1427 config.channel_mask = mTestChannels;
1428 config.format = mTestFormat;
1429 if (offloadInfo != NULL) {
1430 config.offload_info = *offloadInfo;
1431 }
1432 status = mpClientInterface->openOutput(0,
1433 &mTestOutputs[mCurOutput],
1434 &config,
1435 &outputDesc->mDevice,
1436 String8(""),
1437 &outputDesc->mLatency,
1438 outputDesc->mFlags);
1439 if (status == NO_ERROR) {
1440 outputDesc->mSamplingRate = config.sample_rate;
1441 outputDesc->mFormat = config.format;
1442 outputDesc->mChannelMask = config.channel_mask;
1443 AudioParameter outputCmd = AudioParameter();
1444 outputCmd.addInt(String8("set_id"),mCurOutput);
1445 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1446 addOutput(mTestOutputs[mCurOutput], outputDesc);
1447 }
1448 }
1449 return mTestOutputs[mCurOutput];
1450 }
1451#endif //AUDIO_POLICY_TEST
Sharad Sangle36781612015-05-28 16:15:16 +05301452 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1453 (stream != AUDIO_STREAM_MUSIC)) {
1454 // compress should not be used for non-music streams
1455 ALOGE("Offloading only allowed with music stream");
1456 return 0;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301457 }
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301458
1459 if ((stream == AUDIO_STREAM_VOICE_CALL) &&
1460 (channelMask == 1) &&
Shiv Maliyappanahallid77d3b32016-02-02 13:45:50 -08001461 (samplingRate == 8000 || samplingRate == 16000 ||
1462 samplingRate == 32000 || samplingRate == 48000)) {
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301463 // Allow Voip direct output only if:
1464 // audio mode is MODE_IN_COMMUNCATION; AND
1465 // voip output is not opened already; AND
1466 // requested sample rate matches with that of voip input stream (if opened already)
1467 int value = 0;
1468 uint32_t mode = 0, voipOutCount = 1, voipSampleRate = 1;
1469 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1470 String8("audio_mode"));
1471 AudioParameter result = AudioParameter(valueStr);
1472 if (result.getInt(String8("audio_mode"), value) == NO_ERROR) {
1473 mode = value;
1474 }
1475
1476 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1477 String8("voip_out_stream_count"));
1478 result = AudioParameter(valueStr);
1479 if (result.getInt(String8("voip_out_stream_count"), value) == NO_ERROR) {
1480 voipOutCount = value;
1481 }
1482
1483 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1484 String8("voip_sample_rate"));
1485 result = AudioParameter(valueStr);
1486 if (result.getInt(String8("voip_sample_rate"), value) == NO_ERROR) {
1487 voipSampleRate = value;
1488 }
1489
1490 if ((mode == AUDIO_MODE_IN_COMMUNICATION) && (voipOutCount == 0) &&
1491 ((voipSampleRate == 0) || (voipSampleRate == samplingRate))) {
1492 if (audio_is_linear_pcm(format)) {
1493 char propValue[PROPERTY_VALUE_MAX] = {0};
1494 property_get("use.voice.path.for.pcm.voip", propValue, "0");
1495 bool voipPcmSysPropEnabled = !strncmp("true", propValue, sizeof("true"));
1496 if (voipPcmSysPropEnabled && (format == AUDIO_FORMAT_PCM_16_BIT)) {
Karthik Reddy Katta3fb9be72015-11-25 11:18:27 +05301497 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX |
1498 AUDIO_OUTPUT_FLAG_DIRECT);
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301499 ALOGD("Set VoIP and Direct output flags for PCM format");
1500 }
1501 }
1502 }
1503 }
1504
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301505#ifdef VOICE_CONCURRENCY
1506 char propValue[PROPERTY_VALUE_MAX];
1507 bool prop_play_enabled=false, prop_voip_enabled = false;
1508
1509 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1510 prop_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001511 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301512
1513 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1514 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1515 }
1516
1517 if (prop_play_enabled && mvoice_call_state) {
1518 //check if voice call is active / running in background
1519 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1520 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1521 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1522 {
1523 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1524 if(prop_voip_enabled) {
1525 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1526 flags );
1527 return 0;
1528 }
1529 }
1530 else {
1531 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
1532 ALOGD("voice_conc:IN call mode adding ULL flags .. flags: %x ", flags );
1533 flags = AUDIO_OUTPUT_FLAG_FAST;
1534 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
1535 if (AUDIO_STREAM_MUSIC == stream) {
1536 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1537 ALOGD("voice_conc:IN call mode adding deep-buffer flags %x ", flags );
1538 }
1539 else {
1540 flags = AUDIO_OUTPUT_FLAG_FAST;
1541 ALOGD("voice_conc:IN call mode adding fast flags %x ", flags );
1542 }
1543 }
1544 }
1545 }
1546 } else if (prop_voip_enabled && mvoice_call_state) {
1547 //check if voice call is active / running in background
1548 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1549 //return only ULL ouput
1550 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1551 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1552 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1553 {
1554 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1555 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1556 flags );
1557 return 0;
1558 }
1559 }
1560 }
1561#endif
1562#ifdef RECORD_PLAY_CONCURRENCY
1563 char recConcPropValue[PROPERTY_VALUE_MAX];
1564 bool prop_rec_play_enabled = false;
1565
1566 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
1567 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
1568 }
1569 if ((prop_rec_play_enabled) &&
1570 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
1571 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
1572 if (AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1573 // allow VoIP using voice path
1574 // Do nothing
1575 } else if((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1576 ALOGD("voice_conc:MODE_IN_COMM is setforcing deep buffer output for non ULL... flags: %x", flags);
1577 // use deep buffer path for all non ULL outputs
1578 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1579 }
1580 } else if ((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1581 ALOGD("voice_conc:Record mode is on forcing deep buffer output for non ULL... flags: %x ", flags);
1582 // use deep buffer path for all non ULL outputs
1583 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1584 }
1585 }
1586 if (prop_rec_play_enabled &&
1587 (stream == AUDIO_STREAM_ENFORCED_AUDIBLE)) {
1588 ALOGD("Record conc is on forcing ULL output for ENFORCED_AUDIBLE");
1589 flags = AUDIO_OUTPUT_FLAG_FAST;
1590 }
1591#endif
1592
Sharad Sangle4509cef2015-08-19 20:47:12 +05301593#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Sharad Sangle36781612015-05-28 16:15:16 +05301594 /*
1595 * WFD audio routes back to target speaker when starting a ringtone playback.
1596 * This is because primary output is reused for ringtone, so output device is
1597 * updated based on SONIFICATION strategy for both ringtone and music playback.
1598 * The same issue is not seen on remoted_submix HAL based WFD audio because
1599 * primary output is not reused and a new output is created for ringtone playback.
1600 * Issue is fixed by updating output flag to AUDIO_OUTPUT_FLAG_FAST when there is
1601 * a non-music stream playback on WFD, so primary output is not reused for ringtone.
1602 */
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001603 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
1604 if ((availableOutputDeviceTypes & AUDIO_DEVICE_OUT_PROXY)
1605 && (stream != AUDIO_STREAM_MUSIC)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301606 ALOGD("WFD audio: use OUTPUT_FLAG_FAST for non music stream. flags:%x", flags );
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001607 //For voip paths
1608 if(flags & AUDIO_OUTPUT_FLAG_DIRECT)
1609 flags = AUDIO_OUTPUT_FLAG_DIRECT;
1610 else //route every thing else to ULL path
1611 flags = AUDIO_OUTPUT_FLAG_FAST;
1612 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301613#endif
1614
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001615 // open a direct output if required by specified parameters
vivek mehta0ea887a2015-08-26 14:01:20 -07001616 // force direct flag if offload flag is set: offloading implies a direct output stream
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001617 // and all common behaviors are driven by checking only the direct flag
1618 // this should normally be set appropriately in the policy configuration file
1619 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1620 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1621 }
1622 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1623 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1624 }
vivek mehta0ea887a2015-08-26 14:01:20 -07001625
vivek mehta4b0d8192015-10-16 00:25:59 -07001626 bool forced_deep = false;
Sharad Sangle36781612015-05-28 16:15:16 +05301627 // only allow deep buffering for music stream type
1628 if (stream != AUDIO_STREAM_MUSIC) {
1629 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Sharad Sangle497aef82015-08-03 17:55:48 +05301630 } else if (/* stream == AUDIO_STREAM_MUSIC && */
1631 flags == AUDIO_OUTPUT_FLAG_NONE &&
1632 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
vivek mehtac984b992015-11-25 13:28:55 -08001633 forced_deep = true;
Sharad Sangle36781612015-05-28 16:15:16 +05301634 }
Sharad Sangle497aef82015-08-03 17:55:48 +05301635
Sharad Sangle36781612015-05-28 16:15:16 +05301636 if (stream == AUDIO_STREAM_TTS) {
1637 flags = AUDIO_OUTPUT_FLAG_TTS;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001638 }
1639
Zhou Song2b3d1552016-03-21 17:21:04 +08001640 // check if direct output for track offload already exits
1641 bool is_track_offload_active = false;
1642 for (size_t i = 0; i < mOutputs.size(); i++) {
1643 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1644 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) {
1645 is_track_offload_active = true;
1646 ALOGD("Track offload already active");
1647 break;
1648 }
1649 }
1650
vivek mehta4b0d8192015-10-16 00:25:59 -07001651 // Do offload magic here
vivek mehtac984b992015-11-25 13:28:55 -08001652 if ((flags == AUDIO_OUTPUT_FLAG_NONE) &&
1653 (stream == AUDIO_STREAM_MUSIC) &&
Zhou Song2b3d1552016-03-21 17:21:04 +08001654 (offloadInfo != NULL) && !is_track_offload_active &&
vivek mehta4b0d8192015-10-16 00:25:59 -07001655 ((offloadInfo->usage == AUDIO_USAGE_MEDIA) || (offloadInfo->usage == AUDIO_USAGE_GAME))) {
vivek mehtac984b992015-11-25 13:28:55 -08001656 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DIRECT_PCM);
vivek mehta4b0d8192015-10-16 00:25:59 -07001657 ALOGD("AudioCustomHAL --> Force Direct Flag .. flag (0x%x)", flags);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301658 }
1659
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001660 sp<IOProfile> profile;
1661
1662 // skip direct output selection if the request can obviously be attached to a mixed output
1663 // and not explicitly requested
vivek mehtac984b992015-11-25 13:28:55 -08001664 if (((flags & (AUDIO_OUTPUT_FLAG_DIRECT|AUDIO_OUTPUT_FLAG_DIRECT_PCM)) == 0) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301665 audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX &&
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001666 audio_channel_count_from_out_mask(channelMask) <= 2) {
1667 goto non_direct_output;
1668 }
1669
1670 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1671 // creating an offloaded track and tearing it down immediately after start when audioflinger
1672 // detects there is an active non offloadable effect.
1673 // FIXME: We should check the audio session here but we do not have it in this context.
1674 // This may prevent offloading in rare situations where effects are left active by apps
1675 // in the background.
Weiyin Jiang6bba3202016-03-03 14:49:36 +08001676 //
1677 // Supplementary annotation:
1678 // For sake of track offload introduced, we need a rollback for both compress offload
1679 // and track offload use cases.
1680 if ((flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD|AUDIO_OUTPUT_FLAG_DIRECT_PCM)) &&
1681 (mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
1682 ALOGD("non offloadable effect is enabled, try with non direct output");
1683 goto non_direct_output;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001684 }
1685
Weiyin Jiang6bba3202016-03-03 14:49:36 +08001686 profile = getProfileForDirectOutput(device,
1687 samplingRate,
1688 format,
1689 channelMask,
1690 (audio_output_flags_t)flags);
1691
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001692 if (profile != 0) {
vivek mehtac984b992015-11-25 13:28:55 -08001693
1694 if (!(flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301695 (profile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT_PCM)) {
vivek mehtac984b992015-11-25 13:28:55 -08001696 ALOGI("got Direct_PCM without requesting ... reject ");
1697 profile = NULL;
1698 goto non_direct_output;
1699 }
1700
Sharad Sangle36781612015-05-28 16:15:16 +05301701 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001702
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001703 // if multiple concurrent offload decode is supported
1704 // do no check for reuse and also don't close previous output if its offload
1705 // previous output will be closed during track destruction
1706 if (!(property_get_bool("audio.offload.multiple.enabled", false) &&
1707 ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0))) {
1708 for (size_t i = 0; i < mOutputs.size(); i++) {
1709 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1710 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1711 outputDesc = desc;
1712 // reuse direct output if currently open and configured with same parameters
1713 if ((samplingRate == outputDesc->mSamplingRate) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301714 audio_formats_match(format, outputDesc->mFormat) &&
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001715 (channelMask == outputDesc->mChannelMask)) {
1716 outputDesc->mDirectOpenCount++;
1717 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1718 return mOutputs.keyAt(i);
1719 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001720 }
1721 }
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001722 // close direct output if currently open and configured with different parameters
1723 if (outputDesc != NULL) {
1724 closeOutput(outputDesc->mIoHandle);
1725 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001726 }
Sharad Sangle36781612015-05-28 16:15:16 +05301727
1728 // if the selected profile is offloaded and no offload info was specified,
1729 // create a default one
1730 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
Ashish Jain40cab3a2016-03-23 11:14:14 +05301731 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
Sharad Sangle36781612015-05-28 16:15:16 +05301732 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1733 defaultOffloadInfo.sample_rate = samplingRate;
1734 defaultOffloadInfo.channel_mask = channelMask;
1735 defaultOffloadInfo.format = format;
1736 defaultOffloadInfo.stream_type = stream;
1737 defaultOffloadInfo.bit_rate = 0;
1738 defaultOffloadInfo.duration_us = -1;
1739 defaultOffloadInfo.has_video = true; // conservative
1740 defaultOffloadInfo.is_streaming = true; // likely
1741 offloadInfo = &defaultOffloadInfo;
1742 }
1743
1744 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001745 outputDesc->mDevice = device;
1746 outputDesc->mLatency = 0;
Sharad Sangle36781612015-05-28 16:15:16 +05301747 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001748 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1749 config.sample_rate = samplingRate;
1750 config.channel_mask = channelMask;
1751 config.format = format;
1752 if (offloadInfo != NULL) {
1753 config.offload_info = *offloadInfo;
1754 }
Sharad Sangle36781612015-05-28 16:15:16 +05301755 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001756 &output,
1757 &config,
1758 &outputDesc->mDevice,
1759 String8(""),
1760 &outputDesc->mLatency,
1761 outputDesc->mFlags);
1762
1763 // only accept an output with the requested parameters
1764 if (status != NO_ERROR ||
1765 (samplingRate != 0 && samplingRate != config.sample_rate) ||
Ashish Jain40cab3a2016-03-23 11:14:14 +05301766 (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) ||
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001767 (channelMask != 0 && channelMask != config.channel_mask)) {
1768 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1769 "format %d %d, channelMask %04x %04x", output, samplingRate,
1770 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1771 outputDesc->mChannelMask);
1772 if (output != AUDIO_IO_HANDLE_NONE) {
1773 mpClientInterface->closeOutput(output);
1774 }
Sharad Sangle36781612015-05-28 16:15:16 +05301775 // fall back to mixer output if possible when the direct output could not be open
Ashish Jain40cab3a2016-03-23 11:14:14 +05301776 if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) {
Sharad Sangle36781612015-05-28 16:15:16 +05301777 goto non_direct_output;
1778 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001779 return AUDIO_IO_HANDLE_NONE;
1780 }
1781 outputDesc->mSamplingRate = config.sample_rate;
1782 outputDesc->mChannelMask = config.channel_mask;
1783 outputDesc->mFormat = config.format;
1784 outputDesc->mRefCount[stream] = 0;
1785 outputDesc->mStopTime[stream] = 0;
1786 outputDesc->mDirectOpenCount = 1;
1787
1788 audio_io_handle_t srcOutput = getOutputForEffect();
1789 addOutput(output, outputDesc);
1790 audio_io_handle_t dstOutput = getOutputForEffect();
1791 if (dstOutput == output) {
Gao Jiedb057832015-11-12 08:51:22 +08001792#ifdef DOLBY_ENABLE
1793 status_t status = mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1794 if (status == NO_ERROR) {
1795 for (size_t i = 0; i < mEffects.size(); i++) {
1796 sp<EffectDescriptor> desc = mEffects.valueAt(i);
1797 if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX) {
1798 // update the mIo member of EffectDescriptor for the global effect
1799 ALOGV("%s updating mIo", __FUNCTION__);
1800 desc->mIo = dstOutput;
1801 }
1802 }
1803 } else {
1804 ALOGW("%s moveEffects from %d to %d failed", __FUNCTION__, srcOutput, dstOutput);
1805 }
1806#else // DOLBY_END
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001807 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
Gao Jiedb057832015-11-12 08:51:22 +08001808#endif // LINE_ADDED_BY_DOLBY
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001809 }
1810 mPreviousOutputs = mOutputs;
1811 ALOGV("getOutput() returns new direct output %d", output);
1812 mpClientInterface->onAudioPortListUpdate();
1813 return output;
1814 }
1815
1816non_direct_output:
Ashish Jain5fe12412016-04-18 18:44:17 +05301817
1818 // A request for HW A/V sync cannot fallback to a mixed output because time
1819 // stamps are embedded in audio data
1820 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1821 return AUDIO_IO_HANDLE_NONE;
1822 }
1823
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001824 // ignoring channel mask due to downmix capability in mixer
1825
1826 // open a non direct output
1827
1828 // for non direct outputs, only PCM is supported
1829 if (audio_is_linear_pcm(format)) {
1830 // get which output is suitable for the specified stream. The actual
1831 // routing change will happen when startOutput() will be called
1832 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1833
1834 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1835 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
Ashish Jain660d3112016-06-20 10:16:51 +05301836
1837 if (forced_deep) {
1838 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
1839 ALOGI("setting force DEEP buffer now ");
1840 } else if(flags == AUDIO_OUTPUT_FLAG_NONE) {
1841 // no deep buffer playback is requested hence fallback to primary
1842 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_PRIMARY);
1843 ALOGI("FLAG None hence request for a primary output");
1844 }
1845
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001846 output = selectOutput(outputs, flags, format);
1847 }
1848 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1849 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1850
vivek mehta0ea887a2015-08-26 14:01:20 -07001851 ALOGV("getOutputForDevice() returns output %d", output);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001852
1853 return output;
1854}
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301855
1856status_t AudioPolicyManagerCustom::getInputForAttr(const audio_attributes_t *attr,
1857 audio_io_handle_t *input,
1858 audio_session_t session,
1859 uid_t uid,
1860 uint32_t samplingRate,
1861 audio_format_t format,
1862 audio_channel_mask_t channelMask,
1863 audio_input_flags_t flags,
1864 audio_port_handle_t selectedDeviceId,
1865 input_type_t *inputType)
1866{
Ashish Jain40cab3a2016-03-23 11:14:14 +05301867 audio_source_t inputSource;
1868 inputSource = attr->source;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301869#ifdef VOICE_CONCURRENCY
1870
1871 char propValue[PROPERTY_VALUE_MAX];
1872 bool prop_rec_enabled=false, prop_voip_enabled = false;
1873
1874 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
1875 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1876 }
1877
1878 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1879 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1880 }
1881
1882 if (prop_rec_enabled && mvoice_call_state) {
1883 //check if voice call is active / running in background
1884 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1885 //Need to block input request
1886 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1887 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1888 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1889 {
1890 switch(inputSource) {
1891 case AUDIO_SOURCE_VOICE_UPLINK:
1892 case AUDIO_SOURCE_VOICE_DOWNLINK:
1893 case AUDIO_SOURCE_VOICE_CALL:
1894 ALOGD("voice_conc:Creating input during incall mode for inputSource: %d",
1895 inputSource);
1896 break;
1897
1898 case AUDIO_SOURCE_VOICE_COMMUNICATION:
1899 if(prop_voip_enabled) {
1900 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1901 inputSource);
1902 return NO_INIT;
1903 }
1904 break;
1905 default:
1906 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1907 inputSource);
1908 return NO_INIT;
1909 }
1910 }
1911 }//check for VoIP flag
1912 else if(prop_voip_enabled && mvoice_call_state) {
1913 //check if voice call is active / running in background
1914 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1915 //Need to block input request
1916 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1917 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1918 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1919 {
1920 if(inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1921 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
1922 return NO_INIT;
1923 }
1924 }
1925 }
1926
1927#endif
1928
1929 return AudioPolicyManager::getInputForAttr(attr,
1930 input,
1931 session,
1932 uid,
1933 samplingRate,
1934 format,
1935 channelMask,
1936 flags,
1937 selectedDeviceId,
1938 inputType);
1939}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001940
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301941status_t AudioPolicyManagerCustom::startInput(audio_io_handle_t input,
1942 audio_session_t session)
1943{
1944 ALOGV("startInput() input %d", input);
1945 ssize_t index = mInputs.indexOfKey(input);
1946 if (index < 0) {
1947 ALOGW("startInput() unknown input %d", input);
1948 return BAD_VALUE;
1949 }
1950 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1951
Ashish Jain40cab3a2016-03-23 11:14:14 +05301952 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1953 if (audioSession == 0) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301954 ALOGW("startInput() unknown session %d on input %d", session, input);
1955 return BAD_VALUE;
1956 }
1957
1958 // virtual input devices are compatible with other input devices
1959 if (!is_virtual_input_device(inputDesc->mDevice)) {
1960
1961 // for a non-virtual input device, check if there is another (non-virtual) active input
1962 audio_io_handle_t activeInput = mInputs.getActiveInput();
1963 if (activeInput != 0 && activeInput != input) {
1964
1965 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1966 // otherwise the active input continues and the new input cannot be started.
1967 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Ashish Jain40cab3a2016-03-23 11:14:14 +05301968 if ((activeDesc->inputSource() == AUDIO_SOURCE_HOTWORD) &&
1969 !activeDesc->hasPreemptedSession(session)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301970 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
Ashish Jain40cab3a2016-03-23 11:14:14 +05301971 //FIXME: consider all active sessions
1972 AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions();
1973 audio_session_t activeSession = activeSessions.keyAt(0);
1974 SortedVector<audio_session_t> sessions =
1975 activeDesc->getPreemptedSessions();
1976 sessions.add(activeSession);
1977 inputDesc->setPreemptedSessions(sessions);
1978 stopInput(activeInput, activeSession);
1979 releaseInput(activeInput, activeSession);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301980 } else {
1981 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
1982 return INVALID_OPERATION;
1983 }
1984 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05301985 // Do not allow capture if an active voice call is using a software patch and
1986 // the call TX source device is on the same HW module.
1987 // FIXME: would be better to refine to only inputs whose profile connects to the
1988 // call TX device but this information is not in the audio patch
1989 if (mCallTxPatch != 0 &&
1990 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1991 return INVALID_OPERATION;
1992 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301993 }
1994
1995 // Routing?
1996 mInputRoutes.incRouteActivity(session);
1997#ifdef RECORD_PLAY_CONCURRENCY
1998 mIsInputRequestOnProgress = true;
1999
2000 char getPropValue[PROPERTY_VALUE_MAX];
2001 bool prop_rec_play_enabled = false;
2002
2003 if (property_get("rec.playback.conc.disabled", getPropValue, NULL)) {
2004 prop_rec_play_enabled = atoi(getPropValue) || !strncmp("true", getPropValue, 4);
2005 }
2006
2007 if ((prop_rec_play_enabled) &&(mInputs.activeInputsCount() == 0)){
2008 // send update to HAL on record playback concurrency
2009 AudioParameter param = AudioParameter();
2010 param.add(String8("rec_play_conc_on"), String8("true"));
2011 ALOGD("startInput() setParameters rec_play_conc is setting to ON ");
2012 mpClientInterface->setParameters(0, param.toString());
2013
2014 // Call invalidate to reset all opened non ULL audio tracks
2015 // Move tracks associated to this strategy from previous output to new output
2016 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
2017 // Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder)
Sharad Sangle4509cef2015-08-19 20:47:12 +05302018 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE && (i != AUDIO_STREAM_PATCH))) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302019 ALOGD("Invalidate on releaseInput for stream :: %d ", i);
2020 //FIXME see fixme on name change
2021 mpClientInterface->invalidateStream((audio_stream_type_t)i);
2022 }
2023 }
2024 // close compress tracks
2025 for (size_t i = 0; i < mOutputs.size(); i++) {
2026 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
2027 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
2028 ALOGD("ouput desc / profile is NULL");
2029 continue;
2030 }
2031 if (outputDesc->mProfile->mFlags
2032 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2033 // close compress sessions
2034 ALOGD("calling closeOutput on record conc for COMPRESS output");
2035 closeOutput(mOutputs.keyAt(i));
2036 }
2037 }
Ashish Jaine4a22d52016-06-22 11:55:08 +05302038 // If effects where present on any of the above closed outputs,
2039 // audioflinger moved them to the primary output by default
2040 // move them back to the appropriate output.
2041 moveGlobalEffect();
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302042 }
2043#endif
2044
Ashish Jain40cab3a2016-03-23 11:14:14 +05302045 if (!inputDesc->isActive() || mInputRoutes.hasRouteChanged(session)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302046 // if input maps to a dynamic policy with an activity listener, notify of state change
Ashish Jain630c5e12016-04-18 11:50:21 +05302047 if ((inputDesc->mPolicyMix != NULL)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302048 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Ashish Jain5fe12412016-04-18 18:44:17 +05302049 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302050 MIX_STATE_MIXING);
Ashish Jain630c5e12016-04-18 11:50:21 +05302051 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302052
2053 if (mInputs.activeInputsCount() == 0) {
2054 SoundTrigger::setCaptureState(true);
2055 }
2056 setInputDevice(input, getNewInputDevice(input), true /* force */);
2057
2058 // automatically enable the remote submix output when input is started if not
2059 // used by a policy mix of type MIX_TYPE_RECORDERS
2060 // For remote submix (a virtual device), we open only one input per capture request.
2061 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
2062 String8 address = String8("");
2063 if (inputDesc->mPolicyMix == NULL) {
2064 address = String8("0");
Ashish Jain630c5e12016-04-18 11:50:21 +05302065 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
Ashish Jain5fe12412016-04-18 18:44:17 +05302066 address = inputDesc->mPolicyMix->mDeviceAddress;
Ashish Jain630c5e12016-04-18 11:50:21 +05302067 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302068 if (address != "") {
2069 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2070 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2071 address, "remote-submix");
2072 }
2073 }
2074 }
2075
Ashish Jain40cab3a2016-03-23 11:14:14 +05302076 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302077
Ashish Jain40cab3a2016-03-23 11:14:14 +05302078 audioSession->changeActiveCount(1);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302079#ifdef RECORD_PLAY_CONCURRENCY
2080 mIsInputRequestOnProgress = false;
2081#endif
2082 return NO_ERROR;
2083}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08002084
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302085status_t AudioPolicyManagerCustom::stopInput(audio_io_handle_t input,
2086 audio_session_t session)
2087{
2088 status_t status;
2089 status = AudioPolicyManager::stopInput(input, session);
2090#ifdef RECORD_PLAY_CONCURRENCY
2091 char propValue[PROPERTY_VALUE_MAX];
2092 bool prop_rec_play_enabled = false;
2093
2094 if (property_get("rec.playback.conc.disabled", propValue, NULL)) {
2095 prop_rec_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
2096 }
2097
2098 if ((prop_rec_play_enabled) && (mInputs.activeInputsCount() == 0)) {
2099
2100 //send update to HAL on record playback concurrency
2101 AudioParameter param = AudioParameter();
2102 param.add(String8("rec_play_conc_on"), String8("false"));
2103 ALOGD("stopInput() setParameters rec_play_conc is setting to OFF ");
2104 mpClientInterface->setParameters(0, param.toString());
2105
2106 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
2107 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
2108 //Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder stop tone)
2109 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE) && (i != AUDIO_STREAM_PATCH)) {
2110 ALOGD(" Invalidate on stopInput for stream :: %d ", i);
2111 //FIXME see fixme on name change
2112 mpClientInterface->invalidateStream((audio_stream_type_t)i);
2113 }
2114 }
2115 }
2116#endif
2117 return status;
2118}
2119
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302120void AudioPolicyManagerCustom::closeAllInputs() {
2121 bool patchRemoved = false;
2122
Dhanalakshmi Siddanibfeca7f2015-10-08 15:17:11 +05302123 for(size_t input_index = mInputs.size(); input_index > 0; input_index--) {
2124 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index-1);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302125 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302126 if (patch_index >= 0) {
2127 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302128 status_t status;
2129 status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302130 mAudioPatches.removeItemsAt(patch_index);
2131 patchRemoved = true;
2132 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05302133 if ((inputDesc->getOpenRefCount() > 0) && inputDesc->isSoundTrigger()
2134 && (mInputs.size() == 1)) {
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302135 ALOGD("Do not close sound trigger input handle");
2136 } else {
Dhanalakshmi Siddanibfeca7f2015-10-08 15:17:11 +05302137 mpClientInterface->closeInput(mInputs.keyAt(input_index-1));
2138 mInputs.removeItem(mInputs.keyAt(input_index-1));
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302139 }
2140 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05302141 mInputs.clear();
2142 SoundTrigger::setCaptureState(false);
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302143 nextAudioPortGeneration();
2144
2145 if (patchRemoved) {
2146 mpClientInterface->onAudioPatchListUpdate();
2147 }
2148}
2149
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302150AudioPolicyManagerCustom::AudioPolicyManagerCustom(AudioPolicyClientInterface *clientInterface)
Sharad Sangle4509cef2015-08-19 20:47:12 +05302151 : AudioPolicyManager(clientInterface),
2152 mHdmiAudioDisabled(false),
2153 mHdmiAudioEvent(false),
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08002154 mPrevPhoneState(0),
Haynes Mathew George603ae9b2015-12-21 17:23:59 -08002155 mPrevFMVolumeDb(0.0f),
2156 mFMIsActive(false)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302157{
Ashish Jain40cab3a2016-03-23 11:14:14 +05302158
Satya Krishna Pindiproli8f83f102016-06-21 17:56:14 +05302159#ifdef USE_XML_AUDIO_POLICY_CONF
2160 ALOGD("USE_XML_AUDIO_POLICY_CONF is TRUE");
2161#else
2162 ALOGD("USE_XML_AUDIO_POLICY_CONF is FALSE");
2163#endif
2164
Ashish Jain40cab3a2016-03-23 11:14:14 +05302165 //TODO: Check the new logic to parse policy conf and update the below code
2166 // Need this when SSR encoding is enabled
Mingming Yin38ea08c2015-10-05 15:24:04 -07002167 char ssr_enabled[PROPERTY_VALUE_MAX] = {0};
2168 bool prop_ssr_enabled = false;
2169
2170 if (property_get("ro.qc.sdk.audio.ssr", ssr_enabled, NULL)) {
2171 prop_ssr_enabled = atoi(ssr_enabled) || !strncmp("true", ssr_enabled, 4);
2172 }
2173
2174 for (size_t i = 0; i < mHwModules.size(); i++) {
Ashish Jain40cab3a2016-03-23 11:14:14 +05302175 ALOGV("Hw module %zu", i);
Mingming Yin38ea08c2015-10-05 15:24:04 -07002176 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) {
2177 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Ashish Jain40cab3a2016-03-23 11:14:14 +05302178 AudioProfileVector profiles = inProfile->getAudioProfiles();
2179 for (size_t k = 0; k < profiles.size(); k++){
2180 ChannelsVector channels = profiles[k]->getChannels();
2181 for (size_t x = 0; x < channels.size(); x++) {
2182 audio_channel_mask_t channelMask = channels[x];
2183 ALOGV("Channel Mask %x size %zu", channelMask,
2184 channels.size());
2185 if (AUDIO_CHANNEL_IN_5POINT1 == channelMask) {
2186 if (!prop_ssr_enabled) {
2187 ALOGI("removing AUDIO_CHANNEL_IN_5POINT1 from"
2188 " input profile as SSR(surround sound record)"
2189 " is not supported on this chipset variant");
2190 channels.removeItemsAt(x, 1);
2191 ALOGV("Channel Mask size now %zu",
2192 channels.size());
2193 }
Mingming Yin38ea08c2015-10-05 15:24:04 -07002194 }
2195 }
2196 }
2197 }
2198 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302199#ifdef RECORD_PLAY_CONCURRENCY
2200 mIsInputRequestOnProgress = false;
2201#endif
2202
2203
2204#ifdef VOICE_CONCURRENCY
2205 mFallBackflag = getFallBackPath();
2206#endif
2207}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07002208}