blob: 1b1578eaa05fad2f89d62868ee5e32d0af17c8a7 [file] [log] [blame]
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001/*
Sharad Sangle36781612015-05-28 16:15:16 +05302 * Copyright (c) 2013-2015, The Linux Foundation. All rights reserved.
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07003 * Not a contribution.
4 *
5 * Copyright (C) 2009 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Gao Jiedb057832015-11-12 08:51:22 +080018 *
19 * This file was modified by Dolby Laboratories, Inc. The portions of the
20 * code that are surrounded by "DOLBY..." are copyrighted and
21 * licensed separately, as follows:
22 *
23 * (C) 2015 Dolby Laboratories, Inc.
24 *
25 * Licensed under the Apache License, Version 2.0 (the "License");
26 * you may not use this file except in compliance with the License.
27 * You may obtain a copy of the License at
28 *
29 * http://www.apache.org/licenses/LICENSE-2.0
30 *
31 * Unless required by applicable law or agreed to in writing, software
32 * distributed under the License is distributed on an "AS IS" BASIS,
33 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34 * See the License for the specific language governing permissions and
35 * limitations under the License.
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070036 */
37
Sharad Sangle36781612015-05-28 16:15:16 +053038#define LOG_TAG "AudioPolicyManagerCustom"
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070039//#define LOG_NDEBUG 0
40
41//#define VERY_VERBOSE_LOGGING
42#ifdef VERY_VERBOSE_LOGGING
43#define ALOGVV ALOGV
44#else
45#define ALOGVV(a...) do { } while(0)
46#endif
Sharad Sangle36781612015-05-28 16:15:16 +053047
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070048// A device mask for all audio output devices that are considered "remote" when evaluating
49// active output devices in isStreamActiveRemotely()
50#define APM_AUDIO_OUT_DEVICE_REMOTE_ALL AUDIO_DEVICE_OUT_REMOTE_SUBMIX
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070051// A device mask for all audio input and output devices where matching inputs/outputs on device
52// type alone is not enough: the address must match too
53#define APM_AUDIO_DEVICE_MATCH_ADDRESS_ALL (AUDIO_DEVICE_IN_REMOTE_SUBMIX | \
54 AUDIO_DEVICE_OUT_REMOTE_SUBMIX)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070055
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070056#include <inttypes.h>
Mingming Yin0ae14ea2014-07-09 17:55:56 -070057#include <math.h>
Mingming Yin0670f162014-06-12 16:05:49 -070058
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070059#include <cutils/properties.h>
60#include <utils/Log.h>
61#include <hardware/audio.h>
62#include <hardware/audio_effect.h>
63#include <media/AudioParameter.h>
64#include <soundtrigger/SoundTrigger.h>
65#include "AudioPolicyManager.h"
Sharad Sangle36781612015-05-28 16:15:16 +053066#include <policy.h>
Gao Jiedb057832015-11-12 08:51:22 +080067#ifdef DOLBY_ENABLE
68#include "DolbyAudioPolicy_impl.h"
69#endif // DOLBY_END
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070070
71namespace android {
Sharad Sanglec5766ff2015-06-04 20:24:10 +053072#ifdef VOICE_CONCURRENCY
73audio_output_flags_t AudioPolicyManagerCustom::getFallBackPath()
74{
75 audio_output_flags_t flag = AUDIO_OUTPUT_FLAG_FAST;
76 char propValue[PROPERTY_VALUE_MAX];
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070077
Sharad Sanglec5766ff2015-06-04 20:24:10 +053078 if (property_get("voice.conc.fallbackpath", propValue, NULL)) {
79 if (!strncmp(propValue, "deep-buffer", 11)) {
80 flag = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
81 }
82 else if (!strncmp(propValue, "fast", 4)) {
83 flag = AUDIO_OUTPUT_FLAG_FAST;
84 }
85 else {
86 ALOGD("voice_conc:not a recognised path(%s) in prop voice.conc.fallbackpath",
87 propValue);
88 }
89 }
90 else {
91 ALOGD("voice_conc:prop voice.conc.fallbackpath not set");
92 }
93
94 ALOGD("voice_conc:picked up flag(0x%x) from prop voice.conc.fallbackpath",
95 flag);
96
97 return flag;
98}
99#endif /*VOICE_CONCURRENCY*/
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700100// ----------------------------------------------------------------------------
101// AudioPolicyInterface implementation
102// ----------------------------------------------------------------------------
Sharad Sangle36781612015-05-28 16:15:16 +0530103extern "C" AudioPolicyInterface* createAudioPolicyManager(
104 AudioPolicyClientInterface *clientInterface)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700105{
Sharad Sangle36781612015-05-28 16:15:16 +0530106 return new AudioPolicyManagerCustom(clientInterface);
107}
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700108
Sharad Sangle36781612015-05-28 16:15:16 +0530109extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface)
110{
111 delete interface;
112}
113
114status_t AudioPolicyManagerCustom::setDeviceConnectionStateInt(audio_devices_t device,
115 audio_policy_dev_state_t state,
116 const char *device_address,
117 const char *device_name)
118{
119 ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
120 device, state, device_address, device_name);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700121
122 // connect/disconnect only 1 device at a time
123 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
124
Sharad Sangle36781612015-05-28 16:15:16 +0530125 sp<DeviceDescriptor> devDesc =
126 mHwModules.getDeviceDescriptor(device, device_address, device_name);
127
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700128 // handle output devices
129 if (audio_is_output_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700130 SortedVector <audio_io_handle_t> outputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700131
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700132 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700133
134 // save a copy of the opened output descriptors before any output is opened or closed
135 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
136 mPreviousOutputs = mOutputs;
137 switch (state)
138 {
139 // handle output device connection
Sharad Sangle36781612015-05-28 16:15:16 +0530140 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700141 if (index >= 0) {
Sharad Sangle4509cef2015-08-19 20:47:12 +0530142#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
143 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
144 if (!strncmp(device_address, "hdmi_spkr", 9)) {
145 mHdmiAudioDisabled = false;
146 } else {
147 mHdmiAudioEvent = true;
148 }
149 }
150#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700151 ALOGW("setDeviceConnectionState() device already connected: %x", device);
152 return INVALID_OPERATION;
153 }
154 ALOGV("setDeviceConnectionState() connecting device %x", device);
155
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700156 // register new device as available
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700157 index = mAvailableOutputDevices.add(devDesc);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530158#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
159 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
160 if (!strncmp(device_address, "hdmi_spkr", 9)) {
161 mHdmiAudioDisabled = false;
162 } else {
163 mHdmiAudioEvent = true;
164 }
165 if (mHdmiAudioDisabled || !mHdmiAudioEvent) {
166 mAvailableOutputDevices.remove(devDesc);
167 ALOGW("HDMI sink not connected, do not route audio to HDMI out");
168 return INVALID_OPERATION;
169 }
170 }
171#endif
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700172 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530173 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700174 if (module == 0) {
175 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
176 device);
177 mAvailableOutputDevices.remove(devDesc);
178 return INVALID_OPERATION;
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700179 }
Sharad Sangle36781612015-05-28 16:15:16 +0530180 mAvailableOutputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700181 } else {
182 return NO_MEMORY;
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700183 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700184
Sharad Sangle36781612015-05-28 16:15:16 +0530185 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700186 mAvailableOutputDevices.remove(devDesc);
187 return INVALID_OPERATION;
188 }
Sharad Sangle36781612015-05-28 16:15:16 +0530189 // Propagate device availability to Engine
190 mEngine->setDeviceConnectionState(devDesc, state);
191
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700192 // outputs should never be empty here
193 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
194 "checkOutputsForDevice() returned no outputs but status OK");
195 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
196 outputs.size());
Sharad Sangle36781612015-05-28 16:15:16 +0530197
198 // Send connect to HALs
199 AudioParameter param = AudioParameter(devDesc->mAddress);
200 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
201 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
202
203 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700204 // handle output device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700205 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
206 if (index < 0) {
Sharad Sangle4509cef2015-08-19 20:47:12 +0530207#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
208 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
209 if (!strncmp(device_address, "hdmi_spkr", 9)) {
210 mHdmiAudioDisabled = true;
211 } else {
212 mHdmiAudioEvent = false;
213 }
214 }
215#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700216 ALOGW("setDeviceConnectionState() device not connected: %x", device);
217 return INVALID_OPERATION;
218 }
219
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700220 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
221
Sharad Sangle36781612015-05-28 16:15:16 +0530222 // Send Disconnect to HALs
223 AudioParameter param = AudioParameter(devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700224 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
225 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
226
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700227 // remove device from available output devices
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700228 mAvailableOutputDevices.remove(devDesc);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530229#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
230 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
231 if (!strncmp(device_address, "hdmi_spkr", 9)) {
232 mHdmiAudioDisabled = true;
233 } else {
234 mHdmiAudioEvent = false;
235 }
236 }
237#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530238 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
239
240 // Propagate device availability to Engine
241 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700242 } break;
243
244 default:
245 ALOGE("setDeviceConnectionState() invalid state: %x", state);
246 return BAD_VALUE;
247 }
248
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700249 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
250 // output is suspended before any tracks are moved to it
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700251 checkA2dpSuspend();
252 checkOutputForAllStrategies();
253 // outputs must be closed after checkOutputForAllStrategies() is executed
254 if (!outputs.isEmpty()) {
255 for (size_t i = 0; i < outputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530256 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700257 // close unused outputs after device disconnection or direct outputs that have been
258 // opened by checkOutputsForDevice() to query dynamic parameters
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700259 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700260 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
261 (desc->mDirectOpenCount == 0))) {
262 closeOutput(outputs[i]);
263 }
264 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700265 // check again after closing A2DP output to reset mA2dpSuspended if needed
266 checkA2dpSuspend();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700267 }
268
269 updateDevicesAndOutputs();
Gao Jiedb057832015-11-12 08:51:22 +0800270#ifdef DOLBY_ENABLE
271 // Before closing the opened outputs, update endpoint property with device capabilities
272 audio_devices_t audioOutputDevice = getDeviceForStrategy(getStrategy(AUDIO_STREAM_MUSIC), true);
273 mDolbyAudioPolicy.setEndpointSystemProperty(audioOutputDevice, mHwModules);
274#endif // DOLBY_END
Sharad Sangle36781612015-05-28 16:15:16 +0530275 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
276 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700277 updateCallRouting(newDevice);
278 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530279
280#ifdef FM_POWER_OPT
281 // handle FM device connection state to trigger FM AFE loopback
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800282 if (device == AUDIO_DEVICE_OUT_FM && hasPrimaryOutput()) {
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530283 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;
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800287 mFMIsActive = true;
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530288 } else {
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800289 mFMIsActive = false;
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530290 mPrimaryOutput->changeRefCount(AUDIO_STREAM_MUSIC, -1);
291 }
292 AudioParameter param = AudioParameter();
Haynes Mathew George603ae9b2015-12-21 17:23:59 -0800293 float volumeDb = mPrimaryOutput->mCurVolume[AUDIO_STREAM_MUSIC];
294 mPrevFMVolumeDb = volumeDb;
295 param.addFloat(String8("fm_volume"), Volume::DbToAmpl(volumeDb));
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530296 param.addInt(String8("handle_fm"), (int)newDevice);
297 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString());
298 }
299#endif /* FM_POWER_OPT end */
300
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700301 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530302 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
303 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
304 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700305 // do not force device change on duplicated output because if device is 0, it will
306 // also force a device 0 for the two outputs it is duplicated to which may override
307 // a valid device selection on those outputs.
Sharad Sangle36781612015-05-28 16:15:16 +0530308 bool force = !desc->isDuplicated()
309 && (!device_distinguishes_on_address(device)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700310 // always force when disconnecting (a non-duplicated device)
311 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Sharad Sangle36781612015-05-28 16:15:16 +0530312 setOutputDevice(desc, newDevice, force, 0);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700313 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700314 }
315
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700316 mpClientInterface->onAudioPortListUpdate();
317 return NO_ERROR;
318 } // end if is output device
319
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700320 // handle input devices
321 if (audio_is_input_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700322 SortedVector <audio_io_handle_t> inputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700323
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700324 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700325 switch (state)
326 {
327 // handle input device connection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700328 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
329 if (index >= 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700330 ALOGW("setDeviceConnectionState() device already connected: %d", device);
331 return INVALID_OPERATION;
332 }
Sharad Sangle36781612015-05-28 16:15:16 +0530333 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700334 if (module == NULL) {
335 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
336 device);
337 return INVALID_OPERATION;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700338 }
Sharad Sangle36781612015-05-28 16:15:16 +0530339 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700340 return INVALID_OPERATION;
341 }
342
343 index = mAvailableInputDevices.add(devDesc);
344 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530345 mAvailableInputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700346 } else {
347 return NO_MEMORY;
348 }
Sharad Sangle36781612015-05-28 16:15:16 +0530349
350 // Set connect to HALs
351 AudioParameter param = AudioParameter(devDesc->mAddress);
352 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
353 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
354
355 // Propagate device availability to Engine
356 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700357 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700358
359 // handle input device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700360 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
361 if (index < 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700362 ALOGW("setDeviceConnectionState() device not connected: %d", device);
363 return INVALID_OPERATION;
364 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700365
366 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
367
368 // Set Disconnect to HALs
Sharad Sangle36781612015-05-28 16:15:16 +0530369 AudioParameter param = AudioParameter(devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700370 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
371 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
372
Sharad Sangle36781612015-05-28 16:15:16 +0530373 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700374 mAvailableInputDevices.remove(devDesc);
375
Sharad Sangle36781612015-05-28 16:15:16 +0530376 // Propagate device availability to Engine
377 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700378 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700379
380 default:
381 ALOGE("setDeviceConnectionState() invalid state: %x", state);
382 return BAD_VALUE;
383 }
384
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700385 closeAllInputs();
386
Sharad Sangle36781612015-05-28 16:15:16 +0530387 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700388 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
389 updateCallRouting(newDevice);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700390 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700391
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700392 mpClientInterface->onAudioPortListUpdate();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700393 return NO_ERROR;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700394 } // end if is input device
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700395
396 ALOGW("setDeviceConnectionState() invalid device: %x", device);
397 return BAD_VALUE;
398}
Sharad Sangle36781612015-05-28 16:15:16 +0530399// This function checks for the parameters which can be offloaded.
400// This can be enhanced depending on the capability of the DSP and policy
401// of the system.
402bool AudioPolicyManagerCustom::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700403{
Sharad Sangle36781612015-05-28 16:15:16 +0530404 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
405 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
406 offloadInfo.sample_rate, offloadInfo.channel_mask,
407 offloadInfo.format,
408 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
409 offloadInfo.has_video);
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530410#ifdef VOICE_CONCURRENCY
411 char concpropValue[PROPERTY_VALUE_MAX];
412 if (property_get("voice.playback.conc.disabled", concpropValue, NULL)) {
413 bool propenabled = atoi(concpropValue) || !strncmp("true", concpropValue, 4);
414 if (propenabled) {
415 if (isInCall())
416 {
417 ALOGD("\n copl: blocking compress offload on call mode\n");
418 return false;
419 }
420 }
421 }
422#endif
423#ifdef RECORD_PLAY_CONCURRENCY
424 char recConcPropValue[PROPERTY_VALUE_MAX];
425 bool prop_rec_play_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700426
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530427 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
428 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
429 }
430
431 if ((prop_rec_play_enabled) &&
432 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
433 ALOGD("copl: blocking compress offload for record concurrency");
434 return false;
435 }
436#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530437 // Check if stream type is music, then only allow offload as of now.
438 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
439 {
440 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
441 return false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700442 }
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530443
Alexy Joseph0ac09032015-10-16 15:57:53 -0700444 // Check if offload has been disabled
445 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
446 if (offloadDisabled) {
447 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
448 return false;
449 }
450
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530451 char propValue[PROPERTY_VALUE_MAX];
452 bool pcmOffload = false;
453#ifdef PCM_OFFLOAD_ENABLED
454 if ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM_OFFLOAD) {
455 bool prop_enabled = false;
456 if ((AUDIO_FORMAT_PCM_16_BIT_OFFLOAD == offloadInfo.format) &&
457 property_get("audio.offload.pcm.16bit.enable", propValue, NULL)) {
458 prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
459 }
460
461#ifdef PCM_OFFLOAD_ENABLED_24
462 if ((AUDIO_FORMAT_PCM_24_BIT_OFFLOAD == offloadInfo.format) &&
463 property_get("audio.offload.pcm.24bit.enable", propValue, NULL)) {
464 prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530465 }
466#endif
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530467
468 if (prop_enabled) {
469 ALOGI("PCM offload property is enabled");
470 pcmOffload = true;
471 }
472
473 if (!pcmOffload) {
474 ALOGD("system property not enabled for PCM offload format[%x]",offloadInfo.format);
475 return false;
476 }
477 }
478#endif
479 if (!pcmOffload) {
Alexy Joseph0ac09032015-10-16 15:57:53 -0700480
481 bool compressedOffloadDisabled = property_get_bool("audio.offload.compress.disable", false);
482 if (compressedOffloadDisabled) {
483 ALOGI("compressed offload disabled by audio.offload.compress.disable=%d", compressedOffloadDisabled);
484 return false;
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530485 }
Alexy Joseph0ac09032015-10-16 15:57:53 -0700486
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530487 //check if it's multi-channel AAC (includes sub formats) and FLAC format
488 if ((popcount(offloadInfo.channel_mask) > 2) &&
489 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
490 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS))) {
491 ALOGD("offload disabled for multi-channel AAC,FLAC and VORBIS format");
492 return false;
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530493 }
494
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530495#ifdef AUDIO_EXTN_FORMATS_ENABLED
496 //check if it's multi-channel FLAC/ALAC/WMA format with sample rate > 48k
497 if ((popcount(offloadInfo.channel_mask) > 2) &&
498 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
Manish Dewangana6fc5442015-08-24 20:30:31 +0530499 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) && (offloadInfo.sample_rate > 48000)) ||
500 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) && (offloadInfo.sample_rate > 48000)) ||
501 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) && (offloadInfo.sample_rate > 48000)) ||
502 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS))) {
503 ALOGD("offload disabled for multi-channel FLAC/ALAC/WMA/AAC_ADTS clips with sample rate > 48kHz");
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530504 return false;
505 }
506#endif
507 //TODO: enable audio offloading with video when ready
508 const bool allowOffloadWithVideo =
509 property_get_bool("audio.offload.video", false /* default_value */);
510 if (offloadInfo.has_video && !allowOffloadWithVideo) {
511 ALOGV("isOffloadSupported: has_video == true, returning false");
512 return false;
513 }
Manish Dewanganf3cd0f82015-10-13 14:04:36 +0530514
515 const bool allowOffloadStreamingWithVideo = property_get_bool("av.streaming.offload.enable",
516 false /*default value*/);
517 if(offloadInfo.has_video && offloadInfo.is_streaming && !allowOffloadStreamingWithVideo) {
518 ALOGW("offload disabled by av.streaming.offload.enable = %s ", propValue );
519 return false;
520 }
521
Sharad Sangle36781612015-05-28 16:15:16 +0530522 }
523
524 //If duration is less than minimum value defined in property, return false
525 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
526 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
527 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
528 return false;
529 }
530 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
531 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
532 //duration checks only valid for MP3/AAC/ formats,
533 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
534 if ((offloadInfo.format == AUDIO_FORMAT_MP3) ||
535 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530536 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS) ||
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530537#ifdef AUDIO_EXTN_FORMATS_ENABLED
Sharad Sangle36781612015-05-28 16:15:16 +0530538 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
Sharad Sangle36781612015-05-28 16:15:16 +0530539 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) ||
540 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) ||
541 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) ||
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530542 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_APE) ||
Manish Dewangana6fc5442015-08-24 20:30:31 +0530543 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS) ||
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530544#endif
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530545 pcmOffload)
Sharad Sangle36781612015-05-28 16:15:16 +0530546 return false;
547
548 }
549
550 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
551 // creating an offloaded track and tearing it down immediately after start when audioflinger
552 // detects there is an active non offloadable effect.
553 // FIXME: We should check the audio session here but we do not have it in this context.
554 // This may prevent offloading in rare situations where effects are left active by apps
555 // in the background.
556 if (mEffects.isNonOffloadableEffectEnabled()) {
557 return false;
558 }
559 // Check for soundcard status
560 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
561 String8("SND_CARD_STATUS"));
562 AudioParameter result = AudioParameter(valueStr);
563 int isonline = 0;
564 if ((result.getInt(String8("SND_CARD_STATUS"), isonline) == NO_ERROR)
565 && !isonline) {
566 ALOGD("copl: soundcard is offline rejecting offload request");
567 return false;
568 }
569 // See if there is a profile to support this.
570 // AUDIO_DEVICE_NONE
571 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
572 offloadInfo.sample_rate,
573 offloadInfo.format,
574 offloadInfo.channel_mask,
575 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
576 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
577 return (profile != 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700578}
Sharad Sangle36781612015-05-28 16:15:16 +0530579audio_devices_t AudioPolicyManagerCustom::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
580 bool fromCache)
581{
582 audio_devices_t device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700583
Sharad Sangle36781612015-05-28 16:15:16 +0530584 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
585 if (index >= 0) {
586 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
587 if (patchDesc->mUid != mUidCached) {
588 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
589 outputDesc->device(), outputDesc->mPatchHandle);
590 return outputDesc->device();
591 }
592 }
593
594 // check the following by order of priority to request a routing change if necessary:
595 // 1: the strategy enforced audible is active and enforced on the output:
596 // use device for strategy enforced audible
597 // 2: we are in call or the strategy phone is active on the output:
598 // use device for strategy phone
599 // 3: the strategy for enforced audible is active but not enforced on the output:
600 // use the device for strategy enforced audible
601 // 4: the strategy sonification is active on the output:
602 // use device for strategy sonification
603 // 5: the strategy "respectful" sonification is active on the output:
604 // use device for strategy "respectful" sonification
605 // 6: the strategy accessibility is active on the output:
606 // use device for strategy accessibility
607 // 7: the strategy media is active on the output:
608 // use device for strategy media
609 // 8: the strategy DTMF is active on the output:
610 // use device for strategy DTMF
611 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
612 // use device for strategy t-t-s
613 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
614 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
615 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
616 } else if (isInCall() ||
617 isStrategyActive(outputDesc, STRATEGY_PHONE)||
618 isStrategyActive(mPrimaryOutput, STRATEGY_PHONE)) {
619 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
620 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
621 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
622 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)||
623 (isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION)
624 && (!isStrategyActive(mPrimaryOutput,STRATEGY_MEDIA)))) {
625 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530626 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL) ||
627 isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION_RESPECTFUL)) {
Sharad Sangle36781612015-05-28 16:15:16 +0530628 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
629 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
630 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
631 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
632 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
633 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
634 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
635 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
636 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
637 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
638 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
639 }
640
641 ALOGV("getNewOutputDevice() selected device %x", device);
642 return device;
643}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700644void AudioPolicyManagerCustom::setPhoneState(audio_mode_t state)
645{
Sharad Sangle36781612015-05-28 16:15:16 +0530646 ALOGV("setPhoneState() state %d", state);
647 // store previous phone state for management of sonification strategy below
Sharad Sangle4509cef2015-08-19 20:47:12 +0530648 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Sharad Sangle36781612015-05-28 16:15:16 +0530649 int oldState = mEngine->getPhoneState();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700650
Sharad Sangle36781612015-05-28 16:15:16 +0530651 if (mEngine->setPhoneState(state) != NO_ERROR) {
652 ALOGW("setPhoneState() invalid or same state %d", state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700653 return;
654 }
Sharad Sangle36781612015-05-28 16:15:16 +0530655 /// Opens: can these line be executed after the switch of volume curves???
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700656 // if leaving call state, handle special case of active streams
657 // pertaining to sonification strategy see handleIncallSonification()
658 if (isInCall()) {
659 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530660 for (size_t j = 0; j < mOutputs.size(); j++) {
661 audio_io_handle_t curOutput = mOutputs.keyAt(j);
662 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
663 if (stream == AUDIO_STREAM_PATCH) {
664 continue;
665 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530666 handleIncallSonification((audio_stream_type_t)stream, false, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +0530667 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700668 }
Sharad Sangle36781612015-05-28 16:15:16 +0530669
670 // force reevaluating accessibility routing when call starts
671 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700672 }
673
Sharad Sangle36781612015-05-28 16:15:16 +0530674 /**
675 * Switching to or from incall state or switching between telephony and VoIP lead to force
676 * routing command.
677 */
678 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
679 || (is_state_in_call(state) && (state != oldState)));
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700680
681 // check for device and output changes triggered by new phone state
682 checkA2dpSuspend();
683 checkOutputForAllStrategies();
684 updateDevicesAndOutputs();
685
Sharad Sangle36781612015-05-28 16:15:16 +0530686 sp<SwAudioOutputDescriptor> hwOutputDesc = mPrimaryOutput;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530687#ifdef VOICE_CONCURRENCY
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530688 char propValue[PROPERTY_VALUE_MAX];
689 bool prop_playback_enabled = false, prop_rec_enabled=false, prop_voip_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700690
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530691 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
692 prop_playback_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
693 }
694
695 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
696 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
697 }
698
699 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
700 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
701 }
702
Dhananjay Kumar9125b2a2015-12-01 16:24:09 +0530703 if ((AUDIO_MODE_IN_CALL != oldState) && (AUDIO_MODE_IN_CALL == state)) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530704 ALOGD("voice_conc:Entering to call mode oldState :: %d state::%d ",
705 oldState, state);
Dhananjay Kumar9125b2a2015-12-01 16:24:09 +0530706 mvoice_call_state = state;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530707 if (prop_rec_enabled) {
708 //Close all active inputs
709 audio_io_handle_t activeInput = mInputs.getActiveInput();
710 if (activeInput != 0) {
711 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
712 switch(activeDesc->mInputSource) {
713 case AUDIO_SOURCE_VOICE_UPLINK:
714 case AUDIO_SOURCE_VOICE_DOWNLINK:
715 case AUDIO_SOURCE_VOICE_CALL:
716 ALOGD("voice_conc:FOUND active input during call active: %d",activeDesc->mInputSource);
717 break;
718
719 case AUDIO_SOURCE_VOICE_COMMUNICATION:
720 if(prop_voip_enabled) {
721 ALOGD("voice_conc:CLOSING VoIP input source on call setup :%d ",activeDesc->mInputSource);
722 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
723 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
724 }
725 break;
726
727 default:
728 ALOGD("voice_conc:CLOSING input on call setup for inputSource: %d",activeDesc->mInputSource);
729 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
730 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
731 break;
732 }
733 }
734 } else if (prop_voip_enabled) {
735 audio_io_handle_t activeInput = mInputs.getActiveInput();
736 if (activeInput != 0) {
737 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
738 if (AUDIO_SOURCE_VOICE_COMMUNICATION == activeDesc->mInputSource) {
739 ALOGD("voice_conc:CLOSING VoIP on call setup : %d",activeDesc->mInputSource);
740 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
741 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
742 }
743 }
744 }
745 if (prop_playback_enabled) {
746 // Move tracks associated to this strategy from previous output to new output
747 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
748 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
749 if (i == AUDIO_STREAM_PATCH) {
750 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
751 continue;
752 }
753 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
754 if ((AUDIO_STREAM_MUSIC == i) ||
755 (AUDIO_STREAM_VOICE_CALL == i) ) {
756 ALOGD("voice_conc:Invalidate stream type %d", i);
757 mpClientInterface->invalidateStream((audio_stream_type_t)i);
758 }
759 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
760 ALOGD("voice_conc:Invalidate stream type %d", i);
761 mpClientInterface->invalidateStream((audio_stream_type_t)i);
762 }
763 }
764 }
765
766 for (size_t i = 0; i < mOutputs.size(); i++) {
767 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
768 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
769 ALOGD("voice_conc:ouput desc / profile is NULL");
770 continue;
771 }
772
773 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
774 if (((!outputDesc->isDuplicated() &&outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY))
775 && prop_playback_enabled) {
776 ALOGD("voice_conc:calling suspendOutput on call mode for primary output");
777 mpClientInterface->suspendOutput(mOutputs.keyAt(i));
778 } //Close compress all sessions
779 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
780 && prop_playback_enabled) {
781 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
782 closeOutput(mOutputs.keyAt(i));
783 }
784 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_VOIP_RX)
785 && prop_voip_enabled) {
786 ALOGD("voice_conc:calling closeOutput on call mode for DIRECT output");
787 closeOutput(mOutputs.keyAt(i));
788 }
789 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
790 if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)
791 && prop_playback_enabled) {
792 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
793 closeOutput(mOutputs.keyAt(i));
794 }
795 }
796 }
797 }
798
799 if ((AUDIO_MODE_IN_CALL == oldState || AUDIO_MODE_IN_COMMUNICATION == oldState) &&
800 (AUDIO_MODE_NORMAL == state) && prop_playback_enabled && mvoice_call_state) {
801 ALOGD("voice_conc:EXITING from call mode oldState :: %d state::%d \n",oldState, state);
802 mvoice_call_state = 0;
803 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
804 //restore PCM (deep-buffer) output after call termination
805 for (size_t i = 0; i < mOutputs.size(); i++) {
806 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
807 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
808 ALOGD("voice_conc:ouput desc / profile is NULL");
809 continue;
810 }
811 if (!outputDesc->isDuplicated() && outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
812 ALOGD("voice_conc:calling restoreOutput after call mode for primary output");
813 mpClientInterface->restoreOutput(mOutputs.keyAt(i));
814 }
815 }
816 }
817 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
818 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
819 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
820 if (i == AUDIO_STREAM_PATCH) {
821 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
822 continue;
823 }
824 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
825 if ((AUDIO_STREAM_MUSIC == i) ||
826 (AUDIO_STREAM_VOICE_CALL == i) ) {
827 mpClientInterface->invalidateStream((audio_stream_type_t)i);
828 }
829 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
830 mpClientInterface->invalidateStream((audio_stream_type_t)i);
831 }
832 }
833 }
834
835#endif
836#ifdef RECORD_PLAY_CONCURRENCY
837 char recConcPropValue[PROPERTY_VALUE_MAX];
838 bool prop_rec_play_enabled = false;
839
840 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
841 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
842 }
843 if (prop_rec_play_enabled) {
844 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
845 ALOGD("phone state changed to MODE_IN_COMM invlaidating music and voice streams");
846 // call invalidate for voice streams, so that it can use deepbuffer with VoIP out device from HAL
847 mpClientInterface->invalidateStream(AUDIO_STREAM_VOICE_CALL);
848 // call invalidate for music, so that compress will fallback to deep-buffer with VoIP out device
849 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
850
851 // close compress output to make sure session will be closed before timeout(60sec)
852 for (size_t i = 0; i < mOutputs.size(); i++) {
853
854 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
855 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
856 ALOGD("ouput desc / profile is NULL");
857 continue;
858 }
859
860 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
861 ALOGD("calling closeOutput on call mode for COMPRESS output");
862 closeOutput(mOutputs.keyAt(i));
863 }
864 }
865 } else if ((oldState == AUDIO_MODE_IN_COMMUNICATION) &&
866 (mEngine->getPhoneState() == AUDIO_MODE_NORMAL)) {
867 // call invalidate for music so that music can fallback to compress
868 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
869 }
870 }
871#endif
872 mPrevPhoneState = oldState;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700873 int delayMs = 0;
874 if (isStateInCall(state)) {
875 nsecs_t sysTime = systemTime();
876 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530877 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700878 // mute media and sonification strategies and delay device switch by the largest
879 // latency of any output where either strategy is active.
880 // This avoid sending the ring tone or music tail into the earpiece or headset.
Sharad Sangle36781612015-05-28 16:15:16 +0530881 if ((isStrategyActive(desc, STRATEGY_MEDIA,
882 SONIFICATION_HEADSET_MUSIC_DELAY,
883 sysTime) ||
884 isStrategyActive(desc, STRATEGY_SONIFICATION,
885 SONIFICATION_HEADSET_MUSIC_DELAY,
886 sysTime)) &&
887 (delayMs < (int)desc->latency()*2)) {
888 delayMs = desc->latency()*2;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700889 }
Sharad Sangle36781612015-05-28 16:15:16 +0530890 setStrategyMute(STRATEGY_MEDIA, true, desc);
891 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700892 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Sharad Sangle36781612015-05-28 16:15:16 +0530893 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
894 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700895 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
896 }
897 }
898
Sharad Sangle36781612015-05-28 16:15:16 +0530899 if (hasPrimaryOutput()) {
900 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
901 // the device returned is not necessarily reachable via this output
902 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
903 // force routing command to audio hardware when ending call
904 // even if no device change is needed
905 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
906 rxDevice = mPrimaryOutput->device();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700907 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700908
Sharad Sangle36781612015-05-28 16:15:16 +0530909 if (state == AUDIO_MODE_IN_CALL) {
910 updateCallRouting(rxDevice, delayMs);
911 } else if (oldState == AUDIO_MODE_IN_CALL) {
912 if (mCallRxPatch != 0) {
913 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
914 mCallRxPatch.clear();
915 }
916 if (mCallTxPatch != 0) {
917 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
918 mCallTxPatch.clear();
919 }
920 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
921 } else {
922 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700923 }
924 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530925 //update device for all non-primary outputs
926 for (size_t i = 0; i < mOutputs.size(); i++) {
927 audio_io_handle_t output = mOutputs.keyAt(i);
928 if (output != mPrimaryOutput->mIoHandle) {
929 newDevice = getNewOutputDevice(mOutputs.valueFor(output), false /*fromCache*/);
930 setOutputDevice(mOutputs.valueFor(output), newDevice, (newDevice != AUDIO_DEVICE_NONE));
931 }
932 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700933 // if entering in call state, handle special case of active streams
934 // pertaining to sonification strategy see handleIncallSonification()
935 if (isStateInCall(state)) {
936 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530937 for (size_t j = 0; j < mOutputs.size(); j++) {
938 audio_io_handle_t curOutput = mOutputs.keyAt(j);
939 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
940 if (stream == AUDIO_STREAM_PATCH) {
941 continue;
942 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530943 handleIncallSonification((audio_stream_type_t)stream, true, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +0530944 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700945 }
946 }
947
948 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
949 if (state == AUDIO_MODE_RINGTONE &&
950 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
951 mLimitRingtoneVolume = true;
952 } else {
953 mLimitRingtoneVolume = false;
954 }
955}
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +0530956
957void AudioPolicyManagerCustom::setForceUse(audio_policy_force_use_t usage,
958 audio_policy_forced_cfg_t config)
959{
960 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
961
962 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
963 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
964 return;
965 }
966 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
967 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
968 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
969
970 // check for device and output changes triggered by new force usage
971 checkA2dpSuspend();
972 checkOutputForAllStrategies();
973 updateDevicesAndOutputs();
974 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
975 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
976 updateCallRouting(newDevice);
977 }
978 // Use reverse loop to make sure any low latency usecases (generally tones)
979 // are not routed before non LL usecases (generally music).
980 // We can safely assume that LL output would always have lower index,
981 // and use this work-around to avoid routing of output with music stream
982 // from the context of short lived LL output.
983 // Note: in case output's share backend(HAL sharing is implicit) all outputs
984 // gets routing update while processing first output itself.
985 for (size_t i = mOutputs.size(); i > 0; i--) {
986 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i-1);
987 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
988 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || outputDesc != mPrimaryOutput) {
989 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
990 }
991 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
992 applyStreamVolumes(outputDesc, newDevice, 0, true);
993 }
994 }
995
996 audio_io_handle_t activeInput = mInputs.getActiveInput();
997 if (activeInput != 0) {
998 setInputDevice(activeInput, getNewInputDevice(activeInput));
999 }
1000
1001}
1002
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301003status_t AudioPolicyManagerCustom::stopSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301004 audio_stream_type_t stream,
1005 bool forceDeviceUpdate)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001006{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301007 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1008 ALOGW("stopSource() invalid stream %d", stream);
1009 return INVALID_OPERATION;
1010 }
Sharad Sangle36781612015-05-28 16:15:16 +05301011 // always handle stream stop, check which stream type is stopping
1012 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001013
Sharad Sangle36781612015-05-28 16:15:16 +05301014 // handle special case for sonification while in call
Sharad Sangle4509cef2015-08-19 20:47:12 +05301015 if (isInCall() && (outputDesc->mRefCount[stream] == 1)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301016 if (outputDesc->isDuplicated()) {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301017 handleIncallSonification(stream, false, false, outputDesc->subOutput1()->mIoHandle);
1018 handleIncallSonification(stream, false, false, outputDesc->subOutput2()->mIoHandle);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001019 }
Sharad Sangle36781612015-05-28 16:15:16 +05301020 handleIncallSonification(stream, false, false, outputDesc->mIoHandle);
1021 }
1022
1023 if (outputDesc->mRefCount[stream] > 0) {
1024 // decrement usage count of this stream on the output
1025 outputDesc->changeRefCount(stream, -1);
1026
1027 // store time at which the stream was stopped - see isStreamActive()
1028 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
1029 outputDesc->mStopTime[stream] = systemTime();
Zhou Song5dcddc92015-09-21 14:36:57 +08001030 audio_devices_t prevDevice = outputDesc->device();
Sharad Sangle36781612015-05-28 16:15:16 +05301031 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
1032 // delay the device switch by twice the latency because stopOutput() is executed when
1033 // the track stop() command is received and at that time the audio track buffer can
1034 // still contain data that needs to be drained. The latency only covers the audio HAL
1035 // and kernel buffers. Also the latency does not always include additional delay in the
1036 // audio path (audio DSP, CODEC ...)
1037 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
1038
1039 // force restoring the device selection on other active outputs if it differs from the
1040 // one being selected for this output
1041 for (size_t i = 0; i < mOutputs.size(); i++) {
1042 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1043 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1044 if (desc != outputDesc &&
1045 desc->isActive() &&
1046 outputDesc->sharesHwModuleWith(desc) &&
1047 (newDevice != desc->device())) {
Sharad Sangle4509cef2015-08-19 20:47:12 +05301048 audio_devices_t dev = getNewOutputDevice(mOutputs.valueFor(curOutput), false /*fromCache*/);
Zhou Song5dcddc92015-09-21 14:36:57 +08001049 uint32_t delayMs;
1050 if (dev == prevDevice) {
1051 delayMs = 0;
1052 } else {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301053 delayMs = outputDesc->latency()*2;
Zhou Song5dcddc92015-09-21 14:36:57 +08001054 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301055 setOutputDevice(desc,
1056 dev,
Sharad Sangle36781612015-05-28 16:15:16 +05301057 true,
Zhou Song5dcddc92015-09-21 14:36:57 +08001058 delayMs);
Sharad Sangle36781612015-05-28 16:15:16 +05301059 }
1060 }
1061 // update the outputs if stopping one with a stream that can affect notification routing
1062 handleNotificationRoutingForStream(stream);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001063 }
Sharad Sangle36781612015-05-28 16:15:16 +05301064 return NO_ERROR;
1065 } else {
1066 ALOGW("stopOutput() refcount is already 0");
1067 return INVALID_OPERATION;
1068 }
1069}
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301070status_t AudioPolicyManagerCustom::startSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301071 audio_stream_type_t stream,
1072 audio_devices_t device,
1073 uint32_t *delayMs)
1074{
1075 // cannot start playback of STREAM_TTS if any other output is being used
1076 uint32_t beaconMuteLatency = 0;
1077
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301078 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1079 ALOGW("startSource() invalid stream %d", stream);
1080 return INVALID_OPERATION;
1081 }
1082
Sharad Sangle36781612015-05-28 16:15:16 +05301083 *delayMs = 0;
1084 if (stream == AUDIO_STREAM_TTS) {
1085 ALOGV("\t found BEACON stream");
1086 if (mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
1087 return INVALID_OPERATION;
1088 } else {
1089 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001090 }
Sharad Sangle36781612015-05-28 16:15:16 +05301091 } else {
1092 // some playback other than beacon starts
1093 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1094 }
1095
1096 // increment usage count for this stream on the requested output:
1097 // NOTE that the usage count is the same for duplicated output and hardware output which is
1098 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1099 outputDesc->changeRefCount(stream, 1);
1100
1101 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
1102 // starting an output being rerouted?
1103 if (device == AUDIO_DEVICE_NONE) {
1104 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001105 }
Sharad Sangle36781612015-05-28 16:15:16 +05301106 routing_strategy strategy = getStrategy(stream);
1107 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
1108 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1109 (beaconMuteLatency > 0);
1110 uint32_t waitMs = beaconMuteLatency;
1111 bool force = false;
1112 for (size_t i = 0; i < mOutputs.size(); i++) {
1113 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1114 if (desc != outputDesc) {
1115 // force a device change if any other output is managed by the same hw
1116 // module and has a current device selection that differs from selected device.
1117 // In this case, the audio HAL must receive the new device selection so that it can
1118 // change the device currently selected by the other active output.
1119 if (outputDesc->sharesHwModuleWith(desc) &&
1120 desc->device() != device) {
1121 force = true;
1122 }
1123 // wait for audio on other active outputs to be presented when starting
1124 // a notification so that audio focus effect can propagate, or that a mute/unmute
1125 // event occurred for beacon
1126 uint32_t latency = desc->latency();
1127 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1128 waitMs = latency;
1129 }
1130 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001131 }
Sharad Sangle36781612015-05-28 16:15:16 +05301132 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force);
1133
1134 // handle special case for sonification while in call
1135 if (isInCall()) {
1136 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001137 }
Sharad Sangle36781612015-05-28 16:15:16 +05301138
1139 // apply volume rules for current stream and device if necessary
1140 checkAndSetVolume(stream,
1141 mStreams.valueFor(stream).getVolumeIndex(device),
1142 outputDesc,
1143 device);
1144
1145 // update the outputs if starting an output with a stream that can affect notification
1146 // routing
1147 handleNotificationRoutingForStream(stream);
1148
1149 // force reevaluating accessibility routing when ringtone or alarm starts
1150 if (strategy == STRATEGY_SONIFICATION) {
1151 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1152 }
1153 }
1154 else {
1155 // handle special case for sonification while in call
1156 if (isInCall()) {
1157 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
1158 }
1159 }
1160 return NO_ERROR;
1161}
1162void AudioPolicyManagerCustom::handleIncallSonification(audio_stream_type_t stream,
1163 bool starting, bool stateChange,
1164 audio_io_handle_t output)
1165{
1166 if(!hasPrimaryOutput()) {
1167 return;
1168 }
1169 // no action needed for AUDIO_STREAM_PATCH stream type, it's for internal flinger tracks
1170 if (stream == AUDIO_STREAM_PATCH) {
1171 return;
1172 }
1173 // if the stream pertains to sonification strategy and we are in call we must
1174 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
1175 // in the device used for phone strategy and play the tone if the selected device does not
1176 // interfere with the device used for phone strategy
1177 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
1178 // many times as there are active tracks on the output
1179 const routing_strategy stream_strategy = getStrategy(stream);
1180 if ((stream_strategy == STRATEGY_SONIFICATION) ||
1181 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
1182 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
1183 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
1184 stream, starting, outputDesc->mDevice, stateChange);
1185 if (outputDesc->mRefCount[stream]) {
1186 int muteCount = 1;
1187 if (stateChange) {
1188 muteCount = outputDesc->mRefCount[stream];
1189 }
1190 if (audio_is_low_visibility(stream)) {
1191 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
1192 for (int i = 0; i < muteCount; i++) {
1193 setStreamMute(stream, starting, outputDesc);
1194 }
1195 } else {
1196 ALOGV("handleIncallSonification() high visibility");
1197 if (outputDesc->device() &
1198 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
1199 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
1200 for (int i = 0; i < muteCount; i++) {
1201 setStreamMute(stream, starting, outputDesc);
1202 }
1203 }
1204 if (starting) {
1205 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
1206 AUDIO_STREAM_VOICE_CALL);
1207 } else {
1208 mpClientInterface->stopTone();
1209 }
1210 }
1211 }
1212 }
1213}
1214void AudioPolicyManagerCustom::handleNotificationRoutingForStream(audio_stream_type_t stream) {
1215 switch(stream) {
1216 case AUDIO_STREAM_MUSIC:
1217 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
1218 updateDevicesAndOutputs();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001219 break;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001220 default:
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001221 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001222 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001223}
Sharad Sangle36781612015-05-28 16:15:16 +05301224status_t AudioPolicyManagerCustom::checkAndSetVolume(audio_stream_type_t stream,
1225 int index,
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301226 const sp<AudioOutputDescriptor>& outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301227 audio_devices_t device,
1228 int delayMs, bool force)
1229{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301230 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1231 ALOGW("checkAndSetVolume() invalid stream %d", stream);
1232 return INVALID_OPERATION;
1233 }
Sharad Sangle36781612015-05-28 16:15:16 +05301234 // do not change actual stream volume if the stream is muted
1235 if (outputDesc->mMuteCount[stream] != 0) {
1236 ALOGVV("checkAndSetVolume() stream %d muted count %d",
1237 stream, outputDesc->mMuteCount[stream]);
1238 return NO_ERROR;
1239 }
1240 audio_policy_forced_cfg_t forceUseForComm =
1241 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
1242 // do not change in call volume if bluetooth is connected and vice versa
1243 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
1244 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
1245 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
1246 stream, forceUseForComm);
1247 return INVALID_OPERATION;
1248 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001249
Sharad Sangle36781612015-05-28 16:15:16 +05301250 if (device == AUDIO_DEVICE_NONE) {
1251 device = outputDesc->device();
1252 }
1253
1254 float volumeDb = computeVolume(stream, index, device);
1255 if (outputDesc->isFixedVolume(device)) {
1256 volumeDb = 0.0f;
1257 }
1258
1259 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
1260
1261 if (stream == AUDIO_STREAM_VOICE_CALL ||
1262 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
1263 float voiceVolume;
1264 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
1265 if (stream == AUDIO_STREAM_VOICE_CALL) {
1266 voiceVolume = (float)index/(float)mStreams.valueFor(stream).getVolumeIndexMax();
1267 } else {
1268 voiceVolume = 1.0;
1269 }
1270
1271 if (voiceVolume != mLastVoiceVolume && ((outputDesc == mPrimaryOutput) ||
1272 isDirectOutput(outputDesc->mIoHandle) || device & AUDIO_DEVICE_OUT_ALL_USB)) {
1273 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
1274 mLastVoiceVolume = voiceVolume;
1275 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301276#ifdef FM_POWER_OPT
1277 } else if (stream == AUDIO_STREAM_MUSIC && hasPrimaryOutput() &&
Haynes Mathew George603ae9b2015-12-21 17:23:59 -08001278 outputDesc == mPrimaryOutput && mFMIsActive) {
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001279 /* Avoid unnecessary set_parameter calls as it puts the primary
1280 outputs FastMixer in HOT_IDLE leading to breaks in audio */
1281 if (volumeDb != mPrevFMVolumeDb) {
1282 mPrevFMVolumeDb = volumeDb;
1283 AudioParameter param = AudioParameter();
1284 param.addFloat(String8("fm_volume"), Volume::DbToAmpl(volumeDb));
Mingming Yin1433cc62016-01-04 14:58:00 -08001285 //Double delayMs to avoid sound burst while device switch.
1286 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString(), delayMs*2);
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08001287 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301288#endif /* FM_POWER_OPT end */
Sharad Sangle36781612015-05-28 16:15:16 +05301289 }
1290
1291 return NO_ERROR;
1292}
1293bool AudioPolicyManagerCustom::isDirectOutput(audio_io_handle_t output) {
1294 for (size_t i = 0; i < mOutputs.size(); i++) {
1295 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1296 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1297 if ((curOutput == output) && (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
1298 return true;
1299 }
1300 }
1301 return false;
1302}
vivek mehta0ea887a2015-08-26 14:01:20 -07001303
1304status_t AudioPolicyManagerCustom::getOutputForAttr(const audio_attributes_t *attr,
1305 audio_io_handle_t *output,
1306 audio_session_t session,
1307 audio_stream_type_t *stream,
1308 uid_t uid,
1309 uint32_t samplingRate,
1310 audio_format_t format,
1311 audio_channel_mask_t channelMask,
1312 audio_output_flags_t flags,
1313 audio_port_handle_t selectedDeviceId,
1314 const audio_offload_info_t *offloadInfo)
1315{
1316 audio_offload_info_t tOffloadInfo = AUDIO_INFO_INITIALIZER;
1317
Alexy Joseph0ac09032015-10-16 15:57:53 -07001318 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
1319 bool pcmOffloadEnabled = false;
1320
1321 if (offloadDisabled) {
1322 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
1323 }
1324
1325 //read track offload property only if the global offload switch is off.
1326 if (!offloadDisabled) {
1327 pcmOffloadEnabled = property_get_bool("audio.offload.track.enable", false);
1328 }
vivek mehta0ea887a2015-08-26 14:01:20 -07001329
1330 if (offloadInfo == NULL && pcmOffloadEnabled) {
1331 tOffloadInfo.sample_rate = samplingRate;
1332 tOffloadInfo.channel_mask = channelMask;
1333 tOffloadInfo.format = format;
1334 tOffloadInfo.stream_type = *stream;
1335 tOffloadInfo.bit_width = 16; //hard coded for PCM_16
1336 if (attr != NULL) {
1337 ALOGV("found attribute .. setting usage %d ", attr->usage);
1338 tOffloadInfo.usage = attr->usage;
1339 } else {
Alexy Joseph0ac09032015-10-16 15:57:53 -07001340 ALOGI("%s:: attribute is NULL .. no usage set", __func__);
vivek mehta0ea887a2015-08-26 14:01:20 -07001341 }
1342 offloadInfo = &tOffloadInfo;
1343 }
1344
1345 return AudioPolicyManager::getOutputForAttr(attr, output, session, stream,
1346 (uid_t)uid, (uint32_t)samplingRate,
1347 format, (audio_channel_mask_t)channelMask,
1348 flags, (audio_port_handle_t)selectedDeviceId,
1349 offloadInfo);
1350}
1351
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001352audio_io_handle_t AudioPolicyManagerCustom::getOutputForDevice(
1353 audio_devices_t device,
Sharad Sangle36781612015-05-28 16:15:16 +05301354 audio_session_t session __unused,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001355 audio_stream_type_t stream,
1356 uint32_t samplingRate,
1357 audio_format_t format,
1358 audio_channel_mask_t channelMask,
1359 audio_output_flags_t flags,
1360 const audio_offload_info_t *offloadInfo)
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001361{
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001362 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
1363 uint32_t latency = 0;
1364 status_t status;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001365
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001366#ifdef AUDIO_POLICY_TEST
1367 if (mCurOutput != 0) {
1368 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1369 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1370
1371 if (mTestOutputs[mCurOutput] == 0) {
1372 ALOGV("getOutput() opening test output");
Sharad Sangle36781612015-05-28 16:15:16 +05301373 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
1374 mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001375 outputDesc->mDevice = mTestDevice;
1376 outputDesc->mLatency = mTestLatencyMs;
1377 outputDesc->mFlags =
1378 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
1379 outputDesc->mRefCount[stream] = 0;
1380 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1381 config.sample_rate = mTestSamplingRate;
1382 config.channel_mask = mTestChannels;
1383 config.format = mTestFormat;
1384 if (offloadInfo != NULL) {
1385 config.offload_info = *offloadInfo;
1386 }
1387 status = mpClientInterface->openOutput(0,
1388 &mTestOutputs[mCurOutput],
1389 &config,
1390 &outputDesc->mDevice,
1391 String8(""),
1392 &outputDesc->mLatency,
1393 outputDesc->mFlags);
1394 if (status == NO_ERROR) {
1395 outputDesc->mSamplingRate = config.sample_rate;
1396 outputDesc->mFormat = config.format;
1397 outputDesc->mChannelMask = config.channel_mask;
1398 AudioParameter outputCmd = AudioParameter();
1399 outputCmd.addInt(String8("set_id"),mCurOutput);
1400 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1401 addOutput(mTestOutputs[mCurOutput], outputDesc);
1402 }
1403 }
1404 return mTestOutputs[mCurOutput];
1405 }
1406#endif //AUDIO_POLICY_TEST
Sharad Sangle36781612015-05-28 16:15:16 +05301407 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1408 (stream != AUDIO_STREAM_MUSIC)) {
1409 // compress should not be used for non-music streams
1410 ALOGE("Offloading only allowed with music stream");
1411 return 0;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301412 }
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301413
1414 if ((stream == AUDIO_STREAM_VOICE_CALL) &&
1415 (channelMask == 1) &&
1416 (samplingRate == 8000 || samplingRate == 16000)) {
1417 // Allow Voip direct output only if:
1418 // audio mode is MODE_IN_COMMUNCATION; AND
1419 // voip output is not opened already; AND
1420 // requested sample rate matches with that of voip input stream (if opened already)
1421 int value = 0;
1422 uint32_t mode = 0, voipOutCount = 1, voipSampleRate = 1;
1423 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1424 String8("audio_mode"));
1425 AudioParameter result = AudioParameter(valueStr);
1426 if (result.getInt(String8("audio_mode"), value) == NO_ERROR) {
1427 mode = value;
1428 }
1429
1430 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1431 String8("voip_out_stream_count"));
1432 result = AudioParameter(valueStr);
1433 if (result.getInt(String8("voip_out_stream_count"), value) == NO_ERROR) {
1434 voipOutCount = value;
1435 }
1436
1437 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1438 String8("voip_sample_rate"));
1439 result = AudioParameter(valueStr);
1440 if (result.getInt(String8("voip_sample_rate"), value) == NO_ERROR) {
1441 voipSampleRate = value;
1442 }
1443
1444 if ((mode == AUDIO_MODE_IN_COMMUNICATION) && (voipOutCount == 0) &&
1445 ((voipSampleRate == 0) || (voipSampleRate == samplingRate))) {
1446 if (audio_is_linear_pcm(format)) {
1447 char propValue[PROPERTY_VALUE_MAX] = {0};
1448 property_get("use.voice.path.for.pcm.voip", propValue, "0");
1449 bool voipPcmSysPropEnabled = !strncmp("true", propValue, sizeof("true"));
1450 if (voipPcmSysPropEnabled && (format == AUDIO_FORMAT_PCM_16_BIT)) {
Karthik Reddy Katta3fb9be72015-11-25 11:18:27 +05301451 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX |
1452 AUDIO_OUTPUT_FLAG_DIRECT);
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301453 ALOGD("Set VoIP and Direct output flags for PCM format");
1454 }
1455 }
1456 }
1457 }
1458
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301459#ifdef VOICE_CONCURRENCY
1460 char propValue[PROPERTY_VALUE_MAX];
1461 bool prop_play_enabled=false, prop_voip_enabled = false;
1462
1463 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1464 prop_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001465 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301466
1467 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1468 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1469 }
1470
1471 if (prop_play_enabled && mvoice_call_state) {
1472 //check if voice call is active / running in background
1473 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1474 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1475 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1476 {
1477 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1478 if(prop_voip_enabled) {
1479 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1480 flags );
1481 return 0;
1482 }
1483 }
1484 else {
1485 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
1486 ALOGD("voice_conc:IN call mode adding ULL flags .. flags: %x ", flags );
1487 flags = AUDIO_OUTPUT_FLAG_FAST;
1488 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
1489 if (AUDIO_STREAM_MUSIC == stream) {
1490 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1491 ALOGD("voice_conc:IN call mode adding deep-buffer flags %x ", flags );
1492 }
1493 else {
1494 flags = AUDIO_OUTPUT_FLAG_FAST;
1495 ALOGD("voice_conc:IN call mode adding fast flags %x ", flags );
1496 }
1497 }
1498 }
1499 }
1500 } else if (prop_voip_enabled && mvoice_call_state) {
1501 //check if voice call is active / running in background
1502 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1503 //return only ULL ouput
1504 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1505 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1506 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1507 {
1508 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1509 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1510 flags );
1511 return 0;
1512 }
1513 }
1514 }
1515#endif
1516#ifdef RECORD_PLAY_CONCURRENCY
1517 char recConcPropValue[PROPERTY_VALUE_MAX];
1518 bool prop_rec_play_enabled = false;
1519
1520 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
1521 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
1522 }
1523 if ((prop_rec_play_enabled) &&
1524 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
1525 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
1526 if (AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1527 // allow VoIP using voice path
1528 // Do nothing
1529 } else if((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1530 ALOGD("voice_conc:MODE_IN_COMM is setforcing deep buffer output for non ULL... flags: %x", flags);
1531 // use deep buffer path for all non ULL outputs
1532 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1533 }
1534 } else if ((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1535 ALOGD("voice_conc:Record mode is on forcing deep buffer output for non ULL... flags: %x ", flags);
1536 // use deep buffer path for all non ULL outputs
1537 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1538 }
1539 }
1540 if (prop_rec_play_enabled &&
1541 (stream == AUDIO_STREAM_ENFORCED_AUDIBLE)) {
1542 ALOGD("Record conc is on forcing ULL output for ENFORCED_AUDIBLE");
1543 flags = AUDIO_OUTPUT_FLAG_FAST;
1544 }
1545#endif
1546
Sharad Sangle4509cef2015-08-19 20:47:12 +05301547#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Sharad Sangle36781612015-05-28 16:15:16 +05301548 /*
1549 * WFD audio routes back to target speaker when starting a ringtone playback.
1550 * This is because primary output is reused for ringtone, so output device is
1551 * updated based on SONIFICATION strategy for both ringtone and music playback.
1552 * The same issue is not seen on remoted_submix HAL based WFD audio because
1553 * primary output is not reused and a new output is created for ringtone playback.
1554 * Issue is fixed by updating output flag to AUDIO_OUTPUT_FLAG_FAST when there is
1555 * a non-music stream playback on WFD, so primary output is not reused for ringtone.
1556 */
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001557 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
1558 if ((availableOutputDeviceTypes & AUDIO_DEVICE_OUT_PROXY)
1559 && (stream != AUDIO_STREAM_MUSIC)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301560 ALOGD("WFD audio: use OUTPUT_FLAG_FAST for non music stream. flags:%x", flags );
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001561 //For voip paths
1562 if(flags & AUDIO_OUTPUT_FLAG_DIRECT)
1563 flags = AUDIO_OUTPUT_FLAG_DIRECT;
1564 else //route every thing else to ULL path
1565 flags = AUDIO_OUTPUT_FLAG_FAST;
1566 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301567#endif
1568
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001569 // open a direct output if required by specified parameters
vivek mehta0ea887a2015-08-26 14:01:20 -07001570 // force direct flag if offload flag is set: offloading implies a direct output stream
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001571 // and all common behaviors are driven by checking only the direct flag
1572 // this should normally be set appropriately in the policy configuration file
1573 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1574 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1575 }
1576 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1577 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1578 }
vivek mehta0ea887a2015-08-26 14:01:20 -07001579
vivek mehta4b0d8192015-10-16 00:25:59 -07001580 bool forced_deep = false;
Sharad Sangle36781612015-05-28 16:15:16 +05301581 // only allow deep buffering for music stream type
1582 if (stream != AUDIO_STREAM_MUSIC) {
1583 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Sharad Sangle497aef82015-08-03 17:55:48 +05301584 } else if (/* stream == AUDIO_STREAM_MUSIC && */
1585 flags == AUDIO_OUTPUT_FLAG_NONE &&
1586 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
vivek mehtac984b992015-11-25 13:28:55 -08001587 forced_deep = true;
Sharad Sangle36781612015-05-28 16:15:16 +05301588 }
Sharad Sangle497aef82015-08-03 17:55:48 +05301589
Sharad Sangle36781612015-05-28 16:15:16 +05301590 if (stream == AUDIO_STREAM_TTS) {
1591 flags = AUDIO_OUTPUT_FLAG_TTS;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001592 }
1593
vivek mehta4b0d8192015-10-16 00:25:59 -07001594 // Do offload magic here
vivek mehtac984b992015-11-25 13:28:55 -08001595 if ((flags == AUDIO_OUTPUT_FLAG_NONE) &&
1596 (stream == AUDIO_STREAM_MUSIC) &&
1597 (offloadInfo != NULL) &&
vivek mehta4b0d8192015-10-16 00:25:59 -07001598 ((offloadInfo->usage == AUDIO_USAGE_MEDIA) || (offloadInfo->usage == AUDIO_USAGE_GAME))) {
vivek mehtac984b992015-11-25 13:28:55 -08001599 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DIRECT_PCM);
vivek mehta4b0d8192015-10-16 00:25:59 -07001600 ALOGD("AudioCustomHAL --> Force Direct Flag .. flag (0x%x)", flags);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301601 }
1602
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001603 sp<IOProfile> profile;
1604
1605 // skip direct output selection if the request can obviously be attached to a mixed output
1606 // and not explicitly requested
vivek mehtac984b992015-11-25 13:28:55 -08001607 if (((flags & (AUDIO_OUTPUT_FLAG_DIRECT|AUDIO_OUTPUT_FLAG_DIRECT_PCM)) == 0) &&
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001608 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
1609 audio_channel_count_from_out_mask(channelMask) <= 2) {
1610 goto non_direct_output;
1611 }
1612
1613 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1614 // creating an offloaded track and tearing it down immediately after start when audioflinger
1615 // detects there is an active non offloadable effect.
1616 // FIXME: We should check the audio session here but we do not have it in this context.
1617 // This may prevent offloading in rare situations where effects are left active by apps
1618 // in the background.
1619
Sharad Sangle36781612015-05-28 16:15:16 +05301620 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
1621 !mEffects.isNonOffloadableEffectEnabled()) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001622 profile = getProfileForDirectOutput(device,
1623 samplingRate,
1624 format,
1625 channelMask,
1626 (audio_output_flags_t)flags);
1627 }
1628
1629 if (profile != 0) {
vivek mehtac984b992015-11-25 13:28:55 -08001630
1631 if (!(flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) &&
1632 (profile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT_PCM)) {
1633 ALOGI("got Direct_PCM without requesting ... reject ");
1634 profile = NULL;
1635 goto non_direct_output;
1636 }
1637
Sharad Sangle36781612015-05-28 16:15:16 +05301638 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001639
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001640 // if multiple concurrent offload decode is supported
1641 // do no check for reuse and also don't close previous output if its offload
1642 // previous output will be closed during track destruction
1643 if (!(property_get_bool("audio.offload.multiple.enabled", false) &&
1644 ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0))) {
1645 for (size_t i = 0; i < mOutputs.size(); i++) {
1646 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1647 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1648 outputDesc = desc;
1649 // reuse direct output if currently open and configured with same parameters
1650 if ((samplingRate == outputDesc->mSamplingRate) &&
1651 (format == outputDesc->mFormat) &&
1652 (channelMask == outputDesc->mChannelMask)) {
1653 outputDesc->mDirectOpenCount++;
1654 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1655 return mOutputs.keyAt(i);
1656 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001657 }
1658 }
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001659 // close direct output if currently open and configured with different parameters
1660 if (outputDesc != NULL) {
1661 closeOutput(outputDesc->mIoHandle);
1662 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001663 }
Sharad Sangle36781612015-05-28 16:15:16 +05301664
1665 // if the selected profile is offloaded and no offload info was specified,
1666 // create a default one
1667 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
1668 if ((profile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
1669 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1670 defaultOffloadInfo.sample_rate = samplingRate;
1671 defaultOffloadInfo.channel_mask = channelMask;
1672 defaultOffloadInfo.format = format;
1673 defaultOffloadInfo.stream_type = stream;
1674 defaultOffloadInfo.bit_rate = 0;
1675 defaultOffloadInfo.duration_us = -1;
1676 defaultOffloadInfo.has_video = true; // conservative
1677 defaultOffloadInfo.is_streaming = true; // likely
1678 offloadInfo = &defaultOffloadInfo;
1679 }
1680
1681 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001682 outputDesc->mDevice = device;
1683 outputDesc->mLatency = 0;
Sharad Sangle36781612015-05-28 16:15:16 +05301684 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001685 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1686 config.sample_rate = samplingRate;
1687 config.channel_mask = channelMask;
1688 config.format = format;
1689 if (offloadInfo != NULL) {
1690 config.offload_info = *offloadInfo;
1691 }
Sharad Sangle36781612015-05-28 16:15:16 +05301692 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001693 &output,
1694 &config,
1695 &outputDesc->mDevice,
1696 String8(""),
1697 &outputDesc->mLatency,
1698 outputDesc->mFlags);
1699
1700 // only accept an output with the requested parameters
1701 if (status != NO_ERROR ||
1702 (samplingRate != 0 && samplingRate != config.sample_rate) ||
1703 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
1704 (channelMask != 0 && channelMask != config.channel_mask)) {
1705 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1706 "format %d %d, channelMask %04x %04x", output, samplingRate,
1707 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1708 outputDesc->mChannelMask);
1709 if (output != AUDIO_IO_HANDLE_NONE) {
1710 mpClientInterface->closeOutput(output);
1711 }
Sharad Sangle36781612015-05-28 16:15:16 +05301712 // fall back to mixer output if possible when the direct output could not be open
1713 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
1714 goto non_direct_output;
1715 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001716 return AUDIO_IO_HANDLE_NONE;
1717 }
1718 outputDesc->mSamplingRate = config.sample_rate;
1719 outputDesc->mChannelMask = config.channel_mask;
1720 outputDesc->mFormat = config.format;
1721 outputDesc->mRefCount[stream] = 0;
1722 outputDesc->mStopTime[stream] = 0;
1723 outputDesc->mDirectOpenCount = 1;
1724
1725 audio_io_handle_t srcOutput = getOutputForEffect();
1726 addOutput(output, outputDesc);
1727 audio_io_handle_t dstOutput = getOutputForEffect();
1728 if (dstOutput == output) {
Gao Jiedb057832015-11-12 08:51:22 +08001729#ifdef DOLBY_ENABLE
1730 status_t status = mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1731 if (status == NO_ERROR) {
1732 for (size_t i = 0; i < mEffects.size(); i++) {
1733 sp<EffectDescriptor> desc = mEffects.valueAt(i);
1734 if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX) {
1735 // update the mIo member of EffectDescriptor for the global effect
1736 ALOGV("%s updating mIo", __FUNCTION__);
1737 desc->mIo = dstOutput;
1738 }
1739 }
1740 } else {
1741 ALOGW("%s moveEffects from %d to %d failed", __FUNCTION__, srcOutput, dstOutput);
1742 }
1743#else // DOLBY_END
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001744 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
Gao Jiedb057832015-11-12 08:51:22 +08001745#endif // LINE_ADDED_BY_DOLBY
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001746 }
1747 mPreviousOutputs = mOutputs;
1748 ALOGV("getOutput() returns new direct output %d", output);
1749 mpClientInterface->onAudioPortListUpdate();
1750 return output;
1751 }
1752
1753non_direct_output:
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001754 // ignoring channel mask due to downmix capability in mixer
1755
1756 // open a non direct output
1757
1758 // for non direct outputs, only PCM is supported
1759 if (audio_is_linear_pcm(format)) {
vivek mehtac984b992015-11-25 13:28:55 -08001760 if (forced_deep) {
1761 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
1762 ALOGI("setting force DEEP buffer now ");
1763 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001764 // get which output is suitable for the specified stream. The actual
1765 // routing change will happen when startOutput() will be called
1766 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1767
1768 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1769 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1770 output = selectOutput(outputs, flags, format);
1771 }
1772 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1773 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1774
vivek mehta0ea887a2015-08-26 14:01:20 -07001775 ALOGV("getOutputForDevice() returns output %d", output);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001776
1777 return output;
1778}
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301779
1780status_t AudioPolicyManagerCustom::getInputForAttr(const audio_attributes_t *attr,
1781 audio_io_handle_t *input,
1782 audio_session_t session,
1783 uid_t uid,
1784 uint32_t samplingRate,
1785 audio_format_t format,
1786 audio_channel_mask_t channelMask,
1787 audio_input_flags_t flags,
1788 audio_port_handle_t selectedDeviceId,
1789 input_type_t *inputType)
1790{
1791 audio_source_t inputSource = attr->source;
1792#ifdef VOICE_CONCURRENCY
1793
1794 char propValue[PROPERTY_VALUE_MAX];
1795 bool prop_rec_enabled=false, prop_voip_enabled = false;
1796
1797 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
1798 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1799 }
1800
1801 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1802 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1803 }
1804
1805 if (prop_rec_enabled && mvoice_call_state) {
1806 //check if voice call is active / running in background
1807 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1808 //Need to block input request
1809 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1810 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1811 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1812 {
1813 switch(inputSource) {
1814 case AUDIO_SOURCE_VOICE_UPLINK:
1815 case AUDIO_SOURCE_VOICE_DOWNLINK:
1816 case AUDIO_SOURCE_VOICE_CALL:
1817 ALOGD("voice_conc:Creating input during incall mode for inputSource: %d",
1818 inputSource);
1819 break;
1820
1821 case AUDIO_SOURCE_VOICE_COMMUNICATION:
1822 if(prop_voip_enabled) {
1823 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1824 inputSource);
1825 return NO_INIT;
1826 }
1827 break;
1828 default:
1829 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1830 inputSource);
1831 return NO_INIT;
1832 }
1833 }
1834 }//check for VoIP flag
1835 else if(prop_voip_enabled && mvoice_call_state) {
1836 //check if voice call is active / running in background
1837 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1838 //Need to block input request
1839 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1840 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1841 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1842 {
1843 if(inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1844 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
1845 return NO_INIT;
1846 }
1847 }
1848 }
1849
1850#endif
1851
1852 return AudioPolicyManager::getInputForAttr(attr,
1853 input,
1854 session,
1855 uid,
1856 samplingRate,
1857 format,
1858 channelMask,
1859 flags,
1860 selectedDeviceId,
1861 inputType);
1862}
1863status_t AudioPolicyManagerCustom::startInput(audio_io_handle_t input,
1864 audio_session_t session)
1865{
1866 ALOGV("startInput() input %d", input);
1867 ssize_t index = mInputs.indexOfKey(input);
1868 if (index < 0) {
1869 ALOGW("startInput() unknown input %d", input);
1870 return BAD_VALUE;
1871 }
1872 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1873
1874 index = inputDesc->mSessions.indexOf(session);
1875 if (index < 0) {
1876 ALOGW("startInput() unknown session %d on input %d", session, input);
1877 return BAD_VALUE;
1878 }
1879
1880 // virtual input devices are compatible with other input devices
1881 if (!is_virtual_input_device(inputDesc->mDevice)) {
1882
1883 // for a non-virtual input device, check if there is another (non-virtual) active input
1884 audio_io_handle_t activeInput = mInputs.getActiveInput();
1885 if (activeInput != 0 && activeInput != input) {
1886
1887 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1888 // otherwise the active input continues and the new input cannot be started.
1889 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
1890 if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
1891 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
1892 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
1893 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
1894 } else {
1895 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
1896 return INVALID_OPERATION;
1897 }
1898 }
1899 }
1900
1901 // Routing?
1902 mInputRoutes.incRouteActivity(session);
1903#ifdef RECORD_PLAY_CONCURRENCY
1904 mIsInputRequestOnProgress = true;
1905
1906 char getPropValue[PROPERTY_VALUE_MAX];
1907 bool prop_rec_play_enabled = false;
1908
1909 if (property_get("rec.playback.conc.disabled", getPropValue, NULL)) {
1910 prop_rec_play_enabled = atoi(getPropValue) || !strncmp("true", getPropValue, 4);
1911 }
1912
1913 if ((prop_rec_play_enabled) &&(mInputs.activeInputsCount() == 0)){
1914 // send update to HAL on record playback concurrency
1915 AudioParameter param = AudioParameter();
1916 param.add(String8("rec_play_conc_on"), String8("true"));
1917 ALOGD("startInput() setParameters rec_play_conc is setting to ON ");
1918 mpClientInterface->setParameters(0, param.toString());
1919
1920 // Call invalidate to reset all opened non ULL audio tracks
1921 // Move tracks associated to this strategy from previous output to new output
1922 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
1923 // Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder)
Sharad Sangle4509cef2015-08-19 20:47:12 +05301924 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE && (i != AUDIO_STREAM_PATCH))) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301925 ALOGD("Invalidate on releaseInput for stream :: %d ", i);
1926 //FIXME see fixme on name change
1927 mpClientInterface->invalidateStream((audio_stream_type_t)i);
1928 }
1929 }
1930 // close compress tracks
1931 for (size_t i = 0; i < mOutputs.size(); i++) {
1932 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
1933 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
1934 ALOGD("ouput desc / profile is NULL");
1935 continue;
1936 }
1937 if (outputDesc->mProfile->mFlags
1938 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1939 // close compress sessions
1940 ALOGD("calling closeOutput on record conc for COMPRESS output");
1941 closeOutput(mOutputs.keyAt(i));
1942 }
1943 }
1944 }
1945#endif
1946
1947 if (inputDesc->mRefCount == 0 || mInputRoutes.hasRouteChanged(session)) {
1948 // if input maps to a dynamic policy with an activity listener, notify of state change
1949 if ((inputDesc->mPolicyMix != NULL)
1950 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1951 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1952 MIX_STATE_MIXING);
1953 }
1954
1955 if (mInputs.activeInputsCount() == 0) {
1956 SoundTrigger::setCaptureState(true);
1957 }
1958 setInputDevice(input, getNewInputDevice(input), true /* force */);
1959
1960 // automatically enable the remote submix output when input is started if not
1961 // used by a policy mix of type MIX_TYPE_RECORDERS
1962 // For remote submix (a virtual device), we open only one input per capture request.
1963 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1964 String8 address = String8("");
1965 if (inputDesc->mPolicyMix == NULL) {
1966 address = String8("0");
1967 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1968 address = inputDesc->mPolicyMix->mRegistrationId;
1969 }
1970 if (address != "") {
1971 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1972 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1973 address, "remote-submix");
1974 }
1975 }
1976 }
1977
1978 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1979
1980 inputDesc->mRefCount++;
1981#ifdef RECORD_PLAY_CONCURRENCY
1982 mIsInputRequestOnProgress = false;
1983#endif
1984 return NO_ERROR;
1985}
1986status_t AudioPolicyManagerCustom::stopInput(audio_io_handle_t input,
1987 audio_session_t session)
1988{
1989 status_t status;
1990 status = AudioPolicyManager::stopInput(input, session);
1991#ifdef RECORD_PLAY_CONCURRENCY
1992 char propValue[PROPERTY_VALUE_MAX];
1993 bool prop_rec_play_enabled = false;
1994
1995 if (property_get("rec.playback.conc.disabled", propValue, NULL)) {
1996 prop_rec_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1997 }
1998
1999 if ((prop_rec_play_enabled) && (mInputs.activeInputsCount() == 0)) {
2000
2001 //send update to HAL on record playback concurrency
2002 AudioParameter param = AudioParameter();
2003 param.add(String8("rec_play_conc_on"), String8("false"));
2004 ALOGD("stopInput() setParameters rec_play_conc is setting to OFF ");
2005 mpClientInterface->setParameters(0, param.toString());
2006
2007 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
2008 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
2009 //Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder stop tone)
2010 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE) && (i != AUDIO_STREAM_PATCH)) {
2011 ALOGD(" Invalidate on stopInput for stream :: %d ", i);
2012 //FIXME see fixme on name change
2013 mpClientInterface->invalidateStream((audio_stream_type_t)i);
2014 }
2015 }
2016 }
2017#endif
2018 return status;
2019}
2020
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302021void AudioPolicyManagerCustom::closeAllInputs() {
2022 bool patchRemoved = false;
2023
Dhanalakshmi Siddanibfeca7f2015-10-08 15:17:11 +05302024 for(size_t input_index = mInputs.size(); input_index > 0; input_index--) {
2025 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index-1);
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302026 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
2027 if (patch_index >= 0) {
2028 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
2029 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2030 mAudioPatches.removeItemsAt(patch_index);
2031 patchRemoved = true;
2032 }
2033 if ((inputDesc->mIsSoundTrigger) && (mInputs.size() == 1)) {
2034 ALOGD("Do not close sound trigger input handle");
2035 } else {
Dhanalakshmi Siddanibfeca7f2015-10-08 15:17:11 +05302036 mpClientInterface->closeInput(mInputs.keyAt(input_index-1));
2037 mInputs.removeItem(mInputs.keyAt(input_index-1));
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05302038 }
2039 }
2040 nextAudioPortGeneration();
2041
2042 if (patchRemoved) {
2043 mpClientInterface->onAudioPatchListUpdate();
2044 }
2045}
2046
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302047AudioPolicyManagerCustom::AudioPolicyManagerCustom(AudioPolicyClientInterface *clientInterface)
Sharad Sangle4509cef2015-08-19 20:47:12 +05302048 : AudioPolicyManager(clientInterface),
2049 mHdmiAudioDisabled(false),
2050 mHdmiAudioEvent(false),
Haynes Mathew George0b2c3772015-12-14 14:22:07 -08002051 mPrevPhoneState(0),
Haynes Mathew George603ae9b2015-12-21 17:23:59 -08002052 mPrevFMVolumeDb(0.0f),
2053 mFMIsActive(false)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302054{
Mingming Yin38ea08c2015-10-05 15:24:04 -07002055 char ssr_enabled[PROPERTY_VALUE_MAX] = {0};
2056 bool prop_ssr_enabled = false;
2057
2058 if (property_get("ro.qc.sdk.audio.ssr", ssr_enabled, NULL)) {
2059 prop_ssr_enabled = atoi(ssr_enabled) || !strncmp("true", ssr_enabled, 4);
2060 }
2061
2062 for (size_t i = 0; i < mHwModules.size(); i++) {
2063 ALOGV("Hw module %d", i);
2064 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) {
2065 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
2066 ALOGV("Input profile ", j);
2067 for (size_t k = 0; k < inProfile->mChannelMasks.size(); k++) {
2068 audio_channel_mask_t channelMask =
2069 inProfile->mChannelMasks.itemAt(k);
2070 ALOGV("Channel Mask %x size %d", channelMask,
2071 inProfile->mChannelMasks.size());
2072 if (AUDIO_CHANNEL_IN_5POINT1 == channelMask) {
2073 if (!prop_ssr_enabled) {
2074 ALOGI("removing AUDIO_CHANNEL_IN_5POINT1 from"
2075 " input profile as SSR(surround sound record)"
2076 " is not supported on this chipset variant");
2077 inProfile->mChannelMasks.removeItemsAt(k, 1);
2078 ALOGV("Channel Mask size now %d",
2079 inProfile->mChannelMasks.size());
2080 }
2081 }
2082 }
2083 }
2084 }
2085
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302086#ifdef RECORD_PLAY_CONCURRENCY
2087 mIsInputRequestOnProgress = false;
2088#endif
2089
2090
2091#ifdef VOICE_CONCURRENCY
2092 mFallBackflag = getFallBackPath();
2093#endif
2094}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07002095}