blob: 37cceabbf19151b87c22ce60c507b75e24db1c66 [file] [log] [blame]
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001/*
Sharad Sangle36781612015-05-28 16:15:16 +05302 * Copyright (c) 2013-2015, 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{
119 ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
120 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();
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800294 float volumeDb = mPrimaryOutput->mCurVolume[AUDIO_STREAM_MUSIC];
295 mPrevFMVolumeDb = volumeDb;
296 param.addFloat(String8("fm_volume"), Volume::DbToAmpl(volumeDb));
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530297 param.addInt(String8("handle_fm"), (int)newDevice);
298 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString());
299 }
300#endif /* FM_POWER_OPT end */
301
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700302 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530303 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
304 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
305 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700306 // do not force device change on duplicated output because if device is 0, it will
307 // also force a device 0 for the two outputs it is duplicated to which may override
308 // a valid device selection on those outputs.
Sharad Sangle36781612015-05-28 16:15:16 +0530309 bool force = !desc->isDuplicated()
310 && (!device_distinguishes_on_address(device)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700311 // always force when disconnecting (a non-duplicated device)
312 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Sharad Sangle36781612015-05-28 16:15:16 +0530313 setOutputDevice(desc, newDevice, force, 0);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700314 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700315 }
316
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700317 mpClientInterface->onAudioPortListUpdate();
318 return NO_ERROR;
319 } // end if is output device
320
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700321 // handle input devices
322 if (audio_is_input_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700323 SortedVector <audio_io_handle_t> inputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700324
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700325 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700326 switch (state)
327 {
328 // handle input device connection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700329 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
330 if (index >= 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700331 ALOGW("setDeviceConnectionState() device already connected: %d", device);
332 return INVALID_OPERATION;
333 }
Sharad Sangle36781612015-05-28 16:15:16 +0530334 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700335 if (module == NULL) {
336 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
337 device);
338 return INVALID_OPERATION;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700339 }
Sharad Sangle36781612015-05-28 16:15:16 +0530340 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700341 return INVALID_OPERATION;
342 }
343
344 index = mAvailableInputDevices.add(devDesc);
345 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530346 mAvailableInputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700347 } else {
348 return NO_MEMORY;
349 }
Sharad Sangle36781612015-05-28 16:15:16 +0530350
351 // Set connect to HALs
352 AudioParameter param = AudioParameter(devDesc->mAddress);
353 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
354 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
355
356 // Propagate device availability to Engine
357 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700358 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700359
360 // handle input device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700361 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
362 if (index < 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700363 ALOGW("setDeviceConnectionState() device not connected: %d", device);
364 return INVALID_OPERATION;
365 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700366
367 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
368
369 // Set Disconnect to HALs
Sharad Sangle36781612015-05-28 16:15:16 +0530370 AudioParameter param = AudioParameter(devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700371 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
372 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
373
Sharad Sangle36781612015-05-28 16:15:16 +0530374 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700375 mAvailableInputDevices.remove(devDesc);
376
Sharad Sangle36781612015-05-28 16:15:16 +0530377 // Propagate device availability to Engine
378 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700379 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700380
381 default:
382 ALOGE("setDeviceConnectionState() invalid state: %x", state);
383 return BAD_VALUE;
384 }
385
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700386 closeAllInputs();
387
Sharad Sangle36781612015-05-28 16:15:16 +0530388 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700389 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
390 updateCallRouting(newDevice);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700391 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700392
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700393 mpClientInterface->onAudioPortListUpdate();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700394 return NO_ERROR;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700395 } // end if is input device
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700396
397 ALOGW("setDeviceConnectionState() invalid device: %x", device);
398 return BAD_VALUE;
399}
Sharad Sangle36781612015-05-28 16:15:16 +0530400// This function checks for the parameters which can be offloaded.
401// This can be enhanced depending on the capability of the DSP and policy
402// of the system.
403bool AudioPolicyManagerCustom::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700404{
Sharad Sangle36781612015-05-28 16:15:16 +0530405 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
406 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
407 offloadInfo.sample_rate, offloadInfo.channel_mask,
408 offloadInfo.format,
409 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
410 offloadInfo.has_video);
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530411#ifdef VOICE_CONCURRENCY
412 char concpropValue[PROPERTY_VALUE_MAX];
413 if (property_get("voice.playback.conc.disabled", concpropValue, NULL)) {
414 bool propenabled = atoi(concpropValue) || !strncmp("true", concpropValue, 4);
415 if (propenabled) {
416 if (isInCall())
417 {
418 ALOGD("\n copl: blocking compress offload on call mode\n");
419 return false;
420 }
421 }
422 }
423#endif
424#ifdef RECORD_PLAY_CONCURRENCY
425 char recConcPropValue[PROPERTY_VALUE_MAX];
426 bool prop_rec_play_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700427
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530428 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
429 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
430 }
431
432 if ((prop_rec_play_enabled) &&
433 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
434 ALOGD("copl: blocking compress offload for record concurrency");
435 return false;
436 }
437#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530438 // Check if stream type is music, then only allow offload as of now.
439 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
440 {
441 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
442 return false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700443 }
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530444
Alexy Joseph0ac09032015-10-16 15:57:53 -0700445 // Check if offload has been disabled
446 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
447 if (offloadDisabled) {
448 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
449 return false;
450 }
451
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530452 char propValue[PROPERTY_VALUE_MAX];
453 bool pcmOffload = false;
454#ifdef PCM_OFFLOAD_ENABLED
455 if ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM_OFFLOAD) {
456 bool prop_enabled = false;
457 if ((AUDIO_FORMAT_PCM_16_BIT_OFFLOAD == offloadInfo.format) &&
458 property_get("audio.offload.pcm.16bit.enable", propValue, NULL)) {
459 prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
460 }
461
462#ifdef PCM_OFFLOAD_ENABLED_24
463 if ((AUDIO_FORMAT_PCM_24_BIT_OFFLOAD == offloadInfo.format) &&
464 property_get("audio.offload.pcm.24bit.enable", propValue, NULL)) {
465 prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530466 }
467#endif
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530468
469 if (prop_enabled) {
470 ALOGI("PCM offload property is enabled");
471 pcmOffload = true;
472 }
473
474 if (!pcmOffload) {
475 ALOGD("system property not enabled for PCM offload format[%x]",offloadInfo.format);
476 return false;
477 }
478 }
479#endif
480 if (!pcmOffload) {
Alexy Joseph0ac09032015-10-16 15:57:53 -0700481
482 bool compressedOffloadDisabled = property_get_bool("audio.offload.compress.disable", false);
483 if (compressedOffloadDisabled) {
484 ALOGI("compressed offload disabled by audio.offload.compress.disable=%d", compressedOffloadDisabled);
485 return false;
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530486 }
Alexy Joseph0ac09032015-10-16 15:57:53 -0700487
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530488 //check if it's multi-channel AAC (includes sub formats) and FLAC format
489 if ((popcount(offloadInfo.channel_mask) > 2) &&
490 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
491 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS))) {
492 ALOGD("offload disabled for multi-channel AAC,FLAC and VORBIS format");
493 return false;
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530494 }
495
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530496#ifdef AUDIO_EXTN_FORMATS_ENABLED
497 //check if it's multi-channel FLAC/ALAC/WMA format with sample rate > 48k
498 if ((popcount(offloadInfo.channel_mask) > 2) &&
499 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
Manish Dewangana6fc5442015-08-24 20:30:31 +0530500 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) && (offloadInfo.sample_rate > 48000)) ||
501 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) && (offloadInfo.sample_rate > 48000)) ||
502 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) && (offloadInfo.sample_rate > 48000)) ||
503 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS))) {
504 ALOGD("offload disabled for multi-channel FLAC/ALAC/WMA/AAC_ADTS clips with sample rate > 48kHz");
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530505 return false;
506 }
507#endif
508 //TODO: enable audio offloading with video when ready
509 const bool allowOffloadWithVideo =
510 property_get_bool("audio.offload.video", false /* default_value */);
511 if (offloadInfo.has_video && !allowOffloadWithVideo) {
512 ALOGV("isOffloadSupported: has_video == true, returning false");
513 return false;
514 }
Manish Dewanganf3cd0f82015-10-13 14:04:36 +0530515
516 const bool allowOffloadStreamingWithVideo = property_get_bool("av.streaming.offload.enable",
517 false /*default value*/);
518 if(offloadInfo.has_video && offloadInfo.is_streaming && !allowOffloadStreamingWithVideo) {
519 ALOGW("offload disabled by av.streaming.offload.enable = %s ", propValue );
520 return false;
521 }
522
Sharad Sangle36781612015-05-28 16:15:16 +0530523 }
524
525 //If duration is less than minimum value defined in property, return false
526 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
527 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
528 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
529 return false;
530 }
531 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
532 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
533 //duration checks only valid for MP3/AAC/ formats,
534 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
535 if ((offloadInfo.format == AUDIO_FORMAT_MP3) ||
536 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530537 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS) ||
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530538#ifdef AUDIO_EXTN_FORMATS_ENABLED
Sharad Sangle36781612015-05-28 16:15:16 +0530539 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
Sharad Sangle36781612015-05-28 16:15:16 +0530540 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) ||
541 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) ||
542 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) ||
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530543 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_APE) ||
Manish Dewangana6fc5442015-08-24 20:30:31 +0530544 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS) ||
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530545#endif
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530546 pcmOffload)
Sharad Sangle36781612015-05-28 16:15:16 +0530547 return false;
548
549 }
550
551 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
552 // creating an offloaded track and tearing it down immediately after start when audioflinger
553 // detects there is an active non offloadable effect.
554 // FIXME: We should check the audio session here but we do not have it in this context.
555 // This may prevent offloading in rare situations where effects are left active by apps
556 // in the background.
557 if (mEffects.isNonOffloadableEffectEnabled()) {
558 return false;
559 }
560 // Check for soundcard status
561 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
562 String8("SND_CARD_STATUS"));
563 AudioParameter result = AudioParameter(valueStr);
564 int isonline = 0;
565 if ((result.getInt(String8("SND_CARD_STATUS"), isonline) == NO_ERROR)
566 && !isonline) {
567 ALOGD("copl: soundcard is offline rejecting offload request");
568 return false;
569 }
570 // See if there is a profile to support this.
571 // AUDIO_DEVICE_NONE
572 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
573 offloadInfo.sample_rate,
574 offloadInfo.format,
575 offloadInfo.channel_mask,
576 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
577 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
578 return (profile != 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700579}
Sharad Sangle36781612015-05-28 16:15:16 +0530580audio_devices_t AudioPolicyManagerCustom::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
581 bool fromCache)
582{
583 audio_devices_t device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700584
Sharad Sangle36781612015-05-28 16:15:16 +0530585 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
586 if (index >= 0) {
587 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
588 if (patchDesc->mUid != mUidCached) {
589 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
590 outputDesc->device(), outputDesc->mPatchHandle);
591 return outputDesc->device();
592 }
593 }
594
595 // check the following by order of priority to request a routing change if necessary:
596 // 1: the strategy enforced audible is active and enforced on the output:
597 // use device for strategy enforced audible
598 // 2: we are in call or the strategy phone is active on the output:
599 // use device for strategy phone
600 // 3: the strategy for enforced audible is active but not enforced on the output:
601 // use the device for strategy enforced audible
602 // 4: the strategy sonification is active on the output:
603 // use device for strategy sonification
604 // 5: the strategy "respectful" sonification is active on the output:
605 // use device for strategy "respectful" sonification
606 // 6: the strategy accessibility is active on the output:
607 // use device for strategy accessibility
608 // 7: the strategy media is active on the output:
609 // use device for strategy media
610 // 8: the strategy DTMF is active on the output:
611 // use device for strategy DTMF
612 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
613 // use device for strategy t-t-s
614 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
615 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
616 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
617 } else if (isInCall() ||
618 isStrategyActive(outputDesc, STRATEGY_PHONE)||
619 isStrategyActive(mPrimaryOutput, STRATEGY_PHONE)) {
620 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
621 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
622 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
623 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)||
624 (isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION)
625 && (!isStrategyActive(mPrimaryOutput,STRATEGY_MEDIA)))) {
626 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530627 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL) ||
628 isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION_RESPECTFUL)) {
Sharad Sangle36781612015-05-28 16:15:16 +0530629 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
630 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
631 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
632 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
633 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
634 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
635 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
636 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
637 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
638 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
639 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
640 }
641
642 ALOGV("getNewOutputDevice() selected device %x", device);
643 return device;
644}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700645void AudioPolicyManagerCustom::setPhoneState(audio_mode_t state)
646{
Sharad Sangle36781612015-05-28 16:15:16 +0530647 ALOGV("setPhoneState() state %d", state);
648 // store previous phone state for management of sonification strategy below
Sharad Sangle4509cef2015-08-19 20:47:12 +0530649 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Sharad Sangle36781612015-05-28 16:15:16 +0530650 int oldState = mEngine->getPhoneState();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700651
Sharad Sangle36781612015-05-28 16:15:16 +0530652 if (mEngine->setPhoneState(state) != NO_ERROR) {
653 ALOGW("setPhoneState() invalid or same state %d", state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700654 return;
655 }
Sharad Sangle36781612015-05-28 16:15:16 +0530656 /// Opens: can these line be executed after the switch of volume curves???
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700657 // if leaving call state, handle special case of active streams
658 // pertaining to sonification strategy see handleIncallSonification()
659 if (isInCall()) {
660 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530661 for (size_t j = 0; j < mOutputs.size(); j++) {
662 audio_io_handle_t curOutput = mOutputs.keyAt(j);
663 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
664 if (stream == AUDIO_STREAM_PATCH) {
665 continue;
666 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530667 handleIncallSonification((audio_stream_type_t)stream, false, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +0530668 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700669 }
Sharad Sangle36781612015-05-28 16:15:16 +0530670
671 // force reevaluating accessibility routing when call starts
672 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700673 }
674
Sharad Sangle36781612015-05-28 16:15:16 +0530675 /**
676 * Switching to or from incall state or switching between telephony and VoIP lead to force
677 * routing command.
678 */
679 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
680 || (is_state_in_call(state) && (state != oldState)));
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700681
682 // check for device and output changes triggered by new phone state
683 checkA2dpSuspend();
684 checkOutputForAllStrategies();
685 updateDevicesAndOutputs();
686
Sharad Sangle36781612015-05-28 16:15:16 +0530687 sp<SwAudioOutputDescriptor> hwOutputDesc = mPrimaryOutput;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530688#ifdef VOICE_CONCURRENCY
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530689 char propValue[PROPERTY_VALUE_MAX];
690 bool prop_playback_enabled = false, prop_rec_enabled=false, prop_voip_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700691
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530692 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
693 prop_playback_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
694 }
695
696 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
697 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
698 }
699
700 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
701 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
702 }
703
Dhananjay Kumar9125b2a2015-12-01 16:24:09 +0530704 if ((AUDIO_MODE_IN_CALL != oldState) && (AUDIO_MODE_IN_CALL == state)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530705 ALOGD("voice_conc:Entering to call mode oldState :: %d state::%d ",
706 oldState, state);
Dhananjay Kumar9125b2a2015-12-01 16:24:09 +0530707 mvoice_call_state = state;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530708 if (prop_rec_enabled) {
709 //Close all active inputs
710 audio_io_handle_t activeInput = mInputs.getActiveInput();
711 if (activeInput != 0) {
712 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
713 switch(activeDesc->mInputSource) {
714 case AUDIO_SOURCE_VOICE_UPLINK:
715 case AUDIO_SOURCE_VOICE_DOWNLINK:
716 case AUDIO_SOURCE_VOICE_CALL:
717 ALOGD("voice_conc:FOUND active input during call active: %d",activeDesc->mInputSource);
718 break;
719
720 case AUDIO_SOURCE_VOICE_COMMUNICATION:
721 if(prop_voip_enabled) {
722 ALOGD("voice_conc:CLOSING VoIP input source on call setup :%d ",activeDesc->mInputSource);
723 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
724 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
725 }
726 break;
727
728 default:
729 ALOGD("voice_conc:CLOSING input on call setup for inputSource: %d",activeDesc->mInputSource);
730 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
731 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
732 break;
733 }
734 }
735 } else if (prop_voip_enabled) {
736 audio_io_handle_t activeInput = mInputs.getActiveInput();
737 if (activeInput != 0) {
738 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
739 if (AUDIO_SOURCE_VOICE_COMMUNICATION == activeDesc->mInputSource) {
740 ALOGD("voice_conc:CLOSING VoIP on call setup : %d",activeDesc->mInputSource);
741 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
742 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
743 }
744 }
745 }
746 if (prop_playback_enabled) {
747 // Move tracks associated to this strategy from previous output to new output
748 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
749 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
750 if (i == AUDIO_STREAM_PATCH) {
751 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
752 continue;
753 }
754 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
755 if ((AUDIO_STREAM_MUSIC == i) ||
756 (AUDIO_STREAM_VOICE_CALL == i) ) {
757 ALOGD("voice_conc:Invalidate stream type %d", i);
758 mpClientInterface->invalidateStream((audio_stream_type_t)i);
759 }
760 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
761 ALOGD("voice_conc:Invalidate stream type %d", i);
762 mpClientInterface->invalidateStream((audio_stream_type_t)i);
763 }
764 }
765 }
766
767 for (size_t i = 0; i < mOutputs.size(); i++) {
768 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
769 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
770 ALOGD("voice_conc:ouput desc / profile is NULL");
771 continue;
772 }
773
774 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
775 if (((!outputDesc->isDuplicated() &&outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY))
776 && prop_playback_enabled) {
777 ALOGD("voice_conc:calling suspendOutput on call mode for primary output");
778 mpClientInterface->suspendOutput(mOutputs.keyAt(i));
779 } //Close compress all sessions
780 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
781 && prop_playback_enabled) {
782 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
783 closeOutput(mOutputs.keyAt(i));
784 }
785 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_VOIP_RX)
786 && prop_voip_enabled) {
787 ALOGD("voice_conc:calling closeOutput on call mode for DIRECT output");
788 closeOutput(mOutputs.keyAt(i));
789 }
790 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
791 if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)
792 && prop_playback_enabled) {
793 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
794 closeOutput(mOutputs.keyAt(i));
795 }
796 }
797 }
798 }
799
800 if ((AUDIO_MODE_IN_CALL == oldState || AUDIO_MODE_IN_COMMUNICATION == oldState) &&
801 (AUDIO_MODE_NORMAL == state) && prop_playback_enabled && mvoice_call_state) {
802 ALOGD("voice_conc:EXITING from call mode oldState :: %d state::%d \n",oldState, state);
803 mvoice_call_state = 0;
804 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
805 //restore PCM (deep-buffer) output after call termination
806 for (size_t i = 0; i < mOutputs.size(); i++) {
807 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
808 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
809 ALOGD("voice_conc:ouput desc / profile is NULL");
810 continue;
811 }
812 if (!outputDesc->isDuplicated() && outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
813 ALOGD("voice_conc:calling restoreOutput after call mode for primary output");
814 mpClientInterface->restoreOutput(mOutputs.keyAt(i));
815 }
816 }
817 }
818 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
819 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
820 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
821 if (i == AUDIO_STREAM_PATCH) {
822 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
823 continue;
824 }
825 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
826 if ((AUDIO_STREAM_MUSIC == i) ||
827 (AUDIO_STREAM_VOICE_CALL == i) ) {
828 mpClientInterface->invalidateStream((audio_stream_type_t)i);
829 }
830 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
831 mpClientInterface->invalidateStream((audio_stream_type_t)i);
832 }
833 }
834 }
835
836#endif
837#ifdef RECORD_PLAY_CONCURRENCY
838 char recConcPropValue[PROPERTY_VALUE_MAX];
839 bool prop_rec_play_enabled = false;
840
841 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
842 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
843 }
844 if (prop_rec_play_enabled) {
845 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
846 ALOGD("phone state changed to MODE_IN_COMM invlaidating music and voice streams");
847 // call invalidate for voice streams, so that it can use deepbuffer with VoIP out device from HAL
848 mpClientInterface->invalidateStream(AUDIO_STREAM_VOICE_CALL);
849 // call invalidate for music, so that compress will fallback to deep-buffer with VoIP out device
850 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
851
852 // close compress output to make sure session will be closed before timeout(60sec)
853 for (size_t i = 0; i < mOutputs.size(); i++) {
854
855 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
856 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
857 ALOGD("ouput desc / profile is NULL");
858 continue;
859 }
860
861 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
862 ALOGD("calling closeOutput on call mode for COMPRESS output");
863 closeOutput(mOutputs.keyAt(i));
864 }
865 }
866 } else if ((oldState == AUDIO_MODE_IN_COMMUNICATION) &&
867 (mEngine->getPhoneState() == AUDIO_MODE_NORMAL)) {
868 // call invalidate for music so that music can fallback to compress
869 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
870 }
871 }
872#endif
873 mPrevPhoneState = oldState;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700874 int delayMs = 0;
875 if (isStateInCall(state)) {
876 nsecs_t sysTime = systemTime();
877 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530878 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700879 // mute media and sonification strategies and delay device switch by the largest
880 // latency of any output where either strategy is active.
881 // This avoid sending the ring tone or music tail into the earpiece or headset.
Sharad Sangle36781612015-05-28 16:15:16 +0530882 if ((isStrategyActive(desc, STRATEGY_MEDIA,
883 SONIFICATION_HEADSET_MUSIC_DELAY,
884 sysTime) ||
885 isStrategyActive(desc, STRATEGY_SONIFICATION,
886 SONIFICATION_HEADSET_MUSIC_DELAY,
887 sysTime)) &&
888 (delayMs < (int)desc->latency()*2)) {
889 delayMs = desc->latency()*2;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700890 }
Sharad Sangle36781612015-05-28 16:15:16 +0530891 setStrategyMute(STRATEGY_MEDIA, true, desc);
892 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700893 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Sharad Sangle36781612015-05-28 16:15:16 +0530894 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
895 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700896 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
897 }
898 }
899
Sharad Sangle36781612015-05-28 16:15:16 +0530900 if (hasPrimaryOutput()) {
901 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
902 // the device returned is not necessarily reachable via this output
903 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
904 // force routing command to audio hardware when ending call
905 // even if no device change is needed
906 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
907 rxDevice = mPrimaryOutput->device();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700908 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700909
Sharad Sangle36781612015-05-28 16:15:16 +0530910 if (state == AUDIO_MODE_IN_CALL) {
911 updateCallRouting(rxDevice, delayMs);
912 } else if (oldState == AUDIO_MODE_IN_CALL) {
913 if (mCallRxPatch != 0) {
914 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
915 mCallRxPatch.clear();
916 }
917 if (mCallTxPatch != 0) {
918 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
919 mCallTxPatch.clear();
920 }
921 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
922 } else {
923 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700924 }
925 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530926 //update device for all non-primary outputs
927 for (size_t i = 0; i < mOutputs.size(); i++) {
928 audio_io_handle_t output = mOutputs.keyAt(i);
929 if (output != mPrimaryOutput->mIoHandle) {
930 newDevice = getNewOutputDevice(mOutputs.valueFor(output), false /*fromCache*/);
931 setOutputDevice(mOutputs.valueFor(output), newDevice, (newDevice != AUDIO_DEVICE_NONE));
932 }
933 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700934 // if entering in call state, handle special case of active streams
935 // pertaining to sonification strategy see handleIncallSonification()
936 if (isStateInCall(state)) {
937 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530938 for (size_t j = 0; j < mOutputs.size(); j++) {
939 audio_io_handle_t curOutput = mOutputs.keyAt(j);
940 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
941 if (stream == AUDIO_STREAM_PATCH) {
942 continue;
943 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530944 handleIncallSonification((audio_stream_type_t)stream, true, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +0530945 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700946 }
947 }
948
949 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
950 if (state == AUDIO_MODE_RINGTONE &&
951 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
952 mLimitRingtoneVolume = true;
953 } else {
954 mLimitRingtoneVolume = false;
955 }
956}
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +0530957
958void AudioPolicyManagerCustom::setForceUse(audio_policy_force_use_t usage,
959 audio_policy_forced_cfg_t config)
960{
961 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
962
963 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
964 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
965 return;
966 }
967 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
968 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
969 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
970
971 // check for device and output changes triggered by new force usage
972 checkA2dpSuspend();
973 checkOutputForAllStrategies();
974 updateDevicesAndOutputs();
975 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
976 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
977 updateCallRouting(newDevice);
978 }
979 // Use reverse loop to make sure any low latency usecases (generally tones)
980 // are not routed before non LL usecases (generally music).
981 // We can safely assume that LL output would always have lower index,
982 // and use this work-around to avoid routing of output with music stream
983 // from the context of short lived LL output.
984 // Note: in case output's share backend(HAL sharing is implicit) all outputs
985 // gets routing update while processing first output itself.
986 for (size_t i = mOutputs.size(); i > 0; i--) {
987 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i-1);
988 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
989 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || outputDesc != mPrimaryOutput) {
990 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
991 }
992 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
993 applyStreamVolumes(outputDesc, newDevice, 0, true);
994 }
995 }
996
997 audio_io_handle_t activeInput = mInputs.getActiveInput();
998 if (activeInput != 0) {
999 setInputDevice(activeInput, getNewInputDevice(activeInput));
1000 }
1001
1002}
1003
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301004status_t AudioPolicyManagerCustom::stopSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301005 audio_stream_type_t stream,
1006 bool forceDeviceUpdate)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001007{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301008 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1009 ALOGW("stopSource() invalid stream %d", stream);
1010 return INVALID_OPERATION;
1011 }
Sharad Sangle36781612015-05-28 16:15:16 +05301012 // always handle stream stop, check which stream type is stopping
1013 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001014
Sharad Sangle36781612015-05-28 16:15:16 +05301015 // handle special case for sonification while in call
Sharad Sangle4509cef2015-08-19 20:47:12 +05301016 if (isInCall() && (outputDesc->mRefCount[stream] == 1)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301017 if (outputDesc->isDuplicated()) {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301018 handleIncallSonification(stream, false, false, outputDesc->subOutput1()->mIoHandle);
1019 handleIncallSonification(stream, false, false, outputDesc->subOutput2()->mIoHandle);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001020 }
Sharad Sangle36781612015-05-28 16:15:16 +05301021 handleIncallSonification(stream, false, false, outputDesc->mIoHandle);
1022 }
1023
1024 if (outputDesc->mRefCount[stream] > 0) {
1025 // decrement usage count of this stream on the output
1026 outputDesc->changeRefCount(stream, -1);
1027
1028 // store time at which the stream was stopped - see isStreamActive()
1029 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
1030 outputDesc->mStopTime[stream] = systemTime();
Zhou Song5dcddc92015-09-21 14:36:57 +08001031 audio_devices_t prevDevice = outputDesc->device();
Sharad Sangle36781612015-05-28 16:15:16 +05301032 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
1033 // delay the device switch by twice the latency because stopOutput() is executed when
1034 // the track stop() command is received and at that time the audio track buffer can
1035 // still contain data that needs to be drained. The latency only covers the audio HAL
1036 // and kernel buffers. Also the latency does not always include additional delay in the
1037 // audio path (audio DSP, CODEC ...)
1038 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
1039
1040 // force restoring the device selection on other active outputs if it differs from the
1041 // one being selected for this output
1042 for (size_t i = 0; i < mOutputs.size(); i++) {
1043 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1044 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1045 if (desc != outputDesc &&
1046 desc->isActive() &&
1047 outputDesc->sharesHwModuleWith(desc) &&
1048 (newDevice != desc->device())) {
Sharad Sangle4509cef2015-08-19 20:47:12 +05301049 audio_devices_t dev = getNewOutputDevice(mOutputs.valueFor(curOutput), false /*fromCache*/);
Zhou Song5dcddc92015-09-21 14:36:57 +08001050 uint32_t delayMs;
1051 if (dev == prevDevice) {
1052 delayMs = 0;
1053 } else {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301054 delayMs = outputDesc->latency()*2;
Zhou Song5dcddc92015-09-21 14:36:57 +08001055 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301056 setOutputDevice(desc,
1057 dev,
Sharad Sangle36781612015-05-28 16:15:16 +05301058 true,
Zhou Song5dcddc92015-09-21 14:36:57 +08001059 delayMs);
Sharad Sangle36781612015-05-28 16:15:16 +05301060 }
1061 }
1062 // update the outputs if stopping one with a stream that can affect notification routing
1063 handleNotificationRoutingForStream(stream);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001064 }
Sharad Sangle36781612015-05-28 16:15:16 +05301065 return NO_ERROR;
1066 } else {
1067 ALOGW("stopOutput() refcount is already 0");
1068 return INVALID_OPERATION;
1069 }
1070}
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301071status_t AudioPolicyManagerCustom::startSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301072 audio_stream_type_t stream,
1073 audio_devices_t device,
1074 uint32_t *delayMs)
1075{
1076 // cannot start playback of STREAM_TTS if any other output is being used
1077 uint32_t beaconMuteLatency = 0;
1078
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301079 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1080 ALOGW("startSource() invalid stream %d", stream);
1081 return INVALID_OPERATION;
1082 }
1083
Sharad Sangle36781612015-05-28 16:15:16 +05301084 *delayMs = 0;
1085 if (stream == AUDIO_STREAM_TTS) {
1086 ALOGV("\t found BEACON stream");
1087 if (mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
1088 return INVALID_OPERATION;
1089 } else {
1090 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001091 }
Sharad Sangle36781612015-05-28 16:15:16 +05301092 } else {
1093 // some playback other than beacon starts
1094 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1095 }
1096
1097 // increment usage count for this stream on the requested output:
1098 // NOTE that the usage count is the same for duplicated output and hardware output which is
1099 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1100 outputDesc->changeRefCount(stream, 1);
1101
1102 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
1103 // starting an output being rerouted?
1104 if (device == AUDIO_DEVICE_NONE) {
1105 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001106 }
Sharad Sangle36781612015-05-28 16:15:16 +05301107 routing_strategy strategy = getStrategy(stream);
1108 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
1109 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1110 (beaconMuteLatency > 0);
1111 uint32_t waitMs = beaconMuteLatency;
1112 bool force = false;
1113 for (size_t i = 0; i < mOutputs.size(); i++) {
1114 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1115 if (desc != outputDesc) {
1116 // force a device change if any other output is managed by the same hw
1117 // module and has a current device selection that differs from selected device.
1118 // In this case, the audio HAL must receive the new device selection so that it can
1119 // change the device currently selected by the other active output.
1120 if (outputDesc->sharesHwModuleWith(desc) &&
1121 desc->device() != device) {
1122 force = true;
1123 }
1124 // wait for audio on other active outputs to be presented when starting
1125 // a notification so that audio focus effect can propagate, or that a mute/unmute
1126 // event occurred for beacon
1127 uint32_t latency = desc->latency();
1128 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1129 waitMs = latency;
1130 }
1131 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001132 }
Sharad Sangle36781612015-05-28 16:15:16 +05301133 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force);
1134
1135 // handle special case for sonification while in call
1136 if (isInCall()) {
1137 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001138 }
Sharad Sangle36781612015-05-28 16:15:16 +05301139
1140 // apply volume rules for current stream and device if necessary
1141 checkAndSetVolume(stream,
1142 mStreams.valueFor(stream).getVolumeIndex(device),
1143 outputDesc,
1144 device);
1145
1146 // update the outputs if starting an output with a stream that can affect notification
1147 // routing
1148 handleNotificationRoutingForStream(stream);
1149
1150 // force reevaluating accessibility routing when ringtone or alarm starts
1151 if (strategy == STRATEGY_SONIFICATION) {
1152 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1153 }
1154 }
1155 else {
1156 // handle special case for sonification while in call
1157 if (isInCall()) {
1158 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
1159 }
1160 }
1161 return NO_ERROR;
1162}
1163void AudioPolicyManagerCustom::handleIncallSonification(audio_stream_type_t stream,
1164 bool starting, bool stateChange,
1165 audio_io_handle_t output)
1166{
1167 if(!hasPrimaryOutput()) {
1168 return;
1169 }
1170 // no action needed for AUDIO_STREAM_PATCH stream type, it's for internal flinger tracks
1171 if (stream == AUDIO_STREAM_PATCH) {
1172 return;
1173 }
1174 // if the stream pertains to sonification strategy and we are in call we must
1175 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
1176 // in the device used for phone strategy and play the tone if the selected device does not
1177 // interfere with the device used for phone strategy
1178 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
1179 // many times as there are active tracks on the output
1180 const routing_strategy stream_strategy = getStrategy(stream);
1181 if ((stream_strategy == STRATEGY_SONIFICATION) ||
1182 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
1183 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
1184 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
1185 stream, starting, outputDesc->mDevice, stateChange);
1186 if (outputDesc->mRefCount[stream]) {
1187 int muteCount = 1;
1188 if (stateChange) {
1189 muteCount = outputDesc->mRefCount[stream];
1190 }
1191 if (audio_is_low_visibility(stream)) {
1192 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
1193 for (int i = 0; i < muteCount; i++) {
1194 setStreamMute(stream, starting, outputDesc);
1195 }
1196 } else {
1197 ALOGV("handleIncallSonification() high visibility");
1198 if (outputDesc->device() &
1199 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
1200 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
1201 for (int i = 0; i < muteCount; i++) {
1202 setStreamMute(stream, starting, outputDesc);
1203 }
1204 }
1205 if (starting) {
1206 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
1207 AUDIO_STREAM_VOICE_CALL);
1208 } else {
1209 mpClientInterface->stopTone();
1210 }
1211 }
1212 }
1213 }
1214}
1215void AudioPolicyManagerCustom::handleNotificationRoutingForStream(audio_stream_type_t stream) {
1216 switch(stream) {
1217 case AUDIO_STREAM_MUSIC:
1218 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
1219 updateDevicesAndOutputs();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001220 break;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001221 default:
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001222 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001223 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001224}
Sharad Sangle36781612015-05-28 16:15:16 +05301225status_t AudioPolicyManagerCustom::checkAndSetVolume(audio_stream_type_t stream,
1226 int index,
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301227 const sp<AudioOutputDescriptor>& outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301228 audio_devices_t device,
1229 int delayMs, bool force)
1230{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301231 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1232 ALOGW("checkAndSetVolume() invalid stream %d", stream);
1233 return INVALID_OPERATION;
1234 }
Sharad Sangle36781612015-05-28 16:15:16 +05301235 // do not change actual stream volume if the stream is muted
1236 if (outputDesc->mMuteCount[stream] != 0) {
1237 ALOGVV("checkAndSetVolume() stream %d muted count %d",
1238 stream, outputDesc->mMuteCount[stream]);
1239 return NO_ERROR;
1240 }
1241 audio_policy_forced_cfg_t forceUseForComm =
1242 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
1243 // do not change in call volume if bluetooth is connected and vice versa
1244 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
1245 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
1246 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
1247 stream, forceUseForComm);
1248 return INVALID_OPERATION;
1249 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001250
Sharad Sangle36781612015-05-28 16:15:16 +05301251 if (device == AUDIO_DEVICE_NONE) {
1252 device = outputDesc->device();
1253 }
1254
1255 float volumeDb = computeVolume(stream, index, device);
1256 if (outputDesc->isFixedVolume(device)) {
1257 volumeDb = 0.0f;
1258 }
1259
1260 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
1261
1262 if (stream == AUDIO_STREAM_VOICE_CALL ||
1263 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
1264 float voiceVolume;
1265 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
1266 if (stream == AUDIO_STREAM_VOICE_CALL) {
1267 voiceVolume = (float)index/(float)mStreams.valueFor(stream).getVolumeIndexMax();
1268 } else {
1269 voiceVolume = 1.0;
1270 }
1271
1272 if (voiceVolume != mLastVoiceVolume && ((outputDesc == mPrimaryOutput) ||
1273 isDirectOutput(outputDesc->mIoHandle) || device & AUDIO_DEVICE_OUT_ALL_USB)) {
1274 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
1275 mLastVoiceVolume = voiceVolume;
1276 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301277#ifdef FM_POWER_OPT
1278 } else if (stream == AUDIO_STREAM_MUSIC && hasPrimaryOutput() &&
Haynes Mathew George603ae9b2015-12-21 17:23:59 -08001279 outputDesc == mPrimaryOutput && mFMIsActive) {
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001280 /* Avoid unnecessary set_parameter calls as it puts the primary
1281 outputs FastMixer in HOT_IDLE leading to breaks in audio */
1282 if (volumeDb != mPrevFMVolumeDb) {
1283 mPrevFMVolumeDb = volumeDb;
1284 AudioParameter param = AudioParameter();
1285 param.addFloat(String8("fm_volume"), Volume::DbToAmpl(volumeDb));
Mingming Yin1433cc62016-01-04 14:58:00 -08001286 //Double delayMs to avoid sound burst while device switch.
1287 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString(), delayMs*2);
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001288 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301289#endif /* FM_POWER_OPT end */
Sharad Sangle36781612015-05-28 16:15:16 +05301290 }
1291
1292 return NO_ERROR;
1293}
1294bool AudioPolicyManagerCustom::isDirectOutput(audio_io_handle_t output) {
1295 for (size_t i = 0; i < mOutputs.size(); i++) {
1296 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1297 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1298 if ((curOutput == output) && (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
1299 return true;
1300 }
1301 }
1302 return false;
1303}
vivek mehta0ea887a2015-08-26 14:01:20 -07001304
1305status_t AudioPolicyManagerCustom::getOutputForAttr(const audio_attributes_t *attr,
1306 audio_io_handle_t *output,
1307 audio_session_t session,
1308 audio_stream_type_t *stream,
1309 uid_t uid,
1310 uint32_t samplingRate,
1311 audio_format_t format,
1312 audio_channel_mask_t channelMask,
1313 audio_output_flags_t flags,
1314 audio_port_handle_t selectedDeviceId,
1315 const audio_offload_info_t *offloadInfo)
1316{
1317 audio_offload_info_t tOffloadInfo = AUDIO_INFO_INITIALIZER;
1318
Alexy Joseph0ac09032015-10-16 15:57:53 -07001319 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
1320 bool pcmOffloadEnabled = false;
1321
1322 if (offloadDisabled) {
1323 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
1324 }
1325
1326 //read track offload property only if the global offload switch is off.
1327 if (!offloadDisabled) {
1328 pcmOffloadEnabled = property_get_bool("audio.offload.track.enable", false);
1329 }
vivek mehta0ea887a2015-08-26 14:01:20 -07001330
1331 if (offloadInfo == NULL && pcmOffloadEnabled) {
1332 tOffloadInfo.sample_rate = samplingRate;
1333 tOffloadInfo.channel_mask = channelMask;
1334 tOffloadInfo.format = format;
1335 tOffloadInfo.stream_type = *stream;
1336 tOffloadInfo.bit_width = 16; //hard coded for PCM_16
1337 if (attr != NULL) {
1338 ALOGV("found attribute .. setting usage %d ", attr->usage);
1339 tOffloadInfo.usage = attr->usage;
1340 } else {
Alexy Joseph0ac09032015-10-16 15:57:53 -07001341 ALOGI("%s:: attribute is NULL .. no usage set", __func__);
vivek mehta0ea887a2015-08-26 14:01:20 -07001342 }
1343 offloadInfo = &tOffloadInfo;
1344 }
1345
1346 return AudioPolicyManager::getOutputForAttr(attr, output, session, stream,
1347 (uid_t)uid, (uint32_t)samplingRate,
1348 format, (audio_channel_mask_t)channelMask,
1349 flags, (audio_port_handle_t)selectedDeviceId,
1350 offloadInfo);
1351}
1352
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001353audio_io_handle_t AudioPolicyManagerCustom::getOutputForDevice(
1354 audio_devices_t device,
Sharad Sangle36781612015-05-28 16:15:16 +05301355 audio_session_t session __unused,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001356 audio_stream_type_t stream,
1357 uint32_t samplingRate,
1358 audio_format_t format,
1359 audio_channel_mask_t channelMask,
1360 audio_output_flags_t flags,
1361 const audio_offload_info_t *offloadInfo)
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001362{
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001363 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
1364 uint32_t latency = 0;
1365 status_t status;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001366
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001367#ifdef AUDIO_POLICY_TEST
1368 if (mCurOutput != 0) {
1369 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1370 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1371
1372 if (mTestOutputs[mCurOutput] == 0) {
1373 ALOGV("getOutput() opening test output");
Sharad Sangle36781612015-05-28 16:15:16 +05301374 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
1375 mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001376 outputDesc->mDevice = mTestDevice;
1377 outputDesc->mLatency = mTestLatencyMs;
1378 outputDesc->mFlags =
1379 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
1380 outputDesc->mRefCount[stream] = 0;
1381 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1382 config.sample_rate = mTestSamplingRate;
1383 config.channel_mask = mTestChannels;
1384 config.format = mTestFormat;
1385 if (offloadInfo != NULL) {
1386 config.offload_info = *offloadInfo;
1387 }
1388 status = mpClientInterface->openOutput(0,
1389 &mTestOutputs[mCurOutput],
1390 &config,
1391 &outputDesc->mDevice,
1392 String8(""),
1393 &outputDesc->mLatency,
1394 outputDesc->mFlags);
1395 if (status == NO_ERROR) {
1396 outputDesc->mSamplingRate = config.sample_rate;
1397 outputDesc->mFormat = config.format;
1398 outputDesc->mChannelMask = config.channel_mask;
1399 AudioParameter outputCmd = AudioParameter();
1400 outputCmd.addInt(String8("set_id"),mCurOutput);
1401 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1402 addOutput(mTestOutputs[mCurOutput], outputDesc);
1403 }
1404 }
1405 return mTestOutputs[mCurOutput];
1406 }
1407#endif //AUDIO_POLICY_TEST
Sharad Sangle36781612015-05-28 16:15:16 +05301408 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1409 (stream != AUDIO_STREAM_MUSIC)) {
1410 // compress should not be used for non-music streams
1411 ALOGE("Offloading only allowed with music stream");
1412 return 0;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301413 }
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301414
1415 if ((stream == AUDIO_STREAM_VOICE_CALL) &&
1416 (channelMask == 1) &&
1417 (samplingRate == 8000 || samplingRate == 16000)) {
1418 // Allow Voip direct output only if:
1419 // audio mode is MODE_IN_COMMUNCATION; AND
1420 // voip output is not opened already; AND
1421 // requested sample rate matches with that of voip input stream (if opened already)
1422 int value = 0;
1423 uint32_t mode = 0, voipOutCount = 1, voipSampleRate = 1;
1424 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1425 String8("audio_mode"));
1426 AudioParameter result = AudioParameter(valueStr);
1427 if (result.getInt(String8("audio_mode"), value) == NO_ERROR) {
1428 mode = value;
1429 }
1430
1431 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1432 String8("voip_out_stream_count"));
1433 result = AudioParameter(valueStr);
1434 if (result.getInt(String8("voip_out_stream_count"), value) == NO_ERROR) {
1435 voipOutCount = value;
1436 }
1437
1438 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1439 String8("voip_sample_rate"));
1440 result = AudioParameter(valueStr);
1441 if (result.getInt(String8("voip_sample_rate"), value) == NO_ERROR) {
1442 voipSampleRate = value;
1443 }
1444
1445 if ((mode == AUDIO_MODE_IN_COMMUNICATION) && (voipOutCount == 0) &&
1446 ((voipSampleRate == 0) || (voipSampleRate == samplingRate))) {
1447 if (audio_is_linear_pcm(format)) {
1448 char propValue[PROPERTY_VALUE_MAX] = {0};
1449 property_get("use.voice.path.for.pcm.voip", propValue, "0");
1450 bool voipPcmSysPropEnabled = !strncmp("true", propValue, sizeof("true"));
1451 if (voipPcmSysPropEnabled && (format == AUDIO_FORMAT_PCM_16_BIT)) {
Karthik Reddy Katta3fb9be72015-11-25 11:18:27 +05301452 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX |
1453 AUDIO_OUTPUT_FLAG_DIRECT);
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301454 ALOGD("Set VoIP and Direct output flags for PCM format");
1455 }
1456 }
1457 }
1458 }
1459
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301460#ifdef VOICE_CONCURRENCY
1461 char propValue[PROPERTY_VALUE_MAX];
1462 bool prop_play_enabled=false, prop_voip_enabled = false;
1463
1464 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1465 prop_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001466 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301467
1468 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1469 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1470 }
1471
1472 if (prop_play_enabled && mvoice_call_state) {
1473 //check if voice call is active / running in background
1474 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1475 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1476 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1477 {
1478 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1479 if(prop_voip_enabled) {
1480 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1481 flags );
1482 return 0;
1483 }
1484 }
1485 else {
1486 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
1487 ALOGD("voice_conc:IN call mode adding ULL flags .. flags: %x ", flags );
1488 flags = AUDIO_OUTPUT_FLAG_FAST;
1489 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
1490 if (AUDIO_STREAM_MUSIC == stream) {
1491 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1492 ALOGD("voice_conc:IN call mode adding deep-buffer flags %x ", flags );
1493 }
1494 else {
1495 flags = AUDIO_OUTPUT_FLAG_FAST;
1496 ALOGD("voice_conc:IN call mode adding fast flags %x ", flags );
1497 }
1498 }
1499 }
1500 }
1501 } else if (prop_voip_enabled && mvoice_call_state) {
1502 //check if voice call is active / running in background
1503 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1504 //return only ULL ouput
1505 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1506 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1507 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1508 {
1509 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1510 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1511 flags );
1512 return 0;
1513 }
1514 }
1515 }
1516#endif
1517#ifdef RECORD_PLAY_CONCURRENCY
1518 char recConcPropValue[PROPERTY_VALUE_MAX];
1519 bool prop_rec_play_enabled = false;
1520
1521 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
1522 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
1523 }
1524 if ((prop_rec_play_enabled) &&
1525 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
1526 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
1527 if (AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1528 // allow VoIP using voice path
1529 // Do nothing
1530 } else if((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1531 ALOGD("voice_conc:MODE_IN_COMM is setforcing deep buffer output for non ULL... flags: %x", flags);
1532 // use deep buffer path for all non ULL outputs
1533 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1534 }
1535 } else if ((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1536 ALOGD("voice_conc:Record mode is on forcing deep buffer output for non ULL... flags: %x ", flags);
1537 // use deep buffer path for all non ULL outputs
1538 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1539 }
1540 }
1541 if (prop_rec_play_enabled &&
1542 (stream == AUDIO_STREAM_ENFORCED_AUDIBLE)) {
1543 ALOGD("Record conc is on forcing ULL output for ENFORCED_AUDIBLE");
1544 flags = AUDIO_OUTPUT_FLAG_FAST;
1545 }
1546#endif
1547
Sharad Sangle4509cef2015-08-19 20:47:12 +05301548#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Sharad Sangle36781612015-05-28 16:15:16 +05301549 /*
1550 * WFD audio routes back to target speaker when starting a ringtone playback.
1551 * This is because primary output is reused for ringtone, so output device is
1552 * updated based on SONIFICATION strategy for both ringtone and music playback.
1553 * The same issue is not seen on remoted_submix HAL based WFD audio because
1554 * primary output is not reused and a new output is created for ringtone playback.
1555 * Issue is fixed by updating output flag to AUDIO_OUTPUT_FLAG_FAST when there is
1556 * a non-music stream playback on WFD, so primary output is not reused for ringtone.
1557 */
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001558 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
1559 if ((availableOutputDeviceTypes & AUDIO_DEVICE_OUT_PROXY)
1560 && (stream != AUDIO_STREAM_MUSIC)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301561 ALOGD("WFD audio: use OUTPUT_FLAG_FAST for non music stream. flags:%x", flags );
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001562 //For voip paths
1563 if(flags & AUDIO_OUTPUT_FLAG_DIRECT)
1564 flags = AUDIO_OUTPUT_FLAG_DIRECT;
1565 else //route every thing else to ULL path
1566 flags = AUDIO_OUTPUT_FLAG_FAST;
1567 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301568#endif
1569
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001570 // open a direct output if required by specified parameters
vivek mehta0ea887a2015-08-26 14:01:20 -07001571 // force direct flag if offload flag is set: offloading implies a direct output stream
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001572 // and all common behaviors are driven by checking only the direct flag
1573 // this should normally be set appropriately in the policy configuration file
1574 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1575 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1576 }
1577 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1578 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1579 }
vivek mehta0ea887a2015-08-26 14:01:20 -07001580
vivek mehta4b0d8192015-10-16 00:25:59 -07001581 bool forced_deep = false;
Sharad Sangle36781612015-05-28 16:15:16 +05301582 // only allow deep buffering for music stream type
1583 if (stream != AUDIO_STREAM_MUSIC) {
1584 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Sharad Sangle497aef82015-08-03 17:55:48 +05301585 } else if (/* stream == AUDIO_STREAM_MUSIC && */
1586 flags == AUDIO_OUTPUT_FLAG_NONE &&
1587 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
vivek mehtac984b992015-11-25 13:28:55 -08001588 forced_deep = true;
Sharad Sangle36781612015-05-28 16:15:16 +05301589 }
Sharad Sangle497aef82015-08-03 17:55:48 +05301590
Sharad Sangle36781612015-05-28 16:15:16 +05301591 if (stream == AUDIO_STREAM_TTS) {
1592 flags = AUDIO_OUTPUT_FLAG_TTS;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001593 }
1594
vivek mehta4b0d8192015-10-16 00:25:59 -07001595 // Do offload magic here
vivek mehtac984b992015-11-25 13:28:55 -08001596 if ((flags == AUDIO_OUTPUT_FLAG_NONE) &&
1597 (stream == AUDIO_STREAM_MUSIC) &&
1598 (offloadInfo != NULL) &&
vivek mehta4b0d8192015-10-16 00:25:59 -07001599 ((offloadInfo->usage == AUDIO_USAGE_MEDIA) || (offloadInfo->usage == AUDIO_USAGE_GAME))) {
vivek mehtac984b992015-11-25 13:28:55 -08001600 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DIRECT_PCM);
vivek mehta4b0d8192015-10-16 00:25:59 -07001601 ALOGD("AudioCustomHAL --> Force Direct Flag .. flag (0x%x)", flags);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301602 }
1603
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001604 sp<IOProfile> profile;
1605
1606 // skip direct output selection if the request can obviously be attached to a mixed output
1607 // and not explicitly requested
vivek mehtac984b992015-11-25 13:28:55 -08001608 if (((flags & (AUDIO_OUTPUT_FLAG_DIRECT|AUDIO_OUTPUT_FLAG_DIRECT_PCM)) == 0) &&
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001609 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
1610 audio_channel_count_from_out_mask(channelMask) <= 2) {
1611 goto non_direct_output;
1612 }
1613
1614 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1615 // creating an offloaded track and tearing it down immediately after start when audioflinger
1616 // detects there is an active non offloadable effect.
1617 // FIXME: We should check the audio session here but we do not have it in this context.
1618 // This may prevent offloading in rare situations where effects are left active by apps
1619 // in the background.
1620
Sharad Sangle36781612015-05-28 16:15:16 +05301621 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
1622 !mEffects.isNonOffloadableEffectEnabled()) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001623 profile = getProfileForDirectOutput(device,
1624 samplingRate,
1625 format,
1626 channelMask,
1627 (audio_output_flags_t)flags);
1628 }
1629
1630 if (profile != 0) {
vivek mehtac984b992015-11-25 13:28:55 -08001631
1632 if (!(flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) &&
1633 (profile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT_PCM)) {
1634 ALOGI("got Direct_PCM without requesting ... reject ");
1635 profile = NULL;
1636 goto non_direct_output;
1637 }
1638
Sharad Sangle36781612015-05-28 16:15:16 +05301639 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001640
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001641 // if multiple concurrent offload decode is supported
1642 // do no check for reuse and also don't close previous output if its offload
1643 // previous output will be closed during track destruction
1644 if (!(property_get_bool("audio.offload.multiple.enabled", false) &&
1645 ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0))) {
1646 for (size_t i = 0; i < mOutputs.size(); i++) {
1647 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1648 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1649 outputDesc = desc;
1650 // reuse direct output if currently open and configured with same parameters
1651 if ((samplingRate == outputDesc->mSamplingRate) &&
1652 (format == outputDesc->mFormat) &&
1653 (channelMask == outputDesc->mChannelMask)) {
1654 outputDesc->mDirectOpenCount++;
1655 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1656 return mOutputs.keyAt(i);
1657 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001658 }
1659 }
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001660 // close direct output if currently open and configured with different parameters
1661 if (outputDesc != NULL) {
1662 closeOutput(outputDesc->mIoHandle);
1663 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001664 }
Sharad Sangle36781612015-05-28 16:15:16 +05301665
1666 // if the selected profile is offloaded and no offload info was specified,
1667 // create a default one
1668 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
1669 if ((profile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
1670 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1671 defaultOffloadInfo.sample_rate = samplingRate;
1672 defaultOffloadInfo.channel_mask = channelMask;
1673 defaultOffloadInfo.format = format;
1674 defaultOffloadInfo.stream_type = stream;
1675 defaultOffloadInfo.bit_rate = 0;
1676 defaultOffloadInfo.duration_us = -1;
1677 defaultOffloadInfo.has_video = true; // conservative
1678 defaultOffloadInfo.is_streaming = true; // likely
1679 offloadInfo = &defaultOffloadInfo;
1680 }
1681
1682 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001683 outputDesc->mDevice = device;
1684 outputDesc->mLatency = 0;
Sharad Sangle36781612015-05-28 16:15:16 +05301685 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001686 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1687 config.sample_rate = samplingRate;
1688 config.channel_mask = channelMask;
1689 config.format = format;
1690 if (offloadInfo != NULL) {
1691 config.offload_info = *offloadInfo;
1692 }
Sharad Sangle36781612015-05-28 16:15:16 +05301693 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001694 &output,
1695 &config,
1696 &outputDesc->mDevice,
1697 String8(""),
1698 &outputDesc->mLatency,
1699 outputDesc->mFlags);
1700
1701 // only accept an output with the requested parameters
1702 if (status != NO_ERROR ||
1703 (samplingRate != 0 && samplingRate != config.sample_rate) ||
1704 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
1705 (channelMask != 0 && channelMask != config.channel_mask)) {
1706 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1707 "format %d %d, channelMask %04x %04x", output, samplingRate,
1708 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1709 outputDesc->mChannelMask);
1710 if (output != AUDIO_IO_HANDLE_NONE) {
1711 mpClientInterface->closeOutput(output);
1712 }
Sharad Sangle36781612015-05-28 16:15:16 +05301713 // fall back to mixer output if possible when the direct output could not be open
1714 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
1715 goto non_direct_output;
1716 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001717 return AUDIO_IO_HANDLE_NONE;
1718 }
1719 outputDesc->mSamplingRate = config.sample_rate;
1720 outputDesc->mChannelMask = config.channel_mask;
1721 outputDesc->mFormat = config.format;
1722 outputDesc->mRefCount[stream] = 0;
1723 outputDesc->mStopTime[stream] = 0;
1724 outputDesc->mDirectOpenCount = 1;
1725
1726 audio_io_handle_t srcOutput = getOutputForEffect();
1727 addOutput(output, outputDesc);
1728 audio_io_handle_t dstOutput = getOutputForEffect();
1729 if (dstOutput == output) {
Gao Jiedb057832015-11-12 08:51:22 +08001730#ifdef DOLBY_ENABLE
1731 status_t status = mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1732 if (status == NO_ERROR) {
1733 for (size_t i = 0; i < mEffects.size(); i++) {
1734 sp<EffectDescriptor> desc = mEffects.valueAt(i);
1735 if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX) {
1736 // update the mIo member of EffectDescriptor for the global effect
1737 ALOGV("%s updating mIo", __FUNCTION__);
1738 desc->mIo = dstOutput;
1739 }
1740 }
1741 } else {
1742 ALOGW("%s moveEffects from %d to %d failed", __FUNCTION__, srcOutput, dstOutput);
1743 }
1744#else // DOLBY_END
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001745 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
Gao Jiedb057832015-11-12 08:51:22 +08001746#endif // LINE_ADDED_BY_DOLBY
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001747 }
1748 mPreviousOutputs = mOutputs;
1749 ALOGV("getOutput() returns new direct output %d", output);
1750 mpClientInterface->onAudioPortListUpdate();
1751 return output;
1752 }
1753
1754non_direct_output:
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001755 // ignoring channel mask due to downmix capability in mixer
1756
1757 // open a non direct output
1758
1759 // for non direct outputs, only PCM is supported
1760 if (audio_is_linear_pcm(format)) {
vivek mehtac984b992015-11-25 13:28:55 -08001761 if (forced_deep) {
1762 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
1763 ALOGI("setting force DEEP buffer now ");
1764 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001765 // get which output is suitable for the specified stream. The actual
1766 // routing change will happen when startOutput() will be called
1767 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1768
1769 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1770 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1771 output = selectOutput(outputs, flags, format);
1772 }
1773 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1774 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1775
vivek mehta0ea887a2015-08-26 14:01:20 -07001776 ALOGV("getOutputForDevice() returns output %d", output);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001777
1778 return output;
1779}
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301780
1781status_t AudioPolicyManagerCustom::getInputForAttr(const audio_attributes_t *attr,
1782 audio_io_handle_t *input,
1783 audio_session_t session,
1784 uid_t uid,
1785 uint32_t samplingRate,
1786 audio_format_t format,
1787 audio_channel_mask_t channelMask,
1788 audio_input_flags_t flags,
1789 audio_port_handle_t selectedDeviceId,
1790 input_type_t *inputType)
1791{
1792 audio_source_t inputSource = attr->source;
1793#ifdef VOICE_CONCURRENCY
1794
1795 char propValue[PROPERTY_VALUE_MAX];
1796 bool prop_rec_enabled=false, prop_voip_enabled = false;
1797
1798 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
1799 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1800 }
1801
1802 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1803 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1804 }
1805
1806 if (prop_rec_enabled && mvoice_call_state) {
1807 //check if voice call is active / running in background
1808 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1809 //Need to block input request
1810 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1811 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1812 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1813 {
1814 switch(inputSource) {
1815 case AUDIO_SOURCE_VOICE_UPLINK:
1816 case AUDIO_SOURCE_VOICE_DOWNLINK:
1817 case AUDIO_SOURCE_VOICE_CALL:
1818 ALOGD("voice_conc:Creating input during incall mode for inputSource: %d",
1819 inputSource);
1820 break;
1821
1822 case AUDIO_SOURCE_VOICE_COMMUNICATION:
1823 if(prop_voip_enabled) {
1824 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1825 inputSource);
1826 return NO_INIT;
1827 }
1828 break;
1829 default:
1830 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1831 inputSource);
1832 return NO_INIT;
1833 }
1834 }
1835 }//check for VoIP flag
1836 else if(prop_voip_enabled && mvoice_call_state) {
1837 //check if voice call is active / running in background
1838 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1839 //Need to block input request
1840 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1841 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1842 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1843 {
1844 if(inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1845 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
1846 return NO_INIT;
1847 }
1848 }
1849 }
1850
1851#endif
1852
1853 return AudioPolicyManager::getInputForAttr(attr,
1854 input,
1855 session,
1856 uid,
1857 samplingRate,
1858 format,
1859 channelMask,
1860 flags,
1861 selectedDeviceId,
1862 inputType);
1863}
1864status_t AudioPolicyManagerCustom::startInput(audio_io_handle_t input,
1865 audio_session_t session)
1866{
1867 ALOGV("startInput() input %d", input);
1868 ssize_t index = mInputs.indexOfKey(input);
1869 if (index < 0) {
1870 ALOGW("startInput() unknown input %d", input);
1871 return BAD_VALUE;
1872 }
1873 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1874
1875 index = inputDesc->mSessions.indexOf(session);
1876 if (index < 0) {
1877 ALOGW("startInput() unknown session %d on input %d", session, input);
1878 return BAD_VALUE;
1879 }
1880
1881 // virtual input devices are compatible with other input devices
1882 if (!is_virtual_input_device(inputDesc->mDevice)) {
1883
1884 // for a non-virtual input device, check if there is another (non-virtual) active input
1885 audio_io_handle_t activeInput = mInputs.getActiveInput();
1886 if (activeInput != 0 && activeInput != input) {
1887
1888 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1889 // otherwise the active input continues and the new input cannot be started.
1890 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
1891 if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
1892 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
1893 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
1894 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
1895 } else {
1896 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
1897 return INVALID_OPERATION;
1898 }
1899 }
1900 }
1901
1902 // Routing?
1903 mInputRoutes.incRouteActivity(session);
1904#ifdef RECORD_PLAY_CONCURRENCY
1905 mIsInputRequestOnProgress = true;
1906
1907 char getPropValue[PROPERTY_VALUE_MAX];
1908 bool prop_rec_play_enabled = false;
1909
1910 if (property_get("rec.playback.conc.disabled", getPropValue, NULL)) {
1911 prop_rec_play_enabled = atoi(getPropValue) || !strncmp("true", getPropValue, 4);
1912 }
1913
1914 if ((prop_rec_play_enabled) &&(mInputs.activeInputsCount() == 0)){
1915 // send update to HAL on record playback concurrency
1916 AudioParameter param = AudioParameter();
1917 param.add(String8("rec_play_conc_on"), String8("true"));
1918 ALOGD("startInput() setParameters rec_play_conc is setting to ON ");
1919 mpClientInterface->setParameters(0, param.toString());
1920
1921 // Call invalidate to reset all opened non ULL audio tracks
1922 // Move tracks associated to this strategy from previous output to new output
1923 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
1924 // Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder)
Sharad Sangle4509cef2015-08-19 20:47:12 +05301925 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE && (i != AUDIO_STREAM_PATCH))) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301926 ALOGD("Invalidate on releaseInput for stream :: %d ", i);
1927 //FIXME see fixme on name change
1928 mpClientInterface->invalidateStream((audio_stream_type_t)i);
1929 }
1930 }
1931 // close compress tracks
1932 for (size_t i = 0; i < mOutputs.size(); i++) {
1933 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
1934 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
1935 ALOGD("ouput desc / profile is NULL");
1936 continue;
1937 }
1938 if (outputDesc->mProfile->mFlags
1939 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1940 // close compress sessions
1941 ALOGD("calling closeOutput on record conc for COMPRESS output");
1942 closeOutput(mOutputs.keyAt(i));
1943 }
1944 }
1945 }
1946#endif
1947
1948 if (inputDesc->mRefCount == 0 || mInputRoutes.hasRouteChanged(session)) {
1949 // if input maps to a dynamic policy with an activity listener, notify of state change
1950 if ((inputDesc->mPolicyMix != NULL)
1951 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1952 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1953 MIX_STATE_MIXING);
1954 }
1955
1956 if (mInputs.activeInputsCount() == 0) {
1957 SoundTrigger::setCaptureState(true);
1958 }
1959 setInputDevice(input, getNewInputDevice(input), true /* force */);
1960
1961 // automatically enable the remote submix output when input is started if not
1962 // used by a policy mix of type MIX_TYPE_RECORDERS
1963 // For remote submix (a virtual device), we open only one input per capture request.
1964 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1965 String8 address = String8("");
1966 if (inputDesc->mPolicyMix == NULL) {
1967 address = String8("0");
1968 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1969 address = inputDesc->mPolicyMix->mRegistrationId;
1970 }
1971 if (address != "") {
1972 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1973 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1974 address, "remote-submix");
1975 }
1976 }
1977 }
1978
1979 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1980
1981 inputDesc->mRefCount++;
1982#ifdef RECORD_PLAY_CONCURRENCY
1983 mIsInputRequestOnProgress = false;
1984#endif
1985 return NO_ERROR;
1986}
1987status_t AudioPolicyManagerCustom::stopInput(audio_io_handle_t input,
1988 audio_session_t session)
1989{
1990 status_t status;
1991 status = AudioPolicyManager::stopInput(input, session);
1992#ifdef RECORD_PLAY_CONCURRENCY
1993 char propValue[PROPERTY_VALUE_MAX];
1994 bool prop_rec_play_enabled = false;
1995
1996 if (property_get("rec.playback.conc.disabled", propValue, NULL)) {
1997 prop_rec_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1998 }
1999
2000 if ((prop_rec_play_enabled) && (mInputs.activeInputsCount() == 0)) {
2001
2002 //send update to HAL on record playback concurrency
2003 AudioParameter param = AudioParameter();
2004 param.add(String8("rec_play_conc_on"), String8("false"));
2005 ALOGD("stopInput() setParameters rec_play_conc is setting to OFF ");
2006 mpClientInterface->setParameters(0, param.toString());
2007
2008 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
2009 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
2010 //Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder stop tone)
2011 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE) && (i != AUDIO_STREAM_PATCH)) {
2012 ALOGD(" Invalidate on stopInput for stream :: %d ", i);
2013 //FIXME see fixme on name change
2014 mpClientInterface->invalidateStream((audio_stream_type_t)i);
2015 }
2016 }
2017 }
2018#endif
2019 return status;
2020}
2021
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302022void AudioPolicyManagerCustom::closeAllInputs() {
2023 bool patchRemoved = false;
2024
Dhanalakshmi Siddanibfeca7f2015-10-08 15:17:11 +05302025 for(size_t input_index = mInputs.size(); input_index > 0; input_index--) {
2026 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index-1);
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302027 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
2028 if (patch_index >= 0) {
2029 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
2030 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2031 mAudioPatches.removeItemsAt(patch_index);
2032 patchRemoved = true;
2033 }
2034 if ((inputDesc->mIsSoundTrigger) && (mInputs.size() == 1)) {
2035 ALOGD("Do not close sound trigger input handle");
2036 } else {
Dhanalakshmi Siddanibfeca7f2015-10-08 15:17:11 +05302037 mpClientInterface->closeInput(mInputs.keyAt(input_index-1));
2038 mInputs.removeItem(mInputs.keyAt(input_index-1));
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302039 }
2040 }
2041 nextAudioPortGeneration();
2042
2043 if (patchRemoved) {
2044 mpClientInterface->onAudioPatchListUpdate();
2045 }
2046}
2047
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302048AudioPolicyManagerCustom::AudioPolicyManagerCustom(AudioPolicyClientInterface *clientInterface)
Sharad Sangle4509cef2015-08-19 20:47:12 +05302049 : AudioPolicyManager(clientInterface),
2050 mHdmiAudioDisabled(false),
2051 mHdmiAudioEvent(false),
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08002052 mPrevPhoneState(0),
Haynes Mathew George603ae9b2015-12-21 17:23:59 -08002053 mPrevFMVolumeDb(0.0f),
2054 mFMIsActive(false)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302055{
Mingming Yin38ea08c2015-10-05 15:24:04 -07002056 char ssr_enabled[PROPERTY_VALUE_MAX] = {0};
2057 bool prop_ssr_enabled = false;
2058
2059 if (property_get("ro.qc.sdk.audio.ssr", ssr_enabled, NULL)) {
2060 prop_ssr_enabled = atoi(ssr_enabled) || !strncmp("true", ssr_enabled, 4);
2061 }
2062
2063 for (size_t i = 0; i < mHwModules.size(); i++) {
2064 ALOGV("Hw module %d", i);
2065 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) {
2066 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
2067 ALOGV("Input profile ", j);
2068 for (size_t k = 0; k < inProfile->mChannelMasks.size(); k++) {
2069 audio_channel_mask_t channelMask =
2070 inProfile->mChannelMasks.itemAt(k);
2071 ALOGV("Channel Mask %x size %d", channelMask,
2072 inProfile->mChannelMasks.size());
2073 if (AUDIO_CHANNEL_IN_5POINT1 == channelMask) {
2074 if (!prop_ssr_enabled) {
2075 ALOGI("removing AUDIO_CHANNEL_IN_5POINT1 from"
2076 " input profile as SSR(surround sound record)"
2077 " is not supported on this chipset variant");
2078 inProfile->mChannelMasks.removeItemsAt(k, 1);
2079 ALOGV("Channel Mask size now %d",
2080 inProfile->mChannelMasks.size());
2081 }
2082 }
2083 }
2084 }
2085 }
2086
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302087#ifdef RECORD_PLAY_CONCURRENCY
2088 mIsInputRequestOnProgress = false;
2089#endif
2090
2091
2092#ifdef VOICE_CONCURRENCY
2093 mFallBackflag = getFallBackPath();
2094#endif
2095}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07002096}