blob: a2a62c66b2878cbc549eac651e89d73300ddb514 [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}
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001130audio_io_handle_t AudioPolicyManagerCustom::getOutputForDevice(
1131 audio_devices_t device,
Sharad Sangle36781612015-05-28 16:15:16 +05301132 audio_session_t session __unused,
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001133 audio_stream_type_t stream,
1134 uint32_t samplingRate,
1135 audio_format_t format,
1136 audio_channel_mask_t channelMask,
1137 audio_output_flags_t flags,
1138 const audio_offload_info_t *offloadInfo)
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001139{
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001140 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
1141 uint32_t latency = 0;
1142 status_t status;
Mingming Yin0ae14ea2014-07-09 17:55:56 -07001143
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001144#ifdef AUDIO_POLICY_TEST
1145 if (mCurOutput != 0) {
1146 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
1147 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
1148
1149 if (mTestOutputs[mCurOutput] == 0) {
1150 ALOGV("getOutput() opening test output");
Sharad Sangle36781612015-05-28 16:15:16 +05301151 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
1152 mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001153 outputDesc->mDevice = mTestDevice;
1154 outputDesc->mLatency = mTestLatencyMs;
1155 outputDesc->mFlags =
1156 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
1157 outputDesc->mRefCount[stream] = 0;
1158 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1159 config.sample_rate = mTestSamplingRate;
1160 config.channel_mask = mTestChannels;
1161 config.format = mTestFormat;
1162 if (offloadInfo != NULL) {
1163 config.offload_info = *offloadInfo;
1164 }
1165 status = mpClientInterface->openOutput(0,
1166 &mTestOutputs[mCurOutput],
1167 &config,
1168 &outputDesc->mDevice,
1169 String8(""),
1170 &outputDesc->mLatency,
1171 outputDesc->mFlags);
1172 if (status == NO_ERROR) {
1173 outputDesc->mSamplingRate = config.sample_rate;
1174 outputDesc->mFormat = config.format;
1175 outputDesc->mChannelMask = config.channel_mask;
1176 AudioParameter outputCmd = AudioParameter();
1177 outputCmd.addInt(String8("set_id"),mCurOutput);
1178 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
1179 addOutput(mTestOutputs[mCurOutput], outputDesc);
1180 }
1181 }
1182 return mTestOutputs[mCurOutput];
1183 }
1184#endif //AUDIO_POLICY_TEST
Sharad Sangle36781612015-05-28 16:15:16 +05301185 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1186 (stream != AUDIO_STREAM_MUSIC)) {
1187 // compress should not be used for non-music streams
1188 ALOGE("Offloading only allowed with music stream");
1189 return 0;
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301190 }
Karthik Reddy Katta7249d662015-07-14 16:05:18 +05301191
1192 if ((stream == AUDIO_STREAM_VOICE_CALL) &&
1193 (channelMask == 1) &&
1194 (samplingRate == 8000 || samplingRate == 16000)) {
1195 // Allow Voip direct output only if:
1196 // audio mode is MODE_IN_COMMUNCATION; AND
1197 // voip output is not opened already; AND
1198 // requested sample rate matches with that of voip input stream (if opened already)
1199 int value = 0;
1200 uint32_t mode = 0, voipOutCount = 1, voipSampleRate = 1;
1201 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1202 String8("audio_mode"));
1203 AudioParameter result = AudioParameter(valueStr);
1204 if (result.getInt(String8("audio_mode"), value) == NO_ERROR) {
1205 mode = value;
1206 }
1207
1208 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1209 String8("voip_out_stream_count"));
1210 result = AudioParameter(valueStr);
1211 if (result.getInt(String8("voip_out_stream_count"), value) == NO_ERROR) {
1212 voipOutCount = value;
1213 }
1214
1215 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
1216 String8("voip_sample_rate"));
1217 result = AudioParameter(valueStr);
1218 if (result.getInt(String8("voip_sample_rate"), value) == NO_ERROR) {
1219 voipSampleRate = value;
1220 }
1221
1222 if ((mode == AUDIO_MODE_IN_COMMUNICATION) && (voipOutCount == 0) &&
1223 ((voipSampleRate == 0) || (voipSampleRate == samplingRate))) {
1224 if (audio_is_linear_pcm(format)) {
1225 char propValue[PROPERTY_VALUE_MAX] = {0};
1226 property_get("use.voice.path.for.pcm.voip", propValue, "0");
1227 bool voipPcmSysPropEnabled = !strncmp("true", propValue, sizeof("true"));
1228 if (voipPcmSysPropEnabled && (format == AUDIO_FORMAT_PCM_16_BIT)) {
1229 flags = (audio_output_flags_t)((flags &~AUDIO_OUTPUT_FLAG_FAST) |
1230 AUDIO_OUTPUT_FLAG_VOIP_RX | AUDIO_OUTPUT_FLAG_DIRECT);
1231 ALOGD("Set VoIP and Direct output flags for PCM format");
1232 }
1233 }
1234 }
1235 }
1236
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301237#ifdef VOICE_CONCURRENCY
1238 char propValue[PROPERTY_VALUE_MAX];
1239 bool prop_play_enabled=false, prop_voip_enabled = false;
1240
1241 if(property_get("voice.playback.conc.disabled", propValue, NULL)) {
1242 prop_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001243 }
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301244
1245 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1246 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1247 }
1248
1249 if (prop_play_enabled && mvoice_call_state) {
1250 //check if voice call is active / running in background
1251 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1252 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1253 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1254 {
1255 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1256 if(prop_voip_enabled) {
1257 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1258 flags );
1259 return 0;
1260 }
1261 }
1262 else {
1263 if (AUDIO_OUTPUT_FLAG_FAST == mFallBackflag) {
1264 ALOGD("voice_conc:IN call mode adding ULL flags .. flags: %x ", flags );
1265 flags = AUDIO_OUTPUT_FLAG_FAST;
1266 } else if (AUDIO_OUTPUT_FLAG_DEEP_BUFFER == mFallBackflag) {
1267 if (AUDIO_STREAM_MUSIC == stream) {
1268 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1269 ALOGD("voice_conc:IN call mode adding deep-buffer flags %x ", flags );
1270 }
1271 else {
1272 flags = AUDIO_OUTPUT_FLAG_FAST;
1273 ALOGD("voice_conc:IN call mode adding fast flags %x ", flags );
1274 }
1275 }
1276 }
1277 }
1278 } else if (prop_voip_enabled && mvoice_call_state) {
1279 //check if voice call is active / running in background
1280 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1281 //return only ULL ouput
1282 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1283 ((AUDIO_MODE_IN_CALL == mPrevPhoneState)
1284 && (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1285 {
1286 if(AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1287 ALOGD("voice_conc:getoutput:IN call mode return no o/p for VoIP %x",
1288 flags );
1289 return 0;
1290 }
1291 }
1292 }
1293#endif
1294#ifdef RECORD_PLAY_CONCURRENCY
1295 char recConcPropValue[PROPERTY_VALUE_MAX];
1296 bool prop_rec_play_enabled = false;
1297
1298 if (property_get("rec.playback.conc.disabled", recConcPropValue, NULL)) {
1299 prop_rec_play_enabled = atoi(recConcPropValue) || !strncmp("true", recConcPropValue, 4);
1300 }
1301 if ((prop_rec_play_enabled) &&
1302 ((true == mIsInputRequestOnProgress) || (mInputs.activeInputsCount() > 0))) {
1303 if (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState()) {
1304 if (AUDIO_OUTPUT_FLAG_VOIP_RX & flags) {
1305 // allow VoIP using voice path
1306 // Do nothing
1307 } else if((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1308 ALOGD("voice_conc:MODE_IN_COMM is setforcing deep buffer output for non ULL... flags: %x", flags);
1309 // use deep buffer path for all non ULL outputs
1310 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1311 }
1312 } else if ((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
1313 ALOGD("voice_conc:Record mode is on forcing deep buffer output for non ULL... flags: %x ", flags);
1314 // use deep buffer path for all non ULL outputs
1315 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1316 }
1317 }
1318 if (prop_rec_play_enabled &&
1319 (stream == AUDIO_STREAM_ENFORCED_AUDIBLE)) {
1320 ALOGD("Record conc is on forcing ULL output for ENFORCED_AUDIBLE");
1321 flags = AUDIO_OUTPUT_FLAG_FAST;
1322 }
1323#endif
1324
Sharad Sangle36781612015-05-28 16:15:16 +05301325 /*
1326 * WFD audio routes back to target speaker when starting a ringtone playback.
1327 * This is because primary output is reused for ringtone, so output device is
1328 * updated based on SONIFICATION strategy for both ringtone and music playback.
1329 * The same issue is not seen on remoted_submix HAL based WFD audio because
1330 * primary output is not reused and a new output is created for ringtone playback.
1331 * Issue is fixed by updating output flag to AUDIO_OUTPUT_FLAG_FAST when there is
1332 * a non-music stream playback on WFD, so primary output is not reused for ringtone.
1333 */
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001334 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
1335 if ((availableOutputDeviceTypes & AUDIO_DEVICE_OUT_PROXY)
1336 && (stream != AUDIO_STREAM_MUSIC)) {
Sharad Sangle36781612015-05-28 16:15:16 +05301337 ALOGD("WFD audio: use OUTPUT_FLAG_FAST for non music stream. flags:%x", flags );
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001338 //For voip paths
1339 if(flags & AUDIO_OUTPUT_FLAG_DIRECT)
1340 flags = AUDIO_OUTPUT_FLAG_DIRECT;
1341 else //route every thing else to ULL path
1342 flags = AUDIO_OUTPUT_FLAG_FAST;
1343 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001344 // open a direct output if required by specified parameters
1345 //force direct flag if offload flag is set: offloading implies a direct output stream
1346 // and all common behaviors are driven by checking only the direct flag
1347 // this should normally be set appropriately in the policy configuration file
1348 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1349 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1350 }
1351 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1352 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1353 }
Sharad Sangle36781612015-05-28 16:15:16 +05301354 // only allow deep buffering for music stream type
1355 if (stream != AUDIO_STREAM_MUSIC) {
1356 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Sharad Sangle497aef82015-08-03 17:55:48 +05301357 } else if (/* stream == AUDIO_STREAM_MUSIC && */
1358 flags == AUDIO_OUTPUT_FLAG_NONE &&
1359 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
1360 flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Sharad Sangle36781612015-05-28 16:15:16 +05301361 }
Sharad Sangle497aef82015-08-03 17:55:48 +05301362
Sharad Sangle36781612015-05-28 16:15:16 +05301363 if (stream == AUDIO_STREAM_TTS) {
1364 flags = AUDIO_OUTPUT_FLAG_TTS;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001365 }
1366
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301367 // open a direct output if required by specified parameters
1368 //force direct flag if offload flag is set: offloading implies a direct output stream
1369 // and all common behaviors are driven by checking only the direct flag
1370 // this should normally be set appropriately in the policy configuration file
1371 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1372 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1373 }
1374 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1375 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1376 }
1377 // only allow deep buffering for music stream type
1378 if (stream != AUDIO_STREAM_MUSIC) {
1379 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
1380 }
1381 if (stream == AUDIO_STREAM_TTS) {
1382 flags = AUDIO_OUTPUT_FLAG_TTS;
1383 }
1384
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001385 sp<IOProfile> profile;
1386
1387 // skip direct output selection if the request can obviously be attached to a mixed output
1388 // and not explicitly requested
1389 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
1390 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
1391 audio_channel_count_from_out_mask(channelMask) <= 2) {
1392 goto non_direct_output;
1393 }
1394
1395 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1396 // creating an offloaded track and tearing it down immediately after start when audioflinger
1397 // detects there is an active non offloadable effect.
1398 // FIXME: We should check the audio session here but we do not have it in this context.
1399 // This may prevent offloading in rare situations where effects are left active by apps
1400 // in the background.
1401
Sharad Sangle36781612015-05-28 16:15:16 +05301402 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
1403 !mEffects.isNonOffloadableEffectEnabled()) {
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001404 profile = getProfileForDirectOutput(device,
1405 samplingRate,
1406 format,
1407 channelMask,
1408 (audio_output_flags_t)flags);
1409 }
1410
1411 if (profile != 0) {
Sharad Sangle36781612015-05-28 16:15:16 +05301412 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001413
1414 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangle36781612015-05-28 16:15:16 +05301415 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001416 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1417 outputDesc = desc;
1418 // reuse direct output if currently open and configured with same parameters
1419 if ((samplingRate == outputDesc->mSamplingRate) &&
1420 (format == outputDesc->mFormat) &&
1421 (channelMask == outputDesc->mChannelMask)) {
1422 outputDesc->mDirectOpenCount++;
1423 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1424 return mOutputs.keyAt(i);
1425 }
1426 }
1427 }
1428 // close direct output if currently open and configured with different parameters
1429 if (outputDesc != NULL) {
1430 closeOutput(outputDesc->mIoHandle);
1431 }
Sharad Sangle36781612015-05-28 16:15:16 +05301432
1433 // if the selected profile is offloaded and no offload info was specified,
1434 // create a default one
1435 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
1436 if ((profile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
1437 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1438 defaultOffloadInfo.sample_rate = samplingRate;
1439 defaultOffloadInfo.channel_mask = channelMask;
1440 defaultOffloadInfo.format = format;
1441 defaultOffloadInfo.stream_type = stream;
1442 defaultOffloadInfo.bit_rate = 0;
1443 defaultOffloadInfo.duration_us = -1;
1444 defaultOffloadInfo.has_video = true; // conservative
1445 defaultOffloadInfo.is_streaming = true; // likely
1446 offloadInfo = &defaultOffloadInfo;
1447 }
1448
1449 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001450 outputDesc->mDevice = device;
1451 outputDesc->mLatency = 0;
Sharad Sangle36781612015-05-28 16:15:16 +05301452 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001453 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1454 config.sample_rate = samplingRate;
1455 config.channel_mask = channelMask;
1456 config.format = format;
1457 if (offloadInfo != NULL) {
1458 config.offload_info = *offloadInfo;
1459 }
Sharad Sangle36781612015-05-28 16:15:16 +05301460 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001461 &output,
1462 &config,
1463 &outputDesc->mDevice,
1464 String8(""),
1465 &outputDesc->mLatency,
1466 outputDesc->mFlags);
1467
1468 // only accept an output with the requested parameters
1469 if (status != NO_ERROR ||
1470 (samplingRate != 0 && samplingRate != config.sample_rate) ||
1471 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
1472 (channelMask != 0 && channelMask != config.channel_mask)) {
1473 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1474 "format %d %d, channelMask %04x %04x", output, samplingRate,
1475 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1476 outputDesc->mChannelMask);
1477 if (output != AUDIO_IO_HANDLE_NONE) {
1478 mpClientInterface->closeOutput(output);
1479 }
Sharad Sangle36781612015-05-28 16:15:16 +05301480 // fall back to mixer output if possible when the direct output could not be open
1481 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
1482 goto non_direct_output;
1483 }
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001484 return AUDIO_IO_HANDLE_NONE;
1485 }
1486 outputDesc->mSamplingRate = config.sample_rate;
1487 outputDesc->mChannelMask = config.channel_mask;
1488 outputDesc->mFormat = config.format;
1489 outputDesc->mRefCount[stream] = 0;
1490 outputDesc->mStopTime[stream] = 0;
1491 outputDesc->mDirectOpenCount = 1;
1492
1493 audio_io_handle_t srcOutput = getOutputForEffect();
1494 addOutput(output, outputDesc);
1495 audio_io_handle_t dstOutput = getOutputForEffect();
1496 if (dstOutput == output) {
1497 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1498 }
1499 mPreviousOutputs = mOutputs;
1500 ALOGV("getOutput() returns new direct output %d", output);
1501 mpClientInterface->onAudioPortListUpdate();
1502 return output;
1503 }
1504
1505non_direct_output:
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001506 // ignoring channel mask due to downmix capability in mixer
1507
1508 // open a non direct output
1509
1510 // for non direct outputs, only PCM is supported
1511 if (audio_is_linear_pcm(format)) {
1512 // get which output is suitable for the specified stream. The actual
1513 // routing change will happen when startOutput() will be called
1514 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1515
1516 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1517 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1518 output = selectOutput(outputs, flags, format);
1519 }
1520 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1521 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1522
Sharad Sangle36781612015-05-28 16:15:16 +05301523 ALOGV(" getOutputForDevice() returns output %d", output);
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001524
1525 return output;
1526}
Sharad Sanglec5766ff2015-06-04 20:24:10 +05301527
1528status_t AudioPolicyManagerCustom::getInputForAttr(const audio_attributes_t *attr,
1529 audio_io_handle_t *input,
1530 audio_session_t session,
1531 uid_t uid,
1532 uint32_t samplingRate,
1533 audio_format_t format,
1534 audio_channel_mask_t channelMask,
1535 audio_input_flags_t flags,
1536 audio_port_handle_t selectedDeviceId,
1537 input_type_t *inputType)
1538{
1539 audio_source_t inputSource = attr->source;
1540#ifdef VOICE_CONCURRENCY
1541
1542 char propValue[PROPERTY_VALUE_MAX];
1543 bool prop_rec_enabled=false, prop_voip_enabled = false;
1544
1545 if(property_get("voice.record.conc.disabled", propValue, NULL)) {
1546 prop_rec_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1547 }
1548
1549 if(property_get("voice.voip.conc.disabled", propValue, NULL)) {
1550 prop_voip_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1551 }
1552
1553 if (prop_rec_enabled && mvoice_call_state) {
1554 //check if voice call is active / running in background
1555 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1556 //Need to block input request
1557 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1558 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1559 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1560 {
1561 switch(inputSource) {
1562 case AUDIO_SOURCE_VOICE_UPLINK:
1563 case AUDIO_SOURCE_VOICE_DOWNLINK:
1564 case AUDIO_SOURCE_VOICE_CALL:
1565 ALOGD("voice_conc:Creating input during incall mode for inputSource: %d",
1566 inputSource);
1567 break;
1568
1569 case AUDIO_SOURCE_VOICE_COMMUNICATION:
1570 if(prop_voip_enabled) {
1571 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1572 inputSource);
1573 return NO_INIT;
1574 }
1575 break;
1576 default:
1577 ALOGD("voice_conc:BLOCK VoIP requst incall mode for inputSource: %d",
1578 inputSource);
1579 return NO_INIT;
1580 }
1581 }
1582 }//check for VoIP flag
1583 else if(prop_voip_enabled && mvoice_call_state) {
1584 //check if voice call is active / running in background
1585 //some of VoIP apps(like SIP2SIP call) supports resume of VoIP call when call in progress
1586 //Need to block input request
1587 if((AUDIO_MODE_IN_CALL == mEngine->getPhoneState()) ||
1588 ((AUDIO_MODE_IN_CALL == mPrevPhoneState) &&
1589 (AUDIO_MODE_IN_COMMUNICATION == mEngine->getPhoneState())))
1590 {
1591 if(inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1592 ALOGD("BLOCKING VoIP request during incall mode for inputSource: %d ",inputSource);
1593 return NO_INIT;
1594 }
1595 }
1596 }
1597
1598#endif
1599
1600 return AudioPolicyManager::getInputForAttr(attr,
1601 input,
1602 session,
1603 uid,
1604 samplingRate,
1605 format,
1606 channelMask,
1607 flags,
1608 selectedDeviceId,
1609 inputType);
1610}
1611status_t AudioPolicyManagerCustom::startInput(audio_io_handle_t input,
1612 audio_session_t session)
1613{
1614 ALOGV("startInput() input %d", input);
1615 ssize_t index = mInputs.indexOfKey(input);
1616 if (index < 0) {
1617 ALOGW("startInput() unknown input %d", input);
1618 return BAD_VALUE;
1619 }
1620 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1621
1622 index = inputDesc->mSessions.indexOf(session);
1623 if (index < 0) {
1624 ALOGW("startInput() unknown session %d on input %d", session, input);
1625 return BAD_VALUE;
1626 }
1627
1628 // virtual input devices are compatible with other input devices
1629 if (!is_virtual_input_device(inputDesc->mDevice)) {
1630
1631 // for a non-virtual input device, check if there is another (non-virtual) active input
1632 audio_io_handle_t activeInput = mInputs.getActiveInput();
1633 if (activeInput != 0 && activeInput != input) {
1634
1635 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1636 // otherwise the active input continues and the new input cannot be started.
1637 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
1638 if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
1639 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
1640 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
1641 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
1642 } else {
1643 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
1644 return INVALID_OPERATION;
1645 }
1646 }
1647 }
1648
1649 // Routing?
1650 mInputRoutes.incRouteActivity(session);
1651#ifdef RECORD_PLAY_CONCURRENCY
1652 mIsInputRequestOnProgress = true;
1653
1654 char getPropValue[PROPERTY_VALUE_MAX];
1655 bool prop_rec_play_enabled = false;
1656
1657 if (property_get("rec.playback.conc.disabled", getPropValue, NULL)) {
1658 prop_rec_play_enabled = atoi(getPropValue) || !strncmp("true", getPropValue, 4);
1659 }
1660
1661 if ((prop_rec_play_enabled) &&(mInputs.activeInputsCount() == 0)){
1662 // send update to HAL on record playback concurrency
1663 AudioParameter param = AudioParameter();
1664 param.add(String8("rec_play_conc_on"), String8("true"));
1665 ALOGD("startInput() setParameters rec_play_conc is setting to ON ");
1666 mpClientInterface->setParameters(0, param.toString());
1667
1668 // Call invalidate to reset all opened non ULL audio tracks
1669 // Move tracks associated to this strategy from previous output to new output
1670 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
1671 // Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder)
1672 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE && (i != AUDIO_STREAM_PATCH)) {
1673 ALOGD("Invalidate on releaseInput for stream :: %d ", i);
1674 //FIXME see fixme on name change
1675 mpClientInterface->invalidateStream((audio_stream_type_t)i);
1676 }
1677 }
1678 // close compress tracks
1679 for (size_t i = 0; i < mOutputs.size(); i++) {
1680 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
1681 if ((outputDesc == NULL) || (outputDesc->mProfile == NULL)) {
1682 ALOGD("ouput desc / profile is NULL");
1683 continue;
1684 }
1685 if (outputDesc->mProfile->mFlags
1686 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1687 // close compress sessions
1688 ALOGD("calling closeOutput on record conc for COMPRESS output");
1689 closeOutput(mOutputs.keyAt(i));
1690 }
1691 }
1692 }
1693#endif
1694
1695 if (inputDesc->mRefCount == 0 || mInputRoutes.hasRouteChanged(session)) {
1696 // if input maps to a dynamic policy with an activity listener, notify of state change
1697 if ((inputDesc->mPolicyMix != NULL)
1698 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1699 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1700 MIX_STATE_MIXING);
1701 }
1702
1703 if (mInputs.activeInputsCount() == 0) {
1704 SoundTrigger::setCaptureState(true);
1705 }
1706 setInputDevice(input, getNewInputDevice(input), true /* force */);
1707
1708 // automatically enable the remote submix output when input is started if not
1709 // used by a policy mix of type MIX_TYPE_RECORDERS
1710 // For remote submix (a virtual device), we open only one input per capture request.
1711 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1712 String8 address = String8("");
1713 if (inputDesc->mPolicyMix == NULL) {
1714 address = String8("0");
1715 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1716 address = inputDesc->mPolicyMix->mRegistrationId;
1717 }
1718 if (address != "") {
1719 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1720 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1721 address, "remote-submix");
1722 }
1723 }
1724 }
1725
1726 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1727
1728 inputDesc->mRefCount++;
1729#ifdef RECORD_PLAY_CONCURRENCY
1730 mIsInputRequestOnProgress = false;
1731#endif
1732 return NO_ERROR;
1733}
1734status_t AudioPolicyManagerCustom::stopInput(audio_io_handle_t input,
1735 audio_session_t session)
1736{
1737 status_t status;
1738 status = AudioPolicyManager::stopInput(input, session);
1739#ifdef RECORD_PLAY_CONCURRENCY
1740 char propValue[PROPERTY_VALUE_MAX];
1741 bool prop_rec_play_enabled = false;
1742
1743 if (property_get("rec.playback.conc.disabled", propValue, NULL)) {
1744 prop_rec_play_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
1745 }
1746
1747 if ((prop_rec_play_enabled) && (mInputs.activeInputsCount() == 0)) {
1748
1749 //send update to HAL on record playback concurrency
1750 AudioParameter param = AudioParameter();
1751 param.add(String8("rec_play_conc_on"), String8("false"));
1752 ALOGD("stopInput() setParameters rec_play_conc is setting to OFF ");
1753 mpClientInterface->setParameters(0, param.toString());
1754
1755 //call invalidate tracks so that any open streams can fall back to deep buffer/compress path from ULL
1756 for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
1757 //Do not call invalidate for ENFORCED_AUDIBLE (otherwise pops are seen for camcorder stop tone)
1758 if ((i != AUDIO_STREAM_ENFORCED_AUDIBLE) && (i != AUDIO_STREAM_PATCH)) {
1759 ALOGD(" Invalidate on stopInput for stream :: %d ", i);
1760 //FIXME see fixme on name change
1761 mpClientInterface->invalidateStream((audio_stream_type_t)i);
1762 }
1763 }
1764 }
1765#endif
1766 return status;
1767}
1768
1769AudioPolicyManagerCustom::AudioPolicyManagerCustom(AudioPolicyClientInterface *clientInterface)
1770 : AudioPolicyManager(clientInterface)
1771{
1772#ifdef RECORD_PLAY_CONCURRENCY
1773 mIsInputRequestOnProgress = false;
1774#endif
1775
1776
1777#ifdef VOICE_CONCURRENCY
1778 mFallBackflag = getFallBackPath();
1779#endif
1780}
Sharad Sanglec60f6fa2015-07-27 15:14:23 +05301781audio_devices_t AudioPolicyManagerCustom::getDeviceForStrategy(routing_strategy strategy, bool fromCache)
1782{
1783 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
1784 audio_devices_t device = AUDIO_DEVICE_NONE;
1785 switch (strategy) {
1786 case STRATEGY_SONIFICATION:
1787 case STRATEGY_ENFORCED_AUDIBLE:
1788 case STRATEGY_ACCESSIBILITY:
1789 case STRATEGY_REROUTING:
1790 case STRATEGY_MEDIA:
1791 if (strategy != STRATEGY_SONIFICATION){
1792 // no sonification on WFD sink
1793 device |= availableOutputDeviceTypes & AUDIO_DEVICE_OUT_PROXY;
1794 if (device != AUDIO_DEVICE_NONE) {
1795 ALOGV("Found proxy for strategy %d", strategy);
1796 return device;
1797 }
1798 }
1799 break;
1800 default:
1801 ALOGV("getDeviceForStrategy() unknown strategy: %d", strategy);
1802 break;
1803 }
1804 device = AudioPolicyManager::getDeviceForStrategy(strategy, fromCache);
1805 return device;
1806}
1807
Ravi Kumar Alamanda1cf2a592014-10-29 20:31:15 -07001808}