blob: 8bfea9adf5dcc11a304e4089018aaf11446468bd [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
Preetam Singh Ranawat3db94ec2016-10-13 18:35:12 +0530438bool AudioPolicyManagerCustom::isInvalidationOfMusicStreamNeeded(routing_strategy strategy)
439{
440 if (strategy == STRATEGY_MEDIA) {
441 for (size_t i = 0; i < mOutputs.size(); i++) {
442 sp<SwAudioOutputDescriptor> newOutputDesc = mOutputs.valueAt(i);
443 if (newOutputDesc->mFormat == AUDIO_FORMAT_DSD)
444 return false;
445 }
446 }
447 return true;
448}
449
450void AudioPolicyManagerCustom::checkOutputForStrategy(routing_strategy strategy)
451{
452 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
453 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
454 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mOutputs);
455 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
456
457 // also take into account external policy-related changes: add all outputs which are
458 // associated with policies in the "before" and "after" output vectors
459 ALOGV("checkOutputForStrategy(): policy related outputs");
460 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
461 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
462 if (desc != 0 && desc->mPolicyMix != NULL) {
463 srcOutputs.add(desc->mIoHandle);
464 ALOGV(" previous outputs: adding %d", desc->mIoHandle);
465 }
466 }
467 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
468 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
469 if (desc != 0 && desc->mPolicyMix != NULL) {
470 dstOutputs.add(desc->mIoHandle);
471 ALOGV(" new outputs: adding %d", desc->mIoHandle);
472 }
473 }
474
475 if (!vectorsEqual(srcOutputs,dstOutputs) && isInvalidationOfMusicStreamNeeded(strategy)) {
476 AudioPolicyManager::checkOutputForStrategy(strategy);
477 }
478}
479
Sharad Sangle36781612015-05-28 16:15:16 +0530480// This function checks for the parameters which can be offloaded.
481// This can be enhanced depending on the capability of the DSP and policy
482// of the system.
483bool AudioPolicyManagerCustom::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700484{
Sharad Sangle36781612015-05-28 16:15:16 +0530485 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
486 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
487 offloadInfo.sample_rate, offloadInfo.channel_mask,
488 offloadInfo.format,
489 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
490 offloadInfo.has_video);
Ashish Jain40cab3a2016-03-23 11:14:14 +0530491
492 if (mMasterMono) {
493 return false; // no offloading if mono is set.
494 }
495
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530496#ifdef VOICE_CONCURRENCY
497 char concpropValue[PROPERTY_VALUE_MAX];
498 if (property_get("voice.playback.conc.disabled", concpropValue, NULL)) {
499 bool propenabled = atoi(concpropValue) || !strncmp("true", concpropValue, 4);
500 if (propenabled) {
501 if (isInCall())
502 {
503 ALOGD("\n copl: blocking compress offload on call mode\n");
504 return false;
505 }
506 }
507 }
508#endif
Preetam Singh Ranawat96afc162016-08-25 14:35:02 +0530509 if (property_get_bool("voice.dsd.playback.conc.disabled", true) &&
510 isInCall() && (offloadInfo.format == AUDIO_FORMAT_DSD)) {
511 ALOGD("blocking DSD compress offload on call mode");
512 return false;
513 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530514#ifdef RECORD_PLAY_CONCURRENCY
515 char recConcPropValue[PROPERTY_VALUE_MAX];
516 bool prop_rec_play_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700517
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530518 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
519 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
520 }
521
522 if ((prop_rec_play_enabled) &&
523 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
524 ALOGD("copl: blocking compress offload for record concurrency");
525 return false;
526 }
527#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530528 // Check if stream type is music, then only allow offload as of now.
529 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
530 {
531 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
532 return false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700533 }
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530534
Alexy Joseph0ac09032015-10-16 15:57:53 -0700535 // Check if offload has been disabled
536 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
537 if (offloadDisabled) {
538 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
539 return false;
540 }
541
Ashish Jainac510a72016-04-28 12:22:58 +0530542 //check if it's multi-channel AAC (includes sub formats) and FLAC format
543 if ((popcount(offloadInfo.channel_mask) > 2) &&
544 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
545 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS))) {
546 ALOGD("offload disabled for multi-channel AAC,FLAC and VORBIS format");
547 return false;
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530548 }
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530549
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530550#ifdef AUDIO_EXTN_FORMATS_ENABLED
Ashish Jainac510a72016-04-28 12:22:58 +0530551 //check if it's multi-channel FLAC/ALAC/WMA format with sample rate > 48k
552 if ((popcount(offloadInfo.channel_mask) > 2) &&
553 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
554 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) && (offloadInfo.sample_rate > 48000)) ||
555 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) && (offloadInfo.sample_rate > 48000)) ||
556 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) && (offloadInfo.sample_rate > 48000)) ||
557 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS))) {
558 ALOGD("offload disabled for multi-channel FLAC/ALAC/WMA/AAC_ADTS clips with sample rate > 48kHz");
559 return false;
560 }
yidonghae28e3a2016-08-19 11:31:15 +0800561
562 if ((((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) && (offloadInfo.bit_rate > MAX_BITRATE_WMA)) ||
563 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) && (offloadInfo.bit_rate > MAX_BITRATE_WMA_PRO)) ||
564 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) && (offloadInfo.bit_rate > MAX_BITRATE_WMA_LOSSLESS))){
565 //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.
566 ALOGD("offload disabled for WMA/WMA_PRO/WMA_LOSSLESS clips with bit rate over maximum supported value");
567 return false;
568 }
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530569#endif
Ashish Jainac510a72016-04-28 12:22:58 +0530570 //TODO: enable audio offloading with video when ready
571 const bool allowOffloadWithVideo =
572 property_get_bool("audio.offload.video", false /* default_value */);
573 if (offloadInfo.has_video && !allowOffloadWithVideo) {
574 ALOGV("isOffloadSupported: has_video == true, returning false");
575 return false;
576 }
Manish Dewanganf3cd0f82015-10-13 14:04:36 +0530577
Ashish Jainac510a72016-04-28 12:22:58 +0530578 const bool allowOffloadStreamingWithVideo = property_get_bool("av.streaming.offload.enable",
579 false /*default value*/);
580 if (offloadInfo.has_video && offloadInfo.is_streaming && !allowOffloadStreamingWithVideo) {
581 ALOGW("offload disabled by av.streaming.offload.enable %d",allowOffloadStreamingWithVideo);
582 return false;
Sharad Sangle36781612015-05-28 16:15:16 +0530583 }
584
585 //If duration is less than minimum value defined in property, return false
Ashish Jainac510a72016-04-28 12:22:58 +0530586 char propValue[PROPERTY_VALUE_MAX];
Sharad Sangle36781612015-05-28 16:15:16 +0530587 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
588 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
589 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
590 return false;
591 }
592 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
593 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
594 //duration checks only valid for MP3/AAC/ formats,
595 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
596 if ((offloadInfo.format == AUDIO_FORMAT_MP3) ||
597 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
598 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
Ashish Jainac510a72016-04-28 12:22:58 +0530599 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS))
600 return false;
601
602#ifdef AUDIO_EXTN_FORMATS_ENABLED
603 if (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) ||
Sharad Sangle36781612015-05-28 16:15:16 +0530604 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) ||
605 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) ||
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530606 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_APE) ||
Preetam Singh Ranawat191eb772016-09-09 17:10:19 +0530607 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_DSD) ||
Ashish Jainac510a72016-04-28 12:22:58 +0530608 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS))
Sharad Sangle36781612015-05-28 16:15:16 +0530609 return false;
Ashish Jainac510a72016-04-28 12:22:58 +0530610#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530611 }
612
613 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
614 // creating an offloaded track and tearing it down immediately after start when audioflinger
615 // detects there is an active non offloadable effect.
616 // FIXME: We should check the audio session here but we do not have it in this context.
617 // This may prevent offloading in rare situations where effects are left active by apps
618 // in the background.
619 if (mEffects.isNonOffloadableEffectEnabled()) {
620 return false;
621 }
Dhananjay Kumarcf558ba2016-07-29 19:33:13 +0530622
Sharad Sangle36781612015-05-28 16:15:16 +0530623 // See if there is a profile to support this.
624 // AUDIO_DEVICE_NONE
625 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
626 offloadInfo.sample_rate,
627 offloadInfo.format,
628 offloadInfo.channel_mask,
629 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
630 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
631 return (profile != 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700632}
Weiyin Jiangd211ef52016-02-16 15:55:11 +0800633
Sharad Sangle36781612015-05-28 16:15:16 +0530634audio_devices_t AudioPolicyManagerCustom::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
635 bool fromCache)
636{
637 audio_devices_t device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700638
Ashish Jain40cab3a2016-03-23 11:14:14 +0530639 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Sharad Sangle36781612015-05-28 16:15:16 +0530640 if (index >= 0) {
641 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
642 if (patchDesc->mUid != mUidCached) {
643 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Ashish Jain40cab3a2016-03-23 11:14:14 +0530644 outputDesc->device(), outputDesc->getPatchHandle());
Sharad Sangle36781612015-05-28 16:15:16 +0530645 return outputDesc->device();
646 }
647 }
648
649 // check the following by order of priority to request a routing change if necessary:
650 // 1: the strategy enforced audible is active and enforced on the output:
651 // use device for strategy enforced audible
652 // 2: we are in call or the strategy phone is active on the output:
653 // use device for strategy phone
654 // 3: the strategy for enforced audible is active but not enforced on the output:
655 // use the device for strategy enforced audible
656 // 4: the strategy sonification is active on the output:
657 // use device for strategy sonification
658 // 5: the strategy "respectful" sonification is active on the output:
659 // use device for strategy "respectful" sonification
660 // 6: the strategy accessibility is active on the output:
661 // use device for strategy accessibility
662 // 7: the strategy media is active on the output:
663 // use device for strategy media
664 // 8: the strategy DTMF is active on the output:
665 // use device for strategy DTMF
666 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
667 // use device for strategy t-t-s
668 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
669 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
670 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
671 } else if (isInCall() ||
672 isStrategyActive(outputDesc, STRATEGY_PHONE)||
673 isStrategyActive(mPrimaryOutput, STRATEGY_PHONE)) {
674 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
675 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
676 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
677 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)||
678 (isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION)
679 && (!isStrategyActive(mPrimaryOutput,STRATEGY_MEDIA)))) {
680 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530681 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL) ||
682 isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION_RESPECTFUL)) {
Sharad Sangle36781612015-05-28 16:15:16 +0530683 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
684 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
685 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
686 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
687 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
688 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
689 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
690 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
691 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
692 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
693 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
694 }
695
696 ALOGV("getNewOutputDevice() selected device %x", device);
697 return device;
698}
Weiyin Jiangd211ef52016-02-16 15:55:11 +0800699
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700700void AudioPolicyManagerCustom::setPhoneState(audio_mode_t state)
701{
Ashish Jain40cab3a2016-03-23 11:14:14 +0530702 ALOGD("setPhoneState() state %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530703 // store previous phone state for management of sonification strategy below
Sharad Sangle4509cef2015-08-19 20:47:12 +0530704 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Sharad Sangle36781612015-05-28 16:15:16 +0530705 int oldState = mEngine->getPhoneState();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700706
Sharad Sangle36781612015-05-28 16:15:16 +0530707 if (mEngine->setPhoneState(state) != NO_ERROR) {
708 ALOGW("setPhoneState() invalid or same state %d", state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700709 return;
710 }
Sharad Sangle36781612015-05-28 16:15:16 +0530711 /// Opens: can these line be executed after the switch of volume curves???
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700712 // if leaving call state, handle special case of active streams
713 // pertaining to sonification strategy see handleIncallSonification()
Zhou Song58081b62016-02-24 13:10:55 +0800714 if (isStateInCall(oldState)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700715 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530716 for (size_t j = 0; j < mOutputs.size(); j++) {
717 audio_io_handle_t curOutput = mOutputs.keyAt(j);
718 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
719 if (stream == AUDIO_STREAM_PATCH) {
720 continue;
721 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530722 handleIncallSonification((audio_stream_type_t)stream, false, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +0530723 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700724 }
Sharad Sangle36781612015-05-28 16:15:16 +0530725
Zhou Song58081b62016-02-24 13:10:55 +0800726 // force reevaluating accessibility routing when call stops
Sharad Sangle36781612015-05-28 16:15:16 +0530727 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700728 }
729
Sharad Sangle36781612015-05-28 16:15:16 +0530730 /**
731 * Switching to or from incall state or switching between telephony and VoIP lead to force
732 * routing command.
733 */
734 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
735 || (is_state_in_call(state) && (state != oldState)));
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700736
737 // check for device and output changes triggered by new phone state
738 checkA2dpSuspend();
739 checkOutputForAllStrategies();
740 updateDevicesAndOutputs();
741
Sharad Sangle36781612015-05-28 16:15:16 +0530742 sp<SwAudioOutputDescriptor> hwOutputDesc = mPrimaryOutput;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530743#ifdef VOICE_CONCURRENCY
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530744 char propValue[PROPERTY_VALUE_MAX];
745 bool prop_playback_enabled = false, prop_rec_enabled=false, prop_voip_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700746
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530747 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
748 prop_playback_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
749 }
750
751 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
752 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
753 }
754
755 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
756 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
757 }
758
Dhananjay Kumar9125b2a2015-12-01 16:24:09 +0530759 if ((AUDIO_MODE_IN_CALL != oldState) && (AUDIO_MODE_IN_CALL == state)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530760 ALOGD("voice_conc:Entering to call mode oldState :: %d state::%d ",
761 oldState, state);
Dhananjay Kumar9125b2a2015-12-01 16:24:09 +0530762 mvoice_call_state = state;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530763 if (prop_rec_enabled) {
764 //Close all active inputs
765 audio_io_handle_t activeInput = mInputs.getActiveInput();
766 if (activeInput != 0) {
767 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
768 switch(activeDesc->mInputSource) {
769 case AUDIO_SOURCE_VOICE_UPLINK:
770 case AUDIO_SOURCE_VOICE_DOWNLINK:
771 case AUDIO_SOURCE_VOICE_CALL:
772 ALOGD("voice_conc:FOUND active input during call active: %d",activeDesc->mInputSource);
773 break;
774
775 case AUDIO_SOURCE_VOICE_COMMUNICATION:
776 if(prop_voip_enabled) {
777 ALOGD("voice_conc:CLOSING VoIP input source on call setup :%d ",activeDesc->mInputSource);
778 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
779 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
780 }
781 break;
782
783 default:
784 ALOGD("voice_conc:CLOSING input on call setup for inputSource: %d",activeDesc->mInputSource);
785 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
786 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
787 break;
788 }
789 }
790 } else if (prop_voip_enabled) {
791 audio_io_handle_t activeInput = mInputs.getActiveInput();
792 if (activeInput != 0) {
793 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
794 if (AUDIO_SOURCE_VOICE_COMMUNICATION == activeDesc->mInputSource) {
795 ALOGD("voice_conc:CLOSING VoIP on call setup : %d",activeDesc->mInputSource);
796 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
797 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
798 }
799 }
800 }
801 if (prop_playback_enabled) {
802 // Move tracks associated to this strategy from previous output to new output
803 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
804 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
805 if (i == AUDIO_STREAM_PATCH) {
806 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
807 continue;
808 }
809 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
810 if ((AUDIO_STREAM_MUSIC == i) ||
811 (AUDIO_STREAM_VOICE_CALL == i) ) {
812 ALOGD("voice_conc:Invalidate stream type %d", i);
813 mpClientInterface->invalidateStream((audio_stream_type_t)i);
814 }
815 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
816 ALOGD("voice_conc:Invalidate stream type %d", i);
817 mpClientInterface->invalidateStream((audio_stream_type_t)i);
818 }
819 }
820 }
821
822 for (size_t i = 0; i < mOutputs.size(); i++) {
823 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
824 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
825 ALOGD("voice_conc:ouput desc / profile is NULL");
826 continue;
827 }
828
829 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
830 if (((!outputDesc->isDuplicated() &&outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY))
831 && prop_playback_enabled) {
832 ALOGD("voice_conc:calling suspendOutput on call mode for primary output");
833 mpClientInterface->suspendOutput(mOutputs.keyAt(i));
834 } //Close compress all sessions
835 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
836 && prop_playback_enabled) {
837 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
838 closeOutput(mOutputs.keyAt(i));
839 }
840 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_VOIP_RX)
841 && prop_voip_enabled) {
842 ALOGD("voice_conc:calling closeOutput on call mode for DIRECT output");
843 closeOutput(mOutputs.keyAt(i));
844 }
845 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
846 if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)
847 && prop_playback_enabled) {
848 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
849 closeOutput(mOutputs.keyAt(i));
850 }
851 }
852 }
Ashish Jaine4a22d52016-06-22 11:55:08 +0530853 // If effects where present on any of the above closed outputs,
854 // audioflinger moved them to the primary output by default
855 // move them back to the appropriate output.
856 moveGlobalEffect();
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530857 }
858
859 if ((AUDIO_MODE_IN_CALL == oldState || AUDIO_MODE_IN_COMMUNICATION == oldState) &&
860 (AUDIO_MODE_NORMAL == state) && prop_playback_enabled && mvoice_call_state) {
861 ALOGD("voice_conc:EXITING from call mode oldState :: %d state::%d \n",oldState, state);
862 mvoice_call_state = 0;
863 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
864 //restore PCM (deep-buffer) output after call termination
865 for (size_t i = 0; i < mOutputs.size(); i++) {
866 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
867 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
868 ALOGD("voice_conc:ouput desc / profile is NULL");
869 continue;
870 }
871 if (!outputDesc->isDuplicated() && outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
872 ALOGD("voice_conc:calling restoreOutput after call mode for primary output");
873 mpClientInterface->restoreOutput(mOutputs.keyAt(i));
874 }
875 }
876 }
877 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
878 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
879 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
880 if (i == AUDIO_STREAM_PATCH) {
881 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
882 continue;
883 }
884 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
885 if ((AUDIO_STREAM_MUSIC == i) ||
886 (AUDIO_STREAM_VOICE_CALL == i) ) {
887 mpClientInterface->invalidateStream((audio_stream_type_t)i);
888 }
889 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
890 mpClientInterface->invalidateStream((audio_stream_type_t)i);
891 }
892 }
893 }
894
895#endif
Preetam Singh Ranawat96afc162016-08-25 14:35:02 +0530896
897 sp<SwAudioOutputDescriptor> outputDesc = NULL;
898 for (size_t i = 0; i < mOutputs.size(); i++) {
899 outputDesc = mOutputs.valueAt(i);
900 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
901 ALOGD("voice_conc:ouput desc / profile is NULL");
902 continue;
903 }
904
905 if (property_get_bool("voice.dsd.playback.conc.disabled", true) &&
906 (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
907 (outputDesc->mFormat == AUDIO_FORMAT_DSD)) {
908 ALOGD("voice_conc:calling closeOutput on call mode for DSD COMPRESS output");
909 closeOutput(mOutputs.keyAt(i));
910 // call invalidate for music, so that DSD compress will fallback to deep-buffer.
911 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
912 }
913
914 }
915
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530916#ifdef RECORD_PLAY_CONCURRENCY
917 char recConcPropValue[PROPERTY_VALUE_MAX];
918 bool prop_rec_play_enabled = false;
919
920 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
921 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
922 }
923 if (prop_rec_play_enabled) {
924 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
925 ALOGD("phone state changed to MODE_IN_COMM invlaidating music and voice streams");
926 // call invalidate for voice streams, so that it can use deepbuffer with VoIP out device from HAL
927 mpClientInterface->invalidateStream(AUDIO_STREAM_VOICE_CALL);
928 // call invalidate for music, so that compress will fallback to deep-buffer with VoIP out device
929 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
930
931 // close compress output to make sure session will be closed before timeout(60sec)
932 for (size_t i = 0; i < mOutputs.size(); i++) {
933
934 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
935 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
936 ALOGD("ouput desc / profile is NULL");
937 continue;
938 }
939
940 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
941 ALOGD("calling closeOutput on call mode for COMPRESS output");
942 closeOutput(mOutputs.keyAt(i));
943 }
944 }
Ashish Jaine4a22d52016-06-22 11:55:08 +0530945 // If effects where present on any of the above closed outputs,
946 // audioflinger moved them to the primary output by default
947 // move them back to the appropriate output.
948 moveGlobalEffect();
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530949 } else if ((oldState == AUDIO_MODE_IN_COMMUNICATION) &&
950 (mEngine->getPhoneState() == AUDIO_MODE_NORMAL)) {
951 // call invalidate for music so that music can fallback to compress
952 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
953 }
954 }
955#endif
956 mPrevPhoneState = oldState;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700957 int delayMs = 0;
958 if (isStateInCall(state)) {
959 nsecs_t sysTime = systemTime();
960 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530961 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700962 // mute media and sonification strategies and delay device switch by the largest
963 // latency of any output where either strategy is active.
964 // This avoid sending the ring tone or music tail into the earpiece or headset.
Sharad Sangle36781612015-05-28 16:15:16 +0530965 if ((isStrategyActive(desc, STRATEGY_MEDIA,
966 SONIFICATION_HEADSET_MUSIC_DELAY,
967 sysTime) ||
968 isStrategyActive(desc, STRATEGY_SONIFICATION,
969 SONIFICATION_HEADSET_MUSIC_DELAY,
970 sysTime)) &&
971 (delayMs < (int)desc->latency()*2)) {
972 delayMs = desc->latency()*2;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700973 }
Sharad Sangle36781612015-05-28 16:15:16 +0530974 setStrategyMute(STRATEGY_MEDIA, true, desc);
975 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700976 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Sharad Sangle36781612015-05-28 16:15:16 +0530977 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
978 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700979 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
980 }
981 }
982
Sharad Sangle36781612015-05-28 16:15:16 +0530983 if (hasPrimaryOutput()) {
984 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
985 // the device returned is not necessarily reachable via this output
986 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
987 // force routing command to audio hardware when ending call
988 // even if no device change is needed
989 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
990 rxDevice = mPrimaryOutput->device();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700991 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700992
Sharad Sangle36781612015-05-28 16:15:16 +0530993 if (state == AUDIO_MODE_IN_CALL) {
994 updateCallRouting(rxDevice, delayMs);
995 } else if (oldState == AUDIO_MODE_IN_CALL) {
996 if (mCallRxPatch != 0) {
997 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
998 mCallRxPatch.clear();
999 }
1000 if (mCallTxPatch != 0) {
1001 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
1002 mCallTxPatch.clear();
1003 }
1004 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
1005 } else {
1006 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001007 }
1008 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301009 //update device for all non-primary outputs
1010 for (size_t i = 0; i < mOutputs.size(); i++) {
1011 audio_io_handle_t output = mOutputs.keyAt(i);
1012 if (output != mPrimaryOutput->mIoHandle) {
1013 newDevice = getNewOutputDevice(mOutputs.valueFor(output), false /*fromCache*/);
1014 setOutputDevice(mOutputs.valueFor(output), newDevice, (newDevice != AUDIO_DEVICE_NONE));
1015 }
1016 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001017 // if entering in call state, handle special case of active streams
1018 // pertaining to sonification strategy see handleIncallSonification()
1019 if (isStateInCall(state)) {
1020 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +05301021 for (size_t j = 0; j < mOutputs.size(); j++) {
1022 audio_io_handle_t curOutput = mOutputs.keyAt(j);
1023 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
1024 if (stream == AUDIO_STREAM_PATCH) {
1025 continue;
1026 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301027 handleIncallSonification((audio_stream_type_t)stream, true, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +05301028 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001029 }
Zhou Song58081b62016-02-24 13:10:55 +08001030
1031 // force reevaluating accessibility routing when call starts
1032 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001033 }
1034
1035 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
1036 if (state == AUDIO_MODE_RINGTONE &&
1037 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
1038 mLimitRingtoneVolume = true;
1039 } else {
1040 mLimitRingtoneVolume = false;
1041 }
1042}
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301043
1044void AudioPolicyManagerCustom::setForceUse(audio_policy_force_use_t usage,
1045 audio_policy_forced_cfg_t config)
1046{
Ashish Jain40cab3a2016-03-23 11:14:14 +05301047 ALOGD("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301048
1049 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
1050 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
1051 return;
1052 }
1053 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
1054 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
1055 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
1056
1057 // check for device and output changes triggered by new force usage
1058 checkA2dpSuspend();
1059 checkOutputForAllStrategies();
1060 updateDevicesAndOutputs();
Ashish Jain40cab3a2016-03-23 11:14:14 +05301061
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301062 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
1063 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
1064 updateCallRouting(newDevice);
1065 }
1066 // Use reverse loop to make sure any low latency usecases (generally tones)
1067 // are not routed before non LL usecases (generally music).
1068 // We can safely assume that LL output would always have lower index,
1069 // and use this work-around to avoid routing of output with music stream
1070 // from the context of short lived LL output.
1071 // Note: in case output's share backend(HAL sharing is implicit) all outputs
1072 // gets routing update while processing first output itself.
1073 for (size_t i = mOutputs.size(); i > 0; i--) {
1074 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i-1);
1075 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
1076 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || outputDesc != mPrimaryOutput) {
1077 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
1078 }
1079 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
1080 applyStreamVolumes(outputDesc, newDevice, 0, true);
1081 }
1082 }
1083
1084 audio_io_handle_t activeInput = mInputs.getActiveInput();
1085 if (activeInput != 0) {
Ashish Jain40cab3a2016-03-23 11:14:14 +05301086 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
1087 audio_devices_t newDevice = getNewInputDevice(activeInput);
1088 // Force new input selection if the new device can not be reached via current input
1089 if (activeDesc->mProfile->getSupportedDevices().types() & (newDevice & ~AUDIO_DEVICE_BIT_IN)) {
1090 setInputDevice(activeInput, newDevice);
1091 } else {
1092 closeInput(activeInput);
1093 }
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301094 }
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301095}
1096
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301097status_t AudioPolicyManagerCustom::stopSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301098 audio_stream_type_t stream,
1099 bool forceDeviceUpdate)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001100{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301101 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1102 ALOGW("stopSource() invalid stream %d", stream);
1103 return INVALID_OPERATION;
1104 }
Sharad Sangle36781612015-05-28 16:15:16 +05301105 // always handle stream stop, check which stream type is stopping
1106 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001107
Sharad Sangle36781612015-05-28 16:15:16 +05301108 // handle special case for sonification while in call
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001109 if (isInCall()) {
Sharad Sangle36781612015-05-28 16:15:16 +05301110 if (outputDesc->isDuplicated()) {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301111 handleIncallSonification(stream, false, false, outputDesc->subOutput1()->mIoHandle);
1112 handleIncallSonification(stream, false, false, outputDesc->subOutput2()->mIoHandle);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001113 }
Sharad Sangle36781612015-05-28 16:15:16 +05301114 handleIncallSonification(stream, false, false, outputDesc->mIoHandle);
1115 }
1116
1117 if (outputDesc->mRefCount[stream] > 0) {
1118 // decrement usage count of this stream on the output
1119 outputDesc->changeRefCount(stream, -1);
1120
1121 // store time at which the stream was stopped - see isStreamActive()
1122 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
1123 outputDesc->mStopTime[stream] = systemTime();
Zhou Song5dcddc92015-09-21 14:36:57 +08001124 audio_devices_t prevDevice = outputDesc->device();
Sharad Sangle36781612015-05-28 16:15:16 +05301125 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
1126 // delay the device switch by twice the latency because stopOutput() is executed when
1127 // the track stop() command is received and at that time the audio track buffer can
1128 // still contain data that needs to be drained. The latency only covers the audio HAL
1129 // and kernel buffers. Also the latency does not always include additional delay in the
1130 // audio path (audio DSP, CODEC ...)
1131 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
1132
1133 // force restoring the device selection on other active outputs if it differs from the
1134 // one being selected for this output
1135 for (size_t i = 0; i < mOutputs.size(); i++) {
1136 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1137 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1138 if (desc != outputDesc &&
1139 desc->isActive() &&
1140 outputDesc->sharesHwModuleWith(desc) &&
1141 (newDevice != desc->device())) {
Sharad Sangle4509cef2015-08-19 20:47:12 +05301142 audio_devices_t dev = getNewOutputDevice(mOutputs.valueFor(curOutput), false /*fromCache*/);
Naresh Tanniru11393702016-09-14 11:25:37 +05301143 bool force = desc->device() != dev;
Zhou Song5dcddc92015-09-21 14:36:57 +08001144 uint32_t delayMs;
1145 if (dev == prevDevice) {
1146 delayMs = 0;
1147 } else {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301148 delayMs = outputDesc->latency()*2;
Zhou Song5dcddc92015-09-21 14:36:57 +08001149 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301150 setOutputDevice(desc,
1151 dev,
Naresh Tanniru11393702016-09-14 11:25:37 +05301152 force,
Zhou Song5dcddc92015-09-21 14:36:57 +08001153 delayMs);
Sharad Sangle36781612015-05-28 16:15:16 +05301154 }
1155 }
1156 // update the outputs if stopping one with a stream that can affect notification routing
1157 handleNotificationRoutingForStream(stream);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001158 }
Sharad Sangle36781612015-05-28 16:15:16 +05301159 return NO_ERROR;
1160 } else {
1161 ALOGW("stopOutput() refcount is already 0");
1162 return INVALID_OPERATION;
1163 }
1164}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001165
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301166status_t AudioPolicyManagerCustom::startSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301167 audio_stream_type_t stream,
1168 audio_devices_t device,
Lalit Kansaraf803ac52016-05-09 19:12:01 +05301169 const char *address,
Sharad Sangle36781612015-05-28 16:15:16 +05301170 uint32_t *delayMs)
1171{
1172 // cannot start playback of STREAM_TTS if any other output is being used
1173 uint32_t beaconMuteLatency = 0;
1174
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301175 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1176 ALOGW("startSource() invalid stream %d", stream);
1177 return INVALID_OPERATION;
1178 }
1179
Sharad Sangle36781612015-05-28 16:15:16 +05301180 *delayMs = 0;
1181 if (stream == AUDIO_STREAM_TTS) {
1182 ALOGV("\t found BEACON stream");
Ashish Jain40cab3a2016-03-23 11:14:14 +05301183 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301184 return INVALID_OPERATION;
1185 } else {
1186 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001187 }
Sharad Sangle36781612015-05-28 16:15:16 +05301188 } else {
1189 // some playback other than beacon starts
1190 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1191 }
1192
Ashish Jain40cab3a2016-03-23 11:14:14 +05301193 // check active before incrementing usage count
1194 bool force = !outputDesc->isActive();
1195
Sharad Sangle36781612015-05-28 16:15:16 +05301196 // increment usage count for this stream on the requested output:
1197 // NOTE that the usage count is the same for duplicated output and hardware output which is
1198 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1199 outputDesc->changeRefCount(stream, 1);
1200
1201 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
1202 // starting an output being rerouted?
1203 if (device == AUDIO_DEVICE_NONE) {
1204 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001205 }
Sharad Sangle36781612015-05-28 16:15:16 +05301206 routing_strategy strategy = getStrategy(stream);
1207 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
1208 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1209 (beaconMuteLatency > 0);
1210 uint32_t waitMs = beaconMuteLatency;
Sharad Sangle36781612015-05-28 16:15:16 +05301211 for (size_t i = 0; i < mOutputs.size(); i++) {
1212 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1213 if (desc != outputDesc) {
1214 // force a device change if any other output is managed by the same hw
1215 // module and has a current device selection that differs from selected device.
1216 // In this case, the audio HAL must receive the new device selection so that it can
1217 // change the device currently selected by the other active output.
1218 if (outputDesc->sharesHwModuleWith(desc) &&
1219 desc->device() != device) {
1220 force = true;
1221 }
1222 // wait for audio on other active outputs to be presented when starting
1223 // a notification so that audio focus effect can propagate, or that a mute/unmute
1224 // event occurred for beacon
1225 uint32_t latency = desc->latency();
1226 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1227 waitMs = latency;
1228 }
1229 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001230 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05301231 uint32_t muteWaitMs;
Lalit Kansaraf803ac52016-05-09 19:12:01 +05301232 muteWaitMs = setOutputDevice(outputDesc, device, force, 0, NULL, address);
Sharad Sangle36781612015-05-28 16:15:16 +05301233
1234 // handle special case for sonification while in call
1235 if (isInCall()) {
1236 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001237 }
Sharad Sangle36781612015-05-28 16:15:16 +05301238
1239 // apply volume rules for current stream and device if necessary
1240 checkAndSetVolume(stream,
Ashish Jain40cab3a2016-03-23 11:14:14 +05301241 mVolumeCurves->getVolumeIndex(stream, device),
Sharad Sangle36781612015-05-28 16:15:16 +05301242 outputDesc,
1243 device);
1244
1245 // update the outputs if starting an output with a stream that can affect notification
1246 // routing
1247 handleNotificationRoutingForStream(stream);
1248
1249 // force reevaluating accessibility routing when ringtone or alarm starts
1250 if (strategy == STRATEGY_SONIFICATION) {
1251 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1252 }
1253 }
1254 else {
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001255 // handle special case for sonification while in call
1256 if (isInCall()) {
1257 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Sharad Sangle36781612015-05-28 16:15:16 +05301258 }
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001259 }
Sharad Sangle36781612015-05-28 16:15:16 +05301260 return NO_ERROR;
1261}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001262
Sharad Sangle36781612015-05-28 16:15:16 +05301263void AudioPolicyManagerCustom::handleIncallSonification(audio_stream_type_t stream,
1264 bool starting, bool stateChange,
1265 audio_io_handle_t output)
1266{
1267 if(!hasPrimaryOutput()) {
1268 return;
1269 }
1270 // no action needed for AUDIO_STREAM_PATCH stream type, it's for internal flinger tracks
1271 if (stream == AUDIO_STREAM_PATCH) {
1272 return;
1273 }
1274 // if the stream pertains to sonification strategy and we are in call we must
1275 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
1276 // in the device used for phone strategy and play the tone if the selected device does not
1277 // interfere with the device used for phone strategy
1278 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
1279 // many times as there are active tracks on the output
1280 const routing_strategy stream_strategy = getStrategy(stream);
1281 if ((stream_strategy == STRATEGY_SONIFICATION) ||
1282 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
1283 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
1284 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
1285 stream, starting, outputDesc->mDevice, stateChange);
1286 if (outputDesc->mRefCount[stream]) {
1287 int muteCount = 1;
1288 if (stateChange) {
1289 muteCount = outputDesc->mRefCount[stream];
1290 }
1291 if (audio_is_low_visibility(stream)) {
1292 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
1293 for (int i = 0; i < muteCount; i++) {
1294 setStreamMute(stream, starting, outputDesc);
1295 }
1296 } else {
1297 ALOGV("handleIncallSonification() high visibility");
1298 if (outputDesc->device() &
1299 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
1300 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
1301 for (int i = 0; i < muteCount; i++) {
1302 setStreamMute(stream, starting, outputDesc);
1303 }
1304 }
1305 if (starting) {
1306 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
1307 AUDIO_STREAM_VOICE_CALL);
1308 } else {
1309 mpClientInterface->stopTone();
1310 }
1311 }
1312 }
1313 }
1314}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001315
Sharad Sangle36781612015-05-28 16:15:16 +05301316void AudioPolicyManagerCustom::handleNotificationRoutingForStream(audio_stream_type_t stream) {
1317 switch(stream) {
1318 case AUDIO_STREAM_MUSIC:
1319 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
1320 updateDevicesAndOutputs();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001321 break;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001322 default:
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001323 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001324 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001325}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001326
Sharad Sangle36781612015-05-28 16:15:16 +05301327status_t AudioPolicyManagerCustom::checkAndSetVolume(audio_stream_type_t stream,
1328 int index,
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301329 const sp<AudioOutputDescriptor>& outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301330 audio_devices_t device,
1331 int delayMs, bool force)
1332{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301333 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1334 ALOGW("checkAndSetVolume() invalid stream %d", stream);
1335 return INVALID_OPERATION;
1336 }
Sharad Sangle36781612015-05-28 16:15:16 +05301337 // do not change actual stream volume if the stream is muted
1338 if (outputDesc->mMuteCount[stream] != 0) {
1339 ALOGVV("checkAndSetVolume() stream %d muted count %d",
1340 stream, outputDesc->mMuteCount[stream]);
1341 return NO_ERROR;
1342 }
1343 audio_policy_forced_cfg_t forceUseForComm =
1344 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
1345 // do not change in call volume if bluetooth is connected and vice versa
1346 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
1347 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
1348 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
1349 stream, forceUseForComm);
1350 return INVALID_OPERATION;
1351 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001352
Sharad Sangle36781612015-05-28 16:15:16 +05301353 if (device == AUDIO_DEVICE_NONE) {
1354 device = outputDesc->device();
1355 }
1356
1357 float volumeDb = computeVolume(stream, index, device);
1358 if (outputDesc->isFixedVolume(device)) {
1359 volumeDb = 0.0f;
1360 }
1361
1362 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
1363
1364 if (stream == AUDIO_STREAM_VOICE_CALL ||
1365 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
1366 float voiceVolume;
1367 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
1368 if (stream == AUDIO_STREAM_VOICE_CALL) {
Ashish Jain40cab3a2016-03-23 11:14:14 +05301369 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Sharad Sangle36781612015-05-28 16:15:16 +05301370 } else {
1371 voiceVolume = 1.0;
1372 }
1373
1374 if (voiceVolume != mLastVoiceVolume && ((outputDesc == mPrimaryOutput) ||
1375 isDirectOutput(outputDesc->mIoHandle) || device & AUDIO_DEVICE_OUT_ALL_USB)) {
1376 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
1377 mLastVoiceVolume = voiceVolume;
1378 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301379#ifdef FM_POWER_OPT
1380 } else if (stream == AUDIO_STREAM_MUSIC && hasPrimaryOutput() &&
Haynes Mathew George603ae9b2015-12-21 17:23:59 -08001381 outputDesc == mPrimaryOutput && mFMIsActive) {
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001382 /* Avoid unnecessary set_parameter calls as it puts the primary
1383 outputs FastMixer in HOT_IDLE leading to breaks in audio */
1384 if (volumeDb != mPrevFMVolumeDb) {
1385 mPrevFMVolumeDb = volumeDb;
1386 AudioParameter param = AudioParameter();
1387 param.addFloat(String8("fm_volume"), Volume::DbToAmpl(volumeDb));
Mingming Yin1433cc62016-01-04 14:58:00 -08001388 //Double delayMs to avoid sound burst while device switch.
1389 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString(), delayMs*2);
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001390 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301391#endif /* FM_POWER_OPT end */
Sharad Sangle36781612015-05-28 16:15:16 +05301392 }
1393
1394 return NO_ERROR;
1395}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001396
Sharad Sangle36781612015-05-28 16:15:16 +05301397bool AudioPolicyManagerCustom::isDirectOutput(audio_io_handle_t output) {
1398 for (size_t i = 0; i < mOutputs.size(); i++) {
1399 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1400 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1401 if ((curOutput == output) && (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
1402 return true;
1403 }
1404 }
1405 return false;
1406}
vivek mehta0ea887a2015-08-26 14:01:20 -07001407
Ashish Jainac510a72016-04-28 12:22:58 +05301408bool static isDirectPCMEnabled(int bitWidth)
1409{
1410 bool directPCMEnabled = false;
1411 if (bitWidth == 24 || bitWidth == 32)
1412 directPCMEnabled =
1413 property_get_bool("audio.offload.pcm.24bit.enable", false);
1414 else
1415 directPCMEnabled =
1416 property_get_bool("audio.offload.pcm.16bit.enable", false);
1417
1418 return directPCMEnabled;
1419}
1420
vivek mehta0ea887a2015-08-26 14:01:20 -07001421status_t AudioPolicyManagerCustom::getOutputForAttr(const audio_attributes_t *attr,
1422 audio_io_handle_t *output,
1423 audio_session_t session,
1424 audio_stream_type_t *stream,
1425 uid_t uid,
1426 uint32_t samplingRate,
1427 audio_format_t format,
1428 audio_channel_mask_t channelMask,
1429 audio_output_flags_t flags,
1430 audio_port_handle_t selectedDeviceId,
1431 const audio_offload_info_t *offloadInfo)
1432{
1433 audio_offload_info_t tOffloadInfo = AUDIO_INFO_INITIALIZER;
1434
Alexy Joseph0ac09032015-10-16 15:57:53 -07001435 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
Ashish Jainac510a72016-04-28 12:22:58 +05301436 uint32_t bitWidth = (audio_bytes_per_sample(format) * 8);
Alexy Joseph0ac09032015-10-16 15:57:53 -07001437
1438 if (offloadDisabled) {
1439 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
1440 }
1441
Ashish Jainac510a72016-04-28 12:22:58 +05301442 if (!offloadDisabled && (offloadInfo == NULL) &&
1443 isDirectPCMEnabled(bitWidth) &&
1444 (flags == AUDIO_OUTPUT_FLAG_NONE)) {
vivek mehta0ea887a2015-08-26 14:01:20 -07001445
vivek mehta0ea887a2015-08-26 14:01:20 -07001446 tOffloadInfo.sample_rate = samplingRate;
1447 tOffloadInfo.channel_mask = channelMask;
1448 tOffloadInfo.format = format;
1449 tOffloadInfo.stream_type = *stream;
Ashish Jainac510a72016-04-28 12:22:58 +05301450 tOffloadInfo.bit_width = bitWidth;
vivek mehta0ea887a2015-08-26 14:01:20 -07001451 if (attr != NULL) {
1452 ALOGV("found attribute .. setting usage %d ", attr->usage);
1453 tOffloadInfo.usage = attr->usage;
1454 } else {
Alexy Joseph0ac09032015-10-16 15:57:53 -07001455 ALOGI("%s:: attribute is NULL .. no usage set", __func__);
vivek mehta0ea887a2015-08-26 14:01:20 -07001456 }
1457 offloadInfo = &tOffloadInfo;
1458 }
1459
1460 return AudioPolicyManager::getOutputForAttr(attr, output, session, stream,
1461 (uid_t)uid, (uint32_t)samplingRate,
1462 format, (audio_channel_mask_t)channelMask,
1463 flags, (audio_port_handle_t)selectedDeviceId,
1464 offloadInfo);
1465}
1466
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001467audio_io_handle_t AudioPolicyManagerCustom::getOutputForDevice(
1468 audio_devices_t device,
Sharad Sangle36781612015-05-28 16:15:16 +05301469 audio_session_t session __unused,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001470 audio_stream_type_t stream,
1471 uint32_t samplingRate,
1472 audio_format_t format,
1473 audio_channel_mask_t channelMask,
1474 audio_output_flags_t flags,
1475 const audio_offload_info_t *offloadInfo)
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001476{
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001477 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001478 status_t status;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001479
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001480#ifdef AUDIO_POLICY_TEST
1481 if (mCurOutput != 0) {
1482 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1483 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1484
1485 if (mTestOutputs[mCurOutput] == 0) {
1486 ALOGV("getOutput() opening test output");
Sharad Sangle36781612015-05-28 16:15:16 +05301487 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
1488 mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001489 outputDesc->mDevice = mTestDevice;
1490 outputDesc->mLatency = mTestLatencyMs;
1491 outputDesc->mFlags =
1492 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
1493 outputDesc->mRefCount[stream] = 0;
1494 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1495 config.sample_rate = mTestSamplingRate;
1496 config.channel_mask = mTestChannels;
1497 config.format = mTestFormat;
1498 if (offloadInfo != NULL) {
1499 config.offload_info = *offloadInfo;
1500 }
1501 status = mpClientInterface->openOutput(0,
1502 &mTestOutputs[mCurOutput],
1503 &config,
1504 &outputDesc->mDevice,
1505 String8(""),
1506 &outputDesc->mLatency,
1507 outputDesc->mFlags);
1508 if (status == NO_ERROR) {
1509 outputDesc->mSamplingRate = config.sample_rate;
1510 outputDesc->mFormat = config.format;
1511 outputDesc->mChannelMask = config.channel_mask;
1512 AudioParameter outputCmd = AudioParameter();
1513 outputCmd.addInt(String8("set_id"),mCurOutput);
1514 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1515 addOutput(mTestOutputs[mCurOutput], outputDesc);
1516 }
1517 }
1518 return mTestOutputs[mCurOutput];
1519 }
1520#endif //AUDIO_POLICY_TEST
Sharad Sangle36781612015-05-28 16:15:16 +05301521 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1522 (stream != AUDIO_STREAM_MUSIC)) {
1523 // compress should not be used for non-music streams
1524 ALOGE("Offloading only allowed with music stream");
1525 return 0;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301526 }
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301527
1528 if ((stream == AUDIO_STREAM_VOICE_CALL) &&
1529 (channelMask == 1) &&
Shiv Maliyappanahallid77d3b32016-02-02 13:45:50 -08001530 (samplingRate == 8000 || samplingRate == 16000 ||
1531 samplingRate == 32000 || samplingRate == 48000)) {
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301532 // Allow Voip direct output only if:
1533 // audio mode is MODE_IN_COMMUNCATION; AND
1534 // voip output is not opened already; AND
1535 // requested sample rate matches with that of voip input stream (if opened already)
1536 int value = 0;
1537 uint32_t mode = 0, voipOutCount = 1, voipSampleRate = 1;
1538 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1539 String8("audio_mode"));
1540 AudioParameter result = AudioParameter(valueStr);
1541 if (result.getInt(String8("audio_mode"), value) == NO_ERROR) {
1542 mode = value;
1543 }
1544
1545 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1546 String8("voip_out_stream_count"));
1547 result = AudioParameter(valueStr);
1548 if (result.getInt(String8("voip_out_stream_count"), value) == NO_ERROR) {
1549 voipOutCount = value;
1550 }
1551
1552 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1553 String8("voip_sample_rate"));
1554 result = AudioParameter(valueStr);
1555 if (result.getInt(String8("voip_sample_rate"), value) == NO_ERROR) {
1556 voipSampleRate = value;
1557 }
1558
1559 if ((mode == AUDIO_MODE_IN_COMMUNICATION) && (voipOutCount == 0) &&
1560 ((voipSampleRate == 0) || (voipSampleRate == samplingRate))) {
1561 if (audio_is_linear_pcm(format)) {
1562 char propValue[PROPERTY_VALUE_MAX] = {0};
1563 property_get("use.voice.path.for.pcm.voip", propValue, "0");
1564 bool voipPcmSysPropEnabled = !strncmp("true", propValue, sizeof("true"));
1565 if (voipPcmSysPropEnabled && (format == AUDIO_FORMAT_PCM_16_BIT)) {
Karthik Reddy Katta3fb9be72015-11-25 11:18:27 +05301566 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX |
1567 AUDIO_OUTPUT_FLAG_DIRECT);
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301568 ALOGD("Set VoIP and Direct output flags for PCM format");
1569 }
1570 }
1571 }
1572 }
1573
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301574#ifdef VOICE_CONCURRENCY
1575 char propValue[PROPERTY_VALUE_MAX];
1576 bool prop_play_enabled=false, prop_voip_enabled = false;
1577
1578 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1579 prop_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001580 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301581
1582 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1583 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1584 }
1585
1586 if (prop_play_enabled && mvoice_call_state) {
1587 //check if voice call is active / running in background
1588 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1589 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1590 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1591 {
1592 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1593 if(prop_voip_enabled) {
1594 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1595 flags );
1596 return 0;
1597 }
1598 }
1599 else {
1600 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
1601 ALOGD("voice_conc:IN call mode adding ULL flags .. flags: %x ", flags );
1602 flags = AUDIO_OUTPUT_FLAG_FAST;
1603 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
1604 if (AUDIO_STREAM_MUSIC == stream) {
1605 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1606 ALOGD("voice_conc:IN call mode adding deep-buffer flags %x ", flags );
1607 }
1608 else {
1609 flags = AUDIO_OUTPUT_FLAG_FAST;
1610 ALOGD("voice_conc:IN call mode adding fast flags %x ", flags );
1611 }
1612 }
1613 }
1614 }
1615 } else if (prop_voip_enabled && mvoice_call_state) {
1616 //check if voice call is active / running in background
1617 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1618 //return only ULL ouput
1619 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1620 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1621 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1622 {
1623 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1624 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1625 flags );
1626 return 0;
1627 }
1628 }
1629 }
1630#endif
1631#ifdef RECORD_PLAY_CONCURRENCY
1632 char recConcPropValue[PROPERTY_VALUE_MAX];
1633 bool prop_rec_play_enabled = false;
1634
1635 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
1636 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
1637 }
1638 if ((prop_rec_play_enabled) &&
1639 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
1640 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
1641 if (AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1642 // allow VoIP using voice path
1643 // Do nothing
1644 } else if((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1645 ALOGD("voice_conc:MODE_IN_COMM is setforcing deep buffer output for non ULL... flags: %x", flags);
1646 // use deep buffer path for all non ULL outputs
1647 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1648 }
1649 } else if ((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1650 ALOGD("voice_conc:Record mode is on forcing deep buffer output for non ULL... flags: %x ", flags);
1651 // use deep buffer path for all non ULL outputs
1652 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1653 }
1654 }
1655 if (prop_rec_play_enabled &&
1656 (stream == AUDIO_STREAM_ENFORCED_AUDIBLE)) {
1657 ALOGD("Record conc is on forcing ULL output for ENFORCED_AUDIBLE");
1658 flags = AUDIO_OUTPUT_FLAG_FAST;
1659 }
1660#endif
1661
Sharad Sangle4509cef2015-08-19 20:47:12 +05301662#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Sharad Sangle36781612015-05-28 16:15:16 +05301663 /*
1664 * WFD audio routes back to target speaker when starting a ringtone playback.
1665 * This is because primary output is reused for ringtone, so output device is
1666 * updated based on SONIFICATION strategy for both ringtone and music playback.
1667 * The same issue is not seen on remoted_submix HAL based WFD audio because
1668 * primary output is not reused and a new output is created for ringtone playback.
1669 * Issue is fixed by updating output flag to AUDIO_OUTPUT_FLAG_FAST when there is
1670 * a non-music stream playback on WFD, so primary output is not reused for ringtone.
1671 */
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001672 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
1673 if ((availableOutputDeviceTypes & AUDIO_DEVICE_OUT_PROXY)
1674 && (stream != AUDIO_STREAM_MUSIC)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301675 ALOGD("WFD audio: use OUTPUT_FLAG_FAST for non music stream. flags:%x", flags );
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001676 //For voip paths
1677 if(flags & AUDIO_OUTPUT_FLAG_DIRECT)
1678 flags = AUDIO_OUTPUT_FLAG_DIRECT;
1679 else //route every thing else to ULL path
1680 flags = AUDIO_OUTPUT_FLAG_FAST;
1681 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301682#endif
1683
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001684 // open a direct output if required by specified parameters
vivek mehta0ea887a2015-08-26 14:01:20 -07001685 // force direct flag if offload flag is set: offloading implies a direct output stream
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001686 // and all common behaviors are driven by checking only the direct flag
1687 // this should normally be set appropriately in the policy configuration file
1688 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1689 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1690 }
1691 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1692 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1693 }
vivek mehta0ea887a2015-08-26 14:01:20 -07001694
vivek mehta4b0d8192015-10-16 00:25:59 -07001695 bool forced_deep = false;
Sharad Sangle36781612015-05-28 16:15:16 +05301696 // only allow deep buffering for music stream type
1697 if (stream != AUDIO_STREAM_MUSIC) {
1698 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Sharad Sangle497aef82015-08-03 17:55:48 +05301699 } else if (/* stream == AUDIO_STREAM_MUSIC && */
1700 flags == AUDIO_OUTPUT_FLAG_NONE &&
1701 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
vivek mehtac984b992015-11-25 13:28:55 -08001702 forced_deep = true;
Sharad Sangle36781612015-05-28 16:15:16 +05301703 }
Sharad Sangle497aef82015-08-03 17:55:48 +05301704
Sharad Sangle36781612015-05-28 16:15:16 +05301705 if (stream == AUDIO_STREAM_TTS) {
1706 flags = AUDIO_OUTPUT_FLAG_TTS;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001707 }
1708
Zhou Song2b3d1552016-03-21 17:21:04 +08001709 // check if direct output for track offload already exits
1710 bool is_track_offload_active = false;
1711 for (size_t i = 0; i < mOutputs.size(); i++) {
1712 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1713 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) {
1714 is_track_offload_active = true;
1715 ALOGD("Track offload already active");
1716 break;
1717 }
1718 }
1719
vivek mehta4b0d8192015-10-16 00:25:59 -07001720 // Do offload magic here
vivek mehtac984b992015-11-25 13:28:55 -08001721 if ((flags == AUDIO_OUTPUT_FLAG_NONE) &&
1722 (stream == AUDIO_STREAM_MUSIC) &&
Zhou Song2b3d1552016-03-21 17:21:04 +08001723 (offloadInfo != NULL) && !is_track_offload_active &&
vivek mehta4b0d8192015-10-16 00:25:59 -07001724 ((offloadInfo->usage == AUDIO_USAGE_MEDIA) || (offloadInfo->usage == AUDIO_USAGE_GAME))) {
vivek mehtac984b992015-11-25 13:28:55 -08001725 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DIRECT_PCM);
vivek mehta4b0d8192015-10-16 00:25:59 -07001726 ALOGD("AudioCustomHAL --> Force Direct Flag .. flag (0x%x)", flags);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301727 }
1728
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001729 sp<IOProfile> profile;
1730
1731 // skip direct output selection if the request can obviously be attached to a mixed output
1732 // and not explicitly requested
vivek mehtac984b992015-11-25 13:28:55 -08001733 if (((flags & (AUDIO_OUTPUT_FLAG_DIRECT|AUDIO_OUTPUT_FLAG_DIRECT_PCM)) == 0) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301734 audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX &&
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001735 audio_channel_count_from_out_mask(channelMask) <= 2) {
1736 goto non_direct_output;
1737 }
1738
1739 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1740 // creating an offloaded track and tearing it down immediately after start when audioflinger
1741 // detects there is an active non offloadable effect.
1742 // FIXME: We should check the audio session here but we do not have it in this context.
1743 // This may prevent offloading in rare situations where effects are left active by apps
1744 // in the background.
Weiyin Jiang6bba3202016-03-03 14:49:36 +08001745 //
1746 // Supplementary annotation:
1747 // For sake of track offload introduced, we need a rollback for both compress offload
1748 // and track offload use cases.
1749 if ((flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD|AUDIO_OUTPUT_FLAG_DIRECT_PCM)) &&
1750 (mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
1751 ALOGD("non offloadable effect is enabled, try with non direct output");
1752 goto non_direct_output;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001753 }
1754
Weiyin Jiang6bba3202016-03-03 14:49:36 +08001755 profile = getProfileForDirectOutput(device,
1756 samplingRate,
1757 format,
1758 channelMask,
1759 (audio_output_flags_t)flags);
1760
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001761 if (profile != 0) {
vivek mehtac984b992015-11-25 13:28:55 -08001762
1763 if (!(flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301764 (profile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT_PCM)) {
vivek mehtac984b992015-11-25 13:28:55 -08001765 ALOGI("got Direct_PCM without requesting ... reject ");
1766 profile = NULL;
1767 goto non_direct_output;
1768 }
1769
Sharad Sangle36781612015-05-28 16:15:16 +05301770 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001771
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001772 // if multiple concurrent offload decode is supported
1773 // do no check for reuse and also don't close previous output if its offload
1774 // previous output will be closed during track destruction
1775 if (!(property_get_bool("audio.offload.multiple.enabled", false) &&
1776 ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0))) {
1777 for (size_t i = 0; i < mOutputs.size(); i++) {
1778 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1779 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1780 outputDesc = desc;
1781 // reuse direct output if currently open and configured with same parameters
1782 if ((samplingRate == outputDesc->mSamplingRate) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301783 audio_formats_match(format, outputDesc->mFormat) &&
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001784 (channelMask == outputDesc->mChannelMask)) {
1785 outputDesc->mDirectOpenCount++;
1786 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1787 return mOutputs.keyAt(i);
1788 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001789 }
1790 }
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001791 // close direct output if currently open and configured with different parameters
1792 if (outputDesc != NULL) {
1793 closeOutput(outputDesc->mIoHandle);
1794 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001795 }
Sharad Sangle36781612015-05-28 16:15:16 +05301796
1797 // if the selected profile is offloaded and no offload info was specified,
1798 // create a default one
1799 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
Ashish Jain40cab3a2016-03-23 11:14:14 +05301800 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
Sharad Sangle36781612015-05-28 16:15:16 +05301801 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1802 defaultOffloadInfo.sample_rate = samplingRate;
1803 defaultOffloadInfo.channel_mask = channelMask;
1804 defaultOffloadInfo.format = format;
1805 defaultOffloadInfo.stream_type = stream;
1806 defaultOffloadInfo.bit_rate = 0;
1807 defaultOffloadInfo.duration_us = -1;
1808 defaultOffloadInfo.has_video = true; // conservative
1809 defaultOffloadInfo.is_streaming = true; // likely
1810 offloadInfo = &defaultOffloadInfo;
1811 }
1812
1813 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001814 outputDesc->mDevice = device;
1815 outputDesc->mLatency = 0;
Sharad Sangle36781612015-05-28 16:15:16 +05301816 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001817 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1818 config.sample_rate = samplingRate;
1819 config.channel_mask = channelMask;
1820 config.format = format;
1821 if (offloadInfo != NULL) {
1822 config.offload_info = *offloadInfo;
1823 }
Sharad Sangle36781612015-05-28 16:15:16 +05301824 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001825 &output,
1826 &config,
1827 &outputDesc->mDevice,
1828 String8(""),
1829 &outputDesc->mLatency,
1830 outputDesc->mFlags);
1831
1832 // only accept an output with the requested parameters
1833 if (status != NO_ERROR ||
1834 (samplingRate != 0 && samplingRate != config.sample_rate) ||
Ashish Jain40cab3a2016-03-23 11:14:14 +05301835 (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) ||
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001836 (channelMask != 0 && channelMask != config.channel_mask)) {
1837 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1838 "format %d %d, channelMask %04x %04x", output, samplingRate,
1839 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1840 outputDesc->mChannelMask);
1841 if (output != AUDIO_IO_HANDLE_NONE) {
1842 mpClientInterface->closeOutput(output);
1843 }
Sharad Sangle36781612015-05-28 16:15:16 +05301844 // fall back to mixer output if possible when the direct output could not be open
Ashish Jain40cab3a2016-03-23 11:14:14 +05301845 if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) {
Sharad Sangle36781612015-05-28 16:15:16 +05301846 goto non_direct_output;
1847 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001848 return AUDIO_IO_HANDLE_NONE;
1849 }
1850 outputDesc->mSamplingRate = config.sample_rate;
1851 outputDesc->mChannelMask = config.channel_mask;
1852 outputDesc->mFormat = config.format;
1853 outputDesc->mRefCount[stream] = 0;
1854 outputDesc->mStopTime[stream] = 0;
1855 outputDesc->mDirectOpenCount = 1;
1856
1857 audio_io_handle_t srcOutput = getOutputForEffect();
1858 addOutput(output, outputDesc);
1859 audio_io_handle_t dstOutput = getOutputForEffect();
1860 if (dstOutput == output) {
Gao Jiedb057832015-11-12 08:51:22 +08001861#ifdef DOLBY_ENABLE
1862 status_t status = mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1863 if (status == NO_ERROR) {
1864 for (size_t i = 0; i < mEffects.size(); i++) {
1865 sp<EffectDescriptor> desc = mEffects.valueAt(i);
1866 if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX) {
1867 // update the mIo member of EffectDescriptor for the global effect
1868 ALOGV("%s updating mIo", __FUNCTION__);
1869 desc->mIo = dstOutput;
1870 }
1871 }
1872 } else {
1873 ALOGW("%s moveEffects from %d to %d failed", __FUNCTION__, srcOutput, dstOutput);
1874 }
1875#else // DOLBY_END
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001876 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
Gao Jiedb057832015-11-12 08:51:22 +08001877#endif // LINE_ADDED_BY_DOLBY
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001878 }
1879 mPreviousOutputs = mOutputs;
1880 ALOGV("getOutput() returns new direct output %d", output);
1881 mpClientInterface->onAudioPortListUpdate();
1882 return output;
1883 }
1884
1885non_direct_output:
Ashish Jain5fe12412016-04-18 18:44:17 +05301886
1887 // A request for HW A/V sync cannot fallback to a mixed output because time
1888 // stamps are embedded in audio data
1889 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1890 return AUDIO_IO_HANDLE_NONE;
1891 }
1892
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001893 // ignoring channel mask due to downmix capability in mixer
1894
1895 // open a non direct output
1896
1897 // for non direct outputs, only PCM is supported
1898 if (audio_is_linear_pcm(format)) {
1899 // get which output is suitable for the specified stream. The actual
1900 // routing change will happen when startOutput() will be called
1901 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1902
1903 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1904 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
Ashish Jain660d3112016-06-20 10:16:51 +05301905
1906 if (forced_deep) {
1907 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
1908 ALOGI("setting force DEEP buffer now ");
1909 } else if(flags == AUDIO_OUTPUT_FLAG_NONE) {
1910 // no deep buffer playback is requested hence fallback to primary
1911 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_PRIMARY);
1912 ALOGI("FLAG None hence request for a primary output");
1913 }
1914
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001915 output = selectOutput(outputs, flags, format);
1916 }
1917 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1918 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1919
vivek mehta0ea887a2015-08-26 14:01:20 -07001920 ALOGV("getOutputForDevice() returns output %d", output);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001921
1922 return output;
1923}
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301924
1925status_t AudioPolicyManagerCustom::getInputForAttr(const audio_attributes_t *attr,
1926 audio_io_handle_t *input,
1927 audio_session_t session,
1928 uid_t uid,
1929 uint32_t samplingRate,
1930 audio_format_t format,
1931 audio_channel_mask_t channelMask,
1932 audio_input_flags_t flags,
1933 audio_port_handle_t selectedDeviceId,
1934 input_type_t *inputType)
1935{
Ashish Jain40cab3a2016-03-23 11:14:14 +05301936 audio_source_t inputSource;
1937 inputSource = attr->source;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301938#ifdef VOICE_CONCURRENCY
1939
1940 char propValue[PROPERTY_VALUE_MAX];
1941 bool prop_rec_enabled=false, prop_voip_enabled = false;
1942
1943 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
1944 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1945 }
1946
1947 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1948 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1949 }
1950
1951 if (prop_rec_enabled && mvoice_call_state) {
1952 //check if voice call is active / running in background
1953 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1954 //Need to block input request
1955 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1956 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1957 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1958 {
1959 switch(inputSource) {
1960 case AUDIO_SOURCE_VOICE_UPLINK:
1961 case AUDIO_SOURCE_VOICE_DOWNLINK:
1962 case AUDIO_SOURCE_VOICE_CALL:
1963 ALOGD("voice_conc:Creating input during incall mode for inputSource: %d",
1964 inputSource);
1965 break;
1966
1967 case AUDIO_SOURCE_VOICE_COMMUNICATION:
1968 if(prop_voip_enabled) {
1969 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1970 inputSource);
1971 return NO_INIT;
1972 }
1973 break;
1974 default:
1975 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1976 inputSource);
1977 return NO_INIT;
1978 }
1979 }
1980 }//check for VoIP flag
1981 else if(prop_voip_enabled && mvoice_call_state) {
1982 //check if voice call is active / running in background
1983 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1984 //Need to block input request
1985 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1986 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1987 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1988 {
1989 if(inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1990 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
1991 return NO_INIT;
1992 }
1993 }
1994 }
1995
1996#endif
1997
1998 return AudioPolicyManager::getInputForAttr(attr,
1999 input,
2000 session,
2001 uid,
2002 samplingRate,
2003 format,
2004 channelMask,
2005 flags,
2006 selectedDeviceId,
2007 inputType);
2008}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08002009
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302010status_t AudioPolicyManagerCustom::startInput(audio_io_handle_t input,
2011 audio_session_t session)
2012{
2013 ALOGV("startInput() input %d", input);
2014 ssize_t index = mInputs.indexOfKey(input);
2015 if (index < 0) {
2016 ALOGW("startInput() unknown input %d", input);
2017 return BAD_VALUE;
2018 }
2019 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
2020
Ashish Jain40cab3a2016-03-23 11:14:14 +05302021 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
2022 if (audioSession == 0) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302023 ALOGW("startInput() unknown session %d on input %d", session, input);
2024 return BAD_VALUE;
2025 }
2026
2027 // virtual input devices are compatible with other input devices
2028 if (!is_virtual_input_device(inputDesc->mDevice)) {
2029
2030 // for a non-virtual input device, check if there is another (non-virtual) active input
2031 audio_io_handle_t activeInput = mInputs.getActiveInput();
2032 if (activeInput != 0 && activeInput != input) {
2033
2034 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
2035 // otherwise the active input continues and the new input cannot be started.
2036 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302037 if ((activeDesc->inputSource() == AUDIO_SOURCE_HOTWORD) &&
2038 !activeDesc->hasPreemptedSession(session)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302039 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302040 //FIXME: consider all active sessions
2041 AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions();
2042 audio_session_t activeSession = activeSessions.keyAt(0);
2043 SortedVector<audio_session_t> sessions =
2044 activeDesc->getPreemptedSessions();
2045 sessions.add(activeSession);
2046 inputDesc->setPreemptedSessions(sessions);
2047 stopInput(activeInput, activeSession);
2048 releaseInput(activeInput, activeSession);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302049 } else {
2050 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
2051 return INVALID_OPERATION;
2052 }
2053 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05302054 // Do not allow capture if an active voice call is using a software patch and
2055 // the call TX source device is on the same HW module.
2056 // FIXME: would be better to refine to only inputs whose profile connects to the
2057 // call TX device but this information is not in the audio patch
2058 if (mCallTxPatch != 0 &&
2059 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
2060 return INVALID_OPERATION;
2061 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302062 }
2063
2064 // Routing?
2065 mInputRoutes.incRouteActivity(session);
2066#ifdef RECORD_PLAY_CONCURRENCY
2067 mIsInputRequestOnProgress = true;
2068
2069 char getPropValue[PROPERTY_VALUE_MAX];
2070 bool prop_rec_play_enabled = false;
2071
2072 if (property_get("rec.playback.conc.disabled", getPropValue, NULL)) {
2073 prop_rec_play_enabled = atoi(getPropValue) || !strncmp("true", getPropValue, 4);
2074 }
2075
2076 if ((prop_rec_play_enabled) &&(mInputs.activeInputsCount() == 0)){
2077 // send update to HAL on record playback concurrency
2078 AudioParameter param = AudioParameter();
2079 param.add(String8("rec_play_conc_on"), String8("true"));
2080 ALOGD("startInput() setParameters rec_play_conc is setting to ON ");
2081 mpClientInterface->setParameters(0, param.toString());
2082
2083 // Call invalidate to reset all opened non ULL audio tracks
2084 // Move tracks associated to this strategy from previous output to new output
2085 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
2086 // Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder)
Sharad Sangle4509cef2015-08-19 20:47:12 +05302087 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE && (i != AUDIO_STREAM_PATCH))) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302088 ALOGD("Invalidate on releaseInput for stream :: %d ", i);
2089 //FIXME see fixme on name change
2090 mpClientInterface->invalidateStream((audio_stream_type_t)i);
2091 }
2092 }
2093 // close compress tracks
2094 for (size_t i = 0; i < mOutputs.size(); i++) {
2095 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
2096 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
2097 ALOGD("ouput desc / profile is NULL");
2098 continue;
2099 }
2100 if (outputDesc->mProfile->mFlags
2101 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2102 // close compress sessions
2103 ALOGD("calling closeOutput on record conc for COMPRESS output");
2104 closeOutput(mOutputs.keyAt(i));
2105 }
2106 }
Ashish Jaine4a22d52016-06-22 11:55:08 +05302107 // If effects where present on any of the above closed outputs,
2108 // audioflinger moved them to the primary output by default
2109 // move them back to the appropriate output.
2110 moveGlobalEffect();
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302111 }
2112#endif
2113
Ashish Jain40cab3a2016-03-23 11:14:14 +05302114 if (!inputDesc->isActive() || mInputRoutes.hasRouteChanged(session)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302115 // if input maps to a dynamic policy with an activity listener, notify of state change
Ashish Jain630c5e12016-04-18 11:50:21 +05302116 if ((inputDesc->mPolicyMix != NULL)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302117 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Ashish Jain5fe12412016-04-18 18:44:17 +05302118 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302119 MIX_STATE_MIXING);
Ashish Jain630c5e12016-04-18 11:50:21 +05302120 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302121
2122 if (mInputs.activeInputsCount() == 0) {
2123 SoundTrigger::setCaptureState(true);
2124 }
2125 setInputDevice(input, getNewInputDevice(input), true /* force */);
2126
2127 // automatically enable the remote submix output when input is started if not
2128 // used by a policy mix of type MIX_TYPE_RECORDERS
2129 // For remote submix (a virtual device), we open only one input per capture request.
2130 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
2131 String8 address = String8("");
2132 if (inputDesc->mPolicyMix == NULL) {
2133 address = String8("0");
Ashish Jain630c5e12016-04-18 11:50:21 +05302134 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
Ashish Jain5fe12412016-04-18 18:44:17 +05302135 address = inputDesc->mPolicyMix->mDeviceAddress;
Ashish Jain630c5e12016-04-18 11:50:21 +05302136 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302137 if (address != "") {
2138 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2139 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2140 address, "remote-submix");
2141 }
2142 }
2143 }
2144
Ashish Jain40cab3a2016-03-23 11:14:14 +05302145 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302146
Ashish Jain40cab3a2016-03-23 11:14:14 +05302147 audioSession->changeActiveCount(1);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302148#ifdef RECORD_PLAY_CONCURRENCY
2149 mIsInputRequestOnProgress = false;
2150#endif
2151 return NO_ERROR;
2152}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08002153
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302154status_t AudioPolicyManagerCustom::stopInput(audio_io_handle_t input,
2155 audio_session_t session)
2156{
2157 status_t status;
2158 status = AudioPolicyManager::stopInput(input, session);
2159#ifdef RECORD_PLAY_CONCURRENCY
2160 char propValue[PROPERTY_VALUE_MAX];
2161 bool prop_rec_play_enabled = false;
2162
2163 if (property_get("rec.playback.conc.disabled", propValue, NULL)) {
2164 prop_rec_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
2165 }
2166
2167 if ((prop_rec_play_enabled) && (mInputs.activeInputsCount() == 0)) {
2168
2169 //send update to HAL on record playback concurrency
2170 AudioParameter param = AudioParameter();
2171 param.add(String8("rec_play_conc_on"), String8("false"));
2172 ALOGD("stopInput() setParameters rec_play_conc is setting to OFF ");
2173 mpClientInterface->setParameters(0, param.toString());
2174
2175 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
2176 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
2177 //Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder stop tone)
2178 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE) && (i != AUDIO_STREAM_PATCH)) {
2179 ALOGD(" Invalidate on stopInput for stream :: %d ", i);
2180 //FIXME see fixme on name change
2181 mpClientInterface->invalidateStream((audio_stream_type_t)i);
2182 }
2183 }
2184 }
2185#endif
2186 return status;
2187}
2188
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302189void AudioPolicyManagerCustom::closeAllInputs() {
2190 bool patchRemoved = false;
2191
Dhanalakshmi Siddanibfeca7f2015-10-08 15:17:11 +05302192 for(size_t input_index = mInputs.size(); input_index > 0; input_index--) {
2193 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index-1);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302194 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302195 if (patch_index >= 0) {
2196 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302197 status_t status;
2198 status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302199 mAudioPatches.removeItemsAt(patch_index);
2200 patchRemoved = true;
2201 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05302202 if ((inputDesc->getOpenRefCount() > 0) && inputDesc->isSoundTrigger()
2203 && (mInputs.size() == 1)) {
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302204 ALOGD("Do not close sound trigger input handle");
2205 } else {
Dhanalakshmi Siddanibfeca7f2015-10-08 15:17:11 +05302206 mpClientInterface->closeInput(mInputs.keyAt(input_index-1));
2207 mInputs.removeItem(mInputs.keyAt(input_index-1));
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302208 }
2209 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05302210 mInputs.clear();
2211 SoundTrigger::setCaptureState(false);
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302212 nextAudioPortGeneration();
2213
2214 if (patchRemoved) {
2215 mpClientInterface->onAudioPatchListUpdate();
2216 }
2217}
2218
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302219AudioPolicyManagerCustom::AudioPolicyManagerCustom(AudioPolicyClientInterface *clientInterface)
Sharad Sangle4509cef2015-08-19 20:47:12 +05302220 : AudioPolicyManager(clientInterface),
2221 mHdmiAudioDisabled(false),
2222 mHdmiAudioEvent(false),
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08002223 mPrevPhoneState(0),
Haynes Mathew George603ae9b2015-12-21 17:23:59 -08002224 mPrevFMVolumeDb(0.0f),
2225 mFMIsActive(false)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302226{
Ashish Jain40cab3a2016-03-23 11:14:14 +05302227
Satya Krishna Pindiproli8f83f102016-06-21 17:56:14 +05302228#ifdef USE_XML_AUDIO_POLICY_CONF
2229 ALOGD("USE_XML_AUDIO_POLICY_CONF is TRUE");
2230#else
2231 ALOGD("USE_XML_AUDIO_POLICY_CONF is FALSE");
2232#endif
2233
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302234#ifdef RECORD_PLAY_CONCURRENCY
2235 mIsInputRequestOnProgress = false;
2236#endif
2237
2238
2239#ifdef VOICE_CONCURRENCY
2240 mFallBackflag = getFallBackPath();
2241#endif
2242}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07002243}