blob: 6c26b2354d62a78a58ea843685c592981eb5912b [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)
Sharad Sangle75170722016-11-21 16:21:06 +053055#define SAMPLE_RATE_8000 8000
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) &&
Lalit Kansara33918092016-12-06 22:42:26 +0530523 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCountOnDevices() > 0))) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530524 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) {
kunleiz0b55ec72016-11-10 16:23:58 +0800846 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_VOIP_RX) {
847 if (prop_voip_enabled) {
848 ALOGD("voice_conc:calling closeOutput on call mode for DIRECT output");
849 closeOutput(mOutputs.keyAt(i));
850 }
851 }
852 else if (prop_playback_enabled
853 && (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530854 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
855 closeOutput(mOutputs.keyAt(i));
856 }
857 }
858 }
Ashish Jaine4a22d52016-06-22 11:55:08 +0530859 // If effects where present on any of the above closed outputs,
860 // audioflinger moved them to the primary output by default
861 // move them back to the appropriate output.
862 moveGlobalEffect();
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530863 }
864
865 if ((AUDIO_MODE_IN_CALL == oldState || AUDIO_MODE_IN_COMMUNICATION == oldState) &&
866 (AUDIO_MODE_NORMAL == state) && prop_playback_enabled && mvoice_call_state) {
867 ALOGD("voice_conc:EXITING from call mode oldState :: %d state::%d \n",oldState, state);
868 mvoice_call_state = 0;
869 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
870 //restore PCM (deep-buffer) output after call termination
871 for (size_t i = 0; i < mOutputs.size(); i++) {
872 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
873 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
874 ALOGD("voice_conc:ouput desc / profile is NULL");
875 continue;
876 }
877 if (!outputDesc->isDuplicated() && outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
878 ALOGD("voice_conc:calling restoreOutput after call mode for primary output");
879 mpClientInterface->restoreOutput(mOutputs.keyAt(i));
880 }
881 }
882 }
883 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
884 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
885 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
886 if (i == AUDIO_STREAM_PATCH) {
887 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
888 continue;
889 }
890 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
891 if ((AUDIO_STREAM_MUSIC == i) ||
892 (AUDIO_STREAM_VOICE_CALL == i) ) {
893 mpClientInterface->invalidateStream((audio_stream_type_t)i);
894 }
895 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
896 mpClientInterface->invalidateStream((audio_stream_type_t)i);
897 }
898 }
899 }
900
901#endif
Preetam Singh Ranawat96afc162016-08-25 14:35:02 +0530902
903 sp<SwAudioOutputDescriptor> outputDesc = NULL;
904 for (size_t i = 0; i < mOutputs.size(); i++) {
905 outputDesc = mOutputs.valueAt(i);
906 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
907 ALOGD("voice_conc:ouput desc / profile is NULL");
908 continue;
909 }
910
911 if (property_get_bool("voice.dsd.playback.conc.disabled", true) &&
912 (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
913 (outputDesc->mFormat == AUDIO_FORMAT_DSD)) {
914 ALOGD("voice_conc:calling closeOutput on call mode for DSD COMPRESS output");
915 closeOutput(mOutputs.keyAt(i));
916 // call invalidate for music, so that DSD compress will fallback to deep-buffer.
917 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
918 }
919
920 }
921
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530922#ifdef RECORD_PLAY_CONCURRENCY
923 char recConcPropValue[PROPERTY_VALUE_MAX];
924 bool prop_rec_play_enabled = false;
925
926 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
927 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
928 }
929 if (prop_rec_play_enabled) {
930 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
931 ALOGD("phone state changed to MODE_IN_COMM invlaidating music and voice streams");
932 // call invalidate for voice streams, so that it can use deepbuffer with VoIP out device from HAL
933 mpClientInterface->invalidateStream(AUDIO_STREAM_VOICE_CALL);
934 // call invalidate for music, so that compress will fallback to deep-buffer with VoIP out device
935 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
936
937 // close compress output to make sure session will be closed before timeout(60sec)
938 for (size_t i = 0; i < mOutputs.size(); i++) {
939
940 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
941 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
942 ALOGD("ouput desc / profile is NULL");
943 continue;
944 }
945
946 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
947 ALOGD("calling closeOutput on call mode for COMPRESS output");
948 closeOutput(mOutputs.keyAt(i));
949 }
950 }
Ashish Jaine4a22d52016-06-22 11:55:08 +0530951 // If effects where present on any of the above closed outputs,
952 // audioflinger moved them to the primary output by default
953 // move them back to the appropriate output.
954 moveGlobalEffect();
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530955 } else if ((oldState == AUDIO_MODE_IN_COMMUNICATION) &&
956 (mEngine->getPhoneState() == AUDIO_MODE_NORMAL)) {
957 // call invalidate for music so that music can fallback to compress
958 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
959 }
960 }
961#endif
962 mPrevPhoneState = oldState;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700963 int delayMs = 0;
964 if (isStateInCall(state)) {
965 nsecs_t sysTime = systemTime();
966 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530967 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700968 // mute media and sonification strategies and delay device switch by the largest
969 // latency of any output where either strategy is active.
970 // This avoid sending the ring tone or music tail into the earpiece or headset.
Sharad Sangle36781612015-05-28 16:15:16 +0530971 if ((isStrategyActive(desc, STRATEGY_MEDIA,
972 SONIFICATION_HEADSET_MUSIC_DELAY,
973 sysTime) ||
974 isStrategyActive(desc, STRATEGY_SONIFICATION,
975 SONIFICATION_HEADSET_MUSIC_DELAY,
976 sysTime)) &&
977 (delayMs < (int)desc->latency()*2)) {
978 delayMs = desc->latency()*2;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700979 }
Sharad Sangle36781612015-05-28 16:15:16 +0530980 setStrategyMute(STRATEGY_MEDIA, true, desc);
981 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700982 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Sharad Sangle36781612015-05-28 16:15:16 +0530983 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
984 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700985 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
986 }
987 }
988
Sharad Sangle36781612015-05-28 16:15:16 +0530989 if (hasPrimaryOutput()) {
990 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
991 // the device returned is not necessarily reachable via this output
992 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
993 // force routing command to audio hardware when ending call
994 // even if no device change is needed
995 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
996 rxDevice = mPrimaryOutput->device();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700997 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700998
Sharad Sangle36781612015-05-28 16:15:16 +0530999 if (state == AUDIO_MODE_IN_CALL) {
1000 updateCallRouting(rxDevice, delayMs);
1001 } else if (oldState == AUDIO_MODE_IN_CALL) {
1002 if (mCallRxPatch != 0) {
1003 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
1004 mCallRxPatch.clear();
1005 }
1006 if (mCallTxPatch != 0) {
1007 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
1008 mCallTxPatch.clear();
1009 }
1010 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
1011 } else {
1012 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001013 }
1014 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301015 //update device for all non-primary outputs
1016 for (size_t i = 0; i < mOutputs.size(); i++) {
1017 audio_io_handle_t output = mOutputs.keyAt(i);
1018 if (output != mPrimaryOutput->mIoHandle) {
1019 newDevice = getNewOutputDevice(mOutputs.valueFor(output), false /*fromCache*/);
1020 setOutputDevice(mOutputs.valueFor(output), newDevice, (newDevice != AUDIO_DEVICE_NONE));
1021 }
1022 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001023 // if entering in call state, handle special case of active streams
1024 // pertaining to sonification strategy see handleIncallSonification()
1025 if (isStateInCall(state)) {
1026 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +05301027 for (size_t j = 0; j < mOutputs.size(); j++) {
1028 audio_io_handle_t curOutput = mOutputs.keyAt(j);
1029 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
1030 if (stream == AUDIO_STREAM_PATCH) {
1031 continue;
1032 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301033 handleIncallSonification((audio_stream_type_t)stream, true, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +05301034 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001035 }
Zhou Song58081b62016-02-24 13:10:55 +08001036
1037 // force reevaluating accessibility routing when call starts
1038 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001039 }
1040
1041 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
1042 if (state == AUDIO_MODE_RINGTONE &&
1043 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
1044 mLimitRingtoneVolume = true;
1045 } else {
1046 mLimitRingtoneVolume = false;
1047 }
1048}
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301049
1050void AudioPolicyManagerCustom::setForceUse(audio_policy_force_use_t usage,
1051 audio_policy_forced_cfg_t config)
1052{
Ashish Jain40cab3a2016-03-23 11:14:14 +05301053 ALOGD("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301054
1055 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
1056 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
1057 return;
1058 }
1059 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
1060 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
1061 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
1062
1063 // check for device and output changes triggered by new force usage
1064 checkA2dpSuspend();
1065 checkOutputForAllStrategies();
1066 updateDevicesAndOutputs();
Ashish Jain40cab3a2016-03-23 11:14:14 +05301067
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301068 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
1069 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
1070 updateCallRouting(newDevice);
1071 }
1072 // Use reverse loop to make sure any low latency usecases (generally tones)
1073 // are not routed before non LL usecases (generally music).
1074 // We can safely assume that LL output would always have lower index,
1075 // and use this work-around to avoid routing of output with music stream
1076 // from the context of short lived LL output.
1077 // Note: in case output's share backend(HAL sharing is implicit) all outputs
1078 // gets routing update while processing first output itself.
1079 for (size_t i = mOutputs.size(); i > 0; i--) {
1080 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i-1);
1081 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
1082 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || outputDesc != mPrimaryOutput) {
1083 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
1084 }
1085 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
1086 applyStreamVolumes(outputDesc, newDevice, 0, true);
1087 }
1088 }
1089
1090 audio_io_handle_t activeInput = mInputs.getActiveInput();
1091 if (activeInput != 0) {
Ashish Jain40cab3a2016-03-23 11:14:14 +05301092 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
1093 audio_devices_t newDevice = getNewInputDevice(activeInput);
1094 // Force new input selection if the new device can not be reached via current input
1095 if (activeDesc->mProfile->getSupportedDevices().types() & (newDevice & ~AUDIO_DEVICE_BIT_IN)) {
1096 setInputDevice(activeInput, newDevice);
1097 } else {
1098 closeInput(activeInput);
1099 }
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301100 }
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301101}
1102
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301103status_t AudioPolicyManagerCustom::stopSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301104 audio_stream_type_t stream,
1105 bool forceDeviceUpdate)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001106{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301107 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1108 ALOGW("stopSource() invalid stream %d", stream);
1109 return INVALID_OPERATION;
1110 }
Sharad Sangle36781612015-05-28 16:15:16 +05301111 // always handle stream stop, check which stream type is stopping
1112 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001113
Sharad Sangle36781612015-05-28 16:15:16 +05301114 // handle special case for sonification while in call
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001115 if (isInCall()) {
Sharad Sangle36781612015-05-28 16:15:16 +05301116 if (outputDesc->isDuplicated()) {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301117 handleIncallSonification(stream, false, false, outputDesc->subOutput1()->mIoHandle);
1118 handleIncallSonification(stream, false, false, outputDesc->subOutput2()->mIoHandle);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001119 }
Sharad Sangle36781612015-05-28 16:15:16 +05301120 handleIncallSonification(stream, false, false, outputDesc->mIoHandle);
1121 }
1122
1123 if (outputDesc->mRefCount[stream] > 0) {
1124 // decrement usage count of this stream on the output
1125 outputDesc->changeRefCount(stream, -1);
1126
1127 // store time at which the stream was stopped - see isStreamActive()
1128 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
1129 outputDesc->mStopTime[stream] = systemTime();
Zhou Song5dcddc92015-09-21 14:36:57 +08001130 audio_devices_t prevDevice = outputDesc->device();
Sharad Sangle36781612015-05-28 16:15:16 +05301131 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
1132 // delay the device switch by twice the latency because stopOutput() is executed when
1133 // the track stop() command is received and at that time the audio track buffer can
1134 // still contain data that needs to be drained. The latency only covers the audio HAL
1135 // and kernel buffers. Also the latency does not always include additional delay in the
1136 // audio path (audio DSP, CODEC ...)
1137 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
1138
1139 // force restoring the device selection on other active outputs if it differs from the
1140 // one being selected for this output
1141 for (size_t i = 0; i < mOutputs.size(); i++) {
1142 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1143 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1144 if (desc != outputDesc &&
1145 desc->isActive() &&
1146 outputDesc->sharesHwModuleWith(desc) &&
1147 (newDevice != desc->device())) {
Sharad Sangle4509cef2015-08-19 20:47:12 +05301148 audio_devices_t dev = getNewOutputDevice(mOutputs.valueFor(curOutput), false /*fromCache*/);
Naresh Tanniru11393702016-09-14 11:25:37 +05301149 bool force = desc->device() != dev;
Zhou Song5dcddc92015-09-21 14:36:57 +08001150 uint32_t delayMs;
1151 if (dev == prevDevice) {
1152 delayMs = 0;
1153 } else {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301154 delayMs = outputDesc->latency()*2;
Zhou Song5dcddc92015-09-21 14:36:57 +08001155 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301156 setOutputDevice(desc,
1157 dev,
Naresh Tanniru11393702016-09-14 11:25:37 +05301158 force,
Zhou Song5dcddc92015-09-21 14:36:57 +08001159 delayMs);
Sharad Sangle36781612015-05-28 16:15:16 +05301160 }
1161 }
1162 // update the outputs if stopping one with a stream that can affect notification routing
1163 handleNotificationRoutingForStream(stream);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001164 }
Sharad Sangle36781612015-05-28 16:15:16 +05301165 return NO_ERROR;
1166 } else {
1167 ALOGW("stopOutput() refcount is already 0");
1168 return INVALID_OPERATION;
1169 }
1170}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001171
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301172status_t AudioPolicyManagerCustom::startSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301173 audio_stream_type_t stream,
1174 audio_devices_t device,
Lalit Kansaraf803ac52016-05-09 19:12:01 +05301175 const char *address,
Sharad Sangle36781612015-05-28 16:15:16 +05301176 uint32_t *delayMs)
1177{
1178 // cannot start playback of STREAM_TTS if any other output is being used
1179 uint32_t beaconMuteLatency = 0;
1180
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301181 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1182 ALOGW("startSource() invalid stream %d", stream);
1183 return INVALID_OPERATION;
1184 }
1185
Sharad Sangle36781612015-05-28 16:15:16 +05301186 *delayMs = 0;
1187 if (stream == AUDIO_STREAM_TTS) {
1188 ALOGV("\t found BEACON stream");
Ashish Jain40cab3a2016-03-23 11:14:14 +05301189 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301190 return INVALID_OPERATION;
1191 } else {
1192 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001193 }
Sharad Sangle36781612015-05-28 16:15:16 +05301194 } else {
1195 // some playback other than beacon starts
1196 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1197 }
1198
Ashish Jain40cab3a2016-03-23 11:14:14 +05301199 // check active before incrementing usage count
1200 bool force = !outputDesc->isActive();
1201
Sharad Sangle36781612015-05-28 16:15:16 +05301202 // increment usage count for this stream on the requested output:
1203 // NOTE that the usage count is the same for duplicated output and hardware output which is
1204 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1205 outputDesc->changeRefCount(stream, 1);
1206
1207 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
1208 // starting an output being rerouted?
1209 if (device == AUDIO_DEVICE_NONE) {
1210 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001211 }
Sharad Sangle36781612015-05-28 16:15:16 +05301212 routing_strategy strategy = getStrategy(stream);
1213 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
1214 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1215 (beaconMuteLatency > 0);
1216 uint32_t waitMs = beaconMuteLatency;
Sharad Sangle36781612015-05-28 16:15:16 +05301217 for (size_t i = 0; i < mOutputs.size(); i++) {
1218 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1219 if (desc != outputDesc) {
1220 // force a device change if any other output is managed by the same hw
1221 // module and has a current device selection that differs from selected device.
1222 // In this case, the audio HAL must receive the new device selection so that it can
1223 // change the device currently selected by the other active output.
1224 if (outputDesc->sharesHwModuleWith(desc) &&
1225 desc->device() != device) {
1226 force = true;
1227 }
1228 // wait for audio on other active outputs to be presented when starting
1229 // a notification so that audio focus effect can propagate, or that a mute/unmute
1230 // event occurred for beacon
1231 uint32_t latency = desc->latency();
1232 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1233 waitMs = latency;
1234 }
1235 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001236 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05301237 uint32_t muteWaitMs;
Lalit Kansaraf803ac52016-05-09 19:12:01 +05301238 muteWaitMs = setOutputDevice(outputDesc, device, force, 0, NULL, address);
Sharad Sangle36781612015-05-28 16:15:16 +05301239
1240 // handle special case for sonification while in call
1241 if (isInCall()) {
1242 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001243 }
Sharad Sangle36781612015-05-28 16:15:16 +05301244
1245 // apply volume rules for current stream and device if necessary
1246 checkAndSetVolume(stream,
Ashish Jain40cab3a2016-03-23 11:14:14 +05301247 mVolumeCurves->getVolumeIndex(stream, device),
Sharad Sangle36781612015-05-28 16:15:16 +05301248 outputDesc,
1249 device);
1250
1251 // update the outputs if starting an output with a stream that can affect notification
1252 // routing
1253 handleNotificationRoutingForStream(stream);
1254
1255 // force reevaluating accessibility routing when ringtone or alarm starts
1256 if (strategy == STRATEGY_SONIFICATION) {
1257 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1258 }
1259 }
1260 else {
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001261 // handle special case for sonification while in call
1262 if (isInCall()) {
1263 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Sharad Sangle36781612015-05-28 16:15:16 +05301264 }
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001265 }
Sharad Sangle36781612015-05-28 16:15:16 +05301266 return NO_ERROR;
1267}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001268
Sharad Sangle36781612015-05-28 16:15:16 +05301269void AudioPolicyManagerCustom::handleIncallSonification(audio_stream_type_t stream,
1270 bool starting, bool stateChange,
1271 audio_io_handle_t output)
1272{
1273 if(!hasPrimaryOutput()) {
1274 return;
1275 }
1276 // no action needed for AUDIO_STREAM_PATCH stream type, it's for internal flinger tracks
1277 if (stream == AUDIO_STREAM_PATCH) {
1278 return;
1279 }
1280 // if the stream pertains to sonification strategy and we are in call we must
1281 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
1282 // in the device used for phone strategy and play the tone if the selected device does not
1283 // interfere with the device used for phone strategy
1284 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
1285 // many times as there are active tracks on the output
1286 const routing_strategy stream_strategy = getStrategy(stream);
1287 if ((stream_strategy == STRATEGY_SONIFICATION) ||
1288 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
1289 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
1290 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
1291 stream, starting, outputDesc->mDevice, stateChange);
1292 if (outputDesc->mRefCount[stream]) {
1293 int muteCount = 1;
1294 if (stateChange) {
1295 muteCount = outputDesc->mRefCount[stream];
1296 }
1297 if (audio_is_low_visibility(stream)) {
1298 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
1299 for (int i = 0; i < muteCount; i++) {
1300 setStreamMute(stream, starting, outputDesc);
1301 }
1302 } else {
1303 ALOGV("handleIncallSonification() high visibility");
1304 if (outputDesc->device() &
1305 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
1306 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
1307 for (int i = 0; i < muteCount; i++) {
1308 setStreamMute(stream, starting, outputDesc);
1309 }
1310 }
1311 if (starting) {
1312 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
1313 AUDIO_STREAM_VOICE_CALL);
1314 } else {
1315 mpClientInterface->stopTone();
1316 }
1317 }
1318 }
1319 }
1320}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001321
Sharad Sangle36781612015-05-28 16:15:16 +05301322void AudioPolicyManagerCustom::handleNotificationRoutingForStream(audio_stream_type_t stream) {
1323 switch(stream) {
1324 case AUDIO_STREAM_MUSIC:
1325 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
1326 updateDevicesAndOutputs();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001327 break;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001328 default:
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001329 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001330 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001331}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001332
Sharad Sangle36781612015-05-28 16:15:16 +05301333status_t AudioPolicyManagerCustom::checkAndSetVolume(audio_stream_type_t stream,
1334 int index,
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301335 const sp<AudioOutputDescriptor>& outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301336 audio_devices_t device,
1337 int delayMs, bool force)
1338{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301339 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1340 ALOGW("checkAndSetVolume() invalid stream %d", stream);
1341 return INVALID_OPERATION;
1342 }
Sharad Sangle36781612015-05-28 16:15:16 +05301343 // do not change actual stream volume if the stream is muted
1344 if (outputDesc->mMuteCount[stream] != 0) {
1345 ALOGVV("checkAndSetVolume() stream %d muted count %d",
1346 stream, outputDesc->mMuteCount[stream]);
1347 return NO_ERROR;
1348 }
1349 audio_policy_forced_cfg_t forceUseForComm =
1350 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
1351 // do not change in call volume if bluetooth is connected and vice versa
1352 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
1353 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
1354 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
1355 stream, forceUseForComm);
1356 return INVALID_OPERATION;
1357 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001358
Sharad Sangle36781612015-05-28 16:15:16 +05301359 if (device == AUDIO_DEVICE_NONE) {
1360 device = outputDesc->device();
1361 }
1362
1363 float volumeDb = computeVolume(stream, index, device);
1364 if (outputDesc->isFixedVolume(device)) {
1365 volumeDb = 0.0f;
1366 }
1367
1368 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
1369
1370 if (stream == AUDIO_STREAM_VOICE_CALL ||
1371 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
1372 float voiceVolume;
1373 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
1374 if (stream == AUDIO_STREAM_VOICE_CALL) {
Ashish Jain40cab3a2016-03-23 11:14:14 +05301375 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Sharad Sangle36781612015-05-28 16:15:16 +05301376 } else {
1377 voiceVolume = 1.0;
1378 }
1379
1380 if (voiceVolume != mLastVoiceVolume && ((outputDesc == mPrimaryOutput) ||
1381 isDirectOutput(outputDesc->mIoHandle) || device & AUDIO_DEVICE_OUT_ALL_USB)) {
1382 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
1383 mLastVoiceVolume = voiceVolume;
1384 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301385#ifdef FM_POWER_OPT
1386 } else if (stream == AUDIO_STREAM_MUSIC && hasPrimaryOutput() &&
Haynes Mathew George603ae9b2015-12-21 17:23:59 -08001387 outputDesc == mPrimaryOutput && mFMIsActive) {
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001388 /* Avoid unnecessary set_parameter calls as it puts the primary
1389 outputs FastMixer in HOT_IDLE leading to breaks in audio */
1390 if (volumeDb != mPrevFMVolumeDb) {
1391 mPrevFMVolumeDb = volumeDb;
1392 AudioParameter param = AudioParameter();
1393 param.addFloat(String8("fm_volume"), Volume::DbToAmpl(volumeDb));
Mingming Yin1433cc62016-01-04 14:58:00 -08001394 //Double delayMs to avoid sound burst while device switch.
1395 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString(), delayMs*2);
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001396 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301397#endif /* FM_POWER_OPT end */
Sharad Sangle36781612015-05-28 16:15:16 +05301398 }
1399
1400 return NO_ERROR;
1401}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001402
Sharad Sangle36781612015-05-28 16:15:16 +05301403bool AudioPolicyManagerCustom::isDirectOutput(audio_io_handle_t output) {
1404 for (size_t i = 0; i < mOutputs.size(); i++) {
1405 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1406 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1407 if ((curOutput == output) && (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
1408 return true;
1409 }
1410 }
1411 return false;
1412}
vivek mehta0ea887a2015-08-26 14:01:20 -07001413
Sharad Sangle75170722016-11-21 16:21:06 +05301414bool static tryForDirectPCM(int bitWidth, audio_output_flags_t *flags, uint32_t samplingRate)
Ashish Jainac510a72016-04-28 12:22:58 +05301415{
Ashish Jain771d02f2016-10-27 22:35:46 +05301416 bool playerDirectPCM = false; // Output request for Track created by mediaplayer
1417 bool trackDirectPCM = false; // Output request for track created by other apps
Ashish Jain27564312016-11-14 14:58:06 +05301418 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
Ashish Jainac510a72016-04-28 12:22:58 +05301419
Ashish Jain771d02f2016-10-27 22:35:46 +05301420 // Direct PCM is allowed only if
1421 // In case of mediaPlayer playback
1422 // 16 bit direct pcm or 24bit direct PCM property is set and
1423 // the FLAG requested is DIRECT_PCM ( NuPlayer case) or
1424 // In case of AudioTracks created by apps
1425 // track offload is enabled and FLAG requested is FLAG_NONE.
1426
Ashish Jain27564312016-11-14 14:58:06 +05301427 if (offloadDisabled) {
1428 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
1429 }
1430
Ashish Jain771d02f2016-10-27 22:35:46 +05301431 if (*flags == AUDIO_OUTPUT_FLAG_DIRECT_PCM) {
1432 if (bitWidth == 24 || bitWidth == 32)
1433 playerDirectPCM =
1434 property_get_bool("audio.offload.pcm.24bit.enable", false);
1435 else
1436 playerDirectPCM =
1437 property_get_bool("audio.offload.pcm.16bit.enable", false);
1438 // Reset flag to NONE so that we can still reuse direct pcm criteria check
1439 // in getOutputforDevice
1440 *flags = AUDIO_OUTPUT_FLAG_NONE;
Sharad Sangle75170722016-11-21 16:21:06 +05301441 } else if ((*flags == AUDIO_OUTPUT_FLAG_NONE) && (samplingRate % SAMPLE_RATE_8000 == 0)) {
Ashish Jain771d02f2016-10-27 22:35:46 +05301442 trackDirectPCM = property_get_bool("audio.offload.track.enable", true);
1443 }
1444
Ashish Jain27564312016-11-14 14:58:06 +05301445 ALOGI("Direct PCM %s for this request",
1446 (!offloadDisabled && (trackDirectPCM || playerDirectPCM))?"can be enabled":"is disabled");
1447
1448 return (!offloadDisabled && (trackDirectPCM || playerDirectPCM));
Ashish Jainac510a72016-04-28 12:22:58 +05301449}
1450
vivek mehta0ea887a2015-08-26 14:01:20 -07001451status_t AudioPolicyManagerCustom::getOutputForAttr(const audio_attributes_t *attr,
1452 audio_io_handle_t *output,
1453 audio_session_t session,
1454 audio_stream_type_t *stream,
1455 uid_t uid,
1456 uint32_t samplingRate,
1457 audio_format_t format,
1458 audio_channel_mask_t channelMask,
1459 audio_output_flags_t flags,
1460 audio_port_handle_t selectedDeviceId,
1461 const audio_offload_info_t *offloadInfo)
1462{
1463 audio_offload_info_t tOffloadInfo = AUDIO_INFO_INITIALIZER;
1464
Ashish Jainac510a72016-04-28 12:22:58 +05301465 uint32_t bitWidth = (audio_bytes_per_sample(format) * 8);
Alexy Joseph0ac09032015-10-16 15:57:53 -07001466
Alexy Joseph0ac09032015-10-16 15:57:53 -07001467
Sharad Sangle75170722016-11-21 16:21:06 +05301468 if (tryForDirectPCM(bitWidth, &flags, samplingRate) &&
Ashish Jain27564312016-11-14 14:58:06 +05301469 (offloadInfo == NULL)) {
vivek mehta0ea887a2015-08-26 14:01:20 -07001470
vivek mehta0ea887a2015-08-26 14:01:20 -07001471 tOffloadInfo.sample_rate = samplingRate;
1472 tOffloadInfo.channel_mask = channelMask;
1473 tOffloadInfo.format = format;
1474 tOffloadInfo.stream_type = *stream;
Ashish Jainac510a72016-04-28 12:22:58 +05301475 tOffloadInfo.bit_width = bitWidth;
vivek mehta0ea887a2015-08-26 14:01:20 -07001476 if (attr != NULL) {
1477 ALOGV("found attribute .. setting usage %d ", attr->usage);
1478 tOffloadInfo.usage = attr->usage;
1479 } else {
Alexy Joseph0ac09032015-10-16 15:57:53 -07001480 ALOGI("%s:: attribute is NULL .. no usage set", __func__);
vivek mehta0ea887a2015-08-26 14:01:20 -07001481 }
1482 offloadInfo = &tOffloadInfo;
1483 }
1484
1485 return AudioPolicyManager::getOutputForAttr(attr, output, session, stream,
1486 (uid_t)uid, (uint32_t)samplingRate,
1487 format, (audio_channel_mask_t)channelMask,
1488 flags, (audio_port_handle_t)selectedDeviceId,
1489 offloadInfo);
1490}
1491
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001492audio_io_handle_t AudioPolicyManagerCustom::getOutputForDevice(
1493 audio_devices_t device,
Sharad Sangle36781612015-05-28 16:15:16 +05301494 audio_session_t session __unused,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001495 audio_stream_type_t stream,
1496 uint32_t samplingRate,
1497 audio_format_t format,
1498 audio_channel_mask_t channelMask,
1499 audio_output_flags_t flags,
1500 const audio_offload_info_t *offloadInfo)
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001501{
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001502 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001503 status_t status;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001504
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001505#ifdef AUDIO_POLICY_TEST
1506 if (mCurOutput != 0) {
1507 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1508 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1509
1510 if (mTestOutputs[mCurOutput] == 0) {
1511 ALOGV("getOutput() opening test output");
Sharad Sangle36781612015-05-28 16:15:16 +05301512 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
1513 mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001514 outputDesc->mDevice = mTestDevice;
1515 outputDesc->mLatency = mTestLatencyMs;
1516 outputDesc->mFlags =
1517 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
1518 outputDesc->mRefCount[stream] = 0;
1519 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1520 config.sample_rate = mTestSamplingRate;
1521 config.channel_mask = mTestChannels;
1522 config.format = mTestFormat;
1523 if (offloadInfo != NULL) {
1524 config.offload_info = *offloadInfo;
1525 }
1526 status = mpClientInterface->openOutput(0,
1527 &mTestOutputs[mCurOutput],
1528 &config,
1529 &outputDesc->mDevice,
1530 String8(""),
1531 &outputDesc->mLatency,
1532 outputDesc->mFlags);
1533 if (status == NO_ERROR) {
1534 outputDesc->mSamplingRate = config.sample_rate;
1535 outputDesc->mFormat = config.format;
1536 outputDesc->mChannelMask = config.channel_mask;
1537 AudioParameter outputCmd = AudioParameter();
1538 outputCmd.addInt(String8("set_id"),mCurOutput);
1539 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1540 addOutput(mTestOutputs[mCurOutput], outputDesc);
1541 }
1542 }
1543 return mTestOutputs[mCurOutput];
1544 }
1545#endif //AUDIO_POLICY_TEST
Sharad Sangle36781612015-05-28 16:15:16 +05301546 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1547 (stream != AUDIO_STREAM_MUSIC)) {
1548 // compress should not be used for non-music streams
1549 ALOGE("Offloading only allowed with music stream");
1550 return 0;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301551 }
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301552
1553 if ((stream == AUDIO_STREAM_VOICE_CALL) &&
1554 (channelMask == 1) &&
Shiv Maliyappanahallid77d3b32016-02-02 13:45:50 -08001555 (samplingRate == 8000 || samplingRate == 16000 ||
1556 samplingRate == 32000 || samplingRate == 48000)) {
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301557 // Allow Voip direct output only if:
1558 // audio mode is MODE_IN_COMMUNCATION; AND
1559 // voip output is not opened already; AND
1560 // requested sample rate matches with that of voip input stream (if opened already)
1561 int value = 0;
1562 uint32_t mode = 0, voipOutCount = 1, voipSampleRate = 1;
1563 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1564 String8("audio_mode"));
1565 AudioParameter result = AudioParameter(valueStr);
1566 if (result.getInt(String8("audio_mode"), value) == NO_ERROR) {
1567 mode = value;
1568 }
1569
1570 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1571 String8("voip_out_stream_count"));
1572 result = AudioParameter(valueStr);
1573 if (result.getInt(String8("voip_out_stream_count"), value) == NO_ERROR) {
1574 voipOutCount = value;
1575 }
1576
1577 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1578 String8("voip_sample_rate"));
1579 result = AudioParameter(valueStr);
1580 if (result.getInt(String8("voip_sample_rate"), value) == NO_ERROR) {
1581 voipSampleRate = value;
1582 }
1583
1584 if ((mode == AUDIO_MODE_IN_COMMUNICATION) && (voipOutCount == 0) &&
1585 ((voipSampleRate == 0) || (voipSampleRate == samplingRate))) {
1586 if (audio_is_linear_pcm(format)) {
1587 char propValue[PROPERTY_VALUE_MAX] = {0};
1588 property_get("use.voice.path.for.pcm.voip", propValue, "0");
1589 bool voipPcmSysPropEnabled = !strncmp("true", propValue, sizeof("true"));
1590 if (voipPcmSysPropEnabled && (format == AUDIO_FORMAT_PCM_16_BIT)) {
Karthik Reddy Katta3fb9be72015-11-25 11:18:27 +05301591 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX |
1592 AUDIO_OUTPUT_FLAG_DIRECT);
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301593 ALOGD("Set VoIP and Direct output flags for PCM format");
1594 }
1595 }
1596 }
1597 }
1598
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301599#ifdef VOICE_CONCURRENCY
1600 char propValue[PROPERTY_VALUE_MAX];
1601 bool prop_play_enabled=false, prop_voip_enabled = false;
1602
1603 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1604 prop_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001605 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301606
1607 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1608 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1609 }
1610
1611 if (prop_play_enabled && mvoice_call_state) {
1612 //check if voice call is active / running in background
1613 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1614 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1615 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1616 {
1617 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1618 if(prop_voip_enabled) {
1619 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1620 flags );
1621 return 0;
1622 }
1623 }
1624 else {
1625 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
1626 ALOGD("voice_conc:IN call mode adding ULL flags .. flags: %x ", flags );
1627 flags = AUDIO_OUTPUT_FLAG_FAST;
1628 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
1629 if (AUDIO_STREAM_MUSIC == stream) {
1630 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1631 ALOGD("voice_conc:IN call mode adding deep-buffer flags %x ", flags );
1632 }
1633 else {
1634 flags = AUDIO_OUTPUT_FLAG_FAST;
1635 ALOGD("voice_conc:IN call mode adding fast flags %x ", flags );
1636 }
1637 }
1638 }
1639 }
1640 } else if (prop_voip_enabled && mvoice_call_state) {
1641 //check if voice call is active / running in background
1642 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1643 //return only ULL ouput
1644 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1645 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1646 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1647 {
1648 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1649 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1650 flags );
1651 return 0;
1652 }
1653 }
1654 }
1655#endif
1656#ifdef RECORD_PLAY_CONCURRENCY
1657 char recConcPropValue[PROPERTY_VALUE_MAX];
1658 bool prop_rec_play_enabled = false;
1659
1660 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
1661 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
1662 }
1663 if ((prop_rec_play_enabled) &&
Lalit Kansara33918092016-12-06 22:42:26 +05301664 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCountOnDevices() > 0))) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301665 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
1666 if (AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1667 // allow VoIP using voice path
1668 // Do nothing
1669 } else if((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1670 ALOGD("voice_conc:MODE_IN_COMM is setforcing deep buffer output for non ULL... flags: %x", flags);
1671 // use deep buffer path for all non ULL outputs
1672 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1673 }
1674 } else if ((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1675 ALOGD("voice_conc:Record mode is on forcing deep buffer output for non ULL... flags: %x ", flags);
1676 // use deep buffer path for all non ULL outputs
1677 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1678 }
1679 }
1680 if (prop_rec_play_enabled &&
1681 (stream == AUDIO_STREAM_ENFORCED_AUDIBLE)) {
1682 ALOGD("Record conc is on forcing ULL output for ENFORCED_AUDIBLE");
1683 flags = AUDIO_OUTPUT_FLAG_FAST;
1684 }
1685#endif
1686
Sharad Sangle4509cef2015-08-19 20:47:12 +05301687#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Sharad Sangle36781612015-05-28 16:15:16 +05301688 /*
1689 * WFD audio routes back to target speaker when starting a ringtone playback.
1690 * This is because primary output is reused for ringtone, so output device is
1691 * updated based on SONIFICATION strategy for both ringtone and music playback.
1692 * The same issue is not seen on remoted_submix HAL based WFD audio because
1693 * primary output is not reused and a new output is created for ringtone playback.
1694 * Issue is fixed by updating output flag to AUDIO_OUTPUT_FLAG_FAST when there is
1695 * a non-music stream playback on WFD, so primary output is not reused for ringtone.
1696 */
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001697 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
1698 if ((availableOutputDeviceTypes & AUDIO_DEVICE_OUT_PROXY)
1699 && (stream != AUDIO_STREAM_MUSIC)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301700 ALOGD("WFD audio: use OUTPUT_FLAG_FAST for non music stream. flags:%x", flags );
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001701 //For voip paths
1702 if(flags & AUDIO_OUTPUT_FLAG_DIRECT)
1703 flags = AUDIO_OUTPUT_FLAG_DIRECT;
1704 else //route every thing else to ULL path
1705 flags = AUDIO_OUTPUT_FLAG_FAST;
1706 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301707#endif
1708
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001709 // open a direct output if required by specified parameters
vivek mehta0ea887a2015-08-26 14:01:20 -07001710 // force direct flag if offload flag is set: offloading implies a direct output stream
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001711 // and all common behaviors are driven by checking only the direct flag
1712 // this should normally be set appropriately in the policy configuration file
1713 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1714 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1715 }
1716 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1717 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1718 }
vivek mehta0ea887a2015-08-26 14:01:20 -07001719
vivek mehta4b0d8192015-10-16 00:25:59 -07001720 bool forced_deep = false;
Sharad Sangle36781612015-05-28 16:15:16 +05301721 // only allow deep buffering for music stream type
1722 if (stream != AUDIO_STREAM_MUSIC) {
1723 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Sharad Sangle497aef82015-08-03 17:55:48 +05301724 } else if (/* stream == AUDIO_STREAM_MUSIC && */
1725 flags == AUDIO_OUTPUT_FLAG_NONE &&
1726 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
vivek mehtac984b992015-11-25 13:28:55 -08001727 forced_deep = true;
Sharad Sangle36781612015-05-28 16:15:16 +05301728 }
Sharad Sangle497aef82015-08-03 17:55:48 +05301729
Sharad Sangle36781612015-05-28 16:15:16 +05301730 if (stream == AUDIO_STREAM_TTS) {
1731 flags = AUDIO_OUTPUT_FLAG_TTS;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001732 }
1733
Zhou Song2b3d1552016-03-21 17:21:04 +08001734 // check if direct output for track offload already exits
1735 bool is_track_offload_active = false;
1736 for (size_t i = 0; i < mOutputs.size(); i++) {
1737 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1738 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) {
1739 is_track_offload_active = true;
1740 ALOGD("Track offload already active");
1741 break;
1742 }
1743 }
1744
vivek mehta4b0d8192015-10-16 00:25:59 -07001745 // Do offload magic here
vivek mehtac984b992015-11-25 13:28:55 -08001746 if ((flags == AUDIO_OUTPUT_FLAG_NONE) &&
1747 (stream == AUDIO_STREAM_MUSIC) &&
Zhou Song2b3d1552016-03-21 17:21:04 +08001748 (offloadInfo != NULL) && !is_track_offload_active &&
vivek mehta4b0d8192015-10-16 00:25:59 -07001749 ((offloadInfo->usage == AUDIO_USAGE_MEDIA) || (offloadInfo->usage == AUDIO_USAGE_GAME))) {
vivek mehtac984b992015-11-25 13:28:55 -08001750 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DIRECT_PCM);
vivek mehta4b0d8192015-10-16 00:25:59 -07001751 ALOGD("AudioCustomHAL --> Force Direct Flag .. flag (0x%x)", flags);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301752 }
1753
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001754 sp<IOProfile> profile;
1755
1756 // skip direct output selection if the request can obviously be attached to a mixed output
1757 // and not explicitly requested
vivek mehtac984b992015-11-25 13:28:55 -08001758 if (((flags & (AUDIO_OUTPUT_FLAG_DIRECT|AUDIO_OUTPUT_FLAG_DIRECT_PCM)) == 0) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301759 audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX &&
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001760 audio_channel_count_from_out_mask(channelMask) <= 2) {
1761 goto non_direct_output;
1762 }
1763
1764 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1765 // creating an offloaded track and tearing it down immediately after start when audioflinger
1766 // detects there is an active non offloadable effect.
1767 // FIXME: We should check the audio session here but we do not have it in this context.
1768 // This may prevent offloading in rare situations where effects are left active by apps
1769 // in the background.
Weiyin Jiang6bba3202016-03-03 14:49:36 +08001770 //
1771 // Supplementary annotation:
1772 // For sake of track offload introduced, we need a rollback for both compress offload
1773 // and track offload use cases.
1774 if ((flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD|AUDIO_OUTPUT_FLAG_DIRECT_PCM)) &&
1775 (mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
1776 ALOGD("non offloadable effect is enabled, try with non direct output");
1777 goto non_direct_output;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001778 }
1779
Weiyin Jiang6bba3202016-03-03 14:49:36 +08001780 profile = getProfileForDirectOutput(device,
1781 samplingRate,
1782 format,
1783 channelMask,
1784 (audio_output_flags_t)flags);
1785
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001786 if (profile != 0) {
vivek mehtac984b992015-11-25 13:28:55 -08001787
1788 if (!(flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301789 (profile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT_PCM)) {
vivek mehtac984b992015-11-25 13:28:55 -08001790 ALOGI("got Direct_PCM without requesting ... reject ");
1791 profile = NULL;
1792 goto non_direct_output;
1793 }
1794
Sharad Sangle36781612015-05-28 16:15:16 +05301795 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001796
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001797 // if multiple concurrent offload decode is supported
1798 // do no check for reuse and also don't close previous output if its offload
1799 // previous output will be closed during track destruction
1800 if (!(property_get_bool("audio.offload.multiple.enabled", false) &&
1801 ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0))) {
1802 for (size_t i = 0; i < mOutputs.size(); i++) {
1803 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1804 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1805 outputDesc = desc;
1806 // reuse direct output if currently open and configured with same parameters
1807 if ((samplingRate == outputDesc->mSamplingRate) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301808 audio_formats_match(format, outputDesc->mFormat) &&
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001809 (channelMask == outputDesc->mChannelMask)) {
1810 outputDesc->mDirectOpenCount++;
1811 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1812 return mOutputs.keyAt(i);
1813 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001814 }
1815 }
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001816 // close direct output if currently open and configured with different parameters
1817 if (outputDesc != NULL) {
1818 closeOutput(outputDesc->mIoHandle);
1819 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001820 }
Sharad Sangle36781612015-05-28 16:15:16 +05301821
1822 // if the selected profile is offloaded and no offload info was specified,
1823 // create a default one
1824 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
Ashish Jain40cab3a2016-03-23 11:14:14 +05301825 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
Sharad Sangle36781612015-05-28 16:15:16 +05301826 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1827 defaultOffloadInfo.sample_rate = samplingRate;
1828 defaultOffloadInfo.channel_mask = channelMask;
1829 defaultOffloadInfo.format = format;
1830 defaultOffloadInfo.stream_type = stream;
1831 defaultOffloadInfo.bit_rate = 0;
1832 defaultOffloadInfo.duration_us = -1;
1833 defaultOffloadInfo.has_video = true; // conservative
1834 defaultOffloadInfo.is_streaming = true; // likely
1835 offloadInfo = &defaultOffloadInfo;
1836 }
1837
1838 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001839 outputDesc->mDevice = device;
1840 outputDesc->mLatency = 0;
Sharad Sangle36781612015-05-28 16:15:16 +05301841 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001842 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1843 config.sample_rate = samplingRate;
1844 config.channel_mask = channelMask;
1845 config.format = format;
1846 if (offloadInfo != NULL) {
1847 config.offload_info = *offloadInfo;
1848 }
Sharad Sangle36781612015-05-28 16:15:16 +05301849 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001850 &output,
1851 &config,
1852 &outputDesc->mDevice,
1853 String8(""),
1854 &outputDesc->mLatency,
1855 outputDesc->mFlags);
1856
1857 // only accept an output with the requested parameters
1858 if (status != NO_ERROR ||
1859 (samplingRate != 0 && samplingRate != config.sample_rate) ||
Ashish Jain40cab3a2016-03-23 11:14:14 +05301860 (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) ||
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001861 (channelMask != 0 && channelMask != config.channel_mask)) {
1862 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1863 "format %d %d, channelMask %04x %04x", output, samplingRate,
1864 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1865 outputDesc->mChannelMask);
1866 if (output != AUDIO_IO_HANDLE_NONE) {
1867 mpClientInterface->closeOutput(output);
1868 }
Sharad Sangle36781612015-05-28 16:15:16 +05301869 // fall back to mixer output if possible when the direct output could not be open
Ashish Jain40cab3a2016-03-23 11:14:14 +05301870 if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) {
Sharad Sangle36781612015-05-28 16:15:16 +05301871 goto non_direct_output;
1872 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001873 return AUDIO_IO_HANDLE_NONE;
1874 }
1875 outputDesc->mSamplingRate = config.sample_rate;
1876 outputDesc->mChannelMask = config.channel_mask;
1877 outputDesc->mFormat = config.format;
1878 outputDesc->mRefCount[stream] = 0;
1879 outputDesc->mStopTime[stream] = 0;
1880 outputDesc->mDirectOpenCount = 1;
1881
1882 audio_io_handle_t srcOutput = getOutputForEffect();
1883 addOutput(output, outputDesc);
1884 audio_io_handle_t dstOutput = getOutputForEffect();
1885 if (dstOutput == output) {
Gao Jiedb057832015-11-12 08:51:22 +08001886#ifdef DOLBY_ENABLE
1887 status_t status = mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1888 if (status == NO_ERROR) {
1889 for (size_t i = 0; i < mEffects.size(); i++) {
1890 sp<EffectDescriptor> desc = mEffects.valueAt(i);
1891 if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX) {
1892 // update the mIo member of EffectDescriptor for the global effect
1893 ALOGV("%s updating mIo", __FUNCTION__);
1894 desc->mIo = dstOutput;
1895 }
1896 }
1897 } else {
1898 ALOGW("%s moveEffects from %d to %d failed", __FUNCTION__, srcOutput, dstOutput);
1899 }
1900#else // DOLBY_END
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001901 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
Gao Jiedb057832015-11-12 08:51:22 +08001902#endif // LINE_ADDED_BY_DOLBY
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001903 }
1904 mPreviousOutputs = mOutputs;
1905 ALOGV("getOutput() returns new direct output %d", output);
1906 mpClientInterface->onAudioPortListUpdate();
1907 return output;
1908 }
1909
1910non_direct_output:
Ashish Jain5fe12412016-04-18 18:44:17 +05301911
1912 // A request for HW A/V sync cannot fallback to a mixed output because time
1913 // stamps are embedded in audio data
1914 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1915 return AUDIO_IO_HANDLE_NONE;
1916 }
1917
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001918 // ignoring channel mask due to downmix capability in mixer
1919
1920 // open a non direct output
1921
1922 // for non direct outputs, only PCM is supported
1923 if (audio_is_linear_pcm(format)) {
1924 // get which output is suitable for the specified stream. The actual
1925 // routing change will happen when startOutput() will be called
1926 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1927
1928 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1929 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
Ashish Jain660d3112016-06-20 10:16:51 +05301930
1931 if (forced_deep) {
1932 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
1933 ALOGI("setting force DEEP buffer now ");
1934 } else if(flags == AUDIO_OUTPUT_FLAG_NONE) {
1935 // no deep buffer playback is requested hence fallback to primary
1936 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_PRIMARY);
1937 ALOGI("FLAG None hence request for a primary output");
1938 }
1939
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001940 output = selectOutput(outputs, flags, format);
1941 }
1942 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1943 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1944
vivek mehta0ea887a2015-08-26 14:01:20 -07001945 ALOGV("getOutputForDevice() returns output %d", output);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001946
1947 return output;
1948}
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301949
1950status_t AudioPolicyManagerCustom::getInputForAttr(const audio_attributes_t *attr,
1951 audio_io_handle_t *input,
1952 audio_session_t session,
1953 uid_t uid,
1954 uint32_t samplingRate,
1955 audio_format_t format,
1956 audio_channel_mask_t channelMask,
1957 audio_input_flags_t flags,
1958 audio_port_handle_t selectedDeviceId,
1959 input_type_t *inputType)
1960{
Ashish Jain40cab3a2016-03-23 11:14:14 +05301961 audio_source_t inputSource;
1962 inputSource = attr->source;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301963#ifdef VOICE_CONCURRENCY
1964
1965 char propValue[PROPERTY_VALUE_MAX];
1966 bool prop_rec_enabled=false, prop_voip_enabled = false;
1967
1968 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
1969 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1970 }
1971
1972 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1973 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1974 }
1975
1976 if (prop_rec_enabled && mvoice_call_state) {
1977 //check if voice call is active / running in background
1978 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1979 //Need to block input request
1980 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1981 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1982 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1983 {
1984 switch(inputSource) {
1985 case AUDIO_SOURCE_VOICE_UPLINK:
1986 case AUDIO_SOURCE_VOICE_DOWNLINK:
1987 case AUDIO_SOURCE_VOICE_CALL:
1988 ALOGD("voice_conc:Creating input during incall mode for inputSource: %d",
1989 inputSource);
1990 break;
1991
1992 case AUDIO_SOURCE_VOICE_COMMUNICATION:
1993 if(prop_voip_enabled) {
1994 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1995 inputSource);
1996 return NO_INIT;
1997 }
1998 break;
1999 default:
2000 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
2001 inputSource);
2002 return NO_INIT;
2003 }
2004 }
2005 }//check for VoIP flag
2006 else if(prop_voip_enabled && mvoice_call_state) {
2007 //check if voice call is active / running in background
2008 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
2009 //Need to block input request
2010 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
2011 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
2012 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
2013 {
2014 if(inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION) {
2015 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
2016 return NO_INIT;
2017 }
2018 }
2019 }
2020
2021#endif
2022
2023 return AudioPolicyManager::getInputForAttr(attr,
2024 input,
2025 session,
2026 uid,
2027 samplingRate,
2028 format,
2029 channelMask,
2030 flags,
2031 selectedDeviceId,
2032 inputType);
2033}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08002034
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302035status_t AudioPolicyManagerCustom::startInput(audio_io_handle_t input,
2036 audio_session_t session)
2037{
2038 ALOGV("startInput() input %d", input);
2039 ssize_t index = mInputs.indexOfKey(input);
2040 if (index < 0) {
2041 ALOGW("startInput() unknown input %d", input);
2042 return BAD_VALUE;
2043 }
2044 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
2045
Ashish Jain40cab3a2016-03-23 11:14:14 +05302046 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
2047 if (audioSession == 0) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302048 ALOGW("startInput() unknown session %d on input %d", session, input);
2049 return BAD_VALUE;
2050 }
2051
2052 // virtual input devices are compatible with other input devices
2053 if (!is_virtual_input_device(inputDesc->mDevice)) {
2054
2055 // for a non-virtual input device, check if there is another (non-virtual) active input
2056 audio_io_handle_t activeInput = mInputs.getActiveInput();
2057 if (activeInput != 0 && activeInput != input) {
2058
2059 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
2060 // otherwise the active input continues and the new input cannot be started.
2061 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302062 if ((activeDesc->inputSource() == AUDIO_SOURCE_HOTWORD) &&
2063 !activeDesc->hasPreemptedSession(session)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302064 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302065 //FIXME: consider all active sessions
2066 AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions();
2067 audio_session_t activeSession = activeSessions.keyAt(0);
2068 SortedVector<audio_session_t> sessions =
2069 activeDesc->getPreemptedSessions();
2070 sessions.add(activeSession);
2071 inputDesc->setPreemptedSessions(sessions);
2072 stopInput(activeInput, activeSession);
2073 releaseInput(activeInput, activeSession);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302074 } else {
2075 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
2076 return INVALID_OPERATION;
2077 }
2078 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05302079 // Do not allow capture if an active voice call is using a software patch and
2080 // the call TX source device is on the same HW module.
2081 // FIXME: would be better to refine to only inputs whose profile connects to the
2082 // call TX device but this information is not in the audio patch
2083 if (mCallTxPatch != 0 &&
2084 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
2085 return INVALID_OPERATION;
2086 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302087 }
2088
2089 // Routing?
2090 mInputRoutes.incRouteActivity(session);
2091#ifdef RECORD_PLAY_CONCURRENCY
2092 mIsInputRequestOnProgress = true;
2093
2094 char getPropValue[PROPERTY_VALUE_MAX];
2095 bool prop_rec_play_enabled = false;
2096
2097 if (property_get("rec.playback.conc.disabled", getPropValue, NULL)) {
2098 prop_rec_play_enabled = atoi(getPropValue) || !strncmp("true", getPropValue, 4);
2099 }
2100
Lalit Kansara33918092016-12-06 22:42:26 +05302101 if ((prop_rec_play_enabled) &&(mInputs.activeInputsCountOnDevices() == 0)){
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302102 // send update to HAL on record playback concurrency
2103 AudioParameter param = AudioParameter();
2104 param.add(String8("rec_play_conc_on"), String8("true"));
2105 ALOGD("startInput() setParameters rec_play_conc is setting to ON ");
2106 mpClientInterface->setParameters(0, param.toString());
2107
2108 // Call invalidate to reset all opened non ULL audio tracks
2109 // Move tracks associated to this strategy from previous output to new output
2110 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
2111 // Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder)
Sharad Sangle4509cef2015-08-19 20:47:12 +05302112 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE && (i != AUDIO_STREAM_PATCH))) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302113 ALOGD("Invalidate on releaseInput for stream :: %d ", i);
2114 //FIXME see fixme on name change
2115 mpClientInterface->invalidateStream((audio_stream_type_t)i);
2116 }
2117 }
2118 // close compress tracks
2119 for (size_t i = 0; i < mOutputs.size(); i++) {
2120 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
2121 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
2122 ALOGD("ouput desc / profile is NULL");
2123 continue;
2124 }
2125 if (outputDesc->mProfile->mFlags
2126 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2127 // close compress sessions
2128 ALOGD("calling closeOutput on record conc for COMPRESS output");
2129 closeOutput(mOutputs.keyAt(i));
2130 }
2131 }
Ashish Jaine4a22d52016-06-22 11:55:08 +05302132 // If effects where present on any of the above closed outputs,
2133 // audioflinger moved them to the primary output by default
2134 // move them back to the appropriate output.
2135 moveGlobalEffect();
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302136 }
2137#endif
2138
Ashish Jain40cab3a2016-03-23 11:14:14 +05302139 if (!inputDesc->isActive() || mInputRoutes.hasRouteChanged(session)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302140 // if input maps to a dynamic policy with an activity listener, notify of state change
Ashish Jain630c5e12016-04-18 11:50:21 +05302141 if ((inputDesc->mPolicyMix != NULL)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302142 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Ashish Jain5fe12412016-04-18 18:44:17 +05302143 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302144 MIX_STATE_MIXING);
Ashish Jain630c5e12016-04-18 11:50:21 +05302145 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302146
Lalit Kansara33918092016-12-06 22:42:26 +05302147 if (mInputs.activeInputsCountOnDevices() == 0) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302148 SoundTrigger::setCaptureState(true);
2149 }
2150 setInputDevice(input, getNewInputDevice(input), true /* force */);
2151
2152 // automatically enable the remote submix output when input is started if not
2153 // used by a policy mix of type MIX_TYPE_RECORDERS
2154 // For remote submix (a virtual device), we open only one input per capture request.
2155 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
2156 String8 address = String8("");
2157 if (inputDesc->mPolicyMix == NULL) {
2158 address = String8("0");
Ashish Jain630c5e12016-04-18 11:50:21 +05302159 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
Ashish Jain5fe12412016-04-18 18:44:17 +05302160 address = inputDesc->mPolicyMix->mDeviceAddress;
Ashish Jain630c5e12016-04-18 11:50:21 +05302161 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302162 if (address != "") {
2163 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2164 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2165 address, "remote-submix");
2166 }
2167 }
2168 }
2169
Ashish Jain40cab3a2016-03-23 11:14:14 +05302170 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302171
Ashish Jain40cab3a2016-03-23 11:14:14 +05302172 audioSession->changeActiveCount(1);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302173#ifdef RECORD_PLAY_CONCURRENCY
2174 mIsInputRequestOnProgress = false;
2175#endif
2176 return NO_ERROR;
2177}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08002178
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302179status_t AudioPolicyManagerCustom::stopInput(audio_io_handle_t input,
2180 audio_session_t session)
2181{
2182 status_t status;
2183 status = AudioPolicyManager::stopInput(input, session);
2184#ifdef RECORD_PLAY_CONCURRENCY
2185 char propValue[PROPERTY_VALUE_MAX];
2186 bool prop_rec_play_enabled = false;
2187
2188 if (property_get("rec.playback.conc.disabled", propValue, NULL)) {
2189 prop_rec_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
2190 }
2191
Lalit Kansara33918092016-12-06 22:42:26 +05302192 if ((prop_rec_play_enabled) && (mInputs.activeInputsCountOnDevices() == 0)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302193
2194 //send update to HAL on record playback concurrency
2195 AudioParameter param = AudioParameter();
2196 param.add(String8("rec_play_conc_on"), String8("false"));
2197 ALOGD("stopInput() setParameters rec_play_conc is setting to OFF ");
2198 mpClientInterface->setParameters(0, param.toString());
2199
2200 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
2201 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
2202 //Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder stop tone)
2203 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE) && (i != AUDIO_STREAM_PATCH)) {
2204 ALOGD(" Invalidate on stopInput for stream :: %d ", i);
2205 //FIXME see fixme on name change
2206 mpClientInterface->invalidateStream((audio_stream_type_t)i);
2207 }
2208 }
2209 }
2210#endif
2211 return status;
2212}
2213
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302214void AudioPolicyManagerCustom::closeAllInputs() {
2215 bool patchRemoved = false;
2216
Dhanalakshmi Siddanibfeca7f2015-10-08 15:17:11 +05302217 for(size_t input_index = mInputs.size(); input_index > 0; input_index--) {
2218 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index-1);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302219 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302220 if (patch_index >= 0) {
2221 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302222 status_t status;
2223 status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302224 mAudioPatches.removeItemsAt(patch_index);
2225 patchRemoved = true;
2226 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05302227 if ((inputDesc->getOpenRefCount() > 0) && inputDesc->isSoundTrigger()
2228 && (mInputs.size() == 1)) {
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302229 ALOGD("Do not close sound trigger input handle");
2230 } else {
Dhanalakshmi Siddanibfeca7f2015-10-08 15:17:11 +05302231 mpClientInterface->closeInput(mInputs.keyAt(input_index-1));
2232 mInputs.removeItem(mInputs.keyAt(input_index-1));
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302233 }
2234 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05302235 mInputs.clear();
2236 SoundTrigger::setCaptureState(false);
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302237 nextAudioPortGeneration();
2238
2239 if (patchRemoved) {
2240 mpClientInterface->onAudioPatchListUpdate();
2241 }
2242}
2243
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302244AudioPolicyManagerCustom::AudioPolicyManagerCustom(AudioPolicyClientInterface *clientInterface)
Sharad Sangle4509cef2015-08-19 20:47:12 +05302245 : AudioPolicyManager(clientInterface),
2246 mHdmiAudioDisabled(false),
2247 mHdmiAudioEvent(false),
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08002248 mPrevPhoneState(0),
Haynes Mathew George603ae9b2015-12-21 17:23:59 -08002249 mPrevFMVolumeDb(0.0f),
2250 mFMIsActive(false)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302251{
Ashish Jain40cab3a2016-03-23 11:14:14 +05302252
Satya Krishna Pindiproli8f83f102016-06-21 17:56:14 +05302253#ifdef USE_XML_AUDIO_POLICY_CONF
2254 ALOGD("USE_XML_AUDIO_POLICY_CONF is TRUE");
2255#else
2256 ALOGD("USE_XML_AUDIO_POLICY_CONF is FALSE");
2257#endif
2258
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302259#ifdef RECORD_PLAY_CONCURRENCY
2260 mIsInputRequestOnProgress = false;
2261#endif
2262
2263
2264#ifdef VOICE_CONCURRENCY
2265 mFallBackflag = getFallBackPath();
2266#endif
2267}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07002268}