blob: 900e6cc22c8b3fdea8afb0002ff7ced7dc7af896 [file] [log] [blame]
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001/*
Sharad Sangle36781612015-05-28 16:15:16 +05302 * Copyright (c) 2013-2015, The Linux Foundation. All rights reserved.
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07003 * Not a contribution.
4 *
5 * Copyright (C) 2009 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
Sharad Sangle36781612015-05-28 16:15:16 +053020#define LOG_TAG "AudioPolicyManagerCustom"
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070021//#define LOG_NDEBUG 0
22
23//#define VERY_VERBOSE_LOGGING
24#ifdef VERY_VERBOSE_LOGGING
25#define ALOGVV ALOGV
26#else
27#define ALOGVV(a...) do { } while(0)
28#endif
Sharad Sangle36781612015-05-28 16:15:16 +053029#define MIN(a, b) ((a) < (b) ? (a) : (b))
30
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070031// A device mask for all audio output devices that are considered "remote" when evaluating
32// active output devices in isStreamActiveRemotely()
33#define APM_AUDIO_OUT_DEVICE_REMOTE_ALL AUDIO_DEVICE_OUT_REMOTE_SUBMIX
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070034// A device mask for all audio input and output devices where matching inputs/outputs on device
35// type alone is not enough: the address must match too
36#define APM_AUDIO_DEVICE_MATCH_ADDRESS_ALL (AUDIO_DEVICE_IN_REMOTE_SUBMIX | \
37 AUDIO_DEVICE_OUT_REMOTE_SUBMIX)
Sharad Sangle36781612015-05-28 16:15:16 +053038// Following delay should be used if the calculated routing delay from all active
39// input streams is higher than this value
40#define MAX_VOICE_CALL_START_DELAY_MS 100
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070041
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070042#include <inttypes.h>
Mingming Yin0ae14ea2014-07-09 17:55:56 -070043#include <math.h>
Mingming Yin0670f162014-06-12 16:05:49 -070044
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070045#include <cutils/properties.h>
46#include <utils/Log.h>
47#include <hardware/audio.h>
48#include <hardware/audio_effect.h>
49#include <media/AudioParameter.h>
50#include <soundtrigger/SoundTrigger.h>
51#include "AudioPolicyManager.h"
Sharad Sangle36781612015-05-28 16:15:16 +053052#include <policy.h>
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070053
54namespace android {
Sharad Sanglec5766ff2015-06-04 20:24:10 +053055#ifdef VOICE_CONCURRENCY
56audio_output_flags_t AudioPolicyManagerCustom::getFallBackPath()
57{
58 audio_output_flags_t flag = AUDIO_OUTPUT_FLAG_FAST;
59 char propValue[PROPERTY_VALUE_MAX];
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070060
Sharad Sanglec5766ff2015-06-04 20:24:10 +053061 if (property_get("voice.conc.fallbackpath", propValue, NULL)) {
62 if (!strncmp(propValue, "deep-buffer", 11)) {
63 flag = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
64 }
65 else if (!strncmp(propValue, "fast", 4)) {
66 flag = AUDIO_OUTPUT_FLAG_FAST;
67 }
68 else {
69 ALOGD("voice_conc:not a recognised path(%s) in prop voice.conc.fallbackpath",
70 propValue);
71 }
72 }
73 else {
74 ALOGD("voice_conc:prop voice.conc.fallbackpath not set");
75 }
76
77 ALOGD("voice_conc:picked up flag(0x%x) from prop voice.conc.fallbackpath",
78 flag);
79
80 return flag;
81}
82#endif /*VOICE_CONCURRENCY*/
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070083// ----------------------------------------------------------------------------
84// AudioPolicyInterface implementation
85// ----------------------------------------------------------------------------
Sharad Sangle36781612015-05-28 16:15:16 +053086extern "C" AudioPolicyInterface* createAudioPolicyManager(
87 AudioPolicyClientInterface *clientInterface)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070088{
Sharad Sangle36781612015-05-28 16:15:16 +053089 return new AudioPolicyManagerCustom(clientInterface);
90}
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070091
Sharad Sangle36781612015-05-28 16:15:16 +053092extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface)
93{
94 delete interface;
95}
96
97status_t AudioPolicyManagerCustom::setDeviceConnectionStateInt(audio_devices_t device,
98 audio_policy_dev_state_t state,
99 const char *device_address,
100 const char *device_name)
101{
102 ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
103 device, state, device_address, device_name);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700104
105 // connect/disconnect only 1 device at a time
106 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
107
Sharad Sangle36781612015-05-28 16:15:16 +0530108 sp<DeviceDescriptor> devDesc =
109 mHwModules.getDeviceDescriptor(device, device_address, device_name);
110
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700111 // handle output devices
112 if (audio_is_output_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700113 SortedVector <audio_io_handle_t> outputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700114
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700115 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700116
117 // save a copy of the opened output descriptors before any output is opened or closed
118 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
119 mPreviousOutputs = mOutputs;
120 switch (state)
121 {
122 // handle output device connection
Sharad Sangle36781612015-05-28 16:15:16 +0530123 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700124 if (index >= 0) {
Sharad Sangle4509cef2015-08-19 20:47:12 +0530125#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
126 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
127 if (!strncmp(device_address, "hdmi_spkr", 9)) {
128 mHdmiAudioDisabled = false;
129 } else {
130 mHdmiAudioEvent = true;
131 }
132 }
133#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700134 ALOGW("setDeviceConnectionState() device already connected: %x", device);
135 return INVALID_OPERATION;
136 }
137 ALOGV("setDeviceConnectionState() connecting device %x", device);
138
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700139 // register new device as available
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700140 index = mAvailableOutputDevices.add(devDesc);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530141#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
142 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
143 if (!strncmp(device_address, "hdmi_spkr", 9)) {
144 mHdmiAudioDisabled = false;
145 } else {
146 mHdmiAudioEvent = true;
147 }
148 if (mHdmiAudioDisabled || !mHdmiAudioEvent) {
149 mAvailableOutputDevices.remove(devDesc);
150 ALOGW("HDMI sink not connected, do not route audio to HDMI out");
151 return INVALID_OPERATION;
152 }
153 }
154#endif
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700155 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530156 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700157 if (module == 0) {
158 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
159 device);
160 mAvailableOutputDevices.remove(devDesc);
161 return INVALID_OPERATION;
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700162 }
Sharad Sangle36781612015-05-28 16:15:16 +0530163 mAvailableOutputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700164 } else {
165 return NO_MEMORY;
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700166 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700167
Sharad Sangle36781612015-05-28 16:15:16 +0530168 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700169 mAvailableOutputDevices.remove(devDesc);
170 return INVALID_OPERATION;
171 }
Sharad Sangle36781612015-05-28 16:15:16 +0530172 // Propagate device availability to Engine
173 mEngine->setDeviceConnectionState(devDesc, state);
174
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700175 // outputs should never be empty here
176 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
177 "checkOutputsForDevice() returned no outputs but status OK");
178 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
179 outputs.size());
Sharad Sangle36781612015-05-28 16:15:16 +0530180
181 // Send connect to HALs
182 AudioParameter param = AudioParameter(devDesc->mAddress);
183 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
184 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
185
186 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700187 // handle output device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700188 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
189 if (index < 0) {
Sharad Sangle4509cef2015-08-19 20:47:12 +0530190#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
191 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
192 if (!strncmp(device_address, "hdmi_spkr", 9)) {
193 mHdmiAudioDisabled = true;
194 } else {
195 mHdmiAudioEvent = false;
196 }
197 }
198#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700199 ALOGW("setDeviceConnectionState() device not connected: %x", device);
200 return INVALID_OPERATION;
201 }
202
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700203 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
204
Sharad Sangle36781612015-05-28 16:15:16 +0530205 // Send Disconnect to HALs
206 AudioParameter param = AudioParameter(devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700207 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
208 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
209
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700210 // remove device from available output devices
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700211 mAvailableOutputDevices.remove(devDesc);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530212#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
213 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
214 if (!strncmp(device_address, "hdmi_spkr", 9)) {
215 mHdmiAudioDisabled = true;
216 } else {
217 mHdmiAudioEvent = false;
218 }
219 }
220#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530221 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
222
223 // Propagate device availability to Engine
224 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700225 } break;
226
227 default:
228 ALOGE("setDeviceConnectionState() invalid state: %x", state);
229 return BAD_VALUE;
230 }
231
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700232 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
233 // output is suspended before any tracks are moved to it
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700234 checkA2dpSuspend();
235 checkOutputForAllStrategies();
236 // outputs must be closed after checkOutputForAllStrategies() is executed
237 if (!outputs.isEmpty()) {
238 for (size_t i = 0; i < outputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530239 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700240 // close unused outputs after device disconnection or direct outputs that have been
241 // opened by checkOutputsForDevice() to query dynamic parameters
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700242 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700243 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
244 (desc->mDirectOpenCount == 0))) {
245 closeOutput(outputs[i]);
246 }
247 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700248 // check again after closing A2DP output to reset mA2dpSuspended if needed
249 checkA2dpSuspend();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700250 }
251
252 updateDevicesAndOutputs();
Sharad Sangle36781612015-05-28 16:15:16 +0530253 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
254 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700255 updateCallRouting(newDevice);
256 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700257 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530258 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
259 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
260 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700261 // do not force device change on duplicated output because if device is 0, it will
262 // also force a device 0 for the two outputs it is duplicated to which may override
263 // a valid device selection on those outputs.
Sharad Sangle36781612015-05-28 16:15:16 +0530264 bool force = !desc->isDuplicated()
265 && (!device_distinguishes_on_address(device)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700266 // always force when disconnecting (a non-duplicated device)
267 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Sharad Sangle36781612015-05-28 16:15:16 +0530268 setOutputDevice(desc, newDevice, force, 0);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700269 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700270 }
271
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700272 mpClientInterface->onAudioPortListUpdate();
273 return NO_ERROR;
274 } // end if is output device
275
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700276 // handle input devices
277 if (audio_is_input_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700278 SortedVector <audio_io_handle_t> inputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700279
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700280 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700281 switch (state)
282 {
283 // handle input device connection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700284 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
285 if (index >= 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700286 ALOGW("setDeviceConnectionState() device already connected: %d", device);
287 return INVALID_OPERATION;
288 }
Sharad Sangle36781612015-05-28 16:15:16 +0530289 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700290 if (module == NULL) {
291 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
292 device);
293 return INVALID_OPERATION;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700294 }
Sharad Sangle36781612015-05-28 16:15:16 +0530295 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700296 return INVALID_OPERATION;
297 }
298
299 index = mAvailableInputDevices.add(devDesc);
300 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530301 mAvailableInputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700302 } else {
303 return NO_MEMORY;
304 }
Sharad Sangle36781612015-05-28 16:15:16 +0530305
306 // Set connect to HALs
307 AudioParameter param = AudioParameter(devDesc->mAddress);
308 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
309 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
310
311 // Propagate device availability to Engine
312 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700313 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700314
315 // handle input device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700316 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
317 if (index < 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700318 ALOGW("setDeviceConnectionState() device not connected: %d", device);
319 return INVALID_OPERATION;
320 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700321
322 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
323
324 // Set Disconnect to HALs
Sharad Sangle36781612015-05-28 16:15:16 +0530325 AudioParameter param = AudioParameter(devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700326 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
327 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
328
Sharad Sangle36781612015-05-28 16:15:16 +0530329 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700330 mAvailableInputDevices.remove(devDesc);
331
Sharad Sangle36781612015-05-28 16:15:16 +0530332 // Propagate device availability to Engine
333 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700334 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700335
336 default:
337 ALOGE("setDeviceConnectionState() invalid state: %x", state);
338 return BAD_VALUE;
339 }
340
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700341 closeAllInputs();
342
Sharad Sangle36781612015-05-28 16:15:16 +0530343 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700344 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
345 updateCallRouting(newDevice);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700346 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700347
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700348 mpClientInterface->onAudioPortListUpdate();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700349 return NO_ERROR;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700350 } // end if is input device
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700351
352 ALOGW("setDeviceConnectionState() invalid device: %x", device);
353 return BAD_VALUE;
354}
Sharad Sangle36781612015-05-28 16:15:16 +0530355// This function checks for the parameters which can be offloaded.
356// This can be enhanced depending on the capability of the DSP and policy
357// of the system.
358bool AudioPolicyManagerCustom::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700359{
Sharad Sangle36781612015-05-28 16:15:16 +0530360 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
361 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
362 offloadInfo.sample_rate, offloadInfo.channel_mask,
363 offloadInfo.format,
364 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
365 offloadInfo.has_video);
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530366#ifdef VOICE_CONCURRENCY
367 char concpropValue[PROPERTY_VALUE_MAX];
368 if (property_get("voice.playback.conc.disabled", concpropValue, NULL)) {
369 bool propenabled = atoi(concpropValue) || !strncmp("true", concpropValue, 4);
370 if (propenabled) {
371 if (isInCall())
372 {
373 ALOGD("\n copl: blocking compress offload on call mode\n");
374 return false;
375 }
376 }
377 }
378#endif
379#ifdef RECORD_PLAY_CONCURRENCY
380 char recConcPropValue[PROPERTY_VALUE_MAX];
381 bool prop_rec_play_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700382
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530383 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
384 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
385 }
386
387 if ((prop_rec_play_enabled) &&
388 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
389 ALOGD("copl: blocking compress offload for record concurrency");
390 return false;
391 }
392#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530393 // Check if stream type is music, then only allow offload as of now.
394 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
395 {
396 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
397 return false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700398 }
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530399
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 Sanglec5766ff2015-06-04 20:24:10 +0530414 }
415#endif
Preetam Singh Ranawat8152ab42015-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 Pindiproli5d82d012015-08-12 18:21:25 +0530442 }
443
Preetam Singh Ranawat8152ab42015-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 Dewangana6fc5442015-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 Ranawat8152ab42015-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 }
Manish Dewanganf3cd0f82015-10-13 14:04:36 +0530463
464 const bool allowOffloadStreamingWithVideo = property_get_bool("av.streaming.offload.enable",
465 false /*default value*/);
466 if(offloadInfo.has_video && offloadInfo.is_streaming && !allowOffloadStreamingWithVideo) {
467 ALOGW("offload disabled by av.streaming.offload.enable = %s ", propValue );
468 return false;
469 }
470
Sharad Sangle36781612015-05-28 16:15:16 +0530471 }
472
473 //If duration is less than minimum value defined in property, return false
474 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
475 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
476 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
477 return false;
478 }
479 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
480 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
481 //duration checks only valid for MP3/AAC/ formats,
482 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
483 if ((offloadInfo.format == AUDIO_FORMAT_MP3) ||
484 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530485 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS) ||
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530486#ifdef AUDIO_EXTN_FORMATS_ENABLED
Sharad Sangle36781612015-05-28 16:15:16 +0530487 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
Sharad Sangle36781612015-05-28 16:15:16 +0530488 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) ||
489 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) ||
490 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) ||
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530491 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_APE) ||
Manish Dewangana6fc5442015-08-24 20:30:31 +0530492 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS) ||
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530493#endif
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530494 pcmOffload)
Sharad Sangle36781612015-05-28 16:15:16 +0530495 return false;
496
497 }
498
499 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
500 // creating an offloaded track and tearing it down immediately after start when audioflinger
501 // detects there is an active non offloadable effect.
502 // FIXME: We should check the audio session here but we do not have it in this context.
503 // This may prevent offloading in rare situations where effects are left active by apps
504 // in the background.
505 if (mEffects.isNonOffloadableEffectEnabled()) {
506 return false;
507 }
508 // Check for soundcard status
509 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
510 String8("SND_CARD_STATUS"));
511 AudioParameter result = AudioParameter(valueStr);
512 int isonline = 0;
513 if ((result.getInt(String8("SND_CARD_STATUS"), isonline) == NO_ERROR)
514 && !isonline) {
515 ALOGD("copl: soundcard is offline rejecting offload request");
516 return false;
517 }
518 // See if there is a profile to support this.
519 // AUDIO_DEVICE_NONE
520 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
521 offloadInfo.sample_rate,
522 offloadInfo.format,
523 offloadInfo.channel_mask,
524 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
525 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
526 return (profile != 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700527}
Sharad Sangle36781612015-05-28 16:15:16 +0530528audio_devices_t AudioPolicyManagerCustom::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
529 bool fromCache)
530{
531 audio_devices_t device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700532
Sharad Sangle36781612015-05-28 16:15:16 +0530533 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
534 if (index >= 0) {
535 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
536 if (patchDesc->mUid != mUidCached) {
537 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
538 outputDesc->device(), outputDesc->mPatchHandle);
539 return outputDesc->device();
540 }
541 }
542
543 // check the following by order of priority to request a routing change if necessary:
544 // 1: the strategy enforced audible is active and enforced on the output:
545 // use device for strategy enforced audible
546 // 2: we are in call or the strategy phone is active on the output:
547 // use device for strategy phone
548 // 3: the strategy for enforced audible is active but not enforced on the output:
549 // use the device for strategy enforced audible
550 // 4: the strategy sonification is active on the output:
551 // use device for strategy sonification
552 // 5: the strategy "respectful" sonification is active on the output:
553 // use device for strategy "respectful" sonification
554 // 6: the strategy accessibility is active on the output:
555 // use device for strategy accessibility
556 // 7: the strategy media is active on the output:
557 // use device for strategy media
558 // 8: the strategy DTMF is active on the output:
559 // use device for strategy DTMF
560 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
561 // use device for strategy t-t-s
562 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
563 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
564 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
565 } else if (isInCall() ||
566 isStrategyActive(outputDesc, STRATEGY_PHONE)||
567 isStrategyActive(mPrimaryOutput, STRATEGY_PHONE)) {
568 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
569 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
570 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
571 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)||
572 (isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION)
573 && (!isStrategyActive(mPrimaryOutput,STRATEGY_MEDIA)))) {
574 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Sharad Sangle4509cef2015-08-19 20:47:12 +0530575 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL) ||
576 isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION_RESPECTFUL)) {
Sharad Sangle36781612015-05-28 16:15:16 +0530577 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
578 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
579 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
580 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
581 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
582 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
583 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
584 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
585 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
586 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
587 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
588 }
589
590 ALOGV("getNewOutputDevice() selected device %x", device);
591 return device;
592}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700593void AudioPolicyManagerCustom::setPhoneState(audio_mode_t state)
594{
Sharad Sangle36781612015-05-28 16:15:16 +0530595 ALOGV("setPhoneState() state %d", state);
596 // store previous phone state for management of sonification strategy below
Sharad Sangle4509cef2015-08-19 20:47:12 +0530597 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Sharad Sangle36781612015-05-28 16:15:16 +0530598 int oldState = mEngine->getPhoneState();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700599
Sharad Sangle36781612015-05-28 16:15:16 +0530600 if (mEngine->setPhoneState(state) != NO_ERROR) {
601 ALOGW("setPhoneState() invalid or same state %d", state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700602 return;
603 }
Sharad Sangle36781612015-05-28 16:15:16 +0530604 /// Opens: can these line be executed after the switch of volume curves???
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700605 // if leaving call state, handle special case of active streams
606 // pertaining to sonification strategy see handleIncallSonification()
607 if (isInCall()) {
608 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530609 for (size_t j = 0; j < mOutputs.size(); j++) {
610 audio_io_handle_t curOutput = mOutputs.keyAt(j);
611 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
612 if (stream == AUDIO_STREAM_PATCH) {
613 continue;
614 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530615 handleIncallSonification((audio_stream_type_t)stream, false, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +0530616 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700617 }
Sharad Sangle36781612015-05-28 16:15:16 +0530618
619 // force reevaluating accessibility routing when call starts
620 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700621 }
622
Sharad Sangle36781612015-05-28 16:15:16 +0530623 /**
624 * Switching to or from incall state or switching between telephony and VoIP lead to force
625 * routing command.
626 */
627 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
628 || (is_state_in_call(state) && (state != oldState)));
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700629
630 // check for device and output changes triggered by new phone state
631 checkA2dpSuspend();
632 checkOutputForAllStrategies();
633 updateDevicesAndOutputs();
634
Sharad Sangle36781612015-05-28 16:15:16 +0530635 sp<SwAudioOutputDescriptor> hwOutputDesc = mPrimaryOutput;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530636#ifdef VOICE_CONCURRENCY
637 int voice_call_state = 0;
638 char propValue[PROPERTY_VALUE_MAX];
639 bool prop_playback_enabled = false, prop_rec_enabled=false, prop_voip_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700640
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530641 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
642 prop_playback_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
643 }
644
645 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
646 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
647 }
648
649 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
650 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
651 }
652
653 bool mode_in_call = (AUDIO_MODE_IN_CALL != oldState) && (AUDIO_MODE_IN_CALL == state);
654 //query if it is a actual voice call initiated by telephony
655 if (mode_in_call) {
656 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0, String8("in_call"));
657 AudioParameter result = AudioParameter(valueStr);
658 if (result.getInt(String8("in_call"), voice_call_state) == NO_ERROR)
659 ALOGD("voice_conc:SetPhoneState: Voice call state = %d", voice_call_state);
660 }
661
662 if (mode_in_call && voice_call_state && !mvoice_call_state) {
663 ALOGD("voice_conc:Entering to call mode oldState :: %d state::%d ",
664 oldState, state);
665 mvoice_call_state = voice_call_state;
666 if (prop_rec_enabled) {
667 //Close all active inputs
668 audio_io_handle_t activeInput = mInputs.getActiveInput();
669 if (activeInput != 0) {
670 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
671 switch(activeDesc->mInputSource) {
672 case AUDIO_SOURCE_VOICE_UPLINK:
673 case AUDIO_SOURCE_VOICE_DOWNLINK:
674 case AUDIO_SOURCE_VOICE_CALL:
675 ALOGD("voice_conc:FOUND active input during call active: %d",activeDesc->mInputSource);
676 break;
677
678 case AUDIO_SOURCE_VOICE_COMMUNICATION:
679 if(prop_voip_enabled) {
680 ALOGD("voice_conc:CLOSING VoIP input source on call setup :%d ",activeDesc->mInputSource);
681 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
682 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
683 }
684 break;
685
686 default:
687 ALOGD("voice_conc:CLOSING input on call setup for inputSource: %d",activeDesc->mInputSource);
688 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
689 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
690 break;
691 }
692 }
693 } else if (prop_voip_enabled) {
694 audio_io_handle_t activeInput = mInputs.getActiveInput();
695 if (activeInput != 0) {
696 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
697 if (AUDIO_SOURCE_VOICE_COMMUNICATION == activeDesc->mInputSource) {
698 ALOGD("voice_conc:CLOSING VoIP on call setup : %d",activeDesc->mInputSource);
699 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
700 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
701 }
702 }
703 }
704 if (prop_playback_enabled) {
705 // Move tracks associated to this strategy from previous output to new output
706 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
707 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
708 if (i == AUDIO_STREAM_PATCH) {
709 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
710 continue;
711 }
712 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
713 if ((AUDIO_STREAM_MUSIC == i) ||
714 (AUDIO_STREAM_VOICE_CALL == i) ) {
715 ALOGD("voice_conc:Invalidate stream type %d", i);
716 mpClientInterface->invalidateStream((audio_stream_type_t)i);
717 }
718 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
719 ALOGD("voice_conc:Invalidate stream type %d", i);
720 mpClientInterface->invalidateStream((audio_stream_type_t)i);
721 }
722 }
723 }
724
725 for (size_t i = 0; i < mOutputs.size(); i++) {
726 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
727 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
728 ALOGD("voice_conc:ouput desc / profile is NULL");
729 continue;
730 }
731
732 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
733 if (((!outputDesc->isDuplicated() &&outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY))
734 && prop_playback_enabled) {
735 ALOGD("voice_conc:calling suspendOutput on call mode for primary output");
736 mpClientInterface->suspendOutput(mOutputs.keyAt(i));
737 } //Close compress all sessions
738 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
739 && prop_playback_enabled) {
740 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
741 closeOutput(mOutputs.keyAt(i));
742 }
743 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_VOIP_RX)
744 && prop_voip_enabled) {
745 ALOGD("voice_conc:calling closeOutput on call mode for DIRECT output");
746 closeOutput(mOutputs.keyAt(i));
747 }
748 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
749 if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)
750 && prop_playback_enabled) {
751 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
752 closeOutput(mOutputs.keyAt(i));
753 }
754 }
755 }
756 }
757
758 if ((AUDIO_MODE_IN_CALL == oldState || AUDIO_MODE_IN_COMMUNICATION == oldState) &&
759 (AUDIO_MODE_NORMAL == state) && prop_playback_enabled && mvoice_call_state) {
760 ALOGD("voice_conc:EXITING from call mode oldState :: %d state::%d \n",oldState, state);
761 mvoice_call_state = 0;
762 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
763 //restore PCM (deep-buffer) output after call termination
764 for (size_t i = 0; i < mOutputs.size(); i++) {
765 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
766 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
767 ALOGD("voice_conc:ouput desc / profile is NULL");
768 continue;
769 }
770 if (!outputDesc->isDuplicated() && outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
771 ALOGD("voice_conc:calling restoreOutput after call mode for primary output");
772 mpClientInterface->restoreOutput(mOutputs.keyAt(i));
773 }
774 }
775 }
776 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
777 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
778 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
779 if (i == AUDIO_STREAM_PATCH) {
780 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
781 continue;
782 }
783 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
784 if ((AUDIO_STREAM_MUSIC == i) ||
785 (AUDIO_STREAM_VOICE_CALL == i) ) {
786 mpClientInterface->invalidateStream((audio_stream_type_t)i);
787 }
788 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
789 mpClientInterface->invalidateStream((audio_stream_type_t)i);
790 }
791 }
792 }
793
794#endif
795#ifdef RECORD_PLAY_CONCURRENCY
796 char recConcPropValue[PROPERTY_VALUE_MAX];
797 bool prop_rec_play_enabled = false;
798
799 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
800 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
801 }
802 if (prop_rec_play_enabled) {
803 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
804 ALOGD("phone state changed to MODE_IN_COMM invlaidating music and voice streams");
805 // call invalidate for voice streams, so that it can use deepbuffer with VoIP out device from HAL
806 mpClientInterface->invalidateStream(AUDIO_STREAM_VOICE_CALL);
807 // call invalidate for music, so that compress will fallback to deep-buffer with VoIP out device
808 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
809
810 // close compress output to make sure session will be closed before timeout(60sec)
811 for (size_t i = 0; i < mOutputs.size(); i++) {
812
813 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
814 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
815 ALOGD("ouput desc / profile is NULL");
816 continue;
817 }
818
819 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
820 ALOGD("calling closeOutput on call mode for COMPRESS output");
821 closeOutput(mOutputs.keyAt(i));
822 }
823 }
824 } else if ((oldState == AUDIO_MODE_IN_COMMUNICATION) &&
825 (mEngine->getPhoneState() == AUDIO_MODE_NORMAL)) {
826 // call invalidate for music so that music can fallback to compress
827 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
828 }
829 }
830#endif
831 mPrevPhoneState = oldState;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700832 int delayMs = 0;
833 if (isStateInCall(state)) {
834 nsecs_t sysTime = systemTime();
835 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530836 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700837 // mute media and sonification strategies and delay device switch by the largest
838 // latency of any output where either strategy is active.
839 // This avoid sending the ring tone or music tail into the earpiece or headset.
Sharad Sangle36781612015-05-28 16:15:16 +0530840 if ((isStrategyActive(desc, STRATEGY_MEDIA,
841 SONIFICATION_HEADSET_MUSIC_DELAY,
842 sysTime) ||
843 isStrategyActive(desc, STRATEGY_SONIFICATION,
844 SONIFICATION_HEADSET_MUSIC_DELAY,
845 sysTime)) &&
846 (delayMs < (int)desc->latency()*2)) {
847 delayMs = desc->latency()*2;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700848 }
Sharad Sangle36781612015-05-28 16:15:16 +0530849 setStrategyMute(STRATEGY_MEDIA, true, desc);
850 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700851 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Sharad Sangle36781612015-05-28 16:15:16 +0530852 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
853 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700854 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
855 }
Sharad Sangle36781612015-05-28 16:15:16 +0530856 ALOGV("Setting the delay from %dms to %dms", delayMs,
857 MIN(delayMs, MAX_VOICE_CALL_START_DELAY_MS));
858 delayMs = MIN(delayMs, MAX_VOICE_CALL_START_DELAY_MS);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700859 }
860
Sharad Sangle36781612015-05-28 16:15:16 +0530861 if (hasPrimaryOutput()) {
862 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
863 // the device returned is not necessarily reachable via this output
864 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
865 // force routing command to audio hardware when ending call
866 // even if no device change is needed
867 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
868 rxDevice = mPrimaryOutput->device();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700869 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700870
Sharad Sangle36781612015-05-28 16:15:16 +0530871 if (state == AUDIO_MODE_IN_CALL) {
872 updateCallRouting(rxDevice, delayMs);
873 } else if (oldState == AUDIO_MODE_IN_CALL) {
874 if (mCallRxPatch != 0) {
875 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
876 mCallRxPatch.clear();
877 }
878 if (mCallTxPatch != 0) {
879 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
880 mCallTxPatch.clear();
881 }
882 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
883 } else {
884 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700885 }
886 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530887 //update device for all non-primary outputs
888 for (size_t i = 0; i < mOutputs.size(); i++) {
889 audio_io_handle_t output = mOutputs.keyAt(i);
890 if (output != mPrimaryOutput->mIoHandle) {
891 newDevice = getNewOutputDevice(mOutputs.valueFor(output), false /*fromCache*/);
892 setOutputDevice(mOutputs.valueFor(output), newDevice, (newDevice != AUDIO_DEVICE_NONE));
893 }
894 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700895 // if entering in call state, handle special case of active streams
896 // pertaining to sonification strategy see handleIncallSonification()
897 if (isStateInCall(state)) {
898 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530899 for (size_t j = 0; j < mOutputs.size(); j++) {
900 audio_io_handle_t curOutput = mOutputs.keyAt(j);
901 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
902 if (stream == AUDIO_STREAM_PATCH) {
903 continue;
904 }
Sharad Sangle4509cef2015-08-19 20:47:12 +0530905 handleIncallSonification((audio_stream_type_t)stream, true, true, curOutput);
Sharad Sangle36781612015-05-28 16:15:16 +0530906 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700907 }
908 }
909
910 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
911 if (state == AUDIO_MODE_RINGTONE &&
912 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
913 mLimitRingtoneVolume = true;
914 } else {
915 mLimitRingtoneVolume = false;
916 }
917}
Dhananjay Kumar87dea1b2015-09-16 19:44:33 +0530918
919void AudioPolicyManagerCustom::setForceUse(audio_policy_force_use_t usage,
920 audio_policy_forced_cfg_t config)
921{
922 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
923
924 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
925 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
926 return;
927 }
928 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
929 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
930 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
931
932 // check for device and output changes triggered by new force usage
933 checkA2dpSuspend();
934 checkOutputForAllStrategies();
935 updateDevicesAndOutputs();
936 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
937 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
938 updateCallRouting(newDevice);
939 }
940 // Use reverse loop to make sure any low latency usecases (generally tones)
941 // are not routed before non LL usecases (generally music).
942 // We can safely assume that LL output would always have lower index,
943 // and use this work-around to avoid routing of output with music stream
944 // from the context of short lived LL output.
945 // Note: in case output's share backend(HAL sharing is implicit) all outputs
946 // gets routing update while processing first output itself.
947 for (size_t i = mOutputs.size(); i > 0; i--) {
948 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i-1);
949 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
950 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || outputDesc != mPrimaryOutput) {
951 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
952 }
953 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
954 applyStreamVolumes(outputDesc, newDevice, 0, true);
955 }
956 }
957
958 audio_io_handle_t activeInput = mInputs.getActiveInput();
959 if (activeInput != 0) {
960 setInputDevice(activeInput, getNewInputDevice(activeInput));
961 }
962
963}
964
Sharad Sangle36781612015-05-28 16:15:16 +0530965status_t AudioPolicyManagerCustom::stopSource(sp<SwAudioOutputDescriptor> outputDesc,
966 audio_stream_type_t stream,
967 bool forceDeviceUpdate)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700968{
Sharad Sangle36781612015-05-28 16:15:16 +0530969 // always handle stream stop, check which stream type is stopping
970 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700971
Sharad Sangle36781612015-05-28 16:15:16 +0530972 // handle special case for sonification while in call
Sharad Sangle4509cef2015-08-19 20:47:12 +0530973 if (isInCall() && (outputDesc->mRefCount[stream] == 1)) {
Sharad Sangle36781612015-05-28 16:15:16 +0530974 if (outputDesc->isDuplicated()) {
Sharad Sangle4509cef2015-08-19 20:47:12 +0530975 handleIncallSonification(stream, false, false, outputDesc->mOutput1->mIoHandle);
976 handleIncallSonification(stream, false, false, outputDesc->mOutput2->mIoHandle);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700977 }
Sharad Sangle36781612015-05-28 16:15:16 +0530978 handleIncallSonification(stream, false, false, outputDesc->mIoHandle);
979 }
980
981 if (outputDesc->mRefCount[stream] > 0) {
982 // decrement usage count of this stream on the output
983 outputDesc->changeRefCount(stream, -1);
984
985 // store time at which the stream was stopped - see isStreamActive()
986 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
987 outputDesc->mStopTime[stream] = systemTime();
Zhou Song5dcddc92015-09-21 14:36:57 +0800988 audio_devices_t prevDevice = outputDesc->device();
Sharad Sangle36781612015-05-28 16:15:16 +0530989 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
990 // delay the device switch by twice the latency because stopOutput() is executed when
991 // the track stop() command is received and at that time the audio track buffer can
992 // still contain data that needs to be drained. The latency only covers the audio HAL
993 // and kernel buffers. Also the latency does not always include additional delay in the
994 // audio path (audio DSP, CODEC ...)
995 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
996
997 // force restoring the device selection on other active outputs if it differs from the
998 // one being selected for this output
999 for (size_t i = 0; i < mOutputs.size(); i++) {
1000 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1001 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1002 if (desc != outputDesc &&
1003 desc->isActive() &&
1004 outputDesc->sharesHwModuleWith(desc) &&
1005 (newDevice != desc->device())) {
Sharad Sangle4509cef2015-08-19 20:47:12 +05301006 audio_devices_t dev = getNewOutputDevice(mOutputs.valueFor(curOutput), false /*fromCache*/);
Zhou Song5dcddc92015-09-21 14:36:57 +08001007 uint32_t delayMs;
1008 if (dev == prevDevice) {
1009 delayMs = 0;
1010 } else {
1011 delayMs = outputDesc->mLatency*2;
1012 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301013 setOutputDevice(desc,
1014 dev,
Sharad Sangle36781612015-05-28 16:15:16 +05301015 true,
Zhou Song5dcddc92015-09-21 14:36:57 +08001016 delayMs);
Sharad Sangle36781612015-05-28 16:15:16 +05301017 }
1018 }
1019 // update the outputs if stopping one with a stream that can affect notification routing
1020 handleNotificationRoutingForStream(stream);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001021 }
Sharad Sangle36781612015-05-28 16:15:16 +05301022 return NO_ERROR;
1023 } else {
1024 ALOGW("stopOutput() refcount is already 0");
1025 return INVALID_OPERATION;
1026 }
1027}
1028status_t AudioPolicyManagerCustom::startSource(sp<SwAudioOutputDescriptor> outputDesc,
1029 audio_stream_type_t stream,
1030 audio_devices_t device,
1031 uint32_t *delayMs)
1032{
1033 // cannot start playback of STREAM_TTS if any other output is being used
1034 uint32_t beaconMuteLatency = 0;
1035
1036 *delayMs = 0;
1037 if (stream == AUDIO_STREAM_TTS) {
1038 ALOGV("\t found BEACON stream");
1039 if (mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
1040 return INVALID_OPERATION;
1041 } else {
1042 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001043 }
Sharad Sangle36781612015-05-28 16:15:16 +05301044 } else {
1045 // some playback other than beacon starts
1046 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1047 }
1048
1049 // increment usage count for this stream on the requested output:
1050 // NOTE that the usage count is the same for duplicated output and hardware output which is
1051 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1052 outputDesc->changeRefCount(stream, 1);
1053
1054 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
1055 // starting an output being rerouted?
1056 if (device == AUDIO_DEVICE_NONE) {
1057 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001058 }
Sharad Sangle36781612015-05-28 16:15:16 +05301059 routing_strategy strategy = getStrategy(stream);
1060 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
1061 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1062 (beaconMuteLatency > 0);
1063 uint32_t waitMs = beaconMuteLatency;
1064 bool force = false;
1065 for (size_t i = 0; i < mOutputs.size(); i++) {
1066 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
1067 if (desc != outputDesc) {
1068 // force a device change if any other output is managed by the same hw
1069 // module and has a current device selection that differs from selected device.
1070 // In this case, the audio HAL must receive the new device selection so that it can
1071 // change the device currently selected by the other active output.
1072 if (outputDesc->sharesHwModuleWith(desc) &&
1073 desc->device() != device) {
1074 force = true;
1075 }
1076 // wait for audio on other active outputs to be presented when starting
1077 // a notification so that audio focus effect can propagate, or that a mute/unmute
1078 // event occurred for beacon
1079 uint32_t latency = desc->latency();
1080 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1081 waitMs = latency;
1082 }
1083 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001084 }
Sharad Sangle36781612015-05-28 16:15:16 +05301085 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force);
1086
1087 // handle special case for sonification while in call
1088 if (isInCall()) {
1089 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001090 }
Sharad Sangle36781612015-05-28 16:15:16 +05301091
1092 // apply volume rules for current stream and device if necessary
1093 checkAndSetVolume(stream,
1094 mStreams.valueFor(stream).getVolumeIndex(device),
1095 outputDesc,
1096 device);
1097
1098 // update the outputs if starting an output with a stream that can affect notification
1099 // routing
1100 handleNotificationRoutingForStream(stream);
1101
1102 // force reevaluating accessibility routing when ringtone or alarm starts
1103 if (strategy == STRATEGY_SONIFICATION) {
1104 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1105 }
1106 }
1107 else {
1108 // handle special case for sonification while in call
1109 if (isInCall()) {
1110 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
1111 }
1112 }
1113 return NO_ERROR;
1114}
1115void AudioPolicyManagerCustom::handleIncallSonification(audio_stream_type_t stream,
1116 bool starting, bool stateChange,
1117 audio_io_handle_t output)
1118{
1119 if(!hasPrimaryOutput()) {
1120 return;
1121 }
1122 // no action needed for AUDIO_STREAM_PATCH stream type, it's for internal flinger tracks
1123 if (stream == AUDIO_STREAM_PATCH) {
1124 return;
1125 }
1126 // if the stream pertains to sonification strategy and we are in call we must
1127 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
1128 // in the device used for phone strategy and play the tone if the selected device does not
1129 // interfere with the device used for phone strategy
1130 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
1131 // many times as there are active tracks on the output
1132 const routing_strategy stream_strategy = getStrategy(stream);
1133 if ((stream_strategy == STRATEGY_SONIFICATION) ||
1134 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
1135 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
1136 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
1137 stream, starting, outputDesc->mDevice, stateChange);
1138 if (outputDesc->mRefCount[stream]) {
1139 int muteCount = 1;
1140 if (stateChange) {
1141 muteCount = outputDesc->mRefCount[stream];
1142 }
1143 if (audio_is_low_visibility(stream)) {
1144 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
1145 for (int i = 0; i < muteCount; i++) {
1146 setStreamMute(stream, starting, outputDesc);
1147 }
1148 } else {
1149 ALOGV("handleIncallSonification() high visibility");
1150 if (outputDesc->device() &
1151 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
1152 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
1153 for (int i = 0; i < muteCount; i++) {
1154 setStreamMute(stream, starting, outputDesc);
1155 }
1156 }
1157 if (starting) {
1158 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
1159 AUDIO_STREAM_VOICE_CALL);
1160 } else {
1161 mpClientInterface->stopTone();
1162 }
1163 }
1164 }
1165 }
1166}
1167void AudioPolicyManagerCustom::handleNotificationRoutingForStream(audio_stream_type_t stream) {
1168 switch(stream) {
1169 case AUDIO_STREAM_MUSIC:
1170 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
1171 updateDevicesAndOutputs();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001172 break;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001173 default:
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001174 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001175 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001176}
Sharad Sangle36781612015-05-28 16:15:16 +05301177status_t AudioPolicyManagerCustom::checkAndSetVolume(audio_stream_type_t stream,
1178 int index,
1179 const sp<SwAudioOutputDescriptor>& outputDesc,
1180 audio_devices_t device,
1181 int delayMs, bool force)
1182{
1183 // do not change actual stream volume if the stream is muted
1184 if (outputDesc->mMuteCount[stream] != 0) {
1185 ALOGVV("checkAndSetVolume() stream %d muted count %d",
1186 stream, outputDesc->mMuteCount[stream]);
1187 return NO_ERROR;
1188 }
1189 audio_policy_forced_cfg_t forceUseForComm =
1190 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
1191 // do not change in call volume if bluetooth is connected and vice versa
1192 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
1193 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
1194 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
1195 stream, forceUseForComm);
1196 return INVALID_OPERATION;
1197 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001198
Sharad Sangle36781612015-05-28 16:15:16 +05301199 if (device == AUDIO_DEVICE_NONE) {
1200 device = outputDesc->device();
1201 }
1202
1203 float volumeDb = computeVolume(stream, index, device);
1204 if (outputDesc->isFixedVolume(device)) {
1205 volumeDb = 0.0f;
1206 }
1207
1208 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
1209
1210 if (stream == AUDIO_STREAM_VOICE_CALL ||
1211 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
1212 float voiceVolume;
1213 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
1214 if (stream == AUDIO_STREAM_VOICE_CALL) {
1215 voiceVolume = (float)index/(float)mStreams.valueFor(stream).getVolumeIndexMax();
1216 } else {
1217 voiceVolume = 1.0;
1218 }
1219
1220 if (voiceVolume != mLastVoiceVolume && ((outputDesc == mPrimaryOutput) ||
1221 isDirectOutput(outputDesc->mIoHandle) || device & AUDIO_DEVICE_OUT_ALL_USB)) {
1222 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
1223 mLastVoiceVolume = voiceVolume;
1224 }
1225 }
1226
1227 return NO_ERROR;
1228}
1229bool AudioPolicyManagerCustom::isDirectOutput(audio_io_handle_t output) {
1230 for (size_t i = 0; i < mOutputs.size(); i++) {
1231 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1232 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1233 if ((curOutput == output) && (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
1234 return true;
1235 }
1236 }
1237 return false;
1238}
vivek mehta0ea887a2015-08-26 14:01:20 -07001239
1240status_t AudioPolicyManagerCustom::getOutputForAttr(const audio_attributes_t *attr,
1241 audio_io_handle_t *output,
1242 audio_session_t session,
1243 audio_stream_type_t *stream,
1244 uid_t uid,
1245 uint32_t samplingRate,
1246 audio_format_t format,
1247 audio_channel_mask_t channelMask,
1248 audio_output_flags_t flags,
1249 audio_port_handle_t selectedDeviceId,
1250 const audio_offload_info_t *offloadInfo)
1251{
1252 audio_offload_info_t tOffloadInfo = AUDIO_INFO_INITIALIZER;
1253
1254 bool pcmOffloadEnabled = property_get_bool("audio.offload.track.enable", false);
1255
1256 if (offloadInfo == NULL && pcmOffloadEnabled) {
1257 tOffloadInfo.sample_rate = samplingRate;
1258 tOffloadInfo.channel_mask = channelMask;
1259 tOffloadInfo.format = format;
1260 tOffloadInfo.stream_type = *stream;
1261 tOffloadInfo.bit_width = 16; //hard coded for PCM_16
1262 if (attr != NULL) {
1263 ALOGV("found attribute .. setting usage %d ", attr->usage);
1264 tOffloadInfo.usage = attr->usage;
1265 } else {
1266 ALOGD("%s:: attribute is NULL .. no usage set", __func__);
1267 }
1268 offloadInfo = &tOffloadInfo;
1269 }
1270
1271 return AudioPolicyManager::getOutputForAttr(attr, output, session, stream,
1272 (uid_t)uid, (uint32_t)samplingRate,
1273 format, (audio_channel_mask_t)channelMask,
1274 flags, (audio_port_handle_t)selectedDeviceId,
1275 offloadInfo);
1276}
1277
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001278audio_io_handle_t AudioPolicyManagerCustom::getOutputForDevice(
1279 audio_devices_t device,
Sharad Sangle36781612015-05-28 16:15:16 +05301280 audio_session_t session __unused,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001281 audio_stream_type_t stream,
1282 uint32_t samplingRate,
1283 audio_format_t format,
1284 audio_channel_mask_t channelMask,
1285 audio_output_flags_t flags,
1286 const audio_offload_info_t *offloadInfo)
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001287{
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001288 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
1289 uint32_t latency = 0;
1290 status_t status;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001291
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001292#ifdef AUDIO_POLICY_TEST
1293 if (mCurOutput != 0) {
1294 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1295 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1296
1297 if (mTestOutputs[mCurOutput] == 0) {
1298 ALOGV("getOutput() opening test output");
Sharad Sangle36781612015-05-28 16:15:16 +05301299 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
1300 mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001301 outputDesc->mDevice = mTestDevice;
1302 outputDesc->mLatency = mTestLatencyMs;
1303 outputDesc->mFlags =
1304 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
1305 outputDesc->mRefCount[stream] = 0;
1306 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1307 config.sample_rate = mTestSamplingRate;
1308 config.channel_mask = mTestChannels;
1309 config.format = mTestFormat;
1310 if (offloadInfo != NULL) {
1311 config.offload_info = *offloadInfo;
1312 }
1313 status = mpClientInterface->openOutput(0,
1314 &mTestOutputs[mCurOutput],
1315 &config,
1316 &outputDesc->mDevice,
1317 String8(""),
1318 &outputDesc->mLatency,
1319 outputDesc->mFlags);
1320 if (status == NO_ERROR) {
1321 outputDesc->mSamplingRate = config.sample_rate;
1322 outputDesc->mFormat = config.format;
1323 outputDesc->mChannelMask = config.channel_mask;
1324 AudioParameter outputCmd = AudioParameter();
1325 outputCmd.addInt(String8("set_id"),mCurOutput);
1326 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1327 addOutput(mTestOutputs[mCurOutput], outputDesc);
1328 }
1329 }
1330 return mTestOutputs[mCurOutput];
1331 }
1332#endif //AUDIO_POLICY_TEST
Sharad Sangle36781612015-05-28 16:15:16 +05301333 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1334 (stream != AUDIO_STREAM_MUSIC)) {
1335 // compress should not be used for non-music streams
1336 ALOGE("Offloading only allowed with music stream");
1337 return 0;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301338 }
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301339
1340 if ((stream == AUDIO_STREAM_VOICE_CALL) &&
1341 (channelMask == 1) &&
1342 (samplingRate == 8000 || samplingRate == 16000)) {
1343 // Allow Voip direct output only if:
1344 // audio mode is MODE_IN_COMMUNCATION; AND
1345 // voip output is not opened already; AND
1346 // requested sample rate matches with that of voip input stream (if opened already)
1347 int value = 0;
1348 uint32_t mode = 0, voipOutCount = 1, voipSampleRate = 1;
1349 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1350 String8("audio_mode"));
1351 AudioParameter result = AudioParameter(valueStr);
1352 if (result.getInt(String8("audio_mode"), value) == NO_ERROR) {
1353 mode = value;
1354 }
1355
1356 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1357 String8("voip_out_stream_count"));
1358 result = AudioParameter(valueStr);
1359 if (result.getInt(String8("voip_out_stream_count"), value) == NO_ERROR) {
1360 voipOutCount = value;
1361 }
1362
1363 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1364 String8("voip_sample_rate"));
1365 result = AudioParameter(valueStr);
1366 if (result.getInt(String8("voip_sample_rate"), value) == NO_ERROR) {
1367 voipSampleRate = value;
1368 }
1369
1370 if ((mode == AUDIO_MODE_IN_COMMUNICATION) && (voipOutCount == 0) &&
1371 ((voipSampleRate == 0) || (voipSampleRate == samplingRate))) {
1372 if (audio_is_linear_pcm(format)) {
1373 char propValue[PROPERTY_VALUE_MAX] = {0};
1374 property_get("use.voice.path.for.pcm.voip", propValue, "0");
1375 bool voipPcmSysPropEnabled = !strncmp("true", propValue, sizeof("true"));
1376 if (voipPcmSysPropEnabled && (format == AUDIO_FORMAT_PCM_16_BIT)) {
1377 flags = (audio_output_flags_t)((flags &~AUDIO_OUTPUT_FLAG_FAST) |
1378 AUDIO_OUTPUT_FLAG_VOIP_RX | AUDIO_OUTPUT_FLAG_DIRECT);
1379 ALOGD("Set VoIP and Direct output flags for PCM format");
1380 }
1381 }
1382 }
1383 }
1384
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301385#ifdef VOICE_CONCURRENCY
1386 char propValue[PROPERTY_VALUE_MAX];
1387 bool prop_play_enabled=false, prop_voip_enabled = false;
1388
1389 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1390 prop_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001391 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301392
1393 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1394 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1395 }
1396
1397 if (prop_play_enabled && mvoice_call_state) {
1398 //check if voice call is active / running in background
1399 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1400 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1401 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1402 {
1403 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1404 if(prop_voip_enabled) {
1405 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1406 flags );
1407 return 0;
1408 }
1409 }
1410 else {
1411 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
1412 ALOGD("voice_conc:IN call mode adding ULL flags .. flags: %x ", flags );
1413 flags = AUDIO_OUTPUT_FLAG_FAST;
1414 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
1415 if (AUDIO_STREAM_MUSIC == stream) {
1416 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1417 ALOGD("voice_conc:IN call mode adding deep-buffer flags %x ", flags );
1418 }
1419 else {
1420 flags = AUDIO_OUTPUT_FLAG_FAST;
1421 ALOGD("voice_conc:IN call mode adding fast flags %x ", flags );
1422 }
1423 }
1424 }
1425 }
1426 } else if (prop_voip_enabled && mvoice_call_state) {
1427 //check if voice call is active / running in background
1428 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1429 //return only ULL ouput
1430 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1431 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1432 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1433 {
1434 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1435 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1436 flags );
1437 return 0;
1438 }
1439 }
1440 }
1441#endif
1442#ifdef RECORD_PLAY_CONCURRENCY
1443 char recConcPropValue[PROPERTY_VALUE_MAX];
1444 bool prop_rec_play_enabled = false;
1445
1446 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
1447 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
1448 }
1449 if ((prop_rec_play_enabled) &&
1450 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
1451 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
1452 if (AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1453 // allow VoIP using voice path
1454 // Do nothing
1455 } else if((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1456 ALOGD("voice_conc:MODE_IN_COMM is setforcing deep buffer output for non ULL... flags: %x", flags);
1457 // use deep buffer path for all non ULL outputs
1458 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1459 }
1460 } else if ((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1461 ALOGD("voice_conc:Record mode is on forcing deep buffer output for non ULL... flags: %x ", flags);
1462 // use deep buffer path for all non ULL outputs
1463 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1464 }
1465 }
1466 if (prop_rec_play_enabled &&
1467 (stream == AUDIO_STREAM_ENFORCED_AUDIBLE)) {
1468 ALOGD("Record conc is on forcing ULL output for ENFORCED_AUDIBLE");
1469 flags = AUDIO_OUTPUT_FLAG_FAST;
1470 }
1471#endif
1472
Sharad Sangle4509cef2015-08-19 20:47:12 +05301473#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Sharad Sangle36781612015-05-28 16:15:16 +05301474 /*
1475 * WFD audio routes back to target speaker when starting a ringtone playback.
1476 * This is because primary output is reused for ringtone, so output device is
1477 * updated based on SONIFICATION strategy for both ringtone and music playback.
1478 * The same issue is not seen on remoted_submix HAL based WFD audio because
1479 * primary output is not reused and a new output is created for ringtone playback.
1480 * Issue is fixed by updating output flag to AUDIO_OUTPUT_FLAG_FAST when there is
1481 * a non-music stream playback on WFD, so primary output is not reused for ringtone.
1482 */
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001483 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
1484 if ((availableOutputDeviceTypes & AUDIO_DEVICE_OUT_PROXY)
1485 && (stream != AUDIO_STREAM_MUSIC)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301486 ALOGD("WFD audio: use OUTPUT_FLAG_FAST for non music stream. flags:%x", flags );
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001487 //For voip paths
1488 if(flags & AUDIO_OUTPUT_FLAG_DIRECT)
1489 flags = AUDIO_OUTPUT_FLAG_DIRECT;
1490 else //route every thing else to ULL path
1491 flags = AUDIO_OUTPUT_FLAG_FAST;
1492 }
Sharad Sangle4509cef2015-08-19 20:47:12 +05301493#endif
1494
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001495 // open a direct output if required by specified parameters
vivek mehta0ea887a2015-08-26 14:01:20 -07001496 // force direct flag if offload flag is set: offloading implies a direct output stream
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001497 // and all common behaviors are driven by checking only the direct flag
1498 // this should normally be set appropriately in the policy configuration file
1499 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1500 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1501 }
1502 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1503 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1504 }
vivek mehta0ea887a2015-08-26 14:01:20 -07001505
1506 // Do offload magic here
1507 if ((flags == AUDIO_OUTPUT_FLAG_NONE) && (stream == AUDIO_STREAM_MUSIC) &&
1508 (offloadInfo != NULL) &&
1509 ((offloadInfo->usage == AUDIO_USAGE_MEDIA ||
1510 (offloadInfo->usage == AUDIO_USAGE_GAME)))) {
1511 if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) {
1512 ALOGD("AudioCustomHAL --> Force Direct Flag ..");
1513 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1514 }
1515 }
1516
Sharad Sangle36781612015-05-28 16:15:16 +05301517 // only allow deep buffering for music stream type
1518 if (stream != AUDIO_STREAM_MUSIC) {
1519 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Sharad Sangle497aef82015-08-03 17:55:48 +05301520 } else if (/* stream == AUDIO_STREAM_MUSIC && */
1521 flags == AUDIO_OUTPUT_FLAG_NONE &&
1522 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
1523 flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Sharad Sangle36781612015-05-28 16:15:16 +05301524 }
Sharad Sangle497aef82015-08-03 17:55:48 +05301525
Sharad Sangle36781612015-05-28 16:15:16 +05301526 if (stream == AUDIO_STREAM_TTS) {
1527 flags = AUDIO_OUTPUT_FLAG_TTS;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001528 }
1529
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301530 // open a direct output if required by specified parameters
1531 //force direct flag if offload flag is set: offloading implies a direct output stream
1532 // and all common behaviors are driven by checking only the direct flag
1533 // this should normally be set appropriately in the policy configuration file
1534 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1535 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1536 }
1537 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1538 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1539 }
1540 // only allow deep buffering for music stream type
1541 if (stream != AUDIO_STREAM_MUSIC) {
1542 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
1543 }
1544 if (stream == AUDIO_STREAM_TTS) {
1545 flags = AUDIO_OUTPUT_FLAG_TTS;
1546 }
1547
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001548 sp<IOProfile> profile;
1549
1550 // skip direct output selection if the request can obviously be attached to a mixed output
1551 // and not explicitly requested
1552 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
1553 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
1554 audio_channel_count_from_out_mask(channelMask) <= 2) {
1555 goto non_direct_output;
1556 }
1557
1558 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1559 // creating an offloaded track and tearing it down immediately after start when audioflinger
1560 // detects there is an active non offloadable effect.
1561 // FIXME: We should check the audio session here but we do not have it in this context.
1562 // This may prevent offloading in rare situations where effects are left active by apps
1563 // in the background.
1564
Sharad Sangle36781612015-05-28 16:15:16 +05301565 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
1566 !mEffects.isNonOffloadableEffectEnabled()) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001567 profile = getProfileForDirectOutput(device,
1568 samplingRate,
1569 format,
1570 channelMask,
1571 (audio_output_flags_t)flags);
1572 }
1573
1574 if (profile != 0) {
Sharad Sangle36781612015-05-28 16:15:16 +05301575 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001576
1577 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +05301578 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001579 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1580 outputDesc = desc;
1581 // reuse direct output if currently open and configured with same parameters
1582 if ((samplingRate == outputDesc->mSamplingRate) &&
1583 (format == outputDesc->mFormat) &&
1584 (channelMask == outputDesc->mChannelMask)) {
1585 outputDesc->mDirectOpenCount++;
1586 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1587 return mOutputs.keyAt(i);
1588 }
1589 }
1590 }
1591 // close direct output if currently open and configured with different parameters
1592 if (outputDesc != NULL) {
1593 closeOutput(outputDesc->mIoHandle);
1594 }
Sharad Sangle36781612015-05-28 16:15:16 +05301595
1596 // if the selected profile is offloaded and no offload info was specified,
1597 // create a default one
1598 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
1599 if ((profile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
1600 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1601 defaultOffloadInfo.sample_rate = samplingRate;
1602 defaultOffloadInfo.channel_mask = channelMask;
1603 defaultOffloadInfo.format = format;
1604 defaultOffloadInfo.stream_type = stream;
1605 defaultOffloadInfo.bit_rate = 0;
1606 defaultOffloadInfo.duration_us = -1;
1607 defaultOffloadInfo.has_video = true; // conservative
1608 defaultOffloadInfo.is_streaming = true; // likely
1609 offloadInfo = &defaultOffloadInfo;
1610 }
1611
1612 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001613 outputDesc->mDevice = device;
1614 outputDesc->mLatency = 0;
Sharad Sangle36781612015-05-28 16:15:16 +05301615 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001616 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1617 config.sample_rate = samplingRate;
1618 config.channel_mask = channelMask;
1619 config.format = format;
1620 if (offloadInfo != NULL) {
1621 config.offload_info = *offloadInfo;
1622 }
Sharad Sangle36781612015-05-28 16:15:16 +05301623 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001624 &output,
1625 &config,
1626 &outputDesc->mDevice,
1627 String8(""),
1628 &outputDesc->mLatency,
1629 outputDesc->mFlags);
1630
1631 // only accept an output with the requested parameters
1632 if (status != NO_ERROR ||
1633 (samplingRate != 0 && samplingRate != config.sample_rate) ||
1634 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
1635 (channelMask != 0 && channelMask != config.channel_mask)) {
1636 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1637 "format %d %d, channelMask %04x %04x", output, samplingRate,
1638 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1639 outputDesc->mChannelMask);
1640 if (output != AUDIO_IO_HANDLE_NONE) {
1641 mpClientInterface->closeOutput(output);
1642 }
Sharad Sangle36781612015-05-28 16:15:16 +05301643 // fall back to mixer output if possible when the direct output could not be open
1644 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
1645 goto non_direct_output;
1646 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001647 return AUDIO_IO_HANDLE_NONE;
1648 }
1649 outputDesc->mSamplingRate = config.sample_rate;
1650 outputDesc->mChannelMask = config.channel_mask;
1651 outputDesc->mFormat = config.format;
1652 outputDesc->mRefCount[stream] = 0;
1653 outputDesc->mStopTime[stream] = 0;
1654 outputDesc->mDirectOpenCount = 1;
1655
1656 audio_io_handle_t srcOutput = getOutputForEffect();
1657 addOutput(output, outputDesc);
1658 audio_io_handle_t dstOutput = getOutputForEffect();
1659 if (dstOutput == output) {
1660 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1661 }
1662 mPreviousOutputs = mOutputs;
1663 ALOGV("getOutput() returns new direct output %d", output);
1664 mpClientInterface->onAudioPortListUpdate();
1665 return output;
1666 }
1667
1668non_direct_output:
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001669 // ignoring channel mask due to downmix capability in mixer
1670
1671 // open a non direct output
1672
1673 // for non direct outputs, only PCM is supported
1674 if (audio_is_linear_pcm(format)) {
1675 // get which output is suitable for the specified stream. The actual
1676 // routing change will happen when startOutput() will be called
1677 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1678
1679 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1680 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1681 output = selectOutput(outputs, flags, format);
1682 }
1683 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1684 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1685
vivek mehta0ea887a2015-08-26 14:01:20 -07001686 ALOGV("getOutputForDevice() returns output %d", output);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001687
1688 return output;
1689}
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301690
1691status_t AudioPolicyManagerCustom::getInputForAttr(const audio_attributes_t *attr,
1692 audio_io_handle_t *input,
1693 audio_session_t session,
1694 uid_t uid,
1695 uint32_t samplingRate,
1696 audio_format_t format,
1697 audio_channel_mask_t channelMask,
1698 audio_input_flags_t flags,
1699 audio_port_handle_t selectedDeviceId,
1700 input_type_t *inputType)
1701{
1702 audio_source_t inputSource = attr->source;
1703#ifdef VOICE_CONCURRENCY
1704
1705 char propValue[PROPERTY_VALUE_MAX];
1706 bool prop_rec_enabled=false, prop_voip_enabled = false;
1707
1708 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
1709 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1710 }
1711
1712 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1713 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1714 }
1715
1716 if (prop_rec_enabled && mvoice_call_state) {
1717 //check if voice call is active / running in background
1718 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1719 //Need to block input request
1720 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1721 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1722 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1723 {
1724 switch(inputSource) {
1725 case AUDIO_SOURCE_VOICE_UPLINK:
1726 case AUDIO_SOURCE_VOICE_DOWNLINK:
1727 case AUDIO_SOURCE_VOICE_CALL:
1728 ALOGD("voice_conc:Creating input during incall mode for inputSource: %d",
1729 inputSource);
1730 break;
1731
1732 case AUDIO_SOURCE_VOICE_COMMUNICATION:
1733 if(prop_voip_enabled) {
1734 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1735 inputSource);
1736 return NO_INIT;
1737 }
1738 break;
1739 default:
1740 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1741 inputSource);
1742 return NO_INIT;
1743 }
1744 }
1745 }//check for VoIP flag
1746 else if(prop_voip_enabled && mvoice_call_state) {
1747 //check if voice call is active / running in background
1748 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1749 //Need to block input request
1750 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1751 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1752 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1753 {
1754 if(inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1755 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
1756 return NO_INIT;
1757 }
1758 }
1759 }
1760
1761#endif
1762
1763 return AudioPolicyManager::getInputForAttr(attr,
1764 input,
1765 session,
1766 uid,
1767 samplingRate,
1768 format,
1769 channelMask,
1770 flags,
1771 selectedDeviceId,
1772 inputType);
1773}
1774status_t AudioPolicyManagerCustom::startInput(audio_io_handle_t input,
1775 audio_session_t session)
1776{
1777 ALOGV("startInput() input %d", input);
1778 ssize_t index = mInputs.indexOfKey(input);
1779 if (index < 0) {
1780 ALOGW("startInput() unknown input %d", input);
1781 return BAD_VALUE;
1782 }
1783 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1784
1785 index = inputDesc->mSessions.indexOf(session);
1786 if (index < 0) {
1787 ALOGW("startInput() unknown session %d on input %d", session, input);
1788 return BAD_VALUE;
1789 }
1790
1791 // virtual input devices are compatible with other input devices
1792 if (!is_virtual_input_device(inputDesc->mDevice)) {
1793
1794 // for a non-virtual input device, check if there is another (non-virtual) active input
1795 audio_io_handle_t activeInput = mInputs.getActiveInput();
1796 if (activeInput != 0 && activeInput != input) {
1797
1798 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1799 // otherwise the active input continues and the new input cannot be started.
1800 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
1801 if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
1802 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
1803 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
1804 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
1805 } else {
1806 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
1807 return INVALID_OPERATION;
1808 }
1809 }
1810 }
1811
1812 // Routing?
1813 mInputRoutes.incRouteActivity(session);
1814#ifdef RECORD_PLAY_CONCURRENCY
1815 mIsInputRequestOnProgress = true;
1816
1817 char getPropValue[PROPERTY_VALUE_MAX];
1818 bool prop_rec_play_enabled = false;
1819
1820 if (property_get("rec.playback.conc.disabled", getPropValue, NULL)) {
1821 prop_rec_play_enabled = atoi(getPropValue) || !strncmp("true", getPropValue, 4);
1822 }
1823
1824 if ((prop_rec_play_enabled) &&(mInputs.activeInputsCount() == 0)){
1825 // send update to HAL on record playback concurrency
1826 AudioParameter param = AudioParameter();
1827 param.add(String8("rec_play_conc_on"), String8("true"));
1828 ALOGD("startInput() setParameters rec_play_conc is setting to ON ");
1829 mpClientInterface->setParameters(0, param.toString());
1830
1831 // Call invalidate to reset all opened non ULL audio tracks
1832 // Move tracks associated to this strategy from previous output to new output
1833 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
1834 // Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder)
Sharad Sangle4509cef2015-08-19 20:47:12 +05301835 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE && (i != AUDIO_STREAM_PATCH))) {
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301836 ALOGD("Invalidate on releaseInput for stream :: %d ", i);
1837 //FIXME see fixme on name change
1838 mpClientInterface->invalidateStream((audio_stream_type_t)i);
1839 }
1840 }
1841 // close compress tracks
1842 for (size_t i = 0; i < mOutputs.size(); i++) {
1843 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
1844 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
1845 ALOGD("ouput desc / profile is NULL");
1846 continue;
1847 }
1848 if (outputDesc->mProfile->mFlags
1849 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1850 // close compress sessions
1851 ALOGD("calling closeOutput on record conc for COMPRESS output");
1852 closeOutput(mOutputs.keyAt(i));
1853 }
1854 }
1855 }
1856#endif
1857
1858 if (inputDesc->mRefCount == 0 || mInputRoutes.hasRouteChanged(session)) {
1859 // if input maps to a dynamic policy with an activity listener, notify of state change
1860 if ((inputDesc->mPolicyMix != NULL)
1861 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1862 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1863 MIX_STATE_MIXING);
1864 }
1865
1866 if (mInputs.activeInputsCount() == 0) {
1867 SoundTrigger::setCaptureState(true);
1868 }
1869 setInputDevice(input, getNewInputDevice(input), true /* force */);
1870
1871 // automatically enable the remote submix output when input is started if not
1872 // used by a policy mix of type MIX_TYPE_RECORDERS
1873 // For remote submix (a virtual device), we open only one input per capture request.
1874 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1875 String8 address = String8("");
1876 if (inputDesc->mPolicyMix == NULL) {
1877 address = String8("0");
1878 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1879 address = inputDesc->mPolicyMix->mRegistrationId;
1880 }
1881 if (address != "") {
1882 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1883 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1884 address, "remote-submix");
1885 }
1886 }
1887 }
1888
1889 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1890
1891 inputDesc->mRefCount++;
1892#ifdef RECORD_PLAY_CONCURRENCY
1893 mIsInputRequestOnProgress = false;
1894#endif
1895 return NO_ERROR;
1896}
1897status_t AudioPolicyManagerCustom::stopInput(audio_io_handle_t input,
1898 audio_session_t session)
1899{
1900 status_t status;
1901 status = AudioPolicyManager::stopInput(input, session);
1902#ifdef RECORD_PLAY_CONCURRENCY
1903 char propValue[PROPERTY_VALUE_MAX];
1904 bool prop_rec_play_enabled = false;
1905
1906 if (property_get("rec.playback.conc.disabled", propValue, NULL)) {
1907 prop_rec_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1908 }
1909
1910 if ((prop_rec_play_enabled) && (mInputs.activeInputsCount() == 0)) {
1911
1912 //send update to HAL on record playback concurrency
1913 AudioParameter param = AudioParameter();
1914 param.add(String8("rec_play_conc_on"), String8("false"));
1915 ALOGD("stopInput() setParameters rec_play_conc is setting to OFF ");
1916 mpClientInterface->setParameters(0, param.toString());
1917
1918 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
1919 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
1920 //Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder stop tone)
1921 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE) && (i != AUDIO_STREAM_PATCH)) {
1922 ALOGD(" Invalidate on stopInput for stream :: %d ", i);
1923 //FIXME see fixme on name change
1924 mpClientInterface->invalidateStream((audio_stream_type_t)i);
1925 }
1926 }
1927 }
1928#endif
1929 return status;
1930}
1931
1932AudioPolicyManagerCustom::AudioPolicyManagerCustom(AudioPolicyClientInterface *clientInterface)
Sharad Sangle4509cef2015-08-19 20:47:12 +05301933 : AudioPolicyManager(clientInterface),
1934 mHdmiAudioDisabled(false),
1935 mHdmiAudioEvent(false),
1936 mPrevPhoneState(0)
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301937{
Mingming Yin38ea08c2015-10-05 15:24:04 -07001938 char ssr_enabled[PROPERTY_VALUE_MAX] = {0};
1939 bool prop_ssr_enabled = false;
1940
1941 if (property_get("ro.qc.sdk.audio.ssr", ssr_enabled, NULL)) {
1942 prop_ssr_enabled = atoi(ssr_enabled) || !strncmp("true", ssr_enabled, 4);
1943 }
1944
1945 for (size_t i = 0; i < mHwModules.size(); i++) {
1946 ALOGV("Hw module %d", i);
1947 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) {
1948 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
1949 ALOGV("Input profile ", j);
1950 for (size_t k = 0; k < inProfile->mChannelMasks.size(); k++) {
1951 audio_channel_mask_t channelMask =
1952 inProfile->mChannelMasks.itemAt(k);
1953 ALOGV("Channel Mask %x size %d", channelMask,
1954 inProfile->mChannelMasks.size());
1955 if (AUDIO_CHANNEL_IN_5POINT1 == channelMask) {
1956 if (!prop_ssr_enabled) {
1957 ALOGI("removing AUDIO_CHANNEL_IN_5POINT1 from"
1958 " input profile as SSR(surround sound record)"
1959 " is not supported on this chipset variant");
1960 inProfile->mChannelMasks.removeItemsAt(k, 1);
1961 ALOGV("Channel Mask size now %d",
1962 inProfile->mChannelMasks.size());
1963 }
1964 }
1965 }
1966 }
1967 }
1968
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301969#ifdef RECORD_PLAY_CONCURRENCY
1970 mIsInputRequestOnProgress = false;
1971#endif
1972
1973
1974#ifdef VOICE_CONCURRENCY
1975 mFallBackflag = getFallBackPath();
1976#endif
1977}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001978}