blob: 93d014ec0fb9bcc4622e6108054d26901e4dd237 [file] [log] [blame]
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001/*
Sharad Sangle36781612015-05-28 16:15:16 +05302 * Copyright (c) 2013-2015, The Linux Foundation. All rights reserved.
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07003 * Not a contribution.
4 *
5 * Copyright (C) 2009 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
Sharad Sangle36781612015-05-28 16:15:16 +053020#define LOG_TAG "AudioPolicyManagerCustom"
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070021//#define LOG_NDEBUG 0
22
23//#define VERY_VERBOSE_LOGGING
24#ifdef VERY_VERBOSE_LOGGING
25#define ALOGVV ALOGV
26#else
27#define ALOGVV(a...) do { } while(0)
28#endif
29
Sharad Sangle36781612015-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
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070035// 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)
Sharad Sangle36781612015-05-28 16:15:16 +053039// 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
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070043#include <inttypes.h>
Mingming Yin0ae14ea2014-07-09 17:55:56 -070044#include <math.h>
Mingming Yin0670f162014-06-12 16:05:49 -070045
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070046#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"
Sharad Sangle36781612015-05-28 16:15:16 +053053#include <policy.h>
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070054
55namespace android {
Sharad Sanglec5766ff2015-06-04 20:24:10 +053056#ifdef VOICE_CONCURRENCY
57audio_output_flags_t AudioPolicyManagerCustom::getFallBackPath()
58{
59 audio_output_flags_t flag = AUDIO_OUTPUT_FLAG_FAST;
60 char propValue[PROPERTY_VALUE_MAX];
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070061
Sharad Sanglec5766ff2015-06-04 20:24:10 +053062 if (property_get("voice.conc.fallbackpath", propValue, NULL)) {
63 if (!strncmp(propValue, "deep-buffer", 11)) {
64 flag = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
65 }
66 else if (!strncmp(propValue, "fast", 4)) {
67 flag = AUDIO_OUTPUT_FLAG_FAST;
68 }
69 else {
70 ALOGD("voice_conc:not a recognised path(%s) in prop voice.conc.fallbackpath",
71 propValue);
72 }
73 }
74 else {
75 ALOGD("voice_conc:prop voice.conc.fallbackpath not set");
76 }
77
78 ALOGD("voice_conc:picked up flag(0x%x) from prop voice.conc.fallbackpath",
79 flag);
80
81 return flag;
82}
83#endif /*VOICE_CONCURRENCY*/
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070084// ----------------------------------------------------------------------------
85// AudioPolicyInterface implementation
86// ----------------------------------------------------------------------------
Sharad Sangle36781612015-05-28 16:15:16 +053087extern "C" AudioPolicyInterface* createAudioPolicyManager(
88 AudioPolicyClientInterface *clientInterface)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070089{
Sharad Sangle36781612015-05-28 16:15:16 +053090 return new AudioPolicyManagerCustom(clientInterface);
91}
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070092
Sharad Sangle36781612015-05-28 16:15:16 +053093extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface)
94{
95 delete interface;
96}
97
98status_t AudioPolicyManagerCustom::setDeviceConnectionStateInt(audio_devices_t device,
99 audio_policy_dev_state_t state,
100 const char *device_address,
101 const char *device_name)
102{
103 ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
104 device, state, device_address, device_name);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700105
106 // connect/disconnect only 1 device at a time
107 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
108
Sharad Sangle36781612015-05-28 16:15:16 +0530109 sp<DeviceDescriptor> devDesc =
110 mHwModules.getDeviceDescriptor(device, device_address, device_name);
111
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700112 // handle output devices
113 if (audio_is_output_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700114 SortedVector <audio_io_handle_t> outputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700115
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700116 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700117
118 // save a copy of the opened output descriptors before any output is opened or closed
119 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
120 mPreviousOutputs = mOutputs;
121 switch (state)
122 {
123 // handle output device connection
Sharad Sangle36781612015-05-28 16:15:16 +0530124 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700125 if (index >= 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700126 ALOGW("setDeviceConnectionState() device already connected: %x", device);
127 return INVALID_OPERATION;
128 }
129 ALOGV("setDeviceConnectionState() connecting device %x", device);
130
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700131 // register new device as available
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700132 index = mAvailableOutputDevices.add(devDesc);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700133 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530134 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700135 if (module == 0) {
136 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
137 device);
138 mAvailableOutputDevices.remove(devDesc);
139 return INVALID_OPERATION;
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700140 }
Sharad Sangle36781612015-05-28 16:15:16 +0530141 mAvailableOutputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700142 } else {
143 return NO_MEMORY;
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700144 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700145
Sharad Sangle36781612015-05-28 16:15:16 +0530146 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700147 mAvailableOutputDevices.remove(devDesc);
148 return INVALID_OPERATION;
149 }
Sharad Sangle36781612015-05-28 16:15:16 +0530150 // Propagate device availability to Engine
151 mEngine->setDeviceConnectionState(devDesc, state);
152
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700153 // outputs should never be empty here
154 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
155 "checkOutputsForDevice() returned no outputs but status OK");
156 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
157 outputs.size());
Sharad Sangle36781612015-05-28 16:15:16 +0530158
159 // Send connect to HALs
160 AudioParameter param = AudioParameter(devDesc->mAddress);
161 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
162 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
163
164 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700165 // handle output device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700166 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
167 if (index < 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700168 ALOGW("setDeviceConnectionState() device not connected: %x", device);
169 return INVALID_OPERATION;
170 }
171
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700172 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
173
Sharad Sangle36781612015-05-28 16:15:16 +0530174 // Send Disconnect to HALs
175 AudioParameter param = AudioParameter(devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700176 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
177 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
178
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700179 // remove device from available output devices
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700180 mAvailableOutputDevices.remove(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700181
Sharad Sangle36781612015-05-28 16:15:16 +0530182 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
183
184 // Propagate device availability to Engine
185 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700186 } break;
187
188 default:
189 ALOGE("setDeviceConnectionState() invalid state: %x", state);
190 return BAD_VALUE;
191 }
192
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700193 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
194 // output is suspended before any tracks are moved to it
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700195 checkA2dpSuspend();
196 checkOutputForAllStrategies();
197 // outputs must be closed after checkOutputForAllStrategies() is executed
198 if (!outputs.isEmpty()) {
199 for (size_t i = 0; i < outputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530200 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700201 // close unused outputs after device disconnection or direct outputs that have been
202 // opened by checkOutputsForDevice() to query dynamic parameters
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700203 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700204 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
205 (desc->mDirectOpenCount == 0))) {
206 closeOutput(outputs[i]);
207 }
208 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700209 // check again after closing A2DP output to reset mA2dpSuspended if needed
210 checkA2dpSuspend();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700211 }
212
213 updateDevicesAndOutputs();
Sharad Sangle36781612015-05-28 16:15:16 +0530214 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
215 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700216 updateCallRouting(newDevice);
217 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700218 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530219 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
220 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
221 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700222 // do not force device change on duplicated output because if device is 0, it will
223 // also force a device 0 for the two outputs it is duplicated to which may override
224 // a valid device selection on those outputs.
Sharad Sangle36781612015-05-28 16:15:16 +0530225 bool force = !desc->isDuplicated()
226 && (!device_distinguishes_on_address(device)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700227 // always force when disconnecting (a non-duplicated device)
228 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Sharad Sangle36781612015-05-28 16:15:16 +0530229 setOutputDevice(desc, newDevice, force, 0);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700230 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700231 }
232
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700233 mpClientInterface->onAudioPortListUpdate();
234 return NO_ERROR;
235 } // end if is output device
236
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700237 // handle input devices
238 if (audio_is_input_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700239 SortedVector <audio_io_handle_t> inputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700240
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700241 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700242 switch (state)
243 {
244 // handle input device connection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700245 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
246 if (index >= 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700247 ALOGW("setDeviceConnectionState() device already connected: %d", device);
248 return INVALID_OPERATION;
249 }
Sharad Sangle36781612015-05-28 16:15:16 +0530250 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700251 if (module == NULL) {
252 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
253 device);
254 return INVALID_OPERATION;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700255 }
Sharad Sangle36781612015-05-28 16:15:16 +0530256 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700257 return INVALID_OPERATION;
258 }
259
260 index = mAvailableInputDevices.add(devDesc);
261 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530262 mAvailableInputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700263 } else {
264 return NO_MEMORY;
265 }
Sharad Sangle36781612015-05-28 16:15:16 +0530266
267 // Set connect to HALs
268 AudioParameter param = AudioParameter(devDesc->mAddress);
269 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
270 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
271
272 // Propagate device availability to Engine
273 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700274 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700275
276 // handle input device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700277 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
278 if (index < 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700279 ALOGW("setDeviceConnectionState() device not connected: %d", device);
280 return INVALID_OPERATION;
281 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700282
283 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
284
285 // Set Disconnect to HALs
Sharad Sangle36781612015-05-28 16:15:16 +0530286 AudioParameter param = AudioParameter(devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700287 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
288 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
289
Sharad Sangle36781612015-05-28 16:15:16 +0530290 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700291 mAvailableInputDevices.remove(devDesc);
292
Sharad Sangle36781612015-05-28 16:15:16 +0530293 // Propagate device availability to Engine
294 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700295 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700296
297 default:
298 ALOGE("setDeviceConnectionState() invalid state: %x", state);
299 return BAD_VALUE;
300 }
301
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700302 closeAllInputs();
303
Sharad Sangle36781612015-05-28 16:15:16 +0530304 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700305 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
306 updateCallRouting(newDevice);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700307 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700308
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700309 mpClientInterface->onAudioPortListUpdate();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700310 return NO_ERROR;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700311 } // end if is input device
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700312
313 ALOGW("setDeviceConnectionState() invalid device: %x", device);
314 return BAD_VALUE;
315}
Sharad Sangle36781612015-05-28 16:15:16 +0530316// This function checks for the parameters which can be offloaded.
317// This can be enhanced depending on the capability of the DSP and policy
318// of the system.
319bool AudioPolicyManagerCustom::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700320{
Sharad Sangle36781612015-05-28 16:15:16 +0530321 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
322 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
323 offloadInfo.sample_rate, offloadInfo.channel_mask,
324 offloadInfo.format,
325 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
326 offloadInfo.has_video);
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530327#ifdef VOICE_CONCURRENCY
328 char concpropValue[PROPERTY_VALUE_MAX];
329 if (property_get("voice.playback.conc.disabled", concpropValue, NULL)) {
330 bool propenabled = atoi(concpropValue) || !strncmp("true", concpropValue, 4);
331 if (propenabled) {
332 if (isInCall())
333 {
334 ALOGD("\n copl: blocking compress offload on call mode\n");
335 return false;
336 }
337 }
338 }
339#endif
340#ifdef RECORD_PLAY_CONCURRENCY
341 char recConcPropValue[PROPERTY_VALUE_MAX];
342 bool prop_rec_play_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700343
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530344 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
345 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
346 }
347
348 if ((prop_rec_play_enabled) &&
349 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
350 ALOGD("copl: blocking compress offload for record concurrency");
351 return false;
352 }
353#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530354 // Check if stream type is music, then only allow offload as of now.
355 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
356 {
357 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
358 return false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700359 }
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530360
361 char propValue[PROPERTY_VALUE_MAX];
362 bool pcmOffload = false;
363#ifdef PCM_OFFLOAD_ENABLED
364 if ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM_OFFLOAD) {
365 bool prop_enabled = false;
366 if ((AUDIO_FORMAT_PCM_16_BIT_OFFLOAD == offloadInfo.format) &&
367 property_get("audio.offload.pcm.16bit.enable", propValue, NULL)) {
368 prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
369 }
370
371#ifdef PCM_OFFLOAD_ENABLED_24
372 if ((AUDIO_FORMAT_PCM_24_BIT_OFFLOAD == offloadInfo.format) &&
373 property_get("audio.offload.pcm.24bit.enable", propValue, NULL)) {
374 prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530375 }
376#endif
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530377
378 if (prop_enabled) {
379 ALOGI("PCM offload property is enabled");
380 pcmOffload = true;
381 }
382
383 if (!pcmOffload) {
384 ALOGD("system property not enabled for PCM offload format[%x]",offloadInfo.format);
385 return false;
386 }
387 }
388#endif
389 if (!pcmOffload) {
390 // Check if offload has been disabled
391 if (property_get("audio.offload.disable", propValue, "0")) {
392 if (atoi(propValue) != 0) {
393 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
394 return false;
395 }
396 }
397 //check if it's multi-channel AAC (includes sub formats) and FLAC format
398 if ((popcount(offloadInfo.channel_mask) > 2) &&
399 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
400 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS))) {
401 ALOGD("offload disabled for multi-channel AAC,FLAC and VORBIS format");
402 return false;
403 }
404#ifdef AUDIO_EXTN_FORMATS_ENABLED
405 //check if it's multi-channel FLAC/ALAC/WMA format with sample rate > 48k
406 if ((popcount(offloadInfo.channel_mask) > 2) &&
407 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
408 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) && offloadInfo.sample_rate > 48000) ||
409 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) && offloadInfo.sample_rate > 48000) ||
410 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) && offloadInfo.sample_rate > 48000))) {
411 ALOGD("offload disabled for multi-channel FLAC/ALAC/WMA clips with sample rate > 48kHz");
412 return false;
413 }
414#endif
415 //TODO: enable audio offloading with video when ready
416 const bool allowOffloadWithVideo =
417 property_get_bool("audio.offload.video", false /* default_value */);
418 if (offloadInfo.has_video && !allowOffloadWithVideo) {
419 ALOGV("isOffloadSupported: has_video == true, returning false");
420 return false;
421 }
Sharad Sangle36781612015-05-28 16:15:16 +0530422 }
423
424 //If duration is less than minimum value defined in property, return false
425 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
426 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
427 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
428 return false;
429 }
430 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
431 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
432 //duration checks only valid for MP3/AAC/ formats,
433 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
434 if ((offloadInfo.format == AUDIO_FORMAT_MP3) ||
435 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530436 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS)
437#ifdef AUDIO_EXTN_FORMATS_ENABLED
Sharad Sangle36781612015-05-28 16:15:16 +0530438 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
Sharad Sangle36781612015-05-28 16:15:16 +0530439 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) ||
440 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) ||
441 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) ||
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530442 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_APE)
443#endif
444 )
Sharad Sangle36781612015-05-28 16:15:16 +0530445 return false;
446
447 }
448
449 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
450 // creating an offloaded track and tearing it down immediately after start when audioflinger
451 // detects there is an active non offloadable effect.
452 // FIXME: We should check the audio session here but we do not have it in this context.
453 // This may prevent offloading in rare situations where effects are left active by apps
454 // in the background.
455 if (mEffects.isNonOffloadableEffectEnabled()) {
456 return false;
457 }
458 // Check for soundcard status
459 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
460 String8("SND_CARD_STATUS"));
461 AudioParameter result = AudioParameter(valueStr);
462 int isonline = 0;
463 if ((result.getInt(String8("SND_CARD_STATUS"), isonline) == NO_ERROR)
464 && !isonline) {
465 ALOGD("copl: soundcard is offline rejecting offload request");
466 return false;
467 }
468 // See if there is a profile to support this.
469 // AUDIO_DEVICE_NONE
470 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
471 offloadInfo.sample_rate,
472 offloadInfo.format,
473 offloadInfo.channel_mask,
474 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
475 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
476 return (profile != 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700477}
Sharad Sangle36781612015-05-28 16:15:16 +0530478audio_devices_t AudioPolicyManagerCustom::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
479 bool fromCache)
480{
481 audio_devices_t device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700482
Sharad Sangle36781612015-05-28 16:15:16 +0530483 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
484 if (index >= 0) {
485 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
486 if (patchDesc->mUid != mUidCached) {
487 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
488 outputDesc->device(), outputDesc->mPatchHandle);
489 return outputDesc->device();
490 }
491 }
492
493 // check the following by order of priority to request a routing change if necessary:
494 // 1: the strategy enforced audible is active and enforced on the output:
495 // use device for strategy enforced audible
496 // 2: we are in call or the strategy phone is active on the output:
497 // use device for strategy phone
498 // 3: the strategy for enforced audible is active but not enforced on the output:
499 // use the device for strategy enforced audible
500 // 4: the strategy sonification is active on the output:
501 // use device for strategy sonification
502 // 5: the strategy "respectful" sonification is active on the output:
503 // use device for strategy "respectful" sonification
504 // 6: the strategy accessibility is active on the output:
505 // use device for strategy accessibility
506 // 7: the strategy media is active on the output:
507 // use device for strategy media
508 // 8: the strategy DTMF is active on the output:
509 // use device for strategy DTMF
510 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
511 // use device for strategy t-t-s
512 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
513 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
514 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
515 } else if (isInCall() ||
516 isStrategyActive(outputDesc, STRATEGY_PHONE)||
517 isStrategyActive(mPrimaryOutput, STRATEGY_PHONE)) {
518 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
519 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
520 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
521 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)||
522 (isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION)
523 && (!isStrategyActive(mPrimaryOutput,STRATEGY_MEDIA)))) {
524 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
525 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)||
526 (isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION_RESPECTFUL)
527 && (!isStrategyActive(mPrimaryOutput, STRATEGY_MEDIA)))) {
528 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
529 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
530 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
531 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
532 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
533 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
534 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
535 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
536 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
537 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
538 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
539 }
540
541 ALOGV("getNewOutputDevice() selected device %x", device);
542 return device;
543}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700544void AudioPolicyManagerCustom::setPhoneState(audio_mode_t state)
545{
Sharad Sangle36781612015-05-28 16:15:16 +0530546 ALOGV("setPhoneState() state %d", state);
547 // store previous phone state for management of sonification strategy below
548 int oldState = mEngine->getPhoneState();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700549
Sharad Sangle36781612015-05-28 16:15:16 +0530550 if (mEngine->setPhoneState(state) != NO_ERROR) {
551 ALOGW("setPhoneState() invalid or same state %d", state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700552 return;
553 }
Sharad Sangle36781612015-05-28 16:15:16 +0530554 /// Opens: can these line be executed after the switch of volume curves???
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700555 // if leaving call state, handle special case of active streams
556 // pertaining to sonification strategy see handleIncallSonification()
557 if (isInCall()) {
558 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530559 for (size_t j = 0; j < mOutputs.size(); j++) {
560 audio_io_handle_t curOutput = mOutputs.keyAt(j);
561 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
562 if (stream == AUDIO_STREAM_PATCH) {
563 continue;
564 }
565
566 handleIncallSonification((audio_stream_type_t)stream, false, true, curOutput);
567 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700568 }
Sharad Sangle36781612015-05-28 16:15:16 +0530569
570 // force reevaluating accessibility routing when call starts
571 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700572 }
573
Sharad Sangle36781612015-05-28 16:15:16 +0530574 /**
575 * Switching to or from incall state or switching between telephony and VoIP lead to force
576 * routing command.
577 */
578 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
579 || (is_state_in_call(state) && (state != oldState)));
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700580
581 // check for device and output changes triggered by new phone state
582 checkA2dpSuspend();
583 checkOutputForAllStrategies();
584 updateDevicesAndOutputs();
585
Sharad Sangle36781612015-05-28 16:15:16 +0530586 sp<SwAudioOutputDescriptor> hwOutputDesc = mPrimaryOutput;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530587#ifdef VOICE_CONCURRENCY
588 int voice_call_state = 0;
589 char propValue[PROPERTY_VALUE_MAX];
590 bool prop_playback_enabled = false, prop_rec_enabled=false, prop_voip_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700591
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530592 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
593 prop_playback_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
594 }
595
596 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
597 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
598 }
599
600 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
601 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
602 }
603
604 bool mode_in_call = (AUDIO_MODE_IN_CALL != oldState) && (AUDIO_MODE_IN_CALL == state);
605 //query if it is a actual voice call initiated by telephony
606 if (mode_in_call) {
607 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0, String8("in_call"));
608 AudioParameter result = AudioParameter(valueStr);
609 if (result.getInt(String8("in_call"), voice_call_state) == NO_ERROR)
610 ALOGD("voice_conc:SetPhoneState: Voice call state = %d", voice_call_state);
611 }
612
613 if (mode_in_call && voice_call_state && !mvoice_call_state) {
614 ALOGD("voice_conc:Entering to call mode oldState :: %d state::%d ",
615 oldState, state);
616 mvoice_call_state = voice_call_state;
617 if (prop_rec_enabled) {
618 //Close all active inputs
619 audio_io_handle_t activeInput = mInputs.getActiveInput();
620 if (activeInput != 0) {
621 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
622 switch(activeDesc->mInputSource) {
623 case AUDIO_SOURCE_VOICE_UPLINK:
624 case AUDIO_SOURCE_VOICE_DOWNLINK:
625 case AUDIO_SOURCE_VOICE_CALL:
626 ALOGD("voice_conc:FOUND active input during call active: %d",activeDesc->mInputSource);
627 break;
628
629 case AUDIO_SOURCE_VOICE_COMMUNICATION:
630 if(prop_voip_enabled) {
631 ALOGD("voice_conc:CLOSING VoIP input source on call setup :%d ",activeDesc->mInputSource);
632 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
633 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
634 }
635 break;
636
637 default:
638 ALOGD("voice_conc:CLOSING input on call setup for inputSource: %d",activeDesc->mInputSource);
639 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
640 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
641 break;
642 }
643 }
644 } else if (prop_voip_enabled) {
645 audio_io_handle_t activeInput = mInputs.getActiveInput();
646 if (activeInput != 0) {
647 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
648 if (AUDIO_SOURCE_VOICE_COMMUNICATION == activeDesc->mInputSource) {
649 ALOGD("voice_conc:CLOSING VoIP on call setup : %d",activeDesc->mInputSource);
650 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
651 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
652 }
653 }
654 }
655 if (prop_playback_enabled) {
656 // Move tracks associated to this strategy from previous output to new output
657 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
658 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
659 if (i == AUDIO_STREAM_PATCH) {
660 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
661 continue;
662 }
663 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
664 if ((AUDIO_STREAM_MUSIC == i) ||
665 (AUDIO_STREAM_VOICE_CALL == i) ) {
666 ALOGD("voice_conc:Invalidate stream type %d", i);
667 mpClientInterface->invalidateStream((audio_stream_type_t)i);
668 }
669 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
670 ALOGD("voice_conc:Invalidate stream type %d", i);
671 mpClientInterface->invalidateStream((audio_stream_type_t)i);
672 }
673 }
674 }
675
676 for (size_t i = 0; i < mOutputs.size(); i++) {
677 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
678 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
679 ALOGD("voice_conc:ouput desc / profile is NULL");
680 continue;
681 }
682
683 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
684 if (((!outputDesc->isDuplicated() &&outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY))
685 && prop_playback_enabled) {
686 ALOGD("voice_conc:calling suspendOutput on call mode for primary output");
687 mpClientInterface->suspendOutput(mOutputs.keyAt(i));
688 } //Close compress all sessions
689 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
690 && prop_playback_enabled) {
691 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
692 closeOutput(mOutputs.keyAt(i));
693 }
694 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_VOIP_RX)
695 && prop_voip_enabled) {
696 ALOGD("voice_conc:calling closeOutput on call mode for DIRECT output");
697 closeOutput(mOutputs.keyAt(i));
698 }
699 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
700 if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)
701 && prop_playback_enabled) {
702 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
703 closeOutput(mOutputs.keyAt(i));
704 }
705 }
706 }
707 }
708
709 if ((AUDIO_MODE_IN_CALL == oldState || AUDIO_MODE_IN_COMMUNICATION == oldState) &&
710 (AUDIO_MODE_NORMAL == state) && prop_playback_enabled && mvoice_call_state) {
711 ALOGD("voice_conc:EXITING from call mode oldState :: %d state::%d \n",oldState, state);
712 mvoice_call_state = 0;
713 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
714 //restore PCM (deep-buffer) output after call termination
715 for (size_t i = 0; i < mOutputs.size(); i++) {
716 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
717 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
718 ALOGD("voice_conc:ouput desc / profile is NULL");
719 continue;
720 }
721 if (!outputDesc->isDuplicated() && outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
722 ALOGD("voice_conc:calling restoreOutput after call mode for primary output");
723 mpClientInterface->restoreOutput(mOutputs.keyAt(i));
724 }
725 }
726 }
727 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
728 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
729 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
730 if (i == AUDIO_STREAM_PATCH) {
731 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
732 continue;
733 }
734 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
735 if ((AUDIO_STREAM_MUSIC == i) ||
736 (AUDIO_STREAM_VOICE_CALL == i) ) {
737 mpClientInterface->invalidateStream((audio_stream_type_t)i);
738 }
739 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
740 mpClientInterface->invalidateStream((audio_stream_type_t)i);
741 }
742 }
743 }
744
745#endif
746#ifdef RECORD_PLAY_CONCURRENCY
747 char recConcPropValue[PROPERTY_VALUE_MAX];
748 bool prop_rec_play_enabled = false;
749
750 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
751 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
752 }
753 if (prop_rec_play_enabled) {
754 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
755 ALOGD("phone state changed to MODE_IN_COMM invlaidating music and voice streams");
756 // call invalidate for voice streams, so that it can use deepbuffer with VoIP out device from HAL
757 mpClientInterface->invalidateStream(AUDIO_STREAM_VOICE_CALL);
758 // call invalidate for music, so that compress will fallback to deep-buffer with VoIP out device
759 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
760
761 // close compress output to make sure session will be closed before timeout(60sec)
762 for (size_t i = 0; i < mOutputs.size(); i++) {
763
764 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
765 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
766 ALOGD("ouput desc / profile is NULL");
767 continue;
768 }
769
770 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
771 ALOGD("calling closeOutput on call mode for COMPRESS output");
772 closeOutput(mOutputs.keyAt(i));
773 }
774 }
775 } else if ((oldState == AUDIO_MODE_IN_COMMUNICATION) &&
776 (mEngine->getPhoneState() == AUDIO_MODE_NORMAL)) {
777 // call invalidate for music so that music can fallback to compress
778 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
779 }
780 }
781#endif
782 mPrevPhoneState = oldState;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700783 int delayMs = 0;
784 if (isStateInCall(state)) {
785 nsecs_t sysTime = systemTime();
786 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530787 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700788 // mute media and sonification strategies and delay device switch by the largest
789 // latency of any output where either strategy is active.
790 // This avoid sending the ring tone or music tail into the earpiece or headset.
Sharad Sangle36781612015-05-28 16:15:16 +0530791 if ((isStrategyActive(desc, STRATEGY_MEDIA,
792 SONIFICATION_HEADSET_MUSIC_DELAY,
793 sysTime) ||
794 isStrategyActive(desc, STRATEGY_SONIFICATION,
795 SONIFICATION_HEADSET_MUSIC_DELAY,
796 sysTime)) &&
797 (delayMs < (int)desc->latency()*2)) {
798 delayMs = desc->latency()*2;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700799 }
Sharad Sangle36781612015-05-28 16:15:16 +0530800 setStrategyMute(STRATEGY_MEDIA, true, desc);
801 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700802 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Sharad Sangle36781612015-05-28 16:15:16 +0530803 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
804 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700805 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
806 }
Sharad Sangle36781612015-05-28 16:15:16 +0530807 ALOGV("Setting the delay from %dms to %dms", delayMs,
808 MIN(delayMs, MAX_VOICE_CALL_START_DELAY_MS));
809 delayMs = MIN(delayMs, MAX_VOICE_CALL_START_DELAY_MS);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700810 }
811
Sharad Sangle36781612015-05-28 16:15:16 +0530812 if (hasPrimaryOutput()) {
813 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
814 // the device returned is not necessarily reachable via this output
815 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
816 // force routing command to audio hardware when ending call
817 // even if no device change is needed
818 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
819 rxDevice = mPrimaryOutput->device();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700820 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700821
Sharad Sangle36781612015-05-28 16:15:16 +0530822 if (state == AUDIO_MODE_IN_CALL) {
823 updateCallRouting(rxDevice, delayMs);
824 } else if (oldState == AUDIO_MODE_IN_CALL) {
825 if (mCallRxPatch != 0) {
826 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
827 mCallRxPatch.clear();
828 }
829 if (mCallTxPatch != 0) {
830 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
831 mCallTxPatch.clear();
832 }
833 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
834 } else {
835 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700836 }
837 }
838
839 // if entering in call state, handle special case of active streams
840 // pertaining to sonification strategy see handleIncallSonification()
841 if (isStateInCall(state)) {
842 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530843 for (size_t j = 0; j < mOutputs.size(); j++) {
844 audio_io_handle_t curOutput = mOutputs.keyAt(j);
845 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
846 if (stream == AUDIO_STREAM_PATCH) {
847 continue;
848 }
849 handleIncallSonification((audio_stream_type_t)stream, true, true, curOutput);
850 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700851 }
852 }
853
854 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
855 if (state == AUDIO_MODE_RINGTONE &&
856 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
857 mLimitRingtoneVolume = true;
858 } else {
859 mLimitRingtoneVolume = false;
860 }
861}
Sharad Sangle36781612015-05-28 16:15:16 +0530862status_t AudioPolicyManagerCustom::stopSource(sp<SwAudioOutputDescriptor> outputDesc,
863 audio_stream_type_t stream,
864 bool forceDeviceUpdate)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700865{
Sharad Sangle36781612015-05-28 16:15:16 +0530866 // always handle stream stop, check which stream type is stopping
867 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700868
Sharad Sangle36781612015-05-28 16:15:16 +0530869 // handle special case for sonification while in call
870 if (isInCall()) {
871 if (outputDesc->isDuplicated()) {
872 handleIncallSonification(stream, false, false, outputDesc->mIoHandle);
873 handleIncallSonification(stream, false, false, outputDesc->mIoHandle);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700874 }
Sharad Sangle36781612015-05-28 16:15:16 +0530875 handleIncallSonification(stream, false, false, outputDesc->mIoHandle);
876 }
877
878 if (outputDesc->mRefCount[stream] > 0) {
879 // decrement usage count of this stream on the output
880 outputDesc->changeRefCount(stream, -1);
881
882 // store time at which the stream was stopped - see isStreamActive()
883 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
884 outputDesc->mStopTime[stream] = systemTime();
885 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
886 // delay the device switch by twice the latency because stopOutput() is executed when
887 // the track stop() command is received and at that time the audio track buffer can
888 // still contain data that needs to be drained. The latency only covers the audio HAL
889 // and kernel buffers. Also the latency does not always include additional delay in the
890 // audio path (audio DSP, CODEC ...)
891 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
892
893 // force restoring the device selection on other active outputs if it differs from the
894 // one being selected for this output
895 for (size_t i = 0; i < mOutputs.size(); i++) {
896 audio_io_handle_t curOutput = mOutputs.keyAt(i);
897 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
898 if (desc != outputDesc &&
899 desc->isActive() &&
900 outputDesc->sharesHwModuleWith(desc) &&
901 (newDevice != desc->device())) {
902 setOutputDevice(desc,
903 getNewOutputDevice(desc, false /*fromCache*/),
904 true,
905 outputDesc->latency()*2);
906 }
907 }
908 // update the outputs if stopping one with a stream that can affect notification routing
909 handleNotificationRoutingForStream(stream);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700910 }
Sharad Sangle36781612015-05-28 16:15:16 +0530911 return NO_ERROR;
912 } else {
913 ALOGW("stopOutput() refcount is already 0");
914 return INVALID_OPERATION;
915 }
916}
917status_t AudioPolicyManagerCustom::startSource(sp<SwAudioOutputDescriptor> outputDesc,
918 audio_stream_type_t stream,
919 audio_devices_t device,
920 uint32_t *delayMs)
921{
922 // cannot start playback of STREAM_TTS if any other output is being used
923 uint32_t beaconMuteLatency = 0;
924
925 *delayMs = 0;
926 if (stream == AUDIO_STREAM_TTS) {
927 ALOGV("\t found BEACON stream");
928 if (mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
929 return INVALID_OPERATION;
930 } else {
931 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700932 }
Sharad Sangle36781612015-05-28 16:15:16 +0530933 } else {
934 // some playback other than beacon starts
935 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
936 }
937
938 // increment usage count for this stream on the requested output:
939 // NOTE that the usage count is the same for duplicated output and hardware output which is
940 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
941 outputDesc->changeRefCount(stream, 1);
942
943 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
944 // starting an output being rerouted?
945 if (device == AUDIO_DEVICE_NONE) {
946 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700947 }
Sharad Sangle36781612015-05-28 16:15:16 +0530948 routing_strategy strategy = getStrategy(stream);
949 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
950 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
951 (beaconMuteLatency > 0);
952 uint32_t waitMs = beaconMuteLatency;
953 bool force = false;
954 for (size_t i = 0; i < mOutputs.size(); i++) {
955 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
956 if (desc != outputDesc) {
957 // force a device change if any other output is managed by the same hw
958 // module and has a current device selection that differs from selected device.
959 // In this case, the audio HAL must receive the new device selection so that it can
960 // change the device currently selected by the other active output.
961 if (outputDesc->sharesHwModuleWith(desc) &&
962 desc->device() != device) {
963 force = true;
964 }
965 // wait for audio on other active outputs to be presented when starting
966 // a notification so that audio focus effect can propagate, or that a mute/unmute
967 // event occurred for beacon
968 uint32_t latency = desc->latency();
969 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
970 waitMs = latency;
971 }
972 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700973 }
Sharad Sangle36781612015-05-28 16:15:16 +0530974 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force);
975
976 // handle special case for sonification while in call
977 if (isInCall()) {
978 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700979 }
Sharad Sangle36781612015-05-28 16:15:16 +0530980
981 // apply volume rules for current stream and device if necessary
982 checkAndSetVolume(stream,
983 mStreams.valueFor(stream).getVolumeIndex(device),
984 outputDesc,
985 device);
986
987 // update the outputs if starting an output with a stream that can affect notification
988 // routing
989 handleNotificationRoutingForStream(stream);
990
991 // force reevaluating accessibility routing when ringtone or alarm starts
992 if (strategy == STRATEGY_SONIFICATION) {
993 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
994 }
995 }
996 else {
997 // handle special case for sonification while in call
998 if (isInCall()) {
999 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
1000 }
1001 }
1002 return NO_ERROR;
1003}
1004void AudioPolicyManagerCustom::handleIncallSonification(audio_stream_type_t stream,
1005 bool starting, bool stateChange,
1006 audio_io_handle_t output)
1007{
1008 if(!hasPrimaryOutput()) {
1009 return;
1010 }
1011 // no action needed for AUDIO_STREAM_PATCH stream type, it's for internal flinger tracks
1012 if (stream == AUDIO_STREAM_PATCH) {
1013 return;
1014 }
1015 // if the stream pertains to sonification strategy and we are in call we must
1016 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
1017 // in the device used for phone strategy and play the tone if the selected device does not
1018 // interfere with the device used for phone strategy
1019 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
1020 // many times as there are active tracks on the output
1021 const routing_strategy stream_strategy = getStrategy(stream);
1022 if ((stream_strategy == STRATEGY_SONIFICATION) ||
1023 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
1024 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
1025 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
1026 stream, starting, outputDesc->mDevice, stateChange);
1027 if (outputDesc->mRefCount[stream]) {
1028 int muteCount = 1;
1029 if (stateChange) {
1030 muteCount = outputDesc->mRefCount[stream];
1031 }
1032 if (audio_is_low_visibility(stream)) {
1033 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
1034 for (int i = 0; i < muteCount; i++) {
1035 setStreamMute(stream, starting, outputDesc);
1036 }
1037 } else {
1038 ALOGV("handleIncallSonification() high visibility");
1039 if (outputDesc->device() &
1040 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
1041 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
1042 for (int i = 0; i < muteCount; i++) {
1043 setStreamMute(stream, starting, outputDesc);
1044 }
1045 }
1046 if (starting) {
1047 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
1048 AUDIO_STREAM_VOICE_CALL);
1049 } else {
1050 mpClientInterface->stopTone();
1051 }
1052 }
1053 }
1054 }
1055}
1056void AudioPolicyManagerCustom::handleNotificationRoutingForStream(audio_stream_type_t stream) {
1057 switch(stream) {
1058 case AUDIO_STREAM_MUSIC:
1059 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
1060 updateDevicesAndOutputs();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001061 break;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001062 default:
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001063 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001064 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001065}
Sharad Sangle36781612015-05-28 16:15:16 +05301066status_t AudioPolicyManagerCustom::checkAndSetVolume(audio_stream_type_t stream,
1067 int index,
1068 const sp<SwAudioOutputDescriptor>& outputDesc,
1069 audio_devices_t device,
1070 int delayMs, bool force)
1071{
1072 // do not change actual stream volume if the stream is muted
1073 if (outputDesc->mMuteCount[stream] != 0) {
1074 ALOGVV("checkAndSetVolume() stream %d muted count %d",
1075 stream, outputDesc->mMuteCount[stream]);
1076 return NO_ERROR;
1077 }
1078 audio_policy_forced_cfg_t forceUseForComm =
1079 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
1080 // do not change in call volume if bluetooth is connected and vice versa
1081 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
1082 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
1083 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
1084 stream, forceUseForComm);
1085 return INVALID_OPERATION;
1086 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001087
Sharad Sangle36781612015-05-28 16:15:16 +05301088 if (device == AUDIO_DEVICE_NONE) {
1089 device = outputDesc->device();
1090 }
1091
1092 float volumeDb = computeVolume(stream, index, device);
1093 if (outputDesc->isFixedVolume(device)) {
1094 volumeDb = 0.0f;
1095 }
1096
1097 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
1098
1099 if (stream == AUDIO_STREAM_VOICE_CALL ||
1100 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
1101 float voiceVolume;
1102 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
1103 if (stream == AUDIO_STREAM_VOICE_CALL) {
1104 voiceVolume = (float)index/(float)mStreams.valueFor(stream).getVolumeIndexMax();
1105 } else {
1106 voiceVolume = 1.0;
1107 }
1108
1109 if (voiceVolume != mLastVoiceVolume && ((outputDesc == mPrimaryOutput) ||
1110 isDirectOutput(outputDesc->mIoHandle) || device & AUDIO_DEVICE_OUT_ALL_USB)) {
1111 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
1112 mLastVoiceVolume = voiceVolume;
1113 }
1114 }
1115
1116 return NO_ERROR;
1117}
1118bool AudioPolicyManagerCustom::isDirectOutput(audio_io_handle_t output) {
1119 for (size_t i = 0; i < mOutputs.size(); i++) {
1120 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1121 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1122 if ((curOutput == output) && (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
1123 return true;
1124 }
1125 }
1126 return false;
1127}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001128audio_io_handle_t AudioPolicyManagerCustom::getOutputForDevice(
1129 audio_devices_t device,
Sharad Sangle36781612015-05-28 16:15:16 +05301130 audio_session_t session __unused,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001131 audio_stream_type_t stream,
1132 uint32_t samplingRate,
1133 audio_format_t format,
1134 audio_channel_mask_t channelMask,
1135 audio_output_flags_t flags,
1136 const audio_offload_info_t *offloadInfo)
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001137{
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001138 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
1139 uint32_t latency = 0;
1140 status_t status;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001141
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001142#ifdef AUDIO_POLICY_TEST
1143 if (mCurOutput != 0) {
1144 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1145 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1146
1147 if (mTestOutputs[mCurOutput] == 0) {
1148 ALOGV("getOutput() opening test output");
Sharad Sangle36781612015-05-28 16:15:16 +05301149 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
1150 mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001151 outputDesc->mDevice = mTestDevice;
1152 outputDesc->mLatency = mTestLatencyMs;
1153 outputDesc->mFlags =
1154 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
1155 outputDesc->mRefCount[stream] = 0;
1156 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1157 config.sample_rate = mTestSamplingRate;
1158 config.channel_mask = mTestChannels;
1159 config.format = mTestFormat;
1160 if (offloadInfo != NULL) {
1161 config.offload_info = *offloadInfo;
1162 }
1163 status = mpClientInterface->openOutput(0,
1164 &mTestOutputs[mCurOutput],
1165 &config,
1166 &outputDesc->mDevice,
1167 String8(""),
1168 &outputDesc->mLatency,
1169 outputDesc->mFlags);
1170 if (status == NO_ERROR) {
1171 outputDesc->mSamplingRate = config.sample_rate;
1172 outputDesc->mFormat = config.format;
1173 outputDesc->mChannelMask = config.channel_mask;
1174 AudioParameter outputCmd = AudioParameter();
1175 outputCmd.addInt(String8("set_id"),mCurOutput);
1176 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1177 addOutput(mTestOutputs[mCurOutput], outputDesc);
1178 }
1179 }
1180 return mTestOutputs[mCurOutput];
1181 }
1182#endif //AUDIO_POLICY_TEST
Sharad Sangle36781612015-05-28 16:15:16 +05301183 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1184 (stream != AUDIO_STREAM_MUSIC)) {
1185 // compress should not be used for non-music streams
1186 ALOGE("Offloading only allowed with music stream");
1187 return 0;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301188 }
1189#ifdef VOICE_CONCURRENCY
1190 char propValue[PROPERTY_VALUE_MAX];
1191 bool prop_play_enabled=false, prop_voip_enabled = false;
1192
1193 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1194 prop_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001195 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301196
1197 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1198 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1199 }
1200
1201 if (prop_play_enabled && mvoice_call_state) {
1202 //check if voice call is active / running in background
1203 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1204 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1205 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1206 {
1207 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1208 if(prop_voip_enabled) {
1209 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1210 flags );
1211 return 0;
1212 }
1213 }
1214 else {
1215 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
1216 ALOGD("voice_conc:IN call mode adding ULL flags .. flags: %x ", flags );
1217 flags = AUDIO_OUTPUT_FLAG_FAST;
1218 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
1219 if (AUDIO_STREAM_MUSIC == stream) {
1220 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1221 ALOGD("voice_conc:IN call mode adding deep-buffer flags %x ", flags );
1222 }
1223 else {
1224 flags = AUDIO_OUTPUT_FLAG_FAST;
1225 ALOGD("voice_conc:IN call mode adding fast flags %x ", flags );
1226 }
1227 }
1228 }
1229 }
1230 } else if (prop_voip_enabled && mvoice_call_state) {
1231 //check if voice call is active / running in background
1232 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1233 //return only ULL ouput
1234 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1235 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1236 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1237 {
1238 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1239 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1240 flags );
1241 return 0;
1242 }
1243 }
1244 }
1245#endif
1246#ifdef RECORD_PLAY_CONCURRENCY
1247 char recConcPropValue[PROPERTY_VALUE_MAX];
1248 bool prop_rec_play_enabled = false;
1249
1250 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
1251 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
1252 }
1253 if ((prop_rec_play_enabled) &&
1254 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
1255 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
1256 if (AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1257 // allow VoIP using voice path
1258 // Do nothing
1259 } else if((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1260 ALOGD("voice_conc:MODE_IN_COMM is setforcing deep buffer output for non ULL... flags: %x", flags);
1261 // use deep buffer path for all non ULL outputs
1262 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1263 }
1264 } else if ((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1265 ALOGD("voice_conc:Record mode is on forcing deep buffer output for non ULL... flags: %x ", flags);
1266 // use deep buffer path for all non ULL outputs
1267 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1268 }
1269 }
1270 if (prop_rec_play_enabled &&
1271 (stream == AUDIO_STREAM_ENFORCED_AUDIBLE)) {
1272 ALOGD("Record conc is on forcing ULL output for ENFORCED_AUDIBLE");
1273 flags = AUDIO_OUTPUT_FLAG_FAST;
1274 }
1275#endif
1276
Sharad Sangle36781612015-05-28 16:15:16 +05301277 /*
1278 * WFD audio routes back to target speaker when starting a ringtone playback.
1279 * This is because primary output is reused for ringtone, so output device is
1280 * updated based on SONIFICATION strategy for both ringtone and music playback.
1281 * The same issue is not seen on remoted_submix HAL based WFD audio because
1282 * primary output is not reused and a new output is created for ringtone playback.
1283 * Issue is fixed by updating output flag to AUDIO_OUTPUT_FLAG_FAST when there is
1284 * a non-music stream playback on WFD, so primary output is not reused for ringtone.
1285 */
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001286 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
1287 if ((availableOutputDeviceTypes & AUDIO_DEVICE_OUT_PROXY)
1288 && (stream != AUDIO_STREAM_MUSIC)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301289 ALOGD("WFD audio: use OUTPUT_FLAG_FAST for non music stream. flags:%x", flags );
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001290 //For voip paths
1291 if(flags & AUDIO_OUTPUT_FLAG_DIRECT)
1292 flags = AUDIO_OUTPUT_FLAG_DIRECT;
1293 else //route every thing else to ULL path
1294 flags = AUDIO_OUTPUT_FLAG_FAST;
1295 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001296 // open a direct output if required by specified parameters
1297 //force direct flag if offload flag is set: offloading implies a direct output stream
1298 // and all common behaviors are driven by checking only the direct flag
1299 // this should normally be set appropriately in the policy configuration file
1300 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1301 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1302 }
1303 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1304 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1305 }
Sharad Sangle36781612015-05-28 16:15:16 +05301306 // only allow deep buffering for music stream type
1307 if (stream != AUDIO_STREAM_MUSIC) {
1308 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
1309 }
1310 if (stream == AUDIO_STREAM_TTS) {
1311 flags = AUDIO_OUTPUT_FLAG_TTS;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001312 }
1313
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301314 // open a direct output if required by specified parameters
1315 //force direct flag if offload flag is set: offloading implies a direct output stream
1316 // and all common behaviors are driven by checking only the direct flag
1317 // this should normally be set appropriately in the policy configuration file
1318 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1319 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1320 }
1321 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1322 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1323 }
1324 // only allow deep buffering for music stream type
1325 if (stream != AUDIO_STREAM_MUSIC) {
1326 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
1327 }
1328 if (stream == AUDIO_STREAM_TTS) {
1329 flags = AUDIO_OUTPUT_FLAG_TTS;
1330 }
1331
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001332 sp<IOProfile> profile;
1333
1334 // skip direct output selection if the request can obviously be attached to a mixed output
1335 // and not explicitly requested
1336 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
1337 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
1338 audio_channel_count_from_out_mask(channelMask) <= 2) {
1339 goto non_direct_output;
1340 }
1341
1342 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1343 // creating an offloaded track and tearing it down immediately after start when audioflinger
1344 // detects there is an active non offloadable effect.
1345 // FIXME: We should check the audio session here but we do not have it in this context.
1346 // This may prevent offloading in rare situations where effects are left active by apps
1347 // in the background.
1348
Sharad Sangle36781612015-05-28 16:15:16 +05301349 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
1350 !mEffects.isNonOffloadableEffectEnabled()) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001351 profile = getProfileForDirectOutput(device,
1352 samplingRate,
1353 format,
1354 channelMask,
1355 (audio_output_flags_t)flags);
1356 }
1357
1358 if (profile != 0) {
Sharad Sangle36781612015-05-28 16:15:16 +05301359 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001360
1361 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +05301362 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001363 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1364 outputDesc = desc;
1365 // reuse direct output if currently open and configured with same parameters
1366 if ((samplingRate == outputDesc->mSamplingRate) &&
1367 (format == outputDesc->mFormat) &&
1368 (channelMask == outputDesc->mChannelMask)) {
1369 outputDesc->mDirectOpenCount++;
1370 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1371 return mOutputs.keyAt(i);
1372 }
1373 }
1374 }
1375 // close direct output if currently open and configured with different parameters
1376 if (outputDesc != NULL) {
1377 closeOutput(outputDesc->mIoHandle);
1378 }
Sharad Sangle36781612015-05-28 16:15:16 +05301379
1380 // if the selected profile is offloaded and no offload info was specified,
1381 // create a default one
1382 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
1383 if ((profile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
1384 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1385 defaultOffloadInfo.sample_rate = samplingRate;
1386 defaultOffloadInfo.channel_mask = channelMask;
1387 defaultOffloadInfo.format = format;
1388 defaultOffloadInfo.stream_type = stream;
1389 defaultOffloadInfo.bit_rate = 0;
1390 defaultOffloadInfo.duration_us = -1;
1391 defaultOffloadInfo.has_video = true; // conservative
1392 defaultOffloadInfo.is_streaming = true; // likely
1393 offloadInfo = &defaultOffloadInfo;
1394 }
1395
1396 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001397 outputDesc->mDevice = device;
1398 outputDesc->mLatency = 0;
Sharad Sangle36781612015-05-28 16:15:16 +05301399 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001400 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1401 config.sample_rate = samplingRate;
1402 config.channel_mask = channelMask;
1403 config.format = format;
1404 if (offloadInfo != NULL) {
1405 config.offload_info = *offloadInfo;
1406 }
Sharad Sangle36781612015-05-28 16:15:16 +05301407 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001408 &output,
1409 &config,
1410 &outputDesc->mDevice,
1411 String8(""),
1412 &outputDesc->mLatency,
1413 outputDesc->mFlags);
1414
1415 // only accept an output with the requested parameters
1416 if (status != NO_ERROR ||
1417 (samplingRate != 0 && samplingRate != config.sample_rate) ||
1418 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
1419 (channelMask != 0 && channelMask != config.channel_mask)) {
1420 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1421 "format %d %d, channelMask %04x %04x", output, samplingRate,
1422 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1423 outputDesc->mChannelMask);
1424 if (output != AUDIO_IO_HANDLE_NONE) {
1425 mpClientInterface->closeOutput(output);
1426 }
Sharad Sangle36781612015-05-28 16:15:16 +05301427 // fall back to mixer output if possible when the direct output could not be open
1428 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
1429 goto non_direct_output;
1430 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001431 return AUDIO_IO_HANDLE_NONE;
1432 }
1433 outputDesc->mSamplingRate = config.sample_rate;
1434 outputDesc->mChannelMask = config.channel_mask;
1435 outputDesc->mFormat = config.format;
1436 outputDesc->mRefCount[stream] = 0;
1437 outputDesc->mStopTime[stream] = 0;
1438 outputDesc->mDirectOpenCount = 1;
1439
1440 audio_io_handle_t srcOutput = getOutputForEffect();
1441 addOutput(output, outputDesc);
1442 audio_io_handle_t dstOutput = getOutputForEffect();
1443 if (dstOutput == output) {
1444 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1445 }
1446 mPreviousOutputs = mOutputs;
1447 ALOGV("getOutput() returns new direct output %d", output);
1448 mpClientInterface->onAudioPortListUpdate();
1449 return output;
1450 }
1451
1452non_direct_output:
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001453 // ignoring channel mask due to downmix capability in mixer
1454
1455 // open a non direct output
1456
1457 // for non direct outputs, only PCM is supported
1458 if (audio_is_linear_pcm(format)) {
1459 // get which output is suitable for the specified stream. The actual
1460 // routing change will happen when startOutput() will be called
1461 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1462
1463 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1464 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1465 output = selectOutput(outputs, flags, format);
1466 }
1467 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1468 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1469
Sharad Sangle36781612015-05-28 16:15:16 +05301470 ALOGV(" getOutputForDevice() returns output %d", output);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001471
1472 return output;
1473}
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301474
1475status_t AudioPolicyManagerCustom::getInputForAttr(const audio_attributes_t *attr,
1476 audio_io_handle_t *input,
1477 audio_session_t session,
1478 uid_t uid,
1479 uint32_t samplingRate,
1480 audio_format_t format,
1481 audio_channel_mask_t channelMask,
1482 audio_input_flags_t flags,
1483 audio_port_handle_t selectedDeviceId,
1484 input_type_t *inputType)
1485{
1486 audio_source_t inputSource = attr->source;
1487#ifdef VOICE_CONCURRENCY
1488
1489 char propValue[PROPERTY_VALUE_MAX];
1490 bool prop_rec_enabled=false, prop_voip_enabled = false;
1491
1492 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
1493 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1494 }
1495
1496 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1497 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1498 }
1499
1500 if (prop_rec_enabled && mvoice_call_state) {
1501 //check if voice call is active / running in background
1502 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1503 //Need to block input request
1504 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1505 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1506 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1507 {
1508 switch(inputSource) {
1509 case AUDIO_SOURCE_VOICE_UPLINK:
1510 case AUDIO_SOURCE_VOICE_DOWNLINK:
1511 case AUDIO_SOURCE_VOICE_CALL:
1512 ALOGD("voice_conc:Creating input during incall mode for inputSource: %d",
1513 inputSource);
1514 break;
1515
1516 case AUDIO_SOURCE_VOICE_COMMUNICATION:
1517 if(prop_voip_enabled) {
1518 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1519 inputSource);
1520 return NO_INIT;
1521 }
1522 break;
1523 default:
1524 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1525 inputSource);
1526 return NO_INIT;
1527 }
1528 }
1529 }//check for VoIP flag
1530 else if(prop_voip_enabled && mvoice_call_state) {
1531 //check if voice call is active / running in background
1532 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1533 //Need to block input request
1534 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1535 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1536 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1537 {
1538 if(inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1539 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
1540 return NO_INIT;
1541 }
1542 }
1543 }
1544
1545#endif
1546
1547 return AudioPolicyManager::getInputForAttr(attr,
1548 input,
1549 session,
1550 uid,
1551 samplingRate,
1552 format,
1553 channelMask,
1554 flags,
1555 selectedDeviceId,
1556 inputType);
1557}
1558status_t AudioPolicyManagerCustom::startInput(audio_io_handle_t input,
1559 audio_session_t session)
1560{
1561 ALOGV("startInput() input %d", input);
1562 ssize_t index = mInputs.indexOfKey(input);
1563 if (index < 0) {
1564 ALOGW("startInput() unknown input %d", input);
1565 return BAD_VALUE;
1566 }
1567 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1568
1569 index = inputDesc->mSessions.indexOf(session);
1570 if (index < 0) {
1571 ALOGW("startInput() unknown session %d on input %d", session, input);
1572 return BAD_VALUE;
1573 }
1574
1575 // virtual input devices are compatible with other input devices
1576 if (!is_virtual_input_device(inputDesc->mDevice)) {
1577
1578 // for a non-virtual input device, check if there is another (non-virtual) active input
1579 audio_io_handle_t activeInput = mInputs.getActiveInput();
1580 if (activeInput != 0 && activeInput != input) {
1581
1582 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1583 // otherwise the active input continues and the new input cannot be started.
1584 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
1585 if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
1586 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
1587 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
1588 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
1589 } else {
1590 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
1591 return INVALID_OPERATION;
1592 }
1593 }
1594 }
1595
1596 // Routing?
1597 mInputRoutes.incRouteActivity(session);
1598#ifdef RECORD_PLAY_CONCURRENCY
1599 mIsInputRequestOnProgress = true;
1600
1601 char getPropValue[PROPERTY_VALUE_MAX];
1602 bool prop_rec_play_enabled = false;
1603
1604 if (property_get("rec.playback.conc.disabled", getPropValue, NULL)) {
1605 prop_rec_play_enabled = atoi(getPropValue) || !strncmp("true", getPropValue, 4);
1606 }
1607
1608 if ((prop_rec_play_enabled) &&(mInputs.activeInputsCount() == 0)){
1609 // send update to HAL on record playback concurrency
1610 AudioParameter param = AudioParameter();
1611 param.add(String8("rec_play_conc_on"), String8("true"));
1612 ALOGD("startInput() setParameters rec_play_conc is setting to ON ");
1613 mpClientInterface->setParameters(0, param.toString());
1614
1615 // Call invalidate to reset all opened non ULL audio tracks
1616 // Move tracks associated to this strategy from previous output to new output
1617 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
1618 // Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder)
1619 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE && (i != AUDIO_STREAM_PATCH)) {
1620 ALOGD("Invalidate on releaseInput for stream :: %d ", i);
1621 //FIXME see fixme on name change
1622 mpClientInterface->invalidateStream((audio_stream_type_t)i);
1623 }
1624 }
1625 // close compress tracks
1626 for (size_t i = 0; i < mOutputs.size(); i++) {
1627 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
1628 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
1629 ALOGD("ouput desc / profile is NULL");
1630 continue;
1631 }
1632 if (outputDesc->mProfile->mFlags
1633 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1634 // close compress sessions
1635 ALOGD("calling closeOutput on record conc for COMPRESS output");
1636 closeOutput(mOutputs.keyAt(i));
1637 }
1638 }
1639 }
1640#endif
1641
1642 if (inputDesc->mRefCount == 0 || mInputRoutes.hasRouteChanged(session)) {
1643 // if input maps to a dynamic policy with an activity listener, notify of state change
1644 if ((inputDesc->mPolicyMix != NULL)
1645 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1646 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1647 MIX_STATE_MIXING);
1648 }
1649
1650 if (mInputs.activeInputsCount() == 0) {
1651 SoundTrigger::setCaptureState(true);
1652 }
1653 setInputDevice(input, getNewInputDevice(input), true /* force */);
1654
1655 // automatically enable the remote submix output when input is started if not
1656 // used by a policy mix of type MIX_TYPE_RECORDERS
1657 // For remote submix (a virtual device), we open only one input per capture request.
1658 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1659 String8 address = String8("");
1660 if (inputDesc->mPolicyMix == NULL) {
1661 address = String8("0");
1662 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1663 address = inputDesc->mPolicyMix->mRegistrationId;
1664 }
1665 if (address != "") {
1666 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1667 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1668 address, "remote-submix");
1669 }
1670 }
1671 }
1672
1673 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1674
1675 inputDesc->mRefCount++;
1676#ifdef RECORD_PLAY_CONCURRENCY
1677 mIsInputRequestOnProgress = false;
1678#endif
1679 return NO_ERROR;
1680}
1681status_t AudioPolicyManagerCustom::stopInput(audio_io_handle_t input,
1682 audio_session_t session)
1683{
1684 status_t status;
1685 status = AudioPolicyManager::stopInput(input, session);
1686#ifdef RECORD_PLAY_CONCURRENCY
1687 char propValue[PROPERTY_VALUE_MAX];
1688 bool prop_rec_play_enabled = false;
1689
1690 if (property_get("rec.playback.conc.disabled", propValue, NULL)) {
1691 prop_rec_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1692 }
1693
1694 if ((prop_rec_play_enabled) && (mInputs.activeInputsCount() == 0)) {
1695
1696 //send update to HAL on record playback concurrency
1697 AudioParameter param = AudioParameter();
1698 param.add(String8("rec_play_conc_on"), String8("false"));
1699 ALOGD("stopInput() setParameters rec_play_conc is setting to OFF ");
1700 mpClientInterface->setParameters(0, param.toString());
1701
1702 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
1703 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
1704 //Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder stop tone)
1705 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE) && (i != AUDIO_STREAM_PATCH)) {
1706 ALOGD(" Invalidate on stopInput for stream :: %d ", i);
1707 //FIXME see fixme on name change
1708 mpClientInterface->invalidateStream((audio_stream_type_t)i);
1709 }
1710 }
1711 }
1712#endif
1713 return status;
1714}
1715
1716AudioPolicyManagerCustom::AudioPolicyManagerCustom(AudioPolicyClientInterface *clientInterface)
1717 : AudioPolicyManager(clientInterface)
1718{
1719#ifdef RECORD_PLAY_CONCURRENCY
1720 mIsInputRequestOnProgress = false;
1721#endif
1722
1723
1724#ifdef VOICE_CONCURRENCY
1725 mFallBackflag = getFallBackPath();
1726#endif
1727}
Sharad Sanglec60f6fa2015-07-27 15:14:23 +05301728audio_devices_t AudioPolicyManagerCustom::getDeviceForStrategy(routing_strategy strategy, bool fromCache)
1729{
1730 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
1731 audio_devices_t device = AUDIO_DEVICE_NONE;
1732 switch (strategy) {
1733 case STRATEGY_SONIFICATION:
1734 case STRATEGY_ENFORCED_AUDIBLE:
1735 case STRATEGY_ACCESSIBILITY:
1736 case STRATEGY_REROUTING:
1737 case STRATEGY_MEDIA:
1738 if (strategy != STRATEGY_SONIFICATION){
1739 // no sonification on WFD sink
1740 device |= availableOutputDeviceTypes & AUDIO_DEVICE_OUT_PROXY;
1741 if (device != AUDIO_DEVICE_NONE) {
1742 ALOGV("Found proxy for strategy %d", strategy);
1743 return device;
1744 }
1745 }
1746 break;
1747 default:
1748 ALOGV("getDeviceForStrategy() unknown strategy: %d", strategy);
1749 break;
1750 }
1751 device = AudioPolicyManager::getDeviceForStrategy(strategy, fromCache);
1752 return device;
1753}
1754
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001755}