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