blob: dbf1b5ac8066939fca2ade5eca21a28982cefa09 [file] [log] [blame]
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001/*
Sharad Sangle36781612015-05-28 16:15:16 +05302 * Copyright (c) 2013-2015, The Linux Foundation. All rights reserved.
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07003 * Not a contribution.
4 *
5 * Copyright (C) 2009 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
Sharad Sangle36781612015-05-28 16:15:16 +053020#define LOG_TAG "AudioPolicyManagerCustom"
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070021//#define LOG_NDEBUG 0
22
23//#define VERY_VERBOSE_LOGGING
24#ifdef VERY_VERBOSE_LOGGING
25#define ALOGVV ALOGV
26#else
27#define ALOGVV(a...) do { } while(0)
28#endif
Sharad Sangle36781612015-05-28 16:15:16 +053029#define MIN(a, b) ((a) < (b) ? (a) : (b))
30
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070031// A device mask for all audio output devices that are considered "remote" when evaluating
32// active output devices in isStreamActiveRemotely()
33#define APM_AUDIO_OUT_DEVICE_REMOTE_ALL AUDIO_DEVICE_OUT_REMOTE_SUBMIX
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070034// A device mask for all audio input and output devices where matching inputs/outputs on device
35// type alone is not enough: the address must match too
36#define APM_AUDIO_DEVICE_MATCH_ADDRESS_ALL (AUDIO_DEVICE_IN_REMOTE_SUBMIX | \
37 AUDIO_DEVICE_OUT_REMOTE_SUBMIX)
Sharad Sangle36781612015-05-28 16:15:16 +053038// Following delay should be used if the calculated routing delay from all active
39// input streams is higher than this value
40#define MAX_VOICE_CALL_START_DELAY_MS 100
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070041
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070042#include <inttypes.h>
Mingming Yin0ae14ea2014-07-09 17:55:56 -070043#include <math.h>
Mingming Yin0670f162014-06-12 16:05:49 -070044
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070045#include <cutils/properties.h>
46#include <utils/Log.h>
47#include <hardware/audio.h>
48#include <hardware/audio_effect.h>
49#include <media/AudioParameter.h>
50#include <soundtrigger/SoundTrigger.h>
51#include "AudioPolicyManager.h"
Sharad Sangle36781612015-05-28 16:15:16 +053052#include <policy.h>
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -070053
54namespace android {
Sharad Sanglec5766ff2015-06-04 20:24:10 +053055#ifdef VOICE_CONCURRENCY
56audio_output_flags_t AudioPolicyManagerCustom::getFallBackPath()
57{
58 audio_output_flags_t flag = AUDIO_OUTPUT_FLAG_FAST;
59 char propValue[PROPERTY_VALUE_MAX];
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070060
Sharad Sanglec5766ff2015-06-04 20:24:10 +053061 if (property_get("voice.conc.fallbackpath", propValue, NULL)) {
62 if (!strncmp(propValue, "deep-buffer", 11)) {
63 flag = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
64 }
65 else if (!strncmp(propValue, "fast", 4)) {
66 flag = AUDIO_OUTPUT_FLAG_FAST;
67 }
68 else {
69 ALOGD("voice_conc:not a recognised path(%s) in prop voice.conc.fallbackpath",
70 propValue);
71 }
72 }
73 else {
74 ALOGD("voice_conc:prop voice.conc.fallbackpath not set");
75 }
76
77 ALOGD("voice_conc:picked up flag(0x%x) from prop voice.conc.fallbackpath",
78 flag);
79
80 return flag;
81}
82#endif /*VOICE_CONCURRENCY*/
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070083// ----------------------------------------------------------------------------
84// AudioPolicyInterface implementation
85// ----------------------------------------------------------------------------
Sharad Sangle36781612015-05-28 16:15:16 +053086extern "C" AudioPolicyInterface* createAudioPolicyManager(
87 AudioPolicyClientInterface *clientInterface)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070088{
Sharad Sangle36781612015-05-28 16:15:16 +053089 return new AudioPolicyManagerCustom(clientInterface);
90}
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070091
Sharad Sangle36781612015-05-28 16:15:16 +053092extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface)
93{
94 delete interface;
95}
96
97status_t AudioPolicyManagerCustom::setDeviceConnectionStateInt(audio_devices_t device,
98 audio_policy_dev_state_t state,
99 const char *device_address,
100 const char *device_name)
101{
102 ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
103 device, state, device_address, device_name);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700104
105 // connect/disconnect only 1 device at a time
106 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
107
Sharad Sangle36781612015-05-28 16:15:16 +0530108 sp<DeviceDescriptor> devDesc =
109 mHwModules.getDeviceDescriptor(device, device_address, device_name);
110
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700111 // handle output devices
112 if (audio_is_output_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700113 SortedVector <audio_io_handle_t> outputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700114
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700115 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700116
117 // save a copy of the opened output descriptors before any output is opened or closed
118 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
119 mPreviousOutputs = mOutputs;
120 switch (state)
121 {
122 // handle output device connection
Sharad Sangle36781612015-05-28 16:15:16 +0530123 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700124 if (index >= 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700125 ALOGW("setDeviceConnectionState() device already connected: %x", device);
126 return INVALID_OPERATION;
127 }
128 ALOGV("setDeviceConnectionState() connecting device %x", device);
129
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700130 // register new device as available
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700131 index = mAvailableOutputDevices.add(devDesc);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700132 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530133 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700134 if (module == 0) {
135 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
136 device);
137 mAvailableOutputDevices.remove(devDesc);
138 return INVALID_OPERATION;
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700139 }
Sharad Sangle36781612015-05-28 16:15:16 +0530140 mAvailableOutputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700141 } else {
142 return NO_MEMORY;
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700143 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700144
Sharad Sangle36781612015-05-28 16:15:16 +0530145 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700146 mAvailableOutputDevices.remove(devDesc);
147 return INVALID_OPERATION;
148 }
Sharad Sangle36781612015-05-28 16:15:16 +0530149 // Propagate device availability to Engine
150 mEngine->setDeviceConnectionState(devDesc, state);
151
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700152 // outputs should never be empty here
153 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
154 "checkOutputsForDevice() returned no outputs but status OK");
155 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
156 outputs.size());
Sharad Sangle36781612015-05-28 16:15:16 +0530157
158 // Send connect to HALs
159 AudioParameter param = AudioParameter(devDesc->mAddress);
160 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
161 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
162
163 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700164 // handle output device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700165 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
166 if (index < 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700167 ALOGW("setDeviceConnectionState() device not connected: %x", device);
168 return INVALID_OPERATION;
169 }
170
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700171 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
172
Sharad Sangle36781612015-05-28 16:15:16 +0530173 // Send Disconnect to HALs
174 AudioParameter param = AudioParameter(devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700175 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
176 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
177
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700178 // remove device from available output devices
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700179 mAvailableOutputDevices.remove(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700180
Sharad Sangle36781612015-05-28 16:15:16 +0530181 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
182
183 // Propagate device availability to Engine
184 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700185 } break;
186
187 default:
188 ALOGE("setDeviceConnectionState() invalid state: %x", state);
189 return BAD_VALUE;
190 }
191
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700192 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
193 // output is suspended before any tracks are moved to it
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700194 checkA2dpSuspend();
195 checkOutputForAllStrategies();
196 // outputs must be closed after checkOutputForAllStrategies() is executed
197 if (!outputs.isEmpty()) {
198 for (size_t i = 0; i < outputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530199 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700200 // close unused outputs after device disconnection or direct outputs that have been
201 // opened by checkOutputsForDevice() to query dynamic parameters
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700202 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700203 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
204 (desc->mDirectOpenCount == 0))) {
205 closeOutput(outputs[i]);
206 }
207 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700208 // check again after closing A2DP output to reset mA2dpSuspended if needed
209 checkA2dpSuspend();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700210 }
211
212 updateDevicesAndOutputs();
Sharad Sangle36781612015-05-28 16:15:16 +0530213 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
214 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700215 updateCallRouting(newDevice);
216 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700217 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530218 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
219 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
220 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700221 // do not force device change on duplicated output because if device is 0, it will
222 // also force a device 0 for the two outputs it is duplicated to which may override
223 // a valid device selection on those outputs.
Sharad Sangle36781612015-05-28 16:15:16 +0530224 bool force = !desc->isDuplicated()
225 && (!device_distinguishes_on_address(device)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700226 // always force when disconnecting (a non-duplicated device)
227 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Sharad Sangle36781612015-05-28 16:15:16 +0530228 setOutputDevice(desc, newDevice, force, 0);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700229 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700230 }
231
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700232 mpClientInterface->onAudioPortListUpdate();
233 return NO_ERROR;
234 } // end if is output device
235
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700236 // handle input devices
237 if (audio_is_input_device(device)) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700238 SortedVector <audio_io_handle_t> inputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700239
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700240 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700241 switch (state)
242 {
243 // handle input device connection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700244 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
245 if (index >= 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700246 ALOGW("setDeviceConnectionState() device already connected: %d", device);
247 return INVALID_OPERATION;
248 }
Sharad Sangle36781612015-05-28 16:15:16 +0530249 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700250 if (module == NULL) {
251 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
252 device);
253 return INVALID_OPERATION;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700254 }
Sharad Sangle36781612015-05-28 16:15:16 +0530255 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700256 return INVALID_OPERATION;
257 }
258
259 index = mAvailableInputDevices.add(devDesc);
260 if (index >= 0) {
Sharad Sangle36781612015-05-28 16:15:16 +0530261 mAvailableInputDevices[index]->attach(module);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700262 } else {
263 return NO_MEMORY;
264 }
Sharad Sangle36781612015-05-28 16:15:16 +0530265
266 // Set connect to HALs
267 AudioParameter param = AudioParameter(devDesc->mAddress);
268 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
269 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
270
271 // Propagate device availability to Engine
272 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700273 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700274
275 // handle input device disconnection
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700276 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
277 if (index < 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700278 ALOGW("setDeviceConnectionState() device not connected: %d", device);
279 return INVALID_OPERATION;
280 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700281
282 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
283
284 // Set Disconnect to HALs
Sharad Sangle36781612015-05-28 16:15:16 +0530285 AudioParameter param = AudioParameter(devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700286 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
287 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
288
Sharad Sangle36781612015-05-28 16:15:16 +0530289 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700290 mAvailableInputDevices.remove(devDesc);
291
Sharad Sangle36781612015-05-28 16:15:16 +0530292 // Propagate device availability to Engine
293 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700294 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700295
296 default:
297 ALOGE("setDeviceConnectionState() invalid state: %x", state);
298 return BAD_VALUE;
299 }
300
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700301 closeAllInputs();
302
Sharad Sangle36781612015-05-28 16:15:16 +0530303 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700304 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
305 updateCallRouting(newDevice);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700306 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700307
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700308 mpClientInterface->onAudioPortListUpdate();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700309 return NO_ERROR;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700310 } // end if is input device
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700311
312 ALOGW("setDeviceConnectionState() invalid device: %x", device);
313 return BAD_VALUE;
314}
Sharad Sangle36781612015-05-28 16:15:16 +0530315// This function checks for the parameters which can be offloaded.
316// This can be enhanced depending on the capability of the DSP and policy
317// of the system.
318bool AudioPolicyManagerCustom::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700319{
Sharad Sangle36781612015-05-28 16:15:16 +0530320 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
321 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
322 offloadInfo.sample_rate, offloadInfo.channel_mask,
323 offloadInfo.format,
324 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
325 offloadInfo.has_video);
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530326#ifdef VOICE_CONCURRENCY
327 char concpropValue[PROPERTY_VALUE_MAX];
328 if (property_get("voice.playback.conc.disabled", concpropValue, NULL)) {
329 bool propenabled = atoi(concpropValue) || !strncmp("true", concpropValue, 4);
330 if (propenabled) {
331 if (isInCall())
332 {
333 ALOGD("\n copl: blocking compress offload on call mode\n");
334 return false;
335 }
336 }
337 }
338#endif
339#ifdef RECORD_PLAY_CONCURRENCY
340 char recConcPropValue[PROPERTY_VALUE_MAX];
341 bool prop_rec_play_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700342
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530343 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
344 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
345 }
346
347 if ((prop_rec_play_enabled) &&
348 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
349 ALOGD("copl: blocking compress offload for record concurrency");
350 return false;
351 }
352#endif
Sharad Sangle36781612015-05-28 16:15:16 +0530353 // Check if stream type is music, then only allow offload as of now.
354 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
355 {
356 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
357 return false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700358 }
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530359
360 char propValue[PROPERTY_VALUE_MAX];
361 bool pcmOffload = false;
362#ifdef PCM_OFFLOAD_ENABLED
363 if ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM_OFFLOAD) {
364 bool prop_enabled = false;
365 if ((AUDIO_FORMAT_PCM_16_BIT_OFFLOAD == offloadInfo.format) &&
366 property_get("audio.offload.pcm.16bit.enable", propValue, NULL)) {
367 prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
368 }
369
370#ifdef PCM_OFFLOAD_ENABLED_24
371 if ((AUDIO_FORMAT_PCM_24_BIT_OFFLOAD == offloadInfo.format) &&
372 property_get("audio.offload.pcm.24bit.enable", propValue, NULL)) {
373 prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530374 }
375#endif
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530376
377 if (prop_enabled) {
378 ALOGI("PCM offload property is enabled");
379 pcmOffload = true;
380 }
381
382 if (!pcmOffload) {
383 ALOGD("system property not enabled for PCM offload format[%x]",offloadInfo.format);
384 return false;
385 }
386 }
387#endif
388 if (!pcmOffload) {
389 // Check if offload has been disabled
390 if (property_get("audio.offload.disable", propValue, "0")) {
391 if (atoi(propValue) != 0) {
392 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
393 return false;
394 }
395 }
396 //check if it's multi-channel AAC (includes sub formats) and FLAC format
397 if ((popcount(offloadInfo.channel_mask) > 2) &&
398 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
399 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS))) {
400 ALOGD("offload disabled for multi-channel AAC,FLAC and VORBIS format");
401 return false;
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530402 }
403
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530404#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) ||
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530436 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS) ||
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530437#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) ||
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530442 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_APE) ||
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530443#endif
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530444 pcmOffload)
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}
vivek mehta0ea887a2015-08-26 14:01:20 -07001128
1129status_t AudioPolicyManagerCustom::getOutputForAttr(const audio_attributes_t *attr,
1130 audio_io_handle_t *output,
1131 audio_session_t session,
1132 audio_stream_type_t *stream,
1133 uid_t uid,
1134 uint32_t samplingRate,
1135 audio_format_t format,
1136 audio_channel_mask_t channelMask,
1137 audio_output_flags_t flags,
1138 audio_port_handle_t selectedDeviceId,
1139 const audio_offload_info_t *offloadInfo)
1140{
1141 audio_offload_info_t tOffloadInfo = AUDIO_INFO_INITIALIZER;
1142
1143 bool pcmOffloadEnabled = property_get_bool("audio.offload.track.enable", false);
1144
1145 if (offloadInfo == NULL && pcmOffloadEnabled) {
1146 tOffloadInfo.sample_rate = samplingRate;
1147 tOffloadInfo.channel_mask = channelMask;
1148 tOffloadInfo.format = format;
1149 tOffloadInfo.stream_type = *stream;
1150 tOffloadInfo.bit_width = 16; //hard coded for PCM_16
1151 if (attr != NULL) {
1152 ALOGV("found attribute .. setting usage %d ", attr->usage);
1153 tOffloadInfo.usage = attr->usage;
1154 } else {
1155 ALOGD("%s:: attribute is NULL .. no usage set", __func__);
1156 }
1157 offloadInfo = &tOffloadInfo;
1158 }
1159
1160 return AudioPolicyManager::getOutputForAttr(attr, output, session, stream,
1161 (uid_t)uid, (uint32_t)samplingRate,
1162 format, (audio_channel_mask_t)channelMask,
1163 flags, (audio_port_handle_t)selectedDeviceId,
1164 offloadInfo);
1165}
1166
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001167audio_io_handle_t AudioPolicyManagerCustom::getOutputForDevice(
1168 audio_devices_t device,
Sharad Sangle36781612015-05-28 16:15:16 +05301169 audio_session_t session __unused,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001170 audio_stream_type_t stream,
1171 uint32_t samplingRate,
1172 audio_format_t format,
1173 audio_channel_mask_t channelMask,
1174 audio_output_flags_t flags,
1175 const audio_offload_info_t *offloadInfo)
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001176{
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001177 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
1178 uint32_t latency = 0;
1179 status_t status;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001180
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001181#ifdef AUDIO_POLICY_TEST
1182 if (mCurOutput != 0) {
1183 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1184 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1185
1186 if (mTestOutputs[mCurOutput] == 0) {
1187 ALOGV("getOutput() opening test output");
Sharad Sangle36781612015-05-28 16:15:16 +05301188 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
1189 mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001190 outputDesc->mDevice = mTestDevice;
1191 outputDesc->mLatency = mTestLatencyMs;
1192 outputDesc->mFlags =
1193 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
1194 outputDesc->mRefCount[stream] = 0;
1195 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1196 config.sample_rate = mTestSamplingRate;
1197 config.channel_mask = mTestChannels;
1198 config.format = mTestFormat;
1199 if (offloadInfo != NULL) {
1200 config.offload_info = *offloadInfo;
1201 }
1202 status = mpClientInterface->openOutput(0,
1203 &mTestOutputs[mCurOutput],
1204 &config,
1205 &outputDesc->mDevice,
1206 String8(""),
1207 &outputDesc->mLatency,
1208 outputDesc->mFlags);
1209 if (status == NO_ERROR) {
1210 outputDesc->mSamplingRate = config.sample_rate;
1211 outputDesc->mFormat = config.format;
1212 outputDesc->mChannelMask = config.channel_mask;
1213 AudioParameter outputCmd = AudioParameter();
1214 outputCmd.addInt(String8("set_id"),mCurOutput);
1215 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1216 addOutput(mTestOutputs[mCurOutput], outputDesc);
1217 }
1218 }
1219 return mTestOutputs[mCurOutput];
1220 }
1221#endif //AUDIO_POLICY_TEST
Sharad Sangle36781612015-05-28 16:15:16 +05301222 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1223 (stream != AUDIO_STREAM_MUSIC)) {
1224 // compress should not be used for non-music streams
1225 ALOGE("Offloading only allowed with music stream");
1226 return 0;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301227 }
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301228
1229 if ((stream == AUDIO_STREAM_VOICE_CALL) &&
1230 (channelMask == 1) &&
1231 (samplingRate == 8000 || samplingRate == 16000)) {
1232 // Allow Voip direct output only if:
1233 // audio mode is MODE_IN_COMMUNCATION; AND
1234 // voip output is not opened already; AND
1235 // requested sample rate matches with that of voip input stream (if opened already)
1236 int value = 0;
1237 uint32_t mode = 0, voipOutCount = 1, voipSampleRate = 1;
1238 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1239 String8("audio_mode"));
1240 AudioParameter result = AudioParameter(valueStr);
1241 if (result.getInt(String8("audio_mode"), value) == NO_ERROR) {
1242 mode = value;
1243 }
1244
1245 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1246 String8("voip_out_stream_count"));
1247 result = AudioParameter(valueStr);
1248 if (result.getInt(String8("voip_out_stream_count"), value) == NO_ERROR) {
1249 voipOutCount = value;
1250 }
1251
1252 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1253 String8("voip_sample_rate"));
1254 result = AudioParameter(valueStr);
1255 if (result.getInt(String8("voip_sample_rate"), value) == NO_ERROR) {
1256 voipSampleRate = value;
1257 }
1258
1259 if ((mode == AUDIO_MODE_IN_COMMUNICATION) && (voipOutCount == 0) &&
1260 ((voipSampleRate == 0) || (voipSampleRate == samplingRate))) {
1261 if (audio_is_linear_pcm(format)) {
1262 char propValue[PROPERTY_VALUE_MAX] = {0};
1263 property_get("use.voice.path.for.pcm.voip", propValue, "0");
1264 bool voipPcmSysPropEnabled = !strncmp("true", propValue, sizeof("true"));
1265 if (voipPcmSysPropEnabled && (format == AUDIO_FORMAT_PCM_16_BIT)) {
1266 flags = (audio_output_flags_t)((flags &~AUDIO_OUTPUT_FLAG_FAST) |
1267 AUDIO_OUTPUT_FLAG_VOIP_RX | AUDIO_OUTPUT_FLAG_DIRECT);
1268 ALOGD("Set VoIP and Direct output flags for PCM format");
1269 }
1270 }
1271 }
1272 }
1273
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301274#ifdef VOICE_CONCURRENCY
1275 char propValue[PROPERTY_VALUE_MAX];
1276 bool prop_play_enabled=false, prop_voip_enabled = false;
1277
1278 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1279 prop_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001280 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301281
1282 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1283 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1284 }
1285
1286 if (prop_play_enabled && mvoice_call_state) {
1287 //check if voice call is active / running in background
1288 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1289 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1290 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1291 {
1292 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1293 if(prop_voip_enabled) {
1294 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1295 flags );
1296 return 0;
1297 }
1298 }
1299 else {
1300 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
1301 ALOGD("voice_conc:IN call mode adding ULL flags .. flags: %x ", flags );
1302 flags = AUDIO_OUTPUT_FLAG_FAST;
1303 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
1304 if (AUDIO_STREAM_MUSIC == stream) {
1305 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1306 ALOGD("voice_conc:IN call mode adding deep-buffer flags %x ", flags );
1307 }
1308 else {
1309 flags = AUDIO_OUTPUT_FLAG_FAST;
1310 ALOGD("voice_conc:IN call mode adding fast flags %x ", flags );
1311 }
1312 }
1313 }
1314 }
1315 } else if (prop_voip_enabled && mvoice_call_state) {
1316 //check if voice call is active / running in background
1317 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1318 //return only ULL ouput
1319 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1320 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1321 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1322 {
1323 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1324 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1325 flags );
1326 return 0;
1327 }
1328 }
1329 }
1330#endif
1331#ifdef RECORD_PLAY_CONCURRENCY
1332 char recConcPropValue[PROPERTY_VALUE_MAX];
1333 bool prop_rec_play_enabled = false;
1334
1335 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
1336 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
1337 }
1338 if ((prop_rec_play_enabled) &&
1339 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
1340 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
1341 if (AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1342 // allow VoIP using voice path
1343 // Do nothing
1344 } else if((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1345 ALOGD("voice_conc:MODE_IN_COMM is setforcing deep buffer output for non ULL... flags: %x", flags);
1346 // use deep buffer path for all non ULL outputs
1347 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1348 }
1349 } else if ((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1350 ALOGD("voice_conc:Record mode is on forcing deep buffer output for non ULL... flags: %x ", flags);
1351 // use deep buffer path for all non ULL outputs
1352 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1353 }
1354 }
1355 if (prop_rec_play_enabled &&
1356 (stream == AUDIO_STREAM_ENFORCED_AUDIBLE)) {
1357 ALOGD("Record conc is on forcing ULL output for ENFORCED_AUDIBLE");
1358 flags = AUDIO_OUTPUT_FLAG_FAST;
1359 }
1360#endif
1361
Sharad Sangle36781612015-05-28 16:15:16 +05301362 /*
1363 * WFD audio routes back to target speaker when starting a ringtone playback.
1364 * This is because primary output is reused for ringtone, so output device is
1365 * updated based on SONIFICATION strategy for both ringtone and music playback.
1366 * The same issue is not seen on remoted_submix HAL based WFD audio because
1367 * primary output is not reused and a new output is created for ringtone playback.
1368 * Issue is fixed by updating output flag to AUDIO_OUTPUT_FLAG_FAST when there is
1369 * a non-music stream playback on WFD, so primary output is not reused for ringtone.
1370 */
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001371 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
1372 if ((availableOutputDeviceTypes & AUDIO_DEVICE_OUT_PROXY)
1373 && (stream != AUDIO_STREAM_MUSIC)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301374 ALOGD("WFD audio: use OUTPUT_FLAG_FAST for non music stream. flags:%x", flags );
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001375 //For voip paths
1376 if(flags & AUDIO_OUTPUT_FLAG_DIRECT)
1377 flags = AUDIO_OUTPUT_FLAG_DIRECT;
1378 else //route every thing else to ULL path
1379 flags = AUDIO_OUTPUT_FLAG_FAST;
1380 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001381 // open a direct output if required by specified parameters
vivek mehta0ea887a2015-08-26 14:01:20 -07001382 // force direct flag if offload flag is set: offloading implies a direct output stream
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001383 // and all common behaviors are driven by checking only the direct flag
1384 // this should normally be set appropriately in the policy configuration file
1385 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1386 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1387 }
1388 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1389 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1390 }
vivek mehta0ea887a2015-08-26 14:01:20 -07001391
1392 // Do offload magic here
1393 if ((flags == AUDIO_OUTPUT_FLAG_NONE) && (stream == AUDIO_STREAM_MUSIC) &&
1394 (offloadInfo != NULL) &&
1395 ((offloadInfo->usage == AUDIO_USAGE_MEDIA ||
1396 (offloadInfo->usage == AUDIO_USAGE_GAME)))) {
1397 if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) {
1398 ALOGD("AudioCustomHAL --> Force Direct Flag ..");
1399 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1400 }
1401 }
1402
Sharad Sangle36781612015-05-28 16:15:16 +05301403 // only allow deep buffering for music stream type
1404 if (stream != AUDIO_STREAM_MUSIC) {
1405 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Sharad Sangle497aef82015-08-03 17:55:48 +05301406 } else if (/* stream == AUDIO_STREAM_MUSIC && */
1407 flags == AUDIO_OUTPUT_FLAG_NONE &&
1408 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
1409 flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Sharad Sangle36781612015-05-28 16:15:16 +05301410 }
Sharad Sangle497aef82015-08-03 17:55:48 +05301411
Sharad Sangle36781612015-05-28 16:15:16 +05301412 if (stream == AUDIO_STREAM_TTS) {
1413 flags = AUDIO_OUTPUT_FLAG_TTS;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001414 }
1415
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301416 // open a direct output if required by specified parameters
1417 //force direct flag if offload flag is set: offloading implies a direct output stream
1418 // and all common behaviors are driven by checking only the direct flag
1419 // this should normally be set appropriately in the policy configuration file
1420 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1421 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1422 }
1423 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1424 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1425 }
1426 // only allow deep buffering for music stream type
1427 if (stream != AUDIO_STREAM_MUSIC) {
1428 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
1429 }
1430 if (stream == AUDIO_STREAM_TTS) {
1431 flags = AUDIO_OUTPUT_FLAG_TTS;
1432 }
1433
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001434 sp<IOProfile> profile;
1435
1436 // skip direct output selection if the request can obviously be attached to a mixed output
1437 // and not explicitly requested
1438 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
1439 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
1440 audio_channel_count_from_out_mask(channelMask) <= 2) {
1441 goto non_direct_output;
1442 }
1443
1444 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1445 // creating an offloaded track and tearing it down immediately after start when audioflinger
1446 // detects there is an active non offloadable effect.
1447 // FIXME: We should check the audio session here but we do not have it in this context.
1448 // This may prevent offloading in rare situations where effects are left active by apps
1449 // in the background.
1450
Sharad Sangle36781612015-05-28 16:15:16 +05301451 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
1452 !mEffects.isNonOffloadableEffectEnabled()) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001453 profile = getProfileForDirectOutput(device,
1454 samplingRate,
1455 format,
1456 channelMask,
1457 (audio_output_flags_t)flags);
1458 }
1459
1460 if (profile != 0) {
Sharad Sangle36781612015-05-28 16:15:16 +05301461 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001462
1463 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +05301464 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001465 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1466 outputDesc = desc;
1467 // reuse direct output if currently open and configured with same parameters
1468 if ((samplingRate == outputDesc->mSamplingRate) &&
1469 (format == outputDesc->mFormat) &&
1470 (channelMask == outputDesc->mChannelMask)) {
1471 outputDesc->mDirectOpenCount++;
1472 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1473 return mOutputs.keyAt(i);
1474 }
1475 }
1476 }
1477 // close direct output if currently open and configured with different parameters
1478 if (outputDesc != NULL) {
1479 closeOutput(outputDesc->mIoHandle);
1480 }
Sharad Sangle36781612015-05-28 16:15:16 +05301481
1482 // if the selected profile is offloaded and no offload info was specified,
1483 // create a default one
1484 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
1485 if ((profile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
1486 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1487 defaultOffloadInfo.sample_rate = samplingRate;
1488 defaultOffloadInfo.channel_mask = channelMask;
1489 defaultOffloadInfo.format = format;
1490 defaultOffloadInfo.stream_type = stream;
1491 defaultOffloadInfo.bit_rate = 0;
1492 defaultOffloadInfo.duration_us = -1;
1493 defaultOffloadInfo.has_video = true; // conservative
1494 defaultOffloadInfo.is_streaming = true; // likely
1495 offloadInfo = &defaultOffloadInfo;
1496 }
1497
1498 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001499 outputDesc->mDevice = device;
1500 outputDesc->mLatency = 0;
Sharad Sangle36781612015-05-28 16:15:16 +05301501 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001502 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1503 config.sample_rate = samplingRate;
1504 config.channel_mask = channelMask;
1505 config.format = format;
1506 if (offloadInfo != NULL) {
1507 config.offload_info = *offloadInfo;
1508 }
Sharad Sangle36781612015-05-28 16:15:16 +05301509 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001510 &output,
1511 &config,
1512 &outputDesc->mDevice,
1513 String8(""),
1514 &outputDesc->mLatency,
1515 outputDesc->mFlags);
1516
1517 // only accept an output with the requested parameters
1518 if (status != NO_ERROR ||
1519 (samplingRate != 0 && samplingRate != config.sample_rate) ||
1520 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
1521 (channelMask != 0 && channelMask != config.channel_mask)) {
1522 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1523 "format %d %d, channelMask %04x %04x", output, samplingRate,
1524 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1525 outputDesc->mChannelMask);
1526 if (output != AUDIO_IO_HANDLE_NONE) {
1527 mpClientInterface->closeOutput(output);
1528 }
Sharad Sangle36781612015-05-28 16:15:16 +05301529 // fall back to mixer output if possible when the direct output could not be open
1530 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
1531 goto non_direct_output;
1532 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001533 return AUDIO_IO_HANDLE_NONE;
1534 }
1535 outputDesc->mSamplingRate = config.sample_rate;
1536 outputDesc->mChannelMask = config.channel_mask;
1537 outputDesc->mFormat = config.format;
1538 outputDesc->mRefCount[stream] = 0;
1539 outputDesc->mStopTime[stream] = 0;
1540 outputDesc->mDirectOpenCount = 1;
1541
1542 audio_io_handle_t srcOutput = getOutputForEffect();
1543 addOutput(output, outputDesc);
1544 audio_io_handle_t dstOutput = getOutputForEffect();
1545 if (dstOutput == output) {
1546 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1547 }
1548 mPreviousOutputs = mOutputs;
1549 ALOGV("getOutput() returns new direct output %d", output);
1550 mpClientInterface->onAudioPortListUpdate();
1551 return output;
1552 }
1553
1554non_direct_output:
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001555 // ignoring channel mask due to downmix capability in mixer
1556
1557 // open a non direct output
1558
1559 // for non direct outputs, only PCM is supported
1560 if (audio_is_linear_pcm(format)) {
1561 // get which output is suitable for the specified stream. The actual
1562 // routing change will happen when startOutput() will be called
1563 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1564
1565 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1566 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1567 output = selectOutput(outputs, flags, format);
1568 }
1569 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1570 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1571
vivek mehta0ea887a2015-08-26 14:01:20 -07001572 ALOGV("getOutputForDevice() returns output %d", output);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001573
1574 return output;
1575}
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301576
1577status_t AudioPolicyManagerCustom::getInputForAttr(const audio_attributes_t *attr,
1578 audio_io_handle_t *input,
1579 audio_session_t session,
1580 uid_t uid,
1581 uint32_t samplingRate,
1582 audio_format_t format,
1583 audio_channel_mask_t channelMask,
1584 audio_input_flags_t flags,
1585 audio_port_handle_t selectedDeviceId,
1586 input_type_t *inputType)
1587{
1588 audio_source_t inputSource = attr->source;
1589#ifdef VOICE_CONCURRENCY
1590
1591 char propValue[PROPERTY_VALUE_MAX];
1592 bool prop_rec_enabled=false, prop_voip_enabled = false;
1593
1594 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
1595 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1596 }
1597
1598 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1599 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1600 }
1601
1602 if (prop_rec_enabled && mvoice_call_state) {
1603 //check if voice call is active / running in background
1604 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1605 //Need to block input request
1606 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1607 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1608 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1609 {
1610 switch(inputSource) {
1611 case AUDIO_SOURCE_VOICE_UPLINK:
1612 case AUDIO_SOURCE_VOICE_DOWNLINK:
1613 case AUDIO_SOURCE_VOICE_CALL:
1614 ALOGD("voice_conc:Creating input during incall mode for inputSource: %d",
1615 inputSource);
1616 break;
1617
1618 case AUDIO_SOURCE_VOICE_COMMUNICATION:
1619 if(prop_voip_enabled) {
1620 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1621 inputSource);
1622 return NO_INIT;
1623 }
1624 break;
1625 default:
1626 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1627 inputSource);
1628 return NO_INIT;
1629 }
1630 }
1631 }//check for VoIP flag
1632 else if(prop_voip_enabled && mvoice_call_state) {
1633 //check if voice call is active / running in background
1634 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1635 //Need to block input request
1636 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1637 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1638 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1639 {
1640 if(inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1641 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
1642 return NO_INIT;
1643 }
1644 }
1645 }
1646
1647#endif
1648
1649 return AudioPolicyManager::getInputForAttr(attr,
1650 input,
1651 session,
1652 uid,
1653 samplingRate,
1654 format,
1655 channelMask,
1656 flags,
1657 selectedDeviceId,
1658 inputType);
1659}
1660status_t AudioPolicyManagerCustom::startInput(audio_io_handle_t input,
1661 audio_session_t session)
1662{
1663 ALOGV("startInput() input %d", input);
1664 ssize_t index = mInputs.indexOfKey(input);
1665 if (index < 0) {
1666 ALOGW("startInput() unknown input %d", input);
1667 return BAD_VALUE;
1668 }
1669 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1670
1671 index = inputDesc->mSessions.indexOf(session);
1672 if (index < 0) {
1673 ALOGW("startInput() unknown session %d on input %d", session, input);
1674 return BAD_VALUE;
1675 }
1676
1677 // virtual input devices are compatible with other input devices
1678 if (!is_virtual_input_device(inputDesc->mDevice)) {
1679
1680 // for a non-virtual input device, check if there is another (non-virtual) active input
1681 audio_io_handle_t activeInput = mInputs.getActiveInput();
1682 if (activeInput != 0 && activeInput != input) {
1683
1684 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1685 // otherwise the active input continues and the new input cannot be started.
1686 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
1687 if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
1688 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
1689 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
1690 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
1691 } else {
1692 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
1693 return INVALID_OPERATION;
1694 }
1695 }
1696 }
1697
1698 // Routing?
1699 mInputRoutes.incRouteActivity(session);
1700#ifdef RECORD_PLAY_CONCURRENCY
1701 mIsInputRequestOnProgress = true;
1702
1703 char getPropValue[PROPERTY_VALUE_MAX];
1704 bool prop_rec_play_enabled = false;
1705
1706 if (property_get("rec.playback.conc.disabled", getPropValue, NULL)) {
1707 prop_rec_play_enabled = atoi(getPropValue) || !strncmp("true", getPropValue, 4);
1708 }
1709
1710 if ((prop_rec_play_enabled) &&(mInputs.activeInputsCount() == 0)){
1711 // send update to HAL on record playback concurrency
1712 AudioParameter param = AudioParameter();
1713 param.add(String8("rec_play_conc_on"), String8("true"));
1714 ALOGD("startInput() setParameters rec_play_conc is setting to ON ");
1715 mpClientInterface->setParameters(0, param.toString());
1716
1717 // Call invalidate to reset all opened non ULL audio tracks
1718 // Move tracks associated to this strategy from previous output to new output
1719 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
1720 // Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder)
1721 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE && (i != AUDIO_STREAM_PATCH)) {
1722 ALOGD("Invalidate on releaseInput for stream :: %d ", i);
1723 //FIXME see fixme on name change
1724 mpClientInterface->invalidateStream((audio_stream_type_t)i);
1725 }
1726 }
1727 // close compress tracks
1728 for (size_t i = 0; i < mOutputs.size(); i++) {
1729 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
1730 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
1731 ALOGD("ouput desc / profile is NULL");
1732 continue;
1733 }
1734 if (outputDesc->mProfile->mFlags
1735 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1736 // close compress sessions
1737 ALOGD("calling closeOutput on record conc for COMPRESS output");
1738 closeOutput(mOutputs.keyAt(i));
1739 }
1740 }
1741 }
1742#endif
1743
1744 if (inputDesc->mRefCount == 0 || mInputRoutes.hasRouteChanged(session)) {
1745 // if input maps to a dynamic policy with an activity listener, notify of state change
1746 if ((inputDesc->mPolicyMix != NULL)
1747 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1748 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1749 MIX_STATE_MIXING);
1750 }
1751
1752 if (mInputs.activeInputsCount() == 0) {
1753 SoundTrigger::setCaptureState(true);
1754 }
1755 setInputDevice(input, getNewInputDevice(input), true /* force */);
1756
1757 // automatically enable the remote submix output when input is started if not
1758 // used by a policy mix of type MIX_TYPE_RECORDERS
1759 // For remote submix (a virtual device), we open only one input per capture request.
1760 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1761 String8 address = String8("");
1762 if (inputDesc->mPolicyMix == NULL) {
1763 address = String8("0");
1764 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1765 address = inputDesc->mPolicyMix->mRegistrationId;
1766 }
1767 if (address != "") {
1768 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1769 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1770 address, "remote-submix");
1771 }
1772 }
1773 }
1774
1775 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1776
1777 inputDesc->mRefCount++;
1778#ifdef RECORD_PLAY_CONCURRENCY
1779 mIsInputRequestOnProgress = false;
1780#endif
1781 return NO_ERROR;
1782}
1783status_t AudioPolicyManagerCustom::stopInput(audio_io_handle_t input,
1784 audio_session_t session)
1785{
1786 status_t status;
1787 status = AudioPolicyManager::stopInput(input, session);
1788#ifdef RECORD_PLAY_CONCURRENCY
1789 char propValue[PROPERTY_VALUE_MAX];
1790 bool prop_rec_play_enabled = false;
1791
1792 if (property_get("rec.playback.conc.disabled", propValue, NULL)) {
1793 prop_rec_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1794 }
1795
1796 if ((prop_rec_play_enabled) && (mInputs.activeInputsCount() == 0)) {
1797
1798 //send update to HAL on record playback concurrency
1799 AudioParameter param = AudioParameter();
1800 param.add(String8("rec_play_conc_on"), String8("false"));
1801 ALOGD("stopInput() setParameters rec_play_conc is setting to OFF ");
1802 mpClientInterface->setParameters(0, param.toString());
1803
1804 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
1805 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
1806 //Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder stop tone)
1807 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE) && (i != AUDIO_STREAM_PATCH)) {
1808 ALOGD(" Invalidate on stopInput for stream :: %d ", i);
1809 //FIXME see fixme on name change
1810 mpClientInterface->invalidateStream((audio_stream_type_t)i);
1811 }
1812 }
1813 }
1814#endif
1815 return status;
1816}
1817
1818AudioPolicyManagerCustom::AudioPolicyManagerCustom(AudioPolicyClientInterface *clientInterface)
1819 : AudioPolicyManager(clientInterface)
1820{
1821#ifdef RECORD_PLAY_CONCURRENCY
1822 mIsInputRequestOnProgress = false;
1823#endif
1824
1825
1826#ifdef VOICE_CONCURRENCY
1827 mFallBackflag = getFallBackPath();
1828#endif
1829}
Sharad Sanglec60f6fa2015-07-27 15:14:23 +05301830audio_devices_t AudioPolicyManagerCustom::getDeviceForStrategy(routing_strategy strategy, bool fromCache)
1831{
1832 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
1833 audio_devices_t device = AUDIO_DEVICE_NONE;
1834 switch (strategy) {
1835 case STRATEGY_SONIFICATION:
1836 case STRATEGY_ENFORCED_AUDIBLE:
1837 case STRATEGY_ACCESSIBILITY:
1838 case STRATEGY_REROUTING:
1839 case STRATEGY_MEDIA:
1840 if (strategy != STRATEGY_SONIFICATION){
1841 // no sonification on WFD sink
1842 device |= availableOutputDeviceTypes & AUDIO_DEVICE_OUT_PROXY;
1843 if (device != AUDIO_DEVICE_NONE) {
1844 ALOGV("Found proxy for strategy %d", strategy);
1845 return device;
1846 }
1847 }
1848 break;
1849 default:
1850 ALOGV("getDeviceForStrategy() unknown strategy: %d", strategy);
1851 break;
1852 }
1853 device = AudioPolicyManager::getDeviceForStrategy(strategy, fromCache);
1854 return device;
1855}
1856
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001857}