blob: d24d476633e8efad8f936136b19815ed29bf0ac2 [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 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700257 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530258 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
259 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
260 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700261 // do not force device change on duplicated output because if device is 0, it will
262 // also force a device 0 for the two outputs it is duplicated to which may override
263 // a valid device selection on those outputs.
Sharad Sangle36781612015-05-28 16:15:16 +0530264 bool force = !desc->isDuplicated()
265 && (!device_distinguishes_on_address(device)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700266 // always force when disconnecting (a non-duplicated device)
267 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Sharad Sangle36781612015-05-28 16:15:16 +0530268 setOutputDevice(desc, newDevice, force, 0);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700269 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700270 }
271
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700272 mpClientInterface->onAudioPortListUpdate();
273 return NO_ERROR;
274 } // end if is output device
275
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700276 // handle input devices
277 if (audio_is_input_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700278 SortedVector <audio_io_handle_t> inputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700279
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700280 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700281 switch (state)
282 {
283 // handle input device connection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700284 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
285 if (index >= 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700286 ALOGW("setDeviceConnectionState() device already connected: %d", device);
287 return INVALID_OPERATION;
288 }
Sharad Sangle36781612015-05-28 16:15:16 +0530289 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700290 if (module == NULL) {
291 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
292 device);
293 return INVALID_OPERATION;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700294 }
Sharad Sangle36781612015-05-28 16:15:16 +0530295 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700296 return INVALID_OPERATION;
297 }
298
299 index = mAvailableInputDevices.add(devDesc);
300 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530301 mAvailableInputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700302 } else {
303 return NO_MEMORY;
304 }
Sharad Sangle36781612015-05-28 16:15:16 +0530305
306 // Set connect to HALs
307 AudioParameter param = AudioParameter(devDesc->mAddress);
308 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
309 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
310
311 // Propagate device availability to Engine
312 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700313 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700314
315 // handle input device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700316 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
317 if (index < 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700318 ALOGW("setDeviceConnectionState() device not connected: %d", device);
319 return INVALID_OPERATION;
320 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700321
322 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
323
324 // Set Disconnect to HALs
Sharad Sangle36781612015-05-28 16:15:16 +0530325 AudioParameter param = AudioParameter(devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700326 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
327 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
328
Sharad Sangle36781612015-05-28 16:15:16 +0530329 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700330 mAvailableInputDevices.remove(devDesc);
331
Sharad Sangle36781612015-05-28 16:15:16 +0530332 // Propagate device availability to Engine
333 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700334 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700335
336 default:
337 ALOGE("setDeviceConnectionState() invalid state: %x", state);
338 return BAD_VALUE;
339 }
340
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700341 closeAllInputs();
342
Sharad Sangle36781612015-05-28 16:15:16 +0530343 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700344 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
345 updateCallRouting(newDevice);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700346 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700347
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700348 mpClientInterface->onAudioPortListUpdate();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700349 return NO_ERROR;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700350 } // end if is input device
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700351
352 ALOGW("setDeviceConnectionState() invalid device: %x", device);
353 return BAD_VALUE;
354}
Sharad Sangle36781612015-05-28 16:15:16 +0530355// This function checks for the parameters which can be offloaded.
356// This can be enhanced depending on the capability of the DSP and policy
357// of the system.
358bool AudioPolicyManagerCustom::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700359{
Sharad Sangle36781612015-05-28 16:15:16 +0530360 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
361 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
362 offloadInfo.sample_rate, offloadInfo.channel_mask,
363 offloadInfo.format,
364 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
365 offloadInfo.has_video);
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530366#ifdef VOICE_CONCURRENCY
367 char concpropValue[PROPERTY_VALUE_MAX];
368 if (property_get("voice.playback.conc.disabled", concpropValue, NULL)) {
369 bool propenabled = atoi(concpropValue) || !strncmp("true", concpropValue, 4);
370 if (propenabled) {
371 if (isInCall())
372 {
373 ALOGD("\n copl: blocking compress offload on call mode\n");
374 return false;
375 }
376 }
377 }
378#endif
379#ifdef RECORD_PLAY_CONCURRENCY
380 char recConcPropValue[PROPERTY_VALUE_MAX];
381 bool prop_rec_play_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700382
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530383 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
384 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
385 }
386
387 if ((prop_rec_play_enabled) &&
388 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
389 ALOGD("copl: blocking compress offload for record concurrency");
390 return false;
391 }
392#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530393 // Check if stream type is music, then only allow offload as of now.
394 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
395 {
396 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
397 return false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700398 }
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530399
Alexy Joseph0ac09032015-10-16 15:57:53 -0700400 // Check if offload has been disabled
401 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
402 if (offloadDisabled) {
403 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
404 return false;
405 }
406
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530407 char propValue[PROPERTY_VALUE_MAX];
408 bool pcmOffload = false;
409#ifdef PCM_OFFLOAD_ENABLED
410 if ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM_OFFLOAD) {
411 bool prop_enabled = false;
412 if ((AUDIO_FORMAT_PCM_16_BIT_OFFLOAD == offloadInfo.format) &&
413 property_get("audio.offload.pcm.16bit.enable", propValue, NULL)) {
414 prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
415 }
416
417#ifdef PCM_OFFLOAD_ENABLED_24
418 if ((AUDIO_FORMAT_PCM_24_BIT_OFFLOAD == offloadInfo.format) &&
419 property_get("audio.offload.pcm.24bit.enable", propValue, NULL)) {
420 prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530421 }
422#endif
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530423
424 if (prop_enabled) {
425 ALOGI("PCM offload property is enabled");
426 pcmOffload = true;
427 }
428
429 if (!pcmOffload) {
430 ALOGD("system property not enabled for PCM offload format[%x]",offloadInfo.format);
431 return false;
432 }
433 }
434#endif
435 if (!pcmOffload) {
Alexy Joseph0ac09032015-10-16 15:57:53 -0700436
437 bool compressedOffloadDisabled = property_get_bool("audio.offload.compress.disable", false);
438 if (compressedOffloadDisabled) {
439 ALOGI("compressed offload disabled by audio.offload.compress.disable=%d", compressedOffloadDisabled);
440 return false;
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530441 }
Alexy Joseph0ac09032015-10-16 15:57:53 -0700442
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530443 //check if it's multi-channel AAC (includes sub formats) and FLAC format
444 if ((popcount(offloadInfo.channel_mask) > 2) &&
445 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
446 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS))) {
447 ALOGD("offload disabled for multi-channel AAC,FLAC and VORBIS format");
448 return false;
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530449 }
450
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530451#ifdef AUDIO_EXTN_FORMATS_ENABLED
452 //check if it's multi-channel FLAC/ALAC/WMA format with sample rate > 48k
453 if ((popcount(offloadInfo.channel_mask) > 2) &&
454 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
Manish Dewangana6fc5442015-08-24 20:30:31 +0530455 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) && (offloadInfo.sample_rate > 48000)) ||
456 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) && (offloadInfo.sample_rate > 48000)) ||
457 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) && (offloadInfo.sample_rate > 48000)) ||
458 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS))) {
459 ALOGD("offload disabled for multi-channel FLAC/ALAC/WMA/AAC_ADTS clips with sample rate > 48kHz");
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530460 return false;
461 }
462#endif
463 //TODO: enable audio offloading with video when ready
464 const bool allowOffloadWithVideo =
465 property_get_bool("audio.offload.video", false /* default_value */);
466 if (offloadInfo.has_video && !allowOffloadWithVideo) {
467 ALOGV("isOffloadSupported: has_video == true, returning false");
468 return false;
469 }
Sharad Sangle36781612015-05-28 16:15:16 +0530470 }
471
472 //If duration is less than minimum value defined in property, return false
473 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
474 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
475 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
476 return false;
477 }
478 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
479 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
480 //duration checks only valid for MP3/AAC/ formats,
481 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
482 if ((offloadInfo.format == AUDIO_FORMAT_MP3) ||
483 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530484 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS) ||
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530485#ifdef AUDIO_EXTN_FORMATS_ENABLED
Sharad Sangle36781612015-05-28 16:15:16 +0530486 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
Sharad Sangle36781612015-05-28 16:15:16 +0530487 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) ||
488 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) ||
489 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) ||
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530490 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_APE) ||
Manish Dewangana6fc5442015-08-24 20:30:31 +0530491 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS) ||
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530492#endif
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530493 pcmOffload)
Sharad Sangle36781612015-05-28 16:15:16 +0530494 return false;
495
496 }
497
498 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
499 // creating an offloaded track and tearing it down immediately after start when audioflinger
500 // detects there is an active non offloadable effect.
501 // FIXME: We should check the audio session here but we do not have it in this context.
502 // This may prevent offloading in rare situations where effects are left active by apps
503 // in the background.
504 if (mEffects.isNonOffloadableEffectEnabled()) {
505 return false;
506 }
507 // Check for soundcard status
508 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
509 String8("SND_CARD_STATUS"));
510 AudioParameter result = AudioParameter(valueStr);
511 int isonline = 0;
512 if ((result.getInt(String8("SND_CARD_STATUS"), isonline) == NO_ERROR)
513 && !isonline) {
514 ALOGD("copl: soundcard is offline rejecting offload request");
515 return false;
516 }
517 // See if there is a profile to support this.
518 // AUDIO_DEVICE_NONE
519 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
520 offloadInfo.sample_rate,
521 offloadInfo.format,
522 offloadInfo.channel_mask,
523 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
524 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
525 return (profile != 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700526}
Sharad Sangle36781612015-05-28 16:15:16 +0530527audio_devices_t AudioPolicyManagerCustom::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
528 bool fromCache)
529{
530 audio_devices_t device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700531
Sharad Sangle36781612015-05-28 16:15:16 +0530532 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
533 if (index >= 0) {
534 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
535 if (patchDesc->mUid != mUidCached) {
536 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
537 outputDesc->device(), outputDesc->mPatchHandle);
538 return outputDesc->device();
539 }
540 }
541
542 // check the following by order of priority to request a routing change if necessary:
543 // 1: the strategy enforced audible is active and enforced on the output:
544 // use device for strategy enforced audible
545 // 2: we are in call or the strategy phone is active on the output:
546 // use device for strategy phone
547 // 3: the strategy for enforced audible is active but not enforced on the output:
548 // use the device for strategy enforced audible
549 // 4: the strategy sonification is active on the output:
550 // use device for strategy sonification
551 // 5: the strategy "respectful" sonification is active on the output:
552 // use device for strategy "respectful" sonification
553 // 6: the strategy accessibility is active on the output:
554 // use device for strategy accessibility
555 // 7: the strategy media is active on the output:
556 // use device for strategy media
557 // 8: the strategy DTMF is active on the output:
558 // use device for strategy DTMF
559 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
560 // use device for strategy t-t-s
561 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
562 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
563 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
564 } else if (isInCall() ||
565 isStrategyActive(outputDesc, STRATEGY_PHONE)||
566 isStrategyActive(mPrimaryOutput, STRATEGY_PHONE)) {
567 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
568 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
569 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
570 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)||
571 (isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION)
572 && (!isStrategyActive(mPrimaryOutput,STRATEGY_MEDIA)))) {
573 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530574 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL) ||
575 isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION_RESPECTFUL)) {
Sharad Sangle36781612015-05-28 16:15:16 +0530576 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
577 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
578 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
579 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
580 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
581 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
582 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
583 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
584 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
585 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
586 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
587 }
588
589 ALOGV("getNewOutputDevice() selected device %x", device);
590 return device;
591}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700592void AudioPolicyManagerCustom::setPhoneState(audio_mode_t state)
593{
Sharad Sangle36781612015-05-28 16:15:16 +0530594 ALOGV("setPhoneState() state %d", state);
595 // store previous phone state for management of sonification strategy below
Sharad Sangle4509cef2015-08-19 20:47:12 +0530596 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Sharad Sangle36781612015-05-28 16:15:16 +0530597 int oldState = mEngine->getPhoneState();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700598
Sharad Sangle36781612015-05-28 16:15:16 +0530599 if (mEngine->setPhoneState(state) != NO_ERROR) {
600 ALOGW("setPhoneState() invalid or same state %d", state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700601 return;
602 }
Sharad Sangle36781612015-05-28 16:15:16 +0530603 /// Opens: can these line be executed after the switch of volume curves???
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700604 // if leaving call state, handle special case of active streams
605 // pertaining to sonification strategy see handleIncallSonification()
606 if (isInCall()) {
607 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530608 for (size_t j = 0; j < mOutputs.size(); j++) {
609 audio_io_handle_t curOutput = mOutputs.keyAt(j);
610 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
611 if (stream == AUDIO_STREAM_PATCH) {
612 continue;
613 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530614 handleIncallSonification((audio_stream_type_t)stream, false, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +0530615 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700616 }
Sharad Sangle36781612015-05-28 16:15:16 +0530617
618 // force reevaluating accessibility routing when call starts
619 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700620 }
621
Sharad Sangle36781612015-05-28 16:15:16 +0530622 /**
623 * Switching to or from incall state or switching between telephony and VoIP lead to force
624 * routing command.
625 */
626 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
627 || (is_state_in_call(state) && (state != oldState)));
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700628
629 // check for device and output changes triggered by new phone state
630 checkA2dpSuspend();
631 checkOutputForAllStrategies();
632 updateDevicesAndOutputs();
633
Sharad Sangle36781612015-05-28 16:15:16 +0530634 sp<SwAudioOutputDescriptor> hwOutputDesc = mPrimaryOutput;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530635#ifdef VOICE_CONCURRENCY
636 int voice_call_state = 0;
637 char propValue[PROPERTY_VALUE_MAX];
638 bool prop_playback_enabled = false, prop_rec_enabled=false, prop_voip_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700639
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530640 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
641 prop_playback_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
642 }
643
644 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
645 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
646 }
647
648 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
649 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
650 }
651
652 bool mode_in_call = (AUDIO_MODE_IN_CALL != oldState) && (AUDIO_MODE_IN_CALL == state);
653 //query if it is a actual voice call initiated by telephony
654 if (mode_in_call) {
655 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0, String8("in_call"));
656 AudioParameter result = AudioParameter(valueStr);
657 if (result.getInt(String8("in_call"), voice_call_state) == NO_ERROR)
658 ALOGD("voice_conc:SetPhoneState: Voice call state = %d", voice_call_state);
659 }
660
661 if (mode_in_call && voice_call_state && !mvoice_call_state) {
662 ALOGD("voice_conc:Entering to call mode oldState :: %d state::%d ",
663 oldState, state);
664 mvoice_call_state = voice_call_state;
665 if (prop_rec_enabled) {
666 //Close all active inputs
667 audio_io_handle_t activeInput = mInputs.getActiveInput();
668 if (activeInput != 0) {
669 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
670 switch(activeDesc->mInputSource) {
671 case AUDIO_SOURCE_VOICE_UPLINK:
672 case AUDIO_SOURCE_VOICE_DOWNLINK:
673 case AUDIO_SOURCE_VOICE_CALL:
674 ALOGD("voice_conc:FOUND active input during call active: %d",activeDesc->mInputSource);
675 break;
676
677 case AUDIO_SOURCE_VOICE_COMMUNICATION:
678 if(prop_voip_enabled) {
679 ALOGD("voice_conc:CLOSING VoIP input source on call setup :%d ",activeDesc->mInputSource);
680 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
681 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
682 }
683 break;
684
685 default:
686 ALOGD("voice_conc:CLOSING input on call setup for inputSource: %d",activeDesc->mInputSource);
687 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
688 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
689 break;
690 }
691 }
692 } else if (prop_voip_enabled) {
693 audio_io_handle_t activeInput = mInputs.getActiveInput();
694 if (activeInput != 0) {
695 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
696 if (AUDIO_SOURCE_VOICE_COMMUNICATION == activeDesc->mInputSource) {
697 ALOGD("voice_conc:CLOSING VoIP on call setup : %d",activeDesc->mInputSource);
698 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
699 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
700 }
701 }
702 }
703 if (prop_playback_enabled) {
704 // Move tracks associated to this strategy from previous output to new output
705 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
706 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
707 if (i == AUDIO_STREAM_PATCH) {
708 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
709 continue;
710 }
711 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
712 if ((AUDIO_STREAM_MUSIC == i) ||
713 (AUDIO_STREAM_VOICE_CALL == i) ) {
714 ALOGD("voice_conc:Invalidate stream type %d", i);
715 mpClientInterface->invalidateStream((audio_stream_type_t)i);
716 }
717 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
718 ALOGD("voice_conc:Invalidate stream type %d", i);
719 mpClientInterface->invalidateStream((audio_stream_type_t)i);
720 }
721 }
722 }
723
724 for (size_t i = 0; i < mOutputs.size(); i++) {
725 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
726 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
727 ALOGD("voice_conc:ouput desc / profile is NULL");
728 continue;
729 }
730
731 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
732 if (((!outputDesc->isDuplicated() &&outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY))
733 && prop_playback_enabled) {
734 ALOGD("voice_conc:calling suspendOutput on call mode for primary output");
735 mpClientInterface->suspendOutput(mOutputs.keyAt(i));
736 } //Close compress all sessions
737 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
738 && prop_playback_enabled) {
739 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
740 closeOutput(mOutputs.keyAt(i));
741 }
742 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_VOIP_RX)
743 && prop_voip_enabled) {
744 ALOGD("voice_conc:calling closeOutput on call mode for DIRECT output");
745 closeOutput(mOutputs.keyAt(i));
746 }
747 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
748 if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)
749 && prop_playback_enabled) {
750 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
751 closeOutput(mOutputs.keyAt(i));
752 }
753 }
754 }
755 }
756
757 if ((AUDIO_MODE_IN_CALL == oldState || AUDIO_MODE_IN_COMMUNICATION == oldState) &&
758 (AUDIO_MODE_NORMAL == state) && prop_playback_enabled && mvoice_call_state) {
759 ALOGD("voice_conc:EXITING from call mode oldState :: %d state::%d \n",oldState, state);
760 mvoice_call_state = 0;
761 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
762 //restore PCM (deep-buffer) output after call termination
763 for (size_t i = 0; i < mOutputs.size(); i++) {
764 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
765 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
766 ALOGD("voice_conc:ouput desc / profile is NULL");
767 continue;
768 }
769 if (!outputDesc->isDuplicated() && outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
770 ALOGD("voice_conc:calling restoreOutput after call mode for primary output");
771 mpClientInterface->restoreOutput(mOutputs.keyAt(i));
772 }
773 }
774 }
775 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
776 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
777 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
778 if (i == AUDIO_STREAM_PATCH) {
779 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
780 continue;
781 }
782 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
783 if ((AUDIO_STREAM_MUSIC == i) ||
784 (AUDIO_STREAM_VOICE_CALL == i) ) {
785 mpClientInterface->invalidateStream((audio_stream_type_t)i);
786 }
787 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
788 mpClientInterface->invalidateStream((audio_stream_type_t)i);
789 }
790 }
791 }
792
793#endif
794#ifdef RECORD_PLAY_CONCURRENCY
795 char recConcPropValue[PROPERTY_VALUE_MAX];
796 bool prop_rec_play_enabled = false;
797
798 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
799 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
800 }
801 if (prop_rec_play_enabled) {
802 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
803 ALOGD("phone state changed to MODE_IN_COMM invlaidating music and voice streams");
804 // call invalidate for voice streams, so that it can use deepbuffer with VoIP out device from HAL
805 mpClientInterface->invalidateStream(AUDIO_STREAM_VOICE_CALL);
806 // call invalidate for music, so that compress will fallback to deep-buffer with VoIP out device
807 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
808
809 // close compress output to make sure session will be closed before timeout(60sec)
810 for (size_t i = 0; i < mOutputs.size(); i++) {
811
812 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
813 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
814 ALOGD("ouput desc / profile is NULL");
815 continue;
816 }
817
818 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
819 ALOGD("calling closeOutput on call mode for COMPRESS output");
820 closeOutput(mOutputs.keyAt(i));
821 }
822 }
823 } else if ((oldState == AUDIO_MODE_IN_COMMUNICATION) &&
824 (mEngine->getPhoneState() == AUDIO_MODE_NORMAL)) {
825 // call invalidate for music so that music can fallback to compress
826 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
827 }
828 }
829#endif
830 mPrevPhoneState = oldState;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700831 int delayMs = 0;
832 if (isStateInCall(state)) {
833 nsecs_t sysTime = systemTime();
834 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530835 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700836 // mute media and sonification strategies and delay device switch by the largest
837 // latency of any output where either strategy is active.
838 // This avoid sending the ring tone or music tail into the earpiece or headset.
Sharad Sangle36781612015-05-28 16:15:16 +0530839 if ((isStrategyActive(desc, STRATEGY_MEDIA,
840 SONIFICATION_HEADSET_MUSIC_DELAY,
841 sysTime) ||
842 isStrategyActive(desc, STRATEGY_SONIFICATION,
843 SONIFICATION_HEADSET_MUSIC_DELAY,
844 sysTime)) &&
845 (delayMs < (int)desc->latency()*2)) {
846 delayMs = desc->latency()*2;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700847 }
Sharad Sangle36781612015-05-28 16:15:16 +0530848 setStrategyMute(STRATEGY_MEDIA, true, desc);
849 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700850 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Sharad Sangle36781612015-05-28 16:15:16 +0530851 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
852 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700853 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
854 }
Sharad Sangle36781612015-05-28 16:15:16 +0530855 ALOGV("Setting the delay from %dms to %dms", delayMs,
856 MIN(delayMs, MAX_VOICE_CALL_START_DELAY_MS));
857 delayMs = MIN(delayMs, MAX_VOICE_CALL_START_DELAY_MS);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700858 }
859
Sharad Sangle36781612015-05-28 16:15:16 +0530860 if (hasPrimaryOutput()) {
861 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
862 // the device returned is not necessarily reachable via this output
863 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
864 // force routing command to audio hardware when ending call
865 // even if no device change is needed
866 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
867 rxDevice = mPrimaryOutput->device();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700868 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700869
Sharad Sangle36781612015-05-28 16:15:16 +0530870 if (state == AUDIO_MODE_IN_CALL) {
871 updateCallRouting(rxDevice, delayMs);
872 } else if (oldState == AUDIO_MODE_IN_CALL) {
873 if (mCallRxPatch != 0) {
874 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
875 mCallRxPatch.clear();
876 }
877 if (mCallTxPatch != 0) {
878 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
879 mCallTxPatch.clear();
880 }
881 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
882 } else {
883 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700884 }
885 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530886 //update device for all non-primary outputs
887 for (size_t i = 0; i < mOutputs.size(); i++) {
888 audio_io_handle_t output = mOutputs.keyAt(i);
889 if (output != mPrimaryOutput->mIoHandle) {
890 newDevice = getNewOutputDevice(mOutputs.valueFor(output), false /*fromCache*/);
891 setOutputDevice(mOutputs.valueFor(output), newDevice, (newDevice != AUDIO_DEVICE_NONE));
892 }
893 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700894 // if entering in call state, handle special case of active streams
895 // pertaining to sonification strategy see handleIncallSonification()
896 if (isStateInCall(state)) {
897 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530898 for (size_t j = 0; j < mOutputs.size(); j++) {
899 audio_io_handle_t curOutput = mOutputs.keyAt(j);
900 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
901 if (stream == AUDIO_STREAM_PATCH) {
902 continue;
903 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530904 handleIncallSonification((audio_stream_type_t)stream, true, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +0530905 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700906 }
907 }
908
909 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
910 if (state == AUDIO_MODE_RINGTONE &&
911 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
912 mLimitRingtoneVolume = true;
913 } else {
914 mLimitRingtoneVolume = false;
915 }
916}
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +0530917
918void AudioPolicyManagerCustom::setForceUse(audio_policy_force_use_t usage,
919 audio_policy_forced_cfg_t config)
920{
921 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
922
923 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
924 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
925 return;
926 }
927 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
928 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
929 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
930
931 // check for device and output changes triggered by new force usage
932 checkA2dpSuspend();
933 checkOutputForAllStrategies();
934 updateDevicesAndOutputs();
935 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
936 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
937 updateCallRouting(newDevice);
938 }
939 // Use reverse loop to make sure any low latency usecases (generally tones)
940 // are not routed before non LL usecases (generally music).
941 // We can safely assume that LL output would always have lower index,
942 // and use this work-around to avoid routing of output with music stream
943 // from the context of short lived LL output.
944 // Note: in case output's share backend(HAL sharing is implicit) all outputs
945 // gets routing update while processing first output itself.
946 for (size_t i = mOutputs.size(); i > 0; i--) {
947 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i-1);
948 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
949 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || outputDesc != mPrimaryOutput) {
950 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
951 }
952 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
953 applyStreamVolumes(outputDesc, newDevice, 0, true);
954 }
955 }
956
957 audio_io_handle_t activeInput = mInputs.getActiveInput();
958 if (activeInput != 0) {
959 setInputDevice(activeInput, getNewInputDevice(activeInput));
960 }
961
962}
963
Sharad Sangle36781612015-05-28 16:15:16 +0530964status_t AudioPolicyManagerCustom::stopSource(sp<SwAudioOutputDescriptor> outputDesc,
965 audio_stream_type_t stream,
966 bool forceDeviceUpdate)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700967{
Sharad Sangle36781612015-05-28 16:15:16 +0530968 // always handle stream stop, check which stream type is stopping
969 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700970
Sharad Sangle36781612015-05-28 16:15:16 +0530971 // handle special case for sonification while in call
Sharad Sangle4509cef2015-08-19 20:47:12 +0530972 if (isInCall() && (outputDesc->mRefCount[stream] == 1)) {
Sharad Sangle36781612015-05-28 16:15:16 +0530973 if (outputDesc->isDuplicated()) {
Sharad Sangle4509cef2015-08-19 20:47:12 +0530974 handleIncallSonification(stream, false, false, outputDesc->mOutput1->mIoHandle);
975 handleIncallSonification(stream, false, false, outputDesc->mOutput2->mIoHandle);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700976 }
Sharad Sangle36781612015-05-28 16:15:16 +0530977 handleIncallSonification(stream, false, false, outputDesc->mIoHandle);
978 }
979
980 if (outputDesc->mRefCount[stream] > 0) {
981 // decrement usage count of this stream on the output
982 outputDesc->changeRefCount(stream, -1);
983
984 // store time at which the stream was stopped - see isStreamActive()
985 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
986 outputDesc->mStopTime[stream] = systemTime();
Zhou Song5dcddc92015-09-21 14:36:57 +0800987 audio_devices_t prevDevice = outputDesc->device();
Sharad Sangle36781612015-05-28 16:15:16 +0530988 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
989 // delay the device switch by twice the latency because stopOutput() is executed when
990 // the track stop() command is received and at that time the audio track buffer can
991 // still contain data that needs to be drained. The latency only covers the audio HAL
992 // and kernel buffers. Also the latency does not always include additional delay in the
993 // audio path (audio DSP, CODEC ...)
994 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
995
996 // force restoring the device selection on other active outputs if it differs from the
997 // one being selected for this output
998 for (size_t i = 0; i < mOutputs.size(); i++) {
999 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1000 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1001 if (desc != outputDesc &&
1002 desc->isActive() &&
1003 outputDesc->sharesHwModuleWith(desc) &&
1004 (newDevice != desc->device())) {
Sharad Sangle4509cef2015-08-19 20:47:12 +05301005 audio_devices_t dev = getNewOutputDevice(mOutputs.valueFor(curOutput), false /*fromCache*/);
Zhou Song5dcddc92015-09-21 14:36:57 +08001006 uint32_t delayMs;
1007 if (dev == prevDevice) {
1008 delayMs = 0;
1009 } else {
1010 delayMs = outputDesc->mLatency*2;
1011 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301012 setOutputDevice(desc,
1013 dev,
Sharad Sangle36781612015-05-28 16:15:16 +05301014 true,
Zhou Song5dcddc92015-09-21 14:36:57 +08001015 delayMs);
Sharad Sangle36781612015-05-28 16:15:16 +05301016 }
1017 }
1018 // update the outputs if stopping one with a stream that can affect notification routing
1019 handleNotificationRoutingForStream(stream);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001020 }
Sharad Sangle36781612015-05-28 16:15:16 +05301021 return NO_ERROR;
1022 } else {
1023 ALOGW("stopOutput() refcount is already 0");
1024 return INVALID_OPERATION;
1025 }
1026}
1027status_t AudioPolicyManagerCustom::startSource(sp<SwAudioOutputDescriptor> outputDesc,
1028 audio_stream_type_t stream,
1029 audio_devices_t device,
1030 uint32_t *delayMs)
1031{
1032 // cannot start playback of STREAM_TTS if any other output is being used
1033 uint32_t beaconMuteLatency = 0;
1034
1035 *delayMs = 0;
1036 if (stream == AUDIO_STREAM_TTS) {
1037 ALOGV("\t found BEACON stream");
1038 if (mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
1039 return INVALID_OPERATION;
1040 } else {
1041 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001042 }
Sharad Sangle36781612015-05-28 16:15:16 +05301043 } else {
1044 // some playback other than beacon starts
1045 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1046 }
1047
1048 // increment usage count for this stream on the requested output:
1049 // NOTE that the usage count is the same for duplicated output and hardware output which is
1050 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1051 outputDesc->changeRefCount(stream, 1);
1052
1053 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
1054 // starting an output being rerouted?
1055 if (device == AUDIO_DEVICE_NONE) {
1056 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001057 }
Sharad Sangle36781612015-05-28 16:15:16 +05301058 routing_strategy strategy = getStrategy(stream);
1059 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
1060 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1061 (beaconMuteLatency > 0);
1062 uint32_t waitMs = beaconMuteLatency;
1063 bool force = false;
1064 for (size_t i = 0; i < mOutputs.size(); i++) {
1065 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1066 if (desc != outputDesc) {
1067 // force a device change if any other output is managed by the same hw
1068 // module and has a current device selection that differs from selected device.
1069 // In this case, the audio HAL must receive the new device selection so that it can
1070 // change the device currently selected by the other active output.
1071 if (outputDesc->sharesHwModuleWith(desc) &&
1072 desc->device() != device) {
1073 force = true;
1074 }
1075 // wait for audio on other active outputs to be presented when starting
1076 // a notification so that audio focus effect can propagate, or that a mute/unmute
1077 // event occurred for beacon
1078 uint32_t latency = desc->latency();
1079 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1080 waitMs = latency;
1081 }
1082 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001083 }
Sharad Sangle36781612015-05-28 16:15:16 +05301084 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force);
1085
1086 // handle special case for sonification while in call
1087 if (isInCall()) {
1088 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001089 }
Sharad Sangle36781612015-05-28 16:15:16 +05301090
1091 // apply volume rules for current stream and device if necessary
1092 checkAndSetVolume(stream,
1093 mStreams.valueFor(stream).getVolumeIndex(device),
1094 outputDesc,
1095 device);
1096
1097 // update the outputs if starting an output with a stream that can affect notification
1098 // routing
1099 handleNotificationRoutingForStream(stream);
1100
1101 // force reevaluating accessibility routing when ringtone or alarm starts
1102 if (strategy == STRATEGY_SONIFICATION) {
1103 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1104 }
1105 }
1106 else {
1107 // handle special case for sonification while in call
1108 if (isInCall()) {
1109 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
1110 }
1111 }
1112 return NO_ERROR;
1113}
1114void AudioPolicyManagerCustom::handleIncallSonification(audio_stream_type_t stream,
1115 bool starting, bool stateChange,
1116 audio_io_handle_t output)
1117{
1118 if(!hasPrimaryOutput()) {
1119 return;
1120 }
1121 // no action needed for AUDIO_STREAM_PATCH stream type, it's for internal flinger tracks
1122 if (stream == AUDIO_STREAM_PATCH) {
1123 return;
1124 }
1125 // if the stream pertains to sonification strategy and we are in call we must
1126 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
1127 // in the device used for phone strategy and play the tone if the selected device does not
1128 // interfere with the device used for phone strategy
1129 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
1130 // many times as there are active tracks on the output
1131 const routing_strategy stream_strategy = getStrategy(stream);
1132 if ((stream_strategy == STRATEGY_SONIFICATION) ||
1133 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
1134 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
1135 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
1136 stream, starting, outputDesc->mDevice, stateChange);
1137 if (outputDesc->mRefCount[stream]) {
1138 int muteCount = 1;
1139 if (stateChange) {
1140 muteCount = outputDesc->mRefCount[stream];
1141 }
1142 if (audio_is_low_visibility(stream)) {
1143 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
1144 for (int i = 0; i < muteCount; i++) {
1145 setStreamMute(stream, starting, outputDesc);
1146 }
1147 } else {
1148 ALOGV("handleIncallSonification() high visibility");
1149 if (outputDesc->device() &
1150 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
1151 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
1152 for (int i = 0; i < muteCount; i++) {
1153 setStreamMute(stream, starting, outputDesc);
1154 }
1155 }
1156 if (starting) {
1157 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
1158 AUDIO_STREAM_VOICE_CALL);
1159 } else {
1160 mpClientInterface->stopTone();
1161 }
1162 }
1163 }
1164 }
1165}
1166void AudioPolicyManagerCustom::handleNotificationRoutingForStream(audio_stream_type_t stream) {
1167 switch(stream) {
1168 case AUDIO_STREAM_MUSIC:
1169 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
1170 updateDevicesAndOutputs();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001171 break;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001172 default:
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001173 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001174 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001175}
Sharad Sangle36781612015-05-28 16:15:16 +05301176status_t AudioPolicyManagerCustom::checkAndSetVolume(audio_stream_type_t stream,
1177 int index,
1178 const sp<SwAudioOutputDescriptor>& outputDesc,
1179 audio_devices_t device,
1180 int delayMs, bool force)
1181{
1182 // do not change actual stream volume if the stream is muted
1183 if (outputDesc->mMuteCount[stream] != 0) {
1184 ALOGVV("checkAndSetVolume() stream %d muted count %d",
1185 stream, outputDesc->mMuteCount[stream]);
1186 return NO_ERROR;
1187 }
1188 audio_policy_forced_cfg_t forceUseForComm =
1189 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
1190 // do not change in call volume if bluetooth is connected and vice versa
1191 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
1192 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
1193 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
1194 stream, forceUseForComm);
1195 return INVALID_OPERATION;
1196 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001197
Sharad Sangle36781612015-05-28 16:15:16 +05301198 if (device == AUDIO_DEVICE_NONE) {
1199 device = outputDesc->device();
1200 }
1201
1202 float volumeDb = computeVolume(stream, index, device);
1203 if (outputDesc->isFixedVolume(device)) {
1204 volumeDb = 0.0f;
1205 }
1206
1207 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
1208
1209 if (stream == AUDIO_STREAM_VOICE_CALL ||
1210 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
1211 float voiceVolume;
1212 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
1213 if (stream == AUDIO_STREAM_VOICE_CALL) {
1214 voiceVolume = (float)index/(float)mStreams.valueFor(stream).getVolumeIndexMax();
1215 } else {
1216 voiceVolume = 1.0;
1217 }
1218
1219 if (voiceVolume != mLastVoiceVolume && ((outputDesc == mPrimaryOutput) ||
1220 isDirectOutput(outputDesc->mIoHandle) || device & AUDIO_DEVICE_OUT_ALL_USB)) {
1221 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
1222 mLastVoiceVolume = voiceVolume;
1223 }
1224 }
1225
1226 return NO_ERROR;
1227}
1228bool AudioPolicyManagerCustom::isDirectOutput(audio_io_handle_t output) {
1229 for (size_t i = 0; i < mOutputs.size(); i++) {
1230 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1231 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1232 if ((curOutput == output) && (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
1233 return true;
1234 }
1235 }
1236 return false;
1237}
vivek mehta0ea887a2015-08-26 14:01:20 -07001238
1239status_t AudioPolicyManagerCustom::getOutputForAttr(const audio_attributes_t *attr,
1240 audio_io_handle_t *output,
1241 audio_session_t session,
1242 audio_stream_type_t *stream,
1243 uid_t uid,
1244 uint32_t samplingRate,
1245 audio_format_t format,
1246 audio_channel_mask_t channelMask,
1247 audio_output_flags_t flags,
1248 audio_port_handle_t selectedDeviceId,
1249 const audio_offload_info_t *offloadInfo)
1250{
1251 audio_offload_info_t tOffloadInfo = AUDIO_INFO_INITIALIZER;
1252
Alexy Joseph0ac09032015-10-16 15:57:53 -07001253 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
1254 bool pcmOffloadEnabled = false;
1255
1256 if (offloadDisabled) {
1257 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
1258 }
1259
1260 //read track offload property only if the global offload switch is off.
1261 if (!offloadDisabled) {
1262 pcmOffloadEnabled = property_get_bool("audio.offload.track.enable", false);
1263 }
vivek mehta0ea887a2015-08-26 14:01:20 -07001264
1265 if (offloadInfo == NULL && pcmOffloadEnabled) {
1266 tOffloadInfo.sample_rate = samplingRate;
1267 tOffloadInfo.channel_mask = channelMask;
1268 tOffloadInfo.format = format;
1269 tOffloadInfo.stream_type = *stream;
1270 tOffloadInfo.bit_width = 16; //hard coded for PCM_16
1271 if (attr != NULL) {
1272 ALOGV("found attribute .. setting usage %d ", attr->usage);
1273 tOffloadInfo.usage = attr->usage;
1274 } else {
Alexy Joseph0ac09032015-10-16 15:57:53 -07001275 ALOGI("%s:: attribute is NULL .. no usage set", __func__);
vivek mehta0ea887a2015-08-26 14:01:20 -07001276 }
1277 offloadInfo = &tOffloadInfo;
1278 }
1279
1280 return AudioPolicyManager::getOutputForAttr(attr, output, session, stream,
1281 (uid_t)uid, (uint32_t)samplingRate,
1282 format, (audio_channel_mask_t)channelMask,
1283 flags, (audio_port_handle_t)selectedDeviceId,
1284 offloadInfo);
1285}
1286
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001287audio_io_handle_t AudioPolicyManagerCustom::getOutputForDevice(
1288 audio_devices_t device,
Sharad Sangle36781612015-05-28 16:15:16 +05301289 audio_session_t session __unused,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001290 audio_stream_type_t stream,
1291 uint32_t samplingRate,
1292 audio_format_t format,
1293 audio_channel_mask_t channelMask,
1294 audio_output_flags_t flags,
1295 const audio_offload_info_t *offloadInfo)
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001296{
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001297 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
1298 uint32_t latency = 0;
1299 status_t status;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001300
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001301#ifdef AUDIO_POLICY_TEST
1302 if (mCurOutput != 0) {
1303 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1304 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1305
1306 if (mTestOutputs[mCurOutput] == 0) {
1307 ALOGV("getOutput() opening test output");
Sharad Sangle36781612015-05-28 16:15:16 +05301308 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
1309 mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001310 outputDesc->mDevice = mTestDevice;
1311 outputDesc->mLatency = mTestLatencyMs;
1312 outputDesc->mFlags =
1313 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
1314 outputDesc->mRefCount[stream] = 0;
1315 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1316 config.sample_rate = mTestSamplingRate;
1317 config.channel_mask = mTestChannels;
1318 config.format = mTestFormat;
1319 if (offloadInfo != NULL) {
1320 config.offload_info = *offloadInfo;
1321 }
1322 status = mpClientInterface->openOutput(0,
1323 &mTestOutputs[mCurOutput],
1324 &config,
1325 &outputDesc->mDevice,
1326 String8(""),
1327 &outputDesc->mLatency,
1328 outputDesc->mFlags);
1329 if (status == NO_ERROR) {
1330 outputDesc->mSamplingRate = config.sample_rate;
1331 outputDesc->mFormat = config.format;
1332 outputDesc->mChannelMask = config.channel_mask;
1333 AudioParameter outputCmd = AudioParameter();
1334 outputCmd.addInt(String8("set_id"),mCurOutput);
1335 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1336 addOutput(mTestOutputs[mCurOutput], outputDesc);
1337 }
1338 }
1339 return mTestOutputs[mCurOutput];
1340 }
1341#endif //AUDIO_POLICY_TEST
Sharad Sangle36781612015-05-28 16:15:16 +05301342 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1343 (stream != AUDIO_STREAM_MUSIC)) {
1344 // compress should not be used for non-music streams
1345 ALOGE("Offloading only allowed with music stream");
1346 return 0;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301347 }
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301348
1349 if ((stream == AUDIO_STREAM_VOICE_CALL) &&
1350 (channelMask == 1) &&
1351 (samplingRate == 8000 || samplingRate == 16000)) {
1352 // Allow Voip direct output only if:
1353 // audio mode is MODE_IN_COMMUNCATION; AND
1354 // voip output is not opened already; AND
1355 // requested sample rate matches with that of voip input stream (if opened already)
1356 int value = 0;
1357 uint32_t mode = 0, voipOutCount = 1, voipSampleRate = 1;
1358 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1359 String8("audio_mode"));
1360 AudioParameter result = AudioParameter(valueStr);
1361 if (result.getInt(String8("audio_mode"), value) == NO_ERROR) {
1362 mode = value;
1363 }
1364
1365 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1366 String8("voip_out_stream_count"));
1367 result = AudioParameter(valueStr);
1368 if (result.getInt(String8("voip_out_stream_count"), value) == NO_ERROR) {
1369 voipOutCount = value;
1370 }
1371
1372 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1373 String8("voip_sample_rate"));
1374 result = AudioParameter(valueStr);
1375 if (result.getInt(String8("voip_sample_rate"), value) == NO_ERROR) {
1376 voipSampleRate = value;
1377 }
1378
1379 if ((mode == AUDIO_MODE_IN_COMMUNICATION) && (voipOutCount == 0) &&
1380 ((voipSampleRate == 0) || (voipSampleRate == samplingRate))) {
1381 if (audio_is_linear_pcm(format)) {
1382 char propValue[PROPERTY_VALUE_MAX] = {0};
1383 property_get("use.voice.path.for.pcm.voip", propValue, "0");
1384 bool voipPcmSysPropEnabled = !strncmp("true", propValue, sizeof("true"));
1385 if (voipPcmSysPropEnabled && (format == AUDIO_FORMAT_PCM_16_BIT)) {
1386 flags = (audio_output_flags_t)((flags &~AUDIO_OUTPUT_FLAG_FAST) |
1387 AUDIO_OUTPUT_FLAG_VOIP_RX | AUDIO_OUTPUT_FLAG_DIRECT);
1388 ALOGD("Set VoIP and Direct output flags for PCM format");
1389 }
1390 }
1391 }
1392 }
1393
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301394#ifdef VOICE_CONCURRENCY
1395 char propValue[PROPERTY_VALUE_MAX];
1396 bool prop_play_enabled=false, prop_voip_enabled = false;
1397
1398 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1399 prop_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001400 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301401
1402 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1403 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1404 }
1405
1406 if (prop_play_enabled && mvoice_call_state) {
1407 //check if voice call is active / running in background
1408 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1409 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1410 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1411 {
1412 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1413 if(prop_voip_enabled) {
1414 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1415 flags );
1416 return 0;
1417 }
1418 }
1419 else {
1420 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
1421 ALOGD("voice_conc:IN call mode adding ULL flags .. flags: %x ", flags );
1422 flags = AUDIO_OUTPUT_FLAG_FAST;
1423 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
1424 if (AUDIO_STREAM_MUSIC == stream) {
1425 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1426 ALOGD("voice_conc:IN call mode adding deep-buffer flags %x ", flags );
1427 }
1428 else {
1429 flags = AUDIO_OUTPUT_FLAG_FAST;
1430 ALOGD("voice_conc:IN call mode adding fast flags %x ", flags );
1431 }
1432 }
1433 }
1434 }
1435 } else if (prop_voip_enabled && mvoice_call_state) {
1436 //check if voice call is active / running in background
1437 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1438 //return only ULL ouput
1439 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1440 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1441 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1442 {
1443 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1444 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1445 flags );
1446 return 0;
1447 }
1448 }
1449 }
1450#endif
1451#ifdef RECORD_PLAY_CONCURRENCY
1452 char recConcPropValue[PROPERTY_VALUE_MAX];
1453 bool prop_rec_play_enabled = false;
1454
1455 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
1456 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
1457 }
1458 if ((prop_rec_play_enabled) &&
1459 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
1460 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
1461 if (AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1462 // allow VoIP using voice path
1463 // Do nothing
1464 } else if((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1465 ALOGD("voice_conc:MODE_IN_COMM is setforcing deep buffer output for non ULL... flags: %x", flags);
1466 // use deep buffer path for all non ULL outputs
1467 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1468 }
1469 } else if ((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1470 ALOGD("voice_conc:Record mode is on forcing deep buffer output for non ULL... flags: %x ", flags);
1471 // use deep buffer path for all non ULL outputs
1472 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1473 }
1474 }
1475 if (prop_rec_play_enabled &&
1476 (stream == AUDIO_STREAM_ENFORCED_AUDIBLE)) {
1477 ALOGD("Record conc is on forcing ULL output for ENFORCED_AUDIBLE");
1478 flags = AUDIO_OUTPUT_FLAG_FAST;
1479 }
1480#endif
1481
Sharad Sangle4509cef2015-08-19 20:47:12 +05301482#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Sharad Sangle36781612015-05-28 16:15:16 +05301483 /*
1484 * WFD audio routes back to target speaker when starting a ringtone playback.
1485 * This is because primary output is reused for ringtone, so output device is
1486 * updated based on SONIFICATION strategy for both ringtone and music playback.
1487 * The same issue is not seen on remoted_submix HAL based WFD audio because
1488 * primary output is not reused and a new output is created for ringtone playback.
1489 * Issue is fixed by updating output flag to AUDIO_OUTPUT_FLAG_FAST when there is
1490 * a non-music stream playback on WFD, so primary output is not reused for ringtone.
1491 */
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001492 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
1493 if ((availableOutputDeviceTypes & AUDIO_DEVICE_OUT_PROXY)
1494 && (stream != AUDIO_STREAM_MUSIC)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301495 ALOGD("WFD audio: use OUTPUT_FLAG_FAST for non music stream. flags:%x", flags );
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001496 //For voip paths
1497 if(flags & AUDIO_OUTPUT_FLAG_DIRECT)
1498 flags = AUDIO_OUTPUT_FLAG_DIRECT;
1499 else //route every thing else to ULL path
1500 flags = AUDIO_OUTPUT_FLAG_FAST;
1501 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301502#endif
1503
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001504 // open a direct output if required by specified parameters
vivek mehta0ea887a2015-08-26 14:01:20 -07001505 // force direct flag if offload flag is set: offloading implies a direct output stream
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001506 // and all common behaviors are driven by checking only the direct flag
1507 // this should normally be set appropriately in the policy configuration file
1508 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1509 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1510 }
1511 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1512 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1513 }
vivek mehta0ea887a2015-08-26 14:01:20 -07001514
1515 // Do offload magic here
1516 if ((flags == AUDIO_OUTPUT_FLAG_NONE) && (stream == AUDIO_STREAM_MUSIC) &&
1517 (offloadInfo != NULL) &&
1518 ((offloadInfo->usage == AUDIO_USAGE_MEDIA ||
1519 (offloadInfo->usage == AUDIO_USAGE_GAME)))) {
1520 if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) {
1521 ALOGD("AudioCustomHAL --> Force Direct Flag ..");
1522 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1523 }
1524 }
1525
Sharad Sangle36781612015-05-28 16:15:16 +05301526 // only allow deep buffering for music stream type
1527 if (stream != AUDIO_STREAM_MUSIC) {
1528 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Sharad Sangle497aef82015-08-03 17:55:48 +05301529 } else if (/* stream == AUDIO_STREAM_MUSIC && */
1530 flags == AUDIO_OUTPUT_FLAG_NONE &&
1531 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
1532 flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Sharad Sangle36781612015-05-28 16:15:16 +05301533 }
Sharad Sangle497aef82015-08-03 17:55:48 +05301534
Sharad Sangle36781612015-05-28 16:15:16 +05301535 if (stream == AUDIO_STREAM_TTS) {
1536 flags = AUDIO_OUTPUT_FLAG_TTS;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001537 }
1538
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301539 // open a direct output if required by specified parameters
1540 //force direct flag if offload flag is set: offloading implies a direct output stream
1541 // and all common behaviors are driven by checking only the direct flag
1542 // this should normally be set appropriately in the policy configuration file
1543 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1544 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1545 }
1546 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1547 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1548 }
1549 // only allow deep buffering for music stream type
1550 if (stream != AUDIO_STREAM_MUSIC) {
1551 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
1552 }
1553 if (stream == AUDIO_STREAM_TTS) {
1554 flags = AUDIO_OUTPUT_FLAG_TTS;
1555 }
1556
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001557 sp<IOProfile> profile;
1558
1559 // skip direct output selection if the request can obviously be attached to a mixed output
1560 // and not explicitly requested
1561 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
1562 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
1563 audio_channel_count_from_out_mask(channelMask) <= 2) {
1564 goto non_direct_output;
1565 }
1566
1567 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1568 // creating an offloaded track and tearing it down immediately after start when audioflinger
1569 // detects there is an active non offloadable effect.
1570 // FIXME: We should check the audio session here but we do not have it in this context.
1571 // This may prevent offloading in rare situations where effects are left active by apps
1572 // in the background.
1573
Sharad Sangle36781612015-05-28 16:15:16 +05301574 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
1575 !mEffects.isNonOffloadableEffectEnabled()) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001576 profile = getProfileForDirectOutput(device,
1577 samplingRate,
1578 format,
1579 channelMask,
1580 (audio_output_flags_t)flags);
1581 }
1582
1583 if (profile != 0) {
Sharad Sangle36781612015-05-28 16:15:16 +05301584 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001585
1586 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +05301587 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001588 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1589 outputDesc = desc;
1590 // reuse direct output if currently open and configured with same parameters
1591 if ((samplingRate == outputDesc->mSamplingRate) &&
1592 (format == outputDesc->mFormat) &&
1593 (channelMask == outputDesc->mChannelMask)) {
1594 outputDesc->mDirectOpenCount++;
1595 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1596 return mOutputs.keyAt(i);
1597 }
1598 }
1599 }
1600 // close direct output if currently open and configured with different parameters
1601 if (outputDesc != NULL) {
1602 closeOutput(outputDesc->mIoHandle);
1603 }
Sharad Sangle36781612015-05-28 16:15:16 +05301604
1605 // if the selected profile is offloaded and no offload info was specified,
1606 // create a default one
1607 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
1608 if ((profile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
1609 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1610 defaultOffloadInfo.sample_rate = samplingRate;
1611 defaultOffloadInfo.channel_mask = channelMask;
1612 defaultOffloadInfo.format = format;
1613 defaultOffloadInfo.stream_type = stream;
1614 defaultOffloadInfo.bit_rate = 0;
1615 defaultOffloadInfo.duration_us = -1;
1616 defaultOffloadInfo.has_video = true; // conservative
1617 defaultOffloadInfo.is_streaming = true; // likely
1618 offloadInfo = &defaultOffloadInfo;
1619 }
1620
1621 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001622 outputDesc->mDevice = device;
1623 outputDesc->mLatency = 0;
Sharad Sangle36781612015-05-28 16:15:16 +05301624 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001625 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1626 config.sample_rate = samplingRate;
1627 config.channel_mask = channelMask;
1628 config.format = format;
1629 if (offloadInfo != NULL) {
1630 config.offload_info = *offloadInfo;
1631 }
Sharad Sangle36781612015-05-28 16:15:16 +05301632 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001633 &output,
1634 &config,
1635 &outputDesc->mDevice,
1636 String8(""),
1637 &outputDesc->mLatency,
1638 outputDesc->mFlags);
1639
1640 // only accept an output with the requested parameters
1641 if (status != NO_ERROR ||
1642 (samplingRate != 0 && samplingRate != config.sample_rate) ||
1643 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
1644 (channelMask != 0 && channelMask != config.channel_mask)) {
1645 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1646 "format %d %d, channelMask %04x %04x", output, samplingRate,
1647 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1648 outputDesc->mChannelMask);
1649 if (output != AUDIO_IO_HANDLE_NONE) {
1650 mpClientInterface->closeOutput(output);
1651 }
Sharad Sangle36781612015-05-28 16:15:16 +05301652 // fall back to mixer output if possible when the direct output could not be open
1653 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
1654 goto non_direct_output;
1655 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001656 return AUDIO_IO_HANDLE_NONE;
1657 }
1658 outputDesc->mSamplingRate = config.sample_rate;
1659 outputDesc->mChannelMask = config.channel_mask;
1660 outputDesc->mFormat = config.format;
1661 outputDesc->mRefCount[stream] = 0;
1662 outputDesc->mStopTime[stream] = 0;
1663 outputDesc->mDirectOpenCount = 1;
1664
1665 audio_io_handle_t srcOutput = getOutputForEffect();
1666 addOutput(output, outputDesc);
1667 audio_io_handle_t dstOutput = getOutputForEffect();
1668 if (dstOutput == output) {
1669 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1670 }
1671 mPreviousOutputs = mOutputs;
1672 ALOGV("getOutput() returns new direct output %d", output);
1673 mpClientInterface->onAudioPortListUpdate();
1674 return output;
1675 }
1676
1677non_direct_output:
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001678 // ignoring channel mask due to downmix capability in mixer
1679
1680 // open a non direct output
1681
1682 // for non direct outputs, only PCM is supported
1683 if (audio_is_linear_pcm(format)) {
1684 // get which output is suitable for the specified stream. The actual
1685 // routing change will happen when startOutput() will be called
1686 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1687
1688 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1689 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1690 output = selectOutput(outputs, flags, format);
1691 }
1692 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1693 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1694
vivek mehta0ea887a2015-08-26 14:01:20 -07001695 ALOGV("getOutputForDevice() returns output %d", output);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001696
1697 return output;
1698}
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301699
1700status_t AudioPolicyManagerCustom::getInputForAttr(const audio_attributes_t *attr,
1701 audio_io_handle_t *input,
1702 audio_session_t session,
1703 uid_t uid,
1704 uint32_t samplingRate,
1705 audio_format_t format,
1706 audio_channel_mask_t channelMask,
1707 audio_input_flags_t flags,
1708 audio_port_handle_t selectedDeviceId,
1709 input_type_t *inputType)
1710{
1711 audio_source_t inputSource = attr->source;
1712#ifdef VOICE_CONCURRENCY
1713
1714 char propValue[PROPERTY_VALUE_MAX];
1715 bool prop_rec_enabled=false, prop_voip_enabled = false;
1716
1717 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
1718 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1719 }
1720
1721 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1722 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1723 }
1724
1725 if (prop_rec_enabled && mvoice_call_state) {
1726 //check if voice call is active / running in background
1727 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1728 //Need to block input request
1729 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1730 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1731 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1732 {
1733 switch(inputSource) {
1734 case AUDIO_SOURCE_VOICE_UPLINK:
1735 case AUDIO_SOURCE_VOICE_DOWNLINK:
1736 case AUDIO_SOURCE_VOICE_CALL:
1737 ALOGD("voice_conc:Creating input during incall mode for inputSource: %d",
1738 inputSource);
1739 break;
1740
1741 case AUDIO_SOURCE_VOICE_COMMUNICATION:
1742 if(prop_voip_enabled) {
1743 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1744 inputSource);
1745 return NO_INIT;
1746 }
1747 break;
1748 default:
1749 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1750 inputSource);
1751 return NO_INIT;
1752 }
1753 }
1754 }//check for VoIP flag
1755 else if(prop_voip_enabled && mvoice_call_state) {
1756 //check if voice call is active / running in background
1757 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1758 //Need to block input request
1759 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1760 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1761 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1762 {
1763 if(inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1764 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
1765 return NO_INIT;
1766 }
1767 }
1768 }
1769
1770#endif
1771
1772 return AudioPolicyManager::getInputForAttr(attr,
1773 input,
1774 session,
1775 uid,
1776 samplingRate,
1777 format,
1778 channelMask,
1779 flags,
1780 selectedDeviceId,
1781 inputType);
1782}
1783status_t AudioPolicyManagerCustom::startInput(audio_io_handle_t input,
1784 audio_session_t session)
1785{
1786 ALOGV("startInput() input %d", input);
1787 ssize_t index = mInputs.indexOfKey(input);
1788 if (index < 0) {
1789 ALOGW("startInput() unknown input %d", input);
1790 return BAD_VALUE;
1791 }
1792 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1793
1794 index = inputDesc->mSessions.indexOf(session);
1795 if (index < 0) {
1796 ALOGW("startInput() unknown session %d on input %d", session, input);
1797 return BAD_VALUE;
1798 }
1799
1800 // virtual input devices are compatible with other input devices
1801 if (!is_virtual_input_device(inputDesc->mDevice)) {
1802
1803 // for a non-virtual input device, check if there is another (non-virtual) active input
1804 audio_io_handle_t activeInput = mInputs.getActiveInput();
1805 if (activeInput != 0 && activeInput != input) {
1806
1807 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1808 // otherwise the active input continues and the new input cannot be started.
1809 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
1810 if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
1811 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
1812 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
1813 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
1814 } else {
1815 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
1816 return INVALID_OPERATION;
1817 }
1818 }
1819 }
1820
1821 // Routing?
1822 mInputRoutes.incRouteActivity(session);
1823#ifdef RECORD_PLAY_CONCURRENCY
1824 mIsInputRequestOnProgress = true;
1825
1826 char getPropValue[PROPERTY_VALUE_MAX];
1827 bool prop_rec_play_enabled = false;
1828
1829 if (property_get("rec.playback.conc.disabled", getPropValue, NULL)) {
1830 prop_rec_play_enabled = atoi(getPropValue) || !strncmp("true", getPropValue, 4);
1831 }
1832
1833 if ((prop_rec_play_enabled) &&(mInputs.activeInputsCount() == 0)){
1834 // send update to HAL on record playback concurrency
1835 AudioParameter param = AudioParameter();
1836 param.add(String8("rec_play_conc_on"), String8("true"));
1837 ALOGD("startInput() setParameters rec_play_conc is setting to ON ");
1838 mpClientInterface->setParameters(0, param.toString());
1839
1840 // Call invalidate to reset all opened non ULL audio tracks
1841 // Move tracks associated to this strategy from previous output to new output
1842 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
1843 // Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder)
Sharad Sangle4509cef2015-08-19 20:47:12 +05301844 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE && (i != AUDIO_STREAM_PATCH))) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301845 ALOGD("Invalidate on releaseInput for stream :: %d ", i);
1846 //FIXME see fixme on name change
1847 mpClientInterface->invalidateStream((audio_stream_type_t)i);
1848 }
1849 }
1850 // close compress tracks
1851 for (size_t i = 0; i < mOutputs.size(); i++) {
1852 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
1853 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
1854 ALOGD("ouput desc / profile is NULL");
1855 continue;
1856 }
1857 if (outputDesc->mProfile->mFlags
1858 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1859 // close compress sessions
1860 ALOGD("calling closeOutput on record conc for COMPRESS output");
1861 closeOutput(mOutputs.keyAt(i));
1862 }
1863 }
1864 }
1865#endif
1866
1867 if (inputDesc->mRefCount == 0 || mInputRoutes.hasRouteChanged(session)) {
1868 // if input maps to a dynamic policy with an activity listener, notify of state change
1869 if ((inputDesc->mPolicyMix != NULL)
1870 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1871 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1872 MIX_STATE_MIXING);
1873 }
1874
1875 if (mInputs.activeInputsCount() == 0) {
1876 SoundTrigger::setCaptureState(true);
1877 }
1878 setInputDevice(input, getNewInputDevice(input), true /* force */);
1879
1880 // automatically enable the remote submix output when input is started if not
1881 // used by a policy mix of type MIX_TYPE_RECORDERS
1882 // For remote submix (a virtual device), we open only one input per capture request.
1883 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1884 String8 address = String8("");
1885 if (inputDesc->mPolicyMix == NULL) {
1886 address = String8("0");
1887 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1888 address = inputDesc->mPolicyMix->mRegistrationId;
1889 }
1890 if (address != "") {
1891 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1892 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1893 address, "remote-submix");
1894 }
1895 }
1896 }
1897
1898 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1899
1900 inputDesc->mRefCount++;
1901#ifdef RECORD_PLAY_CONCURRENCY
1902 mIsInputRequestOnProgress = false;
1903#endif
1904 return NO_ERROR;
1905}
1906status_t AudioPolicyManagerCustom::stopInput(audio_io_handle_t input,
1907 audio_session_t session)
1908{
1909 status_t status;
1910 status = AudioPolicyManager::stopInput(input, session);
1911#ifdef RECORD_PLAY_CONCURRENCY
1912 char propValue[PROPERTY_VALUE_MAX];
1913 bool prop_rec_play_enabled = false;
1914
1915 if (property_get("rec.playback.conc.disabled", propValue, NULL)) {
1916 prop_rec_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1917 }
1918
1919 if ((prop_rec_play_enabled) && (mInputs.activeInputsCount() == 0)) {
1920
1921 //send update to HAL on record playback concurrency
1922 AudioParameter param = AudioParameter();
1923 param.add(String8("rec_play_conc_on"), String8("false"));
1924 ALOGD("stopInput() setParameters rec_play_conc is setting to OFF ");
1925 mpClientInterface->setParameters(0, param.toString());
1926
1927 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
1928 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
1929 //Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder stop tone)
1930 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE) && (i != AUDIO_STREAM_PATCH)) {
1931 ALOGD(" Invalidate on stopInput for stream :: %d ", i);
1932 //FIXME see fixme on name change
1933 mpClientInterface->invalidateStream((audio_stream_type_t)i);
1934 }
1935 }
1936 }
1937#endif
1938 return status;
1939}
1940
1941AudioPolicyManagerCustom::AudioPolicyManagerCustom(AudioPolicyClientInterface *clientInterface)
Sharad Sangle4509cef2015-08-19 20:47:12 +05301942 : AudioPolicyManager(clientInterface),
1943 mHdmiAudioDisabled(false),
1944 mHdmiAudioEvent(false),
1945 mPrevPhoneState(0)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301946{
Mingming Yin38ea08c2015-10-05 15:24:04 -07001947 char ssr_enabled[PROPERTY_VALUE_MAX] = {0};
1948 bool prop_ssr_enabled = false;
1949
1950 if (property_get("ro.qc.sdk.audio.ssr", ssr_enabled, NULL)) {
1951 prop_ssr_enabled = atoi(ssr_enabled) || !strncmp("true", ssr_enabled, 4);
1952 }
1953
1954 for (size_t i = 0; i < mHwModules.size(); i++) {
1955 ALOGV("Hw module %d", i);
1956 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) {
1957 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
1958 ALOGV("Input profile ", j);
1959 for (size_t k = 0; k < inProfile->mChannelMasks.size(); k++) {
1960 audio_channel_mask_t channelMask =
1961 inProfile->mChannelMasks.itemAt(k);
1962 ALOGV("Channel Mask %x size %d", channelMask,
1963 inProfile->mChannelMasks.size());
1964 if (AUDIO_CHANNEL_IN_5POINT1 == channelMask) {
1965 if (!prop_ssr_enabled) {
1966 ALOGI("removing AUDIO_CHANNEL_IN_5POINT1 from"
1967 " input profile as SSR(surround sound record)"
1968 " is not supported on this chipset variant");
1969 inProfile->mChannelMasks.removeItemsAt(k, 1);
1970 ALOGV("Channel Mask size now %d",
1971 inProfile->mChannelMasks.size());
1972 }
1973 }
1974 }
1975 }
1976 }
1977
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301978#ifdef RECORD_PLAY_CONCURRENCY
1979 mIsInputRequestOnProgress = false;
1980#endif
1981
1982
1983#ifdef VOICE_CONCURRENCY
1984 mFallBackflag = getFallBackPath();
1985#endif
1986}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001987}