blob: 1a6c758a8ec2cc2fabb021a1c07404fdb96d252f [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#define MIN(a, b) ((a) < (b) ? (a) : (b))
30
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070031// A device mask for all audio output devices that are considered "remote" when evaluating
32// active output devices in isStreamActiveRemotely()
33#define APM_AUDIO_OUT_DEVICE_REMOTE_ALL AUDIO_DEVICE_OUT_REMOTE_SUBMIX
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070034// A device mask for all audio input and output devices where matching inputs/outputs on device
35// type alone is not enough: the address must match too
36#define APM_AUDIO_DEVICE_MATCH_ADDRESS_ALL (AUDIO_DEVICE_IN_REMOTE_SUBMIX | \
37 AUDIO_DEVICE_OUT_REMOTE_SUBMIX)
Sharad Sangle36781612015-05-28 16:15:16 +053038// Following delay should be used if the calculated routing delay from all active
39// input streams is higher than this value
40#define MAX_VOICE_CALL_START_DELAY_MS 100
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070041
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070042#include <inttypes.h>
Mingming Yin0ae14ea2014-07-09 17:55:56 -070043#include <math.h>
Mingming Yin0670f162014-06-12 16:05:49 -070044
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070045#include <cutils/properties.h>
46#include <utils/Log.h>
47#include <hardware/audio.h>
48#include <hardware/audio_effect.h>
49#include <media/AudioParameter.h>
50#include <soundtrigger/SoundTrigger.h>
51#include "AudioPolicyManager.h"
Sharad Sangle36781612015-05-28 16:15:16 +053052#include <policy.h>
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070053
54namespace android {
Sharad Sanglec5766ff2015-06-04 20:24:10 +053055#ifdef VOICE_CONCURRENCY
56audio_output_flags_t AudioPolicyManagerCustom::getFallBackPath()
57{
58 audio_output_flags_t flag = AUDIO_OUTPUT_FLAG_FAST;
59 char propValue[PROPERTY_VALUE_MAX];
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070060
Sharad Sanglec5766ff2015-06-04 20:24:10 +053061 if (property_get("voice.conc.fallbackpath", propValue, NULL)) {
62 if (!strncmp(propValue, "deep-buffer", 11)) {
63 flag = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
64 }
65 else if (!strncmp(propValue, "fast", 4)) {
66 flag = AUDIO_OUTPUT_FLAG_FAST;
67 }
68 else {
69 ALOGD("voice_conc:not a recognised path(%s) in prop voice.conc.fallbackpath",
70 propValue);
71 }
72 }
73 else {
74 ALOGD("voice_conc:prop voice.conc.fallbackpath not set");
75 }
76
77 ALOGD("voice_conc:picked up flag(0x%x) from prop voice.conc.fallbackpath",
78 flag);
79
80 return flag;
81}
82#endif /*VOICE_CONCURRENCY*/
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070083// ----------------------------------------------------------------------------
84// AudioPolicyInterface implementation
85// ----------------------------------------------------------------------------
Sharad Sangle36781612015-05-28 16:15:16 +053086extern "C" AudioPolicyInterface* createAudioPolicyManager(
87 AudioPolicyClientInterface *clientInterface)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070088{
Sharad Sangle36781612015-05-28 16:15:16 +053089 return new AudioPolicyManagerCustom(clientInterface);
90}
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070091
Sharad Sangle36781612015-05-28 16:15:16 +053092extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface)
93{
94 delete interface;
95}
96
97status_t AudioPolicyManagerCustom::setDeviceConnectionStateInt(audio_devices_t device,
98 audio_policy_dev_state_t state,
99 const char *device_address,
100 const char *device_name)
101{
102 ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
103 device, state, device_address, device_name);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700104
105 // connect/disconnect only 1 device at a time
106 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
107
Sharad Sangle36781612015-05-28 16:15:16 +0530108 sp<DeviceDescriptor> devDesc =
109 mHwModules.getDeviceDescriptor(device, device_address, device_name);
110
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700111 // handle output devices
112 if (audio_is_output_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700113 SortedVector <audio_io_handle_t> outputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700114
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700115 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700116
117 // save a copy of the opened output descriptors before any output is opened or closed
118 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
119 mPreviousOutputs = mOutputs;
120 switch (state)
121 {
122 // handle output device connection
Sharad Sangle36781612015-05-28 16:15:16 +0530123 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700124 if (index >= 0) {
Sharad Sangle4509cef2015-08-19 20:47:12 +0530125#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
126 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
127 if (!strncmp(device_address, "hdmi_spkr", 9)) {
128 mHdmiAudioDisabled = false;
129 } else {
130 mHdmiAudioEvent = true;
131 }
132 }
133#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700134 ALOGW("setDeviceConnectionState() device already connected: %x", device);
135 return INVALID_OPERATION;
136 }
137 ALOGV("setDeviceConnectionState() connecting device %x", device);
138
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700139 // register new device as available
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700140 index = mAvailableOutputDevices.add(devDesc);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530141#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
142 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
143 if (!strncmp(device_address, "hdmi_spkr", 9)) {
144 mHdmiAudioDisabled = false;
145 } else {
146 mHdmiAudioEvent = true;
147 }
148 if (mHdmiAudioDisabled || !mHdmiAudioEvent) {
149 mAvailableOutputDevices.remove(devDesc);
150 ALOGW("HDMI sink not connected, do not route audio to HDMI out");
151 return INVALID_OPERATION;
152 }
153 }
154#endif
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700155 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530156 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700157 if (module == 0) {
158 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
159 device);
160 mAvailableOutputDevices.remove(devDesc);
161 return INVALID_OPERATION;
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700162 }
Sharad Sangle36781612015-05-28 16:15:16 +0530163 mAvailableOutputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700164 } else {
165 return NO_MEMORY;
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700166 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700167
Sharad Sangle36781612015-05-28 16:15:16 +0530168 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700169 mAvailableOutputDevices.remove(devDesc);
170 return INVALID_OPERATION;
171 }
Sharad Sangle36781612015-05-28 16:15:16 +0530172 // Propagate device availability to Engine
173 mEngine->setDeviceConnectionState(devDesc, state);
174
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700175 // outputs should never be empty here
176 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
177 "checkOutputsForDevice() returned no outputs but status OK");
178 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
179 outputs.size());
Sharad Sangle36781612015-05-28 16:15:16 +0530180
181 // Send connect to HALs
182 AudioParameter param = AudioParameter(devDesc->mAddress);
183 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
184 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
185
186 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700187 // handle output device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700188 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
189 if (index < 0) {
Sharad Sangle4509cef2015-08-19 20:47:12 +0530190#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
191 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
192 if (!strncmp(device_address, "hdmi_spkr", 9)) {
193 mHdmiAudioDisabled = true;
194 } else {
195 mHdmiAudioEvent = false;
196 }
197 }
198#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700199 ALOGW("setDeviceConnectionState() device not connected: %x", device);
200 return INVALID_OPERATION;
201 }
202
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700203 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
204
Sharad Sangle36781612015-05-28 16:15:16 +0530205 // Send Disconnect to HALs
206 AudioParameter param = AudioParameter(devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700207 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
208 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
209
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700210 // remove device from available output devices
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700211 mAvailableOutputDevices.remove(devDesc);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530212#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
213 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
214 if (!strncmp(device_address, "hdmi_spkr", 9)) {
215 mHdmiAudioDisabled = true;
216 } else {
217 mHdmiAudioEvent = false;
218 }
219 }
220#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530221 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
222
223 // Propagate device availability to Engine
224 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700225 } break;
226
227 default:
228 ALOGE("setDeviceConnectionState() invalid state: %x", state);
229 return BAD_VALUE;
230 }
231
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700232 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
233 // output is suspended before any tracks are moved to it
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700234 checkA2dpSuspend();
235 checkOutputForAllStrategies();
236 // outputs must be closed after checkOutputForAllStrategies() is executed
237 if (!outputs.isEmpty()) {
238 for (size_t i = 0; i < outputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530239 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700240 // close unused outputs after device disconnection or direct outputs that have been
241 // opened by checkOutputsForDevice() to query dynamic parameters
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700242 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700243 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
244 (desc->mDirectOpenCount == 0))) {
245 closeOutput(outputs[i]);
246 }
247 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700248 // check again after closing A2DP output to reset mA2dpSuspended if needed
249 checkA2dpSuspend();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700250 }
251
252 updateDevicesAndOutputs();
Sharad Sangle36781612015-05-28 16:15:16 +0530253 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
254 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700255 updateCallRouting(newDevice);
256 }
Dhananjay Kumar8ccb8312015-10-21 12:36:19 +0530257
258#ifdef FM_POWER_OPT
259 // handle FM device connection state to trigger FM AFE loopback
260 if(device == AUDIO_DEVICE_OUT_FM && hasPrimaryOutput()) {
261 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
262 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
263 mPrimaryOutput->changeRefCount(AUDIO_STREAM_MUSIC, 1);
264 newDevice = newDevice | AUDIO_DEVICE_OUT_FM;
265 } else {
266 mPrimaryOutput->changeRefCount(AUDIO_STREAM_MUSIC, -1);
267 }
268 AudioParameter param = AudioParameter();
269 param.addInt(String8("handle_fm"), (int)newDevice);
270 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString());
271 }
272#endif /* FM_POWER_OPT end */
273
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700274 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530275 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
276 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
277 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700278 // do not force device change on duplicated output because if device is 0, it will
279 // also force a device 0 for the two outputs it is duplicated to which may override
280 // a valid device selection on those outputs.
Sharad Sangle36781612015-05-28 16:15:16 +0530281 bool force = !desc->isDuplicated()
282 && (!device_distinguishes_on_address(device)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700283 // always force when disconnecting (a non-duplicated device)
284 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Sharad Sangle36781612015-05-28 16:15:16 +0530285 setOutputDevice(desc, newDevice, force, 0);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700286 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700287 }
288
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700289 mpClientInterface->onAudioPortListUpdate();
290 return NO_ERROR;
291 } // end if is output device
292
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700293 // handle input devices
294 if (audio_is_input_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700295 SortedVector <audio_io_handle_t> inputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700296
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700297 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700298 switch (state)
299 {
300 // handle input device connection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700301 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
302 if (index >= 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700303 ALOGW("setDeviceConnectionState() device already connected: %d", device);
304 return INVALID_OPERATION;
305 }
Sharad Sangle36781612015-05-28 16:15:16 +0530306 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700307 if (module == NULL) {
308 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
309 device);
310 return INVALID_OPERATION;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700311 }
Sharad Sangle36781612015-05-28 16:15:16 +0530312 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700313 return INVALID_OPERATION;
314 }
315
316 index = mAvailableInputDevices.add(devDesc);
317 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530318 mAvailableInputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700319 } else {
320 return NO_MEMORY;
321 }
Sharad Sangle36781612015-05-28 16:15:16 +0530322
323 // Set connect to HALs
324 AudioParameter param = AudioParameter(devDesc->mAddress);
325 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
326 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
327
328 // Propagate device availability to Engine
329 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700330 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700331
332 // handle input device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700333 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
334 if (index < 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700335 ALOGW("setDeviceConnectionState() device not connected: %d", device);
336 return INVALID_OPERATION;
337 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700338
339 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
340
341 // Set Disconnect to HALs
Sharad Sangle36781612015-05-28 16:15:16 +0530342 AudioParameter param = AudioParameter(devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700343 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
344 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
345
Sharad Sangle36781612015-05-28 16:15:16 +0530346 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700347 mAvailableInputDevices.remove(devDesc);
348
Sharad Sangle36781612015-05-28 16:15:16 +0530349 // Propagate device availability to Engine
350 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700351 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700352
353 default:
354 ALOGE("setDeviceConnectionState() invalid state: %x", state);
355 return BAD_VALUE;
356 }
357
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700358 closeAllInputs();
359
Sharad Sangle36781612015-05-28 16:15:16 +0530360 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700361 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
362 updateCallRouting(newDevice);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700363 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700364
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700365 mpClientInterface->onAudioPortListUpdate();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700366 return NO_ERROR;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700367 } // end if is input device
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700368
369 ALOGW("setDeviceConnectionState() invalid device: %x", device);
370 return BAD_VALUE;
371}
Sharad Sangle36781612015-05-28 16:15:16 +0530372// This function checks for the parameters which can be offloaded.
373// This can be enhanced depending on the capability of the DSP and policy
374// of the system.
375bool AudioPolicyManagerCustom::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700376{
Sharad Sangle36781612015-05-28 16:15:16 +0530377 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
378 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
379 offloadInfo.sample_rate, offloadInfo.channel_mask,
380 offloadInfo.format,
381 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
382 offloadInfo.has_video);
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530383#ifdef VOICE_CONCURRENCY
384 char concpropValue[PROPERTY_VALUE_MAX];
385 if (property_get("voice.playback.conc.disabled", concpropValue, NULL)) {
386 bool propenabled = atoi(concpropValue) || !strncmp("true", concpropValue, 4);
387 if (propenabled) {
388 if (isInCall())
389 {
390 ALOGD("\n copl: blocking compress offload on call mode\n");
391 return false;
392 }
393 }
394 }
395#endif
396#ifdef RECORD_PLAY_CONCURRENCY
397 char recConcPropValue[PROPERTY_VALUE_MAX];
398 bool prop_rec_play_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700399
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530400 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
401 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
402 }
403
404 if ((prop_rec_play_enabled) &&
405 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
406 ALOGD("copl: blocking compress offload for record concurrency");
407 return false;
408 }
409#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530410 // Check if stream type is music, then only allow offload as of now.
411 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
412 {
413 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
414 return false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700415 }
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530416
417 char propValue[PROPERTY_VALUE_MAX];
418 bool pcmOffload = false;
419#ifdef PCM_OFFLOAD_ENABLED
420 if ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM_OFFLOAD) {
421 bool prop_enabled = false;
422 if ((AUDIO_FORMAT_PCM_16_BIT_OFFLOAD == offloadInfo.format) &&
423 property_get("audio.offload.pcm.16bit.enable", propValue, NULL)) {
424 prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
425 }
426
427#ifdef PCM_OFFLOAD_ENABLED_24
428 if ((AUDIO_FORMAT_PCM_24_BIT_OFFLOAD == offloadInfo.format) &&
429 property_get("audio.offload.pcm.24bit.enable", propValue, NULL)) {
430 prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530431 }
432#endif
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530433
434 if (prop_enabled) {
435 ALOGI("PCM offload property is enabled");
436 pcmOffload = true;
437 }
438
439 if (!pcmOffload) {
440 ALOGD("system property not enabled for PCM offload format[%x]",offloadInfo.format);
441 return false;
442 }
443 }
444#endif
445 if (!pcmOffload) {
446 // Check if offload has been disabled
447 if (property_get("audio.offload.disable", propValue, "0")) {
448 if (atoi(propValue) != 0) {
449 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
450 return false;
451 }
452 }
453 //check if it's multi-channel AAC (includes sub formats) and FLAC format
454 if ((popcount(offloadInfo.channel_mask) > 2) &&
455 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
456 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS))) {
457 ALOGD("offload disabled for multi-channel AAC,FLAC and VORBIS format");
458 return false;
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530459 }
460
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530461#ifdef AUDIO_EXTN_FORMATS_ENABLED
462 //check if it's multi-channel FLAC/ALAC/WMA format with sample rate > 48k
463 if ((popcount(offloadInfo.channel_mask) > 2) &&
464 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
Manish Dewangana6fc5442015-08-24 20:30:31 +0530465 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) && (offloadInfo.sample_rate > 48000)) ||
466 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) && (offloadInfo.sample_rate > 48000)) ||
467 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) && (offloadInfo.sample_rate > 48000)) ||
468 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS))) {
469 ALOGD("offload disabled for multi-channel FLAC/ALAC/WMA/AAC_ADTS clips with sample rate > 48kHz");
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530470 return false;
471 }
472#endif
473 //TODO: enable audio offloading with video when ready
474 const bool allowOffloadWithVideo =
475 property_get_bool("audio.offload.video", false /* default_value */);
476 if (offloadInfo.has_video && !allowOffloadWithVideo) {
477 ALOGV("isOffloadSupported: has_video == true, returning false");
478 return false;
479 }
Manish Dewanganf3cd0f82015-10-13 14:04:36 +0530480
481 const bool allowOffloadStreamingWithVideo = property_get_bool("av.streaming.offload.enable",
482 false /*default value*/);
483 if(offloadInfo.has_video && offloadInfo.is_streaming && !allowOffloadStreamingWithVideo) {
484 ALOGW("offload disabled by av.streaming.offload.enable = %s ", propValue );
485 return false;
486 }
487
Sharad Sangle36781612015-05-28 16:15:16 +0530488 }
489
490 //If duration is less than minimum value defined in property, return false
491 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
492 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
493 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
494 return false;
495 }
496 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
497 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
498 //duration checks only valid for MP3/AAC/ formats,
499 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
500 if ((offloadInfo.format == AUDIO_FORMAT_MP3) ||
501 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530502 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS) ||
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530503#ifdef AUDIO_EXTN_FORMATS_ENABLED
Sharad Sangle36781612015-05-28 16:15:16 +0530504 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
Sharad Sangle36781612015-05-28 16:15:16 +0530505 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) ||
506 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) ||
507 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) ||
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530508 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_APE) ||
Manish Dewangana6fc5442015-08-24 20:30:31 +0530509 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS) ||
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530510#endif
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530511 pcmOffload)
Sharad Sangle36781612015-05-28 16:15:16 +0530512 return false;
513
514 }
515
516 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
517 // creating an offloaded track and tearing it down immediately after start when audioflinger
518 // detects there is an active non offloadable effect.
519 // FIXME: We should check the audio session here but we do not have it in this context.
520 // This may prevent offloading in rare situations where effects are left active by apps
521 // in the background.
522 if (mEffects.isNonOffloadableEffectEnabled()) {
523 return false;
524 }
525 // Check for soundcard status
526 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
527 String8("SND_CARD_STATUS"));
528 AudioParameter result = AudioParameter(valueStr);
529 int isonline = 0;
530 if ((result.getInt(String8("SND_CARD_STATUS"), isonline) == NO_ERROR)
531 && !isonline) {
532 ALOGD("copl: soundcard is offline rejecting offload request");
533 return false;
534 }
535 // See if there is a profile to support this.
536 // AUDIO_DEVICE_NONE
537 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
538 offloadInfo.sample_rate,
539 offloadInfo.format,
540 offloadInfo.channel_mask,
541 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
542 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
543 return (profile != 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700544}
Sharad Sangle36781612015-05-28 16:15:16 +0530545audio_devices_t AudioPolicyManagerCustom::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
546 bool fromCache)
547{
548 audio_devices_t device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700549
Sharad Sangle36781612015-05-28 16:15:16 +0530550 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
551 if (index >= 0) {
552 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
553 if (patchDesc->mUid != mUidCached) {
554 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
555 outputDesc->device(), outputDesc->mPatchHandle);
556 return outputDesc->device();
557 }
558 }
559
560 // check the following by order of priority to request a routing change if necessary:
561 // 1: the strategy enforced audible is active and enforced on the output:
562 // use device for strategy enforced audible
563 // 2: we are in call or the strategy phone is active on the output:
564 // use device for strategy phone
565 // 3: the strategy for enforced audible is active but not enforced on the output:
566 // use the device for strategy enforced audible
567 // 4: the strategy sonification is active on the output:
568 // use device for strategy sonification
569 // 5: the strategy "respectful" sonification is active on the output:
570 // use device for strategy "respectful" sonification
571 // 6: the strategy accessibility is active on the output:
572 // use device for strategy accessibility
573 // 7: the strategy media is active on the output:
574 // use device for strategy media
575 // 8: the strategy DTMF is active on the output:
576 // use device for strategy DTMF
577 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
578 // use device for strategy t-t-s
579 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
580 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
581 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
582 } else if (isInCall() ||
583 isStrategyActive(outputDesc, STRATEGY_PHONE)||
584 isStrategyActive(mPrimaryOutput, STRATEGY_PHONE)) {
585 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
586 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
587 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
588 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)||
589 (isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION)
590 && (!isStrategyActive(mPrimaryOutput,STRATEGY_MEDIA)))) {
591 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530592 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL) ||
593 isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION_RESPECTFUL)) {
Sharad Sangle36781612015-05-28 16:15:16 +0530594 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
595 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
596 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
597 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
598 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
599 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
600 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
601 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
602 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
603 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
604 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
605 }
606
607 ALOGV("getNewOutputDevice() selected device %x", device);
608 return device;
609}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700610void AudioPolicyManagerCustom::setPhoneState(audio_mode_t state)
611{
Sharad Sangle36781612015-05-28 16:15:16 +0530612 ALOGV("setPhoneState() state %d", state);
613 // store previous phone state for management of sonification strategy below
Sharad Sangle4509cef2015-08-19 20:47:12 +0530614 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Sharad Sangle36781612015-05-28 16:15:16 +0530615 int oldState = mEngine->getPhoneState();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700616
Sharad Sangle36781612015-05-28 16:15:16 +0530617 if (mEngine->setPhoneState(state) != NO_ERROR) {
618 ALOGW("setPhoneState() invalid or same state %d", state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700619 return;
620 }
Sharad Sangle36781612015-05-28 16:15:16 +0530621 /// Opens: can these line be executed after the switch of volume curves???
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700622 // if leaving call state, handle special case of active streams
623 // pertaining to sonification strategy see handleIncallSonification()
624 if (isInCall()) {
625 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530626 for (size_t j = 0; j < mOutputs.size(); j++) {
627 audio_io_handle_t curOutput = mOutputs.keyAt(j);
628 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
629 if (stream == AUDIO_STREAM_PATCH) {
630 continue;
631 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530632 handleIncallSonification((audio_stream_type_t)stream, false, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +0530633 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700634 }
Sharad Sangle36781612015-05-28 16:15:16 +0530635
636 // force reevaluating accessibility routing when call starts
637 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700638 }
639
Sharad Sangle36781612015-05-28 16:15:16 +0530640 /**
641 * Switching to or from incall state or switching between telephony and VoIP lead to force
642 * routing command.
643 */
644 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
645 || (is_state_in_call(state) && (state != oldState)));
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700646
647 // check for device and output changes triggered by new phone state
648 checkA2dpSuspend();
649 checkOutputForAllStrategies();
650 updateDevicesAndOutputs();
651
Sharad Sangle36781612015-05-28 16:15:16 +0530652 sp<SwAudioOutputDescriptor> hwOutputDesc = mPrimaryOutput;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530653#ifdef VOICE_CONCURRENCY
654 int voice_call_state = 0;
655 char propValue[PROPERTY_VALUE_MAX];
656 bool prop_playback_enabled = false, prop_rec_enabled=false, prop_voip_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700657
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530658 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
659 prop_playback_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
660 }
661
662 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
663 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
664 }
665
666 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
667 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
668 }
669
670 bool mode_in_call = (AUDIO_MODE_IN_CALL != oldState) && (AUDIO_MODE_IN_CALL == state);
671 //query if it is a actual voice call initiated by telephony
672 if (mode_in_call) {
673 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0, String8("in_call"));
674 AudioParameter result = AudioParameter(valueStr);
675 if (result.getInt(String8("in_call"), voice_call_state) == NO_ERROR)
676 ALOGD("voice_conc:SetPhoneState: Voice call state = %d", voice_call_state);
677 }
678
679 if (mode_in_call && voice_call_state && !mvoice_call_state) {
680 ALOGD("voice_conc:Entering to call mode oldState :: %d state::%d ",
681 oldState, state);
682 mvoice_call_state = voice_call_state;
683 if (prop_rec_enabled) {
684 //Close all active inputs
685 audio_io_handle_t activeInput = mInputs.getActiveInput();
686 if (activeInput != 0) {
687 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
688 switch(activeDesc->mInputSource) {
689 case AUDIO_SOURCE_VOICE_UPLINK:
690 case AUDIO_SOURCE_VOICE_DOWNLINK:
691 case AUDIO_SOURCE_VOICE_CALL:
692 ALOGD("voice_conc:FOUND active input during call active: %d",activeDesc->mInputSource);
693 break;
694
695 case AUDIO_SOURCE_VOICE_COMMUNICATION:
696 if(prop_voip_enabled) {
697 ALOGD("voice_conc:CLOSING VoIP input source on call setup :%d ",activeDesc->mInputSource);
698 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
699 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
700 }
701 break;
702
703 default:
704 ALOGD("voice_conc:CLOSING input on call setup for inputSource: %d",activeDesc->mInputSource);
705 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
706 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
707 break;
708 }
709 }
710 } else if (prop_voip_enabled) {
711 audio_io_handle_t activeInput = mInputs.getActiveInput();
712 if (activeInput != 0) {
713 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
714 if (AUDIO_SOURCE_VOICE_COMMUNICATION == activeDesc->mInputSource) {
715 ALOGD("voice_conc:CLOSING VoIP on call setup : %d",activeDesc->mInputSource);
716 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
717 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
718 }
719 }
720 }
721 if (prop_playback_enabled) {
722 // Move tracks associated to this strategy from previous output to new output
723 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
724 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
725 if (i == AUDIO_STREAM_PATCH) {
726 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
727 continue;
728 }
729 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
730 if ((AUDIO_STREAM_MUSIC == i) ||
731 (AUDIO_STREAM_VOICE_CALL == i) ) {
732 ALOGD("voice_conc:Invalidate stream type %d", i);
733 mpClientInterface->invalidateStream((audio_stream_type_t)i);
734 }
735 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
736 ALOGD("voice_conc:Invalidate stream type %d", i);
737 mpClientInterface->invalidateStream((audio_stream_type_t)i);
738 }
739 }
740 }
741
742 for (size_t i = 0; i < mOutputs.size(); i++) {
743 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
744 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
745 ALOGD("voice_conc:ouput desc / profile is NULL");
746 continue;
747 }
748
749 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
750 if (((!outputDesc->isDuplicated() &&outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY))
751 && prop_playback_enabled) {
752 ALOGD("voice_conc:calling suspendOutput on call mode for primary output");
753 mpClientInterface->suspendOutput(mOutputs.keyAt(i));
754 } //Close compress all sessions
755 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
756 && prop_playback_enabled) {
757 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
758 closeOutput(mOutputs.keyAt(i));
759 }
760 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_VOIP_RX)
761 && prop_voip_enabled) {
762 ALOGD("voice_conc:calling closeOutput on call mode for DIRECT output");
763 closeOutput(mOutputs.keyAt(i));
764 }
765 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
766 if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)
767 && prop_playback_enabled) {
768 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
769 closeOutput(mOutputs.keyAt(i));
770 }
771 }
772 }
773 }
774
775 if ((AUDIO_MODE_IN_CALL == oldState || AUDIO_MODE_IN_COMMUNICATION == oldState) &&
776 (AUDIO_MODE_NORMAL == state) && prop_playback_enabled && mvoice_call_state) {
777 ALOGD("voice_conc:EXITING from call mode oldState :: %d state::%d \n",oldState, state);
778 mvoice_call_state = 0;
779 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
780 //restore PCM (deep-buffer) output after call termination
781 for (size_t i = 0; i < mOutputs.size(); i++) {
782 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
783 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
784 ALOGD("voice_conc:ouput desc / profile is NULL");
785 continue;
786 }
787 if (!outputDesc->isDuplicated() && outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
788 ALOGD("voice_conc:calling restoreOutput after call mode for primary output");
789 mpClientInterface->restoreOutput(mOutputs.keyAt(i));
790 }
791 }
792 }
793 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
794 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
795 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
796 if (i == AUDIO_STREAM_PATCH) {
797 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
798 continue;
799 }
800 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
801 if ((AUDIO_STREAM_MUSIC == i) ||
802 (AUDIO_STREAM_VOICE_CALL == i) ) {
803 mpClientInterface->invalidateStream((audio_stream_type_t)i);
804 }
805 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
806 mpClientInterface->invalidateStream((audio_stream_type_t)i);
807 }
808 }
809 }
810
811#endif
812#ifdef RECORD_PLAY_CONCURRENCY
813 char recConcPropValue[PROPERTY_VALUE_MAX];
814 bool prop_rec_play_enabled = false;
815
816 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
817 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
818 }
819 if (prop_rec_play_enabled) {
820 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
821 ALOGD("phone state changed to MODE_IN_COMM invlaidating music and voice streams");
822 // call invalidate for voice streams, so that it can use deepbuffer with VoIP out device from HAL
823 mpClientInterface->invalidateStream(AUDIO_STREAM_VOICE_CALL);
824 // call invalidate for music, so that compress will fallback to deep-buffer with VoIP out device
825 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
826
827 // close compress output to make sure session will be closed before timeout(60sec)
828 for (size_t i = 0; i < mOutputs.size(); i++) {
829
830 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
831 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
832 ALOGD("ouput desc / profile is NULL");
833 continue;
834 }
835
836 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
837 ALOGD("calling closeOutput on call mode for COMPRESS output");
838 closeOutput(mOutputs.keyAt(i));
839 }
840 }
841 } else if ((oldState == AUDIO_MODE_IN_COMMUNICATION) &&
842 (mEngine->getPhoneState() == AUDIO_MODE_NORMAL)) {
843 // call invalidate for music so that music can fallback to compress
844 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
845 }
846 }
847#endif
848 mPrevPhoneState = oldState;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700849 int delayMs = 0;
850 if (isStateInCall(state)) {
851 nsecs_t sysTime = systemTime();
852 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530853 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700854 // mute media and sonification strategies and delay device switch by the largest
855 // latency of any output where either strategy is active.
856 // This avoid sending the ring tone or music tail into the earpiece or headset.
Sharad Sangle36781612015-05-28 16:15:16 +0530857 if ((isStrategyActive(desc, STRATEGY_MEDIA,
858 SONIFICATION_HEADSET_MUSIC_DELAY,
859 sysTime) ||
860 isStrategyActive(desc, STRATEGY_SONIFICATION,
861 SONIFICATION_HEADSET_MUSIC_DELAY,
862 sysTime)) &&
863 (delayMs < (int)desc->latency()*2)) {
864 delayMs = desc->latency()*2;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700865 }
Sharad Sangle36781612015-05-28 16:15:16 +0530866 setStrategyMute(STRATEGY_MEDIA, true, desc);
867 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700868 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Sharad Sangle36781612015-05-28 16:15:16 +0530869 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
870 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700871 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
872 }
Sharad Sangle36781612015-05-28 16:15:16 +0530873 ALOGV("Setting the delay from %dms to %dms", delayMs,
874 MIN(delayMs, MAX_VOICE_CALL_START_DELAY_MS));
875 delayMs = MIN(delayMs, MAX_VOICE_CALL_START_DELAY_MS);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700876 }
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
1291 bool pcmOffloadEnabled = property_get_bool("audio.offload.track.enable", false);
1292
1293 if (offloadInfo == NULL && pcmOffloadEnabled) {
1294 tOffloadInfo.sample_rate = samplingRate;
1295 tOffloadInfo.channel_mask = channelMask;
1296 tOffloadInfo.format = format;
1297 tOffloadInfo.stream_type = *stream;
1298 tOffloadInfo.bit_width = 16; //hard coded for PCM_16
1299 if (attr != NULL) {
1300 ALOGV("found attribute .. setting usage %d ", attr->usage);
1301 tOffloadInfo.usage = attr->usage;
1302 } else {
1303 ALOGD("%s:: attribute is NULL .. no usage set", __func__);
1304 }
1305 offloadInfo = &tOffloadInfo;
1306 }
1307
1308 return AudioPolicyManager::getOutputForAttr(attr, output, session, stream,
1309 (uid_t)uid, (uint32_t)samplingRate,
1310 format, (audio_channel_mask_t)channelMask,
1311 flags, (audio_port_handle_t)selectedDeviceId,
1312 offloadInfo);
1313}
1314
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001315audio_io_handle_t AudioPolicyManagerCustom::getOutputForDevice(
1316 audio_devices_t device,
Sharad Sangle36781612015-05-28 16:15:16 +05301317 audio_session_t session __unused,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001318 audio_stream_type_t stream,
1319 uint32_t samplingRate,
1320 audio_format_t format,
1321 audio_channel_mask_t channelMask,
1322 audio_output_flags_t flags,
1323 const audio_offload_info_t *offloadInfo)
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001324{
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001325 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
1326 uint32_t latency = 0;
1327 status_t status;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001328
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001329#ifdef AUDIO_POLICY_TEST
1330 if (mCurOutput != 0) {
1331 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1332 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1333
1334 if (mTestOutputs[mCurOutput] == 0) {
1335 ALOGV("getOutput() opening test output");
Sharad Sangle36781612015-05-28 16:15:16 +05301336 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
1337 mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001338 outputDesc->mDevice = mTestDevice;
1339 outputDesc->mLatency = mTestLatencyMs;
1340 outputDesc->mFlags =
1341 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
1342 outputDesc->mRefCount[stream] = 0;
1343 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1344 config.sample_rate = mTestSamplingRate;
1345 config.channel_mask = mTestChannels;
1346 config.format = mTestFormat;
1347 if (offloadInfo != NULL) {
1348 config.offload_info = *offloadInfo;
1349 }
1350 status = mpClientInterface->openOutput(0,
1351 &mTestOutputs[mCurOutput],
1352 &config,
1353 &outputDesc->mDevice,
1354 String8(""),
1355 &outputDesc->mLatency,
1356 outputDesc->mFlags);
1357 if (status == NO_ERROR) {
1358 outputDesc->mSamplingRate = config.sample_rate;
1359 outputDesc->mFormat = config.format;
1360 outputDesc->mChannelMask = config.channel_mask;
1361 AudioParameter outputCmd = AudioParameter();
1362 outputCmd.addInt(String8("set_id"),mCurOutput);
1363 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1364 addOutput(mTestOutputs[mCurOutput], outputDesc);
1365 }
1366 }
1367 return mTestOutputs[mCurOutput];
1368 }
1369#endif //AUDIO_POLICY_TEST
Sharad Sangle36781612015-05-28 16:15:16 +05301370 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1371 (stream != AUDIO_STREAM_MUSIC)) {
1372 // compress should not be used for non-music streams
1373 ALOGE("Offloading only allowed with music stream");
1374 return 0;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301375 }
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301376
1377 if ((stream == AUDIO_STREAM_VOICE_CALL) &&
1378 (channelMask == 1) &&
1379 (samplingRate == 8000 || samplingRate == 16000)) {
1380 // Allow Voip direct output only if:
1381 // audio mode is MODE_IN_COMMUNCATION; AND
1382 // voip output is not opened already; AND
1383 // requested sample rate matches with that of voip input stream (if opened already)
1384 int value = 0;
1385 uint32_t mode = 0, voipOutCount = 1, voipSampleRate = 1;
1386 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1387 String8("audio_mode"));
1388 AudioParameter result = AudioParameter(valueStr);
1389 if (result.getInt(String8("audio_mode"), value) == NO_ERROR) {
1390 mode = value;
1391 }
1392
1393 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1394 String8("voip_out_stream_count"));
1395 result = AudioParameter(valueStr);
1396 if (result.getInt(String8("voip_out_stream_count"), value) == NO_ERROR) {
1397 voipOutCount = value;
1398 }
1399
1400 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1401 String8("voip_sample_rate"));
1402 result = AudioParameter(valueStr);
1403 if (result.getInt(String8("voip_sample_rate"), value) == NO_ERROR) {
1404 voipSampleRate = value;
1405 }
1406
1407 if ((mode == AUDIO_MODE_IN_COMMUNICATION) && (voipOutCount == 0) &&
1408 ((voipSampleRate == 0) || (voipSampleRate == samplingRate))) {
1409 if (audio_is_linear_pcm(format)) {
1410 char propValue[PROPERTY_VALUE_MAX] = {0};
1411 property_get("use.voice.path.for.pcm.voip", propValue, "0");
1412 bool voipPcmSysPropEnabled = !strncmp("true", propValue, sizeof("true"));
1413 if (voipPcmSysPropEnabled && (format == AUDIO_FORMAT_PCM_16_BIT)) {
1414 flags = (audio_output_flags_t)((flags &~AUDIO_OUTPUT_FLAG_FAST) |
1415 AUDIO_OUTPUT_FLAG_VOIP_RX | AUDIO_OUTPUT_FLAG_DIRECT);
1416 ALOGD("Set VoIP and Direct output flags for PCM format");
1417 }
1418 }
1419 }
1420 }
1421
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301422#ifdef VOICE_CONCURRENCY
1423 char propValue[PROPERTY_VALUE_MAX];
1424 bool prop_play_enabled=false, prop_voip_enabled = false;
1425
1426 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1427 prop_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001428 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301429
1430 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1431 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1432 }
1433
1434 if (prop_play_enabled && mvoice_call_state) {
1435 //check if voice call is active / running in background
1436 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1437 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1438 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1439 {
1440 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1441 if(prop_voip_enabled) {
1442 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1443 flags );
1444 return 0;
1445 }
1446 }
1447 else {
1448 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
1449 ALOGD("voice_conc:IN call mode adding ULL flags .. flags: %x ", flags );
1450 flags = AUDIO_OUTPUT_FLAG_FAST;
1451 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
1452 if (AUDIO_STREAM_MUSIC == stream) {
1453 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1454 ALOGD("voice_conc:IN call mode adding deep-buffer flags %x ", flags );
1455 }
1456 else {
1457 flags = AUDIO_OUTPUT_FLAG_FAST;
1458 ALOGD("voice_conc:IN call mode adding fast flags %x ", flags );
1459 }
1460 }
1461 }
1462 }
1463 } else if (prop_voip_enabled && mvoice_call_state) {
1464 //check if voice call is active / running in background
1465 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1466 //return only ULL ouput
1467 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1468 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1469 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1470 {
1471 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1472 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1473 flags );
1474 return 0;
1475 }
1476 }
1477 }
1478#endif
1479#ifdef RECORD_PLAY_CONCURRENCY
1480 char recConcPropValue[PROPERTY_VALUE_MAX];
1481 bool prop_rec_play_enabled = false;
1482
1483 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
1484 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
1485 }
1486 if ((prop_rec_play_enabled) &&
1487 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
1488 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
1489 if (AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1490 // allow VoIP using voice path
1491 // Do nothing
1492 } else if((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1493 ALOGD("voice_conc:MODE_IN_COMM is setforcing deep buffer output for non ULL... flags: %x", flags);
1494 // use deep buffer path for all non ULL outputs
1495 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1496 }
1497 } else if ((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1498 ALOGD("voice_conc:Record mode is on forcing deep buffer output for non ULL... flags: %x ", flags);
1499 // use deep buffer path for all non ULL outputs
1500 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1501 }
1502 }
1503 if (prop_rec_play_enabled &&
1504 (stream == AUDIO_STREAM_ENFORCED_AUDIBLE)) {
1505 ALOGD("Record conc is on forcing ULL output for ENFORCED_AUDIBLE");
1506 flags = AUDIO_OUTPUT_FLAG_FAST;
1507 }
1508#endif
1509
Sharad Sangle4509cef2015-08-19 20:47:12 +05301510#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Sharad Sangle36781612015-05-28 16:15:16 +05301511 /*
1512 * WFD audio routes back to target speaker when starting a ringtone playback.
1513 * This is because primary output is reused for ringtone, so output device is
1514 * updated based on SONIFICATION strategy for both ringtone and music playback.
1515 * The same issue is not seen on remoted_submix HAL based WFD audio because
1516 * primary output is not reused and a new output is created for ringtone playback.
1517 * Issue is fixed by updating output flag to AUDIO_OUTPUT_FLAG_FAST when there is
1518 * a non-music stream playback on WFD, so primary output is not reused for ringtone.
1519 */
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001520 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
1521 if ((availableOutputDeviceTypes & AUDIO_DEVICE_OUT_PROXY)
1522 && (stream != AUDIO_STREAM_MUSIC)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301523 ALOGD("WFD audio: use OUTPUT_FLAG_FAST for non music stream. flags:%x", flags );
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001524 //For voip paths
1525 if(flags & AUDIO_OUTPUT_FLAG_DIRECT)
1526 flags = AUDIO_OUTPUT_FLAG_DIRECT;
1527 else //route every thing else to ULL path
1528 flags = AUDIO_OUTPUT_FLAG_FAST;
1529 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301530#endif
1531
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001532 // open a direct output if required by specified parameters
vivek mehta0ea887a2015-08-26 14:01:20 -07001533 // force direct flag if offload flag is set: offloading implies a direct output stream
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001534 // and all common behaviors are driven by checking only the direct flag
1535 // this should normally be set appropriately in the policy configuration file
1536 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1537 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1538 }
1539 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1540 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1541 }
vivek mehta0ea887a2015-08-26 14:01:20 -07001542
1543 // Do offload magic here
1544 if ((flags == AUDIO_OUTPUT_FLAG_NONE) && (stream == AUDIO_STREAM_MUSIC) &&
1545 (offloadInfo != NULL) &&
1546 ((offloadInfo->usage == AUDIO_USAGE_MEDIA ||
1547 (offloadInfo->usage == AUDIO_USAGE_GAME)))) {
1548 if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) {
1549 ALOGD("AudioCustomHAL --> Force Direct Flag ..");
1550 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1551 }
1552 }
1553
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 */)) {
1560 flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Sharad Sangle36781612015-05-28 16:15:16 +05301561 }
Sharad Sangle497aef82015-08-03 17:55:48 +05301562
Sharad Sangle36781612015-05-28 16:15:16 +05301563 if (stream == AUDIO_STREAM_TTS) {
1564 flags = AUDIO_OUTPUT_FLAG_TTS;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001565 }
1566
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301567 // open a direct output if required by specified parameters
1568 //force direct flag if offload flag is set: offloading implies a direct output stream
1569 // and all common behaviors are driven by checking only the direct flag
1570 // this should normally be set appropriately in the policy configuration file
1571 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1572 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1573 }
1574 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1575 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1576 }
1577 // only allow deep buffering for music stream type
1578 if (stream != AUDIO_STREAM_MUSIC) {
1579 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
1580 }
1581 if (stream == AUDIO_STREAM_TTS) {
1582 flags = AUDIO_OUTPUT_FLAG_TTS;
1583 }
1584
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001585 sp<IOProfile> profile;
1586
1587 // skip direct output selection if the request can obviously be attached to a mixed output
1588 // and not explicitly requested
1589 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
1590 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
1591 audio_channel_count_from_out_mask(channelMask) <= 2) {
1592 goto non_direct_output;
1593 }
1594
1595 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1596 // creating an offloaded track and tearing it down immediately after start when audioflinger
1597 // detects there is an active non offloadable effect.
1598 // FIXME: We should check the audio session here but we do not have it in this context.
1599 // This may prevent offloading in rare situations where effects are left active by apps
1600 // in the background.
1601
Sharad Sangle36781612015-05-28 16:15:16 +05301602 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
1603 !mEffects.isNonOffloadableEffectEnabled()) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001604 profile = getProfileForDirectOutput(device,
1605 samplingRate,
1606 format,
1607 channelMask,
1608 (audio_output_flags_t)flags);
1609 }
1610
1611 if (profile != 0) {
Sharad Sangle36781612015-05-28 16:15:16 +05301612 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001613
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001614 // if multiple concurrent offload decode is supported
1615 // do no check for reuse and also don't close previous output if its offload
1616 // previous output will be closed during track destruction
1617 if (!(property_get_bool("audio.offload.multiple.enabled", false) &&
1618 ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0))) {
1619 for (size_t i = 0; i < mOutputs.size(); i++) {
1620 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1621 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1622 outputDesc = desc;
1623 // reuse direct output if currently open and configured with same parameters
1624 if ((samplingRate == outputDesc->mSamplingRate) &&
1625 (format == outputDesc->mFormat) &&
1626 (channelMask == outputDesc->mChannelMask)) {
1627 outputDesc->mDirectOpenCount++;
1628 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1629 return mOutputs.keyAt(i);
1630 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001631 }
1632 }
Mingming Yin4a4a8c82015-10-21 11:05:08 -07001633 // close direct output if currently open and configured with different parameters
1634 if (outputDesc != NULL) {
1635 closeOutput(outputDesc->mIoHandle);
1636 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001637 }
Sharad Sangle36781612015-05-28 16:15:16 +05301638
1639 // if the selected profile is offloaded and no offload info was specified,
1640 // create a default one
1641 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
1642 if ((profile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
1643 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1644 defaultOffloadInfo.sample_rate = samplingRate;
1645 defaultOffloadInfo.channel_mask = channelMask;
1646 defaultOffloadInfo.format = format;
1647 defaultOffloadInfo.stream_type = stream;
1648 defaultOffloadInfo.bit_rate = 0;
1649 defaultOffloadInfo.duration_us = -1;
1650 defaultOffloadInfo.has_video = true; // conservative
1651 defaultOffloadInfo.is_streaming = true; // likely
1652 offloadInfo = &defaultOffloadInfo;
1653 }
1654
1655 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001656 outputDesc->mDevice = device;
1657 outputDesc->mLatency = 0;
Sharad Sangle36781612015-05-28 16:15:16 +05301658 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001659 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1660 config.sample_rate = samplingRate;
1661 config.channel_mask = channelMask;
1662 config.format = format;
1663 if (offloadInfo != NULL) {
1664 config.offload_info = *offloadInfo;
1665 }
Sharad Sangle36781612015-05-28 16:15:16 +05301666 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001667 &output,
1668 &config,
1669 &outputDesc->mDevice,
1670 String8(""),
1671 &outputDesc->mLatency,
1672 outputDesc->mFlags);
1673
1674 // only accept an output with the requested parameters
1675 if (status != NO_ERROR ||
1676 (samplingRate != 0 && samplingRate != config.sample_rate) ||
1677 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
1678 (channelMask != 0 && channelMask != config.channel_mask)) {
1679 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1680 "format %d %d, channelMask %04x %04x", output, samplingRate,
1681 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1682 outputDesc->mChannelMask);
1683 if (output != AUDIO_IO_HANDLE_NONE) {
1684 mpClientInterface->closeOutput(output);
1685 }
Sharad Sangle36781612015-05-28 16:15:16 +05301686 // fall back to mixer output if possible when the direct output could not be open
1687 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
1688 goto non_direct_output;
1689 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001690 return AUDIO_IO_HANDLE_NONE;
1691 }
1692 outputDesc->mSamplingRate = config.sample_rate;
1693 outputDesc->mChannelMask = config.channel_mask;
1694 outputDesc->mFormat = config.format;
1695 outputDesc->mRefCount[stream] = 0;
1696 outputDesc->mStopTime[stream] = 0;
1697 outputDesc->mDirectOpenCount = 1;
1698
1699 audio_io_handle_t srcOutput = getOutputForEffect();
1700 addOutput(output, outputDesc);
1701 audio_io_handle_t dstOutput = getOutputForEffect();
1702 if (dstOutput == output) {
1703 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1704 }
1705 mPreviousOutputs = mOutputs;
1706 ALOGV("getOutput() returns new direct output %d", output);
1707 mpClientInterface->onAudioPortListUpdate();
1708 return output;
1709 }
1710
1711non_direct_output:
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001712 // ignoring channel mask due to downmix capability in mixer
1713
1714 // open a non direct output
1715
1716 // for non direct outputs, only PCM is supported
1717 if (audio_is_linear_pcm(format)) {
1718 // get which output is suitable for the specified stream. The actual
1719 // routing change will happen when startOutput() will be called
1720 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1721
1722 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1723 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1724 output = selectOutput(outputs, flags, format);
1725 }
1726 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1727 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1728
vivek mehta0ea887a2015-08-26 14:01:20 -07001729 ALOGV("getOutputForDevice() returns output %d", output);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001730
1731 return output;
1732}
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301733
1734status_t AudioPolicyManagerCustom::getInputForAttr(const audio_attributes_t *attr,
1735 audio_io_handle_t *input,
1736 audio_session_t session,
1737 uid_t uid,
1738 uint32_t samplingRate,
1739 audio_format_t format,
1740 audio_channel_mask_t channelMask,
1741 audio_input_flags_t flags,
1742 audio_port_handle_t selectedDeviceId,
1743 input_type_t *inputType)
1744{
1745 audio_source_t inputSource = attr->source;
1746#ifdef VOICE_CONCURRENCY
1747
1748 char propValue[PROPERTY_VALUE_MAX];
1749 bool prop_rec_enabled=false, prop_voip_enabled = false;
1750
1751 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
1752 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1753 }
1754
1755 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1756 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1757 }
1758
1759 if (prop_rec_enabled && mvoice_call_state) {
1760 //check if voice call is active / running in background
1761 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1762 //Need to block input request
1763 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1764 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1765 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1766 {
1767 switch(inputSource) {
1768 case AUDIO_SOURCE_VOICE_UPLINK:
1769 case AUDIO_SOURCE_VOICE_DOWNLINK:
1770 case AUDIO_SOURCE_VOICE_CALL:
1771 ALOGD("voice_conc:Creating input during incall mode for inputSource: %d",
1772 inputSource);
1773 break;
1774
1775 case AUDIO_SOURCE_VOICE_COMMUNICATION:
1776 if(prop_voip_enabled) {
1777 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1778 inputSource);
1779 return NO_INIT;
1780 }
1781 break;
1782 default:
1783 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1784 inputSource);
1785 return NO_INIT;
1786 }
1787 }
1788 }//check for VoIP flag
1789 else if(prop_voip_enabled && mvoice_call_state) {
1790 //check if voice call is active / running in background
1791 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1792 //Need to block input request
1793 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1794 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1795 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1796 {
1797 if(inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1798 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
1799 return NO_INIT;
1800 }
1801 }
1802 }
1803
1804#endif
1805
1806 return AudioPolicyManager::getInputForAttr(attr,
1807 input,
1808 session,
1809 uid,
1810 samplingRate,
1811 format,
1812 channelMask,
1813 flags,
1814 selectedDeviceId,
1815 inputType);
1816}
1817status_t AudioPolicyManagerCustom::startInput(audio_io_handle_t input,
1818 audio_session_t session)
1819{
1820 ALOGV("startInput() input %d", input);
1821 ssize_t index = mInputs.indexOfKey(input);
1822 if (index < 0) {
1823 ALOGW("startInput() unknown input %d", input);
1824 return BAD_VALUE;
1825 }
1826 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1827
1828 index = inputDesc->mSessions.indexOf(session);
1829 if (index < 0) {
1830 ALOGW("startInput() unknown session %d on input %d", session, input);
1831 return BAD_VALUE;
1832 }
1833
1834 // virtual input devices are compatible with other input devices
1835 if (!is_virtual_input_device(inputDesc->mDevice)) {
1836
1837 // for a non-virtual input device, check if there is another (non-virtual) active input
1838 audio_io_handle_t activeInput = mInputs.getActiveInput();
1839 if (activeInput != 0 && activeInput != input) {
1840
1841 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1842 // otherwise the active input continues and the new input cannot be started.
1843 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
1844 if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
1845 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
1846 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
1847 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
1848 } else {
1849 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
1850 return INVALID_OPERATION;
1851 }
1852 }
1853 }
1854
1855 // Routing?
1856 mInputRoutes.incRouteActivity(session);
1857#ifdef RECORD_PLAY_CONCURRENCY
1858 mIsInputRequestOnProgress = true;
1859
1860 char getPropValue[PROPERTY_VALUE_MAX];
1861 bool prop_rec_play_enabled = false;
1862
1863 if (property_get("rec.playback.conc.disabled", getPropValue, NULL)) {
1864 prop_rec_play_enabled = atoi(getPropValue) || !strncmp("true", getPropValue, 4);
1865 }
1866
1867 if ((prop_rec_play_enabled) &&(mInputs.activeInputsCount() == 0)){
1868 // send update to HAL on record playback concurrency
1869 AudioParameter param = AudioParameter();
1870 param.add(String8("rec_play_conc_on"), String8("true"));
1871 ALOGD("startInput() setParameters rec_play_conc is setting to ON ");
1872 mpClientInterface->setParameters(0, param.toString());
1873
1874 // Call invalidate to reset all opened non ULL audio tracks
1875 // Move tracks associated to this strategy from previous output to new output
1876 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
1877 // Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder)
Sharad Sangle4509cef2015-08-19 20:47:12 +05301878 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE && (i != AUDIO_STREAM_PATCH))) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301879 ALOGD("Invalidate on releaseInput for stream :: %d ", i);
1880 //FIXME see fixme on name change
1881 mpClientInterface->invalidateStream((audio_stream_type_t)i);
1882 }
1883 }
1884 // close compress tracks
1885 for (size_t i = 0; i < mOutputs.size(); i++) {
1886 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
1887 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
1888 ALOGD("ouput desc / profile is NULL");
1889 continue;
1890 }
1891 if (outputDesc->mProfile->mFlags
1892 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1893 // close compress sessions
1894 ALOGD("calling closeOutput on record conc for COMPRESS output");
1895 closeOutput(mOutputs.keyAt(i));
1896 }
1897 }
1898 }
1899#endif
1900
1901 if (inputDesc->mRefCount == 0 || mInputRoutes.hasRouteChanged(session)) {
1902 // if input maps to a dynamic policy with an activity listener, notify of state change
1903 if ((inputDesc->mPolicyMix != NULL)
1904 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1905 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1906 MIX_STATE_MIXING);
1907 }
1908
1909 if (mInputs.activeInputsCount() == 0) {
1910 SoundTrigger::setCaptureState(true);
1911 }
1912 setInputDevice(input, getNewInputDevice(input), true /* force */);
1913
1914 // automatically enable the remote submix output when input is started if not
1915 // used by a policy mix of type MIX_TYPE_RECORDERS
1916 // For remote submix (a virtual device), we open only one input per capture request.
1917 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1918 String8 address = String8("");
1919 if (inputDesc->mPolicyMix == NULL) {
1920 address = String8("0");
1921 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1922 address = inputDesc->mPolicyMix->mRegistrationId;
1923 }
1924 if (address != "") {
1925 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1926 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1927 address, "remote-submix");
1928 }
1929 }
1930 }
1931
1932 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1933
1934 inputDesc->mRefCount++;
1935#ifdef RECORD_PLAY_CONCURRENCY
1936 mIsInputRequestOnProgress = false;
1937#endif
1938 return NO_ERROR;
1939}
1940status_t AudioPolicyManagerCustom::stopInput(audio_io_handle_t input,
1941 audio_session_t session)
1942{
1943 status_t status;
1944 status = AudioPolicyManager::stopInput(input, session);
1945#ifdef RECORD_PLAY_CONCURRENCY
1946 char propValue[PROPERTY_VALUE_MAX];
1947 bool prop_rec_play_enabled = false;
1948
1949 if (property_get("rec.playback.conc.disabled", propValue, NULL)) {
1950 prop_rec_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1951 }
1952
1953 if ((prop_rec_play_enabled) && (mInputs.activeInputsCount() == 0)) {
1954
1955 //send update to HAL on record playback concurrency
1956 AudioParameter param = AudioParameter();
1957 param.add(String8("rec_play_conc_on"), String8("false"));
1958 ALOGD("stopInput() setParameters rec_play_conc is setting to OFF ");
1959 mpClientInterface->setParameters(0, param.toString());
1960
1961 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
1962 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
1963 //Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder stop tone)
1964 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE) && (i != AUDIO_STREAM_PATCH)) {
1965 ALOGD(" Invalidate on stopInput for stream :: %d ", i);
1966 //FIXME see fixme on name change
1967 mpClientInterface->invalidateStream((audio_stream_type_t)i);
1968 }
1969 }
1970 }
1971#endif
1972 return status;
1973}
1974
1975AudioPolicyManagerCustom::AudioPolicyManagerCustom(AudioPolicyClientInterface *clientInterface)
Sharad Sangle4509cef2015-08-19 20:47:12 +05301976 : AudioPolicyManager(clientInterface),
1977 mHdmiAudioDisabled(false),
1978 mHdmiAudioEvent(false),
1979 mPrevPhoneState(0)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301980{
Mingming Yin38ea08c2015-10-05 15:24:04 -07001981 char ssr_enabled[PROPERTY_VALUE_MAX] = {0};
1982 bool prop_ssr_enabled = false;
1983
1984 if (property_get("ro.qc.sdk.audio.ssr", ssr_enabled, NULL)) {
1985 prop_ssr_enabled = atoi(ssr_enabled) || !strncmp("true", ssr_enabled, 4);
1986 }
1987
1988 for (size_t i = 0; i < mHwModules.size(); i++) {
1989 ALOGV("Hw module %d", i);
1990 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) {
1991 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
1992 ALOGV("Input profile ", j);
1993 for (size_t k = 0; k < inProfile->mChannelMasks.size(); k++) {
1994 audio_channel_mask_t channelMask =
1995 inProfile->mChannelMasks.itemAt(k);
1996 ALOGV("Channel Mask %x size %d", channelMask,
1997 inProfile->mChannelMasks.size());
1998 if (AUDIO_CHANNEL_IN_5POINT1 == channelMask) {
1999 if (!prop_ssr_enabled) {
2000 ALOGI("removing AUDIO_CHANNEL_IN_5POINT1 from"
2001 " input profile as SSR(surround sound record)"
2002 " is not supported on this chipset variant");
2003 inProfile->mChannelMasks.removeItemsAt(k, 1);
2004 ALOGV("Channel Mask size now %d",
2005 inProfile->mChannelMasks.size());
2006 }
2007 }
2008 }
2009 }
2010 }
2011
Sharad Sanglec5766ff2015-06-04 20:24:10 +05302012#ifdef RECORD_PLAY_CONCURRENCY
2013 mIsInputRequestOnProgress = false;
2014#endif
2015
2016
2017#ifdef VOICE_CONCURRENCY
2018 mFallBackflag = getFallBackPath();
2019#endif
2020}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07002021}