blob: ac814cd78430d551ca3a23488af59e953c318957 [file] [log] [blame]
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001/*
Sharad Sangle36781612015-05-28 16:15:16 +05302 * Copyright (c) 2013-2015, The Linux Foundation. All rights reserved.
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07003 * Not a contribution.
4 *
5 * Copyright (C) 2009 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Gao Jiedb057832015-11-12 08:51:22 +080018 *
19 * This file was modified by Dolby Laboratories, Inc. The portions of the
20 * code that are surrounded by "DOLBY..." are copyrighted and
21 * licensed separately, as follows:
22 *
23 * (C) 2015 Dolby Laboratories, Inc.
24 *
25 * Licensed under the Apache License, Version 2.0 (the "License");
26 * you may not use this file except in compliance with the License.
27 * You may obtain a copy of the License at
28 *
29 * http://www.apache.org/licenses/LICENSE-2.0
30 *
31 * Unless required by applicable law or agreed to in writing, software
32 * distributed under the License is distributed on an "AS IS" BASIS,
33 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34 * See the License for the specific language governing permissions and
35 * limitations under the License.
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070036 */
37
Sharad Sangle36781612015-05-28 16:15:16 +053038#define LOG_TAG "AudioPolicyManagerCustom"
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070039//#define LOG_NDEBUG 0
40
41//#define VERY_VERBOSE_LOGGING
42#ifdef VERY_VERBOSE_LOGGING
43#define ALOGVV ALOGV
44#else
45#define ALOGVV(a...) do { } while(0)
46#endif
Sharad Sangle36781612015-05-28 16:15:16 +053047
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070048// A device mask for all audio output devices that are considered "remote" when evaluating
49// active output devices in isStreamActiveRemotely()
50#define APM_AUDIO_OUT_DEVICE_REMOTE_ALL AUDIO_DEVICE_OUT_REMOTE_SUBMIX
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070051// A device mask for all audio input and output devices where matching inputs/outputs on device
52// type alone is not enough: the address must match too
53#define APM_AUDIO_DEVICE_MATCH_ADDRESS_ALL (AUDIO_DEVICE_IN_REMOTE_SUBMIX | \
54 AUDIO_DEVICE_OUT_REMOTE_SUBMIX)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070055
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070056#include <inttypes.h>
Mingming Yin0ae14ea2014-07-09 17:55:56 -070057#include <math.h>
Mingming Yin0670f162014-06-12 16:05:49 -070058
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070059#include <cutils/properties.h>
60#include <utils/Log.h>
61#include <hardware/audio.h>
62#include <hardware/audio_effect.h>
63#include <media/AudioParameter.h>
64#include <soundtrigger/SoundTrigger.h>
65#include "AudioPolicyManager.h"
Sharad Sangle36781612015-05-28 16:15:16 +053066#include <policy.h>
Gao Jiedb057832015-11-12 08:51:22 +080067#ifdef DOLBY_ENABLE
68#include "DolbyAudioPolicy_impl.h"
69#endif // DOLBY_END
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070070
71namespace android {
Sharad Sanglec5766ff2015-06-04 20:24:10 +053072#ifdef VOICE_CONCURRENCY
73audio_output_flags_t AudioPolicyManagerCustom::getFallBackPath()
74{
75 audio_output_flags_t flag = AUDIO_OUTPUT_FLAG_FAST;
76 char propValue[PROPERTY_VALUE_MAX];
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070077
Sharad Sanglec5766ff2015-06-04 20:24:10 +053078 if (property_get("voice.conc.fallbackpath", propValue, NULL)) {
79 if (!strncmp(propValue, "deep-buffer", 11)) {
80 flag = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
81 }
82 else if (!strncmp(propValue, "fast", 4)) {
83 flag = AUDIO_OUTPUT_FLAG_FAST;
84 }
85 else {
86 ALOGD("voice_conc:not a recognised path(%s) in prop voice.conc.fallbackpath",
87 propValue);
88 }
89 }
90 else {
91 ALOGD("voice_conc:prop voice.conc.fallbackpath not set");
92 }
93
94 ALOGD("voice_conc:picked up flag(0x%x) from prop voice.conc.fallbackpath",
95 flag);
96
97 return flag;
98}
99#endif /*VOICE_CONCURRENCY*/
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700100// ----------------------------------------------------------------------------
101// AudioPolicyInterface implementation
102// ----------------------------------------------------------------------------
Sharad Sangle36781612015-05-28 16:15:16 +0530103extern "C" AudioPolicyInterface* createAudioPolicyManager(
104 AudioPolicyClientInterface *clientInterface)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700105{
Sharad Sangle36781612015-05-28 16:15:16 +0530106 return new AudioPolicyManagerCustom(clientInterface);
107}
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700108
Sharad Sangle36781612015-05-28 16:15:16 +0530109extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface)
110{
111 delete interface;
112}
113
114status_t AudioPolicyManagerCustom::setDeviceConnectionStateInt(audio_devices_t device,
115 audio_policy_dev_state_t state,
116 const char *device_address,
117 const char *device_name)
118{
119 ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
120 device, state, device_address, device_name);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700121
122 // connect/disconnect only 1 device at a time
123 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
124
Sharad Sangle36781612015-05-28 16:15:16 +0530125 sp<DeviceDescriptor> devDesc =
126 mHwModules.getDeviceDescriptor(device, device_address, device_name);
127
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700128 // handle output devices
129 if (audio_is_output_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700130 SortedVector <audio_io_handle_t> outputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700131
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700132 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700133
134 // save a copy of the opened output descriptors before any output is opened or closed
135 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
136 mPreviousOutputs = mOutputs;
137 switch (state)
138 {
139 // handle output device connection
Sharad Sangle36781612015-05-28 16:15:16 +0530140 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700141 if (index >= 0) {
Sharad Sangle4509cef2015-08-19 20:47:12 +0530142#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
143 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
144 if (!strncmp(device_address, "hdmi_spkr", 9)) {
145 mHdmiAudioDisabled = false;
146 } else {
147 mHdmiAudioEvent = true;
148 }
149 }
150#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700151 ALOGW("setDeviceConnectionState() device already connected: %x", device);
152 return INVALID_OPERATION;
153 }
154 ALOGV("setDeviceConnectionState() connecting device %x", device);
155
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700156 // register new device as available
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700157 index = mAvailableOutputDevices.add(devDesc);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530158#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
159 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
160 if (!strncmp(device_address, "hdmi_spkr", 9)) {
161 mHdmiAudioDisabled = false;
162 } else {
163 mHdmiAudioEvent = true;
164 }
165 if (mHdmiAudioDisabled || !mHdmiAudioEvent) {
166 mAvailableOutputDevices.remove(devDesc);
167 ALOGW("HDMI sink not connected, do not route audio to HDMI out");
168 return INVALID_OPERATION;
169 }
170 }
171#endif
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700172 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530173 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700174 if (module == 0) {
175 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
176 device);
177 mAvailableOutputDevices.remove(devDesc);
178 return INVALID_OPERATION;
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700179 }
Sharad Sangle36781612015-05-28 16:15:16 +0530180 mAvailableOutputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700181 } else {
182 return NO_MEMORY;
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700183 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700184
Sharad Sangle36781612015-05-28 16:15:16 +0530185 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700186 mAvailableOutputDevices.remove(devDesc);
187 return INVALID_OPERATION;
188 }
Sharad Sangle36781612015-05-28 16:15:16 +0530189 // Propagate device availability to Engine
190 mEngine->setDeviceConnectionState(devDesc, state);
191
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700192 // outputs should never be empty here
193 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
194 "checkOutputsForDevice() returned no outputs but status OK");
195 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
196 outputs.size());
Sharad Sangle36781612015-05-28 16:15:16 +0530197
198 // Send connect to HALs
199 AudioParameter param = AudioParameter(devDesc->mAddress);
200 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
201 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
202
203 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700204 // handle output device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700205 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
206 if (index < 0) {
Sharad Sangle4509cef2015-08-19 20:47:12 +0530207#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
208 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
209 if (!strncmp(device_address, "hdmi_spkr", 9)) {
210 mHdmiAudioDisabled = true;
211 } else {
212 mHdmiAudioEvent = false;
213 }
214 }
215#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700216 ALOGW("setDeviceConnectionState() device not connected: %x", device);
217 return INVALID_OPERATION;
218 }
219
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700220 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
221
Sharad Sangle36781612015-05-28 16:15:16 +0530222 // Send Disconnect to HALs
223 AudioParameter param = AudioParameter(devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700224 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
225 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
226
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700227 // remove device from available output devices
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700228 mAvailableOutputDevices.remove(devDesc);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530229#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
230 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
231 if (!strncmp(device_address, "hdmi_spkr", 9)) {
232 mHdmiAudioDisabled = true;
233 } else {
234 mHdmiAudioEvent = false;
235 }
236 }
237#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530238 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
239
240 // Propagate device availability to Engine
241 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700242 } break;
243
244 default:
245 ALOGE("setDeviceConnectionState() invalid state: %x", state);
246 return BAD_VALUE;
247 }
248
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700249 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
250 // output is suspended before any tracks are moved to it
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700251 checkA2dpSuspend();
252 checkOutputForAllStrategies();
253 // outputs must be closed after checkOutputForAllStrategies() is executed
254 if (!outputs.isEmpty()) {
255 for (size_t i = 0; i < outputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530256 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700257 // close unused outputs after device disconnection or direct outputs that have been
258 // opened by checkOutputsForDevice() to query dynamic parameters
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700259 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700260 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
261 (desc->mDirectOpenCount == 0))) {
262 closeOutput(outputs[i]);
263 }
264 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700265 // check again after closing A2DP output to reset mA2dpSuspended if needed
266 checkA2dpSuspend();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700267 }
268
269 updateDevicesAndOutputs();
Gao Jiedb057832015-11-12 08:51:22 +0800270#ifdef DOLBY_ENABLE
271 // Before closing the opened outputs, update endpoint property with device capabilities
272 audio_devices_t audioOutputDevice = getDeviceForStrategy(getStrategy(AUDIO_STREAM_MUSIC), true);
273 mDolbyAudioPolicy.setEndpointSystemProperty(audioOutputDevice, mHwModules);
274#endif // DOLBY_END
Sharad Sangle36781612015-05-28 16:15:16 +0530275 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
276 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700277 updateCallRouting(newDevice);
278 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530279
280#ifdef FM_POWER_OPT
281 // handle FM device connection state to trigger FM AFE loopback
282 if(device == AUDIO_DEVICE_OUT_FM && hasPrimaryOutput()) {
283 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
284 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
285 mPrimaryOutput->changeRefCount(AUDIO_STREAM_MUSIC, 1);
286 newDevice = newDevice | AUDIO_DEVICE_OUT_FM;
287 } else {
288 mPrimaryOutput->changeRefCount(AUDIO_STREAM_MUSIC, -1);
289 }
290 AudioParameter param = AudioParameter();
291 param.addInt(String8("handle_fm"), (int)newDevice);
292 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString());
293 }
294#endif /* FM_POWER_OPT end */
295
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700296 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530297 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
298 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
299 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700300 // do not force device change on duplicated output because if device is 0, it will
301 // also force a device 0 for the two outputs it is duplicated to which may override
302 // a valid device selection on those outputs.
Sharad Sangle36781612015-05-28 16:15:16 +0530303 bool force = !desc->isDuplicated()
304 && (!device_distinguishes_on_address(device)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700305 // always force when disconnecting (a non-duplicated device)
306 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Sharad Sangle36781612015-05-28 16:15:16 +0530307 setOutputDevice(desc, newDevice, force, 0);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700308 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700309 }
310
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700311 mpClientInterface->onAudioPortListUpdate();
312 return NO_ERROR;
313 } // end if is output device
314
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700315 // handle input devices
316 if (audio_is_input_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700317 SortedVector <audio_io_handle_t> inputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700318
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700319 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700320 switch (state)
321 {
322 // handle input device connection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700323 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
324 if (index >= 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700325 ALOGW("setDeviceConnectionState() device already connected: %d", device);
326 return INVALID_OPERATION;
327 }
Sharad Sangle36781612015-05-28 16:15:16 +0530328 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700329 if (module == NULL) {
330 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
331 device);
332 return INVALID_OPERATION;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700333 }
Sharad Sangle36781612015-05-28 16:15:16 +0530334 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700335 return INVALID_OPERATION;
336 }
337
338 index = mAvailableInputDevices.add(devDesc);
339 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530340 mAvailableInputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700341 } else {
342 return NO_MEMORY;
343 }
Sharad Sangle36781612015-05-28 16:15:16 +0530344
345 // Set connect to HALs
346 AudioParameter param = AudioParameter(devDesc->mAddress);
347 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
348 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
349
350 // Propagate device availability to Engine
351 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700352 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700353
354 // handle input device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700355 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
356 if (index < 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700357 ALOGW("setDeviceConnectionState() device not connected: %d", device);
358 return INVALID_OPERATION;
359 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700360
361 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
362
363 // Set Disconnect to HALs
Sharad Sangle36781612015-05-28 16:15:16 +0530364 AudioParameter param = AudioParameter(devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700365 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
366 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
367
Sharad Sangle36781612015-05-28 16:15:16 +0530368 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700369 mAvailableInputDevices.remove(devDesc);
370
Sharad Sangle36781612015-05-28 16:15:16 +0530371 // Propagate device availability to Engine
372 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700373 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700374
375 default:
376 ALOGE("setDeviceConnectionState() invalid state: %x", state);
377 return BAD_VALUE;
378 }
379
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700380 closeAllInputs();
381
Sharad Sangle36781612015-05-28 16:15:16 +0530382 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700383 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
384 updateCallRouting(newDevice);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700385 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700386
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700387 mpClientInterface->onAudioPortListUpdate();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700388 return NO_ERROR;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700389 } // end if is input device
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700390
391 ALOGW("setDeviceConnectionState() invalid device: %x", device);
392 return BAD_VALUE;
393}
Sharad Sangle36781612015-05-28 16:15:16 +0530394// This function checks for the parameters which can be offloaded.
395// This can be enhanced depending on the capability of the DSP and policy
396// of the system.
397bool AudioPolicyManagerCustom::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700398{
Sharad Sangle36781612015-05-28 16:15:16 +0530399 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
400 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
401 offloadInfo.sample_rate, offloadInfo.channel_mask,
402 offloadInfo.format,
403 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
404 offloadInfo.has_video);
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530405#ifdef VOICE_CONCURRENCY
406 char concpropValue[PROPERTY_VALUE_MAX];
407 if (property_get("voice.playback.conc.disabled", concpropValue, NULL)) {
408 bool propenabled = atoi(concpropValue) || !strncmp("true", concpropValue, 4);
409 if (propenabled) {
410 if (isInCall())
411 {
412 ALOGD("\n copl: blocking compress offload on call mode\n");
413 return false;
414 }
415 }
416 }
417#endif
418#ifdef RECORD_PLAY_CONCURRENCY
419 char recConcPropValue[PROPERTY_VALUE_MAX];
420 bool prop_rec_play_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700421
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530422 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
423 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
424 }
425
426 if ((prop_rec_play_enabled) &&
427 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
428 ALOGD("copl: blocking compress offload for record concurrency");
429 return false;
430 }
431#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530432 // Check if stream type is music, then only allow offload as of now.
433 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
434 {
435 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
436 return false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700437 }
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530438
Alexy Joseph0ac09032015-10-16 15:57:53 -0700439 // Check if offload has been disabled
440 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
441 if (offloadDisabled) {
442 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
443 return false;
444 }
445
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530446 char propValue[PROPERTY_VALUE_MAX];
447 bool pcmOffload = false;
448#ifdef PCM_OFFLOAD_ENABLED
449 if ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM_OFFLOAD) {
450 bool prop_enabled = false;
451 if ((AUDIO_FORMAT_PCM_16_BIT_OFFLOAD == offloadInfo.format) &&
452 property_get("audio.offload.pcm.16bit.enable", propValue, NULL)) {
453 prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
454 }
455
456#ifdef PCM_OFFLOAD_ENABLED_24
457 if ((AUDIO_FORMAT_PCM_24_BIT_OFFLOAD == offloadInfo.format) &&
458 property_get("audio.offload.pcm.24bit.enable", propValue, NULL)) {
459 prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530460 }
461#endif
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530462
463 if (prop_enabled) {
464 ALOGI("PCM offload property is enabled");
465 pcmOffload = true;
466 }
467
468 if (!pcmOffload) {
469 ALOGD("system property not enabled for PCM offload format[%x]",offloadInfo.format);
470 return false;
471 }
472 }
473#endif
474 if (!pcmOffload) {
Alexy Joseph0ac09032015-10-16 15:57:53 -0700475
476 bool compressedOffloadDisabled = property_get_bool("audio.offload.compress.disable", false);
477 if (compressedOffloadDisabled) {
478 ALOGI("compressed offload disabled by audio.offload.compress.disable=%d", compressedOffloadDisabled);
479 return false;
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530480 }
Alexy Joseph0ac09032015-10-16 15:57:53 -0700481
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530482 //check if it's multi-channel AAC (includes sub formats) and FLAC format
483 if ((popcount(offloadInfo.channel_mask) > 2) &&
484 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
485 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS))) {
486 ALOGD("offload disabled for multi-channel AAC,FLAC and VORBIS format");
487 return false;
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530488 }
489
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530490#ifdef AUDIO_EXTN_FORMATS_ENABLED
491 //check if it's multi-channel FLAC/ALAC/WMA format with sample rate > 48k
492 if ((popcount(offloadInfo.channel_mask) > 2) &&
493 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
Manish Dewangana6fc5442015-08-24 20:30:31 +0530494 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) && (offloadInfo.sample_rate > 48000)) ||
495 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) && (offloadInfo.sample_rate > 48000)) ||
496 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) && (offloadInfo.sample_rate > 48000)) ||
497 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS))) {
498 ALOGD("offload disabled for multi-channel FLAC/ALAC/WMA/AAC_ADTS clips with sample rate > 48kHz");
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530499 return false;
500 }
501#endif
502 //TODO: enable audio offloading with video when ready
503 const bool allowOffloadWithVideo =
504 property_get_bool("audio.offload.video", false /* default_value */);
505 if (offloadInfo.has_video && !allowOffloadWithVideo) {
506 ALOGV("isOffloadSupported: has_video == true, returning false");
507 return false;
508 }
Manish Dewanganf3cd0f82015-10-13 14:04:36 +0530509
510 const bool allowOffloadStreamingWithVideo = property_get_bool("av.streaming.offload.enable",
511 false /*default value*/);
512 if(offloadInfo.has_video && offloadInfo.is_streaming && !allowOffloadStreamingWithVideo) {
513 ALOGW("offload disabled by av.streaming.offload.enable = %s ", propValue );
514 return false;
515 }
516
Sharad Sangle36781612015-05-28 16:15:16 +0530517 }
518
519 //If duration is less than minimum value defined in property, return false
520 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
521 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
522 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
523 return false;
524 }
525 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
526 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
527 //duration checks only valid for MP3/AAC/ formats,
528 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
529 if ((offloadInfo.format == AUDIO_FORMAT_MP3) ||
530 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530531 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS) ||
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530532#ifdef AUDIO_EXTN_FORMATS_ENABLED
Sharad Sangle36781612015-05-28 16:15:16 +0530533 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
Sharad Sangle36781612015-05-28 16:15:16 +0530534 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) ||
535 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) ||
536 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) ||
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530537 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_APE) ||
Manish Dewangana6fc5442015-08-24 20:30:31 +0530538 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS) ||
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530539#endif
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530540 pcmOffload)
Sharad Sangle36781612015-05-28 16:15:16 +0530541 return false;
542
543 }
544
545 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
546 // creating an offloaded track and tearing it down immediately after start when audioflinger
547 // detects there is an active non offloadable effect.
548 // FIXME: We should check the audio session here but we do not have it in this context.
549 // This may prevent offloading in rare situations where effects are left active by apps
550 // in the background.
551 if (mEffects.isNonOffloadableEffectEnabled()) {
552 return false;
553 }
554 // Check for soundcard status
555 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
556 String8("SND_CARD_STATUS"));
557 AudioParameter result = AudioParameter(valueStr);
558 int isonline = 0;
559 if ((result.getInt(String8("SND_CARD_STATUS"), isonline) == NO_ERROR)
560 && !isonline) {
561 ALOGD("copl: soundcard is offline rejecting offload request");
562 return false;
563 }
564 // See if there is a profile to support this.
565 // AUDIO_DEVICE_NONE
566 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
567 offloadInfo.sample_rate,
568 offloadInfo.format,
569 offloadInfo.channel_mask,
570 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
571 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
572 return (profile != 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700573}
Sharad Sangle36781612015-05-28 16:15:16 +0530574audio_devices_t AudioPolicyManagerCustom::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
575 bool fromCache)
576{
577 audio_devices_t device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700578
Sharad Sangle36781612015-05-28 16:15:16 +0530579 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
580 if (index >= 0) {
581 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
582 if (patchDesc->mUid != mUidCached) {
583 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
584 outputDesc->device(), outputDesc->mPatchHandle);
585 return outputDesc->device();
586 }
587 }
588
589 // check the following by order of priority to request a routing change if necessary:
590 // 1: the strategy enforced audible is active and enforced on the output:
591 // use device for strategy enforced audible
592 // 2: we are in call or the strategy phone is active on the output:
593 // use device for strategy phone
594 // 3: the strategy for enforced audible is active but not enforced on the output:
595 // use the device for strategy enforced audible
596 // 4: the strategy sonification is active on the output:
597 // use device for strategy sonification
598 // 5: the strategy "respectful" sonification is active on the output:
599 // use device for strategy "respectful" sonification
600 // 6: the strategy accessibility is active on the output:
601 // use device for strategy accessibility
602 // 7: the strategy media is active on the output:
603 // use device for strategy media
604 // 8: the strategy DTMF is active on the output:
605 // use device for strategy DTMF
606 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
607 // use device for strategy t-t-s
608 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
609 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
610 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
611 } else if (isInCall() ||
612 isStrategyActive(outputDesc, STRATEGY_PHONE)||
613 isStrategyActive(mPrimaryOutput, STRATEGY_PHONE)) {
614 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
615 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
616 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
617 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)||
618 (isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION)
619 && (!isStrategyActive(mPrimaryOutput,STRATEGY_MEDIA)))) {
620 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530621 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL) ||
622 isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION_RESPECTFUL)) {
Sharad Sangle36781612015-05-28 16:15:16 +0530623 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
624 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
625 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
626 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
627 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
628 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
629 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
630 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
631 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
632 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
633 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
634 }
635
636 ALOGV("getNewOutputDevice() selected device %x", device);
637 return device;
638}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700639void AudioPolicyManagerCustom::setPhoneState(audio_mode_t state)
640{
Sharad Sangle36781612015-05-28 16:15:16 +0530641 ALOGV("setPhoneState() state %d", state);
642 // store previous phone state for management of sonification strategy below
Sharad Sangle4509cef2015-08-19 20:47:12 +0530643 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Sharad Sangle36781612015-05-28 16:15:16 +0530644 int oldState = mEngine->getPhoneState();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700645
Sharad Sangle36781612015-05-28 16:15:16 +0530646 if (mEngine->setPhoneState(state) != NO_ERROR) {
647 ALOGW("setPhoneState() invalid or same state %d", state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700648 return;
649 }
Sharad Sangle36781612015-05-28 16:15:16 +0530650 /// Opens: can these line be executed after the switch of volume curves???
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700651 // if leaving call state, handle special case of active streams
652 // pertaining to sonification strategy see handleIncallSonification()
653 if (isInCall()) {
654 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530655 for (size_t j = 0; j < mOutputs.size(); j++) {
656 audio_io_handle_t curOutput = mOutputs.keyAt(j);
657 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
658 if (stream == AUDIO_STREAM_PATCH) {
659 continue;
660 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530661 handleIncallSonification((audio_stream_type_t)stream, false, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +0530662 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700663 }
Sharad Sangle36781612015-05-28 16:15:16 +0530664
665 // force reevaluating accessibility routing when call starts
666 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700667 }
668
Sharad Sangle36781612015-05-28 16:15:16 +0530669 /**
670 * Switching to or from incall state or switching between telephony and VoIP lead to force
671 * routing command.
672 */
673 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
674 || (is_state_in_call(state) && (state != oldState)));
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700675
676 // check for device and output changes triggered by new phone state
677 checkA2dpSuspend();
678 checkOutputForAllStrategies();
679 updateDevicesAndOutputs();
680
Sharad Sangle36781612015-05-28 16:15:16 +0530681 sp<SwAudioOutputDescriptor> hwOutputDesc = mPrimaryOutput;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530682#ifdef VOICE_CONCURRENCY
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530683 char propValue[PROPERTY_VALUE_MAX];
684 bool prop_playback_enabled = false, prop_rec_enabled=false, prop_voip_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700685
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530686 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
687 prop_playback_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
688 }
689
690 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
691 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
692 }
693
694 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
695 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
696 }
697
Dhananjay Kumar9125b2a2015-12-01 16:24:09 +0530698 if ((AUDIO_MODE_IN_CALL != oldState) && (AUDIO_MODE_IN_CALL == state)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530699 ALOGD("voice_conc:Entering to call mode oldState :: %d state::%d ",
700 oldState, state);
Dhananjay Kumar9125b2a2015-12-01 16:24:09 +0530701 mvoice_call_state = state;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530702 if (prop_rec_enabled) {
703 //Close all active inputs
704 audio_io_handle_t activeInput = mInputs.getActiveInput();
705 if (activeInput != 0) {
706 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
707 switch(activeDesc->mInputSource) {
708 case AUDIO_SOURCE_VOICE_UPLINK:
709 case AUDIO_SOURCE_VOICE_DOWNLINK:
710 case AUDIO_SOURCE_VOICE_CALL:
711 ALOGD("voice_conc:FOUND active input during call active: %d",activeDesc->mInputSource);
712 break;
713
714 case AUDIO_SOURCE_VOICE_COMMUNICATION:
715 if(prop_voip_enabled) {
716 ALOGD("voice_conc:CLOSING VoIP input source on call setup :%d ",activeDesc->mInputSource);
717 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
718 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
719 }
720 break;
721
722 default:
723 ALOGD("voice_conc:CLOSING input on call setup for inputSource: %d",activeDesc->mInputSource);
724 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
725 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
726 break;
727 }
728 }
729 } else if (prop_voip_enabled) {
730 audio_io_handle_t activeInput = mInputs.getActiveInput();
731 if (activeInput != 0) {
732 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
733 if (AUDIO_SOURCE_VOICE_COMMUNICATION == activeDesc->mInputSource) {
734 ALOGD("voice_conc:CLOSING VoIP on call setup : %d",activeDesc->mInputSource);
735 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
736 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
737 }
738 }
739 }
740 if (prop_playback_enabled) {
741 // Move tracks associated to this strategy from previous output to new output
742 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
743 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
744 if (i == AUDIO_STREAM_PATCH) {
745 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
746 continue;
747 }
748 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
749 if ((AUDIO_STREAM_MUSIC == i) ||
750 (AUDIO_STREAM_VOICE_CALL == i) ) {
751 ALOGD("voice_conc:Invalidate stream type %d", i);
752 mpClientInterface->invalidateStream((audio_stream_type_t)i);
753 }
754 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
755 ALOGD("voice_conc:Invalidate stream type %d", i);
756 mpClientInterface->invalidateStream((audio_stream_type_t)i);
757 }
758 }
759 }
760
761 for (size_t i = 0; i < mOutputs.size(); i++) {
762 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
763 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
764 ALOGD("voice_conc:ouput desc / profile is NULL");
765 continue;
766 }
767
768 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
769 if (((!outputDesc->isDuplicated() &&outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY))
770 && prop_playback_enabled) {
771 ALOGD("voice_conc:calling suspendOutput on call mode for primary output");
772 mpClientInterface->suspendOutput(mOutputs.keyAt(i));
773 } //Close compress all sessions
774 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
775 && prop_playback_enabled) {
776 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
777 closeOutput(mOutputs.keyAt(i));
778 }
779 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_VOIP_RX)
780 && prop_voip_enabled) {
781 ALOGD("voice_conc:calling closeOutput on call mode for DIRECT output");
782 closeOutput(mOutputs.keyAt(i));
783 }
784 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
785 if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)
786 && prop_playback_enabled) {
787 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
788 closeOutput(mOutputs.keyAt(i));
789 }
790 }
791 }
792 }
793
794 if ((AUDIO_MODE_IN_CALL == oldState || AUDIO_MODE_IN_COMMUNICATION == oldState) &&
795 (AUDIO_MODE_NORMAL == state) && prop_playback_enabled && mvoice_call_state) {
796 ALOGD("voice_conc:EXITING from call mode oldState :: %d state::%d \n",oldState, state);
797 mvoice_call_state = 0;
798 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
799 //restore PCM (deep-buffer) output after call termination
800 for (size_t i = 0; i < mOutputs.size(); i++) {
801 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
802 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
803 ALOGD("voice_conc:ouput desc / profile is NULL");
804 continue;
805 }
806 if (!outputDesc->isDuplicated() && outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
807 ALOGD("voice_conc:calling restoreOutput after call mode for primary output");
808 mpClientInterface->restoreOutput(mOutputs.keyAt(i));
809 }
810 }
811 }
812 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
813 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
814 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
815 if (i == AUDIO_STREAM_PATCH) {
816 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
817 continue;
818 }
819 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
820 if ((AUDIO_STREAM_MUSIC == i) ||
821 (AUDIO_STREAM_VOICE_CALL == i) ) {
822 mpClientInterface->invalidateStream((audio_stream_type_t)i);
823 }
824 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
825 mpClientInterface->invalidateStream((audio_stream_type_t)i);
826 }
827 }
828 }
829
830#endif
831#ifdef RECORD_PLAY_CONCURRENCY
832 char recConcPropValue[PROPERTY_VALUE_MAX];
833 bool prop_rec_play_enabled = false;
834
835 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
836 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
837 }
838 if (prop_rec_play_enabled) {
839 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
840 ALOGD("phone state changed to MODE_IN_COMM invlaidating music and voice streams");
841 // call invalidate for voice streams, so that it can use deepbuffer with VoIP out device from HAL
842 mpClientInterface->invalidateStream(AUDIO_STREAM_VOICE_CALL);
843 // call invalidate for music, so that compress will fallback to deep-buffer with VoIP out device
844 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
845
846 // close compress output to make sure session will be closed before timeout(60sec)
847 for (size_t i = 0; i < mOutputs.size(); i++) {
848
849 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
850 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
851 ALOGD("ouput desc / profile is NULL");
852 continue;
853 }
854
855 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
856 ALOGD("calling closeOutput on call mode for COMPRESS output");
857 closeOutput(mOutputs.keyAt(i));
858 }
859 }
860 } else if ((oldState == AUDIO_MODE_IN_COMMUNICATION) &&
861 (mEngine->getPhoneState() == AUDIO_MODE_NORMAL)) {
862 // call invalidate for music so that music can fallback to compress
863 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
864 }
865 }
866#endif
867 mPrevPhoneState = oldState;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700868 int delayMs = 0;
869 if (isStateInCall(state)) {
870 nsecs_t sysTime = systemTime();
871 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530872 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700873 // mute media and sonification strategies and delay device switch by the largest
874 // latency of any output where either strategy is active.
875 // This avoid sending the ring tone or music tail into the earpiece or headset.
Sharad Sangle36781612015-05-28 16:15:16 +0530876 if ((isStrategyActive(desc, STRATEGY_MEDIA,
877 SONIFICATION_HEADSET_MUSIC_DELAY,
878 sysTime) ||
879 isStrategyActive(desc, STRATEGY_SONIFICATION,
880 SONIFICATION_HEADSET_MUSIC_DELAY,
881 sysTime)) &&
882 (delayMs < (int)desc->latency()*2)) {
883 delayMs = desc->latency()*2;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700884 }
Sharad Sangle36781612015-05-28 16:15:16 +0530885 setStrategyMute(STRATEGY_MEDIA, true, desc);
886 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700887 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Sharad Sangle36781612015-05-28 16:15:16 +0530888 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
889 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700890 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
891 }
892 }
893
Sharad Sangle36781612015-05-28 16:15:16 +0530894 if (hasPrimaryOutput()) {
895 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
896 // the device returned is not necessarily reachable via this output
897 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
898 // force routing command to audio hardware when ending call
899 // even if no device change is needed
900 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
901 rxDevice = mPrimaryOutput->device();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700902 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700903
Sharad Sangle36781612015-05-28 16:15:16 +0530904 if (state == AUDIO_MODE_IN_CALL) {
905 updateCallRouting(rxDevice, delayMs);
906 } else if (oldState == AUDIO_MODE_IN_CALL) {
907 if (mCallRxPatch != 0) {
908 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
909 mCallRxPatch.clear();
910 }
911 if (mCallTxPatch != 0) {
912 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
913 mCallTxPatch.clear();
914 }
915 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
916 } else {
917 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700918 }
919 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530920 //update device for all non-primary outputs
921 for (size_t i = 0; i < mOutputs.size(); i++) {
922 audio_io_handle_t output = mOutputs.keyAt(i);
923 if (output != mPrimaryOutput->mIoHandle) {
924 newDevice = getNewOutputDevice(mOutputs.valueFor(output), false /*fromCache*/);
925 setOutputDevice(mOutputs.valueFor(output), newDevice, (newDevice != AUDIO_DEVICE_NONE));
926 }
927 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700928 // if entering in call state, handle special case of active streams
929 // pertaining to sonification strategy see handleIncallSonification()
930 if (isStateInCall(state)) {
931 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530932 for (size_t j = 0; j < mOutputs.size(); j++) {
933 audio_io_handle_t curOutput = mOutputs.keyAt(j);
934 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
935 if (stream == AUDIO_STREAM_PATCH) {
936 continue;
937 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530938 handleIncallSonification((audio_stream_type_t)stream, true, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +0530939 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700940 }
941 }
942
943 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
944 if (state == AUDIO_MODE_RINGTONE &&
945 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
946 mLimitRingtoneVolume = true;
947 } else {
948 mLimitRingtoneVolume = false;
949 }
950}
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +0530951
952void AudioPolicyManagerCustom::setForceUse(audio_policy_force_use_t usage,
953 audio_policy_forced_cfg_t config)
954{
955 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
956
957 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
958 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
959 return;
960 }
961 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
962 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
963 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
964
965 // check for device and output changes triggered by new force usage
966 checkA2dpSuspend();
967 checkOutputForAllStrategies();
968 updateDevicesAndOutputs();
969 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
970 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
971 updateCallRouting(newDevice);
972 }
973 // Use reverse loop to make sure any low latency usecases (generally tones)
974 // are not routed before non LL usecases (generally music).
975 // We can safely assume that LL output would always have lower index,
976 // and use this work-around to avoid routing of output with music stream
977 // from the context of short lived LL output.
978 // Note: in case output's share backend(HAL sharing is implicit) all outputs
979 // gets routing update while processing first output itself.
980 for (size_t i = mOutputs.size(); i > 0; i--) {
981 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i-1);
982 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
983 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || outputDesc != mPrimaryOutput) {
984 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
985 }
986 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
987 applyStreamVolumes(outputDesc, newDevice, 0, true);
988 }
989 }
990
991 audio_io_handle_t activeInput = mInputs.getActiveInput();
992 if (activeInput != 0) {
993 setInputDevice(activeInput, getNewInputDevice(activeInput));
994 }
995
996}
997
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +0530998status_t AudioPolicyManagerCustom::stopSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +0530999 audio_stream_type_t stream,
1000 bool forceDeviceUpdate)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001001{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301002 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1003 ALOGW("stopSource() invalid stream %d", stream);
1004 return INVALID_OPERATION;
1005 }
Sharad Sangle36781612015-05-28 16:15:16 +05301006 // always handle stream stop, check which stream type is stopping
1007 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001008
Sharad Sangle36781612015-05-28 16:15:16 +05301009 // handle special case for sonification while in call
Sharad Sangle4509cef2015-08-19 20:47:12 +05301010 if (isInCall() && (outputDesc->mRefCount[stream] == 1)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301011 if (outputDesc->isDuplicated()) {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301012 handleIncallSonification(stream, false, false, outputDesc->subOutput1()->mIoHandle);
1013 handleIncallSonification(stream, false, false, outputDesc->subOutput2()->mIoHandle);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001014 }
Sharad Sangle36781612015-05-28 16:15:16 +05301015 handleIncallSonification(stream, false, false, outputDesc->mIoHandle);
1016 }
1017
1018 if (outputDesc->mRefCount[stream] > 0) {
1019 // decrement usage count of this stream on the output
1020 outputDesc->changeRefCount(stream, -1);
1021
1022 // store time at which the stream was stopped - see isStreamActive()
1023 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
1024 outputDesc->mStopTime[stream] = systemTime();
Zhou Song5dcddc92015-09-21 14:36:57 +08001025 audio_devices_t prevDevice = outputDesc->device();
Sharad Sangle36781612015-05-28 16:15:16 +05301026 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
1027 // delay the device switch by twice the latency because stopOutput() is executed when
1028 // the track stop() command is received and at that time the audio track buffer can
1029 // still contain data that needs to be drained. The latency only covers the audio HAL
1030 // and kernel buffers. Also the latency does not always include additional delay in the
1031 // audio path (audio DSP, CODEC ...)
1032 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
1033
1034 // force restoring the device selection on other active outputs if it differs from the
1035 // one being selected for this output
1036 for (size_t i = 0; i < mOutputs.size(); i++) {
1037 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1038 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1039 if (desc != outputDesc &&
1040 desc->isActive() &&
1041 outputDesc->sharesHwModuleWith(desc) &&
1042 (newDevice != desc->device())) {
Sharad Sangle4509cef2015-08-19 20:47:12 +05301043 audio_devices_t dev = getNewOutputDevice(mOutputs.valueFor(curOutput), false /*fromCache*/);
Zhou Song5dcddc92015-09-21 14:36:57 +08001044 uint32_t delayMs;
1045 if (dev == prevDevice) {
1046 delayMs = 0;
1047 } else {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301048 delayMs = outputDesc->latency()*2;
Zhou Song5dcddc92015-09-21 14:36:57 +08001049 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301050 setOutputDevice(desc,
1051 dev,
Sharad Sangle36781612015-05-28 16:15:16 +05301052 true,
Zhou Song5dcddc92015-09-21 14:36:57 +08001053 delayMs);
Sharad Sangle36781612015-05-28 16:15:16 +05301054 }
1055 }
1056 // update the outputs if stopping one with a stream that can affect notification routing
1057 handleNotificationRoutingForStream(stream);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001058 }
Sharad Sangle36781612015-05-28 16:15:16 +05301059 return NO_ERROR;
1060 } else {
1061 ALOGW("stopOutput() refcount is already 0");
1062 return INVALID_OPERATION;
1063 }
1064}
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301065status_t AudioPolicyManagerCustom::startSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301066 audio_stream_type_t stream,
1067 audio_devices_t device,
1068 uint32_t *delayMs)
1069{
1070 // cannot start playback of STREAM_TTS if any other output is being used
1071 uint32_t beaconMuteLatency = 0;
1072
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301073 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1074 ALOGW("startSource() invalid stream %d", stream);
1075 return INVALID_OPERATION;
1076 }
1077
Sharad Sangle36781612015-05-28 16:15:16 +05301078 *delayMs = 0;
1079 if (stream == AUDIO_STREAM_TTS) {
1080 ALOGV("\t found BEACON stream");
1081 if (mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
1082 return INVALID_OPERATION;
1083 } else {
1084 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001085 }
Sharad Sangle36781612015-05-28 16:15:16 +05301086 } else {
1087 // some playback other than beacon starts
1088 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1089 }
1090
1091 // increment usage count for this stream on the requested output:
1092 // NOTE that the usage count is the same for duplicated output and hardware output which is
1093 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1094 outputDesc->changeRefCount(stream, 1);
1095
1096 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
1097 // starting an output being rerouted?
1098 if (device == AUDIO_DEVICE_NONE) {
1099 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001100 }
Sharad Sangle36781612015-05-28 16:15:16 +05301101 routing_strategy strategy = getStrategy(stream);
1102 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
1103 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1104 (beaconMuteLatency > 0);
1105 uint32_t waitMs = beaconMuteLatency;
1106 bool force = false;
1107 for (size_t i = 0; i < mOutputs.size(); i++) {
1108 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1109 if (desc != outputDesc) {
1110 // force a device change if any other output is managed by the same hw
1111 // module and has a current device selection that differs from selected device.
1112 // In this case, the audio HAL must receive the new device selection so that it can
1113 // change the device currently selected by the other active output.
1114 if (outputDesc->sharesHwModuleWith(desc) &&
1115 desc->device() != device) {
1116 force = true;
1117 }
1118 // wait for audio on other active outputs to be presented when starting
1119 // a notification so that audio focus effect can propagate, or that a mute/unmute
1120 // event occurred for beacon
1121 uint32_t latency = desc->latency();
1122 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1123 waitMs = latency;
1124 }
1125 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001126 }
Sharad Sangle36781612015-05-28 16:15:16 +05301127 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force);
1128
1129 // handle special case for sonification while in call
1130 if (isInCall()) {
1131 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001132 }
Sharad Sangle36781612015-05-28 16:15:16 +05301133
1134 // apply volume rules for current stream and device if necessary
1135 checkAndSetVolume(stream,
1136 mStreams.valueFor(stream).getVolumeIndex(device),
1137 outputDesc,
1138 device);
1139
1140 // update the outputs if starting an output with a stream that can affect notification
1141 // routing
1142 handleNotificationRoutingForStream(stream);
1143
1144 // force reevaluating accessibility routing when ringtone or alarm starts
1145 if (strategy == STRATEGY_SONIFICATION) {
1146 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1147 }
1148 }
1149 else {
1150 // handle special case for sonification while in call
1151 if (isInCall()) {
1152 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
1153 }
1154 }
1155 return NO_ERROR;
1156}
1157void AudioPolicyManagerCustom::handleIncallSonification(audio_stream_type_t stream,
1158 bool starting, bool stateChange,
1159 audio_io_handle_t output)
1160{
1161 if(!hasPrimaryOutput()) {
1162 return;
1163 }
1164 // no action needed for AUDIO_STREAM_PATCH stream type, it's for internal flinger tracks
1165 if (stream == AUDIO_STREAM_PATCH) {
1166 return;
1167 }
1168 // if the stream pertains to sonification strategy and we are in call we must
1169 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
1170 // in the device used for phone strategy and play the tone if the selected device does not
1171 // interfere with the device used for phone strategy
1172 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
1173 // many times as there are active tracks on the output
1174 const routing_strategy stream_strategy = getStrategy(stream);
1175 if ((stream_strategy == STRATEGY_SONIFICATION) ||
1176 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
1177 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
1178 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
1179 stream, starting, outputDesc->mDevice, stateChange);
1180 if (outputDesc->mRefCount[stream]) {
1181 int muteCount = 1;
1182 if (stateChange) {
1183 muteCount = outputDesc->mRefCount[stream];
1184 }
1185 if (audio_is_low_visibility(stream)) {
1186 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
1187 for (int i = 0; i < muteCount; i++) {
1188 setStreamMute(stream, starting, outputDesc);
1189 }
1190 } else {
1191 ALOGV("handleIncallSonification() high visibility");
1192 if (outputDesc->device() &
1193 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
1194 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
1195 for (int i = 0; i < muteCount; i++) {
1196 setStreamMute(stream, starting, outputDesc);
1197 }
1198 }
1199 if (starting) {
1200 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
1201 AUDIO_STREAM_VOICE_CALL);
1202 } else {
1203 mpClientInterface->stopTone();
1204 }
1205 }
1206 }
1207 }
1208}
1209void AudioPolicyManagerCustom::handleNotificationRoutingForStream(audio_stream_type_t stream) {
1210 switch(stream) {
1211 case AUDIO_STREAM_MUSIC:
1212 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
1213 updateDevicesAndOutputs();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001214 break;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001215 default:
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001216 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001217 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001218}
Sharad Sangle36781612015-05-28 16:15:16 +05301219status_t AudioPolicyManagerCustom::checkAndSetVolume(audio_stream_type_t stream,
1220 int index,
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301221 const sp<AudioOutputDescriptor>& outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301222 audio_devices_t device,
1223 int delayMs, bool force)
1224{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301225 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1226 ALOGW("checkAndSetVolume() invalid stream %d", stream);
1227 return INVALID_OPERATION;
1228 }
Sharad Sangle36781612015-05-28 16:15:16 +05301229 // do not change actual stream volume if the stream is muted
1230 if (outputDesc->mMuteCount[stream] != 0) {
1231 ALOGVV("checkAndSetVolume() stream %d muted count %d",
1232 stream, outputDesc->mMuteCount[stream]);
1233 return NO_ERROR;
1234 }
1235 audio_policy_forced_cfg_t forceUseForComm =
1236 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
1237 // do not change in call volume if bluetooth is connected and vice versa
1238 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
1239 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
1240 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
1241 stream, forceUseForComm);
1242 return INVALID_OPERATION;
1243 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001244
Sharad Sangle36781612015-05-28 16:15:16 +05301245 if (device == AUDIO_DEVICE_NONE) {
1246 device = outputDesc->device();
1247 }
1248
1249 float volumeDb = computeVolume(stream, index, device);
1250 if (outputDesc->isFixedVolume(device)) {
1251 volumeDb = 0.0f;
1252 }
1253
1254 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
1255
1256 if (stream == AUDIO_STREAM_VOICE_CALL ||
1257 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
1258 float voiceVolume;
1259 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
1260 if (stream == AUDIO_STREAM_VOICE_CALL) {
1261 voiceVolume = (float)index/(float)mStreams.valueFor(stream).getVolumeIndexMax();
1262 } else {
1263 voiceVolume = 1.0;
1264 }
1265
1266 if (voiceVolume != mLastVoiceVolume && ((outputDesc == mPrimaryOutput) ||
1267 isDirectOutput(outputDesc->mIoHandle) || device & AUDIO_DEVICE_OUT_ALL_USB)) {
1268 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
1269 mLastVoiceVolume = voiceVolume;
1270 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301271#ifdef FM_POWER_OPT
1272 } else if (stream == AUDIO_STREAM_MUSIC && hasPrimaryOutput() &&
1273 outputDesc == mPrimaryOutput) {
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001274 /* Avoid unnecessary set_parameter calls as it puts the primary
1275 outputs FastMixer in HOT_IDLE leading to breaks in audio */
1276 if (volumeDb != mPrevFMVolumeDb) {
1277 mPrevFMVolumeDb = volumeDb;
1278 AudioParameter param = AudioParameter();
1279 param.addFloat(String8("fm_volume"), Volume::DbToAmpl(volumeDb));
1280 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString(), delayMs);
1281 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301282#endif /* FM_POWER_OPT end */
Sharad Sangle36781612015-05-28 16:15:16 +05301283 }
1284
1285 return NO_ERROR;
1286}
1287bool AudioPolicyManagerCustom::isDirectOutput(audio_io_handle_t output) {
1288 for (size_t i = 0; i < mOutputs.size(); i++) {
1289 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1290 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1291 if ((curOutput == output) && (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
1292 return true;
1293 }
1294 }
1295 return false;
1296}
vivek mehta0ea887a2015-08-26 14:01:20 -07001297
1298status_t AudioPolicyManagerCustom::getOutputForAttr(const audio_attributes_t *attr,
1299 audio_io_handle_t *output,
1300 audio_session_t session,
1301 audio_stream_type_t *stream,
1302 uid_t uid,
1303 uint32_t samplingRate,
1304 audio_format_t format,
1305 audio_channel_mask_t channelMask,
1306 audio_output_flags_t flags,
1307 audio_port_handle_t selectedDeviceId,
1308 const audio_offload_info_t *offloadInfo)
1309{
1310 audio_offload_info_t tOffloadInfo = AUDIO_INFO_INITIALIZER;
1311
Alexy Joseph0ac09032015-10-16 15:57:53 -07001312 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
1313 bool pcmOffloadEnabled = false;
1314
1315 if (offloadDisabled) {
1316 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
1317 }
1318
1319 //read track offload property only if the global offload switch is off.
1320 if (!offloadDisabled) {
1321 pcmOffloadEnabled = property_get_bool("audio.offload.track.enable", false);
1322 }
vivek mehta0ea887a2015-08-26 14:01:20 -07001323
1324 if (offloadInfo == NULL && pcmOffloadEnabled) {
1325 tOffloadInfo.sample_rate = samplingRate;
1326 tOffloadInfo.channel_mask = channelMask;
1327 tOffloadInfo.format = format;
1328 tOffloadInfo.stream_type = *stream;
1329 tOffloadInfo.bit_width = 16; //hard coded for PCM_16
1330 if (attr != NULL) {
1331 ALOGV("found attribute .. setting usage %d ", attr->usage);
1332 tOffloadInfo.usage = attr->usage;
1333 } else {
Alexy Joseph0ac09032015-10-16 15:57:53 -07001334 ALOGI("%s:: attribute is NULL .. no usage set", __func__);
vivek mehta0ea887a2015-08-26 14:01:20 -07001335 }
1336 offloadInfo = &tOffloadInfo;
1337 }
1338
1339 return AudioPolicyManager::getOutputForAttr(attr, output, session, stream,
1340 (uid_t)uid, (uint32_t)samplingRate,
1341 format, (audio_channel_mask_t)channelMask,
1342 flags, (audio_port_handle_t)selectedDeviceId,
1343 offloadInfo);
1344}
1345
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001346audio_io_handle_t AudioPolicyManagerCustom::getOutputForDevice(
1347 audio_devices_t device,
Sharad Sangle36781612015-05-28 16:15:16 +05301348 audio_session_t session __unused,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001349 audio_stream_type_t stream,
1350 uint32_t samplingRate,
1351 audio_format_t format,
1352 audio_channel_mask_t channelMask,
1353 audio_output_flags_t flags,
1354 const audio_offload_info_t *offloadInfo)
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001355{
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001356 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
1357 uint32_t latency = 0;
1358 status_t status;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001359
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001360#ifdef AUDIO_POLICY_TEST
1361 if (mCurOutput != 0) {
1362 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1363 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1364
1365 if (mTestOutputs[mCurOutput] == 0) {
1366 ALOGV("getOutput() opening test output");
Sharad Sangle36781612015-05-28 16:15:16 +05301367 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
1368 mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001369 outputDesc->mDevice = mTestDevice;
1370 outputDesc->mLatency = mTestLatencyMs;
1371 outputDesc->mFlags =
1372 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
1373 outputDesc->mRefCount[stream] = 0;
1374 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1375 config.sample_rate = mTestSamplingRate;
1376 config.channel_mask = mTestChannels;
1377 config.format = mTestFormat;
1378 if (offloadInfo != NULL) {
1379 config.offload_info = *offloadInfo;
1380 }
1381 status = mpClientInterface->openOutput(0,
1382 &mTestOutputs[mCurOutput],
1383 &config,
1384 &outputDesc->mDevice,
1385 String8(""),
1386 &outputDesc->mLatency,
1387 outputDesc->mFlags);
1388 if (status == NO_ERROR) {
1389 outputDesc->mSamplingRate = config.sample_rate;
1390 outputDesc->mFormat = config.format;
1391 outputDesc->mChannelMask = config.channel_mask;
1392 AudioParameter outputCmd = AudioParameter();
1393 outputCmd.addInt(String8("set_id"),mCurOutput);
1394 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1395 addOutput(mTestOutputs[mCurOutput], outputDesc);
1396 }
1397 }
1398 return mTestOutputs[mCurOutput];
1399 }
1400#endif //AUDIO_POLICY_TEST
Sharad Sangle36781612015-05-28 16:15:16 +05301401 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1402 (stream != AUDIO_STREAM_MUSIC)) {
1403 // compress should not be used for non-music streams
1404 ALOGE("Offloading only allowed with music stream");
1405 return 0;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301406 }
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301407
1408 if ((stream == AUDIO_STREAM_VOICE_CALL) &&
1409 (channelMask == 1) &&
1410 (samplingRate == 8000 || samplingRate == 16000)) {
1411 // Allow Voip direct output only if:
1412 // audio mode is MODE_IN_COMMUNCATION; AND
1413 // voip output is not opened already; AND
1414 // requested sample rate matches with that of voip input stream (if opened already)
1415 int value = 0;
1416 uint32_t mode = 0, voipOutCount = 1, voipSampleRate = 1;
1417 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1418 String8("audio_mode"));
1419 AudioParameter result = AudioParameter(valueStr);
1420 if (result.getInt(String8("audio_mode"), value) == NO_ERROR) {
1421 mode = value;
1422 }
1423
1424 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1425 String8("voip_out_stream_count"));
1426 result = AudioParameter(valueStr);
1427 if (result.getInt(String8("voip_out_stream_count"), value) == NO_ERROR) {
1428 voipOutCount = value;
1429 }
1430
1431 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1432 String8("voip_sample_rate"));
1433 result = AudioParameter(valueStr);
1434 if (result.getInt(String8("voip_sample_rate"), value) == NO_ERROR) {
1435 voipSampleRate = value;
1436 }
1437
1438 if ((mode == AUDIO_MODE_IN_COMMUNICATION) && (voipOutCount == 0) &&
1439 ((voipSampleRate == 0) || (voipSampleRate == samplingRate))) {
1440 if (audio_is_linear_pcm(format)) {
1441 char propValue[PROPERTY_VALUE_MAX] = {0};
1442 property_get("use.voice.path.for.pcm.voip", propValue, "0");
1443 bool voipPcmSysPropEnabled = !strncmp("true", propValue, sizeof("true"));
1444 if (voipPcmSysPropEnabled && (format == AUDIO_FORMAT_PCM_16_BIT)) {
Karthik Reddy Katta3fb9be72015-11-25 11:18:27 +05301445 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX |
1446 AUDIO_OUTPUT_FLAG_DIRECT);
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301447 ALOGD("Set VoIP and Direct output flags for PCM format");
1448 }
1449 }
1450 }
1451 }
1452
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301453#ifdef VOICE_CONCURRENCY
1454 char propValue[PROPERTY_VALUE_MAX];
1455 bool prop_play_enabled=false, prop_voip_enabled = false;
1456
1457 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1458 prop_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001459 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301460
1461 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1462 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1463 }
1464
1465 if (prop_play_enabled && mvoice_call_state) {
1466 //check if voice call is active / running in background
1467 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1468 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1469 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1470 {
1471 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1472 if(prop_voip_enabled) {
1473 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1474 flags );
1475 return 0;
1476 }
1477 }
1478 else {
1479 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
1480 ALOGD("voice_conc:IN call mode adding ULL flags .. flags: %x ", flags );
1481 flags = AUDIO_OUTPUT_FLAG_FAST;
1482 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
1483 if (AUDIO_STREAM_MUSIC == stream) {
1484 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1485 ALOGD("voice_conc:IN call mode adding deep-buffer flags %x ", flags );
1486 }
1487 else {
1488 flags = AUDIO_OUTPUT_FLAG_FAST;
1489 ALOGD("voice_conc:IN call mode adding fast flags %x ", flags );
1490 }
1491 }
1492 }
1493 }
1494 } else if (prop_voip_enabled && mvoice_call_state) {
1495 //check if voice call is active / running in background
1496 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1497 //return only ULL ouput
1498 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1499 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1500 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1501 {
1502 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1503 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1504 flags );
1505 return 0;
1506 }
1507 }
1508 }
1509#endif
1510#ifdef RECORD_PLAY_CONCURRENCY
1511 char recConcPropValue[PROPERTY_VALUE_MAX];
1512 bool prop_rec_play_enabled = false;
1513
1514 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
1515 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
1516 }
1517 if ((prop_rec_play_enabled) &&
1518 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
1519 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
1520 if (AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1521 // allow VoIP using voice path
1522 // Do nothing
1523 } else if((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1524 ALOGD("voice_conc:MODE_IN_COMM is setforcing deep buffer output for non ULL... flags: %x", flags);
1525 // use deep buffer path for all non ULL outputs
1526 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1527 }
1528 } else if ((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1529 ALOGD("voice_conc:Record mode is on forcing deep buffer output for non ULL... flags: %x ", flags);
1530 // use deep buffer path for all non ULL outputs
1531 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1532 }
1533 }
1534 if (prop_rec_play_enabled &&
1535 (stream == AUDIO_STREAM_ENFORCED_AUDIBLE)) {
1536 ALOGD("Record conc is on forcing ULL output for ENFORCED_AUDIBLE");
1537 flags = AUDIO_OUTPUT_FLAG_FAST;
1538 }
1539#endif
1540
Sharad Sangle4509cef2015-08-19 20:47:12 +05301541#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Sharad Sangle36781612015-05-28 16:15:16 +05301542 /*
1543 * WFD audio routes back to target speaker when starting a ringtone playback.
1544 * This is because primary output is reused for ringtone, so output device is
1545 * updated based on SONIFICATION strategy for both ringtone and music playback.
1546 * The same issue is not seen on remoted_submix HAL based WFD audio because
1547 * primary output is not reused and a new output is created for ringtone playback.
1548 * Issue is fixed by updating output flag to AUDIO_OUTPUT_FLAG_FAST when there is
1549 * a non-music stream playback on WFD, so primary output is not reused for ringtone.
1550 */
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001551 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
1552 if ((availableOutputDeviceTypes & AUDIO_DEVICE_OUT_PROXY)
1553 && (stream != AUDIO_STREAM_MUSIC)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301554 ALOGD("WFD audio: use OUTPUT_FLAG_FAST for non music stream. flags:%x", flags );
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001555 //For voip paths
1556 if(flags & AUDIO_OUTPUT_FLAG_DIRECT)
1557 flags = AUDIO_OUTPUT_FLAG_DIRECT;
1558 else //route every thing else to ULL path
1559 flags = AUDIO_OUTPUT_FLAG_FAST;
1560 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301561#endif
1562
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001563 // open a direct output if required by specified parameters
vivek mehta0ea887a2015-08-26 14:01:20 -07001564 // force direct flag if offload flag is set: offloading implies a direct output stream
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001565 // and all common behaviors are driven by checking only the direct flag
1566 // this should normally be set appropriately in the policy configuration file
1567 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1568 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1569 }
1570 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1571 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1572 }
vivek mehta0ea887a2015-08-26 14:01:20 -07001573
vivek mehta4b0d8192015-10-16 00:25:59 -07001574 bool forced_deep = false;
Sharad Sangle36781612015-05-28 16:15:16 +05301575 // only allow deep buffering for music stream type
1576 if (stream != AUDIO_STREAM_MUSIC) {
1577 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Sharad Sangle497aef82015-08-03 17:55:48 +05301578 } else if (/* stream == AUDIO_STREAM_MUSIC && */
1579 flags == AUDIO_OUTPUT_FLAG_NONE &&
1580 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
vivek mehtac984b992015-11-25 13:28:55 -08001581 forced_deep = true;
Sharad Sangle36781612015-05-28 16:15:16 +05301582 }
Sharad Sangle497aef82015-08-03 17:55:48 +05301583
Sharad Sangle36781612015-05-28 16:15:16 +05301584 if (stream == AUDIO_STREAM_TTS) {
1585 flags = AUDIO_OUTPUT_FLAG_TTS;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001586 }
1587
vivek mehta4b0d8192015-10-16 00:25:59 -07001588 // Do offload magic here
vivek mehtac984b992015-11-25 13:28:55 -08001589 if ((flags == AUDIO_OUTPUT_FLAG_NONE) &&
1590 (stream == AUDIO_STREAM_MUSIC) &&
1591 (offloadInfo != NULL) &&
vivek mehta4b0d8192015-10-16 00:25:59 -07001592 ((offloadInfo->usage == AUDIO_USAGE_MEDIA) || (offloadInfo->usage == AUDIO_USAGE_GAME))) {
vivek mehtac984b992015-11-25 13:28:55 -08001593 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DIRECT_PCM);
vivek mehta4b0d8192015-10-16 00:25:59 -07001594 ALOGD("AudioCustomHAL --> Force Direct Flag .. flag (0x%x)", flags);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301595 }
1596
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001597 sp<IOProfile> profile;
1598
1599 // skip direct output selection if the request can obviously be attached to a mixed output
1600 // and not explicitly requested
vivek mehtac984b992015-11-25 13:28:55 -08001601 if (((flags & (AUDIO_OUTPUT_FLAG_DIRECT|AUDIO_OUTPUT_FLAG_DIRECT_PCM)) == 0) &&
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001602 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
1603 audio_channel_count_from_out_mask(channelMask) <= 2) {
1604 goto non_direct_output;
1605 }
1606
1607 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1608 // creating an offloaded track and tearing it down immediately after start when audioflinger
1609 // detects there is an active non offloadable effect.
1610 // FIXME: We should check the audio session here but we do not have it in this context.
1611 // This may prevent offloading in rare situations where effects are left active by apps
1612 // in the background.
1613
Sharad Sangle36781612015-05-28 16:15:16 +05301614 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
1615 !mEffects.isNonOffloadableEffectEnabled()) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001616 profile = getProfileForDirectOutput(device,
1617 samplingRate,
1618 format,
1619 channelMask,
1620 (audio_output_flags_t)flags);
1621 }
1622
1623 if (profile != 0) {
vivek mehtac984b992015-11-25 13:28:55 -08001624
1625 if (!(flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) &&
1626 (profile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT_PCM)) {
1627 ALOGI("got Direct_PCM without requesting ... reject ");
1628 profile = NULL;
1629 goto non_direct_output;
1630 }
1631
Sharad Sangle36781612015-05-28 16:15:16 +05301632 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001633
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001634 // if multiple concurrent offload decode is supported
1635 // do no check for reuse and also don't close previous output if its offload
1636 // previous output will be closed during track destruction
1637 if (!(property_get_bool("audio.offload.multiple.enabled", false) &&
1638 ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0))) {
1639 for (size_t i = 0; i < mOutputs.size(); i++) {
1640 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1641 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1642 outputDesc = desc;
1643 // reuse direct output if currently open and configured with same parameters
1644 if ((samplingRate == outputDesc->mSamplingRate) &&
1645 (format == outputDesc->mFormat) &&
1646 (channelMask == outputDesc->mChannelMask)) {
1647 outputDesc->mDirectOpenCount++;
1648 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1649 return mOutputs.keyAt(i);
1650 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001651 }
1652 }
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001653 // close direct output if currently open and configured with different parameters
1654 if (outputDesc != NULL) {
1655 closeOutput(outputDesc->mIoHandle);
1656 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001657 }
Sharad Sangle36781612015-05-28 16:15:16 +05301658
1659 // if the selected profile is offloaded and no offload info was specified,
1660 // create a default one
1661 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
1662 if ((profile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
1663 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1664 defaultOffloadInfo.sample_rate = samplingRate;
1665 defaultOffloadInfo.channel_mask = channelMask;
1666 defaultOffloadInfo.format = format;
1667 defaultOffloadInfo.stream_type = stream;
1668 defaultOffloadInfo.bit_rate = 0;
1669 defaultOffloadInfo.duration_us = -1;
1670 defaultOffloadInfo.has_video = true; // conservative
1671 defaultOffloadInfo.is_streaming = true; // likely
1672 offloadInfo = &defaultOffloadInfo;
1673 }
1674
1675 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001676 outputDesc->mDevice = device;
1677 outputDesc->mLatency = 0;
Sharad Sangle36781612015-05-28 16:15:16 +05301678 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001679 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1680 config.sample_rate = samplingRate;
1681 config.channel_mask = channelMask;
1682 config.format = format;
1683 if (offloadInfo != NULL) {
1684 config.offload_info = *offloadInfo;
1685 }
Sharad Sangle36781612015-05-28 16:15:16 +05301686 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001687 &output,
1688 &config,
1689 &outputDesc->mDevice,
1690 String8(""),
1691 &outputDesc->mLatency,
1692 outputDesc->mFlags);
1693
1694 // only accept an output with the requested parameters
1695 if (status != NO_ERROR ||
1696 (samplingRate != 0 && samplingRate != config.sample_rate) ||
1697 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
1698 (channelMask != 0 && channelMask != config.channel_mask)) {
1699 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1700 "format %d %d, channelMask %04x %04x", output, samplingRate,
1701 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1702 outputDesc->mChannelMask);
1703 if (output != AUDIO_IO_HANDLE_NONE) {
1704 mpClientInterface->closeOutput(output);
1705 }
Sharad Sangle36781612015-05-28 16:15:16 +05301706 // fall back to mixer output if possible when the direct output could not be open
1707 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
1708 goto non_direct_output;
1709 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001710 return AUDIO_IO_HANDLE_NONE;
1711 }
1712 outputDesc->mSamplingRate = config.sample_rate;
1713 outputDesc->mChannelMask = config.channel_mask;
1714 outputDesc->mFormat = config.format;
1715 outputDesc->mRefCount[stream] = 0;
1716 outputDesc->mStopTime[stream] = 0;
1717 outputDesc->mDirectOpenCount = 1;
1718
1719 audio_io_handle_t srcOutput = getOutputForEffect();
1720 addOutput(output, outputDesc);
1721 audio_io_handle_t dstOutput = getOutputForEffect();
1722 if (dstOutput == output) {
Gao Jiedb057832015-11-12 08:51:22 +08001723#ifdef DOLBY_ENABLE
1724 status_t status = mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1725 if (status == NO_ERROR) {
1726 for (size_t i = 0; i < mEffects.size(); i++) {
1727 sp<EffectDescriptor> desc = mEffects.valueAt(i);
1728 if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX) {
1729 // update the mIo member of EffectDescriptor for the global effect
1730 ALOGV("%s updating mIo", __FUNCTION__);
1731 desc->mIo = dstOutput;
1732 }
1733 }
1734 } else {
1735 ALOGW("%s moveEffects from %d to %d failed", __FUNCTION__, srcOutput, dstOutput);
1736 }
1737#else // DOLBY_END
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001738 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
Gao Jiedb057832015-11-12 08:51:22 +08001739#endif // LINE_ADDED_BY_DOLBY
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001740 }
1741 mPreviousOutputs = mOutputs;
1742 ALOGV("getOutput() returns new direct output %d", output);
1743 mpClientInterface->onAudioPortListUpdate();
1744 return output;
1745 }
1746
1747non_direct_output:
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001748 // ignoring channel mask due to downmix capability in mixer
1749
1750 // open a non direct output
1751
1752 // for non direct outputs, only PCM is supported
1753 if (audio_is_linear_pcm(format)) {
vivek mehtac984b992015-11-25 13:28:55 -08001754 if (forced_deep) {
1755 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
1756 ALOGI("setting force DEEP buffer now ");
1757 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001758 // get which output is suitable for the specified stream. The actual
1759 // routing change will happen when startOutput() will be called
1760 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1761
1762 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1763 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1764 output = selectOutput(outputs, flags, format);
1765 }
1766 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1767 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1768
vivek mehta0ea887a2015-08-26 14:01:20 -07001769 ALOGV("getOutputForDevice() returns output %d", output);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001770
1771 return output;
1772}
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301773
1774status_t AudioPolicyManagerCustom::getInputForAttr(const audio_attributes_t *attr,
1775 audio_io_handle_t *input,
1776 audio_session_t session,
1777 uid_t uid,
1778 uint32_t samplingRate,
1779 audio_format_t format,
1780 audio_channel_mask_t channelMask,
1781 audio_input_flags_t flags,
1782 audio_port_handle_t selectedDeviceId,
1783 input_type_t *inputType)
1784{
1785 audio_source_t inputSource = attr->source;
1786#ifdef VOICE_CONCURRENCY
1787
1788 char propValue[PROPERTY_VALUE_MAX];
1789 bool prop_rec_enabled=false, prop_voip_enabled = false;
1790
1791 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
1792 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1793 }
1794
1795 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1796 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1797 }
1798
1799 if (prop_rec_enabled && mvoice_call_state) {
1800 //check if voice call is active / running in background
1801 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1802 //Need to block input request
1803 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1804 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1805 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1806 {
1807 switch(inputSource) {
1808 case AUDIO_SOURCE_VOICE_UPLINK:
1809 case AUDIO_SOURCE_VOICE_DOWNLINK:
1810 case AUDIO_SOURCE_VOICE_CALL:
1811 ALOGD("voice_conc:Creating input during incall mode for inputSource: %d",
1812 inputSource);
1813 break;
1814
1815 case AUDIO_SOURCE_VOICE_COMMUNICATION:
1816 if(prop_voip_enabled) {
1817 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1818 inputSource);
1819 return NO_INIT;
1820 }
1821 break;
1822 default:
1823 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1824 inputSource);
1825 return NO_INIT;
1826 }
1827 }
1828 }//check for VoIP flag
1829 else if(prop_voip_enabled && mvoice_call_state) {
1830 //check if voice call is active / running in background
1831 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1832 //Need to block input request
1833 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1834 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1835 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1836 {
1837 if(inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1838 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
1839 return NO_INIT;
1840 }
1841 }
1842 }
1843
1844#endif
1845
1846 return AudioPolicyManager::getInputForAttr(attr,
1847 input,
1848 session,
1849 uid,
1850 samplingRate,
1851 format,
1852 channelMask,
1853 flags,
1854 selectedDeviceId,
1855 inputType);
1856}
1857status_t AudioPolicyManagerCustom::startInput(audio_io_handle_t input,
1858 audio_session_t session)
1859{
1860 ALOGV("startInput() input %d", input);
1861 ssize_t index = mInputs.indexOfKey(input);
1862 if (index < 0) {
1863 ALOGW("startInput() unknown input %d", input);
1864 return BAD_VALUE;
1865 }
1866 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1867
1868 index = inputDesc->mSessions.indexOf(session);
1869 if (index < 0) {
1870 ALOGW("startInput() unknown session %d on input %d", session, input);
1871 return BAD_VALUE;
1872 }
1873
1874 // virtual input devices are compatible with other input devices
1875 if (!is_virtual_input_device(inputDesc->mDevice)) {
1876
1877 // for a non-virtual input device, check if there is another (non-virtual) active input
1878 audio_io_handle_t activeInput = mInputs.getActiveInput();
1879 if (activeInput != 0 && activeInput != input) {
1880
1881 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1882 // otherwise the active input continues and the new input cannot be started.
1883 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
1884 if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
1885 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
1886 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
1887 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
1888 } else {
1889 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
1890 return INVALID_OPERATION;
1891 }
1892 }
1893 }
1894
1895 // Routing?
1896 mInputRoutes.incRouteActivity(session);
1897#ifdef RECORD_PLAY_CONCURRENCY
1898 mIsInputRequestOnProgress = true;
1899
1900 char getPropValue[PROPERTY_VALUE_MAX];
1901 bool prop_rec_play_enabled = false;
1902
1903 if (property_get("rec.playback.conc.disabled", getPropValue, NULL)) {
1904 prop_rec_play_enabled = atoi(getPropValue) || !strncmp("true", getPropValue, 4);
1905 }
1906
1907 if ((prop_rec_play_enabled) &&(mInputs.activeInputsCount() == 0)){
1908 // send update to HAL on record playback concurrency
1909 AudioParameter param = AudioParameter();
1910 param.add(String8("rec_play_conc_on"), String8("true"));
1911 ALOGD("startInput() setParameters rec_play_conc is setting to ON ");
1912 mpClientInterface->setParameters(0, param.toString());
1913
1914 // Call invalidate to reset all opened non ULL audio tracks
1915 // Move tracks associated to this strategy from previous output to new output
1916 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
1917 // Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder)
Sharad Sangle4509cef2015-08-19 20:47:12 +05301918 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE && (i != AUDIO_STREAM_PATCH))) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301919 ALOGD("Invalidate on releaseInput for stream :: %d ", i);
1920 //FIXME see fixme on name change
1921 mpClientInterface->invalidateStream((audio_stream_type_t)i);
1922 }
1923 }
1924 // close compress tracks
1925 for (size_t i = 0; i < mOutputs.size(); i++) {
1926 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
1927 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
1928 ALOGD("ouput desc / profile is NULL");
1929 continue;
1930 }
1931 if (outputDesc->mProfile->mFlags
1932 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1933 // close compress sessions
1934 ALOGD("calling closeOutput on record conc for COMPRESS output");
1935 closeOutput(mOutputs.keyAt(i));
1936 }
1937 }
1938 }
1939#endif
1940
1941 if (inputDesc->mRefCount == 0 || mInputRoutes.hasRouteChanged(session)) {
1942 // if input maps to a dynamic policy with an activity listener, notify of state change
1943 if ((inputDesc->mPolicyMix != NULL)
1944 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1945 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1946 MIX_STATE_MIXING);
1947 }
1948
1949 if (mInputs.activeInputsCount() == 0) {
1950 SoundTrigger::setCaptureState(true);
1951 }
1952 setInputDevice(input, getNewInputDevice(input), true /* force */);
1953
1954 // automatically enable the remote submix output when input is started if not
1955 // used by a policy mix of type MIX_TYPE_RECORDERS
1956 // For remote submix (a virtual device), we open only one input per capture request.
1957 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1958 String8 address = String8("");
1959 if (inputDesc->mPolicyMix == NULL) {
1960 address = String8("0");
1961 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1962 address = inputDesc->mPolicyMix->mRegistrationId;
1963 }
1964 if (address != "") {
1965 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1966 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1967 address, "remote-submix");
1968 }
1969 }
1970 }
1971
1972 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1973
1974 inputDesc->mRefCount++;
1975#ifdef RECORD_PLAY_CONCURRENCY
1976 mIsInputRequestOnProgress = false;
1977#endif
1978 return NO_ERROR;
1979}
1980status_t AudioPolicyManagerCustom::stopInput(audio_io_handle_t input,
1981 audio_session_t session)
1982{
1983 status_t status;
1984 status = AudioPolicyManager::stopInput(input, session);
1985#ifdef RECORD_PLAY_CONCURRENCY
1986 char propValue[PROPERTY_VALUE_MAX];
1987 bool prop_rec_play_enabled = false;
1988
1989 if (property_get("rec.playback.conc.disabled", propValue, NULL)) {
1990 prop_rec_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1991 }
1992
1993 if ((prop_rec_play_enabled) && (mInputs.activeInputsCount() == 0)) {
1994
1995 //send update to HAL on record playback concurrency
1996 AudioParameter param = AudioParameter();
1997 param.add(String8("rec_play_conc_on"), String8("false"));
1998 ALOGD("stopInput() setParameters rec_play_conc is setting to OFF ");
1999 mpClientInterface->setParameters(0, param.toString());
2000
2001 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
2002 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
2003 //Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder stop tone)
2004 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE) && (i != AUDIO_STREAM_PATCH)) {
2005 ALOGD(" Invalidate on stopInput for stream :: %d ", i);
2006 //FIXME see fixme on name change
2007 mpClientInterface->invalidateStream((audio_stream_type_t)i);
2008 }
2009 }
2010 }
2011#endif
2012 return status;
2013}
2014
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302015void AudioPolicyManagerCustom::closeAllInputs() {
2016 bool patchRemoved = false;
2017
Dhanalakshmi Siddanibfeca7f2015-10-08 15:17:11 +05302018 for(size_t input_index = mInputs.size(); input_index > 0; input_index--) {
2019 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index-1);
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302020 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
2021 if (patch_index >= 0) {
2022 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
2023 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2024 mAudioPatches.removeItemsAt(patch_index);
2025 patchRemoved = true;
2026 }
2027 if ((inputDesc->mIsSoundTrigger) && (mInputs.size() == 1)) {
2028 ALOGD("Do not close sound trigger input handle");
2029 } else {
Dhanalakshmi Siddanibfeca7f2015-10-08 15:17:11 +05302030 mpClientInterface->closeInput(mInputs.keyAt(input_index-1));
2031 mInputs.removeItem(mInputs.keyAt(input_index-1));
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302032 }
2033 }
2034 nextAudioPortGeneration();
2035
2036 if (patchRemoved) {
2037 mpClientInterface->onAudioPatchListUpdate();
2038 }
2039}
2040
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302041AudioPolicyManagerCustom::AudioPolicyManagerCustom(AudioPolicyClientInterface *clientInterface)
Sharad Sangle4509cef2015-08-19 20:47:12 +05302042 : AudioPolicyManager(clientInterface),
2043 mHdmiAudioDisabled(false),
2044 mHdmiAudioEvent(false),
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08002045 mPrevPhoneState(0),
2046 mPrevFMVolumeDb(0.0f)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302047{
Mingming Yin38ea08c2015-10-05 15:24:04 -07002048 char ssr_enabled[PROPERTY_VALUE_MAX] = {0};
2049 bool prop_ssr_enabled = false;
2050
2051 if (property_get("ro.qc.sdk.audio.ssr", ssr_enabled, NULL)) {
2052 prop_ssr_enabled = atoi(ssr_enabled) || !strncmp("true", ssr_enabled, 4);
2053 }
2054
2055 for (size_t i = 0; i < mHwModules.size(); i++) {
2056 ALOGV("Hw module %d", i);
2057 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) {
2058 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
2059 ALOGV("Input profile ", j);
2060 for (size_t k = 0; k < inProfile->mChannelMasks.size(); k++) {
2061 audio_channel_mask_t channelMask =
2062 inProfile->mChannelMasks.itemAt(k);
2063 ALOGV("Channel Mask %x size %d", channelMask,
2064 inProfile->mChannelMasks.size());
2065 if (AUDIO_CHANNEL_IN_5POINT1 == channelMask) {
2066 if (!prop_ssr_enabled) {
2067 ALOGI("removing AUDIO_CHANNEL_IN_5POINT1 from"
2068 " input profile as SSR(surround sound record)"
2069 " is not supported on this chipset variant");
2070 inProfile->mChannelMasks.removeItemsAt(k, 1);
2071 ALOGV("Channel Mask size now %d",
2072 inProfile->mChannelMasks.size());
2073 }
2074 }
2075 }
2076 }
2077 }
2078
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302079#ifdef RECORD_PLAY_CONCURRENCY
2080 mIsInputRequestOnProgress = false;
2081#endif
2082
2083
2084#ifdef VOICE_CONCURRENCY
2085 mFallBackflag = getFallBackPath();
2086#endif
2087}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07002088}