blob: f105e15e418757d926c57a2939177bbfe3f111eb [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) ||
Manish Dewangana6fc5442015-08-24 20:30:31 +0530408 (((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 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS))) {
412 ALOGD("offload disabled for multi-channel FLAC/ALAC/WMA/AAC_ADTS clips with sample rate > 48kHz");
Preetam Singh Ranawat8152ab42015-07-21 19:30:09 +0530413 return false;
414 }
415#endif
416 //TODO: enable audio offloading with video when ready
417 const bool allowOffloadWithVideo =
418 property_get_bool("audio.offload.video", false /* default_value */);
419 if (offloadInfo.has_video && !allowOffloadWithVideo) {
420 ALOGV("isOffloadSupported: has_video == true, returning false");
421 return false;
422 }
Sharad Sangle36781612015-05-28 16:15:16 +0530423 }
424
425 //If duration is less than minimum value defined in property, return false
426 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
427 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
428 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
429 return false;
430 }
431 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
432 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
433 //duration checks only valid for MP3/AAC/ formats,
434 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
435 if ((offloadInfo.format == AUDIO_FORMAT_MP3) ||
436 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530437 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS) ||
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530438#ifdef AUDIO_EXTN_FORMATS_ENABLED
Sharad Sangle36781612015-05-28 16:15:16 +0530439 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
Sharad Sangle36781612015-05-28 16:15:16 +0530440 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) ||
441 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) ||
442 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) ||
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530443 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_APE) ||
Manish Dewangana6fc5442015-08-24 20:30:31 +0530444 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS) ||
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530445#endif
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530446 pcmOffload)
Sharad Sangle36781612015-05-28 16:15:16 +0530447 return false;
448
449 }
450
451 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
452 // creating an offloaded track and tearing it down immediately after start when audioflinger
453 // detects there is an active non offloadable effect.
454 // FIXME: We should check the audio session here but we do not have it in this context.
455 // This may prevent offloading in rare situations where effects are left active by apps
456 // in the background.
457 if (mEffects.isNonOffloadableEffectEnabled()) {
458 return false;
459 }
460 // Check for soundcard status
461 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
462 String8("SND_CARD_STATUS"));
463 AudioParameter result = AudioParameter(valueStr);
464 int isonline = 0;
465 if ((result.getInt(String8("SND_CARD_STATUS"), isonline) == NO_ERROR)
466 && !isonline) {
467 ALOGD("copl: soundcard is offline rejecting offload request");
468 return false;
469 }
470 // See if there is a profile to support this.
471 // AUDIO_DEVICE_NONE
472 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
473 offloadInfo.sample_rate,
474 offloadInfo.format,
475 offloadInfo.channel_mask,
476 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
477 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
478 return (profile != 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700479}
Sharad Sangle36781612015-05-28 16:15:16 +0530480audio_devices_t AudioPolicyManagerCustom::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
481 bool fromCache)
482{
483 audio_devices_t device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700484
Sharad Sangle36781612015-05-28 16:15:16 +0530485 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
486 if (index >= 0) {
487 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
488 if (patchDesc->mUid != mUidCached) {
489 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
490 outputDesc->device(), outputDesc->mPatchHandle);
491 return outputDesc->device();
492 }
493 }
494
495 // check the following by order of priority to request a routing change if necessary:
496 // 1: the strategy enforced audible is active and enforced on the output:
497 // use device for strategy enforced audible
498 // 2: we are in call or the strategy phone is active on the output:
499 // use device for strategy phone
500 // 3: the strategy for enforced audible is active but not enforced on the output:
501 // use the device for strategy enforced audible
502 // 4: the strategy sonification is active on the output:
503 // use device for strategy sonification
504 // 5: the strategy "respectful" sonification is active on the output:
505 // use device for strategy "respectful" sonification
506 // 6: the strategy accessibility is active on the output:
507 // use device for strategy accessibility
508 // 7: the strategy media is active on the output:
509 // use device for strategy media
510 // 8: the strategy DTMF is active on the output:
511 // use device for strategy DTMF
512 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
513 // use device for strategy t-t-s
514 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
515 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
516 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
517 } else if (isInCall() ||
518 isStrategyActive(outputDesc, STRATEGY_PHONE)||
519 isStrategyActive(mPrimaryOutput, STRATEGY_PHONE)) {
520 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
521 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
522 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
523 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)||
524 (isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION)
525 && (!isStrategyActive(mPrimaryOutput,STRATEGY_MEDIA)))) {
526 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
527 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)||
528 (isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION_RESPECTFUL)
529 && (!isStrategyActive(mPrimaryOutput, STRATEGY_MEDIA)))) {
530 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
531 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
532 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
533 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
534 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
535 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
536 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
537 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
538 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
539 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
540 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
541 }
542
543 ALOGV("getNewOutputDevice() selected device %x", device);
544 return device;
545}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700546void AudioPolicyManagerCustom::setPhoneState(audio_mode_t state)
547{
Sharad Sangle36781612015-05-28 16:15:16 +0530548 ALOGV("setPhoneState() state %d", state);
549 // store previous phone state for management of sonification strategy below
550 int oldState = mEngine->getPhoneState();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700551
Sharad Sangle36781612015-05-28 16:15:16 +0530552 if (mEngine->setPhoneState(state) != NO_ERROR) {
553 ALOGW("setPhoneState() invalid or same state %d", state);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700554 return;
555 }
Sharad Sangle36781612015-05-28 16:15:16 +0530556 /// Opens: can these line be executed after the switch of volume curves???
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700557 // if leaving call state, handle special case of active streams
558 // pertaining to sonification strategy see handleIncallSonification()
559 if (isInCall()) {
560 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530561 for (size_t j = 0; j < mOutputs.size(); j++) {
562 audio_io_handle_t curOutput = mOutputs.keyAt(j);
563 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
564 if (stream == AUDIO_STREAM_PATCH) {
565 continue;
566 }
567
568 handleIncallSonification((audio_stream_type_t)stream, false, true, curOutput);
569 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700570 }
Sharad Sangle36781612015-05-28 16:15:16 +0530571
572 // force reevaluating accessibility routing when call starts
573 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700574 }
575
Sharad Sangle36781612015-05-28 16:15:16 +0530576 /**
577 * Switching to or from incall state or switching between telephony and VoIP lead to force
578 * routing command.
579 */
580 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
581 || (is_state_in_call(state) && (state != oldState)));
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700582
583 // check for device and output changes triggered by new phone state
584 checkA2dpSuspend();
585 checkOutputForAllStrategies();
586 updateDevicesAndOutputs();
587
Sharad Sangle36781612015-05-28 16:15:16 +0530588 sp<SwAudioOutputDescriptor> hwOutputDesc = mPrimaryOutput;
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530589#ifdef VOICE_CONCURRENCY
590 int voice_call_state = 0;
591 char propValue[PROPERTY_VALUE_MAX];
592 bool prop_playback_enabled = false, prop_rec_enabled=false, prop_voip_enabled = false;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700593
Sharad Sanglec5766ff2015-06-04 20:24:10 +0530594 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
595 prop_playback_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
596 }
597
598 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
599 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
600 }
601
602 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
603 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
604 }
605
606 bool mode_in_call = (AUDIO_MODE_IN_CALL != oldState) && (AUDIO_MODE_IN_CALL == state);
607 //query if it is a actual voice call initiated by telephony
608 if (mode_in_call) {
609 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0, String8("in_call"));
610 AudioParameter result = AudioParameter(valueStr);
611 if (result.getInt(String8("in_call"), voice_call_state) == NO_ERROR)
612 ALOGD("voice_conc:SetPhoneState: Voice call state = %d", voice_call_state);
613 }
614
615 if (mode_in_call && voice_call_state && !mvoice_call_state) {
616 ALOGD("voice_conc:Entering to call mode oldState :: %d state::%d ",
617 oldState, state);
618 mvoice_call_state = voice_call_state;
619 if (prop_rec_enabled) {
620 //Close all active inputs
621 audio_io_handle_t activeInput = mInputs.getActiveInput();
622 if (activeInput != 0) {
623 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
624 switch(activeDesc->mInputSource) {
625 case AUDIO_SOURCE_VOICE_UPLINK:
626 case AUDIO_SOURCE_VOICE_DOWNLINK:
627 case AUDIO_SOURCE_VOICE_CALL:
628 ALOGD("voice_conc:FOUND active input during call active: %d",activeDesc->mInputSource);
629 break;
630
631 case AUDIO_SOURCE_VOICE_COMMUNICATION:
632 if(prop_voip_enabled) {
633 ALOGD("voice_conc:CLOSING VoIP input source on call setup :%d ",activeDesc->mInputSource);
634 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
635 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
636 }
637 break;
638
639 default:
640 ALOGD("voice_conc:CLOSING input on call setup for inputSource: %d",activeDesc->mInputSource);
641 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
642 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
643 break;
644 }
645 }
646 } else if (prop_voip_enabled) {
647 audio_io_handle_t activeInput = mInputs.getActiveInput();
648 if (activeInput != 0) {
649 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
650 if (AUDIO_SOURCE_VOICE_COMMUNICATION == activeDesc->mInputSource) {
651 ALOGD("voice_conc:CLOSING VoIP on call setup : %d",activeDesc->mInputSource);
652 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
653 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
654 }
655 }
656 }
657 if (prop_playback_enabled) {
658 // Move tracks associated to this strategy from previous output to new output
659 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
660 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
661 if (i == AUDIO_STREAM_PATCH) {
662 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
663 continue;
664 }
665 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
666 if ((AUDIO_STREAM_MUSIC == i) ||
667 (AUDIO_STREAM_VOICE_CALL == i) ) {
668 ALOGD("voice_conc:Invalidate stream type %d", i);
669 mpClientInterface->invalidateStream((audio_stream_type_t)i);
670 }
671 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
672 ALOGD("voice_conc:Invalidate stream type %d", i);
673 mpClientInterface->invalidateStream((audio_stream_type_t)i);
674 }
675 }
676 }
677
678 for (size_t i = 0; i < mOutputs.size(); i++) {
679 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
680 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
681 ALOGD("voice_conc:ouput desc / profile is NULL");
682 continue;
683 }
684
685 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
686 if (((!outputDesc->isDuplicated() &&outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY))
687 && prop_playback_enabled) {
688 ALOGD("voice_conc:calling suspendOutput on call mode for primary output");
689 mpClientInterface->suspendOutput(mOutputs.keyAt(i));
690 } //Close compress all sessions
691 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
692 && prop_playback_enabled) {
693 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
694 closeOutput(mOutputs.keyAt(i));
695 }
696 else if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_VOIP_RX)
697 && prop_voip_enabled) {
698 ALOGD("voice_conc:calling closeOutput on call mode for DIRECT output");
699 closeOutput(mOutputs.keyAt(i));
700 }
701 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
702 if ((outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)
703 && prop_playback_enabled) {
704 ALOGD("voice_conc:calling closeOutput on call mode for COMPRESS output");
705 closeOutput(mOutputs.keyAt(i));
706 }
707 }
708 }
709 }
710
711 if ((AUDIO_MODE_IN_CALL == oldState || AUDIO_MODE_IN_COMMUNICATION == oldState) &&
712 (AUDIO_MODE_NORMAL == state) && prop_playback_enabled && mvoice_call_state) {
713 ALOGD("voice_conc:EXITING from call mode oldState :: %d state::%d \n",oldState, state);
714 mvoice_call_state = 0;
715 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
716 //restore PCM (deep-buffer) output after call termination
717 for (size_t i = 0; i < mOutputs.size(); i++) {
718 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
719 if ( (outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
720 ALOGD("voice_conc:ouput desc / profile is NULL");
721 continue;
722 }
723 if (!outputDesc->isDuplicated() && outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
724 ALOGD("voice_conc:calling restoreOutput after call mode for primary output");
725 mpClientInterface->restoreOutput(mOutputs.keyAt(i));
726 }
727 }
728 }
729 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
730 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
731 ALOGV("voice_conc:Invalidate on call mode for stream :: %d ", i);
732 if (i == AUDIO_STREAM_PATCH) {
733 ALOGV("voice_conc:not calling invalidate for AUDIO_STREAM_PATCH");
734 continue;
735 }
736 if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
737 if ((AUDIO_STREAM_MUSIC == i) ||
738 (AUDIO_STREAM_VOICE_CALL == i) ) {
739 mpClientInterface->invalidateStream((audio_stream_type_t)i);
740 }
741 } else if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
742 mpClientInterface->invalidateStream((audio_stream_type_t)i);
743 }
744 }
745 }
746
747#endif
748#ifdef RECORD_PLAY_CONCURRENCY
749 char recConcPropValue[PROPERTY_VALUE_MAX];
750 bool prop_rec_play_enabled = false;
751
752 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
753 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
754 }
755 if (prop_rec_play_enabled) {
756 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
757 ALOGD("phone state changed to MODE_IN_COMM invlaidating music and voice streams");
758 // call invalidate for voice streams, so that it can use deepbuffer with VoIP out device from HAL
759 mpClientInterface->invalidateStream(AUDIO_STREAM_VOICE_CALL);
760 // call invalidate for music, so that compress will fallback to deep-buffer with VoIP out device
761 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
762
763 // close compress output to make sure session will be closed before timeout(60sec)
764 for (size_t i = 0; i < mOutputs.size(); i++) {
765
766 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
767 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
768 ALOGD("ouput desc / profile is NULL");
769 continue;
770 }
771
772 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
773 ALOGD("calling closeOutput on call mode for COMPRESS output");
774 closeOutput(mOutputs.keyAt(i));
775 }
776 }
777 } else if ((oldState == AUDIO_MODE_IN_COMMUNICATION) &&
778 (mEngine->getPhoneState() == AUDIO_MODE_NORMAL)) {
779 // call invalidate for music so that music can fallback to compress
780 mpClientInterface->invalidateStream(AUDIO_STREAM_MUSIC);
781 }
782 }
783#endif
784 mPrevPhoneState = oldState;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700785 int delayMs = 0;
786 if (isStateInCall(state)) {
787 nsecs_t sysTime = systemTime();
788 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +0530789 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700790 // mute media and sonification strategies and delay device switch by the largest
791 // latency of any output where either strategy is active.
792 // This avoid sending the ring tone or music tail into the earpiece or headset.
Sharad Sangle36781612015-05-28 16:15:16 +0530793 if ((isStrategyActive(desc, STRATEGY_MEDIA,
794 SONIFICATION_HEADSET_MUSIC_DELAY,
795 sysTime) ||
796 isStrategyActive(desc, STRATEGY_SONIFICATION,
797 SONIFICATION_HEADSET_MUSIC_DELAY,
798 sysTime)) &&
799 (delayMs < (int)desc->latency()*2)) {
800 delayMs = desc->latency()*2;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700801 }
Sharad Sangle36781612015-05-28 16:15:16 +0530802 setStrategyMute(STRATEGY_MEDIA, true, desc);
803 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700804 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Sharad Sangle36781612015-05-28 16:15:16 +0530805 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
806 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700807 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
808 }
Sharad Sangle36781612015-05-28 16:15:16 +0530809 ALOGV("Setting the delay from %dms to %dms", delayMs,
810 MIN(delayMs, MAX_VOICE_CALL_START_DELAY_MS));
811 delayMs = MIN(delayMs, MAX_VOICE_CALL_START_DELAY_MS);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700812 }
813
Sharad Sangle36781612015-05-28 16:15:16 +0530814 if (hasPrimaryOutput()) {
815 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
816 // the device returned is not necessarily reachable via this output
817 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
818 // force routing command to audio hardware when ending call
819 // even if no device change is needed
820 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
821 rxDevice = mPrimaryOutput->device();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700822 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700823
Sharad Sangle36781612015-05-28 16:15:16 +0530824 if (state == AUDIO_MODE_IN_CALL) {
825 updateCallRouting(rxDevice, delayMs);
826 } else if (oldState == AUDIO_MODE_IN_CALL) {
827 if (mCallRxPatch != 0) {
828 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
829 mCallRxPatch.clear();
830 }
831 if (mCallTxPatch != 0) {
832 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
833 mCallTxPatch.clear();
834 }
835 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
836 } else {
837 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700838 }
839 }
840
841 // if entering in call state, handle special case of active streams
842 // pertaining to sonification strategy see handleIncallSonification()
843 if (isStateInCall(state)) {
844 ALOGV("setPhoneState() in call state management: new state is %d", state);
Sharad Sangle36781612015-05-28 16:15:16 +0530845 for (size_t j = 0; j < mOutputs.size(); j++) {
846 audio_io_handle_t curOutput = mOutputs.keyAt(j);
847 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
848 if (stream == AUDIO_STREAM_PATCH) {
849 continue;
850 }
851 handleIncallSonification((audio_stream_type_t)stream, true, true, curOutput);
852 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700853 }
854 }
855
856 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
857 if (state == AUDIO_MODE_RINGTONE &&
858 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
859 mLimitRingtoneVolume = true;
860 } else {
861 mLimitRingtoneVolume = false;
862 }
863}
Sharad Sangle36781612015-05-28 16:15:16 +0530864status_t AudioPolicyManagerCustom::stopSource(sp<SwAudioOutputDescriptor> outputDesc,
865 audio_stream_type_t stream,
866 bool forceDeviceUpdate)
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700867{
Sharad Sangle36781612015-05-28 16:15:16 +0530868 // always handle stream stop, check which stream type is stopping
869 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700870
Sharad Sangle36781612015-05-28 16:15:16 +0530871 // handle special case for sonification while in call
872 if (isInCall()) {
873 if (outputDesc->isDuplicated()) {
874 handleIncallSonification(stream, false, false, outputDesc->mIoHandle);
875 handleIncallSonification(stream, false, false, outputDesc->mIoHandle);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700876 }
Sharad Sangle36781612015-05-28 16:15:16 +0530877 handleIncallSonification(stream, false, false, outputDesc->mIoHandle);
878 }
879
880 if (outputDesc->mRefCount[stream] > 0) {
881 // decrement usage count of this stream on the output
882 outputDesc->changeRefCount(stream, -1);
883
884 // store time at which the stream was stopped - see isStreamActive()
885 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
886 outputDesc->mStopTime[stream] = systemTime();
887 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
888 // delay the device switch by twice the latency because stopOutput() is executed when
889 // the track stop() command is received and at that time the audio track buffer can
890 // still contain data that needs to be drained. The latency only covers the audio HAL
891 // and kernel buffers. Also the latency does not always include additional delay in the
892 // audio path (audio DSP, CODEC ...)
893 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
894
895 // force restoring the device selection on other active outputs if it differs from the
896 // one being selected for this output
897 for (size_t i = 0; i < mOutputs.size(); i++) {
898 audio_io_handle_t curOutput = mOutputs.keyAt(i);
899 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
900 if (desc != outputDesc &&
901 desc->isActive() &&
902 outputDesc->sharesHwModuleWith(desc) &&
903 (newDevice != desc->device())) {
904 setOutputDevice(desc,
905 getNewOutputDevice(desc, false /*fromCache*/),
906 true,
907 outputDesc->latency()*2);
908 }
909 }
910 // update the outputs if stopping one with a stream that can affect notification routing
911 handleNotificationRoutingForStream(stream);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700912 }
Sharad Sangle36781612015-05-28 16:15:16 +0530913 return NO_ERROR;
914 } else {
915 ALOGW("stopOutput() refcount is already 0");
916 return INVALID_OPERATION;
917 }
918}
919status_t AudioPolicyManagerCustom::startSource(sp<SwAudioOutputDescriptor> outputDesc,
920 audio_stream_type_t stream,
921 audio_devices_t device,
922 uint32_t *delayMs)
923{
924 // cannot start playback of STREAM_TTS if any other output is being used
925 uint32_t beaconMuteLatency = 0;
926
927 *delayMs = 0;
928 if (stream == AUDIO_STREAM_TTS) {
929 ALOGV("\t found BEACON stream");
930 if (mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
931 return INVALID_OPERATION;
932 } else {
933 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700934 }
Sharad Sangle36781612015-05-28 16:15:16 +0530935 } else {
936 // some playback other than beacon starts
937 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
938 }
939
940 // increment usage count for this stream on the requested output:
941 // NOTE that the usage count is the same for duplicated output and hardware output which is
942 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
943 outputDesc->changeRefCount(stream, 1);
944
945 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
946 // starting an output being rerouted?
947 if (device == AUDIO_DEVICE_NONE) {
948 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700949 }
Sharad Sangle36781612015-05-28 16:15:16 +0530950 routing_strategy strategy = getStrategy(stream);
951 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
952 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
953 (beaconMuteLatency > 0);
954 uint32_t waitMs = beaconMuteLatency;
955 bool force = false;
956 for (size_t i = 0; i < mOutputs.size(); i++) {
957 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
958 if (desc != outputDesc) {
959 // force a device change if any other output is managed by the same hw
960 // module and has a current device selection that differs from selected device.
961 // In this case, the audio HAL must receive the new device selection so that it can
962 // change the device currently selected by the other active output.
963 if (outputDesc->sharesHwModuleWith(desc) &&
964 desc->device() != device) {
965 force = true;
966 }
967 // wait for audio on other active outputs to be presented when starting
968 // a notification so that audio focus effect can propagate, or that a mute/unmute
969 // event occurred for beacon
970 uint32_t latency = desc->latency();
971 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
972 waitMs = latency;
973 }
974 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -0700975 }
Sharad Sangle36781612015-05-28 16:15:16 +0530976 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force);
977
978 // handle special case for sonification while in call
979 if (isInCall()) {
980 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -0700981 }
Sharad Sangle36781612015-05-28 16:15:16 +0530982
983 // apply volume rules for current stream and device if necessary
984 checkAndSetVolume(stream,
985 mStreams.valueFor(stream).getVolumeIndex(device),
986 outputDesc,
987 device);
988
989 // update the outputs if starting an output with a stream that can affect notification
990 // routing
991 handleNotificationRoutingForStream(stream);
992
993 // force reevaluating accessibility routing when ringtone or alarm starts
994 if (strategy == STRATEGY_SONIFICATION) {
995 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
996 }
997 }
998 else {
999 // handle special case for sonification while in call
1000 if (isInCall()) {
1001 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
1002 }
1003 }
1004 return NO_ERROR;
1005}
1006void AudioPolicyManagerCustom::handleIncallSonification(audio_stream_type_t stream,
1007 bool starting, bool stateChange,
1008 audio_io_handle_t output)
1009{
1010 if(!hasPrimaryOutput()) {
1011 return;
1012 }
1013 // no action needed for AUDIO_STREAM_PATCH stream type, it's for internal flinger tracks
1014 if (stream == AUDIO_STREAM_PATCH) {
1015 return;
1016 }
1017 // if the stream pertains to sonification strategy and we are in call we must
1018 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
1019 // in the device used for phone strategy and play the tone if the selected device does not
1020 // interfere with the device used for phone strategy
1021 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
1022 // many times as there are active tracks on the output
1023 const routing_strategy stream_strategy = getStrategy(stream);
1024 if ((stream_strategy == STRATEGY_SONIFICATION) ||
1025 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
1026 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
1027 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
1028 stream, starting, outputDesc->mDevice, stateChange);
1029 if (outputDesc->mRefCount[stream]) {
1030 int muteCount = 1;
1031 if (stateChange) {
1032 muteCount = outputDesc->mRefCount[stream];
1033 }
1034 if (audio_is_low_visibility(stream)) {
1035 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
1036 for (int i = 0; i < muteCount; i++) {
1037 setStreamMute(stream, starting, outputDesc);
1038 }
1039 } else {
1040 ALOGV("handleIncallSonification() high visibility");
1041 if (outputDesc->device() &
1042 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
1043 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
1044 for (int i = 0; i < muteCount; i++) {
1045 setStreamMute(stream, starting, outputDesc);
1046 }
1047 }
1048 if (starting) {
1049 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
1050 AUDIO_STREAM_VOICE_CALL);
1051 } else {
1052 mpClientInterface->stopTone();
1053 }
1054 }
1055 }
1056 }
1057}
1058void AudioPolicyManagerCustom::handleNotificationRoutingForStream(audio_stream_type_t stream) {
1059 switch(stream) {
1060 case AUDIO_STREAM_MUSIC:
1061 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
1062 updateDevicesAndOutputs();
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001063 break;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001064 default:
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001065 break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -07001066 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001067}
Sharad Sangle36781612015-05-28 16:15:16 +05301068status_t AudioPolicyManagerCustom::checkAndSetVolume(audio_stream_type_t stream,
1069 int index,
1070 const sp<SwAudioOutputDescriptor>& outputDesc,
1071 audio_devices_t device,
1072 int delayMs, bool force)
1073{
1074 // do not change actual stream volume if the stream is muted
1075 if (outputDesc->mMuteCount[stream] != 0) {
1076 ALOGVV("checkAndSetVolume() stream %d muted count %d",
1077 stream, outputDesc->mMuteCount[stream]);
1078 return NO_ERROR;
1079 }
1080 audio_policy_forced_cfg_t forceUseForComm =
1081 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
1082 // do not change in call volume if bluetooth is connected and vice versa
1083 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
1084 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
1085 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
1086 stream, forceUseForComm);
1087 return INVALID_OPERATION;
1088 }
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001089
Sharad Sangle36781612015-05-28 16:15:16 +05301090 if (device == AUDIO_DEVICE_NONE) {
1091 device = outputDesc->device();
1092 }
1093
1094 float volumeDb = computeVolume(stream, index, device);
1095 if (outputDesc->isFixedVolume(device)) {
1096 volumeDb = 0.0f;
1097 }
1098
1099 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
1100
1101 if (stream == AUDIO_STREAM_VOICE_CALL ||
1102 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
1103 float voiceVolume;
1104 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
1105 if (stream == AUDIO_STREAM_VOICE_CALL) {
1106 voiceVolume = (float)index/(float)mStreams.valueFor(stream).getVolumeIndexMax();
1107 } else {
1108 voiceVolume = 1.0;
1109 }
1110
1111 if (voiceVolume != mLastVoiceVolume && ((outputDesc == mPrimaryOutput) ||
1112 isDirectOutput(outputDesc->mIoHandle) || device & AUDIO_DEVICE_OUT_ALL_USB)) {
1113 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
1114 mLastVoiceVolume = voiceVolume;
1115 }
1116 }
1117
1118 return NO_ERROR;
1119}
1120bool AudioPolicyManagerCustom::isDirectOutput(audio_io_handle_t output) {
1121 for (size_t i = 0; i < mOutputs.size(); i++) {
1122 audio_io_handle_t curOutput = mOutputs.keyAt(i);
1123 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1124 if ((curOutput == output) && (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
1125 return true;
1126 }
1127 }
1128 return false;
1129}
vivek mehta0ea887a2015-08-26 14:01:20 -07001130
1131status_t AudioPolicyManagerCustom::getOutputForAttr(const audio_attributes_t *attr,
1132 audio_io_handle_t *output,
1133 audio_session_t session,
1134 audio_stream_type_t *stream,
1135 uid_t uid,
1136 uint32_t samplingRate,
1137 audio_format_t format,
1138 audio_channel_mask_t channelMask,
1139 audio_output_flags_t flags,
1140 audio_port_handle_t selectedDeviceId,
1141 const audio_offload_info_t *offloadInfo)
1142{
1143 audio_offload_info_t tOffloadInfo = AUDIO_INFO_INITIALIZER;
1144
1145 bool pcmOffloadEnabled = property_get_bool("audio.offload.track.enable", false);
1146
1147 if (offloadInfo == NULL && pcmOffloadEnabled) {
1148 tOffloadInfo.sample_rate = samplingRate;
1149 tOffloadInfo.channel_mask = channelMask;
1150 tOffloadInfo.format = format;
1151 tOffloadInfo.stream_type = *stream;
1152 tOffloadInfo.bit_width = 16; //hard coded for PCM_16
1153 if (attr != NULL) {
1154 ALOGV("found attribute .. setting usage %d ", attr->usage);
1155 tOffloadInfo.usage = attr->usage;
1156 } else {
1157 ALOGD("%s:: attribute is NULL .. no usage set", __func__);
1158 }
1159 offloadInfo = &tOffloadInfo;
1160 }
1161
1162 return AudioPolicyManager::getOutputForAttr(attr, output, session, stream,
1163 (uid_t)uid, (uint32_t)samplingRate,
1164 format, (audio_channel_mask_t)channelMask,
1165 flags, (audio_port_handle_t)selectedDeviceId,
1166 offloadInfo);
1167}
1168
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001169audio_io_handle_t AudioPolicyManagerCustom::getOutputForDevice(
1170 audio_devices_t device,
Sharad Sangle36781612015-05-28 16:15:16 +05301171 audio_session_t session __unused,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001172 audio_stream_type_t stream,
1173 uint32_t samplingRate,
1174 audio_format_t format,
1175 audio_channel_mask_t channelMask,
1176 audio_output_flags_t flags,
1177 const audio_offload_info_t *offloadInfo)
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001178{
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001179 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
1180 uint32_t latency = 0;
1181 status_t status;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001182
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001183#ifdef AUDIO_POLICY_TEST
1184 if (mCurOutput != 0) {
1185 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1186 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1187
1188 if (mTestOutputs[mCurOutput] == 0) {
1189 ALOGV("getOutput() opening test output");
Sharad Sangle36781612015-05-28 16:15:16 +05301190 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
1191 mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001192 outputDesc->mDevice = mTestDevice;
1193 outputDesc->mLatency = mTestLatencyMs;
1194 outputDesc->mFlags =
1195 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
1196 outputDesc->mRefCount[stream] = 0;
1197 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1198 config.sample_rate = mTestSamplingRate;
1199 config.channel_mask = mTestChannels;
1200 config.format = mTestFormat;
1201 if (offloadInfo != NULL) {
1202 config.offload_info = *offloadInfo;
1203 }
1204 status = mpClientInterface->openOutput(0,
1205 &mTestOutputs[mCurOutput],
1206 &config,
1207 &outputDesc->mDevice,
1208 String8(""),
1209 &outputDesc->mLatency,
1210 outputDesc->mFlags);
1211 if (status == NO_ERROR) {
1212 outputDesc->mSamplingRate = config.sample_rate;
1213 outputDesc->mFormat = config.format;
1214 outputDesc->mChannelMask = config.channel_mask;
1215 AudioParameter outputCmd = AudioParameter();
1216 outputCmd.addInt(String8("set_id"),mCurOutput);
1217 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1218 addOutput(mTestOutputs[mCurOutput], outputDesc);
1219 }
1220 }
1221 return mTestOutputs[mCurOutput];
1222 }
1223#endif //AUDIO_POLICY_TEST
Sharad Sangle36781612015-05-28 16:15:16 +05301224 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1225 (stream != AUDIO_STREAM_MUSIC)) {
1226 // compress should not be used for non-music streams
1227 ALOGE("Offloading only allowed with music stream");
1228 return 0;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301229 }
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301230
1231 if ((stream == AUDIO_STREAM_VOICE_CALL) &&
1232 (channelMask == 1) &&
1233 (samplingRate == 8000 || samplingRate == 16000)) {
1234 // Allow Voip direct output only if:
1235 // audio mode is MODE_IN_COMMUNCATION; AND
1236 // voip output is not opened already; AND
1237 // requested sample rate matches with that of voip input stream (if opened already)
1238 int value = 0;
1239 uint32_t mode = 0, voipOutCount = 1, voipSampleRate = 1;
1240 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1241 String8("audio_mode"));
1242 AudioParameter result = AudioParameter(valueStr);
1243 if (result.getInt(String8("audio_mode"), value) == NO_ERROR) {
1244 mode = value;
1245 }
1246
1247 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1248 String8("voip_out_stream_count"));
1249 result = AudioParameter(valueStr);
1250 if (result.getInt(String8("voip_out_stream_count"), value) == NO_ERROR) {
1251 voipOutCount = value;
1252 }
1253
1254 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1255 String8("voip_sample_rate"));
1256 result = AudioParameter(valueStr);
1257 if (result.getInt(String8("voip_sample_rate"), value) == NO_ERROR) {
1258 voipSampleRate = value;
1259 }
1260
1261 if ((mode == AUDIO_MODE_IN_COMMUNICATION) && (voipOutCount == 0) &&
1262 ((voipSampleRate == 0) || (voipSampleRate == samplingRate))) {
1263 if (audio_is_linear_pcm(format)) {
1264 char propValue[PROPERTY_VALUE_MAX] = {0};
1265 property_get("use.voice.path.for.pcm.voip", propValue, "0");
1266 bool voipPcmSysPropEnabled = !strncmp("true", propValue, sizeof("true"));
1267 if (voipPcmSysPropEnabled && (format == AUDIO_FORMAT_PCM_16_BIT)) {
1268 flags = (audio_output_flags_t)((flags &~AUDIO_OUTPUT_FLAG_FAST) |
1269 AUDIO_OUTPUT_FLAG_VOIP_RX | AUDIO_OUTPUT_FLAG_DIRECT);
1270 ALOGD("Set VoIP and Direct output flags for PCM format");
1271 }
1272 }
1273 }
1274 }
1275
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301276#ifdef VOICE_CONCURRENCY
1277 char propValue[PROPERTY_VALUE_MAX];
1278 bool prop_play_enabled=false, prop_voip_enabled = false;
1279
1280 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1281 prop_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001282 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301283
1284 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1285 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1286 }
1287
1288 if (prop_play_enabled && mvoice_call_state) {
1289 //check if voice call is active / running in background
1290 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1291 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1292 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1293 {
1294 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1295 if(prop_voip_enabled) {
1296 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1297 flags );
1298 return 0;
1299 }
1300 }
1301 else {
1302 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
1303 ALOGD("voice_conc:IN call mode adding ULL flags .. flags: %x ", flags );
1304 flags = AUDIO_OUTPUT_FLAG_FAST;
1305 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
1306 if (AUDIO_STREAM_MUSIC == stream) {
1307 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1308 ALOGD("voice_conc:IN call mode adding deep-buffer flags %x ", flags );
1309 }
1310 else {
1311 flags = AUDIO_OUTPUT_FLAG_FAST;
1312 ALOGD("voice_conc:IN call mode adding fast flags %x ", flags );
1313 }
1314 }
1315 }
1316 }
1317 } else if (prop_voip_enabled && mvoice_call_state) {
1318 //check if voice call is active / running in background
1319 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1320 //return only ULL ouput
1321 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1322 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1323 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1324 {
1325 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1326 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1327 flags );
1328 return 0;
1329 }
1330 }
1331 }
1332#endif
1333#ifdef RECORD_PLAY_CONCURRENCY
1334 char recConcPropValue[PROPERTY_VALUE_MAX];
1335 bool prop_rec_play_enabled = false;
1336
1337 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
1338 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
1339 }
1340 if ((prop_rec_play_enabled) &&
1341 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
1342 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
1343 if (AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1344 // allow VoIP using voice path
1345 // Do nothing
1346 } else if((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1347 ALOGD("voice_conc:MODE_IN_COMM is setforcing deep buffer output for non ULL... flags: %x", flags);
1348 // use deep buffer path for all non ULL outputs
1349 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1350 }
1351 } else if ((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1352 ALOGD("voice_conc:Record mode is on forcing deep buffer output for non ULL... flags: %x ", flags);
1353 // use deep buffer path for all non ULL outputs
1354 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1355 }
1356 }
1357 if (prop_rec_play_enabled &&
1358 (stream == AUDIO_STREAM_ENFORCED_AUDIBLE)) {
1359 ALOGD("Record conc is on forcing ULL output for ENFORCED_AUDIBLE");
1360 flags = AUDIO_OUTPUT_FLAG_FAST;
1361 }
1362#endif
1363
Sharad Sangle36781612015-05-28 16:15:16 +05301364 /*
1365 * WFD audio routes back to target speaker when starting a ringtone playback.
1366 * This is because primary output is reused for ringtone, so output device is
1367 * updated based on SONIFICATION strategy for both ringtone and music playback.
1368 * The same issue is not seen on remoted_submix HAL based WFD audio because
1369 * primary output is not reused and a new output is created for ringtone playback.
1370 * Issue is fixed by updating output flag to AUDIO_OUTPUT_FLAG_FAST when there is
1371 * a non-music stream playback on WFD, so primary output is not reused for ringtone.
1372 */
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001373 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
1374 if ((availableOutputDeviceTypes & AUDIO_DEVICE_OUT_PROXY)
1375 && (stream != AUDIO_STREAM_MUSIC)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301376 ALOGD("WFD audio: use OUTPUT_FLAG_FAST for non music stream. flags:%x", flags );
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001377 //For voip paths
1378 if(flags & AUDIO_OUTPUT_FLAG_DIRECT)
1379 flags = AUDIO_OUTPUT_FLAG_DIRECT;
1380 else //route every thing else to ULL path
1381 flags = AUDIO_OUTPUT_FLAG_FAST;
1382 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001383 // open a direct output if required by specified parameters
vivek mehta0ea887a2015-08-26 14:01:20 -07001384 // force direct flag if offload flag is set: offloading implies a direct output stream
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001385 // and all common behaviors are driven by checking only the direct flag
1386 // this should normally be set appropriately in the policy configuration file
1387 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1388 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1389 }
1390 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1391 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1392 }
vivek mehta0ea887a2015-08-26 14:01:20 -07001393
1394 // Do offload magic here
1395 if ((flags == AUDIO_OUTPUT_FLAG_NONE) && (stream == AUDIO_STREAM_MUSIC) &&
1396 (offloadInfo != NULL) &&
1397 ((offloadInfo->usage == AUDIO_USAGE_MEDIA ||
1398 (offloadInfo->usage == AUDIO_USAGE_GAME)))) {
1399 if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) {
1400 ALOGD("AudioCustomHAL --> Force Direct Flag ..");
1401 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1402 }
1403 }
1404
Sharad Sangle36781612015-05-28 16:15:16 +05301405 // only allow deep buffering for music stream type
1406 if (stream != AUDIO_STREAM_MUSIC) {
1407 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Sharad Sangle497aef82015-08-03 17:55:48 +05301408 } else if (/* stream == AUDIO_STREAM_MUSIC && */
1409 flags == AUDIO_OUTPUT_FLAG_NONE &&
1410 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
1411 flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Sharad Sangle36781612015-05-28 16:15:16 +05301412 }
Sharad Sangle497aef82015-08-03 17:55:48 +05301413
Sharad Sangle36781612015-05-28 16:15:16 +05301414 if (stream == AUDIO_STREAM_TTS) {
1415 flags = AUDIO_OUTPUT_FLAG_TTS;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001416 }
1417
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301418 // open a direct output if required by specified parameters
1419 //force direct flag if offload flag is set: offloading implies a direct output stream
1420 // and all common behaviors are driven by checking only the direct flag
1421 // this should normally be set appropriately in the policy configuration file
1422 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1423 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1424 }
1425 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1426 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1427 }
1428 // only allow deep buffering for music stream type
1429 if (stream != AUDIO_STREAM_MUSIC) {
1430 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
1431 }
1432 if (stream == AUDIO_STREAM_TTS) {
1433 flags = AUDIO_OUTPUT_FLAG_TTS;
1434 }
1435
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001436 sp<IOProfile> profile;
1437
1438 // skip direct output selection if the request can obviously be attached to a mixed output
1439 // and not explicitly requested
1440 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
1441 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
1442 audio_channel_count_from_out_mask(channelMask) <= 2) {
1443 goto non_direct_output;
1444 }
1445
1446 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1447 // creating an offloaded track and tearing it down immediately after start when audioflinger
1448 // detects there is an active non offloadable effect.
1449 // FIXME: We should check the audio session here but we do not have it in this context.
1450 // This may prevent offloading in rare situations where effects are left active by apps
1451 // in the background.
1452
Sharad Sangle36781612015-05-28 16:15:16 +05301453 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
1454 !mEffects.isNonOffloadableEffectEnabled()) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001455 profile = getProfileForDirectOutput(device,
1456 samplingRate,
1457 format,
1458 channelMask,
1459 (audio_output_flags_t)flags);
1460 }
1461
1462 if (profile != 0) {
Sharad Sangle36781612015-05-28 16:15:16 +05301463 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001464
1465 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +05301466 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001467 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1468 outputDesc = desc;
1469 // reuse direct output if currently open and configured with same parameters
1470 if ((samplingRate == outputDesc->mSamplingRate) &&
1471 (format == outputDesc->mFormat) &&
1472 (channelMask == outputDesc->mChannelMask)) {
1473 outputDesc->mDirectOpenCount++;
1474 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1475 return mOutputs.keyAt(i);
1476 }
1477 }
1478 }
1479 // close direct output if currently open and configured with different parameters
1480 if (outputDesc != NULL) {
1481 closeOutput(outputDesc->mIoHandle);
1482 }
Sharad Sangle36781612015-05-28 16:15:16 +05301483
1484 // if the selected profile is offloaded and no offload info was specified,
1485 // create a default one
1486 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
1487 if ((profile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
1488 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1489 defaultOffloadInfo.sample_rate = samplingRate;
1490 defaultOffloadInfo.channel_mask = channelMask;
1491 defaultOffloadInfo.format = format;
1492 defaultOffloadInfo.stream_type = stream;
1493 defaultOffloadInfo.bit_rate = 0;
1494 defaultOffloadInfo.duration_us = -1;
1495 defaultOffloadInfo.has_video = true; // conservative
1496 defaultOffloadInfo.is_streaming = true; // likely
1497 offloadInfo = &defaultOffloadInfo;
1498 }
1499
1500 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001501 outputDesc->mDevice = device;
1502 outputDesc->mLatency = 0;
Sharad Sangle36781612015-05-28 16:15:16 +05301503 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001504 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1505 config.sample_rate = samplingRate;
1506 config.channel_mask = channelMask;
1507 config.format = format;
1508 if (offloadInfo != NULL) {
1509 config.offload_info = *offloadInfo;
1510 }
Sharad Sangle36781612015-05-28 16:15:16 +05301511 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001512 &output,
1513 &config,
1514 &outputDesc->mDevice,
1515 String8(""),
1516 &outputDesc->mLatency,
1517 outputDesc->mFlags);
1518
1519 // only accept an output with the requested parameters
1520 if (status != NO_ERROR ||
1521 (samplingRate != 0 && samplingRate != config.sample_rate) ||
1522 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
1523 (channelMask != 0 && channelMask != config.channel_mask)) {
1524 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1525 "format %d %d, channelMask %04x %04x", output, samplingRate,
1526 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1527 outputDesc->mChannelMask);
1528 if (output != AUDIO_IO_HANDLE_NONE) {
1529 mpClientInterface->closeOutput(output);
1530 }
Sharad Sangle36781612015-05-28 16:15:16 +05301531 // fall back to mixer output if possible when the direct output could not be open
1532 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
1533 goto non_direct_output;
1534 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001535 return AUDIO_IO_HANDLE_NONE;
1536 }
1537 outputDesc->mSamplingRate = config.sample_rate;
1538 outputDesc->mChannelMask = config.channel_mask;
1539 outputDesc->mFormat = config.format;
1540 outputDesc->mRefCount[stream] = 0;
1541 outputDesc->mStopTime[stream] = 0;
1542 outputDesc->mDirectOpenCount = 1;
1543
1544 audio_io_handle_t srcOutput = getOutputForEffect();
1545 addOutput(output, outputDesc);
1546 audio_io_handle_t dstOutput = getOutputForEffect();
1547 if (dstOutput == output) {
1548 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1549 }
1550 mPreviousOutputs = mOutputs;
1551 ALOGV("getOutput() returns new direct output %d", output);
1552 mpClientInterface->onAudioPortListUpdate();
1553 return output;
1554 }
1555
1556non_direct_output:
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001557 // ignoring channel mask due to downmix capability in mixer
1558
1559 // open a non direct output
1560
1561 // for non direct outputs, only PCM is supported
1562 if (audio_is_linear_pcm(format)) {
1563 // get which output is suitable for the specified stream. The actual
1564 // routing change will happen when startOutput() will be called
1565 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1566
1567 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1568 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1569 output = selectOutput(outputs, flags, format);
1570 }
1571 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1572 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1573
vivek mehta0ea887a2015-08-26 14:01:20 -07001574 ALOGV("getOutputForDevice() returns output %d", output);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001575
1576 return output;
1577}
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301578
1579status_t AudioPolicyManagerCustom::getInputForAttr(const audio_attributes_t *attr,
1580 audio_io_handle_t *input,
1581 audio_session_t session,
1582 uid_t uid,
1583 uint32_t samplingRate,
1584 audio_format_t format,
1585 audio_channel_mask_t channelMask,
1586 audio_input_flags_t flags,
1587 audio_port_handle_t selectedDeviceId,
1588 input_type_t *inputType)
1589{
1590 audio_source_t inputSource = attr->source;
1591#ifdef VOICE_CONCURRENCY
1592
1593 char propValue[PROPERTY_VALUE_MAX];
1594 bool prop_rec_enabled=false, prop_voip_enabled = false;
1595
1596 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
1597 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1598 }
1599
1600 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1601 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1602 }
1603
1604 if (prop_rec_enabled && mvoice_call_state) {
1605 //check if voice call is active / running in background
1606 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1607 //Need to block input request
1608 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1609 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1610 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1611 {
1612 switch(inputSource) {
1613 case AUDIO_SOURCE_VOICE_UPLINK:
1614 case AUDIO_SOURCE_VOICE_DOWNLINK:
1615 case AUDIO_SOURCE_VOICE_CALL:
1616 ALOGD("voice_conc:Creating input during incall mode for inputSource: %d",
1617 inputSource);
1618 break;
1619
1620 case AUDIO_SOURCE_VOICE_COMMUNICATION:
1621 if(prop_voip_enabled) {
1622 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1623 inputSource);
1624 return NO_INIT;
1625 }
1626 break;
1627 default:
1628 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1629 inputSource);
1630 return NO_INIT;
1631 }
1632 }
1633 }//check for VoIP flag
1634 else if(prop_voip_enabled && mvoice_call_state) {
1635 //check if voice call is active / running in background
1636 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1637 //Need to block input request
1638 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1639 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1640 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1641 {
1642 if(inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1643 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
1644 return NO_INIT;
1645 }
1646 }
1647 }
1648
1649#endif
1650
1651 return AudioPolicyManager::getInputForAttr(attr,
1652 input,
1653 session,
1654 uid,
1655 samplingRate,
1656 format,
1657 channelMask,
1658 flags,
1659 selectedDeviceId,
1660 inputType);
1661}
1662status_t AudioPolicyManagerCustom::startInput(audio_io_handle_t input,
1663 audio_session_t session)
1664{
1665 ALOGV("startInput() input %d", input);
1666 ssize_t index = mInputs.indexOfKey(input);
1667 if (index < 0) {
1668 ALOGW("startInput() unknown input %d", input);
1669 return BAD_VALUE;
1670 }
1671 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1672
1673 index = inputDesc->mSessions.indexOf(session);
1674 if (index < 0) {
1675 ALOGW("startInput() unknown session %d on input %d", session, input);
1676 return BAD_VALUE;
1677 }
1678
1679 // virtual input devices are compatible with other input devices
1680 if (!is_virtual_input_device(inputDesc->mDevice)) {
1681
1682 // for a non-virtual input device, check if there is another (non-virtual) active input
1683 audio_io_handle_t activeInput = mInputs.getActiveInput();
1684 if (activeInput != 0 && activeInput != input) {
1685
1686 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1687 // otherwise the active input continues and the new input cannot be started.
1688 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
1689 if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
1690 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
1691 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
1692 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
1693 } else {
1694 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
1695 return INVALID_OPERATION;
1696 }
1697 }
1698 }
1699
1700 // Routing?
1701 mInputRoutes.incRouteActivity(session);
1702#ifdef RECORD_PLAY_CONCURRENCY
1703 mIsInputRequestOnProgress = true;
1704
1705 char getPropValue[PROPERTY_VALUE_MAX];
1706 bool prop_rec_play_enabled = false;
1707
1708 if (property_get("rec.playback.conc.disabled", getPropValue, NULL)) {
1709 prop_rec_play_enabled = atoi(getPropValue) || !strncmp("true", getPropValue, 4);
1710 }
1711
1712 if ((prop_rec_play_enabled) &&(mInputs.activeInputsCount() == 0)){
1713 // send update to HAL on record playback concurrency
1714 AudioParameter param = AudioParameter();
1715 param.add(String8("rec_play_conc_on"), String8("true"));
1716 ALOGD("startInput() setParameters rec_play_conc is setting to ON ");
1717 mpClientInterface->setParameters(0, param.toString());
1718
1719 // Call invalidate to reset all opened non ULL audio tracks
1720 // Move tracks associated to this strategy from previous output to new output
1721 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
1722 // Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder)
1723 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE && (i != AUDIO_STREAM_PATCH)) {
1724 ALOGD("Invalidate on releaseInput for stream :: %d ", i);
1725 //FIXME see fixme on name change
1726 mpClientInterface->invalidateStream((audio_stream_type_t)i);
1727 }
1728 }
1729 // close compress tracks
1730 for (size_t i = 0; i < mOutputs.size(); i++) {
1731 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
1732 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
1733 ALOGD("ouput desc / profile is NULL");
1734 continue;
1735 }
1736 if (outputDesc->mProfile->mFlags
1737 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1738 // close compress sessions
1739 ALOGD("calling closeOutput on record conc for COMPRESS output");
1740 closeOutput(mOutputs.keyAt(i));
1741 }
1742 }
1743 }
1744#endif
1745
1746 if (inputDesc->mRefCount == 0 || mInputRoutes.hasRouteChanged(session)) {
1747 // if input maps to a dynamic policy with an activity listener, notify of state change
1748 if ((inputDesc->mPolicyMix != NULL)
1749 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1750 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1751 MIX_STATE_MIXING);
1752 }
1753
1754 if (mInputs.activeInputsCount() == 0) {
1755 SoundTrigger::setCaptureState(true);
1756 }
1757 setInputDevice(input, getNewInputDevice(input), true /* force */);
1758
1759 // automatically enable the remote submix output when input is started if not
1760 // used by a policy mix of type MIX_TYPE_RECORDERS
1761 // For remote submix (a virtual device), we open only one input per capture request.
1762 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1763 String8 address = String8("");
1764 if (inputDesc->mPolicyMix == NULL) {
1765 address = String8("0");
1766 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1767 address = inputDesc->mPolicyMix->mRegistrationId;
1768 }
1769 if (address != "") {
1770 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1771 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1772 address, "remote-submix");
1773 }
1774 }
1775 }
1776
1777 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1778
1779 inputDesc->mRefCount++;
1780#ifdef RECORD_PLAY_CONCURRENCY
1781 mIsInputRequestOnProgress = false;
1782#endif
1783 return NO_ERROR;
1784}
1785status_t AudioPolicyManagerCustom::stopInput(audio_io_handle_t input,
1786 audio_session_t session)
1787{
1788 status_t status;
1789 status = AudioPolicyManager::stopInput(input, session);
1790#ifdef RECORD_PLAY_CONCURRENCY
1791 char propValue[PROPERTY_VALUE_MAX];
1792 bool prop_rec_play_enabled = false;
1793
1794 if (property_get("rec.playback.conc.disabled", propValue, NULL)) {
1795 prop_rec_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1796 }
1797
1798 if ((prop_rec_play_enabled) && (mInputs.activeInputsCount() == 0)) {
1799
1800 //send update to HAL on record playback concurrency
1801 AudioParameter param = AudioParameter();
1802 param.add(String8("rec_play_conc_on"), String8("false"));
1803 ALOGD("stopInput() setParameters rec_play_conc is setting to OFF ");
1804 mpClientInterface->setParameters(0, param.toString());
1805
1806 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
1807 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
1808 //Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder stop tone)
1809 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE) && (i != AUDIO_STREAM_PATCH)) {
1810 ALOGD(" Invalidate on stopInput for stream :: %d ", i);
1811 //FIXME see fixme on name change
1812 mpClientInterface->invalidateStream((audio_stream_type_t)i);
1813 }
1814 }
1815 }
1816#endif
1817 return status;
1818}
1819
1820AudioPolicyManagerCustom::AudioPolicyManagerCustom(AudioPolicyClientInterface *clientInterface)
1821 : AudioPolicyManager(clientInterface)
1822{
1823#ifdef RECORD_PLAY_CONCURRENCY
1824 mIsInputRequestOnProgress = false;
1825#endif
1826
1827
1828#ifdef VOICE_CONCURRENCY
1829 mFallBackflag = getFallBackPath();
1830#endif
1831}
Sharad Sanglec60f6fa2015-07-27 15:14:23 +05301832audio_devices_t AudioPolicyManagerCustom::getDeviceForStrategy(routing_strategy strategy, bool fromCache)
1833{
1834 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
1835 audio_devices_t device = AUDIO_DEVICE_NONE;
1836 switch (strategy) {
1837 case STRATEGY_SONIFICATION:
1838 case STRATEGY_ENFORCED_AUDIBLE:
1839 case STRATEGY_ACCESSIBILITY:
1840 case STRATEGY_REROUTING:
1841 case STRATEGY_MEDIA:
1842 if (strategy != STRATEGY_SONIFICATION){
1843 // no sonification on WFD sink
1844 device |= availableOutputDeviceTypes & AUDIO_DEVICE_OUT_PROXY;
1845 if (device != AUDIO_DEVICE_NONE) {
1846 ALOGV("Found proxy for strategy %d", strategy);
1847 return device;
1848 }
1849 }
1850 break;
1851 default:
1852 ALOGV("getDeviceForStrategy() unknown strategy: %d", strategy);
1853 break;
1854 }
1855 device = AudioPolicyManager::getDeviceForStrategy(strategy, fromCache);
1856 return device;
1857}
1858
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001859}