blob: 77bf0e1bc1caf05695423463e356464422f92857 [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.
18 */
19
Sharad Sangle36781612015-05-28 16:15:16 +053020#define LOG_TAG "AudioPolicyManagerCustom"
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070021//#define LOG_NDEBUG 0
22
23//#define VERY_VERBOSE_LOGGING
24#ifdef VERY_VERBOSE_LOGGING
25#define ALOGVV ALOGV
26#else
27#define ALOGVV(a...) do { } while(0)
28#endif
Sharad Sangle36781612015-05-28 16:15:16 +053029
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070030// A device mask for all audio output devices that are considered "remote" when evaluating
31// active output devices in isStreamActiveRemotely()
32#define APM_AUDIO_OUT_DEVICE_REMOTE_ALL AUDIO_DEVICE_OUT_REMOTE_SUBMIX
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070033// A device mask for all audio input and output devices where matching inputs/outputs on device
34// type alone is not enough: the address must match too
35#define APM_AUDIO_DEVICE_MATCH_ADDRESS_ALL (AUDIO_DEVICE_IN_REMOTE_SUBMIX | \
36 AUDIO_DEVICE_OUT_REMOTE_SUBMIX)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070037
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070038#include <inttypes.h>
Mingming Yin0ae14ea2014-07-09 17:55:56 -070039#include <math.h>
Mingming Yin0670f162014-06-12 16:05:49 -070040
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070041#include <cutils/properties.h>
42#include <utils/Log.h>
43#include <hardware/audio.h>
44#include <hardware/audio_effect.h>
45#include <media/AudioParameter.h>
46#include <soundtrigger/SoundTrigger.h>
47#include "AudioPolicyManager.h"
Sharad Sangle36781612015-05-28 16:15:16 +053048#include <policy.h>
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070049
50namespace android {
Sharad Sanglec5766ff2015-06-04 20:24:10 +053051#ifdef VOICE_CONCURRENCY
52audio_output_flags_t AudioPolicyManagerCustom::getFallBackPath()
53{
54 audio_output_flags_t flag = AUDIO_OUTPUT_FLAG_FAST;
55 char propValue[PROPERTY_VALUE_MAX];
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070056
Sharad Sanglec5766ff2015-06-04 20:24:10 +053057 if (property_get("voice.conc.fallbackpath", propValue, NULL)) {
58 if (!strncmp(propValue, "deep-buffer", 11)) {
59 flag = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
60 }
61 else if (!strncmp(propValue, "fast", 4)) {
62 flag = AUDIO_OUTPUT_FLAG_FAST;
63 }
64 else {
65 ALOGD("voice_conc:not a recognised path(%s) in prop voice.conc.fallbackpath",
66 propValue);
67 }
68 }
69 else {
70 ALOGD("voice_conc:prop voice.conc.fallbackpath not set");
71 }
72
73 ALOGD("voice_conc:picked up flag(0x%x) from prop voice.conc.fallbackpath",
74 flag);
75
76 return flag;
77}
78#endif /*VOICE_CONCURRENCY*/
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070079// ----------------------------------------------------------------------------
80// AudioPolicyInterface implementation
81// ----------------------------------------------------------------------------
Sharad Sangle36781612015-05-28 16:15:16 +053082extern "C" AudioPolicyInterface* createAudioPolicyManager(
83 AudioPolicyClientInterface *clientInterface)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070084{
Sharad Sangle36781612015-05-28 16:15:16 +053085 return new AudioPolicyManagerCustom(clientInterface);
86}
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070087
Sharad Sangle36781612015-05-28 16:15:16 +053088extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface)
89{
90 delete interface;
91}
92
93status_t AudioPolicyManagerCustom::setDeviceConnectionStateInt(audio_devices_t device,
94 audio_policy_dev_state_t state,
95 const char *device_address,
96 const char *device_name)
97{
98 ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
99 device, state, device_address, device_name);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700100
101 // connect/disconnect only 1 device at a time
102 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
103
Sharad Sangle36781612015-05-28 16:15:16 +0530104 sp<DeviceDescriptor> devDesc =
105 mHwModules.getDeviceDescriptor(device, device_address, device_name);
106
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700107 // handle output devices
108 if (audio_is_output_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700109 SortedVector <audio_io_handle_t> outputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700110
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700111 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700112
113 // save a copy of the opened output descriptors before any output is opened or closed
114 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
115 mPreviousOutputs = mOutputs;
116 switch (state)
117 {
118 // handle output device connection
Sharad Sangle36781612015-05-28 16:15:16 +0530119 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700120 if (index >= 0) {
Sharad Sangle4509cef2015-08-19 20:47:12 +0530121#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
122 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
123 if (!strncmp(device_address, "hdmi_spkr", 9)) {
124 mHdmiAudioDisabled = false;
125 } else {
126 mHdmiAudioEvent = true;
127 }
128 }
129#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700130 ALOGW("setDeviceConnectionState() device already connected: %x", device);
131 return INVALID_OPERATION;
132 }
133 ALOGV("setDeviceConnectionState() connecting device %x", device);
134
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700135 // register new device as available
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700136 index = mAvailableOutputDevices.add(devDesc);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530137#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
138 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
139 if (!strncmp(device_address, "hdmi_spkr", 9)) {
140 mHdmiAudioDisabled = false;
141 } else {
142 mHdmiAudioEvent = true;
143 }
144 if (mHdmiAudioDisabled || !mHdmiAudioEvent) {
145 mAvailableOutputDevices.remove(devDesc);
146 ALOGW("HDMI sink not connected, do not route audio to HDMI out");
147 return INVALID_OPERATION;
148 }
149 }
150#endif
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700151 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530152 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700153 if (module == 0) {
154 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
155 device);
156 mAvailableOutputDevices.remove(devDesc);
157 return INVALID_OPERATION;
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700158 }
Sharad Sangle36781612015-05-28 16:15:16 +0530159 mAvailableOutputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700160 } else {
161 return NO_MEMORY;
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700162 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700163
Sharad Sangle36781612015-05-28 16:15:16 +0530164 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700165 mAvailableOutputDevices.remove(devDesc);
166 return INVALID_OPERATION;
167 }
Sharad Sangle36781612015-05-28 16:15:16 +0530168 // Propagate device availability to Engine
169 mEngine->setDeviceConnectionState(devDesc, state);
170
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700171 // outputs should never be empty here
172 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
173 "checkOutputsForDevice() returned no outputs but status OK");
174 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
175 outputs.size());
Sharad Sangle36781612015-05-28 16:15:16 +0530176
177 // Send connect to HALs
178 AudioParameter param = AudioParameter(devDesc->mAddress);
179 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
180 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
181
182 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700183 // handle output device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700184 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
185 if (index < 0) {
Sharad Sangle4509cef2015-08-19 20:47:12 +0530186#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
187 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
188 if (!strncmp(device_address, "hdmi_spkr", 9)) {
189 mHdmiAudioDisabled = true;
190 } else {
191 mHdmiAudioEvent = false;
192 }
193 }
194#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700195 ALOGW("setDeviceConnectionState() device not connected: %x", device);
196 return INVALID_OPERATION;
197 }
198
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700199 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
200
Sharad Sangle36781612015-05-28 16:15:16 +0530201 // Send Disconnect to HALs
202 AudioParameter param = AudioParameter(devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700203 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
204 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
205
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700206 // remove device from available output devices
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700207 mAvailableOutputDevices.remove(devDesc);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530208#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
209 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
210 if (!strncmp(device_address, "hdmi_spkr", 9)) {
211 mHdmiAudioDisabled = true;
212 } else {
213 mHdmiAudioEvent = false;
214 }
215 }
216#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530217 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
218
219 // Propagate device availability to Engine
220 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700221 } break;
222
223 default:
224 ALOGE("setDeviceConnectionState() invalid state: %x", state);
225 return BAD_VALUE;
226 }
227
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700228 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
229 // output is suspended before any tracks are moved to it
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700230 checkA2dpSuspend();
231 checkOutputForAllStrategies();
232 // outputs must be closed after checkOutputForAllStrategies() is executed
233 if (!outputs.isEmpty()) {
234 for (size_t i = 0; i < outputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530235 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700236 // close unused outputs after device disconnection or direct outputs that have been
237 // opened by checkOutputsForDevice() to query dynamic parameters
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700238 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700239 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
240 (desc->mDirectOpenCount == 0))) {
241 closeOutput(outputs[i]);
242 }
243 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700244 // check again after closing A2DP output to reset mA2dpSuspended if needed
245 checkA2dpSuspend();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700246 }
247
248 updateDevicesAndOutputs();
Sharad Sangle36781612015-05-28 16:15:16 +0530249 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
250 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700251 updateCallRouting(newDevice);
252 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530253
254#ifdef FM_POWER_OPT
255 // handle FM device connection state to trigger FM AFE loopback
256 if(device == AUDIO_DEVICE_OUT_FM && hasPrimaryOutput()) {
257 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
258 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
259 mPrimaryOutput->changeRefCount(AUDIO_STREAM_MUSIC, 1);
260 newDevice = newDevice | AUDIO_DEVICE_OUT_FM;
261 } else {
262 mPrimaryOutput->changeRefCount(AUDIO_STREAM_MUSIC, -1);
263 }
264 AudioParameter param = AudioParameter();
265 param.addInt(String8("handle_fm"), (int)newDevice);
266 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString());
267 }
268#endif /* FM_POWER_OPT end */
269
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700270 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530271 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
272 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
273 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700274 // do not force device change on duplicated output because if device is 0, it will
275 // also force a device 0 for the two outputs it is duplicated to which may override
276 // a valid device selection on those outputs.
Sharad Sangle36781612015-05-28 16:15:16 +0530277 bool force = !desc->isDuplicated()
278 && (!device_distinguishes_on_address(device)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700279 // always force when disconnecting (a non-duplicated device)
280 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Sharad Sangle36781612015-05-28 16:15:16 +0530281 setOutputDevice(desc, newDevice, force, 0);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700282 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700283 }
284
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700285 mpClientInterface->onAudioPortListUpdate();
286 return NO_ERROR;
287 } // end if is output device
288
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700289 // handle input devices
290 if (audio_is_input_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700291 SortedVector <audio_io_handle_t> inputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700292
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700293 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700294 switch (state)
295 {
296 // handle input device connection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700297 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
298 if (index >= 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700299 ALOGW("setDeviceConnectionState() device already connected: %d", device);
300 return INVALID_OPERATION;
301 }
Sharad Sangle36781612015-05-28 16:15:16 +0530302 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700303 if (module == NULL) {
304 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
305 device);
306 return INVALID_OPERATION;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700307 }
Sharad Sangle36781612015-05-28 16:15:16 +0530308 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700309 return INVALID_OPERATION;
310 }
311
312 index = mAvailableInputDevices.add(devDesc);
313 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530314 mAvailableInputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700315 } else {
316 return NO_MEMORY;
317 }
Sharad Sangle36781612015-05-28 16:15:16 +0530318
319 // Set connect to HALs
320 AudioParameter param = AudioParameter(devDesc->mAddress);
321 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
322 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
323
324 // Propagate device availability to Engine
325 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700326 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700327
328 // handle input device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700329 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
330 if (index < 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700331 ALOGW("setDeviceConnectionState() device not connected: %d", device);
332 return INVALID_OPERATION;
333 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700334
335 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
336
337 // Set Disconnect to HALs
Sharad Sangle36781612015-05-28 16:15:16 +0530338 AudioParameter param = AudioParameter(devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700339 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
340 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
341
Sharad Sangle36781612015-05-28 16:15:16 +0530342 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700343 mAvailableInputDevices.remove(devDesc);
344
Sharad Sangle36781612015-05-28 16:15:16 +0530345 // Propagate device availability to Engine
346 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700347 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700348
349 default:
350 ALOGE("setDeviceConnectionState() invalid state: %x", state);
351 return BAD_VALUE;
352 }
353
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700354 closeAllInputs();
355
Sharad Sangle36781612015-05-28 16:15:16 +0530356 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700357 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
358 updateCallRouting(newDevice);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700359 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700360
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700361 mpClientInterface->onAudioPortListUpdate();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700362 return NO_ERROR;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700363 } // end if is input device
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700364
365 ALOGW("setDeviceConnectionState() invalid device: %x", device);
366 return BAD_VALUE;
367}
Sharad Sangle36781612015-05-28 16:15:16 +0530368// This function checks for the parameters which can be offloaded.
369// This can be enhanced depending on the capability of the DSP and policy
370// of the system.
371bool AudioPolicyManagerCustom::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700372{
Sharad Sangle36781612015-05-28 16:15:16 +0530373 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
374 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
375 offloadInfo.sample_rate, offloadInfo.channel_mask,
376 offloadInfo.format,
377 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
378 offloadInfo.has_video);
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530379#ifdef VOICE_CONCURRENCY
380 char concpropValue[PROPERTY_VALUE_MAX];
381 if (property_get("voice.playback.conc.disabled", concpropValue, NULL)) {
382 bool propenabled = atoi(concpropValue) || !strncmp("true", concpropValue, 4);
383 if (propenabled) {
384 if (isInCall())
385 {
386 ALOGD("\n copl: blocking compress offload on call mode\n");
387 return false;
388 }
389 }
390 }
391#endif
392#ifdef RECORD_PLAY_CONCURRENCY
393 char recConcPropValue[PROPERTY_VALUE_MAX];
394 bool prop_rec_play_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700395
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530396 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
397 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
398 }
399
400 if ((prop_rec_play_enabled) &&
401 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
402 ALOGD("copl: blocking compress offload for record concurrency");
403 return false;
404 }
405#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530406 // Check if stream type is music, then only allow offload as of now.
407 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
408 {
409 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
410 return false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700411 }
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530412
Alexy Joseph0ac09032015-10-16 15:57:53 -0700413 // Check if offload has been disabled
414 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
415 if (offloadDisabled) {
416 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
417 return false;
418 }
419
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530420 char propValue[PROPERTY_VALUE_MAX];
421 bool pcmOffload = false;
422#ifdef PCM_OFFLOAD_ENABLED
423 if ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM_OFFLOAD) {
424 bool prop_enabled = false;
425 if ((AUDIO_FORMAT_PCM_16_BIT_OFFLOAD == offloadInfo.format) &&
426 property_get("audio.offload.pcm.16bit.enable", propValue, NULL)) {
427 prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
428 }
429
430#ifdef PCM_OFFLOAD_ENABLED_24
431 if ((AUDIO_FORMAT_PCM_24_BIT_OFFLOAD == offloadInfo.format) &&
432 property_get("audio.offload.pcm.24bit.enable", propValue, NULL)) {
433 prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530434 }
435#endif
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530436
437 if (prop_enabled) {
438 ALOGI("PCM offload property is enabled");
439 pcmOffload = true;
440 }
441
442 if (!pcmOffload) {
443 ALOGD("system property not enabled for PCM offload format[%x]",offloadInfo.format);
444 return false;
445 }
446 }
447#endif
448 if (!pcmOffload) {
Alexy Joseph0ac09032015-10-16 15:57:53 -0700449
450 bool compressedOffloadDisabled = property_get_bool("audio.offload.compress.disable", false);
451 if (compressedOffloadDisabled) {
452 ALOGI("compressed offload disabled by audio.offload.compress.disable=%d", compressedOffloadDisabled);
453 return false;
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530454 }
Alexy Joseph0ac09032015-10-16 15:57:53 -0700455
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530456 //check if it's multi-channel AAC (includes sub formats) and FLAC format
457 if ((popcount(offloadInfo.channel_mask) > 2) &&
458 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
459 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS))) {
460 ALOGD("offload disabled for multi-channel AAC,FLAC and VORBIS format");
461 return false;
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530462 }
463
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530464#ifdef AUDIO_EXTN_FORMATS_ENABLED
465 //check if it's multi-channel FLAC/ALAC/WMA format with sample rate > 48k
466 if ((popcount(offloadInfo.channel_mask) > 2) &&
467 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
Manish Dewangana6fc5442015-08-24 20:30:31 +0530468 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) && (offloadInfo.sample_rate > 48000)) ||
469 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) && (offloadInfo.sample_rate > 48000)) ||
470 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) && (offloadInfo.sample_rate > 48000)) ||
471 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS))) {
472 ALOGD("offload disabled for multi-channel FLAC/ALAC/WMA/AAC_ADTS clips with sample rate > 48kHz");
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530473 return false;
474 }
475#endif
476 //TODO: enable audio offloading with video when ready
477 const bool allowOffloadWithVideo =
478 property_get_bool("audio.offload.video", false /* default_value */);
479 if (offloadInfo.has_video && !allowOffloadWithVideo) {
480 ALOGV("isOffloadSupported: has_video == true, returning false");
481 return false;
482 }
Manish Dewanganf3cd0f82015-10-13 14:04:36 +0530483
484 const bool allowOffloadStreamingWithVideo = property_get_bool("av.streaming.offload.enable",
485 false /*default value*/);
486 if(offloadInfo.has_video && offloadInfo.is_streaming && !allowOffloadStreamingWithVideo) {
487 ALOGW("offload disabled by av.streaming.offload.enable = %s ", propValue );
488 return false;
489 }
490
Sharad Sangle36781612015-05-28 16:15:16 +0530491 }
492
493 //If duration is less than minimum value defined in property, return false
494 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
495 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
496 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
497 return false;
498 }
499 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
500 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
501 //duration checks only valid for MP3/AAC/ formats,
502 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
503 if ((offloadInfo.format == AUDIO_FORMAT_MP3) ||
504 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530505 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS) ||
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530506#ifdef AUDIO_EXTN_FORMATS_ENABLED
Sharad Sangle36781612015-05-28 16:15:16 +0530507 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
Sharad Sangle36781612015-05-28 16:15:16 +0530508 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) ||
509 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) ||
510 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) ||
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530511 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_APE) ||
Manish Dewangana6fc5442015-08-24 20:30:31 +0530512 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS) ||
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530513#endif
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530514 pcmOffload)
Sharad Sangle36781612015-05-28 16:15:16 +0530515 return false;
516
517 }
518
519 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
520 // creating an offloaded track and tearing it down immediately after start when audioflinger
521 // detects there is an active non offloadable effect.
522 // FIXME: We should check the audio session here but we do not have it in this context.
523 // This may prevent offloading in rare situations where effects are left active by apps
524 // in the background.
525 if (mEffects.isNonOffloadableEffectEnabled()) {
526 return false;
527 }
528 // Check for soundcard status
529 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
530 String8("SND_CARD_STATUS"));
531 AudioParameter result = AudioParameter(valueStr);
532 int isonline = 0;
533 if ((result.getInt(String8("SND_CARD_STATUS"), isonline) == NO_ERROR)
534 && !isonline) {
535 ALOGD("copl: soundcard is offline rejecting offload request");
536 return false;
537 }
538 // See if there is a profile to support this.
539 // AUDIO_DEVICE_NONE
540 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
541 offloadInfo.sample_rate,
542 offloadInfo.format,
543 offloadInfo.channel_mask,
544 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
545 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
546 return (profile != 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700547}
Sharad Sangle36781612015-05-28 16:15:16 +0530548audio_devices_t AudioPolicyManagerCustom::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
549 bool fromCache)
550{
551 audio_devices_t device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700552
Sharad Sangle36781612015-05-28 16:15:16 +0530553 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
554 if (index >= 0) {
555 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
556 if (patchDesc->mUid != mUidCached) {
557 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
558 outputDesc->device(), outputDesc->mPatchHandle);
559 return outputDesc->device();
560 }
561 }
562
563 // check the following by order of priority to request a routing change if necessary:
564 // 1: the strategy enforced audible is active and enforced on the output:
565 // use device for strategy enforced audible
566 // 2: we are in call or the strategy phone is active on the output:
567 // use device for strategy phone
568 // 3: the strategy for enforced audible is active but not enforced on the output:
569 // use the device for strategy enforced audible
570 // 4: the strategy sonification is active on the output:
571 // use device for strategy sonification
572 // 5: the strategy "respectful" sonification is active on the output:
573 // use device for strategy "respectful" sonification
574 // 6: the strategy accessibility is active on the output:
575 // use device for strategy accessibility
576 // 7: the strategy media is active on the output:
577 // use device for strategy media
578 // 8: the strategy DTMF is active on the output:
579 // use device for strategy DTMF
580 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
581 // use device for strategy t-t-s
582 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
583 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
584 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
585 } else if (isInCall() ||
586 isStrategyActive(outputDesc, STRATEGY_PHONE)||
587 isStrategyActive(mPrimaryOutput, STRATEGY_PHONE)) {
588 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
589 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
590 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
591 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)||
592 (isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION)
593 && (!isStrategyActive(mPrimaryOutput,STRATEGY_MEDIA)))) {
594 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530595 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL) ||
596 isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION_RESPECTFUL)) {
Sharad Sangle36781612015-05-28 16:15:16 +0530597 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
598 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
599 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
600 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
601 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
602 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
603 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
604 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
605 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
606 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
607 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
608 }
609
610 ALOGV("getNewOutputDevice() selected device %x", device);
611 return device;
612}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700613void AudioPolicyManagerCustom::setPhoneState(audio_mode_t state)
614{
Sharad Sangle36781612015-05-28 16:15:16 +0530615 ALOGV("setPhoneState() state %d", state);
616 // store previous phone state for management of sonification strategy below
Sharad Sangle4509cef2015-08-19 20:47:12 +0530617 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Sharad Sangle36781612015-05-28 16:15:16 +0530618 int oldState = mEngine->getPhoneState();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700619
Sharad Sangle36781612015-05-28 16:15:16 +0530620 if (mEngine->setPhoneState(state) != NO_ERROR) {
621 ALOGW("setPhoneState() invalid or same state %d", state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700622 return;
623 }
Sharad Sangle36781612015-05-28 16:15:16 +0530624 /// Opens: can these line be executed after the switch of volume curves???
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700625 // if leaving call state, handle special case of active streams
626 // pertaining to sonification strategy see handleIncallSonification()
627 if (isInCall()) {
628 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530629 for (size_t j = 0; j < mOutputs.size(); j++) {
630 audio_io_handle_t curOutput = mOutputs.keyAt(j);
631 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
632 if (stream == AUDIO_STREAM_PATCH) {
633 continue;
634 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530635 handleIncallSonification((audio_stream_type_t)stream, false, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +0530636 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700637 }
Sharad Sangle36781612015-05-28 16:15:16 +0530638
639 // force reevaluating accessibility routing when call starts
640 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700641 }
642
Sharad Sangle36781612015-05-28 16:15:16 +0530643 /**
644 * Switching to or from incall state or switching between telephony and VoIP lead to force
645 * routing command.
646 */
647 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
648 || (is_state_in_call(state) && (state != oldState)));
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700649
650 // check for device and output changes triggered by new phone state
651 checkA2dpSuspend();
652 checkOutputForAllStrategies();
653 updateDevicesAndOutputs();
654
Sharad Sangle36781612015-05-28 16:15:16 +0530655 sp<SwAudioOutputDescriptor> hwOutputDesc = mPrimaryOutput;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530656#ifdef VOICE_CONCURRENCY
657 int voice_call_state = 0;
658 char propValue[PROPERTY_VALUE_MAX];
659 bool prop_playback_enabled = false, prop_rec_enabled=false, prop_voip_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700660
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530661 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
662 prop_playback_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
663 }
664
665 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
666 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
667 }
668
669 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
670 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
671 }
672
673 bool mode_in_call = (AUDIO_MODE_IN_CALL != oldState) && (AUDIO_MODE_IN_CALL == state);
674 //query if it is a actual voice call initiated by telephony
675 if (mode_in_call) {
676 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0, String8("in_call"));
677 AudioParameter result = AudioParameter(valueStr);
678 if (result.getInt(String8("in_call"), voice_call_state) == NO_ERROR)
679 ALOGD("voice_conc:SetPhoneState: Voice call state = %d", voice_call_state);
680 }
681
682 if (mode_in_call && voice_call_state && !mvoice_call_state) {
683 ALOGD("voice_conc:Entering to call mode oldState :: %d state::%d ",
684 oldState, state);
685 mvoice_call_state = voice_call_state;
686 if (prop_rec_enabled) {
687 //Close all active inputs
688 audio_io_handle_t activeInput = mInputs.getActiveInput();
689 if (activeInput != 0) {
690 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
691 switch(activeDesc->mInputSource) {
692 case AUDIO_SOURCE_VOICE_UPLINK:
693 case AUDIO_SOURCE_VOICE_DOWNLINK:
694 case AUDIO_SOURCE_VOICE_CALL:
695 ALOGD("voice_conc:FOUND active input during call active: %d",activeDesc->mInputSource);
696 break;
697
698 case AUDIO_SOURCE_VOICE_COMMUNICATION:
699 if(prop_voip_enabled) {
700 ALOGD("voice_conc:CLOSING VoIP input source on call setup :%d ",activeDesc->mInputSource);
701 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
702 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
703 }
704 break;
705
706 default:
707 ALOGD("voice_conc:CLOSING input on call setup for inputSource: %d",activeDesc->mInputSource);
708 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
709 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
710 break;
711 }
712 }
713 } else if (prop_voip_enabled) {
714 audio_io_handle_t activeInput = mInputs.getActiveInput();
715 if (activeInput != 0) {
716 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
717 if (AUDIO_SOURCE_VOICE_COMMUNICATION == activeDesc->mInputSource) {
718 ALOGD("voice_conc:CLOSING VoIP on call setup : %d",activeDesc->mInputSource);
719 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
720 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
721 }
722 }
723 }
724 if (prop_playback_enabled) {
725 // Move tracks associated to this strategy from previous output to new output
726 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
727 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
728 if (i == AUDIO_STREAM_PATCH) {
729 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
730 continue;
731 }
732 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
733 if ((AUDIO_STREAM_MUSIC == i) ||
734 (AUDIO_STREAM_VOICE_CALL == i) ) {
735 ALOGD("voice_conc:Invalidate stream type %d", i);
736 mpClientInterface->invalidateStream((audio_stream_type_t)i);
737 }
738 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
739 ALOGD("voice_conc:Invalidate stream type %d", i);
740 mpClientInterface->invalidateStream((audio_stream_type_t)i);
741 }
742 }
743 }
744
745 for (size_t i = 0; i < mOutputs.size(); i++) {
746 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
747 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
748 ALOGD("voice_conc:ouput desc / profile is NULL");
749 continue;
750 }
751
752 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
753 if (((!outputDesc->isDuplicated() &&outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY))
754 && prop_playback_enabled) {
755 ALOGD("voice_conc:calling suspendOutput on call mode for primary output");
756 mpClientInterface->suspendOutput(mOutputs.keyAt(i));
757 } //Close compress all sessions
758 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
759 && prop_playback_enabled) {
760 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
761 closeOutput(mOutputs.keyAt(i));
762 }
763 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_VOIP_RX)
764 && prop_voip_enabled) {
765 ALOGD("voice_conc:calling closeOutput on call mode for DIRECT output");
766 closeOutput(mOutputs.keyAt(i));
767 }
768 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
769 if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)
770 && prop_playback_enabled) {
771 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
772 closeOutput(mOutputs.keyAt(i));
773 }
774 }
775 }
776 }
777
778 if ((AUDIO_MODE_IN_CALL == oldState || AUDIO_MODE_IN_COMMUNICATION == oldState) &&
779 (AUDIO_MODE_NORMAL == state) && prop_playback_enabled && mvoice_call_state) {
780 ALOGD("voice_conc:EXITING from call mode oldState :: %d state::%d \n",oldState, state);
781 mvoice_call_state = 0;
782 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
783 //restore PCM (deep-buffer) output after call termination
784 for (size_t i = 0; i < mOutputs.size(); i++) {
785 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
786 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
787 ALOGD("voice_conc:ouput desc / profile is NULL");
788 continue;
789 }
790 if (!outputDesc->isDuplicated() && outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
791 ALOGD("voice_conc:calling restoreOutput after call mode for primary output");
792 mpClientInterface->restoreOutput(mOutputs.keyAt(i));
793 }
794 }
795 }
796 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
797 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
798 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
799 if (i == AUDIO_STREAM_PATCH) {
800 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
801 continue;
802 }
803 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
804 if ((AUDIO_STREAM_MUSIC == i) ||
805 (AUDIO_STREAM_VOICE_CALL == i) ) {
806 mpClientInterface->invalidateStream((audio_stream_type_t)i);
807 }
808 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
809 mpClientInterface->invalidateStream((audio_stream_type_t)i);
810 }
811 }
812 }
813
814#endif
815#ifdef RECORD_PLAY_CONCURRENCY
816 char recConcPropValue[PROPERTY_VALUE_MAX];
817 bool prop_rec_play_enabled = false;
818
819 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
820 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
821 }
822 if (prop_rec_play_enabled) {
823 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
824 ALOGD("phone state changed to MODE_IN_COMM invlaidating music and voice streams");
825 // call invalidate for voice streams, so that it can use deepbuffer with VoIP out device from HAL
826 mpClientInterface->invalidateStream(AUDIO_STREAM_VOICE_CALL);
827 // call invalidate for music, so that compress will fallback to deep-buffer with VoIP out device
828 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
829
830 // close compress output to make sure session will be closed before timeout(60sec)
831 for (size_t i = 0; i < mOutputs.size(); i++) {
832
833 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
834 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
835 ALOGD("ouput desc / profile is NULL");
836 continue;
837 }
838
839 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
840 ALOGD("calling closeOutput on call mode for COMPRESS output");
841 closeOutput(mOutputs.keyAt(i));
842 }
843 }
844 } else if ((oldState == AUDIO_MODE_IN_COMMUNICATION) &&
845 (mEngine->getPhoneState() == AUDIO_MODE_NORMAL)) {
846 // call invalidate for music so that music can fallback to compress
847 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
848 }
849 }
850#endif
851 mPrevPhoneState = oldState;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700852 int delayMs = 0;
853 if (isStateInCall(state)) {
854 nsecs_t sysTime = systemTime();
855 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530856 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700857 // mute media and sonification strategies and delay device switch by the largest
858 // latency of any output where either strategy is active.
859 // This avoid sending the ring tone or music tail into the earpiece or headset.
Sharad Sangle36781612015-05-28 16:15:16 +0530860 if ((isStrategyActive(desc, STRATEGY_MEDIA,
861 SONIFICATION_HEADSET_MUSIC_DELAY,
862 sysTime) ||
863 isStrategyActive(desc, STRATEGY_SONIFICATION,
864 SONIFICATION_HEADSET_MUSIC_DELAY,
865 sysTime)) &&
866 (delayMs < (int)desc->latency()*2)) {
867 delayMs = desc->latency()*2;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700868 }
Sharad Sangle36781612015-05-28 16:15:16 +0530869 setStrategyMute(STRATEGY_MEDIA, true, desc);
870 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700871 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Sharad Sangle36781612015-05-28 16:15:16 +0530872 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
873 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700874 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
875 }
876 }
877
Sharad Sangle36781612015-05-28 16:15:16 +0530878 if (hasPrimaryOutput()) {
879 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
880 // the device returned is not necessarily reachable via this output
881 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
882 // force routing command to audio hardware when ending call
883 // even if no device change is needed
884 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
885 rxDevice = mPrimaryOutput->device();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700886 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700887
Sharad Sangle36781612015-05-28 16:15:16 +0530888 if (state == AUDIO_MODE_IN_CALL) {
889 updateCallRouting(rxDevice, delayMs);
890 } else if (oldState == AUDIO_MODE_IN_CALL) {
891 if (mCallRxPatch != 0) {
892 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
893 mCallRxPatch.clear();
894 }
895 if (mCallTxPatch != 0) {
896 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
897 mCallTxPatch.clear();
898 }
899 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
900 } else {
901 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700902 }
903 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530904 //update device for all non-primary outputs
905 for (size_t i = 0; i < mOutputs.size(); i++) {
906 audio_io_handle_t output = mOutputs.keyAt(i);
907 if (output != mPrimaryOutput->mIoHandle) {
908 newDevice = getNewOutputDevice(mOutputs.valueFor(output), false /*fromCache*/);
909 setOutputDevice(mOutputs.valueFor(output), newDevice, (newDevice != AUDIO_DEVICE_NONE));
910 }
911 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700912 // if entering in call state, handle special case of active streams
913 // pertaining to sonification strategy see handleIncallSonification()
914 if (isStateInCall(state)) {
915 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530916 for (size_t j = 0; j < mOutputs.size(); j++) {
917 audio_io_handle_t curOutput = mOutputs.keyAt(j);
918 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
919 if (stream == AUDIO_STREAM_PATCH) {
920 continue;
921 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530922 handleIncallSonification((audio_stream_type_t)stream, true, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +0530923 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700924 }
925 }
926
927 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
928 if (state == AUDIO_MODE_RINGTONE &&
929 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
930 mLimitRingtoneVolume = true;
931 } else {
932 mLimitRingtoneVolume = false;
933 }
934}
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +0530935
936void AudioPolicyManagerCustom::setForceUse(audio_policy_force_use_t usage,
937 audio_policy_forced_cfg_t config)
938{
939 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
940
941 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
942 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
943 return;
944 }
945 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
946 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
947 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
948
949 // check for device and output changes triggered by new force usage
950 checkA2dpSuspend();
951 checkOutputForAllStrategies();
952 updateDevicesAndOutputs();
953 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
954 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
955 updateCallRouting(newDevice);
956 }
957 // Use reverse loop to make sure any low latency usecases (generally tones)
958 // are not routed before non LL usecases (generally music).
959 // We can safely assume that LL output would always have lower index,
960 // and use this work-around to avoid routing of output with music stream
961 // from the context of short lived LL output.
962 // Note: in case output's share backend(HAL sharing is implicit) all outputs
963 // gets routing update while processing first output itself.
964 for (size_t i = mOutputs.size(); i > 0; i--) {
965 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i-1);
966 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
967 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || outputDesc != mPrimaryOutput) {
968 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
969 }
970 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
971 applyStreamVolumes(outputDesc, newDevice, 0, true);
972 }
973 }
974
975 audio_io_handle_t activeInput = mInputs.getActiveInput();
976 if (activeInput != 0) {
977 setInputDevice(activeInput, getNewInputDevice(activeInput));
978 }
979
980}
981
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +0530982status_t AudioPolicyManagerCustom::stopSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +0530983 audio_stream_type_t stream,
984 bool forceDeviceUpdate)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700985{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +0530986 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
987 ALOGW("stopSource() invalid stream %d", stream);
988 return INVALID_OPERATION;
989 }
Sharad Sangle36781612015-05-28 16:15:16 +0530990 // always handle stream stop, check which stream type is stopping
991 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700992
Sharad Sangle36781612015-05-28 16:15:16 +0530993 // handle special case for sonification while in call
Sharad Sangle4509cef2015-08-19 20:47:12 +0530994 if (isInCall() && (outputDesc->mRefCount[stream] == 1)) {
Sharad Sangle36781612015-05-28 16:15:16 +0530995 if (outputDesc->isDuplicated()) {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +0530996 handleIncallSonification(stream, false, false, outputDesc->subOutput1()->mIoHandle);
997 handleIncallSonification(stream, false, false, outputDesc->subOutput2()->mIoHandle);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700998 }
Sharad Sangle36781612015-05-28 16:15:16 +0530999 handleIncallSonification(stream, false, false, outputDesc->mIoHandle);
1000 }
1001
1002 if (outputDesc->mRefCount[stream] > 0) {
1003 // decrement usage count of this stream on the output
1004 outputDesc->changeRefCount(stream, -1);
1005
1006 // store time at which the stream was stopped - see isStreamActive()
1007 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
1008 outputDesc->mStopTime[stream] = systemTime();
Zhou Song5dcddc92015-09-21 14:36:57 +08001009 audio_devices_t prevDevice = outputDesc->device();
Sharad Sangle36781612015-05-28 16:15:16 +05301010 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
1011 // delay the device switch by twice the latency because stopOutput() is executed when
1012 // the track stop() command is received and at that time the audio track buffer can
1013 // still contain data that needs to be drained. The latency only covers the audio HAL
1014 // and kernel buffers. Also the latency does not always include additional delay in the
1015 // audio path (audio DSP, CODEC ...)
1016 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
1017
1018 // force restoring the device selection on other active outputs if it differs from the
1019 // one being selected for this output
1020 for (size_t i = 0; i < mOutputs.size(); i++) {
1021 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1022 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1023 if (desc != outputDesc &&
1024 desc->isActive() &&
1025 outputDesc->sharesHwModuleWith(desc) &&
1026 (newDevice != desc->device())) {
Sharad Sangle4509cef2015-08-19 20:47:12 +05301027 audio_devices_t dev = getNewOutputDevice(mOutputs.valueFor(curOutput), false /*fromCache*/);
Zhou Song5dcddc92015-09-21 14:36:57 +08001028 uint32_t delayMs;
1029 if (dev == prevDevice) {
1030 delayMs = 0;
1031 } else {
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301032 delayMs = outputDesc->latency()*2;
Zhou Song5dcddc92015-09-21 14:36:57 +08001033 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301034 setOutputDevice(desc,
1035 dev,
Sharad Sangle36781612015-05-28 16:15:16 +05301036 true,
Zhou Song5dcddc92015-09-21 14:36:57 +08001037 delayMs);
Sharad Sangle36781612015-05-28 16:15:16 +05301038 }
1039 }
1040 // update the outputs if stopping one with a stream that can affect notification routing
1041 handleNotificationRoutingForStream(stream);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001042 }
Sharad Sangle36781612015-05-28 16:15:16 +05301043 return NO_ERROR;
1044 } else {
1045 ALOGW("stopOutput() refcount is already 0");
1046 return INVALID_OPERATION;
1047 }
1048}
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301049status_t AudioPolicyManagerCustom::startSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301050 audio_stream_type_t stream,
1051 audio_devices_t device,
1052 uint32_t *delayMs)
1053{
1054 // cannot start playback of STREAM_TTS if any other output is being used
1055 uint32_t beaconMuteLatency = 0;
1056
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301057 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1058 ALOGW("startSource() invalid stream %d", stream);
1059 return INVALID_OPERATION;
1060 }
1061
Sharad Sangle36781612015-05-28 16:15:16 +05301062 *delayMs = 0;
1063 if (stream == AUDIO_STREAM_TTS) {
1064 ALOGV("\t found BEACON stream");
1065 if (mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
1066 return INVALID_OPERATION;
1067 } else {
1068 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001069 }
Sharad Sangle36781612015-05-28 16:15:16 +05301070 } else {
1071 // some playback other than beacon starts
1072 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1073 }
1074
1075 // increment usage count for this stream on the requested output:
1076 // NOTE that the usage count is the same for duplicated output and hardware output which is
1077 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1078 outputDesc->changeRefCount(stream, 1);
1079
1080 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
1081 // starting an output being rerouted?
1082 if (device == AUDIO_DEVICE_NONE) {
1083 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001084 }
Sharad Sangle36781612015-05-28 16:15:16 +05301085 routing_strategy strategy = getStrategy(stream);
1086 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
1087 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1088 (beaconMuteLatency > 0);
1089 uint32_t waitMs = beaconMuteLatency;
1090 bool force = false;
1091 for (size_t i = 0; i < mOutputs.size(); i++) {
1092 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1093 if (desc != outputDesc) {
1094 // force a device change if any other output is managed by the same hw
1095 // module and has a current device selection that differs from selected device.
1096 // In this case, the audio HAL must receive the new device selection so that it can
1097 // change the device currently selected by the other active output.
1098 if (outputDesc->sharesHwModuleWith(desc) &&
1099 desc->device() != device) {
1100 force = true;
1101 }
1102 // wait for audio on other active outputs to be presented when starting
1103 // a notification so that audio focus effect can propagate, or that a mute/unmute
1104 // event occurred for beacon
1105 uint32_t latency = desc->latency();
1106 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1107 waitMs = latency;
1108 }
1109 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001110 }
Sharad Sangle36781612015-05-28 16:15:16 +05301111 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force);
1112
1113 // handle special case for sonification while in call
1114 if (isInCall()) {
1115 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001116 }
Sharad Sangle36781612015-05-28 16:15:16 +05301117
1118 // apply volume rules for current stream and device if necessary
1119 checkAndSetVolume(stream,
1120 mStreams.valueFor(stream).getVolumeIndex(device),
1121 outputDesc,
1122 device);
1123
1124 // update the outputs if starting an output with a stream that can affect notification
1125 // routing
1126 handleNotificationRoutingForStream(stream);
1127
1128 // force reevaluating accessibility routing when ringtone or alarm starts
1129 if (strategy == STRATEGY_SONIFICATION) {
1130 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1131 }
1132 }
1133 else {
1134 // handle special case for sonification while in call
1135 if (isInCall()) {
1136 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
1137 }
1138 }
1139 return NO_ERROR;
1140}
1141void AudioPolicyManagerCustom::handleIncallSonification(audio_stream_type_t stream,
1142 bool starting, bool stateChange,
1143 audio_io_handle_t output)
1144{
1145 if(!hasPrimaryOutput()) {
1146 return;
1147 }
1148 // no action needed for AUDIO_STREAM_PATCH stream type, it's for internal flinger tracks
1149 if (stream == AUDIO_STREAM_PATCH) {
1150 return;
1151 }
1152 // if the stream pertains to sonification strategy and we are in call we must
1153 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
1154 // in the device used for phone strategy and play the tone if the selected device does not
1155 // interfere with the device used for phone strategy
1156 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
1157 // many times as there are active tracks on the output
1158 const routing_strategy stream_strategy = getStrategy(stream);
1159 if ((stream_strategy == STRATEGY_SONIFICATION) ||
1160 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
1161 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
1162 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
1163 stream, starting, outputDesc->mDevice, stateChange);
1164 if (outputDesc->mRefCount[stream]) {
1165 int muteCount = 1;
1166 if (stateChange) {
1167 muteCount = outputDesc->mRefCount[stream];
1168 }
1169 if (audio_is_low_visibility(stream)) {
1170 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
1171 for (int i = 0; i < muteCount; i++) {
1172 setStreamMute(stream, starting, outputDesc);
1173 }
1174 } else {
1175 ALOGV("handleIncallSonification() high visibility");
1176 if (outputDesc->device() &
1177 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
1178 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
1179 for (int i = 0; i < muteCount; i++) {
1180 setStreamMute(stream, starting, outputDesc);
1181 }
1182 }
1183 if (starting) {
1184 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
1185 AUDIO_STREAM_VOICE_CALL);
1186 } else {
1187 mpClientInterface->stopTone();
1188 }
1189 }
1190 }
1191 }
1192}
1193void AudioPolicyManagerCustom::handleNotificationRoutingForStream(audio_stream_type_t stream) {
1194 switch(stream) {
1195 case AUDIO_STREAM_MUSIC:
1196 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
1197 updateDevicesAndOutputs();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001198 break;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001199 default:
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001200 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001201 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001202}
Sharad Sangle36781612015-05-28 16:15:16 +05301203status_t AudioPolicyManagerCustom::checkAndSetVolume(audio_stream_type_t stream,
1204 int index,
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301205 const sp<AudioOutputDescriptor>& outputDesc,
Sharad Sangle36781612015-05-28 16:15:16 +05301206 audio_devices_t device,
1207 int delayMs, bool force)
1208{
Dhananjay Kumar0ffa7112015-10-20 17:56:50 +05301209 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1210 ALOGW("checkAndSetVolume() invalid stream %d", stream);
1211 return INVALID_OPERATION;
1212 }
Sharad Sangle36781612015-05-28 16:15:16 +05301213 // do not change actual stream volume if the stream is muted
1214 if (outputDesc->mMuteCount[stream] != 0) {
1215 ALOGVV("checkAndSetVolume() stream %d muted count %d",
1216 stream, outputDesc->mMuteCount[stream]);
1217 return NO_ERROR;
1218 }
1219 audio_policy_forced_cfg_t forceUseForComm =
1220 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
1221 // do not change in call volume if bluetooth is connected and vice versa
1222 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
1223 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
1224 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
1225 stream, forceUseForComm);
1226 return INVALID_OPERATION;
1227 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001228
Sharad Sangle36781612015-05-28 16:15:16 +05301229 if (device == AUDIO_DEVICE_NONE) {
1230 device = outputDesc->device();
1231 }
1232
1233 float volumeDb = computeVolume(stream, index, device);
1234 if (outputDesc->isFixedVolume(device)) {
1235 volumeDb = 0.0f;
1236 }
1237
1238 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
1239
1240 if (stream == AUDIO_STREAM_VOICE_CALL ||
1241 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
1242 float voiceVolume;
1243 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
1244 if (stream == AUDIO_STREAM_VOICE_CALL) {
1245 voiceVolume = (float)index/(float)mStreams.valueFor(stream).getVolumeIndexMax();
1246 } else {
1247 voiceVolume = 1.0;
1248 }
1249
1250 if (voiceVolume != mLastVoiceVolume && ((outputDesc == mPrimaryOutput) ||
1251 isDirectOutput(outputDesc->mIoHandle) || device & AUDIO_DEVICE_OUT_ALL_USB)) {
1252 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
1253 mLastVoiceVolume = voiceVolume;
1254 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +05301255#ifdef FM_POWER_OPT
1256 } else if (stream == AUDIO_STREAM_MUSIC && hasPrimaryOutput() &&
1257 outputDesc == mPrimaryOutput) {
1258 AudioParameter param = AudioParameter();
1259 param.addFloat(String8("fm_volume"), Volume::DbToAmpl(volumeDb));
1260 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString(), delayMs);
1261#endif /* FM_POWER_OPT end */
Sharad Sangle36781612015-05-28 16:15:16 +05301262 }
1263
1264 return NO_ERROR;
1265}
1266bool AudioPolicyManagerCustom::isDirectOutput(audio_io_handle_t output) {
1267 for (size_t i = 0; i < mOutputs.size(); i++) {
1268 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1269 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1270 if ((curOutput == output) && (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
1271 return true;
1272 }
1273 }
1274 return false;
1275}
vivek mehta0ea887a2015-08-26 14:01:20 -07001276
1277status_t AudioPolicyManagerCustom::getOutputForAttr(const audio_attributes_t *attr,
1278 audio_io_handle_t *output,
1279 audio_session_t session,
1280 audio_stream_type_t *stream,
1281 uid_t uid,
1282 uint32_t samplingRate,
1283 audio_format_t format,
1284 audio_channel_mask_t channelMask,
1285 audio_output_flags_t flags,
1286 audio_port_handle_t selectedDeviceId,
1287 const audio_offload_info_t *offloadInfo)
1288{
1289 audio_offload_info_t tOffloadInfo = AUDIO_INFO_INITIALIZER;
1290
Alexy Joseph0ac09032015-10-16 15:57:53 -07001291 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
1292 bool pcmOffloadEnabled = false;
1293
1294 if (offloadDisabled) {
1295 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
1296 }
1297
1298 //read track offload property only if the global offload switch is off.
1299 if (!offloadDisabled) {
1300 pcmOffloadEnabled = property_get_bool("audio.offload.track.enable", false);
1301 }
vivek mehta0ea887a2015-08-26 14:01:20 -07001302
1303 if (offloadInfo == NULL && pcmOffloadEnabled) {
1304 tOffloadInfo.sample_rate = samplingRate;
1305 tOffloadInfo.channel_mask = channelMask;
1306 tOffloadInfo.format = format;
1307 tOffloadInfo.stream_type = *stream;
1308 tOffloadInfo.bit_width = 16; //hard coded for PCM_16
1309 if (attr != NULL) {
1310 ALOGV("found attribute .. setting usage %d ", attr->usage);
1311 tOffloadInfo.usage = attr->usage;
1312 } else {
Alexy Joseph0ac09032015-10-16 15:57:53 -07001313 ALOGI("%s:: attribute is NULL .. no usage set", __func__);
vivek mehta0ea887a2015-08-26 14:01:20 -07001314 }
1315 offloadInfo = &tOffloadInfo;
1316 }
1317
1318 return AudioPolicyManager::getOutputForAttr(attr, output, session, stream,
1319 (uid_t)uid, (uint32_t)samplingRate,
1320 format, (audio_channel_mask_t)channelMask,
1321 flags, (audio_port_handle_t)selectedDeviceId,
1322 offloadInfo);
1323}
1324
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001325audio_io_handle_t AudioPolicyManagerCustom::getOutputForDevice(
1326 audio_devices_t device,
Sharad Sangle36781612015-05-28 16:15:16 +05301327 audio_session_t session __unused,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001328 audio_stream_type_t stream,
1329 uint32_t samplingRate,
1330 audio_format_t format,
1331 audio_channel_mask_t channelMask,
1332 audio_output_flags_t flags,
1333 const audio_offload_info_t *offloadInfo)
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001334{
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001335 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
1336 uint32_t latency = 0;
1337 status_t status;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001338
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001339#ifdef AUDIO_POLICY_TEST
1340 if (mCurOutput != 0) {
1341 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1342 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1343
1344 if (mTestOutputs[mCurOutput] == 0) {
1345 ALOGV("getOutput() opening test output");
Sharad Sangle36781612015-05-28 16:15:16 +05301346 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
1347 mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001348 outputDesc->mDevice = mTestDevice;
1349 outputDesc->mLatency = mTestLatencyMs;
1350 outputDesc->mFlags =
1351 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
1352 outputDesc->mRefCount[stream] = 0;
1353 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1354 config.sample_rate = mTestSamplingRate;
1355 config.channel_mask = mTestChannels;
1356 config.format = mTestFormat;
1357 if (offloadInfo != NULL) {
1358 config.offload_info = *offloadInfo;
1359 }
1360 status = mpClientInterface->openOutput(0,
1361 &mTestOutputs[mCurOutput],
1362 &config,
1363 &outputDesc->mDevice,
1364 String8(""),
1365 &outputDesc->mLatency,
1366 outputDesc->mFlags);
1367 if (status == NO_ERROR) {
1368 outputDesc->mSamplingRate = config.sample_rate;
1369 outputDesc->mFormat = config.format;
1370 outputDesc->mChannelMask = config.channel_mask;
1371 AudioParameter outputCmd = AudioParameter();
1372 outputCmd.addInt(String8("set_id"),mCurOutput);
1373 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1374 addOutput(mTestOutputs[mCurOutput], outputDesc);
1375 }
1376 }
1377 return mTestOutputs[mCurOutput];
1378 }
1379#endif //AUDIO_POLICY_TEST
Sharad Sangle36781612015-05-28 16:15:16 +05301380 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1381 (stream != AUDIO_STREAM_MUSIC)) {
1382 // compress should not be used for non-music streams
1383 ALOGE("Offloading only allowed with music stream");
1384 return 0;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301385 }
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301386
1387 if ((stream == AUDIO_STREAM_VOICE_CALL) &&
1388 (channelMask == 1) &&
1389 (samplingRate == 8000 || samplingRate == 16000)) {
1390 // Allow Voip direct output only if:
1391 // audio mode is MODE_IN_COMMUNCATION; AND
1392 // voip output is not opened already; AND
1393 // requested sample rate matches with that of voip input stream (if opened already)
1394 int value = 0;
1395 uint32_t mode = 0, voipOutCount = 1, voipSampleRate = 1;
1396 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1397 String8("audio_mode"));
1398 AudioParameter result = AudioParameter(valueStr);
1399 if (result.getInt(String8("audio_mode"), value) == NO_ERROR) {
1400 mode = value;
1401 }
1402
1403 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1404 String8("voip_out_stream_count"));
1405 result = AudioParameter(valueStr);
1406 if (result.getInt(String8("voip_out_stream_count"), value) == NO_ERROR) {
1407 voipOutCount = value;
1408 }
1409
1410 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1411 String8("voip_sample_rate"));
1412 result = AudioParameter(valueStr);
1413 if (result.getInt(String8("voip_sample_rate"), value) == NO_ERROR) {
1414 voipSampleRate = value;
1415 }
1416
1417 if ((mode == AUDIO_MODE_IN_COMMUNICATION) && (voipOutCount == 0) &&
1418 ((voipSampleRate == 0) || (voipSampleRate == samplingRate))) {
1419 if (audio_is_linear_pcm(format)) {
1420 char propValue[PROPERTY_VALUE_MAX] = {0};
1421 property_get("use.voice.path.for.pcm.voip", propValue, "0");
1422 bool voipPcmSysPropEnabled = !strncmp("true", propValue, sizeof("true"));
1423 if (voipPcmSysPropEnabled && (format == AUDIO_FORMAT_PCM_16_BIT)) {
1424 flags = (audio_output_flags_t)((flags &~AUDIO_OUTPUT_FLAG_FAST) |
1425 AUDIO_OUTPUT_FLAG_VOIP_RX | AUDIO_OUTPUT_FLAG_DIRECT);
1426 ALOGD("Set VoIP and Direct output flags for PCM format");
1427 }
1428 }
1429 }
1430 }
1431
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301432#ifdef VOICE_CONCURRENCY
1433 char propValue[PROPERTY_VALUE_MAX];
1434 bool prop_play_enabled=false, prop_voip_enabled = false;
1435
1436 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1437 prop_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001438 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301439
1440 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1441 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1442 }
1443
1444 if (prop_play_enabled && mvoice_call_state) {
1445 //check if voice call is active / running in background
1446 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1447 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1448 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1449 {
1450 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1451 if(prop_voip_enabled) {
1452 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1453 flags );
1454 return 0;
1455 }
1456 }
1457 else {
1458 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
1459 ALOGD("voice_conc:IN call mode adding ULL flags .. flags: %x ", flags );
1460 flags = AUDIO_OUTPUT_FLAG_FAST;
1461 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
1462 if (AUDIO_STREAM_MUSIC == stream) {
1463 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1464 ALOGD("voice_conc:IN call mode adding deep-buffer flags %x ", flags );
1465 }
1466 else {
1467 flags = AUDIO_OUTPUT_FLAG_FAST;
1468 ALOGD("voice_conc:IN call mode adding fast flags %x ", flags );
1469 }
1470 }
1471 }
1472 }
1473 } else if (prop_voip_enabled && mvoice_call_state) {
1474 //check if voice call is active / running in background
1475 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1476 //return only ULL ouput
1477 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1478 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1479 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1480 {
1481 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1482 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1483 flags );
1484 return 0;
1485 }
1486 }
1487 }
1488#endif
1489#ifdef RECORD_PLAY_CONCURRENCY
1490 char recConcPropValue[PROPERTY_VALUE_MAX];
1491 bool prop_rec_play_enabled = false;
1492
1493 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
1494 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
1495 }
1496 if ((prop_rec_play_enabled) &&
1497 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
1498 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
1499 if (AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1500 // allow VoIP using voice path
1501 // Do nothing
1502 } else if((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1503 ALOGD("voice_conc:MODE_IN_COMM is setforcing deep buffer output for non ULL... flags: %x", flags);
1504 // use deep buffer path for all non ULL outputs
1505 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1506 }
1507 } else if ((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1508 ALOGD("voice_conc:Record mode is on forcing deep buffer output for non ULL... flags: %x ", flags);
1509 // use deep buffer path for all non ULL outputs
1510 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1511 }
1512 }
1513 if (prop_rec_play_enabled &&
1514 (stream == AUDIO_STREAM_ENFORCED_AUDIBLE)) {
1515 ALOGD("Record conc is on forcing ULL output for ENFORCED_AUDIBLE");
1516 flags = AUDIO_OUTPUT_FLAG_FAST;
1517 }
1518#endif
1519
Sharad Sangle4509cef2015-08-19 20:47:12 +05301520#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Sharad Sangle36781612015-05-28 16:15:16 +05301521 /*
1522 * WFD audio routes back to target speaker when starting a ringtone playback.
1523 * This is because primary output is reused for ringtone, so output device is
1524 * updated based on SONIFICATION strategy for both ringtone and music playback.
1525 * The same issue is not seen on remoted_submix HAL based WFD audio because
1526 * primary output is not reused and a new output is created for ringtone playback.
1527 * Issue is fixed by updating output flag to AUDIO_OUTPUT_FLAG_FAST when there is
1528 * a non-music stream playback on WFD, so primary output is not reused for ringtone.
1529 */
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001530 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
1531 if ((availableOutputDeviceTypes & AUDIO_DEVICE_OUT_PROXY)
1532 && (stream != AUDIO_STREAM_MUSIC)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301533 ALOGD("WFD audio: use OUTPUT_FLAG_FAST for non music stream. flags:%x", flags );
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001534 //For voip paths
1535 if(flags & AUDIO_OUTPUT_FLAG_DIRECT)
1536 flags = AUDIO_OUTPUT_FLAG_DIRECT;
1537 else //route every thing else to ULL path
1538 flags = AUDIO_OUTPUT_FLAG_FAST;
1539 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301540#endif
1541
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001542 // open a direct output if required by specified parameters
vivek mehta0ea887a2015-08-26 14:01:20 -07001543 // force direct flag if offload flag is set: offloading implies a direct output stream
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001544 // and all common behaviors are driven by checking only the direct flag
1545 // this should normally be set appropriately in the policy configuration file
1546 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1547 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1548 }
1549 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1550 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1551 }
vivek mehta0ea887a2015-08-26 14:01:20 -07001552
vivek mehta4b0d8192015-10-16 00:25:59 -07001553 bool forced_deep = false;
Sharad Sangle36781612015-05-28 16:15:16 +05301554 // only allow deep buffering for music stream type
1555 if (stream != AUDIO_STREAM_MUSIC) {
1556 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Sharad Sangle497aef82015-08-03 17:55:48 +05301557 } else if (/* stream == AUDIO_STREAM_MUSIC && */
1558 flags == AUDIO_OUTPUT_FLAG_NONE &&
1559 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
vivek mehta4b0d8192015-10-16 00:25:59 -07001560 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
1561 forced_deep = true;
Sharad Sangle36781612015-05-28 16:15:16 +05301562 }
Sharad Sangle497aef82015-08-03 17:55:48 +05301563
Sharad Sangle36781612015-05-28 16:15:16 +05301564 if (stream == AUDIO_STREAM_TTS) {
1565 flags = AUDIO_OUTPUT_FLAG_TTS;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001566 }
1567
vivek mehta4b0d8192015-10-16 00:25:59 -07001568 // Do offload magic here
1569 if (((flags == AUDIO_OUTPUT_FLAG_NONE) || forced_deep) &&
1570 (stream == AUDIO_STREAM_MUSIC) && (offloadInfo != NULL) &&
1571 ((offloadInfo->usage == AUDIO_USAGE_MEDIA) || (offloadInfo->usage == AUDIO_USAGE_GAME))) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301572 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
vivek mehta4b0d8192015-10-16 00:25:59 -07001573 ALOGD("AudioCustomHAL --> Force Direct Flag .. flag (0x%x)", flags);
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301574 }
1575
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001576 sp<IOProfile> profile;
1577
1578 // skip direct output selection if the request can obviously be attached to a mixed output
1579 // and not explicitly requested
1580 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
1581 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
1582 audio_channel_count_from_out_mask(channelMask) <= 2) {
1583 goto non_direct_output;
1584 }
1585
1586 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1587 // creating an offloaded track and tearing it down immediately after start when audioflinger
1588 // detects there is an active non offloadable effect.
1589 // FIXME: We should check the audio session here but we do not have it in this context.
1590 // This may prevent offloading in rare situations where effects are left active by apps
1591 // in the background.
1592
Sharad Sangle36781612015-05-28 16:15:16 +05301593 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
1594 !mEffects.isNonOffloadableEffectEnabled()) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001595 profile = getProfileForDirectOutput(device,
1596 samplingRate,
1597 format,
1598 channelMask,
1599 (audio_output_flags_t)flags);
1600 }
1601
1602 if (profile != 0) {
Sharad Sangle36781612015-05-28 16:15:16 +05301603 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001604
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001605 // if multiple concurrent offload decode is supported
1606 // do no check for reuse and also don't close previous output if its offload
1607 // previous output will be closed during track destruction
1608 if (!(property_get_bool("audio.offload.multiple.enabled", false) &&
1609 ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0))) {
1610 for (size_t i = 0; i < mOutputs.size(); i++) {
1611 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1612 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1613 outputDesc = desc;
1614 // reuse direct output if currently open and configured with same parameters
1615 if ((samplingRate == outputDesc->mSamplingRate) &&
1616 (format == outputDesc->mFormat) &&
1617 (channelMask == outputDesc->mChannelMask)) {
1618 outputDesc->mDirectOpenCount++;
1619 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1620 return mOutputs.keyAt(i);
1621 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001622 }
1623 }
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001624 // close direct output if currently open and configured with different parameters
1625 if (outputDesc != NULL) {
1626 closeOutput(outputDesc->mIoHandle);
1627 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001628 }
Sharad Sangle36781612015-05-28 16:15:16 +05301629
1630 // if the selected profile is offloaded and no offload info was specified,
1631 // create a default one
1632 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
1633 if ((profile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
1634 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1635 defaultOffloadInfo.sample_rate = samplingRate;
1636 defaultOffloadInfo.channel_mask = channelMask;
1637 defaultOffloadInfo.format = format;
1638 defaultOffloadInfo.stream_type = stream;
1639 defaultOffloadInfo.bit_rate = 0;
1640 defaultOffloadInfo.duration_us = -1;
1641 defaultOffloadInfo.has_video = true; // conservative
1642 defaultOffloadInfo.is_streaming = true; // likely
1643 offloadInfo = &defaultOffloadInfo;
1644 }
1645
1646 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001647 outputDesc->mDevice = device;
1648 outputDesc->mLatency = 0;
Sharad Sangle36781612015-05-28 16:15:16 +05301649 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001650 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1651 config.sample_rate = samplingRate;
1652 config.channel_mask = channelMask;
1653 config.format = format;
1654 if (offloadInfo != NULL) {
1655 config.offload_info = *offloadInfo;
1656 }
Sharad Sangle36781612015-05-28 16:15:16 +05301657 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001658 &output,
1659 &config,
1660 &outputDesc->mDevice,
1661 String8(""),
1662 &outputDesc->mLatency,
1663 outputDesc->mFlags);
1664
1665 // only accept an output with the requested parameters
1666 if (status != NO_ERROR ||
1667 (samplingRate != 0 && samplingRate != config.sample_rate) ||
1668 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
1669 (channelMask != 0 && channelMask != config.channel_mask)) {
1670 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1671 "format %d %d, channelMask %04x %04x", output, samplingRate,
1672 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1673 outputDesc->mChannelMask);
1674 if (output != AUDIO_IO_HANDLE_NONE) {
1675 mpClientInterface->closeOutput(output);
1676 }
Sharad Sangle36781612015-05-28 16:15:16 +05301677 // fall back to mixer output if possible when the direct output could not be open
1678 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
1679 goto non_direct_output;
1680 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001681 return AUDIO_IO_HANDLE_NONE;
1682 }
1683 outputDesc->mSamplingRate = config.sample_rate;
1684 outputDesc->mChannelMask = config.channel_mask;
1685 outputDesc->mFormat = config.format;
1686 outputDesc->mRefCount[stream] = 0;
1687 outputDesc->mStopTime[stream] = 0;
1688 outputDesc->mDirectOpenCount = 1;
1689
1690 audio_io_handle_t srcOutput = getOutputForEffect();
1691 addOutput(output, outputDesc);
1692 audio_io_handle_t dstOutput = getOutputForEffect();
1693 if (dstOutput == output) {
1694 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1695 }
1696 mPreviousOutputs = mOutputs;
1697 ALOGV("getOutput() returns new direct output %d", output);
1698 mpClientInterface->onAudioPortListUpdate();
1699 return output;
1700 }
1701
1702non_direct_output:
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001703 // ignoring channel mask due to downmix capability in mixer
1704
1705 // open a non direct output
1706
1707 // for non direct outputs, only PCM is supported
1708 if (audio_is_linear_pcm(format)) {
1709 // get which output is suitable for the specified stream. The actual
1710 // routing change will happen when startOutput() will be called
1711 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1712
1713 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1714 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1715 output = selectOutput(outputs, flags, format);
1716 }
1717 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1718 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1719
vivek mehta0ea887a2015-08-26 14:01:20 -07001720 ALOGV("getOutputForDevice() returns output %d", output);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001721
1722 return output;
1723}
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301724
1725status_t AudioPolicyManagerCustom::getInputForAttr(const audio_attributes_t *attr,
1726 audio_io_handle_t *input,
1727 audio_session_t session,
1728 uid_t uid,
1729 uint32_t samplingRate,
1730 audio_format_t format,
1731 audio_channel_mask_t channelMask,
1732 audio_input_flags_t flags,
1733 audio_port_handle_t selectedDeviceId,
1734 input_type_t *inputType)
1735{
1736 audio_source_t inputSource = attr->source;
1737#ifdef VOICE_CONCURRENCY
1738
1739 char propValue[PROPERTY_VALUE_MAX];
1740 bool prop_rec_enabled=false, prop_voip_enabled = false;
1741
1742 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
1743 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1744 }
1745
1746 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1747 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1748 }
1749
1750 if (prop_rec_enabled && mvoice_call_state) {
1751 //check if voice call is active / running in background
1752 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1753 //Need to block input request
1754 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1755 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1756 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1757 {
1758 switch(inputSource) {
1759 case AUDIO_SOURCE_VOICE_UPLINK:
1760 case AUDIO_SOURCE_VOICE_DOWNLINK:
1761 case AUDIO_SOURCE_VOICE_CALL:
1762 ALOGD("voice_conc:Creating input during incall mode for inputSource: %d",
1763 inputSource);
1764 break;
1765
1766 case AUDIO_SOURCE_VOICE_COMMUNICATION:
1767 if(prop_voip_enabled) {
1768 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1769 inputSource);
1770 return NO_INIT;
1771 }
1772 break;
1773 default:
1774 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1775 inputSource);
1776 return NO_INIT;
1777 }
1778 }
1779 }//check for VoIP flag
1780 else if(prop_voip_enabled && mvoice_call_state) {
1781 //check if voice call is active / running in background
1782 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1783 //Need to block input request
1784 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1785 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1786 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1787 {
1788 if(inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1789 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
1790 return NO_INIT;
1791 }
1792 }
1793 }
1794
1795#endif
1796
1797 return AudioPolicyManager::getInputForAttr(attr,
1798 input,
1799 session,
1800 uid,
1801 samplingRate,
1802 format,
1803 channelMask,
1804 flags,
1805 selectedDeviceId,
1806 inputType);
1807}
1808status_t AudioPolicyManagerCustom::startInput(audio_io_handle_t input,
1809 audio_session_t session)
1810{
1811 ALOGV("startInput() input %d", input);
1812 ssize_t index = mInputs.indexOfKey(input);
1813 if (index < 0) {
1814 ALOGW("startInput() unknown input %d", input);
1815 return BAD_VALUE;
1816 }
1817 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1818
1819 index = inputDesc->mSessions.indexOf(session);
1820 if (index < 0) {
1821 ALOGW("startInput() unknown session %d on input %d", session, input);
1822 return BAD_VALUE;
1823 }
1824
1825 // virtual input devices are compatible with other input devices
1826 if (!is_virtual_input_device(inputDesc->mDevice)) {
1827
1828 // for a non-virtual input device, check if there is another (non-virtual) active input
1829 audio_io_handle_t activeInput = mInputs.getActiveInput();
1830 if (activeInput != 0 && activeInput != input) {
1831
1832 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1833 // otherwise the active input continues and the new input cannot be started.
1834 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
1835 if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
1836 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
1837 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
1838 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
1839 } else {
1840 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
1841 return INVALID_OPERATION;
1842 }
1843 }
1844 }
1845
1846 // Routing?
1847 mInputRoutes.incRouteActivity(session);
1848#ifdef RECORD_PLAY_CONCURRENCY
1849 mIsInputRequestOnProgress = true;
1850
1851 char getPropValue[PROPERTY_VALUE_MAX];
1852 bool prop_rec_play_enabled = false;
1853
1854 if (property_get("rec.playback.conc.disabled", getPropValue, NULL)) {
1855 prop_rec_play_enabled = atoi(getPropValue) || !strncmp("true", getPropValue, 4);
1856 }
1857
1858 if ((prop_rec_play_enabled) &&(mInputs.activeInputsCount() == 0)){
1859 // send update to HAL on record playback concurrency
1860 AudioParameter param = AudioParameter();
1861 param.add(String8("rec_play_conc_on"), String8("true"));
1862 ALOGD("startInput() setParameters rec_play_conc is setting to ON ");
1863 mpClientInterface->setParameters(0, param.toString());
1864
1865 // Call invalidate to reset all opened non ULL audio tracks
1866 // Move tracks associated to this strategy from previous output to new output
1867 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
1868 // Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder)
Sharad Sangle4509cef2015-08-19 20:47:12 +05301869 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE && (i != AUDIO_STREAM_PATCH))) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301870 ALOGD("Invalidate on releaseInput for stream :: %d ", i);
1871 //FIXME see fixme on name change
1872 mpClientInterface->invalidateStream((audio_stream_type_t)i);
1873 }
1874 }
1875 // close compress tracks
1876 for (size_t i = 0; i < mOutputs.size(); i++) {
1877 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
1878 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
1879 ALOGD("ouput desc / profile is NULL");
1880 continue;
1881 }
1882 if (outputDesc->mProfile->mFlags
1883 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1884 // close compress sessions
1885 ALOGD("calling closeOutput on record conc for COMPRESS output");
1886 closeOutput(mOutputs.keyAt(i));
1887 }
1888 }
1889 }
1890#endif
1891
1892 if (inputDesc->mRefCount == 0 || mInputRoutes.hasRouteChanged(session)) {
1893 // if input maps to a dynamic policy with an activity listener, notify of state change
1894 if ((inputDesc->mPolicyMix != NULL)
1895 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1896 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1897 MIX_STATE_MIXING);
1898 }
1899
1900 if (mInputs.activeInputsCount() == 0) {
1901 SoundTrigger::setCaptureState(true);
1902 }
1903 setInputDevice(input, getNewInputDevice(input), true /* force */);
1904
1905 // automatically enable the remote submix output when input is started if not
1906 // used by a policy mix of type MIX_TYPE_RECORDERS
1907 // For remote submix (a virtual device), we open only one input per capture request.
1908 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1909 String8 address = String8("");
1910 if (inputDesc->mPolicyMix == NULL) {
1911 address = String8("0");
1912 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1913 address = inputDesc->mPolicyMix->mRegistrationId;
1914 }
1915 if (address != "") {
1916 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1917 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1918 address, "remote-submix");
1919 }
1920 }
1921 }
1922
1923 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1924
1925 inputDesc->mRefCount++;
1926#ifdef RECORD_PLAY_CONCURRENCY
1927 mIsInputRequestOnProgress = false;
1928#endif
1929 return NO_ERROR;
1930}
1931status_t AudioPolicyManagerCustom::stopInput(audio_io_handle_t input,
1932 audio_session_t session)
1933{
1934 status_t status;
1935 status = AudioPolicyManager::stopInput(input, session);
1936#ifdef RECORD_PLAY_CONCURRENCY
1937 char propValue[PROPERTY_VALUE_MAX];
1938 bool prop_rec_play_enabled = false;
1939
1940 if (property_get("rec.playback.conc.disabled", propValue, NULL)) {
1941 prop_rec_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1942 }
1943
1944 if ((prop_rec_play_enabled) && (mInputs.activeInputsCount() == 0)) {
1945
1946 //send update to HAL on record playback concurrency
1947 AudioParameter param = AudioParameter();
1948 param.add(String8("rec_play_conc_on"), String8("false"));
1949 ALOGD("stopInput() setParameters rec_play_conc is setting to OFF ");
1950 mpClientInterface->setParameters(0, param.toString());
1951
1952 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
1953 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
1954 //Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder stop tone)
1955 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE) && (i != AUDIO_STREAM_PATCH)) {
1956 ALOGD(" Invalidate on stopInput for stream :: %d ", i);
1957 //FIXME see fixme on name change
1958 mpClientInterface->invalidateStream((audio_stream_type_t)i);
1959 }
1960 }
1961 }
1962#endif
1963 return status;
1964}
1965
1966AudioPolicyManagerCustom::AudioPolicyManagerCustom(AudioPolicyClientInterface *clientInterface)
Sharad Sangle4509cef2015-08-19 20:47:12 +05301967 : AudioPolicyManager(clientInterface),
1968 mHdmiAudioDisabled(false),
1969 mHdmiAudioEvent(false),
1970 mPrevPhoneState(0)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301971{
Mingming Yin38ea08c2015-10-05 15:24:04 -07001972 char ssr_enabled[PROPERTY_VALUE_MAX] = {0};
1973 bool prop_ssr_enabled = false;
1974
1975 if (property_get("ro.qc.sdk.audio.ssr", ssr_enabled, NULL)) {
1976 prop_ssr_enabled = atoi(ssr_enabled) || !strncmp("true", ssr_enabled, 4);
1977 }
1978
1979 for (size_t i = 0; i < mHwModules.size(); i++) {
1980 ALOGV("Hw module %d", i);
1981 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) {
1982 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
1983 ALOGV("Input profile ", j);
1984 for (size_t k = 0; k < inProfile->mChannelMasks.size(); k++) {
1985 audio_channel_mask_t channelMask =
1986 inProfile->mChannelMasks.itemAt(k);
1987 ALOGV("Channel Mask %x size %d", channelMask,
1988 inProfile->mChannelMasks.size());
1989 if (AUDIO_CHANNEL_IN_5POINT1 == channelMask) {
1990 if (!prop_ssr_enabled) {
1991 ALOGI("removing AUDIO_CHANNEL_IN_5POINT1 from"
1992 " input profile as SSR(surround sound record)"
1993 " is not supported on this chipset variant");
1994 inProfile->mChannelMasks.removeItemsAt(k, 1);
1995 ALOGV("Channel Mask size now %d",
1996 inProfile->mChannelMasks.size());
1997 }
1998 }
1999 }
2000 }
2001 }
2002
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302003#ifdef RECORD_PLAY_CONCURRENCY
2004 mIsInputRequestOnProgress = false;
2005#endif
2006
2007
2008#ifdef VOICE_CONCURRENCY
2009 mFallBackflag = getFallBackPath();
2010#endif
2011}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07002012}