blob: b54dfe3db9018dc253ce37797a73216e7db484f0 [file] [log] [blame]
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001/*
Arne Coucheron3b374442016-09-03 06:44:26 +02002 * Copyright (c) 2013-2016, 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.
Gao Jiec2e73aa2015-11-12 08:51:22 +080018 *
19 * This file was modified by Dolby Laboratories, Inc. The portions of the
20 * code that are surrounded by "DOLBY..." are copyrighted and
21 * licensed separately, as follows:
22 *
23 * (C) 2015 Dolby Laboratories, Inc.
24 *
25 * Licensed under the Apache License, Version 2.0 (the "License");
26 * you may not use this file except in compliance with the License.
27 * You may obtain a copy of the License at
28 *
29 * http://www.apache.org/licenses/LICENSE-2.0
30 *
31 * Unless required by applicable law or agreed to in writing, software
32 * distributed under the License is distributed on an "AS IS" BASIS,
33 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34 * See the License for the specific language governing permissions and
35 * limitations under the License.
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070036 */
37
Sharad Sangleca67a022015-05-28 16:15:16 +053038#define LOG_TAG "AudioPolicyManagerCustom"
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070039//#define LOG_NDEBUG 0
40
41//#define VERY_VERBOSE_LOGGING
42#ifdef VERY_VERBOSE_LOGGING
43#define ALOGVV ALOGV
44#else
45#define ALOGVV(a...) do { } while(0)
46#endif
47
Sharad Sangleca67a022015-05-28 16:15:16 +053048#define MIN(a, b) ((a) < (b) ? (a) : (b))
49
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070050// A device mask for all audio output devices that are considered "remote" when evaluating
51// active output devices in isStreamActiveRemotely()
52#define APM_AUDIO_OUT_DEVICE_REMOTE_ALL AUDIO_DEVICE_OUT_REMOTE_SUBMIX
Sharad Sangleca67a022015-05-28 16:15:16 +053053// A device mask for all audio input and output devices where matching inputs/outputs on device
54// type alone is not enough: the address must match too
55#define APM_AUDIO_DEVICE_MATCH_ADDRESS_ALL (AUDIO_DEVICE_IN_REMOTE_SUBMIX | \
56 AUDIO_DEVICE_OUT_REMOTE_SUBMIX)
57// Following delay should be used if the calculated routing delay from all active
58// input streams is higher than this value
59#define MAX_VOICE_CALL_START_DELAY_MS 100
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070060
Sharad Sangleca67a022015-05-28 16:15:16 +053061#include <inttypes.h>
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070062#include <math.h>
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070063
Sharad Sangleca67a022015-05-28 16:15:16 +053064#include <cutils/properties.h>
65#include <utils/Log.h>
66#include <hardware/audio.h>
67#include <hardware/audio_effect.h>
68#include <media/AudioParameter.h>
69#include <soundtrigger/SoundTrigger.h>
70#include "AudioPolicyManager.h"
71#include <policy.h>
Gao Jiec2e73aa2015-11-12 08:51:22 +080072#ifdef DOLBY_ENABLE
73#include "DolbyAudioPolicy_impl.h"
74#endif // DOLBY_END
Sharad Sangleca67a022015-05-28 16:15:16 +053075
76namespace android {
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070077
78// ----------------------------------------------------------------------------
79// AudioPolicyInterface implementation
80// ----------------------------------------------------------------------------
Sharad Sangleca67a022015-05-28 16:15:16 +053081extern "C" AudioPolicyInterface* createAudioPolicyManager(
82 AudioPolicyClientInterface *clientInterface)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070083{
Sharad Sangleca67a022015-05-28 16:15:16 +053084 return new AudioPolicyManagerCustom(clientInterface);
85}
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070086
Sharad Sangleca67a022015-05-28 16:15:16 +053087extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface)
88{
89 delete interface;
90}
91
92status_t AudioPolicyManagerCustom::setDeviceConnectionStateInt(audio_devices_t device,
93 audio_policy_dev_state_t state,
94 const char *device_address,
95 const char *device_name)
96{
Arne Coucheron3b374442016-09-03 06:44:26 +020097 ALOGD("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
Sharad Sangleca67a022015-05-28 16:15:16 +053098 device, state, device_address, device_name);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070099
100 // connect/disconnect only 1 device at a time
101 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
102
Sharad Sangleca67a022015-05-28 16:15:16 +0530103 sp<DeviceDescriptor> devDesc =
104 mHwModules.getDeviceDescriptor(device, device_address, device_name);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700105
106 // handle output devices
107 if (audio_is_output_device(device)) {
Sharad Sangleca67a022015-05-28 16:15:16 +0530108 SortedVector <audio_io_handle_t> outputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700109
Sharad Sangleca67a022015-05-28 16:15:16 +0530110 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700111
112 // save a copy of the opened output descriptors before any output is opened or closed
113 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
114 mPreviousOutputs = mOutputs;
115 switch (state)
116 {
117 // handle output device connection
Sharad Sangleca67a022015-05-28 16:15:16 +0530118 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
119 if (index >= 0) {
Ramjee Singhae5f8902015-08-19 20:47:12 +0530120#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
121 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
122 if (!strncmp(device_address, "hdmi_spkr", 9)) {
123 mHdmiAudioDisabled = false;
124 } else {
125 mHdmiAudioEvent = true;
126 }
127 }
128#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700129 ALOGW("setDeviceConnectionState() device already connected: %x", device);
130 return INVALID_OPERATION;
131 }
132 ALOGV("setDeviceConnectionState() connecting device %x", device);
133
Sharad Sangleca67a022015-05-28 16:15:16 +0530134 // register new device as available
135 index = mAvailableOutputDevices.add(devDesc);
Ramjee Singhae5f8902015-08-19 20:47:12 +0530136#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
137 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
138 if (!strncmp(device_address, "hdmi_spkr", 9)) {
139 mHdmiAudioDisabled = false;
140 } else {
141 mHdmiAudioEvent = true;
142 }
143 if (mHdmiAudioDisabled || !mHdmiAudioEvent) {
144 mAvailableOutputDevices.remove(devDesc);
145 ALOGW("HDMI sink not connected, do not route audio to HDMI out");
146 return INVALID_OPERATION;
147 }
148 }
149#endif
Sharad Sangleca67a022015-05-28 16:15:16 +0530150 if (index >= 0) {
151 sp<HwModule> module = mHwModules.getModuleForDevice(device);
152 if (module == 0) {
153 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
154 device);
155 mAvailableOutputDevices.remove(devDesc);
156 return INVALID_OPERATION;
157 }
158 mAvailableOutputDevices[index]->attach(module);
159 } else {
160 return NO_MEMORY;
161 }
162
163 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
164 mAvailableOutputDevices.remove(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700165 return INVALID_OPERATION;
166 }
Sharad Sangleca67a022015-05-28 16:15:16 +0530167 // Propagate device availability to Engine
168 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700169
Sharad Sangleca67a022015-05-28 16:15:16 +0530170 // outputs should never be empty here
171 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
172 "checkOutputsForDevice() returned no outputs but status OK");
173 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
174 outputs.size());
175
176 // Send connect to HALs
177 AudioParameter param = AudioParameter(devDesc->mAddress);
178 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
179 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
180
181 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700182 // handle output device disconnection
Sharad Sangleca67a022015-05-28 16:15:16 +0530183 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
184 if (index < 0) {
Ramjee Singhae5f8902015-08-19 20:47:12 +0530185#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
186 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
187 if (!strncmp(device_address, "hdmi_spkr", 9)) {
188 mHdmiAudioDisabled = true;
189 } else {
190 mHdmiAudioEvent = false;
191 }
192 }
193#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700194 ALOGW("setDeviceConnectionState() device not connected: %x", device);
195 return INVALID_OPERATION;
196 }
197
Sharad Sangleca67a022015-05-28 16:15:16 +0530198 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700199
Sharad Sangleca67a022015-05-28 16:15:16 +0530200 // Send Disconnect to HALs
201 AudioParameter param = AudioParameter(devDesc->mAddress);
202 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
203 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
204
205 // remove device from available output devices
206 mAvailableOutputDevices.remove(devDesc);
Ramjee Singhae5f8902015-08-19 20:47:12 +0530207#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
208 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
209 if (!strncmp(device_address, "hdmi_spkr", 9)) {
210 mHdmiAudioDisabled = true;
211 } else {
212 mHdmiAudioEvent = false;
213 }
214 }
215#endif
Sharad Sangleca67a022015-05-28 16:15:16 +0530216 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
217
218 // Propagate device availability to Engine
219 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700220 } break;
221
222 default:
223 ALOGE("setDeviceConnectionState() invalid state: %x", state);
224 return BAD_VALUE;
225 }
226
Sharad Sangleca67a022015-05-28 16:15:16 +0530227 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
228 // output is suspended before any tracks are moved to it
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700229 checkA2dpSuspend();
230 checkOutputForAllStrategies();
231 // outputs must be closed after checkOutputForAllStrategies() is executed
232 if (!outputs.isEmpty()) {
233 for (size_t i = 0; i < outputs.size(); i++) {
Sharad Sangleca67a022015-05-28 16:15:16 +0530234 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700235 // close unused outputs after device disconnection or direct outputs that have been
236 // opened by checkOutputsForDevice() to query dynamic parameters
Sharad Sangleca67a022015-05-28 16:15:16 +0530237 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700238 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
239 (desc->mDirectOpenCount == 0))) {
240 closeOutput(outputs[i]);
241 }
242 }
Sharad Sangleca67a022015-05-28 16:15:16 +0530243 // check again after closing A2DP output to reset mA2dpSuspended if needed
244 checkA2dpSuspend();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700245 }
246
247 updateDevicesAndOutputs();
Gao Jiec2e73aa2015-11-12 08:51:22 +0800248#ifdef DOLBY_ENABLE
249 // Before closing the opened outputs, update endpoint property with device capabilities
250 audio_devices_t audioOutputDevice = getDeviceForStrategy(getStrategy(AUDIO_STREAM_MUSIC), true);
251 mDolbyAudioPolicy.setEndpointSystemProperty(audioOutputDevice, mHwModules);
252#endif // DOLBY_END
Sharad Sangleca67a022015-05-28 16:15:16 +0530253 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
254 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
255 updateCallRouting(newDevice);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700256 }
Ramjee Singh5300daa2015-11-18 13:31:47 +0530257
258#ifdef FM_POWER_OPT
259 // handle FM device connection state to trigger FM AFE loopback
260 if(device == AUDIO_DEVICE_OUT_FM && hasPrimaryOutput()) {
261 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
262 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
263 mPrimaryOutput->changeRefCount(AUDIO_STREAM_MUSIC, 1);
264 newDevice = (audio_devices_t)getNewOutputDevice(mPrimaryOutput, false /*fromCache*/) | AUDIO_DEVICE_OUT_FM;
265 } else {
266 mPrimaryOutput->changeRefCount(AUDIO_STREAM_MUSIC, -1);
267 }
268 AudioParameter param = AudioParameter();
269 param.addInt(String8("handle_fm"), (int)newDevice);
270 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString());
271 }
272#endif /* FM_POWER_OPT end */
273
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700274 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangleca67a022015-05-28 16:15:16 +0530275 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
276 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
277 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
278 // do not force device change on duplicated output because if device is 0, it will
279 // also force a device 0 for the two outputs it is duplicated to which may override
280 // a valid device selection on those outputs.
281 bool force = !desc->isDuplicated()
282 && (!device_distinguishes_on_address(device)
283 // always force when disconnecting (a non-duplicated device)
284 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
285 setOutputDevice(desc, newDevice, force, 0);
Satya Krishna Pindiproli5cf8c1a2014-05-08 12:22:16 +0530286 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700287 }
288
Arne Coucheron3b374442016-09-03 06:44:26 +0200289 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
290 cleanUpForDevice(devDesc);
291 }
292
Sharad Sangleca67a022015-05-28 16:15:16 +0530293 mpClientInterface->onAudioPortListUpdate();
294 return NO_ERROR;
295 } // end if is output device
296
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700297 // handle input devices
298 if (audio_is_input_device(device)) {
Sharad Sangleca67a022015-05-28 16:15:16 +0530299 SortedVector <audio_io_handle_t> inputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700300
Sharad Sangleca67a022015-05-28 16:15:16 +0530301 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700302 switch (state)
303 {
304 // handle input device connection
Sharad Sangleca67a022015-05-28 16:15:16 +0530305 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
306 if (index >= 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700307 ALOGW("setDeviceConnectionState() device already connected: %d", device);
308 return INVALID_OPERATION;
309 }
Sharad Sangleca67a022015-05-28 16:15:16 +0530310 sp<HwModule> module = mHwModules.getModuleForDevice(device);
311 if (module == NULL) {
312 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
313 device);
314 return INVALID_OPERATION;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700315 }
Sharad Sangleca67a022015-05-28 16:15:16 +0530316 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
317 return INVALID_OPERATION;
318 }
319
320 index = mAvailableInputDevices.add(devDesc);
321 if (index >= 0) {
322 mAvailableInputDevices[index]->attach(module);
323 } else {
324 return NO_MEMORY;
325 }
326
327 // Set connect to HALs
328 AudioParameter param = AudioParameter(devDesc->mAddress);
329 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
330 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
331
332 // Propagate device availability to Engine
333 mEngine->setDeviceConnectionState(devDesc, state);
334 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700335
336 // handle input device disconnection
Sharad Sangleca67a022015-05-28 16:15:16 +0530337 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
338 if (index < 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700339 ALOGW("setDeviceConnectionState() device not connected: %d", device);
340 return INVALID_OPERATION;
341 }
Sharad Sangleca67a022015-05-28 16:15:16 +0530342
343 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
344
345 // Set Disconnect to HALs
346 AudioParameter param = AudioParameter(devDesc->mAddress);
347 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
348 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
349
350 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
351 mAvailableInputDevices.remove(devDesc);
352
353 // Propagate device availability to Engine
354 mEngine->setDeviceConnectionState(devDesc, state);
355 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700356
357 default:
358 ALOGE("setDeviceConnectionState() invalid state: %x", state);
359 return BAD_VALUE;
360 }
361
Sharad Sangleca67a022015-05-28 16:15:16 +0530362 closeAllInputs();
363
364 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
365 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
366 updateCallRouting(newDevice);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700367 }
368
Arne Coucheron3b374442016-09-03 06:44:26 +0200369 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
370 cleanUpForDevice(devDesc);
371 }
372
Sharad Sangleca67a022015-05-28 16:15:16 +0530373 mpClientInterface->onAudioPortListUpdate();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700374 return NO_ERROR;
Sharad Sangleca67a022015-05-28 16:15:16 +0530375 } // end if is input device
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700376
377 ALOGW("setDeviceConnectionState() invalid device: %x", device);
378 return BAD_VALUE;
379}
ApurupaPattapu0c566872014-01-10 14:46:02 -0800380// This function checks for the parameters which can be offloaded.
381// This can be enhanced depending on the capability of the DSP and policy
382// of the system.
Sharad Sangleca67a022015-05-28 16:15:16 +0530383bool AudioPolicyManagerCustom::isOffloadSupported(const audio_offload_info_t& offloadInfo)
ApurupaPattapu0c566872014-01-10 14:46:02 -0800384{
Sharad Sangleca67a022015-05-28 16:15:16 +0530385 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
386 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
ApurupaPattapu0c566872014-01-10 14:46:02 -0800387 offloadInfo.sample_rate, offloadInfo.channel_mask,
388 offloadInfo.format,
389 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
390 offloadInfo.has_video);
391
Arne Coucheron3b374442016-09-03 06:44:26 +0200392 if (mMasterMono) {
393 return false; // no offloading if mono is set.
394 }
395
ApurupaPattapu0c566872014-01-10 14:46:02 -0800396 // Check if stream type is music, then only allow offload as of now.
397 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
398 {
399 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
400 return false;
401 }
ApurupaPattapu0c566872014-01-10 14:46:02 -0800402
Alexy Josephb970dcb2015-10-16 15:57:53 -0700403 // Check if offload has been disabled
404 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
405 if (offloadDisabled) {
406 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
407 return false;
408 }
409
Preetam Singh Ranawatfdba8132015-07-21 19:30:09 +0530410 char propValue[PROPERTY_VALUE_MAX];
411 bool pcmOffload = false;
Preetam Singh Ranawatfdba8132015-07-21 19:30:09 +0530412 if ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM_OFFLOAD) {
413 bool prop_enabled = false;
414 if ((AUDIO_FORMAT_PCM_16_BIT_OFFLOAD == offloadInfo.format) &&
415 property_get("audio.offload.pcm.16bit.enable", propValue, NULL)) {
416 prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
417 }
418
Preetam Singh Ranawatfdba8132015-07-21 19:30:09 +0530419 if ((AUDIO_FORMAT_PCM_24_BIT_OFFLOAD == offloadInfo.format) &&
420 property_get("audio.offload.pcm.24bit.enable", propValue, NULL)) {
421 prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
422 }
Preetam Singh Ranawatfdba8132015-07-21 19:30:09 +0530423
424 if (prop_enabled) {
425 ALOGI("PCM offload property is enabled");
426 pcmOffload = true;
427 }
428
429 if (!pcmOffload) {
430 ALOGD("system property not enabled for PCM offload format[%x]",offloadInfo.format);
431 return false;
432 }
433 }
Preetam Singh Ranawatfdba8132015-07-21 19:30:09 +0530434 if (!pcmOffload) {
Alexy Josephb970dcb2015-10-16 15:57:53 -0700435
436 bool compressedOffloadDisabled = property_get_bool("audio.offload.compress.disable", false);
437 if (compressedOffloadDisabled) {
438 ALOGI("compressed offload disabled by audio.offload.compress.disable=%d", compressedOffloadDisabled);
439 return false;
Preetam Singh Ranawatfdba8132015-07-21 19:30:09 +0530440 }
Alexy Josephb970dcb2015-10-16 15:57:53 -0700441
Preetam Singh Ranawatfdba8132015-07-21 19:30:09 +0530442 //check if it's multi-channel AAC (includes sub formats) and FLAC format
443 if ((popcount(offloadInfo.channel_mask) > 2) &&
444 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
445 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS))) {
446 ALOGD("offload disabled for multi-channel AAC,FLAC and VORBIS format");
447 return false;
Preetam Singh Ranawatfdba8132015-07-21 19:30:09 +0530448 }
Alexy Joseph505da3f2015-09-10 01:31:51 -0700449
450 //check if it's multi-channel FLAC/ALAC/WMA format with sample rate > 48k
451 if (popcount(offloadInfo.channel_mask) > 2) {
452#ifdef FLAC_OFFLOAD_ENABLED
453 if (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) && offloadInfo.sample_rate > 48000) {
454 return false;
455 }
Preetam Singh Ranawatfdba8132015-07-21 19:30:09 +0530456#endif
Alexy Joseph505da3f2015-09-10 01:31:51 -0700457#ifdef WMA_OFFLOAD_ENABLED
458 if ((((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) && offloadInfo.sample_rate > 48000) ||
459 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) && offloadInfo.sample_rate > 48000)) {
460 return false;
461 }
462#endif
463 }
464
Preetam Singh Ranawatfdba8132015-07-21 19:30:09 +0530465 //TODO: enable audio offloading with video when ready
466 const bool allowOffloadWithVideo =
467 property_get_bool("audio.offload.video", false /* default_value */);
468 if (offloadInfo.has_video && !allowOffloadWithVideo) {
469 ALOGV("isOffloadSupported: has_video == true, returning false");
470 return false;
471 }
Manish Dewangan66e27c92015-10-13 14:04:36 +0530472
473 const bool allowOffloadStreamingWithVideo = property_get_bool("av.streaming.offload.enable",
474 false /*default value*/);
475 if(offloadInfo.has_video && offloadInfo.is_streaming && !allowOffloadStreamingWithVideo) {
Keith Mok17067a92016-05-10 12:59:09 -0700476 ALOGW("offload disabled by av.streaming.offload.enable = %d",
477 allowOffloadStreamingWithVideo);
Manish Dewangan66e27c92015-10-13 14:04:36 +0530478 return false;
479 }
480
ApurupaPattapu0c566872014-01-10 14:46:02 -0800481 }
482
483 //If duration is less than minimum value defined in property, return false
484 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
485 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
486 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
487 return false;
488 }
489 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
490 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
Sharad Sangleca67a022015-05-28 16:15:16 +0530491 //duration checks only valid for MP3/AAC/ formats,
ApurupaPattapu0c566872014-01-10 14:46:02 -0800492 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
Sharad Sangleca67a022015-05-28 16:15:16 +0530493 if ((offloadInfo.format == AUDIO_FORMAT_MP3) ||
494 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
Alexy Joseph505da3f2015-09-10 01:31:51 -0700495#ifdef FLAC_OFFLOAD_ENABLED
Sharad Sangleca67a022015-05-28 16:15:16 +0530496 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
Alexy Joseph505da3f2015-09-10 01:31:51 -0700497#endif
498#ifdef WMA_OFFLOAD_ENABLED
Sharad Sangleca67a022015-05-28 16:15:16 +0530499 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) ||
500 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) ||
Alexy Joseph505da3f2015-09-10 01:31:51 -0700501#endif
502 pcmOffload)
ApurupaPattapu0c566872014-01-10 14:46:02 -0800503 return false;
Sharad Sangleca67a022015-05-28 16:15:16 +0530504
ApurupaPattapu0c566872014-01-10 14:46:02 -0800505 }
506
507 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
508 // creating an offloaded track and tearing it down immediately after start when audioflinger
509 // detects there is an active non offloadable effect.
510 // FIXME: We should check the audio session here but we do not have it in this context.
511 // This may prevent offloading in rare situations where effects are left active by apps
512 // in the background.
Sharad Sangleca67a022015-05-28 16:15:16 +0530513 if (mEffects.isNonOffloadableEffectEnabled()) {
ApurupaPattapu0c566872014-01-10 14:46:02 -0800514 return false;
515 }
Sharad Sangleca67a022015-05-28 16:15:16 +0530516 // Check for soundcard status
517 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
518 String8("SND_CARD_STATUS"));
519 AudioParameter result = AudioParameter(valueStr);
520 int isonline = 0;
521 if ((result.getInt(String8("SND_CARD_STATUS"), isonline) == NO_ERROR)
522 && !isonline) {
523 ALOGD("copl: soundcard is offline rejecting offload request");
524 return false;
525 }
ApurupaPattapu0c566872014-01-10 14:46:02 -0800526 // See if there is a profile to support this.
527 // AUDIO_DEVICE_NONE
Sharad Sangleca67a022015-05-28 16:15:16 +0530528 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
ApurupaPattapu0c566872014-01-10 14:46:02 -0800529 offloadInfo.sample_rate,
530 offloadInfo.format,
531 offloadInfo.channel_mask,
532 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Sharad Sangleca67a022015-05-28 16:15:16 +0530533 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
534 return (profile != 0);
ApurupaPattapu0c566872014-01-10 14:46:02 -0800535}
Sharad Sangleca67a022015-05-28 16:15:16 +0530536audio_devices_t AudioPolicyManagerCustom::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
537 bool fromCache)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700538{
Sharad Sangleca67a022015-05-28 16:15:16 +0530539 audio_devices_t device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700540
Arne Coucheron3b374442016-09-03 06:44:26 +0200541 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Sharad Sangleca67a022015-05-28 16:15:16 +0530542 if (index >= 0) {
543 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
544 if (patchDesc->mUid != mUidCached) {
545 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Arne Coucheron3b374442016-09-03 06:44:26 +0200546 outputDesc->device(), outputDesc->getPatchHandle());
Sharad Sangleca67a022015-05-28 16:15:16 +0530547 return outputDesc->device();
548 }
549 }
550
551 // check the following by order of priority to request a routing change if necessary:
552 // 1: the strategy enforced audible is active and enforced on the output:
553 // use device for strategy enforced audible
554 // 2: we are in call or the strategy phone is active on the output:
555 // use device for strategy phone
556 // 3: the strategy for enforced audible is active but not enforced on the output:
557 // use the device for strategy enforced audible
558 // 4: the strategy sonification is active on the output:
559 // use device for strategy sonification
560 // 5: the strategy "respectful" sonification is active on the output:
561 // use device for strategy "respectful" sonification
562 // 6: the strategy accessibility is active on the output:
563 // use device for strategy accessibility
564 // 7: the strategy media is active on the output:
565 // use device for strategy media
566 // 8: the strategy DTMF is active on the output:
567 // use device for strategy DTMF
568 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
569 // use device for strategy t-t-s
570 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
571 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
572 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
573 } else if (isInCall() ||
574 isStrategyActive(outputDesc, STRATEGY_PHONE)||
575 isStrategyActive(mPrimaryOutput, STRATEGY_PHONE)) {
576 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
577 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
578 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
579 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)||
580 (isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION)
581 && (!isStrategyActive(mPrimaryOutput,STRATEGY_MEDIA)))) {
582 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
583 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)||
584 (isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION_RESPECTFUL)
585 && (!isStrategyActive(mPrimaryOutput, STRATEGY_MEDIA)))) {
586 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
587 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
588 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
589 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
590 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
591 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
592 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
593 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
594 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
595 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
596 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
597 }
598
599 ALOGV("getNewOutputDevice() selected device %x", device);
600 return device;
601}
602void AudioPolicyManagerCustom::setPhoneState(audio_mode_t state)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700603{
Arne Coucheron3b374442016-09-03 06:44:26 +0200604 ALOGD("setPhoneState() state %d", state);
Sharad Sangleca67a022015-05-28 16:15:16 +0530605 // store previous phone state for management of sonification strategy below
Ramjee Singhae5f8902015-08-19 20:47:12 +0530606 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Sharad Sangleca67a022015-05-28 16:15:16 +0530607 int oldState = mEngine->getPhoneState();
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700608
Sharad Sangleca67a022015-05-28 16:15:16 +0530609 if (mEngine->setPhoneState(state) != NO_ERROR) {
610 ALOGW("setPhoneState() invalid or same state %d", state);
611 return;
612 }
613 /// Opens: can these line be executed after the switch of volume curves???
614 // if leaving call state, handle special case of active streams
615 // pertaining to sonification strategy see handleIncallSonification()
Ramjee Singhf9345b32015-10-13 18:13:04 +0530616 if (isStateInCall(oldState)) {
Sharad Sangleca67a022015-05-28 16:15:16 +0530617 ALOGV("setPhoneState() in call state management: new state is %d", state);
618 for (size_t j = 0; j < mOutputs.size(); j++) {
619 audio_io_handle_t curOutput = mOutputs.keyAt(j);
620 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
621 if (stream == AUDIO_STREAM_PATCH) {
622 continue;
623 }
624
625 handleIncallSonification((audio_stream_type_t)stream, false, true, curOutput);
626 }
627 }
628
629 // force reevaluating accessibility routing when call starts
630 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
631 }
632
633 /**
634 * Switching to or from incall state or switching between telephony and VoIP lead to force
635 * routing command.
636 */
637 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
638 || (is_state_in_call(state) && (state != oldState)));
639
640 // check for device and output changes triggered by new phone state
641 checkA2dpSuspend();
642 checkOutputForAllStrategies();
643 updateDevicesAndOutputs();
644
645 sp<SwAudioOutputDescriptor> hwOutputDesc = mPrimaryOutput;
646
647 int delayMs = 0;
648 if (isStateInCall(state)) {
649 nsecs_t sysTime = systemTime();
650 for (size_t i = 0; i < mOutputs.size(); i++) {
651 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
652 // mute media and sonification strategies and delay device switch by the largest
653 // latency of any output where either strategy is active.
654 // This avoid sending the ring tone or music tail into the earpiece or headset.
655 if ((isStrategyActive(desc, STRATEGY_MEDIA,
656 SONIFICATION_HEADSET_MUSIC_DELAY,
657 sysTime) ||
658 isStrategyActive(desc, STRATEGY_SONIFICATION,
659 SONIFICATION_HEADSET_MUSIC_DELAY,
660 sysTime)) &&
661 (delayMs < (int)desc->latency()*2)) {
662 delayMs = desc->latency()*2;
663 }
664 setStrategyMute(STRATEGY_MEDIA, true, desc);
665 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
666 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
667 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
668 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
669 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
670 }
671 ALOGV("Setting the delay from %dms to %dms", delayMs,
672 MIN(delayMs, MAX_VOICE_CALL_START_DELAY_MS));
673 delayMs = MIN(delayMs, MAX_VOICE_CALL_START_DELAY_MS);
674 }
675
676 if (hasPrimaryOutput()) {
677 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
678 // the device returned is not necessarily reachable via this output
679 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
680 // force routing command to audio hardware when ending call
681 // even if no device change is needed
682 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
683 rxDevice = mPrimaryOutput->device();
684 }
685
686 if (state == AUDIO_MODE_IN_CALL) {
687 updateCallRouting(rxDevice, delayMs);
688 } else if (oldState == AUDIO_MODE_IN_CALL) {
689 if (mCallRxPatch != 0) {
690 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
691 mCallRxPatch.clear();
692 }
693 if (mCallTxPatch != 0) {
694 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
695 mCallTxPatch.clear();
696 }
697 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
698 } else {
699 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
700 }
701 }
Ramjee Singhae5f8902015-08-19 20:47:12 +0530702 //update device for all non-primary outputs
703 for (size_t i = 0; i < mOutputs.size(); i++) {
704 audio_io_handle_t output = mOutputs.keyAt(i);
705 if (output != mPrimaryOutput->mIoHandle) {
706 newDevice = getNewOutputDevice(mOutputs.valueFor(output), false /*fromCache*/);
707 setOutputDevice(mOutputs.valueFor(output), newDevice, (newDevice != AUDIO_DEVICE_NONE));
708 }
709 }
Sharad Sangleca67a022015-05-28 16:15:16 +0530710 // if entering in call state, handle special case of active streams
711 // pertaining to sonification strategy see handleIncallSonification()
712 if (isStateInCall(state)) {
713 ALOGV("setPhoneState() in call state management: new state is %d", state);
714 for (size_t j = 0; j < mOutputs.size(); j++) {
715 audio_io_handle_t curOutput = mOutputs.keyAt(j);
716 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
717 if (stream == AUDIO_STREAM_PATCH) {
718 continue;
719 }
720 handleIncallSonification((audio_stream_type_t)stream, true, true, curOutput);
721 }
722 }
723 }
724
725 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
726 if (state == AUDIO_MODE_RINGTONE &&
727 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
728 mLimitRingtoneVolume = true;
729 } else {
730 mLimitRingtoneVolume = false;
731 }
732}
Dhananjay Kumar42441032015-09-16 19:44:33 +0530733
734void AudioPolicyManagerCustom::setForceUse(audio_policy_force_use_t usage,
735 audio_policy_forced_cfg_t config)
736{
Arne Coucheron3b374442016-09-03 06:44:26 +0200737 ALOGD("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Dhananjay Kumar42441032015-09-16 19:44:33 +0530738
739 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
740 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
741 return;
742 }
743 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
744 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
745 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
746
747 // check for device and output changes triggered by new force usage
748 checkA2dpSuspend();
749 checkOutputForAllStrategies();
750 updateDevicesAndOutputs();
Arne Coucheron3b374442016-09-03 06:44:26 +0200751
Dhananjay Kumar42441032015-09-16 19:44:33 +0530752 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
753 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
754 updateCallRouting(newDevice);
755 }
756 // Use reverse loop to make sure any low latency usecases (generally tones)
757 // are not routed before non LL usecases (generally music).
758 // We can safely assume that LL output would always have lower index,
759 // and use this work-around to avoid routing of output with music stream
760 // from the context of short lived LL output.
761 // Note: in case output's share backend(HAL sharing is implicit) all outputs
762 // gets routing update while processing first output itself.
763 for (size_t i = mOutputs.size(); i > 0; i--) {
764 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i-1);
765 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
766 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || outputDesc != mPrimaryOutput) {
767 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
768 }
769 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
770 applyStreamVolumes(outputDesc, newDevice, 0, true);
771 }
772 }
773
774 audio_io_handle_t activeInput = mInputs.getActiveInput();
775 if (activeInput != 0) {
Arne Coucheron3b374442016-09-03 06:44:26 +0200776 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
777 audio_devices_t newDevice = getNewInputDevice(activeInput);
778 // Force new input selection if the new device can not be reached via current input
779 if (activeDesc->mProfile->getSupportedDevices().types() & (newDevice & ~AUDIO_DEVICE_BIT_IN)) {
780 setInputDevice(activeInput, newDevice);
781 } else {
782 closeInput(activeInput);
783 }
Dhananjay Kumar42441032015-09-16 19:44:33 +0530784 }
785
786}
787
Dhananjay Kumard3bfcb32015-10-20 17:56:50 +0530788status_t AudioPolicyManagerCustom::stopSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangleca67a022015-05-28 16:15:16 +0530789 audio_stream_type_t stream,
790 bool forceDeviceUpdate)
791{
Dhananjay Kumard3bfcb32015-10-20 17:56:50 +0530792 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
793 ALOGW("stopSource() invalid stream %d", stream);
794 return INVALID_OPERATION;
795 }
Sharad Sangleca67a022015-05-28 16:15:16 +0530796 // always handle stream stop, check which stream type is stopping
797 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
798
799 // handle special case for sonification while in call
Ramjee Singhae5f8902015-08-19 20:47:12 +0530800 if (isInCall() && (outputDesc->mRefCount[stream] == 1)) {
Sharad Sangleca67a022015-05-28 16:15:16 +0530801 if (outputDesc->isDuplicated()) {
Dhananjay Kumard3bfcb32015-10-20 17:56:50 +0530802 handleIncallSonification(stream, false, false, outputDesc->subOutput1()->mIoHandle);
803 handleIncallSonification(stream, false, false, outputDesc->subOutput2()->mIoHandle);
Sharad Sangleca67a022015-05-28 16:15:16 +0530804 }
805 handleIncallSonification(stream, false, false, outputDesc->mIoHandle);
806 }
807
808 if (outputDesc->mRefCount[stream] > 0) {
809 // decrement usage count of this stream on the output
810 outputDesc->changeRefCount(stream, -1);
811
812 // store time at which the stream was stopped - see isStreamActive()
813 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
814 outputDesc->mStopTime[stream] = systemTime();
Dhananjay Kumard3bfcb32015-10-20 17:56:50 +0530815 audio_devices_t prevDevice = outputDesc->device();
Sharad Sangleca67a022015-05-28 16:15:16 +0530816 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
817 // delay the device switch by twice the latency because stopOutput() is executed when
818 // the track stop() command is received and at that time the audio track buffer can
819 // still contain data that needs to be drained. The latency only covers the audio HAL
820 // and kernel buffers. Also the latency does not always include additional delay in the
821 // audio path (audio DSP, CODEC ...)
822 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
823
824 // force restoring the device selection on other active outputs if it differs from the
825 // one being selected for this output
826 for (size_t i = 0; i < mOutputs.size(); i++) {
827 audio_io_handle_t curOutput = mOutputs.keyAt(i);
828 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
829 if (desc != outputDesc &&
830 desc->isActive() &&
831 outputDesc->sharesHwModuleWith(desc) &&
832 (newDevice != desc->device())) {
Ramjee Singhae5f8902015-08-19 20:47:12 +0530833 audio_devices_t dev = getNewOutputDevice(mOutputs.valueFor(curOutput), false
834 /*fromCache*/);
Naresh Tanniru7010e5b2016-09-14 11:25:37 +0530835 bool force = desc->device() != dev;
Dhananjay Kumard3bfcb32015-10-20 17:56:50 +0530836 uint32_t delayMs;
837 if (dev == prevDevice) {
838 delayMs = 0;
839 } else {
840 delayMs = outputDesc->latency()*2;
841 }
Ramjee Singhae5f8902015-08-19 20:47:12 +0530842 setOutputDevice(desc,
843 dev,
Naresh Tanniru7010e5b2016-09-14 11:25:37 +0530844 force,
Dhananjay Kumard3bfcb32015-10-20 17:56:50 +0530845 delayMs);
Eric Laurentb5f28ec2016-09-28 16:59:11 -0700846 // re-apply device specific volume if not done by setOutputDevice()
847 if (!force) {
Luca Stefani592a45c2016-12-09 13:08:00 +0100848 applyStreamVolumes(desc, dev, delayMs);
Eric Laurentb5f28ec2016-09-28 16:59:11 -0700849 }
Sharad Sangleca67a022015-05-28 16:15:16 +0530850 }
851 }
852 // update the outputs if stopping one with a stream that can affect notification routing
853 handleNotificationRoutingForStream(stream);
854 }
855 return NO_ERROR;
856 } else {
857 ALOGW("stopOutput() refcount is already 0");
858 return INVALID_OPERATION;
859 }
860}
Dhananjay Kumard3bfcb32015-10-20 17:56:50 +0530861status_t AudioPolicyManagerCustom::startSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangleca67a022015-05-28 16:15:16 +0530862 audio_stream_type_t stream,
863 audio_devices_t device,
Arne Coucheron3b374442016-09-03 06:44:26 +0200864 const char *address,
Sharad Sangleca67a022015-05-28 16:15:16 +0530865 uint32_t *delayMs)
866{
867 // cannot start playback of STREAM_TTS if any other output is being used
868 uint32_t beaconMuteLatency = 0;
869
Dhananjay Kumard3bfcb32015-10-20 17:56:50 +0530870 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
871 ALOGW("startSource() invalid stream %d", stream);
872 return INVALID_OPERATION;
873 }
Sharad Sangleca67a022015-05-28 16:15:16 +0530874
875 *delayMs = 0;
876 if (stream == AUDIO_STREAM_TTS) {
877 ALOGV("\t found BEACON stream");
Arne Coucheron3b374442016-09-03 06:44:26 +0200878 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Sharad Sangleca67a022015-05-28 16:15:16 +0530879 return INVALID_OPERATION;
880 } else {
881 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
882 }
883 } else {
884 // some playback other than beacon starts
885 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
886 }
887
Arne Coucheron3b374442016-09-03 06:44:26 +0200888 // check active before incrementing usage count
889 bool force = !outputDesc->isActive();
890
Sharad Sangleca67a022015-05-28 16:15:16 +0530891 // increment usage count for this stream on the requested output:
892 // NOTE that the usage count is the same for duplicated output and hardware output which is
893 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
894 outputDesc->changeRefCount(stream, 1);
895
896 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
897 // starting an output being rerouted?
898 if (device == AUDIO_DEVICE_NONE) {
899 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
900 }
901 routing_strategy strategy = getStrategy(stream);
902 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
903 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
904 (beaconMuteLatency > 0);
905 uint32_t waitMs = beaconMuteLatency;
Sharad Sangleca67a022015-05-28 16:15:16 +0530906 for (size_t i = 0; i < mOutputs.size(); i++) {
907 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
908 if (desc != outputDesc) {
909 // force a device change if any other output is managed by the same hw
910 // module and has a current device selection that differs from selected device.
911 // In this case, the audio HAL must receive the new device selection so that it can
912 // change the device currently selected by the other active output.
913 if (outputDesc->sharesHwModuleWith(desc) &&
914 desc->device() != device) {
915 force = true;
916 }
917 // wait for audio on other active outputs to be presented when starting
918 // a notification so that audio focus effect can propagate, or that a mute/unmute
919 // event occurred for beacon
920 uint32_t latency = desc->latency();
921 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
922 waitMs = latency;
923 }
924 }
925 }
Arne Coucheron3b374442016-09-03 06:44:26 +0200926 uint32_t muteWaitMs;
927 muteWaitMs = setOutputDevice(outputDesc, device, force, 0, NULL, address);
Sharad Sangleca67a022015-05-28 16:15:16 +0530928
929 // handle special case for sonification while in call
930 if (isInCall()) {
931 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
932 }
933
934 // apply volume rules for current stream and device if necessary
935 checkAndSetVolume(stream,
Arne Coucheron3b374442016-09-03 06:44:26 +0200936 mVolumeCurves->getVolumeIndex(stream, device),
Sharad Sangleca67a022015-05-28 16:15:16 +0530937 outputDesc,
938 device);
939
940 // update the outputs if starting an output with a stream that can affect notification
941 // routing
942 handleNotificationRoutingForStream(stream);
943
944 // force reevaluating accessibility routing when ringtone or alarm starts
945 if (strategy == STRATEGY_SONIFICATION) {
946 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
947 }
948 }
949 else {
950 // handle special case for sonification while in call
951 if (isInCall()) {
952 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
953 }
954 }
955 return NO_ERROR;
956}
957void AudioPolicyManagerCustom::handleIncallSonification(audio_stream_type_t stream,
958 bool starting, bool stateChange,
959 audio_io_handle_t output)
960{
961 if(!hasPrimaryOutput()) {
962 return;
963 }
964 // no action needed for AUDIO_STREAM_PATCH stream type, it's for internal flinger tracks
965 if (stream == AUDIO_STREAM_PATCH) {
966 return;
967 }
968 // if the stream pertains to sonification strategy and we are in call we must
969 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
970 // in the device used for phone strategy and play the tone if the selected device does not
971 // interfere with the device used for phone strategy
972 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
973 // many times as there are active tracks on the output
974 const routing_strategy stream_strategy = getStrategy(stream);
975 if ((stream_strategy == STRATEGY_SONIFICATION) ||
976 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
977 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
978 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
979 stream, starting, outputDesc->mDevice, stateChange);
980 if (outputDesc->mRefCount[stream]) {
981 int muteCount = 1;
982 if (stateChange) {
983 muteCount = outputDesc->mRefCount[stream];
984 }
985 if (audio_is_low_visibility(stream)) {
986 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
987 for (int i = 0; i < muteCount; i++) {
988 setStreamMute(stream, starting, outputDesc);
989 }
990 } else {
991 ALOGV("handleIncallSonification() high visibility");
992 if (outputDesc->device() &
993 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
994 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
995 for (int i = 0; i < muteCount; i++) {
996 setStreamMute(stream, starting, outputDesc);
997 }
998 }
999 if (starting) {
1000 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
1001 AUDIO_STREAM_VOICE_CALL);
1002 } else {
1003 mpClientInterface->stopTone();
1004 }
1005 }
1006 }
1007 }
1008}
1009void AudioPolicyManagerCustom::handleNotificationRoutingForStream(audio_stream_type_t stream) {
1010 switch(stream) {
1011 case AUDIO_STREAM_MUSIC:
1012 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
1013 updateDevicesAndOutputs();
1014 break;
1015 default:
1016 break;
1017 }
1018}
1019status_t AudioPolicyManagerCustom::checkAndSetVolume(audio_stream_type_t stream,
1020 int index,
Dhananjay Kumard3bfcb32015-10-20 17:56:50 +05301021 const sp<AudioOutputDescriptor>& outputDesc,
Sharad Sangleca67a022015-05-28 16:15:16 +05301022 audio_devices_t device,
1023 int delayMs, bool force)
1024{
Dhananjay Kumard3bfcb32015-10-20 17:56:50 +05301025 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
1026 ALOGW("checkAndSetVolume() invalid stream %d", stream);
1027 return INVALID_OPERATION;
1028 }
Sharad Sangleca67a022015-05-28 16:15:16 +05301029 // do not change actual stream volume if the stream is muted
1030 if (outputDesc->mMuteCount[stream] != 0) {
1031 ALOGVV("checkAndSetVolume() stream %d muted count %d",
1032 stream, outputDesc->mMuteCount[stream]);
1033 return NO_ERROR;
1034 }
1035 audio_policy_forced_cfg_t forceUseForComm =
1036 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
1037 // do not change in call volume if bluetooth is connected and vice versa
1038 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
1039 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
1040 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
1041 stream, forceUseForComm);
1042 return INVALID_OPERATION;
1043 }
1044
1045 if (device == AUDIO_DEVICE_NONE) {
1046 device = outputDesc->device();
1047 }
1048
1049 float volumeDb = computeVolume(stream, index, device);
1050 if (outputDesc->isFixedVolume(device)) {
1051 volumeDb = 0.0f;
1052 }
1053
1054 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
1055
1056 if (stream == AUDIO_STREAM_VOICE_CALL ||
1057 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
1058 float voiceVolume;
1059 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
1060 if (stream == AUDIO_STREAM_VOICE_CALL) {
Arne Coucheron3b374442016-09-03 06:44:26 +02001061 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Sharad Sangleca67a022015-05-28 16:15:16 +05301062 } else {
1063 voiceVolume = 1.0;
1064 }
1065
1066 if (voiceVolume != mLastVoiceVolume && ((outputDesc == mPrimaryOutput) ||
1067 isDirectOutput(outputDesc->mIoHandle) || device & AUDIO_DEVICE_OUT_ALL_USB)) {
1068 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
1069 mLastVoiceVolume = voiceVolume;
1070 }
Ramjee Singh5300daa2015-11-18 13:31:47 +05301071#ifdef FM_POWER_OPT
1072 } else if (stream == AUDIO_STREAM_MUSIC && hasPrimaryOutput() &&
1073 outputDesc == mPrimaryOutput) {
1074 AudioParameter param = AudioParameter();
1075 param.addFloat(String8("fm_volume"), Volume::DbToAmpl(volumeDb));
1076 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString(), delayMs);
1077#endif /* FM_POWER_OPT end */
Sharad Sangleca67a022015-05-28 16:15:16 +05301078 }
1079
1080 return NO_ERROR;
1081}
1082bool AudioPolicyManagerCustom::isDirectOutput(audio_io_handle_t output) {
1083 for (size_t i = 0; i < mOutputs.size(); i++) {
1084 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1085 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1086 if ((curOutput == output) && (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
1087 return true;
1088 }
1089 }
1090 return false;
1091}
vivek mehtac2711cd2015-08-26 14:01:20 -07001092
1093status_t AudioPolicyManagerCustom::getOutputForAttr(const audio_attributes_t *attr,
1094 audio_io_handle_t *output,
1095 audio_session_t session,
1096 audio_stream_type_t *stream,
1097 uid_t uid,
1098 uint32_t samplingRate,
1099 audio_format_t format,
1100 audio_channel_mask_t channelMask,
1101 audio_output_flags_t flags,
1102 audio_port_handle_t selectedDeviceId,
1103 const audio_offload_info_t *offloadInfo)
1104{
1105 audio_offload_info_t tOffloadInfo = AUDIO_INFO_INITIALIZER;
1106
Alexy Josephb970dcb2015-10-16 15:57:53 -07001107 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
1108 bool pcmOffloadEnabled = false;
1109
1110 if (offloadDisabled) {
1111 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
1112 }
1113
1114 //read track offload property only if the global offload switch is off.
1115 if (!offloadDisabled) {
1116 pcmOffloadEnabled = property_get_bool("audio.offload.track.enable", false);
1117 }
vivek mehtac2711cd2015-08-26 14:01:20 -07001118
1119 if (offloadInfo == NULL && pcmOffloadEnabled) {
1120 tOffloadInfo.sample_rate = samplingRate;
1121 tOffloadInfo.channel_mask = channelMask;
1122 tOffloadInfo.format = format;
1123 tOffloadInfo.stream_type = *stream;
1124 tOffloadInfo.bit_width = 16; //hard coded for PCM_16
1125 if (attr != NULL) {
1126 ALOGV("found attribute .. setting usage %d ", attr->usage);
1127 tOffloadInfo.usage = attr->usage;
1128 } else {
Alexy Josephb970dcb2015-10-16 15:57:53 -07001129 ALOGI("%s:: attribute is NULL .. no usage set", __func__);
vivek mehtac2711cd2015-08-26 14:01:20 -07001130 }
1131 offloadInfo = &tOffloadInfo;
1132 }
1133
1134 return AudioPolicyManager::getOutputForAttr(attr, output, session, stream,
1135 (uid_t)uid, (uint32_t)samplingRate,
1136 format, (audio_channel_mask_t)channelMask,
1137 flags, (audio_port_handle_t)selectedDeviceId,
1138 offloadInfo);
1139}
1140
Sharad Sangleca67a022015-05-28 16:15:16 +05301141audio_io_handle_t AudioPolicyManagerCustom::getOutputForDevice(
1142 audio_devices_t device,
1143 audio_session_t session __unused,
1144 audio_stream_type_t stream,
1145 uint32_t samplingRate,
1146 audio_format_t format,
1147 audio_channel_mask_t channelMask,
1148 audio_output_flags_t flags,
1149 const audio_offload_info_t *offloadInfo)
1150{
1151 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Sharad Sangleca67a022015-05-28 16:15:16 +05301152 status_t status;
1153
1154#ifdef AUDIO_POLICY_TEST
1155 if (mCurOutput != 0) {
1156 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1157 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1158
1159 if (mTestOutputs[mCurOutput] == 0) {
1160 ALOGV("getOutput() opening test output");
1161 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
1162 mpClientInterface);
1163 outputDesc->mDevice = mTestDevice;
1164 outputDesc->mLatency = mTestLatencyMs;
1165 outputDesc->mFlags =
1166 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
1167 outputDesc->mRefCount[stream] = 0;
1168 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1169 config.sample_rate = mTestSamplingRate;
1170 config.channel_mask = mTestChannels;
1171 config.format = mTestFormat;
1172 if (offloadInfo != NULL) {
1173 config.offload_info = *offloadInfo;
1174 }
1175 status = mpClientInterface->openOutput(0,
1176 &mTestOutputs[mCurOutput],
1177 &config,
1178 &outputDesc->mDevice,
1179 String8(""),
1180 &outputDesc->mLatency,
1181 outputDesc->mFlags);
1182 if (status == NO_ERROR) {
1183 outputDesc->mSamplingRate = config.sample_rate;
1184 outputDesc->mFormat = config.format;
1185 outputDesc->mChannelMask = config.channel_mask;
1186 AudioParameter outputCmd = AudioParameter();
1187 outputCmd.addInt(String8("set_id"),mCurOutput);
1188 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1189 addOutput(mTestOutputs[mCurOutput], outputDesc);
1190 }
1191 }
1192 return mTestOutputs[mCurOutput];
1193 }
1194#endif //AUDIO_POLICY_TEST
1195 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1196 (stream != AUDIO_STREAM_MUSIC)) {
1197 // compress should not be used for non-music streams
1198 ALOGE("Offloading only allowed with music stream");
1199 return 0;
Karthik Reddy Kattaeb605c92015-07-14 16:05:18 +05301200 }
1201
1202 if ((stream == AUDIO_STREAM_VOICE_CALL) &&
1203 (channelMask == 1) &&
1204 (samplingRate == 8000 || samplingRate == 16000)) {
1205 // Allow Voip direct output only if:
1206 // audio mode is MODE_IN_COMMUNCATION; AND
1207 // voip output is not opened already; AND
1208 // requested sample rate matches with that of voip input stream (if opened already)
1209 int value = 0;
1210 uint32_t mode = 0, voipOutCount = 1, voipSampleRate = 1;
1211 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1212 String8("audio_mode"));
1213 AudioParameter result = AudioParameter(valueStr);
1214 if (result.getInt(String8("audio_mode"), value) == NO_ERROR) {
1215 mode = value;
1216 }
1217
1218 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1219 String8("voip_out_stream_count"));
1220 result = AudioParameter(valueStr);
1221 if (result.getInt(String8("voip_out_stream_count"), value) == NO_ERROR) {
1222 voipOutCount = value;
1223 }
1224
1225 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1226 String8("voip_sample_rate"));
1227 result = AudioParameter(valueStr);
1228 if (result.getInt(String8("voip_sample_rate"), value) == NO_ERROR) {
1229 voipSampleRate = value;
1230 }
1231
1232 if ((mode == AUDIO_MODE_IN_COMMUNICATION) && (voipOutCount == 0) &&
1233 ((voipSampleRate == 0) || (voipSampleRate == samplingRate))) {
1234 if (audio_is_linear_pcm(format)) {
1235 char propValue[PROPERTY_VALUE_MAX] = {0};
1236 property_get("use.voice.path.for.pcm.voip", propValue, "0");
1237 bool voipPcmSysPropEnabled = !strncmp("true", propValue, sizeof("true"));
1238 if (voipPcmSysPropEnabled && (format == AUDIO_FORMAT_PCM_16_BIT)) {
1239 flags = (audio_output_flags_t)((flags &~AUDIO_OUTPUT_FLAG_FAST) |
1240 AUDIO_OUTPUT_FLAG_VOIP_RX | AUDIO_OUTPUT_FLAG_DIRECT);
1241 ALOGD("Set VoIP and Direct output flags for PCM format");
1242 }
1243 }
1244 }
Sharad Sangleca67a022015-05-28 16:15:16 +05301245 }
Karthik Reddy Kattaeb605c92015-07-14 16:05:18 +05301246
Ramjee Singhae5f8902015-08-19 20:47:12 +05301247#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Sharad Sangleca67a022015-05-28 16:15:16 +05301248 /*
1249 * WFD audio routes back to target speaker when starting a ringtone playback.
1250 * This is because primary output is reused for ringtone, so output device is
1251 * updated based on SONIFICATION strategy for both ringtone and music playback.
1252 * The same issue is not seen on remoted_submix HAL based WFD audio because
1253 * primary output is not reused and a new output is created for ringtone playback.
1254 * Issue is fixed by updating output flag to AUDIO_OUTPUT_FLAG_FAST when there is
1255 * a non-music stream playback on WFD, so primary output is not reused for ringtone.
1256 */
1257 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
1258 if ((availableOutputDeviceTypes & AUDIO_DEVICE_OUT_PROXY)
1259 && (stream != AUDIO_STREAM_MUSIC)) {
1260 ALOGD("WFD audio: use OUTPUT_FLAG_FAST for non music stream. flags:%x", flags );
1261 //For voip paths
1262 if(flags & AUDIO_OUTPUT_FLAG_DIRECT)
1263 flags = AUDIO_OUTPUT_FLAG_DIRECT;
1264 else //route every thing else to ULL path
1265 flags = AUDIO_OUTPUT_FLAG_FAST;
1266 }
Ramjee Singhae5f8902015-08-19 20:47:12 +05301267#endif
Sharad Sangleca67a022015-05-28 16:15:16 +05301268 // open a direct output if required by specified parameters
vivek mehtac2711cd2015-08-26 14:01:20 -07001269 // force direct flag if offload flag is set: offloading implies a direct output stream
Sharad Sangleca67a022015-05-28 16:15:16 +05301270 // and all common behaviors are driven by checking only the direct flag
1271 // this should normally be set appropriately in the policy configuration file
1272 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1273 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1274 }
1275 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1276 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1277 }
vivek mehtac2711cd2015-08-26 14:01:20 -07001278
vivek mehta8c2602a2015-10-16 00:25:59 -07001279 bool forced_deep = false;
Sharad Sangleca67a022015-05-28 16:15:16 +05301280 // only allow deep buffering for music stream type
1281 if (stream != AUDIO_STREAM_MUSIC) {
1282 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Sharad Sangle412d96c2015-08-03 17:55:48 +05301283 } else if (/* stream == AUDIO_STREAM_MUSIC && */
1284 flags == AUDIO_OUTPUT_FLAG_NONE &&
1285 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
vivek mehta8c2602a2015-10-16 00:25:59 -07001286 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
1287 forced_deep = true;
Sharad Sangleca67a022015-05-28 16:15:16 +05301288 }
Sharad Sangle412d96c2015-08-03 17:55:48 +05301289
Sharad Sangleca67a022015-05-28 16:15:16 +05301290 if (stream == AUDIO_STREAM_TTS) {
1291 flags = AUDIO_OUTPUT_FLAG_TTS;
1292 }
1293
vivek mehta8c2602a2015-10-16 00:25:59 -07001294 // Do offload magic here
1295 if (((flags == AUDIO_OUTPUT_FLAG_NONE) || forced_deep) &&
1296 (stream == AUDIO_STREAM_MUSIC) && (offloadInfo != NULL) &&
1297 ((offloadInfo->usage == AUDIO_USAGE_MEDIA) || (offloadInfo->usage == AUDIO_USAGE_GAME))) {
1298 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1299 ALOGD("AudioCustomHAL --> Force Direct Flag .. flag (0x%x)", flags);
1300 }
1301
Sharad Sangleca67a022015-05-28 16:15:16 +05301302 sp<IOProfile> profile;
1303
1304 // skip direct output selection if the request can obviously be attached to a mixed output
1305 // and not explicitly requested
1306 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
Arne Coucheron3b374442016-09-03 06:44:26 +02001307 audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX &&
Sharad Sangleca67a022015-05-28 16:15:16 +05301308 audio_channel_count_from_out_mask(channelMask) <= 2) {
1309 goto non_direct_output;
1310 }
1311
1312 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1313 // creating an offloaded track and tearing it down immediately after start when audioflinger
1314 // detects there is an active non offloadable effect.
1315 // FIXME: We should check the audio session here but we do not have it in this context.
1316 // This may prevent offloading in rare situations where effects are left active by apps
1317 // in the background.
1318
1319 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
Arne Coucheron3b374442016-09-03 06:44:26 +02001320 !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
Sharad Sangleca67a022015-05-28 16:15:16 +05301321 profile = getProfileForDirectOutput(device,
1322 samplingRate,
1323 format,
1324 channelMask,
1325 (audio_output_flags_t)flags);
1326 }
1327
1328 if (profile != 0) {
1329 sp<SwAudioOutputDescriptor> outputDesc = NULL;
1330
1331 for (size_t i = 0; i < mOutputs.size(); i++) {
1332 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1333 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1334 outputDesc = desc;
1335 // reuse direct output if currently open and configured with same parameters
1336 if ((samplingRate == outputDesc->mSamplingRate) &&
Arne Coucheron3b374442016-09-03 06:44:26 +02001337 audio_formats_match(format, outputDesc->mFormat) &&
Sharad Sangleca67a022015-05-28 16:15:16 +05301338 (channelMask == outputDesc->mChannelMask)) {
1339 outputDesc->mDirectOpenCount++;
1340 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1341 return mOutputs.keyAt(i);
1342 }
1343 }
1344 }
1345 // close direct output if currently open and configured with different parameters
1346 if (outputDesc != NULL) {
1347 closeOutput(outputDesc->mIoHandle);
1348 }
1349
1350 // if the selected profile is offloaded and no offload info was specified,
1351 // create a default one
1352 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
Arne Coucheron3b374442016-09-03 06:44:26 +02001353 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
Sharad Sangleca67a022015-05-28 16:15:16 +05301354 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1355 defaultOffloadInfo.sample_rate = samplingRate;
1356 defaultOffloadInfo.channel_mask = channelMask;
1357 defaultOffloadInfo.format = format;
1358 defaultOffloadInfo.stream_type = stream;
1359 defaultOffloadInfo.bit_rate = 0;
1360 defaultOffloadInfo.duration_us = -1;
1361 defaultOffloadInfo.has_video = true; // conservative
1362 defaultOffloadInfo.is_streaming = true; // likely
1363 offloadInfo = &defaultOffloadInfo;
1364 }
1365
1366 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
1367 outputDesc->mDevice = device;
1368 outputDesc->mLatency = 0;
1369 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
1370 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1371 config.sample_rate = samplingRate;
1372 config.channel_mask = channelMask;
1373 config.format = format;
1374 if (offloadInfo != NULL) {
1375 config.offload_info = *offloadInfo;
1376 }
1377 status = mpClientInterface->openOutput(profile->getModuleHandle(),
1378 &output,
1379 &config,
1380 &outputDesc->mDevice,
1381 String8(""),
1382 &outputDesc->mLatency,
1383 outputDesc->mFlags);
1384
1385 // only accept an output with the requested parameters
1386 if (status != NO_ERROR ||
1387 (samplingRate != 0 && samplingRate != config.sample_rate) ||
Arne Coucheron3b374442016-09-03 06:44:26 +02001388 (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) ||
Sharad Sangleca67a022015-05-28 16:15:16 +05301389 (channelMask != 0 && channelMask != config.channel_mask)) {
1390 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1391 "format %d %d, channelMask %04x %04x", output, samplingRate,
1392 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1393 outputDesc->mChannelMask);
1394 if (output != AUDIO_IO_HANDLE_NONE) {
1395 mpClientInterface->closeOutput(output);
1396 }
1397 // fall back to mixer output if possible when the direct output could not be open
Arne Coucheron3b374442016-09-03 06:44:26 +02001398 if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) {
Sharad Sangleca67a022015-05-28 16:15:16 +05301399 goto non_direct_output;
1400 }
1401 return AUDIO_IO_HANDLE_NONE;
1402 }
1403 outputDesc->mSamplingRate = config.sample_rate;
1404 outputDesc->mChannelMask = config.channel_mask;
1405 outputDesc->mFormat = config.format;
1406 outputDesc->mRefCount[stream] = 0;
1407 outputDesc->mStopTime[stream] = 0;
1408 outputDesc->mDirectOpenCount = 1;
1409
1410 audio_io_handle_t srcOutput = getOutputForEffect();
1411 addOutput(output, outputDesc);
1412 audio_io_handle_t dstOutput = getOutputForEffect();
1413 if (dstOutput == output) {
Gao Jiec2e73aa2015-11-12 08:51:22 +08001414#ifdef DOLBY_ENABLE
1415 status_t status = mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1416 if (status == NO_ERROR) {
1417 for (size_t i = 0; i < mEffects.size(); i++) {
1418 sp<EffectDescriptor> desc = mEffects.valueAt(i);
1419 if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX) {
1420 // update the mIo member of EffectDescriptor for the global effect
1421 ALOGV("%s updating mIo", __FUNCTION__);
1422 desc->mIo = dstOutput;
1423 }
1424 }
1425 } else {
1426 ALOGW("%s moveEffects from %d to %d failed", __FUNCTION__, srcOutput, dstOutput);
1427 }
1428#else // DOLBY_END
Sharad Sangleca67a022015-05-28 16:15:16 +05301429 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
Gao Jiec2e73aa2015-11-12 08:51:22 +08001430#endif // LINE_ADDED_BY_DOLBY
Sharad Sangleca67a022015-05-28 16:15:16 +05301431 }
1432 mPreviousOutputs = mOutputs;
1433 ALOGV("getOutput() returns new direct output %d", output);
1434 mpClientInterface->onAudioPortListUpdate();
1435 return output;
1436 }
1437
1438non_direct_output:
Arne Coucheron3b374442016-09-03 06:44:26 +02001439
1440 // A request for HW A/V sync cannot fallback to a mixed output because time
1441 // stamps are embedded in audio data
1442 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1443 return AUDIO_IO_HANDLE_NONE;
1444 }
1445
Sharad Sangleca67a022015-05-28 16:15:16 +05301446 // ignoring channel mask due to downmix capability in mixer
1447
1448 // open a non direct output
1449
1450 // for non direct outputs, only PCM is supported
1451 if (audio_is_linear_pcm(format)) {
1452 // get which output is suitable for the specified stream. The actual
1453 // routing change will happen when startOutput() will be called
1454 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1455
1456 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1457 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1458 output = selectOutput(outputs, flags, format);
1459 }
1460 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1461 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1462
vivek mehtac2711cd2015-08-26 14:01:20 -07001463 ALOGV("getOutputForDevice() returns output %d", output);
Sharad Sangleca67a022015-05-28 16:15:16 +05301464
1465 return output;
1466}
Ramjee Singh58a28312015-08-14 14:40:44 +05301467
Mingming Yin38877802015-10-05 15:24:04 -07001468AudioPolicyManagerCustom::AudioPolicyManagerCustom(AudioPolicyClientInterface *clientInterface)
1469 : AudioPolicyManager(clientInterface),
1470 mHdmiAudioDisabled(false),
1471 mHdmiAudioEvent(false),
1472 mPrevPhoneState(0)
1473{
Arne Coucheron3b374442016-09-03 06:44:26 +02001474
1475 //TODO: Check the new logic to parse policy conf and update the below code
1476 // Need this when SSR encoding is enabled
Mingming Yin38877802015-10-05 15:24:04 -07001477 char ssr_enabled[PROPERTY_VALUE_MAX] = {0};
1478 bool prop_ssr_enabled = false;
1479
1480 if (property_get("ro.qc.sdk.audio.ssr", ssr_enabled, NULL)) {
1481 prop_ssr_enabled = atoi(ssr_enabled) || !strncmp("true", ssr_enabled, 4);
1482 }
1483
1484 for (size_t i = 0; i < mHwModules.size(); i++) {
Arne Coucheron3b374442016-09-03 06:44:26 +02001485 ALOGV("Hw module %zu", i);
Mingming Yin38877802015-10-05 15:24:04 -07001486 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) {
1487 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Arne Coucheron3b374442016-09-03 06:44:26 +02001488 AudioProfileVector profiles = inProfile->getAudioProfiles();
1489 for (size_t k = 0; k < profiles.size(); k++){
1490 ChannelsVector channels = profiles[k]->getChannels();
1491 for (size_t x = 0; x < channels.size(); x++) {
1492 audio_channel_mask_t channelMask = channels[x];
1493 ALOGV("Channel Mask %x size %zu", channelMask,
1494 channels.size());
1495 if (AUDIO_CHANNEL_IN_5POINT1 == channelMask) {
1496 if (!prop_ssr_enabled) {
1497 ALOGI("removing AUDIO_CHANNEL_IN_5POINT1 from"
1498 " input profile as SSR(surround sound record)"
1499 " is not supported on this chipset variant");
1500 channels.removeItemsAt(x, 1);
1501 ALOGV("Channel Mask size now %zu",
1502 channels.size());
1503 }
Mingming Yin38877802015-10-05 15:24:04 -07001504 }
1505 }
1506 }
1507 }
1508 }
1509
1510#ifdef RECORD_PLAY_CONCURRENCY
1511 mIsInputRequestOnProgress = false;
1512#endif
1513
1514
1515#ifdef VOICE_CONCURRENCY
1516 mFallBackflag = getFallBackPath();
1517#endif
1518}
1519
Sharad Sangleca67a022015-05-28 16:15:16 +05301520}