blob: 85a73930f17179382ef8cd9dc0e721f7ab4f6b3c [file] [log] [blame]
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001/*
Sharad Sanglef2e11662015-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 Sanglef2e11662015-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 Sanglef2e11662015-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 Sanglef2e11662015-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 Sanglef2e11662015-05-28 16:15:16 +053052#include <policy.h>
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070053
54namespace android {
Sharad Sangle78d53242015-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 Sangle78d53242015-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 Sanglef2e11662015-05-28 16:15:16 +053086extern "C" AudioPolicyInterface* createAudioPolicyManager(
87 AudioPolicyClientInterface *clientInterface)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070088{
Sharad Sanglef2e11662015-05-28 16:15:16 +053089 return new AudioPolicyManagerCustom(clientInterface);
90}
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070091
Sharad Sanglef2e11662015-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 Sanglef2e11662015-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 Sanglef2e11662015-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 Sangleb53795f2015-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 Sangleb53795f2015-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 Sanglef2e11662015-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 Sanglef2e11662015-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 Sanglef2e11662015-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 Sanglef2e11662015-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 Sanglef2e11662015-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 Sangleb53795f2015-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 Sanglef2e11662015-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 Sangleb53795f2015-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 Sanglef2e11662015-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 Sanglef2e11662015-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 Sanglef2e11662015-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 Sanglef2e11662015-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 Sanglef2e11662015-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 Sanglef2e11662015-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 Sanglef2e11662015-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 Sanglef2e11662015-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 Sanglef2e11662015-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 Sanglef2e11662015-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 Sanglef2e11662015-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 Sanglef2e11662015-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 Sanglef2e11662015-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 Sanglef2e11662015-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 Sanglef2e11662015-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 Sanglef2e11662015-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 Sangle78d53242015-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 Sangle78d53242015-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 Sanglef2e11662015-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 Ranawatc374a422015-07-21 19:30:09 +0530399
400 char propValue[PROPERTY_VALUE_MAX];
401 bool pcmOffload = false;
402#ifdef PCM_OFFLOAD_ENABLED
403 if ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM_OFFLOAD) {
404 bool prop_enabled = false;
405 if ((AUDIO_FORMAT_PCM_16_BIT_OFFLOAD == offloadInfo.format) &&
406 property_get("audio.offload.pcm.16bit.enable", propValue, NULL)) {
407 prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
408 }
409
410#ifdef PCM_OFFLOAD_ENABLED_24
411 if ((AUDIO_FORMAT_PCM_24_BIT_OFFLOAD == offloadInfo.format) &&
412 property_get("audio.offload.pcm.24bit.enable", propValue, NULL)) {
413 prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Sharad Sangle78d53242015-06-04 20:24:10 +0530414 }
415#endif
Preetam Singh Ranawatc374a422015-07-21 19:30:09 +0530416
417 if (prop_enabled) {
418 ALOGI("PCM offload property is enabled");
419 pcmOffload = true;
420 }
421
422 if (!pcmOffload) {
423 ALOGD("system property not enabled for PCM offload format[%x]",offloadInfo.format);
424 return false;
425 }
426 }
427#endif
428 if (!pcmOffload) {
429 // Check if offload has been disabled
430 if (property_get("audio.offload.disable", propValue, "0")) {
431 if (atoi(propValue) != 0) {
432 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
433 return false;
434 }
435 }
436 //check if it's multi-channel AAC (includes sub formats) and FLAC format
437 if ((popcount(offloadInfo.channel_mask) > 2) &&
438 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
439 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS))) {
440 ALOGD("offload disabled for multi-channel AAC,FLAC and VORBIS format");
441 return false;
Satya Krishna Pindiproli4cda5352015-08-12 18:21:25 +0530442 }
443
Preetam Singh Ranawatc374a422015-07-21 19:30:09 +0530444#ifdef AUDIO_EXTN_FORMATS_ENABLED
445 //check if it's multi-channel FLAC/ALAC/WMA format with sample rate > 48k
446 if ((popcount(offloadInfo.channel_mask) > 2) &&
447 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
Manish Dewangan93672f12015-08-24 20:30:31 +0530448 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) && (offloadInfo.sample_rate > 48000)) ||
449 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) && (offloadInfo.sample_rate > 48000)) ||
450 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) && (offloadInfo.sample_rate > 48000)) ||
451 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS))) {
452 ALOGD("offload disabled for multi-channel FLAC/ALAC/WMA/AAC_ADTS clips with sample rate > 48kHz");
Preetam Singh Ranawatc374a422015-07-21 19:30:09 +0530453 return false;
454 }
455#endif
456 //TODO: enable audio offloading with video when ready
457 const bool allowOffloadWithVideo =
458 property_get_bool("audio.offload.video", false /* default_value */);
459 if (offloadInfo.has_video && !allowOffloadWithVideo) {
460 ALOGV("isOffloadSupported: has_video == true, returning false");
461 return false;
462 }
Sharad Sanglef2e11662015-05-28 16:15:16 +0530463 }
464
465 //If duration is less than minimum value defined in property, return false
466 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
467 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
468 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
469 return false;
470 }
471 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
472 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
473 //duration checks only valid for MP3/AAC/ formats,
474 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
475 if ((offloadInfo.format == AUDIO_FORMAT_MP3) ||
476 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
Satya Krishna Pindiproli4cda5352015-08-12 18:21:25 +0530477 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS) ||
Sharad Sangle78d53242015-06-04 20:24:10 +0530478#ifdef AUDIO_EXTN_FORMATS_ENABLED
Sharad Sanglef2e11662015-05-28 16:15:16 +0530479 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
Sharad Sanglef2e11662015-05-28 16:15:16 +0530480 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) ||
481 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) ||
482 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) ||
Satya Krishna Pindiproli4cda5352015-08-12 18:21:25 +0530483 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_APE) ||
Manish Dewangan93672f12015-08-24 20:30:31 +0530484 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS) ||
Sharad Sangle78d53242015-06-04 20:24:10 +0530485#endif
Satya Krishna Pindiproli4cda5352015-08-12 18:21:25 +0530486 pcmOffload)
Sharad Sanglef2e11662015-05-28 16:15:16 +0530487 return false;
488
489 }
490
491 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
492 // creating an offloaded track and tearing it down immediately after start when audioflinger
493 // detects there is an active non offloadable effect.
494 // FIXME: We should check the audio session here but we do not have it in this context.
495 // This may prevent offloading in rare situations where effects are left active by apps
496 // in the background.
497 if (mEffects.isNonOffloadableEffectEnabled()) {
498 return false;
499 }
500 // Check for soundcard status
501 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
502 String8("SND_CARD_STATUS"));
503 AudioParameter result = AudioParameter(valueStr);
504 int isonline = 0;
505 if ((result.getInt(String8("SND_CARD_STATUS"), isonline) == NO_ERROR)
506 && !isonline) {
507 ALOGD("copl: soundcard is offline rejecting offload request");
508 return false;
509 }
510 // See if there is a profile to support this.
511 // AUDIO_DEVICE_NONE
512 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
513 offloadInfo.sample_rate,
514 offloadInfo.format,
515 offloadInfo.channel_mask,
516 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
517 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
518 return (profile != 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700519}
Sharad Sanglef2e11662015-05-28 16:15:16 +0530520audio_devices_t AudioPolicyManagerCustom::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
521 bool fromCache)
522{
523 audio_devices_t device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700524
Sharad Sanglef2e11662015-05-28 16:15:16 +0530525 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
526 if (index >= 0) {
527 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
528 if (patchDesc->mUid != mUidCached) {
529 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
530 outputDesc->device(), outputDesc->mPatchHandle);
531 return outputDesc->device();
532 }
533 }
534
535 // check the following by order of priority to request a routing change if necessary:
536 // 1: the strategy enforced audible is active and enforced on the output:
537 // use device for strategy enforced audible
538 // 2: we are in call or the strategy phone is active on the output:
539 // use device for strategy phone
540 // 3: the strategy for enforced audible is active but not enforced on the output:
541 // use the device for strategy enforced audible
542 // 4: the strategy sonification is active on the output:
543 // use device for strategy sonification
544 // 5: the strategy "respectful" sonification is active on the output:
545 // use device for strategy "respectful" sonification
546 // 6: the strategy accessibility is active on the output:
547 // use device for strategy accessibility
548 // 7: the strategy media is active on the output:
549 // use device for strategy media
550 // 8: the strategy DTMF is active on the output:
551 // use device for strategy DTMF
552 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
553 // use device for strategy t-t-s
554 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
555 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
556 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
557 } else if (isInCall() ||
558 isStrategyActive(outputDesc, STRATEGY_PHONE)||
559 isStrategyActive(mPrimaryOutput, STRATEGY_PHONE)) {
560 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
561 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
562 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
563 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)||
564 (isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION)
565 && (!isStrategyActive(mPrimaryOutput,STRATEGY_MEDIA)))) {
566 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Sharad Sangleb53795f2015-08-19 20:47:12 +0530567 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL) ||
568 isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION_RESPECTFUL)) {
Sharad Sanglef2e11662015-05-28 16:15:16 +0530569 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
570 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
571 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
572 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
573 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
574 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
575 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
576 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
577 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
578 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
579 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
580 }
581
582 ALOGV("getNewOutputDevice() selected device %x", device);
583 return device;
584}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700585void AudioPolicyManagerCustom::setPhoneState(audio_mode_t state)
586{
Sharad Sanglef2e11662015-05-28 16:15:16 +0530587 ALOGV("setPhoneState() state %d", state);
588 // store previous phone state for management of sonification strategy below
Sharad Sangleb53795f2015-08-19 20:47:12 +0530589 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Sharad Sanglef2e11662015-05-28 16:15:16 +0530590 int oldState = mEngine->getPhoneState();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700591
Sharad Sanglef2e11662015-05-28 16:15:16 +0530592 if (mEngine->setPhoneState(state) != NO_ERROR) {
593 ALOGW("setPhoneState() invalid or same state %d", state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700594 return;
595 }
Sharad Sanglef2e11662015-05-28 16:15:16 +0530596 /// Opens: can these line be executed after the switch of volume curves???
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700597 // if leaving call state, handle special case of active streams
598 // pertaining to sonification strategy see handleIncallSonification()
599 if (isInCall()) {
600 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sanglef2e11662015-05-28 16:15:16 +0530601 for (size_t j = 0; j < mOutputs.size(); j++) {
602 audio_io_handle_t curOutput = mOutputs.keyAt(j);
603 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
604 if (stream == AUDIO_STREAM_PATCH) {
605 continue;
606 }
Sharad Sangleb53795f2015-08-19 20:47:12 +0530607 handleIncallSonification((audio_stream_type_t)stream, false, true, curOutput);
Sharad Sanglef2e11662015-05-28 16:15:16 +0530608 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700609 }
Sharad Sanglef2e11662015-05-28 16:15:16 +0530610
611 // force reevaluating accessibility routing when call starts
612 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700613 }
614
Sharad Sanglef2e11662015-05-28 16:15:16 +0530615 /**
616 * Switching to or from incall state or switching between telephony and VoIP lead to force
617 * routing command.
618 */
619 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
620 || (is_state_in_call(state) && (state != oldState)));
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700621
622 // check for device and output changes triggered by new phone state
623 checkA2dpSuspend();
624 checkOutputForAllStrategies();
625 updateDevicesAndOutputs();
626
Sharad Sanglef2e11662015-05-28 16:15:16 +0530627 sp<SwAudioOutputDescriptor> hwOutputDesc = mPrimaryOutput;
Sharad Sangle78d53242015-06-04 20:24:10 +0530628#ifdef VOICE_CONCURRENCY
629 int voice_call_state = 0;
630 char propValue[PROPERTY_VALUE_MAX];
631 bool prop_playback_enabled = false, prop_rec_enabled=false, prop_voip_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700632
Sharad Sangle78d53242015-06-04 20:24:10 +0530633 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
634 prop_playback_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
635 }
636
637 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
638 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
639 }
640
641 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
642 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
643 }
644
645 bool mode_in_call = (AUDIO_MODE_IN_CALL != oldState) && (AUDIO_MODE_IN_CALL == state);
646 //query if it is a actual voice call initiated by telephony
647 if (mode_in_call) {
648 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0, String8("in_call"));
649 AudioParameter result = AudioParameter(valueStr);
650 if (result.getInt(String8("in_call"), voice_call_state) == NO_ERROR)
651 ALOGD("voice_conc:SetPhoneState: Voice call state = %d", voice_call_state);
652 }
653
654 if (mode_in_call && voice_call_state && !mvoice_call_state) {
655 ALOGD("voice_conc:Entering to call mode oldState :: %d state::%d ",
656 oldState, state);
657 mvoice_call_state = voice_call_state;
658 if (prop_rec_enabled) {
659 //Close all active inputs
660 audio_io_handle_t activeInput = mInputs.getActiveInput();
661 if (activeInput != 0) {
662 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
663 switch(activeDesc->mInputSource) {
664 case AUDIO_SOURCE_VOICE_UPLINK:
665 case AUDIO_SOURCE_VOICE_DOWNLINK:
666 case AUDIO_SOURCE_VOICE_CALL:
667 ALOGD("voice_conc:FOUND active input during call active: %d",activeDesc->mInputSource);
668 break;
669
670 case AUDIO_SOURCE_VOICE_COMMUNICATION:
671 if(prop_voip_enabled) {
672 ALOGD("voice_conc:CLOSING VoIP input source on call setup :%d ",activeDesc->mInputSource);
673 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
674 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
675 }
676 break;
677
678 default:
679 ALOGD("voice_conc:CLOSING input on call setup for inputSource: %d",activeDesc->mInputSource);
680 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
681 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
682 break;
683 }
684 }
685 } else if (prop_voip_enabled) {
686 audio_io_handle_t activeInput = mInputs.getActiveInput();
687 if (activeInput != 0) {
688 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
689 if (AUDIO_SOURCE_VOICE_COMMUNICATION == activeDesc->mInputSource) {
690 ALOGD("voice_conc:CLOSING VoIP on call setup : %d",activeDesc->mInputSource);
691 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
692 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
693 }
694 }
695 }
696 if (prop_playback_enabled) {
697 // Move tracks associated to this strategy from previous output to new output
698 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
699 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
700 if (i == AUDIO_STREAM_PATCH) {
701 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
702 continue;
703 }
704 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
705 if ((AUDIO_STREAM_MUSIC == i) ||
706 (AUDIO_STREAM_VOICE_CALL == i) ) {
707 ALOGD("voice_conc:Invalidate stream type %d", i);
708 mpClientInterface->invalidateStream((audio_stream_type_t)i);
709 }
710 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
711 ALOGD("voice_conc:Invalidate stream type %d", i);
712 mpClientInterface->invalidateStream((audio_stream_type_t)i);
713 }
714 }
715 }
716
717 for (size_t i = 0; i < mOutputs.size(); i++) {
718 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
719 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
720 ALOGD("voice_conc:ouput desc / profile is NULL");
721 continue;
722 }
723
724 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
725 if (((!outputDesc->isDuplicated() &&outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY))
726 && prop_playback_enabled) {
727 ALOGD("voice_conc:calling suspendOutput on call mode for primary output");
728 mpClientInterface->suspendOutput(mOutputs.keyAt(i));
729 } //Close compress all sessions
730 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
731 && prop_playback_enabled) {
732 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
733 closeOutput(mOutputs.keyAt(i));
734 }
735 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_VOIP_RX)
736 && prop_voip_enabled) {
737 ALOGD("voice_conc:calling closeOutput on call mode for DIRECT output");
738 closeOutput(mOutputs.keyAt(i));
739 }
740 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
741 if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)
742 && prop_playback_enabled) {
743 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
744 closeOutput(mOutputs.keyAt(i));
745 }
746 }
747 }
748 }
749
750 if ((AUDIO_MODE_IN_CALL == oldState || AUDIO_MODE_IN_COMMUNICATION == oldState) &&
751 (AUDIO_MODE_NORMAL == state) && prop_playback_enabled && mvoice_call_state) {
752 ALOGD("voice_conc:EXITING from call mode oldState :: %d state::%d \n",oldState, state);
753 mvoice_call_state = 0;
754 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
755 //restore PCM (deep-buffer) output after call termination
756 for (size_t i = 0; i < mOutputs.size(); i++) {
757 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
758 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
759 ALOGD("voice_conc:ouput desc / profile is NULL");
760 continue;
761 }
762 if (!outputDesc->isDuplicated() && outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
763 ALOGD("voice_conc:calling restoreOutput after call mode for primary output");
764 mpClientInterface->restoreOutput(mOutputs.keyAt(i));
765 }
766 }
767 }
768 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
769 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
770 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
771 if (i == AUDIO_STREAM_PATCH) {
772 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
773 continue;
774 }
775 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
776 if ((AUDIO_STREAM_MUSIC == i) ||
777 (AUDIO_STREAM_VOICE_CALL == i) ) {
778 mpClientInterface->invalidateStream((audio_stream_type_t)i);
779 }
780 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
781 mpClientInterface->invalidateStream((audio_stream_type_t)i);
782 }
783 }
784 }
785
786#endif
787#ifdef RECORD_PLAY_CONCURRENCY
788 char recConcPropValue[PROPERTY_VALUE_MAX];
789 bool prop_rec_play_enabled = false;
790
791 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
792 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
793 }
794 if (prop_rec_play_enabled) {
795 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
796 ALOGD("phone state changed to MODE_IN_COMM invlaidating music and voice streams");
797 // call invalidate for voice streams, so that it can use deepbuffer with VoIP out device from HAL
798 mpClientInterface->invalidateStream(AUDIO_STREAM_VOICE_CALL);
799 // call invalidate for music, so that compress will fallback to deep-buffer with VoIP out device
800 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
801
802 // close compress output to make sure session will be closed before timeout(60sec)
803 for (size_t i = 0; i < mOutputs.size(); i++) {
804
805 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
806 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
807 ALOGD("ouput desc / profile is NULL");
808 continue;
809 }
810
811 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
812 ALOGD("calling closeOutput on call mode for COMPRESS output");
813 closeOutput(mOutputs.keyAt(i));
814 }
815 }
816 } else if ((oldState == AUDIO_MODE_IN_COMMUNICATION) &&
817 (mEngine->getPhoneState() == AUDIO_MODE_NORMAL)) {
818 // call invalidate for music so that music can fallback to compress
819 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
820 }
821 }
822#endif
823 mPrevPhoneState = oldState;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700824 int delayMs = 0;
825 if (isStateInCall(state)) {
826 nsecs_t sysTime = systemTime();
827 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sanglef2e11662015-05-28 16:15:16 +0530828 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700829 // mute media and sonification strategies and delay device switch by the largest
830 // latency of any output where either strategy is active.
831 // This avoid sending the ring tone or music tail into the earpiece or headset.
Sharad Sanglef2e11662015-05-28 16:15:16 +0530832 if ((isStrategyActive(desc, STRATEGY_MEDIA,
833 SONIFICATION_HEADSET_MUSIC_DELAY,
834 sysTime) ||
835 isStrategyActive(desc, STRATEGY_SONIFICATION,
836 SONIFICATION_HEADSET_MUSIC_DELAY,
837 sysTime)) &&
838 (delayMs < (int)desc->latency()*2)) {
839 delayMs = desc->latency()*2;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700840 }
Sharad Sanglef2e11662015-05-28 16:15:16 +0530841 setStrategyMute(STRATEGY_MEDIA, true, desc);
842 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700843 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Sharad Sanglef2e11662015-05-28 16:15:16 +0530844 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
845 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700846 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
847 }
Sharad Sanglef2e11662015-05-28 16:15:16 +0530848 ALOGV("Setting the delay from %dms to %dms", delayMs,
849 MIN(delayMs, MAX_VOICE_CALL_START_DELAY_MS));
850 delayMs = MIN(delayMs, MAX_VOICE_CALL_START_DELAY_MS);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700851 }
852
Sharad Sanglef2e11662015-05-28 16:15:16 +0530853 if (hasPrimaryOutput()) {
854 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
855 // the device returned is not necessarily reachable via this output
856 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
857 // force routing command to audio hardware when ending call
858 // even if no device change is needed
859 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
860 rxDevice = mPrimaryOutput->device();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700861 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700862
Sharad Sanglef2e11662015-05-28 16:15:16 +0530863 if (state == AUDIO_MODE_IN_CALL) {
864 updateCallRouting(rxDevice, delayMs);
865 } else if (oldState == AUDIO_MODE_IN_CALL) {
866 if (mCallRxPatch != 0) {
867 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
868 mCallRxPatch.clear();
869 }
870 if (mCallTxPatch != 0) {
871 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
872 mCallTxPatch.clear();
873 }
874 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
875 } else {
876 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700877 }
878 }
Sharad Sangleb53795f2015-08-19 20:47:12 +0530879 //update device for all non-primary outputs
880 for (size_t i = 0; i < mOutputs.size(); i++) {
881 audio_io_handle_t output = mOutputs.keyAt(i);
882 if (output != mPrimaryOutput->mIoHandle) {
883 newDevice = getNewOutputDevice(mOutputs.valueFor(output), false /*fromCache*/);
884 setOutputDevice(mOutputs.valueFor(output), newDevice, (newDevice != AUDIO_DEVICE_NONE));
885 }
886 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700887 // if entering in call state, handle special case of active streams
888 // pertaining to sonification strategy see handleIncallSonification()
889 if (isStateInCall(state)) {
890 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sanglef2e11662015-05-28 16:15:16 +0530891 for (size_t j = 0; j < mOutputs.size(); j++) {
892 audio_io_handle_t curOutput = mOutputs.keyAt(j);
893 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
894 if (stream == AUDIO_STREAM_PATCH) {
895 continue;
896 }
Sharad Sangleb53795f2015-08-19 20:47:12 +0530897 handleIncallSonification((audio_stream_type_t)stream, true, true, curOutput);
Sharad Sanglef2e11662015-05-28 16:15:16 +0530898 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700899 }
900 }
901
902 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
903 if (state == AUDIO_MODE_RINGTONE &&
904 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
905 mLimitRingtoneVolume = true;
906 } else {
907 mLimitRingtoneVolume = false;
908 }
909}
Dhananjay Kumar22b33982015-09-16 19:44:33 +0530910
911void AudioPolicyManagerCustom::setForceUse(audio_policy_force_use_t usage,
912 audio_policy_forced_cfg_t config)
913{
914 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
915
916 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
917 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
918 return;
919 }
920 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
921 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
922 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
923
924 // check for device and output changes triggered by new force usage
925 checkA2dpSuspend();
926 checkOutputForAllStrategies();
927 updateDevicesAndOutputs();
928 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
929 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
930 updateCallRouting(newDevice);
931 }
932 // Use reverse loop to make sure any low latency usecases (generally tones)
933 // are not routed before non LL usecases (generally music).
934 // We can safely assume that LL output would always have lower index,
935 // and use this work-around to avoid routing of output with music stream
936 // from the context of short lived LL output.
937 // Note: in case output's share backend(HAL sharing is implicit) all outputs
938 // gets routing update while processing first output itself.
939 for (size_t i = mOutputs.size(); i > 0; i--) {
940 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i-1);
941 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
942 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || outputDesc != mPrimaryOutput) {
943 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
944 }
945 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
946 applyStreamVolumes(outputDesc, newDevice, 0, true);
947 }
948 }
949
950 audio_io_handle_t activeInput = mInputs.getActiveInput();
951 if (activeInput != 0) {
952 setInputDevice(activeInput, getNewInputDevice(activeInput));
953 }
954
955}
956
Ramjee Singh27fb4052015-09-10 12:10:38 +0530957status_t AudioPolicyManagerCustom::stopOutput(audio_io_handle_t output,
958 audio_stream_type_t stream,
959 audio_session_t session)
960{
961 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
962 ssize_t index = mOutputs.indexOfKey(output);
963 if (index < 0) {
964 ALOGW("stopOutput() unknown output %d", output);
965 return BAD_VALUE;
966 }
967
968 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
969
970 if (outputDesc->mRefCount[stream] == 1) {
971 // Automatically disable the remote submix input when output is stopped on a
972 // re routing mix of type MIX_TYPE_RECORDERS
973 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
974 outputDesc->mPolicyMix != NULL &&
975 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
976 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
977 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
978 outputDesc->mPolicyMix->mRegistrationId,
979 "remote-submix");
980 }
981 }
982
983 // Routing?
984 bool forceDeviceUpdate = false;
985 if (outputDesc->mRefCount[stream] > 0) {
986 int activityCount = mOutputRoutes.decRouteActivity(session);
987 forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0));
988
989 if (forceDeviceUpdate) {
990 checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE);
991 }
992 }
993
994 return stopSource(outputDesc, stream, forceDeviceUpdate);
995}
996
Sharad Sanglef2e11662015-05-28 16:15:16 +0530997status_t AudioPolicyManagerCustom::stopSource(sp<SwAudioOutputDescriptor> outputDesc,
998 audio_stream_type_t stream,
999 bool forceDeviceUpdate)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001000{
Sharad Sanglef2e11662015-05-28 16:15:16 +05301001 // always handle stream stop, check which stream type is stopping
1002 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001003
Sharad Sanglef2e11662015-05-28 16:15:16 +05301004 // handle special case for sonification while in call
Sharad Sangleb53795f2015-08-19 20:47:12 +05301005 if (isInCall() && (outputDesc->mRefCount[stream] == 1)) {
Sharad Sanglef2e11662015-05-28 16:15:16 +05301006 if (outputDesc->isDuplicated()) {
Sharad Sangleb53795f2015-08-19 20:47:12 +05301007 handleIncallSonification(stream, false, false, outputDesc->mOutput1->mIoHandle);
1008 handleIncallSonification(stream, false, false, outputDesc->mOutput2->mIoHandle);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001009 }
Sharad Sanglef2e11662015-05-28 16:15:16 +05301010 handleIncallSonification(stream, false, false, outputDesc->mIoHandle);
1011 }
1012
1013 if (outputDesc->mRefCount[stream] > 0) {
1014 // decrement usage count of this stream on the output
1015 outputDesc->changeRefCount(stream, -1);
1016
1017 // store time at which the stream was stopped - see isStreamActive()
1018 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
1019 outputDesc->mStopTime[stream] = systemTime();
Zhou Songfab75972015-09-21 14:36:57 +08001020 audio_devices_t prevDevice = outputDesc->device();
Sharad Sanglef2e11662015-05-28 16:15:16 +05301021 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
1022 // delay the device switch by twice the latency because stopOutput() is executed when
1023 // the track stop() command is received and at that time the audio track buffer can
1024 // still contain data that needs to be drained. The latency only covers the audio HAL
1025 // and kernel buffers. Also the latency does not always include additional delay in the
1026 // audio path (audio DSP, CODEC ...)
1027 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
1028
1029 // force restoring the device selection on other active outputs if it differs from the
1030 // one being selected for this output
1031 for (size_t i = 0; i < mOutputs.size(); i++) {
1032 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1033 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1034 if (desc != outputDesc &&
1035 desc->isActive() &&
1036 outputDesc->sharesHwModuleWith(desc) &&
1037 (newDevice != desc->device())) {
Sharad Sangleb53795f2015-08-19 20:47:12 +05301038 audio_devices_t dev = getNewOutputDevice(mOutputs.valueFor(curOutput), false /*fromCache*/);
Zhou Songfab75972015-09-21 14:36:57 +08001039 uint32_t delayMs;
1040 if (dev == prevDevice) {
1041 delayMs = 0;
1042 } else {
1043 delayMs = outputDesc->mLatency*2;
1044 }
Sharad Sangleb53795f2015-08-19 20:47:12 +05301045 setOutputDevice(desc,
1046 dev,
Sharad Sanglef2e11662015-05-28 16:15:16 +05301047 true,
Zhou Songfab75972015-09-21 14:36:57 +08001048 delayMs);
Sharad Sanglef2e11662015-05-28 16:15:16 +05301049 }
1050 }
1051 // update the outputs if stopping one with a stream that can affect notification routing
1052 handleNotificationRoutingForStream(stream);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001053 }
Sharad Sanglef2e11662015-05-28 16:15:16 +05301054 return NO_ERROR;
1055 } else {
1056 ALOGW("stopOutput() refcount is already 0");
1057 return INVALID_OPERATION;
1058 }
1059}
1060status_t AudioPolicyManagerCustom::startSource(sp<SwAudioOutputDescriptor> outputDesc,
1061 audio_stream_type_t stream,
1062 audio_devices_t device,
1063 uint32_t *delayMs)
1064{
1065 // cannot start playback of STREAM_TTS if any other output is being used
1066 uint32_t beaconMuteLatency = 0;
1067
1068 *delayMs = 0;
1069 if (stream == AUDIO_STREAM_TTS) {
1070 ALOGV("\t found BEACON stream");
1071 if (mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
1072 return INVALID_OPERATION;
1073 } else {
1074 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001075 }
Sharad Sanglef2e11662015-05-28 16:15:16 +05301076 } else {
1077 // some playback other than beacon starts
1078 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1079 }
1080
1081 // increment usage count for this stream on the requested output:
1082 // NOTE that the usage count is the same for duplicated output and hardware output which is
1083 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1084 outputDesc->changeRefCount(stream, 1);
1085
1086 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
1087 // starting an output being rerouted?
1088 if (device == AUDIO_DEVICE_NONE) {
1089 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001090 }
Sharad Sanglef2e11662015-05-28 16:15:16 +05301091 routing_strategy strategy = getStrategy(stream);
1092 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
1093 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1094 (beaconMuteLatency > 0);
1095 uint32_t waitMs = beaconMuteLatency;
1096 bool force = false;
1097 for (size_t i = 0; i < mOutputs.size(); i++) {
1098 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1099 if (desc != outputDesc) {
1100 // force a device change if any other output is managed by the same hw
1101 // module and has a current device selection that differs from selected device.
1102 // In this case, the audio HAL must receive the new device selection so that it can
1103 // change the device currently selected by the other active output.
1104 if (outputDesc->sharesHwModuleWith(desc) &&
1105 desc->device() != device) {
1106 force = true;
1107 }
1108 // wait for audio on other active outputs to be presented when starting
1109 // a notification so that audio focus effect can propagate, or that a mute/unmute
1110 // event occurred for beacon
1111 uint32_t latency = desc->latency();
1112 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1113 waitMs = latency;
1114 }
1115 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001116 }
Sharad Sanglef2e11662015-05-28 16:15:16 +05301117 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force);
1118
1119 // handle special case for sonification while in call
1120 if (isInCall()) {
1121 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001122 }
Sharad Sanglef2e11662015-05-28 16:15:16 +05301123
1124 // apply volume rules for current stream and device if necessary
1125 checkAndSetVolume(stream,
1126 mStreams.valueFor(stream).getVolumeIndex(device),
1127 outputDesc,
1128 device);
1129
1130 // update the outputs if starting an output with a stream that can affect notification
1131 // routing
1132 handleNotificationRoutingForStream(stream);
1133
1134 // force reevaluating accessibility routing when ringtone or alarm starts
1135 if (strategy == STRATEGY_SONIFICATION) {
1136 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1137 }
1138 }
1139 else {
1140 // handle special case for sonification while in call
1141 if (isInCall()) {
1142 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
1143 }
1144 }
1145 return NO_ERROR;
1146}
1147void AudioPolicyManagerCustom::handleIncallSonification(audio_stream_type_t stream,
1148 bool starting, bool stateChange,
1149 audio_io_handle_t output)
1150{
1151 if(!hasPrimaryOutput()) {
1152 return;
1153 }
1154 // no action needed for AUDIO_STREAM_PATCH stream type, it's for internal flinger tracks
1155 if (stream == AUDIO_STREAM_PATCH) {
1156 return;
1157 }
1158 // if the stream pertains to sonification strategy and we are in call we must
1159 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
1160 // in the device used for phone strategy and play the tone if the selected device does not
1161 // interfere with the device used for phone strategy
1162 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
1163 // many times as there are active tracks on the output
1164 const routing_strategy stream_strategy = getStrategy(stream);
1165 if ((stream_strategy == STRATEGY_SONIFICATION) ||
1166 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
1167 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
1168 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
1169 stream, starting, outputDesc->mDevice, stateChange);
1170 if (outputDesc->mRefCount[stream]) {
1171 int muteCount = 1;
1172 if (stateChange) {
1173 muteCount = outputDesc->mRefCount[stream];
1174 }
1175 if (audio_is_low_visibility(stream)) {
1176 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
1177 for (int i = 0; i < muteCount; i++) {
1178 setStreamMute(stream, starting, outputDesc);
1179 }
1180 } else {
1181 ALOGV("handleIncallSonification() high visibility");
1182 if (outputDesc->device() &
1183 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
1184 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
1185 for (int i = 0; i < muteCount; i++) {
1186 setStreamMute(stream, starting, outputDesc);
1187 }
1188 }
1189 if (starting) {
1190 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
1191 AUDIO_STREAM_VOICE_CALL);
1192 } else {
1193 mpClientInterface->stopTone();
1194 }
1195 }
1196 }
1197 }
1198}
1199void AudioPolicyManagerCustom::handleNotificationRoutingForStream(audio_stream_type_t stream) {
1200 switch(stream) {
1201 case AUDIO_STREAM_MUSIC:
1202 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
1203 updateDevicesAndOutputs();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001204 break;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001205 default:
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001206 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001207 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001208}
Sharad Sanglef2e11662015-05-28 16:15:16 +05301209status_t AudioPolicyManagerCustom::checkAndSetVolume(audio_stream_type_t stream,
1210 int index,
1211 const sp<SwAudioOutputDescriptor>& outputDesc,
1212 audio_devices_t device,
1213 int delayMs, bool force)
1214{
1215 // do not change actual stream volume if the stream is muted
1216 if (outputDesc->mMuteCount[stream] != 0) {
1217 ALOGVV("checkAndSetVolume() stream %d muted count %d",
1218 stream, outputDesc->mMuteCount[stream]);
1219 return NO_ERROR;
1220 }
1221 audio_policy_forced_cfg_t forceUseForComm =
1222 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
1223 // do not change in call volume if bluetooth is connected and vice versa
1224 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
1225 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
1226 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
1227 stream, forceUseForComm);
1228 return INVALID_OPERATION;
1229 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001230
Sharad Sanglef2e11662015-05-28 16:15:16 +05301231 if (device == AUDIO_DEVICE_NONE) {
1232 device = outputDesc->device();
1233 }
1234
1235 float volumeDb = computeVolume(stream, index, device);
1236 if (outputDesc->isFixedVolume(device)) {
1237 volumeDb = 0.0f;
1238 }
1239
1240 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
1241
1242 if (stream == AUDIO_STREAM_VOICE_CALL ||
1243 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
1244 float voiceVolume;
1245 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
1246 if (stream == AUDIO_STREAM_VOICE_CALL) {
1247 voiceVolume = (float)index/(float)mStreams.valueFor(stream).getVolumeIndexMax();
1248 } else {
1249 voiceVolume = 1.0;
1250 }
1251
1252 if (voiceVolume != mLastVoiceVolume && ((outputDesc == mPrimaryOutput) ||
1253 isDirectOutput(outputDesc->mIoHandle) || device & AUDIO_DEVICE_OUT_ALL_USB)) {
1254 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
1255 mLastVoiceVolume = voiceVolume;
1256 }
1257 }
1258
1259 return NO_ERROR;
1260}
1261bool AudioPolicyManagerCustom::isDirectOutput(audio_io_handle_t output) {
1262 for (size_t i = 0; i < mOutputs.size(); i++) {
1263 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1264 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1265 if ((curOutput == output) && (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
1266 return true;
1267 }
1268 }
1269 return false;
1270}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001271audio_io_handle_t AudioPolicyManagerCustom::getOutputForDevice(
1272 audio_devices_t device,
Sharad Sanglef2e11662015-05-28 16:15:16 +05301273 audio_session_t session __unused,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001274 audio_stream_type_t stream,
1275 uint32_t samplingRate,
1276 audio_format_t format,
1277 audio_channel_mask_t channelMask,
1278 audio_output_flags_t flags,
1279 const audio_offload_info_t *offloadInfo)
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001280{
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001281 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
1282 uint32_t latency = 0;
1283 status_t status;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001284
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001285#ifdef AUDIO_POLICY_TEST
1286 if (mCurOutput != 0) {
1287 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1288 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1289
1290 if (mTestOutputs[mCurOutput] == 0) {
1291 ALOGV("getOutput() opening test output");
Sharad Sanglef2e11662015-05-28 16:15:16 +05301292 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
1293 mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001294 outputDesc->mDevice = mTestDevice;
1295 outputDesc->mLatency = mTestLatencyMs;
1296 outputDesc->mFlags =
1297 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
1298 outputDesc->mRefCount[stream] = 0;
1299 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1300 config.sample_rate = mTestSamplingRate;
1301 config.channel_mask = mTestChannels;
1302 config.format = mTestFormat;
1303 if (offloadInfo != NULL) {
1304 config.offload_info = *offloadInfo;
1305 }
1306 status = mpClientInterface->openOutput(0,
1307 &mTestOutputs[mCurOutput],
1308 &config,
1309 &outputDesc->mDevice,
1310 String8(""),
1311 &outputDesc->mLatency,
1312 outputDesc->mFlags);
1313 if (status == NO_ERROR) {
1314 outputDesc->mSamplingRate = config.sample_rate;
1315 outputDesc->mFormat = config.format;
1316 outputDesc->mChannelMask = config.channel_mask;
1317 AudioParameter outputCmd = AudioParameter();
1318 outputCmd.addInt(String8("set_id"),mCurOutput);
1319 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1320 addOutput(mTestOutputs[mCurOutput], outputDesc);
1321 }
1322 }
1323 return mTestOutputs[mCurOutput];
1324 }
1325#endif //AUDIO_POLICY_TEST
Sharad Sanglef2e11662015-05-28 16:15:16 +05301326 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1327 (stream != AUDIO_STREAM_MUSIC)) {
1328 // compress should not be used for non-music streams
1329 ALOGE("Offloading only allowed with music stream");
1330 return 0;
Sharad Sangle78d53242015-06-04 20:24:10 +05301331 }
Karthik Reddy Katta707ff772015-07-14 16:05:18 +05301332
1333 if ((stream == AUDIO_STREAM_VOICE_CALL) &&
1334 (channelMask == 1) &&
1335 (samplingRate == 8000 || samplingRate == 16000)) {
1336 // Allow Voip direct output only if:
1337 // audio mode is MODE_IN_COMMUNCATION; AND
1338 // voip output is not opened already; AND
1339 // requested sample rate matches with that of voip input stream (if opened already)
1340 int value = 0;
1341 uint32_t mode = 0, voipOutCount = 1, voipSampleRate = 1;
1342 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1343 String8("audio_mode"));
1344 AudioParameter result = AudioParameter(valueStr);
1345 if (result.getInt(String8("audio_mode"), value) == NO_ERROR) {
1346 mode = value;
1347 }
1348
1349 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1350 String8("voip_out_stream_count"));
1351 result = AudioParameter(valueStr);
1352 if (result.getInt(String8("voip_out_stream_count"), value) == NO_ERROR) {
1353 voipOutCount = value;
1354 }
1355
1356 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1357 String8("voip_sample_rate"));
1358 result = AudioParameter(valueStr);
1359 if (result.getInt(String8("voip_sample_rate"), value) == NO_ERROR) {
1360 voipSampleRate = value;
1361 }
1362
1363 if ((mode == AUDIO_MODE_IN_COMMUNICATION) && (voipOutCount == 0) &&
1364 ((voipSampleRate == 0) || (voipSampleRate == samplingRate))) {
1365 if (audio_is_linear_pcm(format)) {
1366 char propValue[PROPERTY_VALUE_MAX] = {0};
1367 property_get("use.voice.path.for.pcm.voip", propValue, "0");
1368 bool voipPcmSysPropEnabled = !strncmp("true", propValue, sizeof("true"));
1369 if (voipPcmSysPropEnabled && (format == AUDIO_FORMAT_PCM_16_BIT)) {
1370 flags = (audio_output_flags_t)((flags &~AUDIO_OUTPUT_FLAG_FAST) |
1371 AUDIO_OUTPUT_FLAG_VOIP_RX | AUDIO_OUTPUT_FLAG_DIRECT);
1372 ALOGD("Set VoIP and Direct output flags for PCM format");
1373 }
1374 }
1375 }
1376 }
1377
Sharad Sangle78d53242015-06-04 20:24:10 +05301378#ifdef VOICE_CONCURRENCY
1379 char propValue[PROPERTY_VALUE_MAX];
1380 bool prop_play_enabled=false, prop_voip_enabled = false;
1381
1382 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1383 prop_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001384 }
Sharad Sangle78d53242015-06-04 20:24:10 +05301385
1386 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1387 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1388 }
1389
1390 if (prop_play_enabled && mvoice_call_state) {
1391 //check if voice call is active / running in background
1392 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1393 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1394 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1395 {
1396 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1397 if(prop_voip_enabled) {
1398 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1399 flags );
1400 return 0;
1401 }
1402 }
1403 else {
1404 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
1405 ALOGD("voice_conc:IN call mode adding ULL flags .. flags: %x ", flags );
1406 flags = AUDIO_OUTPUT_FLAG_FAST;
1407 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
1408 if (AUDIO_STREAM_MUSIC == stream) {
1409 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1410 ALOGD("voice_conc:IN call mode adding deep-buffer flags %x ", flags );
1411 }
1412 else {
1413 flags = AUDIO_OUTPUT_FLAG_FAST;
1414 ALOGD("voice_conc:IN call mode adding fast flags %x ", flags );
1415 }
1416 }
1417 }
1418 }
1419 } else if (prop_voip_enabled && mvoice_call_state) {
1420 //check if voice call is active / running in background
1421 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1422 //return only ULL ouput
1423 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1424 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1425 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1426 {
1427 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1428 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1429 flags );
1430 return 0;
1431 }
1432 }
1433 }
1434#endif
1435#ifdef RECORD_PLAY_CONCURRENCY
1436 char recConcPropValue[PROPERTY_VALUE_MAX];
1437 bool prop_rec_play_enabled = false;
1438
1439 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
1440 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
1441 }
1442 if ((prop_rec_play_enabled) &&
1443 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
1444 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
1445 if (AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1446 // allow VoIP using voice path
1447 // Do nothing
1448 } else if((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1449 ALOGD("voice_conc:MODE_IN_COMM is setforcing deep buffer output for non ULL... flags: %x", flags);
1450 // use deep buffer path for all non ULL outputs
1451 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1452 }
1453 } else if ((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1454 ALOGD("voice_conc:Record mode is on forcing deep buffer output for non ULL... flags: %x ", flags);
1455 // use deep buffer path for all non ULL outputs
1456 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1457 }
1458 }
1459 if (prop_rec_play_enabled &&
1460 (stream == AUDIO_STREAM_ENFORCED_AUDIBLE)) {
1461 ALOGD("Record conc is on forcing ULL output for ENFORCED_AUDIBLE");
1462 flags = AUDIO_OUTPUT_FLAG_FAST;
1463 }
1464#endif
1465
Sharad Sangleb53795f2015-08-19 20:47:12 +05301466#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Sharad Sanglef2e11662015-05-28 16:15:16 +05301467 /*
1468 * WFD audio routes back to target speaker when starting a ringtone playback.
1469 * This is because primary output is reused for ringtone, so output device is
1470 * updated based on SONIFICATION strategy for both ringtone and music playback.
1471 * The same issue is not seen on remoted_submix HAL based WFD audio because
1472 * primary output is not reused and a new output is created for ringtone playback.
1473 * Issue is fixed by updating output flag to AUDIO_OUTPUT_FLAG_FAST when there is
1474 * a non-music stream playback on WFD, so primary output is not reused for ringtone.
1475 */
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001476 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
1477 if ((availableOutputDeviceTypes & AUDIO_DEVICE_OUT_PROXY)
1478 && (stream != AUDIO_STREAM_MUSIC)) {
Sharad Sanglef2e11662015-05-28 16:15:16 +05301479 ALOGD("WFD audio: use OUTPUT_FLAG_FAST for non music stream. flags:%x", flags );
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001480 //For voip paths
1481 if(flags & AUDIO_OUTPUT_FLAG_DIRECT)
1482 flags = AUDIO_OUTPUT_FLAG_DIRECT;
1483 else //route every thing else to ULL path
1484 flags = AUDIO_OUTPUT_FLAG_FAST;
1485 }
Sharad Sangleb53795f2015-08-19 20:47:12 +05301486#endif
1487
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001488 // open a direct output if required by specified parameters
1489 //force direct flag if offload flag is set: offloading implies a direct output stream
1490 // and all common behaviors are driven by checking only the direct flag
1491 // this should normally be set appropriately in the policy configuration file
1492 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1493 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1494 }
1495 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1496 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1497 }
Sharad Sanglef2e11662015-05-28 16:15:16 +05301498 // only allow deep buffering for music stream type
1499 if (stream != AUDIO_STREAM_MUSIC) {
1500 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
1501 }
1502 if (stream == AUDIO_STREAM_TTS) {
1503 flags = AUDIO_OUTPUT_FLAG_TTS;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001504 }
1505
Sharad Sangle78d53242015-06-04 20:24:10 +05301506 // open a direct output if required by specified parameters
1507 //force direct flag if offload flag is set: offloading implies a direct output stream
1508 // and all common behaviors are driven by checking only the direct flag
1509 // this should normally be set appropriately in the policy configuration file
1510 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1511 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1512 }
1513 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1514 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1515 }
1516 // only allow deep buffering for music stream type
1517 if (stream != AUDIO_STREAM_MUSIC) {
1518 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Sharad Sangle47b26cd2015-08-03 17:55:48 +05301519 } else if (/* stream == AUDIO_STREAM_MUSIC && */
1520 flags == AUDIO_OUTPUT_FLAG_NONE &&
Preetam Singh Ranawatc7ea3dd2015-08-28 11:53:19 +05301521 property_get_bool("audio.deep_buffer.media", true/* default_value */)) {
Sharad Sangle47b26cd2015-08-03 17:55:48 +05301522 flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Sharad Sangle78d53242015-06-04 20:24:10 +05301523 }
Sharad Sangle47b26cd2015-08-03 17:55:48 +05301524
Sharad Sangle78d53242015-06-04 20:24:10 +05301525 if (stream == AUDIO_STREAM_TTS) {
1526 flags = AUDIO_OUTPUT_FLAG_TTS;
1527 }
1528
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001529 sp<IOProfile> profile;
1530
1531 // skip direct output selection if the request can obviously be attached to a mixed output
1532 // and not explicitly requested
1533 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
1534 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
1535 audio_channel_count_from_out_mask(channelMask) <= 2) {
1536 goto non_direct_output;
1537 }
1538
1539 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1540 // creating an offloaded track and tearing it down immediately after start when audioflinger
1541 // detects there is an active non offloadable effect.
1542 // FIXME: We should check the audio session here but we do not have it in this context.
1543 // This may prevent offloading in rare situations where effects are left active by apps
1544 // in the background.
1545
Sharad Sanglef2e11662015-05-28 16:15:16 +05301546 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
1547 !mEffects.isNonOffloadableEffectEnabled()) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001548 profile = getProfileForDirectOutput(device,
1549 samplingRate,
1550 format,
1551 channelMask,
1552 (audio_output_flags_t)flags);
1553 }
1554
1555 if (profile != 0) {
Sharad Sanglef2e11662015-05-28 16:15:16 +05301556 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001557
1558 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sanglef2e11662015-05-28 16:15:16 +05301559 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001560 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1561 outputDesc = desc;
1562 // reuse direct output if currently open and configured with same parameters
1563 if ((samplingRate == outputDesc->mSamplingRate) &&
1564 (format == outputDesc->mFormat) &&
1565 (channelMask == outputDesc->mChannelMask)) {
1566 outputDesc->mDirectOpenCount++;
1567 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1568 return mOutputs.keyAt(i);
1569 }
1570 }
1571 }
1572 // close direct output if currently open and configured with different parameters
1573 if (outputDesc != NULL) {
1574 closeOutput(outputDesc->mIoHandle);
1575 }
Sharad Sanglef2e11662015-05-28 16:15:16 +05301576
1577 // if the selected profile is offloaded and no offload info was specified,
1578 // create a default one
1579 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
1580 if ((profile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
1581 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1582 defaultOffloadInfo.sample_rate = samplingRate;
1583 defaultOffloadInfo.channel_mask = channelMask;
1584 defaultOffloadInfo.format = format;
1585 defaultOffloadInfo.stream_type = stream;
1586 defaultOffloadInfo.bit_rate = 0;
1587 defaultOffloadInfo.duration_us = -1;
1588 defaultOffloadInfo.has_video = true; // conservative
1589 defaultOffloadInfo.is_streaming = true; // likely
1590 offloadInfo = &defaultOffloadInfo;
1591 }
1592
1593 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001594 outputDesc->mDevice = device;
1595 outputDesc->mLatency = 0;
Sharad Sanglef2e11662015-05-28 16:15:16 +05301596 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001597 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1598 config.sample_rate = samplingRate;
1599 config.channel_mask = channelMask;
1600 config.format = format;
1601 if (offloadInfo != NULL) {
1602 config.offload_info = *offloadInfo;
1603 }
Sharad Sanglef2e11662015-05-28 16:15:16 +05301604 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001605 &output,
1606 &config,
1607 &outputDesc->mDevice,
1608 String8(""),
1609 &outputDesc->mLatency,
1610 outputDesc->mFlags);
1611
1612 // only accept an output with the requested parameters
1613 if (status != NO_ERROR ||
1614 (samplingRate != 0 && samplingRate != config.sample_rate) ||
1615 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
1616 (channelMask != 0 && channelMask != config.channel_mask)) {
1617 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1618 "format %d %d, channelMask %04x %04x", output, samplingRate,
1619 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1620 outputDesc->mChannelMask);
1621 if (output != AUDIO_IO_HANDLE_NONE) {
1622 mpClientInterface->closeOutput(output);
1623 }
Sharad Sanglef2e11662015-05-28 16:15:16 +05301624 // fall back to mixer output if possible when the direct output could not be open
1625 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
1626 goto non_direct_output;
1627 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001628 return AUDIO_IO_HANDLE_NONE;
1629 }
1630 outputDesc->mSamplingRate = config.sample_rate;
1631 outputDesc->mChannelMask = config.channel_mask;
1632 outputDesc->mFormat = config.format;
1633 outputDesc->mRefCount[stream] = 0;
1634 outputDesc->mStopTime[stream] = 0;
1635 outputDesc->mDirectOpenCount = 1;
1636
1637 audio_io_handle_t srcOutput = getOutputForEffect();
1638 addOutput(output, outputDesc);
1639 audio_io_handle_t dstOutput = getOutputForEffect();
1640 if (dstOutput == output) {
1641 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1642 }
1643 mPreviousOutputs = mOutputs;
1644 ALOGV("getOutput() returns new direct output %d", output);
1645 mpClientInterface->onAudioPortListUpdate();
1646 return output;
1647 }
1648
1649non_direct_output:
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001650 // ignoring channel mask due to downmix capability in mixer
1651
1652 // open a non direct output
1653
1654 // for non direct outputs, only PCM is supported
1655 if (audio_is_linear_pcm(format)) {
1656 // get which output is suitable for the specified stream. The actual
1657 // routing change will happen when startOutput() will be called
1658 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1659
1660 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1661 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1662 output = selectOutput(outputs, flags, format);
1663 }
1664 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1665 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1666
Sharad Sanglef2e11662015-05-28 16:15:16 +05301667 ALOGV(" getOutputForDevice() returns output %d", output);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001668
1669 return output;
1670}
Sharad Sangle78d53242015-06-04 20:24:10 +05301671
1672status_t AudioPolicyManagerCustom::getInputForAttr(const audio_attributes_t *attr,
1673 audio_io_handle_t *input,
1674 audio_session_t session,
1675 uid_t uid,
1676 uint32_t samplingRate,
1677 audio_format_t format,
1678 audio_channel_mask_t channelMask,
1679 audio_input_flags_t flags,
1680 audio_port_handle_t selectedDeviceId,
1681 input_type_t *inputType)
1682{
1683 audio_source_t inputSource = attr->source;
1684#ifdef VOICE_CONCURRENCY
1685
1686 char propValue[PROPERTY_VALUE_MAX];
1687 bool prop_rec_enabled=false, prop_voip_enabled = false;
1688
1689 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
1690 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1691 }
1692
1693 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1694 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1695 }
1696
1697 if (prop_rec_enabled && mvoice_call_state) {
1698 //check if voice call is active / running in background
1699 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1700 //Need to block input request
1701 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1702 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1703 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1704 {
1705 switch(inputSource) {
1706 case AUDIO_SOURCE_VOICE_UPLINK:
1707 case AUDIO_SOURCE_VOICE_DOWNLINK:
1708 case AUDIO_SOURCE_VOICE_CALL:
1709 ALOGD("voice_conc:Creating input during incall mode for inputSource: %d",
1710 inputSource);
1711 break;
1712
1713 case AUDIO_SOURCE_VOICE_COMMUNICATION:
1714 if(prop_voip_enabled) {
1715 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1716 inputSource);
1717 return NO_INIT;
1718 }
1719 break;
1720 default:
1721 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1722 inputSource);
1723 return NO_INIT;
1724 }
1725 }
1726 }//check for VoIP flag
1727 else if(prop_voip_enabled && mvoice_call_state) {
1728 //check if voice call is active / running in background
1729 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1730 //Need to block input request
1731 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1732 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1733 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1734 {
1735 if(inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1736 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
1737 return NO_INIT;
1738 }
1739 }
1740 }
1741
1742#endif
1743
1744 return AudioPolicyManager::getInputForAttr(attr,
1745 input,
1746 session,
1747 uid,
1748 samplingRate,
1749 format,
1750 channelMask,
1751 flags,
1752 selectedDeviceId,
1753 inputType);
1754}
1755status_t AudioPolicyManagerCustom::startInput(audio_io_handle_t input,
1756 audio_session_t session)
1757{
1758 ALOGV("startInput() input %d", input);
1759 ssize_t index = mInputs.indexOfKey(input);
1760 if (index < 0) {
1761 ALOGW("startInput() unknown input %d", input);
1762 return BAD_VALUE;
1763 }
1764 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1765
1766 index = inputDesc->mSessions.indexOf(session);
1767 if (index < 0) {
1768 ALOGW("startInput() unknown session %d on input %d", session, input);
1769 return BAD_VALUE;
1770 }
1771
1772 // virtual input devices are compatible with other input devices
1773 if (!is_virtual_input_device(inputDesc->mDevice)) {
1774
1775 // for a non-virtual input device, check if there is another (non-virtual) active input
1776 audio_io_handle_t activeInput = mInputs.getActiveInput();
1777 if (activeInput != 0 && activeInput != input) {
1778
1779 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1780 // otherwise the active input continues and the new input cannot be started.
1781 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
1782 if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
1783 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
1784 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
1785 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
1786 } else {
1787 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
1788 return INVALID_OPERATION;
1789 }
1790 }
1791 }
1792
1793 // Routing?
1794 mInputRoutes.incRouteActivity(session);
1795#ifdef RECORD_PLAY_CONCURRENCY
1796 mIsInputRequestOnProgress = true;
1797
1798 char getPropValue[PROPERTY_VALUE_MAX];
1799 bool prop_rec_play_enabled = false;
1800
1801 if (property_get("rec.playback.conc.disabled", getPropValue, NULL)) {
1802 prop_rec_play_enabled = atoi(getPropValue) || !strncmp("true", getPropValue, 4);
1803 }
1804
1805 if ((prop_rec_play_enabled) &&(mInputs.activeInputsCount() == 0)){
1806 // send update to HAL on record playback concurrency
1807 AudioParameter param = AudioParameter();
1808 param.add(String8("rec_play_conc_on"), String8("true"));
1809 ALOGD("startInput() setParameters rec_play_conc is setting to ON ");
1810 mpClientInterface->setParameters(0, param.toString());
1811
1812 // Call invalidate to reset all opened non ULL audio tracks
1813 // Move tracks associated to this strategy from previous output to new output
1814 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
1815 // Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder)
Sharad Sangleb53795f2015-08-19 20:47:12 +05301816 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE && (i != AUDIO_STREAM_PATCH))) {
Sharad Sangle78d53242015-06-04 20:24:10 +05301817 ALOGD("Invalidate on releaseInput for stream :: %d ", i);
1818 //FIXME see fixme on name change
1819 mpClientInterface->invalidateStream((audio_stream_type_t)i);
1820 }
1821 }
1822 // close compress tracks
1823 for (size_t i = 0; i < mOutputs.size(); i++) {
1824 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
1825 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
1826 ALOGD("ouput desc / profile is NULL");
1827 continue;
1828 }
1829 if (outputDesc->mProfile->mFlags
1830 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1831 // close compress sessions
1832 ALOGD("calling closeOutput on record conc for COMPRESS output");
1833 closeOutput(mOutputs.keyAt(i));
1834 }
1835 }
1836 }
1837#endif
1838
1839 if (inputDesc->mRefCount == 0 || mInputRoutes.hasRouteChanged(session)) {
1840 // if input maps to a dynamic policy with an activity listener, notify of state change
1841 if ((inputDesc->mPolicyMix != NULL)
1842 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1843 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1844 MIX_STATE_MIXING);
1845 }
1846
1847 if (mInputs.activeInputsCount() == 0) {
1848 SoundTrigger::setCaptureState(true);
1849 }
1850 setInputDevice(input, getNewInputDevice(input), true /* force */);
1851
1852 // automatically enable the remote submix output when input is started if not
1853 // used by a policy mix of type MIX_TYPE_RECORDERS
1854 // For remote submix (a virtual device), we open only one input per capture request.
1855 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1856 String8 address = String8("");
1857 if (inputDesc->mPolicyMix == NULL) {
1858 address = String8("0");
1859 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1860 address = inputDesc->mPolicyMix->mRegistrationId;
1861 }
1862 if (address != "") {
1863 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1864 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1865 address, "remote-submix");
1866 }
1867 }
1868 }
1869
1870 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1871
1872 inputDesc->mRefCount++;
1873#ifdef RECORD_PLAY_CONCURRENCY
1874 mIsInputRequestOnProgress = false;
1875#endif
1876 return NO_ERROR;
1877}
1878status_t AudioPolicyManagerCustom::stopInput(audio_io_handle_t input,
1879 audio_session_t session)
1880{
1881 status_t status;
1882 status = AudioPolicyManager::stopInput(input, session);
1883#ifdef RECORD_PLAY_CONCURRENCY
1884 char propValue[PROPERTY_VALUE_MAX];
1885 bool prop_rec_play_enabled = false;
1886
1887 if (property_get("rec.playback.conc.disabled", propValue, NULL)) {
1888 prop_rec_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1889 }
1890
1891 if ((prop_rec_play_enabled) && (mInputs.activeInputsCount() == 0)) {
1892
1893 //send update to HAL on record playback concurrency
1894 AudioParameter param = AudioParameter();
1895 param.add(String8("rec_play_conc_on"), String8("false"));
1896 ALOGD("stopInput() setParameters rec_play_conc is setting to OFF ");
1897 mpClientInterface->setParameters(0, param.toString());
1898
1899 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
1900 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
1901 //Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder stop tone)
1902 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE) && (i != AUDIO_STREAM_PATCH)) {
1903 ALOGD(" Invalidate on stopInput for stream :: %d ", i);
1904 //FIXME see fixme on name change
1905 mpClientInterface->invalidateStream((audio_stream_type_t)i);
1906 }
1907 }
1908 }
1909#endif
1910 return status;
1911}
1912
Chaithanya Krishna Bacharaju54177c42015-09-14 15:05:09 +05301913void AudioPolicyManagerCustom::closeAllInputs() {
1914 bool patchRemoved = false;
1915
1916 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
1917 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
1918 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
1919 if (patch_index >= 0) {
1920 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
1921 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
1922 mAudioPatches.removeItemsAt(patch_index);
1923 patchRemoved = true;
1924 }
1925 if ((inputDesc->mIsSoundTrigger) && (mInputs.size() == 1)) {
1926 ALOGD("Do not close sound trigger input handle");
1927 } else {
1928 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1929 mInputs.removeItem(mInputs.keyAt(input_index));
1930 }
1931 }
1932 nextAudioPortGeneration();
1933
1934 if (patchRemoved) {
1935 mpClientInterface->onAudioPatchListUpdate();
1936 }
1937}
1938
Sharad Sangle78d53242015-06-04 20:24:10 +05301939AudioPolicyManagerCustom::AudioPolicyManagerCustom(AudioPolicyClientInterface *clientInterface)
Sharad Sangleb53795f2015-08-19 20:47:12 +05301940 : AudioPolicyManager(clientInterface),
1941 mHdmiAudioDisabled(false),
1942 mHdmiAudioEvent(false),
1943 mPrevPhoneState(0)
Sharad Sangle78d53242015-06-04 20:24:10 +05301944{
Sidipotu Ashok5fbcb902015-09-02 12:11:10 +05301945
1946 for (size_t i = 0; i < mHwModules.size(); i++) {
1947 ALOGV("Hw module %d", i);
1948 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) {
1949 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
1950 ALOGV("\t Input profile ", j);
1951 for (size_t k = 0; k < inProfile->mChannelMasks.size(); k++) {
1952 audio_channel_mask_t channelMask =
1953 inProfile->mChannelMasks.itemAt(k);
1954 ALOGV("\t\tChannel Mask %x size %d", channelMask,
1955 inProfile->mChannelMasks.size());
1956 if (AUDIO_CHANNEL_IN_5POINT1 == channelMask) {
1957 char ssr_enabled[PROPERTY_VALUE_MAX]={0};
1958 if ((property_get("ro.qc.sdk.audio.ssr",
1959 ssr_enabled, NULL) > 0) &&
1960 (!strncmp("true", ssr_enabled, 4))) {
1961 ALOGV("\t\t SSR supported, retain 5.1 channel"
1962 " in input profile");
1963 } else {
1964 ALOGE("\t\t removing AUDIO_CHANNEL_IN_5POINT1 from"
1965 " input profile as SSR(surround sound record)"
1966 " is not supported on this chipset variant");
1967 inProfile->mChannelMasks.removeItemsAt(k, 1);
1968 ALOGV("\t\t Channel Mask size now %d",
1969 inProfile->mChannelMasks.size());
1970 }
1971 }
1972 }
1973 }
1974 }
1975
Sharad Sangle78d53242015-06-04 20:24:10 +05301976#ifdef RECORD_PLAY_CONCURRENCY
1977 mIsInputRequestOnProgress = false;
1978#endif
1979
1980
1981#ifdef VOICE_CONCURRENCY
1982 mFallBackflag = getFallBackPath();
1983#endif
1984}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001985}