blob: 022a3c0243b7606f6bc9e631dbbc07edb0bda78e [file] [log] [blame]
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001/*
Weiyin Jiangd211ef52016-02-16 15:55:11 +08002 * Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07003 * Not a contribution.
4 *
5 * Copyright (C) 2009 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Gao Jiedb057832015-11-12 08:51:22 +080018 *
19 * This file was modified by Dolby Laboratories, Inc. The portions of the
20 * code that are surrounded by "DOLBY..." are copyrighted and
21 * licensed separately, as follows:
22 *
23 * (C) 2015 Dolby Laboratories, Inc.
24 *
25 * Licensed under the Apache License, Version 2.0 (the "License");
26 * you may not use this file except in compliance with the License.
27 * You may obtain a copy of the License at
28 *
29 * http://www.apache.org/licenses/LICENSE-2.0
30 *
31 * Unless required by applicable law or agreed to in writing, software
32 * distributed under the License is distributed on an "AS IS" BASIS,
33 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34 * See the License for the specific language governing permissions and
35 * limitations under the License.
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070036 */
37
Sharad Sangle36781612015-05-28 16:15:16 +053038#define LOG_TAG "AudioPolicyManagerCustom"
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070039//#define LOG_NDEBUG 0
40
41//#define VERY_VERBOSE_LOGGING
42#ifdef VERY_VERBOSE_LOGGING
43#define ALOGVV ALOGV
44#else
45#define ALOGVV(a...) do { } while(0)
46#endif
Sharad Sangle36781612015-05-28 16:15:16 +053047
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070048// A device mask for all audio output devices that are considered "remote" when evaluating
49// active output devices in isStreamActiveRemotely()
50#define APM_AUDIO_OUT_DEVICE_REMOTE_ALL AUDIO_DEVICE_OUT_REMOTE_SUBMIX
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070051// A device mask for all audio input and output devices where matching inputs/outputs on device
52// type alone is not enough: the address must match too
53#define APM_AUDIO_DEVICE_MATCH_ADDRESS_ALL (AUDIO_DEVICE_IN_REMOTE_SUBMIX | \
54 AUDIO_DEVICE_OUT_REMOTE_SUBMIX)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070055
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070056#include <inttypes.h>
Mingming Yin0ae14ea2014-07-09 17:55:56 -070057#include <math.h>
Mingming Yin0670f162014-06-12 16:05:49 -070058
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070059#include <cutils/properties.h>
60#include <utils/Log.h>
61#include <hardware/audio.h>
62#include <hardware/audio_effect.h>
63#include <media/AudioParameter.h>
64#include <soundtrigger/SoundTrigger.h>
65#include "AudioPolicyManager.h"
Sharad Sangle36781612015-05-28 16:15:16 +053066#include <policy.h>
Gao Jiedb057832015-11-12 08:51:22 +080067#ifdef DOLBY_ENABLE
68#include "DolbyAudioPolicy_impl.h"
69#endif // DOLBY_END
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070070
71namespace android {
Sharad Sanglec5766ff2015-06-04 20:24:10 +053072#ifdef VOICE_CONCURRENCY
73audio_output_flags_t AudioPolicyManagerCustom::getFallBackPath()
74{
75 audio_output_flags_t flag = AUDIO_OUTPUT_FLAG_FAST;
76 char propValue[PROPERTY_VALUE_MAX];
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070077
Sharad Sanglec5766ff2015-06-04 20:24:10 +053078 if (property_get("voice.conc.fallbackpath", propValue, NULL)) {
79 if (!strncmp(propValue, "deep-buffer", 11)) {
80 flag = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
81 }
82 else if (!strncmp(propValue, "fast", 4)) {
83 flag = AUDIO_OUTPUT_FLAG_FAST;
84 }
85 else {
86 ALOGD("voice_conc:not a recognised path(%s) in prop voice.conc.fallbackpath",
87 propValue);
88 }
89 }
90 else {
91 ALOGD("voice_conc:prop voice.conc.fallbackpath not set");
92 }
93
94 ALOGD("voice_conc:picked up flag(0x%x) from prop voice.conc.fallbackpath",
95 flag);
96
97 return flag;
98}
99#endif /*VOICE_CONCURRENCY*/
Ashish Jaine4a22d52016-06-22 11:55:08 +0530100
101void AudioPolicyManagerCustom::moveGlobalEffect()
102{
103 audio_io_handle_t dstOutput = getOutputForEffect();
104 if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) {
105#ifdef DOLBY_ENABLE
106 status_t status =
107 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
108 mPrimaryOutput->mIoHandle,
109 dstOutput);
110 if (status == NO_ERROR) {
111 for (size_t i = 0; i < mEffects.size(); i++) {
112 sp<EffectDescriptor> desc = mEffects.valueAt(i);
113 if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX) {
114 // update the mIo member of EffectDescriptor
115 // for the global effect
116 ALOGV("%s updating mIo", __FUNCTION__);
117 desc->mIo = dstOutput;
118 }
119 }
120 } else {
121 ALOGW("%s moveEffects from %d to %d failed", __FUNCTION__,
122 mPrimaryOutput->mIoHandle, dstOutput);
123 }
124#else // DOLBY_END
125 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
126 mPrimaryOutput->mIoHandle, dstOutput);
127#endif
128 }
129}
130
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700131// ----------------------------------------------------------------------------
132// AudioPolicyInterface implementation
133// ----------------------------------------------------------------------------
Sharad Sangle36781612015-05-28 16:15:16 +0530134extern "C" AudioPolicyInterface* createAudioPolicyManager(
135 AudioPolicyClientInterface *clientInterface)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700136{
Sharad Sangle36781612015-05-28 16:15:16 +0530137 return new AudioPolicyManagerCustom(clientInterface);
138}
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700139
Sharad Sangle36781612015-05-28 16:15:16 +0530140extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface)
141{
142 delete interface;
143}
144
145status_t AudioPolicyManagerCustom::setDeviceConnectionStateInt(audio_devices_t device,
146 audio_policy_dev_state_t state,
147 const char *device_address,
148 const char *device_name)
149{
Ashish Jain40cab3a2016-03-23 11:14:14 +0530150 ALOGD("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
Sharad Sangle36781612015-05-28 16:15:16 +0530151 device, state, device_address, device_name);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700152
153 // connect/disconnect only 1 device at a time
154 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
155
Sharad Sangle36781612015-05-28 16:15:16 +0530156 sp<DeviceDescriptor> devDesc =
157 mHwModules.getDeviceDescriptor(device, device_address, device_name);
158
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700159 // handle output devices
160 if (audio_is_output_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700161 SortedVector <audio_io_handle_t> outputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700162
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700163 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700164
165 // save a copy of the opened output descriptors before any output is opened or closed
166 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
167 mPreviousOutputs = mOutputs;
168 switch (state)
169 {
170 // handle output device connection
Sharad Sangle36781612015-05-28 16:15:16 +0530171 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700172 if (index >= 0) {
Sharad Sangle4509cef2015-08-19 20:47:12 +0530173#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
174 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
175 if (!strncmp(device_address, "hdmi_spkr", 9)) {
176 mHdmiAudioDisabled = false;
177 } else {
178 mHdmiAudioEvent = true;
179 }
180 }
181#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700182 ALOGW("setDeviceConnectionState() device already connected: %x", device);
183 return INVALID_OPERATION;
184 }
185 ALOGV("setDeviceConnectionState() connecting device %x", device);
186
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700187 // register new device as available
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700188 index = mAvailableOutputDevices.add(devDesc);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530189#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
190 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
191 if (!strncmp(device_address, "hdmi_spkr", 9)) {
192 mHdmiAudioDisabled = false;
193 } else {
194 mHdmiAudioEvent = true;
195 }
196 if (mHdmiAudioDisabled || !mHdmiAudioEvent) {
197 mAvailableOutputDevices.remove(devDesc);
198 ALOGW("HDMI sink not connected, do not route audio to HDMI out");
199 return INVALID_OPERATION;
200 }
201 }
202#endif
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700203 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530204 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700205 if (module == 0) {
206 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
207 device);
208 mAvailableOutputDevices.remove(devDesc);
209 return INVALID_OPERATION;
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700210 }
Sharad Sangle36781612015-05-28 16:15:16 +0530211 mAvailableOutputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700212 } else {
213 return NO_MEMORY;
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700214 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700215
Sharad Sangle36781612015-05-28 16:15:16 +0530216 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700217 mAvailableOutputDevices.remove(devDesc);
218 return INVALID_OPERATION;
219 }
Sharad Sangle36781612015-05-28 16:15:16 +0530220 // Propagate device availability to Engine
221 mEngine->setDeviceConnectionState(devDesc, state);
222
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700223 // outputs should never be empty here
224 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
225 "checkOutputsForDevice() returned no outputs but status OK");
226 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
227 outputs.size());
Sharad Sangle36781612015-05-28 16:15:16 +0530228
229 // Send connect to HALs
230 AudioParameter param = AudioParameter(devDesc->mAddress);
231 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
232 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
233
234 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700235 // handle output device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700236 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
237 if (index < 0) {
Sharad Sangle4509cef2015-08-19 20:47:12 +0530238#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
239 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
240 if (!strncmp(device_address, "hdmi_spkr", 9)) {
241 mHdmiAudioDisabled = true;
242 } else {
243 mHdmiAudioEvent = false;
244 }
245 }
246#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700247 ALOGW("setDeviceConnectionState() device not connected: %x", device);
248 return INVALID_OPERATION;
249 }
250
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700251 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
252
Sharad Sangle36781612015-05-28 16:15:16 +0530253 // Send Disconnect to HALs
254 AudioParameter param = AudioParameter(devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700255 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
256 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
257
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700258 // remove device from available output devices
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700259 mAvailableOutputDevices.remove(devDesc);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530260#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
261 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
262 if (!strncmp(device_address, "hdmi_spkr", 9)) {
263 mHdmiAudioDisabled = true;
264 } else {
265 mHdmiAudioEvent = false;
266 }
267 }
268#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530269 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
270
271 // Propagate device availability to Engine
272 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700273 } break;
274
275 default:
276 ALOGE("setDeviceConnectionState() invalid state: %x", state);
277 return BAD_VALUE;
278 }
279
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700280 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
281 // output is suspended before any tracks are moved to it
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700282 checkA2dpSuspend();
283 checkOutputForAllStrategies();
284 // outputs must be closed after checkOutputForAllStrategies() is executed
285 if (!outputs.isEmpty()) {
286 for (size_t i = 0; i < outputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530287 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700288 // close unused outputs after device disconnection or direct outputs that have been
289 // opened by checkOutputsForDevice() to query dynamic parameters
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700290 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700291 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
292 (desc->mDirectOpenCount == 0))) {
293 closeOutput(outputs[i]);
294 }
295 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700296 // check again after closing A2DP output to reset mA2dpSuspended if needed
297 checkA2dpSuspend();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700298 }
299
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530300#ifdef FM_POWER_OPT
301 // handle FM device connection state to trigger FM AFE loopback
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800302 if (device == AUDIO_DEVICE_OUT_FM && hasPrimaryOutput()) {
Mingming Yin2dd77c22016-01-06 18:02:42 -0800303 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530304 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
305 mPrimaryOutput->changeRefCount(AUDIO_STREAM_MUSIC, 1);
Mingming Yin2dd77c22016-01-06 18:02:42 -0800306 newDevice = (audio_devices_t)(getNewOutputDevice(mPrimaryOutput, false)|AUDIO_DEVICE_OUT_FM);
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800307 mFMIsActive = true;
Zhou Songbd0264e2016-06-21 11:01:06 +0800308 mPrimaryOutput->mDevice = newDevice & ~AUDIO_DEVICE_OUT_FM;
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530309 } else {
Mingming Yin2dd77c22016-01-06 18:02:42 -0800310 newDevice = (audio_devices_t)(getNewOutputDevice(mPrimaryOutput, false));
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800311 mFMIsActive = false;
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530312 mPrimaryOutput->changeRefCount(AUDIO_STREAM_MUSIC, -1);
313 }
314 AudioParameter param = AudioParameter();
315 param.addInt(String8("handle_fm"), (int)newDevice);
316 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString());
317 }
318#endif /* FM_POWER_OPT end */
319
Zhou Songbd0264e2016-06-21 11:01:06 +0800320 updateDevicesAndOutputs();
321#ifdef DOLBY_ENABLE
322 // Before closing the opened outputs, update endpoint property with device capabilities
323 audio_devices_t audioOutputDevice = getDeviceForStrategy(getStrategy(AUDIO_STREAM_MUSIC), true);
324 mDolbyAudioPolicy.setEndpointSystemProperty(audioOutputDevice, mHwModules);
325#endif // DOLBY_END
326 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
327 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
328 updateCallRouting(newDevice);
329 }
330
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700331 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530332 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
333 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
334 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700335 // do not force device change on duplicated output because if device is 0, it will
336 // also force a device 0 for the two outputs it is duplicated to which may override
337 // a valid device selection on those outputs.
Sharad Sangle36781612015-05-28 16:15:16 +0530338 bool force = !desc->isDuplicated()
339 && (!device_distinguishes_on_address(device)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700340 // always force when disconnecting (a non-duplicated device)
341 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Sharad Sangle36781612015-05-28 16:15:16 +0530342 setOutputDevice(desc, newDevice, force, 0);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700343 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700344 }
345
Ashish Jain40cab3a2016-03-23 11:14:14 +0530346 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
347 cleanUpForDevice(devDesc);
348 }
349
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700350 mpClientInterface->onAudioPortListUpdate();
351 return NO_ERROR;
352 } // end if is output device
353
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700354 // handle input devices
355 if (audio_is_input_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700356 SortedVector <audio_io_handle_t> inputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700357
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700358 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700359 switch (state)
360 {
361 // handle input device connection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700362 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
363 if (index >= 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700364 ALOGW("setDeviceConnectionState() device already connected: %d", device);
365 return INVALID_OPERATION;
366 }
Sharad Sangle36781612015-05-28 16:15:16 +0530367 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700368 if (module == NULL) {
369 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
370 device);
371 return INVALID_OPERATION;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700372 }
Sharad Sangle36781612015-05-28 16:15:16 +0530373 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700374 return INVALID_OPERATION;
375 }
376
377 index = mAvailableInputDevices.add(devDesc);
378 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530379 mAvailableInputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700380 } else {
381 return NO_MEMORY;
382 }
Sharad Sangle36781612015-05-28 16:15:16 +0530383
384 // Set connect to HALs
385 AudioParameter param = AudioParameter(devDesc->mAddress);
386 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
387 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
388
389 // Propagate device availability to Engine
390 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700391 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700392
393 // handle input device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700394 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
395 if (index < 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700396 ALOGW("setDeviceConnectionState() device not connected: %d", device);
397 return INVALID_OPERATION;
398 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700399
400 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
401
402 // Set Disconnect to HALs
Sharad Sangle36781612015-05-28 16:15:16 +0530403 AudioParameter param = AudioParameter(devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700404 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
405 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
406
Sharad Sangle36781612015-05-28 16:15:16 +0530407 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700408 mAvailableInputDevices.remove(devDesc);
409
Sharad Sangle36781612015-05-28 16:15:16 +0530410 // Propagate device availability to Engine
411 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700412 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700413
414 default:
415 ALOGE("setDeviceConnectionState() invalid state: %x", state);
416 return BAD_VALUE;
417 }
418
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700419 closeAllInputs();
420
Sharad Sangle36781612015-05-28 16:15:16 +0530421 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700422 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
423 updateCallRouting(newDevice);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700424 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700425
Ashish Jain40cab3a2016-03-23 11:14:14 +0530426 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
427 cleanUpForDevice(devDesc);
428 }
429
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700430 mpClientInterface->onAudioPortListUpdate();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700431 return NO_ERROR;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700432 } // end if is input device
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700433
434 ALOGW("setDeviceConnectionState() invalid device: %x", device);
435 return BAD_VALUE;
436}
Weiyin Jiangd211ef52016-02-16 15:55:11 +0800437
Sharad Sangle36781612015-05-28 16:15:16 +0530438// This function checks for the parameters which can be offloaded.
439// This can be enhanced depending on the capability of the DSP and policy
440// of the system.
441bool AudioPolicyManagerCustom::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700442{
Sharad Sangle36781612015-05-28 16:15:16 +0530443 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
444 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
445 offloadInfo.sample_rate, offloadInfo.channel_mask,
446 offloadInfo.format,
447 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
448 offloadInfo.has_video);
Ashish Jain40cab3a2016-03-23 11:14:14 +0530449
450 if (mMasterMono) {
451 return false; // no offloading if mono is set.
452 }
453
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530454#ifdef VOICE_CONCURRENCY
455 char concpropValue[PROPERTY_VALUE_MAX];
456 if (property_get("voice.playback.conc.disabled", concpropValue, NULL)) {
457 bool propenabled = atoi(concpropValue) || !strncmp("true", concpropValue, 4);
458 if (propenabled) {
459 if (isInCall())
460 {
461 ALOGD("\n copl: blocking compress offload on call mode\n");
462 return false;
463 }
464 }
465 }
466#endif
Preetam Singh Ranawat96afc162016-08-25 14:35:02 +0530467 if (property_get_bool("voice.dsd.playback.conc.disabled", true) &&
468 isInCall() && (offloadInfo.format == AUDIO_FORMAT_DSD)) {
469 ALOGD("blocking DSD compress offload on call mode");
470 return false;
471 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530472#ifdef RECORD_PLAY_CONCURRENCY
473 char recConcPropValue[PROPERTY_VALUE_MAX];
474 bool prop_rec_play_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700475
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530476 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
477 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
478 }
479
480 if ((prop_rec_play_enabled) &&
481 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
482 ALOGD("copl: blocking compress offload for record concurrency");
483 return false;
484 }
485#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530486 // Check if stream type is music, then only allow offload as of now.
487 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
488 {
489 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
490 return false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700491 }
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530492
Alexy Joseph0ac09032015-10-16 15:57:53 -0700493 // Check if offload has been disabled
494 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
495 if (offloadDisabled) {
496 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
497 return false;
498 }
499
Ashish Jainac510a72016-04-28 12:22:58 +0530500 //check if it's multi-channel AAC (includes sub formats) and FLAC format
501 if ((popcount(offloadInfo.channel_mask) > 2) &&
502 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
503 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS))) {
504 ALOGD("offload disabled for multi-channel AAC,FLAC and VORBIS format");
505 return false;
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530506 }
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530507
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530508#ifdef AUDIO_EXTN_FORMATS_ENABLED
Ashish Jainac510a72016-04-28 12:22:58 +0530509 //check if it's multi-channel FLAC/ALAC/WMA format with sample rate > 48k
510 if ((popcount(offloadInfo.channel_mask) > 2) &&
511 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
512 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) && (offloadInfo.sample_rate > 48000)) ||
513 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) && (offloadInfo.sample_rate > 48000)) ||
514 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) && (offloadInfo.sample_rate > 48000)) ||
515 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS))) {
516 ALOGD("offload disabled for multi-channel FLAC/ALAC/WMA/AAC_ADTS clips with sample rate > 48kHz");
517 return false;
518 }
yidonghae28e3a2016-08-19 11:31:15 +0800519
520 if ((((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) && (offloadInfo.bit_rate > MAX_BITRATE_WMA)) ||
521 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) && (offloadInfo.bit_rate > MAX_BITRATE_WMA_PRO)) ||
522 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) && (offloadInfo.bit_rate > MAX_BITRATE_WMA_LOSSLESS))){
523 //Safely choose the min bitrate as threshold and leave the restriction to NT decoder as we can't distinguish wma pro and wma lossless here.
524 ALOGD("offload disabled for WMA/WMA_PRO/WMA_LOSSLESS clips with bit rate over maximum supported value");
525 return false;
526 }
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530527#endif
Ashish Jainac510a72016-04-28 12:22:58 +0530528 //TODO: enable audio offloading with video when ready
529 const bool allowOffloadWithVideo =
530 property_get_bool("audio.offload.video", false /* default_value */);
531 if (offloadInfo.has_video && !allowOffloadWithVideo) {
532 ALOGV("isOffloadSupported: has_video == true, returning false");
533 return false;
534 }
Manish Dewanganf3cd0f82015-10-13 14:04:36 +0530535
Ashish Jainac510a72016-04-28 12:22:58 +0530536 const bool allowOffloadStreamingWithVideo = property_get_bool("av.streaming.offload.enable",
537 false /*default value*/);
538 if (offloadInfo.has_video && offloadInfo.is_streaming && !allowOffloadStreamingWithVideo) {
539 ALOGW("offload disabled by av.streaming.offload.enable %d",allowOffloadStreamingWithVideo);
540 return false;
Sharad Sangle36781612015-05-28 16:15:16 +0530541 }
542
543 //If duration is less than minimum value defined in property, return false
Ashish Jainac510a72016-04-28 12:22:58 +0530544 char propValue[PROPERTY_VALUE_MAX];
Sharad Sangle36781612015-05-28 16:15:16 +0530545 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
546 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
547 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
548 return false;
549 }
550 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
551 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
552 //duration checks only valid for MP3/AAC/ formats,
553 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
554 if ((offloadInfo.format == AUDIO_FORMAT_MP3) ||
555 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
556 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
Ashish Jainac510a72016-04-28 12:22:58 +0530557 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS))
558 return false;
559
560#ifdef AUDIO_EXTN_FORMATS_ENABLED
561 if (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) ||
Sharad Sangle36781612015-05-28 16:15:16 +0530562 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) ||
563 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) ||
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530564 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_APE) ||
Preetam Singh Ranawat191eb772016-09-09 17:10:19 +0530565 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_DSD) ||
Ashish Jainac510a72016-04-28 12:22:58 +0530566 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS))
Sharad Sangle36781612015-05-28 16:15:16 +0530567 return false;
Ashish Jainac510a72016-04-28 12:22:58 +0530568#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530569 }
570
571 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
572 // creating an offloaded track and tearing it down immediately after start when audioflinger
573 // detects there is an active non offloadable effect.
574 // FIXME: We should check the audio session here but we do not have it in this context.
575 // This may prevent offloading in rare situations where effects are left active by apps
576 // in the background.
577 if (mEffects.isNonOffloadableEffectEnabled()) {
578 return false;
579 }
Dhananjay Kumarcf558ba2016-07-29 19:33:13 +0530580
Sharad Sangle36781612015-05-28 16:15:16 +0530581 // See if there is a profile to support this.
582 // AUDIO_DEVICE_NONE
583 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
584 offloadInfo.sample_rate,
585 offloadInfo.format,
586 offloadInfo.channel_mask,
587 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
588 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
589 return (profile != 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700590}
Weiyin Jiangd211ef52016-02-16 15:55:11 +0800591
Sharad Sangle36781612015-05-28 16:15:16 +0530592audio_devices_t AudioPolicyManagerCustom::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
593 bool fromCache)
594{
595 audio_devices_t device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700596
Ashish Jain40cab3a2016-03-23 11:14:14 +0530597 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Sharad Sangle36781612015-05-28 16:15:16 +0530598 if (index >= 0) {
599 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
600 if (patchDesc->mUid != mUidCached) {
601 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Ashish Jain40cab3a2016-03-23 11:14:14 +0530602 outputDesc->device(), outputDesc->getPatchHandle());
Sharad Sangle36781612015-05-28 16:15:16 +0530603 return outputDesc->device();
604 }
605 }
606
607 // check the following by order of priority to request a routing change if necessary:
608 // 1: the strategy enforced audible is active and enforced on the output:
609 // use device for strategy enforced audible
610 // 2: we are in call or the strategy phone is active on the output:
611 // use device for strategy phone
612 // 3: the strategy for enforced audible is active but not enforced on the output:
613 // use the device for strategy enforced audible
614 // 4: the strategy sonification is active on the output:
615 // use device for strategy sonification
616 // 5: the strategy "respectful" sonification is active on the output:
617 // use device for strategy "respectful" sonification
618 // 6: the strategy accessibility is active on the output:
619 // use device for strategy accessibility
620 // 7: the strategy media is active on the output:
621 // use device for strategy media
622 // 8: the strategy DTMF is active on the output:
623 // use device for strategy DTMF
624 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
625 // use device for strategy t-t-s
626 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
627 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
628 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
629 } else if (isInCall() ||
630 isStrategyActive(outputDesc, STRATEGY_PHONE)||
631 isStrategyActive(mPrimaryOutput, STRATEGY_PHONE)) {
632 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
633 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
634 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
635 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)||
636 (isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION)
637 && (!isStrategyActive(mPrimaryOutput,STRATEGY_MEDIA)))) {
638 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530639 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL) ||
640 isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION_RESPECTFUL)) {
Sharad Sangle36781612015-05-28 16:15:16 +0530641 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
642 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
643 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
644 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
645 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
646 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
647 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
648 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
649 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
650 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
651 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
652 }
653
654 ALOGV("getNewOutputDevice() selected device %x", device);
655 return device;
656}
Weiyin Jiangd211ef52016-02-16 15:55:11 +0800657
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700658void AudioPolicyManagerCustom::setPhoneState(audio_mode_t state)
659{
Ashish Jain40cab3a2016-03-23 11:14:14 +0530660 ALOGD("setPhoneState() state %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530661 // store previous phone state for management of sonification strategy below
Sharad Sangle4509cef2015-08-19 20:47:12 +0530662 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Sharad Sangle36781612015-05-28 16:15:16 +0530663 int oldState = mEngine->getPhoneState();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700664
Sharad Sangle36781612015-05-28 16:15:16 +0530665 if (mEngine->setPhoneState(state) != NO_ERROR) {
666 ALOGW("setPhoneState() invalid or same state %d", state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700667 return;
668 }
Sharad Sangle36781612015-05-28 16:15:16 +0530669 /// Opens: can these line be executed after the switch of volume curves???
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700670 // if leaving call state, handle special case of active streams
671 // pertaining to sonification strategy see handleIncallSonification()
Zhou Song58081b62016-02-24 13:10:55 +0800672 if (isStateInCall(oldState)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700673 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530674 for (size_t j = 0; j < mOutputs.size(); j++) {
675 audio_io_handle_t curOutput = mOutputs.keyAt(j);
676 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
677 if (stream == AUDIO_STREAM_PATCH) {
678 continue;
679 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530680 handleIncallSonification((audio_stream_type_t)stream, false, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +0530681 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700682 }
Sharad Sangle36781612015-05-28 16:15:16 +0530683
Zhou Song58081b62016-02-24 13:10:55 +0800684 // force reevaluating accessibility routing when call stops
Sharad Sangle36781612015-05-28 16:15:16 +0530685 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700686 }
687
Sharad Sangle36781612015-05-28 16:15:16 +0530688 /**
689 * Switching to or from incall state or switching between telephony and VoIP lead to force
690 * routing command.
691 */
692 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
693 || (is_state_in_call(state) && (state != oldState)));
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700694
695 // check for device and output changes triggered by new phone state
696 checkA2dpSuspend();
697 checkOutputForAllStrategies();
698 updateDevicesAndOutputs();
699
Sharad Sangle36781612015-05-28 16:15:16 +0530700 sp<SwAudioOutputDescriptor> hwOutputDesc = mPrimaryOutput;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530701#ifdef VOICE_CONCURRENCY
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530702 char propValue[PROPERTY_VALUE_MAX];
703 bool prop_playback_enabled = false, prop_rec_enabled=false, prop_voip_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700704
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530705 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
706 prop_playback_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
707 }
708
709 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
710 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
711 }
712
713 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
714 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
715 }
716
Dhananjay Kumar9125b2a2015-12-01 16:24:09 +0530717 if ((AUDIO_MODE_IN_CALL != oldState) && (AUDIO_MODE_IN_CALL == state)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530718 ALOGD("voice_conc:Entering to call mode oldState :: %d state::%d ",
719 oldState, state);
Dhananjay Kumar9125b2a2015-12-01 16:24:09 +0530720 mvoice_call_state = state;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530721 if (prop_rec_enabled) {
722 //Close all active inputs
723 audio_io_handle_t activeInput = mInputs.getActiveInput();
724 if (activeInput != 0) {
725 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
726 switch(activeDesc->mInputSource) {
727 case AUDIO_SOURCE_VOICE_UPLINK:
728 case AUDIO_SOURCE_VOICE_DOWNLINK:
729 case AUDIO_SOURCE_VOICE_CALL:
730 ALOGD("voice_conc:FOUND active input during call active: %d",activeDesc->mInputSource);
731 break;
732
733 case AUDIO_SOURCE_VOICE_COMMUNICATION:
734 if(prop_voip_enabled) {
735 ALOGD("voice_conc:CLOSING VoIP input source on call setup :%d ",activeDesc->mInputSource);
736 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
737 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
738 }
739 break;
740
741 default:
742 ALOGD("voice_conc:CLOSING input on call setup for inputSource: %d",activeDesc->mInputSource);
743 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
744 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
745 break;
746 }
747 }
748 } else if (prop_voip_enabled) {
749 audio_io_handle_t activeInput = mInputs.getActiveInput();
750 if (activeInput != 0) {
751 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
752 if (AUDIO_SOURCE_VOICE_COMMUNICATION == activeDesc->mInputSource) {
753 ALOGD("voice_conc:CLOSING VoIP on call setup : %d",activeDesc->mInputSource);
754 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
755 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
756 }
757 }
758 }
759 if (prop_playback_enabled) {
760 // Move tracks associated to this strategy from previous output to new output
761 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
762 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
763 if (i == AUDIO_STREAM_PATCH) {
764 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
765 continue;
766 }
767 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
768 if ((AUDIO_STREAM_MUSIC == i) ||
769 (AUDIO_STREAM_VOICE_CALL == i) ) {
770 ALOGD("voice_conc:Invalidate stream type %d", i);
771 mpClientInterface->invalidateStream((audio_stream_type_t)i);
772 }
773 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
774 ALOGD("voice_conc:Invalidate stream type %d", i);
775 mpClientInterface->invalidateStream((audio_stream_type_t)i);
776 }
777 }
778 }
779
780 for (size_t i = 0; i < mOutputs.size(); i++) {
781 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
782 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
783 ALOGD("voice_conc:ouput desc / profile is NULL");
784 continue;
785 }
786
787 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
788 if (((!outputDesc->isDuplicated() &&outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY))
789 && prop_playback_enabled) {
790 ALOGD("voice_conc:calling suspendOutput on call mode for primary output");
791 mpClientInterface->suspendOutput(mOutputs.keyAt(i));
792 } //Close compress all sessions
793 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
794 && prop_playback_enabled) {
795 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
796 closeOutput(mOutputs.keyAt(i));
797 }
798 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_VOIP_RX)
799 && prop_voip_enabled) {
800 ALOGD("voice_conc:calling closeOutput on call mode for DIRECT output");
801 closeOutput(mOutputs.keyAt(i));
802 }
803 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
804 if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)
805 && prop_playback_enabled) {
806 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
807 closeOutput(mOutputs.keyAt(i));
808 }
809 }
810 }
Ashish Jaine4a22d52016-06-22 11:55:08 +0530811 // If effects where present on any of the above closed outputs,
812 // audioflinger moved them to the primary output by default
813 // move them back to the appropriate output.
814 moveGlobalEffect();
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530815 }
816
817 if ((AUDIO_MODE_IN_CALL == oldState || AUDIO_MODE_IN_COMMUNICATION == oldState) &&
818 (AUDIO_MODE_NORMAL == state) && prop_playback_enabled && mvoice_call_state) {
819 ALOGD("voice_conc:EXITING from call mode oldState :: %d state::%d \n",oldState, state);
820 mvoice_call_state = 0;
821 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
822 //restore PCM (deep-buffer) output after call termination
823 for (size_t i = 0; i < mOutputs.size(); i++) {
824 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
825 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
826 ALOGD("voice_conc:ouput desc / profile is NULL");
827 continue;
828 }
829 if (!outputDesc->isDuplicated() && outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
830 ALOGD("voice_conc:calling restoreOutput after call mode for primary output");
831 mpClientInterface->restoreOutput(mOutputs.keyAt(i));
832 }
833 }
834 }
835 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
836 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
837 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
838 if (i == AUDIO_STREAM_PATCH) {
839 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
840 continue;
841 }
842 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
843 if ((AUDIO_STREAM_MUSIC == i) ||
844 (AUDIO_STREAM_VOICE_CALL == i) ) {
845 mpClientInterface->invalidateStream((audio_stream_type_t)i);
846 }
847 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
848 mpClientInterface->invalidateStream((audio_stream_type_t)i);
849 }
850 }
851 }
852
853#endif
Preetam Singh Ranawat96afc162016-08-25 14:35:02 +0530854
855 sp<SwAudioOutputDescriptor> outputDesc = NULL;
856 for (size_t i = 0; i < mOutputs.size(); i++) {
857 outputDesc = mOutputs.valueAt(i);
858 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
859 ALOGD("voice_conc:ouput desc / profile is NULL");
860 continue;
861 }
862
863 if (property_get_bool("voice.dsd.playback.conc.disabled", true) &&
864 (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
865 (outputDesc->mFormat == AUDIO_FORMAT_DSD)) {
866 ALOGD("voice_conc:calling closeOutput on call mode for DSD COMPRESS output");
867 closeOutput(mOutputs.keyAt(i));
868 // call invalidate for music, so that DSD compress will fallback to deep-buffer.
869 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
870 }
871
872 }
873
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530874#ifdef RECORD_PLAY_CONCURRENCY
875 char recConcPropValue[PROPERTY_VALUE_MAX];
876 bool prop_rec_play_enabled = false;
877
878 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
879 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
880 }
881 if (prop_rec_play_enabled) {
882 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
883 ALOGD("phone state changed to MODE_IN_COMM invlaidating music and voice streams");
884 // call invalidate for voice streams, so that it can use deepbuffer with VoIP out device from HAL
885 mpClientInterface->invalidateStream(AUDIO_STREAM_VOICE_CALL);
886 // call invalidate for music, so that compress will fallback to deep-buffer with VoIP out device
887 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
888
889 // close compress output to make sure session will be closed before timeout(60sec)
890 for (size_t i = 0; i < mOutputs.size(); i++) {
891
892 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
893 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
894 ALOGD("ouput desc / profile is NULL");
895 continue;
896 }
897
898 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
899 ALOGD("calling closeOutput on call mode for COMPRESS output");
900 closeOutput(mOutputs.keyAt(i));
901 }
902 }
Ashish Jaine4a22d52016-06-22 11:55:08 +0530903 // If effects where present on any of the above closed outputs,
904 // audioflinger moved them to the primary output by default
905 // move them back to the appropriate output.
906 moveGlobalEffect();
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530907 } else if ((oldState == AUDIO_MODE_IN_COMMUNICATION) &&
908 (mEngine->getPhoneState() == AUDIO_MODE_NORMAL)) {
909 // call invalidate for music so that music can fallback to compress
910 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
911 }
912 }
913#endif
914 mPrevPhoneState = oldState;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700915 int delayMs = 0;
916 if (isStateInCall(state)) {
917 nsecs_t sysTime = systemTime();
918 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530919 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700920 // mute media and sonification strategies and delay device switch by the largest
921 // latency of any output where either strategy is active.
922 // This avoid sending the ring tone or music tail into the earpiece or headset.
Sharad Sangle36781612015-05-28 16:15:16 +0530923 if ((isStrategyActive(desc, STRATEGY_MEDIA,
924 SONIFICATION_HEADSET_MUSIC_DELAY,
925 sysTime) ||
926 isStrategyActive(desc, STRATEGY_SONIFICATION,
927 SONIFICATION_HEADSET_MUSIC_DELAY,
928 sysTime)) &&
929 (delayMs < (int)desc->latency()*2)) {
930 delayMs = desc->latency()*2;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700931 }
Sharad Sangle36781612015-05-28 16:15:16 +0530932 setStrategyMute(STRATEGY_MEDIA, true, desc);
933 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700934 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Sharad Sangle36781612015-05-28 16:15:16 +0530935 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
936 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700937 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
938 }
939 }
940
Sharad Sangle36781612015-05-28 16:15:16 +0530941 if (hasPrimaryOutput()) {
942 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
943 // the device returned is not necessarily reachable via this output
944 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
945 // force routing command to audio hardware when ending call
946 // even if no device change is needed
947 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
948 rxDevice = mPrimaryOutput->device();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700949 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700950
Sharad Sangle36781612015-05-28 16:15:16 +0530951 if (state == AUDIO_MODE_IN_CALL) {
952 updateCallRouting(rxDevice, delayMs);
953 } else if (oldState == AUDIO_MODE_IN_CALL) {
954 if (mCallRxPatch != 0) {
955 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
956 mCallRxPatch.clear();
957 }
958 if (mCallTxPatch != 0) {
959 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
960 mCallTxPatch.clear();
961 }
962 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
963 } else {
964 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700965 }
966 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530967 //update device for all non-primary outputs
968 for (size_t i = 0; i < mOutputs.size(); i++) {
969 audio_io_handle_t output = mOutputs.keyAt(i);
970 if (output != mPrimaryOutput->mIoHandle) {
971 newDevice = getNewOutputDevice(mOutputs.valueFor(output), false /*fromCache*/);
972 setOutputDevice(mOutputs.valueFor(output), newDevice, (newDevice != AUDIO_DEVICE_NONE));
973 }
974 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700975 // if entering in call state, handle special case of active streams
976 // pertaining to sonification strategy see handleIncallSonification()
977 if (isStateInCall(state)) {
978 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530979 for (size_t j = 0; j < mOutputs.size(); j++) {
980 audio_io_handle_t curOutput = mOutputs.keyAt(j);
981 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
982 if (stream == AUDIO_STREAM_PATCH) {
983 continue;
984 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530985 handleIncallSonification((audio_stream_type_t)stream, true, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +0530986 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700987 }
Zhou Song58081b62016-02-24 13:10:55 +0800988
989 // force reevaluating accessibility routing when call starts
990 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700991 }
992
993 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
994 if (state == AUDIO_MODE_RINGTONE &&
995 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
996 mLimitRingtoneVolume = true;
997 } else {
998 mLimitRingtoneVolume = false;
999 }
1000}
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301001
1002void AudioPolicyManagerCustom::setForceUse(audio_policy_force_use_t usage,
1003 audio_policy_forced_cfg_t config)
1004{
Ashish Jain40cab3a2016-03-23 11:14:14 +05301005 ALOGD("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301006
1007 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
1008 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
1009 return;
1010 }
1011 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
1012 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
1013 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
1014
1015 // check for device and output changes triggered by new force usage
1016 checkA2dpSuspend();
1017 checkOutputForAllStrategies();
1018 updateDevicesAndOutputs();
Ashish Jain40cab3a2016-03-23 11:14:14 +05301019
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301020 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
1021 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
1022 updateCallRouting(newDevice);
1023 }
1024 // Use reverse loop to make sure any low latency usecases (generally tones)
1025 // are not routed before non LL usecases (generally music).
1026 // We can safely assume that LL output would always have lower index,
1027 // and use this work-around to avoid routing of output with music stream
1028 // from the context of short lived LL output.
1029 // Note: in case output's share backend(HAL sharing is implicit) all outputs
1030 // gets routing update while processing first output itself.
1031 for (size_t i = mOutputs.size(); i > 0; i--) {
1032 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i-1);
1033 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
1034 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || outputDesc != mPrimaryOutput) {
1035 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
1036 }
1037 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
1038 applyStreamVolumes(outputDesc, newDevice, 0, true);
1039 }
1040 }
1041
1042 audio_io_handle_t activeInput = mInputs.getActiveInput();
1043 if (activeInput != 0) {
Ashish Jain40cab3a2016-03-23 11:14:14 +05301044 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
1045 audio_devices_t newDevice = getNewInputDevice(activeInput);
1046 // Force new input selection if the new device can not be reached via current input
1047 if (activeDesc->mProfile->getSupportedDevices().types() & (newDevice & ~AUDIO_DEVICE_BIT_IN)) {
1048 setInputDevice(activeInput, newDevice);
1049 } else {
1050 closeInput(activeInput);
1051 }
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301052 }
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301053}
1054
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301055status_t AudioPolicyManagerCustom::stopSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301056 audio_stream_type_t stream,
1057 bool forceDeviceUpdate)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001058{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301059 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1060 ALOGW("stopSource() invalid stream %d", stream);
1061 return INVALID_OPERATION;
1062 }
Sharad Sangle36781612015-05-28 16:15:16 +05301063 // always handle stream stop, check which stream type is stopping
1064 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001065
Sharad Sangle36781612015-05-28 16:15:16 +05301066 // handle special case for sonification while in call
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001067 if (isInCall()) {
Sharad Sangle36781612015-05-28 16:15:16 +05301068 if (outputDesc->isDuplicated()) {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301069 handleIncallSonification(stream, false, false, outputDesc->subOutput1()->mIoHandle);
1070 handleIncallSonification(stream, false, false, outputDesc->subOutput2()->mIoHandle);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001071 }
Sharad Sangle36781612015-05-28 16:15:16 +05301072 handleIncallSonification(stream, false, false, outputDesc->mIoHandle);
1073 }
1074
1075 if (outputDesc->mRefCount[stream] > 0) {
1076 // decrement usage count of this stream on the output
1077 outputDesc->changeRefCount(stream, -1);
1078
1079 // store time at which the stream was stopped - see isStreamActive()
1080 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
1081 outputDesc->mStopTime[stream] = systemTime();
Zhou Song5dcddc92015-09-21 14:36:57 +08001082 audio_devices_t prevDevice = outputDesc->device();
Sharad Sangle36781612015-05-28 16:15:16 +05301083 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
1084 // delay the device switch by twice the latency because stopOutput() is executed when
1085 // the track stop() command is received and at that time the audio track buffer can
1086 // still contain data that needs to be drained. The latency only covers the audio HAL
1087 // and kernel buffers. Also the latency does not always include additional delay in the
1088 // audio path (audio DSP, CODEC ...)
1089 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
1090
1091 // force restoring the device selection on other active outputs if it differs from the
1092 // one being selected for this output
1093 for (size_t i = 0; i < mOutputs.size(); i++) {
1094 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1095 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1096 if (desc != outputDesc &&
1097 desc->isActive() &&
1098 outputDesc->sharesHwModuleWith(desc) &&
1099 (newDevice != desc->device())) {
Sharad Sangle4509cef2015-08-19 20:47:12 +05301100 audio_devices_t dev = getNewOutputDevice(mOutputs.valueFor(curOutput), false /*fromCache*/);
Naresh Tanniru11393702016-09-14 11:25:37 +05301101 bool force = desc->device() != dev;
Zhou Song5dcddc92015-09-21 14:36:57 +08001102 uint32_t delayMs;
1103 if (dev == prevDevice) {
1104 delayMs = 0;
1105 } else {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301106 delayMs = outputDesc->latency()*2;
Zhou Song5dcddc92015-09-21 14:36:57 +08001107 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301108 setOutputDevice(desc,
1109 dev,
Naresh Tanniru11393702016-09-14 11:25:37 +05301110 force,
Zhou Song5dcddc92015-09-21 14:36:57 +08001111 delayMs);
Sharad Sangle36781612015-05-28 16:15:16 +05301112 }
1113 }
1114 // update the outputs if stopping one with a stream that can affect notification routing
1115 handleNotificationRoutingForStream(stream);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001116 }
Sharad Sangle36781612015-05-28 16:15:16 +05301117 return NO_ERROR;
1118 } else {
1119 ALOGW("stopOutput() refcount is already 0");
1120 return INVALID_OPERATION;
1121 }
1122}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001123
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301124status_t AudioPolicyManagerCustom::startSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301125 audio_stream_type_t stream,
1126 audio_devices_t device,
Lalit Kansaraf803ac52016-05-09 19:12:01 +05301127 const char *address,
Sharad Sangle36781612015-05-28 16:15:16 +05301128 uint32_t *delayMs)
1129{
1130 // cannot start playback of STREAM_TTS if any other output is being used
1131 uint32_t beaconMuteLatency = 0;
1132
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301133 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1134 ALOGW("startSource() invalid stream %d", stream);
1135 return INVALID_OPERATION;
1136 }
1137
Sharad Sangle36781612015-05-28 16:15:16 +05301138 *delayMs = 0;
1139 if (stream == AUDIO_STREAM_TTS) {
1140 ALOGV("\t found BEACON stream");
Ashish Jain40cab3a2016-03-23 11:14:14 +05301141 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301142 return INVALID_OPERATION;
1143 } else {
1144 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001145 }
Sharad Sangle36781612015-05-28 16:15:16 +05301146 } else {
1147 // some playback other than beacon starts
1148 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1149 }
1150
Ashish Jain40cab3a2016-03-23 11:14:14 +05301151 // check active before incrementing usage count
1152 bool force = !outputDesc->isActive();
1153
Sharad Sangle36781612015-05-28 16:15:16 +05301154 // increment usage count for this stream on the requested output:
1155 // NOTE that the usage count is the same for duplicated output and hardware output which is
1156 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1157 outputDesc->changeRefCount(stream, 1);
1158
1159 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
1160 // starting an output being rerouted?
1161 if (device == AUDIO_DEVICE_NONE) {
1162 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001163 }
Sharad Sangle36781612015-05-28 16:15:16 +05301164 routing_strategy strategy = getStrategy(stream);
1165 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
1166 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1167 (beaconMuteLatency > 0);
1168 uint32_t waitMs = beaconMuteLatency;
Sharad Sangle36781612015-05-28 16:15:16 +05301169 for (size_t i = 0; i < mOutputs.size(); i++) {
1170 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1171 if (desc != outputDesc) {
1172 // force a device change if any other output is managed by the same hw
1173 // module and has a current device selection that differs from selected device.
1174 // In this case, the audio HAL must receive the new device selection so that it can
1175 // change the device currently selected by the other active output.
1176 if (outputDesc->sharesHwModuleWith(desc) &&
1177 desc->device() != device) {
1178 force = true;
1179 }
1180 // wait for audio on other active outputs to be presented when starting
1181 // a notification so that audio focus effect can propagate, or that a mute/unmute
1182 // event occurred for beacon
1183 uint32_t latency = desc->latency();
1184 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1185 waitMs = latency;
1186 }
1187 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001188 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05301189 uint32_t muteWaitMs;
Lalit Kansaraf803ac52016-05-09 19:12:01 +05301190 muteWaitMs = setOutputDevice(outputDesc, device, force, 0, NULL, address);
Sharad Sangle36781612015-05-28 16:15:16 +05301191
1192 // handle special case for sonification while in call
1193 if (isInCall()) {
1194 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001195 }
Sharad Sangle36781612015-05-28 16:15:16 +05301196
1197 // apply volume rules for current stream and device if necessary
1198 checkAndSetVolume(stream,
Ashish Jain40cab3a2016-03-23 11:14:14 +05301199 mVolumeCurves->getVolumeIndex(stream, device),
Sharad Sangle36781612015-05-28 16:15:16 +05301200 outputDesc,
1201 device);
1202
1203 // update the outputs if starting an output with a stream that can affect notification
1204 // routing
1205 handleNotificationRoutingForStream(stream);
1206
1207 // force reevaluating accessibility routing when ringtone or alarm starts
1208 if (strategy == STRATEGY_SONIFICATION) {
1209 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1210 }
1211 }
1212 else {
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001213 // handle special case for sonification while in call
1214 if (isInCall()) {
1215 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Sharad Sangle36781612015-05-28 16:15:16 +05301216 }
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001217 }
Sharad Sangle36781612015-05-28 16:15:16 +05301218 return NO_ERROR;
1219}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001220
Sharad Sangle36781612015-05-28 16:15:16 +05301221void AudioPolicyManagerCustom::handleIncallSonification(audio_stream_type_t stream,
1222 bool starting, bool stateChange,
1223 audio_io_handle_t output)
1224{
1225 if(!hasPrimaryOutput()) {
1226 return;
1227 }
1228 // no action needed for AUDIO_STREAM_PATCH stream type, it's for internal flinger tracks
1229 if (stream == AUDIO_STREAM_PATCH) {
1230 return;
1231 }
1232 // if the stream pertains to sonification strategy and we are in call we must
1233 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
1234 // in the device used for phone strategy and play the tone if the selected device does not
1235 // interfere with the device used for phone strategy
1236 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
1237 // many times as there are active tracks on the output
1238 const routing_strategy stream_strategy = getStrategy(stream);
1239 if ((stream_strategy == STRATEGY_SONIFICATION) ||
1240 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
1241 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
1242 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
1243 stream, starting, outputDesc->mDevice, stateChange);
1244 if (outputDesc->mRefCount[stream]) {
1245 int muteCount = 1;
1246 if (stateChange) {
1247 muteCount = outputDesc->mRefCount[stream];
1248 }
1249 if (audio_is_low_visibility(stream)) {
1250 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
1251 for (int i = 0; i < muteCount; i++) {
1252 setStreamMute(stream, starting, outputDesc);
1253 }
1254 } else {
1255 ALOGV("handleIncallSonification() high visibility");
1256 if (outputDesc->device() &
1257 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
1258 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
1259 for (int i = 0; i < muteCount; i++) {
1260 setStreamMute(stream, starting, outputDesc);
1261 }
1262 }
1263 if (starting) {
1264 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
1265 AUDIO_STREAM_VOICE_CALL);
1266 } else {
1267 mpClientInterface->stopTone();
1268 }
1269 }
1270 }
1271 }
1272}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001273
Sharad Sangle36781612015-05-28 16:15:16 +05301274void AudioPolicyManagerCustom::handleNotificationRoutingForStream(audio_stream_type_t stream) {
1275 switch(stream) {
1276 case AUDIO_STREAM_MUSIC:
1277 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
1278 updateDevicesAndOutputs();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001279 break;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001280 default:
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001281 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001282 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001283}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001284
Sharad Sangle36781612015-05-28 16:15:16 +05301285status_t AudioPolicyManagerCustom::checkAndSetVolume(audio_stream_type_t stream,
1286 int index,
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301287 const sp<AudioOutputDescriptor>& outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301288 audio_devices_t device,
1289 int delayMs, bool force)
1290{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301291 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1292 ALOGW("checkAndSetVolume() invalid stream %d", stream);
1293 return INVALID_OPERATION;
1294 }
Sharad Sangle36781612015-05-28 16:15:16 +05301295 // do not change actual stream volume if the stream is muted
1296 if (outputDesc->mMuteCount[stream] != 0) {
1297 ALOGVV("checkAndSetVolume() stream %d muted count %d",
1298 stream, outputDesc->mMuteCount[stream]);
1299 return NO_ERROR;
1300 }
1301 audio_policy_forced_cfg_t forceUseForComm =
1302 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
1303 // do not change in call volume if bluetooth is connected and vice versa
1304 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
1305 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
1306 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
1307 stream, forceUseForComm);
1308 return INVALID_OPERATION;
1309 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001310
Sharad Sangle36781612015-05-28 16:15:16 +05301311 if (device == AUDIO_DEVICE_NONE) {
1312 device = outputDesc->device();
1313 }
1314
1315 float volumeDb = computeVolume(stream, index, device);
1316 if (outputDesc->isFixedVolume(device)) {
1317 volumeDb = 0.0f;
1318 }
1319
1320 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
1321
1322 if (stream == AUDIO_STREAM_VOICE_CALL ||
1323 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
1324 float voiceVolume;
1325 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
1326 if (stream == AUDIO_STREAM_VOICE_CALL) {
Ashish Jain40cab3a2016-03-23 11:14:14 +05301327 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Sharad Sangle36781612015-05-28 16:15:16 +05301328 } else {
1329 voiceVolume = 1.0;
1330 }
1331
1332 if (voiceVolume != mLastVoiceVolume && ((outputDesc == mPrimaryOutput) ||
1333 isDirectOutput(outputDesc->mIoHandle) || device & AUDIO_DEVICE_OUT_ALL_USB)) {
1334 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
1335 mLastVoiceVolume = voiceVolume;
1336 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301337#ifdef FM_POWER_OPT
1338 } else if (stream == AUDIO_STREAM_MUSIC && hasPrimaryOutput() &&
Haynes Mathew George603ae9b2015-12-21 17:23:59 -08001339 outputDesc == mPrimaryOutput && mFMIsActive) {
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001340 /* Avoid unnecessary set_parameter calls as it puts the primary
1341 outputs FastMixer in HOT_IDLE leading to breaks in audio */
1342 if (volumeDb != mPrevFMVolumeDb) {
1343 mPrevFMVolumeDb = volumeDb;
1344 AudioParameter param = AudioParameter();
1345 param.addFloat(String8("fm_volume"), Volume::DbToAmpl(volumeDb));
Mingming Yin1433cc62016-01-04 14:58:00 -08001346 //Double delayMs to avoid sound burst while device switch.
1347 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString(), delayMs*2);
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001348 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301349#endif /* FM_POWER_OPT end */
Sharad Sangle36781612015-05-28 16:15:16 +05301350 }
1351
1352 return NO_ERROR;
1353}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001354
Sharad Sangle36781612015-05-28 16:15:16 +05301355bool AudioPolicyManagerCustom::isDirectOutput(audio_io_handle_t output) {
1356 for (size_t i = 0; i < mOutputs.size(); i++) {
1357 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1358 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1359 if ((curOutput == output) && (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
1360 return true;
1361 }
1362 }
1363 return false;
1364}
vivek mehta0ea887a2015-08-26 14:01:20 -07001365
Ashish Jainac510a72016-04-28 12:22:58 +05301366bool static isDirectPCMEnabled(int bitWidth)
1367{
1368 bool directPCMEnabled = false;
1369 if (bitWidth == 24 || bitWidth == 32)
1370 directPCMEnabled =
1371 property_get_bool("audio.offload.pcm.24bit.enable", false);
1372 else
1373 directPCMEnabled =
1374 property_get_bool("audio.offload.pcm.16bit.enable", false);
1375
1376 return directPCMEnabled;
1377}
1378
vivek mehta0ea887a2015-08-26 14:01:20 -07001379status_t AudioPolicyManagerCustom::getOutputForAttr(const audio_attributes_t *attr,
1380 audio_io_handle_t *output,
1381 audio_session_t session,
1382 audio_stream_type_t *stream,
1383 uid_t uid,
1384 uint32_t samplingRate,
1385 audio_format_t format,
1386 audio_channel_mask_t channelMask,
1387 audio_output_flags_t flags,
1388 audio_port_handle_t selectedDeviceId,
1389 const audio_offload_info_t *offloadInfo)
1390{
1391 audio_offload_info_t tOffloadInfo = AUDIO_INFO_INITIALIZER;
1392
Alexy Joseph0ac09032015-10-16 15:57:53 -07001393 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
Ashish Jainac510a72016-04-28 12:22:58 +05301394 uint32_t bitWidth = (audio_bytes_per_sample(format) * 8);
Alexy Joseph0ac09032015-10-16 15:57:53 -07001395
1396 if (offloadDisabled) {
1397 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
1398 }
1399
Ashish Jainac510a72016-04-28 12:22:58 +05301400 if (!offloadDisabled && (offloadInfo == NULL) &&
1401 isDirectPCMEnabled(bitWidth) &&
1402 (flags == AUDIO_OUTPUT_FLAG_NONE)) {
vivek mehta0ea887a2015-08-26 14:01:20 -07001403
vivek mehta0ea887a2015-08-26 14:01:20 -07001404 tOffloadInfo.sample_rate = samplingRate;
1405 tOffloadInfo.channel_mask = channelMask;
1406 tOffloadInfo.format = format;
1407 tOffloadInfo.stream_type = *stream;
Ashish Jainac510a72016-04-28 12:22:58 +05301408 tOffloadInfo.bit_width = bitWidth;
vivek mehta0ea887a2015-08-26 14:01:20 -07001409 if (attr != NULL) {
1410 ALOGV("found attribute .. setting usage %d ", attr->usage);
1411 tOffloadInfo.usage = attr->usage;
1412 } else {
Alexy Joseph0ac09032015-10-16 15:57:53 -07001413 ALOGI("%s:: attribute is NULL .. no usage set", __func__);
vivek mehta0ea887a2015-08-26 14:01:20 -07001414 }
1415 offloadInfo = &tOffloadInfo;
1416 }
1417
1418 return AudioPolicyManager::getOutputForAttr(attr, output, session, stream,
1419 (uid_t)uid, (uint32_t)samplingRate,
1420 format, (audio_channel_mask_t)channelMask,
1421 flags, (audio_port_handle_t)selectedDeviceId,
1422 offloadInfo);
1423}
1424
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001425audio_io_handle_t AudioPolicyManagerCustom::getOutputForDevice(
1426 audio_devices_t device,
Sharad Sangle36781612015-05-28 16:15:16 +05301427 audio_session_t session __unused,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001428 audio_stream_type_t stream,
1429 uint32_t samplingRate,
1430 audio_format_t format,
1431 audio_channel_mask_t channelMask,
1432 audio_output_flags_t flags,
1433 const audio_offload_info_t *offloadInfo)
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001434{
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001435 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001436 status_t status;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001437
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001438#ifdef AUDIO_POLICY_TEST
1439 if (mCurOutput != 0) {
1440 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1441 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1442
1443 if (mTestOutputs[mCurOutput] == 0) {
1444 ALOGV("getOutput() opening test output");
Sharad Sangle36781612015-05-28 16:15:16 +05301445 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
1446 mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001447 outputDesc->mDevice = mTestDevice;
1448 outputDesc->mLatency = mTestLatencyMs;
1449 outputDesc->mFlags =
1450 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
1451 outputDesc->mRefCount[stream] = 0;
1452 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1453 config.sample_rate = mTestSamplingRate;
1454 config.channel_mask = mTestChannels;
1455 config.format = mTestFormat;
1456 if (offloadInfo != NULL) {
1457 config.offload_info = *offloadInfo;
1458 }
1459 status = mpClientInterface->openOutput(0,
1460 &mTestOutputs[mCurOutput],
1461 &config,
1462 &outputDesc->mDevice,
1463 String8(""),
1464 &outputDesc->mLatency,
1465 outputDesc->mFlags);
1466 if (status == NO_ERROR) {
1467 outputDesc->mSamplingRate = config.sample_rate;
1468 outputDesc->mFormat = config.format;
1469 outputDesc->mChannelMask = config.channel_mask;
1470 AudioParameter outputCmd = AudioParameter();
1471 outputCmd.addInt(String8("set_id"),mCurOutput);
1472 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1473 addOutput(mTestOutputs[mCurOutput], outputDesc);
1474 }
1475 }
1476 return mTestOutputs[mCurOutput];
1477 }
1478#endif //AUDIO_POLICY_TEST
Sharad Sangle36781612015-05-28 16:15:16 +05301479 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1480 (stream != AUDIO_STREAM_MUSIC)) {
1481 // compress should not be used for non-music streams
1482 ALOGE("Offloading only allowed with music stream");
1483 return 0;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301484 }
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301485
1486 if ((stream == AUDIO_STREAM_VOICE_CALL) &&
1487 (channelMask == 1) &&
Shiv Maliyappanahallid77d3b32016-02-02 13:45:50 -08001488 (samplingRate == 8000 || samplingRate == 16000 ||
1489 samplingRate == 32000 || samplingRate == 48000)) {
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301490 // Allow Voip direct output only if:
1491 // audio mode is MODE_IN_COMMUNCATION; AND
1492 // voip output is not opened already; AND
1493 // requested sample rate matches with that of voip input stream (if opened already)
1494 int value = 0;
1495 uint32_t mode = 0, voipOutCount = 1, voipSampleRate = 1;
1496 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1497 String8("audio_mode"));
1498 AudioParameter result = AudioParameter(valueStr);
1499 if (result.getInt(String8("audio_mode"), value) == NO_ERROR) {
1500 mode = value;
1501 }
1502
1503 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1504 String8("voip_out_stream_count"));
1505 result = AudioParameter(valueStr);
1506 if (result.getInt(String8("voip_out_stream_count"), value) == NO_ERROR) {
1507 voipOutCount = value;
1508 }
1509
1510 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1511 String8("voip_sample_rate"));
1512 result = AudioParameter(valueStr);
1513 if (result.getInt(String8("voip_sample_rate"), value) == NO_ERROR) {
1514 voipSampleRate = value;
1515 }
1516
1517 if ((mode == AUDIO_MODE_IN_COMMUNICATION) && (voipOutCount == 0) &&
1518 ((voipSampleRate == 0) || (voipSampleRate == samplingRate))) {
1519 if (audio_is_linear_pcm(format)) {
1520 char propValue[PROPERTY_VALUE_MAX] = {0};
1521 property_get("use.voice.path.for.pcm.voip", propValue, "0");
1522 bool voipPcmSysPropEnabled = !strncmp("true", propValue, sizeof("true"));
1523 if (voipPcmSysPropEnabled && (format == AUDIO_FORMAT_PCM_16_BIT)) {
Karthik Reddy Katta3fb9be72015-11-25 11:18:27 +05301524 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX |
1525 AUDIO_OUTPUT_FLAG_DIRECT);
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301526 ALOGD("Set VoIP and Direct output flags for PCM format");
1527 }
1528 }
1529 }
1530 }
1531
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301532#ifdef VOICE_CONCURRENCY
1533 char propValue[PROPERTY_VALUE_MAX];
1534 bool prop_play_enabled=false, prop_voip_enabled = false;
1535
1536 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1537 prop_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001538 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301539
1540 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1541 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1542 }
1543
1544 if (prop_play_enabled && mvoice_call_state) {
1545 //check if voice call is active / running in background
1546 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1547 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1548 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1549 {
1550 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1551 if(prop_voip_enabled) {
1552 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1553 flags );
1554 return 0;
1555 }
1556 }
1557 else {
1558 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
1559 ALOGD("voice_conc:IN call mode adding ULL flags .. flags: %x ", flags );
1560 flags = AUDIO_OUTPUT_FLAG_FAST;
1561 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
1562 if (AUDIO_STREAM_MUSIC == stream) {
1563 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1564 ALOGD("voice_conc:IN call mode adding deep-buffer flags %x ", flags );
1565 }
1566 else {
1567 flags = AUDIO_OUTPUT_FLAG_FAST;
1568 ALOGD("voice_conc:IN call mode adding fast flags %x ", flags );
1569 }
1570 }
1571 }
1572 }
1573 } else if (prop_voip_enabled && mvoice_call_state) {
1574 //check if voice call is active / running in background
1575 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1576 //return only ULL ouput
1577 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1578 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1579 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1580 {
1581 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1582 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1583 flags );
1584 return 0;
1585 }
1586 }
1587 }
1588#endif
1589#ifdef RECORD_PLAY_CONCURRENCY
1590 char recConcPropValue[PROPERTY_VALUE_MAX];
1591 bool prop_rec_play_enabled = false;
1592
1593 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
1594 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
1595 }
1596 if ((prop_rec_play_enabled) &&
1597 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
1598 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
1599 if (AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1600 // allow VoIP using voice path
1601 // Do nothing
1602 } else if((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1603 ALOGD("voice_conc:MODE_IN_COMM is setforcing deep buffer output for non ULL... flags: %x", flags);
1604 // use deep buffer path for all non ULL outputs
1605 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1606 }
1607 } else if ((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1608 ALOGD("voice_conc:Record mode is on forcing deep buffer output for non ULL... flags: %x ", flags);
1609 // use deep buffer path for all non ULL outputs
1610 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1611 }
1612 }
1613 if (prop_rec_play_enabled &&
1614 (stream == AUDIO_STREAM_ENFORCED_AUDIBLE)) {
1615 ALOGD("Record conc is on forcing ULL output for ENFORCED_AUDIBLE");
1616 flags = AUDIO_OUTPUT_FLAG_FAST;
1617 }
1618#endif
1619
Sharad Sangle4509cef2015-08-19 20:47:12 +05301620#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Sharad Sangle36781612015-05-28 16:15:16 +05301621 /*
1622 * WFD audio routes back to target speaker when starting a ringtone playback.
1623 * This is because primary output is reused for ringtone, so output device is
1624 * updated based on SONIFICATION strategy for both ringtone and music playback.
1625 * The same issue is not seen on remoted_submix HAL based WFD audio because
1626 * primary output is not reused and a new output is created for ringtone playback.
1627 * Issue is fixed by updating output flag to AUDIO_OUTPUT_FLAG_FAST when there is
1628 * a non-music stream playback on WFD, so primary output is not reused for ringtone.
1629 */
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001630 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
1631 if ((availableOutputDeviceTypes & AUDIO_DEVICE_OUT_PROXY)
1632 && (stream != AUDIO_STREAM_MUSIC)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301633 ALOGD("WFD audio: use OUTPUT_FLAG_FAST for non music stream. flags:%x", flags );
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001634 //For voip paths
1635 if(flags & AUDIO_OUTPUT_FLAG_DIRECT)
1636 flags = AUDIO_OUTPUT_FLAG_DIRECT;
1637 else //route every thing else to ULL path
1638 flags = AUDIO_OUTPUT_FLAG_FAST;
1639 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301640#endif
1641
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001642 // open a direct output if required by specified parameters
vivek mehta0ea887a2015-08-26 14:01:20 -07001643 // force direct flag if offload flag is set: offloading implies a direct output stream
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001644 // and all common behaviors are driven by checking only the direct flag
1645 // this should normally be set appropriately in the policy configuration file
1646 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1647 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1648 }
1649 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1650 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1651 }
vivek mehta0ea887a2015-08-26 14:01:20 -07001652
vivek mehta4b0d8192015-10-16 00:25:59 -07001653 bool forced_deep = false;
Sharad Sangle36781612015-05-28 16:15:16 +05301654 // only allow deep buffering for music stream type
1655 if (stream != AUDIO_STREAM_MUSIC) {
1656 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Sharad Sangle497aef82015-08-03 17:55:48 +05301657 } else if (/* stream == AUDIO_STREAM_MUSIC && */
1658 flags == AUDIO_OUTPUT_FLAG_NONE &&
1659 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
vivek mehtac984b992015-11-25 13:28:55 -08001660 forced_deep = true;
Sharad Sangle36781612015-05-28 16:15:16 +05301661 }
Sharad Sangle497aef82015-08-03 17:55:48 +05301662
Sharad Sangle36781612015-05-28 16:15:16 +05301663 if (stream == AUDIO_STREAM_TTS) {
1664 flags = AUDIO_OUTPUT_FLAG_TTS;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001665 }
1666
Zhou Song2b3d1552016-03-21 17:21:04 +08001667 // check if direct output for track offload already exits
1668 bool is_track_offload_active = false;
1669 for (size_t i = 0; i < mOutputs.size(); i++) {
1670 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1671 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) {
1672 is_track_offload_active = true;
1673 ALOGD("Track offload already active");
1674 break;
1675 }
1676 }
1677
vivek mehta4b0d8192015-10-16 00:25:59 -07001678 // Do offload magic here
vivek mehtac984b992015-11-25 13:28:55 -08001679 if ((flags == AUDIO_OUTPUT_FLAG_NONE) &&
1680 (stream == AUDIO_STREAM_MUSIC) &&
Zhou Song2b3d1552016-03-21 17:21:04 +08001681 (offloadInfo != NULL) && !is_track_offload_active &&
vivek mehta4b0d8192015-10-16 00:25:59 -07001682 ((offloadInfo->usage == AUDIO_USAGE_MEDIA) || (offloadInfo->usage == AUDIO_USAGE_GAME))) {
vivek mehtac984b992015-11-25 13:28:55 -08001683 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DIRECT_PCM);
vivek mehta4b0d8192015-10-16 00:25:59 -07001684 ALOGD("AudioCustomHAL --> Force Direct Flag .. flag (0x%x)", flags);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301685 }
1686
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001687 sp<IOProfile> profile;
1688
1689 // skip direct output selection if the request can obviously be attached to a mixed output
1690 // and not explicitly requested
vivek mehtac984b992015-11-25 13:28:55 -08001691 if (((flags & (AUDIO_OUTPUT_FLAG_DIRECT|AUDIO_OUTPUT_FLAG_DIRECT_PCM)) == 0) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301692 audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX &&
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001693 audio_channel_count_from_out_mask(channelMask) <= 2) {
1694 goto non_direct_output;
1695 }
1696
1697 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1698 // creating an offloaded track and tearing it down immediately after start when audioflinger
1699 // detects there is an active non offloadable effect.
1700 // FIXME: We should check the audio session here but we do not have it in this context.
1701 // This may prevent offloading in rare situations where effects are left active by apps
1702 // in the background.
Weiyin Jiang6bba3202016-03-03 14:49:36 +08001703 //
1704 // Supplementary annotation:
1705 // For sake of track offload introduced, we need a rollback for both compress offload
1706 // and track offload use cases.
1707 if ((flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD|AUDIO_OUTPUT_FLAG_DIRECT_PCM)) &&
1708 (mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
1709 ALOGD("non offloadable effect is enabled, try with non direct output");
1710 goto non_direct_output;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001711 }
1712
Weiyin Jiang6bba3202016-03-03 14:49:36 +08001713 profile = getProfileForDirectOutput(device,
1714 samplingRate,
1715 format,
1716 channelMask,
1717 (audio_output_flags_t)flags);
1718
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001719 if (profile != 0) {
vivek mehtac984b992015-11-25 13:28:55 -08001720
1721 if (!(flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301722 (profile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT_PCM)) {
vivek mehtac984b992015-11-25 13:28:55 -08001723 ALOGI("got Direct_PCM without requesting ... reject ");
1724 profile = NULL;
1725 goto non_direct_output;
1726 }
1727
Sharad Sangle36781612015-05-28 16:15:16 +05301728 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001729
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001730 // if multiple concurrent offload decode is supported
1731 // do no check for reuse and also don't close previous output if its offload
1732 // previous output will be closed during track destruction
1733 if (!(property_get_bool("audio.offload.multiple.enabled", false) &&
1734 ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0))) {
1735 for (size_t i = 0; i < mOutputs.size(); i++) {
1736 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1737 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1738 outputDesc = desc;
1739 // reuse direct output if currently open and configured with same parameters
1740 if ((samplingRate == outputDesc->mSamplingRate) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301741 audio_formats_match(format, outputDesc->mFormat) &&
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001742 (channelMask == outputDesc->mChannelMask)) {
1743 outputDesc->mDirectOpenCount++;
1744 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1745 return mOutputs.keyAt(i);
1746 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001747 }
1748 }
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001749 // close direct output if currently open and configured with different parameters
1750 if (outputDesc != NULL) {
1751 closeOutput(outputDesc->mIoHandle);
1752 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001753 }
Sharad Sangle36781612015-05-28 16:15:16 +05301754
1755 // if the selected profile is offloaded and no offload info was specified,
1756 // create a default one
1757 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
Ashish Jain40cab3a2016-03-23 11:14:14 +05301758 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
Sharad Sangle36781612015-05-28 16:15:16 +05301759 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1760 defaultOffloadInfo.sample_rate = samplingRate;
1761 defaultOffloadInfo.channel_mask = channelMask;
1762 defaultOffloadInfo.format = format;
1763 defaultOffloadInfo.stream_type = stream;
1764 defaultOffloadInfo.bit_rate = 0;
1765 defaultOffloadInfo.duration_us = -1;
1766 defaultOffloadInfo.has_video = true; // conservative
1767 defaultOffloadInfo.is_streaming = true; // likely
1768 offloadInfo = &defaultOffloadInfo;
1769 }
1770
1771 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001772 outputDesc->mDevice = device;
1773 outputDesc->mLatency = 0;
Sharad Sangle36781612015-05-28 16:15:16 +05301774 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001775 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1776 config.sample_rate = samplingRate;
1777 config.channel_mask = channelMask;
1778 config.format = format;
1779 if (offloadInfo != NULL) {
1780 config.offload_info = *offloadInfo;
1781 }
Sharad Sangle36781612015-05-28 16:15:16 +05301782 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001783 &output,
1784 &config,
1785 &outputDesc->mDevice,
1786 String8(""),
1787 &outputDesc->mLatency,
1788 outputDesc->mFlags);
1789
1790 // only accept an output with the requested parameters
1791 if (status != NO_ERROR ||
1792 (samplingRate != 0 && samplingRate != config.sample_rate) ||
Ashish Jain40cab3a2016-03-23 11:14:14 +05301793 (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) ||
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001794 (channelMask != 0 && channelMask != config.channel_mask)) {
1795 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1796 "format %d %d, channelMask %04x %04x", output, samplingRate,
1797 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1798 outputDesc->mChannelMask);
1799 if (output != AUDIO_IO_HANDLE_NONE) {
1800 mpClientInterface->closeOutput(output);
1801 }
Sharad Sangle36781612015-05-28 16:15:16 +05301802 // fall back to mixer output if possible when the direct output could not be open
Ashish Jain40cab3a2016-03-23 11:14:14 +05301803 if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) {
Sharad Sangle36781612015-05-28 16:15:16 +05301804 goto non_direct_output;
1805 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001806 return AUDIO_IO_HANDLE_NONE;
1807 }
1808 outputDesc->mSamplingRate = config.sample_rate;
1809 outputDesc->mChannelMask = config.channel_mask;
1810 outputDesc->mFormat = config.format;
1811 outputDesc->mRefCount[stream] = 0;
1812 outputDesc->mStopTime[stream] = 0;
1813 outputDesc->mDirectOpenCount = 1;
1814
1815 audio_io_handle_t srcOutput = getOutputForEffect();
1816 addOutput(output, outputDesc);
1817 audio_io_handle_t dstOutput = getOutputForEffect();
1818 if (dstOutput == output) {
Gao Jiedb057832015-11-12 08:51:22 +08001819#ifdef DOLBY_ENABLE
1820 status_t status = mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1821 if (status == NO_ERROR) {
1822 for (size_t i = 0; i < mEffects.size(); i++) {
1823 sp<EffectDescriptor> desc = mEffects.valueAt(i);
1824 if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX) {
1825 // update the mIo member of EffectDescriptor for the global effect
1826 ALOGV("%s updating mIo", __FUNCTION__);
1827 desc->mIo = dstOutput;
1828 }
1829 }
1830 } else {
1831 ALOGW("%s moveEffects from %d to %d failed", __FUNCTION__, srcOutput, dstOutput);
1832 }
1833#else // DOLBY_END
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001834 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
Gao Jiedb057832015-11-12 08:51:22 +08001835#endif // LINE_ADDED_BY_DOLBY
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001836 }
1837 mPreviousOutputs = mOutputs;
1838 ALOGV("getOutput() returns new direct output %d", output);
1839 mpClientInterface->onAudioPortListUpdate();
1840 return output;
1841 }
1842
1843non_direct_output:
Ashish Jain5fe12412016-04-18 18:44:17 +05301844
1845 // A request for HW A/V sync cannot fallback to a mixed output because time
1846 // stamps are embedded in audio data
1847 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1848 return AUDIO_IO_HANDLE_NONE;
1849 }
1850
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001851 // ignoring channel mask due to downmix capability in mixer
1852
1853 // open a non direct output
1854
1855 // for non direct outputs, only PCM is supported
1856 if (audio_is_linear_pcm(format)) {
1857 // get which output is suitable for the specified stream. The actual
1858 // routing change will happen when startOutput() will be called
1859 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1860
1861 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1862 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
Ashish Jain660d3112016-06-20 10:16:51 +05301863
1864 if (forced_deep) {
1865 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
1866 ALOGI("setting force DEEP buffer now ");
1867 } else if(flags == AUDIO_OUTPUT_FLAG_NONE) {
1868 // no deep buffer playback is requested hence fallback to primary
1869 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_PRIMARY);
1870 ALOGI("FLAG None hence request for a primary output");
1871 }
1872
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001873 output = selectOutput(outputs, flags, format);
1874 }
1875 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1876 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1877
vivek mehta0ea887a2015-08-26 14:01:20 -07001878 ALOGV("getOutputForDevice() returns output %d", output);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001879
1880 return output;
1881}
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301882
1883status_t AudioPolicyManagerCustom::getInputForAttr(const audio_attributes_t *attr,
1884 audio_io_handle_t *input,
1885 audio_session_t session,
1886 uid_t uid,
1887 uint32_t samplingRate,
1888 audio_format_t format,
1889 audio_channel_mask_t channelMask,
1890 audio_input_flags_t flags,
1891 audio_port_handle_t selectedDeviceId,
1892 input_type_t *inputType)
1893{
Ashish Jain40cab3a2016-03-23 11:14:14 +05301894 audio_source_t inputSource;
1895 inputSource = attr->source;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301896#ifdef VOICE_CONCURRENCY
1897
1898 char propValue[PROPERTY_VALUE_MAX];
1899 bool prop_rec_enabled=false, prop_voip_enabled = false;
1900
1901 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
1902 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1903 }
1904
1905 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1906 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1907 }
1908
1909 if (prop_rec_enabled && mvoice_call_state) {
1910 //check if voice call is active / running in background
1911 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1912 //Need to block input request
1913 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1914 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1915 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1916 {
1917 switch(inputSource) {
1918 case AUDIO_SOURCE_VOICE_UPLINK:
1919 case AUDIO_SOURCE_VOICE_DOWNLINK:
1920 case AUDIO_SOURCE_VOICE_CALL:
1921 ALOGD("voice_conc:Creating input during incall mode for inputSource: %d",
1922 inputSource);
1923 break;
1924
1925 case AUDIO_SOURCE_VOICE_COMMUNICATION:
1926 if(prop_voip_enabled) {
1927 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1928 inputSource);
1929 return NO_INIT;
1930 }
1931 break;
1932 default:
1933 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1934 inputSource);
1935 return NO_INIT;
1936 }
1937 }
1938 }//check for VoIP flag
1939 else if(prop_voip_enabled && mvoice_call_state) {
1940 //check if voice call is active / running in background
1941 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1942 //Need to block input request
1943 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1944 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1945 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1946 {
1947 if(inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1948 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
1949 return NO_INIT;
1950 }
1951 }
1952 }
1953
1954#endif
1955
1956 return AudioPolicyManager::getInputForAttr(attr,
1957 input,
1958 session,
1959 uid,
1960 samplingRate,
1961 format,
1962 channelMask,
1963 flags,
1964 selectedDeviceId,
1965 inputType);
1966}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001967
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301968status_t AudioPolicyManagerCustom::startInput(audio_io_handle_t input,
1969 audio_session_t session)
1970{
1971 ALOGV("startInput() input %d", input);
1972 ssize_t index = mInputs.indexOfKey(input);
1973 if (index < 0) {
1974 ALOGW("startInput() unknown input %d", input);
1975 return BAD_VALUE;
1976 }
1977 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1978
Ashish Jain40cab3a2016-03-23 11:14:14 +05301979 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1980 if (audioSession == 0) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301981 ALOGW("startInput() unknown session %d on input %d", session, input);
1982 return BAD_VALUE;
1983 }
1984
1985 // virtual input devices are compatible with other input devices
1986 if (!is_virtual_input_device(inputDesc->mDevice)) {
1987
1988 // for a non-virtual input device, check if there is another (non-virtual) active input
1989 audio_io_handle_t activeInput = mInputs.getActiveInput();
1990 if (activeInput != 0 && activeInput != input) {
1991
1992 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1993 // otherwise the active input continues and the new input cannot be started.
1994 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Ashish Jain40cab3a2016-03-23 11:14:14 +05301995 if ((activeDesc->inputSource() == AUDIO_SOURCE_HOTWORD) &&
1996 !activeDesc->hasPreemptedSession(session)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301997 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
Ashish Jain40cab3a2016-03-23 11:14:14 +05301998 //FIXME: consider all active sessions
1999 AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions();
2000 audio_session_t activeSession = activeSessions.keyAt(0);
2001 SortedVector<audio_session_t> sessions =
2002 activeDesc->getPreemptedSessions();
2003 sessions.add(activeSession);
2004 inputDesc->setPreemptedSessions(sessions);
2005 stopInput(activeInput, activeSession);
2006 releaseInput(activeInput, activeSession);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302007 } else {
2008 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
2009 return INVALID_OPERATION;
2010 }
2011 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05302012 // Do not allow capture if an active voice call is using a software patch and
2013 // the call TX source device is on the same HW module.
2014 // FIXME: would be better to refine to only inputs whose profile connects to the
2015 // call TX device but this information is not in the audio patch
2016 if (mCallTxPatch != 0 &&
2017 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
2018 return INVALID_OPERATION;
2019 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302020 }
2021
2022 // Routing?
2023 mInputRoutes.incRouteActivity(session);
2024#ifdef RECORD_PLAY_CONCURRENCY
2025 mIsInputRequestOnProgress = true;
2026
2027 char getPropValue[PROPERTY_VALUE_MAX];
2028 bool prop_rec_play_enabled = false;
2029
2030 if (property_get("rec.playback.conc.disabled", getPropValue, NULL)) {
2031 prop_rec_play_enabled = atoi(getPropValue) || !strncmp("true", getPropValue, 4);
2032 }
2033
2034 if ((prop_rec_play_enabled) &&(mInputs.activeInputsCount() == 0)){
2035 // send update to HAL on record playback concurrency
2036 AudioParameter param = AudioParameter();
2037 param.add(String8("rec_play_conc_on"), String8("true"));
2038 ALOGD("startInput() setParameters rec_play_conc is setting to ON ");
2039 mpClientInterface->setParameters(0, param.toString());
2040
2041 // Call invalidate to reset all opened non ULL audio tracks
2042 // Move tracks associated to this strategy from previous output to new output
2043 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
2044 // Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder)
Sharad Sangle4509cef2015-08-19 20:47:12 +05302045 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE && (i != AUDIO_STREAM_PATCH))) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302046 ALOGD("Invalidate on releaseInput for stream :: %d ", i);
2047 //FIXME see fixme on name change
2048 mpClientInterface->invalidateStream((audio_stream_type_t)i);
2049 }
2050 }
2051 // close compress tracks
2052 for (size_t i = 0; i < mOutputs.size(); i++) {
2053 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
2054 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
2055 ALOGD("ouput desc / profile is NULL");
2056 continue;
2057 }
2058 if (outputDesc->mProfile->mFlags
2059 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2060 // close compress sessions
2061 ALOGD("calling closeOutput on record conc for COMPRESS output");
2062 closeOutput(mOutputs.keyAt(i));
2063 }
2064 }
Ashish Jaine4a22d52016-06-22 11:55:08 +05302065 // If effects where present on any of the above closed outputs,
2066 // audioflinger moved them to the primary output by default
2067 // move them back to the appropriate output.
2068 moveGlobalEffect();
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302069 }
2070#endif
2071
Ashish Jain40cab3a2016-03-23 11:14:14 +05302072 if (!inputDesc->isActive() || mInputRoutes.hasRouteChanged(session)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302073 // if input maps to a dynamic policy with an activity listener, notify of state change
Ashish Jain630c5e12016-04-18 11:50:21 +05302074 if ((inputDesc->mPolicyMix != NULL)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302075 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Ashish Jain5fe12412016-04-18 18:44:17 +05302076 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302077 MIX_STATE_MIXING);
Ashish Jain630c5e12016-04-18 11:50:21 +05302078 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302079
2080 if (mInputs.activeInputsCount() == 0) {
2081 SoundTrigger::setCaptureState(true);
2082 }
2083 setInputDevice(input, getNewInputDevice(input), true /* force */);
2084
2085 // automatically enable the remote submix output when input is started if not
2086 // used by a policy mix of type MIX_TYPE_RECORDERS
2087 // For remote submix (a virtual device), we open only one input per capture request.
2088 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
2089 String8 address = String8("");
2090 if (inputDesc->mPolicyMix == NULL) {
2091 address = String8("0");
Ashish Jain630c5e12016-04-18 11:50:21 +05302092 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
Ashish Jain5fe12412016-04-18 18:44:17 +05302093 address = inputDesc->mPolicyMix->mDeviceAddress;
Ashish Jain630c5e12016-04-18 11:50:21 +05302094 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302095 if (address != "") {
2096 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2097 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2098 address, "remote-submix");
2099 }
2100 }
2101 }
2102
Ashish Jain40cab3a2016-03-23 11:14:14 +05302103 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302104
Ashish Jain40cab3a2016-03-23 11:14:14 +05302105 audioSession->changeActiveCount(1);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302106#ifdef RECORD_PLAY_CONCURRENCY
2107 mIsInputRequestOnProgress = false;
2108#endif
2109 return NO_ERROR;
2110}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08002111
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302112status_t AudioPolicyManagerCustom::stopInput(audio_io_handle_t input,
2113 audio_session_t session)
2114{
2115 status_t status;
2116 status = AudioPolicyManager::stopInput(input, session);
2117#ifdef RECORD_PLAY_CONCURRENCY
2118 char propValue[PROPERTY_VALUE_MAX];
2119 bool prop_rec_play_enabled = false;
2120
2121 if (property_get("rec.playback.conc.disabled", propValue, NULL)) {
2122 prop_rec_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
2123 }
2124
2125 if ((prop_rec_play_enabled) && (mInputs.activeInputsCount() == 0)) {
2126
2127 //send update to HAL on record playback concurrency
2128 AudioParameter param = AudioParameter();
2129 param.add(String8("rec_play_conc_on"), String8("false"));
2130 ALOGD("stopInput() setParameters rec_play_conc is setting to OFF ");
2131 mpClientInterface->setParameters(0, param.toString());
2132
2133 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
2134 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
2135 //Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder stop tone)
2136 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE) && (i != AUDIO_STREAM_PATCH)) {
2137 ALOGD(" Invalidate on stopInput for stream :: %d ", i);
2138 //FIXME see fixme on name change
2139 mpClientInterface->invalidateStream((audio_stream_type_t)i);
2140 }
2141 }
2142 }
2143#endif
2144 return status;
2145}
2146
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302147void AudioPolicyManagerCustom::closeAllInputs() {
2148 bool patchRemoved = false;
2149
Dhanalakshmi Siddanibfeca7f2015-10-08 15:17:11 +05302150 for(size_t input_index = mInputs.size(); input_index > 0; input_index--) {
2151 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index-1);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302152 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302153 if (patch_index >= 0) {
2154 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302155 status_t status;
2156 status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302157 mAudioPatches.removeItemsAt(patch_index);
2158 patchRemoved = true;
2159 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05302160 if ((inputDesc->getOpenRefCount() > 0) && inputDesc->isSoundTrigger()
2161 && (mInputs.size() == 1)) {
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302162 ALOGD("Do not close sound trigger input handle");
2163 } else {
Dhanalakshmi Siddanibfeca7f2015-10-08 15:17:11 +05302164 mpClientInterface->closeInput(mInputs.keyAt(input_index-1));
2165 mInputs.removeItem(mInputs.keyAt(input_index-1));
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302166 }
2167 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05302168 mInputs.clear();
2169 SoundTrigger::setCaptureState(false);
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302170 nextAudioPortGeneration();
2171
2172 if (patchRemoved) {
2173 mpClientInterface->onAudioPatchListUpdate();
2174 }
2175}
2176
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302177AudioPolicyManagerCustom::AudioPolicyManagerCustom(AudioPolicyClientInterface *clientInterface)
Sharad Sangle4509cef2015-08-19 20:47:12 +05302178 : AudioPolicyManager(clientInterface),
2179 mHdmiAudioDisabled(false),
2180 mHdmiAudioEvent(false),
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08002181 mPrevPhoneState(0),
Haynes Mathew George603ae9b2015-12-21 17:23:59 -08002182 mPrevFMVolumeDb(0.0f),
2183 mFMIsActive(false)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302184{
Ashish Jain40cab3a2016-03-23 11:14:14 +05302185
Satya Krishna Pindiproli8f83f102016-06-21 17:56:14 +05302186#ifdef USE_XML_AUDIO_POLICY_CONF
2187 ALOGD("USE_XML_AUDIO_POLICY_CONF is TRUE");
2188#else
2189 ALOGD("USE_XML_AUDIO_POLICY_CONF is FALSE");
2190#endif
2191
Ashish Jain40cab3a2016-03-23 11:14:14 +05302192 //TODO: Check the new logic to parse policy conf and update the below code
2193 // Need this when SSR encoding is enabled
Mingming Yin38ea08c2015-10-05 15:24:04 -07002194 char ssr_enabled[PROPERTY_VALUE_MAX] = {0};
2195 bool prop_ssr_enabled = false;
2196
2197 if (property_get("ro.qc.sdk.audio.ssr", ssr_enabled, NULL)) {
2198 prop_ssr_enabled = atoi(ssr_enabled) || !strncmp("true", ssr_enabled, 4);
2199 }
2200
2201 for (size_t i = 0; i < mHwModules.size(); i++) {
Ashish Jain40cab3a2016-03-23 11:14:14 +05302202 ALOGV("Hw module %zu", i);
Mingming Yin38ea08c2015-10-05 15:24:04 -07002203 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) {
2204 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Ashish Jain40cab3a2016-03-23 11:14:14 +05302205 AudioProfileVector profiles = inProfile->getAudioProfiles();
2206 for (size_t k = 0; k < profiles.size(); k++){
2207 ChannelsVector channels = profiles[k]->getChannels();
2208 for (size_t x = 0; x < channels.size(); x++) {
2209 audio_channel_mask_t channelMask = channels[x];
2210 ALOGV("Channel Mask %x size %zu", channelMask,
2211 channels.size());
2212 if (AUDIO_CHANNEL_IN_5POINT1 == channelMask) {
2213 if (!prop_ssr_enabled) {
2214 ALOGI("removing AUDIO_CHANNEL_IN_5POINT1 from"
2215 " input profile as SSR(surround sound record)"
2216 " is not supported on this chipset variant");
2217 channels.removeItemsAt(x, 1);
2218 ALOGV("Channel Mask size now %zu",
2219 channels.size());
2220 }
Mingming Yin38ea08c2015-10-05 15:24:04 -07002221 }
2222 }
2223 }
2224 }
2225 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302226#ifdef RECORD_PLAY_CONCURRENCY
2227 mIsInputRequestOnProgress = false;
2228#endif
2229
2230
2231#ifdef VOICE_CONCURRENCY
2232 mFallBackflag = getFallBackPath();
2233#endif
2234}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07002235}