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