blob: b3d93cd8413289e189ced1ffc40b7256114f789c [file] [log] [blame]
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001/*
Sharad Sangleca67a022015-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 Sangleca67a022015-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
29
Sharad Sangleca67a022015-05-28 16:15:16 +053030#define MIN(a, b) ((a) < (b) ? (a) : (b))
31
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070032// A device mask for all audio output devices that are considered "remote" when evaluating
33// active output devices in isStreamActiveRemotely()
34#define APM_AUDIO_OUT_DEVICE_REMOTE_ALL AUDIO_DEVICE_OUT_REMOTE_SUBMIX
Sharad Sangleca67a022015-05-28 16:15:16 +053035// A device mask for all audio input and output devices where matching inputs/outputs on device
36// type alone is not enough: the address must match too
37#define APM_AUDIO_DEVICE_MATCH_ADDRESS_ALL (AUDIO_DEVICE_IN_REMOTE_SUBMIX | \
38 AUDIO_DEVICE_OUT_REMOTE_SUBMIX)
39// Following delay should be used if the calculated routing delay from all active
40// input streams is higher than this value
41#define MAX_VOICE_CALL_START_DELAY_MS 100
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070042
Sharad Sangleca67a022015-05-28 16:15:16 +053043#include <inttypes.h>
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070044#include <math.h>
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070045
Sharad Sangleca67a022015-05-28 16:15:16 +053046#include <cutils/properties.h>
47#include <utils/Log.h>
48#include <hardware/audio.h>
49#include <hardware/audio_effect.h>
50#include <media/AudioParameter.h>
51#include <soundtrigger/SoundTrigger.h>
52#include "AudioPolicyManager.h"
53#include <policy.h>
54
55namespace android {
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070056
57// ----------------------------------------------------------------------------
58// AudioPolicyInterface implementation
59// ----------------------------------------------------------------------------
Sharad Sangleca67a022015-05-28 16:15:16 +053060extern "C" AudioPolicyInterface* createAudioPolicyManager(
61 AudioPolicyClientInterface *clientInterface)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070062{
Sharad Sangleca67a022015-05-28 16:15:16 +053063 return new AudioPolicyManagerCustom(clientInterface);
64}
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070065
Sharad Sangleca67a022015-05-28 16:15:16 +053066extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface)
67{
68 delete interface;
69}
70
71status_t AudioPolicyManagerCustom::setDeviceConnectionStateInt(audio_devices_t device,
72 audio_policy_dev_state_t state,
73 const char *device_address,
74 const char *device_name)
75{
76 ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
77 device, state, device_address, device_name);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070078
79 // connect/disconnect only 1 device at a time
80 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
81
Sharad Sangleca67a022015-05-28 16:15:16 +053082 sp<DeviceDescriptor> devDesc =
83 mHwModules.getDeviceDescriptor(device, device_address, device_name);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070084
85 // handle output devices
86 if (audio_is_output_device(device)) {
Sharad Sangleca67a022015-05-28 16:15:16 +053087 SortedVector <audio_io_handle_t> outputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070088
Sharad Sangleca67a022015-05-28 16:15:16 +053089 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070090
91 // save a copy of the opened output descriptors before any output is opened or closed
92 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
93 mPreviousOutputs = mOutputs;
94 switch (state)
95 {
96 // handle output device connection
Sharad Sangleca67a022015-05-28 16:15:16 +053097 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
98 if (index >= 0) {
Ramjee Singhae5f8902015-08-19 20:47:12 +053099#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
100 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
101 if (!strncmp(device_address, "hdmi_spkr", 9)) {
102 mHdmiAudioDisabled = false;
103 } else {
104 mHdmiAudioEvent = true;
105 }
106 }
107#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700108 ALOGW("setDeviceConnectionState() device already connected: %x", device);
109 return INVALID_OPERATION;
110 }
111 ALOGV("setDeviceConnectionState() connecting device %x", device);
112
Sharad Sangleca67a022015-05-28 16:15:16 +0530113 // register new device as available
114 index = mAvailableOutputDevices.add(devDesc);
Ramjee Singhae5f8902015-08-19 20:47:12 +0530115#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
116 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
117 if (!strncmp(device_address, "hdmi_spkr", 9)) {
118 mHdmiAudioDisabled = false;
119 } else {
120 mHdmiAudioEvent = true;
121 }
122 if (mHdmiAudioDisabled || !mHdmiAudioEvent) {
123 mAvailableOutputDevices.remove(devDesc);
124 ALOGW("HDMI sink not connected, do not route audio to HDMI out");
125 return INVALID_OPERATION;
126 }
127 }
128#endif
Sharad Sangleca67a022015-05-28 16:15:16 +0530129 if (index >= 0) {
130 sp<HwModule> module = mHwModules.getModuleForDevice(device);
131 if (module == 0) {
132 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
133 device);
134 mAvailableOutputDevices.remove(devDesc);
135 return INVALID_OPERATION;
136 }
137 mAvailableOutputDevices[index]->attach(module);
138 } else {
139 return NO_MEMORY;
140 }
141
142 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
143 mAvailableOutputDevices.remove(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700144 return INVALID_OPERATION;
145 }
Sharad Sangleca67a022015-05-28 16:15:16 +0530146 // Propagate device availability to Engine
147 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700148
Sharad Sangleca67a022015-05-28 16:15:16 +0530149 // outputs should never be empty here
150 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
151 "checkOutputsForDevice() returned no outputs but status OK");
152 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
153 outputs.size());
154
155 // Send connect to HALs
156 AudioParameter param = AudioParameter(devDesc->mAddress);
157 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
158 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
159
160 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700161 // handle output device disconnection
Sharad Sangleca67a022015-05-28 16:15:16 +0530162 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
163 if (index < 0) {
Ramjee Singhae5f8902015-08-19 20:47:12 +0530164#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
165 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
166 if (!strncmp(device_address, "hdmi_spkr", 9)) {
167 mHdmiAudioDisabled = true;
168 } else {
169 mHdmiAudioEvent = false;
170 }
171 }
172#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700173 ALOGW("setDeviceConnectionState() device not connected: %x", device);
174 return INVALID_OPERATION;
175 }
176
Sharad Sangleca67a022015-05-28 16:15:16 +0530177 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700178
Sharad Sangleca67a022015-05-28 16:15:16 +0530179 // Send Disconnect to HALs
180 AudioParameter param = AudioParameter(devDesc->mAddress);
181 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
182 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
183
184 // remove device from available output devices
185 mAvailableOutputDevices.remove(devDesc);
Ramjee Singhae5f8902015-08-19 20:47:12 +0530186#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
187 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
188 if (!strncmp(device_address, "hdmi_spkr", 9)) {
189 mHdmiAudioDisabled = true;
190 } else {
191 mHdmiAudioEvent = false;
192 }
193 }
194#endif
Sharad Sangleca67a022015-05-28 16:15:16 +0530195 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
196
197 // Propagate device availability to Engine
198 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700199 } break;
200
201 default:
202 ALOGE("setDeviceConnectionState() invalid state: %x", state);
203 return BAD_VALUE;
204 }
205
Sharad Sangleca67a022015-05-28 16:15:16 +0530206 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
207 // output is suspended before any tracks are moved to it
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700208 checkA2dpSuspend();
209 checkOutputForAllStrategies();
210 // outputs must be closed after checkOutputForAllStrategies() is executed
211 if (!outputs.isEmpty()) {
212 for (size_t i = 0; i < outputs.size(); i++) {
Sharad Sangleca67a022015-05-28 16:15:16 +0530213 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700214 // close unused outputs after device disconnection or direct outputs that have been
215 // opened by checkOutputsForDevice() to query dynamic parameters
Sharad Sangleca67a022015-05-28 16:15:16 +0530216 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700217 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
218 (desc->mDirectOpenCount == 0))) {
219 closeOutput(outputs[i]);
220 }
221 }
Sharad Sangleca67a022015-05-28 16:15:16 +0530222 // check again after closing A2DP output to reset mA2dpSuspended if needed
223 checkA2dpSuspend();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700224 }
225
226 updateDevicesAndOutputs();
Sharad Sangleca67a022015-05-28 16:15:16 +0530227 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
228 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
229 updateCallRouting(newDevice);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700230 }
Ramjee Singh5300daa2015-11-18 13:31:47 +0530231
232#ifdef FM_POWER_OPT
233 // handle FM device connection state to trigger FM AFE loopback
234 if(device == AUDIO_DEVICE_OUT_FM && hasPrimaryOutput()) {
235 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
236 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
237 mPrimaryOutput->changeRefCount(AUDIO_STREAM_MUSIC, 1);
238 newDevice = (audio_devices_t)getNewOutputDevice(mPrimaryOutput, false /*fromCache*/) | AUDIO_DEVICE_OUT_FM;
239 } else {
240 mPrimaryOutput->changeRefCount(AUDIO_STREAM_MUSIC, -1);
241 }
242 AudioParameter param = AudioParameter();
243 param.addInt(String8("handle_fm"), (int)newDevice);
244 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString());
245 }
246#endif /* FM_POWER_OPT end */
247
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700248 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangleca67a022015-05-28 16:15:16 +0530249 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
250 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
251 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
252 // do not force device change on duplicated output because if device is 0, it will
253 // also force a device 0 for the two outputs it is duplicated to which may override
254 // a valid device selection on those outputs.
255 bool force = !desc->isDuplicated()
256 && (!device_distinguishes_on_address(device)
257 // always force when disconnecting (a non-duplicated device)
258 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
259 setOutputDevice(desc, newDevice, force, 0);
Satya Krishna Pindiproli5cf8c1a2014-05-08 12:22:16 +0530260 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700261 }
262
Sharad Sangleca67a022015-05-28 16:15:16 +0530263 mpClientInterface->onAudioPortListUpdate();
264 return NO_ERROR;
265 } // end if is output device
266
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700267 // handle input devices
268 if (audio_is_input_device(device)) {
Sharad Sangleca67a022015-05-28 16:15:16 +0530269 SortedVector <audio_io_handle_t> inputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700270
Sharad Sangleca67a022015-05-28 16:15:16 +0530271 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700272 switch (state)
273 {
274 // handle input device connection
Sharad Sangleca67a022015-05-28 16:15:16 +0530275 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
276 if (index >= 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700277 ALOGW("setDeviceConnectionState() device already connected: %d", device);
278 return INVALID_OPERATION;
279 }
Sharad Sangleca67a022015-05-28 16:15:16 +0530280 sp<HwModule> module = mHwModules.getModuleForDevice(device);
281 if (module == NULL) {
282 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
283 device);
284 return INVALID_OPERATION;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700285 }
Sharad Sangleca67a022015-05-28 16:15:16 +0530286 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
287 return INVALID_OPERATION;
288 }
289
290 index = mAvailableInputDevices.add(devDesc);
291 if (index >= 0) {
292 mAvailableInputDevices[index]->attach(module);
293 } else {
294 return NO_MEMORY;
295 }
296
297 // Set connect to HALs
298 AudioParameter param = AudioParameter(devDesc->mAddress);
299 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
300 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
301
302 // Propagate device availability to Engine
303 mEngine->setDeviceConnectionState(devDesc, state);
304 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700305
306 // handle input device disconnection
Sharad Sangleca67a022015-05-28 16:15:16 +0530307 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
308 if (index < 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700309 ALOGW("setDeviceConnectionState() device not connected: %d", device);
310 return INVALID_OPERATION;
311 }
Sharad Sangleca67a022015-05-28 16:15:16 +0530312
313 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
314
315 // Set Disconnect to HALs
316 AudioParameter param = AudioParameter(devDesc->mAddress);
317 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
318 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
319
320 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
321 mAvailableInputDevices.remove(devDesc);
322
323 // Propagate device availability to Engine
324 mEngine->setDeviceConnectionState(devDesc, state);
325 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700326
327 default:
328 ALOGE("setDeviceConnectionState() invalid state: %x", state);
329 return BAD_VALUE;
330 }
331
Sharad Sangleca67a022015-05-28 16:15:16 +0530332 closeAllInputs();
333
334 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
335 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
336 updateCallRouting(newDevice);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700337 }
338
Sharad Sangleca67a022015-05-28 16:15:16 +0530339 mpClientInterface->onAudioPortListUpdate();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700340 return NO_ERROR;
Sharad Sangleca67a022015-05-28 16:15:16 +0530341 } // end if is input device
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700342
343 ALOGW("setDeviceConnectionState() invalid device: %x", device);
344 return BAD_VALUE;
345}
ApurupaPattapu0c566872014-01-10 14:46:02 -0800346// This function checks for the parameters which can be offloaded.
347// This can be enhanced depending on the capability of the DSP and policy
348// of the system.
Sharad Sangleca67a022015-05-28 16:15:16 +0530349bool AudioPolicyManagerCustom::isOffloadSupported(const audio_offload_info_t& offloadInfo)
ApurupaPattapu0c566872014-01-10 14:46:02 -0800350{
Sharad Sangleca67a022015-05-28 16:15:16 +0530351 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
352 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
ApurupaPattapu0c566872014-01-10 14:46:02 -0800353 offloadInfo.sample_rate, offloadInfo.channel_mask,
354 offloadInfo.format,
355 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
356 offloadInfo.has_video);
357
ApurupaPattapu0c566872014-01-10 14:46:02 -0800358 // Check if stream type is music, then only allow offload as of now.
359 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
360 {
361 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
362 return false;
363 }
ApurupaPattapu0c566872014-01-10 14:46:02 -0800364
Alexy Josephb970dcb2015-10-16 15:57:53 -0700365 // Check if offload has been disabled
366 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
367 if (offloadDisabled) {
368 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
369 return false;
370 }
371
Preetam Singh Ranawatfdba8132015-07-21 19:30:09 +0530372 char propValue[PROPERTY_VALUE_MAX];
373 bool pcmOffload = false;
Preetam Singh Ranawatfdba8132015-07-21 19:30:09 +0530374 if ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM_OFFLOAD) {
375 bool prop_enabled = false;
376 if ((AUDIO_FORMAT_PCM_16_BIT_OFFLOAD == offloadInfo.format) &&
377 property_get("audio.offload.pcm.16bit.enable", propValue, NULL)) {
378 prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
379 }
380
Preetam Singh Ranawatfdba8132015-07-21 19:30:09 +0530381 if ((AUDIO_FORMAT_PCM_24_BIT_OFFLOAD == offloadInfo.format) &&
382 property_get("audio.offload.pcm.24bit.enable", propValue, NULL)) {
383 prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
384 }
Preetam Singh Ranawatfdba8132015-07-21 19:30:09 +0530385
386 if (prop_enabled) {
387 ALOGI("PCM offload property is enabled");
388 pcmOffload = true;
389 }
390
391 if (!pcmOffload) {
392 ALOGD("system property not enabled for PCM offload format[%x]",offloadInfo.format);
393 return false;
394 }
395 }
Preetam Singh Ranawatfdba8132015-07-21 19:30:09 +0530396 if (!pcmOffload) {
Alexy Josephb970dcb2015-10-16 15:57:53 -0700397
398 bool compressedOffloadDisabled = property_get_bool("audio.offload.compress.disable", false);
399 if (compressedOffloadDisabled) {
400 ALOGI("compressed offload disabled by audio.offload.compress.disable=%d", compressedOffloadDisabled);
401 return false;
Preetam Singh Ranawatfdba8132015-07-21 19:30:09 +0530402 }
Alexy Josephb970dcb2015-10-16 15:57:53 -0700403
Preetam Singh Ranawatfdba8132015-07-21 19:30:09 +0530404 //check if it's multi-channel AAC (includes sub formats) and FLAC format
405 if ((popcount(offloadInfo.channel_mask) > 2) &&
406 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
407 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS))) {
408 ALOGD("offload disabled for multi-channel AAC,FLAC and VORBIS format");
409 return false;
Preetam Singh Ranawatfdba8132015-07-21 19:30:09 +0530410 }
Alexy Joseph505da3f2015-09-10 01:31:51 -0700411
412 //check if it's multi-channel FLAC/ALAC/WMA format with sample rate > 48k
413 if (popcount(offloadInfo.channel_mask) > 2) {
414#ifdef FLAC_OFFLOAD_ENABLED
415 if (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) && offloadInfo.sample_rate > 48000) {
416 return false;
417 }
Preetam Singh Ranawatfdba8132015-07-21 19:30:09 +0530418#endif
Alexy Joseph505da3f2015-09-10 01:31:51 -0700419#ifdef WMA_OFFLOAD_ENABLED
420 if ((((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) && offloadInfo.sample_rate > 48000) ||
421 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) && offloadInfo.sample_rate > 48000)) {
422 return false;
423 }
424#endif
425 }
426
Preetam Singh Ranawatfdba8132015-07-21 19:30:09 +0530427 //TODO: enable audio offloading with video when ready
428 const bool allowOffloadWithVideo =
429 property_get_bool("audio.offload.video", false /* default_value */);
430 if (offloadInfo.has_video && !allowOffloadWithVideo) {
431 ALOGV("isOffloadSupported: has_video == true, returning false");
432 return false;
433 }
Manish Dewangan66e27c92015-10-13 14:04:36 +0530434
435 const bool allowOffloadStreamingWithVideo = property_get_bool("av.streaming.offload.enable",
436 false /*default value*/);
437 if(offloadInfo.has_video && offloadInfo.is_streaming && !allowOffloadStreamingWithVideo) {
438 ALOGW("offload disabled by av.streaming.offload.enable = %s ", propValue );
439 return false;
440 }
441
ApurupaPattapu0c566872014-01-10 14:46:02 -0800442 }
443
444 //If duration is less than minimum value defined in property, return false
445 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
446 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
447 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
448 return false;
449 }
450 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
451 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
Sharad Sangleca67a022015-05-28 16:15:16 +0530452 //duration checks only valid for MP3/AAC/ formats,
ApurupaPattapu0c566872014-01-10 14:46:02 -0800453 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
Sharad Sangleca67a022015-05-28 16:15:16 +0530454 if ((offloadInfo.format == AUDIO_FORMAT_MP3) ||
455 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
Alexy Joseph505da3f2015-09-10 01:31:51 -0700456#ifdef FLAC_OFFLOAD_ENABLED
Sharad Sangleca67a022015-05-28 16:15:16 +0530457 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
Alexy Joseph505da3f2015-09-10 01:31:51 -0700458#endif
459#ifdef WMA_OFFLOAD_ENABLED
Sharad Sangleca67a022015-05-28 16:15:16 +0530460 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) ||
461 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) ||
Alexy Joseph505da3f2015-09-10 01:31:51 -0700462#endif
463 pcmOffload)
ApurupaPattapu0c566872014-01-10 14:46:02 -0800464 return false;
Sharad Sangleca67a022015-05-28 16:15:16 +0530465
ApurupaPattapu0c566872014-01-10 14:46:02 -0800466 }
467
468 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
469 // creating an offloaded track and tearing it down immediately after start when audioflinger
470 // detects there is an active non offloadable effect.
471 // FIXME: We should check the audio session here but we do not have it in this context.
472 // This may prevent offloading in rare situations where effects are left active by apps
473 // in the background.
Sharad Sangleca67a022015-05-28 16:15:16 +0530474 if (mEffects.isNonOffloadableEffectEnabled()) {
ApurupaPattapu0c566872014-01-10 14:46:02 -0800475 return false;
476 }
Sharad Sangleca67a022015-05-28 16:15:16 +0530477 // Check for soundcard status
478 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
479 String8("SND_CARD_STATUS"));
480 AudioParameter result = AudioParameter(valueStr);
481 int isonline = 0;
482 if ((result.getInt(String8("SND_CARD_STATUS"), isonline) == NO_ERROR)
483 && !isonline) {
484 ALOGD("copl: soundcard is offline rejecting offload request");
485 return false;
486 }
ApurupaPattapu0c566872014-01-10 14:46:02 -0800487 // See if there is a profile to support this.
488 // AUDIO_DEVICE_NONE
Sharad Sangleca67a022015-05-28 16:15:16 +0530489 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
ApurupaPattapu0c566872014-01-10 14:46:02 -0800490 offloadInfo.sample_rate,
491 offloadInfo.format,
492 offloadInfo.channel_mask,
493 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Sharad Sangleca67a022015-05-28 16:15:16 +0530494 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
495 return (profile != 0);
ApurupaPattapu0c566872014-01-10 14:46:02 -0800496}
Sharad Sangleca67a022015-05-28 16:15:16 +0530497audio_devices_t AudioPolicyManagerCustom::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
498 bool fromCache)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700499{
Sharad Sangleca67a022015-05-28 16:15:16 +0530500 audio_devices_t device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700501
Sharad Sangleca67a022015-05-28 16:15:16 +0530502 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
503 if (index >= 0) {
504 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
505 if (patchDesc->mUid != mUidCached) {
506 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
507 outputDesc->device(), outputDesc->mPatchHandle);
508 return outputDesc->device();
509 }
510 }
511
512 // check the following by order of priority to request a routing change if necessary:
513 // 1: the strategy enforced audible is active and enforced on the output:
514 // use device for strategy enforced audible
515 // 2: we are in call or the strategy phone is active on the output:
516 // use device for strategy phone
517 // 3: the strategy for enforced audible is active but not enforced on the output:
518 // use the device for strategy enforced audible
519 // 4: the strategy sonification is active on the output:
520 // use device for strategy sonification
521 // 5: the strategy "respectful" sonification is active on the output:
522 // use device for strategy "respectful" sonification
523 // 6: the strategy accessibility is active on the output:
524 // use device for strategy accessibility
525 // 7: the strategy media is active on the output:
526 // use device for strategy media
527 // 8: the strategy DTMF is active on the output:
528 // use device for strategy DTMF
529 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
530 // use device for strategy t-t-s
531 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
532 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
533 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
534 } else if (isInCall() ||
535 isStrategyActive(outputDesc, STRATEGY_PHONE)||
536 isStrategyActive(mPrimaryOutput, STRATEGY_PHONE)) {
537 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
538 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
539 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
540 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)||
541 (isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION)
542 && (!isStrategyActive(mPrimaryOutput,STRATEGY_MEDIA)))) {
543 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
544 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)||
545 (isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION_RESPECTFUL)
546 && (!isStrategyActive(mPrimaryOutput, STRATEGY_MEDIA)))) {
547 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
548 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
549 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
550 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
551 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
552 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
553 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
554 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
555 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
556 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
557 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
558 }
559
560 ALOGV("getNewOutputDevice() selected device %x", device);
561 return device;
562}
563void AudioPolicyManagerCustom::setPhoneState(audio_mode_t state)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700564{
Sharad Sangleca67a022015-05-28 16:15:16 +0530565 ALOGV("setPhoneState() state %d", state);
566 // store previous phone state for management of sonification strategy below
Ramjee Singhae5f8902015-08-19 20:47:12 +0530567 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Sharad Sangleca67a022015-05-28 16:15:16 +0530568 int oldState = mEngine->getPhoneState();
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700569
Sharad Sangleca67a022015-05-28 16:15:16 +0530570 if (mEngine->setPhoneState(state) != NO_ERROR) {
571 ALOGW("setPhoneState() invalid or same state %d", state);
572 return;
573 }
574 /// Opens: can these line be executed after the switch of volume curves???
575 // if leaving call state, handle special case of active streams
576 // pertaining to sonification strategy see handleIncallSonification()
Ramjee Singhf9345b32015-10-13 18:13:04 +0530577 if (isStateInCall(oldState)) {
Sharad Sangleca67a022015-05-28 16:15:16 +0530578 ALOGV("setPhoneState() in call state management: new state is %d", state);
579 for (size_t j = 0; j < mOutputs.size(); j++) {
580 audio_io_handle_t curOutput = mOutputs.keyAt(j);
581 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
582 if (stream == AUDIO_STREAM_PATCH) {
583 continue;
584 }
585
586 handleIncallSonification((audio_stream_type_t)stream, false, true, curOutput);
587 }
588 }
589
590 // force reevaluating accessibility routing when call starts
591 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
592 }
593
594 /**
595 * Switching to or from incall state or switching between telephony and VoIP lead to force
596 * routing command.
597 */
598 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
599 || (is_state_in_call(state) && (state != oldState)));
600
601 // check for device and output changes triggered by new phone state
602 checkA2dpSuspend();
603 checkOutputForAllStrategies();
604 updateDevicesAndOutputs();
605
606 sp<SwAudioOutputDescriptor> hwOutputDesc = mPrimaryOutput;
607
608 int delayMs = 0;
609 if (isStateInCall(state)) {
610 nsecs_t sysTime = systemTime();
611 for (size_t i = 0; i < mOutputs.size(); i++) {
612 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
613 // mute media and sonification strategies and delay device switch by the largest
614 // latency of any output where either strategy is active.
615 // This avoid sending the ring tone or music tail into the earpiece or headset.
616 if ((isStrategyActive(desc, STRATEGY_MEDIA,
617 SONIFICATION_HEADSET_MUSIC_DELAY,
618 sysTime) ||
619 isStrategyActive(desc, STRATEGY_SONIFICATION,
620 SONIFICATION_HEADSET_MUSIC_DELAY,
621 sysTime)) &&
622 (delayMs < (int)desc->latency()*2)) {
623 delayMs = desc->latency()*2;
624 }
625 setStrategyMute(STRATEGY_MEDIA, true, desc);
626 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
627 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
628 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
629 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
630 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
631 }
632 ALOGV("Setting the delay from %dms to %dms", delayMs,
633 MIN(delayMs, MAX_VOICE_CALL_START_DELAY_MS));
634 delayMs = MIN(delayMs, MAX_VOICE_CALL_START_DELAY_MS);
635 }
636
637 if (hasPrimaryOutput()) {
638 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
639 // the device returned is not necessarily reachable via this output
640 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
641 // force routing command to audio hardware when ending call
642 // even if no device change is needed
643 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
644 rxDevice = mPrimaryOutput->device();
645 }
646
647 if (state == AUDIO_MODE_IN_CALL) {
648 updateCallRouting(rxDevice, delayMs);
649 } else if (oldState == AUDIO_MODE_IN_CALL) {
650 if (mCallRxPatch != 0) {
651 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
652 mCallRxPatch.clear();
653 }
654 if (mCallTxPatch != 0) {
655 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
656 mCallTxPatch.clear();
657 }
658 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
659 } else {
660 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
661 }
662 }
Ramjee Singhae5f8902015-08-19 20:47:12 +0530663 //update device for all non-primary outputs
664 for (size_t i = 0; i < mOutputs.size(); i++) {
665 audio_io_handle_t output = mOutputs.keyAt(i);
666 if (output != mPrimaryOutput->mIoHandle) {
667 newDevice = getNewOutputDevice(mOutputs.valueFor(output), false /*fromCache*/);
668 setOutputDevice(mOutputs.valueFor(output), newDevice, (newDevice != AUDIO_DEVICE_NONE));
669 }
670 }
Sharad Sangleca67a022015-05-28 16:15:16 +0530671 // if entering in call state, handle special case of active streams
672 // pertaining to sonification strategy see handleIncallSonification()
673 if (isStateInCall(state)) {
674 ALOGV("setPhoneState() in call state management: new state is %d", state);
675 for (size_t j = 0; j < mOutputs.size(); j++) {
676 audio_io_handle_t curOutput = mOutputs.keyAt(j);
677 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
678 if (stream == AUDIO_STREAM_PATCH) {
679 continue;
680 }
681 handleIncallSonification((audio_stream_type_t)stream, true, true, curOutput);
682 }
683 }
684 }
685
686 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
687 if (state == AUDIO_MODE_RINGTONE &&
688 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
689 mLimitRingtoneVolume = true;
690 } else {
691 mLimitRingtoneVolume = false;
692 }
693}
Dhananjay Kumar42441032015-09-16 19:44:33 +0530694
695void AudioPolicyManagerCustom::setForceUse(audio_policy_force_use_t usage,
696 audio_policy_forced_cfg_t config)
697{
698 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
699
700 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
701 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
702 return;
703 }
704 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
705 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
706 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
707
708 // check for device and output changes triggered by new force usage
709 checkA2dpSuspend();
710 checkOutputForAllStrategies();
711 updateDevicesAndOutputs();
712 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
713 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
714 updateCallRouting(newDevice);
715 }
716 // Use reverse loop to make sure any low latency usecases (generally tones)
717 // are not routed before non LL usecases (generally music).
718 // We can safely assume that LL output would always have lower index,
719 // and use this work-around to avoid routing of output with music stream
720 // from the context of short lived LL output.
721 // Note: in case output's share backend(HAL sharing is implicit) all outputs
722 // gets routing update while processing first output itself.
723 for (size_t i = mOutputs.size(); i > 0; i--) {
724 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i-1);
725 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
726 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || outputDesc != mPrimaryOutput) {
727 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
728 }
729 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
730 applyStreamVolumes(outputDesc, newDevice, 0, true);
731 }
732 }
733
734 audio_io_handle_t activeInput = mInputs.getActiveInput();
735 if (activeInput != 0) {
736 setInputDevice(activeInput, getNewInputDevice(activeInput));
737 }
738
739}
740
Dhananjay Kumard3bfcb32015-10-20 17:56:50 +0530741status_t AudioPolicyManagerCustom::stopSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangleca67a022015-05-28 16:15:16 +0530742 audio_stream_type_t stream,
743 bool forceDeviceUpdate)
744{
Dhananjay Kumard3bfcb32015-10-20 17:56:50 +0530745 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
746 ALOGW("stopSource() invalid stream %d", stream);
747 return INVALID_OPERATION;
748 }
Sharad Sangleca67a022015-05-28 16:15:16 +0530749 // always handle stream stop, check which stream type is stopping
750 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
751
752 // handle special case for sonification while in call
Ramjee Singhae5f8902015-08-19 20:47:12 +0530753 if (isInCall() && (outputDesc->mRefCount[stream] == 1)) {
Sharad Sangleca67a022015-05-28 16:15:16 +0530754 if (outputDesc->isDuplicated()) {
Dhananjay Kumard3bfcb32015-10-20 17:56:50 +0530755 handleIncallSonification(stream, false, false, outputDesc->subOutput1()->mIoHandle);
756 handleIncallSonification(stream, false, false, outputDesc->subOutput2()->mIoHandle);
Sharad Sangleca67a022015-05-28 16:15:16 +0530757 }
758 handleIncallSonification(stream, false, false, outputDesc->mIoHandle);
759 }
760
761 if (outputDesc->mRefCount[stream] > 0) {
762 // decrement usage count of this stream on the output
763 outputDesc->changeRefCount(stream, -1);
764
765 // store time at which the stream was stopped - see isStreamActive()
766 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
767 outputDesc->mStopTime[stream] = systemTime();
Dhananjay Kumard3bfcb32015-10-20 17:56:50 +0530768 audio_devices_t prevDevice = outputDesc->device();
Sharad Sangleca67a022015-05-28 16:15:16 +0530769 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
770 // delay the device switch by twice the latency because stopOutput() is executed when
771 // the track stop() command is received and at that time the audio track buffer can
772 // still contain data that needs to be drained. The latency only covers the audio HAL
773 // and kernel buffers. Also the latency does not always include additional delay in the
774 // audio path (audio DSP, CODEC ...)
775 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
776
777 // force restoring the device selection on other active outputs if it differs from the
778 // one being selected for this output
779 for (size_t i = 0; i < mOutputs.size(); i++) {
780 audio_io_handle_t curOutput = mOutputs.keyAt(i);
781 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
782 if (desc != outputDesc &&
783 desc->isActive() &&
784 outputDesc->sharesHwModuleWith(desc) &&
785 (newDevice != desc->device())) {
Ramjee Singhae5f8902015-08-19 20:47:12 +0530786 audio_devices_t dev = getNewOutputDevice(mOutputs.valueFor(curOutput), false
787 /*fromCache*/);
Dhananjay Kumard3bfcb32015-10-20 17:56:50 +0530788 uint32_t delayMs;
789 if (dev == prevDevice) {
790 delayMs = 0;
791 } else {
792 delayMs = outputDesc->latency()*2;
793 }
Ramjee Singhae5f8902015-08-19 20:47:12 +0530794 setOutputDevice(desc,
795 dev,
Sharad Sangleca67a022015-05-28 16:15:16 +0530796 true,
Dhananjay Kumard3bfcb32015-10-20 17:56:50 +0530797 delayMs);
Sharad Sangleca67a022015-05-28 16:15:16 +0530798 }
799 }
800 // update the outputs if stopping one with a stream that can affect notification routing
801 handleNotificationRoutingForStream(stream);
802 }
803 return NO_ERROR;
804 } else {
805 ALOGW("stopOutput() refcount is already 0");
806 return INVALID_OPERATION;
807 }
808}
Dhananjay Kumard3bfcb32015-10-20 17:56:50 +0530809status_t AudioPolicyManagerCustom::startSource(sp<AudioOutputDescriptor> outputDesc,
Sharad Sangleca67a022015-05-28 16:15:16 +0530810 audio_stream_type_t stream,
811 audio_devices_t device,
812 uint32_t *delayMs)
813{
814 // cannot start playback of STREAM_TTS if any other output is being used
815 uint32_t beaconMuteLatency = 0;
816
Dhananjay Kumard3bfcb32015-10-20 17:56:50 +0530817 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
818 ALOGW("startSource() invalid stream %d", stream);
819 return INVALID_OPERATION;
820 }
Sharad Sangleca67a022015-05-28 16:15:16 +0530821
822 *delayMs = 0;
823 if (stream == AUDIO_STREAM_TTS) {
824 ALOGV("\t found BEACON stream");
825 if (mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
826 return INVALID_OPERATION;
827 } else {
828 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
829 }
830 } else {
831 // some playback other than beacon starts
832 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
833 }
834
835 // increment usage count for this stream on the requested output:
836 // NOTE that the usage count is the same for duplicated output and hardware output which is
837 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
838 outputDesc->changeRefCount(stream, 1);
839
840 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
841 // starting an output being rerouted?
842 if (device == AUDIO_DEVICE_NONE) {
843 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
844 }
845 routing_strategy strategy = getStrategy(stream);
846 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
847 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
848 (beaconMuteLatency > 0);
849 uint32_t waitMs = beaconMuteLatency;
850 bool force = false;
851 for (size_t i = 0; i < mOutputs.size(); i++) {
852 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
853 if (desc != outputDesc) {
854 // force a device change if any other output is managed by the same hw
855 // module and has a current device selection that differs from selected device.
856 // In this case, the audio HAL must receive the new device selection so that it can
857 // change the device currently selected by the other active output.
858 if (outputDesc->sharesHwModuleWith(desc) &&
859 desc->device() != device) {
860 force = true;
861 }
862 // wait for audio on other active outputs to be presented when starting
863 // a notification so that audio focus effect can propagate, or that a mute/unmute
864 // event occurred for beacon
865 uint32_t latency = desc->latency();
866 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
867 waitMs = latency;
868 }
869 }
870 }
871 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force);
872
873 // handle special case for sonification while in call
874 if (isInCall()) {
875 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
876 }
877
878 // apply volume rules for current stream and device if necessary
879 checkAndSetVolume(stream,
880 mStreams.valueFor(stream).getVolumeIndex(device),
881 outputDesc,
882 device);
883
884 // update the outputs if starting an output with a stream that can affect notification
885 // routing
886 handleNotificationRoutingForStream(stream);
887
888 // force reevaluating accessibility routing when ringtone or alarm starts
889 if (strategy == STRATEGY_SONIFICATION) {
890 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
891 }
892 }
893 else {
894 // handle special case for sonification while in call
895 if (isInCall()) {
896 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
897 }
898 }
899 return NO_ERROR;
900}
901void AudioPolicyManagerCustom::handleIncallSonification(audio_stream_type_t stream,
902 bool starting, bool stateChange,
903 audio_io_handle_t output)
904{
905 if(!hasPrimaryOutput()) {
906 return;
907 }
908 // no action needed for AUDIO_STREAM_PATCH stream type, it's for internal flinger tracks
909 if (stream == AUDIO_STREAM_PATCH) {
910 return;
911 }
912 // if the stream pertains to sonification strategy and we are in call we must
913 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
914 // in the device used for phone strategy and play the tone if the selected device does not
915 // interfere with the device used for phone strategy
916 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
917 // many times as there are active tracks on the output
918 const routing_strategy stream_strategy = getStrategy(stream);
919 if ((stream_strategy == STRATEGY_SONIFICATION) ||
920 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
921 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
922 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
923 stream, starting, outputDesc->mDevice, stateChange);
924 if (outputDesc->mRefCount[stream]) {
925 int muteCount = 1;
926 if (stateChange) {
927 muteCount = outputDesc->mRefCount[stream];
928 }
929 if (audio_is_low_visibility(stream)) {
930 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
931 for (int i = 0; i < muteCount; i++) {
932 setStreamMute(stream, starting, outputDesc);
933 }
934 } else {
935 ALOGV("handleIncallSonification() high visibility");
936 if (outputDesc->device() &
937 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
938 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
939 for (int i = 0; i < muteCount; i++) {
940 setStreamMute(stream, starting, outputDesc);
941 }
942 }
943 if (starting) {
944 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
945 AUDIO_STREAM_VOICE_CALL);
946 } else {
947 mpClientInterface->stopTone();
948 }
949 }
950 }
951 }
952}
953void AudioPolicyManagerCustom::handleNotificationRoutingForStream(audio_stream_type_t stream) {
954 switch(stream) {
955 case AUDIO_STREAM_MUSIC:
956 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
957 updateDevicesAndOutputs();
958 break;
959 default:
960 break;
961 }
962}
963status_t AudioPolicyManagerCustom::checkAndSetVolume(audio_stream_type_t stream,
964 int index,
Dhananjay Kumard3bfcb32015-10-20 17:56:50 +0530965 const sp<AudioOutputDescriptor>& outputDesc,
Sharad Sangleca67a022015-05-28 16:15:16 +0530966 audio_devices_t device,
967 int delayMs, bool force)
968{
Dhananjay Kumard3bfcb32015-10-20 17:56:50 +0530969 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
970 ALOGW("checkAndSetVolume() invalid stream %d", stream);
971 return INVALID_OPERATION;
972 }
Sharad Sangleca67a022015-05-28 16:15:16 +0530973 // do not change actual stream volume if the stream is muted
974 if (outputDesc->mMuteCount[stream] != 0) {
975 ALOGVV("checkAndSetVolume() stream %d muted count %d",
976 stream, outputDesc->mMuteCount[stream]);
977 return NO_ERROR;
978 }
979 audio_policy_forced_cfg_t forceUseForComm =
980 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
981 // do not change in call volume if bluetooth is connected and vice versa
982 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
983 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
984 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
985 stream, forceUseForComm);
986 return INVALID_OPERATION;
987 }
988
989 if (device == AUDIO_DEVICE_NONE) {
990 device = outputDesc->device();
991 }
992
993 float volumeDb = computeVolume(stream, index, device);
994 if (outputDesc->isFixedVolume(device)) {
995 volumeDb = 0.0f;
996 }
997
998 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
999
1000 if (stream == AUDIO_STREAM_VOICE_CALL ||
1001 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
1002 float voiceVolume;
1003 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
1004 if (stream == AUDIO_STREAM_VOICE_CALL) {
1005 voiceVolume = (float)index/(float)mStreams.valueFor(stream).getVolumeIndexMax();
1006 } else {
1007 voiceVolume = 1.0;
1008 }
1009
1010 if (voiceVolume != mLastVoiceVolume && ((outputDesc == mPrimaryOutput) ||
1011 isDirectOutput(outputDesc->mIoHandle) || device & AUDIO_DEVICE_OUT_ALL_USB)) {
1012 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
1013 mLastVoiceVolume = voiceVolume;
1014 }
Ramjee Singh5300daa2015-11-18 13:31:47 +05301015#ifdef FM_POWER_OPT
1016 } else if (stream == AUDIO_STREAM_MUSIC && hasPrimaryOutput() &&
1017 outputDesc == mPrimaryOutput) {
1018 AudioParameter param = AudioParameter();
1019 param.addFloat(String8("fm_volume"), Volume::DbToAmpl(volumeDb));
1020 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, param.toString(), delayMs);
1021#endif /* FM_POWER_OPT end */
Sharad Sangleca67a022015-05-28 16:15:16 +05301022 }
1023
1024 return NO_ERROR;
1025}
1026bool AudioPolicyManagerCustom::isDirectOutput(audio_io_handle_t output) {
1027 for (size_t i = 0; i < mOutputs.size(); i++) {
1028 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1029 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1030 if ((curOutput == output) && (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
1031 return true;
1032 }
1033 }
1034 return false;
1035}
vivek mehtac2711cd2015-08-26 14:01:20 -07001036
1037status_t AudioPolicyManagerCustom::getOutputForAttr(const audio_attributes_t *attr,
1038 audio_io_handle_t *output,
1039 audio_session_t session,
1040 audio_stream_type_t *stream,
1041 uid_t uid,
1042 uint32_t samplingRate,
1043 audio_format_t format,
1044 audio_channel_mask_t channelMask,
1045 audio_output_flags_t flags,
1046 audio_port_handle_t selectedDeviceId,
1047 const audio_offload_info_t *offloadInfo)
1048{
1049 audio_offload_info_t tOffloadInfo = AUDIO_INFO_INITIALIZER;
1050
Alexy Josephb970dcb2015-10-16 15:57:53 -07001051 bool offloadDisabled = property_get_bool("audio.offload.disable", false);
1052 bool pcmOffloadEnabled = false;
1053
1054 if (offloadDisabled) {
1055 ALOGI("offload disabled by audio.offload.disable=%d", offloadDisabled);
1056 }
1057
1058 //read track offload property only if the global offload switch is off.
1059 if (!offloadDisabled) {
1060 pcmOffloadEnabled = property_get_bool("audio.offload.track.enable", false);
1061 }
vivek mehtac2711cd2015-08-26 14:01:20 -07001062
1063 if (offloadInfo == NULL && pcmOffloadEnabled) {
1064 tOffloadInfo.sample_rate = samplingRate;
1065 tOffloadInfo.channel_mask = channelMask;
1066 tOffloadInfo.format = format;
1067 tOffloadInfo.stream_type = *stream;
1068 tOffloadInfo.bit_width = 16; //hard coded for PCM_16
1069 if (attr != NULL) {
1070 ALOGV("found attribute .. setting usage %d ", attr->usage);
1071 tOffloadInfo.usage = attr->usage;
1072 } else {
Alexy Josephb970dcb2015-10-16 15:57:53 -07001073 ALOGI("%s:: attribute is NULL .. no usage set", __func__);
vivek mehtac2711cd2015-08-26 14:01:20 -07001074 }
1075 offloadInfo = &tOffloadInfo;
1076 }
1077
1078 return AudioPolicyManager::getOutputForAttr(attr, output, session, stream,
1079 (uid_t)uid, (uint32_t)samplingRate,
1080 format, (audio_channel_mask_t)channelMask,
1081 flags, (audio_port_handle_t)selectedDeviceId,
1082 offloadInfo);
1083}
1084
Sharad Sangleca67a022015-05-28 16:15:16 +05301085audio_io_handle_t AudioPolicyManagerCustom::getOutputForDevice(
1086 audio_devices_t device,
1087 audio_session_t session __unused,
1088 audio_stream_type_t stream,
1089 uint32_t samplingRate,
1090 audio_format_t format,
1091 audio_channel_mask_t channelMask,
1092 audio_output_flags_t flags,
1093 const audio_offload_info_t *offloadInfo)
1094{
1095 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
1096 uint32_t latency = 0;
1097 status_t status;
1098
1099#ifdef AUDIO_POLICY_TEST
1100 if (mCurOutput != 0) {
1101 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1102 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1103
1104 if (mTestOutputs[mCurOutput] == 0) {
1105 ALOGV("getOutput() opening test output");
1106 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
1107 mpClientInterface);
1108 outputDesc->mDevice = mTestDevice;
1109 outputDesc->mLatency = mTestLatencyMs;
1110 outputDesc->mFlags =
1111 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
1112 outputDesc->mRefCount[stream] = 0;
1113 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1114 config.sample_rate = mTestSamplingRate;
1115 config.channel_mask = mTestChannels;
1116 config.format = mTestFormat;
1117 if (offloadInfo != NULL) {
1118 config.offload_info = *offloadInfo;
1119 }
1120 status = mpClientInterface->openOutput(0,
1121 &mTestOutputs[mCurOutput],
1122 &config,
1123 &outputDesc->mDevice,
1124 String8(""),
1125 &outputDesc->mLatency,
1126 outputDesc->mFlags);
1127 if (status == NO_ERROR) {
1128 outputDesc->mSamplingRate = config.sample_rate;
1129 outputDesc->mFormat = config.format;
1130 outputDesc->mChannelMask = config.channel_mask;
1131 AudioParameter outputCmd = AudioParameter();
1132 outputCmd.addInt(String8("set_id"),mCurOutput);
1133 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1134 addOutput(mTestOutputs[mCurOutput], outputDesc);
1135 }
1136 }
1137 return mTestOutputs[mCurOutput];
1138 }
1139#endif //AUDIO_POLICY_TEST
1140 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1141 (stream != AUDIO_STREAM_MUSIC)) {
1142 // compress should not be used for non-music streams
1143 ALOGE("Offloading only allowed with music stream");
1144 return 0;
Karthik Reddy Kattaeb605c92015-07-14 16:05:18 +05301145 }
1146
1147 if ((stream == AUDIO_STREAM_VOICE_CALL) &&
1148 (channelMask == 1) &&
1149 (samplingRate == 8000 || samplingRate == 16000)) {
1150 // Allow Voip direct output only if:
1151 // audio mode is MODE_IN_COMMUNCATION; AND
1152 // voip output is not opened already; AND
1153 // requested sample rate matches with that of voip input stream (if opened already)
1154 int value = 0;
1155 uint32_t mode = 0, voipOutCount = 1, voipSampleRate = 1;
1156 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1157 String8("audio_mode"));
1158 AudioParameter result = AudioParameter(valueStr);
1159 if (result.getInt(String8("audio_mode"), value) == NO_ERROR) {
1160 mode = value;
1161 }
1162
1163 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1164 String8("voip_out_stream_count"));
1165 result = AudioParameter(valueStr);
1166 if (result.getInt(String8("voip_out_stream_count"), value) == NO_ERROR) {
1167 voipOutCount = value;
1168 }
1169
1170 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1171 String8("voip_sample_rate"));
1172 result = AudioParameter(valueStr);
1173 if (result.getInt(String8("voip_sample_rate"), value) == NO_ERROR) {
1174 voipSampleRate = value;
1175 }
1176
1177 if ((mode == AUDIO_MODE_IN_COMMUNICATION) && (voipOutCount == 0) &&
1178 ((voipSampleRate == 0) || (voipSampleRate == samplingRate))) {
1179 if (audio_is_linear_pcm(format)) {
1180 char propValue[PROPERTY_VALUE_MAX] = {0};
1181 property_get("use.voice.path.for.pcm.voip", propValue, "0");
1182 bool voipPcmSysPropEnabled = !strncmp("true", propValue, sizeof("true"));
1183 if (voipPcmSysPropEnabled && (format == AUDIO_FORMAT_PCM_16_BIT)) {
1184 flags = (audio_output_flags_t)((flags &~AUDIO_OUTPUT_FLAG_FAST) |
1185 AUDIO_OUTPUT_FLAG_VOIP_RX | AUDIO_OUTPUT_FLAG_DIRECT);
1186 ALOGD("Set VoIP and Direct output flags for PCM format");
1187 }
1188 }
1189 }
Sharad Sangleca67a022015-05-28 16:15:16 +05301190 }
Karthik Reddy Kattaeb605c92015-07-14 16:05:18 +05301191
Ramjee Singhae5f8902015-08-19 20:47:12 +05301192#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Sharad Sangleca67a022015-05-28 16:15:16 +05301193 /*
1194 * WFD audio routes back to target speaker when starting a ringtone playback.
1195 * This is because primary output is reused for ringtone, so output device is
1196 * updated based on SONIFICATION strategy for both ringtone and music playback.
1197 * The same issue is not seen on remoted_submix HAL based WFD audio because
1198 * primary output is not reused and a new output is created for ringtone playback.
1199 * Issue is fixed by updating output flag to AUDIO_OUTPUT_FLAG_FAST when there is
1200 * a non-music stream playback on WFD, so primary output is not reused for ringtone.
1201 */
1202 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
1203 if ((availableOutputDeviceTypes & AUDIO_DEVICE_OUT_PROXY)
1204 && (stream != AUDIO_STREAM_MUSIC)) {
1205 ALOGD("WFD audio: use OUTPUT_FLAG_FAST for non music stream. flags:%x", flags );
1206 //For voip paths
1207 if(flags & AUDIO_OUTPUT_FLAG_DIRECT)
1208 flags = AUDIO_OUTPUT_FLAG_DIRECT;
1209 else //route every thing else to ULL path
1210 flags = AUDIO_OUTPUT_FLAG_FAST;
1211 }
Ramjee Singhae5f8902015-08-19 20:47:12 +05301212#endif
Sharad Sangleca67a022015-05-28 16:15:16 +05301213 // open a direct output if required by specified parameters
vivek mehtac2711cd2015-08-26 14:01:20 -07001214 // force direct flag if offload flag is set: offloading implies a direct output stream
Sharad Sangleca67a022015-05-28 16:15:16 +05301215 // and all common behaviors are driven by checking only the direct flag
1216 // this should normally be set appropriately in the policy configuration file
1217 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1218 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1219 }
1220 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1221 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1222 }
vivek mehtac2711cd2015-08-26 14:01:20 -07001223
vivek mehta8c2602a2015-10-16 00:25:59 -07001224 bool forced_deep = false;
Sharad Sangleca67a022015-05-28 16:15:16 +05301225 // only allow deep buffering for music stream type
1226 if (stream != AUDIO_STREAM_MUSIC) {
1227 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Sharad Sangle412d96c2015-08-03 17:55:48 +05301228 } else if (/* stream == AUDIO_STREAM_MUSIC && */
1229 flags == AUDIO_OUTPUT_FLAG_NONE &&
1230 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
vivek mehta8c2602a2015-10-16 00:25:59 -07001231 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
1232 forced_deep = true;
Sharad Sangleca67a022015-05-28 16:15:16 +05301233 }
Sharad Sangle412d96c2015-08-03 17:55:48 +05301234
Sharad Sangleca67a022015-05-28 16:15:16 +05301235 if (stream == AUDIO_STREAM_TTS) {
1236 flags = AUDIO_OUTPUT_FLAG_TTS;
1237 }
1238
vivek mehta8c2602a2015-10-16 00:25:59 -07001239 // Do offload magic here
1240 if (((flags == AUDIO_OUTPUT_FLAG_NONE) || forced_deep) &&
1241 (stream == AUDIO_STREAM_MUSIC) && (offloadInfo != NULL) &&
1242 ((offloadInfo->usage == AUDIO_USAGE_MEDIA) || (offloadInfo->usage == AUDIO_USAGE_GAME))) {
1243 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1244 ALOGD("AudioCustomHAL --> Force Direct Flag .. flag (0x%x)", flags);
1245 }
1246
Sharad Sangleca67a022015-05-28 16:15:16 +05301247 sp<IOProfile> profile;
1248
1249 // skip direct output selection if the request can obviously be attached to a mixed output
1250 // and not explicitly requested
1251 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
1252 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
1253 audio_channel_count_from_out_mask(channelMask) <= 2) {
1254 goto non_direct_output;
1255 }
1256
1257 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1258 // creating an offloaded track and tearing it down immediately after start when audioflinger
1259 // detects there is an active non offloadable effect.
1260 // FIXME: We should check the audio session here but we do not have it in this context.
1261 // This may prevent offloading in rare situations where effects are left active by apps
1262 // in the background.
1263
1264 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
1265 !mEffects.isNonOffloadableEffectEnabled()) {
1266 profile = getProfileForDirectOutput(device,
1267 samplingRate,
1268 format,
1269 channelMask,
1270 (audio_output_flags_t)flags);
1271 }
1272
1273 if (profile != 0) {
1274 sp<SwAudioOutputDescriptor> outputDesc = NULL;
1275
1276 for (size_t i = 0; i < mOutputs.size(); i++) {
1277 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1278 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1279 outputDesc = desc;
1280 // reuse direct output if currently open and configured with same parameters
1281 if ((samplingRate == outputDesc->mSamplingRate) &&
1282 (format == outputDesc->mFormat) &&
1283 (channelMask == outputDesc->mChannelMask)) {
1284 outputDesc->mDirectOpenCount++;
1285 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1286 return mOutputs.keyAt(i);
1287 }
1288 }
1289 }
1290 // close direct output if currently open and configured with different parameters
1291 if (outputDesc != NULL) {
1292 closeOutput(outputDesc->mIoHandle);
1293 }
1294
1295 // if the selected profile is offloaded and no offload info was specified,
1296 // create a default one
1297 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
1298 if ((profile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
1299 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1300 defaultOffloadInfo.sample_rate = samplingRate;
1301 defaultOffloadInfo.channel_mask = channelMask;
1302 defaultOffloadInfo.format = format;
1303 defaultOffloadInfo.stream_type = stream;
1304 defaultOffloadInfo.bit_rate = 0;
1305 defaultOffloadInfo.duration_us = -1;
1306 defaultOffloadInfo.has_video = true; // conservative
1307 defaultOffloadInfo.is_streaming = true; // likely
1308 offloadInfo = &defaultOffloadInfo;
1309 }
1310
1311 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
1312 outputDesc->mDevice = device;
1313 outputDesc->mLatency = 0;
1314 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
1315 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1316 config.sample_rate = samplingRate;
1317 config.channel_mask = channelMask;
1318 config.format = format;
1319 if (offloadInfo != NULL) {
1320 config.offload_info = *offloadInfo;
1321 }
1322 status = mpClientInterface->openOutput(profile->getModuleHandle(),
1323 &output,
1324 &config,
1325 &outputDesc->mDevice,
1326 String8(""),
1327 &outputDesc->mLatency,
1328 outputDesc->mFlags);
1329
1330 // only accept an output with the requested parameters
1331 if (status != NO_ERROR ||
1332 (samplingRate != 0 && samplingRate != config.sample_rate) ||
1333 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
1334 (channelMask != 0 && channelMask != config.channel_mask)) {
1335 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1336 "format %d %d, channelMask %04x %04x", output, samplingRate,
1337 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1338 outputDesc->mChannelMask);
1339 if (output != AUDIO_IO_HANDLE_NONE) {
1340 mpClientInterface->closeOutput(output);
1341 }
1342 // fall back to mixer output if possible when the direct output could not be open
1343 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
1344 goto non_direct_output;
1345 }
1346 return AUDIO_IO_HANDLE_NONE;
1347 }
1348 outputDesc->mSamplingRate = config.sample_rate;
1349 outputDesc->mChannelMask = config.channel_mask;
1350 outputDesc->mFormat = config.format;
1351 outputDesc->mRefCount[stream] = 0;
1352 outputDesc->mStopTime[stream] = 0;
1353 outputDesc->mDirectOpenCount = 1;
1354
1355 audio_io_handle_t srcOutput = getOutputForEffect();
1356 addOutput(output, outputDesc);
1357 audio_io_handle_t dstOutput = getOutputForEffect();
1358 if (dstOutput == output) {
1359 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1360 }
1361 mPreviousOutputs = mOutputs;
1362 ALOGV("getOutput() returns new direct output %d", output);
1363 mpClientInterface->onAudioPortListUpdate();
1364 return output;
1365 }
1366
1367non_direct_output:
1368 // ignoring channel mask due to downmix capability in mixer
1369
1370 // open a non direct output
1371
1372 // for non direct outputs, only PCM is supported
1373 if (audio_is_linear_pcm(format)) {
1374 // get which output is suitable for the specified stream. The actual
1375 // routing change will happen when startOutput() will be called
1376 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1377
1378 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1379 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1380 output = selectOutput(outputs, flags, format);
1381 }
1382 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1383 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1384
vivek mehtac2711cd2015-08-26 14:01:20 -07001385 ALOGV("getOutputForDevice() returns output %d", output);
Sharad Sangleca67a022015-05-28 16:15:16 +05301386
1387 return output;
1388}
Ramjee Singh58a28312015-08-14 14:40:44 +05301389
Mingming Yin38877802015-10-05 15:24:04 -07001390AudioPolicyManagerCustom::AudioPolicyManagerCustom(AudioPolicyClientInterface *clientInterface)
1391 : AudioPolicyManager(clientInterface),
1392 mHdmiAudioDisabled(false),
1393 mHdmiAudioEvent(false),
1394 mPrevPhoneState(0)
1395{
1396 char ssr_enabled[PROPERTY_VALUE_MAX] = {0};
1397 bool prop_ssr_enabled = false;
1398
1399 if (property_get("ro.qc.sdk.audio.ssr", ssr_enabled, NULL)) {
1400 prop_ssr_enabled = atoi(ssr_enabled) || !strncmp("true", ssr_enabled, 4);
1401 }
1402
1403 for (size_t i = 0; i < mHwModules.size(); i++) {
1404 ALOGV("Hw module %d", i);
1405 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) {
1406 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Steve Kondik13582572015-12-05 17:19:19 -08001407 ALOGV("Input profile %d", j);
Mingming Yin38877802015-10-05 15:24:04 -07001408 for (size_t k = 0; k < inProfile->mChannelMasks.size(); k++) {
1409 audio_channel_mask_t channelMask =
1410 inProfile->mChannelMasks.itemAt(k);
1411 ALOGV("Channel Mask %x size %d", channelMask,
1412 inProfile->mChannelMasks.size());
1413 if (AUDIO_CHANNEL_IN_5POINT1 == channelMask) {
1414 if (!prop_ssr_enabled) {
1415 ALOGI("removing AUDIO_CHANNEL_IN_5POINT1 from"
1416 " input profile as SSR(surround sound record)"
1417 " is not supported on this chipset variant");
1418 inProfile->mChannelMasks.removeItemsAt(k, 1);
1419 ALOGV("Channel Mask size now %d",
1420 inProfile->mChannelMasks.size());
1421 }
1422 }
1423 }
1424 }
1425 }
1426
1427#ifdef RECORD_PLAY_CONCURRENCY
1428 mIsInputRequestOnProgress = false;
1429#endif
1430
1431
1432#ifdef VOICE_CONCURRENCY
1433 mFallBackflag = getFallBackPath();
1434#endif
1435}
1436
Sharad Sangleca67a022015-05-28 16:15:16 +05301437}