blob: 9c246411b198d22428c57fcc38f9cb5a9df92a68 [file] [log] [blame]
Eric Laurente552edb2014-03-10 17:42:56 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -070017#define LOG_TAG "APM_AudioPolicyManager"
Eric Laurente552edb2014-03-10 17:42:56 -070018//#define LOG_NDEBUG 0
19
20//#define VERY_VERBOSE_LOGGING
21#ifdef VERY_VERBOSE_LOGGING
22#define ALOGVV ALOGV
23#else
24#define ALOGVV(a...) do { } while(0)
25#endif
26
Jaekyun Seok0d4a6af2017-02-17 17:10:17 +090027#define AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH 128
28#define AUDIO_POLICY_XML_CONFIG_FILE_NAME "audio_policy_configuration.xml"
François Gaffief4ad6e52015-11-19 16:59:57 +010029
Eric Laurentd4692962014-05-05 18:13:44 -070030#include <inttypes.h>
Eric Laurente552edb2014-03-10 17:42:56 -070031#include <math.h>
Eric Laurentd4692962014-05-05 18:13:44 -070032
François Gaffie2110e042015-03-24 08:41:51 +010033#include <AudioPolicyManagerInterface.h>
34#include <AudioPolicyEngineInstance.h>
Mathias Agopian05d19b02017-02-28 16:28:19 -080035#include <cutils/atomic.h>
Eric Laurente552edb2014-03-10 17:42:56 -070036#include <cutils/properties.h>
Eric Laurentd4692962014-05-05 18:13:44 -070037#include <utils/Log.h>
Eric Laurent3b73df72014-03-11 09:06:29 -070038#include <media/AudioParameter.h>
Eric Laurente83b55d2014-11-14 10:06:21 -080039#include <media/AudioPolicyHelper.h>
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070040#include <soundtrigger/SoundTrigger.h>
Mikhail Naganovcbc8f612016-10-11 18:05:13 -070041#include <system/audio.h>
Mikhail Naganov9ee05402016-10-13 15:58:17 -070042#include <audio_policy_conf.h>
Eric Laurentd4692962014-05-05 18:13:44 -070043#include "AudioPolicyManager.h"
François Gaffied1ab2bd2015-12-02 18:20:06 +010044#ifndef USE_XML_AUDIO_POLICY_CONF
François Gaffie53615e22015-03-19 09:24:12 +010045#include <ConfigParsingUtils.h>
François Gaffied1ab2bd2015-12-02 18:20:06 +010046#include <StreamDescriptor.h>
François Gaffief4ad6e52015-11-19 16:59:57 +010047#endif
François Gaffied1ab2bd2015-12-02 18:20:06 +010048#include <Serializer.h>
François Gaffiea8ecc2c2015-11-09 16:10:40 +010049#include "TypeConverter.h"
François Gaffie53615e22015-03-19 09:24:12 +010050#include <policy.h>
Eric Laurente552edb2014-03-10 17:42:56 -070051
Eric Laurent3b73df72014-03-11 09:06:29 -070052namespace android {
Eric Laurente552edb2014-03-10 17:42:56 -070053
Eric Laurentdc462862016-07-19 12:29:53 -070054//FIXME: workaround for truncated touch sounds
55// to be removed when the problem is handled by system UI
56#define TOUCH_SOUND_FIXED_DELAY_MS 100
Jean-Michel Trivi719a9872017-08-05 13:51:35 -070057
58// Largest difference in dB on earpiece in call between the voice volume and another
59// media / notification / system volume.
60constexpr float IN_CALL_EARPIECE_HEADROOM_DB = 3.f;
61
Eric Laurente552edb2014-03-10 17:42:56 -070062// ----------------------------------------------------------------------------
63// AudioPolicyInterface implementation
64// ----------------------------------------------------------------------------
65
Eric Laurente0720872014-03-11 09:30:41 -070066status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
Paul McLeane743a472015-01-28 11:07:31 -080067 audio_policy_dev_state_t state,
68 const char *device_address,
69 const char *device_name)
Eric Laurente552edb2014-03-10 17:42:56 -070070{
Paul McLeane743a472015-01-28 11:07:31 -080071 return setDeviceConnectionStateInt(device, state, device_address, device_name);
Eric Laurentc73ca6e2014-12-12 14:34:22 -080072}
73
François Gaffie44481e72016-04-20 07:49:57 +020074void AudioPolicyManager::broadcastDeviceConnectionState(audio_devices_t device,
75 audio_policy_dev_state_t state,
76 const String8 &device_address)
77{
78 AudioParameter param(device_address);
79 const String8 key(state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE ?
Mikhail Naganov388360c2016-10-17 17:09:41 -070080 AudioParameter::keyStreamConnect : AudioParameter::keyStreamDisconnect);
François Gaffie44481e72016-04-20 07:49:57 +020081 param.addInt(key, device);
82 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
83}
84
Eric Laurentc73ca6e2014-12-12 14:34:22 -080085status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device,
Eric Laurenta1d525f2015-01-29 13:36:45 -080086 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -080087 const char *device_address,
88 const char *device_name)
Eric Laurentc73ca6e2014-12-12 14:34:22 -080089{
Paul McLeane743a472015-01-28 11:07:31 -080090 ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
91- device, state, device_address, device_name);
Eric Laurente552edb2014-03-10 17:42:56 -070092
93 // connect/disconnect only 1 device at a time
94 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
95
François Gaffie53615e22015-03-19 09:24:12 +010096 sp<DeviceDescriptor> devDesc =
97 mHwModules.getDeviceDescriptor(device, device_address, device_name);
Paul McLeane743a472015-01-28 11:07:31 -080098
Eric Laurente552edb2014-03-10 17:42:56 -070099 // handle output devices
100 if (audio_is_output_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700101 SortedVector <audio_io_handle_t> outputs;
102
Eric Laurent3a4311c2014-03-17 12:00:47 -0700103 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
104
Eric Laurente552edb2014-03-10 17:42:56 -0700105 // save a copy of the opened output descriptors before any output is opened or closed
106 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
107 mPreviousOutputs = mOutputs;
Eric Laurente552edb2014-03-10 17:42:56 -0700108 switch (state)
109 {
110 // handle output device connection
Eric Laurent3ae5f312015-02-03 17:12:08 -0800111 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700112 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700113 ALOGW("setDeviceConnectionState() device already connected: %x", device);
114 return INVALID_OPERATION;
115 }
116 ALOGV("setDeviceConnectionState() connecting device %x", device);
117
Eric Laurente552edb2014-03-10 17:42:56 -0700118 // register new device as available
Eric Laurent3a4311c2014-03-17 12:00:47 -0700119 index = mAvailableOutputDevices.add(devDesc);
120 if (index >= 0) {
François Gaffie53615e22015-03-19 09:24:12 +0100121 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurentcf817a22014-08-04 20:36:31 -0700122 if (module == 0) {
123 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
124 device);
125 mAvailableOutputDevices.remove(devDesc);
126 return INVALID_OPERATION;
127 }
Paul McLeane743a472015-01-28 11:07:31 -0800128 mAvailableOutputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700129 } else {
130 return NO_MEMORY;
Eric Laurente552edb2014-03-10 17:42:56 -0700131 }
132
François Gaffie44481e72016-04-20 07:49:57 +0200133 // Before checking outputs, broadcast connect event to allow HAL to retrieve dynamic
134 // parameters on newly connected devices (instead of opening the outputs...)
135 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
136
Eric Laurenta1d525f2015-01-29 13:36:45 -0800137 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700138 mAvailableOutputDevices.remove(devDesc);
François Gaffie44481e72016-04-20 07:49:57 +0200139
140 broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
141 devDesc->mAddress);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700142 return INVALID_OPERATION;
143 }
François Gaffie2110e042015-03-24 08:41:51 +0100144 // Propagate device availability to Engine
145 mEngine->setDeviceConnectionState(devDesc, state);
146
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700147 // outputs should never be empty here
148 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
149 "checkOutputsForDevice() returned no outputs but status OK");
150 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
151 outputs.size());
Eric Laurent3ae5f312015-02-03 17:12:08 -0800152
Eric Laurent3ae5f312015-02-03 17:12:08 -0800153 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700154 // handle output device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700155 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700156 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700157 ALOGW("setDeviceConnectionState() device not connected: %x", device);
158 return INVALID_OPERATION;
159 }
160
Paul McLean5c477aa2014-08-20 16:47:57 -0700161 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
162
Paul McLeane743a472015-01-28 11:07:31 -0800163 // Send Disconnect to HALs
François Gaffie44481e72016-04-20 07:49:57 +0200164 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700165
Eric Laurente552edb2014-03-10 17:42:56 -0700166 // remove device from available output devices
Eric Laurent3a4311c2014-03-17 12:00:47 -0700167 mAvailableOutputDevices.remove(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700168
Eric Laurenta1d525f2015-01-29 13:36:45 -0800169 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
François Gaffie2110e042015-03-24 08:41:51 +0100170
171 // Propagate device availability to Engine
172 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurente552edb2014-03-10 17:42:56 -0700173 } break;
174
175 default:
176 ALOGE("setDeviceConnectionState() invalid state: %x", state);
177 return BAD_VALUE;
178 }
179
Eric Laurent3a4311c2014-03-17 12:00:47 -0700180 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
181 // output is suspended before any tracks are moved to it
Eric Laurente552edb2014-03-10 17:42:56 -0700182 checkA2dpSuspend();
183 checkOutputForAllStrategies();
184 // outputs must be closed after checkOutputForAllStrategies() is executed
185 if (!outputs.isEmpty()) {
186 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700187 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700188 // close unused outputs after device disconnection or direct outputs that have been
189 // opened by checkOutputsForDevice() to query dynamic parameters
Eric Laurent3b73df72014-03-11 09:06:29 -0700190 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Eric Laurente552edb2014-03-10 17:42:56 -0700191 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
192 (desc->mDirectOpenCount == 0))) {
193 closeOutput(outputs[i]);
194 }
195 }
Eric Laurent3a4311c2014-03-17 12:00:47 -0700196 // check again after closing A2DP output to reset mA2dpSuspended if needed
197 checkA2dpSuspend();
Eric Laurente552edb2014-03-10 17:42:56 -0700198 }
199
200 updateDevicesAndOutputs();
Eric Laurent87ffa392015-05-22 10:32:38 -0700201 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700202 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
203 updateCallRouting(newDevice);
204 }
Eric Laurente552edb2014-03-10 17:42:56 -0700205 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700206 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
207 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
208 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700209 // do not force device change on duplicated output because if device is 0, it will
210 // also force a device 0 for the two outputs it is duplicated to which may override
211 // a valid device selection on those outputs.
Eric Laurentc75307b2015-03-17 15:29:32 -0700212 bool force = !desc->isDuplicated()
François Gaffie53615e22015-03-19 09:24:12 +0100213 && (!device_distinguishes_on_address(device)
Eric Laurentc2730ba2014-07-20 15:47:07 -0700214 // always force when disconnecting (a non-duplicated device)
215 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Eric Laurentc75307b2015-03-17 15:29:32 -0700216 setOutputDevice(desc, newDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700217 }
Eric Laurente552edb2014-03-10 17:42:56 -0700218 }
219
Eric Laurentd60560a2015-04-10 11:31:20 -0700220 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
221 cleanUpForDevice(devDesc);
222 }
223
Eric Laurent72aa32f2014-05-30 18:51:48 -0700224 mpClientInterface->onAudioPortListUpdate();
Eric Laurentb71e58b2014-05-29 16:08:11 -0700225 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700226 } // end if is output device
227
Eric Laurente552edb2014-03-10 17:42:56 -0700228 // handle input devices
229 if (audio_is_input_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700230 SortedVector <audio_io_handle_t> inputs;
231
Eric Laurent3a4311c2014-03-17 12:00:47 -0700232 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700233 switch (state)
234 {
235 // handle input device connection
Eric Laurent3b73df72014-03-11 09:06:29 -0700236 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700237 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700238 ALOGW("setDeviceConnectionState() device already connected: %d", device);
239 return INVALID_OPERATION;
240 }
François Gaffie53615e22015-03-19 09:24:12 +0100241 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurent6a94d692014-05-20 11:18:06 -0700242 if (module == NULL) {
243 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
244 device);
245 return INVALID_OPERATION;
246 }
François Gaffie44481e72016-04-20 07:49:57 +0200247
248 // Before checking intputs, broadcast connect event to allow HAL to retrieve dynamic
249 // parameters on newly connected devices (instead of opening the inputs...)
250 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
251
Paul McLean9080a4c2015-06-18 08:24:02 -0700252 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
François Gaffie44481e72016-04-20 07:49:57 +0200253 broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
254 devDesc->mAddress);
Eric Laurentd4692962014-05-05 18:13:44 -0700255 return INVALID_OPERATION;
256 }
257
Eric Laurent3a4311c2014-03-17 12:00:47 -0700258 index = mAvailableInputDevices.add(devDesc);
259 if (index >= 0) {
Paul McLeane743a472015-01-28 11:07:31 -0800260 mAvailableInputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700261 } else {
262 return NO_MEMORY;
263 }
Eric Laurent3ae5f312015-02-03 17:12:08 -0800264
François Gaffie2110e042015-03-24 08:41:51 +0100265 // Propagate device availability to Engine
266 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700267 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700268
269 // handle input device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700270 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700271 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700272 ALOGW("setDeviceConnectionState() device not connected: %d", device);
273 return INVALID_OPERATION;
274 }
Paul McLean5c477aa2014-08-20 16:47:57 -0700275
276 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
277
278 // Set Disconnect to HALs
François Gaffie44481e72016-04-20 07:49:57 +0200279 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700280
Paul McLean9080a4c2015-06-18 08:24:02 -0700281 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700282 mAvailableInputDevices.remove(devDesc);
Paul McLean5c477aa2014-08-20 16:47:57 -0700283
François Gaffie2110e042015-03-24 08:41:51 +0100284 // Propagate device availability to Engine
285 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700286 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700287
288 default:
289 ALOGE("setDeviceConnectionState() invalid state: %x", state);
290 return BAD_VALUE;
291 }
292
Eric Laurentd4692962014-05-05 18:13:44 -0700293 closeAllInputs();
Eric Laurent5f5fca52016-08-04 11:48:57 -0700294 // As the input device list can impact the output device selection, update
295 // getDeviceForStrategy() cache
296 updateDevicesAndOutputs();
Eric Laurente552edb2014-03-10 17:42:56 -0700297
Eric Laurent87ffa392015-05-22 10:32:38 -0700298 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700299 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
300 updateCallRouting(newDevice);
301 }
302
Eric Laurentd60560a2015-04-10 11:31:20 -0700303 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
304 cleanUpForDevice(devDesc);
305 }
306
Eric Laurentb52c1522014-05-20 11:27:36 -0700307 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700308 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700309 } // end if is input device
Eric Laurente552edb2014-03-10 17:42:56 -0700310
311 ALOGW("setDeviceConnectionState() invalid device: %x", device);
312 return BAD_VALUE;
313}
314
Eric Laurente0720872014-03-11 09:30:41 -0700315audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +0100316 const char *device_address)
Eric Laurente552edb2014-03-10 17:42:56 -0700317{
Eric Laurent634b7142016-04-20 13:48:02 -0700318 sp<DeviceDescriptor> devDesc =
319 mHwModules.getDeviceDescriptor(device, device_address, "",
320 (strlen(device_address) != 0)/*matchAddress*/);
321
322 if (devDesc == 0) {
323 ALOGW("getDeviceConnectionState() undeclared device, type %08x, address: %s",
324 device, device_address);
325 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
326 }
François Gaffie53615e22015-03-19 09:24:12 +0100327
Eric Laurent3a4311c2014-03-17 12:00:47 -0700328 DeviceVector *deviceVector;
329
Eric Laurente552edb2014-03-10 17:42:56 -0700330 if (audio_is_output_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700331 deviceVector = &mAvailableOutputDevices;
Eric Laurente552edb2014-03-10 17:42:56 -0700332 } else if (audio_is_input_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700333 deviceVector = &mAvailableInputDevices;
334 } else {
335 ALOGW("getDeviceConnectionState() invalid device type %08x", device);
336 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700337 }
Eric Laurent634b7142016-04-20 13:48:02 -0700338
339 return (deviceVector->getDevice(device, String8(device_address)) != 0) ?
340 AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurenta1d525f2015-01-29 13:36:45 -0800341}
342
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800343status_t AudioPolicyManager::handleDeviceConfigChange(audio_devices_t device,
344 const char *device_address,
345 const char *device_name)
346{
347 status_t status;
348
349 ALOGV("handleDeviceConfigChange(() device: 0x%X, address %s name %s",
350 device, device_address, device_name);
351
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -0800352 // connect/disconnect only 1 device at a time
353 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
354
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800355 // Check if the device is currently connected
356 sp<DeviceDescriptor> devDesc =
357 mHwModules.getDeviceDescriptor(device, device_address, device_name);
358 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
359 if (index < 0) {
360 // Nothing to do: device is not connected
361 return NO_ERROR;
362 }
363
364 // Toggle the device state: UNAVAILABLE -> AVAILABLE
365 // This will force reading again the device configuration
366 status = setDeviceConnectionState(device,
367 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
368 device_address, device_name);
369 if (status != NO_ERROR) {
370 ALOGW("handleDeviceConfigChange() error disabling connection state: %d",
371 status);
372 return status;
373 }
374
375 status = setDeviceConnectionState(device,
376 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
377 device_address, device_name);
378 if (status != NO_ERROR) {
379 ALOGW("handleDeviceConfigChange() error enabling connection state: %d",
380 status);
381 return status;
382 }
383
384 return NO_ERROR;
385}
386
Eric Laurentdc462862016-07-19 12:29:53 -0700387uint32_t AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, uint32_t delayMs)
Eric Laurentc2730ba2014-07-20 15:47:07 -0700388{
389 bool createTxPatch = false;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700390 status_t status;
391 audio_patch_handle_t afPatchHandle;
392 DeviceVector deviceList;
Eric Laurentdc462862016-07-19 12:29:53 -0700393 uint32_t muteWaitMs = 0;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700394
Andy Hunga76c7de2016-12-13 19:14:31 -0800395 if(!hasPrimaryOutput() || mPrimaryOutput->device() == AUDIO_DEVICE_OUT_STUB) {
Eric Laurentdc462862016-07-19 12:29:53 -0700396 return muteWaitMs;
Eric Laurent87ffa392015-05-22 10:32:38 -0700397 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -0800398 audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700399 ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice);
400
401 // release existing RX patch if any
402 if (mCallRxPatch != 0) {
403 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
404 mCallRxPatch.clear();
405 }
406 // release TX patch if any
407 if (mCallTxPatch != 0) {
408 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
409 mCallTxPatch.clear();
410 }
411
412 // If the RX device is on the primary HW module, then use legacy routing method for voice calls
413 // via setOutputDevice() on primary output.
414 // Otherwise, create two audio patches for TX and RX path.
415 if (availablePrimaryOutputDevices() & rxDevice) {
Eric Laurentdc462862016-07-19 12:29:53 -0700416 muteWaitMs = setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700417 // If the TX device is also on the primary HW module, setOutputDevice() will take care
418 // of it due to legacy implementation. If not, create a patch.
419 if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN)
420 == AUDIO_DEVICE_NONE) {
421 createTxPatch = true;
422 }
Eric Laurent8ae73122016-04-12 10:13:29 -0700423 } else { // create RX path audio patch
424 struct audio_patch patch;
425
426 patch.num_sources = 1;
427 patch.num_sinks = 1;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700428 deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice);
429 ALOG_ASSERT(!deviceList.isEmpty(),
430 "updateCallRouting() selected device not in output device list");
431 sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0);
432 deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX);
433 ALOG_ASSERT(!deviceList.isEmpty(),
434 "updateCallRouting() no telephony RX device");
435 sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0);
436
437 rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
438 rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
439
440 // request to reuse existing output stream if one is already opened to reach the RX device
441 SortedVector<audio_io_handle_t> outputs =
442 getOutputsForDevice(rxDevice, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700443 audio_io_handle_t output = selectOutput(outputs,
444 AUDIO_OUTPUT_FLAG_NONE,
445 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700446 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700447 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700448 ALOG_ASSERT(!outputDesc->isDuplicated(),
449 "updateCallRouting() RX device output is duplicated");
450 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700451 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700452 patch.num_sources = 2;
453 }
454
455 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentdc462862016-07-19 12:29:53 -0700456 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700457 ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch",
458 status);
459 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100460 mCallRxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700461 mCallRxPatch->mAfPatchHandle = afPatchHandle;
462 mCallRxPatch->mUid = mUidCached;
463 }
464 createTxPatch = true;
465 }
Eric Laurent8ae73122016-04-12 10:13:29 -0700466 if (createTxPatch) { // create TX path audio patch
Eric Laurentc2730ba2014-07-20 15:47:07 -0700467 struct audio_patch patch;
Eric Laurent8ae73122016-04-12 10:13:29 -0700468
Eric Laurentc2730ba2014-07-20 15:47:07 -0700469 patch.num_sources = 1;
470 patch.num_sinks = 1;
471 deviceList = mAvailableInputDevices.getDevicesFromType(txDevice);
472 ALOG_ASSERT(!deviceList.isEmpty(),
473 "updateCallRouting() selected device not in input device list");
474 sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0);
475 txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
476 deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
477 ALOG_ASSERT(!deviceList.isEmpty(),
478 "updateCallRouting() no telephony TX device");
479 sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0);
480 txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
481
482 SortedVector<audio_io_handle_t> outputs =
483 getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700484 audio_io_handle_t output = selectOutput(outputs,
485 AUDIO_OUTPUT_FLAG_NONE,
486 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700487 // request to reuse existing output stream if one is already opened to reach the TX
488 // path output device
489 if (output != AUDIO_IO_HANDLE_NONE) {
490 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
491 ALOG_ASSERT(!outputDesc->isDuplicated(),
492 "updateCallRouting() RX device output is duplicated");
493 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700494 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700495 patch.num_sources = 2;
496 }
497
Eric Laurentc0a889f2015-10-14 14:36:34 -0700498 // terminate active capture if on the same HW module as the call TX source device
499 // FIXME: would be better to refine to only inputs whose profile connects to the
500 // call TX device but this information is not in the audio patch and logic here must be
501 // symmetric to the one in startInput()
Eric Laurentfb66dd92016-01-28 18:32:03 -0800502 Vector<sp <AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
503 for (size_t i = 0; i < activeInputs.size(); i++) {
504 sp<AudioInputDescriptor> activeDesc = activeInputs[i];
505 if (activeDesc->hasSameHwModuleAs(txSourceDeviceDesc)) {
506 AudioSessionCollection activeSessions =
507 activeDesc->getAudioSessions(true /*activeOnly*/);
508 for (size_t j = 0; j < activeSessions.size(); j++) {
509 audio_session_t activeSession = activeSessions.keyAt(j);
510 stopInput(activeDesc->mIoHandle, activeSession);
511 releaseInput(activeDesc->mIoHandle, activeSession);
512 }
Eric Laurentc0a889f2015-10-14 14:36:34 -0700513 }
514 }
515
Eric Laurentc2730ba2014-07-20 15:47:07 -0700516 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentdc462862016-07-19 12:29:53 -0700517 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700518 ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch",
519 status);
520 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100521 mCallTxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700522 mCallTxPatch->mAfPatchHandle = afPatchHandle;
523 mCallTxPatch->mUid = mUidCached;
524 }
525 }
Eric Laurentdc462862016-07-19 12:29:53 -0700526
527 return muteWaitMs;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700528}
529
Eric Laurente0720872014-03-11 09:30:41 -0700530void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700531{
532 ALOGV("setPhoneState() state %d", state);
François Gaffie2110e042015-03-24 08:41:51 +0100533 // store previous phone state for management of sonification strategy below
534 int oldState = mEngine->getPhoneState();
535
536 if (mEngine->setPhoneState(state) != NO_ERROR) {
537 ALOGW("setPhoneState() invalid or same state %d", state);
Eric Laurente552edb2014-03-10 17:42:56 -0700538 return;
539 }
François Gaffie2110e042015-03-24 08:41:51 +0100540 /// Opens: can these line be executed after the switch of volume curves???
Eric Laurente552edb2014-03-10 17:42:56 -0700541 // if leaving call state, handle special case of active streams
542 // pertaining to sonification strategy see handleIncallSonification()
Eric Laurent63dea1d2015-07-02 17:10:28 -0700543 if (isStateInCall(oldState)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700544 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent794fde22016-03-11 09:50:45 -0800545 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700546 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700547 }
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800548
Eric Laurent63dea1d2015-07-02 17:10:28 -0700549 // force reevaluating accessibility routing when call stops
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800550 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700551 }
552
François Gaffie2110e042015-03-24 08:41:51 +0100553 /**
554 * Switching to or from incall state or switching between telephony and VoIP lead to force
555 * routing command.
556 */
557 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
558 || (is_state_in_call(state) && (state != oldState)));
Eric Laurente552edb2014-03-10 17:42:56 -0700559
560 // check for device and output changes triggered by new phone state
Eric Laurente552edb2014-03-10 17:42:56 -0700561 checkA2dpSuspend();
562 checkOutputForAllStrategies();
563 updateDevicesAndOutputs();
564
Eric Laurente552edb2014-03-10 17:42:56 -0700565 int delayMs = 0;
566 if (isStateInCall(state)) {
567 nsecs_t sysTime = systemTime();
568 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700569 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700570 // mute media and sonification strategies and delay device switch by the largest
571 // latency of any output where either strategy is active.
572 // This avoid sending the ring tone or music tail into the earpiece or headset.
François Gaffiead3183e2015-03-18 16:55:35 +0100573 if ((isStrategyActive(desc, STRATEGY_MEDIA,
574 SONIFICATION_HEADSET_MUSIC_DELAY,
575 sysTime) ||
576 isStrategyActive(desc, STRATEGY_SONIFICATION,
577 SONIFICATION_HEADSET_MUSIC_DELAY,
578 sysTime)) &&
Eric Laurentc75307b2015-03-17 15:29:32 -0700579 (delayMs < (int)desc->latency()*2)) {
580 delayMs = desc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -0700581 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700582 setStrategyMute(STRATEGY_MEDIA, true, desc);
583 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700584 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Eric Laurentc75307b2015-03-17 15:29:32 -0700585 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
586 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700587 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
588 }
589 }
590
Eric Laurent87ffa392015-05-22 10:32:38 -0700591 if (hasPrimaryOutput()) {
592 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
593 // the device returned is not necessarily reachable via this output
594 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
595 // force routing command to audio hardware when ending call
596 // even if no device change is needed
597 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
598 rxDevice = mPrimaryOutput->device();
599 }
Eric Laurente552edb2014-03-10 17:42:56 -0700600
Eric Laurent87ffa392015-05-22 10:32:38 -0700601 if (state == AUDIO_MODE_IN_CALL) {
602 updateCallRouting(rxDevice, delayMs);
603 } else if (oldState == AUDIO_MODE_IN_CALL) {
604 if (mCallRxPatch != 0) {
605 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
606 mCallRxPatch.clear();
607 }
608 if (mCallTxPatch != 0) {
609 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
610 mCallTxPatch.clear();
611 }
612 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
613 } else {
614 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700615 }
Eric Laurentc2730ba2014-07-20 15:47:07 -0700616 }
Eric Laurente552edb2014-03-10 17:42:56 -0700617 // if entering in call state, handle special case of active streams
618 // pertaining to sonification strategy see handleIncallSonification()
619 if (isStateInCall(state)) {
620 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent794fde22016-03-11 09:50:45 -0800621 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700622 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700623 }
Eric Laurent63dea1d2015-07-02 17:10:28 -0700624
625 // force reevaluating accessibility routing when call starts
626 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700627 }
628
629 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700630 if (state == AUDIO_MODE_RINGTONE &&
631 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700632 mLimitRingtoneVolume = true;
633 } else {
634 mLimitRingtoneVolume = false;
635 }
636}
637
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700638audio_mode_t AudioPolicyManager::getPhoneState() {
639 return mEngine->getPhoneState();
640}
641
Eric Laurente0720872014-03-11 09:30:41 -0700642void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700643 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700644{
François Gaffie2110e042015-03-24 08:41:51 +0100645 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Eric Laurent8dc87a62017-05-16 19:00:40 -0700646 if (config == mEngine->getForceUse(usage)) {
647 return;
648 }
Eric Laurente552edb2014-03-10 17:42:56 -0700649
François Gaffie2110e042015-03-24 08:41:51 +0100650 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
651 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
652 return;
Eric Laurente552edb2014-03-10 17:42:56 -0700653 }
François Gaffie2110e042015-03-24 08:41:51 +0100654 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
655 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
656 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
Eric Laurente552edb2014-03-10 17:42:56 -0700657
658 // check for device and output changes triggered by new force usage
659 checkA2dpSuspend();
660 checkOutputForAllStrategies();
661 updateDevicesAndOutputs();
Phil Burk09bc4612016-02-24 15:58:15 -0800662
Eric Laurentdc462862016-07-19 12:29:53 -0700663 //FIXME: workaround for truncated touch sounds
664 // to be removed when the problem is handled by system UI
665 uint32_t delayMs = 0;
666 uint32_t waitMs = 0;
667 if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) {
668 delayMs = TOUCH_SOUND_FIXED_DELAY_MS;
669 }
Eric Laurent87ffa392015-05-22 10:32:38 -0700670 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700671 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
Eric Laurentdc462862016-07-19 12:29:53 -0700672 waitMs = updateCallRouting(newDevice, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700673 }
Eric Laurente552edb2014-03-10 17:42:56 -0700674 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700675 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
676 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
677 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
Eric Laurentdc462862016-07-19 12:29:53 -0700678 waitMs = setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE),
679 delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700680 }
Eric Laurente552edb2014-03-10 17:42:56 -0700681 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
Eric Laurentdc462862016-07-19 12:29:53 -0700682 applyStreamVolumes(outputDesc, newDevice, waitMs, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700683 }
684 }
685
Eric Laurentfb66dd92016-01-28 18:32:03 -0800686 Vector<sp <AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
687 for (size_t i = 0; i < activeInputs.size(); i++) {
688 sp<AudioInputDescriptor> activeDesc = activeInputs[i];
689 audio_devices_t newDevice = getNewInputDevice(activeDesc);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700690 // Force new input selection if the new device can not be reached via current input
Eric Laurentfb66dd92016-01-28 18:32:03 -0800691 if (activeDesc->mProfile->getSupportedDevices().types() &
692 (newDevice & ~AUDIO_DEVICE_BIT_IN)) {
693 setInputDevice(activeDesc->mIoHandle, newDevice);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700694 } else {
Eric Laurentfb66dd92016-01-28 18:32:03 -0800695 closeInput(activeDesc->mIoHandle);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700696 }
Eric Laurente552edb2014-03-10 17:42:56 -0700697 }
Eric Laurente552edb2014-03-10 17:42:56 -0700698}
699
Eric Laurente0720872014-03-11 09:30:41 -0700700void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700701{
702 ALOGV("setSystemProperty() property %s, value %s", property, value);
703}
704
705// Find a direct output profile compatible with the parameters passed, even if the input flags do
706// not explicitly request a direct output
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800707sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700708 audio_devices_t device,
709 uint32_t samplingRate,
710 audio_format_t format,
711 audio_channel_mask_t channelMask,
712 audio_output_flags_t flags)
713{
Eric Laurent861a6282015-05-18 15:40:16 -0700714 // only retain flags that will drive the direct output profile selection
715 // if explicitly requested
716 static const uint32_t kRelevantFlags =
Haynes Mathew George84c621e2017-04-25 11:41:50 -0700717 (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD |
718 AUDIO_OUTPUT_FLAG_VOIP_RX);
Eric Laurent861a6282015-05-18 15:40:16 -0700719 flags =
720 (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT);
721
722 sp<IOProfile> profile;
723
Eric Laurente552edb2014-03-10 17:42:56 -0700724 for (size_t i = 0; i < mHwModules.size(); i++) {
725 if (mHwModules[i]->mHandle == 0) {
726 continue;
727 }
728 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent861a6282015-05-18 15:40:16 -0700729 sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j];
730 if (!curProfile->isCompatibleProfile(device, String8(""),
Andy Hungf129b032015-04-07 13:45:50 -0700731 samplingRate, NULL /*updatedSamplingRate*/,
732 format, NULL /*updatedFormat*/,
733 channelMask, NULL /*updatedChannelMask*/,
Eric Laurent861a6282015-05-18 15:40:16 -0700734 flags)) {
735 continue;
736 }
737 // reject profiles not corresponding to a device currently available
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100738 if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) {
Eric Laurent861a6282015-05-18 15:40:16 -0700739 continue;
740 }
741 // if several profiles are compatible, give priority to one with offload capability
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100742 if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) {
Eric Laurent861a6282015-05-18 15:40:16 -0700743 continue;
744 }
745 profile = curProfile;
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100746 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent861a6282015-05-18 15:40:16 -0700747 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700748 }
Eric Laurente552edb2014-03-10 17:42:56 -0700749 }
750 }
Eric Laurent861a6282015-05-18 15:40:16 -0700751 return profile;
Eric Laurente552edb2014-03-10 17:42:56 -0700752}
753
Eric Laurentf4e63452017-11-06 19:31:46 +0000754audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream)
Eric Laurente552edb2014-03-10 17:42:56 -0700755{
Eric Laurent3b73df72014-03-11 09:06:29 -0700756 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700757 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurentf4e63452017-11-06 19:31:46 +0000758 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
759 audio_io_handle_t output = selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID);
Eric Laurente552edb2014-03-10 17:42:56 -0700760
Eric Laurentf4e63452017-11-06 19:31:46 +0000761 ALOGV("getOutput() stream %d selected device %08x, output %d", stream, device, output);
762 return output;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700763}
764
Eric Laurente83b55d2014-11-14 10:06:21 -0800765status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
766 audio_io_handle_t *output,
767 audio_session_t session,
768 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700769 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800770 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800771 audio_output_flags_t flags,
Eric Laurent2ac76942017-06-22 17:17:09 -0700772 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800773 audio_port_handle_t *portId)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700774{
Eric Laurente83b55d2014-11-14 10:06:21 -0800775 audio_attributes_t attributes;
776 if (attr != NULL) {
777 if (!isValidAttributes(attr)) {
778 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
779 attr->usage, attr->content_type, attr->flags,
780 attr->tags);
781 return BAD_VALUE;
782 }
783 attributes = *attr;
784 } else {
785 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
786 ALOGE("getOutputForAttr(): invalid stream type");
787 return BAD_VALUE;
788 }
789 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700790 }
Eric Laurent20b9ef02016-12-05 11:03:16 -0800791
792 // TODO: check for existing client for this port ID
793 if (*portId == AUDIO_PORT_HANDLE_NONE) {
794 *portId = AudioPort::getNextUniqueId();
795 }
796
Eric Laurentc75307b2015-03-17 15:29:32 -0700797 sp<SwAudioOutputDescriptor> desc;
Jean-Michel Trivie8deced2016-02-11 12:50:39 -0800798 if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) {
François Gaffie036e1e92015-03-19 10:16:24 +0100799 ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr");
Eric Laurent20b9ef02016-12-05 11:03:16 -0800800 if (!audio_has_proportional_frames(config->format)) {
François Gaffie036e1e92015-03-19 10:16:24 +0100801 return BAD_VALUE;
Eric Laurent275e8e92014-11-30 15:14:47 -0800802 }
François Gaffie036e1e92015-03-19 10:16:24 +0100803 *stream = streamTypefromAttributesInt(&attributes);
804 *output = desc->mIoHandle;
805 ALOGV("getOutputForAttr() returns output %d", *output);
806 return NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -0800807 }
808 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
809 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
810 return BAD_VALUE;
811 }
812
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700813 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x"
814 " session %d selectedDeviceId %d",
815 attributes.usage, attributes.content_type, attributes.tags, attributes.flags,
Eric Laurent2ac76942017-06-22 17:17:09 -0700816 session, *selectedDeviceId);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700817
818 *stream = streamTypefromAttributesInt(&attributes);
819
820 // Explicit routing?
821 sp<DeviceDescriptor> deviceDesc;
Eric Laurent2ac76942017-06-22 17:17:09 -0700822 if (*selectedDeviceId != AUDIO_PORT_HANDLE_NONE) {
823 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
824 if (mAvailableOutputDevices[i]->getId() == *selectedDeviceId) {
825 deviceDesc = mAvailableOutputDevices[i];
826 break;
827 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700828 }
829 }
830 mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700831
Eric Laurente83b55d2014-11-14 10:06:21 -0800832 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700833 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700834
Eric Laurente83b55d2014-11-14 10:06:21 -0800835 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700836 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
837 }
838
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700839 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Eric Laurent20b9ef02016-12-05 11:03:16 -0800840 device, config->sample_rate, config->format, config->channel_mask, flags);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700841
Kevin Rocard169753c2017-03-06 14:18:23 -0800842 *output = getOutputForDevice(device, session, *stream,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800843 config->sample_rate, config->format, config->channel_mask,
844 flags, &config->offload_info);
Eric Laurente83b55d2014-11-14 10:06:21 -0800845 if (*output == AUDIO_IO_HANDLE_NONE) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700846 mOutputRoutes.removeRoute(session);
Eric Laurente83b55d2014-11-14 10:06:21 -0800847 return INVALID_OPERATION;
848 }
Paul McLeanaa981192015-03-21 09:55:15 -0700849
Eric Laurent2ac76942017-06-22 17:17:09 -0700850 DeviceVector outputDevices = mAvailableOutputDevices.getDevicesFromType(device);
851 *selectedDeviceId = outputDevices.size() > 0 ? outputDevices.itemAt(0)->getId()
852 : AUDIO_PORT_HANDLE_NONE;
853
854 ALOGV(" getOutputForAttr() returns output %d selectedDeviceId %d", *output, *selectedDeviceId);
855
Eric Laurente83b55d2014-11-14 10:06:21 -0800856 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700857}
858
859audio_io_handle_t AudioPolicyManager::getOutputForDevice(
860 audio_devices_t device,
Kevin Rocard169753c2017-03-06 14:18:23 -0800861 audio_session_t session,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700862 audio_stream_type_t stream,
863 uint32_t samplingRate,
864 audio_format_t format,
865 audio_channel_mask_t channelMask,
866 audio_output_flags_t flags,
867 const audio_offload_info_t *offloadInfo)
868{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700869 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700870 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700871
Eric Laurente552edb2014-03-10 17:42:56 -0700872 // open a direct output if required by specified parameters
873 //force direct flag if offload flag is set: offloading implies a direct output stream
874 // and all common behaviors are driven by checking only the direct flag
875 // this should normally be set appropriately in the policy configuration file
876 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700877 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700878 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700879 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
880 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
881 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800882 // only allow deep buffering for music stream type
883 if (stream != AUDIO_STREAM_MUSIC) {
884 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Ravi Kumar Alamanda439e4ed2015-04-03 12:13:21 -0700885 } else if (/* stream == AUDIO_STREAM_MUSIC && */
886 flags == AUDIO_OUTPUT_FLAG_NONE &&
887 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
888 // use DEEP_BUFFER as default output for music stream type
889 flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Eric Laurente83b55d2014-11-14 10:06:21 -0800890 }
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700891 if (stream == AUDIO_STREAM_TTS) {
892 flags = AUDIO_OUTPUT_FLAG_TTS;
Haynes Mathew George84c621e2017-04-25 11:41:50 -0700893 } else if (stream == AUDIO_STREAM_VOICE_CALL &&
Haynes Mathew George84c621e2017-04-25 11:41:50 -0700894 audio_is_linear_pcm(format)) {
895 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX |
896 AUDIO_OUTPUT_FLAG_DIRECT);
897 ALOGV("Set VoIP and Direct output flags for PCM format");
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700898 }
Eric Laurente552edb2014-03-10 17:42:56 -0700899
Eric Laurentb732cf52014-09-24 19:08:21 -0700900 sp<IOProfile> profile;
901
902 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700903 // and not explicitly requested
904 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
Glenn Kasten05ddca52016-02-11 08:17:12 -0800905 audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700906 audio_channel_count_from_out_mask(channelMask) <= 2) {
907 goto non_direct_output;
908 }
909
Andy Hung2ddee192015-12-18 17:34:44 -0800910 // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled.
911 // This prevents creating an offloaded track and tearing it down immediately after start
912 // when audioflinger detects there is an active non offloadable effect.
Eric Laurente552edb2014-03-10 17:42:56 -0700913 // FIXME: We should check the audio session here but we do not have it in this context.
914 // This may prevent offloading in rare situations where effects are left active by apps
915 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700916
Eric Laurente552edb2014-03-10 17:42:56 -0700917 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
Andy Hung2ddee192015-12-18 17:34:44 -0800918 !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700919 profile = getProfileForDirectOutput(device,
920 samplingRate,
921 format,
922 channelMask,
923 (audio_output_flags_t)flags);
924 }
925
Eric Laurent1c333e22014-05-20 10:48:17 -0700926 if (profile != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700927 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700928
929 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700930 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700931 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
932 outputDesc = desc;
Kevin Rocard551061a2017-02-06 15:59:29 -0800933 // reuse direct output if currently open by the same client
934 // and configured with same parameters
Eric Laurente552edb2014-03-10 17:42:56 -0700935 if ((samplingRate == outputDesc->mSamplingRate) &&
Kevin Rocard551061a2017-02-06 15:59:29 -0800936 audio_formats_match(format, outputDesc->mFormat) &&
937 (channelMask == outputDesc->mChannelMask)) {
Kevin Rocard169753c2017-03-06 14:18:23 -0800938 if (session == outputDesc->mDirectClientSession) {
Kevin Rocard551061a2017-02-06 15:59:29 -0800939 outputDesc->mDirectOpenCount++;
Eric Laurentf4e63452017-11-06 19:31:46 +0000940 ALOGV("getOutputForDevice() reusing direct output %d for session %d",
Kevin Rocard169753c2017-03-06 14:18:23 -0800941 mOutputs.keyAt(i), session);
Kevin Rocard551061a2017-02-06 15:59:29 -0800942 return mOutputs.keyAt(i);
943 } else {
Eric Laurentf4e63452017-11-06 19:31:46 +0000944 ALOGV("getOutputForDevice() do not reuse direct output because"
945 "current client (%d) is not the same as requesting client (%d)",
Kevin Rocard169753c2017-03-06 14:18:23 -0800946 outputDesc->mDirectClientSession, session);
Kevin Rocard551061a2017-02-06 15:59:29 -0800947 goto non_direct_output;
948 }
Eric Laurente552edb2014-03-10 17:42:56 -0700949 }
950 }
951 }
952 // close direct output if currently open and configured with different parameters
953 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700954 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700955 }
Eric Laurent861a6282015-05-18 15:40:16 -0700956
957 // if the selected profile is offloaded and no offload info was specified,
958 // create a default one
959 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100960 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
Eric Laurent861a6282015-05-18 15:40:16 -0700961 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
962 defaultOffloadInfo.sample_rate = samplingRate;
963 defaultOffloadInfo.channel_mask = channelMask;
964 defaultOffloadInfo.format = format;
965 defaultOffloadInfo.stream_type = stream;
966 defaultOffloadInfo.bit_rate = 0;
967 defaultOffloadInfo.duration_us = -1;
968 defaultOffloadInfo.has_video = true; // conservative
969 defaultOffloadInfo.is_streaming = true; // likely
970 offloadInfo = &defaultOffloadInfo;
971 }
972
Eric Laurentc75307b2015-03-17 15:29:32 -0700973 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700974 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -0700975 outputDesc->mLatency = 0;
Eric Laurent861a6282015-05-18 15:40:16 -0700976 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700977 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
978 config.sample_rate = samplingRate;
979 config.channel_mask = channelMask;
980 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -0700981 if (offloadInfo != NULL) {
982 config.offload_info = *offloadInfo;
983 }
Eric Laurente3014102017-05-03 11:15:43 -0700984 DeviceVector outputDevices = mAvailableOutputDevices.getDevicesFromType(device);
985 String8 address = outputDevices.size() > 0 ? outputDevices.itemAt(0)->mAddress
986 : String8("");
Eric Laurent322b4d22015-04-03 15:57:54 -0700987 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -0700988 &output,
989 &config,
990 &outputDesc->mDevice,
Eric Laurente3014102017-05-03 11:15:43 -0700991 address,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700992 &outputDesc->mLatency,
993 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -0700994
995 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -0700996 if (status != NO_ERROR ||
997 (samplingRate != 0 && samplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -0800998 (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) ||
Eric Laurentcf2c0212014-07-25 16:20:43 -0700999 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurentf4e63452017-11-06 19:31:46 +00001000 ALOGV("getOutputForDevice() failed opening direct output: output %d samplingRate %d %d,"
Eric Laurente552edb2014-03-10 17:42:56 -07001001 "format %d %d, channelMask %04x %04x", output, samplingRate,
1002 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1003 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001004 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07001005 mpClientInterface->closeOutput(output);
1006 }
Eric Laurenta82797f2015-01-30 11:49:43 -08001007 // fall back to mixer output if possible when the direct output could not be open
Glenn Kasten05ddca52016-02-11 08:17:12 -08001008 if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) {
Eric Laurenta82797f2015-01-30 11:49:43 -08001009 goto non_direct_output;
1010 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001011 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001012 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001013 outputDesc->mSamplingRate = config.sample_rate;
1014 outputDesc->mChannelMask = config.channel_mask;
1015 outputDesc->mFormat = config.format;
1016 outputDesc->mRefCount[stream] = 0;
1017 outputDesc->mStopTime[stream] = 0;
1018 outputDesc->mDirectOpenCount = 1;
Kevin Rocard169753c2017-03-06 14:18:23 -08001019 outputDesc->mDirectClientSession = session;
1020
Eric Laurente552edb2014-03-10 17:42:56 -07001021 addOutput(output, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07001022 mPreviousOutputs = mOutputs;
Eric Laurentf4e63452017-11-06 19:31:46 +00001023 ALOGV("getOutputForDevice() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -07001024 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001025 return output;
1026 }
1027
Eric Laurentb732cf52014-09-24 19:08:21 -07001028non_direct_output:
Eric Laurent14cbfca2016-03-17 09:42:16 -07001029
1030 // A request for HW A/V sync cannot fallback to a mixed output because time
1031 // stamps are embedded in audio data
1032 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1033 return AUDIO_IO_HANDLE_NONE;
1034 }
1035
Eric Laurente552edb2014-03-10 17:42:56 -07001036 // ignoring channel mask due to downmix capability in mixer
1037
1038 // open a non direct output
1039
1040 // for non direct outputs, only PCM is supported
1041 if (audio_is_linear_pcm(format)) {
1042 // get which output is suitable for the specified stream. The actual
1043 // routing change will happen when startOutput() will be called
1044 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1045
Eric Laurent8838a382014-09-08 16:44:28 -07001046 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1047 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1048 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -07001049 }
Eric Laurentf4e63452017-11-06 19:31:46 +00001050 ALOGW_IF((output == 0), "getOutputForDevice() could not find output for stream %d, "
1051 "samplingRate %d, format %d, channels %x, flags %x",
1052 stream, samplingRate, format, channelMask, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001053
Eric Laurente552edb2014-03-10 17:42:56 -07001054 return output;
1055}
1056
Eric Laurente0720872014-03-11 09:30:41 -07001057audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -07001058 audio_output_flags_t flags,
1059 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -07001060{
1061 // select one output among several that provide a path to a particular device or set of
1062 // devices (the list was previously build by getOutputsForDevice()).
1063 // The priority is as follows:
1064 // 1: the output with the highest number of requested policy flags
Eric Laurente6930022016-02-11 10:20:40 -08001065 // 2: the output with the bit depth the closest to the requested one
1066 // 3: the primary output
1067 // 4: the first output in the list
Eric Laurente552edb2014-03-10 17:42:56 -07001068
1069 if (outputs.size() == 0) {
1070 return 0;
1071 }
1072 if (outputs.size() == 1) {
1073 return outputs[0];
1074 }
1075
1076 int maxCommonFlags = 0;
Eric Laurente6930022016-02-11 10:20:40 -08001077 audio_io_handle_t outputForFlags = 0;
1078 audio_io_handle_t outputForPrimary = 0;
1079 audio_io_handle_t outputForFormat = 0;
1080 audio_format_t bestFormat = AUDIO_FORMAT_INVALID;
1081 audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07001082
1083 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001084 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -07001085 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -07001086 // if a valid format is specified, skip output if not compatible
1087 if (format != AUDIO_FORMAT_INVALID) {
1088 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente6930022016-02-11 10:20:40 -08001089 if (!audio_formats_match(format, outputDesc->mFormat)) {
Eric Laurent8838a382014-09-08 16:44:28 -07001090 continue;
1091 }
1092 } else if (!audio_is_linear_pcm(format)) {
1093 continue;
1094 }
Eric Laurente6930022016-02-11 10:20:40 -08001095 if (AudioPort::isBetterFormatMatch(
1096 outputDesc->mFormat, bestFormat, format)) {
1097 outputForFormat = outputs[i];
1098 bestFormat = outputDesc->mFormat;
1099 }
Eric Laurent8838a382014-09-08 16:44:28 -07001100 }
1101
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001102 int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags);
Eric Laurente6930022016-02-11 10:20:40 -08001103 if (commonFlags >= maxCommonFlags) {
1104 if (commonFlags == maxCommonFlags) {
1105 if (AudioPort::isBetterFormatMatch(
1106 outputDesc->mFormat, bestFormatForFlags, format)) {
1107 outputForFlags = outputs[i];
1108 bestFormatForFlags = outputDesc->mFormat;
1109 }
1110 } else {
1111 outputForFlags = outputs[i];
1112 maxCommonFlags = commonFlags;
1113 bestFormatForFlags = outputDesc->mFormat;
1114 }
Eric Laurente552edb2014-03-10 17:42:56 -07001115 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
1116 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001117 if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurente6930022016-02-11 10:20:40 -08001118 outputForPrimary = outputs[i];
Eric Laurente552edb2014-03-10 17:42:56 -07001119 }
1120 }
1121 }
1122
Eric Laurente6930022016-02-11 10:20:40 -08001123 if (outputForFlags != 0) {
1124 return outputForFlags;
Eric Laurente552edb2014-03-10 17:42:56 -07001125 }
Eric Laurente6930022016-02-11 10:20:40 -08001126 if (outputForFormat != 0) {
1127 return outputForFormat;
1128 }
1129 if (outputForPrimary != 0) {
1130 return outputForPrimary;
Eric Laurente552edb2014-03-10 17:42:56 -07001131 }
1132
1133 return outputs[0];
1134}
1135
Eric Laurente0720872014-03-11 09:30:41 -07001136status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001137 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001138 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001139{
Paul McLeanaa981192015-03-21 09:55:15 -07001140 ALOGV("startOutput() output %d, stream %d, session %d",
1141 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -07001142 ssize_t index = mOutputs.indexOfKey(output);
1143 if (index < 0) {
1144 ALOGW("startOutput() unknown output %d", output);
1145 return BAD_VALUE;
1146 }
1147
Eric Laurentc75307b2015-03-17 15:29:32 -07001148 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
1149
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001150 // Routing?
1151 mOutputRoutes.incRouteActivity(session);
1152
Eric Laurentc75307b2015-03-17 15:29:32 -07001153 audio_devices_t newDevice;
Eric Laurentc40d9692016-04-13 19:14:13 -07001154 AudioMix *policyMix = NULL;
1155 const char *address = NULL;
Eric Laurentc75307b2015-03-17 15:29:32 -07001156 if (outputDesc->mPolicyMix != NULL) {
Eric Laurentc40d9692016-04-13 19:14:13 -07001157 policyMix = outputDesc->mPolicyMix;
1158 address = policyMix->mDeviceAddress.string();
1159 if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
1160 newDevice = policyMix->mDeviceType;
1161 } else {
1162 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
1163 }
Eric Laurent493404d2015-04-21 15:07:36 -07001164 } else if (mOutputRoutes.hasRouteChanged(session)) {
1165 newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001166 checkStrategyRoute(getStrategy(stream), output);
Eric Laurentc75307b2015-03-17 15:29:32 -07001167 } else {
1168 newDevice = AUDIO_DEVICE_NONE;
1169 }
1170
1171 uint32_t delayMs = 0;
1172
Eric Laurentc40d9692016-04-13 19:14:13 -07001173 status_t status = startSource(outputDesc, stream, newDevice, address, &delayMs);
Eric Laurentc75307b2015-03-17 15:29:32 -07001174
1175 if (status != NO_ERROR) {
1176 mOutputRoutes.decRouteActivity(session);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001177 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -07001178 }
1179 // Automatically enable the remote submix input when output is started on a re routing mix
1180 // of type MIX_TYPE_RECORDERS
Eric Laurentc40d9692016-04-13 19:14:13 -07001181 if (audio_is_remote_submix_device(newDevice) && policyMix != NULL &&
1182 policyMix->mMixType == MIX_TYPE_RECORDERS) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001183 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1184 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Eric Laurentc40d9692016-04-13 19:14:13 -07001185 address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001186 "remote-submix");
1187 }
1188
1189 if (delayMs != 0) {
1190 usleep(delayMs * 1000);
1191 }
1192
1193 return status;
1194}
1195
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001196status_t AudioPolicyManager::startSource(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurentc75307b2015-03-17 15:29:32 -07001197 audio_stream_type_t stream,
1198 audio_devices_t device,
Eric Laurentc40d9692016-04-13 19:14:13 -07001199 const char *address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001200 uint32_t *delayMs)
1201{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001202 // cannot start playback of STREAM_TTS if any other output is being used
1203 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001204
1205 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001206 if (stream == AUDIO_STREAM_TTS) {
1207 ALOGV("\t found BEACON stream");
Eric Laurent9459fb02015-08-12 18:36:32 -07001208 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001209 return INVALID_OPERATION;
1210 } else {
1211 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1212 }
1213 } else {
1214 // some playback other than beacon starts
1215 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1216 }
1217
Eric Laurent77305a62016-07-25 16:39:22 -07001218 // force device change if the output is inactive and no audio patch is already present.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001219 // check active before incrementing usage count
Eric Laurent77305a62016-07-25 16:39:22 -07001220 bool force = !outputDesc->isActive() &&
1221 (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001222
Andy Hung7c7124e2017-10-20 12:03:34 -07001223 // requiresMuteCheck is false when we can bypass mute strategy.
1224 // It covers a common case when there is no materially active audio
1225 // and muting would result in unnecessary delay and dropped audio.
1226 const uint32_t outputLatencyMs = outputDesc->latency();
1227 bool requiresMuteCheck = outputDesc->isActive(outputLatencyMs * 2); // account for drain
1228
Eric Laurente552edb2014-03-10 17:42:56 -07001229 // increment usage count for this stream on the requested output:
1230 // NOTE that the usage count is the same for duplicated output and hardware output which is
1231 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1232 outputDesc->changeRefCount(stream, 1);
1233
Eric Laurent36829f92017-04-07 19:04:42 -07001234 if (stream == AUDIO_STREAM_MUSIC) {
1235 selectOutputForMusicEffects();
1236 }
1237
Eric Laurent493404d2015-04-21 15:07:36 -07001238 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001239 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001240 if (device == AUDIO_DEVICE_NONE) {
1241 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001242 }
Eric Laurent36829f92017-04-07 19:04:42 -07001243
Eric Laurente552edb2014-03-10 17:42:56 -07001244 routing_strategy strategy = getStrategy(stream);
1245 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001246 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1247 (beaconMuteLatency > 0);
1248 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001249 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001250 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001251 if (desc != outputDesc) {
Andy Hung7c7124e2017-10-20 12:03:34 -07001252 // An output has a shared device if
1253 // - managed by the same hw module
1254 // - supports the currently selected device
1255 const bool sharedDevice = outputDesc->sharesHwModuleWith(desc)
1256 && (desc->supportedDevices() & device) != AUDIO_DEVICE_NONE;
1257
Eric Laurent77305a62016-07-25 16:39:22 -07001258 // force a device change if any other output is:
1259 // - managed by the same hw module
Eric Laurent77305a62016-07-25 16:39:22 -07001260 // - supports currently selected device
Andy Hung7c7124e2017-10-20 12:03:34 -07001261 // - has a current device selection that differs from selected device.
Eric Laurent77305a62016-07-25 16:39:22 -07001262 // - has an active audio patch
Eric Laurente552edb2014-03-10 17:42:56 -07001263 // In this case, the audio HAL must receive the new device selection so that it can
Andy Hung7c7124e2017-10-20 12:03:34 -07001264 // change the device currently selected by the other output.
1265 if (sharedDevice &&
Eric Laurent77305a62016-07-25 16:39:22 -07001266 desc->device() != device &&
Eric Laurent77305a62016-07-25 16:39:22 -07001267 desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07001268 force = true;
1269 }
1270 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001271 // a notification so that audio focus effect can propagate, or that a mute/unmute
1272 // event occurred for beacon
Andy Hung7c7124e2017-10-20 12:03:34 -07001273 const uint32_t latencyMs = desc->latency();
1274 const bool isActive = desc->isActive(latencyMs * 2); // account for drain
1275
1276 if (shouldWait && isActive && (waitMs < latencyMs)) {
1277 waitMs = latencyMs;
Eric Laurente552edb2014-03-10 17:42:56 -07001278 }
Andy Hung7c7124e2017-10-20 12:03:34 -07001279
1280 // Require mute check if another output is on a shared device
1281 // and currently active to have proper drain and avoid pops.
1282 // Note restoring AudioTracks onto this output needs to invoke
1283 // a volume ramp if there is no mute.
1284 requiresMuteCheck |= sharedDevice && isActive;
Eric Laurente552edb2014-03-10 17:42:56 -07001285 }
1286 }
Andy Hung7c7124e2017-10-20 12:03:34 -07001287
1288 const uint32_t muteWaitMs =
1289 setOutputDevice(outputDesc, device, force, 0, NULL, address, requiresMuteCheck);
Eric Laurente552edb2014-03-10 17:42:56 -07001290
1291 // handle special case for sonification while in call
1292 if (isInCall()) {
1293 handleIncallSonification(stream, true, false);
1294 }
1295
1296 // apply volume rules for current stream and device if necessary
1297 checkAndSetVolume(stream,
Shuhei Miyazaki6fe70332017-07-31 15:21:28 +09001298 mVolumeCurves->getVolumeIndex(stream, outputDesc->device()),
Eric Laurentc75307b2015-03-17 15:29:32 -07001299 outputDesc,
Shuhei Miyazaki6fe70332017-07-31 15:21:28 +09001300 outputDesc->device());
Eric Laurente552edb2014-03-10 17:42:56 -07001301
1302 // update the outputs if starting an output with a stream that can affect notification
1303 // routing
1304 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001305
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001306 // force reevaluating accessibility routing when ringtone or alarm starts
1307 if (strategy == STRATEGY_SONIFICATION) {
1308 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1309 }
Eric Laurentdc462862016-07-19 12:29:53 -07001310
1311 if (waitMs > muteWaitMs) {
1312 *delayMs = waitMs - muteWaitMs;
1313 }
Andy Hung7c7124e2017-10-20 12:03:34 -07001314
1315 // FIXME: A device change (muteWaitMs > 0) likely introduces a volume change.
1316 // A volume change enacted by APM with 0 delay is not synchronous, as it goes
1317 // via AudioCommandThread to AudioFlinger. Hence it is possible that the volume
1318 // change occurs after the MixerThread starts and causes a stream volume
1319 // glitch.
1320 //
1321 // We do not introduce additional delay here.
Eric Laurente552edb2014-03-10 17:42:56 -07001322 }
Eric Laurentdc462862016-07-19 12:29:53 -07001323
Eric Laurente552edb2014-03-10 17:42:56 -07001324 return NO_ERROR;
1325}
1326
1327
Eric Laurente0720872014-03-11 09:30:41 -07001328status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001329 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001330 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001331{
1332 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1333 ssize_t index = mOutputs.indexOfKey(output);
1334 if (index < 0) {
1335 ALOGW("stopOutput() unknown output %d", output);
1336 return BAD_VALUE;
1337 }
1338
Eric Laurentc75307b2015-03-17 15:29:32 -07001339 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001340
Eric Laurentc75307b2015-03-17 15:29:32 -07001341 if (outputDesc->mRefCount[stream] == 1) {
1342 // Automatically disable the remote submix input when output is stopped on a
1343 // re routing mix of type MIX_TYPE_RECORDERS
1344 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1345 outputDesc->mPolicyMix != NULL &&
1346 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1347 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1348 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001349 outputDesc->mPolicyMix->mDeviceAddress,
Eric Laurentc75307b2015-03-17 15:29:32 -07001350 "remote-submix");
1351 }
1352 }
1353
1354 // Routing?
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001355 bool forceDeviceUpdate = false;
Eric Laurentc75307b2015-03-17 15:29:32 -07001356 if (outputDesc->mRefCount[stream] > 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001357 int activityCount = mOutputRoutes.decRouteActivity(session);
1358 forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0));
1359
1360 if (forceDeviceUpdate) {
1361 checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE);
1362 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001363 }
1364
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001365 return stopSource(outputDesc, stream, forceDeviceUpdate);
Eric Laurentc75307b2015-03-17 15:29:32 -07001366}
1367
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001368status_t AudioPolicyManager::stopSource(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001369 audio_stream_type_t stream,
1370 bool forceDeviceUpdate)
Eric Laurentc75307b2015-03-17 15:29:32 -07001371{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001372 // always handle stream stop, check which stream type is stopping
1373 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1374
Eric Laurente552edb2014-03-10 17:42:56 -07001375 // handle special case for sonification while in call
1376 if (isInCall()) {
1377 handleIncallSonification(stream, false, false);
1378 }
1379
1380 if (outputDesc->mRefCount[stream] > 0) {
1381 // decrement usage count of this stream on the output
1382 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001383
Eric Laurente552edb2014-03-10 17:42:56 -07001384 // store time at which the stream was stopped - see isStreamActive()
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001385 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
Eric Laurente552edb2014-03-10 17:42:56 -07001386 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001387 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001388 // delay the device switch by twice the latency because stopOutput() is executed when
1389 // the track stop() command is received and at that time the audio track buffer can
1390 // still contain data that needs to be drained. The latency only covers the audio HAL
1391 // and kernel buffers. Also the latency does not always include additional delay in the
1392 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001393 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001394
1395 // force restoring the device selection on other active outputs if it differs from the
1396 // one being selected for this output
Eric Laurent57de36c2016-09-28 16:59:11 -07001397 uint32_t delayMs = outputDesc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -07001398 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001399 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001400 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001401 desc->isActive() &&
1402 outputDesc->sharesHwModuleWith(desc) &&
1403 (newDevice != desc->device())) {
Haynes Mathew George11c499a2016-08-26 12:08:25 -07001404 audio_devices_t newDevice2 = getNewOutputDevice(desc, false /*fromCache*/);
1405 bool force = desc->device() != newDevice2;
Eric Laurentc75307b2015-03-17 15:29:32 -07001406 setOutputDevice(desc,
Haynes Mathew George11c499a2016-08-26 12:08:25 -07001407 newDevice2,
1408 force,
Eric Laurent57de36c2016-09-28 16:59:11 -07001409 delayMs);
1410 // re-apply device specific volume if not done by setOutputDevice()
1411 if (!force) {
1412 applyStreamVolumes(desc, newDevice2, delayMs);
1413 }
Eric Laurente552edb2014-03-10 17:42:56 -07001414 }
1415 }
1416 // update the outputs if stopping one with a stream that can affect notification routing
1417 handleNotificationRoutingForStream(stream);
1418 }
Eric Laurent36829f92017-04-07 19:04:42 -07001419 if (stream == AUDIO_STREAM_MUSIC) {
1420 selectOutputForMusicEffects();
1421 }
Eric Laurente552edb2014-03-10 17:42:56 -07001422 return NO_ERROR;
1423 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001424 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001425 return INVALID_OPERATION;
1426 }
1427}
1428
Eric Laurente83b55d2014-11-14 10:06:21 -08001429void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001430 audio_stream_type_t stream __unused,
1431 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001432{
1433 ALOGV("releaseOutput() %d", output);
1434 ssize_t index = mOutputs.indexOfKey(output);
1435 if (index < 0) {
1436 ALOGW("releaseOutput() releasing unknown output %d", output);
1437 return;
1438 }
1439
Paul McLeanaa981192015-03-21 09:55:15 -07001440 // Routing
1441 mOutputRoutes.removeRoute(session);
1442
Eric Laurentc75307b2015-03-17 15:29:32 -07001443 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001444 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001445 if (desc->mDirectOpenCount <= 0) {
1446 ALOGW("releaseOutput() invalid open count %d for output %d",
1447 desc->mDirectOpenCount, output);
1448 return;
1449 }
1450 if (--desc->mDirectOpenCount == 0) {
1451 closeOutput(output);
Eric Laurentb52c1522014-05-20 11:27:36 -07001452 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001453 }
1454 }
1455}
1456
1457
Eric Laurentcaf7f482014-11-25 17:50:47 -08001458status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1459 audio_io_handle_t *input,
1460 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001461 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001462 const audio_config_base_t *config,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001463 audio_input_flags_t flags,
Eric Laurent2ac76942017-06-22 17:17:09 -07001464 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001465 input_type_t *inputType,
1466 audio_port_handle_t *portId)
Eric Laurente552edb2014-03-10 17:42:56 -07001467{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001468 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1469 "session %d, flags %#x",
Eric Laurent20b9ef02016-12-05 11:03:16 -08001470 attr->source, config->sample_rate, config->format, config->channel_mask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001471
Eric Laurentad2e7b92017-09-14 20:06:42 -07001472 status_t status = NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -08001473 // handle legacy remote submix case where the address was not always specified
1474 String8 address = String8("");
Eric Laurentc447ded2015-01-06 08:47:05 -08001475 audio_source_t halInputSource;
Eric Laurentad2e7b92017-09-14 20:06:42 -07001476 audio_source_t inputSource = attr->source;
Eric Laurentc722f302014-12-10 11:21:49 -08001477 AudioMix *policyMix = NULL;
Eric Laurentad2e7b92017-09-14 20:06:42 -07001478 DeviceVector inputDevices;
Eric Laurent20b9ef02016-12-05 11:03:16 -08001479
Paul McLean466dc8e2015-04-17 13:15:36 -06001480 // Explicit routing?
1481 sp<DeviceDescriptor> deviceDesc;
Eric Laurent2ac76942017-06-22 17:17:09 -07001482 if (*selectedDeviceId != AUDIO_PORT_HANDLE_NONE) {
1483 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1484 if (mAvailableInputDevices[i]->getId() == *selectedDeviceId) {
1485 deviceDesc = mAvailableInputDevices[i];
1486 break;
1487 }
Paul McLean466dc8e2015-04-17 13:15:36 -06001488 }
1489 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001490 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001491
Eric Laurentad2e7b92017-09-14 20:06:42 -07001492 // special case for mmap capture: if an input IO handle is specified, we reuse this input if
1493 // possible
1494 if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) == AUDIO_INPUT_FLAG_MMAP_NOIRQ &&
1495 *input != AUDIO_IO_HANDLE_NONE) {
1496 ssize_t index = mInputs.indexOfKey(*input);
1497 if (index < 0) {
1498 ALOGW("getInputForAttr() unknown MMAP input %d", *input);
1499 status = BAD_VALUE;
1500 goto error;
1501 }
1502 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1503 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1504 if (audioSession == 0) {
1505 ALOGW("getInputForAttr() unknown session %d on input %d", session, *input);
1506 status = BAD_VALUE;
1507 goto error;
1508 }
1509 // For MMAP mode, the first call to getInputForAttr() is made on behalf of audioflinger.
1510 // The second call is for the first active client and sets the UID. Any further call
1511 // corresponds to a new client and is only permitted from the same UId.
1512 if (audioSession->openCount() == 1) {
1513 audioSession->setUid(uid);
1514 } else if (audioSession->uid() != uid) {
1515 ALOGW("getInputForAttr() bad uid %d for session %d uid %d",
1516 uid, session, audioSession->uid());
1517 status = INVALID_OPERATION;
1518 goto error;
1519 }
1520 audioSession->changeOpenCount(1);
1521 *inputType = API_INPUT_LEGACY;
1522 if (*portId == AUDIO_PORT_HANDLE_NONE) {
1523 *portId = AudioPort::getNextUniqueId();
1524 }
1525 inputDevices = mAvailableInputDevices.getDevicesFromType(inputDesc->mDevice);
1526 *selectedDeviceId = inputDevices.size() > 0 ? inputDevices.itemAt(0)->getId()
1527 : AUDIO_PORT_HANDLE_NONE;
1528 ALOGI("%s reusing MMAP input %d for session %d", __FUNCTION__, *input, session);
1529
1530 return NO_ERROR;
1531 }
1532
1533 *input = AUDIO_IO_HANDLE_NONE;
1534 *inputType = API_INPUT_INVALID;
1535
1536 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1537 inputSource = AUDIO_SOURCE_MIC;
1538 }
1539 halInputSource = inputSource;
1540
1541 // TODO: check for existing client for this port ID
1542 if (*portId == AUDIO_PORT_HANDLE_NONE) {
1543 *portId = AudioPort::getNextUniqueId();
1544 }
1545
1546 audio_devices_t device;
1547
Eric Laurentc447ded2015-01-06 08:47:05 -08001548 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001549 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Eric Laurentad2e7b92017-09-14 20:06:42 -07001550 status = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
1551 if (status != NO_ERROR) {
1552 goto error;
François Gaffie036e1e92015-03-19 10:16:24 +01001553 }
1554 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001555 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1556 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001557 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001558 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001559 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001560 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurentad2e7b92017-09-14 20:06:42 -07001561 status = BAD_VALUE;
1562 goto error;
Eric Laurent275e8e92014-11-30 15:14:47 -08001563 }
Eric Laurentc722f302014-12-10 11:21:49 -08001564 if (policyMix != NULL) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001565 address = policyMix->mDeviceAddress;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001566 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1567 // there is an external policy, but this input is attached to a mix of recorders,
1568 // meaning it receives audio injected into the framework, so the recorder doesn't
1569 // know about it and is therefore considered "legacy"
1570 *inputType = API_INPUT_LEGACY;
1571 } else {
1572 // recording a mix of players defined by an external policy, we're rerouting for
1573 // an external policy
1574 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1575 }
Eric Laurentc722f302014-12-10 11:21:49 -08001576 } else if (audio_is_remote_submix_device(device)) {
1577 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001578 *inputType = API_INPUT_MIX_CAPTURE;
Eric Laurent82db2692015-08-07 13:59:42 -07001579 } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
1580 *inputType = API_INPUT_TELEPHONY_RX;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001581 } else {
1582 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001583 }
Ravi Kumar Alamandab367f5b2015-08-25 08:21:37 -07001584
Eric Laurent599c7582015-12-07 18:05:55 -08001585 }
1586
1587 *input = getInputForDevice(device, address, session, uid, inputSource,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001588 config->sample_rate, config->format, config->channel_mask, flags,
Eric Laurent599c7582015-12-07 18:05:55 -08001589 policyMix);
1590 if (*input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentad2e7b92017-09-14 20:06:42 -07001591 status = INVALID_OPERATION;
1592 goto error;
Eric Laurent599c7582015-12-07 18:05:55 -08001593 }
Eric Laurent20b9ef02016-12-05 11:03:16 -08001594
Eric Laurentad2e7b92017-09-14 20:06:42 -07001595 inputDevices = mAvailableInputDevices.getDevicesFromType(device);
Eric Laurent2ac76942017-06-22 17:17:09 -07001596 *selectedDeviceId = inputDevices.size() > 0 ? inputDevices.itemAt(0)->getId()
1597 : AUDIO_PORT_HANDLE_NONE;
1598
1599 ALOGV("getInputForAttr() returns input %d type %d selectedDeviceId %d",
1600 *input, *inputType, *selectedDeviceId);
1601
Eric Laurent599c7582015-12-07 18:05:55 -08001602 return NO_ERROR;
Eric Laurentad2e7b92017-09-14 20:06:42 -07001603
1604error:
1605 mInputRoutes.removeRoute(session);
1606 return status;
Eric Laurent599c7582015-12-07 18:05:55 -08001607}
1608
1609
1610audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device,
1611 String8 address,
1612 audio_session_t session,
1613 uid_t uid,
1614 audio_source_t inputSource,
1615 uint32_t samplingRate,
1616 audio_format_t format,
1617 audio_channel_mask_t channelMask,
1618 audio_input_flags_t flags,
1619 AudioMix *policyMix)
1620{
1621 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
1622 audio_source_t halInputSource = inputSource;
1623 bool isSoundTrigger = false;
1624
1625 if (inputSource == AUDIO_SOURCE_HOTWORD) {
1626 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1627 if (index >= 0) {
1628 input = mSoundTriggerSessions.valueFor(session);
1629 isSoundTrigger = true;
1630 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1631 ALOGV("SoundTrigger capture on session %d input %d", session, input);
1632 } else {
1633 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001634 }
Haynes Mathew George851d3ff2017-06-19 20:01:57 -07001635 } else if (inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION &&
Haynes Mathew George851d3ff2017-06-19 20:01:57 -07001636 audio_is_linear_pcm(format)) {
1637 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_VOIP_TX);
Eric Laurent5dbe4712014-09-19 19:04:57 -07001638 }
1639
Andy Hungf129b032015-04-07 13:45:50 -07001640 // find a compatible input profile (not necessarily identical in parameters)
1641 sp<IOProfile> profile;
1642 // samplingRate and flags may be updated by getInputProfile
Glenn Kasten05ddca52016-02-11 08:17:12 -08001643 uint32_t profileSamplingRate = (samplingRate == 0) ? SAMPLE_RATE_HZ_DEFAULT : samplingRate;
Andy Hungf129b032015-04-07 13:45:50 -07001644 audio_format_t profileFormat = format;
1645 audio_channel_mask_t profileChannelMask = channelMask;
1646 audio_input_flags_t profileFlags = flags;
1647 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001648 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001649 profileSamplingRate, profileFormat, profileChannelMask,
1650 profileFlags);
1651 if (profile != 0) {
1652 break; // success
Eric Laurent05067782016-06-01 18:27:28 -07001653 } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) {
1654 profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry
Andy Hungf129b032015-04-07 13:45:50 -07001655 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1656 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1657 } else { // fail
Glenn Kastenfaa10a62017-05-25 15:52:05 -07001658 ALOGW("getInputForDevice() could not find profile for device 0x%X, "
Eric Laurent599c7582015-12-07 18:05:55 -08001659 "samplingRate %u, format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001660 device, samplingRate, format, channelMask, flags);
Eric Laurent599c7582015-12-07 18:05:55 -08001661 return input;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001662 }
Eric Laurente552edb2014-03-10 17:42:56 -07001663 }
Glenn Kasten05ddca52016-02-11 08:17:12 -08001664 // Pick input sampling rate if not specified by client
1665 if (samplingRate == 0) {
1666 samplingRate = profileSamplingRate;
1667 }
Eric Laurente552edb2014-03-10 17:42:56 -07001668
Eric Laurent322b4d22015-04-03 15:57:54 -07001669 if (profile->getModuleHandle() == 0) {
1670 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurent599c7582015-12-07 18:05:55 -08001671 return input;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001672 }
1673
Eric Laurent599c7582015-12-07 18:05:55 -08001674 sp<AudioSession> audioSession = new AudioSession(session,
1675 inputSource,
1676 format,
1677 samplingRate,
1678 channelMask,
1679 flags,
1680 uid,
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001681 isSoundTrigger,
1682 policyMix, mpClientInterface);
Eric Laurent599c7582015-12-07 18:05:55 -08001683
Eric Laurent74708e72017-04-07 17:13:42 -07001684// FIXME: disable concurrent capture until UI is ready
1685#if 0
Eric Laurent599c7582015-12-07 18:05:55 -08001686 // reuse an open input if possible
Eric Laurent555530a2017-02-07 18:17:24 -08001687 sp<AudioInputDescriptor> reusedInputDesc;
Eric Laurent599c7582015-12-07 18:05:55 -08001688 for (size_t i = 0; i < mInputs.size(); i++) {
1689 sp<AudioInputDescriptor> desc = mInputs.valueAt(i);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001690 // reuse input if:
1691 // - it shares the same profile
1692 // AND
1693 // - it is not a reroute submix input
1694 // AND
1695 // - it is: not used for sound trigger
1696 // OR
1697 // used for sound trigger and all clients use the same session ID
1698 //
1699 if ((profile == desc->mProfile) &&
1700 (isSoundTrigger == desc->isSoundTrigger()) &&
1701 !is_virtual_input_device(device)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001702
1703 sp<AudioSession> as = desc->getAudioSession(session);
1704 if (as != 0) {
1705 // do not allow unmatching properties on same session
1706 if (as->matches(audioSession)) {
1707 as->changeOpenCount(1);
1708 } else {
1709 ALOGW("getInputForDevice() record with different attributes"
1710 " exists for session %d", session);
Eric Laurent555530a2017-02-07 18:17:24 -08001711 continue;
Eric Laurent599c7582015-12-07 18:05:55 -08001712 }
Eric Laurentfb66dd92016-01-28 18:32:03 -08001713 } else if (isSoundTrigger) {
Eric Laurent555530a2017-02-07 18:17:24 -08001714 continue;
Eric Laurentfb66dd92016-01-28 18:32:03 -08001715 }
Eric Laurentbb948092017-01-23 18:33:30 -08001716
Eric Laurent555530a2017-02-07 18:17:24 -08001717 // Reuse the already opened input stream on this profile if:
1718 // - the new capture source is background OR
1719 // - the path requested configurations match OR
1720 // - the new source priority is less than the highest source priority on this input
1721 // If the input stream cannot be reused, close it before opening a new stream
1722 // on the same profile for the new client so that the requested path configuration
1723 // can be selected.
1724 if (!isConcurrentSource(inputSource) &&
Eric Laurentbb948092017-01-23 18:33:30 -08001725 ((desc->mSamplingRate != samplingRate ||
Eric Laurentfb66dd92016-01-28 18:32:03 -08001726 desc->mChannelMask != channelMask ||
Eric Laurente6930022016-02-11 10:20:40 -08001727 !audio_formats_match(desc->mFormat, format)) &&
Eric Laurentfb66dd92016-01-28 18:32:03 -08001728 (source_priority(desc->getHighestPrioritySource(false /*activeOnly*/)) <
Eric Laurentbb948092017-01-23 18:33:30 -08001729 source_priority(inputSource)))) {
Eric Laurent555530a2017-02-07 18:17:24 -08001730 reusedInputDesc = desc;
1731 continue;
Eric Laurent599c7582015-12-07 18:05:55 -08001732 } else {
1733 desc->addAudioSession(session, audioSession);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001734 ALOGV("%s: reusing input %d", __FUNCTION__, mInputs.keyAt(i));
1735 return mInputs.keyAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08001736 }
Eric Laurent599c7582015-12-07 18:05:55 -08001737 }
1738 }
Eric Laurent599c7582015-12-07 18:05:55 -08001739
Eric Laurent555530a2017-02-07 18:17:24 -08001740 if (reusedInputDesc != 0) {
1741 AudioSessionCollection sessions = reusedInputDesc->getAudioSessions(false /*activeOnly*/);
1742 for (size_t j = 0; j < sessions.size(); j++) {
1743 audio_session_t currentSession = sessions.keyAt(j);
1744 stopInput(reusedInputDesc->mIoHandle, currentSession);
1745 releaseInput(reusedInputDesc->mIoHandle, currentSession);
1746 }
1747 }
Eric Laurent74708e72017-04-07 17:13:42 -07001748#endif
Eric Laurent555530a2017-02-07 18:17:24 -08001749
Eric Laurentcf2c0212014-07-25 16:20:43 -07001750 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001751 config.sample_rate = profileSamplingRate;
1752 config.channel_mask = profileChannelMask;
1753 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001754
Eric Laurente3014102017-05-03 11:15:43 -07001755 if (address == "") {
1756 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(device);
1757 // the inputs vector must be of size 1, but we don't want to crash here
1758 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress : String8("");
1759 }
1760
Eric Laurent322b4d22015-04-03 15:57:54 -07001761 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurent599c7582015-12-07 18:05:55 -08001762 &input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001763 &config,
1764 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001765 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001766 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001767 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001768
1769 // only accept input with the exact requested set of parameters
Eric Laurent599c7582015-12-07 18:05:55 -08001770 if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001771 (profileSamplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -08001772 !audio_formats_match(profileFormat, config.format) ||
Andy Hungf129b032015-04-07 13:45:50 -07001773 (profileChannelMask != config.channel_mask)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001774 ALOGW("getInputForAttr() failed opening input: samplingRate %d"
1775 ", format %d, channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001776 samplingRate, format, channelMask);
Eric Laurent599c7582015-12-07 18:05:55 -08001777 if (input != AUDIO_IO_HANDLE_NONE) {
1778 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001779 }
Eric Laurent599c7582015-12-07 18:05:55 -08001780 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001781 }
1782
Eric Laurent1f2f2232014-06-02 12:01:23 -07001783 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Andy Hungf129b032015-04-07 13:45:50 -07001784 inputDesc->mSamplingRate = profileSamplingRate;
1785 inputDesc->mFormat = profileFormat;
1786 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001787 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001788 inputDesc->mPolicyMix = policyMix;
Eric Laurent599c7582015-12-07 18:05:55 -08001789 inputDesc->addAudioSession(session, audioSession);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001790
Eric Laurent599c7582015-12-07 18:05:55 -08001791 addInput(input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001792 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001793
Eric Laurent599c7582015-12-07 18:05:55 -08001794 return input;
Eric Laurente552edb2014-03-10 17:42:56 -07001795}
1796
Eric Laurentbb948092017-01-23 18:33:30 -08001797//static
1798bool AudioPolicyManager::isConcurrentSource(audio_source_t source)
1799{
1800 return (source == AUDIO_SOURCE_HOTWORD) ||
1801 (source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
1802 (source == AUDIO_SOURCE_FM_TUNER);
1803}
1804
Eric Laurentfb66dd92016-01-28 18:32:03 -08001805bool AudioPolicyManager::isConcurentCaptureAllowed(const sp<AudioInputDescriptor>& inputDesc,
1806 const sp<AudioSession>& audioSession)
1807{
1808 // Do not allow capture if an active voice call is using a software patch and
1809 // the call TX source device is on the same HW module.
1810 // FIXME: would be better to refine to only inputs whose profile connects to the
1811 // call TX device but this information is not in the audio patch
1812 if (mCallTxPatch != 0 &&
1813 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1814 return false;
1815 }
1816
1817 // starting concurrent capture is enabled if:
1818 // 1) capturing for re-routing
1819 // 2) capturing for HOTWORD source
1820 // 3) capturing for FM TUNER source
1821 // 3) All other active captures are either for re-routing or HOTWORD
1822
1823 if (is_virtual_input_device(inputDesc->mDevice) ||
Eric Laurentbb948092017-01-23 18:33:30 -08001824 isConcurrentSource(audioSession->inputSource())) {
Eric Laurentfb66dd92016-01-28 18:32:03 -08001825 return true;
1826 }
1827
1828 Vector< sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
1829 for (size_t i = 0; i < activeInputs.size(); i++) {
1830 sp<AudioInputDescriptor> activeInput = activeInputs[i];
Eric Laurentbb948092017-01-23 18:33:30 -08001831 if (!isConcurrentSource(activeInput->inputSource(true)) &&
Eric Laurentfb66dd92016-01-28 18:32:03 -08001832 !is_virtual_input_device(activeInput->mDevice)) {
1833 return false;
1834 }
1835 }
1836
1837 return true;
1838}
1839
Chris Thornton2b864642017-06-27 21:26:07 -07001840// FIXME: remove when concurrent capture is ready. This is a hack to work around bug b/63083537.
1841bool AudioPolicyManager::soundTriggerSupportsConcurrentCapture() {
1842 if (!mHasComputedSoundTriggerSupportsConcurrentCapture) {
1843 bool soundTriggerSupportsConcurrentCapture = false;
1844 unsigned int numModules = 0;
1845 struct sound_trigger_module_descriptor* nModules = NULL;
1846
1847 status_t status = SoundTrigger::listModules(nModules, &numModules);
1848 if (status == NO_ERROR && numModules != 0) {
1849 nModules = (struct sound_trigger_module_descriptor*) calloc(
1850 numModules, sizeof(struct sound_trigger_module_descriptor));
1851 if (nModules == NULL) {
1852 // We failed to malloc the buffer, so just say no for now, and hope that we have more
1853 // ram the next time this function is called.
1854 ALOGE("Failed to allocate buffer for module descriptors");
1855 return false;
1856 }
1857
1858 status = SoundTrigger::listModules(nModules, &numModules);
1859 if (status == NO_ERROR) {
1860 soundTriggerSupportsConcurrentCapture = true;
1861 for (size_t i = 0; i < numModules; ++i) {
1862 soundTriggerSupportsConcurrentCapture &=
1863 nModules[i].properties.concurrent_capture;
1864 }
1865 }
1866 free(nModules);
1867 }
1868 mSoundTriggerSupportsConcurrentCapture = soundTriggerSupportsConcurrentCapture;
1869 mHasComputedSoundTriggerSupportsConcurrentCapture = true;
1870 }
1871 return mSoundTriggerSupportsConcurrentCapture;
1872}
1873
Eric Laurentfb66dd92016-01-28 18:32:03 -08001874
Eric Laurent4dc68062014-07-28 17:26:49 -07001875status_t AudioPolicyManager::startInput(audio_io_handle_t input,
Eric Laurentfb66dd92016-01-28 18:32:03 -08001876 audio_session_t session,
1877 concurrency_type__mask_t *concurrency)
Eric Laurente552edb2014-03-10 17:42:56 -07001878{
1879 ALOGV("startInput() input %d", input);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001880 *concurrency = API_INPUT_CONCURRENCY_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001881 ssize_t index = mInputs.indexOfKey(input);
1882 if (index < 0) {
1883 ALOGW("startInput() unknown input %d", input);
1884 return BAD_VALUE;
1885 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001886 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001887
Eric Laurent599c7582015-12-07 18:05:55 -08001888 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1889 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07001890 ALOGW("startInput() unknown session %d on input %d", session, input);
1891 return BAD_VALUE;
1892 }
1893
Eric Laurent74708e72017-04-07 17:13:42 -07001894// FIXME: disable concurrent capture until UI is ready
1895#if 0
Eric Laurentfb66dd92016-01-28 18:32:03 -08001896 if (!isConcurentCaptureAllowed(inputDesc, audioSession)) {
1897 ALOGW("startInput(%d) failed: other input already started", input);
1898 return INVALID_OPERATION;
1899 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001900
Eric Laurentfb66dd92016-01-28 18:32:03 -08001901 if (isInCall()) {
1902 *concurrency |= API_INPUT_CONCURRENCY_CALL;
1903 }
Eric Laurentf7c50102016-09-30 15:19:43 -07001904 if (mInputs.activeInputsCountOnDevices() != 0) {
Eric Laurentfb66dd92016-01-28 18:32:03 -08001905 *concurrency |= API_INPUT_CONCURRENCY_CAPTURE;
Eric Laurente552edb2014-03-10 17:42:56 -07001906 }
Eric Laurent74708e72017-04-07 17:13:42 -07001907#else
1908 if (!is_virtual_input_device(inputDesc->mDevice)) {
1909 if (mCallTxPatch != 0 &&
1910 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1911 ALOGW("startInput(%d) failed: call in progress", input);
1912 return INVALID_OPERATION;
1913 }
1914
1915 Vector< sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
1916 for (size_t i = 0; i < activeInputs.size(); i++) {
1917 sp<AudioInputDescriptor> activeDesc = activeInputs[i];
1918
1919 if (is_virtual_input_device(activeDesc->mDevice)) {
1920 continue;
1921 }
1922
Eric Laurentcb4dae22017-07-01 19:39:32 -07001923 if ((audioSession->flags() & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0 &&
1924 activeDesc->getId() == inputDesc->getId()) {
1925 continue;
1926 }
1927
Eric Laurent74708e72017-04-07 17:13:42 -07001928 audio_source_t activeSource = activeDesc->inputSource(true);
1929 if (audioSession->inputSource() == AUDIO_SOURCE_HOTWORD) {
1930 if (activeSource == AUDIO_SOURCE_HOTWORD) {
1931 if (activeDesc->hasPreemptedSession(session)) {
1932 ALOGW("startInput(%d) failed for HOTWORD: "
1933 "other input %d already started for HOTWORD",
1934 input, activeDesc->mIoHandle);
1935 return INVALID_OPERATION;
1936 }
1937 } else {
1938 ALOGV("startInput(%d) failed for HOTWORD: other input %d already started",
1939 input, activeDesc->mIoHandle);
1940 return INVALID_OPERATION;
1941 }
1942 } else {
1943 if (activeSource != AUDIO_SOURCE_HOTWORD) {
1944 ALOGW("startInput(%d) failed: other input %d already started",
1945 input, activeDesc->mIoHandle);
1946 return INVALID_OPERATION;
1947 }
1948 }
1949 }
1950
Chris Thornton2b864642017-06-27 21:26:07 -07001951 // We only need to check if the sound trigger session supports concurrent capture if the
1952 // input is also a sound trigger input. Otherwise, we should preempt any hotword stream
1953 // that's running.
1954 const bool allowConcurrentWithSoundTrigger =
1955 inputDesc->isSoundTrigger() ? soundTriggerSupportsConcurrentCapture() : false;
1956
Eric Laurent74708e72017-04-07 17:13:42 -07001957 // if capture is allowed, preempt currently active HOTWORD captures
1958 for (size_t i = 0; i < activeInputs.size(); i++) {
1959 sp<AudioInputDescriptor> activeDesc = activeInputs[i];
1960
1961 if (is_virtual_input_device(activeDesc->mDevice)) {
1962 continue;
1963 }
1964
Chris Thornton2b864642017-06-27 21:26:07 -07001965 if (allowConcurrentWithSoundTrigger && activeDesc->isSoundTrigger()) {
1966 continue;
1967 }
1968
Eric Laurent74708e72017-04-07 17:13:42 -07001969 audio_source_t activeSource = activeDesc->inputSource(true);
1970 if (activeSource == AUDIO_SOURCE_HOTWORD) {
1971 AudioSessionCollection activeSessions =
1972 activeDesc->getAudioSessions(true /*activeOnly*/);
1973 audio_session_t activeSession = activeSessions.keyAt(0);
1974 audio_io_handle_t activeHandle = activeDesc->mIoHandle;
1975 SortedVector<audio_session_t> sessions = activeDesc->getPreemptedSessions();
1976 sessions.add(activeSession);
1977 inputDesc->setPreemptedSessions(sessions);
1978 stopInput(activeHandle, activeSession);
1979 releaseInput(activeHandle, activeSession);
1980 ALOGV("startInput(%d) for HOTWORD preempting HOTWORD input %d",
1981 input, activeDesc->mIoHandle);
1982 }
1983 }
1984 }
1985#endif
Eric Laurente552edb2014-03-10 17:42:56 -07001986
Eric Laurent313d1e72016-01-29 09:56:57 -08001987 // increment activity count before calling getNewInputDevice() below as only active sessions
1988 // are considered for device selection
1989 audioSession->changeActiveCount(1);
1990
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001991 // Routing?
1992 mInputRoutes.incRouteActivity(session);
1993
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001994 if (audioSession->activeCount() == 1 || mInputRoutes.hasRouteChanged(session)) {
Eric Laurent271a93e2016-09-29 14:47:06 -07001995 // indicate active capture to sound trigger service if starting capture from a mic on
1996 // primary HW module
Eric Laurentf7c50102016-09-30 15:19:43 -07001997 audio_devices_t device = getNewInputDevice(inputDesc);
Eric Laurent271a93e2016-09-29 14:47:06 -07001998 setInputDevice(input, device, true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001999
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07002000 if (inputDesc->getAudioSessionCount(true/*activeOnly*/) == 1) {
2001 // if input maps to a dynamic policy with an activity listener, notify of state change
2002 if ((inputDesc->mPolicyMix != NULL)
2003 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
2004 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
2005 MIX_STATE_MIXING);
Eric Laurentc722f302014-12-10 11:21:49 -08002006 }
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07002007
Eric Laurentf7c50102016-09-30 15:19:43 -07002008 audio_devices_t primaryInputDevices = availablePrimaryInputDevices();
2009 if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) &&
Eric Laurent05b345e2016-11-18 12:36:27 -08002010 mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) {
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07002011 SoundTrigger::setCaptureState(true);
2012 }
2013
2014 // automatically enable the remote submix output when input is started if not
2015 // used by a policy mix of type MIX_TYPE_RECORDERS
2016 // For remote submix (a virtual device), we open only one input per capture request.
2017 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
2018 String8 address = String8("");
2019 if (inputDesc->mPolicyMix == NULL) {
2020 address = String8("0");
2021 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
2022 address = inputDesc->mPolicyMix->mDeviceAddress;
2023 }
2024 if (address != "") {
2025 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2026 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2027 address, "remote-submix");
2028 }
Eric Laurentc722f302014-12-10 11:21:49 -08002029 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07002030 }
Eric Laurente552edb2014-03-10 17:42:56 -07002031 }
2032
Eric Laurent599c7582015-12-07 18:05:55 -08002033 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Eric Laurente552edb2014-03-10 17:42:56 -07002034
Eric Laurente552edb2014-03-10 17:42:56 -07002035 return NO_ERROR;
2036}
2037
Eric Laurent4dc68062014-07-28 17:26:49 -07002038status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
2039 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07002040{
2041 ALOGV("stopInput() input %d", input);
2042 ssize_t index = mInputs.indexOfKey(input);
2043 if (index < 0) {
2044 ALOGW("stopInput() unknown input %d", input);
2045 return BAD_VALUE;
2046 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07002047 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07002048
Eric Laurent599c7582015-12-07 18:05:55 -08002049 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07002050 if (index < 0) {
2051 ALOGW("stopInput() unknown session %d on input %d", session, input);
2052 return BAD_VALUE;
2053 }
2054
Eric Laurent599c7582015-12-07 18:05:55 -08002055 if (audioSession->activeCount() == 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07002056 ALOGW("stopInput() input %d already stopped", input);
2057 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002058 }
2059
Eric Laurent599c7582015-12-07 18:05:55 -08002060 audioSession->changeActiveCount(-1);
Paul McLean466dc8e2015-04-17 13:15:36 -06002061
2062 // Routing?
2063 mInputRoutes.decRouteActivity(session);
2064
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07002065 if (audioSession->activeCount() == 0) {
Eric Laurent84332aa2016-01-28 22:19:18 +00002066
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07002067 if (inputDesc->isActive()) {
2068 setInputDevice(input, getNewInputDevice(inputDesc), false /* force */);
2069 } else {
2070 // if input maps to a dynamic policy with an activity listener, notify of state change
2071 if ((inputDesc->mPolicyMix != NULL)
2072 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
2073 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
2074 MIX_STATE_IDLE);
Eric Laurent84332aa2016-01-28 22:19:18 +00002075 }
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07002076
2077 // automatically disable the remote submix output when input is stopped if not
2078 // used by a policy mix of type MIX_TYPE_RECORDERS
2079 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
2080 String8 address = String8("");
2081 if (inputDesc->mPolicyMix == NULL) {
2082 address = String8("0");
2083 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
2084 address = inputDesc->mPolicyMix->mDeviceAddress;
2085 }
2086 if (address != "") {
2087 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2088 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2089 address, "remote-submix");
2090 }
Eric Laurent84332aa2016-01-28 22:19:18 +00002091 }
Eric Laurent84332aa2016-01-28 22:19:18 +00002092
Eric Laurentf7c50102016-09-30 15:19:43 -07002093 audio_devices_t device = inputDesc->mDevice;
Eric Laurentfb66dd92016-01-28 18:32:03 -08002094 resetInputDevice(input);
Eric Laurent84332aa2016-01-28 22:19:18 +00002095
Eric Laurentf7c50102016-09-30 15:19:43 -07002096 // indicate inactive capture to sound trigger service if stopping capture from a mic on
2097 // primary HW module
2098 audio_devices_t primaryInputDevices = availablePrimaryInputDevices();
2099 if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) &&
2100 mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) {
Eric Laurentfb66dd92016-01-28 18:32:03 -08002101 SoundTrigger::setCaptureState(false);
2102 }
2103 inputDesc->clearPreemptedSessions();
Eric Laurent84332aa2016-01-28 22:19:18 +00002104 }
Eric Laurente552edb2014-03-10 17:42:56 -07002105 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002106 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07002107}
2108
Eric Laurent4dc68062014-07-28 17:26:49 -07002109void AudioPolicyManager::releaseInput(audio_io_handle_t input,
2110 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07002111{
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002112
Eric Laurente552edb2014-03-10 17:42:56 -07002113 ALOGV("releaseInput() %d", input);
2114 ssize_t index = mInputs.indexOfKey(input);
2115 if (index < 0) {
2116 ALOGW("releaseInput() releasing unknown input %d", input);
2117 return;
2118 }
Paul McLean466dc8e2015-04-17 13:15:36 -06002119
2120 // Routing
2121 mInputRoutes.removeRoute(session);
2122
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002123 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
2124 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07002125
Eric Laurent599c7582015-12-07 18:05:55 -08002126 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
vivek mehta587b8df2017-01-31 14:58:44 -08002127 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07002128 ALOGW("releaseInput() unknown session %d on input %d", session, input);
2129 return;
2130 }
Eric Laurent599c7582015-12-07 18:05:55 -08002131
2132 if (audioSession->openCount() == 0) {
2133 ALOGW("releaseInput() invalid open count %d on session %d",
2134 audioSession->openCount(), session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002135 return;
2136 }
Eric Laurent599c7582015-12-07 18:05:55 -08002137
2138 if (audioSession->changeOpenCount(-1) == 0) {
2139 inputDesc->removeAudioSession(session);
2140 }
2141
Jean-Michel Trivi65bfe912015-12-04 15:56:47 -08002142 if (inputDesc->getOpenRefCount() > 0) {
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002143 ALOGV("releaseInput() exit > 0");
2144 return;
2145 }
2146
Eric Laurent05b90f82014-08-27 15:32:29 -07002147 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07002148 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07002149 ALOGV("releaseInput() exit");
2150}
2151
Eric Laurentd4692962014-05-05 18:13:44 -07002152void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07002153 bool patchRemoved = false;
2154
Eric Laurentd4692962014-05-05 18:13:44 -07002155 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07002156 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08002157 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07002158 if (patch_index >= 0) {
2159 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07002160 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07002161 mAudioPatches.removeItemsAt(patch_index);
2162 patchRemoved = true;
2163 }
Eric Laurentd4692962014-05-05 18:13:44 -07002164 mpClientInterface->closeInput(mInputs.keyAt(input_index));
2165 }
2166 mInputs.clear();
Chris Thornton52785f32016-02-09 17:28:49 -08002167 SoundTrigger::setCaptureState(false);
Eric Laurent6a94d692014-05-20 11:18:06 -07002168 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07002169
2170 if (patchRemoved) {
2171 mpClientInterface->onAudioPatchListUpdate();
2172 }
Eric Laurentd4692962014-05-05 18:13:44 -07002173}
2174
Eric Laurente0720872014-03-11 09:30:41 -07002175void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07002176 int indexMin,
2177 int indexMax)
2178{
2179 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002180 mVolumeCurves->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent28d09f02016-03-08 10:43:05 -08002181
2182 // initialize other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08002183 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
2184 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002185 continue;
2186 }
2187 mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08002188 }
Eric Laurente552edb2014-03-10 17:42:56 -07002189}
2190
Eric Laurente0720872014-03-11 09:30:41 -07002191status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01002192 int index,
2193 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07002194{
2195
François Gaffied1ab2bd2015-12-02 18:20:06 +01002196 if ((index < mVolumeCurves->getVolumeIndexMin(stream)) ||
2197 (index > mVolumeCurves->getVolumeIndexMax(stream))) {
Eric Laurente552edb2014-03-10 17:42:56 -07002198 return BAD_VALUE;
2199 }
2200 if (!audio_is_output_device(device)) {
2201 return BAD_VALUE;
2202 }
2203
2204 // Force max volume if stream cannot be muted
François Gaffied1ab2bd2015-12-02 18:20:06 +01002205 if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07002206
Eric Laurent1fd372e2016-04-06 14:23:53 -07002207 ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d",
Eric Laurente552edb2014-03-10 17:42:56 -07002208 stream, device, index);
2209
Eric Laurent28d09f02016-03-08 10:43:05 -08002210 // update other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08002211 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
2212 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002213 continue;
2214 }
2215 mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index);
2216 }
Eric Laurente552edb2014-03-10 17:42:56 -07002217
Eric Laurent1fd372e2016-04-06 14:23:53 -07002218 // update volume on all outputs and streams matching the following:
2219 // - The requested stream (or a stream matching for volume control) is active on the output
2220 // - The device (or devices) selected by the strategy corresponding to this stream includes
2221 // the requested device
2222 // - For non default requested device, currently selected device on the output is either the
2223 // requested device or one of the devices selected by the strategy
Eric Laurent5a2b6292016-04-14 18:05:57 -07002224 // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if
2225 // no specific device volume value exists for currently selected device.
Eric Laurente552edb2014-03-10 17:42:56 -07002226 status_t status = NO_ERROR;
2227 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002228 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
2229 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent794fde22016-03-11 09:50:45 -08002230 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
2231 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002232 continue;
Eric Laurente552edb2014-03-10 17:42:56 -07002233 }
Eric Laurentd3926fe2016-04-15 18:10:07 -07002234 if (!(desc->isStreamActive((audio_stream_type_t)curStream) ||
2235 (isInCall() && (curStream == AUDIO_STREAM_VOICE_CALL)))) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07002236 continue;
2237 }
Eric Laurent794fde22016-03-11 09:50:45 -08002238 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Jean-Michel Trivib7fdce62017-06-27 19:38:42 -07002239 audio_devices_t curStreamDevice = Volume::getDeviceForVolume(getDeviceForStrategy(
2240 curStrategy, false /*fromCache*/));
Eric Laurente76f29c2016-09-14 17:17:53 -07002241 if ((device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) &&
2242 ((curStreamDevice & device) == 0)) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07002243 continue;
2244 }
Eric Laurente76f29c2016-09-14 17:17:53 -07002245 bool applyVolume;
Eric Laurent5a2b6292016-04-14 18:05:57 -07002246 if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07002247 curStreamDevice |= device;
Eric Laurente76f29c2016-09-14 17:17:53 -07002248 applyVolume = (curDevice & curStreamDevice) != 0;
2249 } else {
2250 applyVolume = !mVolumeCurves->hasVolumeIndexForDevice(
Jean-Michel Trivib7fdce62017-06-27 19:38:42 -07002251 stream, curStreamDevice);
Eric Laurent1fd372e2016-04-06 14:23:53 -07002252 }
Eric Laurent28d09f02016-03-08 10:43:05 -08002253
Eric Laurente76f29c2016-09-14 17:17:53 -07002254 if (applyVolume) {
Eric Laurentdc462862016-07-19 12:29:53 -07002255 //FIXME: workaround for truncated touch sounds
2256 // delayed volume change for system stream to be removed when the problem is
2257 // handled by system UI
Eric Laurent28d09f02016-03-08 10:43:05 -08002258 status_t volStatus =
Eric Laurentdc462862016-07-19 12:29:53 -07002259 checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice,
2260 (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0);
Eric Laurent28d09f02016-03-08 10:43:05 -08002261 if (volStatus != NO_ERROR) {
2262 status = volStatus;
2263 }
2264 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08002265 }
Eric Laurente552edb2014-03-10 17:42:56 -07002266 }
2267 return status;
2268}
2269
Eric Laurente0720872014-03-11 09:30:41 -07002270status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07002271 int *index,
2272 audio_devices_t device)
2273{
2274 if (index == NULL) {
2275 return BAD_VALUE;
2276 }
2277 if (!audio_is_output_device(device)) {
2278 return BAD_VALUE;
2279 }
Eric Laurent5a2b6292016-04-14 18:05:57 -07002280 // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device corresponding to
Eric Laurente552edb2014-03-10 17:42:56 -07002281 // the strategy the stream belongs to.
Eric Laurent5a2b6292016-04-14 18:05:57 -07002282 if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurente552edb2014-03-10 17:42:56 -07002283 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
2284 }
François Gaffiedfd74092015-03-19 12:10:59 +01002285 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07002286
François Gaffied1ab2bd2015-12-02 18:20:06 +01002287 *index = mVolumeCurves->getVolumeIndex(stream, device);
Eric Laurente552edb2014-03-10 17:42:56 -07002288 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
2289 return NO_ERROR;
2290}
2291
Eric Laurent36829f92017-04-07 19:04:42 -07002292audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects()
Eric Laurente552edb2014-03-10 17:42:56 -07002293{
2294 // select one output among several suitable for global effects.
2295 // The priority is as follows:
2296 // 1: An offloaded output. If the effect ends up not being offloadable,
2297 // AudioFlinger will invalidate the track and the offloaded output
2298 // will be closed causing the effect to be moved to a PCM output.
2299 // 2: A deep buffer output
Eric Laurent36829f92017-04-07 19:04:42 -07002300 // 3: The primary output
2301 // 4: the first output in the list
Eric Laurente552edb2014-03-10 17:42:56 -07002302
Eric Laurent3b73df72014-03-11 09:06:29 -07002303 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07002304 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent36829f92017-04-07 19:04:42 -07002305 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
Eric Laurente552edb2014-03-10 17:42:56 -07002306
Eric Laurent36829f92017-04-07 19:04:42 -07002307 if (outputs.size() == 0) {
2308 return AUDIO_IO_HANDLE_NONE;
2309 }
Eric Laurente552edb2014-03-10 17:42:56 -07002310
Eric Laurent36829f92017-04-07 19:04:42 -07002311 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
2312 bool activeOnly = true;
2313
2314 while (output == AUDIO_IO_HANDLE_NONE) {
2315 audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE;
2316 audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE;
2317 audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE;
2318
2319 for (size_t i = 0; i < outputs.size(); i++) {
2320 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
2321 if (activeOnly && !desc->isStreamActive(AUDIO_STREAM_MUSIC)) {
2322 continue;
2323 }
2324 ALOGV("selectOutputForMusicEffects activeOnly %d outputs[%zu] flags 0x%08x",
2325 activeOnly, i, desc->mFlags);
2326 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
2327 outputOffloaded = outputs[i];
2328 }
2329 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
2330 outputDeepBuffer = outputs[i];
2331 }
2332 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) {
2333 outputPrimary = outputs[i];
2334 }
2335 }
2336 if (outputOffloaded != AUDIO_IO_HANDLE_NONE) {
2337 output = outputOffloaded;
2338 } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) {
2339 output = outputDeepBuffer;
2340 } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) {
2341 output = outputPrimary;
2342 } else {
2343 output = outputs[0];
2344 }
2345 activeOnly = false;
2346 }
2347
2348 if (output != mMusicEffectOutput) {
2349 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output);
2350 mMusicEffectOutput = output;
2351 }
2352
2353 ALOGV("selectOutputForMusicEffects selected output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -07002354 return output;
2355}
2356
Eric Laurent36829f92017-04-07 19:04:42 -07002357audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused)
2358{
2359 return selectOutputForMusicEffects();
2360}
2361
Eric Laurente0720872014-03-11 09:30:41 -07002362status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07002363 audio_io_handle_t io,
2364 uint32_t strategy,
2365 int session,
2366 int id)
2367{
2368 ssize_t index = mOutputs.indexOfKey(io);
2369 if (index < 0) {
2370 index = mInputs.indexOfKey(io);
2371 if (index < 0) {
2372 ALOGW("registerEffect() unknown io %d", io);
2373 return INVALID_OPERATION;
2374 }
2375 }
François Gaffie45ed3b02015-03-19 10:35:14 +01002376 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07002377}
2378
Eric Laurentc75307b2015-03-17 15:29:32 -07002379bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
2380{
Eric Laurent28d09f02016-03-08 10:43:05 -08002381 bool active = false;
Eric Laurent794fde22016-03-11 09:50:45 -08002382 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) {
2383 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002384 continue;
2385 }
2386 active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs);
2387 }
Eric Laurent28d09f02016-03-08 10:43:05 -08002388 return active;
Eric Laurentc75307b2015-03-17 15:29:32 -07002389}
2390
2391bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
2392{
2393 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
2394}
2395
Eric Laurente0720872014-03-11 09:30:41 -07002396bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07002397{
2398 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002399 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002400 if (inputDescriptor->isSourceActive(source)) {
Eric Laurente552edb2014-03-10 17:42:56 -07002401 return true;
2402 }
2403 }
2404 return false;
2405}
2406
Eric Laurent275e8e92014-11-30 15:14:47 -08002407// Register a list of custom mixes with their attributes and format.
2408// When a mix is registered, corresponding input and output profiles are
2409// added to the remote submix hw module. The profile contains only the
2410// parameters (sampling rate, format...) specified by the mix.
2411// The corresponding input remote submix device is also connected.
2412//
2413// When a remote submix device is connected, the address is checked to select the
2414// appropriate profile and the corresponding input or output stream is opened.
2415//
2416// When capture starts, getInputForAttr() will:
2417// - 1 look for a mix matching the address passed in attribtutes tags if any
2418// - 2 if none found, getDeviceForInputSource() will:
2419// - 2.1 look for a mix matching the attributes source
2420// - 2.2 if none found, default to device selection by policy rules
2421// At this time, the corresponding output remote submix device is also connected
2422// and active playback use cases can be transferred to this mix if needed when reconnecting
2423// after AudioTracks are invalidated
2424//
2425// When playback starts, getOutputForAttr() will:
2426// - 1 look for a mix matching the address passed in attribtutes tags if any
2427// - 2 if none found, look for a mix matching the attributes usage
2428// - 3 if none found, default to device and output selection by policy rules.
2429
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07002430status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes)
Eric Laurent275e8e92014-11-30 15:14:47 -08002431{
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002432 ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size());
2433 status_t res = NO_ERROR;
2434
2435 sp<HwModule> rSubmixModule;
2436 // examine each mix's route type
2437 for (size_t i = 0; i < mixes.size(); i++) {
2438 // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination
2439 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) {
2440 res = INVALID_OPERATION;
Eric Laurent275e8e92014-11-30 15:14:47 -08002441 break;
2442 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002443 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
2444 // Loop back through "remote submix"
2445 if (rSubmixModule == 0) {
2446 for (size_t j = 0; i < mHwModules.size(); j++) {
2447 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2448 && mHwModules[j]->mHandle != 0) {
2449 rSubmixModule = mHwModules[j];
2450 break;
2451 }
2452 }
2453 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002454
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002455 ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size());
Eric Laurent275e8e92014-11-30 15:14:47 -08002456
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002457 if (rSubmixModule == 0) {
2458 ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", i);
2459 res = INVALID_OPERATION;
2460 break;
2461 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002462
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002463 String8 address = mixes[i].mDeviceAddress;
François Gaffie036e1e92015-03-19 10:16:24 +01002464
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002465 if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) {
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002466 ALOGE(" Error registering mix %zu for address %s", i, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002467 res = INVALID_OPERATION;
2468 break;
2469 }
2470 audio_config_t outputConfig = mixes[i].mFormat;
2471 audio_config_t inputConfig = mixes[i].mFormat;
2472 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
2473 // stereo and let audio flinger do the channel conversion if needed.
2474 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2475 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
2476 rSubmixModule->addOutputProfile(address, &outputConfig,
2477 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
2478 rSubmixModule->addInputProfile(address, &inputConfig,
2479 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01002480
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002481 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
2482 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2483 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2484 address.string(), "remote-submix");
2485 } else {
2486 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2487 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2488 address.string(), "remote-submix");
2489 }
2490 } else if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002491 String8 address = mixes[i].mDeviceAddress;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002492 audio_devices_t device = mixes[i].mDeviceType;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002493 ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s",
2494 i, mixes.size(), device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002495
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002496 bool foundOutput = false;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002497 for (size_t j = 0 ; j < mOutputs.size() ; j++) {
2498 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j);
2499 sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle());
2500 if ((patch != 0) && (patch->mPatch.num_sinks != 0)
2501 && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE)
2502 && (patch->mPatch.sinks[0].ext.device.type == device)
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002503 && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(),
2504 AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002505 if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) {
2506 res = INVALID_OPERATION;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002507 } else {
2508 foundOutput = true;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002509 }
2510 break;
2511 }
2512 }
2513
2514 if (res != NO_ERROR) {
2515 ALOGE(" Error registering mix %zu for device 0x%X addr %s",
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002516 i, device, address.string());
2517 res = INVALID_OPERATION;
2518 break;
2519 } else if (!foundOutput) {
2520 ALOGE(" Output not found for mix %zu for device 0x%X addr %s",
2521 i, device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002522 res = INVALID_OPERATION;
2523 break;
2524 }
Eric Laurentc722f302014-12-10 11:21:49 -08002525 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002526 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002527 if (res != NO_ERROR) {
2528 unregisterPolicyMixes(mixes);
2529 }
2530 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002531}
2532
2533status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
2534{
Eric Laurent7b279bb2015-12-14 10:18:23 -08002535 ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002536 status_t res = NO_ERROR;
2537 sp<HwModule> rSubmixModule;
2538 // examine each mix's route type
Eric Laurent275e8e92014-11-30 15:14:47 -08002539 for (size_t i = 0; i < mixes.size(); i++) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002540 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
François Gaffie036e1e92015-03-19 10:16:24 +01002541
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002542 if (rSubmixModule == 0) {
2543 for (size_t j = 0; i < mHwModules.size(); j++) {
2544 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2545 && mHwModules[j]->mHandle != 0) {
2546 rSubmixModule = mHwModules[j];
2547 break;
2548 }
2549 }
2550 }
2551 if (rSubmixModule == 0) {
2552 res = INVALID_OPERATION;
2553 continue;
2554 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002555
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002556 String8 address = mixes[i].mDeviceAddress;
Eric Laurent275e8e92014-11-30 15:14:47 -08002557
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002558 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
2559 res = INVALID_OPERATION;
2560 continue;
2561 }
2562
2563 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
2564 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2565 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2566 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2567 address.string(), "remote-submix");
2568 }
2569 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
2570 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2571 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2572 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2573 address.string(), "remote-submix");
2574 }
2575 rSubmixModule->removeOutputProfile(address);
2576 rSubmixModule->removeInputProfile(address);
2577
2578 } if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
2579 if (mPolicyMixes.unregisterMix(mixes[i].mDeviceAddress) != NO_ERROR) {
2580 res = INVALID_OPERATION;
2581 continue;
2582 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002583 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002584 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002585 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002586}
2587
Eric Laurente552edb2014-03-10 17:42:56 -07002588
Eric Laurente0720872014-03-11 09:30:41 -07002589status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07002590{
2591 const size_t SIZE = 256;
2592 char buffer[SIZE];
2593 String8 result;
2594
2595 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
2596 result.append(buffer);
2597
Eric Laurent87ffa392015-05-22 10:32:38 -07002598 snprintf(buffer, SIZE, " Primary Output: %d\n",
2599 hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE);
Eric Laurente552edb2014-03-10 17:42:56 -07002600 result.append(buffer);
Mikhail Naganov0d6a0332016-04-19 17:12:38 -07002601 std::string stateLiteral;
2602 AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral);
2603 snprintf(buffer, SIZE, " Phone state: %s\n", stateLiteral.c_str());
Eric Laurente552edb2014-03-10 17:42:56 -07002604 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002605 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002606 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07002607 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002608 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07002609 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002610 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07002611 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002612 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07002613 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002614 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07002615 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002616 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002617 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002618 result.append(buffer);
Phil Burk09bc4612016-02-24 15:58:15 -08002619 snprintf(buffer, SIZE, " Force use for encoded surround output %d\n",
2620 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND));
2621 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002622 snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available");
2623 result.append(buffer);
Andy Hung2ddee192015-12-18 17:34:44 -08002624 snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off");
2625 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002626
François Gaffie2110e042015-03-24 08:41:51 +01002627 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07002628
François Gaffie112b0af2015-11-19 16:13:25 +01002629 mAvailableOutputDevices.dump(fd, String8("Available output"));
2630 mAvailableInputDevices.dump(fd, String8("Available input"));
François Gaffie53615e22015-03-19 09:24:12 +01002631 mHwModules.dump(fd);
2632 mOutputs.dump(fd);
2633 mInputs.dump(fd);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002634 mVolumeCurves->dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01002635 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002636 mAudioPatches.dump(fd);
Mikhail Naganov44344b02016-12-13 11:21:02 -08002637 mPolicyMixes.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07002638
2639 return NO_ERROR;
2640}
2641
2642// This function checks for the parameters which can be offloaded.
2643// This can be enhanced depending on the capability of the DSP and policy
2644// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002645bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002646{
2647 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002648 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002649 offloadInfo.sample_rate, offloadInfo.channel_mask,
2650 offloadInfo.format,
2651 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2652 offloadInfo.has_video);
2653
Andy Hung2ddee192015-12-18 17:34:44 -08002654 if (mMasterMono) {
2655 return false; // no offloading if mono is set.
2656 }
2657
Eric Laurente552edb2014-03-10 17:42:56 -07002658 // Check if offload has been disabled
2659 char propValue[PROPERTY_VALUE_MAX];
2660 if (property_get("audio.offload.disable", propValue, "0")) {
2661 if (atoi(propValue) != 0) {
2662 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2663 return false;
2664 }
2665 }
2666
2667 // Check if stream type is music, then only allow offload as of now.
2668 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2669 {
2670 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2671 return false;
2672 }
2673
2674 //TODO: enable audio offloading with video when ready
Andy Hung08945c42015-05-31 21:36:46 -07002675 const bool allowOffloadWithVideo =
2676 property_get_bool("audio.offload.video", false /* default_value */);
2677 if (offloadInfo.has_video && !allowOffloadWithVideo) {
Eric Laurente552edb2014-03-10 17:42:56 -07002678 ALOGV("isOffloadSupported: has_video == true, returning false");
2679 return false;
2680 }
2681
2682 //If duration is less than minimum value defined in property, return false
2683 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2684 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2685 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2686 return false;
2687 }
2688 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2689 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2690 return false;
2691 }
2692
2693 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2694 // creating an offloaded track and tearing it down immediately after start when audioflinger
2695 // detects there is an active non offloadable effect.
2696 // FIXME: We should check the audio session here but we do not have it in this context.
2697 // This may prevent offloading in rare situations where effects are left active by apps
2698 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002699 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002700 return false;
2701 }
2702
2703 // See if there is a profile to support this.
2704 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002705 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002706 offloadInfo.sample_rate,
2707 offloadInfo.format,
2708 offloadInfo.channel_mask,
2709 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002710 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2711 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002712}
2713
Eric Laurent6a94d692014-05-20 11:18:06 -07002714status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2715 audio_port_type_t type,
2716 unsigned int *num_ports,
2717 struct audio_port *ports,
2718 unsigned int *generation)
2719{
2720 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2721 generation == NULL) {
2722 return BAD_VALUE;
2723 }
2724 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2725 if (ports == NULL) {
2726 *num_ports = 0;
2727 }
2728
2729 size_t portsWritten = 0;
2730 size_t portsMax = *num_ports;
2731 *num_ports = 0;
2732 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002733 // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB
2734 // as they are used by stub HALs by convention
Eric Laurent6a94d692014-05-20 11:18:06 -07002735 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002736 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
2737 if (mAvailableOutputDevices[i]->type() == AUDIO_DEVICE_OUT_STUB) {
2738 continue;
2739 }
2740 if (portsWritten < portsMax) {
2741 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2742 }
2743 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002744 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002745 }
2746 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002747 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
2748 if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_STUB) {
2749 continue;
2750 }
2751 if (portsWritten < portsMax) {
2752 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2753 }
2754 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002755 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002756 }
2757 }
2758 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2759 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2760 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2761 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2762 }
2763 *num_ports += mInputs.size();
2764 }
2765 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002766 size_t numOutputs = 0;
2767 for (size_t i = 0; i < mOutputs.size(); i++) {
2768 if (!mOutputs[i]->isDuplicated()) {
2769 numOutputs++;
2770 if (portsWritten < portsMax) {
2771 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2772 }
2773 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002774 }
Eric Laurent84c70242014-06-23 08:46:27 -07002775 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002776 }
2777 }
2778 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002779 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002780 return NO_ERROR;
2781}
2782
2783status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2784{
2785 return NO_ERROR;
2786}
2787
Eric Laurent6a94d692014-05-20 11:18:06 -07002788status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2789 audio_patch_handle_t *handle,
2790 uid_t uid)
2791{
2792 ALOGV("createAudioPatch()");
2793
2794 if (handle == NULL || patch == NULL) {
2795 return BAD_VALUE;
2796 }
2797 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2798
Eric Laurent874c42872014-08-08 15:13:39 -07002799 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2800 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2801 return BAD_VALUE;
2802 }
2803 // only one source per audio patch supported for now
2804 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002805 return INVALID_OPERATION;
2806 }
Eric Laurent874c42872014-08-08 15:13:39 -07002807
2808 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002809 return INVALID_OPERATION;
2810 }
Eric Laurent874c42872014-08-08 15:13:39 -07002811 for (size_t i = 0; i < patch->num_sinks; i++) {
2812 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2813 return INVALID_OPERATION;
2814 }
2815 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002816
2817 sp<AudioPatch> patchDesc;
2818 ssize_t index = mAudioPatches.indexOfKey(*handle);
2819
Eric Laurent6a94d692014-05-20 11:18:06 -07002820 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2821 patch->sources[0].role,
2822 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002823#if LOG_NDEBUG == 0
2824 for (size_t i = 0; i < patch->num_sinks; i++) {
Eric Laurent7b279bb2015-12-14 10:18:23 -08002825 ALOGV("createAudioPatch sink %zu: id %d role %d type %d", i, patch->sinks[i].id,
Eric Laurent874c42872014-08-08 15:13:39 -07002826 patch->sinks[i].role,
2827 patch->sinks[i].type);
2828 }
2829#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002830
2831 if (index >= 0) {
2832 patchDesc = mAudioPatches.valueAt(index);
2833 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2834 mUidCached, patchDesc->mUid, uid);
2835 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2836 return INVALID_OPERATION;
2837 }
2838 } else {
Glenn Kastena13cde92016-03-28 15:26:02 -07002839 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07002840 }
2841
2842 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002843 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002844 if (outputDesc == NULL) {
2845 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2846 return BAD_VALUE;
2847 }
Eric Laurent84c70242014-06-23 08:46:27 -07002848 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2849 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002850 if (patchDesc != 0) {
2851 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2852 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2853 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2854 return BAD_VALUE;
2855 }
2856 }
Eric Laurent874c42872014-08-08 15:13:39 -07002857 DeviceVector devices;
2858 for (size_t i = 0; i < patch->num_sinks; i++) {
2859 // Only support mix to devices connection
2860 // TODO add support for mix to mix connection
2861 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2862 ALOGV("createAudioPatch() source mix but sink is not a device");
2863 return INVALID_OPERATION;
2864 }
2865 sp<DeviceDescriptor> devDesc =
2866 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2867 if (devDesc == 0) {
2868 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2869 return BAD_VALUE;
2870 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002871
François Gaffie53615e22015-03-19 09:24:12 +01002872 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002873 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002874 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002875 NULL, // updatedSamplingRate
2876 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002877 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002878 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002879 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002880 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002881 ALOGV("createAudioPatch() profile not supported for device %08x",
2882 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002883 return INVALID_OPERATION;
2884 }
2885 devices.add(devDesc);
2886 }
2887 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002888 return INVALID_OPERATION;
2889 }
Eric Laurent874c42872014-08-08 15:13:39 -07002890
Eric Laurent6a94d692014-05-20 11:18:06 -07002891 // TODO: reconfigure output format and channels here
2892 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002893 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002894 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002895 index = mAudioPatches.indexOfKey(*handle);
2896 if (index >= 0) {
2897 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2898 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2899 }
2900 patchDesc = mAudioPatches.valueAt(index);
2901 patchDesc->mUid = uid;
2902 ALOGV("createAudioPatch() success");
2903 } else {
2904 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2905 return INVALID_OPERATION;
2906 }
2907 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2908 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2909 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002910 // only one sink supported when connecting an input device to a mix
2911 if (patch->num_sinks > 1) {
2912 return INVALID_OPERATION;
2913 }
François Gaffie53615e22015-03-19 09:24:12 +01002914 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002915 if (inputDesc == NULL) {
2916 return BAD_VALUE;
2917 }
2918 if (patchDesc != 0) {
2919 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2920 return BAD_VALUE;
2921 }
2922 }
2923 sp<DeviceDescriptor> devDesc =
2924 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2925 if (devDesc == 0) {
2926 return BAD_VALUE;
2927 }
2928
François Gaffie53615e22015-03-19 09:24:12 +01002929 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002930 devDesc->mAddress,
2931 patch->sinks[0].sample_rate,
2932 NULL, /*updatedSampleRate*/
2933 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002934 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002935 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002936 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002937 // FIXME for the parameter type,
2938 // and the NONE
2939 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002940 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002941 return INVALID_OPERATION;
2942 }
2943 // TODO: reconfigure output format and channels here
2944 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002945 devDesc->type(), inputDesc->mIoHandle);
2946 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002947 index = mAudioPatches.indexOfKey(*handle);
2948 if (index >= 0) {
2949 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2950 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2951 }
2952 patchDesc = mAudioPatches.valueAt(index);
2953 patchDesc->mUid = uid;
2954 ALOGV("createAudioPatch() success");
2955 } else {
2956 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2957 return INVALID_OPERATION;
2958 }
2959 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2960 // device to device connection
2961 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002962 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002963 return BAD_VALUE;
2964 }
2965 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002966 sp<DeviceDescriptor> srcDeviceDesc =
2967 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002968 if (srcDeviceDesc == 0) {
2969 return BAD_VALUE;
2970 }
Eric Laurent874c42872014-08-08 15:13:39 -07002971
Eric Laurent6a94d692014-05-20 11:18:06 -07002972 //update source and sink with our own data as the data passed in the patch may
2973 // be incomplete.
2974 struct audio_patch newPatch = *patch;
2975 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002976
Eric Laurent874c42872014-08-08 15:13:39 -07002977 for (size_t i = 0; i < patch->num_sinks; i++) {
2978 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2979 ALOGV("createAudioPatch() source device but one sink is not a device");
2980 return INVALID_OPERATION;
2981 }
2982
2983 sp<DeviceDescriptor> sinkDeviceDesc =
2984 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2985 if (sinkDeviceDesc == 0) {
2986 return BAD_VALUE;
2987 }
2988 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2989
Eric Laurent3bcf8592015-04-03 12:13:24 -07002990 // create a software bridge in PatchPanel if:
2991 // - source and sink devices are on differnt HW modules OR
2992 // - audio HAL version is < 3.0
Eric Laurentfb66dd92016-01-28 18:32:03 -08002993 if (!srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) ||
Mikhail Naganov9ee05402016-10-13 15:58:17 -07002994 (srcDeviceDesc->mModule->getHalVersionMajor() < 3)) {
Eric Laurent3bcf8592015-04-03 12:13:24 -07002995 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002996 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002997 return INVALID_OPERATION;
2998 }
Eric Laurent874c42872014-08-08 15:13:39 -07002999 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01003000 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07003001 // if the sink device is reachable via an opened output stream, request to go via
3002 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07003003 audio_io_handle_t output = selectOutput(outputs,
3004 AUDIO_OUTPUT_FLAG_NONE,
3005 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07003006 if (output != AUDIO_IO_HANDLE_NONE) {
3007 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
3008 if (outputDesc->isDuplicated()) {
3009 return INVALID_OPERATION;
3010 }
3011 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07003012 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07003013 newPatch.num_sources = 2;
3014 }
Eric Laurent83b88082014-06-20 18:31:16 -07003015 }
Eric Laurent6a94d692014-05-20 11:18:06 -07003016 }
3017 // TODO: check from routing capabilities in config file and other conflicting patches
3018
3019 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3020 if (index >= 0) {
3021 afPatchHandle = patchDesc->mAfPatchHandle;
3022 }
3023
3024 status_t status = mpClientInterface->createAudioPatch(&newPatch,
3025 &afPatchHandle,
3026 0);
3027 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
3028 status, afPatchHandle);
3029 if (status == NO_ERROR) {
3030 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01003031 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07003032 addAudioPatch(patchDesc->mHandle, patchDesc);
3033 } else {
3034 patchDesc->mPatch = newPatch;
3035 }
3036 patchDesc->mAfPatchHandle = afPatchHandle;
3037 *handle = patchDesc->mHandle;
3038 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07003039 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07003040 } else {
3041 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
3042 status);
3043 return INVALID_OPERATION;
3044 }
3045 } else {
3046 return BAD_VALUE;
3047 }
3048 } else {
3049 return BAD_VALUE;
3050 }
3051 return NO_ERROR;
3052}
3053
3054status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
3055 uid_t uid)
3056{
3057 ALOGV("releaseAudioPatch() patch %d", handle);
3058
3059 ssize_t index = mAudioPatches.indexOfKey(handle);
3060
3061 if (index < 0) {
3062 return BAD_VALUE;
3063 }
3064 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3065 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
3066 mUidCached, patchDesc->mUid, uid);
3067 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
3068 return INVALID_OPERATION;
3069 }
3070
3071 struct audio_patch *patch = &patchDesc->mPatch;
3072 patchDesc->mUid = mUidCached;
3073 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003074 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07003075 if (outputDesc == NULL) {
3076 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
3077 return BAD_VALUE;
3078 }
3079
Eric Laurentc75307b2015-03-17 15:29:32 -07003080 setOutputDevice(outputDesc,
3081 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07003082 true,
3083 0,
3084 NULL);
3085 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
3086 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01003087 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07003088 if (inputDesc == NULL) {
3089 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
3090 return BAD_VALUE;
3091 }
3092 setInputDevice(inputDesc->mIoHandle,
Eric Laurentfb66dd92016-01-28 18:32:03 -08003093 getNewInputDevice(inputDesc),
Eric Laurent6a94d692014-05-20 11:18:06 -07003094 true,
3095 NULL);
3096 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003097 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3098 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
3099 status, patchDesc->mAfPatchHandle);
3100 removeAudioPatch(patchDesc->mHandle);
3101 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07003102 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07003103 } else {
3104 return BAD_VALUE;
3105 }
3106 } else {
3107 return BAD_VALUE;
3108 }
3109 return NO_ERROR;
3110}
3111
3112status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
3113 struct audio_patch *patches,
3114 unsigned int *generation)
3115{
François Gaffie53615e22015-03-19 09:24:12 +01003116 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003117 return BAD_VALUE;
3118 }
Eric Laurent6a94d692014-05-20 11:18:06 -07003119 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01003120 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07003121}
3122
Eric Laurente1715a42014-05-20 11:30:42 -07003123status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07003124{
Eric Laurente1715a42014-05-20 11:30:42 -07003125 ALOGV("setAudioPortConfig()");
3126
3127 if (config == NULL) {
3128 return BAD_VALUE;
3129 }
3130 ALOGV("setAudioPortConfig() on port handle %d", config->id);
3131 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07003132 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
3133 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07003134 }
3135
Eric Laurenta121f902014-06-03 13:32:54 -07003136 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07003137 if (config->type == AUDIO_PORT_TYPE_MIX) {
3138 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003139 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07003140 if (outputDesc == NULL) {
3141 return BAD_VALUE;
3142 }
Eric Laurent84c70242014-06-23 08:46:27 -07003143 ALOG_ASSERT(!outputDesc->isDuplicated(),
3144 "setAudioPortConfig() called on duplicated output %d",
3145 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07003146 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07003147 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01003148 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07003149 if (inputDesc == NULL) {
3150 return BAD_VALUE;
3151 }
Eric Laurenta121f902014-06-03 13:32:54 -07003152 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07003153 } else {
3154 return BAD_VALUE;
3155 }
3156 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
3157 sp<DeviceDescriptor> deviceDesc;
3158 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
3159 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
3160 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
3161 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
3162 } else {
3163 return BAD_VALUE;
3164 }
3165 if (deviceDesc == NULL) {
3166 return BAD_VALUE;
3167 }
Eric Laurenta121f902014-06-03 13:32:54 -07003168 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07003169 } else {
3170 return BAD_VALUE;
3171 }
3172
Eric Laurenta121f902014-06-03 13:32:54 -07003173 struct audio_port_config backupConfig;
3174 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
3175 if (status == NO_ERROR) {
3176 struct audio_port_config newConfig;
3177 audioPortConfig->toAudioPortConfig(&newConfig, config);
3178 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07003179 }
Eric Laurenta121f902014-06-03 13:32:54 -07003180 if (status != NO_ERROR) {
3181 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07003182 }
Eric Laurente1715a42014-05-20 11:30:42 -07003183
3184 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07003185}
3186
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003187void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
3188{
Eric Laurentd60560a2015-04-10 11:31:20 -07003189 clearAudioSources(uid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003190 clearAudioPatches(uid);
3191 clearSessionRoutes(uid);
3192}
3193
Eric Laurent6a94d692014-05-20 11:18:06 -07003194void AudioPolicyManager::clearAudioPatches(uid_t uid)
3195{
Eric Laurent0add0fd2014-12-04 18:58:14 -08003196 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003197 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
3198 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08003199 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07003200 }
3201 }
3202}
3203
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003204void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
3205 audio_io_handle_t ouptutToSkip)
3206{
3207 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
3208 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
3209 for (size_t j = 0; j < mOutputs.size(); j++) {
3210 if (mOutputs.keyAt(j) == ouptutToSkip) {
3211 continue;
3212 }
3213 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
3214 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
3215 continue;
3216 }
3217 // If the default device for this strategy is on another output mix,
3218 // invalidate all tracks in this strategy to force re connection.
3219 // Otherwise select new device on the output mix.
3220 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
Eric Laurent794fde22016-03-11 09:50:45 -08003221 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003222 if (getStrategy((audio_stream_type_t)stream) == strategy) {
3223 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
3224 }
3225 }
3226 } else {
3227 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
3228 setOutputDevice(outputDesc, newDevice, false);
3229 }
3230 }
3231}
3232
3233void AudioPolicyManager::clearSessionRoutes(uid_t uid)
3234{
3235 // remove output routes associated with this uid
3236 SortedVector<routing_strategy> affectedStrategies;
3237 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
3238 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
3239 if (route->mUid == uid) {
3240 mOutputRoutes.removeItemsAt(i);
3241 if (route->mDeviceDescriptor != 0) {
3242 affectedStrategies.add(getStrategy(route->mStreamType));
3243 }
3244 }
3245 }
3246 // reroute outputs if necessary
3247 for (size_t i = 0; i < affectedStrategies.size(); i++) {
3248 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
3249 }
3250
3251 // remove input routes associated with this uid
3252 SortedVector<audio_source_t> affectedSources;
3253 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
3254 sp<SessionRoute> route = mInputRoutes.valueAt(i);
3255 if (route->mUid == uid) {
3256 mInputRoutes.removeItemsAt(i);
3257 if (route->mDeviceDescriptor != 0) {
3258 affectedSources.add(route->mSource);
3259 }
3260 }
3261 }
3262 // reroute inputs if necessary
3263 SortedVector<audio_io_handle_t> inputsToClose;
3264 for (size_t i = 0; i < mInputs.size(); i++) {
3265 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08003266 if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003267 inputsToClose.add(inputDesc->mIoHandle);
3268 }
3269 }
3270 for (size_t i = 0; i < inputsToClose.size(); i++) {
3271 closeInput(inputsToClose[i]);
3272 }
3273}
3274
Eric Laurentd60560a2015-04-10 11:31:20 -07003275void AudioPolicyManager::clearAudioSources(uid_t uid)
3276{
3277 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
3278 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3279 if (sourceDesc->mUid == uid) {
3280 stopAudioSource(mAudioSources.keyAt(i));
3281 }
3282 }
3283}
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003284
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07003285status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
3286 audio_io_handle_t *ioHandle,
3287 audio_devices_t *device)
3288{
Glenn Kastenf0c6d7d2016-02-26 10:44:04 -08003289 *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
3290 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
Eric Laurentc73ca6e2014-12-12 14:34:22 -08003291 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07003292
François Gaffiedf372692015-03-19 10:43:27 +01003293 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07003294}
3295
Eric Laurentd60560a2015-04-10 11:31:20 -07003296status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source,
3297 const audio_attributes_t *attributes,
Glenn Kasten559d4392016-03-29 13:42:57 -07003298 audio_patch_handle_t *handle,
Eric Laurentd60560a2015-04-10 11:31:20 -07003299 uid_t uid)
Eric Laurent554a2772015-04-10 11:29:24 -07003300{
Eric Laurentd60560a2015-04-10 11:31:20 -07003301 ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle);
3302 if (source == NULL || attributes == NULL || handle == NULL) {
3303 return BAD_VALUE;
3304 }
3305
Glenn Kasten559d4392016-03-29 13:42:57 -07003306 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentd60560a2015-04-10 11:31:20 -07003307
3308 if (source->role != AUDIO_PORT_ROLE_SOURCE ||
3309 source->type != AUDIO_PORT_TYPE_DEVICE) {
3310 ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type);
3311 return INVALID_OPERATION;
3312 }
3313
3314 sp<DeviceDescriptor> srcDeviceDesc =
3315 mAvailableInputDevices.getDevice(source->ext.device.type,
3316 String8(source->ext.device.address));
3317 if (srcDeviceDesc == 0) {
3318 ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type);
3319 return BAD_VALUE;
3320 }
3321 sp<AudioSourceDescriptor> sourceDesc =
3322 new AudioSourceDescriptor(srcDeviceDesc, attributes, uid);
3323
3324 struct audio_patch dummyPatch;
3325 sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid);
3326 sourceDesc->mPatchDesc = patchDesc;
3327
3328 status_t status = connectAudioSource(sourceDesc);
3329 if (status == NO_ERROR) {
3330 mAudioSources.add(sourceDesc->getHandle(), sourceDesc);
3331 *handle = sourceDesc->getHandle();
3332 }
3333 return status;
3334}
3335
3336status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3337{
3338 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3339
3340 // make sure we only have one patch per source.
3341 disconnectAudioSource(sourceDesc);
3342
3343 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
Eric Laurent28d09f02016-03-08 10:43:05 -08003344 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003345 sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice;
3346
3347 audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true);
3348 sp<DeviceDescriptor> sinkDeviceDesc =
3349 mAvailableOutputDevices.getDevice(sinkDevice, String8(""));
3350
3351 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3352 struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch;
3353
3354 if (srcDeviceDesc->getAudioPort()->mModule->getHandle() ==
3355 sinkDeviceDesc->getAudioPort()->mModule->getHandle() &&
Mikhail Naganov9ee05402016-10-13 15:58:17 -07003356 srcDeviceDesc->getAudioPort()->mModule->getHalVersionMajor() >= 3 &&
Eric Laurentd60560a2015-04-10 11:31:20 -07003357 srcDeviceDesc->getAudioPort()->mGains.size() > 0) {
3358 ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__);
3359 // create patch between src device and output device
3360 // create Hwoutput and add to mHwOutputs
3361 } else {
3362 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs);
3363 audio_io_handle_t output =
3364 selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID);
3365 if (output == AUDIO_IO_HANDLE_NONE) {
3366 ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice);
3367 return INVALID_OPERATION;
3368 }
3369 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
3370 if (outputDesc->isDuplicated()) {
3371 ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice);
3372 return INVALID_OPERATION;
3373 }
3374 // create a special patch with no sink and two sources:
3375 // - the second source indicates to PatchPanel through which output mix this patch should
3376 // be connected as well as the stream type for volume control
3377 // - the sink is defined by whatever output device is currently selected for the output
3378 // though which this patch is routed.
3379 patch->num_sinks = 0;
3380 patch->num_sources = 2;
3381 srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL);
3382 outputDesc->toAudioPortConfig(&patch->sources[1], NULL);
3383 patch->sources[1].ext.mix.usecase.stream = stream;
3384 status_t status = mpClientInterface->createAudioPatch(patch,
3385 &afPatchHandle,
3386 0);
3387 ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__,
3388 status, afPatchHandle);
3389 if (status != NO_ERROR) {
3390 ALOGW("%s patch panel could not connect device patch, error %d",
3391 __FUNCTION__, status);
3392 return INVALID_OPERATION;
3393 }
3394 uint32_t delayMs = 0;
Eric Laurentc40d9692016-04-13 19:14:13 -07003395 status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs);
Eric Laurentd60560a2015-04-10 11:31:20 -07003396
3397 if (status != NO_ERROR) {
3398 mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0);
3399 return status;
3400 }
3401 sourceDesc->mSwOutput = outputDesc;
3402 if (delayMs != 0) {
3403 usleep(delayMs * 1000);
3404 }
3405 }
3406
3407 sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle;
3408 addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc);
3409
3410 return NO_ERROR;
Eric Laurent554a2772015-04-10 11:29:24 -07003411}
3412
Glenn Kasten559d4392016-03-29 13:42:57 -07003413status_t AudioPolicyManager::stopAudioSource(audio_patch_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07003414{
Eric Laurentd60560a2015-04-10 11:31:20 -07003415 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle);
3416 ALOGV("%s handle %d", __FUNCTION__, handle);
3417 if (sourceDesc == 0) {
3418 ALOGW("%s unknown source for handle %d", __FUNCTION__, handle);
3419 return BAD_VALUE;
3420 }
3421 status_t status = disconnectAudioSource(sourceDesc);
3422
3423 mAudioSources.removeItem(handle);
3424 return status;
3425}
3426
Andy Hung2ddee192015-12-18 17:34:44 -08003427status_t AudioPolicyManager::setMasterMono(bool mono)
3428{
3429 if (mMasterMono == mono) {
3430 return NO_ERROR;
3431 }
3432 mMasterMono = mono;
3433 // if enabling mono we close all offloaded devices, which will invalidate the
3434 // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible
3435 // for recreating the new AudioTrack as non-offloaded PCM.
3436 //
3437 // If disabling mono, we leave all tracks as is: we don't know which clients
3438 // and tracks are able to be recreated as offloaded. The next "song" should
3439 // play back offloaded.
3440 if (mMasterMono) {
3441 Vector<audio_io_handle_t> offloaded;
3442 for (size_t i = 0; i < mOutputs.size(); ++i) {
3443 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
3444 if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
3445 offloaded.push(desc->mIoHandle);
3446 }
3447 }
3448 for (size_t i = 0; i < offloaded.size(); ++i) {
3449 closeOutput(offloaded[i]);
3450 }
3451 }
3452 // update master mono for all remaining outputs
3453 for (size_t i = 0; i < mOutputs.size(); ++i) {
3454 updateMono(mOutputs.keyAt(i));
3455 }
3456 return NO_ERROR;
3457}
3458
3459status_t AudioPolicyManager::getMasterMono(bool *mono)
3460{
3461 *mono = mMasterMono;
3462 return NO_ERROR;
3463}
3464
Eric Laurentac9cef52017-06-09 15:46:26 -07003465float AudioPolicyManager::getStreamVolumeDB(
3466 audio_stream_type_t stream, int index, audio_devices_t device)
3467{
3468 return computeVolume(stream, index, device);
3469}
3470
Eric Laurentd60560a2015-04-10 11:31:20 -07003471status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3472{
3473 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3474
3475 sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle);
3476 if (patchDesc == 0) {
3477 ALOGW("%s source has no patch with handle %d", __FUNCTION__,
3478 sourceDesc->mPatchDesc->mHandle);
3479 return BAD_VALUE;
3480 }
3481 removeAudioPatch(sourceDesc->mPatchDesc->mHandle);
3482
Eric Laurent28d09f02016-03-08 10:43:05 -08003483 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003484 sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote();
3485 if (swOutputDesc != 0) {
3486 stopSource(swOutputDesc, stream, false);
3487 mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3488 } else {
3489 sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote();
3490 if (hwOutputDesc != 0) {
3491 // release patch between src device and output device
3492 // close Hwoutput and remove from mHwOutputs
3493 } else {
3494 ALOGW("%s source has neither SW nor HW output", __FUNCTION__);
3495 }
3496 }
3497 return NO_ERROR;
3498}
3499
3500sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput(
3501 audio_io_handle_t output, routing_strategy strategy)
3502{
3503 sp<AudioSourceDescriptor> source;
3504 for (size_t i = 0; i < mAudioSources.size(); i++) {
3505 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3506 routing_strategy sourceStrategy =
3507 (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
3508 sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote();
3509 if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) {
3510 source = sourceDesc;
3511 break;
3512 }
3513 }
3514 return source;
Eric Laurent554a2772015-04-10 11:29:24 -07003515}
3516
Eric Laurente552edb2014-03-10 17:42:56 -07003517// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07003518// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07003519// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07003520uint32_t AudioPolicyManager::nextAudioPortGeneration()
3521{
3522 return android_atomic_inc(&mAudioPortGeneration);
3523}
3524
Jaekyun Seok0d4a6af2017-02-17 17:10:17 +09003525#ifdef USE_XML_AUDIO_POLICY_CONF
3526// Treblized audio policy xml config will be located in /odm/etc or /vendor/etc.
3527static const char *kConfigLocationList[] =
3528 {"/odm/etc", "/vendor/etc", "/system/etc"};
3529static const int kConfigLocationListSize =
3530 (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0]));
3531
3532static status_t deserializeAudioPolicyXmlConfig(AudioPolicyConfig &config) {
3533 char audioPolicyXmlConfigFile[AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH];
3534 status_t ret;
3535
3536 for (int i = 0; i < kConfigLocationListSize; i++) {
3537 PolicySerializer serializer;
3538 snprintf(audioPolicyXmlConfigFile,
3539 sizeof(audioPolicyXmlConfigFile),
3540 "%s/%s",
3541 kConfigLocationList[i],
3542 AUDIO_POLICY_XML_CONFIG_FILE_NAME);
3543 ret = serializer.deserialize(audioPolicyXmlConfigFile, config);
3544 if (ret == NO_ERROR) {
3545 break;
3546 }
3547 }
3548 return ret;
3549}
3550#endif
3551
Eric Laurente0720872014-03-11 09:30:41 -07003552AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07003553 :
Eric Laurente552edb2014-03-10 17:42:56 -07003554 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07003555 mA2dpSuspended(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003556 mAudioPortGeneration(1),
3557 mBeaconMuteRefCount(0),
3558 mBeaconPlayingRefCount(0),
Eric Laurent9459fb02015-08-12 18:36:32 -07003559 mBeaconMuted(false),
Andy Hung2ddee192015-12-18 17:34:44 -08003560 mTtsOutputAvailable(false),
Eric Laurent36829f92017-04-07 19:04:42 -07003561 mMasterMono(false),
Chris Thornton2b864642017-06-27 21:26:07 -07003562 mMusicEffectOutput(AUDIO_IO_HANDLE_NONE),
3563 mHasComputedSoundTriggerSupportsConcurrentCapture(false)
Eric Laurente552edb2014-03-10 17:42:56 -07003564{
François Gaffied1ab2bd2015-12-02 18:20:06 +01003565 mUidCached = getuid();
3566 mpClientInterface = clientInterface;
3567
3568 // TODO: remove when legacy conf file is removed. true on devices that use DRC on the
3569 // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly.
3570 // Note: remove also speaker_drc_enabled from global configuration of XML config file.
3571 bool speakerDrcEnabled = false;
3572
3573#ifdef USE_XML_AUDIO_POLICY_CONF
3574 mVolumeCurves = new VolumeCurvesCollection();
3575 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3576 mDefaultOutputDevice, speakerDrcEnabled,
3577 static_cast<VolumeCurvesCollection *>(mVolumeCurves));
Jaekyun Seok0d4a6af2017-02-17 17:10:17 +09003578 if (deserializeAudioPolicyXmlConfig(config) != NO_ERROR) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01003579#else
3580 mVolumeCurves = new StreamDescriptorCollection();
3581 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3582 mDefaultOutputDevice, speakerDrcEnabled);
3583 if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) &&
3584 (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) {
3585#endif
3586 ALOGE("could not load audio policy configuration file, setting defaults");
3587 config.setDefault();
3588 }
3589 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
3590 mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled);
3591
3592 // Once policy config has been parsed, retrieve an instance of the engine and initialize it.
François Gaffie2110e042015-03-24 08:41:51 +01003593 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
3594 if (!engineInstance) {
3595 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
3596 return;
3597 }
3598 // Retrieve the Policy Manager Interface
3599 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
3600 if (mEngine == NULL) {
3601 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
3602 return;
3603 }
3604 mEngine->setObserver(this);
3605 status_t status = mEngine->initCheck();
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07003606 (void) status;
François Gaffie2110e042015-03-24 08:41:51 +01003607 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
3608
Eric Laurent3a4311c2014-03-17 12:00:47 -07003609 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07003610 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07003611 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
3612 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07003613 for (size_t i = 0; i < mHwModules.size(); i++) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003614 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003615 if (mHwModules[i]->mHandle == 0) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003616 ALOGW("could not open HW module %s", mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003617 continue;
3618 }
3619 // open all output streams needed to access attached devices
3620 // except for direct output streams that are only opened when they are actually
3621 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07003622 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07003623 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3624 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003625 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003626
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003627 if (!outProfile->hasSupportedDevices()) {
3628 ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003629 continue;
3630 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003631 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) {
Eric Laurent9459fb02015-08-12 18:36:32 -07003632 mTtsOutputAvailable = true;
3633 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003634
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003635 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
Eric Laurentd78f1532014-09-16 16:38:20 -07003636 continue;
3637 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003638 audio_devices_t profileType = outProfile->getSupportedDevicesType();
François Gaffie53615e22015-03-19 09:24:12 +01003639 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
3640 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07003641 } else {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003642 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003643 // outputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003644 profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes);
Eric Laurente552edb2014-03-10 17:42:56 -07003645 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003646 if ((profileType & outputDeviceTypes) == 0) {
3647 continue;
3648 }
Eric Laurentc75307b2015-03-17 15:29:32 -07003649 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
3650 mpClientInterface);
Eric Laurentc40d9692016-04-13 19:14:13 -07003651 const DeviceVector &supportedDevices = outProfile->getSupportedDevices();
3652 const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType);
3653 String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress
3654 : String8("");
Eric Laurentd78f1532014-09-16 16:38:20 -07003655
3656 outputDesc->mDevice = profileType;
3657 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3658 config.sample_rate = outputDesc->mSamplingRate;
3659 config.channel_mask = outputDesc->mChannelMask;
3660 config.format = outputDesc->mFormat;
3661 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003662 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003663 &output,
3664 &config,
3665 &outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003666 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003667 &outputDesc->mLatency,
3668 outputDesc->mFlags);
3669
3670 if (status != NO_ERROR) {
3671 ALOGW("Cannot open output stream for device %08x on hw module %s",
3672 outputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003673 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003674 } else {
3675 outputDesc->mSamplingRate = config.sample_rate;
3676 outputDesc->mChannelMask = config.channel_mask;
3677 outputDesc->mFormat = config.format;
3678
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003679 for (size_t k = 0; k < supportedDevices.size(); k++) {
3680 ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003681 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08003682 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
3683 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003684 }
3685 }
3686 if (mPrimaryOutput == 0 &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003687 outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003688 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07003689 }
3690 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003691 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07003692 outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003693 true,
3694 0,
3695 NULL,
3696 address.string());
Eric Laurentd78f1532014-09-16 16:38:20 -07003697 }
Eric Laurente552edb2014-03-10 17:42:56 -07003698 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003699 // open input streams needed to access attached devices to validate
3700 // mAvailableInputDevices list
3701 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
3702 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003703 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003704
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003705 if (!inProfile->hasSupportedDevices()) {
3706 ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003707 continue;
3708 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003709 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003710 // inputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003711 audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes);
3712
Eric Laurentd78f1532014-09-16 16:38:20 -07003713 if ((profileType & inputDeviceTypes) == 0) {
3714 continue;
3715 }
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08003716 sp<AudioInputDescriptor> inputDesc =
3717 new AudioInputDescriptor(inProfile);
Eric Laurentd78f1532014-09-16 16:38:20 -07003718
Eric Laurentd78f1532014-09-16 16:38:20 -07003719 inputDesc->mDevice = profileType;
3720
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003721 // find the address
3722 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
3723 // the inputs vector must be of size 1, but we don't want to crash here
3724 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
3725 : String8("");
3726 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
3727 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
3728
Eric Laurentd78f1532014-09-16 16:38:20 -07003729 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3730 config.sample_rate = inputDesc->mSamplingRate;
3731 config.channel_mask = inputDesc->mChannelMask;
3732 config.format = inputDesc->mFormat;
3733 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003734 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003735 &input,
3736 &config,
3737 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003738 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003739 AUDIO_SOURCE_MIC,
3740 AUDIO_INPUT_FLAG_NONE);
3741
3742 if (status == NO_ERROR) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003743 const DeviceVector &supportedDevices = inProfile->getSupportedDevices();
3744 for (size_t k = 0; k < supportedDevices.size(); k++) {
3745 ssize_t index = mAvailableInputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003746 // give a valid ID to an attached device once confirmed it is reachable
Eric Laurent45aabc32015-08-06 09:11:13 -07003747 if (index >= 0) {
3748 sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index];
3749 if (!devDesc->isAttached()) {
3750 devDesc->attach(mHwModules[i]);
Eric Laurent83efe1c2017-07-09 16:51:08 -07003751 devDesc->importAudioPort(inProfile, true);
Eric Laurent45aabc32015-08-06 09:11:13 -07003752 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003753 }
3754 }
3755 mpClientInterface->closeInput(input);
3756 } else {
3757 ALOGW("Cannot open input stream for device %08x on hw module %s",
3758 inputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003759 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003760 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003761 }
3762 }
3763 // make sure all attached devices have been allocated a unique ID
3764 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003765 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003766 ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003767 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
3768 continue;
3769 }
François Gaffie2110e042015-03-24 08:41:51 +01003770 // The device is now validated and can be appended to the available devices of the engine
3771 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
3772 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003773 i++;
3774 }
3775 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003776 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01003777 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003778 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
3779 continue;
3780 }
François Gaffie2110e042015-03-24 08:41:51 +01003781 // The device is now validated and can be appended to the available devices of the engine
3782 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
3783 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003784 i++;
3785 }
3786 // make sure default device is reachable
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003787 if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01003788 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003789 }
Eric Laurente552edb2014-03-10 17:42:56 -07003790
3791 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
3792
3793 updateDevicesAndOutputs();
Eric Laurente552edb2014-03-10 17:42:56 -07003794}
3795
Eric Laurente0720872014-03-11 09:30:41 -07003796AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07003797{
Eric Laurente552edb2014-03-10 17:42:56 -07003798 for (size_t i = 0; i < mOutputs.size(); i++) {
3799 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003800 }
3801 for (size_t i = 0; i < mInputs.size(); i++) {
3802 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003803 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003804 mAvailableOutputDevices.clear();
3805 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003806 mOutputs.clear();
3807 mInputs.clear();
3808 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07003809}
3810
Eric Laurente0720872014-03-11 09:30:41 -07003811status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07003812{
Eric Laurent87ffa392015-05-22 10:32:38 -07003813 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003814}
3815
Eric Laurente552edb2014-03-10 17:42:56 -07003816// ---
3817
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003818void AudioPolicyManager::addOutput(audio_io_handle_t output, const sp<SwAudioOutputDescriptor>& outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003819{
François Gaffie98cc1912015-03-18 17:52:40 +01003820 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003821 mOutputs.add(output, outputDesc);
Andy Hung2ddee192015-12-18 17:34:44 -08003822 updateMono(output); // update mono status when adding to output list
Eric Laurent36829f92017-04-07 19:04:42 -07003823 selectOutputForMusicEffects();
Eric Laurent6a94d692014-05-20 11:18:06 -07003824 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003825}
3826
François Gaffie53615e22015-03-19 09:24:12 +01003827void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3828{
3829 mOutputs.removeItem(output);
Eric Laurent36829f92017-04-07 19:04:42 -07003830 selectOutputForMusicEffects();
François Gaffie53615e22015-03-19 09:24:12 +01003831}
3832
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003833void AudioPolicyManager::addInput(audio_io_handle_t input, const sp<AudioInputDescriptor>& inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003834{
François Gaffie98cc1912015-03-18 17:52:40 +01003835 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003836 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003837 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003838}
Eric Laurente552edb2014-03-10 17:42:56 -07003839
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003840void AudioPolicyManager::findIoHandlesByAddress(const sp<SwAudioOutputDescriptor>& desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003841 const audio_devices_t device /*in*/,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003842 const String8& address /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003843 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003844 sp<DeviceDescriptor> devDesc =
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003845 desc->mProfile->getSupportedDeviceByAddress(device, address);
keunyoung3190e672014-12-30 13:00:52 -08003846 if (devDesc != 0) {
3847 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3848 desc->mIoHandle, address.string());
3849 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003850 }
3851}
3852
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003853status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003854 audio_policy_dev_state_t state,
3855 SortedVector<audio_io_handle_t>& outputs,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003856 const String8& address)
Eric Laurente552edb2014-03-10 17:42:56 -07003857{
François Gaffie53615e22015-03-19 09:24:12 +01003858 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003859 sp<SwAudioOutputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003860
3861 if (audio_device_is_digital(device)) {
3862 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003863 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003864 }
Eric Laurente552edb2014-03-10 17:42:56 -07003865
Eric Laurent3b73df72014-03-11 09:06:29 -07003866 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003867 // first list already open outputs that can be routed to this device
3868 for (size_t i = 0; i < mOutputs.size(); i++) {
3869 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003870 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003871 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003872 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3873 outputs.add(mOutputs.keyAt(i));
3874 } else {
3875 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003876 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003877 }
Eric Laurente552edb2014-03-10 17:42:56 -07003878 }
3879 }
3880 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003881 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003882 for (size_t i = 0; i < mHwModules.size(); i++)
3883 {
3884 if (mHwModules[i]->mHandle == 0) {
3885 continue;
3886 }
3887 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3888 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003889 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003890 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003891 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003892 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003893 profiles.add(profile);
3894 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3895 }
Eric Laurente552edb2014-03-10 17:42:56 -07003896 }
3897 }
3898 }
3899
Eric Laurent7b279bb2015-12-14 10:18:23 -08003900 ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size());
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003901
Eric Laurente552edb2014-03-10 17:42:56 -07003902 if (profiles.isEmpty() && outputs.isEmpty()) {
3903 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3904 return BAD_VALUE;
3905 }
3906
3907 // open outputs for matching profiles if needed. Direct outputs are also opened to
3908 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3909 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003910 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003911
3912 // nothing to do if one output is already opened for this profile
3913 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003914 for (j = 0; j < outputs.size(); j++) {
3915 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003916 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003917 // matching profile: save the sample rates, format and channel masks supported
3918 // by the profile in our device descriptor
Paul McLean9080a4c2015-06-18 08:24:02 -07003919 if (audio_device_is_digital(device)) {
3920 devDesc->importAudioPort(profile);
3921 }
Eric Laurente552edb2014-03-10 17:42:56 -07003922 break;
3923 }
3924 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003925 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003926 continue;
3927 }
3928
Eric Laurent83efe1c2017-07-09 16:51:08 -07003929 ALOGV("opening output for device %08x with params %s profile %p name %s",
3930 device, address.string(), profile.get(), profile->getName().string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003931 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003932 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003933 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3934 config.sample_rate = desc->mSamplingRate;
3935 config.channel_mask = desc->mChannelMask;
3936 config.format = desc->mFormat;
3937 config.offload_info.sample_rate = desc->mSamplingRate;
3938 config.offload_info.channel_mask = desc->mChannelMask;
3939 config.offload_info.format = desc->mFormat;
3940 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003941 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003942 &output,
3943 &config,
3944 &desc->mDevice,
3945 address,
3946 &desc->mLatency,
3947 desc->mFlags);
3948 if (status == NO_ERROR) {
3949 desc->mSamplingRate = config.sample_rate;
3950 desc->mChannelMask = config.channel_mask;
3951 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003952
Eric Laurentd4692962014-05-05 18:13:44 -07003953 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003954 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003955 char *param = audio_device_address_to_parameter(device, address);
3956 mpClientInterface->setParameters(output, String8(param));
3957 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003958 }
Phil Burk00eeb322016-03-31 12:41:00 -07003959 updateAudioProfiles(device, output, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01003960 if (!profile->hasValidAudioProfile()) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003961 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003962 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003963 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003964 } else if (profile->hasDynamicAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07003965 mpClientInterface->closeOutput(output);
Phil Burk702b1052016-03-02 16:38:26 -08003966 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003967 profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003968 config.offload_info.sample_rate = config.sample_rate;
3969 config.offload_info.channel_mask = config.channel_mask;
3970 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07003971 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003972 &output,
3973 &config,
3974 &desc->mDevice,
3975 address,
3976 &desc->mLatency,
3977 desc->mFlags);
3978 if (status == NO_ERROR) {
3979 desc->mSamplingRate = config.sample_rate;
3980 desc->mChannelMask = config.channel_mask;
3981 desc->mFormat = config.format;
3982 } else {
3983 output = AUDIO_IO_HANDLE_NONE;
3984 }
Eric Laurentd4692962014-05-05 18:13:44 -07003985 }
3986
Eric Laurentcf2c0212014-07-25 16:20:43 -07003987 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003988 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003989 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003990 sp<AudioPolicyMix> policyMix;
3991 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003992 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3993 address.string());
3994 }
François Gaffie036e1e92015-03-19 10:16:24 +01003995 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07003996 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01003997
Eric Laurent87ffa392015-05-22 10:32:38 -07003998 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
3999 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08004000 // no duplicated output for direct outputs and
4001 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07004002 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07004003
Eric Laurentd4692962014-05-05 18:13:44 -07004004 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07004005 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07004006
Eric Laurentd4692962014-05-05 18:13:44 -07004007 //TODO: configure audio effect output stage here
4008
4009 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07004010 duplicatedOutput =
4011 mpClientInterface->openDuplicateOutput(output,
4012 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004013 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07004014 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07004015 sp<SwAudioOutputDescriptor> dupOutputDesc =
4016 new SwAudioOutputDescriptor(NULL, mpClientInterface);
4017 dupOutputDesc->mOutput1 = mPrimaryOutput;
4018 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07004019 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
4020 dupOutputDesc->mFormat = desc->mFormat;
4021 dupOutputDesc->mChannelMask = desc->mChannelMask;
4022 dupOutputDesc->mLatency = desc->mLatency;
4023 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07004024 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07004025 } else {
4026 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07004027 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07004028 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01004029 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07004030 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07004031 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07004032 }
Eric Laurente552edb2014-03-10 17:42:56 -07004033 }
4034 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07004035 } else {
4036 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07004037 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07004038 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07004039 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004040 profiles.removeAt(profile_index);
4041 profile_index--;
4042 } else {
4043 outputs.add(output);
Paul McLean9080a4c2015-06-18 08:24:02 -07004044 // Load digital format info only for digital devices
4045 if (audio_device_is_digital(device)) {
4046 devDesc->importAudioPort(profile);
4047 }
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07004048
François Gaffie53615e22015-03-19 09:24:12 +01004049 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004050 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
4051 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07004052 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004053 NULL/*patch handle*/, address.string());
4054 }
Eric Laurente552edb2014-03-10 17:42:56 -07004055 ALOGV("checkOutputsForDevice(): adding output %d", output);
4056 }
4057 }
4058
4059 if (profiles.isEmpty()) {
4060 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
4061 return BAD_VALUE;
4062 }
Eric Laurentd4692962014-05-05 18:13:44 -07004063 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07004064 // check if one opened output is not needed any more after disconnecting one device
4065 for (size_t i = 0; i < mOutputs.size(); i++) {
4066 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004067 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004068 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01004069 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07004070 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08004071 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07004072 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004073 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
4074 mOutputs.keyAt(i));
4075 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004076 }
Eric Laurente552edb2014-03-10 17:42:56 -07004077 }
4078 }
Eric Laurentd4692962014-05-05 18:13:44 -07004079 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07004080 for (size_t i = 0; i < mHwModules.size(); i++)
4081 {
4082 if (mHwModules[i]->mHandle == 0) {
4083 continue;
4084 }
4085 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
4086 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004087 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004088 if (profile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07004089 ALOGV("checkOutputsForDevice(): "
4090 "clearing direct output profile %zu on module %zu", j, i);
François Gaffie112b0af2015-11-19 16:13:25 +01004091 profile->clearAudioProfiles();
Eric Laurente552edb2014-03-10 17:42:56 -07004092 }
4093 }
4094 }
4095 }
4096 return NO_ERROR;
4097}
4098
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004099status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01004100 audio_policy_dev_state_t state,
4101 SortedVector<audio_io_handle_t>& inputs,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004102 const String8& address)
Eric Laurentd4692962014-05-05 18:13:44 -07004103{
Paul McLean9080a4c2015-06-18 08:24:02 -07004104 audio_devices_t device = devDesc->type();
Eric Laurent1f2f2232014-06-02 12:01:23 -07004105 sp<AudioInputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07004106
4107 if (audio_device_is_digital(device)) {
4108 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08004109 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07004110 }
4111
Eric Laurentd4692962014-05-05 18:13:44 -07004112 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
4113 // first list already open inputs that can be routed to this device
4114 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
4115 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004116 if (desc->mProfile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07004117 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
4118 inputs.add(mInputs.keyAt(input_index));
4119 }
4120 }
4121
4122 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07004123 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07004124 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
4125 {
4126 if (mHwModules[module_idx]->mHandle == 0) {
4127 continue;
4128 }
4129 for (size_t profile_index = 0;
4130 profile_index < mHwModules[module_idx]->mInputProfiles.size();
4131 profile_index++)
4132 {
Eric Laurent275e8e92014-11-30 15:14:47 -08004133 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
4134
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004135 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01004136 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004137 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004138 profiles.add(profile);
4139 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
4140 profile_index, module_idx);
4141 }
Eric Laurentd4692962014-05-05 18:13:44 -07004142 }
4143 }
4144 }
4145
4146 if (profiles.isEmpty() && inputs.isEmpty()) {
4147 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
4148 return BAD_VALUE;
4149 }
4150
4151 // open inputs for matching profiles if needed. Direct inputs are also opened to
4152 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
4153 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
4154
Eric Laurent1c333e22014-05-20 10:48:17 -07004155 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07004156 // nothing to do if one input is already opened for this profile
4157 size_t input_index;
4158 for (input_index = 0; input_index < mInputs.size(); input_index++) {
4159 desc = mInputs.valueAt(input_index);
4160 if (desc->mProfile == profile) {
Paul McLean9080a4c2015-06-18 08:24:02 -07004161 if (audio_device_is_digital(device)) {
4162 devDesc->importAudioPort(profile);
4163 }
Eric Laurentd4692962014-05-05 18:13:44 -07004164 break;
4165 }
4166 }
4167 if (input_index != mInputs.size()) {
4168 continue;
4169 }
4170
4171 ALOGV("opening input for device 0x%X with params %s", device, address.string());
4172 desc = new AudioInputDescriptor(profile);
4173 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07004174 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
4175 config.sample_rate = desc->mSamplingRate;
4176 config.channel_mask = desc->mChannelMask;
4177 config.format = desc->mFormat;
4178 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent83efe1c2017-07-09 16:51:08 -07004179
4180 ALOGV("opening inputput for device %08x with params %s profile %p name %s",
4181 desc->mDevice, address.string(), profile.get(), profile->getName().string());
4182
Eric Laurent322b4d22015-04-03 15:57:54 -07004183 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07004184 &input,
4185 &config,
4186 &desc->mDevice,
4187 address,
4188 AUDIO_SOURCE_MIC,
4189 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07004190
Eric Laurentcf2c0212014-07-25 16:20:43 -07004191 if (status == NO_ERROR) {
4192 desc->mSamplingRate = config.sample_rate;
4193 desc->mChannelMask = config.channel_mask;
4194 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07004195
Eric Laurentd4692962014-05-05 18:13:44 -07004196 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07004197 char *param = audio_device_address_to_parameter(device, address);
4198 mpClientInterface->setParameters(input, String8(param));
4199 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07004200 }
Phil Burk00eeb322016-03-31 12:41:00 -07004201 updateAudioProfiles(device, input, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01004202 if (!profile->hasValidAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07004203 ALOGW("checkInputsForDevice() direct input missing param");
4204 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004205 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07004206 }
4207
4208 if (input != 0) {
4209 addInput(input, desc);
4210 }
4211 } // endif input != 0
4212
Eric Laurentcf2c0212014-07-25 16:20:43 -07004213 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07004214 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07004215 profiles.removeAt(profile_index);
4216 profile_index--;
4217 } else {
4218 inputs.add(input);
Paul McLean9080a4c2015-06-18 08:24:02 -07004219 if (audio_device_is_digital(device)) {
4220 devDesc->importAudioPort(profile);
4221 }
Eric Laurentd4692962014-05-05 18:13:44 -07004222 ALOGV("checkInputsForDevice(): adding input %d", input);
4223 }
4224 } // end scan profiles
4225
4226 if (profiles.isEmpty()) {
4227 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
4228 return BAD_VALUE;
4229 }
4230 } else {
4231 // Disconnect
4232 // check if one opened input is not needed any more after disconnecting one device
4233 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
4234 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004235 if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) {
Eric Laurentd4692962014-05-05 18:13:44 -07004236 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
4237 mInputs.keyAt(input_index));
4238 inputs.add(mInputs.keyAt(input_index));
4239 }
4240 }
4241 // Clear any profiles associated with the disconnected device.
4242 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
4243 if (mHwModules[module_index]->mHandle == 0) {
4244 continue;
4245 }
4246 for (size_t profile_index = 0;
4247 profile_index < mHwModules[module_index]->mInputProfiles.size();
4248 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004249 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004250 if (profile->supportDevice(device)) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07004251 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07004252 profile_index, module_index);
François Gaffie112b0af2015-11-19 16:13:25 +01004253 profile->clearAudioProfiles();
Eric Laurentd4692962014-05-05 18:13:44 -07004254 }
4255 }
4256 }
4257 } // end disconnect
4258
4259 return NO_ERROR;
4260}
4261
4262
Eric Laurente0720872014-03-11 09:30:41 -07004263void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07004264{
4265 ALOGV("closeOutput(%d)", output);
4266
Eric Laurentc75307b2015-03-17 15:29:32 -07004267 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004268 if (outputDesc == NULL) {
4269 ALOGW("closeOutput() unknown output %d", output);
4270 return;
4271 }
François Gaffie036e1e92015-03-19 10:16:24 +01004272 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08004273
Eric Laurente552edb2014-03-10 17:42:56 -07004274 // look for duplicated outputs connected to the output being removed.
4275 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004276 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07004277 if (dupOutputDesc->isDuplicated() &&
4278 (dupOutputDesc->mOutput1 == outputDesc ||
4279 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004280 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07004281 if (dupOutputDesc->mOutput1 == outputDesc) {
4282 outputDesc2 = dupOutputDesc->mOutput2;
4283 } else {
4284 outputDesc2 = dupOutputDesc->mOutput1;
4285 }
4286 // As all active tracks on duplicated output will be deleted,
4287 // and as they were also referenced on the other output, the reference
4288 // count for their stream type must be adjusted accordingly on
4289 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07004290 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07004291 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07004292 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07004293 }
4294 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
4295 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
4296
4297 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01004298 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07004299 }
4300 }
4301
Eric Laurent05b90f82014-08-27 15:32:29 -07004302 nextAudioPortGeneration();
4303
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004304 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004305 if (index >= 0) {
4306 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07004307 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07004308 mAudioPatches.removeItemsAt(index);
4309 mpClientInterface->onAudioPatchListUpdate();
4310 }
4311
Eric Laurente552edb2014-03-10 17:42:56 -07004312 AudioParameter param;
4313 param.add(String8("closing"), String8("true"));
4314 mpClientInterface->setParameters(output, param.toString());
4315
4316 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01004317 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004318 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07004319}
4320
4321void AudioPolicyManager::closeInput(audio_io_handle_t input)
4322{
4323 ALOGV("closeInput(%d)", input);
4324
4325 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
4326 if (inputDesc == NULL) {
4327 ALOGW("closeInput() unknown input %d", input);
4328 return;
4329 }
4330
Eric Laurent6a94d692014-05-20 11:18:06 -07004331 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07004332
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004333 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004334 if (index >= 0) {
4335 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07004336 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07004337 mAudioPatches.removeItemsAt(index);
4338 mpClientInterface->onAudioPatchListUpdate();
4339 }
4340
4341 mpClientInterface->closeInput(input);
4342 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07004343}
4344
Eric Laurentc75307b2015-03-17 15:29:32 -07004345SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
4346 audio_devices_t device,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004347 const SwAudioOutputCollection& openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07004348{
4349 SortedVector<audio_io_handle_t> outputs;
4350
4351 ALOGVV("getOutputsForDevice() device %04x", device);
4352 for (size_t i = 0; i < openOutputs.size(); i++) {
Eric Laurent37ddbb42016-08-10 16:19:14 -07004353 ALOGVV("output %zu isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004354 i, openOutputs.valueAt(i)->isDuplicated(),
4355 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004356 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
4357 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
4358 outputs.add(openOutputs.keyAt(i));
4359 }
4360 }
4361 return outputs;
4362}
4363
Eric Laurente0720872014-03-11 09:30:41 -07004364bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01004365 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07004366{
4367 if (outputs1.size() != outputs2.size()) {
4368 return false;
4369 }
4370 for (size_t i = 0; i < outputs1.size(); i++) {
4371 if (outputs1[i] != outputs2[i]) {
4372 return false;
4373 }
4374 }
4375 return true;
4376}
4377
Eric Laurente0720872014-03-11 09:30:41 -07004378void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07004379{
4380 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
4381 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
4382 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
4383 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
4384
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004385 // also take into account external policy-related changes: add all outputs which are
4386 // associated with policies in the "before" and "after" output vectors
4387 ALOGVV("checkOutputForStrategy(): policy related outputs");
4388 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004389 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004390 if (desc != 0 && desc->mPolicyMix != NULL) {
4391 srcOutputs.add(desc->mIoHandle);
4392 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
4393 }
4394 }
4395 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004396 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004397 if (desc != 0 && desc->mPolicyMix != NULL) {
4398 dstOutputs.add(desc->mIoHandle);
4399 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
4400 }
4401 }
4402
Eric Laurente552edb2014-03-10 17:42:56 -07004403 if (!vectorsEqual(srcOutputs,dstOutputs)) {
4404 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
4405 strategy, srcOutputs[0], dstOutputs[0]);
4406 // mute strategy while moving tracks from one output to another
4407 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004408 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01004409 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004410 setStrategyMute(strategy, true, desc);
4411 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004412 }
Eric Laurentd60560a2015-04-10 11:31:20 -07004413 sp<AudioSourceDescriptor> source =
4414 getSourceForStrategyOnOutput(srcOutputs[i], strategy);
4415 if (source != 0){
4416 connectAudioSource(source);
4417 }
Eric Laurente552edb2014-03-10 17:42:56 -07004418 }
4419
4420 // Move effects associated to this strategy from previous output to new output
4421 if (strategy == STRATEGY_MEDIA) {
Eric Laurent36829f92017-04-07 19:04:42 -07004422 selectOutputForMusicEffects();
Eric Laurente552edb2014-03-10 17:42:56 -07004423 }
4424 // Move tracks associated to this strategy from previous output to new output
Eric Laurent794fde22016-03-11 09:50:45 -08004425 for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004426 if (getStrategy((audio_stream_type_t)i) == strategy) {
4427 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07004428 }
4429 }
4430 }
4431}
4432
Eric Laurente0720872014-03-11 09:30:41 -07004433void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07004434{
François Gaffie2110e042015-03-24 08:41:51 +01004435 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004436 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004437 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01004438 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004439 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004440 checkOutputForStrategy(STRATEGY_SONIFICATION);
4441 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004442 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07004443 checkOutputForStrategy(STRATEGY_MEDIA);
4444 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004445 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07004446}
4447
Eric Laurente0720872014-03-11 09:30:41 -07004448void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07004449{
François Gaffie53615e22015-03-19 09:24:12 +01004450 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07004451 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004452 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07004453 return;
4454 }
4455
Eric Laurent3a4311c2014-03-17 12:00:47 -07004456 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08004457 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
4458 ~AUDIO_DEVICE_BIT_IN) != 0) ||
4459 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurentf732e072016-08-03 19:30:28 -07004460
4461 // if suspended, restore A2DP output if:
4462 // ((SCO device is NOT connected) ||
4463 // ((forced usage communication is NOT SCO) && (forced usage for record is NOT SCO) &&
4464 // (phone state is NOT in call) && (phone state is NOT ringing)))
Eric Laurente552edb2014-03-10 17:42:56 -07004465 //
Eric Laurentf732e072016-08-03 19:30:28 -07004466 // if not suspended, suspend A2DP output if:
4467 // (SCO device is connected) &&
4468 // ((forced usage for communication is SCO) || (forced usage for record is SCO) ||
4469 // ((phone state is in call) || (phone state is ringing)))
Eric Laurente552edb2014-03-10 17:42:56 -07004470 //
4471 if (mA2dpSuspended) {
Eric Laurentf732e072016-08-03 19:30:28 -07004472 if (!isScoConnected ||
4473 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) !=
4474 AUDIO_POLICY_FORCE_BT_SCO) &&
4475 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) !=
4476 AUDIO_POLICY_FORCE_BT_SCO) &&
4477 (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
François Gaffie2110e042015-03-24 08:41:51 +01004478 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004479
4480 mpClientInterface->restoreOutput(a2dpOutput);
4481 mA2dpSuspended = false;
4482 }
4483 } else {
Eric Laurentf732e072016-08-03 19:30:28 -07004484 if (isScoConnected &&
4485 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ==
4486 AUDIO_POLICY_FORCE_BT_SCO) ||
4487 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) ==
4488 AUDIO_POLICY_FORCE_BT_SCO) ||
4489 (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
François Gaffie2110e042015-03-24 08:41:51 +01004490 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004491
4492 mpClientInterface->suspendOutput(a2dpOutput);
4493 mA2dpSuspended = true;
4494 }
4495 }
4496}
4497
Eric Laurentc75307b2015-03-17 15:29:32 -07004498audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
4499 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004500{
4501 audio_devices_t device = AUDIO_DEVICE_NONE;
4502
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004503 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004504 if (index >= 0) {
4505 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4506 if (patchDesc->mUid != mUidCached) {
4507 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004508 outputDesc->device(), outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004509 return outputDesc->device();
4510 }
4511 }
4512
Eric Laurente552edb2014-03-10 17:42:56 -07004513 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05004514 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004515 // use device for strategy enforced audible
4516 // 2: we are in call or the strategy phone is active on the output:
4517 // use device for strategy phone
Jean-Michel Trivi178683b2017-08-30 18:07:06 -07004518 // 3: the strategy sonification is active on the output:
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004519 // use device for strategy sonification
Jean-Michel Trivi178683b2017-08-30 18:07:06 -07004520 // 4: the strategy for enforced audible is active but not enforced on the output:
4521 // use the device for strategy enforced audible
Eric Laurent28d09f02016-03-08 10:43:05 -08004522 // 5: the strategy accessibility is active on the output:
4523 // use device for strategy accessibility
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004524 // 6: the strategy "respectful" sonification is active on the output:
4525 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08004526 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004527 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08004528 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004529 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08004530 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004531 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01004532 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004533 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07004534 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
4535 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01004536 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004537 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004538 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004539 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Jean-Michel Trivi178683b2017-08-30 18:07:06 -07004540 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
4541 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
Eric Laurent28d09f02016-03-08 10:43:05 -08004542 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
4543 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004544 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004545 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004546 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004547 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004548 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004549 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004550 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004551 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004552 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004553 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07004554 }
4555
Eric Laurent1c333e22014-05-20 10:48:17 -07004556 ALOGV("getNewOutputDevice() selected device %x", device);
4557 return device;
4558}
4559
Eric Laurentfb66dd92016-01-28 18:32:03 -08004560audio_devices_t AudioPolicyManager::getNewInputDevice(const sp<AudioInputDescriptor>& inputDesc)
Eric Laurent1c333e22014-05-20 10:48:17 -07004561{
Eric Laurentfb66dd92016-01-28 18:32:03 -08004562 audio_devices_t device = AUDIO_DEVICE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07004563
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004564 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004565 if (index >= 0) {
4566 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4567 if (patchDesc->mUid != mUidCached) {
4568 ALOGV("getNewInputDevice() device %08x forced by patch %d",
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004569 inputDesc->mDevice, inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004570 return inputDesc->mDevice;
4571 }
4572 }
4573
Eric Laurentfb66dd92016-01-28 18:32:03 -08004574 audio_source_t source = inputDesc->getHighestPrioritySource(true /*activeOnly*/);
4575 if (isInCall()) {
4576 device = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
4577 } else if (source != AUDIO_SOURCE_DEFAULT) {
4578 device = getDeviceAndMixForInputSource(source);
4579 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004580
Eric Laurente552edb2014-03-10 17:42:56 -07004581 return device;
4582}
4583
Eric Laurent794fde22016-03-11 09:50:45 -08004584bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1,
4585 audio_stream_type_t stream2) {
Jean-Michel Trivi99bb2f92016-11-23 15:52:07 -08004586 return (stream1 == stream2);
Eric Laurent28d09f02016-03-08 10:43:05 -08004587}
4588
Eric Laurente0720872014-03-11 09:30:41 -07004589uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004590 return (uint32_t)getStrategy(stream);
4591}
4592
Eric Laurente0720872014-03-11 09:30:41 -07004593audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004594 // By checking the range of stream before calling getStrategy, we avoid
4595 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
4596 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08004597 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004598 return AUDIO_DEVICE_NONE;
4599 }
Eric Laurent28d09f02016-03-08 10:43:05 -08004600 audio_devices_t devices = AUDIO_DEVICE_NONE;
Eric Laurent794fde22016-03-11 09:50:45 -08004601 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
4602 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004603 continue;
Eric Laurent6a94d692014-05-20 11:18:06 -07004604 }
Eric Laurent794fde22016-03-11 09:50:45 -08004605 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08004606 audio_devices_t curDevices =
Eric Laurent447a87b2016-07-07 17:32:10 -07004607 getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/);
Eric Laurent28d09f02016-03-08 10:43:05 -08004608 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs);
4609 for (size_t i = 0; i < outputs.size(); i++) {
4610 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurent794fde22016-03-11 09:50:45 -08004611 if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004612 curDevices |= outputDesc->device();
4613 }
4614 }
4615 devices |= curDevices;
Eric Laurente552edb2014-03-10 17:42:56 -07004616 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05004617
4618 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
4619 and doesn't really need to.*/
4620 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
4621 devices |= AUDIO_DEVICE_OUT_SPEAKER;
4622 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4623 }
Eric Laurente552edb2014-03-10 17:42:56 -07004624 return devices;
4625}
4626
François Gaffie2110e042015-03-24 08:41:51 +01004627routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
4628{
Eric Laurent223fd5c2014-11-11 13:43:36 -08004629 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01004630 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004631}
4632
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004633uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4634 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004635 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4636 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4637 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004638 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4639 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4640 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004641 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01004642 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004643}
4644
Eric Laurente0720872014-03-11 09:30:41 -07004645void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004646 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004647 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004648 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4649 updateDevicesAndOutputs();
4650 break;
4651 default:
4652 break;
4653 }
4654}
4655
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004656uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
Eric Laurent9459fb02015-08-12 18:36:32 -07004657
4658 // skip beacon mute management if a dedicated TTS output is available
4659 if (mTtsOutputAvailable) {
4660 return 0;
4661 }
4662
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004663 switch(event) {
4664 case STARTING_OUTPUT:
4665 mBeaconMuteRefCount++;
4666 break;
4667 case STOPPING_OUTPUT:
4668 if (mBeaconMuteRefCount > 0) {
4669 mBeaconMuteRefCount--;
4670 }
4671 break;
4672 case STARTING_BEACON:
4673 mBeaconPlayingRefCount++;
4674 break;
4675 case STOPPING_BEACON:
4676 if (mBeaconPlayingRefCount > 0) {
4677 mBeaconPlayingRefCount--;
4678 }
4679 break;
4680 }
4681
4682 if (mBeaconMuteRefCount > 0) {
4683 // any playback causes beacon to be muted
4684 return setBeaconMute(true);
4685 } else {
4686 // no other playback: unmute when beacon starts playing, mute when it stops
4687 return setBeaconMute(mBeaconPlayingRefCount == 0);
4688 }
4689}
4690
4691uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4692 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4693 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4694 // keep track of muted state to avoid repeating mute/unmute operations
4695 if (mBeaconMuted != mute) {
4696 // mute/unmute AUDIO_STREAM_TTS on all outputs
4697 ALOGV("\t muting %d", mute);
4698 uint32_t maxLatency = 0;
4699 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004700 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004701 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004702 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004703 0 /*delay*/, AUDIO_DEVICE_NONE);
4704 const uint32_t latency = desc->latency() * 2;
4705 if (latency > maxLatency) {
4706 maxLatency = latency;
4707 }
4708 }
4709 mBeaconMuted = mute;
4710 return maxLatency;
4711 }
4712 return 0;
4713}
4714
Eric Laurente0720872014-03-11 09:30:41 -07004715audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004716 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004717{
Paul McLeanaa981192015-03-21 09:55:15 -07004718 // Routing
4719 // see if we have an explicit route
4720 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4721 // (getStrategy(stream)).
4722 // if the strategy from the stream type in the RouteMap is the same as the argument above,
Eric Laurentad2e7b92017-09-14 20:06:42 -07004723 // and activity count is non-zero and the device in the route descriptor is available
4724 // then select this device.
Paul McLeanaa981192015-03-21 09:55:15 -07004725 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4726 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
Eric Laurent28d09f02016-03-08 10:43:05 -08004727 routing_strategy routeStrategy = getStrategy(route->mStreamType);
Eric Laurentad2e7b92017-09-14 20:06:42 -07004728 if ((routeStrategy == strategy) && route->isActive() &&
4729 (mAvailableOutputDevices.indexOf(route->mDeviceDescriptor) >= 0)) {
Paul McLeanaa981192015-03-21 09:55:15 -07004730 return route->mDeviceDescriptor->type();
4731 }
4732 }
4733
Eric Laurente552edb2014-03-10 17:42:56 -07004734 if (fromCache) {
4735 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4736 strategy, mDeviceForStrategy[strategy]);
4737 return mDeviceForStrategy[strategy];
4738 }
François Gaffie2110e042015-03-24 08:41:51 +01004739 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004740}
4741
Eric Laurente0720872014-03-11 09:30:41 -07004742void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004743{
4744 for (int i = 0; i < NUM_STRATEGIES; i++) {
4745 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4746 }
4747 mPreviousOutputs = mOutputs;
4748}
4749
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004750uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004751 audio_devices_t prevDevice,
4752 uint32_t delayMs)
4753{
4754 // mute/unmute strategies using an incompatible device combination
4755 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4756 // if unmuting, unmute only after the specified delay
4757 if (outputDesc->isDuplicated()) {
4758 return 0;
4759 }
4760
4761 uint32_t muteWaitMs = 0;
4762 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004763 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004764
4765 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4766 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004767 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004768 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4769 bool doMute = false;
4770
4771 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4772 doMute = true;
4773 outputDesc->mStrategyMutedByDevice[i] = true;
4774 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4775 doMute = true;
4776 outputDesc->mStrategyMutedByDevice[i] = false;
4777 }
Eric Laurent99401132014-05-07 19:48:15 -07004778 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004779 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004780 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004781 // skip output if it does not share any device with current output
4782 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4783 == AUDIO_DEVICE_NONE) {
4784 continue;
4785 }
Eric Laurent37ddbb42016-08-10 16:19:14 -07004786 ALOGVV("checkDeviceMuteStrategies() %s strategy %zu (curDevice %04x)",
Eric Laurentc75307b2015-03-17 15:29:32 -07004787 mute ? "muting" : "unmuting", i, curDevice);
4788 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004789 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004790 if (mute) {
4791 // FIXME: should not need to double latency if volume could be applied
4792 // immediately by the audioflinger mixer. We must account for the delay
4793 // between now and the next time the audioflinger thread for this output
4794 // will process a buffer (which corresponds to one buffer size,
4795 // usually 1/2 or 1/4 of the latency).
4796 if (muteWaitMs < desc->latency() * 2) {
4797 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004798 }
4799 }
4800 }
4801 }
4802 }
4803 }
4804
Eric Laurent99401132014-05-07 19:48:15 -07004805 // temporary mute output if device selection changes to avoid volume bursts due to
4806 // different per device volumes
4807 if (outputDesc->isActive() && (device != prevDevice)) {
Eric Laurentdc462862016-07-19 12:29:53 -07004808 uint32_t tempMuteWaitMs = outputDesc->latency() * 2;
4809 // temporary mute duration is conservatively set to 4 times the reported latency
4810 uint32_t tempMuteDurationMs = outputDesc->latency() * 4;
4811 if (muteWaitMs < tempMuteWaitMs) {
4812 muteWaitMs = tempMuteWaitMs;
Eric Laurent99401132014-05-07 19:48:15 -07004813 }
Eric Laurentdc462862016-07-19 12:29:53 -07004814
Eric Laurent99401132014-05-07 19:48:15 -07004815 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004816 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentdc462862016-07-19 12:29:53 -07004817 // make sure that we do not start the temporary mute period too early in case of
4818 // delayed device change
4819 setStrategyMute((routing_strategy)i, true, outputDesc, delayMs);
Eric Laurentc75307b2015-03-17 15:29:32 -07004820 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurentdc462862016-07-19 12:29:53 -07004821 delayMs + tempMuteDurationMs, device);
Eric Laurent99401132014-05-07 19:48:15 -07004822 }
4823 }
4824 }
4825
Eric Laurente552edb2014-03-10 17:42:56 -07004826 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4827 if (muteWaitMs > delayMs) {
4828 muteWaitMs -= delayMs;
4829 usleep(muteWaitMs * 1000);
4830 return muteWaitMs;
4831 }
4832 return 0;
4833}
4834
Eric Laurentc75307b2015-03-17 15:29:32 -07004835uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004836 audio_devices_t device,
4837 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004838 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004839 audio_patch_handle_t *patchHandle,
Andy Hung7c7124e2017-10-20 12:03:34 -07004840 const char *address,
4841 bool requiresMuteCheck)
Eric Laurente552edb2014-03-10 17:42:56 -07004842{
Eric Laurentc75307b2015-03-17 15:29:32 -07004843 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004844 AudioParameter param;
4845 uint32_t muteWaitMs;
4846
4847 if (outputDesc->isDuplicated()) {
Andy Hung7c7124e2017-10-20 12:03:34 -07004848 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs,
4849 nullptr /* patchHandle */, nullptr /* address */, requiresMuteCheck);
4850 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs,
4851 nullptr /* patchHandle */, nullptr /* address */, requiresMuteCheck);
Eric Laurente552edb2014-03-10 17:42:56 -07004852 return muteWaitMs;
4853 }
4854 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4855 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004856 if ((device != AUDIO_DEVICE_NONE) &&
Andy Hung7c7124e2017-10-20 12:03:34 -07004857 ((device & outputDesc->supportedDevices()) == AUDIO_DEVICE_NONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004858 return 0;
4859 }
4860
4861 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004862 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004863
4864 audio_devices_t prevDevice = outputDesc->mDevice;
4865
Paul McLeanaa981192015-03-21 09:55:15 -07004866 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004867
4868 if (device != AUDIO_DEVICE_NONE) {
4869 outputDesc->mDevice = device;
4870 }
Andy Hung7c7124e2017-10-20 12:03:34 -07004871
4872 // if the outputs are not materially active, there is no need to mute.
4873 if (requiresMuteCheck) {
4874 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4875 } else {
4876 ALOGV("%s: suppressing checkDeviceMuteStrategies", __func__);
4877 muteWaitMs = 0;
4878 }
Eric Laurente552edb2014-03-10 17:42:56 -07004879
4880 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004881 // the requested device is AUDIO_DEVICE_NONE
4882 // OR the requested device is the same as current device
4883 // AND force is not specified
4884 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004885 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004886 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4887 !force &&
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004888 outputDesc->getPatchHandle() != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004889 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004890 return muteWaitMs;
4891 }
4892
4893 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004894
Eric Laurente552edb2014-03-10 17:42:56 -07004895 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004896 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004897 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004898 } else {
Eric Laurentc40d9692016-04-13 19:14:13 -07004899 DeviceVector deviceList;
4900 if ((address == NULL) || (strlen(address) == 0)) {
4901 deviceList = mAvailableOutputDevices.getDevicesFromType(device);
4902 } else {
4903 deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
4904 }
4905
Eric Laurent1c333e22014-05-20 10:48:17 -07004906 if (!deviceList.isEmpty()) {
4907 struct audio_patch patch;
4908 outputDesc->toAudioPortConfig(&patch.sources[0]);
4909 patch.num_sources = 1;
4910 patch.num_sinks = 0;
4911 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4912 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004913 patch.num_sinks++;
4914 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004915 ssize_t index;
4916 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4917 index = mAudioPatches.indexOfKey(*patchHandle);
4918 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004919 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004920 }
4921 sp< AudioPatch> patchDesc;
4922 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4923 if (index >= 0) {
4924 patchDesc = mAudioPatches.valueAt(index);
4925 afPatchHandle = patchDesc->mAfPatchHandle;
4926 }
4927
Eric Laurent1c333e22014-05-20 10:48:17 -07004928 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004929 &afPatchHandle,
4930 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004931 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4932 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004933 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004934 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004935 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004936 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004937 addAudioPatch(patchDesc->mHandle, patchDesc);
4938 } else {
4939 patchDesc->mPatch = patch;
4940 }
4941 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004942 if (patchHandle) {
4943 *patchHandle = patchDesc->mHandle;
4944 }
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004945 outputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004946 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004947 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004948 }
4949 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004950
4951 // inform all input as well
4952 for (size_t i = 0; i < mInputs.size(); i++) {
4953 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004954 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004955 AudioParameter inputCmd = AudioParameter();
4956 ALOGV("%s: inform input %d of device:%d", __func__,
4957 inputDescriptor->mIoHandle, device);
4958 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4959 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4960 inputCmd.toString(),
4961 delayMs);
4962 }
4963 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004964 }
Eric Laurente552edb2014-03-10 17:42:56 -07004965
4966 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004967 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004968
4969 return muteWaitMs;
4970}
4971
Eric Laurentc75307b2015-03-17 15:29:32 -07004972status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004973 int delayMs,
4974 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004975{
Eric Laurent6a94d692014-05-20 11:18:06 -07004976 ssize_t index;
4977 if (patchHandle) {
4978 index = mAudioPatches.indexOfKey(*patchHandle);
4979 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004980 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004981 }
4982 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004983 return INVALID_OPERATION;
4984 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004985 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4986 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004987 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07004988 outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07004989 removeAudioPatch(patchDesc->mHandle);
4990 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004991 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004992 return status;
4993}
4994
4995status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4996 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004997 bool force,
4998 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004999{
5000 status_t status = NO_ERROR;
5001
Eric Laurent1f2f2232014-06-02 12:01:23 -07005002 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07005003 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
5004 inputDesc->mDevice = device;
5005
5006 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
5007 if (!deviceList.isEmpty()) {
5008 struct audio_patch patch;
5009 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07005010 // AUDIO_SOURCE_HOTWORD is for internal use only:
5011 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07005012 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
Eric Laurent599c7582015-12-07 18:05:55 -08005013 !inputDesc->isSoundTrigger()) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07005014 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
5015 }
Eric Laurent1c333e22014-05-20 10:48:17 -07005016 patch.num_sinks = 1;
5017 //only one input device for now
5018 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07005019 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07005020 ssize_t index;
5021 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
5022 index = mAudioPatches.indexOfKey(*patchHandle);
5023 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08005024 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07005025 }
5026 sp< AudioPatch> patchDesc;
5027 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
5028 if (index >= 0) {
5029 patchDesc = mAudioPatches.valueAt(index);
5030 afPatchHandle = patchDesc->mAfPatchHandle;
5031 }
5032
Eric Laurent1c333e22014-05-20 10:48:17 -07005033 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07005034 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07005035 0);
5036 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07005037 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07005038 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07005039 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01005040 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07005041 addAudioPatch(patchDesc->mHandle, patchDesc);
5042 } else {
5043 patchDesc->mPatch = patch;
5044 }
5045 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07005046 if (patchHandle) {
5047 *patchHandle = patchDesc->mHandle;
5048 }
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08005049 inputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07005050 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07005051 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07005052 }
5053 }
5054 }
5055 return status;
5056}
5057
Eric Laurent6a94d692014-05-20 11:18:06 -07005058status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
5059 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07005060{
Eric Laurent1f2f2232014-06-02 12:01:23 -07005061 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07005062 ssize_t index;
5063 if (patchHandle) {
5064 index = mAudioPatches.indexOfKey(*patchHandle);
5065 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08005066 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07005067 }
5068 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07005069 return INVALID_OPERATION;
5070 }
Eric Laurent6a94d692014-05-20 11:18:06 -07005071 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
5072 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07005073 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07005074 inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07005075 removeAudioPatch(patchDesc->mHandle);
5076 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07005077 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07005078 return status;
5079}
5080
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08005081sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07005082 const String8& address,
François Gaffie53615e22015-03-19 09:24:12 +01005083 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07005084 audio_format_t& format,
5085 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01005086 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07005087{
5088 // Choose an input profile based on the requested capture parameters: select the first available
5089 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07005090 //
5091 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
5092 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07005093
5094 for (size_t i = 0; i < mHwModules.size(); i++)
5095 {
5096 if (mHwModules[i]->mHandle == 0) {
5097 continue;
5098 }
5099 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
5100 {
Eric Laurent1c333e22014-05-20 10:48:17 -07005101 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07005102 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08005103 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07005104 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07005105 format,
5106 &format /*updatedFormat*/,
5107 channelMask,
5108 &channelMask /*updatedChannelMask*/,
5109 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08005110
Eric Laurente552edb2014-03-10 17:42:56 -07005111 return profile;
5112 }
5113 }
5114 }
5115 return NULL;
5116}
5117
Eric Laurentc73ca6e2014-12-12 14:34:22 -08005118
5119audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01005120 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07005121{
François Gaffie036e1e92015-03-19 10:16:24 +01005122 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
5123 audio_devices_t selectedDeviceFromMix =
5124 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08005125
François Gaffie036e1e92015-03-19 10:16:24 +01005126 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
5127 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08005128 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08005129 return getDeviceForInputSource(inputSource);
5130}
5131
5132audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
5133{
Eric Laurentad2e7b92017-09-14 20:06:42 -07005134 // Routing
5135 // Scan the whole RouteMap to see if we have an explicit route:
5136 // if the input source in the RouteMap is the same as the argument above,
5137 // and activity count is non-zero and the device in the route descriptor is available
5138 // then select this device.
Paul McLean466dc8e2015-04-17 13:15:36 -06005139 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
5140 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurentad2e7b92017-09-14 20:06:42 -07005141 if ((inputSource == route->mSource) && route->isActive() &&
5142 (mAvailableInputDevices.indexOf(route->mDeviceDescriptor) >= 0)) {
Paul McLean466dc8e2015-04-17 13:15:36 -06005143 return route->mDeviceDescriptor->type();
5144 }
5145 }
5146
5147 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07005148}
5149
Eric Laurente0720872014-03-11 09:30:41 -07005150float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005151 int index,
5152 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005153{
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005154 float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index);
Jean-Michel Trivi3d8b4a42016-09-14 18:37:46 -07005155
5156 // handle the case of accessibility active while a ringtone is playing: if the ringtone is much
5157 // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch
5158 // exploration of the dialer UI. In this situation, bring the accessibility volume closer to
5159 // the ringtone volume
5160 if ((stream == AUDIO_STREAM_ACCESSIBILITY)
5161 && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState())
5162 && isStreamActive(AUDIO_STREAM_RING, 0)) {
5163 const float ringVolumeDB = computeVolume(AUDIO_STREAM_RING, index, device);
5164 return ringVolumeDB - 4 > volumeDB ? ringVolumeDB - 4 : volumeDB;
5165 }
5166
Jean-Michel Trivi719a9872017-08-05 13:51:35 -07005167 // in-call: always cap earpiece volume by voice volume + some low headroom
5168 if ((stream != AUDIO_STREAM_VOICE_CALL) && (device & AUDIO_DEVICE_OUT_EARPIECE) && isInCall()) {
5169 switch (stream) {
5170 case AUDIO_STREAM_SYSTEM:
5171 case AUDIO_STREAM_RING:
5172 case AUDIO_STREAM_MUSIC:
5173 case AUDIO_STREAM_ALARM:
5174 case AUDIO_STREAM_NOTIFICATION:
5175 case AUDIO_STREAM_ENFORCED_AUDIBLE:
5176 case AUDIO_STREAM_DTMF:
5177 case AUDIO_STREAM_ACCESSIBILITY: {
5178 const float maxVoiceVolDb = computeVolume(AUDIO_STREAM_VOICE_CALL, index, device)
5179 + IN_CALL_EARPIECE_HEADROOM_DB;
5180 if (volumeDB > maxVoiceVolDb) {
5181 ALOGV("computeVolume() stream %d at vol=%f overriden by stream %d at vol=%f",
5182 stream, volumeDB, AUDIO_STREAM_VOICE_CALL, maxVoiceVolDb);
5183 volumeDB = maxVoiceVolDb;
5184 }
5185 } break;
5186 default:
5187 break;
5188 }
5189 }
5190
Eric Laurente552edb2014-03-10 17:42:56 -07005191 // if a headset is connected, apply the following rules to ring tones and notifications
5192 // to avoid sound level bursts in user's ears:
Eric Laurent6af1c1d2016-04-14 11:20:44 -07005193 // - always attenuate notifications volume by 6dB
5194 // - attenuate ring tones volume by 6dB unless music is not playing and
5195 // speaker is part of the select devices
Eric Laurente552edb2014-03-10 17:42:56 -07005196 // - if music is playing, always limit the volume to current music volume,
5197 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07005198 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005199 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
5200 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
5201 AUDIO_DEVICE_OUT_WIRED_HEADSET |
Eric Laurent904d6322017-03-17 17:20:47 -07005202 AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
5203 AUDIO_DEVICE_OUT_USB_HEADSET)) &&
Eric Laurente552edb2014-03-10 17:42:56 -07005204 ((stream_strategy == STRATEGY_SONIFICATION)
5205 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07005206 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07005207 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01005208 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
François Gaffied1ab2bd2015-12-02 18:20:06 +01005209 mVolumeCurves->canBeMuted(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005210 // when the phone is ringing we must consider that music could have been paused just before
5211 // by the music application and behave as if music was active if the last music track was
5212 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07005213 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07005214 mLimitRingtoneVolume) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005215 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07005216 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07005217 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005218 mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC,
5219 musicDevice),
5220 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07005221 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
5222 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005223 if (volumeDB > minVolDB) {
5224 volumeDB = minVolDB;
Eric Laurentffbc80f2015-03-18 18:30:19 -07005225 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07005226 }
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005227 if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
5228 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) {
5229 // on A2DP, also ensure notification volume is not too low compared to media when
5230 // intended to be played
5231 if ((volumeDB > -96.0f) &&
5232 (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) {
5233 ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f",
5234 stream, device,
5235 volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB);
5236 volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB;
5237 }
5238 }
Eric Laurent6af1c1d2016-04-14 11:20:44 -07005239 } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) ||
5240 stream_strategy != STRATEGY_SONIFICATION) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005241 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07005242 }
5243 }
5244
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005245 return volumeDB;
Eric Laurente552edb2014-03-10 17:42:56 -07005246}
5247
Eric Laurente0720872014-03-11 09:30:41 -07005248status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005249 int index,
5250 const sp<AudioOutputDescriptor>& outputDesc,
5251 audio_devices_t device,
5252 int delayMs,
5253 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005254{
Eric Laurente552edb2014-03-10 17:42:56 -07005255 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07005256 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07005257 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07005258 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07005259 return NO_ERROR;
5260 }
François Gaffie2110e042015-03-24 08:41:51 +01005261 audio_policy_forced_cfg_t forceUseForComm =
5262 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07005263 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01005264 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
5265 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005266 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01005267 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07005268 return INVALID_OPERATION;
5269 }
5270
Eric Laurentc75307b2015-03-17 15:29:32 -07005271 if (device == AUDIO_DEVICE_NONE) {
5272 device = outputDesc->device();
5273 }
Eric Laurent275e8e92014-11-30 15:14:47 -08005274
Eric Laurentffbc80f2015-03-18 18:30:19 -07005275 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07005276 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07005277 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08005278 }
Eric Laurentc75307b2015-03-17 15:29:32 -07005279
Eric Laurentffbc80f2015-03-18 18:30:19 -07005280 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07005281
Eric Laurent3b73df72014-03-11 09:06:29 -07005282 if (stream == AUDIO_STREAM_VOICE_CALL ||
5283 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07005284 float voiceVolume;
5285 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07005286 if (stream == AUDIO_STREAM_VOICE_CALL) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005287 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005288 } else {
5289 voiceVolume = 1.0;
5290 }
5291
Eric Laurent18fba842016-03-31 14:41:26 -07005292 if (voiceVolume != mLastVoiceVolume) {
Eric Laurente552edb2014-03-10 17:42:56 -07005293 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
5294 mLastVoiceVolume = voiceVolume;
5295 }
5296 }
5297
5298 return NO_ERROR;
5299}
5300
Eric Laurentc75307b2015-03-17 15:29:32 -07005301void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
5302 audio_devices_t device,
5303 int delayMs,
5304 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005305{
Eric Laurentc75307b2015-03-17 15:29:32 -07005306 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07005307
Eric Laurent794fde22016-03-11 09:50:45 -08005308 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005309 checkAndSetVolume((audio_stream_type_t)stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005310 mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005311 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005312 device,
5313 delayMs,
5314 force);
5315 }
5316}
5317
Eric Laurente0720872014-03-11 09:30:41 -07005318void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07005319 bool on,
5320 const sp<AudioOutputDescriptor>& outputDesc,
5321 int delayMs,
5322 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005323{
Eric Laurent72249d52015-06-08 11:12:15 -07005324 ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d",
5325 strategy, on, outputDesc->getId());
Eric Laurent794fde22016-03-11 09:50:45 -08005326 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005327 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005328 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07005329 }
5330 }
5331}
5332
Eric Laurente0720872014-03-11 09:30:41 -07005333void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005334 bool on,
5335 const sp<AudioOutputDescriptor>& outputDesc,
5336 int delayMs,
5337 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005338{
Eric Laurente552edb2014-03-10 17:42:56 -07005339 if (device == AUDIO_DEVICE_NONE) {
5340 device = outputDesc->device();
5341 }
5342
Eric Laurentc75307b2015-03-17 15:29:32 -07005343 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
5344 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07005345
5346 if (on) {
5347 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005348 if (mVolumeCurves->canBeMuted(stream) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07005349 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01005350 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005351 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005352 }
5353 }
5354 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
5355 outputDesc->mMuteCount[stream]++;
5356 } else {
5357 if (outputDesc->mMuteCount[stream] == 0) {
5358 ALOGV("setStreamMute() unmuting non muted stream!");
5359 return;
5360 }
5361 if (--outputDesc->mMuteCount[stream] == 0) {
5362 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005363 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005364 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005365 device,
5366 delayMs);
5367 }
5368 }
5369}
5370
Eric Laurente0720872014-03-11 09:30:41 -07005371void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07005372 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07005373{
Eric Laurent87ffa392015-05-22 10:32:38 -07005374 if(!hasPrimaryOutput()) {
5375 return;
5376 }
5377
Eric Laurente552edb2014-03-10 17:42:56 -07005378 // if the stream pertains to sonification strategy and we are in call we must
5379 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
5380 // in the device used for phone strategy and play the tone if the selected device does not
5381 // interfere with the device used for phone strategy
5382 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
5383 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07005384 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005385 if ((stream_strategy == STRATEGY_SONIFICATION) ||
5386 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005387 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07005388 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
5389 stream, starting, outputDesc->mDevice, stateChange);
5390 if (outputDesc->mRefCount[stream]) {
5391 int muteCount = 1;
5392 if (stateChange) {
5393 muteCount = outputDesc->mRefCount[stream];
5394 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005395 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005396 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
5397 for (int i = 0; i < muteCount; i++) {
5398 setStreamMute(stream, starting, mPrimaryOutput);
5399 }
5400 } else {
5401 ALOGV("handleIncallSonification() high visibility");
5402 if (outputDesc->device() &
5403 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
5404 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
5405 for (int i = 0; i < muteCount; i++) {
5406 setStreamMute(stream, starting, mPrimaryOutput);
5407 }
5408 }
5409 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005410 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
5411 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07005412 } else {
5413 mpClientInterface->stopTone();
5414 }
5415 }
5416 }
5417 }
5418}
5419
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005420audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
5421{
5422 // flags to stream type mapping
5423 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
5424 return AUDIO_STREAM_ENFORCED_AUDIBLE;
5425 }
5426 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
5427 return AUDIO_STREAM_BLUETOOTH_SCO;
5428 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08005429 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
5430 return AUDIO_STREAM_TTS;
5431 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005432
5433 // usage to stream type mapping
5434 switch (attr->usage) {
5435 case AUDIO_USAGE_MEDIA:
5436 case AUDIO_USAGE_GAME:
Jean-Michel Trivi36867762016-12-29 12:03:28 -08005437 case AUDIO_USAGE_ASSISTANT:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005438 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5439 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08005440 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5441 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005442 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5443 return AUDIO_STREAM_SYSTEM;
5444 case AUDIO_USAGE_VOICE_COMMUNICATION:
5445 return AUDIO_STREAM_VOICE_CALL;
5446
5447 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5448 return AUDIO_STREAM_DTMF;
5449
5450 case AUDIO_USAGE_ALARM:
5451 return AUDIO_STREAM_ALARM;
5452 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5453 return AUDIO_STREAM_RING;
5454
5455 case AUDIO_USAGE_NOTIFICATION:
5456 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5457 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5458 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5459 case AUDIO_USAGE_NOTIFICATION_EVENT:
5460 return AUDIO_STREAM_NOTIFICATION;
5461
5462 case AUDIO_USAGE_UNKNOWN:
5463 default:
5464 return AUDIO_STREAM_MUSIC;
5465 }
5466}
Eric Laurente83b55d2014-11-14 10:06:21 -08005467
François Gaffie53615e22015-03-19 09:24:12 +01005468bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
5469{
Eric Laurente83b55d2014-11-14 10:06:21 -08005470 // has flags that map to a strategy?
5471 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
5472 return true;
5473 }
5474
5475 // has known usage?
5476 switch (paa->usage) {
5477 case AUDIO_USAGE_UNKNOWN:
5478 case AUDIO_USAGE_MEDIA:
5479 case AUDIO_USAGE_VOICE_COMMUNICATION:
5480 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5481 case AUDIO_USAGE_ALARM:
5482 case AUDIO_USAGE_NOTIFICATION:
5483 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5484 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5485 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5486 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5487 case AUDIO_USAGE_NOTIFICATION_EVENT:
5488 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5489 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5490 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5491 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08005492 case AUDIO_USAGE_VIRTUAL_SOURCE:
Jean-Michel Trivi36867762016-12-29 12:03:28 -08005493 case AUDIO_USAGE_ASSISTANT:
Eric Laurente83b55d2014-11-14 10:06:21 -08005494 break;
5495 default:
5496 return false;
5497 }
5498 return true;
5499}
5500
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07005501bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor>& outputDesc,
François Gaffiead3183e2015-03-18 16:55:35 +01005502 routing_strategy strategy, uint32_t inPastMs,
5503 nsecs_t sysTime) const
5504{
5505 if ((sysTime == 0) && (inPastMs != 0)) {
5506 sysTime = systemTime();
5507 }
Eric Laurent794fde22016-03-11 09:50:45 -08005508 for (int i = 0; i < (int)AUDIO_STREAM_FOR_POLICY_CNT; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01005509 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
5510 (NUM_STRATEGIES == strategy)) &&
5511 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
5512 return true;
5513 }
5514 }
5515 return false;
5516}
5517
François Gaffie2110e042015-03-24 08:41:51 +01005518audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
5519{
5520 return mEngine->getForceUse(usage);
5521}
5522
5523bool AudioPolicyManager::isInCall()
5524{
5525 return isStateInCall(mEngine->getPhoneState());
5526}
5527
5528bool AudioPolicyManager::isStateInCall(int state)
5529{
5530 return is_state_in_call(state);
5531}
5532
Eric Laurentd60560a2015-04-10 11:31:20 -07005533void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc)
5534{
5535 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
5536 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
5537 if (sourceDesc->mDevice->equals(deviceDesc)) {
5538 ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle());
5539 stopAudioSource(sourceDesc->getHandle());
5540 }
5541 }
5542
5543 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
5544 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
5545 bool release = false;
5546 for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) {
5547 const struct audio_port_config *source = &patchDesc->mPatch.sources[j];
5548 if (source->type == AUDIO_PORT_TYPE_DEVICE &&
5549 source->ext.device.type == deviceDesc->type()) {
5550 release = true;
5551 }
5552 }
5553 for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) {
5554 const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j];
5555 if (sink->type == AUDIO_PORT_TYPE_DEVICE &&
5556 sink->ext.device.type == deviceDesc->type()) {
5557 release = true;
5558 }
5559 }
5560 if (release) {
5561 ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle);
5562 releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid);
5563 }
5564 }
5565}
5566
Phil Burk09bc4612016-02-24 15:58:15 -08005567// Modify the list of surround sound formats supported.
Phil Burk0709b0a2016-03-31 12:54:57 -07005568void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) {
5569 FormatVector &formats = *formatsPtr;
Phil Burk07ac1142016-03-25 13:39:29 -07005570 // TODO Set this based on Config properties.
5571 const bool alwaysForceAC3 = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005572
5573 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5574 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
Phil Burk0709b0a2016-03-31 12:54:57 -07005575 ALOGD("%s: forced use = %d", __FUNCTION__, forceUse);
Phil Burk09bc4612016-02-24 15:58:15 -08005576
5577 // Analyze original support for various formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005578 bool supportsAC3 = false;
5579 bool supportsOtherSurround = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005580 bool supportsIEC61937 = false;
5581 for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) {
5582 audio_format_t format = formats[formatIndex];
Phil Burk09bc4612016-02-24 15:58:15 -08005583 switch (format) {
5584 case AUDIO_FORMAT_AC3:
Phil Burk07ac1142016-03-25 13:39:29 -07005585 supportsAC3 = true;
5586 break;
Phil Burk09bc4612016-02-24 15:58:15 -08005587 case AUDIO_FORMAT_E_AC3:
5588 case AUDIO_FORMAT_DTS:
5589 case AUDIO_FORMAT_DTS_HD:
Phil Burk07ac1142016-03-25 13:39:29 -07005590 supportsOtherSurround = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005591 break;
5592 case AUDIO_FORMAT_IEC61937:
5593 supportsIEC61937 = true;
5594 break;
5595 default:
5596 break;
5597 }
5598 }
Phil Burk09bc4612016-02-24 15:58:15 -08005599
5600 // Modify formats based on surround preferences.
5601 // If NEVER, remove support for surround formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005602 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5603 if (supportsAC3 || supportsOtherSurround || supportsIEC61937) {
5604 // Remove surround sound related formats.
5605 for (size_t formatIndex = 0; formatIndex < formats.size(); ) {
5606 audio_format_t format = formats[formatIndex];
5607 switch(format) {
5608 case AUDIO_FORMAT_AC3:
5609 case AUDIO_FORMAT_E_AC3:
5610 case AUDIO_FORMAT_DTS:
5611 case AUDIO_FORMAT_DTS_HD:
5612 case AUDIO_FORMAT_IEC61937:
Phil Burk07ac1142016-03-25 13:39:29 -07005613 formats.removeAt(formatIndex);
5614 break;
5615 default:
5616 formatIndex++; // keep it
5617 break;
5618 }
Phil Burk09bc4612016-02-24 15:58:15 -08005619 }
Phil Burk07ac1142016-03-25 13:39:29 -07005620 supportsAC3 = false;
5621 supportsOtherSurround = false;
5622 supportsIEC61937 = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005623 }
Phil Burk07ac1142016-03-25 13:39:29 -07005624 } else { // AUTO or ALWAYS
5625 // Most TVs support AC3 even if they do not report it in the EDID.
5626 if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS))
5627 && !supportsAC3) {
5628 formats.add(AUDIO_FORMAT_AC3);
5629 supportsAC3 = true;
5630 }
5631
5632 // If ALWAYS, add support for raw surround formats if all are missing.
5633 // This assumes that if any of these formats are reported by the HAL
5634 // then the report is valid and should not be modified.
5635 if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)
5636 && !supportsOtherSurround) {
5637 formats.add(AUDIO_FORMAT_E_AC3);
5638 formats.add(AUDIO_FORMAT_DTS);
5639 formats.add(AUDIO_FORMAT_DTS_HD);
5640 supportsOtherSurround = true;
5641 }
5642
5643 // Add support for IEC61937 if any raw surround supported.
5644 // The HAL could do this but add it here, just in case.
5645 if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) {
5646 formats.add(AUDIO_FORMAT_IEC61937);
5647 supportsIEC61937 = true;
5648 }
Phil Burk09bc4612016-02-24 15:58:15 -08005649 }
Phil Burk0709b0a2016-03-31 12:54:57 -07005650}
5651
5652// Modify the list of channel masks supported.
5653void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) {
5654 ChannelsVector &channelMasks = *channelMasksPtr;
5655 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5656 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
5657
5658 // If NEVER, then remove support for channelMasks > stereo.
5659 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5660 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) {
5661 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5662 if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) {
5663 ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask);
5664 channelMasks.removeAt(maskIndex);
5665 } else {
5666 maskIndex++;
5667 }
5668 }
5669 // If ALWAYS, then make sure we at least support 5.1
5670 } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) {
5671 bool supports5dot1 = false;
5672 // Are there any channel masks that can be considered "surround"?
5673 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); maskIndex++) {
5674 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5675 if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) {
5676 supports5dot1 = true;
5677 break;
5678 }
5679 }
5680 // If not then add 5.1 support.
5681 if (!supports5dot1) {
5682 channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1);
5683 ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__);
5684 }
Phil Burk09bc4612016-02-24 15:58:15 -08005685 }
5686}
5687
Phil Burk00eeb322016-03-31 12:41:00 -07005688void AudioPolicyManager::updateAudioProfiles(audio_devices_t device,
5689 audio_io_handle_t ioHandle,
François Gaffie112b0af2015-11-19 16:13:25 +01005690 AudioProfileVector &profiles)
5691{
5692 String8 reply;
Phil Burk0709b0a2016-03-31 12:54:57 -07005693
François Gaffie112b0af2015-11-19 16:13:25 +01005694 // Format MUST be checked first to update the list of AudioProfile
5695 if (profiles.hasDynamicFormat()) {
Mikhail Naganov388360c2016-10-17 17:09:41 -07005696 reply = mpClientInterface->getParameters(
5697 ioHandle, String8(AudioParameter::keyStreamSupportedFormats));
Phil Burk0709b0a2016-03-31 12:54:57 -07005698 ALOGV("%s: supported formats %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005699 AudioParameter repliedParameters(reply);
5700 if (repliedParameters.get(
Mikhail Naganov388360c2016-10-17 17:09:41 -07005701 String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) {
François Gaffie112b0af2015-11-19 16:13:25 +01005702 ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__);
5703 return;
5704 }
Phil Burk09bc4612016-02-24 15:58:15 -08005705 FormatVector formats = formatsFromString(reply.string());
Phil Burk00eeb322016-03-31 12:41:00 -07005706 if (device == AUDIO_DEVICE_OUT_HDMI) {
Phil Burk0709b0a2016-03-31 12:54:57 -07005707 filterSurroundFormats(&formats);
Phil Burk00eeb322016-03-31 12:41:00 -07005708 }
Phil Burk09bc4612016-02-24 15:58:15 -08005709 profiles.setFormats(formats);
François Gaffie112b0af2015-11-19 16:13:25 +01005710 }
5711 const FormatVector &supportedFormats = profiles.getSupportedFormats();
5712
Eric Laurent20eb3a42016-01-26 18:39:17 -08005713 for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) {
François Gaffie112b0af2015-11-19 16:13:25 +01005714 audio_format_t format = supportedFormats[formatIndex];
Eric Laurent20eb3a42016-01-26 18:39:17 -08005715 ChannelsVector channelMasks;
5716 SampleRateVector samplingRates;
François Gaffie112b0af2015-11-19 16:13:25 +01005717 AudioParameter requestedParameters;
Mikhail Naganov388360c2016-10-17 17:09:41 -07005718 requestedParameters.addInt(String8(AudioParameter::keyFormat), format);
François Gaffie112b0af2015-11-19 16:13:25 +01005719
5720 if (profiles.hasDynamicRateFor(format)) {
Mikhail Naganov388360c2016-10-17 17:09:41 -07005721 reply = mpClientInterface->getParameters(
5722 ioHandle,
5723 requestedParameters.toString() + ";" +
5724 AudioParameter::keyStreamSupportedSamplingRates);
François Gaffie112b0af2015-11-19 16:13:25 +01005725 ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005726 AudioParameter repliedParameters(reply);
5727 if (repliedParameters.get(
Mikhail Naganov388360c2016-10-17 17:09:41 -07005728 String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) {
Eric Laurent62e4bc52016-02-02 18:37:28 -08005729 samplingRates = samplingRatesFromString(reply.string());
François Gaffie112b0af2015-11-19 16:13:25 +01005730 }
5731 }
5732 if (profiles.hasDynamicChannelsFor(format)) {
5733 reply = mpClientInterface->getParameters(ioHandle,
5734 requestedParameters.toString() + ";" +
Mikhail Naganov388360c2016-10-17 17:09:41 -07005735 AudioParameter::keyStreamSupportedChannels);
François Gaffie112b0af2015-11-19 16:13:25 +01005736 ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005737 AudioParameter repliedParameters(reply);
5738 if (repliedParameters.get(
Mikhail Naganov388360c2016-10-17 17:09:41 -07005739 String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) {
Eric Laurent62e4bc52016-02-02 18:37:28 -08005740 channelMasks = channelMasksFromString(reply.string());
Phil Burk0709b0a2016-03-31 12:54:57 -07005741 if (device == AUDIO_DEVICE_OUT_HDMI) {
5742 filterSurroundChannelMasks(&channelMasks);
5743 }
François Gaffie112b0af2015-11-19 16:13:25 +01005744 }
5745 }
Eric Laurent20eb3a42016-01-26 18:39:17 -08005746 profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates));
François Gaffie112b0af2015-11-19 16:13:25 +01005747 }
5748}
Eric Laurentd60560a2015-04-10 11:31:20 -07005749
Eric Laurente552edb2014-03-10 17:42:56 -07005750}; // namespace android