blob: 6fc6113791982f6b5f04f923f18f6e54a3c40b14 [file] [log] [blame]
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001/*
Sharad Sangleca67a022015-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 Sangleca67a022015-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 Sangleca67a022015-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
Sharad Sangleca67a022015-05-28 16:15:16 +053035// 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)
39// 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
Sharad Sangleca67a022015-05-28 16:15:16 +053043#include <inttypes.h>
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070044#include <math.h>
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070045
Sharad Sangleca67a022015-05-28 16:15:16 +053046#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"
53#include <policy.h>
54
55namespace android {
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070056
57// ----------------------------------------------------------------------------
58// AudioPolicyInterface implementation
59// ----------------------------------------------------------------------------
Sharad Sangleca67a022015-05-28 16:15:16 +053060extern "C" AudioPolicyInterface* createAudioPolicyManager(
61 AudioPolicyClientInterface *clientInterface)
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070062{
Sharad Sangleca67a022015-05-28 16:15:16 +053063 return new AudioPolicyManagerCustom(clientInterface);
64}
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070065
Sharad Sangleca67a022015-05-28 16:15:16 +053066extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface)
67{
68 delete interface;
69}
70
71status_t AudioPolicyManagerCustom::setDeviceConnectionStateInt(audio_devices_t device,
72 audio_policy_dev_state_t state,
73 const char *device_address,
74 const char *device_name)
75{
76 ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
77 device, state, device_address, device_name);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070078
79 // connect/disconnect only 1 device at a time
80 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
81
Sharad Sangleca67a022015-05-28 16:15:16 +053082 sp<DeviceDescriptor> devDesc =
83 mHwModules.getDeviceDescriptor(device, device_address, device_name);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070084
85 // handle output devices
86 if (audio_is_output_device(device)) {
Sharad Sangleca67a022015-05-28 16:15:16 +053087 SortedVector <audio_io_handle_t> outputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070088
Sharad Sangleca67a022015-05-28 16:15:16 +053089 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070090
91 // save a copy of the opened output descriptors before any output is opened or closed
92 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
93 mPreviousOutputs = mOutputs;
94 switch (state)
95 {
96 // handle output device connection
Sharad Sangleca67a022015-05-28 16:15:16 +053097 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
98 if (index >= 0) {
Ramjee Singhae5f8902015-08-19 20:47:12 +053099#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
100 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
101 if (!strncmp(device_address, "hdmi_spkr", 9)) {
102 mHdmiAudioDisabled = false;
103 } else {
104 mHdmiAudioEvent = true;
105 }
106 }
107#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700108 ALOGW("setDeviceConnectionState() device already connected: %x", device);
109 return INVALID_OPERATION;
110 }
111 ALOGV("setDeviceConnectionState() connecting device %x", device);
112
Sharad Sangleca67a022015-05-28 16:15:16 +0530113 // register new device as available
114 index = mAvailableOutputDevices.add(devDesc);
Ramjee Singhae5f8902015-08-19 20:47:12 +0530115#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
116 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
117 if (!strncmp(device_address, "hdmi_spkr", 9)) {
118 mHdmiAudioDisabled = false;
119 } else {
120 mHdmiAudioEvent = true;
121 }
122 if (mHdmiAudioDisabled || !mHdmiAudioEvent) {
123 mAvailableOutputDevices.remove(devDesc);
124 ALOGW("HDMI sink not connected, do not route audio to HDMI out");
125 return INVALID_OPERATION;
126 }
127 }
128#endif
Sharad Sangleca67a022015-05-28 16:15:16 +0530129 if (index >= 0) {
130 sp<HwModule> module = mHwModules.getModuleForDevice(device);
131 if (module == 0) {
132 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
133 device);
134 mAvailableOutputDevices.remove(devDesc);
135 return INVALID_OPERATION;
136 }
137 mAvailableOutputDevices[index]->attach(module);
138 } else {
139 return NO_MEMORY;
140 }
141
142 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
143 mAvailableOutputDevices.remove(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700144 return INVALID_OPERATION;
145 }
Sharad Sangleca67a022015-05-28 16:15:16 +0530146 // Propagate device availability to Engine
147 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700148
Sharad Sangleca67a022015-05-28 16:15:16 +0530149 // outputs should never be empty here
150 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
151 "checkOutputsForDevice() returned no outputs but status OK");
152 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
153 outputs.size());
154
155 // Send connect to HALs
156 AudioParameter param = AudioParameter(devDesc->mAddress);
157 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
158 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
159
160 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700161 // handle output device disconnection
Sharad Sangleca67a022015-05-28 16:15:16 +0530162 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
163 if (index < 0) {
Ramjee Singhae5f8902015-08-19 20:47:12 +0530164#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
165 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
166 if (!strncmp(device_address, "hdmi_spkr", 9)) {
167 mHdmiAudioDisabled = true;
168 } else {
169 mHdmiAudioEvent = false;
170 }
171 }
172#endif
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700173 ALOGW("setDeviceConnectionState() device not connected: %x", device);
174 return INVALID_OPERATION;
175 }
176
Sharad Sangleca67a022015-05-28 16:15:16 +0530177 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700178
Sharad Sangleca67a022015-05-28 16:15:16 +0530179 // Send Disconnect to HALs
180 AudioParameter param = AudioParameter(devDesc->mAddress);
181 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
182 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
183
184 // remove device from available output devices
185 mAvailableOutputDevices.remove(devDesc);
Ramjee Singhae5f8902015-08-19 20:47:12 +0530186#ifdef AUDIO_EXTN_HDMI_SPK_ENABLED
187 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
188 if (!strncmp(device_address, "hdmi_spkr", 9)) {
189 mHdmiAudioDisabled = true;
190 } else {
191 mHdmiAudioEvent = false;
192 }
193 }
194#endif
Sharad Sangleca67a022015-05-28 16:15:16 +0530195 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
196
197 // Propagate device availability to Engine
198 mEngine->setDeviceConnectionState(devDesc, state);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700199 } break;
200
201 default:
202 ALOGE("setDeviceConnectionState() invalid state: %x", state);
203 return BAD_VALUE;
204 }
205
Sharad Sangleca67a022015-05-28 16:15:16 +0530206 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
207 // output is suspended before any tracks are moved to it
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700208 checkA2dpSuspend();
209 checkOutputForAllStrategies();
210 // outputs must be closed after checkOutputForAllStrategies() is executed
211 if (!outputs.isEmpty()) {
212 for (size_t i = 0; i < outputs.size(); i++) {
Sharad Sangleca67a022015-05-28 16:15:16 +0530213 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700214 // close unused outputs after device disconnection or direct outputs that have been
215 // opened by checkOutputsForDevice() to query dynamic parameters
Sharad Sangleca67a022015-05-28 16:15:16 +0530216 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700217 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
218 (desc->mDirectOpenCount == 0))) {
219 closeOutput(outputs[i]);
220 }
221 }
Sharad Sangleca67a022015-05-28 16:15:16 +0530222 // check again after closing A2DP output to reset mA2dpSuspended if needed
223 checkA2dpSuspend();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700224 }
225
226 updateDevicesAndOutputs();
Sharad Sangleca67a022015-05-28 16:15:16 +0530227 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
228 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
229 updateCallRouting(newDevice);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700230 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700231 for (size_t i = 0; i < mOutputs.size(); i++) {
Sharad Sangleca67a022015-05-28 16:15:16 +0530232 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
233 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
234 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
235 // do not force device change on duplicated output because if device is 0, it will
236 // also force a device 0 for the two outputs it is duplicated to which may override
237 // a valid device selection on those outputs.
238 bool force = !desc->isDuplicated()
239 && (!device_distinguishes_on_address(device)
240 // always force when disconnecting (a non-duplicated device)
241 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
242 setOutputDevice(desc, newDevice, force, 0);
Satya Krishna Pindiproli5cf8c1a2014-05-08 12:22:16 +0530243 }
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700244 }
245
Sharad Sangleca67a022015-05-28 16:15:16 +0530246 mpClientInterface->onAudioPortListUpdate();
247 return NO_ERROR;
248 } // end if is output device
249
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700250 // handle input devices
251 if (audio_is_input_device(device)) {
Sharad Sangleca67a022015-05-28 16:15:16 +0530252 SortedVector <audio_io_handle_t> inputs;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700253
Sharad Sangleca67a022015-05-28 16:15:16 +0530254 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700255 switch (state)
256 {
257 // handle input device connection
Sharad Sangleca67a022015-05-28 16:15:16 +0530258 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
259 if (index >= 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700260 ALOGW("setDeviceConnectionState() device already connected: %d", device);
261 return INVALID_OPERATION;
262 }
Sharad Sangleca67a022015-05-28 16:15:16 +0530263 sp<HwModule> module = mHwModules.getModuleForDevice(device);
264 if (module == NULL) {
265 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
266 device);
267 return INVALID_OPERATION;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700268 }
Sharad Sangleca67a022015-05-28 16:15:16 +0530269 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
270 return INVALID_OPERATION;
271 }
272
273 index = mAvailableInputDevices.add(devDesc);
274 if (index >= 0) {
275 mAvailableInputDevices[index]->attach(module);
276 } else {
277 return NO_MEMORY;
278 }
279
280 // Set connect to HALs
281 AudioParameter param = AudioParameter(devDesc->mAddress);
282 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
283 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
284
285 // Propagate device availability to Engine
286 mEngine->setDeviceConnectionState(devDesc, state);
287 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700288
289 // handle input device disconnection
Sharad Sangleca67a022015-05-28 16:15:16 +0530290 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
291 if (index < 0) {
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700292 ALOGW("setDeviceConnectionState() device not connected: %d", device);
293 return INVALID_OPERATION;
294 }
Sharad Sangleca67a022015-05-28 16:15:16 +0530295
296 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
297
298 // Set Disconnect to HALs
299 AudioParameter param = AudioParameter(devDesc->mAddress);
300 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
301 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
302
303 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
304 mAvailableInputDevices.remove(devDesc);
305
306 // Propagate device availability to Engine
307 mEngine->setDeviceConnectionState(devDesc, state);
308 } break;
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700309
310 default:
311 ALOGE("setDeviceConnectionState() invalid state: %x", state);
312 return BAD_VALUE;
313 }
314
Sharad Sangleca67a022015-05-28 16:15:16 +0530315 closeAllInputs();
316
317 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
318 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
319 updateCallRouting(newDevice);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700320 }
321
Sharad Sangleca67a022015-05-28 16:15:16 +0530322 mpClientInterface->onAudioPortListUpdate();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700323 return NO_ERROR;
Sharad Sangleca67a022015-05-28 16:15:16 +0530324 } // end if is input device
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700325
326 ALOGW("setDeviceConnectionState() invalid device: %x", device);
327 return BAD_VALUE;
328}
ApurupaPattapu0c566872014-01-10 14:46:02 -0800329// This function checks for the parameters which can be offloaded.
330// This can be enhanced depending on the capability of the DSP and policy
331// of the system.
Sharad Sangleca67a022015-05-28 16:15:16 +0530332bool AudioPolicyManagerCustom::isOffloadSupported(const audio_offload_info_t& offloadInfo)
ApurupaPattapu0c566872014-01-10 14:46:02 -0800333{
Sharad Sangleca67a022015-05-28 16:15:16 +0530334 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
335 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
ApurupaPattapu0c566872014-01-10 14:46:02 -0800336 offloadInfo.sample_rate, offloadInfo.channel_mask,
337 offloadInfo.format,
338 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
339 offloadInfo.has_video);
340
Sharad Sangleca67a022015-05-28 16:15:16 +0530341 // Check if offload has been disabled
342 char propValue[PROPERTY_VALUE_MAX];
343 if (property_get("audio.offload.disable", propValue, "0")) {
344 if (atoi(propValue) != 0) {
345 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
346 return false;
347 }
ApurupaPattapu0c566872014-01-10 14:46:02 -0800348 }
Sharad Sangleca67a022015-05-28 16:15:16 +0530349
ApurupaPattapu0c566872014-01-10 14:46:02 -0800350 // Check if stream type is music, then only allow offload as of now.
351 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
352 {
353 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
354 return false;
355 }
Sharad Sangleca67a022015-05-28 16:15:16 +0530356 //check if it's multi-channel AAC (includes sub formats) and FLAC format
357 if ((popcount(offloadInfo.channel_mask) > 2) &&
358 (((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
359 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC)||
360 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS))) {
361 ALOGD("offload disabled for multi-channel AAC,FLAC and VORBIS format");
362 return false;
ApurupaPattapu0c566872014-01-10 14:46:02 -0800363 }
364
Sharad Sangleca67a022015-05-28 16:15:16 +0530365 //TODO: enable audio offloading with video when ready
366 const bool allowOffloadWithVideo =
367 property_get_bool("audio.offload.video", false /* default_value */);
368 if (offloadInfo.has_video && !allowOffloadWithVideo) {
369 ALOGV("isOffloadSupported: has_video == true, returning false");
370 return false;
ApurupaPattapu0c566872014-01-10 14:46:02 -0800371 }
372
373 //If duration is less than minimum value defined in property, return false
374 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
375 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
376 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
377 return false;
378 }
379 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
380 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
Sharad Sangleca67a022015-05-28 16:15:16 +0530381 //duration checks only valid for MP3/AAC/ formats,
ApurupaPattapu0c566872014-01-10 14:46:02 -0800382 //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
Sharad Sangleca67a022015-05-28 16:15:16 +0530383 if ((offloadInfo.format == AUDIO_FORMAT_MP3) ||
384 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) ||
385 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_FLAC) ||
386 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_VORBIS) ||
387 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA) ||
388 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_WMA_PRO) ||
389 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_ALAC) ||
390 ((offloadInfo.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_APE))
ApurupaPattapu0c566872014-01-10 14:46:02 -0800391 return false;
Sharad Sangleca67a022015-05-28 16:15:16 +0530392
ApurupaPattapu0c566872014-01-10 14:46:02 -0800393 }
394
395 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
396 // creating an offloaded track and tearing it down immediately after start when audioflinger
397 // detects there is an active non offloadable effect.
398 // FIXME: We should check the audio session here but we do not have it in this context.
399 // This may prevent offloading in rare situations where effects are left active by apps
400 // in the background.
Sharad Sangleca67a022015-05-28 16:15:16 +0530401 if (mEffects.isNonOffloadableEffectEnabled()) {
ApurupaPattapu0c566872014-01-10 14:46:02 -0800402 return false;
403 }
Sharad Sangleca67a022015-05-28 16:15:16 +0530404 // Check for soundcard status
405 String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
406 String8("SND_CARD_STATUS"));
407 AudioParameter result = AudioParameter(valueStr);
408 int isonline = 0;
409 if ((result.getInt(String8("SND_CARD_STATUS"), isonline) == NO_ERROR)
410 && !isonline) {
411 ALOGD("copl: soundcard is offline rejecting offload request");
412 return false;
413 }
ApurupaPattapu0c566872014-01-10 14:46:02 -0800414 // See if there is a profile to support this.
415 // AUDIO_DEVICE_NONE
Sharad Sangleca67a022015-05-28 16:15:16 +0530416 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
ApurupaPattapu0c566872014-01-10 14:46:02 -0800417 offloadInfo.sample_rate,
418 offloadInfo.format,
419 offloadInfo.channel_mask,
420 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Sharad Sangleca67a022015-05-28 16:15:16 +0530421 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
422 return (profile != 0);
ApurupaPattapu0c566872014-01-10 14:46:02 -0800423}
Sharad Sangleca67a022015-05-28 16:15:16 +0530424audio_devices_t AudioPolicyManagerCustom::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
425 bool fromCache)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700426{
Sharad Sangleca67a022015-05-28 16:15:16 +0530427 audio_devices_t device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700428
Sharad Sangleca67a022015-05-28 16:15:16 +0530429 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
430 if (index >= 0) {
431 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
432 if (patchDesc->mUid != mUidCached) {
433 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
434 outputDesc->device(), outputDesc->mPatchHandle);
435 return outputDesc->device();
436 }
437 }
438
439 // check the following by order of priority to request a routing change if necessary:
440 // 1: the strategy enforced audible is active and enforced on the output:
441 // use device for strategy enforced audible
442 // 2: we are in call or the strategy phone is active on the output:
443 // use device for strategy phone
444 // 3: the strategy for enforced audible is active but not enforced on the output:
445 // use the device for strategy enforced audible
446 // 4: the strategy sonification is active on the output:
447 // use device for strategy sonification
448 // 5: the strategy "respectful" sonification is active on the output:
449 // use device for strategy "respectful" sonification
450 // 6: the strategy accessibility is active on the output:
451 // use device for strategy accessibility
452 // 7: the strategy media is active on the output:
453 // use device for strategy media
454 // 8: the strategy DTMF is active on the output:
455 // use device for strategy DTMF
456 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
457 // use device for strategy t-t-s
458 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
459 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
460 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
461 } else if (isInCall() ||
462 isStrategyActive(outputDesc, STRATEGY_PHONE)||
463 isStrategyActive(mPrimaryOutput, STRATEGY_PHONE)) {
464 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
465 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
466 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
467 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)||
468 (isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION)
469 && (!isStrategyActive(mPrimaryOutput,STRATEGY_MEDIA)))) {
470 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
471 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)||
472 (isStrategyActive(mPrimaryOutput,STRATEGY_SONIFICATION_RESPECTFUL)
473 && (!isStrategyActive(mPrimaryOutput, STRATEGY_MEDIA)))) {
474 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
475 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
476 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
477 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
478 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
479 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
480 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
481 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
482 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
483 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
484 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
485 }
486
487 ALOGV("getNewOutputDevice() selected device %x", device);
488 return device;
489}
490void AudioPolicyManagerCustom::setPhoneState(audio_mode_t state)
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700491{
Sharad Sangleca67a022015-05-28 16:15:16 +0530492 ALOGV("setPhoneState() state %d", state);
493 // store previous phone state for management of sonification strategy below
Ramjee Singhae5f8902015-08-19 20:47:12 +0530494 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Sharad Sangleca67a022015-05-28 16:15:16 +0530495 int oldState = mEngine->getPhoneState();
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700496
Sharad Sangleca67a022015-05-28 16:15:16 +0530497 if (mEngine->setPhoneState(state) != NO_ERROR) {
498 ALOGW("setPhoneState() invalid or same state %d", state);
499 return;
500 }
501 /// Opens: can these line be executed after the switch of volume curves???
502 // if leaving call state, handle special case of active streams
503 // pertaining to sonification strategy see handleIncallSonification()
504 if (isInCall()) {
505 ALOGV("setPhoneState() in call state management: new state is %d", state);
506 for (size_t j = 0; j < mOutputs.size(); j++) {
507 audio_io_handle_t curOutput = mOutputs.keyAt(j);
508 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
509 if (stream == AUDIO_STREAM_PATCH) {
510 continue;
511 }
512
513 handleIncallSonification((audio_stream_type_t)stream, false, true, curOutput);
514 }
515 }
516
517 // force reevaluating accessibility routing when call starts
518 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
519 }
520
521 /**
522 * Switching to or from incall state or switching between telephony and VoIP lead to force
523 * routing command.
524 */
525 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
526 || (is_state_in_call(state) && (state != oldState)));
527
528 // check for device and output changes triggered by new phone state
529 checkA2dpSuspend();
530 checkOutputForAllStrategies();
531 updateDevicesAndOutputs();
532
533 sp<SwAudioOutputDescriptor> hwOutputDesc = mPrimaryOutput;
534
535 int delayMs = 0;
536 if (isStateInCall(state)) {
537 nsecs_t sysTime = systemTime();
538 for (size_t i = 0; i < mOutputs.size(); i++) {
539 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
540 // mute media and sonification strategies and delay device switch by the largest
541 // latency of any output where either strategy is active.
542 // This avoid sending the ring tone or music tail into the earpiece or headset.
543 if ((isStrategyActive(desc, STRATEGY_MEDIA,
544 SONIFICATION_HEADSET_MUSIC_DELAY,
545 sysTime) ||
546 isStrategyActive(desc, STRATEGY_SONIFICATION,
547 SONIFICATION_HEADSET_MUSIC_DELAY,
548 sysTime)) &&
549 (delayMs < (int)desc->latency()*2)) {
550 delayMs = desc->latency()*2;
551 }
552 setStrategyMute(STRATEGY_MEDIA, true, desc);
553 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
554 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
555 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
556 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
557 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
558 }
559 ALOGV("Setting the delay from %dms to %dms", delayMs,
560 MIN(delayMs, MAX_VOICE_CALL_START_DELAY_MS));
561 delayMs = MIN(delayMs, MAX_VOICE_CALL_START_DELAY_MS);
562 }
563
564 if (hasPrimaryOutput()) {
565 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
566 // the device returned is not necessarily reachable via this output
567 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
568 // force routing command to audio hardware when ending call
569 // even if no device change is needed
570 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
571 rxDevice = mPrimaryOutput->device();
572 }
573
574 if (state == AUDIO_MODE_IN_CALL) {
575 updateCallRouting(rxDevice, delayMs);
576 } else if (oldState == AUDIO_MODE_IN_CALL) {
577 if (mCallRxPatch != 0) {
578 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
579 mCallRxPatch.clear();
580 }
581 if (mCallTxPatch != 0) {
582 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
583 mCallTxPatch.clear();
584 }
585 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
586 } else {
587 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
588 }
589 }
Ramjee Singhae5f8902015-08-19 20:47:12 +0530590 //update device for all non-primary outputs
591 for (size_t i = 0; i < mOutputs.size(); i++) {
592 audio_io_handle_t output = mOutputs.keyAt(i);
593 if (output != mPrimaryOutput->mIoHandle) {
594 newDevice = getNewOutputDevice(mOutputs.valueFor(output), false /*fromCache*/);
595 setOutputDevice(mOutputs.valueFor(output), newDevice, (newDevice != AUDIO_DEVICE_NONE));
596 }
597 }
Sharad Sangleca67a022015-05-28 16:15:16 +0530598 // if entering in call state, handle special case of active streams
599 // pertaining to sonification strategy see handleIncallSonification()
600 if (isStateInCall(state)) {
601 ALOGV("setPhoneState() in call state management: new state is %d", state);
602 for (size_t j = 0; j < mOutputs.size(); j++) {
603 audio_io_handle_t curOutput = mOutputs.keyAt(j);
604 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
605 if (stream == AUDIO_STREAM_PATCH) {
606 continue;
607 }
608 handleIncallSonification((audio_stream_type_t)stream, true, true, curOutput);
609 }
610 }
611 }
612
613 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
614 if (state == AUDIO_MODE_RINGTONE &&
615 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
616 mLimitRingtoneVolume = true;
617 } else {
618 mLimitRingtoneVolume = false;
619 }
620}
Ramjee Singh5b022de2015-09-15 18:25:20 +0530621status_t AudioPolicyManagerCustom::stopSource(sp<AudioOutputDescriptor> outputDesc1,
Sharad Sangleca67a022015-05-28 16:15:16 +0530622 audio_stream_type_t stream,
623 bool forceDeviceUpdate)
624{
625 // always handle stream stop, check which stream type is stopping
Ramjee Singh5b022de2015-09-15 18:25:20 +0530626 sp<SwAudioOutputDescriptor> outputDesc = (sp<SwAudioOutputDescriptor>) outputDesc1;
Sharad Sangleca67a022015-05-28 16:15:16 +0530627 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
628
629 // handle special case for sonification while in call
Ramjee Singhae5f8902015-08-19 20:47:12 +0530630 if (isInCall() && (outputDesc->mRefCount[stream] == 1)) {
Sharad Sangleca67a022015-05-28 16:15:16 +0530631 if (outputDesc->isDuplicated()) {
Ramjee Singhae5f8902015-08-19 20:47:12 +0530632 handleIncallSonification(stream, false, false, outputDesc->mOutput1->mIoHandle);
633 handleIncallSonification(stream, false, false, outputDesc->mOutput2->mIoHandle);
Sharad Sangleca67a022015-05-28 16:15:16 +0530634 }
635 handleIncallSonification(stream, false, false, outputDesc->mIoHandle);
636 }
637
638 if (outputDesc->mRefCount[stream] > 0) {
639 // decrement usage count of this stream on the output
640 outputDesc->changeRefCount(stream, -1);
641
642 // store time at which the stream was stopped - see isStreamActive()
643 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
644 outputDesc->mStopTime[stream] = systemTime();
645 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
646 // delay the device switch by twice the latency because stopOutput() is executed when
647 // the track stop() command is received and at that time the audio track buffer can
648 // still contain data that needs to be drained. The latency only covers the audio HAL
649 // and kernel buffers. Also the latency does not always include additional delay in the
650 // audio path (audio DSP, CODEC ...)
651 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
652
653 // force restoring the device selection on other active outputs if it differs from the
654 // one being selected for this output
655 for (size_t i = 0; i < mOutputs.size(); i++) {
656 audio_io_handle_t curOutput = mOutputs.keyAt(i);
657 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
658 if (desc != outputDesc &&
659 desc->isActive() &&
660 outputDesc->sharesHwModuleWith(desc) &&
661 (newDevice != desc->device())) {
Ramjee Singhae5f8902015-08-19 20:47:12 +0530662 audio_devices_t dev = getNewOutputDevice(mOutputs.valueFor(curOutput), false
663 /*fromCache*/);
664 setOutputDevice(desc,
665 dev,
Sharad Sangleca67a022015-05-28 16:15:16 +0530666 true,
667 outputDesc->latency()*2);
668 }
669 }
670 // update the outputs if stopping one with a stream that can affect notification routing
671 handleNotificationRoutingForStream(stream);
672 }
673 return NO_ERROR;
674 } else {
675 ALOGW("stopOutput() refcount is already 0");
676 return INVALID_OPERATION;
677 }
678}
Ramjee Singh5b022de2015-09-15 18:25:20 +0530679status_t AudioPolicyManagerCustom::startSource(sp<AudioOutputDescriptor> outputDesc1,
Sharad Sangleca67a022015-05-28 16:15:16 +0530680 audio_stream_type_t stream,
681 audio_devices_t device,
682 uint32_t *delayMs)
683{
684 // cannot start playback of STREAM_TTS if any other output is being used
685 uint32_t beaconMuteLatency = 0;
Ramjee Singh5b022de2015-09-15 18:25:20 +0530686 sp<SwAudioOutputDescriptor> outputDesc = (sp<SwAudioOutputDescriptor>) outputDesc1;
Sharad Sangleca67a022015-05-28 16:15:16 +0530687
688 *delayMs = 0;
689 if (stream == AUDIO_STREAM_TTS) {
690 ALOGV("\t found BEACON stream");
691 if (mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
692 return INVALID_OPERATION;
693 } else {
694 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
695 }
696 } else {
697 // some playback other than beacon starts
698 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
699 }
700
701 // increment usage count for this stream on the requested output:
702 // NOTE that the usage count is the same for duplicated output and hardware output which is
703 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
704 outputDesc->changeRefCount(stream, 1);
705
706 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
707 // starting an output being rerouted?
708 if (device == AUDIO_DEVICE_NONE) {
709 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
710 }
711 routing_strategy strategy = getStrategy(stream);
712 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
713 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
714 (beaconMuteLatency > 0);
715 uint32_t waitMs = beaconMuteLatency;
716 bool force = false;
717 for (size_t i = 0; i < mOutputs.size(); i++) {
718 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
719 if (desc != outputDesc) {
720 // force a device change if any other output is managed by the same hw
721 // module and has a current device selection that differs from selected device.
722 // In this case, the audio HAL must receive the new device selection so that it can
723 // change the device currently selected by the other active output.
724 if (outputDesc->sharesHwModuleWith(desc) &&
725 desc->device() != device) {
726 force = true;
727 }
728 // wait for audio on other active outputs to be presented when starting
729 // a notification so that audio focus effect can propagate, or that a mute/unmute
730 // event occurred for beacon
731 uint32_t latency = desc->latency();
732 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
733 waitMs = latency;
734 }
735 }
736 }
737 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force);
738
739 // handle special case for sonification while in call
740 if (isInCall()) {
741 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
742 }
743
744 // apply volume rules for current stream and device if necessary
745 checkAndSetVolume(stream,
746 mStreams.valueFor(stream).getVolumeIndex(device),
747 outputDesc,
748 device);
749
750 // update the outputs if starting an output with a stream that can affect notification
751 // routing
752 handleNotificationRoutingForStream(stream);
753
754 // force reevaluating accessibility routing when ringtone or alarm starts
755 if (strategy == STRATEGY_SONIFICATION) {
756 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
757 }
758 }
759 else {
760 // handle special case for sonification while in call
761 if (isInCall()) {
762 handleIncallSonification(stream, true, false, outputDesc->mIoHandle);
763 }
764 }
765 return NO_ERROR;
766}
767void AudioPolicyManagerCustom::handleIncallSonification(audio_stream_type_t stream,
768 bool starting, bool stateChange,
769 audio_io_handle_t output)
770{
771 if(!hasPrimaryOutput()) {
772 return;
773 }
774 // no action needed for AUDIO_STREAM_PATCH stream type, it's for internal flinger tracks
775 if (stream == AUDIO_STREAM_PATCH) {
776 return;
777 }
778 // if the stream pertains to sonification strategy and we are in call we must
779 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
780 // in the device used for phone strategy and play the tone if the selected device does not
781 // interfere with the device used for phone strategy
782 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
783 // many times as there are active tracks on the output
784 const routing_strategy stream_strategy = getStrategy(stream);
785 if ((stream_strategy == STRATEGY_SONIFICATION) ||
786 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
787 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
788 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
789 stream, starting, outputDesc->mDevice, stateChange);
790 if (outputDesc->mRefCount[stream]) {
791 int muteCount = 1;
792 if (stateChange) {
793 muteCount = outputDesc->mRefCount[stream];
794 }
795 if (audio_is_low_visibility(stream)) {
796 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
797 for (int i = 0; i < muteCount; i++) {
798 setStreamMute(stream, starting, outputDesc);
799 }
800 } else {
801 ALOGV("handleIncallSonification() high visibility");
802 if (outputDesc->device() &
803 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
804 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
805 for (int i = 0; i < muteCount; i++) {
806 setStreamMute(stream, starting, outputDesc);
807 }
808 }
809 if (starting) {
810 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
811 AUDIO_STREAM_VOICE_CALL);
812 } else {
813 mpClientInterface->stopTone();
814 }
815 }
816 }
817 }
818}
819void AudioPolicyManagerCustom::handleNotificationRoutingForStream(audio_stream_type_t stream) {
820 switch(stream) {
821 case AUDIO_STREAM_MUSIC:
822 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
823 updateDevicesAndOutputs();
824 break;
825 default:
826 break;
827 }
828}
829status_t AudioPolicyManagerCustom::checkAndSetVolume(audio_stream_type_t stream,
830 int index,
831 const sp<SwAudioOutputDescriptor>& outputDesc,
832 audio_devices_t device,
833 int delayMs, bool force)
834{
835 // do not change actual stream volume if the stream is muted
836 if (outputDesc->mMuteCount[stream] != 0) {
837 ALOGVV("checkAndSetVolume() stream %d muted count %d",
838 stream, outputDesc->mMuteCount[stream]);
839 return NO_ERROR;
840 }
841 audio_policy_forced_cfg_t forceUseForComm =
842 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
843 // do not change in call volume if bluetooth is connected and vice versa
844 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
845 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
846 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
847 stream, forceUseForComm);
848 return INVALID_OPERATION;
849 }
850
851 if (device == AUDIO_DEVICE_NONE) {
852 device = outputDesc->device();
853 }
854
855 float volumeDb = computeVolume(stream, index, device);
856 if (outputDesc->isFixedVolume(device)) {
857 volumeDb = 0.0f;
858 }
859
860 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
861
862 if (stream == AUDIO_STREAM_VOICE_CALL ||
863 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
864 float voiceVolume;
865 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
866 if (stream == AUDIO_STREAM_VOICE_CALL) {
867 voiceVolume = (float)index/(float)mStreams.valueFor(stream).getVolumeIndexMax();
868 } else {
869 voiceVolume = 1.0;
870 }
871
872 if (voiceVolume != mLastVoiceVolume && ((outputDesc == mPrimaryOutput) ||
873 isDirectOutput(outputDesc->mIoHandle) || device & AUDIO_DEVICE_OUT_ALL_USB)) {
874 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
875 mLastVoiceVolume = voiceVolume;
876 }
877 }
878
879 return NO_ERROR;
880}
881bool AudioPolicyManagerCustom::isDirectOutput(audio_io_handle_t output) {
882 for (size_t i = 0; i < mOutputs.size(); i++) {
883 audio_io_handle_t curOutput = mOutputs.keyAt(i);
884 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
885 if ((curOutput == output) && (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
886 return true;
887 }
888 }
889 return false;
890}
891audio_io_handle_t AudioPolicyManagerCustom::getOutputForDevice(
892 audio_devices_t device,
893 audio_session_t session __unused,
894 audio_stream_type_t stream,
895 uint32_t samplingRate,
896 audio_format_t format,
897 audio_channel_mask_t channelMask,
898 audio_output_flags_t flags,
899 const audio_offload_info_t *offloadInfo)
900{
901 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
902 uint32_t latency = 0;
903 status_t status;
904
905#ifdef AUDIO_POLICY_TEST
906 if (mCurOutput != 0) {
907 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
908 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
909
910 if (mTestOutputs[mCurOutput] == 0) {
911 ALOGV("getOutput() opening test output");
912 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
913 mpClientInterface);
914 outputDesc->mDevice = mTestDevice;
915 outputDesc->mLatency = mTestLatencyMs;
916 outputDesc->mFlags =
917 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
918 outputDesc->mRefCount[stream] = 0;
919 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
920 config.sample_rate = mTestSamplingRate;
921 config.channel_mask = mTestChannels;
922 config.format = mTestFormat;
923 if (offloadInfo != NULL) {
924 config.offload_info = *offloadInfo;
925 }
926 status = mpClientInterface->openOutput(0,
927 &mTestOutputs[mCurOutput],
928 &config,
929 &outputDesc->mDevice,
930 String8(""),
931 &outputDesc->mLatency,
932 outputDesc->mFlags);
933 if (status == NO_ERROR) {
934 outputDesc->mSamplingRate = config.sample_rate;
935 outputDesc->mFormat = config.format;
936 outputDesc->mChannelMask = config.channel_mask;
937 AudioParameter outputCmd = AudioParameter();
938 outputCmd.addInt(String8("set_id"),mCurOutput);
939 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
940 addOutput(mTestOutputs[mCurOutput], outputDesc);
941 }
942 }
943 return mTestOutputs[mCurOutput];
944 }
945#endif //AUDIO_POLICY_TEST
946 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
947 (stream != AUDIO_STREAM_MUSIC)) {
948 // compress should not be used for non-music streams
949 ALOGE("Offloading only allowed with music stream");
950 return 0;
951 }
Ramjee Singhae5f8902015-08-19 20:47:12 +0530952#ifdef AUDIO_EXTN_AFE_PROXY_ENABLED
Sharad Sangleca67a022015-05-28 16:15:16 +0530953 /*
954 * WFD audio routes back to target speaker when starting a ringtone playback.
955 * This is because primary output is reused for ringtone, so output device is
956 * updated based on SONIFICATION strategy for both ringtone and music playback.
957 * The same issue is not seen on remoted_submix HAL based WFD audio because
958 * primary output is not reused and a new output is created for ringtone playback.
959 * Issue is fixed by updating output flag to AUDIO_OUTPUT_FLAG_FAST when there is
960 * a non-music stream playback on WFD, so primary output is not reused for ringtone.
961 */
962 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
963 if ((availableOutputDeviceTypes & AUDIO_DEVICE_OUT_PROXY)
964 && (stream != AUDIO_STREAM_MUSIC)) {
965 ALOGD("WFD audio: use OUTPUT_FLAG_FAST for non music stream. flags:%x", flags );
966 //For voip paths
967 if(flags & AUDIO_OUTPUT_FLAG_DIRECT)
968 flags = AUDIO_OUTPUT_FLAG_DIRECT;
969 else //route every thing else to ULL path
970 flags = AUDIO_OUTPUT_FLAG_FAST;
971 }
Ramjee Singhae5f8902015-08-19 20:47:12 +0530972#endif
Sharad Sangleca67a022015-05-28 16:15:16 +0530973 // open a direct output if required by specified parameters
974 //force direct flag if offload flag is set: offloading implies a direct output stream
975 // and all common behaviors are driven by checking only the direct flag
976 // this should normally be set appropriately in the policy configuration file
977 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
978 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
979 }
980 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
981 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
982 }
983 // only allow deep buffering for music stream type
984 if (stream != AUDIO_STREAM_MUSIC) {
985 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
986 }
987 if (stream == AUDIO_STREAM_TTS) {
988 flags = AUDIO_OUTPUT_FLAG_TTS;
989 }
990
991 sp<IOProfile> profile;
992
993 // skip direct output selection if the request can obviously be attached to a mixed output
994 // and not explicitly requested
995 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
996 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
997 audio_channel_count_from_out_mask(channelMask) <= 2) {
998 goto non_direct_output;
999 }
1000
1001 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1002 // creating an offloaded track and tearing it down immediately after start when audioflinger
1003 // detects there is an active non offloadable effect.
1004 // FIXME: We should check the audio session here but we do not have it in this context.
1005 // This may prevent offloading in rare situations where effects are left active by apps
1006 // in the background.
1007
1008 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
1009 !mEffects.isNonOffloadableEffectEnabled()) {
1010 profile = getProfileForDirectOutput(device,
1011 samplingRate,
1012 format,
1013 channelMask,
1014 (audio_output_flags_t)flags);
1015 }
1016
1017 if (profile != 0) {
1018 sp<SwAudioOutputDescriptor> outputDesc = NULL;
1019
1020 for (size_t i = 0; i < mOutputs.size(); i++) {
1021 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1022 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1023 outputDesc = desc;
1024 // reuse direct output if currently open and configured with same parameters
1025 if ((samplingRate == outputDesc->mSamplingRate) &&
1026 (format == outputDesc->mFormat) &&
1027 (channelMask == outputDesc->mChannelMask)) {
1028 outputDesc->mDirectOpenCount++;
1029 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1030 return mOutputs.keyAt(i);
1031 }
1032 }
1033 }
1034 // close direct output if currently open and configured with different parameters
1035 if (outputDesc != NULL) {
1036 closeOutput(outputDesc->mIoHandle);
1037 }
1038
1039 // if the selected profile is offloaded and no offload info was specified,
1040 // create a default one
1041 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
1042 if ((profile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
1043 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1044 defaultOffloadInfo.sample_rate = samplingRate;
1045 defaultOffloadInfo.channel_mask = channelMask;
1046 defaultOffloadInfo.format = format;
1047 defaultOffloadInfo.stream_type = stream;
1048 defaultOffloadInfo.bit_rate = 0;
1049 defaultOffloadInfo.duration_us = -1;
1050 defaultOffloadInfo.has_video = true; // conservative
1051 defaultOffloadInfo.is_streaming = true; // likely
1052 offloadInfo = &defaultOffloadInfo;
1053 }
1054
1055 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
1056 outputDesc->mDevice = device;
1057 outputDesc->mLatency = 0;
1058 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
1059 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1060 config.sample_rate = samplingRate;
1061 config.channel_mask = channelMask;
1062 config.format = format;
1063 if (offloadInfo != NULL) {
1064 config.offload_info = *offloadInfo;
1065 }
1066 status = mpClientInterface->openOutput(profile->getModuleHandle(),
1067 &output,
1068 &config,
1069 &outputDesc->mDevice,
1070 String8(""),
1071 &outputDesc->mLatency,
1072 outputDesc->mFlags);
1073
1074 // only accept an output with the requested parameters
1075 if (status != NO_ERROR ||
1076 (samplingRate != 0 && samplingRate != config.sample_rate) ||
1077 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
1078 (channelMask != 0 && channelMask != config.channel_mask)) {
1079 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1080 "format %d %d, channelMask %04x %04x", output, samplingRate,
1081 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1082 outputDesc->mChannelMask);
1083 if (output != AUDIO_IO_HANDLE_NONE) {
1084 mpClientInterface->closeOutput(output);
1085 }
1086 // fall back to mixer output if possible when the direct output could not be open
1087 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
1088 goto non_direct_output;
1089 }
1090 return AUDIO_IO_HANDLE_NONE;
1091 }
1092 outputDesc->mSamplingRate = config.sample_rate;
1093 outputDesc->mChannelMask = config.channel_mask;
1094 outputDesc->mFormat = config.format;
1095 outputDesc->mRefCount[stream] = 0;
1096 outputDesc->mStopTime[stream] = 0;
1097 outputDesc->mDirectOpenCount = 1;
1098
1099 audio_io_handle_t srcOutput = getOutputForEffect();
1100 addOutput(output, outputDesc);
1101 audio_io_handle_t dstOutput = getOutputForEffect();
1102 if (dstOutput == output) {
1103 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1104 }
1105 mPreviousOutputs = mOutputs;
1106 ALOGV("getOutput() returns new direct output %d", output);
1107 mpClientInterface->onAudioPortListUpdate();
1108 return output;
1109 }
1110
1111non_direct_output:
1112 // ignoring channel mask due to downmix capability in mixer
1113
1114 // open a non direct output
1115
1116 // for non direct outputs, only PCM is supported
1117 if (audio_is_linear_pcm(format)) {
1118 // get which output is suitable for the specified stream. The actual
1119 // routing change will happen when startOutput() will be called
1120 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1121
1122 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1123 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1124 output = selectOutput(outputs, flags, format);
1125 }
1126 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1127 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1128
1129 ALOGV(" getOutputForDevice() returns output %d", output);
1130
1131 return output;
1132}
Ramjee Singh58a28312015-08-14 14:40:44 +05301133
Mingming Yin38877802015-10-05 15:24:04 -07001134AudioPolicyManagerCustom::AudioPolicyManagerCustom(AudioPolicyClientInterface *clientInterface)
1135 : AudioPolicyManager(clientInterface),
1136 mHdmiAudioDisabled(false),
1137 mHdmiAudioEvent(false),
1138 mPrevPhoneState(0)
1139{
1140 char ssr_enabled[PROPERTY_VALUE_MAX] = {0};
1141 bool prop_ssr_enabled = false;
1142
1143 if (property_get("ro.qc.sdk.audio.ssr", ssr_enabled, NULL)) {
1144 prop_ssr_enabled = atoi(ssr_enabled) || !strncmp("true", ssr_enabled, 4);
1145 }
1146
1147 for (size_t i = 0; i < mHwModules.size(); i++) {
1148 ALOGV("Hw module %d", i);
1149 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) {
1150 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
1151 ALOGV("Input profile ", j);
1152 for (size_t k = 0; k < inProfile->mChannelMasks.size(); k++) {
1153 audio_channel_mask_t channelMask =
1154 inProfile->mChannelMasks.itemAt(k);
1155 ALOGV("Channel Mask %x size %d", channelMask,
1156 inProfile->mChannelMasks.size());
1157 if (AUDIO_CHANNEL_IN_5POINT1 == channelMask) {
1158 if (!prop_ssr_enabled) {
1159 ALOGI("removing AUDIO_CHANNEL_IN_5POINT1 from"
1160 " input profile as SSR(surround sound record)"
1161 " is not supported on this chipset variant");
1162 inProfile->mChannelMasks.removeItemsAt(k, 1);
1163 ALOGV("Channel Mask size now %d",
1164 inProfile->mChannelMasks.size());
1165 }
1166 }
1167 }
1168 }
1169 }
1170
1171#ifdef RECORD_PLAY_CONCURRENCY
1172 mIsInputRequestOnProgress = false;
1173#endif
1174
1175
1176#ifdef VOICE_CONCURRENCY
1177 mFallBackflag = getFallBackPath();
1178#endif
1179}
1180
Sharad Sangleca67a022015-05-28 16:15:16 +05301181}