blob: 25d5db941dd34f81f7213ea0d9461876ef483b1c [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,
1098 uint32_t *delayMs)
1099{
1100 // cannot start playback of STREAM_TTS if any other output is being used
1101 uint32_t beaconMuteLatency = 0;
1102
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301103 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1104 ALOGW("startSource() invalid stream %d", stream);
1105 return INVALID_OPERATION;
1106 }
1107
Sharad Sangle36781612015-05-28 16:15:16 +05301108 *delayMs = 0;
1109 if (stream == AUDIO_STREAM_TTS) {
1110 ALOGV("\t found BEACON stream");
Ashish Jain40cab3a2016-03-23 11:14:14 +05301111 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301112 return INVALID_OPERATION;
1113 } else {
1114 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001115 }
Sharad Sangle36781612015-05-28 16:15:16 +05301116 } else {
1117 // some playback other than beacon starts
1118 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1119 }
1120
Ashish Jain40cab3a2016-03-23 11:14:14 +05301121 // check active before incrementing usage count
1122 bool force = !outputDesc->isActive();
1123
Sharad Sangle36781612015-05-28 16:15:16 +05301124 // increment usage count for this stream on the requested output:
1125 // NOTE that the usage count is the same for duplicated output and hardware output which is
1126 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1127 outputDesc->changeRefCount(stream, 1);
1128
1129 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
1130 // starting an output being rerouted?
1131 if (device == AUDIO_DEVICE_NONE) {
1132 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001133 }
Sharad Sangle36781612015-05-28 16:15:16 +05301134 routing_strategy strategy = getStrategy(stream);
1135 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
1136 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1137 (beaconMuteLatency > 0);
1138 uint32_t waitMs = beaconMuteLatency;
Sharad Sangle36781612015-05-28 16:15:16 +05301139 for (size_t i = 0; i < mOutputs.size(); i++) {
1140 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1141 if (desc != outputDesc) {
1142 // force a device change if any other output is managed by the same hw
1143 // module and has a current device selection that differs from selected device.
1144 // In this case, the audio HAL must receive the new device selection so that it can
1145 // change the device currently selected by the other active output.
1146 if (outputDesc->sharesHwModuleWith(desc) &&
1147 desc->device() != device) {
1148 force = true;
1149 }
1150 // wait for audio on other active outputs to be presented when starting
1151 // a notification so that audio focus effect can propagate, or that a mute/unmute
1152 // event occurred for beacon
1153 uint32_t latency = desc->latency();
1154 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1155 waitMs = latency;
1156 }
1157 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001158 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05301159 uint32_t muteWaitMs;
1160 muteWaitMs = setOutputDevice(outputDesc, device, force);
Sharad Sangle36781612015-05-28 16:15:16 +05301161
1162 // handle special case for sonification while in call
1163 if (isInCall()) {
1164 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001165 }
Sharad Sangle36781612015-05-28 16:15:16 +05301166
1167 // apply volume rules for current stream and device if necessary
1168 checkAndSetVolume(stream,
Ashish Jain40cab3a2016-03-23 11:14:14 +05301169 mVolumeCurves->getVolumeIndex(stream, device),
Sharad Sangle36781612015-05-28 16:15:16 +05301170 outputDesc,
1171 device);
1172
1173 // update the outputs if starting an output with a stream that can affect notification
1174 // routing
1175 handleNotificationRoutingForStream(stream);
1176
1177 // force reevaluating accessibility routing when ringtone or alarm starts
1178 if (strategy == STRATEGY_SONIFICATION) {
1179 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1180 }
1181 }
1182 else {
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001183 // handle special case for sonification while in call
1184 if (isInCall()) {
1185 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Sharad Sangle36781612015-05-28 16:15:16 +05301186 }
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001187 }
Sharad Sangle36781612015-05-28 16:15:16 +05301188 return NO_ERROR;
1189}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001190
Sharad Sangle36781612015-05-28 16:15:16 +05301191void AudioPolicyManagerCustom::handleIncallSonification(audio_stream_type_t stream,
1192 bool starting, bool stateChange,
1193 audio_io_handle_t output)
1194{
1195 if(!hasPrimaryOutput()) {
1196 return;
1197 }
1198 // no action needed for AUDIO_STREAM_PATCH stream type, it's for internal flinger tracks
1199 if (stream == AUDIO_STREAM_PATCH) {
1200 return;
1201 }
1202 // if the stream pertains to sonification strategy and we are in call we must
1203 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
1204 // in the device used for phone strategy and play the tone if the selected device does not
1205 // interfere with the device used for phone strategy
1206 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
1207 // many times as there are active tracks on the output
1208 const routing_strategy stream_strategy = getStrategy(stream);
1209 if ((stream_strategy == STRATEGY_SONIFICATION) ||
1210 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
1211 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
1212 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
1213 stream, starting, outputDesc->mDevice, stateChange);
1214 if (outputDesc->mRefCount[stream]) {
1215 int muteCount = 1;
1216 if (stateChange) {
1217 muteCount = outputDesc->mRefCount[stream];
1218 }
1219 if (audio_is_low_visibility(stream)) {
1220 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
1221 for (int i = 0; i < muteCount; i++) {
1222 setStreamMute(stream, starting, outputDesc);
1223 }
1224 } else {
1225 ALOGV("handleIncallSonification() high visibility");
1226 if (outputDesc->device() &
1227 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
1228 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
1229 for (int i = 0; i < muteCount; i++) {
1230 setStreamMute(stream, starting, outputDesc);
1231 }
1232 }
1233 if (starting) {
1234 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
1235 AUDIO_STREAM_VOICE_CALL);
1236 } else {
1237 mpClientInterface->stopTone();
1238 }
1239 }
1240 }
1241 }
1242}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001243
Sharad Sangle36781612015-05-28 16:15:16 +05301244void AudioPolicyManagerCustom::handleNotificationRoutingForStream(audio_stream_type_t stream) {
1245 switch(stream) {
1246 case AUDIO_STREAM_MUSIC:
1247 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
1248 updateDevicesAndOutputs();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001249 break;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001250 default:
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001251 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001252 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001253}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001254
Sharad Sangle36781612015-05-28 16:15:16 +05301255status_t AudioPolicyManagerCustom::checkAndSetVolume(audio_stream_type_t stream,
1256 int index,
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301257 const sp<AudioOutputDescriptor>& outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301258 audio_devices_t device,
1259 int delayMs, bool force)
1260{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301261 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1262 ALOGW("checkAndSetVolume() invalid stream %d", stream);
1263 return INVALID_OPERATION;
1264 }
Sharad Sangle36781612015-05-28 16:15:16 +05301265 // do not change actual stream volume if the stream is muted
1266 if (outputDesc->mMuteCount[stream] != 0) {
1267 ALOGVV("checkAndSetVolume() stream %d muted count %d",
1268 stream, outputDesc->mMuteCount[stream]);
1269 return NO_ERROR;
1270 }
1271 audio_policy_forced_cfg_t forceUseForComm =
1272 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
1273 // do not change in call volume if bluetooth is connected and vice versa
1274 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
1275 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
1276 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
1277 stream, forceUseForComm);
1278 return INVALID_OPERATION;
1279 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001280
Sharad Sangle36781612015-05-28 16:15:16 +05301281 if (device == AUDIO_DEVICE_NONE) {
1282 device = outputDesc->device();
1283 }
1284
1285 float volumeDb = computeVolume(stream, index, device);
1286 if (outputDesc->isFixedVolume(device)) {
1287 volumeDb = 0.0f;
1288 }
1289
1290 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
1291
1292 if (stream == AUDIO_STREAM_VOICE_CALL ||
1293 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
1294 float voiceVolume;
1295 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
1296 if (stream == AUDIO_STREAM_VOICE_CALL) {
Ashish Jain40cab3a2016-03-23 11:14:14 +05301297 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Sharad Sangle36781612015-05-28 16:15:16 +05301298 } else {
1299 voiceVolume = 1.0;
1300 }
1301
1302 if (voiceVolume != mLastVoiceVolume && ((outputDesc == mPrimaryOutput) ||
1303 isDirectOutput(outputDesc->mIoHandle) || device & AUDIO_DEVICE_OUT_ALL_USB)) {
1304 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
1305 mLastVoiceVolume = voiceVolume;
1306 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301307#ifdef FM_POWER_OPT
1308 } else if (stream == AUDIO_STREAM_MUSIC && hasPrimaryOutput() &&
Haynes Mathew George603ae9b2015-12-21 17:23:59 -08001309 outputDesc == mPrimaryOutput && mFMIsActive) {
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001310 /* Avoid unnecessary set_parameter calls as it puts the primary
1311 outputs FastMixer in HOT_IDLE leading to breaks in audio */
1312 if (volumeDb != mPrevFMVolumeDb) {
1313 mPrevFMVolumeDb = volumeDb;
1314 AudioParameter param = AudioParameter();
1315 param.addFloat(String8("fm_volume"), Volume::DbToAmpl(volumeDb));
Mingming Yin1433cc62016-01-04 14:58:00 -08001316 //Double delayMs to avoid sound burst while device switch.
1317 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString(), delayMs*2);
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001318 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301319#endif /* FM_POWER_OPT end */
Sharad Sangle36781612015-05-28 16:15:16 +05301320 }
1321
1322 return NO_ERROR;
1323}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001324
Sharad Sangle36781612015-05-28 16:15:16 +05301325bool AudioPolicyManagerCustom::isDirectOutput(audio_io_handle_t output) {
1326 for (size_t i = 0; i < mOutputs.size(); i++) {
1327 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1328 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1329 if ((curOutput == output) && (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
1330 return true;
1331 }
1332 }
1333 return false;
1334}
vivek mehta0ea887a2015-08-26 14:01:20 -07001335
1336status_t AudioPolicyManagerCustom::getOutputForAttr(const audio_attributes_t *attr,
1337 audio_io_handle_t *output,
1338 audio_session_t session,
1339 audio_stream_type_t *stream,
1340 uid_t uid,
1341 uint32_t samplingRate,
1342 audio_format_t format,
1343 audio_channel_mask_t channelMask,
1344 audio_output_flags_t flags,
1345 audio_port_handle_t selectedDeviceId,
1346 const audio_offload_info_t *offloadInfo)
1347{
1348 audio_offload_info_t tOffloadInfo = AUDIO_INFO_INITIALIZER;
1349
Alexy Joseph0ac09032015-10-16 15:57:53 -07001350 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
1351 bool pcmOffloadEnabled = false;
1352
1353 if (offloadDisabled) {
1354 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
1355 }
1356
1357 //read track offload property only if the global offload switch is off.
1358 if (!offloadDisabled) {
1359 pcmOffloadEnabled = property_get_bool("audio.offload.track.enable", false);
1360 }
vivek mehta0ea887a2015-08-26 14:01:20 -07001361
1362 if (offloadInfo == NULL && pcmOffloadEnabled) {
1363 tOffloadInfo.sample_rate = samplingRate;
1364 tOffloadInfo.channel_mask = channelMask;
1365 tOffloadInfo.format = format;
1366 tOffloadInfo.stream_type = *stream;
1367 tOffloadInfo.bit_width = 16; //hard coded for PCM_16
1368 if (attr != NULL) {
1369 ALOGV("found attribute .. setting usage %d ", attr->usage);
1370 tOffloadInfo.usage = attr->usage;
1371 } else {
Alexy Joseph0ac09032015-10-16 15:57:53 -07001372 ALOGI("%s:: attribute is NULL .. no usage set", __func__);
vivek mehta0ea887a2015-08-26 14:01:20 -07001373 }
1374 offloadInfo = &tOffloadInfo;
1375 }
1376
1377 return AudioPolicyManager::getOutputForAttr(attr, output, session, stream,
1378 (uid_t)uid, (uint32_t)samplingRate,
1379 format, (audio_channel_mask_t)channelMask,
1380 flags, (audio_port_handle_t)selectedDeviceId,
1381 offloadInfo);
1382}
1383
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001384audio_io_handle_t AudioPolicyManagerCustom::getOutputForDevice(
1385 audio_devices_t device,
Sharad Sangle36781612015-05-28 16:15:16 +05301386 audio_session_t session __unused,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001387 audio_stream_type_t stream,
1388 uint32_t samplingRate,
1389 audio_format_t format,
1390 audio_channel_mask_t channelMask,
1391 audio_output_flags_t flags,
1392 const audio_offload_info_t *offloadInfo)
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001393{
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001394 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001395 status_t status;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001396
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001397#ifdef AUDIO_POLICY_TEST
1398 if (mCurOutput != 0) {
1399 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1400 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1401
1402 if (mTestOutputs[mCurOutput] == 0) {
1403 ALOGV("getOutput() opening test output");
Sharad Sangle36781612015-05-28 16:15:16 +05301404 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
1405 mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001406 outputDesc->mDevice = mTestDevice;
1407 outputDesc->mLatency = mTestLatencyMs;
1408 outputDesc->mFlags =
1409 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
1410 outputDesc->mRefCount[stream] = 0;
1411 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1412 config.sample_rate = mTestSamplingRate;
1413 config.channel_mask = mTestChannels;
1414 config.format = mTestFormat;
1415 if (offloadInfo != NULL) {
1416 config.offload_info = *offloadInfo;
1417 }
1418 status = mpClientInterface->openOutput(0,
1419 &mTestOutputs[mCurOutput],
1420 &config,
1421 &outputDesc->mDevice,
1422 String8(""),
1423 &outputDesc->mLatency,
1424 outputDesc->mFlags);
1425 if (status == NO_ERROR) {
1426 outputDesc->mSamplingRate = config.sample_rate;
1427 outputDesc->mFormat = config.format;
1428 outputDesc->mChannelMask = config.channel_mask;
1429 AudioParameter outputCmd = AudioParameter();
1430 outputCmd.addInt(String8("set_id"),mCurOutput);
1431 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1432 addOutput(mTestOutputs[mCurOutput], outputDesc);
1433 }
1434 }
1435 return mTestOutputs[mCurOutput];
1436 }
1437#endif //AUDIO_POLICY_TEST
Sharad Sangle36781612015-05-28 16:15:16 +05301438 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1439 (stream != AUDIO_STREAM_MUSIC)) {
1440 // compress should not be used for non-music streams
1441 ALOGE("Offloading only allowed with music stream");
1442 return 0;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301443 }
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301444
1445 if ((stream == AUDIO_STREAM_VOICE_CALL) &&
1446 (channelMask == 1) &&
Shiv Maliyappanahallid77d3b32016-02-02 13:45:50 -08001447 (samplingRate == 8000 || samplingRate == 16000 ||
1448 samplingRate == 32000 || samplingRate == 48000)) {
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301449 // Allow Voip direct output only if:
1450 // audio mode is MODE_IN_COMMUNCATION; AND
1451 // voip output is not opened already; AND
1452 // requested sample rate matches with that of voip input stream (if opened already)
1453 int value = 0;
1454 uint32_t mode = 0, voipOutCount = 1, voipSampleRate = 1;
1455 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1456 String8("audio_mode"));
1457 AudioParameter result = AudioParameter(valueStr);
1458 if (result.getInt(String8("audio_mode"), value) == NO_ERROR) {
1459 mode = value;
1460 }
1461
1462 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1463 String8("voip_out_stream_count"));
1464 result = AudioParameter(valueStr);
1465 if (result.getInt(String8("voip_out_stream_count"), value) == NO_ERROR) {
1466 voipOutCount = value;
1467 }
1468
1469 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1470 String8("voip_sample_rate"));
1471 result = AudioParameter(valueStr);
1472 if (result.getInt(String8("voip_sample_rate"), value) == NO_ERROR) {
1473 voipSampleRate = value;
1474 }
1475
1476 if ((mode == AUDIO_MODE_IN_COMMUNICATION) && (voipOutCount == 0) &&
1477 ((voipSampleRate == 0) || (voipSampleRate == samplingRate))) {
1478 if (audio_is_linear_pcm(format)) {
1479 char propValue[PROPERTY_VALUE_MAX] = {0};
1480 property_get("use.voice.path.for.pcm.voip", propValue, "0");
1481 bool voipPcmSysPropEnabled = !strncmp("true", propValue, sizeof("true"));
1482 if (voipPcmSysPropEnabled && (format == AUDIO_FORMAT_PCM_16_BIT)) {
Karthik Reddy Katta3fb9be72015-11-25 11:18:27 +05301483 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX |
1484 AUDIO_OUTPUT_FLAG_DIRECT);
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301485 ALOGD("Set VoIP and Direct output flags for PCM format");
1486 }
1487 }
1488 }
1489 }
1490
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301491#ifdef VOICE_CONCURRENCY
1492 char propValue[PROPERTY_VALUE_MAX];
1493 bool prop_play_enabled=false, prop_voip_enabled = false;
1494
1495 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1496 prop_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001497 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301498
1499 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1500 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1501 }
1502
1503 if (prop_play_enabled && mvoice_call_state) {
1504 //check if voice call is active / running in background
1505 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1506 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1507 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1508 {
1509 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1510 if(prop_voip_enabled) {
1511 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1512 flags );
1513 return 0;
1514 }
1515 }
1516 else {
1517 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
1518 ALOGD("voice_conc:IN call mode adding ULL flags .. flags: %x ", flags );
1519 flags = AUDIO_OUTPUT_FLAG_FAST;
1520 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
1521 if (AUDIO_STREAM_MUSIC == stream) {
1522 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1523 ALOGD("voice_conc:IN call mode adding deep-buffer flags %x ", flags );
1524 }
1525 else {
1526 flags = AUDIO_OUTPUT_FLAG_FAST;
1527 ALOGD("voice_conc:IN call mode adding fast flags %x ", flags );
1528 }
1529 }
1530 }
1531 }
1532 } else if (prop_voip_enabled && mvoice_call_state) {
1533 //check if voice call is active / running in background
1534 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1535 //return only ULL ouput
1536 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1537 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1538 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1539 {
1540 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1541 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1542 flags );
1543 return 0;
1544 }
1545 }
1546 }
1547#endif
1548#ifdef RECORD_PLAY_CONCURRENCY
1549 char recConcPropValue[PROPERTY_VALUE_MAX];
1550 bool prop_rec_play_enabled = false;
1551
1552 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
1553 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
1554 }
1555 if ((prop_rec_play_enabled) &&
1556 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
1557 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
1558 if (AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1559 // allow VoIP using voice path
1560 // Do nothing
1561 } else if((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1562 ALOGD("voice_conc:MODE_IN_COMM is setforcing deep buffer output for non ULL... flags: %x", flags);
1563 // use deep buffer path for all non ULL outputs
1564 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1565 }
1566 } else if ((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1567 ALOGD("voice_conc:Record mode is on forcing deep buffer output for non ULL... flags: %x ", flags);
1568 // use deep buffer path for all non ULL outputs
1569 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1570 }
1571 }
1572 if (prop_rec_play_enabled &&
1573 (stream == AUDIO_STREAM_ENFORCED_AUDIBLE)) {
1574 ALOGD("Record conc is on forcing ULL output for ENFORCED_AUDIBLE");
1575 flags = AUDIO_OUTPUT_FLAG_FAST;
1576 }
1577#endif
1578
Sharad Sangle4509cef2015-08-19 20:47:12 +05301579#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Sharad Sangle36781612015-05-28 16:15:16 +05301580 /*
1581 * WFD audio routes back to target speaker when starting a ringtone playback.
1582 * This is because primary output is reused for ringtone, so output device is
1583 * updated based on SONIFICATION strategy for both ringtone and music playback.
1584 * The same issue is not seen on remoted_submix HAL based WFD audio because
1585 * primary output is not reused and a new output is created for ringtone playback.
1586 * Issue is fixed by updating output flag to AUDIO_OUTPUT_FLAG_FAST when there is
1587 * a non-music stream playback on WFD, so primary output is not reused for ringtone.
1588 */
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001589 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
1590 if ((availableOutputDeviceTypes & AUDIO_DEVICE_OUT_PROXY)
1591 && (stream != AUDIO_STREAM_MUSIC)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301592 ALOGD("WFD audio: use OUTPUT_FLAG_FAST for non music stream. flags:%x", flags );
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001593 //For voip paths
1594 if(flags & AUDIO_OUTPUT_FLAG_DIRECT)
1595 flags = AUDIO_OUTPUT_FLAG_DIRECT;
1596 else //route every thing else to ULL path
1597 flags = AUDIO_OUTPUT_FLAG_FAST;
1598 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301599#endif
1600
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001601 // open a direct output if required by specified parameters
vivek mehta0ea887a2015-08-26 14:01:20 -07001602 // force direct flag if offload flag is set: offloading implies a direct output stream
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001603 // and all common behaviors are driven by checking only the direct flag
1604 // this should normally be set appropriately in the policy configuration file
1605 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1606 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1607 }
1608 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1609 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1610 }
vivek mehta0ea887a2015-08-26 14:01:20 -07001611
vivek mehta4b0d8192015-10-16 00:25:59 -07001612 bool forced_deep = false;
Sharad Sangle36781612015-05-28 16:15:16 +05301613 // only allow deep buffering for music stream type
1614 if (stream != AUDIO_STREAM_MUSIC) {
1615 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Sharad Sangle497aef82015-08-03 17:55:48 +05301616 } else if (/* stream == AUDIO_STREAM_MUSIC && */
1617 flags == AUDIO_OUTPUT_FLAG_NONE &&
1618 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
vivek mehtac984b992015-11-25 13:28:55 -08001619 forced_deep = true;
Sharad Sangle36781612015-05-28 16:15:16 +05301620 }
Sharad Sangle497aef82015-08-03 17:55:48 +05301621
Sharad Sangle36781612015-05-28 16:15:16 +05301622 if (stream == AUDIO_STREAM_TTS) {
1623 flags = AUDIO_OUTPUT_FLAG_TTS;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001624 }
1625
vivek mehta4b0d8192015-10-16 00:25:59 -07001626 // Do offload magic here
vivek mehtac984b992015-11-25 13:28:55 -08001627 if ((flags == AUDIO_OUTPUT_FLAG_NONE) &&
1628 (stream == AUDIO_STREAM_MUSIC) &&
1629 (offloadInfo != NULL) &&
vivek mehta4b0d8192015-10-16 00:25:59 -07001630 ((offloadInfo->usage == AUDIO_USAGE_MEDIA) || (offloadInfo->usage == AUDIO_USAGE_GAME))) {
vivek mehtac984b992015-11-25 13:28:55 -08001631 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DIRECT_PCM);
vivek mehta4b0d8192015-10-16 00:25:59 -07001632 ALOGD("AudioCustomHAL --> Force Direct Flag .. flag (0x%x)", flags);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301633 }
1634
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001635 sp<IOProfile> profile;
1636
1637 // skip direct output selection if the request can obviously be attached to a mixed output
1638 // and not explicitly requested
vivek mehtac984b992015-11-25 13:28:55 -08001639 if (((flags & (AUDIO_OUTPUT_FLAG_DIRECT|AUDIO_OUTPUT_FLAG_DIRECT_PCM)) == 0) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301640 audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX &&
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001641 audio_channel_count_from_out_mask(channelMask) <= 2) {
1642 goto non_direct_output;
1643 }
1644
1645 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1646 // creating an offloaded track and tearing it down immediately after start when audioflinger
1647 // detects there is an active non offloadable effect.
1648 // FIXME: We should check the audio session here but we do not have it in this context.
1649 // This may prevent offloading in rare situations where effects are left active by apps
1650 // in the background.
1651
Sharad Sangle36781612015-05-28 16:15:16 +05301652 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
Ashish Jain40cab3a2016-03-23 11:14:14 +05301653 !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001654 profile = getProfileForDirectOutput(device,
1655 samplingRate,
1656 format,
1657 channelMask,
1658 (audio_output_flags_t)flags);
1659 }
1660
1661 if (profile != 0) {
vivek mehtac984b992015-11-25 13:28:55 -08001662
1663 if (!(flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301664 (profile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT_PCM)) {
vivek mehtac984b992015-11-25 13:28:55 -08001665 ALOGI("got Direct_PCM without requesting ... reject ");
1666 profile = NULL;
1667 goto non_direct_output;
1668 }
1669
Sharad Sangle36781612015-05-28 16:15:16 +05301670 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001671
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001672 // if multiple concurrent offload decode is supported
1673 // do no check for reuse and also don't close previous output if its offload
1674 // previous output will be closed during track destruction
1675 if (!(property_get_bool("audio.offload.multiple.enabled", false) &&
1676 ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0))) {
1677 for (size_t i = 0; i < mOutputs.size(); i++) {
1678 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1679 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1680 outputDesc = desc;
1681 // reuse direct output if currently open and configured with same parameters
1682 if ((samplingRate == outputDesc->mSamplingRate) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301683 audio_formats_match(format, outputDesc->mFormat) &&
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001684 (channelMask == outputDesc->mChannelMask)) {
1685 outputDesc->mDirectOpenCount++;
1686 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1687 return mOutputs.keyAt(i);
1688 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001689 }
1690 }
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001691 // close direct output if currently open and configured with different parameters
1692 if (outputDesc != NULL) {
1693 closeOutput(outputDesc->mIoHandle);
1694 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001695 }
Sharad Sangle36781612015-05-28 16:15:16 +05301696
1697 // if the selected profile is offloaded and no offload info was specified,
1698 // create a default one
1699 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
Ashish Jain40cab3a2016-03-23 11:14:14 +05301700 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
Sharad Sangle36781612015-05-28 16:15:16 +05301701 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1702 defaultOffloadInfo.sample_rate = samplingRate;
1703 defaultOffloadInfo.channel_mask = channelMask;
1704 defaultOffloadInfo.format = format;
1705 defaultOffloadInfo.stream_type = stream;
1706 defaultOffloadInfo.bit_rate = 0;
1707 defaultOffloadInfo.duration_us = -1;
1708 defaultOffloadInfo.has_video = true; // conservative
1709 defaultOffloadInfo.is_streaming = true; // likely
1710 offloadInfo = &defaultOffloadInfo;
1711 }
1712
1713 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001714 outputDesc->mDevice = device;
1715 outputDesc->mLatency = 0;
Sharad Sangle36781612015-05-28 16:15:16 +05301716 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001717 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1718 config.sample_rate = samplingRate;
1719 config.channel_mask = channelMask;
1720 config.format = format;
1721 if (offloadInfo != NULL) {
1722 config.offload_info = *offloadInfo;
1723 }
Sharad Sangle36781612015-05-28 16:15:16 +05301724 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001725 &output,
1726 &config,
1727 &outputDesc->mDevice,
1728 String8(""),
1729 &outputDesc->mLatency,
1730 outputDesc->mFlags);
1731
1732 // only accept an output with the requested parameters
1733 if (status != NO_ERROR ||
1734 (samplingRate != 0 && samplingRate != config.sample_rate) ||
Ashish Jain40cab3a2016-03-23 11:14:14 +05301735 (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) ||
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001736 (channelMask != 0 && channelMask != config.channel_mask)) {
1737 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1738 "format %d %d, channelMask %04x %04x", output, samplingRate,
1739 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1740 outputDesc->mChannelMask);
1741 if (output != AUDIO_IO_HANDLE_NONE) {
1742 mpClientInterface->closeOutput(output);
1743 }
Sharad Sangle36781612015-05-28 16:15:16 +05301744 // fall back to mixer output if possible when the direct output could not be open
Ashish Jain40cab3a2016-03-23 11:14:14 +05301745 if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) {
Sharad Sangle36781612015-05-28 16:15:16 +05301746 goto non_direct_output;
1747 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001748 return AUDIO_IO_HANDLE_NONE;
1749 }
1750 outputDesc->mSamplingRate = config.sample_rate;
1751 outputDesc->mChannelMask = config.channel_mask;
1752 outputDesc->mFormat = config.format;
1753 outputDesc->mRefCount[stream] = 0;
1754 outputDesc->mStopTime[stream] = 0;
1755 outputDesc->mDirectOpenCount = 1;
1756
1757 audio_io_handle_t srcOutput = getOutputForEffect();
1758 addOutput(output, outputDesc);
1759 audio_io_handle_t dstOutput = getOutputForEffect();
1760 if (dstOutput == output) {
Gao Jiedb057832015-11-12 08:51:22 +08001761#ifdef DOLBY_ENABLE
1762 status_t status = mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1763 if (status == NO_ERROR) {
1764 for (size_t i = 0; i < mEffects.size(); i++) {
1765 sp<EffectDescriptor> desc = mEffects.valueAt(i);
1766 if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX) {
1767 // update the mIo member of EffectDescriptor for the global effect
1768 ALOGV("%s updating mIo", __FUNCTION__);
1769 desc->mIo = dstOutput;
1770 }
1771 }
1772 } else {
1773 ALOGW("%s moveEffects from %d to %d failed", __FUNCTION__, srcOutput, dstOutput);
1774 }
1775#else // DOLBY_END
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001776 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
Gao Jiedb057832015-11-12 08:51:22 +08001777#endif // LINE_ADDED_BY_DOLBY
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001778 }
1779 mPreviousOutputs = mOutputs;
1780 ALOGV("getOutput() returns new direct output %d", output);
1781 mpClientInterface->onAudioPortListUpdate();
1782 return output;
1783 }
1784
1785non_direct_output:
Ashish Jain5fe12412016-04-18 18:44:17 +05301786
1787 // A request for HW A/V sync cannot fallback to a mixed output because time
1788 // stamps are embedded in audio data
1789 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1790 return AUDIO_IO_HANDLE_NONE;
1791 }
1792
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001793 // ignoring channel mask due to downmix capability in mixer
1794
1795 // open a non direct output
1796
1797 // for non direct outputs, only PCM is supported
1798 if (audio_is_linear_pcm(format)) {
vivek mehtac984b992015-11-25 13:28:55 -08001799 if (forced_deep) {
1800 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
1801 ALOGI("setting force DEEP buffer now ");
1802 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001803 // get which output is suitable for the specified stream. The actual
1804 // routing change will happen when startOutput() will be called
1805 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1806
1807 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1808 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1809 output = selectOutput(outputs, flags, format);
1810 }
1811 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1812 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1813
vivek mehta0ea887a2015-08-26 14:01:20 -07001814 ALOGV("getOutputForDevice() returns output %d", output);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001815
1816 return output;
1817}
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301818
1819status_t AudioPolicyManagerCustom::getInputForAttr(const audio_attributes_t *attr,
1820 audio_io_handle_t *input,
1821 audio_session_t session,
1822 uid_t uid,
1823 uint32_t samplingRate,
1824 audio_format_t format,
1825 audio_channel_mask_t channelMask,
1826 audio_input_flags_t flags,
1827 audio_port_handle_t selectedDeviceId,
1828 input_type_t *inputType)
1829{
Ashish Jain40cab3a2016-03-23 11:14:14 +05301830 audio_source_t inputSource;
1831 inputSource = attr->source;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301832#ifdef VOICE_CONCURRENCY
1833
1834 char propValue[PROPERTY_VALUE_MAX];
1835 bool prop_rec_enabled=false, prop_voip_enabled = false;
1836
1837 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
1838 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1839 }
1840
1841 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1842 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1843 }
1844
1845 if (prop_rec_enabled && mvoice_call_state) {
1846 //check if voice call is active / running in background
1847 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1848 //Need to block input request
1849 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1850 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1851 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1852 {
1853 switch(inputSource) {
1854 case AUDIO_SOURCE_VOICE_UPLINK:
1855 case AUDIO_SOURCE_VOICE_DOWNLINK:
1856 case AUDIO_SOURCE_VOICE_CALL:
1857 ALOGD("voice_conc:Creating input during incall mode for inputSource: %d",
1858 inputSource);
1859 break;
1860
1861 case AUDIO_SOURCE_VOICE_COMMUNICATION:
1862 if(prop_voip_enabled) {
1863 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1864 inputSource);
1865 return NO_INIT;
1866 }
1867 break;
1868 default:
1869 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1870 inputSource);
1871 return NO_INIT;
1872 }
1873 }
1874 }//check for VoIP flag
1875 else if(prop_voip_enabled && mvoice_call_state) {
1876 //check if voice call is active / running in background
1877 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1878 //Need to block input request
1879 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1880 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1881 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1882 {
1883 if(inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1884 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
1885 return NO_INIT;
1886 }
1887 }
1888 }
1889
1890#endif
1891
1892 return AudioPolicyManager::getInputForAttr(attr,
1893 input,
1894 session,
1895 uid,
1896 samplingRate,
1897 format,
1898 channelMask,
1899 flags,
1900 selectedDeviceId,
1901 inputType);
1902}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001903
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301904status_t AudioPolicyManagerCustom::startInput(audio_io_handle_t input,
1905 audio_session_t session)
1906{
1907 ALOGV("startInput() input %d", input);
1908 ssize_t index = mInputs.indexOfKey(input);
1909 if (index < 0) {
1910 ALOGW("startInput() unknown input %d", input);
1911 return BAD_VALUE;
1912 }
1913 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1914
Ashish Jain40cab3a2016-03-23 11:14:14 +05301915 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1916 if (audioSession == 0) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301917 ALOGW("startInput() unknown session %d on input %d", session, input);
1918 return BAD_VALUE;
1919 }
1920
1921 // virtual input devices are compatible with other input devices
1922 if (!is_virtual_input_device(inputDesc->mDevice)) {
1923
1924 // for a non-virtual input device, check if there is another (non-virtual) active input
1925 audio_io_handle_t activeInput = mInputs.getActiveInput();
1926 if (activeInput != 0 && activeInput != input) {
1927
1928 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1929 // otherwise the active input continues and the new input cannot be started.
1930 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Ashish Jain40cab3a2016-03-23 11:14:14 +05301931 if ((activeDesc->inputSource() == AUDIO_SOURCE_HOTWORD) &&
1932 !activeDesc->hasPreemptedSession(session)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301933 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
Ashish Jain40cab3a2016-03-23 11:14:14 +05301934 //FIXME: consider all active sessions
1935 AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions();
1936 audio_session_t activeSession = activeSessions.keyAt(0);
1937 SortedVector<audio_session_t> sessions =
1938 activeDesc->getPreemptedSessions();
1939 sessions.add(activeSession);
1940 inputDesc->setPreemptedSessions(sessions);
1941 stopInput(activeInput, activeSession);
1942 releaseInput(activeInput, activeSession);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301943 } else {
1944 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
1945 return INVALID_OPERATION;
1946 }
1947 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05301948 // Do not allow capture if an active voice call is using a software patch and
1949 // the call TX source device is on the same HW module.
1950 // FIXME: would be better to refine to only inputs whose profile connects to the
1951 // call TX device but this information is not in the audio patch
1952 if (mCallTxPatch != 0 &&
1953 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1954 return INVALID_OPERATION;
1955 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301956 }
1957
1958 // Routing?
1959 mInputRoutes.incRouteActivity(session);
1960#ifdef RECORD_PLAY_CONCURRENCY
1961 mIsInputRequestOnProgress = true;
1962
1963 char getPropValue[PROPERTY_VALUE_MAX];
1964 bool prop_rec_play_enabled = false;
1965
1966 if (property_get("rec.playback.conc.disabled", getPropValue, NULL)) {
1967 prop_rec_play_enabled = atoi(getPropValue) || !strncmp("true", getPropValue, 4);
1968 }
1969
1970 if ((prop_rec_play_enabled) &&(mInputs.activeInputsCount() == 0)){
1971 // send update to HAL on record playback concurrency
1972 AudioParameter param = AudioParameter();
1973 param.add(String8("rec_play_conc_on"), String8("true"));
1974 ALOGD("startInput() setParameters rec_play_conc is setting to ON ");
1975 mpClientInterface->setParameters(0, param.toString());
1976
1977 // Call invalidate to reset all opened non ULL audio tracks
1978 // Move tracks associated to this strategy from previous output to new output
1979 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
1980 // Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder)
Sharad Sangle4509cef2015-08-19 20:47:12 +05301981 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE && (i != AUDIO_STREAM_PATCH))) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301982 ALOGD("Invalidate on releaseInput for stream :: %d ", i);
1983 //FIXME see fixme on name change
1984 mpClientInterface->invalidateStream((audio_stream_type_t)i);
1985 }
1986 }
1987 // close compress tracks
1988 for (size_t i = 0; i < mOutputs.size(); i++) {
1989 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
1990 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
1991 ALOGD("ouput desc / profile is NULL");
1992 continue;
1993 }
1994 if (outputDesc->mProfile->mFlags
1995 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1996 // close compress sessions
1997 ALOGD("calling closeOutput on record conc for COMPRESS output");
1998 closeOutput(mOutputs.keyAt(i));
1999 }
2000 }
2001 }
2002#endif
2003
Ashish Jain40cab3a2016-03-23 11:14:14 +05302004 if (!inputDesc->isActive() || mInputRoutes.hasRouteChanged(session)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302005 // if input maps to a dynamic policy with an activity listener, notify of state change
Ashish Jain630c5e12016-04-18 11:50:21 +05302006 if ((inputDesc->mPolicyMix != NULL)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302007 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Ashish Jain5fe12412016-04-18 18:44:17 +05302008 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302009 MIX_STATE_MIXING);
Ashish Jain630c5e12016-04-18 11:50:21 +05302010 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302011
2012 if (mInputs.activeInputsCount() == 0) {
2013 SoundTrigger::setCaptureState(true);
2014 }
2015 setInputDevice(input, getNewInputDevice(input), true /* force */);
2016
2017 // automatically enable the remote submix output when input is started if not
2018 // used by a policy mix of type MIX_TYPE_RECORDERS
2019 // For remote submix (a virtual device), we open only one input per capture request.
2020 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
2021 String8 address = String8("");
2022 if (inputDesc->mPolicyMix == NULL) {
2023 address = String8("0");
Ashish Jain630c5e12016-04-18 11:50:21 +05302024 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
Ashish Jain5fe12412016-04-18 18:44:17 +05302025 address = inputDesc->mPolicyMix->mDeviceAddress;
Ashish Jain630c5e12016-04-18 11:50:21 +05302026 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302027 if (address != "") {
2028 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2029 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2030 address, "remote-submix");
2031 }
2032 }
2033 }
2034
Ashish Jain40cab3a2016-03-23 11:14:14 +05302035 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302036
Ashish Jain40cab3a2016-03-23 11:14:14 +05302037 audioSession->changeActiveCount(1);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302038#ifdef RECORD_PLAY_CONCURRENCY
2039 mIsInputRequestOnProgress = false;
2040#endif
2041 return NO_ERROR;
2042}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08002043
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302044status_t AudioPolicyManagerCustom::stopInput(audio_io_handle_t input,
2045 audio_session_t session)
2046{
2047 status_t status;
2048 status = AudioPolicyManager::stopInput(input, session);
2049#ifdef RECORD_PLAY_CONCURRENCY
2050 char propValue[PROPERTY_VALUE_MAX];
2051 bool prop_rec_play_enabled = false;
2052
2053 if (property_get("rec.playback.conc.disabled", propValue, NULL)) {
2054 prop_rec_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
2055 }
2056
2057 if ((prop_rec_play_enabled) && (mInputs.activeInputsCount() == 0)) {
2058
2059 //send update to HAL on record playback concurrency
2060 AudioParameter param = AudioParameter();
2061 param.add(String8("rec_play_conc_on"), String8("false"));
2062 ALOGD("stopInput() setParameters rec_play_conc is setting to OFF ");
2063 mpClientInterface->setParameters(0, param.toString());
2064
2065 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
2066 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
2067 //Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder stop tone)
2068 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE) && (i != AUDIO_STREAM_PATCH)) {
2069 ALOGD(" Invalidate on stopInput for stream :: %d ", i);
2070 //FIXME see fixme on name change
2071 mpClientInterface->invalidateStream((audio_stream_type_t)i);
2072 }
2073 }
2074 }
2075#endif
2076 return status;
2077}
2078
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302079void AudioPolicyManagerCustom::closeAllInputs() {
2080 bool patchRemoved = false;
2081
Dhanalakshmi Siddanibfeca7f2015-10-08 15:17:11 +05302082 for(size_t input_index = mInputs.size(); input_index > 0; input_index--) {
2083 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index-1);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302084 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302085 if (patch_index >= 0) {
2086 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302087 status_t status;
2088 status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302089 mAudioPatches.removeItemsAt(patch_index);
2090 patchRemoved = true;
2091 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05302092 if ((inputDesc->getOpenRefCount() > 0) && inputDesc->isSoundTrigger()
2093 && (mInputs.size() == 1)) {
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302094 ALOGD("Do not close sound trigger input handle");
2095 } else {
Dhanalakshmi Siddanibfeca7f2015-10-08 15:17:11 +05302096 mpClientInterface->closeInput(mInputs.keyAt(input_index-1));
2097 mInputs.removeItem(mInputs.keyAt(input_index-1));
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302098 }
2099 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05302100 mInputs.clear();
2101 SoundTrigger::setCaptureState(false);
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302102 nextAudioPortGeneration();
2103
2104 if (patchRemoved) {
2105 mpClientInterface->onAudioPatchListUpdate();
2106 }
2107}
2108
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302109AudioPolicyManagerCustom::AudioPolicyManagerCustom(AudioPolicyClientInterface *clientInterface)
Sharad Sangle4509cef2015-08-19 20:47:12 +05302110 : AudioPolicyManager(clientInterface),
2111 mHdmiAudioDisabled(false),
2112 mHdmiAudioEvent(false),
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08002113 mPrevPhoneState(0),
Haynes Mathew George603ae9b2015-12-21 17:23:59 -08002114 mPrevFMVolumeDb(0.0f),
2115 mFMIsActive(false)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302116{
Ashish Jain40cab3a2016-03-23 11:14:14 +05302117
2118 //TODO: Check the new logic to parse policy conf and update the below code
2119 // Need this when SSR encoding is enabled
Mingming Yin38ea08c2015-10-05 15:24:04 -07002120 char ssr_enabled[PROPERTY_VALUE_MAX] = {0};
2121 bool prop_ssr_enabled = false;
2122
2123 if (property_get("ro.qc.sdk.audio.ssr", ssr_enabled, NULL)) {
2124 prop_ssr_enabled = atoi(ssr_enabled) || !strncmp("true", ssr_enabled, 4);
2125 }
2126
2127 for (size_t i = 0; i < mHwModules.size(); i++) {
Ashish Jain40cab3a2016-03-23 11:14:14 +05302128 ALOGV("Hw module %zu", i);
Mingming Yin38ea08c2015-10-05 15:24:04 -07002129 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) {
2130 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Ashish Jain40cab3a2016-03-23 11:14:14 +05302131 AudioProfileVector profiles = inProfile->getAudioProfiles();
2132 for (size_t k = 0; k < profiles.size(); k++){
2133 ChannelsVector channels = profiles[k]->getChannels();
2134 for (size_t x = 0; x < channels.size(); x++) {
2135 audio_channel_mask_t channelMask = channels[x];
2136 ALOGV("Channel Mask %x size %zu", channelMask,
2137 channels.size());
2138 if (AUDIO_CHANNEL_IN_5POINT1 == channelMask) {
2139 if (!prop_ssr_enabled) {
2140 ALOGI("removing AUDIO_CHANNEL_IN_5POINT1 from"
2141 " input profile as SSR(surround sound record)"
2142 " is not supported on this chipset variant");
2143 channels.removeItemsAt(x, 1);
2144 ALOGV("Channel Mask size now %zu",
2145 channels.size());
2146 }
Mingming Yin38ea08c2015-10-05 15:24:04 -07002147 }
2148 }
2149 }
2150 }
2151 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302152#ifdef RECORD_PLAY_CONCURRENCY
2153 mIsInputRequestOnProgress = false;
2154#endif
2155
2156
2157#ifdef VOICE_CONCURRENCY
2158 mFallBackflag = getFallBackPath();
2159#endif
2160}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07002161}