blob: 6ba5fddd403e59dca46e4f39fe99b53de9f3afbb [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{
119 ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
120 device, state, device_address, device_name);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700121
122 // connect/disconnect only 1 device at a time
123 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
124
Sharad Sangle36781612015-05-28 16:15:16 +0530125 sp<DeviceDescriptor> devDesc =
126 mHwModules.getDeviceDescriptor(device, device_address, device_name);
127
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700128 // handle output devices
129 if (audio_is_output_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700130 SortedVector <audio_io_handle_t> outputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700131
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700132 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700133
134 // save a copy of the opened output descriptors before any output is opened or closed
135 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
136 mPreviousOutputs = mOutputs;
137 switch (state)
138 {
139 // handle output device connection
Sharad Sangle36781612015-05-28 16:15:16 +0530140 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700141 if (index >= 0) {
Sharad Sangle4509cef2015-08-19 20:47:12 +0530142#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
143 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
144 if (!strncmp(device_address, "hdmi_spkr", 9)) {
145 mHdmiAudioDisabled = false;
146 } else {
147 mHdmiAudioEvent = true;
148 }
149 }
150#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700151 ALOGW("setDeviceConnectionState() device already connected: %x", device);
152 return INVALID_OPERATION;
153 }
154 ALOGV("setDeviceConnectionState() connecting device %x", device);
155
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700156 // register new device as available
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700157 index = mAvailableOutputDevices.add(devDesc);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530158#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
159 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
160 if (!strncmp(device_address, "hdmi_spkr", 9)) {
161 mHdmiAudioDisabled = false;
162 } else {
163 mHdmiAudioEvent = true;
164 }
165 if (mHdmiAudioDisabled || !mHdmiAudioEvent) {
166 mAvailableOutputDevices.remove(devDesc);
167 ALOGW("HDMI sink not connected, do not route audio to HDMI out");
168 return INVALID_OPERATION;
169 }
170 }
171#endif
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700172 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530173 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700174 if (module == 0) {
175 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
176 device);
177 mAvailableOutputDevices.remove(devDesc);
178 return INVALID_OPERATION;
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700179 }
Sharad Sangle36781612015-05-28 16:15:16 +0530180 mAvailableOutputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700181 } else {
182 return NO_MEMORY;
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700183 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700184
Sharad Sangle36781612015-05-28 16:15:16 +0530185 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700186 mAvailableOutputDevices.remove(devDesc);
187 return INVALID_OPERATION;
188 }
Sharad Sangle36781612015-05-28 16:15:16 +0530189 // Propagate device availability to Engine
190 mEngine->setDeviceConnectionState(devDesc, state);
191
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700192 // outputs should never be empty here
193 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
194 "checkOutputsForDevice() returned no outputs but status OK");
195 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
196 outputs.size());
Sharad Sangle36781612015-05-28 16:15:16 +0530197
198 // Send connect to HALs
199 AudioParameter param = AudioParameter(devDesc->mAddress);
200 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
201 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
202
203 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700204 // handle output device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700205 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
206 if (index < 0) {
Sharad Sangle4509cef2015-08-19 20:47:12 +0530207#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
208 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
209 if (!strncmp(device_address, "hdmi_spkr", 9)) {
210 mHdmiAudioDisabled = true;
211 } else {
212 mHdmiAudioEvent = false;
213 }
214 }
215#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700216 ALOGW("setDeviceConnectionState() device not connected: %x", device);
217 return INVALID_OPERATION;
218 }
219
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700220 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
221
Sharad Sangle36781612015-05-28 16:15:16 +0530222 // Send Disconnect to HALs
223 AudioParameter param = AudioParameter(devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700224 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
225 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
226
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700227 // remove device from available output devices
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700228 mAvailableOutputDevices.remove(devDesc);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530229#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
230 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
231 if (!strncmp(device_address, "hdmi_spkr", 9)) {
232 mHdmiAudioDisabled = true;
233 } else {
234 mHdmiAudioEvent = false;
235 }
236 }
237#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530238 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
239
240 // Propagate device availability to Engine
241 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700242 } break;
243
244 default:
245 ALOGE("setDeviceConnectionState() invalid state: %x", state);
246 return BAD_VALUE;
247 }
248
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700249 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
250 // output is suspended before any tracks are moved to it
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700251 checkA2dpSuspend();
252 checkOutputForAllStrategies();
253 // outputs must be closed after checkOutputForAllStrategies() is executed
254 if (!outputs.isEmpty()) {
255 for (size_t i = 0; i < outputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530256 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700257 // close unused outputs after device disconnection or direct outputs that have been
258 // opened by checkOutputsForDevice() to query dynamic parameters
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700259 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700260 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
261 (desc->mDirectOpenCount == 0))) {
262 closeOutput(outputs[i]);
263 }
264 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700265 // check again after closing A2DP output to reset mA2dpSuspended if needed
266 checkA2dpSuspend();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700267 }
268
269 updateDevicesAndOutputs();
Gao Jiedb057832015-11-12 08:51:22 +0800270#ifdef DOLBY_ENABLE
271 // Before closing the opened outputs, update endpoint property with device capabilities
272 audio_devices_t audioOutputDevice = getDeviceForStrategy(getStrategy(AUDIO_STREAM_MUSIC), true);
273 mDolbyAudioPolicy.setEndpointSystemProperty(audioOutputDevice, mHwModules);
274#endif // DOLBY_END
Sharad Sangle36781612015-05-28 16:15:16 +0530275 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
276 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700277 updateCallRouting(newDevice);
278 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530279
280#ifdef FM_POWER_OPT
281 // handle FM device connection state to trigger FM AFE loopback
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800282 if (device == AUDIO_DEVICE_OUT_FM && hasPrimaryOutput()) {
Mingming Yin2dd77c22016-01-06 18:02:42 -0800283 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530284 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
285 mPrimaryOutput->changeRefCount(AUDIO_STREAM_MUSIC, 1);
Mingming Yin2dd77c22016-01-06 18:02:42 -0800286 newDevice = (audio_devices_t)(getNewOutputDevice(mPrimaryOutput, false)|AUDIO_DEVICE_OUT_FM);
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800287 mFMIsActive = true;
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530288 } else {
Mingming Yin2dd77c22016-01-06 18:02:42 -0800289 newDevice = (audio_devices_t)(getNewOutputDevice(mPrimaryOutput, false));
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800290 mFMIsActive = false;
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530291 mPrimaryOutput->changeRefCount(AUDIO_STREAM_MUSIC, -1);
292 }
293 AudioParameter param = AudioParameter();
294 param.addInt(String8("handle_fm"), (int)newDevice);
295 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString());
296 }
297#endif /* FM_POWER_OPT end */
298
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700299 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530300 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
301 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
302 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700303 // do not force device change on duplicated output because if device is 0, it will
304 // also force a device 0 for the two outputs it is duplicated to which may override
305 // a valid device selection on those outputs.
Sharad Sangle36781612015-05-28 16:15:16 +0530306 bool force = !desc->isDuplicated()
307 && (!device_distinguishes_on_address(device)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700308 // always force when disconnecting (a non-duplicated device)
309 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Sharad Sangle36781612015-05-28 16:15:16 +0530310 setOutputDevice(desc, newDevice, force, 0);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700311 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700312 }
313
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700314 mpClientInterface->onAudioPortListUpdate();
315 return NO_ERROR;
316 } // end if is output device
317
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700318 // handle input devices
319 if (audio_is_input_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700320 SortedVector <audio_io_handle_t> inputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700321
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700322 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700323 switch (state)
324 {
325 // handle input device connection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700326 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
327 if (index >= 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700328 ALOGW("setDeviceConnectionState() device already connected: %d", device);
329 return INVALID_OPERATION;
330 }
Sharad Sangle36781612015-05-28 16:15:16 +0530331 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700332 if (module == NULL) {
333 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
334 device);
335 return INVALID_OPERATION;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700336 }
Sharad Sangle36781612015-05-28 16:15:16 +0530337 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700338 return INVALID_OPERATION;
339 }
340
341 index = mAvailableInputDevices.add(devDesc);
342 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530343 mAvailableInputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700344 } else {
345 return NO_MEMORY;
346 }
Sharad Sangle36781612015-05-28 16:15:16 +0530347
348 // Set connect to HALs
349 AudioParameter param = AudioParameter(devDesc->mAddress);
350 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
351 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
352
353 // Propagate device availability to Engine
354 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700355 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700356
357 // handle input device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700358 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
359 if (index < 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700360 ALOGW("setDeviceConnectionState() device not connected: %d", device);
361 return INVALID_OPERATION;
362 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700363
364 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
365
366 // Set Disconnect to HALs
Sharad Sangle36781612015-05-28 16:15:16 +0530367 AudioParameter param = AudioParameter(devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700368 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
369 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
370
Sharad Sangle36781612015-05-28 16:15:16 +0530371 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700372 mAvailableInputDevices.remove(devDesc);
373
Sharad Sangle36781612015-05-28 16:15:16 +0530374 // Propagate device availability to Engine
375 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700376 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700377
378 default:
379 ALOGE("setDeviceConnectionState() invalid state: %x", state);
380 return BAD_VALUE;
381 }
382
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700383 closeAllInputs();
384
Sharad Sangle36781612015-05-28 16:15:16 +0530385 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700386 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
387 updateCallRouting(newDevice);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700388 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700389
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700390 mpClientInterface->onAudioPortListUpdate();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700391 return NO_ERROR;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700392 } // end if is input device
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700393
394 ALOGW("setDeviceConnectionState() invalid device: %x", device);
395 return BAD_VALUE;
396}
Weiyin Jiangd211ef52016-02-16 15:55:11 +0800397
Sharad Sangle36781612015-05-28 16:15:16 +0530398// This function checks for the parameters which can be offloaded.
399// This can be enhanced depending on the capability of the DSP and policy
400// of the system.
401bool AudioPolicyManagerCustom::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700402{
Sharad Sangle36781612015-05-28 16:15:16 +0530403 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
404 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
405 offloadInfo.sample_rate, offloadInfo.channel_mask,
406 offloadInfo.format,
407 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
408 offloadInfo.has_video);
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530409#ifdef VOICE_CONCURRENCY
410 char concpropValue[PROPERTY_VALUE_MAX];
411 if (property_get("voice.playback.conc.disabled", concpropValue, NULL)) {
412 bool propenabled = atoi(concpropValue) || !strncmp("true", concpropValue, 4);
413 if (propenabled) {
414 if (isInCall())
415 {
416 ALOGD("\n copl: blocking compress offload on call mode\n");
417 return false;
418 }
419 }
420 }
421#endif
422#ifdef RECORD_PLAY_CONCURRENCY
423 char recConcPropValue[PROPERTY_VALUE_MAX];
424 bool prop_rec_play_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700425
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530426 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
427 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
428 }
429
430 if ((prop_rec_play_enabled) &&
431 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
432 ALOGD("copl: blocking compress offload for record concurrency");
433 return false;
434 }
435#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530436 // Check if stream type is music, then only allow offload as of now.
437 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
438 {
439 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
440 return false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700441 }
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530442
Alexy Joseph0ac09032015-10-16 15:57:53 -0700443 // Check if offload has been disabled
444 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
445 if (offloadDisabled) {
446 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
447 return false;
448 }
449
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530450 char propValue[PROPERTY_VALUE_MAX];
451 bool pcmOffload = false;
452#ifdef PCM_OFFLOAD_ENABLED
453 if ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM_OFFLOAD) {
454 bool prop_enabled = false;
455 if ((AUDIO_FORMAT_PCM_16_BIT_OFFLOAD == offloadInfo.format) &&
456 property_get("audio.offload.pcm.16bit.enable", propValue, NULL)) {
457 prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
458 }
459
460#ifdef PCM_OFFLOAD_ENABLED_24
461 if ((AUDIO_FORMAT_PCM_24_BIT_OFFLOAD == offloadInfo.format) &&
462 property_get("audio.offload.pcm.24bit.enable", propValue, NULL)) {
463 prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530464 }
465#endif
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530466
467 if (prop_enabled) {
468 ALOGI("PCM offload property is enabled");
469 pcmOffload = true;
470 }
471
472 if (!pcmOffload) {
473 ALOGD("system property not enabled for PCM offload format[%x]",offloadInfo.format);
474 return false;
475 }
476 }
477#endif
478 if (!pcmOffload) {
Alexy Joseph0ac09032015-10-16 15:57:53 -0700479
480 bool compressedOffloadDisabled = property_get_bool("audio.offload.compress.disable", false);
481 if (compressedOffloadDisabled) {
482 ALOGI("compressed offload disabled by audio.offload.compress.disable=%d", compressedOffloadDisabled);
483 return false;
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530484 }
Alexy Joseph0ac09032015-10-16 15:57:53 -0700485
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530486 //check if it's multi-channel AAC (includes sub formats) and FLAC format
487 if ((popcount(offloadInfo.channel_mask) > 2) &&
488 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
489 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS))) {
490 ALOGD("offload disabled for multi-channel AAC,FLAC and VORBIS format");
491 return false;
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530492 }
493
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530494#ifdef AUDIO_EXTN_FORMATS_ENABLED
495 //check if it's multi-channel FLAC/ALAC/WMA format with sample rate > 48k
496 if ((popcount(offloadInfo.channel_mask) > 2) &&
497 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
Manish Dewangana6fc5442015-08-24 20:30:31 +0530498 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) && (offloadInfo.sample_rate > 48000)) ||
499 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) && (offloadInfo.sample_rate > 48000)) ||
500 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) && (offloadInfo.sample_rate > 48000)) ||
501 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS))) {
502 ALOGD("offload disabled for multi-channel FLAC/ALAC/WMA/AAC_ADTS clips with sample rate > 48kHz");
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530503 return false;
504 }
505#endif
506 //TODO: enable audio offloading with video when ready
507 const bool allowOffloadWithVideo =
508 property_get_bool("audio.offload.video", false /* default_value */);
509 if (offloadInfo.has_video && !allowOffloadWithVideo) {
510 ALOGV("isOffloadSupported: has_video == true, returning false");
511 return false;
512 }
Manish Dewanganf3cd0f82015-10-13 14:04:36 +0530513
514 const bool allowOffloadStreamingWithVideo = property_get_bool("av.streaming.offload.enable",
515 false /*default value*/);
516 if(offloadInfo.has_video && offloadInfo.is_streaming && !allowOffloadStreamingWithVideo) {
517 ALOGW("offload disabled by av.streaming.offload.enable = %s ", propValue );
518 return false;
519 }
520
Sharad Sangle36781612015-05-28 16:15:16 +0530521 }
522
523 //If duration is less than minimum value defined in property, return false
524 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
525 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
526 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
527 return false;
528 }
529 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
530 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
531 //duration checks only valid for MP3/AAC/ formats,
532 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
533 if ((offloadInfo.format == AUDIO_FORMAT_MP3) ||
534 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530535 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS) ||
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530536#ifdef AUDIO_EXTN_FORMATS_ENABLED
Sharad Sangle36781612015-05-28 16:15:16 +0530537 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
Sharad Sangle36781612015-05-28 16:15:16 +0530538 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) ||
539 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) ||
540 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) ||
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530541 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_APE) ||
Manish Dewangana6fc5442015-08-24 20:30:31 +0530542 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS) ||
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530543#endif
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530544 pcmOffload)
Sharad Sangle36781612015-05-28 16:15:16 +0530545 return false;
546
547 }
548
549 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
550 // creating an offloaded track and tearing it down immediately after start when audioflinger
551 // detects there is an active non offloadable effect.
552 // FIXME: We should check the audio session here but we do not have it in this context.
553 // This may prevent offloading in rare situations where effects are left active by apps
554 // in the background.
555 if (mEffects.isNonOffloadableEffectEnabled()) {
556 return false;
557 }
558 // Check for soundcard status
559 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
560 String8("SND_CARD_STATUS"));
561 AudioParameter result = AudioParameter(valueStr);
562 int isonline = 0;
563 if ((result.getInt(String8("SND_CARD_STATUS"), isonline) == NO_ERROR)
564 && !isonline) {
565 ALOGD("copl: soundcard is offline rejecting offload request");
566 return false;
567 }
568 // See if there is a profile to support this.
569 // AUDIO_DEVICE_NONE
570 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
571 offloadInfo.sample_rate,
572 offloadInfo.format,
573 offloadInfo.channel_mask,
574 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
575 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
576 return (profile != 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700577}
Weiyin Jiangd211ef52016-02-16 15:55:11 +0800578
Sharad Sangle36781612015-05-28 16:15:16 +0530579audio_devices_t AudioPolicyManagerCustom::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
580 bool fromCache)
581{
582 audio_devices_t device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700583
Sharad Sangle36781612015-05-28 16:15:16 +0530584 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
585 if (index >= 0) {
586 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
587 if (patchDesc->mUid != mUidCached) {
588 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
589 outputDesc->device(), outputDesc->mPatchHandle);
590 return outputDesc->device();
591 }
592 }
593
594 // check the following by order of priority to request a routing change if necessary:
595 // 1: the strategy enforced audible is active and enforced on the output:
596 // use device for strategy enforced audible
597 // 2: we are in call or the strategy phone is active on the output:
598 // use device for strategy phone
599 // 3: the strategy for enforced audible is active but not enforced on the output:
600 // use the device for strategy enforced audible
601 // 4: the strategy sonification is active on the output:
602 // use device for strategy sonification
603 // 5: the strategy "respectful" sonification is active on the output:
604 // use device for strategy "respectful" sonification
605 // 6: the strategy accessibility is active on the output:
606 // use device for strategy accessibility
607 // 7: the strategy media is active on the output:
608 // use device for strategy media
609 // 8: the strategy DTMF is active on the output:
610 // use device for strategy DTMF
611 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
612 // use device for strategy t-t-s
613 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
614 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
615 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
616 } else if (isInCall() ||
617 isStrategyActive(outputDesc, STRATEGY_PHONE)||
618 isStrategyActive(mPrimaryOutput, STRATEGY_PHONE)) {
619 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
620 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
621 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
622 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)||
623 (isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION)
624 && (!isStrategyActive(mPrimaryOutput,STRATEGY_MEDIA)))) {
625 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530626 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL) ||
627 isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION_RESPECTFUL)) {
Sharad Sangle36781612015-05-28 16:15:16 +0530628 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
629 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
630 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
631 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
632 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
633 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
634 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
635 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
636 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
637 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
638 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
639 }
640
641 ALOGV("getNewOutputDevice() selected device %x", device);
642 return device;
643}
Weiyin Jiangd211ef52016-02-16 15:55:11 +0800644
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700645void AudioPolicyManagerCustom::setPhoneState(audio_mode_t state)
646{
Sharad Sangle36781612015-05-28 16:15:16 +0530647 ALOGV("setPhoneState() state %d", state);
648 // store previous phone state for management of sonification strategy below
Sharad Sangle4509cef2015-08-19 20:47:12 +0530649 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Sharad Sangle36781612015-05-28 16:15:16 +0530650 int oldState = mEngine->getPhoneState();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700651
Sharad Sangle36781612015-05-28 16:15:16 +0530652 if (mEngine->setPhoneState(state) != NO_ERROR) {
653 ALOGW("setPhoneState() invalid or same state %d", state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700654 return;
655 }
Sharad Sangle36781612015-05-28 16:15:16 +0530656 /// Opens: can these line be executed after the switch of volume curves???
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700657 // if leaving call state, handle special case of active streams
658 // pertaining to sonification strategy see handleIncallSonification()
Zhou Song58081b62016-02-24 13:10:55 +0800659 if (isStateInCall(oldState)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700660 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530661 for (size_t j = 0; j < mOutputs.size(); j++) {
662 audio_io_handle_t curOutput = mOutputs.keyAt(j);
663 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
664 if (stream == AUDIO_STREAM_PATCH) {
665 continue;
666 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530667 handleIncallSonification((audio_stream_type_t)stream, false, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +0530668 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700669 }
Sharad Sangle36781612015-05-28 16:15:16 +0530670
Zhou Song58081b62016-02-24 13:10:55 +0800671 // force reevaluating accessibility routing when call stops
Sharad Sangle36781612015-05-28 16:15:16 +0530672 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700673 }
674
Sharad Sangle36781612015-05-28 16:15:16 +0530675 /**
676 * Switching to or from incall state or switching between telephony and VoIP lead to force
677 * routing command.
678 */
679 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
680 || (is_state_in_call(state) && (state != oldState)));
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700681
682 // check for device and output changes triggered by new phone state
683 checkA2dpSuspend();
684 checkOutputForAllStrategies();
685 updateDevicesAndOutputs();
686
Sharad Sangle36781612015-05-28 16:15:16 +0530687 sp<SwAudioOutputDescriptor> hwOutputDesc = mPrimaryOutput;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530688#ifdef VOICE_CONCURRENCY
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530689 char propValue[PROPERTY_VALUE_MAX];
690 bool prop_playback_enabled = false, prop_rec_enabled=false, prop_voip_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700691
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530692 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
693 prop_playback_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
694 }
695
696 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
697 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
698 }
699
700 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
701 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
702 }
703
Dhananjay Kumar9125b2a2015-12-01 16:24:09 +0530704 if ((AUDIO_MODE_IN_CALL != oldState) && (AUDIO_MODE_IN_CALL == state)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530705 ALOGD("voice_conc:Entering to call mode oldState :: %d state::%d ",
706 oldState, state);
Dhananjay Kumar9125b2a2015-12-01 16:24:09 +0530707 mvoice_call_state = state;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530708 if (prop_rec_enabled) {
709 //Close all active inputs
710 audio_io_handle_t activeInput = mInputs.getActiveInput();
711 if (activeInput != 0) {
712 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
713 switch(activeDesc->mInputSource) {
714 case AUDIO_SOURCE_VOICE_UPLINK:
715 case AUDIO_SOURCE_VOICE_DOWNLINK:
716 case AUDIO_SOURCE_VOICE_CALL:
717 ALOGD("voice_conc:FOUND active input during call active: %d",activeDesc->mInputSource);
718 break;
719
720 case AUDIO_SOURCE_VOICE_COMMUNICATION:
721 if(prop_voip_enabled) {
722 ALOGD("voice_conc:CLOSING VoIP input source on call setup :%d ",activeDesc->mInputSource);
723 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
724 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
725 }
726 break;
727
728 default:
729 ALOGD("voice_conc:CLOSING input on call setup for inputSource: %d",activeDesc->mInputSource);
730 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
731 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
732 break;
733 }
734 }
735 } else if (prop_voip_enabled) {
736 audio_io_handle_t activeInput = mInputs.getActiveInput();
737 if (activeInput != 0) {
738 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
739 if (AUDIO_SOURCE_VOICE_COMMUNICATION == activeDesc->mInputSource) {
740 ALOGD("voice_conc:CLOSING VoIP on call setup : %d",activeDesc->mInputSource);
741 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
742 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
743 }
744 }
745 }
746 if (prop_playback_enabled) {
747 // Move tracks associated to this strategy from previous output to new output
748 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
749 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
750 if (i == AUDIO_STREAM_PATCH) {
751 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
752 continue;
753 }
754 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
755 if ((AUDIO_STREAM_MUSIC == i) ||
756 (AUDIO_STREAM_VOICE_CALL == i) ) {
757 ALOGD("voice_conc:Invalidate stream type %d", i);
758 mpClientInterface->invalidateStream((audio_stream_type_t)i);
759 }
760 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
761 ALOGD("voice_conc:Invalidate stream type %d", i);
762 mpClientInterface->invalidateStream((audio_stream_type_t)i);
763 }
764 }
765 }
766
767 for (size_t i = 0; i < mOutputs.size(); i++) {
768 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
769 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
770 ALOGD("voice_conc:ouput desc / profile is NULL");
771 continue;
772 }
773
774 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
775 if (((!outputDesc->isDuplicated() &&outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY))
776 && prop_playback_enabled) {
777 ALOGD("voice_conc:calling suspendOutput on call mode for primary output");
778 mpClientInterface->suspendOutput(mOutputs.keyAt(i));
779 } //Close compress all sessions
780 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
781 && prop_playback_enabled) {
782 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
783 closeOutput(mOutputs.keyAt(i));
784 }
785 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_VOIP_RX)
786 && prop_voip_enabled) {
787 ALOGD("voice_conc:calling closeOutput on call mode for DIRECT output");
788 closeOutput(mOutputs.keyAt(i));
789 }
790 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
791 if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)
792 && prop_playback_enabled) {
793 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
794 closeOutput(mOutputs.keyAt(i));
795 }
796 }
797 }
798 }
799
800 if ((AUDIO_MODE_IN_CALL == oldState || AUDIO_MODE_IN_COMMUNICATION == oldState) &&
801 (AUDIO_MODE_NORMAL == state) && prop_playback_enabled && mvoice_call_state) {
802 ALOGD("voice_conc:EXITING from call mode oldState :: %d state::%d \n",oldState, state);
803 mvoice_call_state = 0;
804 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
805 //restore PCM (deep-buffer) output after call termination
806 for (size_t i = 0; i < mOutputs.size(); i++) {
807 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
808 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
809 ALOGD("voice_conc:ouput desc / profile is NULL");
810 continue;
811 }
812 if (!outputDesc->isDuplicated() && outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
813 ALOGD("voice_conc:calling restoreOutput after call mode for primary output");
814 mpClientInterface->restoreOutput(mOutputs.keyAt(i));
815 }
816 }
817 }
818 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
819 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
820 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
821 if (i == AUDIO_STREAM_PATCH) {
822 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
823 continue;
824 }
825 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
826 if ((AUDIO_STREAM_MUSIC == i) ||
827 (AUDIO_STREAM_VOICE_CALL == i) ) {
828 mpClientInterface->invalidateStream((audio_stream_type_t)i);
829 }
830 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
831 mpClientInterface->invalidateStream((audio_stream_type_t)i);
832 }
833 }
834 }
835
836#endif
837#ifdef RECORD_PLAY_CONCURRENCY
838 char recConcPropValue[PROPERTY_VALUE_MAX];
839 bool prop_rec_play_enabled = false;
840
841 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
842 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
843 }
844 if (prop_rec_play_enabled) {
845 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
846 ALOGD("phone state changed to MODE_IN_COMM invlaidating music and voice streams");
847 // call invalidate for voice streams, so that it can use deepbuffer with VoIP out device from HAL
848 mpClientInterface->invalidateStream(AUDIO_STREAM_VOICE_CALL);
849 // call invalidate for music, so that compress will fallback to deep-buffer with VoIP out device
850 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
851
852 // close compress output to make sure session will be closed before timeout(60sec)
853 for (size_t i = 0; i < mOutputs.size(); i++) {
854
855 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
856 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
857 ALOGD("ouput desc / profile is NULL");
858 continue;
859 }
860
861 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
862 ALOGD("calling closeOutput on call mode for COMPRESS output");
863 closeOutput(mOutputs.keyAt(i));
864 }
865 }
866 } else if ((oldState == AUDIO_MODE_IN_COMMUNICATION) &&
867 (mEngine->getPhoneState() == AUDIO_MODE_NORMAL)) {
868 // call invalidate for music so that music can fallback to compress
869 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
870 }
871 }
872#endif
873 mPrevPhoneState = oldState;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700874 int delayMs = 0;
875 if (isStateInCall(state)) {
876 nsecs_t sysTime = systemTime();
877 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530878 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700879 // mute media and sonification strategies and delay device switch by the largest
880 // latency of any output where either strategy is active.
881 // This avoid sending the ring tone or music tail into the earpiece or headset.
Sharad Sangle36781612015-05-28 16:15:16 +0530882 if ((isStrategyActive(desc, STRATEGY_MEDIA,
883 SONIFICATION_HEADSET_MUSIC_DELAY,
884 sysTime) ||
885 isStrategyActive(desc, STRATEGY_SONIFICATION,
886 SONIFICATION_HEADSET_MUSIC_DELAY,
887 sysTime)) &&
888 (delayMs < (int)desc->latency()*2)) {
889 delayMs = desc->latency()*2;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700890 }
Sharad Sangle36781612015-05-28 16:15:16 +0530891 setStrategyMute(STRATEGY_MEDIA, true, desc);
892 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700893 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Sharad Sangle36781612015-05-28 16:15:16 +0530894 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
895 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700896 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
897 }
898 }
899
Sharad Sangle36781612015-05-28 16:15:16 +0530900 if (hasPrimaryOutput()) {
901 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
902 // the device returned is not necessarily reachable via this output
903 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
904 // force routing command to audio hardware when ending call
905 // even if no device change is needed
906 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
907 rxDevice = mPrimaryOutput->device();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700908 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700909
Sharad Sangle36781612015-05-28 16:15:16 +0530910 if (state == AUDIO_MODE_IN_CALL) {
911 updateCallRouting(rxDevice, delayMs);
912 } else if (oldState == AUDIO_MODE_IN_CALL) {
913 if (mCallRxPatch != 0) {
914 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
915 mCallRxPatch.clear();
916 }
917 if (mCallTxPatch != 0) {
918 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
919 mCallTxPatch.clear();
920 }
921 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
922 } else {
923 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700924 }
925 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530926 //update device for all non-primary outputs
927 for (size_t i = 0; i < mOutputs.size(); i++) {
928 audio_io_handle_t output = mOutputs.keyAt(i);
929 if (output != mPrimaryOutput->mIoHandle) {
930 newDevice = getNewOutputDevice(mOutputs.valueFor(output), false /*fromCache*/);
931 setOutputDevice(mOutputs.valueFor(output), newDevice, (newDevice != AUDIO_DEVICE_NONE));
932 }
933 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700934 // if entering in call state, handle special case of active streams
935 // pertaining to sonification strategy see handleIncallSonification()
936 if (isStateInCall(state)) {
937 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530938 for (size_t j = 0; j < mOutputs.size(); j++) {
939 audio_io_handle_t curOutput = mOutputs.keyAt(j);
940 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
941 if (stream == AUDIO_STREAM_PATCH) {
942 continue;
943 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530944 handleIncallSonification((audio_stream_type_t)stream, true, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +0530945 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700946 }
Zhou Song58081b62016-02-24 13:10:55 +0800947
948 // force reevaluating accessibility routing when call starts
949 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700950 }
951
952 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
953 if (state == AUDIO_MODE_RINGTONE &&
954 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
955 mLimitRingtoneVolume = true;
956 } else {
957 mLimitRingtoneVolume = false;
958 }
959}
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +0530960
961void AudioPolicyManagerCustom::setForceUse(audio_policy_force_use_t usage,
962 audio_policy_forced_cfg_t config)
963{
964 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
965
966 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
967 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
968 return;
969 }
970 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
971 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
972 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
973
974 // check for device and output changes triggered by new force usage
975 checkA2dpSuspend();
976 checkOutputForAllStrategies();
977 updateDevicesAndOutputs();
978 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
979 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
980 updateCallRouting(newDevice);
981 }
982 // Use reverse loop to make sure any low latency usecases (generally tones)
983 // are not routed before non LL usecases (generally music).
984 // We can safely assume that LL output would always have lower index,
985 // and use this work-around to avoid routing of output with music stream
986 // from the context of short lived LL output.
987 // Note: in case output's share backend(HAL sharing is implicit) all outputs
988 // gets routing update while processing first output itself.
989 for (size_t i = mOutputs.size(); i > 0; i--) {
990 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i-1);
991 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
992 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || outputDesc != mPrimaryOutput) {
993 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
994 }
995 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
996 applyStreamVolumes(outputDesc, newDevice, 0, true);
997 }
998 }
999
1000 audio_io_handle_t activeInput = mInputs.getActiveInput();
1001 if (activeInput != 0) {
1002 setInputDevice(activeInput, getNewInputDevice(activeInput));
1003 }
1004
1005}
1006
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301007status_t AudioPolicyManagerCustom::stopSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301008 audio_stream_type_t stream,
1009 bool forceDeviceUpdate)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001010{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301011 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1012 ALOGW("stopSource() invalid stream %d", stream);
1013 return INVALID_OPERATION;
1014 }
Sharad Sangle36781612015-05-28 16:15:16 +05301015 // always handle stream stop, check which stream type is stopping
1016 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001017
Sharad Sangle36781612015-05-28 16:15:16 +05301018 // handle special case for sonification while in call
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001019 if (isInCall()) {
Sharad Sangle36781612015-05-28 16:15:16 +05301020 if (outputDesc->isDuplicated()) {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301021 handleIncallSonification(stream, false, false, outputDesc->subOutput1()->mIoHandle);
1022 handleIncallSonification(stream, false, false, outputDesc->subOutput2()->mIoHandle);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001023 }
Sharad Sangle36781612015-05-28 16:15:16 +05301024 handleIncallSonification(stream, false, false, outputDesc->mIoHandle);
1025 }
1026
1027 if (outputDesc->mRefCount[stream] > 0) {
1028 // decrement usage count of this stream on the output
1029 outputDesc->changeRefCount(stream, -1);
1030
1031 // store time at which the stream was stopped - see isStreamActive()
1032 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
1033 outputDesc->mStopTime[stream] = systemTime();
Zhou Song5dcddc92015-09-21 14:36:57 +08001034 audio_devices_t prevDevice = outputDesc->device();
Sharad Sangle36781612015-05-28 16:15:16 +05301035 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
1036 // delay the device switch by twice the latency because stopOutput() is executed when
1037 // the track stop() command is received and at that time the audio track buffer can
1038 // still contain data that needs to be drained. The latency only covers the audio HAL
1039 // and kernel buffers. Also the latency does not always include additional delay in the
1040 // audio path (audio DSP, CODEC ...)
1041 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
1042
1043 // force restoring the device selection on other active outputs if it differs from the
1044 // one being selected for this output
1045 for (size_t i = 0; i < mOutputs.size(); i++) {
1046 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1047 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1048 if (desc != outputDesc &&
1049 desc->isActive() &&
1050 outputDesc->sharesHwModuleWith(desc) &&
1051 (newDevice != desc->device())) {
Sharad Sangle4509cef2015-08-19 20:47:12 +05301052 audio_devices_t dev = getNewOutputDevice(mOutputs.valueFor(curOutput), false /*fromCache*/);
Zhou Song5dcddc92015-09-21 14:36:57 +08001053 uint32_t delayMs;
1054 if (dev == prevDevice) {
1055 delayMs = 0;
1056 } else {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301057 delayMs = outputDesc->latency()*2;
Zhou Song5dcddc92015-09-21 14:36:57 +08001058 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301059 setOutputDevice(desc,
1060 dev,
Sharad Sangle36781612015-05-28 16:15:16 +05301061 true,
Zhou Song5dcddc92015-09-21 14:36:57 +08001062 delayMs);
Sharad Sangle36781612015-05-28 16:15:16 +05301063 }
1064 }
1065 // update the outputs if stopping one with a stream that can affect notification routing
1066 handleNotificationRoutingForStream(stream);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001067 }
Sharad Sangle36781612015-05-28 16:15:16 +05301068 return NO_ERROR;
1069 } else {
1070 ALOGW("stopOutput() refcount is already 0");
1071 return INVALID_OPERATION;
1072 }
1073}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001074
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301075status_t AudioPolicyManagerCustom::startSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301076 audio_stream_type_t stream,
1077 audio_devices_t device,
1078 uint32_t *delayMs)
1079{
1080 // cannot start playback of STREAM_TTS if any other output is being used
1081 uint32_t beaconMuteLatency = 0;
1082
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301083 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1084 ALOGW("startSource() invalid stream %d", stream);
1085 return INVALID_OPERATION;
1086 }
1087
Sharad Sangle36781612015-05-28 16:15:16 +05301088 *delayMs = 0;
1089 if (stream == AUDIO_STREAM_TTS) {
1090 ALOGV("\t found BEACON stream");
1091 if (mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
1092 return INVALID_OPERATION;
1093 } else {
1094 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001095 }
Sharad Sangle36781612015-05-28 16:15:16 +05301096 } else {
1097 // some playback other than beacon starts
1098 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1099 }
1100
1101 // increment usage count for this stream on the requested output:
1102 // NOTE that the usage count is the same for duplicated output and hardware output which is
1103 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1104 outputDesc->changeRefCount(stream, 1);
1105
1106 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
1107 // starting an output being rerouted?
1108 if (device == AUDIO_DEVICE_NONE) {
1109 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001110 }
Sharad Sangle36781612015-05-28 16:15:16 +05301111 routing_strategy strategy = getStrategy(stream);
1112 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
1113 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1114 (beaconMuteLatency > 0);
1115 uint32_t waitMs = beaconMuteLatency;
1116 bool force = false;
1117 for (size_t i = 0; i < mOutputs.size(); i++) {
1118 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1119 if (desc != outputDesc) {
1120 // force a device change if any other output is managed by the same hw
1121 // module and has a current device selection that differs from selected device.
1122 // In this case, the audio HAL must receive the new device selection so that it can
1123 // change the device currently selected by the other active output.
1124 if (outputDesc->sharesHwModuleWith(desc) &&
1125 desc->device() != device) {
1126 force = true;
1127 }
1128 // wait for audio on other active outputs to be presented when starting
1129 // a notification so that audio focus effect can propagate, or that a mute/unmute
1130 // event occurred for beacon
1131 uint32_t latency = desc->latency();
1132 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1133 waitMs = latency;
1134 }
1135 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001136 }
Sharad Sangle36781612015-05-28 16:15:16 +05301137 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force);
1138
1139 // handle special case for sonification while in call
1140 if (isInCall()) {
1141 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001142 }
Sharad Sangle36781612015-05-28 16:15:16 +05301143
1144 // apply volume rules for current stream and device if necessary
1145 checkAndSetVolume(stream,
1146 mStreams.valueFor(stream).getVolumeIndex(device),
1147 outputDesc,
1148 device);
1149
1150 // update the outputs if starting an output with a stream that can affect notification
1151 // routing
1152 handleNotificationRoutingForStream(stream);
1153
1154 // force reevaluating accessibility routing when ringtone or alarm starts
1155 if (strategy == STRATEGY_SONIFICATION) {
1156 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1157 }
1158 }
1159 else {
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001160 // handle special case for sonification while in call
1161 if (isInCall()) {
1162 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Sharad Sangle36781612015-05-28 16:15:16 +05301163 }
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001164 }
Sharad Sangle36781612015-05-28 16:15:16 +05301165 return NO_ERROR;
1166}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001167
Sharad Sangle36781612015-05-28 16:15:16 +05301168void AudioPolicyManagerCustom::handleIncallSonification(audio_stream_type_t stream,
1169 bool starting, bool stateChange,
1170 audio_io_handle_t output)
1171{
1172 if(!hasPrimaryOutput()) {
1173 return;
1174 }
1175 // no action needed for AUDIO_STREAM_PATCH stream type, it's for internal flinger tracks
1176 if (stream == AUDIO_STREAM_PATCH) {
1177 return;
1178 }
1179 // if the stream pertains to sonification strategy and we are in call we must
1180 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
1181 // in the device used for phone strategy and play the tone if the selected device does not
1182 // interfere with the device used for phone strategy
1183 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
1184 // many times as there are active tracks on the output
1185 const routing_strategy stream_strategy = getStrategy(stream);
1186 if ((stream_strategy == STRATEGY_SONIFICATION) ||
1187 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
1188 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
1189 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
1190 stream, starting, outputDesc->mDevice, stateChange);
1191 if (outputDesc->mRefCount[stream]) {
1192 int muteCount = 1;
1193 if (stateChange) {
1194 muteCount = outputDesc->mRefCount[stream];
1195 }
1196 if (audio_is_low_visibility(stream)) {
1197 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
1198 for (int i = 0; i < muteCount; i++) {
1199 setStreamMute(stream, starting, outputDesc);
1200 }
1201 } else {
1202 ALOGV("handleIncallSonification() high visibility");
1203 if (outputDesc->device() &
1204 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
1205 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
1206 for (int i = 0; i < muteCount; i++) {
1207 setStreamMute(stream, starting, outputDesc);
1208 }
1209 }
1210 if (starting) {
1211 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
1212 AUDIO_STREAM_VOICE_CALL);
1213 } else {
1214 mpClientInterface->stopTone();
1215 }
1216 }
1217 }
1218 }
1219}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001220
Sharad Sangle36781612015-05-28 16:15:16 +05301221void AudioPolicyManagerCustom::handleNotificationRoutingForStream(audio_stream_type_t stream) {
1222 switch(stream) {
1223 case AUDIO_STREAM_MUSIC:
1224 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
1225 updateDevicesAndOutputs();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001226 break;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001227 default:
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001228 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001229 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001230}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001231
Sharad Sangle36781612015-05-28 16:15:16 +05301232status_t AudioPolicyManagerCustom::checkAndSetVolume(audio_stream_type_t stream,
1233 int index,
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301234 const sp<AudioOutputDescriptor>& outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301235 audio_devices_t device,
1236 int delayMs, bool force)
1237{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301238 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1239 ALOGW("checkAndSetVolume() invalid stream %d", stream);
1240 return INVALID_OPERATION;
1241 }
Sharad Sangle36781612015-05-28 16:15:16 +05301242 // do not change actual stream volume if the stream is muted
1243 if (outputDesc->mMuteCount[stream] != 0) {
1244 ALOGVV("checkAndSetVolume() stream %d muted count %d",
1245 stream, outputDesc->mMuteCount[stream]);
1246 return NO_ERROR;
1247 }
1248 audio_policy_forced_cfg_t forceUseForComm =
1249 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
1250 // do not change in call volume if bluetooth is connected and vice versa
1251 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
1252 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
1253 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
1254 stream, forceUseForComm);
1255 return INVALID_OPERATION;
1256 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001257
Sharad Sangle36781612015-05-28 16:15:16 +05301258 if (device == AUDIO_DEVICE_NONE) {
1259 device = outputDesc->device();
1260 }
1261
1262 float volumeDb = computeVolume(stream, index, device);
1263 if (outputDesc->isFixedVolume(device)) {
1264 volumeDb = 0.0f;
1265 }
1266
1267 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
1268
1269 if (stream == AUDIO_STREAM_VOICE_CALL ||
1270 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
1271 float voiceVolume;
1272 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
1273 if (stream == AUDIO_STREAM_VOICE_CALL) {
1274 voiceVolume = (float)index/(float)mStreams.valueFor(stream).getVolumeIndexMax();
1275 } else {
1276 voiceVolume = 1.0;
1277 }
1278
1279 if (voiceVolume != mLastVoiceVolume && ((outputDesc == mPrimaryOutput) ||
1280 isDirectOutput(outputDesc->mIoHandle) || device & AUDIO_DEVICE_OUT_ALL_USB)) {
1281 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
1282 mLastVoiceVolume = voiceVolume;
1283 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301284#ifdef FM_POWER_OPT
1285 } else if (stream == AUDIO_STREAM_MUSIC && hasPrimaryOutput() &&
Haynes Mathew George603ae9b2015-12-21 17:23:59 -08001286 outputDesc == mPrimaryOutput && mFMIsActive) {
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001287 /* Avoid unnecessary set_parameter calls as it puts the primary
1288 outputs FastMixer in HOT_IDLE leading to breaks in audio */
1289 if (volumeDb != mPrevFMVolumeDb) {
1290 mPrevFMVolumeDb = volumeDb;
1291 AudioParameter param = AudioParameter();
1292 param.addFloat(String8("fm_volume"), Volume::DbToAmpl(volumeDb));
Mingming Yin1433cc62016-01-04 14:58:00 -08001293 //Double delayMs to avoid sound burst while device switch.
1294 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString(), delayMs*2);
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001295 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301296#endif /* FM_POWER_OPT end */
Sharad Sangle36781612015-05-28 16:15:16 +05301297 }
1298
1299 return NO_ERROR;
1300}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001301
Sharad Sangle36781612015-05-28 16:15:16 +05301302bool AudioPolicyManagerCustom::isDirectOutput(audio_io_handle_t output) {
1303 for (size_t i = 0; i < mOutputs.size(); i++) {
1304 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1305 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1306 if ((curOutput == output) && (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
1307 return true;
1308 }
1309 }
1310 return false;
1311}
vivek mehta0ea887a2015-08-26 14:01:20 -07001312
1313status_t AudioPolicyManagerCustom::getOutputForAttr(const audio_attributes_t *attr,
1314 audio_io_handle_t *output,
1315 audio_session_t session,
1316 audio_stream_type_t *stream,
1317 uid_t uid,
1318 uint32_t samplingRate,
1319 audio_format_t format,
1320 audio_channel_mask_t channelMask,
1321 audio_output_flags_t flags,
1322 audio_port_handle_t selectedDeviceId,
1323 const audio_offload_info_t *offloadInfo)
1324{
1325 audio_offload_info_t tOffloadInfo = AUDIO_INFO_INITIALIZER;
1326
Alexy Joseph0ac09032015-10-16 15:57:53 -07001327 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
1328 bool pcmOffloadEnabled = false;
1329
1330 if (offloadDisabled) {
1331 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
1332 }
1333
1334 //read track offload property only if the global offload switch is off.
1335 if (!offloadDisabled) {
1336 pcmOffloadEnabled = property_get_bool("audio.offload.track.enable", false);
1337 }
vivek mehta0ea887a2015-08-26 14:01:20 -07001338
1339 if (offloadInfo == NULL && pcmOffloadEnabled) {
1340 tOffloadInfo.sample_rate = samplingRate;
1341 tOffloadInfo.channel_mask = channelMask;
1342 tOffloadInfo.format = format;
1343 tOffloadInfo.stream_type = *stream;
1344 tOffloadInfo.bit_width = 16; //hard coded for PCM_16
1345 if (attr != NULL) {
1346 ALOGV("found attribute .. setting usage %d ", attr->usage);
1347 tOffloadInfo.usage = attr->usage;
1348 } else {
Alexy Joseph0ac09032015-10-16 15:57:53 -07001349 ALOGI("%s:: attribute is NULL .. no usage set", __func__);
vivek mehta0ea887a2015-08-26 14:01:20 -07001350 }
1351 offloadInfo = &tOffloadInfo;
1352 }
1353
1354 return AudioPolicyManager::getOutputForAttr(attr, output, session, stream,
1355 (uid_t)uid, (uint32_t)samplingRate,
1356 format, (audio_channel_mask_t)channelMask,
1357 flags, (audio_port_handle_t)selectedDeviceId,
1358 offloadInfo);
1359}
1360
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001361audio_io_handle_t AudioPolicyManagerCustom::getOutputForDevice(
1362 audio_devices_t device,
Sharad Sangle36781612015-05-28 16:15:16 +05301363 audio_session_t session __unused,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001364 audio_stream_type_t stream,
1365 uint32_t samplingRate,
1366 audio_format_t format,
1367 audio_channel_mask_t channelMask,
1368 audio_output_flags_t flags,
1369 const audio_offload_info_t *offloadInfo)
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001370{
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001371 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
1372 uint32_t latency = 0;
1373 status_t status;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001374
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001375#ifdef AUDIO_POLICY_TEST
1376 if (mCurOutput != 0) {
1377 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1378 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1379
1380 if (mTestOutputs[mCurOutput] == 0) {
1381 ALOGV("getOutput() opening test output");
Sharad Sangle36781612015-05-28 16:15:16 +05301382 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
1383 mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001384 outputDesc->mDevice = mTestDevice;
1385 outputDesc->mLatency = mTestLatencyMs;
1386 outputDesc->mFlags =
1387 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
1388 outputDesc->mRefCount[stream] = 0;
1389 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1390 config.sample_rate = mTestSamplingRate;
1391 config.channel_mask = mTestChannels;
1392 config.format = mTestFormat;
1393 if (offloadInfo != NULL) {
1394 config.offload_info = *offloadInfo;
1395 }
1396 status = mpClientInterface->openOutput(0,
1397 &mTestOutputs[mCurOutput],
1398 &config,
1399 &outputDesc->mDevice,
1400 String8(""),
1401 &outputDesc->mLatency,
1402 outputDesc->mFlags);
1403 if (status == NO_ERROR) {
1404 outputDesc->mSamplingRate = config.sample_rate;
1405 outputDesc->mFormat = config.format;
1406 outputDesc->mChannelMask = config.channel_mask;
1407 AudioParameter outputCmd = AudioParameter();
1408 outputCmd.addInt(String8("set_id"),mCurOutput);
1409 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1410 addOutput(mTestOutputs[mCurOutput], outputDesc);
1411 }
1412 }
1413 return mTestOutputs[mCurOutput];
1414 }
1415#endif //AUDIO_POLICY_TEST
Sharad Sangle36781612015-05-28 16:15:16 +05301416 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1417 (stream != AUDIO_STREAM_MUSIC)) {
1418 // compress should not be used for non-music streams
1419 ALOGE("Offloading only allowed with music stream");
1420 return 0;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301421 }
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301422
1423 if ((stream == AUDIO_STREAM_VOICE_CALL) &&
1424 (channelMask == 1) &&
Shiv Maliyappanahallid77d3b32016-02-02 13:45:50 -08001425 (samplingRate == 8000 || samplingRate == 16000 ||
1426 samplingRate == 32000 || samplingRate == 48000)) {
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301427 // Allow Voip direct output only if:
1428 // audio mode is MODE_IN_COMMUNCATION; AND
1429 // voip output is not opened already; AND
1430 // requested sample rate matches with that of voip input stream (if opened already)
1431 int value = 0;
1432 uint32_t mode = 0, voipOutCount = 1, voipSampleRate = 1;
1433 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1434 String8("audio_mode"));
1435 AudioParameter result = AudioParameter(valueStr);
1436 if (result.getInt(String8("audio_mode"), value) == NO_ERROR) {
1437 mode = value;
1438 }
1439
1440 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1441 String8("voip_out_stream_count"));
1442 result = AudioParameter(valueStr);
1443 if (result.getInt(String8("voip_out_stream_count"), value) == NO_ERROR) {
1444 voipOutCount = value;
1445 }
1446
1447 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1448 String8("voip_sample_rate"));
1449 result = AudioParameter(valueStr);
1450 if (result.getInt(String8("voip_sample_rate"), value) == NO_ERROR) {
1451 voipSampleRate = value;
1452 }
1453
1454 if ((mode == AUDIO_MODE_IN_COMMUNICATION) && (voipOutCount == 0) &&
1455 ((voipSampleRate == 0) || (voipSampleRate == samplingRate))) {
1456 if (audio_is_linear_pcm(format)) {
1457 char propValue[PROPERTY_VALUE_MAX] = {0};
1458 property_get("use.voice.path.for.pcm.voip", propValue, "0");
1459 bool voipPcmSysPropEnabled = !strncmp("true", propValue, sizeof("true"));
1460 if (voipPcmSysPropEnabled && (format == AUDIO_FORMAT_PCM_16_BIT)) {
Karthik Reddy Katta3fb9be72015-11-25 11:18:27 +05301461 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX |
1462 AUDIO_OUTPUT_FLAG_DIRECT);
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301463 ALOGD("Set VoIP and Direct output flags for PCM format");
1464 }
1465 }
1466 }
1467 }
1468
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301469#ifdef VOICE_CONCURRENCY
1470 char propValue[PROPERTY_VALUE_MAX];
1471 bool prop_play_enabled=false, prop_voip_enabled = false;
1472
1473 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1474 prop_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001475 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301476
1477 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1478 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1479 }
1480
1481 if (prop_play_enabled && mvoice_call_state) {
1482 //check if voice call is active / running in background
1483 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1484 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1485 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1486 {
1487 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1488 if(prop_voip_enabled) {
1489 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1490 flags );
1491 return 0;
1492 }
1493 }
1494 else {
1495 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
1496 ALOGD("voice_conc:IN call mode adding ULL flags .. flags: %x ", flags );
1497 flags = AUDIO_OUTPUT_FLAG_FAST;
1498 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
1499 if (AUDIO_STREAM_MUSIC == stream) {
1500 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1501 ALOGD("voice_conc:IN call mode adding deep-buffer flags %x ", flags );
1502 }
1503 else {
1504 flags = AUDIO_OUTPUT_FLAG_FAST;
1505 ALOGD("voice_conc:IN call mode adding fast flags %x ", flags );
1506 }
1507 }
1508 }
1509 }
1510 } else if (prop_voip_enabled && mvoice_call_state) {
1511 //check if voice call is active / running in background
1512 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1513 //return only ULL ouput
1514 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1515 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1516 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1517 {
1518 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1519 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1520 flags );
1521 return 0;
1522 }
1523 }
1524 }
1525#endif
1526#ifdef RECORD_PLAY_CONCURRENCY
1527 char recConcPropValue[PROPERTY_VALUE_MAX];
1528 bool prop_rec_play_enabled = false;
1529
1530 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
1531 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
1532 }
1533 if ((prop_rec_play_enabled) &&
1534 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
1535 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
1536 if (AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1537 // allow VoIP using voice path
1538 // Do nothing
1539 } else if((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1540 ALOGD("voice_conc:MODE_IN_COMM is setforcing deep buffer output for non ULL... flags: %x", flags);
1541 // use deep buffer path for all non ULL outputs
1542 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1543 }
1544 } else if ((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1545 ALOGD("voice_conc:Record mode is on forcing deep buffer output for non ULL... flags: %x ", flags);
1546 // use deep buffer path for all non ULL outputs
1547 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1548 }
1549 }
1550 if (prop_rec_play_enabled &&
1551 (stream == AUDIO_STREAM_ENFORCED_AUDIBLE)) {
1552 ALOGD("Record conc is on forcing ULL output for ENFORCED_AUDIBLE");
1553 flags = AUDIO_OUTPUT_FLAG_FAST;
1554 }
1555#endif
1556
Sharad Sangle4509cef2015-08-19 20:47:12 +05301557#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Sharad Sangle36781612015-05-28 16:15:16 +05301558 /*
1559 * WFD audio routes back to target speaker when starting a ringtone playback.
1560 * This is because primary output is reused for ringtone, so output device is
1561 * updated based on SONIFICATION strategy for both ringtone and music playback.
1562 * The same issue is not seen on remoted_submix HAL based WFD audio because
1563 * primary output is not reused and a new output is created for ringtone playback.
1564 * Issue is fixed by updating output flag to AUDIO_OUTPUT_FLAG_FAST when there is
1565 * a non-music stream playback on WFD, so primary output is not reused for ringtone.
1566 */
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001567 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
1568 if ((availableOutputDeviceTypes & AUDIO_DEVICE_OUT_PROXY)
1569 && (stream != AUDIO_STREAM_MUSIC)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301570 ALOGD("WFD audio: use OUTPUT_FLAG_FAST for non music stream. flags:%x", flags );
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001571 //For voip paths
1572 if(flags & AUDIO_OUTPUT_FLAG_DIRECT)
1573 flags = AUDIO_OUTPUT_FLAG_DIRECT;
1574 else //route every thing else to ULL path
1575 flags = AUDIO_OUTPUT_FLAG_FAST;
1576 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301577#endif
1578
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001579 // open a direct output if required by specified parameters
vivek mehta0ea887a2015-08-26 14:01:20 -07001580 // force direct flag if offload flag is set: offloading implies a direct output stream
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001581 // and all common behaviors are driven by checking only the direct flag
1582 // this should normally be set appropriately in the policy configuration file
1583 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1584 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1585 }
1586 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1587 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1588 }
vivek mehta0ea887a2015-08-26 14:01:20 -07001589
vivek mehta4b0d8192015-10-16 00:25:59 -07001590 bool forced_deep = false;
Sharad Sangle36781612015-05-28 16:15:16 +05301591 // only allow deep buffering for music stream type
1592 if (stream != AUDIO_STREAM_MUSIC) {
1593 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Sharad Sangle497aef82015-08-03 17:55:48 +05301594 } else if (/* stream == AUDIO_STREAM_MUSIC && */
1595 flags == AUDIO_OUTPUT_FLAG_NONE &&
1596 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
vivek mehtac984b992015-11-25 13:28:55 -08001597 forced_deep = true;
Sharad Sangle36781612015-05-28 16:15:16 +05301598 }
Sharad Sangle497aef82015-08-03 17:55:48 +05301599
Sharad Sangle36781612015-05-28 16:15:16 +05301600 if (stream == AUDIO_STREAM_TTS) {
1601 flags = AUDIO_OUTPUT_FLAG_TTS;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001602 }
1603
vivek mehta4b0d8192015-10-16 00:25:59 -07001604 // Do offload magic here
vivek mehtac984b992015-11-25 13:28:55 -08001605 if ((flags == AUDIO_OUTPUT_FLAG_NONE) &&
1606 (stream == AUDIO_STREAM_MUSIC) &&
1607 (offloadInfo != NULL) &&
vivek mehta4b0d8192015-10-16 00:25:59 -07001608 ((offloadInfo->usage == AUDIO_USAGE_MEDIA) || (offloadInfo->usage == AUDIO_USAGE_GAME))) {
vivek mehtac984b992015-11-25 13:28:55 -08001609 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DIRECT_PCM);
vivek mehta4b0d8192015-10-16 00:25:59 -07001610 ALOGD("AudioCustomHAL --> Force Direct Flag .. flag (0x%x)", flags);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301611 }
1612
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001613 sp<IOProfile> profile;
1614
1615 // skip direct output selection if the request can obviously be attached to a mixed output
1616 // and not explicitly requested
vivek mehtac984b992015-11-25 13:28:55 -08001617 if (((flags & (AUDIO_OUTPUT_FLAG_DIRECT|AUDIO_OUTPUT_FLAG_DIRECT_PCM)) == 0) &&
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001618 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
1619 audio_channel_count_from_out_mask(channelMask) <= 2) {
1620 goto non_direct_output;
1621 }
1622
1623 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1624 // creating an offloaded track and tearing it down immediately after start when audioflinger
1625 // detects there is an active non offloadable effect.
1626 // FIXME: We should check the audio session here but we do not have it in this context.
1627 // This may prevent offloading in rare situations where effects are left active by apps
1628 // in the background.
Weiyin Jiang31eefec2016-03-03 14:49:36 +08001629 //
1630 // Supplementary annotation:
1631 // For sake of track offload introduced, we need a rollback for both compress offload
1632 // and track offload use cases.
1633 if ((flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD|AUDIO_OUTPUT_FLAG_DIRECT_PCM)) &&
1634 mEffects.isNonOffloadableEffectEnabled()) {
1635 ALOGD("non offloadable effect is enabled, try with non direct output");
1636 goto non_direct_output;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001637 }
1638
Weiyin Jiang31eefec2016-03-03 14:49:36 +08001639 profile = getProfileForDirectOutput(device,
1640 samplingRate,
1641 format,
1642 channelMask,
1643 (audio_output_flags_t)flags);
1644
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001645 if (profile != 0) {
vivek mehtac984b992015-11-25 13:28:55 -08001646
1647 if (!(flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) &&
1648 (profile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT_PCM)) {
1649 ALOGI("got Direct_PCM without requesting ... reject ");
1650 profile = NULL;
1651 goto non_direct_output;
1652 }
1653
Sharad Sangle36781612015-05-28 16:15:16 +05301654 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001655
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001656 // if multiple concurrent offload decode is supported
1657 // do no check for reuse and also don't close previous output if its offload
1658 // previous output will be closed during track destruction
1659 if (!(property_get_bool("audio.offload.multiple.enabled", false) &&
1660 ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0))) {
1661 for (size_t i = 0; i < mOutputs.size(); i++) {
1662 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1663 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1664 outputDesc = desc;
1665 // reuse direct output if currently open and configured with same parameters
1666 if ((samplingRate == outputDesc->mSamplingRate) &&
1667 (format == outputDesc->mFormat) &&
1668 (channelMask == outputDesc->mChannelMask)) {
1669 outputDesc->mDirectOpenCount++;
1670 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1671 return mOutputs.keyAt(i);
1672 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001673 }
1674 }
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001675 // close direct output if currently open and configured with different parameters
1676 if (outputDesc != NULL) {
1677 closeOutput(outputDesc->mIoHandle);
1678 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001679 }
Sharad Sangle36781612015-05-28 16:15:16 +05301680
1681 // if the selected profile is offloaded and no offload info was specified,
1682 // create a default one
1683 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
1684 if ((profile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
1685 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1686 defaultOffloadInfo.sample_rate = samplingRate;
1687 defaultOffloadInfo.channel_mask = channelMask;
1688 defaultOffloadInfo.format = format;
1689 defaultOffloadInfo.stream_type = stream;
1690 defaultOffloadInfo.bit_rate = 0;
1691 defaultOffloadInfo.duration_us = -1;
1692 defaultOffloadInfo.has_video = true; // conservative
1693 defaultOffloadInfo.is_streaming = true; // likely
1694 offloadInfo = &defaultOffloadInfo;
1695 }
1696
1697 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001698 outputDesc->mDevice = device;
1699 outputDesc->mLatency = 0;
Sharad Sangle36781612015-05-28 16:15:16 +05301700 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001701 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1702 config.sample_rate = samplingRate;
1703 config.channel_mask = channelMask;
1704 config.format = format;
1705 if (offloadInfo != NULL) {
1706 config.offload_info = *offloadInfo;
1707 }
Sharad Sangle36781612015-05-28 16:15:16 +05301708 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001709 &output,
1710 &config,
1711 &outputDesc->mDevice,
1712 String8(""),
1713 &outputDesc->mLatency,
1714 outputDesc->mFlags);
1715
1716 // only accept an output with the requested parameters
1717 if (status != NO_ERROR ||
1718 (samplingRate != 0 && samplingRate != config.sample_rate) ||
1719 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
1720 (channelMask != 0 && channelMask != config.channel_mask)) {
1721 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1722 "format %d %d, channelMask %04x %04x", output, samplingRate,
1723 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1724 outputDesc->mChannelMask);
1725 if (output != AUDIO_IO_HANDLE_NONE) {
1726 mpClientInterface->closeOutput(output);
1727 }
Sharad Sangle36781612015-05-28 16:15:16 +05301728 // fall back to mixer output if possible when the direct output could not be open
1729 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
1730 goto non_direct_output;
1731 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001732 return AUDIO_IO_HANDLE_NONE;
1733 }
1734 outputDesc->mSamplingRate = config.sample_rate;
1735 outputDesc->mChannelMask = config.channel_mask;
1736 outputDesc->mFormat = config.format;
1737 outputDesc->mRefCount[stream] = 0;
1738 outputDesc->mStopTime[stream] = 0;
1739 outputDesc->mDirectOpenCount = 1;
1740
1741 audio_io_handle_t srcOutput = getOutputForEffect();
1742 addOutput(output, outputDesc);
1743 audio_io_handle_t dstOutput = getOutputForEffect();
1744 if (dstOutput == output) {
Gao Jiedb057832015-11-12 08:51:22 +08001745#ifdef DOLBY_ENABLE
1746 status_t status = mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1747 if (status == NO_ERROR) {
1748 for (size_t i = 0; i < mEffects.size(); i++) {
1749 sp<EffectDescriptor> desc = mEffects.valueAt(i);
1750 if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX) {
1751 // update the mIo member of EffectDescriptor for the global effect
1752 ALOGV("%s updating mIo", __FUNCTION__);
1753 desc->mIo = dstOutput;
1754 }
1755 }
1756 } else {
1757 ALOGW("%s moveEffects from %d to %d failed", __FUNCTION__, srcOutput, dstOutput);
1758 }
1759#else // DOLBY_END
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001760 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
Gao Jiedb057832015-11-12 08:51:22 +08001761#endif // LINE_ADDED_BY_DOLBY
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001762 }
1763 mPreviousOutputs = mOutputs;
1764 ALOGV("getOutput() returns new direct output %d", output);
1765 mpClientInterface->onAudioPortListUpdate();
1766 return output;
1767 }
1768
1769non_direct_output:
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001770 // ignoring channel mask due to downmix capability in mixer
1771
1772 // open a non direct output
1773
1774 // for non direct outputs, only PCM is supported
1775 if (audio_is_linear_pcm(format)) {
vivek mehtac984b992015-11-25 13:28:55 -08001776 if (forced_deep) {
1777 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
1778 ALOGI("setting force DEEP buffer now ");
1779 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001780 // get which output is suitable for the specified stream. The actual
1781 // routing change will happen when startOutput() will be called
1782 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1783
1784 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1785 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1786 output = selectOutput(outputs, flags, format);
1787 }
1788 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1789 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1790
vivek mehta0ea887a2015-08-26 14:01:20 -07001791 ALOGV("getOutputForDevice() returns output %d", output);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001792
1793 return output;
1794}
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301795
1796status_t AudioPolicyManagerCustom::getInputForAttr(const audio_attributes_t *attr,
1797 audio_io_handle_t *input,
1798 audio_session_t session,
1799 uid_t uid,
1800 uint32_t samplingRate,
1801 audio_format_t format,
1802 audio_channel_mask_t channelMask,
1803 audio_input_flags_t flags,
1804 audio_port_handle_t selectedDeviceId,
1805 input_type_t *inputType)
1806{
1807 audio_source_t inputSource = attr->source;
1808#ifdef VOICE_CONCURRENCY
1809
1810 char propValue[PROPERTY_VALUE_MAX];
1811 bool prop_rec_enabled=false, prop_voip_enabled = false;
1812
1813 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
1814 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1815 }
1816
1817 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1818 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1819 }
1820
1821 if (prop_rec_enabled && mvoice_call_state) {
1822 //check if voice call is active / running in background
1823 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1824 //Need to block input request
1825 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1826 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1827 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1828 {
1829 switch(inputSource) {
1830 case AUDIO_SOURCE_VOICE_UPLINK:
1831 case AUDIO_SOURCE_VOICE_DOWNLINK:
1832 case AUDIO_SOURCE_VOICE_CALL:
1833 ALOGD("voice_conc:Creating input during incall mode for inputSource: %d",
1834 inputSource);
1835 break;
1836
1837 case AUDIO_SOURCE_VOICE_COMMUNICATION:
1838 if(prop_voip_enabled) {
1839 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1840 inputSource);
1841 return NO_INIT;
1842 }
1843 break;
1844 default:
1845 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1846 inputSource);
1847 return NO_INIT;
1848 }
1849 }
1850 }//check for VoIP flag
1851 else if(prop_voip_enabled && mvoice_call_state) {
1852 //check if voice call is active / running in background
1853 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1854 //Need to block input request
1855 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1856 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1857 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1858 {
1859 if(inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1860 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
1861 return NO_INIT;
1862 }
1863 }
1864 }
1865
1866#endif
1867
1868 return AudioPolicyManager::getInputForAttr(attr,
1869 input,
1870 session,
1871 uid,
1872 samplingRate,
1873 format,
1874 channelMask,
1875 flags,
1876 selectedDeviceId,
1877 inputType);
1878}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08001879
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301880status_t AudioPolicyManagerCustom::startInput(audio_io_handle_t input,
1881 audio_session_t session)
1882{
1883 ALOGV("startInput() input %d", input);
1884 ssize_t index = mInputs.indexOfKey(input);
1885 if (index < 0) {
1886 ALOGW("startInput() unknown input %d", input);
1887 return BAD_VALUE;
1888 }
1889 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1890
1891 index = inputDesc->mSessions.indexOf(session);
1892 if (index < 0) {
1893 ALOGW("startInput() unknown session %d on input %d", session, input);
1894 return BAD_VALUE;
1895 }
1896
1897 // virtual input devices are compatible with other input devices
1898 if (!is_virtual_input_device(inputDesc->mDevice)) {
1899
1900 // for a non-virtual input device, check if there is another (non-virtual) active input
1901 audio_io_handle_t activeInput = mInputs.getActiveInput();
1902 if (activeInput != 0 && activeInput != input) {
1903
1904 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1905 // otherwise the active input continues and the new input cannot be started.
1906 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Mingming Yin60cd7642016-02-12 12:31:41 -08001907 if ((activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) &&
1908 !activeDesc->hasPreemptedSession(session)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301909 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
Mingming Yin60cd7642016-02-12 12:31:41 -08001910 audio_session_t activeSession = activeDesc->mSessions.itemAt(0);
1911 SortedVector<audio_session_t> sessions = activeDesc->getPreemptedSessions();
1912 sessions.add(activeSession);
1913 inputDesc->setPreemptedSessions(sessions);
1914 stopInput(activeInput, activeSession);
1915 releaseInput(activeInput, activeSession);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301916 } else {
1917 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
1918 return INVALID_OPERATION;
1919 }
1920 }
1921 }
1922
1923 // Routing?
1924 mInputRoutes.incRouteActivity(session);
1925#ifdef RECORD_PLAY_CONCURRENCY
1926 mIsInputRequestOnProgress = true;
1927
1928 char getPropValue[PROPERTY_VALUE_MAX];
1929 bool prop_rec_play_enabled = false;
1930
1931 if (property_get("rec.playback.conc.disabled", getPropValue, NULL)) {
1932 prop_rec_play_enabled = atoi(getPropValue) || !strncmp("true", getPropValue, 4);
1933 }
1934
1935 if ((prop_rec_play_enabled) &&(mInputs.activeInputsCount() == 0)){
1936 // send update to HAL on record playback concurrency
1937 AudioParameter param = AudioParameter();
1938 param.add(String8("rec_play_conc_on"), String8("true"));
1939 ALOGD("startInput() setParameters rec_play_conc is setting to ON ");
1940 mpClientInterface->setParameters(0, param.toString());
1941
1942 // Call invalidate to reset all opened non ULL audio tracks
1943 // Move tracks associated to this strategy from previous output to new output
1944 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
1945 // Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder)
Sharad Sangle4509cef2015-08-19 20:47:12 +05301946 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE && (i != AUDIO_STREAM_PATCH))) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301947 ALOGD("Invalidate on releaseInput for stream :: %d ", i);
1948 //FIXME see fixme on name change
1949 mpClientInterface->invalidateStream((audio_stream_type_t)i);
1950 }
1951 }
1952 // close compress tracks
1953 for (size_t i = 0; i < mOutputs.size(); i++) {
1954 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
1955 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
1956 ALOGD("ouput desc / profile is NULL");
1957 continue;
1958 }
1959 if (outputDesc->mProfile->mFlags
1960 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1961 // close compress sessions
1962 ALOGD("calling closeOutput on record conc for COMPRESS output");
1963 closeOutput(mOutputs.keyAt(i));
1964 }
1965 }
1966 }
1967#endif
1968
1969 if (inputDesc->mRefCount == 0 || mInputRoutes.hasRouteChanged(session)) {
1970 // if input maps to a dynamic policy with an activity listener, notify of state change
1971 if ((inputDesc->mPolicyMix != NULL)
1972 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1973 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1974 MIX_STATE_MIXING);
1975 }
1976
1977 if (mInputs.activeInputsCount() == 0) {
1978 SoundTrigger::setCaptureState(true);
1979 }
1980 setInputDevice(input, getNewInputDevice(input), true /* force */);
1981
1982 // automatically enable the remote submix output when input is started if not
1983 // used by a policy mix of type MIX_TYPE_RECORDERS
1984 // For remote submix (a virtual device), we open only one input per capture request.
1985 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1986 String8 address = String8("");
1987 if (inputDesc->mPolicyMix == NULL) {
1988 address = String8("0");
1989 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1990 address = inputDesc->mPolicyMix->mRegistrationId;
1991 }
1992 if (address != "") {
1993 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1994 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1995 address, "remote-submix");
1996 }
1997 }
1998 }
1999
2000 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
2001
2002 inputDesc->mRefCount++;
2003#ifdef RECORD_PLAY_CONCURRENCY
2004 mIsInputRequestOnProgress = false;
2005#endif
2006 return NO_ERROR;
2007}
Weiyin Jiangd211ef52016-02-16 15:55:11 +08002008
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302009status_t AudioPolicyManagerCustom::stopInput(audio_io_handle_t input,
2010 audio_session_t session)
2011{
2012 status_t status;
2013 status = AudioPolicyManager::stopInput(input, session);
2014#ifdef RECORD_PLAY_CONCURRENCY
2015 char propValue[PROPERTY_VALUE_MAX];
2016 bool prop_rec_play_enabled = false;
2017
2018 if (property_get("rec.playback.conc.disabled", propValue, NULL)) {
2019 prop_rec_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
2020 }
2021
2022 if ((prop_rec_play_enabled) && (mInputs.activeInputsCount() == 0)) {
2023
2024 //send update to HAL on record playback concurrency
2025 AudioParameter param = AudioParameter();
2026 param.add(String8("rec_play_conc_on"), String8("false"));
2027 ALOGD("stopInput() setParameters rec_play_conc is setting to OFF ");
2028 mpClientInterface->setParameters(0, param.toString());
2029
2030 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
2031 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
2032 //Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder stop tone)
2033 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE) && (i != AUDIO_STREAM_PATCH)) {
2034 ALOGD(" Invalidate on stopInput for stream :: %d ", i);
2035 //FIXME see fixme on name change
2036 mpClientInterface->invalidateStream((audio_stream_type_t)i);
2037 }
2038 }
2039 }
2040#endif
2041 return status;
2042}
2043
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302044void AudioPolicyManagerCustom::closeAllInputs() {
2045 bool patchRemoved = false;
2046
Dhanalakshmi Siddanibfeca7f2015-10-08 15:17:11 +05302047 for(size_t input_index = mInputs.size(); input_index > 0; input_index--) {
2048 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index-1);
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302049 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
2050 if (patch_index >= 0) {
2051 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
2052 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2053 mAudioPatches.removeItemsAt(patch_index);
2054 patchRemoved = true;
2055 }
2056 if ((inputDesc->mIsSoundTrigger) && (mInputs.size() == 1)) {
2057 ALOGD("Do not close sound trigger input handle");
2058 } else {
Dhanalakshmi Siddanibfeca7f2015-10-08 15:17:11 +05302059 mpClientInterface->closeInput(mInputs.keyAt(input_index-1));
2060 mInputs.removeItem(mInputs.keyAt(input_index-1));
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302061 }
2062 }
2063 nextAudioPortGeneration();
2064
2065 if (patchRemoved) {
2066 mpClientInterface->onAudioPatchListUpdate();
2067 }
2068}
2069
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302070AudioPolicyManagerCustom::AudioPolicyManagerCustom(AudioPolicyClientInterface *clientInterface)
Sharad Sangle4509cef2015-08-19 20:47:12 +05302071 : AudioPolicyManager(clientInterface),
2072 mHdmiAudioDisabled(false),
2073 mHdmiAudioEvent(false),
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08002074 mPrevPhoneState(0),
Haynes Mathew George603ae9b2015-12-21 17:23:59 -08002075 mPrevFMVolumeDb(0.0f),
2076 mFMIsActive(false)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302077{
Mingming Yin38ea08c2015-10-05 15:24:04 -07002078 char ssr_enabled[PROPERTY_VALUE_MAX] = {0};
2079 bool prop_ssr_enabled = false;
2080
2081 if (property_get("ro.qc.sdk.audio.ssr", ssr_enabled, NULL)) {
2082 prop_ssr_enabled = atoi(ssr_enabled) || !strncmp("true", ssr_enabled, 4);
2083 }
2084
2085 for (size_t i = 0; i < mHwModules.size(); i++) {
2086 ALOGV("Hw module %d", i);
2087 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) {
2088 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
2089 ALOGV("Input profile ", j);
2090 for (size_t k = 0; k < inProfile->mChannelMasks.size(); k++) {
2091 audio_channel_mask_t channelMask =
2092 inProfile->mChannelMasks.itemAt(k);
2093 ALOGV("Channel Mask %x size %d", channelMask,
2094 inProfile->mChannelMasks.size());
2095 if (AUDIO_CHANNEL_IN_5POINT1 == channelMask) {
2096 if (!prop_ssr_enabled) {
2097 ALOGI("removing AUDIO_CHANNEL_IN_5POINT1 from"
2098 " input profile as SSR(surround sound record)"
2099 " is not supported on this chipset variant");
2100 inProfile->mChannelMasks.removeItemsAt(k, 1);
2101 ALOGV("Channel Mask size now %d",
2102 inProfile->mChannelMasks.size());
2103 }
2104 }
2105 }
2106 }
2107 }
2108
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302109#ifdef RECORD_PLAY_CONCURRENCY
2110 mIsInputRequestOnProgress = false;
2111#endif
2112
2113
2114#ifdef VOICE_CONCURRENCY
2115 mFallBackflag = getFallBackPath();
2116#endif
2117}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07002118}