blob: 839f1ebeced4464398f176a6118c3dc1d4435936 [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
Ashish Jainac510a72016-04-28 12:22:58 +0530463 //check if it's multi-channel AAC (includes sub formats) and FLAC format
464 if ((popcount(offloadInfo.channel_mask) > 2) &&
465 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
466 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS))) {
467 ALOGD("offload disabled for multi-channel AAC,FLAC and VORBIS format");
468 return false;
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530469 }
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530470
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530471#ifdef AUDIO_EXTN_FORMATS_ENABLED
Ashish Jainac510a72016-04-28 12:22:58 +0530472 //check if it's multi-channel FLAC/ALAC/WMA format with sample rate > 48k
473 if ((popcount(offloadInfo.channel_mask) > 2) &&
474 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
475 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) && (offloadInfo.sample_rate > 48000)) ||
476 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) && (offloadInfo.sample_rate > 48000)) ||
477 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) && (offloadInfo.sample_rate > 48000)) ||
478 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS))) {
479 ALOGD("offload disabled for multi-channel FLAC/ALAC/WMA/AAC_ADTS clips with sample rate > 48kHz");
480 return false;
481 }
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530482#endif
Ashish Jainac510a72016-04-28 12:22:58 +0530483 //TODO: enable audio offloading with video when ready
484 const bool allowOffloadWithVideo =
485 property_get_bool("audio.offload.video", false /* default_value */);
486 if (offloadInfo.has_video && !allowOffloadWithVideo) {
487 ALOGV("isOffloadSupported: has_video == true, returning false");
488 return false;
489 }
Manish Dewanganf3cd0f82015-10-13 14:04:36 +0530490
Ashish Jainac510a72016-04-28 12:22:58 +0530491 const bool allowOffloadStreamingWithVideo = property_get_bool("av.streaming.offload.enable",
492 false /*default value*/);
493 if (offloadInfo.has_video && offloadInfo.is_streaming && !allowOffloadStreamingWithVideo) {
494 ALOGW("offload disabled by av.streaming.offload.enable %d",allowOffloadStreamingWithVideo);
495 return false;
Sharad Sangle36781612015-05-28 16:15:16 +0530496 }
497
498 //If duration is less than minimum value defined in property, return false
Ashish Jainac510a72016-04-28 12:22:58 +0530499 char propValue[PROPERTY_VALUE_MAX];
Sharad Sangle36781612015-05-28 16:15:16 +0530500 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
501 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
502 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
503 return false;
504 }
505 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
506 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
507 //duration checks only valid for MP3/AAC/ formats,
508 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
509 if ((offloadInfo.format == AUDIO_FORMAT_MP3) ||
510 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
511 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
Ashish Jainac510a72016-04-28 12:22:58 +0530512 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS))
513 return false;
514
515#ifdef AUDIO_EXTN_FORMATS_ENABLED
516 if (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) ||
Sharad Sangle36781612015-05-28 16:15:16 +0530517 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) ||
518 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) ||
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530519 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_APE) ||
Ashish Jainac510a72016-04-28 12:22:58 +0530520 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS))
Sharad Sangle36781612015-05-28 16:15:16 +0530521 return false;
Ashish Jainac510a72016-04-28 12:22:58 +0530522#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530523 }
524
525 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
526 // creating an offloaded track and tearing it down immediately after start when audioflinger
527 // detects there is an active non offloadable effect.
528 // FIXME: We should check the audio session here but we do not have it in this context.
529 // This may prevent offloading in rare situations where effects are left active by apps
530 // in the background.
531 if (mEffects.isNonOffloadableEffectEnabled()) {
532 return false;
533 }
534 // Check for soundcard status
535 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
536 String8("SND_CARD_STATUS"));
537 AudioParameter result = AudioParameter(valueStr);
538 int isonline = 0;
539 if ((result.getInt(String8("SND_CARD_STATUS"), isonline) == NO_ERROR)
540 && !isonline) {
541 ALOGD("copl: soundcard is offline rejecting offload request");
542 return false;
543 }
544 // See if there is a profile to support this.
545 // AUDIO_DEVICE_NONE
546 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
547 offloadInfo.sample_rate,
548 offloadInfo.format,
549 offloadInfo.channel_mask,
550 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
551 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
552 return (profile != 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700553}
Weiyin Jiangd211ef52016-02-16 15:55:11 +0800554
Sharad Sangle36781612015-05-28 16:15:16 +0530555audio_devices_t AudioPolicyManagerCustom::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
556 bool fromCache)
557{
558 audio_devices_t device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700559
Ashish Jain40cab3a2016-03-23 11:14:14 +0530560 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Sharad Sangle36781612015-05-28 16:15:16 +0530561 if (index >= 0) {
562 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
563 if (patchDesc->mUid != mUidCached) {
564 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Ashish Jain40cab3a2016-03-23 11:14:14 +0530565 outputDesc->device(), outputDesc->getPatchHandle());
Sharad Sangle36781612015-05-28 16:15:16 +0530566 return outputDesc->device();
567 }
568 }
569
570 // check the following by order of priority to request a routing change if necessary:
571 // 1: the strategy enforced audible is active and enforced on the output:
572 // use device for strategy enforced audible
573 // 2: we are in call or the strategy phone is active on the output:
574 // use device for strategy phone
575 // 3: the strategy for enforced audible is active but not enforced on the output:
576 // use the device for strategy enforced audible
577 // 4: the strategy sonification is active on the output:
578 // use device for strategy sonification
579 // 5: the strategy "respectful" sonification is active on the output:
580 // use device for strategy "respectful" sonification
581 // 6: the strategy accessibility is active on the output:
582 // use device for strategy accessibility
583 // 7: the strategy media is active on the output:
584 // use device for strategy media
585 // 8: the strategy DTMF is active on the output:
586 // use device for strategy DTMF
587 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
588 // use device for strategy t-t-s
589 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
590 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
591 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
592 } else if (isInCall() ||
593 isStrategyActive(outputDesc, STRATEGY_PHONE)||
594 isStrategyActive(mPrimaryOutput, STRATEGY_PHONE)) {
595 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
596 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
597 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
598 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)||
599 (isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION)
600 && (!isStrategyActive(mPrimaryOutput,STRATEGY_MEDIA)))) {
601 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530602 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL) ||
603 isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION_RESPECTFUL)) {
Sharad Sangle36781612015-05-28 16:15:16 +0530604 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
605 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
606 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
607 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
608 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
609 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
610 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
611 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
612 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
613 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
614 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
615 }
616
617 ALOGV("getNewOutputDevice() selected device %x", device);
618 return device;
619}
Weiyin Jiangd211ef52016-02-16 15:55:11 +0800620
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700621void AudioPolicyManagerCustom::setPhoneState(audio_mode_t state)
622{
Ashish Jain40cab3a2016-03-23 11:14:14 +0530623 ALOGD("setPhoneState() state %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530624 // store previous phone state for management of sonification strategy below
Sharad Sangle4509cef2015-08-19 20:47:12 +0530625 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Sharad Sangle36781612015-05-28 16:15:16 +0530626 int oldState = mEngine->getPhoneState();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700627
Sharad Sangle36781612015-05-28 16:15:16 +0530628 if (mEngine->setPhoneState(state) != NO_ERROR) {
629 ALOGW("setPhoneState() invalid or same state %d", state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700630 return;
631 }
Sharad Sangle36781612015-05-28 16:15:16 +0530632 /// Opens: can these line be executed after the switch of volume curves???
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700633 // if leaving call state, handle special case of active streams
634 // pertaining to sonification strategy see handleIncallSonification()
Zhou Song58081b62016-02-24 13:10:55 +0800635 if (isStateInCall(oldState)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700636 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530637 for (size_t j = 0; j < mOutputs.size(); j++) {
638 audio_io_handle_t curOutput = mOutputs.keyAt(j);
639 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
640 if (stream == AUDIO_STREAM_PATCH) {
641 continue;
642 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530643 handleIncallSonification((audio_stream_type_t)stream, false, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +0530644 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700645 }
Sharad Sangle36781612015-05-28 16:15:16 +0530646
Zhou Song58081b62016-02-24 13:10:55 +0800647 // force reevaluating accessibility routing when call stops
Sharad Sangle36781612015-05-28 16:15:16 +0530648 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700649 }
650
Sharad Sangle36781612015-05-28 16:15:16 +0530651 /**
652 * Switching to or from incall state or switching between telephony and VoIP lead to force
653 * routing command.
654 */
655 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
656 || (is_state_in_call(state) && (state != oldState)));
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700657
658 // check for device and output changes triggered by new phone state
659 checkA2dpSuspend();
660 checkOutputForAllStrategies();
661 updateDevicesAndOutputs();
662
Sharad Sangle36781612015-05-28 16:15:16 +0530663 sp<SwAudioOutputDescriptor> hwOutputDesc = mPrimaryOutput;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530664#ifdef VOICE_CONCURRENCY
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530665 char propValue[PROPERTY_VALUE_MAX];
666 bool prop_playback_enabled = false, prop_rec_enabled=false, prop_voip_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700667
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530668 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
669 prop_playback_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
670 }
671
672 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
673 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
674 }
675
676 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
677 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
678 }
679
Dhananjay Kumar9125b2a2015-12-01 16:24:09 +0530680 if ((AUDIO_MODE_IN_CALL != oldState) && (AUDIO_MODE_IN_CALL == state)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530681 ALOGD("voice_conc:Entering to call mode oldState :: %d state::%d ",
682 oldState, state);
Dhananjay Kumar9125b2a2015-12-01 16:24:09 +0530683 mvoice_call_state = state;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530684 if (prop_rec_enabled) {
685 //Close all active inputs
686 audio_io_handle_t activeInput = mInputs.getActiveInput();
687 if (activeInput != 0) {
688 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
689 switch(activeDesc->mInputSource) {
690 case AUDIO_SOURCE_VOICE_UPLINK:
691 case AUDIO_SOURCE_VOICE_DOWNLINK:
692 case AUDIO_SOURCE_VOICE_CALL:
693 ALOGD("voice_conc:FOUND active input during call active: %d",activeDesc->mInputSource);
694 break;
695
696 case AUDIO_SOURCE_VOICE_COMMUNICATION:
697 if(prop_voip_enabled) {
698 ALOGD("voice_conc:CLOSING VoIP input source on call setup :%d ",activeDesc->mInputSource);
699 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
700 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
701 }
702 break;
703
704 default:
705 ALOGD("voice_conc:CLOSING input on call setup for inputSource: %d",activeDesc->mInputSource);
706 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
707 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
708 break;
709 }
710 }
711 } else if (prop_voip_enabled) {
712 audio_io_handle_t activeInput = mInputs.getActiveInput();
713 if (activeInput != 0) {
714 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
715 if (AUDIO_SOURCE_VOICE_COMMUNICATION == activeDesc->mInputSource) {
716 ALOGD("voice_conc:CLOSING VoIP on call setup : %d",activeDesc->mInputSource);
717 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
718 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
719 }
720 }
721 }
722 if (prop_playback_enabled) {
723 // Move tracks associated to this strategy from previous output to new output
724 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
725 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
726 if (i == AUDIO_STREAM_PATCH) {
727 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
728 continue;
729 }
730 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
731 if ((AUDIO_STREAM_MUSIC == i) ||
732 (AUDIO_STREAM_VOICE_CALL == i) ) {
733 ALOGD("voice_conc:Invalidate stream type %d", i);
734 mpClientInterface->invalidateStream((audio_stream_type_t)i);
735 }
736 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
737 ALOGD("voice_conc:Invalidate stream type %d", i);
738 mpClientInterface->invalidateStream((audio_stream_type_t)i);
739 }
740 }
741 }
742
743 for (size_t i = 0; i < mOutputs.size(); i++) {
744 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
745 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
746 ALOGD("voice_conc:ouput desc / profile is NULL");
747 continue;
748 }
749
750 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
751 if (((!outputDesc->isDuplicated() &&outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY))
752 && prop_playback_enabled) {
753 ALOGD("voice_conc:calling suspendOutput on call mode for primary output");
754 mpClientInterface->suspendOutput(mOutputs.keyAt(i));
755 } //Close compress all sessions
756 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
757 && prop_playback_enabled) {
758 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
759 closeOutput(mOutputs.keyAt(i));
760 }
761 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_VOIP_RX)
762 && prop_voip_enabled) {
763 ALOGD("voice_conc:calling closeOutput on call mode for DIRECT output");
764 closeOutput(mOutputs.keyAt(i));
765 }
766 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
767 if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)
768 && prop_playback_enabled) {
769 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
770 closeOutput(mOutputs.keyAt(i));
771 }
772 }
773 }
774 }
775
776 if ((AUDIO_MODE_IN_CALL == oldState || AUDIO_MODE_IN_COMMUNICATION == oldState) &&
777 (AUDIO_MODE_NORMAL == state) && prop_playback_enabled && mvoice_call_state) {
778 ALOGD("voice_conc:EXITING from call mode oldState :: %d state::%d \n",oldState, state);
779 mvoice_call_state = 0;
780 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
781 //restore PCM (deep-buffer) output after call termination
782 for (size_t i = 0; i < mOutputs.size(); i++) {
783 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
784 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
785 ALOGD("voice_conc:ouput desc / profile is NULL");
786 continue;
787 }
788 if (!outputDesc->isDuplicated() && outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
789 ALOGD("voice_conc:calling restoreOutput after call mode for primary output");
790 mpClientInterface->restoreOutput(mOutputs.keyAt(i));
791 }
792 }
793 }
794 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
795 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
796 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
797 if (i == AUDIO_STREAM_PATCH) {
798 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
799 continue;
800 }
801 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
802 if ((AUDIO_STREAM_MUSIC == i) ||
803 (AUDIO_STREAM_VOICE_CALL == i) ) {
804 mpClientInterface->invalidateStream((audio_stream_type_t)i);
805 }
806 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
807 mpClientInterface->invalidateStream((audio_stream_type_t)i);
808 }
809 }
810 }
811
812#endif
813#ifdef RECORD_PLAY_CONCURRENCY
814 char recConcPropValue[PROPERTY_VALUE_MAX];
815 bool prop_rec_play_enabled = false;
816
817 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
818 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
819 }
820 if (prop_rec_play_enabled) {
821 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
822 ALOGD("phone state changed to MODE_IN_COMM invlaidating music and voice streams");
823 // call invalidate for voice streams, so that it can use deepbuffer with VoIP out device from HAL
824 mpClientInterface->invalidateStream(AUDIO_STREAM_VOICE_CALL);
825 // call invalidate for music, so that compress will fallback to deep-buffer with VoIP out device
826 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
827
828 // close compress output to make sure session will be closed before timeout(60sec)
829 for (size_t i = 0; i < mOutputs.size(); i++) {
830
831 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
832 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
833 ALOGD("ouput desc / profile is NULL");
834 continue;
835 }
836
837 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
838 ALOGD("calling closeOutput on call mode for COMPRESS output");
839 closeOutput(mOutputs.keyAt(i));
840 }
841 }
842 } else if ((oldState == AUDIO_MODE_IN_COMMUNICATION) &&
843 (mEngine->getPhoneState() == AUDIO_MODE_NORMAL)) {
844 // call invalidate for music so that music can fallback to compress
845 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
846 }
847 }
848#endif
849 mPrevPhoneState = oldState;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700850 int delayMs = 0;
851 if (isStateInCall(state)) {
852 nsecs_t sysTime = systemTime();
853 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530854 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700855 // mute media and sonification strategies and delay device switch by the largest
856 // latency of any output where either strategy is active.
857 // This avoid sending the ring tone or music tail into the earpiece or headset.
Sharad Sangle36781612015-05-28 16:15:16 +0530858 if ((isStrategyActive(desc, STRATEGY_MEDIA,
859 SONIFICATION_HEADSET_MUSIC_DELAY,
860 sysTime) ||
861 isStrategyActive(desc, STRATEGY_SONIFICATION,
862 SONIFICATION_HEADSET_MUSIC_DELAY,
863 sysTime)) &&
864 (delayMs < (int)desc->latency()*2)) {
865 delayMs = desc->latency()*2;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700866 }
Sharad Sangle36781612015-05-28 16:15:16 +0530867 setStrategyMute(STRATEGY_MEDIA, true, desc);
868 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700869 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Sharad Sangle36781612015-05-28 16:15:16 +0530870 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
871 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700872 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
873 }
874 }
875
Sharad Sangle36781612015-05-28 16:15:16 +0530876 if (hasPrimaryOutput()) {
877 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
878 // the device returned is not necessarily reachable via this output
879 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
880 // force routing command to audio hardware when ending call
881 // even if no device change is needed
882 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
883 rxDevice = mPrimaryOutput->device();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700884 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700885
Sharad Sangle36781612015-05-28 16:15:16 +0530886 if (state == AUDIO_MODE_IN_CALL) {
887 updateCallRouting(rxDevice, delayMs);
888 } else if (oldState == AUDIO_MODE_IN_CALL) {
889 if (mCallRxPatch != 0) {
890 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
891 mCallRxPatch.clear();
892 }
893 if (mCallTxPatch != 0) {
894 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
895 mCallTxPatch.clear();
896 }
897 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
898 } else {
899 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700900 }
901 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530902 //update device for all non-primary outputs
903 for (size_t i = 0; i < mOutputs.size(); i++) {
904 audio_io_handle_t output = mOutputs.keyAt(i);
905 if (output != mPrimaryOutput->mIoHandle) {
906 newDevice = getNewOutputDevice(mOutputs.valueFor(output), false /*fromCache*/);
907 setOutputDevice(mOutputs.valueFor(output), newDevice, (newDevice != AUDIO_DEVICE_NONE));
908 }
909 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700910 // if entering in call state, handle special case of active streams
911 // pertaining to sonification strategy see handleIncallSonification()
912 if (isStateInCall(state)) {
913 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530914 for (size_t j = 0; j < mOutputs.size(); j++) {
915 audio_io_handle_t curOutput = mOutputs.keyAt(j);
916 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
917 if (stream == AUDIO_STREAM_PATCH) {
918 continue;
919 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530920 handleIncallSonification((audio_stream_type_t)stream, true, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +0530921 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700922 }
Zhou Song58081b62016-02-24 13:10:55 +0800923
924 // force reevaluating accessibility routing when call starts
925 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700926 }
927
928 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
929 if (state == AUDIO_MODE_RINGTONE &&
930 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
931 mLimitRingtoneVolume = true;
932 } else {
933 mLimitRingtoneVolume = false;
934 }
935}
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +0530936
937void AudioPolicyManagerCustom::setForceUse(audio_policy_force_use_t usage,
938 audio_policy_forced_cfg_t config)
939{
Ashish Jain40cab3a2016-03-23 11:14:14 +0530940 ALOGD("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +0530941
942 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
943 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
944 return;
945 }
946 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
947 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
948 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
949
950 // check for device and output changes triggered by new force usage
951 checkA2dpSuspend();
952 checkOutputForAllStrategies();
953 updateDevicesAndOutputs();
Ashish Jain40cab3a2016-03-23 11:14:14 +0530954
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +0530955 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
956 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
957 updateCallRouting(newDevice);
958 }
959 // Use reverse loop to make sure any low latency usecases (generally tones)
960 // are not routed before non LL usecases (generally music).
961 // We can safely assume that LL output would always have lower index,
962 // and use this work-around to avoid routing of output with music stream
963 // from the context of short lived LL output.
964 // Note: in case output's share backend(HAL sharing is implicit) all outputs
965 // gets routing update while processing first output itself.
966 for (size_t i = mOutputs.size(); i > 0; i--) {
967 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i-1);
968 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
969 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || outputDesc != mPrimaryOutput) {
970 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
971 }
972 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
973 applyStreamVolumes(outputDesc, newDevice, 0, true);
974 }
975 }
976
977 audio_io_handle_t activeInput = mInputs.getActiveInput();
978 if (activeInput != 0) {
Ashish Jain40cab3a2016-03-23 11:14:14 +0530979 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
980 audio_devices_t newDevice = getNewInputDevice(activeInput);
981 // Force new input selection if the new device can not be reached via current input
982 if (activeDesc->mProfile->getSupportedDevices().types() & (newDevice & ~AUDIO_DEVICE_BIT_IN)) {
983 setInputDevice(activeInput, newDevice);
984 } else {
985 closeInput(activeInput);
986 }
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +0530987 }
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +0530988}
989
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +0530990status_t AudioPolicyManagerCustom::stopSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +0530991 audio_stream_type_t stream,
992 bool forceDeviceUpdate)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700993{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +0530994 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
995 ALOGW("stopSource() invalid stream %d", stream);
996 return INVALID_OPERATION;
997 }
Sharad Sangle36781612015-05-28 16:15:16 +0530998 // always handle stream stop, check which stream type is stopping
999 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001000
Sharad Sangle36781612015-05-28 16:15:16 +05301001 // handle special case for sonification while in call
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001002 if (isInCall()) {
Sharad Sangle36781612015-05-28 16:15:16 +05301003 if (outputDesc->isDuplicated()) {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301004 handleIncallSonification(stream, false, false, outputDesc->subOutput1()->mIoHandle);
1005 handleIncallSonification(stream, false, false, outputDesc->subOutput2()->mIoHandle);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001006 }
Sharad Sangle36781612015-05-28 16:15:16 +05301007 handleIncallSonification(stream, false, false, outputDesc->mIoHandle);
1008 }
1009
1010 if (outputDesc->mRefCount[stream] > 0) {
1011 // decrement usage count of this stream on the output
1012 outputDesc->changeRefCount(stream, -1);
1013
1014 // store time at which the stream was stopped - see isStreamActive()
1015 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
1016 outputDesc->mStopTime[stream] = systemTime();
Zhou Song5dcddc92015-09-21 14:36:57 +08001017 audio_devices_t prevDevice = outputDesc->device();
Sharad Sangle36781612015-05-28 16:15:16 +05301018 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
1019 // delay the device switch by twice the latency because stopOutput() is executed when
1020 // the track stop() command is received and at that time the audio track buffer can
1021 // still contain data that needs to be drained. The latency only covers the audio HAL
1022 // and kernel buffers. Also the latency does not always include additional delay in the
1023 // audio path (audio DSP, CODEC ...)
1024 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
1025
1026 // force restoring the device selection on other active outputs if it differs from the
1027 // one being selected for this output
1028 for (size_t i = 0; i < mOutputs.size(); i++) {
1029 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1030 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1031 if (desc != outputDesc &&
1032 desc->isActive() &&
1033 outputDesc->sharesHwModuleWith(desc) &&
1034 (newDevice != desc->device())) {
Sharad Sangle4509cef2015-08-19 20:47:12 +05301035 audio_devices_t dev = getNewOutputDevice(mOutputs.valueFor(curOutput), false /*fromCache*/);
Zhou Song5dcddc92015-09-21 14:36:57 +08001036 uint32_t delayMs;
1037 if (dev == prevDevice) {
1038 delayMs = 0;
1039 } else {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301040 delayMs = outputDesc->latency()*2;
Zhou Song5dcddc92015-09-21 14:36:57 +08001041 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301042 setOutputDevice(desc,
1043 dev,
Sharad Sangle36781612015-05-28 16:15:16 +05301044 true,
Zhou Song5dcddc92015-09-21 14:36:57 +08001045 delayMs);
Sharad Sangle36781612015-05-28 16:15:16 +05301046 }
1047 }
1048 // update the outputs if stopping one with a stream that can affect notification routing
1049 handleNotificationRoutingForStream(stream);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001050 }
Sharad Sangle36781612015-05-28 16:15:16 +05301051 return NO_ERROR;
1052 } else {
1053 ALOGW("stopOutput() refcount is already 0");
1054 return INVALID_OPERATION;
1055 }
1056}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001057
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301058status_t AudioPolicyManagerCustom::startSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301059 audio_stream_type_t stream,
1060 audio_devices_t device,
Lalit Kansaraf803ac52016-05-09 19:12:01 +05301061 const char *address,
Sharad Sangle36781612015-05-28 16:15:16 +05301062 uint32_t *delayMs)
1063{
1064 // cannot start playback of STREAM_TTS if any other output is being used
1065 uint32_t beaconMuteLatency = 0;
1066
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301067 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1068 ALOGW("startSource() invalid stream %d", stream);
1069 return INVALID_OPERATION;
1070 }
1071
Sharad Sangle36781612015-05-28 16:15:16 +05301072 *delayMs = 0;
1073 if (stream == AUDIO_STREAM_TTS) {
1074 ALOGV("\t found BEACON stream");
Ashish Jain40cab3a2016-03-23 11:14:14 +05301075 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301076 return INVALID_OPERATION;
1077 } else {
1078 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001079 }
Sharad Sangle36781612015-05-28 16:15:16 +05301080 } else {
1081 // some playback other than beacon starts
1082 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1083 }
1084
Ashish Jain40cab3a2016-03-23 11:14:14 +05301085 // check active before incrementing usage count
1086 bool force = !outputDesc->isActive();
1087
Sharad Sangle36781612015-05-28 16:15:16 +05301088 // increment usage count for this stream on the requested output:
1089 // NOTE that the usage count is the same for duplicated output and hardware output which is
1090 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1091 outputDesc->changeRefCount(stream, 1);
1092
1093 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
1094 // starting an output being rerouted?
1095 if (device == AUDIO_DEVICE_NONE) {
1096 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001097 }
Sharad Sangle36781612015-05-28 16:15:16 +05301098 routing_strategy strategy = getStrategy(stream);
1099 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
1100 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1101 (beaconMuteLatency > 0);
1102 uint32_t waitMs = beaconMuteLatency;
Sharad Sangle36781612015-05-28 16:15:16 +05301103 for (size_t i = 0; i < mOutputs.size(); i++) {
1104 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1105 if (desc != outputDesc) {
1106 // force a device change if any other output is managed by the same hw
1107 // module and has a current device selection that differs from selected device.
1108 // In this case, the audio HAL must receive the new device selection so that it can
1109 // change the device currently selected by the other active output.
1110 if (outputDesc->sharesHwModuleWith(desc) &&
1111 desc->device() != device) {
1112 force = true;
1113 }
1114 // wait for audio on other active outputs to be presented when starting
1115 // a notification so that audio focus effect can propagate, or that a mute/unmute
1116 // event occurred for beacon
1117 uint32_t latency = desc->latency();
1118 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1119 waitMs = latency;
1120 }
1121 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001122 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05301123 uint32_t muteWaitMs;
Lalit Kansaraf803ac52016-05-09 19:12:01 +05301124 muteWaitMs = setOutputDevice(outputDesc, device, force, 0, NULL, address);
Sharad Sangle36781612015-05-28 16:15:16 +05301125
1126 // handle special case for sonification while in call
1127 if (isInCall()) {
1128 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001129 }
Sharad Sangle36781612015-05-28 16:15:16 +05301130
1131 // apply volume rules for current stream and device if necessary
1132 checkAndSetVolume(stream,
Ashish Jain40cab3a2016-03-23 11:14:14 +05301133 mVolumeCurves->getVolumeIndex(stream, device),
Sharad Sangle36781612015-05-28 16:15:16 +05301134 outputDesc,
1135 device);
1136
1137 // update the outputs if starting an output with a stream that can affect notification
1138 // routing
1139 handleNotificationRoutingForStream(stream);
1140
1141 // force reevaluating accessibility routing when ringtone or alarm starts
1142 if (strategy == STRATEGY_SONIFICATION) {
1143 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1144 }
1145 }
1146 else {
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001147 // handle special case for sonification while in call
1148 if (isInCall()) {
1149 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Sharad Sangle36781612015-05-28 16:15:16 +05301150 }
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001151 }
Sharad Sangle36781612015-05-28 16:15:16 +05301152 return NO_ERROR;
1153}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001154
Sharad Sangle36781612015-05-28 16:15:16 +05301155void AudioPolicyManagerCustom::handleIncallSonification(audio_stream_type_t stream,
1156 bool starting, bool stateChange,
1157 audio_io_handle_t output)
1158{
1159 if(!hasPrimaryOutput()) {
1160 return;
1161 }
1162 // no action needed for AUDIO_STREAM_PATCH stream type, it's for internal flinger tracks
1163 if (stream == AUDIO_STREAM_PATCH) {
1164 return;
1165 }
1166 // if the stream pertains to sonification strategy and we are in call we must
1167 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
1168 // in the device used for phone strategy and play the tone if the selected device does not
1169 // interfere with the device used for phone strategy
1170 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
1171 // many times as there are active tracks on the output
1172 const routing_strategy stream_strategy = getStrategy(stream);
1173 if ((stream_strategy == STRATEGY_SONIFICATION) ||
1174 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
1175 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
1176 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
1177 stream, starting, outputDesc->mDevice, stateChange);
1178 if (outputDesc->mRefCount[stream]) {
1179 int muteCount = 1;
1180 if (stateChange) {
1181 muteCount = outputDesc->mRefCount[stream];
1182 }
1183 if (audio_is_low_visibility(stream)) {
1184 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
1185 for (int i = 0; i < muteCount; i++) {
1186 setStreamMute(stream, starting, outputDesc);
1187 }
1188 } else {
1189 ALOGV("handleIncallSonification() high visibility");
1190 if (outputDesc->device() &
1191 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
1192 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
1193 for (int i = 0; i < muteCount; i++) {
1194 setStreamMute(stream, starting, outputDesc);
1195 }
1196 }
1197 if (starting) {
1198 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
1199 AUDIO_STREAM_VOICE_CALL);
1200 } else {
1201 mpClientInterface->stopTone();
1202 }
1203 }
1204 }
1205 }
1206}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001207
Sharad Sangle36781612015-05-28 16:15:16 +05301208void AudioPolicyManagerCustom::handleNotificationRoutingForStream(audio_stream_type_t stream) {
1209 switch(stream) {
1210 case AUDIO_STREAM_MUSIC:
1211 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
1212 updateDevicesAndOutputs();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001213 break;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001214 default:
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001215 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001216 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001217}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001218
Sharad Sangle36781612015-05-28 16:15:16 +05301219status_t AudioPolicyManagerCustom::checkAndSetVolume(audio_stream_type_t stream,
1220 int index,
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301221 const sp<AudioOutputDescriptor>& outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301222 audio_devices_t device,
1223 int delayMs, bool force)
1224{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301225 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1226 ALOGW("checkAndSetVolume() invalid stream %d", stream);
1227 return INVALID_OPERATION;
1228 }
Sharad Sangle36781612015-05-28 16:15:16 +05301229 // do not change actual stream volume if the stream is muted
1230 if (outputDesc->mMuteCount[stream] != 0) {
1231 ALOGVV("checkAndSetVolume() stream %d muted count %d",
1232 stream, outputDesc->mMuteCount[stream]);
1233 return NO_ERROR;
1234 }
1235 audio_policy_forced_cfg_t forceUseForComm =
1236 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
1237 // do not change in call volume if bluetooth is connected and vice versa
1238 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
1239 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
1240 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
1241 stream, forceUseForComm);
1242 return INVALID_OPERATION;
1243 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001244
Sharad Sangle36781612015-05-28 16:15:16 +05301245 if (device == AUDIO_DEVICE_NONE) {
1246 device = outputDesc->device();
1247 }
1248
1249 float volumeDb = computeVolume(stream, index, device);
1250 if (outputDesc->isFixedVolume(device)) {
1251 volumeDb = 0.0f;
1252 }
1253
1254 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
1255
1256 if (stream == AUDIO_STREAM_VOICE_CALL ||
1257 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
1258 float voiceVolume;
1259 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
1260 if (stream == AUDIO_STREAM_VOICE_CALL) {
Ashish Jain40cab3a2016-03-23 11:14:14 +05301261 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Sharad Sangle36781612015-05-28 16:15:16 +05301262 } else {
1263 voiceVolume = 1.0;
1264 }
1265
1266 if (voiceVolume != mLastVoiceVolume && ((outputDesc == mPrimaryOutput) ||
1267 isDirectOutput(outputDesc->mIoHandle) || device & AUDIO_DEVICE_OUT_ALL_USB)) {
1268 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
1269 mLastVoiceVolume = voiceVolume;
1270 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301271#ifdef FM_POWER_OPT
1272 } else if (stream == AUDIO_STREAM_MUSIC && hasPrimaryOutput() &&
Haynes Mathew George603ae9b2015-12-21 17:23:59 -08001273 outputDesc == mPrimaryOutput && mFMIsActive) {
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001274 /* Avoid unnecessary set_parameter calls as it puts the primary
1275 outputs FastMixer in HOT_IDLE leading to breaks in audio */
1276 if (volumeDb != mPrevFMVolumeDb) {
1277 mPrevFMVolumeDb = volumeDb;
1278 AudioParameter param = AudioParameter();
1279 param.addFloat(String8("fm_volume"), Volume::DbToAmpl(volumeDb));
Mingming Yin1433cc62016-01-04 14:58:00 -08001280 //Double delayMs to avoid sound burst while device switch.
1281 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString(), delayMs*2);
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001282 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301283#endif /* FM_POWER_OPT end */
Sharad Sangle36781612015-05-28 16:15:16 +05301284 }
1285
1286 return NO_ERROR;
1287}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001288
Sharad Sangle36781612015-05-28 16:15:16 +05301289bool AudioPolicyManagerCustom::isDirectOutput(audio_io_handle_t output) {
1290 for (size_t i = 0; i < mOutputs.size(); i++) {
1291 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1292 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1293 if ((curOutput == output) && (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
1294 return true;
1295 }
1296 }
1297 return false;
1298}
vivek mehta0ea887a2015-08-26 14:01:20 -07001299
Ashish Jainac510a72016-04-28 12:22:58 +05301300bool static isDirectPCMEnabled(int bitWidth)
1301{
1302 bool directPCMEnabled = false;
1303 if (bitWidth == 24 || bitWidth == 32)
1304 directPCMEnabled =
1305 property_get_bool("audio.offload.pcm.24bit.enable", false);
1306 else
1307 directPCMEnabled =
1308 property_get_bool("audio.offload.pcm.16bit.enable", false);
1309
1310 return directPCMEnabled;
1311}
1312
vivek mehta0ea887a2015-08-26 14:01:20 -07001313status_t AudioPolicyManagerCustom::getOutputForAttr(const audio_attributes_t *attr,
1314 audio_io_handle_t *output,
1315 audio_session_t session,
1316 audio_stream_type_t *stream,
1317 uid_t uid,
1318 uint32_t samplingRate,
1319 audio_format_t format,
1320 audio_channel_mask_t channelMask,
1321 audio_output_flags_t flags,
1322 audio_port_handle_t selectedDeviceId,
1323 const audio_offload_info_t *offloadInfo)
1324{
1325 audio_offload_info_t tOffloadInfo = AUDIO_INFO_INITIALIZER;
1326
Alexy Joseph0ac09032015-10-16 15:57:53 -07001327 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
Ashish Jainac510a72016-04-28 12:22:58 +05301328 uint32_t bitWidth = (audio_bytes_per_sample(format) * 8);
Alexy Joseph0ac09032015-10-16 15:57:53 -07001329
1330 if (offloadDisabled) {
1331 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
1332 }
1333
Ashish Jainac510a72016-04-28 12:22:58 +05301334 if (!offloadDisabled && (offloadInfo == NULL) &&
1335 isDirectPCMEnabled(bitWidth) &&
1336 (flags == AUDIO_OUTPUT_FLAG_NONE)) {
vivek mehta0ea887a2015-08-26 14:01:20 -07001337
vivek mehta0ea887a2015-08-26 14:01:20 -07001338 tOffloadInfo.sample_rate = samplingRate;
1339 tOffloadInfo.channel_mask = channelMask;
1340 tOffloadInfo.format = format;
1341 tOffloadInfo.stream_type = *stream;
Ashish Jainac510a72016-04-28 12:22:58 +05301342 tOffloadInfo.bit_width = bitWidth;
vivek mehta0ea887a2015-08-26 14:01:20 -07001343 if (attr != NULL) {
1344 ALOGV("found attribute .. setting usage %d ", attr->usage);
1345 tOffloadInfo.usage = attr->usage;
1346 } else {
Alexy Joseph0ac09032015-10-16 15:57:53 -07001347 ALOGI("%s:: attribute is NULL .. no usage set", __func__);
vivek mehta0ea887a2015-08-26 14:01:20 -07001348 }
1349 offloadInfo = &tOffloadInfo;
1350 }
1351
1352 return AudioPolicyManager::getOutputForAttr(attr, output, session, stream,
1353 (uid_t)uid, (uint32_t)samplingRate,
1354 format, (audio_channel_mask_t)channelMask,
1355 flags, (audio_port_handle_t)selectedDeviceId,
1356 offloadInfo);
1357}
1358
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001359audio_io_handle_t AudioPolicyManagerCustom::getOutputForDevice(
1360 audio_devices_t device,
Sharad Sangle36781612015-05-28 16:15:16 +05301361 audio_session_t session __unused,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001362 audio_stream_type_t stream,
1363 uint32_t samplingRate,
1364 audio_format_t format,
1365 audio_channel_mask_t channelMask,
1366 audio_output_flags_t flags,
1367 const audio_offload_info_t *offloadInfo)
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001368{
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001369 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001370 status_t status;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001371
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001372#ifdef AUDIO_POLICY_TEST
1373 if (mCurOutput != 0) {
1374 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1375 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1376
1377 if (mTestOutputs[mCurOutput] == 0) {
1378 ALOGV("getOutput() opening test output");
Sharad Sangle36781612015-05-28 16:15:16 +05301379 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
1380 mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001381 outputDesc->mDevice = mTestDevice;
1382 outputDesc->mLatency = mTestLatencyMs;
1383 outputDesc->mFlags =
1384 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
1385 outputDesc->mRefCount[stream] = 0;
1386 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1387 config.sample_rate = mTestSamplingRate;
1388 config.channel_mask = mTestChannels;
1389 config.format = mTestFormat;
1390 if (offloadInfo != NULL) {
1391 config.offload_info = *offloadInfo;
1392 }
1393 status = mpClientInterface->openOutput(0,
1394 &mTestOutputs[mCurOutput],
1395 &config,
1396 &outputDesc->mDevice,
1397 String8(""),
1398 &outputDesc->mLatency,
1399 outputDesc->mFlags);
1400 if (status == NO_ERROR) {
1401 outputDesc->mSamplingRate = config.sample_rate;
1402 outputDesc->mFormat = config.format;
1403 outputDesc->mChannelMask = config.channel_mask;
1404 AudioParameter outputCmd = AudioParameter();
1405 outputCmd.addInt(String8("set_id"),mCurOutput);
1406 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1407 addOutput(mTestOutputs[mCurOutput], outputDesc);
1408 }
1409 }
1410 return mTestOutputs[mCurOutput];
1411 }
1412#endif //AUDIO_POLICY_TEST
Sharad Sangle36781612015-05-28 16:15:16 +05301413 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1414 (stream != AUDIO_STREAM_MUSIC)) {
1415 // compress should not be used for non-music streams
1416 ALOGE("Offloading only allowed with music stream");
1417 return 0;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301418 }
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301419
1420 if ((stream == AUDIO_STREAM_VOICE_CALL) &&
1421 (channelMask == 1) &&
Shiv Maliyappanahallid77d3b32016-02-02 13:45:50 -08001422 (samplingRate == 8000 || samplingRate == 16000 ||
1423 samplingRate == 32000 || samplingRate == 48000)) {
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301424 // Allow Voip direct output only if:
1425 // audio mode is MODE_IN_COMMUNCATION; AND
1426 // voip output is not opened already; AND
1427 // requested sample rate matches with that of voip input stream (if opened already)
1428 int value = 0;
1429 uint32_t mode = 0, voipOutCount = 1, voipSampleRate = 1;
1430 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1431 String8("audio_mode"));
1432 AudioParameter result = AudioParameter(valueStr);
1433 if (result.getInt(String8("audio_mode"), value) == NO_ERROR) {
1434 mode = value;
1435 }
1436
1437 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1438 String8("voip_out_stream_count"));
1439 result = AudioParameter(valueStr);
1440 if (result.getInt(String8("voip_out_stream_count"), value) == NO_ERROR) {
1441 voipOutCount = value;
1442 }
1443
1444 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1445 String8("voip_sample_rate"));
1446 result = AudioParameter(valueStr);
1447 if (result.getInt(String8("voip_sample_rate"), value) == NO_ERROR) {
1448 voipSampleRate = value;
1449 }
1450
1451 if ((mode == AUDIO_MODE_IN_COMMUNICATION) && (voipOutCount == 0) &&
1452 ((voipSampleRate == 0) || (voipSampleRate == samplingRate))) {
1453 if (audio_is_linear_pcm(format)) {
1454 char propValue[PROPERTY_VALUE_MAX] = {0};
1455 property_get("use.voice.path.for.pcm.voip", propValue, "0");
1456 bool voipPcmSysPropEnabled = !strncmp("true", propValue, sizeof("true"));
1457 if (voipPcmSysPropEnabled && (format == AUDIO_FORMAT_PCM_16_BIT)) {
Karthik Reddy Katta3fb9be72015-11-25 11:18:27 +05301458 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX |
1459 AUDIO_OUTPUT_FLAG_DIRECT);
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301460 ALOGD("Set VoIP and Direct output flags for PCM format");
1461 }
1462 }
1463 }
1464 }
1465
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301466#ifdef VOICE_CONCURRENCY
1467 char propValue[PROPERTY_VALUE_MAX];
1468 bool prop_play_enabled=false, prop_voip_enabled = false;
1469
1470 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1471 prop_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001472 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301473
1474 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1475 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1476 }
1477
1478 if (prop_play_enabled && mvoice_call_state) {
1479 //check if voice call is active / running in background
1480 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1481 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1482 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1483 {
1484 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1485 if(prop_voip_enabled) {
1486 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1487 flags );
1488 return 0;
1489 }
1490 }
1491 else {
1492 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
1493 ALOGD("voice_conc:IN call mode adding ULL flags .. flags: %x ", flags );
1494 flags = AUDIO_OUTPUT_FLAG_FAST;
1495 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
1496 if (AUDIO_STREAM_MUSIC == stream) {
1497 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1498 ALOGD("voice_conc:IN call mode adding deep-buffer flags %x ", flags );
1499 }
1500 else {
1501 flags = AUDIO_OUTPUT_FLAG_FAST;
1502 ALOGD("voice_conc:IN call mode adding fast flags %x ", flags );
1503 }
1504 }
1505 }
1506 }
1507 } else if (prop_voip_enabled && mvoice_call_state) {
1508 //check if voice call is active / running in background
1509 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1510 //return only ULL ouput
1511 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1512 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1513 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1514 {
1515 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1516 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1517 flags );
1518 return 0;
1519 }
1520 }
1521 }
1522#endif
1523#ifdef RECORD_PLAY_CONCURRENCY
1524 char recConcPropValue[PROPERTY_VALUE_MAX];
1525 bool prop_rec_play_enabled = false;
1526
1527 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
1528 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
1529 }
1530 if ((prop_rec_play_enabled) &&
1531 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
1532 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
1533 if (AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1534 // allow VoIP using voice path
1535 // Do nothing
1536 } else if((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1537 ALOGD("voice_conc:MODE_IN_COMM is setforcing deep buffer output for non ULL... flags: %x", flags);
1538 // use deep buffer path for all non ULL outputs
1539 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1540 }
1541 } else if ((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1542 ALOGD("voice_conc:Record mode is on forcing deep buffer output for non ULL... flags: %x ", flags);
1543 // use deep buffer path for all non ULL outputs
1544 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1545 }
1546 }
1547 if (prop_rec_play_enabled &&
1548 (stream == AUDIO_STREAM_ENFORCED_AUDIBLE)) {
1549 ALOGD("Record conc is on forcing ULL output for ENFORCED_AUDIBLE");
1550 flags = AUDIO_OUTPUT_FLAG_FAST;
1551 }
1552#endif
1553
Sharad Sangle4509cef2015-08-19 20:47:12 +05301554#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Sharad Sangle36781612015-05-28 16:15:16 +05301555 /*
1556 * WFD audio routes back to target speaker when starting a ringtone playback.
1557 * This is because primary output is reused for ringtone, so output device is
1558 * updated based on SONIFICATION strategy for both ringtone and music playback.
1559 * The same issue is not seen on remoted_submix HAL based WFD audio because
1560 * primary output is not reused and a new output is created for ringtone playback.
1561 * Issue is fixed by updating output flag to AUDIO_OUTPUT_FLAG_FAST when there is
1562 * a non-music stream playback on WFD, so primary output is not reused for ringtone.
1563 */
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001564 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
1565 if ((availableOutputDeviceTypes & AUDIO_DEVICE_OUT_PROXY)
1566 && (stream != AUDIO_STREAM_MUSIC)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301567 ALOGD("WFD audio: use OUTPUT_FLAG_FAST for non music stream. flags:%x", flags );
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001568 //For voip paths
1569 if(flags & AUDIO_OUTPUT_FLAG_DIRECT)
1570 flags = AUDIO_OUTPUT_FLAG_DIRECT;
1571 else //route every thing else to ULL path
1572 flags = AUDIO_OUTPUT_FLAG_FAST;
1573 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301574#endif
1575
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001576 // open a direct output if required by specified parameters
vivek mehta0ea887a2015-08-26 14:01:20 -07001577 // force direct flag if offload flag is set: offloading implies a direct output stream
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001578 // and all common behaviors are driven by checking only the direct flag
1579 // this should normally be set appropriately in the policy configuration file
1580 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1581 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1582 }
1583 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1584 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1585 }
vivek mehta0ea887a2015-08-26 14:01:20 -07001586
vivek mehta4b0d8192015-10-16 00:25:59 -07001587 bool forced_deep = false;
Sharad Sangle36781612015-05-28 16:15:16 +05301588 // only allow deep buffering for music stream type
1589 if (stream != AUDIO_STREAM_MUSIC) {
1590 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Sharad Sangle497aef82015-08-03 17:55:48 +05301591 } else if (/* stream == AUDIO_STREAM_MUSIC && */
1592 flags == AUDIO_OUTPUT_FLAG_NONE &&
1593 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
vivek mehtac984b992015-11-25 13:28:55 -08001594 forced_deep = true;
Sharad Sangle36781612015-05-28 16:15:16 +05301595 }
Sharad Sangle497aef82015-08-03 17:55:48 +05301596
Sharad Sangle36781612015-05-28 16:15:16 +05301597 if (stream == AUDIO_STREAM_TTS) {
1598 flags = AUDIO_OUTPUT_FLAG_TTS;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001599 }
1600
vivek mehta4b0d8192015-10-16 00:25:59 -07001601 // Do offload magic here
vivek mehtac984b992015-11-25 13:28:55 -08001602 if ((flags == AUDIO_OUTPUT_FLAG_NONE) &&
1603 (stream == AUDIO_STREAM_MUSIC) &&
1604 (offloadInfo != NULL) &&
vivek mehta4b0d8192015-10-16 00:25:59 -07001605 ((offloadInfo->usage == AUDIO_USAGE_MEDIA) || (offloadInfo->usage == AUDIO_USAGE_GAME))) {
vivek mehtac984b992015-11-25 13:28:55 -08001606 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DIRECT_PCM);
vivek mehta4b0d8192015-10-16 00:25:59 -07001607 ALOGD("AudioCustomHAL --> Force Direct Flag .. flag (0x%x)", flags);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301608 }
1609
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001610 sp<IOProfile> profile;
1611
1612 // skip direct output selection if the request can obviously be attached to a mixed output
1613 // and not explicitly requested
vivek mehtac984b992015-11-25 13:28:55 -08001614 if (((flags & (AUDIO_OUTPUT_FLAG_DIRECT|AUDIO_OUTPUT_FLAG_DIRECT_PCM)) == 0) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301615 audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX &&
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001616 audio_channel_count_from_out_mask(channelMask) <= 2) {
1617 goto non_direct_output;
1618 }
1619
1620 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1621 // creating an offloaded track and tearing it down immediately after start when audioflinger
1622 // detects there is an active non offloadable effect.
1623 // FIXME: We should check the audio session here but we do not have it in this context.
1624 // This may prevent offloading in rare situations where effects are left active by apps
1625 // in the background.
1626
Sharad Sangle36781612015-05-28 16:15:16 +05301627 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
Ashish Jain40cab3a2016-03-23 11:14:14 +05301628 !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001629 profile = getProfileForDirectOutput(device,
1630 samplingRate,
1631 format,
1632 channelMask,
1633 (audio_output_flags_t)flags);
1634 }
1635
1636 if (profile != 0) {
vivek mehtac984b992015-11-25 13:28:55 -08001637
1638 if (!(flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301639 (profile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT_PCM)) {
vivek mehtac984b992015-11-25 13:28:55 -08001640 ALOGI("got Direct_PCM without requesting ... reject ");
1641 profile = NULL;
1642 goto non_direct_output;
1643 }
1644
Sharad Sangle36781612015-05-28 16:15:16 +05301645 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001646
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001647 // if multiple concurrent offload decode is supported
1648 // do no check for reuse and also don't close previous output if its offload
1649 // previous output will be closed during track destruction
1650 if (!(property_get_bool("audio.offload.multiple.enabled", false) &&
1651 ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0))) {
1652 for (size_t i = 0; i < mOutputs.size(); i++) {
1653 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1654 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1655 outputDesc = desc;
1656 // reuse direct output if currently open and configured with same parameters
1657 if ((samplingRate == outputDesc->mSamplingRate) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301658 audio_formats_match(format, outputDesc->mFormat) &&
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001659 (channelMask == outputDesc->mChannelMask)) {
1660 outputDesc->mDirectOpenCount++;
1661 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1662 return mOutputs.keyAt(i);
1663 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001664 }
1665 }
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001666 // close direct output if currently open and configured with different parameters
1667 if (outputDesc != NULL) {
1668 closeOutput(outputDesc->mIoHandle);
1669 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001670 }
Sharad Sangle36781612015-05-28 16:15:16 +05301671
1672 // if the selected profile is offloaded and no offload info was specified,
1673 // create a default one
1674 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
Ashish Jain40cab3a2016-03-23 11:14:14 +05301675 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
Sharad Sangle36781612015-05-28 16:15:16 +05301676 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1677 defaultOffloadInfo.sample_rate = samplingRate;
1678 defaultOffloadInfo.channel_mask = channelMask;
1679 defaultOffloadInfo.format = format;
1680 defaultOffloadInfo.stream_type = stream;
1681 defaultOffloadInfo.bit_rate = 0;
1682 defaultOffloadInfo.duration_us = -1;
1683 defaultOffloadInfo.has_video = true; // conservative
1684 defaultOffloadInfo.is_streaming = true; // likely
1685 offloadInfo = &defaultOffloadInfo;
1686 }
1687
1688 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001689 outputDesc->mDevice = device;
1690 outputDesc->mLatency = 0;
Sharad Sangle36781612015-05-28 16:15:16 +05301691 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001692 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1693 config.sample_rate = samplingRate;
1694 config.channel_mask = channelMask;
1695 config.format = format;
1696 if (offloadInfo != NULL) {
1697 config.offload_info = *offloadInfo;
1698 }
Sharad Sangle36781612015-05-28 16:15:16 +05301699 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001700 &output,
1701 &config,
1702 &outputDesc->mDevice,
1703 String8(""),
1704 &outputDesc->mLatency,
1705 outputDesc->mFlags);
1706
1707 // only accept an output with the requested parameters
1708 if (status != NO_ERROR ||
1709 (samplingRate != 0 && samplingRate != config.sample_rate) ||
Ashish Jain40cab3a2016-03-23 11:14:14 +05301710 (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) ||
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001711 (channelMask != 0 && channelMask != config.channel_mask)) {
1712 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1713 "format %d %d, channelMask %04x %04x", output, samplingRate,
1714 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1715 outputDesc->mChannelMask);
1716 if (output != AUDIO_IO_HANDLE_NONE) {
1717 mpClientInterface->closeOutput(output);
1718 }
Sharad Sangle36781612015-05-28 16:15:16 +05301719 // fall back to mixer output if possible when the direct output could not be open
Ashish Jain40cab3a2016-03-23 11:14:14 +05301720 if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) {
Sharad Sangle36781612015-05-28 16:15:16 +05301721 goto non_direct_output;
1722 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001723 return AUDIO_IO_HANDLE_NONE;
1724 }
1725 outputDesc->mSamplingRate = config.sample_rate;
1726 outputDesc->mChannelMask = config.channel_mask;
1727 outputDesc->mFormat = config.format;
1728 outputDesc->mRefCount[stream] = 0;
1729 outputDesc->mStopTime[stream] = 0;
1730 outputDesc->mDirectOpenCount = 1;
1731
1732 audio_io_handle_t srcOutput = getOutputForEffect();
1733 addOutput(output, outputDesc);
1734 audio_io_handle_t dstOutput = getOutputForEffect();
1735 if (dstOutput == output) {
Gao Jiedb057832015-11-12 08:51:22 +08001736#ifdef DOLBY_ENABLE
1737 status_t status = mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1738 if (status == NO_ERROR) {
1739 for (size_t i = 0; i < mEffects.size(); i++) {
1740 sp<EffectDescriptor> desc = mEffects.valueAt(i);
1741 if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX) {
1742 // update the mIo member of EffectDescriptor for the global effect
1743 ALOGV("%s updating mIo", __FUNCTION__);
1744 desc->mIo = dstOutput;
1745 }
1746 }
1747 } else {
1748 ALOGW("%s moveEffects from %d to %d failed", __FUNCTION__, srcOutput, dstOutput);
1749 }
1750#else // DOLBY_END
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001751 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
Gao Jiedb057832015-11-12 08:51:22 +08001752#endif // LINE_ADDED_BY_DOLBY
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001753 }
1754 mPreviousOutputs = mOutputs;
1755 ALOGV("getOutput() returns new direct output %d", output);
1756 mpClientInterface->onAudioPortListUpdate();
1757 return output;
1758 }
1759
1760non_direct_output:
Ashish Jain5fe12412016-04-18 18:44:17 +05301761
1762 // A request for HW A/V sync cannot fallback to a mixed output because time
1763 // stamps are embedded in audio data
1764 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1765 return AUDIO_IO_HANDLE_NONE;
1766 }
1767
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001768 // ignoring channel mask due to downmix capability in mixer
1769
1770 // open a non direct output
1771
1772 // for non direct outputs, only PCM is supported
1773 if (audio_is_linear_pcm(format)) {
vivek mehtac984b992015-11-25 13:28:55 -08001774 if (forced_deep) {
1775 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
1776 ALOGI("setting force DEEP buffer now ");
1777 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001778 // get which output is suitable for the specified stream. The actual
1779 // routing change will happen when startOutput() will be called
1780 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1781
1782 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1783 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1784 output = selectOutput(outputs, flags, format);
1785 }
1786 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1787 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1788
vivek mehta0ea887a2015-08-26 14:01:20 -07001789 ALOGV("getOutputForDevice() returns output %d", output);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001790
1791 return output;
1792}
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301793
1794status_t AudioPolicyManagerCustom::getInputForAttr(const audio_attributes_t *attr,
1795 audio_io_handle_t *input,
1796 audio_session_t session,
1797 uid_t uid,
1798 uint32_t samplingRate,
1799 audio_format_t format,
1800 audio_channel_mask_t channelMask,
1801 audio_input_flags_t flags,
1802 audio_port_handle_t selectedDeviceId,
1803 input_type_t *inputType)
1804{
Ashish Jain40cab3a2016-03-23 11:14:14 +05301805 audio_source_t inputSource;
1806 inputSource = attr->source;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301807#ifdef VOICE_CONCURRENCY
1808
1809 char propValue[PROPERTY_VALUE_MAX];
1810 bool prop_rec_enabled=false, prop_voip_enabled = false;
1811
1812 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
1813 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1814 }
1815
1816 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1817 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1818 }
1819
1820 if (prop_rec_enabled && mvoice_call_state) {
1821 //check if voice call is active / running in background
1822 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1823 //Need to block input request
1824 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1825 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1826 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1827 {
1828 switch(inputSource) {
1829 case AUDIO_SOURCE_VOICE_UPLINK:
1830 case AUDIO_SOURCE_VOICE_DOWNLINK:
1831 case AUDIO_SOURCE_VOICE_CALL:
1832 ALOGD("voice_conc:Creating input during incall mode for inputSource: %d",
1833 inputSource);
1834 break;
1835
1836 case AUDIO_SOURCE_VOICE_COMMUNICATION:
1837 if(prop_voip_enabled) {
1838 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1839 inputSource);
1840 return NO_INIT;
1841 }
1842 break;
1843 default:
1844 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1845 inputSource);
1846 return NO_INIT;
1847 }
1848 }
1849 }//check for VoIP flag
1850 else if(prop_voip_enabled && mvoice_call_state) {
1851 //check if voice call is active / running in background
1852 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1853 //Need to block input request
1854 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1855 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1856 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1857 {
1858 if(inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1859 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
1860 return NO_INIT;
1861 }
1862 }
1863 }
1864
1865#endif
1866
1867 return AudioPolicyManager::getInputForAttr(attr,
1868 input,
1869 session,
1870 uid,
1871 samplingRate,
1872 format,
1873 channelMask,
1874 flags,
1875 selectedDeviceId,
1876 inputType);
1877}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001878
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301879status_t AudioPolicyManagerCustom::startInput(audio_io_handle_t input,
1880 audio_session_t session)
1881{
1882 ALOGV("startInput() input %d", input);
1883 ssize_t index = mInputs.indexOfKey(input);
1884 if (index < 0) {
1885 ALOGW("startInput() unknown input %d", input);
1886 return BAD_VALUE;
1887 }
1888 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1889
Ashish Jain40cab3a2016-03-23 11:14:14 +05301890 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1891 if (audioSession == 0) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301892 ALOGW("startInput() unknown session %d on input %d", session, input);
1893 return BAD_VALUE;
1894 }
1895
1896 // virtual input devices are compatible with other input devices
1897 if (!is_virtual_input_device(inputDesc->mDevice)) {
1898
1899 // for a non-virtual input device, check if there is another (non-virtual) active input
1900 audio_io_handle_t activeInput = mInputs.getActiveInput();
1901 if (activeInput != 0 && activeInput != input) {
1902
1903 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1904 // otherwise the active input continues and the new input cannot be started.
1905 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Ashish Jain40cab3a2016-03-23 11:14:14 +05301906 if ((activeDesc->inputSource() == AUDIO_SOURCE_HOTWORD) &&
1907 !activeDesc->hasPreemptedSession(session)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301908 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
Ashish Jain40cab3a2016-03-23 11:14:14 +05301909 //FIXME: consider all active sessions
1910 AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions();
1911 audio_session_t activeSession = activeSessions.keyAt(0);
1912 SortedVector<audio_session_t> sessions =
1913 activeDesc->getPreemptedSessions();
1914 sessions.add(activeSession);
1915 inputDesc->setPreemptedSessions(sessions);
1916 stopInput(activeInput, activeSession);
1917 releaseInput(activeInput, activeSession);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301918 } else {
1919 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
1920 return INVALID_OPERATION;
1921 }
1922 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05301923 // Do not allow capture if an active voice call is using a software patch and
1924 // the call TX source device is on the same HW module.
1925 // FIXME: would be better to refine to only inputs whose profile connects to the
1926 // call TX device but this information is not in the audio patch
1927 if (mCallTxPatch != 0 &&
1928 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1929 return INVALID_OPERATION;
1930 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301931 }
1932
1933 // Routing?
1934 mInputRoutes.incRouteActivity(session);
1935#ifdef RECORD_PLAY_CONCURRENCY
1936 mIsInputRequestOnProgress = true;
1937
1938 char getPropValue[PROPERTY_VALUE_MAX];
1939 bool prop_rec_play_enabled = false;
1940
1941 if (property_get("rec.playback.conc.disabled", getPropValue, NULL)) {
1942 prop_rec_play_enabled = atoi(getPropValue) || !strncmp("true", getPropValue, 4);
1943 }
1944
1945 if ((prop_rec_play_enabled) &&(mInputs.activeInputsCount() == 0)){
1946 // send update to HAL on record playback concurrency
1947 AudioParameter param = AudioParameter();
1948 param.add(String8("rec_play_conc_on"), String8("true"));
1949 ALOGD("startInput() setParameters rec_play_conc is setting to ON ");
1950 mpClientInterface->setParameters(0, param.toString());
1951
1952 // Call invalidate to reset all opened non ULL audio tracks
1953 // Move tracks associated to this strategy from previous output to new output
1954 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
1955 // Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder)
Sharad Sangle4509cef2015-08-19 20:47:12 +05301956 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE && (i != AUDIO_STREAM_PATCH))) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301957 ALOGD("Invalidate on releaseInput for stream :: %d ", i);
1958 //FIXME see fixme on name change
1959 mpClientInterface->invalidateStream((audio_stream_type_t)i);
1960 }
1961 }
1962 // close compress tracks
1963 for (size_t i = 0; i < mOutputs.size(); i++) {
1964 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
1965 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
1966 ALOGD("ouput desc / profile is NULL");
1967 continue;
1968 }
1969 if (outputDesc->mProfile->mFlags
1970 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1971 // close compress sessions
1972 ALOGD("calling closeOutput on record conc for COMPRESS output");
1973 closeOutput(mOutputs.keyAt(i));
1974 }
1975 }
1976 }
1977#endif
1978
Ashish Jain40cab3a2016-03-23 11:14:14 +05301979 if (!inputDesc->isActive() || mInputRoutes.hasRouteChanged(session)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301980 // if input maps to a dynamic policy with an activity listener, notify of state change
Ashish Jain630c5e12016-04-18 11:50:21 +05301981 if ((inputDesc->mPolicyMix != NULL)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301982 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Ashish Jain5fe12412016-04-18 18:44:17 +05301983 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301984 MIX_STATE_MIXING);
Ashish Jain630c5e12016-04-18 11:50:21 +05301985 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301986
1987 if (mInputs.activeInputsCount() == 0) {
1988 SoundTrigger::setCaptureState(true);
1989 }
1990 setInputDevice(input, getNewInputDevice(input), true /* force */);
1991
1992 // automatically enable the remote submix output when input is started if not
1993 // used by a policy mix of type MIX_TYPE_RECORDERS
1994 // For remote submix (a virtual device), we open only one input per capture request.
1995 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1996 String8 address = String8("");
1997 if (inputDesc->mPolicyMix == NULL) {
1998 address = String8("0");
Ashish Jain630c5e12016-04-18 11:50:21 +05301999 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
Ashish Jain5fe12412016-04-18 18:44:17 +05302000 address = inputDesc->mPolicyMix->mDeviceAddress;
Ashish Jain630c5e12016-04-18 11:50:21 +05302001 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302002 if (address != "") {
2003 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2004 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2005 address, "remote-submix");
2006 }
2007 }
2008 }
2009
Ashish Jain40cab3a2016-03-23 11:14:14 +05302010 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302011
Ashish Jain40cab3a2016-03-23 11:14:14 +05302012 audioSession->changeActiveCount(1);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302013#ifdef RECORD_PLAY_CONCURRENCY
2014 mIsInputRequestOnProgress = false;
2015#endif
2016 return NO_ERROR;
2017}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08002018
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302019status_t AudioPolicyManagerCustom::stopInput(audio_io_handle_t input,
2020 audio_session_t session)
2021{
2022 status_t status;
2023 status = AudioPolicyManager::stopInput(input, session);
2024#ifdef RECORD_PLAY_CONCURRENCY
2025 char propValue[PROPERTY_VALUE_MAX];
2026 bool prop_rec_play_enabled = false;
2027
2028 if (property_get("rec.playback.conc.disabled", propValue, NULL)) {
2029 prop_rec_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
2030 }
2031
2032 if ((prop_rec_play_enabled) && (mInputs.activeInputsCount() == 0)) {
2033
2034 //send update to HAL on record playback concurrency
2035 AudioParameter param = AudioParameter();
2036 param.add(String8("rec_play_conc_on"), String8("false"));
2037 ALOGD("stopInput() setParameters rec_play_conc is setting to OFF ");
2038 mpClientInterface->setParameters(0, param.toString());
2039
2040 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
2041 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
2042 //Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder stop tone)
2043 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE) && (i != AUDIO_STREAM_PATCH)) {
2044 ALOGD(" Invalidate on stopInput for stream :: %d ", i);
2045 //FIXME see fixme on name change
2046 mpClientInterface->invalidateStream((audio_stream_type_t)i);
2047 }
2048 }
2049 }
2050#endif
2051 return status;
2052}
2053
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302054void AudioPolicyManagerCustom::closeAllInputs() {
2055 bool patchRemoved = false;
2056
Dhanalakshmi Siddanibfeca7f2015-10-08 15:17:11 +05302057 for(size_t input_index = mInputs.size(); input_index > 0; input_index--) {
2058 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index-1);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302059 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302060 if (patch_index >= 0) {
2061 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302062 status_t status;
2063 status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302064 mAudioPatches.removeItemsAt(patch_index);
2065 patchRemoved = true;
2066 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05302067 if ((inputDesc->getOpenRefCount() > 0) && inputDesc->isSoundTrigger()
2068 && (mInputs.size() == 1)) {
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302069 ALOGD("Do not close sound trigger input handle");
2070 } else {
Dhanalakshmi Siddanibfeca7f2015-10-08 15:17:11 +05302071 mpClientInterface->closeInput(mInputs.keyAt(input_index-1));
2072 mInputs.removeItem(mInputs.keyAt(input_index-1));
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302073 }
2074 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05302075 mInputs.clear();
2076 SoundTrigger::setCaptureState(false);
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302077 nextAudioPortGeneration();
2078
2079 if (patchRemoved) {
2080 mpClientInterface->onAudioPatchListUpdate();
2081 }
2082}
2083
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302084AudioPolicyManagerCustom::AudioPolicyManagerCustom(AudioPolicyClientInterface *clientInterface)
Sharad Sangle4509cef2015-08-19 20:47:12 +05302085 : AudioPolicyManager(clientInterface),
2086 mHdmiAudioDisabled(false),
2087 mHdmiAudioEvent(false),
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08002088 mPrevPhoneState(0),
Haynes Mathew George603ae9b2015-12-21 17:23:59 -08002089 mPrevFMVolumeDb(0.0f),
2090 mFMIsActive(false)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302091{
Ashish Jain40cab3a2016-03-23 11:14:14 +05302092
2093 //TODO: Check the new logic to parse policy conf and update the below code
2094 // Need this when SSR encoding is enabled
Mingming Yin38ea08c2015-10-05 15:24:04 -07002095 char ssr_enabled[PROPERTY_VALUE_MAX] = {0};
2096 bool prop_ssr_enabled = false;
2097
2098 if (property_get("ro.qc.sdk.audio.ssr", ssr_enabled, NULL)) {
2099 prop_ssr_enabled = atoi(ssr_enabled) || !strncmp("true", ssr_enabled, 4);
2100 }
2101
2102 for (size_t i = 0; i < mHwModules.size(); i++) {
Ashish Jain40cab3a2016-03-23 11:14:14 +05302103 ALOGV("Hw module %zu", i);
Mingming Yin38ea08c2015-10-05 15:24:04 -07002104 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) {
2105 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Ashish Jain40cab3a2016-03-23 11:14:14 +05302106 AudioProfileVector profiles = inProfile->getAudioProfiles();
2107 for (size_t k = 0; k < profiles.size(); k++){
2108 ChannelsVector channels = profiles[k]->getChannels();
2109 for (size_t x = 0; x < channels.size(); x++) {
2110 audio_channel_mask_t channelMask = channels[x];
2111 ALOGV("Channel Mask %x size %zu", channelMask,
2112 channels.size());
2113 if (AUDIO_CHANNEL_IN_5POINT1 == channelMask) {
2114 if (!prop_ssr_enabled) {
2115 ALOGI("removing AUDIO_CHANNEL_IN_5POINT1 from"
2116 " input profile as SSR(surround sound record)"
2117 " is not supported on this chipset variant");
2118 channels.removeItemsAt(x, 1);
2119 ALOGV("Channel Mask size now %zu",
2120 channels.size());
2121 }
Mingming Yin38ea08c2015-10-05 15:24:04 -07002122 }
2123 }
2124 }
2125 }
2126 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302127#ifdef RECORD_PLAY_CONCURRENCY
2128 mIsInputRequestOnProgress = false;
2129#endif
2130
2131
2132#ifdef VOICE_CONCURRENCY
2133 mFallBackflag = getFallBackPath();
2134#endif
2135}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07002136}