blob: 2f657d5acb372a4b8c2c583a05844232191caa08 [file] [log] [blame]
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001/*
Weiyin Jiangd211ef52016-02-16 15:55:11 +08002 * Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07003 * Not a contribution.
4 *
5 * Copyright (C) 2009 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Gao Jiedb057832015-11-12 08:51:22 +080018 *
19 * This file was modified by Dolby Laboratories, Inc. The portions of the
20 * code that are surrounded by "DOLBY..." are copyrighted and
21 * licensed separately, as follows:
22 *
23 * (C) 2015 Dolby Laboratories, Inc.
24 *
25 * Licensed under the Apache License, Version 2.0 (the "License");
26 * you may not use this file except in compliance with the License.
27 * You may obtain a copy of the License at
28 *
29 * http://www.apache.org/licenses/LICENSE-2.0
30 *
31 * Unless required by applicable law or agreed to in writing, software
32 * distributed under the License is distributed on an "AS IS" BASIS,
33 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34 * See the License for the specific language governing permissions and
35 * limitations under the License.
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070036 */
37
Sharad Sangle36781612015-05-28 16:15:16 +053038#define LOG_TAG "AudioPolicyManagerCustom"
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070039//#define LOG_NDEBUG 0
40
41//#define VERY_VERBOSE_LOGGING
42#ifdef VERY_VERBOSE_LOGGING
43#define ALOGVV ALOGV
44#else
45#define ALOGVV(a...) do { } while(0)
46#endif
Sharad Sangle36781612015-05-28 16:15:16 +053047
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070048// A device mask for all audio output devices that are considered "remote" when evaluating
49// active output devices in isStreamActiveRemotely()
50#define APM_AUDIO_OUT_DEVICE_REMOTE_ALL AUDIO_DEVICE_OUT_REMOTE_SUBMIX
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070051// A device mask for all audio input and output devices where matching inputs/outputs on device
52// type alone is not enough: the address must match too
53#define APM_AUDIO_DEVICE_MATCH_ADDRESS_ALL (AUDIO_DEVICE_IN_REMOTE_SUBMIX | \
54 AUDIO_DEVICE_OUT_REMOTE_SUBMIX)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070055
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070056#include <inttypes.h>
Mingming Yin0ae14ea2014-07-09 17:55:56 -070057#include <math.h>
Mingming Yin0670f162014-06-12 16:05:49 -070058
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070059#include <cutils/properties.h>
60#include <utils/Log.h>
61#include <hardware/audio.h>
62#include <hardware/audio_effect.h>
63#include <media/AudioParameter.h>
64#include <soundtrigger/SoundTrigger.h>
65#include "AudioPolicyManager.h"
Sharad Sangle36781612015-05-28 16:15:16 +053066#include <policy.h>
Gao Jiedb057832015-11-12 08:51:22 +080067#ifdef DOLBY_ENABLE
68#include "DolbyAudioPolicy_impl.h"
69#endif // DOLBY_END
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070070
71namespace android {
Sharad Sanglec5766ff2015-06-04 20:24:10 +053072#ifdef VOICE_CONCURRENCY
73audio_output_flags_t AudioPolicyManagerCustom::getFallBackPath()
74{
75 audio_output_flags_t flag = AUDIO_OUTPUT_FLAG_FAST;
76 char propValue[PROPERTY_VALUE_MAX];
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070077
Sharad Sanglec5766ff2015-06-04 20:24:10 +053078 if (property_get("voice.conc.fallbackpath", propValue, NULL)) {
79 if (!strncmp(propValue, "deep-buffer", 11)) {
80 flag = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
81 }
82 else if (!strncmp(propValue, "fast", 4)) {
83 flag = AUDIO_OUTPUT_FLAG_FAST;
84 }
85 else {
86 ALOGD("voice_conc:not a recognised path(%s) in prop voice.conc.fallbackpath",
87 propValue);
88 }
89 }
90 else {
91 ALOGD("voice_conc:prop voice.conc.fallbackpath not set");
92 }
93
94 ALOGD("voice_conc:picked up flag(0x%x) from prop voice.conc.fallbackpath",
95 flag);
96
97 return flag;
98}
99#endif /*VOICE_CONCURRENCY*/
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700100// ----------------------------------------------------------------------------
101// AudioPolicyInterface implementation
102// ----------------------------------------------------------------------------
Sharad Sangle36781612015-05-28 16:15:16 +0530103extern "C" AudioPolicyInterface* createAudioPolicyManager(
104 AudioPolicyClientInterface *clientInterface)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700105{
Sharad Sangle36781612015-05-28 16:15:16 +0530106 return new AudioPolicyManagerCustom(clientInterface);
107}
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700108
Sharad Sangle36781612015-05-28 16:15:16 +0530109extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface)
110{
111 delete interface;
112}
113
114status_t AudioPolicyManagerCustom::setDeviceConnectionStateInt(audio_devices_t device,
115 audio_policy_dev_state_t state,
116 const char *device_address,
117 const char *device_name)
118{
Ashish Jain40cab3a2016-03-23 11:14:14 +0530119 ALOGD("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
Sharad Sangle36781612015-05-28 16:15:16 +0530120 device, state, device_address, device_name);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700121
122 // connect/disconnect only 1 device at a time
123 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
124
Sharad Sangle36781612015-05-28 16:15:16 +0530125 sp<DeviceDescriptor> devDesc =
126 mHwModules.getDeviceDescriptor(device, device_address, device_name);
127
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700128 // handle output devices
129 if (audio_is_output_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700130 SortedVector <audio_io_handle_t> outputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700131
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700132 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700133
134 // save a copy of the opened output descriptors before any output is opened or closed
135 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
136 mPreviousOutputs = mOutputs;
137 switch (state)
138 {
139 // handle output device connection
Sharad Sangle36781612015-05-28 16:15:16 +0530140 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700141 if (index >= 0) {
Sharad Sangle4509cef2015-08-19 20:47:12 +0530142#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
143 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
144 if (!strncmp(device_address, "hdmi_spkr", 9)) {
145 mHdmiAudioDisabled = false;
146 } else {
147 mHdmiAudioEvent = true;
148 }
149 }
150#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700151 ALOGW("setDeviceConnectionState() device already connected: %x", device);
152 return INVALID_OPERATION;
153 }
154 ALOGV("setDeviceConnectionState() connecting device %x", device);
155
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700156 // register new device as available
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700157 index = mAvailableOutputDevices.add(devDesc);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530158#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
159 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
160 if (!strncmp(device_address, "hdmi_spkr", 9)) {
161 mHdmiAudioDisabled = false;
162 } else {
163 mHdmiAudioEvent = true;
164 }
165 if (mHdmiAudioDisabled || !mHdmiAudioEvent) {
166 mAvailableOutputDevices.remove(devDesc);
167 ALOGW("HDMI sink not connected, do not route audio to HDMI out");
168 return INVALID_OPERATION;
169 }
170 }
171#endif
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700172 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530173 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700174 if (module == 0) {
175 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
176 device);
177 mAvailableOutputDevices.remove(devDesc);
178 return INVALID_OPERATION;
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700179 }
Sharad Sangle36781612015-05-28 16:15:16 +0530180 mAvailableOutputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700181 } else {
182 return NO_MEMORY;
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700183 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700184
Sharad Sangle36781612015-05-28 16:15:16 +0530185 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700186 mAvailableOutputDevices.remove(devDesc);
187 return INVALID_OPERATION;
188 }
Sharad Sangle36781612015-05-28 16:15:16 +0530189 // Propagate device availability to Engine
190 mEngine->setDeviceConnectionState(devDesc, state);
191
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700192 // outputs should never be empty here
193 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
194 "checkOutputsForDevice() returned no outputs but status OK");
195 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
196 outputs.size());
Sharad Sangle36781612015-05-28 16:15:16 +0530197
198 // Send connect to HALs
199 AudioParameter param = AudioParameter(devDesc->mAddress);
200 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
201 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
202
203 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700204 // handle output device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700205 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
206 if (index < 0) {
Sharad Sangle4509cef2015-08-19 20:47:12 +0530207#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
208 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
209 if (!strncmp(device_address, "hdmi_spkr", 9)) {
210 mHdmiAudioDisabled = true;
211 } else {
212 mHdmiAudioEvent = false;
213 }
214 }
215#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700216 ALOGW("setDeviceConnectionState() device not connected: %x", device);
217 return INVALID_OPERATION;
218 }
219
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700220 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
221
Sharad Sangle36781612015-05-28 16:15:16 +0530222 // Send Disconnect to HALs
223 AudioParameter param = AudioParameter(devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700224 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
225 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
226
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700227 // remove device from available output devices
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700228 mAvailableOutputDevices.remove(devDesc);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530229#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
230 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
231 if (!strncmp(device_address, "hdmi_spkr", 9)) {
232 mHdmiAudioDisabled = true;
233 } else {
234 mHdmiAudioEvent = false;
235 }
236 }
237#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530238 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
239
240 // Propagate device availability to Engine
241 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700242 } break;
243
244 default:
245 ALOGE("setDeviceConnectionState() invalid state: %x", state);
246 return BAD_VALUE;
247 }
248
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700249 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
250 // output is suspended before any tracks are moved to it
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700251 checkA2dpSuspend();
252 checkOutputForAllStrategies();
253 // outputs must be closed after checkOutputForAllStrategies() is executed
254 if (!outputs.isEmpty()) {
255 for (size_t i = 0; i < outputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530256 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700257 // close unused outputs after device disconnection or direct outputs that have been
258 // opened by checkOutputsForDevice() to query dynamic parameters
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700259 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700260 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
261 (desc->mDirectOpenCount == 0))) {
262 closeOutput(outputs[i]);
263 }
264 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700265 // check again after closing A2DP output to reset mA2dpSuspended if needed
266 checkA2dpSuspend();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700267 }
268
269 updateDevicesAndOutputs();
Gao Jiedb057832015-11-12 08:51:22 +0800270#ifdef DOLBY_ENABLE
271 // Before closing the opened outputs, update endpoint property with device capabilities
272 audio_devices_t audioOutputDevice = getDeviceForStrategy(getStrategy(AUDIO_STREAM_MUSIC), true);
273 mDolbyAudioPolicy.setEndpointSystemProperty(audioOutputDevice, mHwModules);
274#endif // DOLBY_END
Sharad Sangle36781612015-05-28 16:15:16 +0530275 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
276 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700277 updateCallRouting(newDevice);
278 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530279
280#ifdef FM_POWER_OPT
281 // handle FM device connection state to trigger FM AFE loopback
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800282 if (device == AUDIO_DEVICE_OUT_FM && hasPrimaryOutput()) {
Mingming Yin2dd77c22016-01-06 18:02:42 -0800283 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530284 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
285 mPrimaryOutput->changeRefCount(AUDIO_STREAM_MUSIC, 1);
Mingming Yin2dd77c22016-01-06 18:02:42 -0800286 newDevice = (audio_devices_t)(getNewOutputDevice(mPrimaryOutput, false)|AUDIO_DEVICE_OUT_FM);
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800287 mFMIsActive = true;
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530288 } else {
Mingming Yin2dd77c22016-01-06 18:02:42 -0800289 newDevice = (audio_devices_t)(getNewOutputDevice(mPrimaryOutput, false));
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800290 mFMIsActive = false;
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530291 mPrimaryOutput->changeRefCount(AUDIO_STREAM_MUSIC, -1);
292 }
293 AudioParameter param = AudioParameter();
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
Ashish Jain40cab3a2016-03-23 11:14:14 +0530317 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
318 cleanUpForDevice(devDesc);
319 }
320
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700321 mpClientInterface->onAudioPortListUpdate();
322 return NO_ERROR;
323 } // end if is output device
324
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700325 // handle input devices
326 if (audio_is_input_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700327 SortedVector <audio_io_handle_t> inputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700328
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700329 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700330 switch (state)
331 {
332 // handle input device connection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700333 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
334 if (index >= 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700335 ALOGW("setDeviceConnectionState() device already connected: %d", device);
336 return INVALID_OPERATION;
337 }
Sharad Sangle36781612015-05-28 16:15:16 +0530338 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700339 if (module == NULL) {
340 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
341 device);
342 return INVALID_OPERATION;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700343 }
Sharad Sangle36781612015-05-28 16:15:16 +0530344 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700345 return INVALID_OPERATION;
346 }
347
348 index = mAvailableInputDevices.add(devDesc);
349 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530350 mAvailableInputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700351 } else {
352 return NO_MEMORY;
353 }
Sharad Sangle36781612015-05-28 16:15:16 +0530354
355 // Set connect to HALs
356 AudioParameter param = AudioParameter(devDesc->mAddress);
357 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
358 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
359
360 // Propagate device availability to Engine
361 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700362 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700363
364 // handle input device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700365 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
366 if (index < 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700367 ALOGW("setDeviceConnectionState() device not connected: %d", device);
368 return INVALID_OPERATION;
369 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700370
371 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
372
373 // Set Disconnect to HALs
Sharad Sangle36781612015-05-28 16:15:16 +0530374 AudioParameter param = AudioParameter(devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700375 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
376 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
377
Sharad Sangle36781612015-05-28 16:15:16 +0530378 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700379 mAvailableInputDevices.remove(devDesc);
380
Sharad Sangle36781612015-05-28 16:15:16 +0530381 // Propagate device availability to Engine
382 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700383 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700384
385 default:
386 ALOGE("setDeviceConnectionState() invalid state: %x", state);
387 return BAD_VALUE;
388 }
389
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700390 closeAllInputs();
391
Sharad Sangle36781612015-05-28 16:15:16 +0530392 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700393 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
394 updateCallRouting(newDevice);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700395 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700396
Ashish Jain40cab3a2016-03-23 11:14:14 +0530397 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
398 cleanUpForDevice(devDesc);
399 }
400
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700401 mpClientInterface->onAudioPortListUpdate();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700402 return NO_ERROR;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700403 } // end if is input device
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700404
405 ALOGW("setDeviceConnectionState() invalid device: %x", device);
406 return BAD_VALUE;
407}
Weiyin Jiangd211ef52016-02-16 15:55:11 +0800408
Sharad Sangle36781612015-05-28 16:15:16 +0530409// This function checks for the parameters which can be offloaded.
410// This can be enhanced depending on the capability of the DSP and policy
411// of the system.
412bool AudioPolicyManagerCustom::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700413{
Sharad Sangle36781612015-05-28 16:15:16 +0530414 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
415 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
416 offloadInfo.sample_rate, offloadInfo.channel_mask,
417 offloadInfo.format,
418 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
419 offloadInfo.has_video);
Ashish Jain40cab3a2016-03-23 11:14:14 +0530420
421 if (mMasterMono) {
422 return false; // no offloading if mono is set.
423 }
424
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530425#ifdef VOICE_CONCURRENCY
426 char concpropValue[PROPERTY_VALUE_MAX];
427 if (property_get("voice.playback.conc.disabled", concpropValue, NULL)) {
428 bool propenabled = atoi(concpropValue) || !strncmp("true", concpropValue, 4);
429 if (propenabled) {
430 if (isInCall())
431 {
432 ALOGD("\n copl: blocking compress offload on call mode\n");
433 return false;
434 }
435 }
436 }
437#endif
438#ifdef RECORD_PLAY_CONCURRENCY
439 char recConcPropValue[PROPERTY_VALUE_MAX];
440 bool prop_rec_play_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700441
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530442 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
443 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
444 }
445
446 if ((prop_rec_play_enabled) &&
447 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
448 ALOGD("copl: blocking compress offload for record concurrency");
449 return false;
450 }
451#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530452 // Check if stream type is music, then only allow offload as of now.
453 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
454 {
455 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
456 return false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700457 }
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530458
Alexy Joseph0ac09032015-10-16 15:57:53 -0700459 // Check if offload has been disabled
460 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
461 if (offloadDisabled) {
462 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
463 return false;
464 }
465
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530466 char propValue[PROPERTY_VALUE_MAX];
467 bool pcmOffload = false;
468#ifdef PCM_OFFLOAD_ENABLED
469 if ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM_OFFLOAD) {
470 bool prop_enabled = false;
471 if ((AUDIO_FORMAT_PCM_16_BIT_OFFLOAD == offloadInfo.format) &&
472 property_get("audio.offload.pcm.16bit.enable", propValue, NULL)) {
473 prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
474 }
475
476#ifdef PCM_OFFLOAD_ENABLED_24
477 if ((AUDIO_FORMAT_PCM_24_BIT_OFFLOAD == offloadInfo.format) &&
478 property_get("audio.offload.pcm.24bit.enable", propValue, NULL)) {
479 prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530480 }
481#endif
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530482
483 if (prop_enabled) {
484 ALOGI("PCM offload property is enabled");
485 pcmOffload = true;
486 }
487
488 if (!pcmOffload) {
489 ALOGD("system property not enabled for PCM offload format[%x]",offloadInfo.format);
490 return false;
491 }
492 }
493#endif
494 if (!pcmOffload) {
Alexy Joseph0ac09032015-10-16 15:57:53 -0700495
496 bool compressedOffloadDisabled = property_get_bool("audio.offload.compress.disable", false);
497 if (compressedOffloadDisabled) {
498 ALOGI("compressed offload disabled by audio.offload.compress.disable=%d", compressedOffloadDisabled);
499 return false;
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530500 }
Alexy Joseph0ac09032015-10-16 15:57:53 -0700501
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530502 //check if it's multi-channel AAC (includes sub formats) and FLAC format
503 if ((popcount(offloadInfo.channel_mask) > 2) &&
504 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
505 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS))) {
506 ALOGD("offload disabled for multi-channel AAC,FLAC and VORBIS format");
507 return false;
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530508 }
509
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530510#ifdef AUDIO_EXTN_FORMATS_ENABLED
511 //check if it's multi-channel FLAC/ALAC/WMA format with sample rate > 48k
512 if ((popcount(offloadInfo.channel_mask) > 2) &&
513 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
Manish Dewangana6fc5442015-08-24 20:30:31 +0530514 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) && (offloadInfo.sample_rate > 48000)) ||
515 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) && (offloadInfo.sample_rate > 48000)) ||
516 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) && (offloadInfo.sample_rate > 48000)) ||
517 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS))) {
518 ALOGD("offload disabled for multi-channel FLAC/ALAC/WMA/AAC_ADTS clips with sample rate > 48kHz");
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530519 return false;
520 }
521#endif
522 //TODO: enable audio offloading with video when ready
523 const bool allowOffloadWithVideo =
524 property_get_bool("audio.offload.video", false /* default_value */);
525 if (offloadInfo.has_video && !allowOffloadWithVideo) {
526 ALOGV("isOffloadSupported: has_video == true, returning false");
527 return false;
528 }
Manish Dewanganf3cd0f82015-10-13 14:04:36 +0530529
530 const bool allowOffloadStreamingWithVideo = property_get_bool("av.streaming.offload.enable",
531 false /*default value*/);
532 if(offloadInfo.has_video && offloadInfo.is_streaming && !allowOffloadStreamingWithVideo) {
533 ALOGW("offload disabled by av.streaming.offload.enable = %s ", propValue );
534 return false;
535 }
536
Sharad Sangle36781612015-05-28 16:15:16 +0530537 }
538
539 //If duration is less than minimum value defined in property, return false
540 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
541 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
542 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
543 return false;
544 }
545 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
546 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
547 //duration checks only valid for MP3/AAC/ formats,
548 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
549 if ((offloadInfo.format == AUDIO_FORMAT_MP3) ||
550 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530551 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS) ||
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530552#ifdef AUDIO_EXTN_FORMATS_ENABLED
Sharad Sangle36781612015-05-28 16:15:16 +0530553 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
Sharad Sangle36781612015-05-28 16:15:16 +0530554 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) ||
555 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) ||
556 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) ||
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530557 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_APE) ||
Manish Dewangana6fc5442015-08-24 20:30:31 +0530558 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS) ||
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530559#endif
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530560 pcmOffload)
Sharad Sangle36781612015-05-28 16:15:16 +0530561 return false;
562
563 }
564
565 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
566 // creating an offloaded track and tearing it down immediately after start when audioflinger
567 // detects there is an active non offloadable effect.
568 // FIXME: We should check the audio session here but we do not have it in this context.
569 // This may prevent offloading in rare situations where effects are left active by apps
570 // in the background.
571 if (mEffects.isNonOffloadableEffectEnabled()) {
572 return false;
573 }
574 // Check for soundcard status
575 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
576 String8("SND_CARD_STATUS"));
577 AudioParameter result = AudioParameter(valueStr);
578 int isonline = 0;
579 if ((result.getInt(String8("SND_CARD_STATUS"), isonline) == NO_ERROR)
580 && !isonline) {
581 ALOGD("copl: soundcard is offline rejecting offload request");
582 return false;
583 }
584 // See if there is a profile to support this.
585 // AUDIO_DEVICE_NONE
586 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
587 offloadInfo.sample_rate,
588 offloadInfo.format,
589 offloadInfo.channel_mask,
590 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
591 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
592 return (profile != 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700593}
Weiyin Jiangd211ef52016-02-16 15:55:11 +0800594
Sharad Sangle36781612015-05-28 16:15:16 +0530595audio_devices_t AudioPolicyManagerCustom::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
596 bool fromCache)
597{
598 audio_devices_t device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700599
Ashish Jain40cab3a2016-03-23 11:14:14 +0530600 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Sharad Sangle36781612015-05-28 16:15:16 +0530601 if (index >= 0) {
602 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
603 if (patchDesc->mUid != mUidCached) {
604 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Ashish Jain40cab3a2016-03-23 11:14:14 +0530605 outputDesc->device(), outputDesc->getPatchHandle());
Sharad Sangle36781612015-05-28 16:15:16 +0530606 return outputDesc->device();
607 }
608 }
609
610 // check the following by order of priority to request a routing change if necessary:
611 // 1: the strategy enforced audible is active and enforced on the output:
612 // use device for strategy enforced audible
613 // 2: we are in call or the strategy phone is active on the output:
614 // use device for strategy phone
615 // 3: the strategy for enforced audible is active but not enforced on the output:
616 // use the device for strategy enforced audible
617 // 4: the strategy sonification is active on the output:
618 // use device for strategy sonification
619 // 5: the strategy "respectful" sonification is active on the output:
620 // use device for strategy "respectful" sonification
621 // 6: the strategy accessibility is active on the output:
622 // use device for strategy accessibility
623 // 7: the strategy media is active on the output:
624 // use device for strategy media
625 // 8: the strategy DTMF is active on the output:
626 // use device for strategy DTMF
627 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
628 // use device for strategy t-t-s
629 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
630 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
631 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
632 } else if (isInCall() ||
633 isStrategyActive(outputDesc, STRATEGY_PHONE)||
634 isStrategyActive(mPrimaryOutput, STRATEGY_PHONE)) {
635 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
636 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
637 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
638 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)||
639 (isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION)
640 && (!isStrategyActive(mPrimaryOutput,STRATEGY_MEDIA)))) {
641 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530642 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL) ||
643 isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION_RESPECTFUL)) {
Sharad Sangle36781612015-05-28 16:15:16 +0530644 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
645 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
646 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
647 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
648 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
649 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
650 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
651 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
652 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
653 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
654 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
655 }
656
657 ALOGV("getNewOutputDevice() selected device %x", device);
658 return device;
659}
Weiyin Jiangd211ef52016-02-16 15:55:11 +0800660
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700661void AudioPolicyManagerCustom::setPhoneState(audio_mode_t state)
662{
Ashish Jain40cab3a2016-03-23 11:14:14 +0530663 ALOGD("setPhoneState() state %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530664 // store previous phone state for management of sonification strategy below
Sharad Sangle4509cef2015-08-19 20:47:12 +0530665 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Sharad Sangle36781612015-05-28 16:15:16 +0530666 int oldState = mEngine->getPhoneState();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700667
Sharad Sangle36781612015-05-28 16:15:16 +0530668 if (mEngine->setPhoneState(state) != NO_ERROR) {
669 ALOGW("setPhoneState() invalid or same state %d", state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700670 return;
671 }
Sharad Sangle36781612015-05-28 16:15:16 +0530672 /// Opens: can these line be executed after the switch of volume curves???
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700673 // if leaving call state, handle special case of active streams
674 // pertaining to sonification strategy see handleIncallSonification()
675 if (isInCall()) {
676 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530677 for (size_t j = 0; j < mOutputs.size(); j++) {
678 audio_io_handle_t curOutput = mOutputs.keyAt(j);
679 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
680 if (stream == AUDIO_STREAM_PATCH) {
681 continue;
682 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530683 handleIncallSonification((audio_stream_type_t)stream, false, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +0530684 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700685 }
Sharad Sangle36781612015-05-28 16:15:16 +0530686
687 // force reevaluating accessibility routing when call starts
688 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700689 }
690
Sharad Sangle36781612015-05-28 16:15:16 +0530691 /**
692 * Switching to or from incall state or switching between telephony and VoIP lead to force
693 * routing command.
694 */
695 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
696 || (is_state_in_call(state) && (state != oldState)));
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700697
698 // check for device and output changes triggered by new phone state
699 checkA2dpSuspend();
700 checkOutputForAllStrategies();
701 updateDevicesAndOutputs();
702
Sharad Sangle36781612015-05-28 16:15:16 +0530703 sp<SwAudioOutputDescriptor> hwOutputDesc = mPrimaryOutput;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530704#ifdef VOICE_CONCURRENCY
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530705 char propValue[PROPERTY_VALUE_MAX];
706 bool prop_playback_enabled = false, prop_rec_enabled=false, prop_voip_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700707
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530708 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
709 prop_playback_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
710 }
711
712 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
713 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
714 }
715
716 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
717 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
718 }
719
Dhananjay Kumar9125b2a2015-12-01 16:24:09 +0530720 if ((AUDIO_MODE_IN_CALL != oldState) && (AUDIO_MODE_IN_CALL == state)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530721 ALOGD("voice_conc:Entering to call mode oldState :: %d state::%d ",
722 oldState, state);
Dhananjay Kumar9125b2a2015-12-01 16:24:09 +0530723 mvoice_call_state = state;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530724 if (prop_rec_enabled) {
725 //Close all active inputs
726 audio_io_handle_t activeInput = mInputs.getActiveInput();
727 if (activeInput != 0) {
728 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
729 switch(activeDesc->mInputSource) {
730 case AUDIO_SOURCE_VOICE_UPLINK:
731 case AUDIO_SOURCE_VOICE_DOWNLINK:
732 case AUDIO_SOURCE_VOICE_CALL:
733 ALOGD("voice_conc:FOUND active input during call active: %d",activeDesc->mInputSource);
734 break;
735
736 case AUDIO_SOURCE_VOICE_COMMUNICATION:
737 if(prop_voip_enabled) {
738 ALOGD("voice_conc:CLOSING VoIP input source on call setup :%d ",activeDesc->mInputSource);
739 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
740 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
741 }
742 break;
743
744 default:
745 ALOGD("voice_conc:CLOSING input on call setup for inputSource: %d",activeDesc->mInputSource);
746 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
747 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
748 break;
749 }
750 }
751 } else if (prop_voip_enabled) {
752 audio_io_handle_t activeInput = mInputs.getActiveInput();
753 if (activeInput != 0) {
754 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
755 if (AUDIO_SOURCE_VOICE_COMMUNICATION == activeDesc->mInputSource) {
756 ALOGD("voice_conc:CLOSING VoIP on call setup : %d",activeDesc->mInputSource);
757 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
758 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
759 }
760 }
761 }
762 if (prop_playback_enabled) {
763 // Move tracks associated to this strategy from previous output to new output
764 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
765 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
766 if (i == AUDIO_STREAM_PATCH) {
767 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
768 continue;
769 }
770 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
771 if ((AUDIO_STREAM_MUSIC == i) ||
772 (AUDIO_STREAM_VOICE_CALL == i) ) {
773 ALOGD("voice_conc:Invalidate stream type %d", i);
774 mpClientInterface->invalidateStream((audio_stream_type_t)i);
775 }
776 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
777 ALOGD("voice_conc:Invalidate stream type %d", i);
778 mpClientInterface->invalidateStream((audio_stream_type_t)i);
779 }
780 }
781 }
782
783 for (size_t i = 0; i < mOutputs.size(); i++) {
784 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
785 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
786 ALOGD("voice_conc:ouput desc / profile is NULL");
787 continue;
788 }
789
790 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
791 if (((!outputDesc->isDuplicated() &&outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY))
792 && prop_playback_enabled) {
793 ALOGD("voice_conc:calling suspendOutput on call mode for primary output");
794 mpClientInterface->suspendOutput(mOutputs.keyAt(i));
795 } //Close compress all sessions
796 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
797 && prop_playback_enabled) {
798 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
799 closeOutput(mOutputs.keyAt(i));
800 }
801 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_VOIP_RX)
802 && prop_voip_enabled) {
803 ALOGD("voice_conc:calling closeOutput on call mode for DIRECT output");
804 closeOutput(mOutputs.keyAt(i));
805 }
806 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
807 if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)
808 && prop_playback_enabled) {
809 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
810 closeOutput(mOutputs.keyAt(i));
811 }
812 }
813 }
814 }
815
816 if ((AUDIO_MODE_IN_CALL == oldState || AUDIO_MODE_IN_COMMUNICATION == oldState) &&
817 (AUDIO_MODE_NORMAL == state) && prop_playback_enabled && mvoice_call_state) {
818 ALOGD("voice_conc:EXITING from call mode oldState :: %d state::%d \n",oldState, state);
819 mvoice_call_state = 0;
820 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
821 //restore PCM (deep-buffer) output after call termination
822 for (size_t i = 0; i < mOutputs.size(); i++) {
823 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
824 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
825 ALOGD("voice_conc:ouput desc / profile is NULL");
826 continue;
827 }
828 if (!outputDesc->isDuplicated() && outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
829 ALOGD("voice_conc:calling restoreOutput after call mode for primary output");
830 mpClientInterface->restoreOutput(mOutputs.keyAt(i));
831 }
832 }
833 }
834 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
835 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
836 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
837 if (i == AUDIO_STREAM_PATCH) {
838 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
839 continue;
840 }
841 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
842 if ((AUDIO_STREAM_MUSIC == i) ||
843 (AUDIO_STREAM_VOICE_CALL == i) ) {
844 mpClientInterface->invalidateStream((audio_stream_type_t)i);
845 }
846 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
847 mpClientInterface->invalidateStream((audio_stream_type_t)i);
848 }
849 }
850 }
851
852#endif
853#ifdef RECORD_PLAY_CONCURRENCY
854 char recConcPropValue[PROPERTY_VALUE_MAX];
855 bool prop_rec_play_enabled = false;
856
857 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
858 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
859 }
860 if (prop_rec_play_enabled) {
861 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
862 ALOGD("phone state changed to MODE_IN_COMM invlaidating music and voice streams");
863 // call invalidate for voice streams, so that it can use deepbuffer with VoIP out device from HAL
864 mpClientInterface->invalidateStream(AUDIO_STREAM_VOICE_CALL);
865 // call invalidate for music, so that compress will fallback to deep-buffer with VoIP out device
866 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
867
868 // close compress output to make sure session will be closed before timeout(60sec)
869 for (size_t i = 0; i < mOutputs.size(); i++) {
870
871 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
872 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
873 ALOGD("ouput desc / profile is NULL");
874 continue;
875 }
876
877 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
878 ALOGD("calling closeOutput on call mode for COMPRESS output");
879 closeOutput(mOutputs.keyAt(i));
880 }
881 }
882 } else if ((oldState == AUDIO_MODE_IN_COMMUNICATION) &&
883 (mEngine->getPhoneState() == AUDIO_MODE_NORMAL)) {
884 // call invalidate for music so that music can fallback to compress
885 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
886 }
887 }
888#endif
889 mPrevPhoneState = oldState;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700890 int delayMs = 0;
891 if (isStateInCall(state)) {
892 nsecs_t sysTime = systemTime();
893 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530894 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700895 // mute media and sonification strategies and delay device switch by the largest
896 // latency of any output where either strategy is active.
897 // This avoid sending the ring tone or music tail into the earpiece or headset.
Sharad Sangle36781612015-05-28 16:15:16 +0530898 if ((isStrategyActive(desc, STRATEGY_MEDIA,
899 SONIFICATION_HEADSET_MUSIC_DELAY,
900 sysTime) ||
901 isStrategyActive(desc, STRATEGY_SONIFICATION,
902 SONIFICATION_HEADSET_MUSIC_DELAY,
903 sysTime)) &&
904 (delayMs < (int)desc->latency()*2)) {
905 delayMs = desc->latency()*2;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700906 }
Sharad Sangle36781612015-05-28 16:15:16 +0530907 setStrategyMute(STRATEGY_MEDIA, true, desc);
908 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700909 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Sharad Sangle36781612015-05-28 16:15:16 +0530910 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
911 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700912 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
913 }
914 }
915
Sharad Sangle36781612015-05-28 16:15:16 +0530916 if (hasPrimaryOutput()) {
917 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
918 // the device returned is not necessarily reachable via this output
919 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
920 // force routing command to audio hardware when ending call
921 // even if no device change is needed
922 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
923 rxDevice = mPrimaryOutput->device();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700924 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700925
Sharad Sangle36781612015-05-28 16:15:16 +0530926 if (state == AUDIO_MODE_IN_CALL) {
927 updateCallRouting(rxDevice, delayMs);
928 } else if (oldState == AUDIO_MODE_IN_CALL) {
929 if (mCallRxPatch != 0) {
930 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
931 mCallRxPatch.clear();
932 }
933 if (mCallTxPatch != 0) {
934 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
935 mCallTxPatch.clear();
936 }
937 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
938 } else {
939 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700940 }
941 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530942 //update device for all non-primary outputs
943 for (size_t i = 0; i < mOutputs.size(); i++) {
944 audio_io_handle_t output = mOutputs.keyAt(i);
945 if (output != mPrimaryOutput->mIoHandle) {
946 newDevice = getNewOutputDevice(mOutputs.valueFor(output), false /*fromCache*/);
947 setOutputDevice(mOutputs.valueFor(output), newDevice, (newDevice != AUDIO_DEVICE_NONE));
948 }
949 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700950 // if entering in call state, handle special case of active streams
951 // pertaining to sonification strategy see handleIncallSonification()
952 if (isStateInCall(state)) {
953 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530954 for (size_t j = 0; j < mOutputs.size(); j++) {
955 audio_io_handle_t curOutput = mOutputs.keyAt(j);
956 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
957 if (stream == AUDIO_STREAM_PATCH) {
958 continue;
959 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530960 handleIncallSonification((audio_stream_type_t)stream, true, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +0530961 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700962 }
963 }
964
965 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
966 if (state == AUDIO_MODE_RINGTONE &&
967 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
968 mLimitRingtoneVolume = true;
969 } else {
970 mLimitRingtoneVolume = false;
971 }
972}
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +0530973
974void AudioPolicyManagerCustom::setForceUse(audio_policy_force_use_t usage,
975 audio_policy_forced_cfg_t config)
976{
Ashish Jain40cab3a2016-03-23 11:14:14 +0530977 ALOGD("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +0530978
Ashish Jain40cab3a2016-03-23 11:14:14 +0530979 audio_policy_forced_cfg_t originalConfig = mEngine->getForceUse(usage);
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +0530980 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
981 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
982 return;
983 }
984 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
985 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
986 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
987
988 // check for device and output changes triggered by new force usage
989 checkA2dpSuspend();
990 checkOutputForAllStrategies();
991 updateDevicesAndOutputs();
Ashish Jain40cab3a2016-03-23 11:14:14 +0530992
993 // Did surround forced use change?
994 if ((usage == AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND)
995 && (originalConfig != config)) {
996 const char *device_address = "";
997 // Is it currently connected? If so then cycle the connection
998 // so that the supported surround formats will be reloaded.
999 //
1000 // FIXME As S/PDIF is not a removable device we have to handle this differently.
1001 // Probably by updating the device descriptor directly and manually
1002 // tearing down active playback on S/PDIF
1003 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_HDMI, device_address) ==
1004 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
1005 // Disconnect and reconnect output devices so that the surround
1006 // encodings can be updated.
1007 const char *device_name = "";
1008 // disconnect
1009 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI,
1010 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1011 device_address, device_name);
1012 // reconnect
1013 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI,
1014 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1015 device_address, device_name);
1016 }
1017 }
1018
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301019 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
1020 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
1021 updateCallRouting(newDevice);
1022 }
1023 // Use reverse loop to make sure any low latency usecases (generally tones)
1024 // are not routed before non LL usecases (generally music).
1025 // We can safely assume that LL output would always have lower index,
1026 // and use this work-around to avoid routing of output with music stream
1027 // from the context of short lived LL output.
1028 // Note: in case output's share backend(HAL sharing is implicit) all outputs
1029 // gets routing update while processing first output itself.
1030 for (size_t i = mOutputs.size(); i > 0; i--) {
1031 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i-1);
1032 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
1033 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || outputDesc != mPrimaryOutput) {
1034 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
1035 }
1036 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
1037 applyStreamVolumes(outputDesc, newDevice, 0, true);
1038 }
1039 }
1040
1041 audio_io_handle_t activeInput = mInputs.getActiveInput();
1042 if (activeInput != 0) {
Ashish Jain40cab3a2016-03-23 11:14:14 +05301043 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
1044 audio_devices_t newDevice = getNewInputDevice(activeInput);
1045 // Force new input selection if the new device can not be reached via current input
1046 if (activeDesc->mProfile->getSupportedDevices().types() & (newDevice & ~AUDIO_DEVICE_BIT_IN)) {
1047 setInputDevice(activeInput, newDevice);
1048 } else {
1049 closeInput(activeInput);
1050 }
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301051 }
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +05301052}
1053
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301054status_t AudioPolicyManagerCustom::stopSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301055 audio_stream_type_t stream,
1056 bool forceDeviceUpdate)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001057{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301058 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1059 ALOGW("stopSource() invalid stream %d", stream);
1060 return INVALID_OPERATION;
1061 }
Sharad Sangle36781612015-05-28 16:15:16 +05301062 // always handle stream stop, check which stream type is stopping
1063 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001064
Sharad Sangle36781612015-05-28 16:15:16 +05301065 // handle special case for sonification while in call
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001066 if (isInCall()) {
Sharad Sangle36781612015-05-28 16:15:16 +05301067 if (outputDesc->isDuplicated()) {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301068 handleIncallSonification(stream, false, false, outputDesc->subOutput1()->mIoHandle);
1069 handleIncallSonification(stream, false, false, outputDesc->subOutput2()->mIoHandle);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001070 }
Sharad Sangle36781612015-05-28 16:15:16 +05301071 handleIncallSonification(stream, false, false, outputDesc->mIoHandle);
1072 }
1073
1074 if (outputDesc->mRefCount[stream] > 0) {
1075 // decrement usage count of this stream on the output
1076 outputDesc->changeRefCount(stream, -1);
1077
1078 // store time at which the stream was stopped - see isStreamActive()
1079 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
1080 outputDesc->mStopTime[stream] = systemTime();
Zhou Song5dcddc92015-09-21 14:36:57 +08001081 audio_devices_t prevDevice = outputDesc->device();
Sharad Sangle36781612015-05-28 16:15:16 +05301082 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
1083 // delay the device switch by twice the latency because stopOutput() is executed when
1084 // the track stop() command is received and at that time the audio track buffer can
1085 // still contain data that needs to be drained. The latency only covers the audio HAL
1086 // and kernel buffers. Also the latency does not always include additional delay in the
1087 // audio path (audio DSP, CODEC ...)
1088 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
1089
1090 // force restoring the device selection on other active outputs if it differs from the
1091 // one being selected for this output
1092 for (size_t i = 0; i < mOutputs.size(); i++) {
1093 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1094 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1095 if (desc != outputDesc &&
1096 desc->isActive() &&
1097 outputDesc->sharesHwModuleWith(desc) &&
1098 (newDevice != desc->device())) {
Sharad Sangle4509cef2015-08-19 20:47:12 +05301099 audio_devices_t dev = getNewOutputDevice(mOutputs.valueFor(curOutput), false /*fromCache*/);
Zhou Song5dcddc92015-09-21 14:36:57 +08001100 uint32_t delayMs;
1101 if (dev == prevDevice) {
1102 delayMs = 0;
1103 } else {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301104 delayMs = outputDesc->latency()*2;
Zhou Song5dcddc92015-09-21 14:36:57 +08001105 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301106 setOutputDevice(desc,
1107 dev,
Sharad Sangle36781612015-05-28 16:15:16 +05301108 true,
Zhou Song5dcddc92015-09-21 14:36:57 +08001109 delayMs);
Sharad Sangle36781612015-05-28 16:15:16 +05301110 }
1111 }
1112 // update the outputs if stopping one with a stream that can affect notification routing
1113 handleNotificationRoutingForStream(stream);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001114 }
Sharad Sangle36781612015-05-28 16:15:16 +05301115 return NO_ERROR;
1116 } else {
1117 ALOGW("stopOutput() refcount is already 0");
1118 return INVALID_OPERATION;
1119 }
1120}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001121
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301122status_t AudioPolicyManagerCustom::startSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301123 audio_stream_type_t stream,
1124 audio_devices_t device,
1125 uint32_t *delayMs)
1126{
1127 // cannot start playback of STREAM_TTS if any other output is being used
1128 uint32_t beaconMuteLatency = 0;
1129
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301130 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1131 ALOGW("startSource() invalid stream %d", stream);
1132 return INVALID_OPERATION;
1133 }
1134
Sharad Sangle36781612015-05-28 16:15:16 +05301135 *delayMs = 0;
1136 if (stream == AUDIO_STREAM_TTS) {
1137 ALOGV("\t found BEACON stream");
Ashish Jain40cab3a2016-03-23 11:14:14 +05301138 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301139 return INVALID_OPERATION;
1140 } else {
1141 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001142 }
Sharad Sangle36781612015-05-28 16:15:16 +05301143 } else {
1144 // some playback other than beacon starts
1145 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1146 }
1147
Ashish Jain40cab3a2016-03-23 11:14:14 +05301148 // check active before incrementing usage count
1149 bool force = !outputDesc->isActive();
1150
Sharad Sangle36781612015-05-28 16:15:16 +05301151 // increment usage count for this stream on the requested output:
1152 // NOTE that the usage count is the same for duplicated output and hardware output which is
1153 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1154 outputDesc->changeRefCount(stream, 1);
1155
1156 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
1157 // starting an output being rerouted?
1158 if (device == AUDIO_DEVICE_NONE) {
1159 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001160 }
Sharad Sangle36781612015-05-28 16:15:16 +05301161 routing_strategy strategy = getStrategy(stream);
1162 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
1163 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1164 (beaconMuteLatency > 0);
1165 uint32_t waitMs = beaconMuteLatency;
Sharad Sangle36781612015-05-28 16:15:16 +05301166 for (size_t i = 0; i < mOutputs.size(); i++) {
1167 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1168 if (desc != outputDesc) {
1169 // force a device change if any other output is managed by the same hw
1170 // module and has a current device selection that differs from selected device.
1171 // In this case, the audio HAL must receive the new device selection so that it can
1172 // change the device currently selected by the other active output.
1173 if (outputDesc->sharesHwModuleWith(desc) &&
1174 desc->device() != device) {
1175 force = true;
1176 }
1177 // wait for audio on other active outputs to be presented when starting
1178 // a notification so that audio focus effect can propagate, or that a mute/unmute
1179 // event occurred for beacon
1180 uint32_t latency = desc->latency();
1181 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1182 waitMs = latency;
1183 }
1184 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001185 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05301186 uint32_t muteWaitMs;
1187 muteWaitMs = setOutputDevice(outputDesc, device, force);
Sharad Sangle36781612015-05-28 16:15:16 +05301188
1189 // handle special case for sonification while in call
1190 if (isInCall()) {
1191 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001192 }
Sharad Sangle36781612015-05-28 16:15:16 +05301193
1194 // apply volume rules for current stream and device if necessary
1195 checkAndSetVolume(stream,
Ashish Jain40cab3a2016-03-23 11:14:14 +05301196 mVolumeCurves->getVolumeIndex(stream, device),
Sharad Sangle36781612015-05-28 16:15:16 +05301197 outputDesc,
1198 device);
1199
1200 // update the outputs if starting an output with a stream that can affect notification
1201 // routing
1202 handleNotificationRoutingForStream(stream);
1203
1204 // force reevaluating accessibility routing when ringtone or alarm starts
1205 if (strategy == STRATEGY_SONIFICATION) {
1206 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1207 }
1208 }
1209 else {
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001210 // handle special case for sonification while in call
1211 if (isInCall()) {
1212 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Sharad Sangle36781612015-05-28 16:15:16 +05301213 }
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001214 }
Sharad Sangle36781612015-05-28 16:15:16 +05301215 return NO_ERROR;
1216}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001217
Sharad Sangle36781612015-05-28 16:15:16 +05301218void AudioPolicyManagerCustom::handleIncallSonification(audio_stream_type_t stream,
1219 bool starting, bool stateChange,
1220 audio_io_handle_t output)
1221{
1222 if(!hasPrimaryOutput()) {
1223 return;
1224 }
1225 // no action needed for AUDIO_STREAM_PATCH stream type, it's for internal flinger tracks
1226 if (stream == AUDIO_STREAM_PATCH) {
1227 return;
1228 }
1229 // if the stream pertains to sonification strategy and we are in call we must
1230 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
1231 // in the device used for phone strategy and play the tone if the selected device does not
1232 // interfere with the device used for phone strategy
1233 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
1234 // many times as there are active tracks on the output
1235 const routing_strategy stream_strategy = getStrategy(stream);
1236 if ((stream_strategy == STRATEGY_SONIFICATION) ||
1237 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
1238 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
1239 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
1240 stream, starting, outputDesc->mDevice, stateChange);
1241 if (outputDesc->mRefCount[stream]) {
1242 int muteCount = 1;
1243 if (stateChange) {
1244 muteCount = outputDesc->mRefCount[stream];
1245 }
1246 if (audio_is_low_visibility(stream)) {
1247 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
1248 for (int i = 0; i < muteCount; i++) {
1249 setStreamMute(stream, starting, outputDesc);
1250 }
1251 } else {
1252 ALOGV("handleIncallSonification() high visibility");
1253 if (outputDesc->device() &
1254 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
1255 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
1256 for (int i = 0; i < muteCount; i++) {
1257 setStreamMute(stream, starting, outputDesc);
1258 }
1259 }
1260 if (starting) {
1261 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
1262 AUDIO_STREAM_VOICE_CALL);
1263 } else {
1264 mpClientInterface->stopTone();
1265 }
1266 }
1267 }
1268 }
1269}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001270
Sharad Sangle36781612015-05-28 16:15:16 +05301271void AudioPolicyManagerCustom::handleNotificationRoutingForStream(audio_stream_type_t stream) {
1272 switch(stream) {
1273 case AUDIO_STREAM_MUSIC:
1274 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
1275 updateDevicesAndOutputs();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001276 break;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001277 default:
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001278 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001279 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001280}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001281
Sharad Sangle36781612015-05-28 16:15:16 +05301282status_t AudioPolicyManagerCustom::checkAndSetVolume(audio_stream_type_t stream,
1283 int index,
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301284 const sp<AudioOutputDescriptor>& outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301285 audio_devices_t device,
1286 int delayMs, bool force)
1287{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301288 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1289 ALOGW("checkAndSetVolume() invalid stream %d", stream);
1290 return INVALID_OPERATION;
1291 }
Sharad Sangle36781612015-05-28 16:15:16 +05301292 // do not change actual stream volume if the stream is muted
1293 if (outputDesc->mMuteCount[stream] != 0) {
1294 ALOGVV("checkAndSetVolume() stream %d muted count %d",
1295 stream, outputDesc->mMuteCount[stream]);
1296 return NO_ERROR;
1297 }
1298 audio_policy_forced_cfg_t forceUseForComm =
1299 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
1300 // do not change in call volume if bluetooth is connected and vice versa
1301 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
1302 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
1303 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
1304 stream, forceUseForComm);
1305 return INVALID_OPERATION;
1306 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001307
Sharad Sangle36781612015-05-28 16:15:16 +05301308 if (device == AUDIO_DEVICE_NONE) {
1309 device = outputDesc->device();
1310 }
1311
1312 float volumeDb = computeVolume(stream, index, device);
1313 if (outputDesc->isFixedVolume(device)) {
1314 volumeDb = 0.0f;
1315 }
1316
1317 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
1318
1319 if (stream == AUDIO_STREAM_VOICE_CALL ||
1320 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
1321 float voiceVolume;
1322 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
1323 if (stream == AUDIO_STREAM_VOICE_CALL) {
Ashish Jain40cab3a2016-03-23 11:14:14 +05301324 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Sharad Sangle36781612015-05-28 16:15:16 +05301325 } else {
1326 voiceVolume = 1.0;
1327 }
1328
1329 if (voiceVolume != mLastVoiceVolume && ((outputDesc == mPrimaryOutput) ||
1330 isDirectOutput(outputDesc->mIoHandle) || device & AUDIO_DEVICE_OUT_ALL_USB)) {
1331 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
1332 mLastVoiceVolume = voiceVolume;
1333 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301334#ifdef FM_POWER_OPT
1335 } else if (stream == AUDIO_STREAM_MUSIC && hasPrimaryOutput() &&
Haynes Mathew George603ae9b2015-12-21 17:23:59 -08001336 outputDesc == mPrimaryOutput && mFMIsActive) {
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001337 /* Avoid unnecessary set_parameter calls as it puts the primary
1338 outputs FastMixer in HOT_IDLE leading to breaks in audio */
1339 if (volumeDb != mPrevFMVolumeDb) {
1340 mPrevFMVolumeDb = volumeDb;
1341 AudioParameter param = AudioParameter();
1342 param.addFloat(String8("fm_volume"), Volume::DbToAmpl(volumeDb));
Mingming Yin1433cc62016-01-04 14:58:00 -08001343 //Double delayMs to avoid sound burst while device switch.
1344 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString(), delayMs*2);
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001345 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301346#endif /* FM_POWER_OPT end */
Sharad Sangle36781612015-05-28 16:15:16 +05301347 }
1348
1349 return NO_ERROR;
1350}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001351
Sharad Sangle36781612015-05-28 16:15:16 +05301352bool AudioPolicyManagerCustom::isDirectOutput(audio_io_handle_t output) {
1353 for (size_t i = 0; i < mOutputs.size(); i++) {
1354 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1355 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1356 if ((curOutput == output) && (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
1357 return true;
1358 }
1359 }
1360 return false;
1361}
vivek mehta0ea887a2015-08-26 14:01:20 -07001362
1363status_t AudioPolicyManagerCustom::getOutputForAttr(const audio_attributes_t *attr,
1364 audio_io_handle_t *output,
1365 audio_session_t session,
1366 audio_stream_type_t *stream,
1367 uid_t uid,
1368 uint32_t samplingRate,
1369 audio_format_t format,
1370 audio_channel_mask_t channelMask,
1371 audio_output_flags_t flags,
1372 audio_port_handle_t selectedDeviceId,
1373 const audio_offload_info_t *offloadInfo)
1374{
1375 audio_offload_info_t tOffloadInfo = AUDIO_INFO_INITIALIZER;
1376
Alexy Joseph0ac09032015-10-16 15:57:53 -07001377 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
1378 bool pcmOffloadEnabled = false;
1379
1380 if (offloadDisabled) {
1381 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
1382 }
1383
1384 //read track offload property only if the global offload switch is off.
1385 if (!offloadDisabled) {
1386 pcmOffloadEnabled = property_get_bool("audio.offload.track.enable", false);
1387 }
vivek mehta0ea887a2015-08-26 14:01:20 -07001388
1389 if (offloadInfo == NULL && pcmOffloadEnabled) {
1390 tOffloadInfo.sample_rate = samplingRate;
1391 tOffloadInfo.channel_mask = channelMask;
1392 tOffloadInfo.format = format;
1393 tOffloadInfo.stream_type = *stream;
1394 tOffloadInfo.bit_width = 16; //hard coded for PCM_16
1395 if (attr != NULL) {
1396 ALOGV("found attribute .. setting usage %d ", attr->usage);
1397 tOffloadInfo.usage = attr->usage;
1398 } else {
Alexy Joseph0ac09032015-10-16 15:57:53 -07001399 ALOGI("%s:: attribute is NULL .. no usage set", __func__);
vivek mehta0ea887a2015-08-26 14:01:20 -07001400 }
1401 offloadInfo = &tOffloadInfo;
1402 }
1403
1404 return AudioPolicyManager::getOutputForAttr(attr, output, session, stream,
1405 (uid_t)uid, (uint32_t)samplingRate,
1406 format, (audio_channel_mask_t)channelMask,
1407 flags, (audio_port_handle_t)selectedDeviceId,
1408 offloadInfo);
1409}
1410
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001411audio_io_handle_t AudioPolicyManagerCustom::getOutputForDevice(
1412 audio_devices_t device,
Sharad Sangle36781612015-05-28 16:15:16 +05301413 audio_session_t session __unused,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001414 audio_stream_type_t stream,
1415 uint32_t samplingRate,
1416 audio_format_t format,
1417 audio_channel_mask_t channelMask,
1418 audio_output_flags_t flags,
1419 const audio_offload_info_t *offloadInfo)
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001420{
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001421 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001422 status_t status;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001423
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001424#ifdef AUDIO_POLICY_TEST
1425 if (mCurOutput != 0) {
1426 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1427 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1428
1429 if (mTestOutputs[mCurOutput] == 0) {
1430 ALOGV("getOutput() opening test output");
Sharad Sangle36781612015-05-28 16:15:16 +05301431 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
1432 mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001433 outputDesc->mDevice = mTestDevice;
1434 outputDesc->mLatency = mTestLatencyMs;
1435 outputDesc->mFlags =
1436 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
1437 outputDesc->mRefCount[stream] = 0;
1438 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1439 config.sample_rate = mTestSamplingRate;
1440 config.channel_mask = mTestChannels;
1441 config.format = mTestFormat;
1442 if (offloadInfo != NULL) {
1443 config.offload_info = *offloadInfo;
1444 }
1445 status = mpClientInterface->openOutput(0,
1446 &mTestOutputs[mCurOutput],
1447 &config,
1448 &outputDesc->mDevice,
1449 String8(""),
1450 &outputDesc->mLatency,
1451 outputDesc->mFlags);
1452 if (status == NO_ERROR) {
1453 outputDesc->mSamplingRate = config.sample_rate;
1454 outputDesc->mFormat = config.format;
1455 outputDesc->mChannelMask = config.channel_mask;
1456 AudioParameter outputCmd = AudioParameter();
1457 outputCmd.addInt(String8("set_id"),mCurOutput);
1458 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1459 addOutput(mTestOutputs[mCurOutput], outputDesc);
1460 }
1461 }
1462 return mTestOutputs[mCurOutput];
1463 }
1464#endif //AUDIO_POLICY_TEST
Sharad Sangle36781612015-05-28 16:15:16 +05301465 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1466 (stream != AUDIO_STREAM_MUSIC)) {
1467 // compress should not be used for non-music streams
1468 ALOGE("Offloading only allowed with music stream");
1469 return 0;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301470 }
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301471
1472 if ((stream == AUDIO_STREAM_VOICE_CALL) &&
1473 (channelMask == 1) &&
1474 (samplingRate == 8000 || samplingRate == 16000)) {
1475 // Allow Voip direct output only if:
1476 // audio mode is MODE_IN_COMMUNCATION; AND
1477 // voip output is not opened already; AND
1478 // requested sample rate matches with that of voip input stream (if opened already)
1479 int value = 0;
1480 uint32_t mode = 0, voipOutCount = 1, voipSampleRate = 1;
1481 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1482 String8("audio_mode"));
1483 AudioParameter result = AudioParameter(valueStr);
1484 if (result.getInt(String8("audio_mode"), value) == NO_ERROR) {
1485 mode = value;
1486 }
1487
1488 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1489 String8("voip_out_stream_count"));
1490 result = AudioParameter(valueStr);
1491 if (result.getInt(String8("voip_out_stream_count"), value) == NO_ERROR) {
1492 voipOutCount = value;
1493 }
1494
1495 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1496 String8("voip_sample_rate"));
1497 result = AudioParameter(valueStr);
1498 if (result.getInt(String8("voip_sample_rate"), value) == NO_ERROR) {
1499 voipSampleRate = value;
1500 }
1501
1502 if ((mode == AUDIO_MODE_IN_COMMUNICATION) && (voipOutCount == 0) &&
1503 ((voipSampleRate == 0) || (voipSampleRate == samplingRate))) {
1504 if (audio_is_linear_pcm(format)) {
1505 char propValue[PROPERTY_VALUE_MAX] = {0};
1506 property_get("use.voice.path.for.pcm.voip", propValue, "0");
1507 bool voipPcmSysPropEnabled = !strncmp("true", propValue, sizeof("true"));
1508 if (voipPcmSysPropEnabled && (format == AUDIO_FORMAT_PCM_16_BIT)) {
Karthik Reddy Katta3fb9be72015-11-25 11:18:27 +05301509 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX |
1510 AUDIO_OUTPUT_FLAG_DIRECT);
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301511 ALOGD("Set VoIP and Direct output flags for PCM format");
1512 }
1513 }
1514 }
1515 }
1516
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301517#ifdef VOICE_CONCURRENCY
1518 char propValue[PROPERTY_VALUE_MAX];
1519 bool prop_play_enabled=false, prop_voip_enabled = false;
1520
1521 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1522 prop_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001523 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301524
1525 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1526 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1527 }
1528
1529 if (prop_play_enabled && mvoice_call_state) {
1530 //check if voice call is active / running in background
1531 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1532 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1533 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1534 {
1535 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1536 if(prop_voip_enabled) {
1537 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1538 flags );
1539 return 0;
1540 }
1541 }
1542 else {
1543 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
1544 ALOGD("voice_conc:IN call mode adding ULL flags .. flags: %x ", flags );
1545 flags = AUDIO_OUTPUT_FLAG_FAST;
1546 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
1547 if (AUDIO_STREAM_MUSIC == stream) {
1548 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1549 ALOGD("voice_conc:IN call mode adding deep-buffer flags %x ", flags );
1550 }
1551 else {
1552 flags = AUDIO_OUTPUT_FLAG_FAST;
1553 ALOGD("voice_conc:IN call mode adding fast flags %x ", flags );
1554 }
1555 }
1556 }
1557 }
1558 } else if (prop_voip_enabled && mvoice_call_state) {
1559 //check if voice call is active / running in background
1560 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1561 //return only ULL ouput
1562 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1563 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1564 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1565 {
1566 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1567 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1568 flags );
1569 return 0;
1570 }
1571 }
1572 }
1573#endif
1574#ifdef RECORD_PLAY_CONCURRENCY
1575 char recConcPropValue[PROPERTY_VALUE_MAX];
1576 bool prop_rec_play_enabled = false;
1577
1578 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
1579 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
1580 }
1581 if ((prop_rec_play_enabled) &&
1582 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
1583 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
1584 if (AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1585 // allow VoIP using voice path
1586 // Do nothing
1587 } else if((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1588 ALOGD("voice_conc:MODE_IN_COMM is setforcing deep buffer output for non ULL... flags: %x", flags);
1589 // use deep buffer path for all non ULL outputs
1590 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1591 }
1592 } else if ((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1593 ALOGD("voice_conc:Record mode is on forcing deep buffer output for non ULL... flags: %x ", flags);
1594 // use deep buffer path for all non ULL outputs
1595 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1596 }
1597 }
1598 if (prop_rec_play_enabled &&
1599 (stream == AUDIO_STREAM_ENFORCED_AUDIBLE)) {
1600 ALOGD("Record conc is on forcing ULL output for ENFORCED_AUDIBLE");
1601 flags = AUDIO_OUTPUT_FLAG_FAST;
1602 }
1603#endif
1604
Sharad Sangle4509cef2015-08-19 20:47:12 +05301605#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Sharad Sangle36781612015-05-28 16:15:16 +05301606 /*
1607 * WFD audio routes back to target speaker when starting a ringtone playback.
1608 * This is because primary output is reused for ringtone, so output device is
1609 * updated based on SONIFICATION strategy for both ringtone and music playback.
1610 * The same issue is not seen on remoted_submix HAL based WFD audio because
1611 * primary output is not reused and a new output is created for ringtone playback.
1612 * Issue is fixed by updating output flag to AUDIO_OUTPUT_FLAG_FAST when there is
1613 * a non-music stream playback on WFD, so primary output is not reused for ringtone.
1614 */
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001615 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
1616 if ((availableOutputDeviceTypes & AUDIO_DEVICE_OUT_PROXY)
1617 && (stream != AUDIO_STREAM_MUSIC)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301618 ALOGD("WFD audio: use OUTPUT_FLAG_FAST for non music stream. flags:%x", flags );
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001619 //For voip paths
1620 if(flags & AUDIO_OUTPUT_FLAG_DIRECT)
1621 flags = AUDIO_OUTPUT_FLAG_DIRECT;
1622 else //route every thing else to ULL path
1623 flags = AUDIO_OUTPUT_FLAG_FAST;
1624 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301625#endif
1626
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001627 // open a direct output if required by specified parameters
vivek mehta0ea887a2015-08-26 14:01:20 -07001628 // force direct flag if offload flag is set: offloading implies a direct output stream
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001629 // and all common behaviors are driven by checking only the direct flag
1630 // this should normally be set appropriately in the policy configuration file
1631 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1632 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1633 }
1634 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1635 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1636 }
vivek mehta0ea887a2015-08-26 14:01:20 -07001637
vivek mehta4b0d8192015-10-16 00:25:59 -07001638 bool forced_deep = false;
Sharad Sangle36781612015-05-28 16:15:16 +05301639 // only allow deep buffering for music stream type
1640 if (stream != AUDIO_STREAM_MUSIC) {
1641 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Sharad Sangle497aef82015-08-03 17:55:48 +05301642 } else if (/* stream == AUDIO_STREAM_MUSIC && */
1643 flags == AUDIO_OUTPUT_FLAG_NONE &&
1644 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
vivek mehtac984b992015-11-25 13:28:55 -08001645 forced_deep = true;
Sharad Sangle36781612015-05-28 16:15:16 +05301646 }
Sharad Sangle497aef82015-08-03 17:55:48 +05301647
Sharad Sangle36781612015-05-28 16:15:16 +05301648 if (stream == AUDIO_STREAM_TTS) {
1649 flags = AUDIO_OUTPUT_FLAG_TTS;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001650 }
1651
vivek mehta4b0d8192015-10-16 00:25:59 -07001652 // Do offload magic here
vivek mehtac984b992015-11-25 13:28:55 -08001653 if ((flags == AUDIO_OUTPUT_FLAG_NONE) &&
1654 (stream == AUDIO_STREAM_MUSIC) &&
1655 (offloadInfo != NULL) &&
vivek mehta4b0d8192015-10-16 00:25:59 -07001656 ((offloadInfo->usage == AUDIO_USAGE_MEDIA) || (offloadInfo->usage == AUDIO_USAGE_GAME))) {
vivek mehtac984b992015-11-25 13:28:55 -08001657 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DIRECT_PCM);
vivek mehta4b0d8192015-10-16 00:25:59 -07001658 ALOGD("AudioCustomHAL --> Force Direct Flag .. flag (0x%x)", flags);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301659 }
1660
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001661 sp<IOProfile> profile;
1662
1663 // skip direct output selection if the request can obviously be attached to a mixed output
1664 // and not explicitly requested
vivek mehtac984b992015-11-25 13:28:55 -08001665 if (((flags & (AUDIO_OUTPUT_FLAG_DIRECT|AUDIO_OUTPUT_FLAG_DIRECT_PCM)) == 0) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301666 audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX &&
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001667 audio_channel_count_from_out_mask(channelMask) <= 2) {
1668 goto non_direct_output;
1669 }
1670
1671 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1672 // creating an offloaded track and tearing it down immediately after start when audioflinger
1673 // detects there is an active non offloadable effect.
1674 // FIXME: We should check the audio session here but we do not have it in this context.
1675 // This may prevent offloading in rare situations where effects are left active by apps
1676 // in the background.
1677
Sharad Sangle36781612015-05-28 16:15:16 +05301678 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
Ashish Jain40cab3a2016-03-23 11:14:14 +05301679 !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001680 profile = getProfileForDirectOutput(device,
1681 samplingRate,
1682 format,
1683 channelMask,
1684 (audio_output_flags_t)flags);
1685 }
1686
1687 if (profile != 0) {
vivek mehtac984b992015-11-25 13:28:55 -08001688
1689 if (!(flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301690 (profile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT_PCM)) {
vivek mehtac984b992015-11-25 13:28:55 -08001691 ALOGI("got Direct_PCM without requesting ... reject ");
1692 profile = NULL;
1693 goto non_direct_output;
1694 }
1695
Sharad Sangle36781612015-05-28 16:15:16 +05301696 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001697
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001698 // if multiple concurrent offload decode is supported
1699 // do no check for reuse and also don't close previous output if its offload
1700 // previous output will be closed during track destruction
1701 if (!(property_get_bool("audio.offload.multiple.enabled", false) &&
1702 ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0))) {
1703 for (size_t i = 0; i < mOutputs.size(); i++) {
1704 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1705 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1706 outputDesc = desc;
1707 // reuse direct output if currently open and configured with same parameters
1708 if ((samplingRate == outputDesc->mSamplingRate) &&
Ashish Jain40cab3a2016-03-23 11:14:14 +05301709 audio_formats_match(format, outputDesc->mFormat) &&
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001710 (channelMask == outputDesc->mChannelMask)) {
1711 outputDesc->mDirectOpenCount++;
1712 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1713 return mOutputs.keyAt(i);
1714 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001715 }
1716 }
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001717 // close direct output if currently open and configured with different parameters
1718 if (outputDesc != NULL) {
1719 closeOutput(outputDesc->mIoHandle);
1720 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001721 }
Sharad Sangle36781612015-05-28 16:15:16 +05301722
1723 // if the selected profile is offloaded and no offload info was specified,
1724 // create a default one
1725 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
Ashish Jain40cab3a2016-03-23 11:14:14 +05301726 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
Sharad Sangle36781612015-05-28 16:15:16 +05301727 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1728 defaultOffloadInfo.sample_rate = samplingRate;
1729 defaultOffloadInfo.channel_mask = channelMask;
1730 defaultOffloadInfo.format = format;
1731 defaultOffloadInfo.stream_type = stream;
1732 defaultOffloadInfo.bit_rate = 0;
1733 defaultOffloadInfo.duration_us = -1;
1734 defaultOffloadInfo.has_video = true; // conservative
1735 defaultOffloadInfo.is_streaming = true; // likely
1736 offloadInfo = &defaultOffloadInfo;
1737 }
1738
1739 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001740 outputDesc->mDevice = device;
1741 outputDesc->mLatency = 0;
Sharad Sangle36781612015-05-28 16:15:16 +05301742 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001743 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1744 config.sample_rate = samplingRate;
1745 config.channel_mask = channelMask;
1746 config.format = format;
1747 if (offloadInfo != NULL) {
1748 config.offload_info = *offloadInfo;
1749 }
Sharad Sangle36781612015-05-28 16:15:16 +05301750 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001751 &output,
1752 &config,
1753 &outputDesc->mDevice,
1754 String8(""),
1755 &outputDesc->mLatency,
1756 outputDesc->mFlags);
1757
1758 // only accept an output with the requested parameters
1759 if (status != NO_ERROR ||
1760 (samplingRate != 0 && samplingRate != config.sample_rate) ||
Ashish Jain40cab3a2016-03-23 11:14:14 +05301761 (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) ||
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001762 (channelMask != 0 && channelMask != config.channel_mask)) {
1763 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1764 "format %d %d, channelMask %04x %04x", output, samplingRate,
1765 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1766 outputDesc->mChannelMask);
1767 if (output != AUDIO_IO_HANDLE_NONE) {
1768 mpClientInterface->closeOutput(output);
1769 }
Sharad Sangle36781612015-05-28 16:15:16 +05301770 // fall back to mixer output if possible when the direct output could not be open
Ashish Jain40cab3a2016-03-23 11:14:14 +05301771 if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) {
Sharad Sangle36781612015-05-28 16:15:16 +05301772 goto non_direct_output;
1773 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001774 return AUDIO_IO_HANDLE_NONE;
1775 }
1776 outputDesc->mSamplingRate = config.sample_rate;
1777 outputDesc->mChannelMask = config.channel_mask;
1778 outputDesc->mFormat = config.format;
1779 outputDesc->mRefCount[stream] = 0;
1780 outputDesc->mStopTime[stream] = 0;
1781 outputDesc->mDirectOpenCount = 1;
1782
1783 audio_io_handle_t srcOutput = getOutputForEffect();
1784 addOutput(output, outputDesc);
1785 audio_io_handle_t dstOutput = getOutputForEffect();
1786 if (dstOutput == output) {
Gao Jiedb057832015-11-12 08:51:22 +08001787#ifdef DOLBY_ENABLE
1788 status_t status = mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1789 if (status == NO_ERROR) {
1790 for (size_t i = 0; i < mEffects.size(); i++) {
1791 sp<EffectDescriptor> desc = mEffects.valueAt(i);
1792 if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX) {
1793 // update the mIo member of EffectDescriptor for the global effect
1794 ALOGV("%s updating mIo", __FUNCTION__);
1795 desc->mIo = dstOutput;
1796 }
1797 }
1798 } else {
1799 ALOGW("%s moveEffects from %d to %d failed", __FUNCTION__, srcOutput, dstOutput);
1800 }
1801#else // DOLBY_END
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001802 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
Gao Jiedb057832015-11-12 08:51:22 +08001803#endif // LINE_ADDED_BY_DOLBY
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001804 }
1805 mPreviousOutputs = mOutputs;
1806 ALOGV("getOutput() returns new direct output %d", output);
1807 mpClientInterface->onAudioPortListUpdate();
1808 return output;
1809 }
1810
1811non_direct_output:
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001812 // ignoring channel mask due to downmix capability in mixer
1813
1814 // open a non direct output
1815
1816 // for non direct outputs, only PCM is supported
1817 if (audio_is_linear_pcm(format)) {
vivek mehtac984b992015-11-25 13:28:55 -08001818 if (forced_deep) {
1819 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
1820 ALOGI("setting force DEEP buffer now ");
1821 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001822 // get which output is suitable for the specified stream. The actual
1823 // routing change will happen when startOutput() will be called
1824 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1825
1826 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1827 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1828 output = selectOutput(outputs, flags, format);
1829 }
1830 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1831 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1832
vivek mehta0ea887a2015-08-26 14:01:20 -07001833 ALOGV("getOutputForDevice() returns output %d", output);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001834
1835 return output;
1836}
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301837
1838status_t AudioPolicyManagerCustom::getInputForAttr(const audio_attributes_t *attr,
1839 audio_io_handle_t *input,
1840 audio_session_t session,
1841 uid_t uid,
1842 uint32_t samplingRate,
1843 audio_format_t format,
1844 audio_channel_mask_t channelMask,
1845 audio_input_flags_t flags,
1846 audio_port_handle_t selectedDeviceId,
1847 input_type_t *inputType)
1848{
Ashish Jain40cab3a2016-03-23 11:14:14 +05301849 audio_source_t inputSource;
1850 inputSource = attr->source;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301851#ifdef VOICE_CONCURRENCY
1852
1853 char propValue[PROPERTY_VALUE_MAX];
1854 bool prop_rec_enabled=false, prop_voip_enabled = false;
1855
1856 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
1857 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1858 }
1859
1860 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1861 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1862 }
1863
1864 if (prop_rec_enabled && mvoice_call_state) {
1865 //check if voice call is active / running in background
1866 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1867 //Need to block input request
1868 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1869 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1870 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1871 {
1872 switch(inputSource) {
1873 case AUDIO_SOURCE_VOICE_UPLINK:
1874 case AUDIO_SOURCE_VOICE_DOWNLINK:
1875 case AUDIO_SOURCE_VOICE_CALL:
1876 ALOGD("voice_conc:Creating input during incall mode for inputSource: %d",
1877 inputSource);
1878 break;
1879
1880 case AUDIO_SOURCE_VOICE_COMMUNICATION:
1881 if(prop_voip_enabled) {
1882 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1883 inputSource);
1884 return NO_INIT;
1885 }
1886 break;
1887 default:
1888 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1889 inputSource);
1890 return NO_INIT;
1891 }
1892 }
1893 }//check for VoIP flag
1894 else if(prop_voip_enabled && mvoice_call_state) {
1895 //check if voice call is active / running in background
1896 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1897 //Need to block input request
1898 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1899 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1900 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1901 {
1902 if(inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1903 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
1904 return NO_INIT;
1905 }
1906 }
1907 }
1908
1909#endif
1910
1911 return AudioPolicyManager::getInputForAttr(attr,
1912 input,
1913 session,
1914 uid,
1915 samplingRate,
1916 format,
1917 channelMask,
1918 flags,
1919 selectedDeviceId,
1920 inputType);
1921}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001922
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301923status_t AudioPolicyManagerCustom::startInput(audio_io_handle_t input,
1924 audio_session_t session)
1925{
1926 ALOGV("startInput() input %d", input);
1927 ssize_t index = mInputs.indexOfKey(input);
1928 if (index < 0) {
1929 ALOGW("startInput() unknown input %d", input);
1930 return BAD_VALUE;
1931 }
1932 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1933
Ashish Jain40cab3a2016-03-23 11:14:14 +05301934 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1935 if (audioSession == 0) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301936 ALOGW("startInput() unknown session %d on input %d", session, input);
1937 return BAD_VALUE;
1938 }
1939
1940 // virtual input devices are compatible with other input devices
1941 if (!is_virtual_input_device(inputDesc->mDevice)) {
1942
1943 // for a non-virtual input device, check if there is another (non-virtual) active input
1944 audio_io_handle_t activeInput = mInputs.getActiveInput();
1945 if (activeInput != 0 && activeInput != input) {
1946
1947 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1948 // otherwise the active input continues and the new input cannot be started.
1949 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Ashish Jain40cab3a2016-03-23 11:14:14 +05301950 if ((activeDesc->inputSource() == AUDIO_SOURCE_HOTWORD) &&
1951 !activeDesc->hasPreemptedSession(session)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301952 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
Ashish Jain40cab3a2016-03-23 11:14:14 +05301953 //FIXME: consider all active sessions
1954 AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions();
1955 audio_session_t activeSession = activeSessions.keyAt(0);
1956 SortedVector<audio_session_t> sessions =
1957 activeDesc->getPreemptedSessions();
1958 sessions.add(activeSession);
1959 inputDesc->setPreemptedSessions(sessions);
1960 stopInput(activeInput, activeSession);
1961 releaseInput(activeInput, activeSession);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301962 } else {
1963 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
1964 return INVALID_OPERATION;
1965 }
1966 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05301967 // Do not allow capture if an active voice call is using a software patch and
1968 // the call TX source device is on the same HW module.
1969 // FIXME: would be better to refine to only inputs whose profile connects to the
1970 // call TX device but this information is not in the audio patch
1971 if (mCallTxPatch != 0 &&
1972 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1973 return INVALID_OPERATION;
1974 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301975 }
1976
1977 // Routing?
1978 mInputRoutes.incRouteActivity(session);
1979#ifdef RECORD_PLAY_CONCURRENCY
1980 mIsInputRequestOnProgress = true;
1981
1982 char getPropValue[PROPERTY_VALUE_MAX];
1983 bool prop_rec_play_enabled = false;
1984
1985 if (property_get("rec.playback.conc.disabled", getPropValue, NULL)) {
1986 prop_rec_play_enabled = atoi(getPropValue) || !strncmp("true", getPropValue, 4);
1987 }
1988
1989 if ((prop_rec_play_enabled) &&(mInputs.activeInputsCount() == 0)){
1990 // send update to HAL on record playback concurrency
1991 AudioParameter param = AudioParameter();
1992 param.add(String8("rec_play_conc_on"), String8("true"));
1993 ALOGD("startInput() setParameters rec_play_conc is setting to ON ");
1994 mpClientInterface->setParameters(0, param.toString());
1995
1996 // Call invalidate to reset all opened non ULL audio tracks
1997 // Move tracks associated to this strategy from previous output to new output
1998 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
1999 // Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder)
Sharad Sangle4509cef2015-08-19 20:47:12 +05302000 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE && (i != AUDIO_STREAM_PATCH))) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302001 ALOGD("Invalidate on releaseInput for stream :: %d ", i);
2002 //FIXME see fixme on name change
2003 mpClientInterface->invalidateStream((audio_stream_type_t)i);
2004 }
2005 }
2006 // close compress tracks
2007 for (size_t i = 0; i < mOutputs.size(); i++) {
2008 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
2009 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
2010 ALOGD("ouput desc / profile is NULL");
2011 continue;
2012 }
2013 if (outputDesc->mProfile->mFlags
2014 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2015 // close compress sessions
2016 ALOGD("calling closeOutput on record conc for COMPRESS output");
2017 closeOutput(mOutputs.keyAt(i));
2018 }
2019 }
2020 }
2021#endif
2022
Ashish Jain40cab3a2016-03-23 11:14:14 +05302023 if (!inputDesc->isActive() || mInputRoutes.hasRouteChanged(session)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302024 // if input maps to a dynamic policy with an activity listener, notify of state change
2025 if ((inputDesc->mPolicyMix != NULL)
2026 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
2027 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
2028 MIX_STATE_MIXING);
2029 }
2030
2031 if (mInputs.activeInputsCount() == 0) {
2032 SoundTrigger::setCaptureState(true);
2033 }
2034 setInputDevice(input, getNewInputDevice(input), true /* force */);
2035
2036 // automatically enable the remote submix output when input is started if not
2037 // used by a policy mix of type MIX_TYPE_RECORDERS
2038 // For remote submix (a virtual device), we open only one input per capture request.
2039 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
2040 String8 address = String8("");
2041 if (inputDesc->mPolicyMix == NULL) {
2042 address = String8("0");
2043 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
2044 address = inputDesc->mPolicyMix->mRegistrationId;
2045 }
2046 if (address != "") {
2047 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2048 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2049 address, "remote-submix");
2050 }
2051 }
2052 }
2053
Ashish Jain40cab3a2016-03-23 11:14:14 +05302054 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302055
Ashish Jain40cab3a2016-03-23 11:14:14 +05302056 audioSession->changeActiveCount(1);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302057#ifdef RECORD_PLAY_CONCURRENCY
2058 mIsInputRequestOnProgress = false;
2059#endif
2060 return NO_ERROR;
2061}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08002062
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302063status_t AudioPolicyManagerCustom::stopInput(audio_io_handle_t input,
2064 audio_session_t session)
2065{
2066 status_t status;
2067 status = AudioPolicyManager::stopInput(input, session);
2068#ifdef RECORD_PLAY_CONCURRENCY
2069 char propValue[PROPERTY_VALUE_MAX];
2070 bool prop_rec_play_enabled = false;
2071
2072 if (property_get("rec.playback.conc.disabled", propValue, NULL)) {
2073 prop_rec_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
2074 }
2075
2076 if ((prop_rec_play_enabled) && (mInputs.activeInputsCount() == 0)) {
2077
2078 //send update to HAL on record playback concurrency
2079 AudioParameter param = AudioParameter();
2080 param.add(String8("rec_play_conc_on"), String8("false"));
2081 ALOGD("stopInput() setParameters rec_play_conc is setting to OFF ");
2082 mpClientInterface->setParameters(0, param.toString());
2083
2084 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
2085 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
2086 //Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder stop tone)
2087 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE) && (i != AUDIO_STREAM_PATCH)) {
2088 ALOGD(" Invalidate on stopInput for stream :: %d ", i);
2089 //FIXME see fixme on name change
2090 mpClientInterface->invalidateStream((audio_stream_type_t)i);
2091 }
2092 }
2093 }
2094#endif
2095 return status;
2096}
2097
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302098void AudioPolicyManagerCustom::closeAllInputs() {
2099 bool patchRemoved = false;
2100
Dhanalakshmi Siddanibfeca7f2015-10-08 15:17:11 +05302101 for(size_t input_index = mInputs.size(); input_index > 0; input_index--) {
2102 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index-1);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302103 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302104 if (patch_index >= 0) {
2105 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
Ashish Jain40cab3a2016-03-23 11:14:14 +05302106 status_t status;
2107 status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302108 mAudioPatches.removeItemsAt(patch_index);
2109 patchRemoved = true;
2110 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05302111 if ((inputDesc->getOpenRefCount() > 0) && inputDesc->isSoundTrigger()
2112 && (mInputs.size() == 1)) {
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302113 ALOGD("Do not close sound trigger input handle");
2114 } else {
Dhanalakshmi Siddanibfeca7f2015-10-08 15:17:11 +05302115 mpClientInterface->closeInput(mInputs.keyAt(input_index-1));
2116 mInputs.removeItem(mInputs.keyAt(input_index-1));
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302117 }
2118 }
Ashish Jain40cab3a2016-03-23 11:14:14 +05302119 mInputs.clear();
2120 SoundTrigger::setCaptureState(false);
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302121 nextAudioPortGeneration();
2122
2123 if (patchRemoved) {
2124 mpClientInterface->onAudioPatchListUpdate();
2125 }
2126}
2127
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302128AudioPolicyManagerCustom::AudioPolicyManagerCustom(AudioPolicyClientInterface *clientInterface)
Sharad Sangle4509cef2015-08-19 20:47:12 +05302129 : AudioPolicyManager(clientInterface),
2130 mHdmiAudioDisabled(false),
2131 mHdmiAudioEvent(false),
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08002132 mPrevPhoneState(0),
Haynes Mathew George603ae9b2015-12-21 17:23:59 -08002133 mPrevFMVolumeDb(0.0f),
2134 mFMIsActive(false)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302135{
Ashish Jain40cab3a2016-03-23 11:14:14 +05302136
2137 //TODO: Check the new logic to parse policy conf and update the below code
2138 // Need this when SSR encoding is enabled
Mingming Yin38ea08c2015-10-05 15:24:04 -07002139 char ssr_enabled[PROPERTY_VALUE_MAX] = {0};
2140 bool prop_ssr_enabled = false;
2141
2142 if (property_get("ro.qc.sdk.audio.ssr", ssr_enabled, NULL)) {
2143 prop_ssr_enabled = atoi(ssr_enabled) || !strncmp("true", ssr_enabled, 4);
2144 }
2145
2146 for (size_t i = 0; i < mHwModules.size(); i++) {
Ashish Jain40cab3a2016-03-23 11:14:14 +05302147 ALOGV("Hw module %zu", i);
Mingming Yin38ea08c2015-10-05 15:24:04 -07002148 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) {
2149 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Ashish Jain40cab3a2016-03-23 11:14:14 +05302150 AudioProfileVector profiles = inProfile->getAudioProfiles();
2151 for (size_t k = 0; k < profiles.size(); k++){
2152 ChannelsVector channels = profiles[k]->getChannels();
2153 for (size_t x = 0; x < channels.size(); x++) {
2154 audio_channel_mask_t channelMask = channels[x];
2155 ALOGV("Channel Mask %x size %zu", channelMask,
2156 channels.size());
2157 if (AUDIO_CHANNEL_IN_5POINT1 == channelMask) {
2158 if (!prop_ssr_enabled) {
2159 ALOGI("removing AUDIO_CHANNEL_IN_5POINT1 from"
2160 " input profile as SSR(surround sound record)"
2161 " is not supported on this chipset variant");
2162 channels.removeItemsAt(x, 1);
2163 ALOGV("Channel Mask size now %zu",
2164 channels.size());
2165 }
Mingming Yin38ea08c2015-10-05 15:24:04 -07002166 }
2167 }
2168 }
2169 }
2170 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302171#ifdef RECORD_PLAY_CONCURRENCY
2172 mIsInputRequestOnProgress = false;
2173#endif
2174
2175
2176#ifdef VOICE_CONCURRENCY
2177 mFallBackflag = getFallBackPath();
2178#endif
2179}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07002180}