blob: 24d8f74d6ff51ae2640e542e31a3ca7f66e1d26a [file] [log] [blame]
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001/*
Weiyin Jiangd211ef52016-02-16 15:55:11 +08002 * Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07003 * Not a contribution.
4 *
5 * Copyright (C) 2009 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Gao Jiedb057832015-11-12 08:51:22 +080018 *
19 * This file was modified by Dolby Laboratories, Inc. The portions of the
20 * code that are surrounded by "DOLBY..." are copyrighted and
21 * licensed separately, as follows:
22 *
23 * (C) 2015 Dolby Laboratories, Inc.
24 *
25 * Licensed under the Apache License, Version 2.0 (the "License");
26 * you may not use this file except in compliance with the License.
27 * You may obtain a copy of the License at
28 *
29 * http://www.apache.org/licenses/LICENSE-2.0
30 *
31 * Unless required by applicable law or agreed to in writing, software
32 * distributed under the License is distributed on an "AS IS" BASIS,
33 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34 * See the License for the specific language governing permissions and
35 * limitations under the License.
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070036 */
37
Sharad Sangle36781612015-05-28 16:15:16 +053038#define LOG_TAG "AudioPolicyManagerCustom"
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070039//#define LOG_NDEBUG 0
40
41//#define VERY_VERBOSE_LOGGING
42#ifdef VERY_VERBOSE_LOGGING
43#define ALOGVV ALOGV
44#else
45#define ALOGVV(a...) do { } while(0)
46#endif
Sharad Sangle36781612015-05-28 16:15:16 +053047
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070048// A device mask for all audio output devices that are considered "remote" when evaluating
49// active output devices in isStreamActiveRemotely()
50#define APM_AUDIO_OUT_DEVICE_REMOTE_ALL AUDIO_DEVICE_OUT_REMOTE_SUBMIX
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070051// A device mask for all audio input and output devices where matching inputs/outputs on device
52// type alone is not enough: the address must match too
53#define APM_AUDIO_DEVICE_MATCH_ADDRESS_ALL (AUDIO_DEVICE_IN_REMOTE_SUBMIX | \
54 AUDIO_DEVICE_OUT_REMOTE_SUBMIX)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070055
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070056#include <inttypes.h>
Mingming Yin0ae14ea2014-07-09 17:55:56 -070057#include <math.h>
Mingming Yin0670f162014-06-12 16:05:49 -070058
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070059#include <cutils/properties.h>
60#include <utils/Log.h>
61#include <hardware/audio.h>
62#include <hardware/audio_effect.h>
63#include <media/AudioParameter.h>
64#include <soundtrigger/SoundTrigger.h>
65#include "AudioPolicyManager.h"
Sharad Sangle36781612015-05-28 16:15:16 +053066#include <policy.h>
Gao Jiedb057832015-11-12 08:51:22 +080067#ifdef DOLBY_ENABLE
68#include "DolbyAudioPolicy_impl.h"
69#endif // DOLBY_END
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070070
71namespace android {
Sharad Sanglec5766ff2015-06-04 20:24:10 +053072#ifdef VOICE_CONCURRENCY
73audio_output_flags_t AudioPolicyManagerCustom::getFallBackPath()
74{
75 audio_output_flags_t flag = AUDIO_OUTPUT_FLAG_FAST;
76 char propValue[PROPERTY_VALUE_MAX];
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070077
Sharad Sanglec5766ff2015-06-04 20:24:10 +053078 if (property_get("voice.conc.fallbackpath", propValue, NULL)) {
79 if (!strncmp(propValue, "deep-buffer", 11)) {
80 flag = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
81 }
82 else if (!strncmp(propValue, "fast", 4)) {
83 flag = AUDIO_OUTPUT_FLAG_FAST;
84 }
85 else {
86 ALOGD("voice_conc:not a recognised path(%s) in prop voice.conc.fallbackpath",
87 propValue);
88 }
89 }
90 else {
91 ALOGD("voice_conc:prop voice.conc.fallbackpath not set");
92 }
93
94 ALOGD("voice_conc:picked up flag(0x%x) from prop voice.conc.fallbackpath",
95 flag);
96
97 return flag;
98}
99#endif /*VOICE_CONCURRENCY*/
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700100// ----------------------------------------------------------------------------
101// AudioPolicyInterface implementation
102// ----------------------------------------------------------------------------
Sharad Sangle36781612015-05-28 16:15:16 +0530103extern "C" AudioPolicyInterface* createAudioPolicyManager(
104 AudioPolicyClientInterface *clientInterface)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700105{
Sharad Sangle36781612015-05-28 16:15:16 +0530106 return new AudioPolicyManagerCustom(clientInterface);
107}
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700108
Sharad Sangle36781612015-05-28 16:15:16 +0530109extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface)
110{
111 delete interface;
112}
113
114status_t AudioPolicyManagerCustom::setDeviceConnectionStateInt(audio_devices_t device,
115 audio_policy_dev_state_t state,
116 const char *device_address,
117 const char *device_name)
118{
Ashish Jain40cab3a2016-03-23 11:14:14 +0530119 ALOGD("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
Sharad Sangle36781612015-05-28 16:15:16 +0530120 device, state, device_address, device_name);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700121
122 // connect/disconnect only 1 device at a time
123 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
124
Sharad Sangle36781612015-05-28 16:15:16 +0530125 sp<DeviceDescriptor> devDesc =
126 mHwModules.getDeviceDescriptor(device, device_address, device_name);
127
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700128 // handle output devices
129 if (audio_is_output_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700130 SortedVector <audio_io_handle_t> outputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700131
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700132 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700133
134 // save a copy of the opened output descriptors before any output is opened or closed
135 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
136 mPreviousOutputs = mOutputs;
137 switch (state)
138 {
139 // handle output device connection
Sharad Sangle36781612015-05-28 16:15:16 +0530140 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700141 if (index >= 0) {
Sharad Sangle4509cef2015-08-19 20:47:12 +0530142#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
143 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
144 if (!strncmp(device_address, "hdmi_spkr", 9)) {
145 mHdmiAudioDisabled = false;
146 } else {
147 mHdmiAudioEvent = true;
148 }
149 }
150#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700151 ALOGW("setDeviceConnectionState() device already connected: %x", device);
152 return INVALID_OPERATION;
153 }
154 ALOGV("setDeviceConnectionState() connecting device %x", device);
155
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700156 // register new device as available
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700157 index = mAvailableOutputDevices.add(devDesc);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530158#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
159 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
160 if (!strncmp(device_address, "hdmi_spkr", 9)) {
161 mHdmiAudioDisabled = false;
162 } else {
163 mHdmiAudioEvent = true;
164 }
165 if (mHdmiAudioDisabled || !mHdmiAudioEvent) {
166 mAvailableOutputDevices.remove(devDesc);
167 ALOGW("HDMI sink not connected, do not route audio to HDMI out");
168 return INVALID_OPERATION;
169 }
170 }
171#endif
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700172 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530173 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700174 if (module == 0) {
175 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
176 device);
177 mAvailableOutputDevices.remove(devDesc);
178 return INVALID_OPERATION;
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700179 }
Sharad Sangle36781612015-05-28 16:15:16 +0530180 mAvailableOutputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700181 } else {
182 return NO_MEMORY;
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700183 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700184
Sharad Sangle36781612015-05-28 16:15:16 +0530185 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700186 mAvailableOutputDevices.remove(devDesc);
187 return INVALID_OPERATION;
188 }
Sharad Sangle36781612015-05-28 16:15:16 +0530189 // Propagate device availability to Engine
190 mEngine->setDeviceConnectionState(devDesc, state);
191
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700192 // outputs should never be empty here
193 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
194 "checkOutputsForDevice() returned no outputs but status OK");
195 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
196 outputs.size());
Sharad Sangle36781612015-05-28 16:15:16 +0530197
198 // Send connect to HALs
199 AudioParameter param = AudioParameter(devDesc->mAddress);
200 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
201 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
202
203 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700204 // handle output device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700205 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
206 if (index < 0) {
Sharad Sangle4509cef2015-08-19 20:47:12 +0530207#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
208 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
209 if (!strncmp(device_address, "hdmi_spkr", 9)) {
210 mHdmiAudioDisabled = true;
211 } else {
212 mHdmiAudioEvent = false;
213 }
214 }
215#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700216 ALOGW("setDeviceConnectionState() device not connected: %x", device);
217 return INVALID_OPERATION;
218 }
219
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700220 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
221
Sharad Sangle36781612015-05-28 16:15:16 +0530222 // Send Disconnect to HALs
223 AudioParameter param = AudioParameter(devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700224 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
225 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
226
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700227 // remove device from available output devices
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700228 mAvailableOutputDevices.remove(devDesc);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530229#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
230 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
231 if (!strncmp(device_address, "hdmi_spkr", 9)) {
232 mHdmiAudioDisabled = true;
233 } else {
234 mHdmiAudioEvent = false;
235 }
236 }
237#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530238 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
239
240 // Propagate device availability to Engine
241 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700242 } break;
243
244 default:
245 ALOGE("setDeviceConnectionState() invalid state: %x", state);
246 return BAD_VALUE;
247 }
248
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700249 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
250 // output is suspended before any tracks are moved to it
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700251 checkA2dpSuspend();
252 checkOutputForAllStrategies();
253 // outputs must be closed after checkOutputForAllStrategies() is executed
254 if (!outputs.isEmpty()) {
255 for (size_t i = 0; i < outputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530256 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700257 // close unused outputs after device disconnection or direct outputs that have been
258 // opened by checkOutputsForDevice() to query dynamic parameters
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700259 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700260 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
261 (desc->mDirectOpenCount == 0))) {
262 closeOutput(outputs[i]);
263 }
264 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700265 // check again after closing A2DP output to reset mA2dpSuspended if needed
266 checkA2dpSuspend();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700267 }
268
269 updateDevicesAndOutputs();
Gao Jiedb057832015-11-12 08:51:22 +0800270#ifdef DOLBY_ENABLE
271 // Before closing the opened outputs, update endpoint property with device capabilities
272 audio_devices_t audioOutputDevice = getDeviceForStrategy(getStrategy(AUDIO_STREAM_MUSIC), true);
273 mDolbyAudioPolicy.setEndpointSystemProperty(audioOutputDevice, mHwModules);
274#endif // DOLBY_END
Sharad Sangle36781612015-05-28 16:15:16 +0530275 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
276 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700277 updateCallRouting(newDevice);
278 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530279
280#ifdef FM_POWER_OPT
281 // handle FM device connection state to trigger FM AFE loopback
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800282 if (device == AUDIO_DEVICE_OUT_FM && hasPrimaryOutput()) {
Mingming Yin2dd77c22016-01-06 18:02:42 -0800283 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530284 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
285 mPrimaryOutput->changeRefCount(AUDIO_STREAM_MUSIC, 1);
Mingming Yin2dd77c22016-01-06 18:02:42 -0800286 newDevice = (audio_devices_t)(getNewOutputDevice(mPrimaryOutput, false)|AUDIO_DEVICE_OUT_FM);
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800287 mFMIsActive = true;
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530288 } else {
Mingming Yin2dd77c22016-01-06 18:02:42 -0800289 newDevice = (audio_devices_t)(getNewOutputDevice(mPrimaryOutput, false));
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800290 mFMIsActive = false;
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530291 mPrimaryOutput->changeRefCount(AUDIO_STREAM_MUSIC, -1);
292 }
293 AudioParameter param = AudioParameter();
294 param.addInt(String8("handle_fm"), (int)newDevice);
295 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString());
296 }
297#endif /* FM_POWER_OPT end */
298
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700299 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530300 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
301 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
302 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700303 // do not force device change on duplicated output because if device is 0, it will
304 // also force a device 0 for the two outputs it is duplicated to which may override
305 // a valid device selection on those outputs.
Sharad Sangle36781612015-05-28 16:15:16 +0530306 bool force = !desc->isDuplicated()
307 && (!device_distinguishes_on_address(device)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700308 // always force when disconnecting (a non-duplicated device)
309 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Sharad Sangle36781612015-05-28 16:15:16 +0530310 setOutputDevice(desc, newDevice, force, 0);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700311 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700312 }
313
Ashish Jain40cab3a2016-03-23 11:14:14 +0530314 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
315 cleanUpForDevice(devDesc);
316 }
317
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700318 mpClientInterface->onAudioPortListUpdate();
319 return NO_ERROR;
320 } // end if is output device
321
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700322 // handle input devices
323 if (audio_is_input_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700324 SortedVector <audio_io_handle_t> inputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700325
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700326 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700327 switch (state)
328 {
329 // handle input device connection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700330 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
331 if (index >= 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700332 ALOGW("setDeviceConnectionState() device already connected: %d", device);
333 return INVALID_OPERATION;
334 }
Sharad Sangle36781612015-05-28 16:15:16 +0530335 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700336 if (module == NULL) {
337 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
338 device);
339 return INVALID_OPERATION;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700340 }
Sharad Sangle36781612015-05-28 16:15:16 +0530341 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700342 return INVALID_OPERATION;
343 }
344
345 index = mAvailableInputDevices.add(devDesc);
346 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530347 mAvailableInputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700348 } else {
349 return NO_MEMORY;
350 }
Sharad Sangle36781612015-05-28 16:15:16 +0530351
352 // Set connect to HALs
353 AudioParameter param = AudioParameter(devDesc->mAddress);
354 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
355 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
356
357 // Propagate device availability to Engine
358 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700359 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700360
361 // handle input device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700362 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
363 if (index < 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700364 ALOGW("setDeviceConnectionState() device not connected: %d", device);
365 return INVALID_OPERATION;
366 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700367
368 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
369
370 // Set Disconnect to HALs
Sharad Sangle36781612015-05-28 16:15:16 +0530371 AudioParameter param = AudioParameter(devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700372 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
373 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
374
Sharad Sangle36781612015-05-28 16:15:16 +0530375 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700376 mAvailableInputDevices.remove(devDesc);
377
Sharad Sangle36781612015-05-28 16:15:16 +0530378 // Propagate device availability to Engine
379 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700380 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700381
382 default:
383 ALOGE("setDeviceConnectionState() invalid state: %x", state);
384 return BAD_VALUE;
385 }
386
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700387 closeAllInputs();
388
Sharad Sangle36781612015-05-28 16:15:16 +0530389 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700390 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
391 updateCallRouting(newDevice);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700392 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700393
Ashish Jain40cab3a2016-03-23 11:14:14 +0530394 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
395 cleanUpForDevice(devDesc);
396 }
397
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700398 mpClientInterface->onAudioPortListUpdate();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700399 return NO_ERROR;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700400 } // end if is input device
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700401
402 ALOGW("setDeviceConnectionState() invalid device: %x", device);
403 return BAD_VALUE;
404}
Weiyin Jiangd211ef52016-02-16 15:55:11 +0800405
Sharad Sangle36781612015-05-28 16:15:16 +0530406// This function checks for the parameters which can be offloaded.
407// This can be enhanced depending on the capability of the DSP and policy
408// of the system.
409bool AudioPolicyManagerCustom::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700410{
Sharad Sangle36781612015-05-28 16:15:16 +0530411 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
412 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
413 offloadInfo.sample_rate, offloadInfo.channel_mask,
414 offloadInfo.format,
415 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
416 offloadInfo.has_video);
Ashish Jain40cab3a2016-03-23 11:14:14 +0530417
418 if (mMasterMono) {
419 return false; // no offloading if mono is set.
420 }
421
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530422#ifdef VOICE_CONCURRENCY
423 char concpropValue[PROPERTY_VALUE_MAX];
424 if (property_get("voice.playback.conc.disabled", concpropValue, NULL)) {
425 bool propenabled = atoi(concpropValue) || !strncmp("true", concpropValue, 4);
426 if (propenabled) {
427 if (isInCall())
428 {
429 ALOGD("\n copl: blocking compress offload on call mode\n");
430 return false;
431 }
432 }
433 }
434#endif
435#ifdef RECORD_PLAY_CONCURRENCY
436 char recConcPropValue[PROPERTY_VALUE_MAX];
437 bool prop_rec_play_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700438
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530439 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
440 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
441 }
442
443 if ((prop_rec_play_enabled) &&
444 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
445 ALOGD("copl: blocking compress offload for record concurrency");
446 return false;
447 }
448#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530449 // Check if stream type is music, then only allow offload as of now.
450 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
451 {
452 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
453 return false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700454 }
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530455
Alexy Joseph0ac09032015-10-16 15:57:53 -0700456 // Check if offload has been disabled
457 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
458 if (offloadDisabled) {
459 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
460 return false;
461 }
462
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530463 char propValue[PROPERTY_VALUE_MAX];
464 bool pcmOffload = false;
465#ifdef PCM_OFFLOAD_ENABLED
466 if ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM_OFFLOAD) {
467 bool prop_enabled = false;
468 if ((AUDIO_FORMAT_PCM_16_BIT_OFFLOAD == offloadInfo.format) &&
469 property_get("audio.offload.pcm.16bit.enable", propValue, NULL)) {
470 prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
471 }
472
473#ifdef PCM_OFFLOAD_ENABLED_24
474 if ((AUDIO_FORMAT_PCM_24_BIT_OFFLOAD == offloadInfo.format) &&
475 property_get("audio.offload.pcm.24bit.enable", propValue, NULL)) {
476 prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530477 }
478#endif
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530479
480 if (prop_enabled) {
481 ALOGI("PCM offload property is enabled");
482 pcmOffload = true;
483 }
484
485 if (!pcmOffload) {
486 ALOGD("system property not enabled for PCM offload format[%x]",offloadInfo.format);
487 return false;
488 }
489 }
490#endif
491 if (!pcmOffload) {
Alexy Joseph0ac09032015-10-16 15:57:53 -0700492
493 bool compressedOffloadDisabled = property_get_bool("audio.offload.compress.disable", false);
494 if (compressedOffloadDisabled) {
495 ALOGI("compressed offload disabled by audio.offload.compress.disable=%d", compressedOffloadDisabled);
496 return false;
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530497 }
Alexy Joseph0ac09032015-10-16 15:57:53 -0700498
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530499 //check if it's multi-channel AAC (includes sub formats) and FLAC format
500 if ((popcount(offloadInfo.channel_mask) > 2) &&
501 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
502 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS))) {
503 ALOGD("offload disabled for multi-channel AAC,FLAC and VORBIS format");
504 return false;
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530505 }
506
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530507#ifdef AUDIO_EXTN_FORMATS_ENABLED
508 //check if it's multi-channel FLAC/ALAC/WMA format with sample rate > 48k
509 if ((popcount(offloadInfo.channel_mask) > 2) &&
510 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
Manish Dewangana6fc5442015-08-24 20:30:31 +0530511 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) && (offloadInfo.sample_rate > 48000)) ||
512 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) && (offloadInfo.sample_rate > 48000)) ||
513 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) && (offloadInfo.sample_rate > 48000)) ||
514 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS))) {
515 ALOGD("offload disabled for multi-channel FLAC/ALAC/WMA/AAC_ADTS clips with sample rate > 48kHz");
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530516 return false;
517 }
518#endif
519 //TODO: enable audio offloading with video when ready
520 const bool allowOffloadWithVideo =
521 property_get_bool("audio.offload.video", false /* default_value */);
522 if (offloadInfo.has_video && !allowOffloadWithVideo) {
523 ALOGV("isOffloadSupported: has_video == true, returning false");
524 return false;
525 }
Manish Dewanganf3cd0f82015-10-13 14:04:36 +0530526
527 const bool allowOffloadStreamingWithVideo = property_get_bool("av.streaming.offload.enable",
528 false /*default value*/);
529 if(offloadInfo.has_video && offloadInfo.is_streaming && !allowOffloadStreamingWithVideo) {
530 ALOGW("offload disabled by av.streaming.offload.enable = %s ", propValue );
531 return false;
532 }
533
Sharad Sangle36781612015-05-28 16:15:16 +0530534 }
535
536 //If duration is less than minimum value defined in property, return false
537 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
538 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
539 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
540 return false;
541 }
542 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
543 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
544 //duration checks only valid for MP3/AAC/ formats,
545 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
546 if ((offloadInfo.format == AUDIO_FORMAT_MP3) ||
547 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530548 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS) ||
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530549#ifdef AUDIO_EXTN_FORMATS_ENABLED
Sharad Sangle36781612015-05-28 16:15:16 +0530550 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
Sharad Sangle36781612015-05-28 16:15:16 +0530551 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) ||
552 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) ||
553 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) ||
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530554 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_APE) ||
Manish Dewangana6fc5442015-08-24 20:30:31 +0530555 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS) ||
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530556#endif
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530557 pcmOffload)
Sharad Sangle36781612015-05-28 16:15:16 +0530558 return false;
559
560 }
561
562 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
563 // creating an offloaded track and tearing it down immediately after start when audioflinger
564 // detects there is an active non offloadable effect.
565 // FIXME: We should check the audio session here but we do not have it in this context.
566 // This may prevent offloading in rare situations where effects are left active by apps
567 // in the background.
568 if (mEffects.isNonOffloadableEffectEnabled()) {
569 return false;
570 }
571 // Check for soundcard status
572 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
573 String8("SND_CARD_STATUS"));
574 AudioParameter result = AudioParameter(valueStr);
575 int isonline = 0;
576 if ((result.getInt(String8("SND_CARD_STATUS"), isonline) == NO_ERROR)
577 && !isonline) {
578 ALOGD("copl: soundcard is offline rejecting offload request");
579 return false;
580 }
581 // See if there is a profile to support this.
582 // AUDIO_DEVICE_NONE
583 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
584 offloadInfo.sample_rate,
585 offloadInfo.format,
586 offloadInfo.channel_mask,
587 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
588 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
589 return (profile != 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700590}
Weiyin Jiangd211ef52016-02-16 15:55:11 +0800591
Sharad Sangle36781612015-05-28 16:15:16 +0530592audio_devices_t AudioPolicyManagerCustom::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
593 bool fromCache)
594{
595 audio_devices_t device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700596
Ashish Jain40cab3a2016-03-23 11:14:14 +0530597 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Sharad Sangle36781612015-05-28 16:15:16 +0530598 if (index >= 0) {
599 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
600 if (patchDesc->mUid != mUidCached) {
601 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Ashish Jain40cab3a2016-03-23 11:14:14 +0530602 outputDesc->device(), outputDesc->getPatchHandle());
Sharad Sangle36781612015-05-28 16:15:16 +0530603 return outputDesc->device();
604 }
605 }
606
607 // check the following by order of priority to request a routing change if necessary:
608 // 1: the strategy enforced audible is active and enforced on the output:
609 // use device for strategy enforced audible
610 // 2: we are in call or the strategy phone is active on the output:
611 // use device for strategy phone
612 // 3: the strategy for enforced audible is active but not enforced on the output:
613 // use the device for strategy enforced audible
614 // 4: the strategy sonification is active on the output:
615 // use device for strategy sonification
616 // 5: the strategy "respectful" sonification is active on the output:
617 // use device for strategy "respectful" sonification
618 // 6: the strategy accessibility is active on the output:
619 // use device for strategy accessibility
620 // 7: the strategy media is active on the output:
621 // use device for strategy media
622 // 8: the strategy DTMF is active on the output:
623 // use device for strategy DTMF
624 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
625 // use device for strategy t-t-s
626 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
627 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
628 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
629 } else if (isInCall() ||
630 isStrategyActive(outputDesc, STRATEGY_PHONE)||
631 isStrategyActive(mPrimaryOutput, STRATEGY_PHONE)) {
632 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
633 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
634 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
635 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)||
636 (isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION)
637 && (!isStrategyActive(mPrimaryOutput,STRATEGY_MEDIA)))) {
638 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530639 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL) ||
640 isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION_RESPECTFUL)) {
Sharad Sangle36781612015-05-28 16:15:16 +0530641 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
642 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
643 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
644 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
645 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
646 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
647 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
648 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
649 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
650 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
651 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
652 }
653
654 ALOGV("getNewOutputDevice() selected device %x", device);
655 return device;
656}
Weiyin Jiangd211ef52016-02-16 15:55:11 +0800657
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700658void AudioPolicyManagerCustom::setPhoneState(audio_mode_t state)
659{
Ashish Jain40cab3a2016-03-23 11:14:14 +0530660 ALOGD("setPhoneState() state %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530661 // store previous phone state for management of sonification strategy below
Sharad Sangle4509cef2015-08-19 20:47:12 +0530662 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Sharad Sangle36781612015-05-28 16:15:16 +0530663 int oldState = mEngine->getPhoneState();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700664
Sharad Sangle36781612015-05-28 16:15:16 +0530665 if (mEngine->setPhoneState(state) != NO_ERROR) {
666 ALOGW("setPhoneState() invalid or same state %d", state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700667 return;
668 }
Sharad Sangle36781612015-05-28 16:15:16 +0530669 /// Opens: can these line be executed after the switch of volume curves???
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700670 // if leaving call state, handle special case of active streams
671 // pertaining to sonification strategy see handleIncallSonification()
Zhou Song58081b62016-02-24 13:10:55 +0800672 if (isStateInCall(oldState)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700673 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530674 for (size_t j = 0; j < mOutputs.size(); j++) {
675 audio_io_handle_t curOutput = mOutputs.keyAt(j);
676 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
677 if (stream == AUDIO_STREAM_PATCH) {
678 continue;
679 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530680 handleIncallSonification((audio_stream_type_t)stream, false, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +0530681 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700682 }
Sharad Sangle36781612015-05-28 16:15:16 +0530683
Zhou Song58081b62016-02-24 13:10:55 +0800684 // force reevaluating accessibility routing when call stops
Sharad Sangle36781612015-05-28 16:15:16 +0530685 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700686 }
687
Sharad Sangle36781612015-05-28 16:15:16 +0530688 /**
689 * Switching to or from incall state or switching between telephony and VoIP lead to force
690 * routing command.
691 */
692 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
693 || (is_state_in_call(state) && (state != oldState)));
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700694
695 // check for device and output changes triggered by new phone state
696 checkA2dpSuspend();
697 checkOutputForAllStrategies();
698 updateDevicesAndOutputs();
699
Sharad Sangle36781612015-05-28 16:15:16 +0530700 sp<SwAudioOutputDescriptor> hwOutputDesc = mPrimaryOutput;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530701#ifdef VOICE_CONCURRENCY
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530702 char propValue[PROPERTY_VALUE_MAX];
703 bool prop_playback_enabled = false, prop_rec_enabled=false, prop_voip_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700704
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530705 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
706 prop_playback_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
707 }
708
709 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
710 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
711 }
712
713 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
714 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
715 }
716
Dhananjay Kumar9125b2a2015-12-01 16:24:09 +0530717 if ((AUDIO_MODE_IN_CALL != oldState) && (AUDIO_MODE_IN_CALL == state)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530718 ALOGD("voice_conc:Entering to call mode oldState :: %d state::%d ",
719 oldState, state);
Dhananjay Kumar9125b2a2015-12-01 16:24:09 +0530720 mvoice_call_state = state;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530721 if (prop_rec_enabled) {
722 //Close all active inputs
723 audio_io_handle_t activeInput = mInputs.getActiveInput();
724 if (activeInput != 0) {
725 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
726 switch(activeDesc->mInputSource) {
727 case AUDIO_SOURCE_VOICE_UPLINK:
728 case AUDIO_SOURCE_VOICE_DOWNLINK:
729 case AUDIO_SOURCE_VOICE_CALL:
730 ALOGD("voice_conc:FOUND active input during call active: %d",activeDesc->mInputSource);
731 break;
732
733 case AUDIO_SOURCE_VOICE_COMMUNICATION:
734 if(prop_voip_enabled) {
735 ALOGD("voice_conc:CLOSING VoIP input source on call setup :%d ",activeDesc->mInputSource);
736 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
737 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
738 }
739 break;
740
741 default:
742 ALOGD("voice_conc:CLOSING input on call setup for inputSource: %d",activeDesc->mInputSource);
743 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
744 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
745 break;
746 }
747 }
748 } else if (prop_voip_enabled) {
749 audio_io_handle_t activeInput = mInputs.getActiveInput();
750 if (activeInput != 0) {
751 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
752 if (AUDIO_SOURCE_VOICE_COMMUNICATION == activeDesc->mInputSource) {
753 ALOGD("voice_conc:CLOSING VoIP on call setup : %d",activeDesc->mInputSource);
754 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
755 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
756 }
757 }
758 }
759 if (prop_playback_enabled) {
760 // Move tracks associated to this strategy from previous output to new output
761 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
762 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
763 if (i == AUDIO_STREAM_PATCH) {
764 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
765 continue;
766 }
767 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
768 if ((AUDIO_STREAM_MUSIC == i) ||
769 (AUDIO_STREAM_VOICE_CALL == i) ) {
770 ALOGD("voice_conc:Invalidate stream type %d", i);
771 mpClientInterface->invalidateStream((audio_stream_type_t)i);
772 }
773 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
774 ALOGD("voice_conc:Invalidate stream type %d", i);
775 mpClientInterface->invalidateStream((audio_stream_type_t)i);
776 }
777 }
778 }
779
780 for (size_t i = 0; i < mOutputs.size(); i++) {
781 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
782 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
783 ALOGD("voice_conc:ouput desc / profile is NULL");
784 continue;
785 }
786
787 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
788 if (((!outputDesc->isDuplicated() &&outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY))
789 && prop_playback_enabled) {
790 ALOGD("voice_conc:calling suspendOutput on call mode for primary output");
791 mpClientInterface->suspendOutput(mOutputs.keyAt(i));
792 } //Close compress all sessions
793 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
794 && prop_playback_enabled) {
795 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
796 closeOutput(mOutputs.keyAt(i));
797 }
798 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_VOIP_RX)
799 && prop_voip_enabled) {
800 ALOGD("voice_conc:calling closeOutput on call mode for DIRECT output");
801 closeOutput(mOutputs.keyAt(i));
802 }
803 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
804 if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)
805 && prop_playback_enabled) {
806 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
807 closeOutput(mOutputs.keyAt(i));
808 }
809 }
810 }
811 }
812
813 if ((AUDIO_MODE_IN_CALL == oldState || AUDIO_MODE_IN_COMMUNICATION == oldState) &&
814 (AUDIO_MODE_NORMAL == state) && prop_playback_enabled && mvoice_call_state) {
815 ALOGD("voice_conc:EXITING from call mode oldState :: %d state::%d \n",oldState, state);
816 mvoice_call_state = 0;
817 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
818 //restore PCM (deep-buffer) output after call termination
819 for (size_t i = 0; i < mOutputs.size(); i++) {
820 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
821 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
822 ALOGD("voice_conc:ouput desc / profile is NULL");
823 continue;
824 }
825 if (!outputDesc->isDuplicated() && outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
826 ALOGD("voice_conc:calling restoreOutput after call mode for primary output");
827 mpClientInterface->restoreOutput(mOutputs.keyAt(i));
828 }
829 }
830 }
831 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
832 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
833 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
834 if (i == AUDIO_STREAM_PATCH) {
835 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
836 continue;
837 }
838 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
839 if ((AUDIO_STREAM_MUSIC == i) ||
840 (AUDIO_STREAM_VOICE_CALL == i) ) {
841 mpClientInterface->invalidateStream((audio_stream_type_t)i);
842 }
843 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
844 mpClientInterface->invalidateStream((audio_stream_type_t)i);
845 }
846 }
847 }
848
849#endif
850#ifdef RECORD_PLAY_CONCURRENCY
851 char recConcPropValue[PROPERTY_VALUE_MAX];
852 bool prop_rec_play_enabled = false;
853
854 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
855 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
856 }
857 if (prop_rec_play_enabled) {
858 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
859 ALOGD("phone state changed to MODE_IN_COMM invlaidating music and voice streams");
860 // call invalidate for voice streams, so that it can use deepbuffer with VoIP out device from HAL
861 mpClientInterface->invalidateStream(AUDIO_STREAM_VOICE_CALL);
862 // call invalidate for music, so that compress will fallback to deep-buffer with VoIP out device
863 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
864
865 // close compress output to make sure session will be closed before timeout(60sec)
866 for (size_t i = 0; i < mOutputs.size(); i++) {
867
868 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
869 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
870 ALOGD("ouput desc / profile is NULL");
871 continue;
872 }
873
874 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
875 ALOGD("calling closeOutput on call mode for COMPRESS output");
876 closeOutput(mOutputs.keyAt(i));
877 }
878 }
879 } else if ((oldState == AUDIO_MODE_IN_COMMUNICATION) &&
880 (mEngine->getPhoneState() == AUDIO_MODE_NORMAL)) {
881 // call invalidate for music so that music can fallback to compress
882 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
883 }
884 }
885#endif
886 mPrevPhoneState = oldState;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700887 int delayMs = 0;
888 if (isStateInCall(state)) {
889 nsecs_t sysTime = systemTime();
890 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530891 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700892 // mute media and sonification strategies and delay device switch by the largest
893 // latency of any output where either strategy is active.
894 // This avoid sending the ring tone or music tail into the earpiece or headset.
Sharad Sangle36781612015-05-28 16:15:16 +0530895 if ((isStrategyActive(desc, STRATEGY_MEDIA,
896 SONIFICATION_HEADSET_MUSIC_DELAY,
897 sysTime) ||
898 isStrategyActive(desc, STRATEGY_SONIFICATION,
899 SONIFICATION_HEADSET_MUSIC_DELAY,
900 sysTime)) &&
901 (delayMs < (int)desc->latency()*2)) {
902 delayMs = desc->latency()*2;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700903 }
Sharad Sangle36781612015-05-28 16:15:16 +0530904 setStrategyMute(STRATEGY_MEDIA, true, desc);
905 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700906 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Sharad Sangle36781612015-05-28 16:15:16 +0530907 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
908 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700909 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
910 }
911 }
912
Sharad Sangle36781612015-05-28 16:15:16 +0530913 if (hasPrimaryOutput()) {
914 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
915 // the device returned is not necessarily reachable via this output
916 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
917 // force routing command to audio hardware when ending call
918 // even if no device change is needed
919 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
920 rxDevice = mPrimaryOutput->device();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700921 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700922
Sharad Sangle36781612015-05-28 16:15:16 +0530923 if (state == AUDIO_MODE_IN_CALL) {
924 updateCallRouting(rxDevice, delayMs);
925 } else if (oldState == AUDIO_MODE_IN_CALL) {
926 if (mCallRxPatch != 0) {
927 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
928 mCallRxPatch.clear();
929 }
930 if (mCallTxPatch != 0) {
931 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
932 mCallTxPatch.clear();
933 }
934 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
935 } else {
936 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700937 }
938 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530939 //update device for all non-primary outputs
940 for (size_t i = 0; i < mOutputs.size(); i++) {
941 audio_io_handle_t output = mOutputs.keyAt(i);
942 if (output != mPrimaryOutput->mIoHandle) {
943 newDevice = getNewOutputDevice(mOutputs.valueFor(output), false /*fromCache*/);
944 setOutputDevice(mOutputs.valueFor(output), newDevice, (newDevice != AUDIO_DEVICE_NONE));
945 }
946 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700947 // if entering in call state, handle special case of active streams
948 // pertaining to sonification strategy see handleIncallSonification()
949 if (isStateInCall(state)) {
950 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530951 for (size_t j = 0; j < mOutputs.size(); j++) {
952 audio_io_handle_t curOutput = mOutputs.keyAt(j);
953 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
954 if (stream == AUDIO_STREAM_PATCH) {
955 continue;
956 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530957 handleIncallSonification((audio_stream_type_t)stream, true, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +0530958 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700959 }
Zhou Song58081b62016-02-24 13:10:55 +0800960
961 // force reevaluating accessibility routing when call starts
962 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700963 }
964
965 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
966 if (state == AUDIO_MODE_RINGTONE &&
967 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
968 mLimitRingtoneVolume = true;
969 } else {
970 mLimitRingtoneVolume = false;
971 }
972}
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +0530973
974void AudioPolicyManagerCustom::setForceUse(audio_policy_force_use_t usage,
975 audio_policy_forced_cfg_t config)
976{
Ashish Jain40cab3a2016-03-23 11:14:14 +0530977 ALOGD("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +0530978
979 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
980 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
981 return;
982 }
983 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
984 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
985 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
986
987 // check for device and output changes triggered by new force usage
988 checkA2dpSuspend();
989 checkOutputForAllStrategies();
990 updateDevicesAndOutputs();
Ashish Jain40cab3a2016-03-23 11:14:14 +0530991
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +0530992 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
993 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
994 updateCallRouting(newDevice);
995 }
996 // Use reverse loop to make sure any low latency usecases (generally tones)
997 // are not routed before non LL usecases (generally music).
998 // We can safely assume that LL output would always have lower index,
999 // and use this work-around to avoid routing of output with music stream
1000 // from the context of short lived LL output.
1001 // Note: in case output's share backend(HAL sharing is implicit) all outputs
1002 // gets routing update while processing first output itself.
1003 for (size_t i = mOutputs.size(); i > 0; i--) {
1004 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i-1);
1005 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
1006 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || outputDesc != mPrimaryOutput) {
1007 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
1008 }
1009 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
1010 applyStreamVolumes(outputDesc, newDevice, 0, true);
1011 }
1012 }
1013
1014 audio_io_handle_t activeInput = mInputs.getActiveInput();
1015 if (activeInput != 0) {
Ashish Jain40cab3a2016-03-23 11:14:14 +05301016 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
1017 audio_devices_t newDevice = getNewInputDevice(activeInput);
1018 // Force new input selection if the new device can not be reached via current input
1019 if (activeDesc->mProfile->getSupportedDevices().types() & (newDevice & ~AUDIO_DEVICE_BIT_IN)) {
1020 setInputDevice(activeInput, newDevice);
1021 } else {
1022 closeInput(activeInput);
1023 }
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301024 }
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301025}
1026
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301027status_t AudioPolicyManagerCustom::stopSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301028 audio_stream_type_t stream,
1029 bool forceDeviceUpdate)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001030{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301031 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1032 ALOGW("stopSource() invalid stream %d", stream);
1033 return INVALID_OPERATION;
1034 }
Sharad Sangle36781612015-05-28 16:15:16 +05301035 // always handle stream stop, check which stream type is stopping
1036 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001037
Sharad Sangle36781612015-05-28 16:15:16 +05301038 // handle special case for sonification while in call
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001039 if (isInCall()) {
Sharad Sangle36781612015-05-28 16:15:16 +05301040 if (outputDesc->isDuplicated()) {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301041 handleIncallSonification(stream, false, false, outputDesc->subOutput1()->mIoHandle);
1042 handleIncallSonification(stream, false, false, outputDesc->subOutput2()->mIoHandle);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001043 }
Sharad Sangle36781612015-05-28 16:15:16 +05301044 handleIncallSonification(stream, false, false, outputDesc->mIoHandle);
1045 }
1046
1047 if (outputDesc->mRefCount[stream] > 0) {
1048 // decrement usage count of this stream on the output
1049 outputDesc->changeRefCount(stream, -1);
1050
1051 // store time at which the stream was stopped - see isStreamActive()
1052 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
1053 outputDesc->mStopTime[stream] = systemTime();
Zhou Song5dcddc92015-09-21 14:36:57 +08001054 audio_devices_t prevDevice = outputDesc->device();
Sharad Sangle36781612015-05-28 16:15:16 +05301055 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
1056 // delay the device switch by twice the latency because stopOutput() is executed when
1057 // the track stop() command is received and at that time the audio track buffer can
1058 // still contain data that needs to be drained. The latency only covers the audio HAL
1059 // and kernel buffers. Also the latency does not always include additional delay in the
1060 // audio path (audio DSP, CODEC ...)
1061 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
1062
1063 // force restoring the device selection on other active outputs if it differs from the
1064 // one being selected for this output
1065 for (size_t i = 0; i < mOutputs.size(); i++) {
1066 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1067 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1068 if (desc != outputDesc &&
1069 desc->isActive() &&
1070 outputDesc->sharesHwModuleWith(desc) &&
1071 (newDevice != desc->device())) {
Sharad Sangle4509cef2015-08-19 20:47:12 +05301072 audio_devices_t dev = getNewOutputDevice(mOutputs.valueFor(curOutput), false /*fromCache*/);
Zhou Song5dcddc92015-09-21 14:36:57 +08001073 uint32_t delayMs;
1074 if (dev == prevDevice) {
1075 delayMs = 0;
1076 } else {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301077 delayMs = outputDesc->latency()*2;
Zhou Song5dcddc92015-09-21 14:36:57 +08001078 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301079 setOutputDevice(desc,
1080 dev,
Sharad Sangle36781612015-05-28 16:15:16 +05301081 true,
Zhou Song5dcddc92015-09-21 14:36:57 +08001082 delayMs);
Sharad Sangle36781612015-05-28 16:15:16 +05301083 }
1084 }
1085 // update the outputs if stopping one with a stream that can affect notification routing
1086 handleNotificationRoutingForStream(stream);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001087 }
Sharad Sangle36781612015-05-28 16:15:16 +05301088 return NO_ERROR;
1089 } else {
1090 ALOGW("stopOutput() refcount is already 0");
1091 return INVALID_OPERATION;
1092 }
1093}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001094
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301095status_t AudioPolicyManagerCustom::startSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301096 audio_stream_type_t stream,
1097 audio_devices_t device,
Lalit Kansaraf803ac52016-05-09 19:12:01 +05301098 const char *address,
Sharad Sangle36781612015-05-28 16:15:16 +05301099 uint32_t *delayMs)
1100{
1101 // cannot start playback of STREAM_TTS if any other output is being used
1102 uint32_t beaconMuteLatency = 0;
1103
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301104 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1105 ALOGW("startSource() invalid stream %d", stream);
1106 return INVALID_OPERATION;
1107 }
1108
Sharad Sangle36781612015-05-28 16:15:16 +05301109 *delayMs = 0;
1110 if (stream == AUDIO_STREAM_TTS) {
1111 ALOGV("\t found BEACON stream");
Ashish Jain40cab3a2016-03-23 11:14:14 +05301112 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301113 return INVALID_OPERATION;
1114 } else {
1115 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001116 }
Sharad Sangle36781612015-05-28 16:15:16 +05301117 } else {
1118 // some playback other than beacon starts
1119 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1120 }
1121
Ashish Jain40cab3a2016-03-23 11:14:14 +05301122 // check active before incrementing usage count
1123 bool force = !outputDesc->isActive();
1124
Sharad Sangle36781612015-05-28 16:15:16 +05301125 // increment usage count for this stream on the requested output:
1126 // NOTE that the usage count is the same for duplicated output and hardware output which is
1127 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1128 outputDesc->changeRefCount(stream, 1);
1129
1130 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
1131 // starting an output being rerouted?
1132 if (device == AUDIO_DEVICE_NONE) {
1133 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001134 }
Sharad Sangle36781612015-05-28 16:15:16 +05301135 routing_strategy strategy = getStrategy(stream);
1136 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
1137 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1138 (beaconMuteLatency > 0);
1139 uint32_t waitMs = beaconMuteLatency;
Sharad Sangle36781612015-05-28 16:15:16 +05301140 for (size_t i = 0; i < mOutputs.size(); i++) {
1141 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1142 if (desc != outputDesc) {
1143 // force a device change if any other output is managed by the same hw
1144 // module and has a current device selection that differs from selected device.
1145 // In this case, the audio HAL must receive the new device selection so that it can
1146 // change the device currently selected by the other active output.
1147 if (outputDesc->sharesHwModuleWith(desc) &&
1148 desc->device() != device) {
1149 force = true;
1150 }
1151 // wait for audio on other active outputs to be presented when starting
1152 // a notification so that audio focus effect can propagate, or that a mute/unmute
1153 // event occurred for beacon
1154 uint32_t latency = desc->latency();
1155 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1156 waitMs = latency;
1157 }
1158 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001159 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05301160 uint32_t muteWaitMs;
Lalit Kansaraf803ac52016-05-09 19:12:01 +05301161 muteWaitMs = setOutputDevice(outputDesc, device, force, 0, NULL, address);
Sharad Sangle36781612015-05-28 16:15:16 +05301162
1163 // handle special case for sonification while in call
1164 if (isInCall()) {
1165 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001166 }
Sharad Sangle36781612015-05-28 16:15:16 +05301167
1168 // apply volume rules for current stream and device if necessary
1169 checkAndSetVolume(stream,
Ashish Jain40cab3a2016-03-23 11:14:14 +05301170 mVolumeCurves->getVolumeIndex(stream, device),
Sharad Sangle36781612015-05-28 16:15:16 +05301171 outputDesc,
1172 device);
1173
1174 // update the outputs if starting an output with a stream that can affect notification
1175 // routing
1176 handleNotificationRoutingForStream(stream);
1177
1178 // force reevaluating accessibility routing when ringtone or alarm starts
1179 if (strategy == STRATEGY_SONIFICATION) {
1180 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1181 }
1182 }
1183 else {
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001184 // handle special case for sonification while in call
1185 if (isInCall()) {
1186 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Sharad Sangle36781612015-05-28 16:15:16 +05301187 }
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001188 }
Sharad Sangle36781612015-05-28 16:15:16 +05301189 return NO_ERROR;
1190}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001191
Sharad Sangle36781612015-05-28 16:15:16 +05301192void AudioPolicyManagerCustom::handleIncallSonification(audio_stream_type_t stream,
1193 bool starting, bool stateChange,
1194 audio_io_handle_t output)
1195{
1196 if(!hasPrimaryOutput()) {
1197 return;
1198 }
1199 // no action needed for AUDIO_STREAM_PATCH stream type, it's for internal flinger tracks
1200 if (stream == AUDIO_STREAM_PATCH) {
1201 return;
1202 }
1203 // if the stream pertains to sonification strategy and we are in call we must
1204 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
1205 // in the device used for phone strategy and play the tone if the selected device does not
1206 // interfere with the device used for phone strategy
1207 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
1208 // many times as there are active tracks on the output
1209 const routing_strategy stream_strategy = getStrategy(stream);
1210 if ((stream_strategy == STRATEGY_SONIFICATION) ||
1211 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
1212 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
1213 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
1214 stream, starting, outputDesc->mDevice, stateChange);
1215 if (outputDesc->mRefCount[stream]) {
1216 int muteCount = 1;
1217 if (stateChange) {
1218 muteCount = outputDesc->mRefCount[stream];
1219 }
1220 if (audio_is_low_visibility(stream)) {
1221 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
1222 for (int i = 0; i < muteCount; i++) {
1223 setStreamMute(stream, starting, outputDesc);
1224 }
1225 } else {
1226 ALOGV("handleIncallSonification() high visibility");
1227 if (outputDesc->device() &
1228 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
1229 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
1230 for (int i = 0; i < muteCount; i++) {
1231 setStreamMute(stream, starting, outputDesc);
1232 }
1233 }
1234 if (starting) {
1235 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
1236 AUDIO_STREAM_VOICE_CALL);
1237 } else {
1238 mpClientInterface->stopTone();
1239 }
1240 }
1241 }
1242 }
1243}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001244
Sharad Sangle36781612015-05-28 16:15:16 +05301245void AudioPolicyManagerCustom::handleNotificationRoutingForStream(audio_stream_type_t stream) {
1246 switch(stream) {
1247 case AUDIO_STREAM_MUSIC:
1248 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
1249 updateDevicesAndOutputs();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001250 break;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001251 default:
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001252 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001253 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001254}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001255
Sharad Sangle36781612015-05-28 16:15:16 +05301256status_t AudioPolicyManagerCustom::checkAndSetVolume(audio_stream_type_t stream,
1257 int index,
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301258 const sp<AudioOutputDescriptor>& outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301259 audio_devices_t device,
1260 int delayMs, bool force)
1261{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301262 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1263 ALOGW("checkAndSetVolume() invalid stream %d", stream);
1264 return INVALID_OPERATION;
1265 }
Sharad Sangle36781612015-05-28 16:15:16 +05301266 // do not change actual stream volume if the stream is muted
1267 if (outputDesc->mMuteCount[stream] != 0) {
1268 ALOGVV("checkAndSetVolume() stream %d muted count %d",
1269 stream, outputDesc->mMuteCount[stream]);
1270 return NO_ERROR;
1271 }
1272 audio_policy_forced_cfg_t forceUseForComm =
1273 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
1274 // do not change in call volume if bluetooth is connected and vice versa
1275 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
1276 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
1277 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
1278 stream, forceUseForComm);
1279 return INVALID_OPERATION;
1280 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001281
Sharad Sangle36781612015-05-28 16:15:16 +05301282 if (device == AUDIO_DEVICE_NONE) {
1283 device = outputDesc->device();
1284 }
1285
1286 float volumeDb = computeVolume(stream, index, device);
1287 if (outputDesc->isFixedVolume(device)) {
1288 volumeDb = 0.0f;
1289 }
1290
1291 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
1292
1293 if (stream == AUDIO_STREAM_VOICE_CALL ||
1294 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
1295 float voiceVolume;
1296 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
1297 if (stream == AUDIO_STREAM_VOICE_CALL) {
Ashish Jain40cab3a2016-03-23 11:14:14 +05301298 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Sharad Sangle36781612015-05-28 16:15:16 +05301299 } else {
1300 voiceVolume = 1.0;
1301 }
1302
1303 if (voiceVolume != mLastVoiceVolume && ((outputDesc == mPrimaryOutput) ||
1304 isDirectOutput(outputDesc->mIoHandle) || device & AUDIO_DEVICE_OUT_ALL_USB)) {
1305 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
1306 mLastVoiceVolume = voiceVolume;
1307 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301308#ifdef FM_POWER_OPT
1309 } else if (stream == AUDIO_STREAM_MUSIC && hasPrimaryOutput() &&
Haynes Mathew George603ae9b2015-12-21 17:23:59 -08001310 outputDesc == mPrimaryOutput && mFMIsActive) {
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001311 /* Avoid unnecessary set_parameter calls as it puts the primary
1312 outputs FastMixer in HOT_IDLE leading to breaks in audio */
1313 if (volumeDb != mPrevFMVolumeDb) {
1314 mPrevFMVolumeDb = volumeDb;
1315 AudioParameter param = AudioParameter();
1316 param.addFloat(String8("fm_volume"), Volume::DbToAmpl(volumeDb));
Mingming Yin1433cc62016-01-04 14:58:00 -08001317 //Double delayMs to avoid sound burst while device switch.
1318 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString(), delayMs*2);
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001319 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301320#endif /* FM_POWER_OPT end */
Sharad Sangle36781612015-05-28 16:15:16 +05301321 }
1322
1323 return NO_ERROR;
1324}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001325
Sharad Sangle36781612015-05-28 16:15:16 +05301326bool AudioPolicyManagerCustom::isDirectOutput(audio_io_handle_t output) {
1327 for (size_t i = 0; i < mOutputs.size(); i++) {
1328 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1329 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1330 if ((curOutput == output) && (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
1331 return true;
1332 }
1333 }
1334 return false;
1335}
vivek mehta0ea887a2015-08-26 14:01:20 -07001336
1337status_t AudioPolicyManagerCustom::getOutputForAttr(const audio_attributes_t *attr,
1338 audio_io_handle_t *output,
1339 audio_session_t session,
1340 audio_stream_type_t *stream,
1341 uid_t uid,
1342 uint32_t samplingRate,
1343 audio_format_t format,
1344 audio_channel_mask_t channelMask,
1345 audio_output_flags_t flags,
1346 audio_port_handle_t selectedDeviceId,
1347 const audio_offload_info_t *offloadInfo)
1348{
1349 audio_offload_info_t tOffloadInfo = AUDIO_INFO_INITIALIZER;
1350
Alexy Joseph0ac09032015-10-16 15:57:53 -07001351 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
1352 bool pcmOffloadEnabled = false;
1353
1354 if (offloadDisabled) {
1355 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
1356 }
1357
1358 //read track offload property only if the global offload switch is off.
1359 if (!offloadDisabled) {
1360 pcmOffloadEnabled = property_get_bool("audio.offload.track.enable", false);
1361 }
vivek mehta0ea887a2015-08-26 14:01:20 -07001362
1363 if (offloadInfo == NULL && pcmOffloadEnabled) {
1364 tOffloadInfo.sample_rate = samplingRate;
1365 tOffloadInfo.channel_mask = channelMask;
1366 tOffloadInfo.format = format;
1367 tOffloadInfo.stream_type = *stream;
1368 tOffloadInfo.bit_width = 16; //hard coded for PCM_16
1369 if (attr != NULL) {
1370 ALOGV("found attribute .. setting usage %d ", attr->usage);
1371 tOffloadInfo.usage = attr->usage;
1372 } else {
Alexy Joseph0ac09032015-10-16 15:57:53 -07001373 ALOGI("%s:: attribute is NULL .. no usage set", __func__);
vivek mehta0ea887a2015-08-26 14:01:20 -07001374 }
1375 offloadInfo = &tOffloadInfo;
1376 }
1377
1378 return AudioPolicyManager::getOutputForAttr(attr, output, session, stream,
1379 (uid_t)uid, (uint32_t)samplingRate,
1380 format, (audio_channel_mask_t)channelMask,
1381 flags, (audio_port_handle_t)selectedDeviceId,
1382 offloadInfo);
1383}
1384
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001385audio_io_handle_t AudioPolicyManagerCustom::getOutputForDevice(
1386 audio_devices_t device,
Sharad Sangle36781612015-05-28 16:15:16 +05301387 audio_session_t session __unused,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001388 audio_stream_type_t stream,
1389 uint32_t samplingRate,
1390 audio_format_t format,
1391 audio_channel_mask_t channelMask,
1392 audio_output_flags_t flags,
1393 const audio_offload_info_t *offloadInfo)
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001394{
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001395 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001396 status_t status;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001397
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001398#ifdef AUDIO_POLICY_TEST
1399 if (mCurOutput != 0) {
1400 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1401 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1402
1403 if (mTestOutputs[mCurOutput] == 0) {
1404 ALOGV("getOutput() opening test output");
Sharad Sangle36781612015-05-28 16:15:16 +05301405 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
1406 mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001407 outputDesc->mDevice = mTestDevice;
1408 outputDesc->mLatency = mTestLatencyMs;
1409 outputDesc->mFlags =
1410 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
1411 outputDesc->mRefCount[stream] = 0;
1412 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1413 config.sample_rate = mTestSamplingRate;
1414 config.channel_mask = mTestChannels;
1415 config.format = mTestFormat;
1416 if (offloadInfo != NULL) {
1417 config.offload_info = *offloadInfo;
1418 }
1419 status = mpClientInterface->openOutput(0,
1420 &mTestOutputs[mCurOutput],
1421 &config,
1422 &outputDesc->mDevice,
1423 String8(""),
1424 &outputDesc->mLatency,
1425 outputDesc->mFlags);
1426 if (status == NO_ERROR) {
1427 outputDesc->mSamplingRate = config.sample_rate;
1428 outputDesc->mFormat = config.format;
1429 outputDesc->mChannelMask = config.channel_mask;
1430 AudioParameter outputCmd = AudioParameter();
1431 outputCmd.addInt(String8("set_id"),mCurOutput);
1432 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1433 addOutput(mTestOutputs[mCurOutput], outputDesc);
1434 }
1435 }
1436 return mTestOutputs[mCurOutput];
1437 }
1438#endif //AUDIO_POLICY_TEST
Sharad Sangle36781612015-05-28 16:15:16 +05301439 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1440 (stream != AUDIO_STREAM_MUSIC)) {
1441 // compress should not be used for non-music streams
1442 ALOGE("Offloading only allowed with music stream");
1443 return 0;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301444 }
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301445
1446 if ((stream == AUDIO_STREAM_VOICE_CALL) &&
1447 (channelMask == 1) &&
Shiv Maliyappanahallid77d3b32016-02-02 13:45:50 -08001448 (samplingRate == 8000 || samplingRate == 16000 ||
1449 samplingRate == 32000 || samplingRate == 48000)) {
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301450 // Allow Voip direct output only if:
1451 // audio mode is MODE_IN_COMMUNCATION; AND
1452 // voip output is not opened already; AND
1453 // requested sample rate matches with that of voip input stream (if opened already)
1454 int value = 0;
1455 uint32_t mode = 0, voipOutCount = 1, voipSampleRate = 1;
1456 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1457 String8("audio_mode"));
1458 AudioParameter result = AudioParameter(valueStr);
1459 if (result.getInt(String8("audio_mode"), value) == NO_ERROR) {
1460 mode = value;
1461 }
1462
1463 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1464 String8("voip_out_stream_count"));
1465 result = AudioParameter(valueStr);
1466 if (result.getInt(String8("voip_out_stream_count"), value) == NO_ERROR) {
1467 voipOutCount = value;
1468 }
1469
1470 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1471 String8("voip_sample_rate"));
1472 result = AudioParameter(valueStr);
1473 if (result.getInt(String8("voip_sample_rate"), value) == NO_ERROR) {
1474 voipSampleRate = value;
1475 }
1476
1477 if ((mode == AUDIO_MODE_IN_COMMUNICATION) && (voipOutCount == 0) &&
1478 ((voipSampleRate == 0) || (voipSampleRate == samplingRate))) {
1479 if (audio_is_linear_pcm(format)) {
1480 char propValue[PROPERTY_VALUE_MAX] = {0};
1481 property_get("use.voice.path.for.pcm.voip", propValue, "0");
1482 bool voipPcmSysPropEnabled = !strncmp("true", propValue, sizeof("true"));
1483 if (voipPcmSysPropEnabled && (format == AUDIO_FORMAT_PCM_16_BIT)) {
Karthik Reddy Katta3fb9be72015-11-25 11:18:27 +05301484 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX |
1485 AUDIO_OUTPUT_FLAG_DIRECT);
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301486 ALOGD("Set VoIP and Direct output flags for PCM format");
1487 }
1488 }
1489 }
1490 }
1491
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301492#ifdef VOICE_CONCURRENCY
1493 char propValue[PROPERTY_VALUE_MAX];
1494 bool prop_play_enabled=false, prop_voip_enabled = false;
1495
1496 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1497 prop_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001498 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301499
1500 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1501 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1502 }
1503
1504 if (prop_play_enabled && mvoice_call_state) {
1505 //check if voice call is active / running in background
1506 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1507 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1508 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1509 {
1510 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1511 if(prop_voip_enabled) {
1512 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1513 flags );
1514 return 0;
1515 }
1516 }
1517 else {
1518 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
1519 ALOGD("voice_conc:IN call mode adding ULL flags .. flags: %x ", flags );
1520 flags = AUDIO_OUTPUT_FLAG_FAST;
1521 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
1522 if (AUDIO_STREAM_MUSIC == stream) {
1523 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1524 ALOGD("voice_conc:IN call mode adding deep-buffer flags %x ", flags );
1525 }
1526 else {
1527 flags = AUDIO_OUTPUT_FLAG_FAST;
1528 ALOGD("voice_conc:IN call mode adding fast flags %x ", flags );
1529 }
1530 }
1531 }
1532 }
1533 } else if (prop_voip_enabled && mvoice_call_state) {
1534 //check if voice call is active / running in background
1535 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1536 //return only ULL ouput
1537 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1538 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1539 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1540 {
1541 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1542 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1543 flags );
1544 return 0;
1545 }
1546 }
1547 }
1548#endif
1549#ifdef RECORD_PLAY_CONCURRENCY
1550 char recConcPropValue[PROPERTY_VALUE_MAX];
1551 bool prop_rec_play_enabled = false;
1552
1553 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
1554 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
1555 }
1556 if ((prop_rec_play_enabled) &&
1557 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
1558 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
1559 if (AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1560 // allow VoIP using voice path
1561 // Do nothing
1562 } else if((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1563 ALOGD("voice_conc:MODE_IN_COMM is setforcing deep buffer output for non ULL... flags: %x", flags);
1564 // use deep buffer path for all non ULL outputs
1565 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1566 }
1567 } else if ((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1568 ALOGD("voice_conc:Record mode is on forcing deep buffer output for non ULL... flags: %x ", flags);
1569 // use deep buffer path for all non ULL outputs
1570 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1571 }
1572 }
1573 if (prop_rec_play_enabled &&
1574 (stream == AUDIO_STREAM_ENFORCED_AUDIBLE)) {
1575 ALOGD("Record conc is on forcing ULL output for ENFORCED_AUDIBLE");
1576 flags = AUDIO_OUTPUT_FLAG_FAST;
1577 }
1578#endif
1579
Sharad Sangle4509cef2015-08-19 20:47:12 +05301580#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Sharad Sangle36781612015-05-28 16:15:16 +05301581 /*
1582 * WFD audio routes back to target speaker when starting a ringtone playback.
1583 * This is because primary output is reused for ringtone, so output device is
1584 * updated based on SONIFICATION strategy for both ringtone and music playback.
1585 * The same issue is not seen on remoted_submix HAL based WFD audio because
1586 * primary output is not reused and a new output is created for ringtone playback.
1587 * Issue is fixed by updating output flag to AUDIO_OUTPUT_FLAG_FAST when there is
1588 * a non-music stream playback on WFD, so primary output is not reused for ringtone.
1589 */
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001590 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
1591 if ((availableOutputDeviceTypes & AUDIO_DEVICE_OUT_PROXY)
1592 && (stream != AUDIO_STREAM_MUSIC)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301593 ALOGD("WFD audio: use OUTPUT_FLAG_FAST for non music stream. flags:%x", flags );
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001594 //For voip paths
1595 if(flags & AUDIO_OUTPUT_FLAG_DIRECT)
1596 flags = AUDIO_OUTPUT_FLAG_DIRECT;
1597 else //route every thing else to ULL path
1598 flags = AUDIO_OUTPUT_FLAG_FAST;
1599 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301600#endif
1601
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001602 // open a direct output if required by specified parameters
vivek mehta0ea887a2015-08-26 14:01:20 -07001603 // force direct flag if offload flag is set: offloading implies a direct output stream
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001604 // and all common behaviors are driven by checking only the direct flag
1605 // this should normally be set appropriately in the policy configuration file
1606 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1607 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1608 }
1609 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1610 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1611 }
vivek mehta0ea887a2015-08-26 14:01:20 -07001612
vivek mehta4b0d8192015-10-16 00:25:59 -07001613 bool forced_deep = false;
Sharad Sangle36781612015-05-28 16:15:16 +05301614 // only allow deep buffering for music stream type
1615 if (stream != AUDIO_STREAM_MUSIC) {
1616 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Sharad Sangle497aef82015-08-03 17:55:48 +05301617 } else if (/* stream == AUDIO_STREAM_MUSIC && */
1618 flags == AUDIO_OUTPUT_FLAG_NONE &&
1619 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
vivek mehtac984b992015-11-25 13:28:55 -08001620 forced_deep = true;
Sharad Sangle36781612015-05-28 16:15:16 +05301621 }
Sharad Sangle497aef82015-08-03 17:55:48 +05301622
Sharad Sangle36781612015-05-28 16:15:16 +05301623 if (stream == AUDIO_STREAM_TTS) {
1624 flags = AUDIO_OUTPUT_FLAG_TTS;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001625 }
1626
vivek mehta4b0d8192015-10-16 00:25:59 -07001627 // Do offload magic here
vivek mehtac984b992015-11-25 13:28:55 -08001628 if ((flags == AUDIO_OUTPUT_FLAG_NONE) &&
1629 (stream == AUDIO_STREAM_MUSIC) &&
1630 (offloadInfo != NULL) &&
vivek mehta4b0d8192015-10-16 00:25:59 -07001631 ((offloadInfo->usage == AUDIO_USAGE_MEDIA) || (offloadInfo->usage == AUDIO_USAGE_GAME))) {
vivek mehtac984b992015-11-25 13:28:55 -08001632 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DIRECT_PCM);
vivek mehta4b0d8192015-10-16 00:25:59 -07001633 ALOGD("AudioCustomHAL --> Force Direct Flag .. flag (0x%x)", flags);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301634 }
1635
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001636 sp<IOProfile> profile;
1637
1638 // skip direct output selection if the request can obviously be attached to a mixed output
1639 // and not explicitly requested
vivek mehtac984b992015-11-25 13:28:55 -08001640 if (((flags & (AUDIO_OUTPUT_FLAG_DIRECT|AUDIO_OUTPUT_FLAG_DIRECT_PCM)) == 0) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301641 audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX &&
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001642 audio_channel_count_from_out_mask(channelMask) <= 2) {
1643 goto non_direct_output;
1644 }
1645
1646 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1647 // creating an offloaded track and tearing it down immediately after start when audioflinger
1648 // detects there is an active non offloadable effect.
1649 // FIXME: We should check the audio session here but we do not have it in this context.
1650 // This may prevent offloading in rare situations where effects are left active by apps
1651 // in the background.
1652
Sharad Sangle36781612015-05-28 16:15:16 +05301653 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
Ashish Jain40cab3a2016-03-23 11:14:14 +05301654 !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001655 profile = getProfileForDirectOutput(device,
1656 samplingRate,
1657 format,
1658 channelMask,
1659 (audio_output_flags_t)flags);
1660 }
1661
1662 if (profile != 0) {
vivek mehtac984b992015-11-25 13:28:55 -08001663
1664 if (!(flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301665 (profile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT_PCM)) {
vivek mehtac984b992015-11-25 13:28:55 -08001666 ALOGI("got Direct_PCM without requesting ... reject ");
1667 profile = NULL;
1668 goto non_direct_output;
1669 }
1670
Sharad Sangle36781612015-05-28 16:15:16 +05301671 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001672
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001673 // if multiple concurrent offload decode is supported
1674 // do no check for reuse and also don't close previous output if its offload
1675 // previous output will be closed during track destruction
1676 if (!(property_get_bool("audio.offload.multiple.enabled", false) &&
1677 ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0))) {
1678 for (size_t i = 0; i < mOutputs.size(); i++) {
1679 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1680 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1681 outputDesc = desc;
1682 // reuse direct output if currently open and configured with same parameters
1683 if ((samplingRate == outputDesc->mSamplingRate) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301684 audio_formats_match(format, outputDesc->mFormat) &&
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001685 (channelMask == outputDesc->mChannelMask)) {
1686 outputDesc->mDirectOpenCount++;
1687 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1688 return mOutputs.keyAt(i);
1689 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001690 }
1691 }
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001692 // close direct output if currently open and configured with different parameters
1693 if (outputDesc != NULL) {
1694 closeOutput(outputDesc->mIoHandle);
1695 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001696 }
Sharad Sangle36781612015-05-28 16:15:16 +05301697
1698 // if the selected profile is offloaded and no offload info was specified,
1699 // create a default one
1700 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
Ashish Jain40cab3a2016-03-23 11:14:14 +05301701 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
Sharad Sangle36781612015-05-28 16:15:16 +05301702 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1703 defaultOffloadInfo.sample_rate = samplingRate;
1704 defaultOffloadInfo.channel_mask = channelMask;
1705 defaultOffloadInfo.format = format;
1706 defaultOffloadInfo.stream_type = stream;
1707 defaultOffloadInfo.bit_rate = 0;
1708 defaultOffloadInfo.duration_us = -1;
1709 defaultOffloadInfo.has_video = true; // conservative
1710 defaultOffloadInfo.is_streaming = true; // likely
1711 offloadInfo = &defaultOffloadInfo;
1712 }
1713
1714 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001715 outputDesc->mDevice = device;
1716 outputDesc->mLatency = 0;
Sharad Sangle36781612015-05-28 16:15:16 +05301717 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001718 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1719 config.sample_rate = samplingRate;
1720 config.channel_mask = channelMask;
1721 config.format = format;
1722 if (offloadInfo != NULL) {
1723 config.offload_info = *offloadInfo;
1724 }
Sharad Sangle36781612015-05-28 16:15:16 +05301725 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001726 &output,
1727 &config,
1728 &outputDesc->mDevice,
1729 String8(""),
1730 &outputDesc->mLatency,
1731 outputDesc->mFlags);
1732
1733 // only accept an output with the requested parameters
1734 if (status != NO_ERROR ||
1735 (samplingRate != 0 && samplingRate != config.sample_rate) ||
Ashish Jain40cab3a2016-03-23 11:14:14 +05301736 (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) ||
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001737 (channelMask != 0 && channelMask != config.channel_mask)) {
1738 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1739 "format %d %d, channelMask %04x %04x", output, samplingRate,
1740 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1741 outputDesc->mChannelMask);
1742 if (output != AUDIO_IO_HANDLE_NONE) {
1743 mpClientInterface->closeOutput(output);
1744 }
Sharad Sangle36781612015-05-28 16:15:16 +05301745 // fall back to mixer output if possible when the direct output could not be open
Ashish Jain40cab3a2016-03-23 11:14:14 +05301746 if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) {
Sharad Sangle36781612015-05-28 16:15:16 +05301747 goto non_direct_output;
1748 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001749 return AUDIO_IO_HANDLE_NONE;
1750 }
1751 outputDesc->mSamplingRate = config.sample_rate;
1752 outputDesc->mChannelMask = config.channel_mask;
1753 outputDesc->mFormat = config.format;
1754 outputDesc->mRefCount[stream] = 0;
1755 outputDesc->mStopTime[stream] = 0;
1756 outputDesc->mDirectOpenCount = 1;
1757
1758 audio_io_handle_t srcOutput = getOutputForEffect();
1759 addOutput(output, outputDesc);
1760 audio_io_handle_t dstOutput = getOutputForEffect();
1761 if (dstOutput == output) {
Gao Jiedb057832015-11-12 08:51:22 +08001762#ifdef DOLBY_ENABLE
1763 status_t status = mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1764 if (status == NO_ERROR) {
1765 for (size_t i = 0; i < mEffects.size(); i++) {
1766 sp<EffectDescriptor> desc = mEffects.valueAt(i);
1767 if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX) {
1768 // update the mIo member of EffectDescriptor for the global effect
1769 ALOGV("%s updating mIo", __FUNCTION__);
1770 desc->mIo = dstOutput;
1771 }
1772 }
1773 } else {
1774 ALOGW("%s moveEffects from %d to %d failed", __FUNCTION__, srcOutput, dstOutput);
1775 }
1776#else // DOLBY_END
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001777 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
Gao Jiedb057832015-11-12 08:51:22 +08001778#endif // LINE_ADDED_BY_DOLBY
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001779 }
1780 mPreviousOutputs = mOutputs;
1781 ALOGV("getOutput() returns new direct output %d", output);
1782 mpClientInterface->onAudioPortListUpdate();
1783 return output;
1784 }
1785
1786non_direct_output:
Ashish Jain5fe12412016-04-18 18:44:17 +05301787
1788 // A request for HW A/V sync cannot fallback to a mixed output because time
1789 // stamps are embedded in audio data
1790 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1791 return AUDIO_IO_HANDLE_NONE;
1792 }
1793
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001794 // ignoring channel mask due to downmix capability in mixer
1795
1796 // open a non direct output
1797
1798 // for non direct outputs, only PCM is supported
1799 if (audio_is_linear_pcm(format)) {
vivek mehtac984b992015-11-25 13:28:55 -08001800 if (forced_deep) {
1801 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
1802 ALOGI("setting force DEEP buffer now ");
1803 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001804 // get which output is suitable for the specified stream. The actual
1805 // routing change will happen when startOutput() will be called
1806 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1807
1808 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1809 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1810 output = selectOutput(outputs, flags, format);
1811 }
1812 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1813 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1814
vivek mehta0ea887a2015-08-26 14:01:20 -07001815 ALOGV("getOutputForDevice() returns output %d", output);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001816
1817 return output;
1818}
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301819
1820status_t AudioPolicyManagerCustom::getInputForAttr(const audio_attributes_t *attr,
1821 audio_io_handle_t *input,
1822 audio_session_t session,
1823 uid_t uid,
1824 uint32_t samplingRate,
1825 audio_format_t format,
1826 audio_channel_mask_t channelMask,
1827 audio_input_flags_t flags,
1828 audio_port_handle_t selectedDeviceId,
1829 input_type_t *inputType)
1830{
Ashish Jain40cab3a2016-03-23 11:14:14 +05301831 audio_source_t inputSource;
1832 inputSource = attr->source;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301833#ifdef VOICE_CONCURRENCY
1834
1835 char propValue[PROPERTY_VALUE_MAX];
1836 bool prop_rec_enabled=false, prop_voip_enabled = false;
1837
1838 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
1839 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1840 }
1841
1842 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1843 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1844 }
1845
1846 if (prop_rec_enabled && mvoice_call_state) {
1847 //check if voice call is active / running in background
1848 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1849 //Need to block input request
1850 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1851 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1852 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1853 {
1854 switch(inputSource) {
1855 case AUDIO_SOURCE_VOICE_UPLINK:
1856 case AUDIO_SOURCE_VOICE_DOWNLINK:
1857 case AUDIO_SOURCE_VOICE_CALL:
1858 ALOGD("voice_conc:Creating input during incall mode for inputSource: %d",
1859 inputSource);
1860 break;
1861
1862 case AUDIO_SOURCE_VOICE_COMMUNICATION:
1863 if(prop_voip_enabled) {
1864 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1865 inputSource);
1866 return NO_INIT;
1867 }
1868 break;
1869 default:
1870 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1871 inputSource);
1872 return NO_INIT;
1873 }
1874 }
1875 }//check for VoIP flag
1876 else if(prop_voip_enabled && mvoice_call_state) {
1877 //check if voice call is active / running in background
1878 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1879 //Need to block input request
1880 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1881 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1882 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1883 {
1884 if(inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1885 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
1886 return NO_INIT;
1887 }
1888 }
1889 }
1890
1891#endif
1892
1893 return AudioPolicyManager::getInputForAttr(attr,
1894 input,
1895 session,
1896 uid,
1897 samplingRate,
1898 format,
1899 channelMask,
1900 flags,
1901 selectedDeviceId,
1902 inputType);
1903}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001904
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301905status_t AudioPolicyManagerCustom::startInput(audio_io_handle_t input,
1906 audio_session_t session)
1907{
1908 ALOGV("startInput() input %d", input);
1909 ssize_t index = mInputs.indexOfKey(input);
1910 if (index < 0) {
1911 ALOGW("startInput() unknown input %d", input);
1912 return BAD_VALUE;
1913 }
1914 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1915
Ashish Jain40cab3a2016-03-23 11:14:14 +05301916 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1917 if (audioSession == 0) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301918 ALOGW("startInput() unknown session %d on input %d", session, input);
1919 return BAD_VALUE;
1920 }
1921
1922 // virtual input devices are compatible with other input devices
1923 if (!is_virtual_input_device(inputDesc->mDevice)) {
1924
1925 // for a non-virtual input device, check if there is another (non-virtual) active input
1926 audio_io_handle_t activeInput = mInputs.getActiveInput();
1927 if (activeInput != 0 && activeInput != input) {
1928
1929 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1930 // otherwise the active input continues and the new input cannot be started.
1931 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Ashish Jain40cab3a2016-03-23 11:14:14 +05301932 if ((activeDesc->inputSource() == AUDIO_SOURCE_HOTWORD) &&
1933 !activeDesc->hasPreemptedSession(session)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301934 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
Ashish Jain40cab3a2016-03-23 11:14:14 +05301935 //FIXME: consider all active sessions
1936 AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions();
1937 audio_session_t activeSession = activeSessions.keyAt(0);
1938 SortedVector<audio_session_t> sessions =
1939 activeDesc->getPreemptedSessions();
1940 sessions.add(activeSession);
1941 inputDesc->setPreemptedSessions(sessions);
1942 stopInput(activeInput, activeSession);
1943 releaseInput(activeInput, activeSession);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301944 } else {
1945 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
1946 return INVALID_OPERATION;
1947 }
1948 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05301949 // Do not allow capture if an active voice call is using a software patch and
1950 // the call TX source device is on the same HW module.
1951 // FIXME: would be better to refine to only inputs whose profile connects to the
1952 // call TX device but this information is not in the audio patch
1953 if (mCallTxPatch != 0 &&
1954 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1955 return INVALID_OPERATION;
1956 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301957 }
1958
1959 // Routing?
1960 mInputRoutes.incRouteActivity(session);
1961#ifdef RECORD_PLAY_CONCURRENCY
1962 mIsInputRequestOnProgress = true;
1963
1964 char getPropValue[PROPERTY_VALUE_MAX];
1965 bool prop_rec_play_enabled = false;
1966
1967 if (property_get("rec.playback.conc.disabled", getPropValue, NULL)) {
1968 prop_rec_play_enabled = atoi(getPropValue) || !strncmp("true", getPropValue, 4);
1969 }
1970
1971 if ((prop_rec_play_enabled) &&(mInputs.activeInputsCount() == 0)){
1972 // send update to HAL on record playback concurrency
1973 AudioParameter param = AudioParameter();
1974 param.add(String8("rec_play_conc_on"), String8("true"));
1975 ALOGD("startInput() setParameters rec_play_conc is setting to ON ");
1976 mpClientInterface->setParameters(0, param.toString());
1977
1978 // Call invalidate to reset all opened non ULL audio tracks
1979 // Move tracks associated to this strategy from previous output to new output
1980 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
1981 // Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder)
Sharad Sangle4509cef2015-08-19 20:47:12 +05301982 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE && (i != AUDIO_STREAM_PATCH))) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301983 ALOGD("Invalidate on releaseInput for stream :: %d ", i);
1984 //FIXME see fixme on name change
1985 mpClientInterface->invalidateStream((audio_stream_type_t)i);
1986 }
1987 }
1988 // close compress tracks
1989 for (size_t i = 0; i < mOutputs.size(); i++) {
1990 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
1991 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
1992 ALOGD("ouput desc / profile is NULL");
1993 continue;
1994 }
1995 if (outputDesc->mProfile->mFlags
1996 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1997 // close compress sessions
1998 ALOGD("calling closeOutput on record conc for COMPRESS output");
1999 closeOutput(mOutputs.keyAt(i));
2000 }
2001 }
2002 }
2003#endif
2004
Ashish Jain40cab3a2016-03-23 11:14:14 +05302005 if (!inputDesc->isActive() || mInputRoutes.hasRouteChanged(session)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302006 // if input maps to a dynamic policy with an activity listener, notify of state change
Ashish Jain630c5e12016-04-18 11:50:21 +05302007 if ((inputDesc->mPolicyMix != NULL)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302008 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Ashish Jain5fe12412016-04-18 18:44:17 +05302009 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302010 MIX_STATE_MIXING);
Ashish Jain630c5e12016-04-18 11:50:21 +05302011 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302012
2013 if (mInputs.activeInputsCount() == 0) {
2014 SoundTrigger::setCaptureState(true);
2015 }
2016 setInputDevice(input, getNewInputDevice(input), true /* force */);
2017
2018 // automatically enable the remote submix output when input is started if not
2019 // used by a policy mix of type MIX_TYPE_RECORDERS
2020 // For remote submix (a virtual device), we open only one input per capture request.
2021 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
2022 String8 address = String8("");
2023 if (inputDesc->mPolicyMix == NULL) {
2024 address = String8("0");
Ashish Jain630c5e12016-04-18 11:50:21 +05302025 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
Ashish Jain5fe12412016-04-18 18:44:17 +05302026 address = inputDesc->mPolicyMix->mDeviceAddress;
Ashish Jain630c5e12016-04-18 11:50:21 +05302027 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302028 if (address != "") {
2029 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2030 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2031 address, "remote-submix");
2032 }
2033 }
2034 }
2035
Ashish Jain40cab3a2016-03-23 11:14:14 +05302036 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302037
Ashish Jain40cab3a2016-03-23 11:14:14 +05302038 audioSession->changeActiveCount(1);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302039#ifdef RECORD_PLAY_CONCURRENCY
2040 mIsInputRequestOnProgress = false;
2041#endif
2042 return NO_ERROR;
2043}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08002044
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302045status_t AudioPolicyManagerCustom::stopInput(audio_io_handle_t input,
2046 audio_session_t session)
2047{
2048 status_t status;
2049 status = AudioPolicyManager::stopInput(input, session);
2050#ifdef RECORD_PLAY_CONCURRENCY
2051 char propValue[PROPERTY_VALUE_MAX];
2052 bool prop_rec_play_enabled = false;
2053
2054 if (property_get("rec.playback.conc.disabled", propValue, NULL)) {
2055 prop_rec_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
2056 }
2057
2058 if ((prop_rec_play_enabled) && (mInputs.activeInputsCount() == 0)) {
2059
2060 //send update to HAL on record playback concurrency
2061 AudioParameter param = AudioParameter();
2062 param.add(String8("rec_play_conc_on"), String8("false"));
2063 ALOGD("stopInput() setParameters rec_play_conc is setting to OFF ");
2064 mpClientInterface->setParameters(0, param.toString());
2065
2066 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
2067 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
2068 //Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder stop tone)
2069 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE) && (i != AUDIO_STREAM_PATCH)) {
2070 ALOGD(" Invalidate on stopInput for stream :: %d ", i);
2071 //FIXME see fixme on name change
2072 mpClientInterface->invalidateStream((audio_stream_type_t)i);
2073 }
2074 }
2075 }
2076#endif
2077 return status;
2078}
2079
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302080void AudioPolicyManagerCustom::closeAllInputs() {
2081 bool patchRemoved = false;
2082
Dhanalakshmi Siddanibfeca7f2015-10-08 15:17:11 +05302083 for(size_t input_index = mInputs.size(); input_index > 0; input_index--) {
2084 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index-1);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302085 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302086 if (patch_index >= 0) {
2087 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302088 status_t status;
2089 status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302090 mAudioPatches.removeItemsAt(patch_index);
2091 patchRemoved = true;
2092 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05302093 if ((inputDesc->getOpenRefCount() > 0) && inputDesc->isSoundTrigger()
2094 && (mInputs.size() == 1)) {
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302095 ALOGD("Do not close sound trigger input handle");
2096 } else {
Dhanalakshmi Siddanibfeca7f2015-10-08 15:17:11 +05302097 mpClientInterface->closeInput(mInputs.keyAt(input_index-1));
2098 mInputs.removeItem(mInputs.keyAt(input_index-1));
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302099 }
2100 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05302101 mInputs.clear();
2102 SoundTrigger::setCaptureState(false);
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302103 nextAudioPortGeneration();
2104
2105 if (patchRemoved) {
2106 mpClientInterface->onAudioPatchListUpdate();
2107 }
2108}
2109
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302110AudioPolicyManagerCustom::AudioPolicyManagerCustom(AudioPolicyClientInterface *clientInterface)
Sharad Sangle4509cef2015-08-19 20:47:12 +05302111 : AudioPolicyManager(clientInterface),
2112 mHdmiAudioDisabled(false),
2113 mHdmiAudioEvent(false),
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08002114 mPrevPhoneState(0),
Haynes Mathew George603ae9b2015-12-21 17:23:59 -08002115 mPrevFMVolumeDb(0.0f),
2116 mFMIsActive(false)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302117{
Ashish Jain40cab3a2016-03-23 11:14:14 +05302118
2119 //TODO: Check the new logic to parse policy conf and update the below code
2120 // Need this when SSR encoding is enabled
Mingming Yin38ea08c2015-10-05 15:24:04 -07002121 char ssr_enabled[PROPERTY_VALUE_MAX] = {0};
2122 bool prop_ssr_enabled = false;
2123
2124 if (property_get("ro.qc.sdk.audio.ssr", ssr_enabled, NULL)) {
2125 prop_ssr_enabled = atoi(ssr_enabled) || !strncmp("true", ssr_enabled, 4);
2126 }
2127
2128 for (size_t i = 0; i < mHwModules.size(); i++) {
Ashish Jain40cab3a2016-03-23 11:14:14 +05302129 ALOGV("Hw module %zu", i);
Mingming Yin38ea08c2015-10-05 15:24:04 -07002130 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) {
2131 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Ashish Jain40cab3a2016-03-23 11:14:14 +05302132 AudioProfileVector profiles = inProfile->getAudioProfiles();
2133 for (size_t k = 0; k < profiles.size(); k++){
2134 ChannelsVector channels = profiles[k]->getChannels();
2135 for (size_t x = 0; x < channels.size(); x++) {
2136 audio_channel_mask_t channelMask = channels[x];
2137 ALOGV("Channel Mask %x size %zu", channelMask,
2138 channels.size());
2139 if (AUDIO_CHANNEL_IN_5POINT1 == channelMask) {
2140 if (!prop_ssr_enabled) {
2141 ALOGI("removing AUDIO_CHANNEL_IN_5POINT1 from"
2142 " input profile as SSR(surround sound record)"
2143 " is not supported on this chipset variant");
2144 channels.removeItemsAt(x, 1);
2145 ALOGV("Channel Mask size now %zu",
2146 channels.size());
2147 }
Mingming Yin38ea08c2015-10-05 15:24:04 -07002148 }
2149 }
2150 }
2151 }
2152 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302153#ifdef RECORD_PLAY_CONCURRENCY
2154 mIsInputRequestOnProgress = false;
2155#endif
2156
2157
2158#ifdef VOICE_CONCURRENCY
2159 mFallBackflag = getFallBackPath();
2160#endif
2161}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07002162}