blob: 89e5d77d5772ba3a27b92e2075e5ad975673c2b0 [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 Laurente0720872014-03-11 09:30:41 -0700754audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +0100755 uint32_t samplingRate,
756 audio_format_t format,
757 audio_channel_mask_t channelMask,
758 audio_output_flags_t flags,
759 const audio_offload_info_t *offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -0700760{
Eric Laurent3b73df72014-03-11 09:06:29 -0700761 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700762 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
763 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
764 device, stream, samplingRate, format, channelMask, flags);
765
Kevin Rocard169753c2017-03-06 14:18:23 -0800766 return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE, stream, samplingRate, format,
767 channelMask, flags, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700768}
769
Eric Laurente83b55d2014-11-14 10:06:21 -0800770status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
771 audio_io_handle_t *output,
772 audio_session_t session,
773 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700774 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800775 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800776 audio_output_flags_t flags,
Eric Laurent2ac76942017-06-22 17:17:09 -0700777 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800778 audio_port_handle_t *portId)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700779{
Eric Laurente83b55d2014-11-14 10:06:21 -0800780 audio_attributes_t attributes;
781 if (attr != NULL) {
782 if (!isValidAttributes(attr)) {
783 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
784 attr->usage, attr->content_type, attr->flags,
785 attr->tags);
786 return BAD_VALUE;
787 }
788 attributes = *attr;
789 } else {
790 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
791 ALOGE("getOutputForAttr(): invalid stream type");
792 return BAD_VALUE;
793 }
794 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700795 }
Eric Laurent20b9ef02016-12-05 11:03:16 -0800796
797 // TODO: check for existing client for this port ID
798 if (*portId == AUDIO_PORT_HANDLE_NONE) {
799 *portId = AudioPort::getNextUniqueId();
800 }
801
Eric Laurentc75307b2015-03-17 15:29:32 -0700802 sp<SwAudioOutputDescriptor> desc;
Jean-Michel Trivie8deced2016-02-11 12:50:39 -0800803 if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) {
François Gaffie036e1e92015-03-19 10:16:24 +0100804 ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr");
Eric Laurent20b9ef02016-12-05 11:03:16 -0800805 if (!audio_has_proportional_frames(config->format)) {
François Gaffie036e1e92015-03-19 10:16:24 +0100806 return BAD_VALUE;
Eric Laurent275e8e92014-11-30 15:14:47 -0800807 }
François Gaffie036e1e92015-03-19 10:16:24 +0100808 *stream = streamTypefromAttributesInt(&attributes);
809 *output = desc->mIoHandle;
810 ALOGV("getOutputForAttr() returns output %d", *output);
811 return NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -0800812 }
813 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
814 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
815 return BAD_VALUE;
816 }
817
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700818 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x"
819 " session %d selectedDeviceId %d",
820 attributes.usage, attributes.content_type, attributes.tags, attributes.flags,
Eric Laurent2ac76942017-06-22 17:17:09 -0700821 session, *selectedDeviceId);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700822
823 *stream = streamTypefromAttributesInt(&attributes);
824
825 // Explicit routing?
826 sp<DeviceDescriptor> deviceDesc;
Eric Laurent2ac76942017-06-22 17:17:09 -0700827 if (*selectedDeviceId != AUDIO_PORT_HANDLE_NONE) {
828 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
829 if (mAvailableOutputDevices[i]->getId() == *selectedDeviceId) {
830 deviceDesc = mAvailableOutputDevices[i];
831 break;
832 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700833 }
834 }
835 mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700836
Eric Laurente83b55d2014-11-14 10:06:21 -0800837 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700838 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700839
Eric Laurente83b55d2014-11-14 10:06:21 -0800840 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700841 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
842 }
843
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700844 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Eric Laurent20b9ef02016-12-05 11:03:16 -0800845 device, config->sample_rate, config->format, config->channel_mask, flags);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700846
Kevin Rocard169753c2017-03-06 14:18:23 -0800847 *output = getOutputForDevice(device, session, *stream,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800848 config->sample_rate, config->format, config->channel_mask,
849 flags, &config->offload_info);
Eric Laurente83b55d2014-11-14 10:06:21 -0800850 if (*output == AUDIO_IO_HANDLE_NONE) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700851 mOutputRoutes.removeRoute(session);
Eric Laurente83b55d2014-11-14 10:06:21 -0800852 return INVALID_OPERATION;
853 }
Paul McLeanaa981192015-03-21 09:55:15 -0700854
Eric Laurent2ac76942017-06-22 17:17:09 -0700855 DeviceVector outputDevices = mAvailableOutputDevices.getDevicesFromType(device);
856 *selectedDeviceId = outputDevices.size() > 0 ? outputDevices.itemAt(0)->getId()
857 : AUDIO_PORT_HANDLE_NONE;
858
859 ALOGV(" getOutputForAttr() returns output %d selectedDeviceId %d", *output, *selectedDeviceId);
860
Eric Laurente83b55d2014-11-14 10:06:21 -0800861 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700862}
863
864audio_io_handle_t AudioPolicyManager::getOutputForDevice(
865 audio_devices_t device,
Kevin Rocard169753c2017-03-06 14:18:23 -0800866 audio_session_t session,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700867 audio_stream_type_t stream,
868 uint32_t samplingRate,
869 audio_format_t format,
870 audio_channel_mask_t channelMask,
871 audio_output_flags_t flags,
872 const audio_offload_info_t *offloadInfo)
873{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700874 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700875 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700876
Eric Laurente552edb2014-03-10 17:42:56 -0700877 // open a direct output if required by specified parameters
878 //force direct flag if offload flag is set: offloading implies a direct output stream
879 // and all common behaviors are driven by checking only the direct flag
880 // this should normally be set appropriately in the policy configuration file
881 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700882 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700883 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700884 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
885 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
886 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800887 // only allow deep buffering for music stream type
888 if (stream != AUDIO_STREAM_MUSIC) {
889 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Ravi Kumar Alamanda439e4ed2015-04-03 12:13:21 -0700890 } else if (/* stream == AUDIO_STREAM_MUSIC && */
891 flags == AUDIO_OUTPUT_FLAG_NONE &&
892 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
893 // use DEEP_BUFFER as default output for music stream type
894 flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Eric Laurente83b55d2014-11-14 10:06:21 -0800895 }
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700896 if (stream == AUDIO_STREAM_TTS) {
897 flags = AUDIO_OUTPUT_FLAG_TTS;
Haynes Mathew George84c621e2017-04-25 11:41:50 -0700898 } else if (stream == AUDIO_STREAM_VOICE_CALL &&
Haynes Mathew George84c621e2017-04-25 11:41:50 -0700899 audio_is_linear_pcm(format)) {
900 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX |
901 AUDIO_OUTPUT_FLAG_DIRECT);
902 ALOGV("Set VoIP and Direct output flags for PCM format");
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700903 }
Eric Laurente552edb2014-03-10 17:42:56 -0700904
Eric Laurentb732cf52014-09-24 19:08:21 -0700905 sp<IOProfile> profile;
906
907 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700908 // and not explicitly requested
909 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
Glenn Kasten05ddca52016-02-11 08:17:12 -0800910 audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700911 audio_channel_count_from_out_mask(channelMask) <= 2) {
912 goto non_direct_output;
913 }
914
Andy Hung2ddee192015-12-18 17:34:44 -0800915 // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled.
916 // This prevents creating an offloaded track and tearing it down immediately after start
917 // when audioflinger detects there is an active non offloadable effect.
Eric Laurente552edb2014-03-10 17:42:56 -0700918 // FIXME: We should check the audio session here but we do not have it in this context.
919 // This may prevent offloading in rare situations where effects are left active by apps
920 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700921
Eric Laurente552edb2014-03-10 17:42:56 -0700922 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
Andy Hung2ddee192015-12-18 17:34:44 -0800923 !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700924 profile = getProfileForDirectOutput(device,
925 samplingRate,
926 format,
927 channelMask,
928 (audio_output_flags_t)flags);
929 }
930
Eric Laurent1c333e22014-05-20 10:48:17 -0700931 if (profile != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700932 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700933
934 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700935 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700936 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
937 outputDesc = desc;
Kevin Rocard551061a2017-02-06 15:59:29 -0800938 // reuse direct output if currently open by the same client
939 // and configured with same parameters
Eric Laurente552edb2014-03-10 17:42:56 -0700940 if ((samplingRate == outputDesc->mSamplingRate) &&
Kevin Rocard551061a2017-02-06 15:59:29 -0800941 audio_formats_match(format, outputDesc->mFormat) &&
942 (channelMask == outputDesc->mChannelMask)) {
Kevin Rocard169753c2017-03-06 14:18:23 -0800943 if (session == outputDesc->mDirectClientSession) {
Kevin Rocard551061a2017-02-06 15:59:29 -0800944 outputDesc->mDirectOpenCount++;
Kevin Rocard169753c2017-03-06 14:18:23 -0800945 ALOGV("getOutput() reusing direct output %d for session %d",
946 mOutputs.keyAt(i), session);
Kevin Rocard551061a2017-02-06 15:59:29 -0800947 return mOutputs.keyAt(i);
948 } else {
Kevin Rocard169753c2017-03-06 14:18:23 -0800949 ALOGV("getOutput() do not reuse direct output because current client (%d) "
950 "is not the same as requesting client (%d)",
951 outputDesc->mDirectClientSession, session);
Kevin Rocard551061a2017-02-06 15:59:29 -0800952 goto non_direct_output;
953 }
Eric Laurente552edb2014-03-10 17:42:56 -0700954 }
955 }
956 }
957 // close direct output if currently open and configured with different parameters
958 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700959 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700960 }
Eric Laurent861a6282015-05-18 15:40:16 -0700961
962 // if the selected profile is offloaded and no offload info was specified,
963 // create a default one
964 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100965 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
Eric Laurent861a6282015-05-18 15:40:16 -0700966 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
967 defaultOffloadInfo.sample_rate = samplingRate;
968 defaultOffloadInfo.channel_mask = channelMask;
969 defaultOffloadInfo.format = format;
970 defaultOffloadInfo.stream_type = stream;
971 defaultOffloadInfo.bit_rate = 0;
972 defaultOffloadInfo.duration_us = -1;
973 defaultOffloadInfo.has_video = true; // conservative
974 defaultOffloadInfo.is_streaming = true; // likely
975 offloadInfo = &defaultOffloadInfo;
976 }
977
Eric Laurentc75307b2015-03-17 15:29:32 -0700978 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700979 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -0700980 outputDesc->mLatency = 0;
Eric Laurent861a6282015-05-18 15:40:16 -0700981 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700982 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
983 config.sample_rate = samplingRate;
984 config.channel_mask = channelMask;
985 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -0700986 if (offloadInfo != NULL) {
987 config.offload_info = *offloadInfo;
988 }
Eric Laurente3014102017-05-03 11:15:43 -0700989 DeviceVector outputDevices = mAvailableOutputDevices.getDevicesFromType(device);
990 String8 address = outputDevices.size() > 0 ? outputDevices.itemAt(0)->mAddress
991 : String8("");
Eric Laurent322b4d22015-04-03 15:57:54 -0700992 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -0700993 &output,
994 &config,
995 &outputDesc->mDevice,
Eric Laurente3014102017-05-03 11:15:43 -0700996 address,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700997 &outputDesc->mLatency,
998 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -0700999
1000 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -07001001 if (status != NO_ERROR ||
1002 (samplingRate != 0 && samplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -08001003 (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) ||
Eric Laurentcf2c0212014-07-25 16:20:43 -07001004 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -07001005 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1006 "format %d %d, channelMask %04x %04x", output, samplingRate,
1007 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1008 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001009 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07001010 mpClientInterface->closeOutput(output);
1011 }
Eric Laurenta82797f2015-01-30 11:49:43 -08001012 // fall back to mixer output if possible when the direct output could not be open
Glenn Kasten05ddca52016-02-11 08:17:12 -08001013 if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) {
Eric Laurenta82797f2015-01-30 11:49:43 -08001014 goto non_direct_output;
1015 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001016 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001017 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001018 outputDesc->mSamplingRate = config.sample_rate;
1019 outputDesc->mChannelMask = config.channel_mask;
1020 outputDesc->mFormat = config.format;
1021 outputDesc->mRefCount[stream] = 0;
1022 outputDesc->mStopTime[stream] = 0;
1023 outputDesc->mDirectOpenCount = 1;
Kevin Rocard169753c2017-03-06 14:18:23 -08001024 outputDesc->mDirectClientSession = session;
1025
Eric Laurente552edb2014-03-10 17:42:56 -07001026 addOutput(output, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07001027 mPreviousOutputs = mOutputs;
1028 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -07001029 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001030 return output;
1031 }
1032
Eric Laurentb732cf52014-09-24 19:08:21 -07001033non_direct_output:
Eric Laurent14cbfca2016-03-17 09:42:16 -07001034
1035 // A request for HW A/V sync cannot fallback to a mixed output because time
1036 // stamps are embedded in audio data
1037 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1038 return AUDIO_IO_HANDLE_NONE;
1039 }
1040
Eric Laurente552edb2014-03-10 17:42:56 -07001041 // ignoring channel mask due to downmix capability in mixer
1042
1043 // open a non direct output
1044
1045 // for non direct outputs, only PCM is supported
1046 if (audio_is_linear_pcm(format)) {
1047 // get which output is suitable for the specified stream. The actual
1048 // routing change will happen when startOutput() will be called
1049 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1050
Eric Laurent8838a382014-09-08 16:44:28 -07001051 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1052 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1053 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -07001054 }
Glenn Kastenfaa10a62017-05-25 15:52:05 -07001055 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d, "
Eric Laurente552edb2014-03-10 17:42:56 -07001056 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1057
Eric Laurente552edb2014-03-10 17:42:56 -07001058 return output;
1059}
1060
Eric Laurente0720872014-03-11 09:30:41 -07001061audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -07001062 audio_output_flags_t flags,
1063 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -07001064{
1065 // select one output among several that provide a path to a particular device or set of
1066 // devices (the list was previously build by getOutputsForDevice()).
1067 // The priority is as follows:
1068 // 1: the output with the highest number of requested policy flags
Eric Laurente6930022016-02-11 10:20:40 -08001069 // 2: the output with the bit depth the closest to the requested one
1070 // 3: the primary output
1071 // 4: the first output in the list
Eric Laurente552edb2014-03-10 17:42:56 -07001072
1073 if (outputs.size() == 0) {
1074 return 0;
1075 }
1076 if (outputs.size() == 1) {
1077 return outputs[0];
1078 }
1079
1080 int maxCommonFlags = 0;
Eric Laurente6930022016-02-11 10:20:40 -08001081 audio_io_handle_t outputForFlags = 0;
1082 audio_io_handle_t outputForPrimary = 0;
1083 audio_io_handle_t outputForFormat = 0;
1084 audio_format_t bestFormat = AUDIO_FORMAT_INVALID;
1085 audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07001086
1087 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001088 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -07001089 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -07001090 // if a valid format is specified, skip output if not compatible
1091 if (format != AUDIO_FORMAT_INVALID) {
1092 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente6930022016-02-11 10:20:40 -08001093 if (!audio_formats_match(format, outputDesc->mFormat)) {
Eric Laurent8838a382014-09-08 16:44:28 -07001094 continue;
1095 }
1096 } else if (!audio_is_linear_pcm(format)) {
1097 continue;
1098 }
Eric Laurente6930022016-02-11 10:20:40 -08001099 if (AudioPort::isBetterFormatMatch(
1100 outputDesc->mFormat, bestFormat, format)) {
1101 outputForFormat = outputs[i];
1102 bestFormat = outputDesc->mFormat;
1103 }
Eric Laurent8838a382014-09-08 16:44:28 -07001104 }
1105
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001106 int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags);
Eric Laurente6930022016-02-11 10:20:40 -08001107 if (commonFlags >= maxCommonFlags) {
1108 if (commonFlags == maxCommonFlags) {
1109 if (AudioPort::isBetterFormatMatch(
1110 outputDesc->mFormat, bestFormatForFlags, format)) {
1111 outputForFlags = outputs[i];
1112 bestFormatForFlags = outputDesc->mFormat;
1113 }
1114 } else {
1115 outputForFlags = outputs[i];
1116 maxCommonFlags = commonFlags;
1117 bestFormatForFlags = outputDesc->mFormat;
1118 }
Eric Laurente552edb2014-03-10 17:42:56 -07001119 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
1120 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001121 if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurente6930022016-02-11 10:20:40 -08001122 outputForPrimary = outputs[i];
Eric Laurente552edb2014-03-10 17:42:56 -07001123 }
1124 }
1125 }
1126
Eric Laurente6930022016-02-11 10:20:40 -08001127 if (outputForFlags != 0) {
1128 return outputForFlags;
Eric Laurente552edb2014-03-10 17:42:56 -07001129 }
Eric Laurente6930022016-02-11 10:20:40 -08001130 if (outputForFormat != 0) {
1131 return outputForFormat;
1132 }
1133 if (outputForPrimary != 0) {
1134 return outputForPrimary;
Eric Laurente552edb2014-03-10 17:42:56 -07001135 }
1136
1137 return outputs[0];
1138}
1139
Eric Laurente0720872014-03-11 09:30:41 -07001140status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001141 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001142 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001143{
Paul McLeanaa981192015-03-21 09:55:15 -07001144 ALOGV("startOutput() output %d, stream %d, session %d",
1145 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -07001146 ssize_t index = mOutputs.indexOfKey(output);
1147 if (index < 0) {
1148 ALOGW("startOutput() unknown output %d", output);
1149 return BAD_VALUE;
1150 }
1151
Eric Laurentc75307b2015-03-17 15:29:32 -07001152 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
1153
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001154 // Routing?
1155 mOutputRoutes.incRouteActivity(session);
1156
Eric Laurentc75307b2015-03-17 15:29:32 -07001157 audio_devices_t newDevice;
Eric Laurentc40d9692016-04-13 19:14:13 -07001158 AudioMix *policyMix = NULL;
1159 const char *address = NULL;
Eric Laurentc75307b2015-03-17 15:29:32 -07001160 if (outputDesc->mPolicyMix != NULL) {
Eric Laurentc40d9692016-04-13 19:14:13 -07001161 policyMix = outputDesc->mPolicyMix;
1162 address = policyMix->mDeviceAddress.string();
1163 if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
1164 newDevice = policyMix->mDeviceType;
1165 } else {
1166 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
1167 }
Eric Laurent493404d2015-04-21 15:07:36 -07001168 } else if (mOutputRoutes.hasRouteChanged(session)) {
1169 newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001170 checkStrategyRoute(getStrategy(stream), output);
Eric Laurentc75307b2015-03-17 15:29:32 -07001171 } else {
1172 newDevice = AUDIO_DEVICE_NONE;
1173 }
1174
1175 uint32_t delayMs = 0;
1176
Eric Laurentc40d9692016-04-13 19:14:13 -07001177 status_t status = startSource(outputDesc, stream, newDevice, address, &delayMs);
Eric Laurentc75307b2015-03-17 15:29:32 -07001178
1179 if (status != NO_ERROR) {
1180 mOutputRoutes.decRouteActivity(session);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001181 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -07001182 }
1183 // Automatically enable the remote submix input when output is started on a re routing mix
1184 // of type MIX_TYPE_RECORDERS
Eric Laurentc40d9692016-04-13 19:14:13 -07001185 if (audio_is_remote_submix_device(newDevice) && policyMix != NULL &&
1186 policyMix->mMixType == MIX_TYPE_RECORDERS) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001187 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1188 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Eric Laurentc40d9692016-04-13 19:14:13 -07001189 address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001190 "remote-submix");
1191 }
1192
1193 if (delayMs != 0) {
1194 usleep(delayMs * 1000);
1195 }
1196
1197 return status;
1198}
1199
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001200status_t AudioPolicyManager::startSource(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurentc75307b2015-03-17 15:29:32 -07001201 audio_stream_type_t stream,
1202 audio_devices_t device,
Eric Laurentc40d9692016-04-13 19:14:13 -07001203 const char *address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001204 uint32_t *delayMs)
1205{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001206 // cannot start playback of STREAM_TTS if any other output is being used
1207 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001208
1209 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001210 if (stream == AUDIO_STREAM_TTS) {
1211 ALOGV("\t found BEACON stream");
Eric Laurent9459fb02015-08-12 18:36:32 -07001212 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001213 return INVALID_OPERATION;
1214 } else {
1215 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1216 }
1217 } else {
1218 // some playback other than beacon starts
1219 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1220 }
1221
Eric Laurent77305a62016-07-25 16:39:22 -07001222 // force device change if the output is inactive and no audio patch is already present.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001223 // check active before incrementing usage count
Eric Laurent77305a62016-07-25 16:39:22 -07001224 bool force = !outputDesc->isActive() &&
1225 (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001226
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00001227 // requiresMuteCheck is false when we can bypass mute strategy.
1228 // It covers a common case when there is no materially active audio
1229 // and muting would result in unnecessary delay and dropped audio.
1230 const uint32_t outputLatencyMs = outputDesc->latency();
1231 bool requiresMuteCheck = outputDesc->isActive(outputLatencyMs * 2); // account for drain
1232
Eric Laurente552edb2014-03-10 17:42:56 -07001233 // increment usage count for this stream on the requested output:
1234 // NOTE that the usage count is the same for duplicated output and hardware output which is
1235 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1236 outputDesc->changeRefCount(stream, 1);
1237
Eric Laurent36829f92017-04-07 19:04:42 -07001238 if (stream == AUDIO_STREAM_MUSIC) {
1239 selectOutputForMusicEffects();
1240 }
1241
Eric Laurent493404d2015-04-21 15:07:36 -07001242 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001243 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001244 if (device == AUDIO_DEVICE_NONE) {
1245 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001246 }
Eric Laurent36829f92017-04-07 19:04:42 -07001247
Eric Laurente552edb2014-03-10 17:42:56 -07001248 routing_strategy strategy = getStrategy(stream);
1249 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001250 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1251 (beaconMuteLatency > 0);
1252 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001253 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001254 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001255 if (desc != outputDesc) {
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00001256 // An output has a shared device if
1257 // - managed by the same hw module
1258 // - supports the currently selected device
1259 const bool sharedDevice = outputDesc->sharesHwModuleWith(desc)
1260 && (desc->supportedDevices() & device) != AUDIO_DEVICE_NONE;
1261
Eric Laurent77305a62016-07-25 16:39:22 -07001262 // force a device change if any other output is:
1263 // - managed by the same hw module
Jean-Michel Trivi4a5b4812018-02-08 17:22:32 +00001264 // - supports currently selected device
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00001265 // - has a current device selection that differs from selected device.
Eric Laurent77305a62016-07-25 16:39:22 -07001266 // - has an active audio patch
Eric Laurente552edb2014-03-10 17:42:56 -07001267 // In this case, the audio HAL must receive the new device selection so that it can
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00001268 // change the device currently selected by the other output.
1269 if (sharedDevice &&
Eric Laurent77305a62016-07-25 16:39:22 -07001270 desc->device() != device &&
Eric Laurent77305a62016-07-25 16:39:22 -07001271 desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07001272 force = true;
1273 }
1274 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001275 // a notification so that audio focus effect can propagate, or that a mute/unmute
1276 // event occurred for beacon
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00001277 const uint32_t latencyMs = desc->latency();
1278 const bool isActive = desc->isActive(latencyMs * 2); // account for drain
1279
1280 if (shouldWait && isActive && (waitMs < latencyMs)) {
1281 waitMs = latencyMs;
Eric Laurente552edb2014-03-10 17:42:56 -07001282 }
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00001283
1284 // Require mute check if another output is on a shared device
1285 // and currently active to have proper drain and avoid pops.
1286 // Note restoring AudioTracks onto this output needs to invoke
1287 // a volume ramp if there is no mute.
1288 requiresMuteCheck |= sharedDevice && isActive;
Eric Laurente552edb2014-03-10 17:42:56 -07001289 }
1290 }
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00001291
1292 const uint32_t muteWaitMs =
1293 setOutputDevice(outputDesc, device, force, 0, NULL, address, requiresMuteCheck);
Eric Laurente552edb2014-03-10 17:42:56 -07001294
1295 // handle special case for sonification while in call
1296 if (isInCall()) {
1297 handleIncallSonification(stream, true, false);
1298 }
1299
1300 // apply volume rules for current stream and device if necessary
1301 checkAndSetVolume(stream,
Shuhei Miyazaki6fe70332017-07-31 15:21:28 +09001302 mVolumeCurves->getVolumeIndex(stream, outputDesc->device()),
Eric Laurentc75307b2015-03-17 15:29:32 -07001303 outputDesc,
Shuhei Miyazaki6fe70332017-07-31 15:21:28 +09001304 outputDesc->device());
Eric Laurente552edb2014-03-10 17:42:56 -07001305
1306 // update the outputs if starting an output with a stream that can affect notification
1307 // routing
1308 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001309
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001310 // force reevaluating accessibility routing when ringtone or alarm starts
1311 if (strategy == STRATEGY_SONIFICATION) {
1312 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1313 }
Eric Laurentdc462862016-07-19 12:29:53 -07001314
1315 if (waitMs > muteWaitMs) {
1316 *delayMs = waitMs - muteWaitMs;
1317 }
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00001318
1319 // FIXME: A device change (muteWaitMs > 0) likely introduces a volume change.
1320 // A volume change enacted by APM with 0 delay is not synchronous, as it goes
1321 // via AudioCommandThread to AudioFlinger. Hence it is possible that the volume
1322 // change occurs after the MixerThread starts and causes a stream volume
1323 // glitch.
1324 //
1325 // We do not introduce additional delay here.
Eric Laurente552edb2014-03-10 17:42:56 -07001326 }
Eric Laurentdc462862016-07-19 12:29:53 -07001327
Eric Laurente552edb2014-03-10 17:42:56 -07001328 return NO_ERROR;
1329}
1330
1331
Eric Laurente0720872014-03-11 09:30:41 -07001332status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001333 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001334 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001335{
1336 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1337 ssize_t index = mOutputs.indexOfKey(output);
1338 if (index < 0) {
1339 ALOGW("stopOutput() unknown output %d", output);
1340 return BAD_VALUE;
1341 }
1342
Eric Laurentc75307b2015-03-17 15:29:32 -07001343 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001344
Eric Laurentc75307b2015-03-17 15:29:32 -07001345 if (outputDesc->mRefCount[stream] == 1) {
1346 // Automatically disable the remote submix input when output is stopped on a
1347 // re routing mix of type MIX_TYPE_RECORDERS
1348 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1349 outputDesc->mPolicyMix != NULL &&
1350 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1351 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1352 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001353 outputDesc->mPolicyMix->mDeviceAddress,
Eric Laurentc75307b2015-03-17 15:29:32 -07001354 "remote-submix");
1355 }
1356 }
1357
1358 // Routing?
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001359 bool forceDeviceUpdate = false;
Eric Laurentc75307b2015-03-17 15:29:32 -07001360 if (outputDesc->mRefCount[stream] > 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001361 int activityCount = mOutputRoutes.decRouteActivity(session);
1362 forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0));
1363
1364 if (forceDeviceUpdate) {
1365 checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE);
1366 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001367 }
1368
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001369 return stopSource(outputDesc, stream, forceDeviceUpdate);
Eric Laurentc75307b2015-03-17 15:29:32 -07001370}
1371
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001372status_t AudioPolicyManager::stopSource(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001373 audio_stream_type_t stream,
1374 bool forceDeviceUpdate)
Eric Laurentc75307b2015-03-17 15:29:32 -07001375{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001376 // always handle stream stop, check which stream type is stopping
1377 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1378
Eric Laurente552edb2014-03-10 17:42:56 -07001379 // handle special case for sonification while in call
1380 if (isInCall()) {
1381 handleIncallSonification(stream, false, false);
1382 }
1383
1384 if (outputDesc->mRefCount[stream] > 0) {
1385 // decrement usage count of this stream on the output
1386 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001387
Eric Laurente552edb2014-03-10 17:42:56 -07001388 // store time at which the stream was stopped - see isStreamActive()
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001389 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
Eric Laurente552edb2014-03-10 17:42:56 -07001390 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001391 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001392 // delay the device switch by twice the latency because stopOutput() is executed when
1393 // the track stop() command is received and at that time the audio track buffer can
1394 // still contain data that needs to be drained. The latency only covers the audio HAL
1395 // and kernel buffers. Also the latency does not always include additional delay in the
1396 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001397 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001398
1399 // force restoring the device selection on other active outputs if it differs from the
1400 // one being selected for this output
Eric Laurent57de36c2016-09-28 16:59:11 -07001401 uint32_t delayMs = outputDesc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -07001402 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001403 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001404 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001405 desc->isActive() &&
1406 outputDesc->sharesHwModuleWith(desc) &&
1407 (newDevice != desc->device())) {
Haynes Mathew George11c499a2016-08-26 12:08:25 -07001408 audio_devices_t newDevice2 = getNewOutputDevice(desc, false /*fromCache*/);
1409 bool force = desc->device() != newDevice2;
Eric Laurentc75307b2015-03-17 15:29:32 -07001410 setOutputDevice(desc,
Haynes Mathew George11c499a2016-08-26 12:08:25 -07001411 newDevice2,
1412 force,
Eric Laurent57de36c2016-09-28 16:59:11 -07001413 delayMs);
1414 // re-apply device specific volume if not done by setOutputDevice()
1415 if (!force) {
1416 applyStreamVolumes(desc, newDevice2, delayMs);
1417 }
Eric Laurente552edb2014-03-10 17:42:56 -07001418 }
1419 }
1420 // update the outputs if stopping one with a stream that can affect notification routing
1421 handleNotificationRoutingForStream(stream);
1422 }
Eric Laurent36829f92017-04-07 19:04:42 -07001423 if (stream == AUDIO_STREAM_MUSIC) {
1424 selectOutputForMusicEffects();
1425 }
Eric Laurente552edb2014-03-10 17:42:56 -07001426 return NO_ERROR;
1427 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001428 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001429 return INVALID_OPERATION;
1430 }
1431}
1432
Eric Laurente83b55d2014-11-14 10:06:21 -08001433void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001434 audio_stream_type_t stream __unused,
1435 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001436{
1437 ALOGV("releaseOutput() %d", output);
1438 ssize_t index = mOutputs.indexOfKey(output);
1439 if (index < 0) {
1440 ALOGW("releaseOutput() releasing unknown output %d", output);
1441 return;
1442 }
1443
Paul McLeanaa981192015-03-21 09:55:15 -07001444 // Routing
1445 mOutputRoutes.removeRoute(session);
1446
Eric Laurentc75307b2015-03-17 15:29:32 -07001447 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001448 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001449 if (desc->mDirectOpenCount <= 0) {
1450 ALOGW("releaseOutput() invalid open count %d for output %d",
1451 desc->mDirectOpenCount, output);
1452 return;
1453 }
1454 if (--desc->mDirectOpenCount == 0) {
1455 closeOutput(output);
Eric Laurentb52c1522014-05-20 11:27:36 -07001456 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001457 }
1458 }
1459}
1460
1461
Eric Laurentcaf7f482014-11-25 17:50:47 -08001462status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1463 audio_io_handle_t *input,
1464 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001465 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001466 const audio_config_base_t *config,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001467 audio_input_flags_t flags,
Eric Laurent2ac76942017-06-22 17:17:09 -07001468 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001469 input_type_t *inputType,
1470 audio_port_handle_t *portId)
Eric Laurente552edb2014-03-10 17:42:56 -07001471{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001472 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1473 "session %d, flags %#x",
Eric Laurent20b9ef02016-12-05 11:03:16 -08001474 attr->source, config->sample_rate, config->format, config->channel_mask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001475
Eric Laurentad2e7b92017-09-14 20:06:42 -07001476 status_t status = NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -08001477 // handle legacy remote submix case where the address was not always specified
1478 String8 address = String8("");
Eric Laurentc447ded2015-01-06 08:47:05 -08001479 audio_source_t halInputSource;
Eric Laurentad2e7b92017-09-14 20:06:42 -07001480 audio_source_t inputSource = attr->source;
Eric Laurentc722f302014-12-10 11:21:49 -08001481 AudioMix *policyMix = NULL;
Eric Laurentad2e7b92017-09-14 20:06:42 -07001482 DeviceVector inputDevices;
Eric Laurent20b9ef02016-12-05 11:03:16 -08001483
Paul McLean466dc8e2015-04-17 13:15:36 -06001484 // Explicit routing?
1485 sp<DeviceDescriptor> deviceDesc;
Eric Laurent2ac76942017-06-22 17:17:09 -07001486 if (*selectedDeviceId != AUDIO_PORT_HANDLE_NONE) {
1487 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1488 if (mAvailableInputDevices[i]->getId() == *selectedDeviceId) {
1489 deviceDesc = mAvailableInputDevices[i];
1490 break;
1491 }
Paul McLean466dc8e2015-04-17 13:15:36 -06001492 }
1493 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001494 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001495
Eric Laurentad2e7b92017-09-14 20:06:42 -07001496 // special case for mmap capture: if an input IO handle is specified, we reuse this input if
1497 // possible
1498 if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) == AUDIO_INPUT_FLAG_MMAP_NOIRQ &&
1499 *input != AUDIO_IO_HANDLE_NONE) {
1500 ssize_t index = mInputs.indexOfKey(*input);
1501 if (index < 0) {
1502 ALOGW("getInputForAttr() unknown MMAP input %d", *input);
1503 status = BAD_VALUE;
1504 goto error;
1505 }
1506 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1507 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1508 if (audioSession == 0) {
1509 ALOGW("getInputForAttr() unknown session %d on input %d", session, *input);
1510 status = BAD_VALUE;
1511 goto error;
1512 }
1513 // For MMAP mode, the first call to getInputForAttr() is made on behalf of audioflinger.
1514 // The second call is for the first active client and sets the UID. Any further call
1515 // corresponds to a new client and is only permitted from the same UId.
1516 if (audioSession->openCount() == 1) {
1517 audioSession->setUid(uid);
1518 } else if (audioSession->uid() != uid) {
1519 ALOGW("getInputForAttr() bad uid %d for session %d uid %d",
1520 uid, session, audioSession->uid());
1521 status = INVALID_OPERATION;
1522 goto error;
1523 }
1524 audioSession->changeOpenCount(1);
1525 *inputType = API_INPUT_LEGACY;
1526 if (*portId == AUDIO_PORT_HANDLE_NONE) {
1527 *portId = AudioPort::getNextUniqueId();
1528 }
1529 inputDevices = mAvailableInputDevices.getDevicesFromType(inputDesc->mDevice);
1530 *selectedDeviceId = inputDevices.size() > 0 ? inputDevices.itemAt(0)->getId()
1531 : AUDIO_PORT_HANDLE_NONE;
1532 ALOGI("%s reusing MMAP input %d for session %d", __FUNCTION__, *input, session);
1533
1534 return NO_ERROR;
1535 }
1536
1537 *input = AUDIO_IO_HANDLE_NONE;
1538 *inputType = API_INPUT_INVALID;
1539
1540 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1541 inputSource = AUDIO_SOURCE_MIC;
1542 }
1543 halInputSource = inputSource;
1544
1545 // TODO: check for existing client for this port ID
1546 if (*portId == AUDIO_PORT_HANDLE_NONE) {
1547 *portId = AudioPort::getNextUniqueId();
1548 }
1549
1550 audio_devices_t device;
1551
Eric Laurentc447ded2015-01-06 08:47:05 -08001552 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001553 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Eric Laurentad2e7b92017-09-14 20:06:42 -07001554 status = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
1555 if (status != NO_ERROR) {
1556 goto error;
François Gaffie036e1e92015-03-19 10:16:24 +01001557 }
1558 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001559 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1560 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001561 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001562 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001563 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001564 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurentad2e7b92017-09-14 20:06:42 -07001565 status = BAD_VALUE;
1566 goto error;
Eric Laurent275e8e92014-11-30 15:14:47 -08001567 }
Eric Laurentc722f302014-12-10 11:21:49 -08001568 if (policyMix != NULL) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001569 address = policyMix->mDeviceAddress;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001570 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1571 // there is an external policy, but this input is attached to a mix of recorders,
1572 // meaning it receives audio injected into the framework, so the recorder doesn't
1573 // know about it and is therefore considered "legacy"
1574 *inputType = API_INPUT_LEGACY;
1575 } else {
1576 // recording a mix of players defined by an external policy, we're rerouting for
1577 // an external policy
1578 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1579 }
Eric Laurentc722f302014-12-10 11:21:49 -08001580 } else if (audio_is_remote_submix_device(device)) {
1581 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001582 *inputType = API_INPUT_MIX_CAPTURE;
Eric Laurent82db2692015-08-07 13:59:42 -07001583 } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
1584 *inputType = API_INPUT_TELEPHONY_RX;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001585 } else {
1586 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001587 }
Ravi Kumar Alamandab367f5b2015-08-25 08:21:37 -07001588
Eric Laurent599c7582015-12-07 18:05:55 -08001589 }
1590
1591 *input = getInputForDevice(device, address, session, uid, inputSource,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001592 config->sample_rate, config->format, config->channel_mask, flags,
Eric Laurent599c7582015-12-07 18:05:55 -08001593 policyMix);
1594 if (*input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentad2e7b92017-09-14 20:06:42 -07001595 status = INVALID_OPERATION;
1596 goto error;
Eric Laurent599c7582015-12-07 18:05:55 -08001597 }
Eric Laurent20b9ef02016-12-05 11:03:16 -08001598
Eric Laurentad2e7b92017-09-14 20:06:42 -07001599 inputDevices = mAvailableInputDevices.getDevicesFromType(device);
Eric Laurent2ac76942017-06-22 17:17:09 -07001600 *selectedDeviceId = inputDevices.size() > 0 ? inputDevices.itemAt(0)->getId()
1601 : AUDIO_PORT_HANDLE_NONE;
1602
1603 ALOGV("getInputForAttr() returns input %d type %d selectedDeviceId %d",
1604 *input, *inputType, *selectedDeviceId);
1605
Eric Laurent599c7582015-12-07 18:05:55 -08001606 return NO_ERROR;
Eric Laurentad2e7b92017-09-14 20:06:42 -07001607
1608error:
1609 mInputRoutes.removeRoute(session);
1610 return status;
Eric Laurent599c7582015-12-07 18:05:55 -08001611}
1612
1613
1614audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device,
1615 String8 address,
1616 audio_session_t session,
1617 uid_t uid,
1618 audio_source_t inputSource,
1619 uint32_t samplingRate,
1620 audio_format_t format,
1621 audio_channel_mask_t channelMask,
1622 audio_input_flags_t flags,
1623 AudioMix *policyMix)
1624{
1625 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
1626 audio_source_t halInputSource = inputSource;
1627 bool isSoundTrigger = false;
1628
1629 if (inputSource == AUDIO_SOURCE_HOTWORD) {
1630 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1631 if (index >= 0) {
1632 input = mSoundTriggerSessions.valueFor(session);
1633 isSoundTrigger = true;
1634 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1635 ALOGV("SoundTrigger capture on session %d input %d", session, input);
1636 } else {
1637 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001638 }
Haynes Mathew George851d3ff2017-06-19 20:01:57 -07001639 } else if (inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION &&
Haynes Mathew George851d3ff2017-06-19 20:01:57 -07001640 audio_is_linear_pcm(format)) {
1641 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_VOIP_TX);
Eric Laurent5dbe4712014-09-19 19:04:57 -07001642 }
1643
Andy Hungf129b032015-04-07 13:45:50 -07001644 // find a compatible input profile (not necessarily identical in parameters)
1645 sp<IOProfile> profile;
1646 // samplingRate and flags may be updated by getInputProfile
Glenn Kasten05ddca52016-02-11 08:17:12 -08001647 uint32_t profileSamplingRate = (samplingRate == 0) ? SAMPLE_RATE_HZ_DEFAULT : samplingRate;
Andy Hungf129b032015-04-07 13:45:50 -07001648 audio_format_t profileFormat = format;
1649 audio_channel_mask_t profileChannelMask = channelMask;
1650 audio_input_flags_t profileFlags = flags;
1651 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001652 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001653 profileSamplingRate, profileFormat, profileChannelMask,
1654 profileFlags);
1655 if (profile != 0) {
1656 break; // success
Eric Laurent05067782016-06-01 18:27:28 -07001657 } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) {
1658 profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry
Andy Hungf129b032015-04-07 13:45:50 -07001659 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1660 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1661 } else { // fail
Glenn Kastenfaa10a62017-05-25 15:52:05 -07001662 ALOGW("getInputForDevice() could not find profile for device 0x%X, "
Eric Laurent599c7582015-12-07 18:05:55 -08001663 "samplingRate %u, format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001664 device, samplingRate, format, channelMask, flags);
Eric Laurent599c7582015-12-07 18:05:55 -08001665 return input;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001666 }
Eric Laurente552edb2014-03-10 17:42:56 -07001667 }
Glenn Kasten05ddca52016-02-11 08:17:12 -08001668 // Pick input sampling rate if not specified by client
1669 if (samplingRate == 0) {
1670 samplingRate = profileSamplingRate;
1671 }
Eric Laurente552edb2014-03-10 17:42:56 -07001672
Eric Laurent322b4d22015-04-03 15:57:54 -07001673 if (profile->getModuleHandle() == 0) {
1674 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurent599c7582015-12-07 18:05:55 -08001675 return input;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001676 }
1677
Eric Laurent599c7582015-12-07 18:05:55 -08001678 sp<AudioSession> audioSession = new AudioSession(session,
1679 inputSource,
1680 format,
1681 samplingRate,
1682 channelMask,
1683 flags,
1684 uid,
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001685 isSoundTrigger,
1686 policyMix, mpClientInterface);
Eric Laurent599c7582015-12-07 18:05:55 -08001687
Eric Laurent74708e72017-04-07 17:13:42 -07001688// FIXME: disable concurrent capture until UI is ready
1689#if 0
Eric Laurent599c7582015-12-07 18:05:55 -08001690 // reuse an open input if possible
Eric Laurent555530a2017-02-07 18:17:24 -08001691 sp<AudioInputDescriptor> reusedInputDesc;
Eric Laurent599c7582015-12-07 18:05:55 -08001692 for (size_t i = 0; i < mInputs.size(); i++) {
1693 sp<AudioInputDescriptor> desc = mInputs.valueAt(i);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001694 // reuse input if:
1695 // - it shares the same profile
1696 // AND
1697 // - it is not a reroute submix input
1698 // AND
1699 // - it is: not used for sound trigger
1700 // OR
1701 // used for sound trigger and all clients use the same session ID
1702 //
1703 if ((profile == desc->mProfile) &&
1704 (isSoundTrigger == desc->isSoundTrigger()) &&
1705 !is_virtual_input_device(device)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001706
1707 sp<AudioSession> as = desc->getAudioSession(session);
1708 if (as != 0) {
1709 // do not allow unmatching properties on same session
1710 if (as->matches(audioSession)) {
1711 as->changeOpenCount(1);
1712 } else {
1713 ALOGW("getInputForDevice() record with different attributes"
1714 " exists for session %d", session);
Eric Laurent555530a2017-02-07 18:17:24 -08001715 continue;
Eric Laurent599c7582015-12-07 18:05:55 -08001716 }
Eric Laurentfb66dd92016-01-28 18:32:03 -08001717 } else if (isSoundTrigger) {
Eric Laurent555530a2017-02-07 18:17:24 -08001718 continue;
Eric Laurentfb66dd92016-01-28 18:32:03 -08001719 }
Eric Laurentbb948092017-01-23 18:33:30 -08001720
Eric Laurent555530a2017-02-07 18:17:24 -08001721 // Reuse the already opened input stream on this profile if:
1722 // - the new capture source is background OR
1723 // - the path requested configurations match OR
1724 // - the new source priority is less than the highest source priority on this input
1725 // If the input stream cannot be reused, close it before opening a new stream
1726 // on the same profile for the new client so that the requested path configuration
1727 // can be selected.
1728 if (!isConcurrentSource(inputSource) &&
Eric Laurentbb948092017-01-23 18:33:30 -08001729 ((desc->mSamplingRate != samplingRate ||
Eric Laurentfb66dd92016-01-28 18:32:03 -08001730 desc->mChannelMask != channelMask ||
Eric Laurente6930022016-02-11 10:20:40 -08001731 !audio_formats_match(desc->mFormat, format)) &&
Eric Laurentfb66dd92016-01-28 18:32:03 -08001732 (source_priority(desc->getHighestPrioritySource(false /*activeOnly*/)) <
Eric Laurentbb948092017-01-23 18:33:30 -08001733 source_priority(inputSource)))) {
Eric Laurent555530a2017-02-07 18:17:24 -08001734 reusedInputDesc = desc;
1735 continue;
Eric Laurent599c7582015-12-07 18:05:55 -08001736 } else {
1737 desc->addAudioSession(session, audioSession);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001738 ALOGV("%s: reusing input %d", __FUNCTION__, mInputs.keyAt(i));
1739 return mInputs.keyAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08001740 }
Eric Laurent599c7582015-12-07 18:05:55 -08001741 }
1742 }
Eric Laurent599c7582015-12-07 18:05:55 -08001743
Eric Laurent555530a2017-02-07 18:17:24 -08001744 if (reusedInputDesc != 0) {
1745 AudioSessionCollection sessions = reusedInputDesc->getAudioSessions(false /*activeOnly*/);
1746 for (size_t j = 0; j < sessions.size(); j++) {
1747 audio_session_t currentSession = sessions.keyAt(j);
1748 stopInput(reusedInputDesc->mIoHandle, currentSession);
1749 releaseInput(reusedInputDesc->mIoHandle, currentSession);
1750 }
1751 }
Eric Laurent74708e72017-04-07 17:13:42 -07001752#endif
Eric Laurent555530a2017-02-07 18:17:24 -08001753
Eric Laurentcf2c0212014-07-25 16:20:43 -07001754 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001755 config.sample_rate = profileSamplingRate;
1756 config.channel_mask = profileChannelMask;
1757 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001758
Eric Laurente3014102017-05-03 11:15:43 -07001759 if (address == "") {
1760 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(device);
1761 // the inputs vector must be of size 1, but we don't want to crash here
1762 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress : String8("");
1763 }
1764
Eric Laurent322b4d22015-04-03 15:57:54 -07001765 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurent599c7582015-12-07 18:05:55 -08001766 &input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001767 &config,
1768 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001769 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001770 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001771 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001772
1773 // only accept input with the exact requested set of parameters
Eric Laurent599c7582015-12-07 18:05:55 -08001774 if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001775 (profileSamplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -08001776 !audio_formats_match(profileFormat, config.format) ||
Andy Hungf129b032015-04-07 13:45:50 -07001777 (profileChannelMask != config.channel_mask)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001778 ALOGW("getInputForAttr() failed opening input: samplingRate %d"
1779 ", format %d, channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001780 samplingRate, format, channelMask);
Eric Laurent599c7582015-12-07 18:05:55 -08001781 if (input != AUDIO_IO_HANDLE_NONE) {
1782 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001783 }
Eric Laurent599c7582015-12-07 18:05:55 -08001784 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001785 }
1786
Eric Laurent1f2f2232014-06-02 12:01:23 -07001787 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Andy Hungf129b032015-04-07 13:45:50 -07001788 inputDesc->mSamplingRate = profileSamplingRate;
1789 inputDesc->mFormat = profileFormat;
1790 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001791 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001792 inputDesc->mPolicyMix = policyMix;
Eric Laurent599c7582015-12-07 18:05:55 -08001793 inputDesc->addAudioSession(session, audioSession);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001794
Eric Laurent599c7582015-12-07 18:05:55 -08001795 addInput(input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001796 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001797
Eric Laurent599c7582015-12-07 18:05:55 -08001798 return input;
Eric Laurente552edb2014-03-10 17:42:56 -07001799}
1800
Eric Laurentbb948092017-01-23 18:33:30 -08001801//static
1802bool AudioPolicyManager::isConcurrentSource(audio_source_t source)
1803{
1804 return (source == AUDIO_SOURCE_HOTWORD) ||
1805 (source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
1806 (source == AUDIO_SOURCE_FM_TUNER);
1807}
1808
Eric Laurentfb66dd92016-01-28 18:32:03 -08001809bool AudioPolicyManager::isConcurentCaptureAllowed(const sp<AudioInputDescriptor>& inputDesc,
1810 const sp<AudioSession>& audioSession)
1811{
1812 // Do not allow capture if an active voice call is using a software patch and
1813 // the call TX source device is on the same HW module.
1814 // FIXME: would be better to refine to only inputs whose profile connects to the
1815 // call TX device but this information is not in the audio patch
1816 if (mCallTxPatch != 0 &&
1817 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1818 return false;
1819 }
1820
1821 // starting concurrent capture is enabled if:
1822 // 1) capturing for re-routing
1823 // 2) capturing for HOTWORD source
1824 // 3) capturing for FM TUNER source
1825 // 3) All other active captures are either for re-routing or HOTWORD
1826
1827 if (is_virtual_input_device(inputDesc->mDevice) ||
Eric Laurentbb948092017-01-23 18:33:30 -08001828 isConcurrentSource(audioSession->inputSource())) {
Eric Laurentfb66dd92016-01-28 18:32:03 -08001829 return true;
1830 }
1831
1832 Vector< sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
1833 for (size_t i = 0; i < activeInputs.size(); i++) {
1834 sp<AudioInputDescriptor> activeInput = activeInputs[i];
Eric Laurentbb948092017-01-23 18:33:30 -08001835 if (!isConcurrentSource(activeInput->inputSource(true)) &&
Eric Laurentfb66dd92016-01-28 18:32:03 -08001836 !is_virtual_input_device(activeInput->mDevice)) {
1837 return false;
1838 }
1839 }
1840
1841 return true;
1842}
1843
Chris Thornton2b864642017-06-27 21:26:07 -07001844// FIXME: remove when concurrent capture is ready. This is a hack to work around bug b/63083537.
1845bool AudioPolicyManager::soundTriggerSupportsConcurrentCapture() {
1846 if (!mHasComputedSoundTriggerSupportsConcurrentCapture) {
1847 bool soundTriggerSupportsConcurrentCapture = false;
1848 unsigned int numModules = 0;
1849 struct sound_trigger_module_descriptor* nModules = NULL;
1850
1851 status_t status = SoundTrigger::listModules(nModules, &numModules);
1852 if (status == NO_ERROR && numModules != 0) {
1853 nModules = (struct sound_trigger_module_descriptor*) calloc(
1854 numModules, sizeof(struct sound_trigger_module_descriptor));
1855 if (nModules == NULL) {
1856 // We failed to malloc the buffer, so just say no for now, and hope that we have more
1857 // ram the next time this function is called.
1858 ALOGE("Failed to allocate buffer for module descriptors");
1859 return false;
1860 }
1861
1862 status = SoundTrigger::listModules(nModules, &numModules);
1863 if (status == NO_ERROR) {
1864 soundTriggerSupportsConcurrentCapture = true;
1865 for (size_t i = 0; i < numModules; ++i) {
1866 soundTriggerSupportsConcurrentCapture &=
1867 nModules[i].properties.concurrent_capture;
1868 }
1869 }
1870 free(nModules);
1871 }
1872 mSoundTriggerSupportsConcurrentCapture = soundTriggerSupportsConcurrentCapture;
1873 mHasComputedSoundTriggerSupportsConcurrentCapture = true;
1874 }
1875 return mSoundTriggerSupportsConcurrentCapture;
1876}
1877
Eric Laurentfb66dd92016-01-28 18:32:03 -08001878
Eric Laurent4dc68062014-07-28 17:26:49 -07001879status_t AudioPolicyManager::startInput(audio_io_handle_t input,
Eric Laurentfb66dd92016-01-28 18:32:03 -08001880 audio_session_t session,
1881 concurrency_type__mask_t *concurrency)
Eric Laurente552edb2014-03-10 17:42:56 -07001882{
1883 ALOGV("startInput() input %d", input);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001884 *concurrency = API_INPUT_CONCURRENCY_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001885 ssize_t index = mInputs.indexOfKey(input);
1886 if (index < 0) {
1887 ALOGW("startInput() unknown input %d", input);
1888 return BAD_VALUE;
1889 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001890 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001891
Eric Laurent599c7582015-12-07 18:05:55 -08001892 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1893 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07001894 ALOGW("startInput() unknown session %d on input %d", session, input);
1895 return BAD_VALUE;
1896 }
1897
Eric Laurent74708e72017-04-07 17:13:42 -07001898// FIXME: disable concurrent capture until UI is ready
1899#if 0
Eric Laurentfb66dd92016-01-28 18:32:03 -08001900 if (!isConcurentCaptureAllowed(inputDesc, audioSession)) {
1901 ALOGW("startInput(%d) failed: other input already started", input);
1902 return INVALID_OPERATION;
1903 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001904
Eric Laurentfb66dd92016-01-28 18:32:03 -08001905 if (isInCall()) {
1906 *concurrency |= API_INPUT_CONCURRENCY_CALL;
1907 }
Eric Laurentf7c50102016-09-30 15:19:43 -07001908 if (mInputs.activeInputsCountOnDevices() != 0) {
Eric Laurentfb66dd92016-01-28 18:32:03 -08001909 *concurrency |= API_INPUT_CONCURRENCY_CAPTURE;
Eric Laurente552edb2014-03-10 17:42:56 -07001910 }
Eric Laurent74708e72017-04-07 17:13:42 -07001911#else
1912 if (!is_virtual_input_device(inputDesc->mDevice)) {
1913 if (mCallTxPatch != 0 &&
1914 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1915 ALOGW("startInput(%d) failed: call in progress", input);
1916 return INVALID_OPERATION;
1917 }
1918
1919 Vector< sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
1920 for (size_t i = 0; i < activeInputs.size(); i++) {
1921 sp<AudioInputDescriptor> activeDesc = activeInputs[i];
1922
1923 if (is_virtual_input_device(activeDesc->mDevice)) {
1924 continue;
1925 }
1926
Eric Laurentcb4dae22017-07-01 19:39:32 -07001927 if ((audioSession->flags() & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0 &&
1928 activeDesc->getId() == inputDesc->getId()) {
1929 continue;
1930 }
1931
Eric Laurent74708e72017-04-07 17:13:42 -07001932 audio_source_t activeSource = activeDesc->inputSource(true);
1933 if (audioSession->inputSource() == AUDIO_SOURCE_HOTWORD) {
1934 if (activeSource == AUDIO_SOURCE_HOTWORD) {
1935 if (activeDesc->hasPreemptedSession(session)) {
1936 ALOGW("startInput(%d) failed for HOTWORD: "
1937 "other input %d already started for HOTWORD",
1938 input, activeDesc->mIoHandle);
1939 return INVALID_OPERATION;
1940 }
1941 } else {
1942 ALOGV("startInput(%d) failed for HOTWORD: other input %d already started",
1943 input, activeDesc->mIoHandle);
1944 return INVALID_OPERATION;
1945 }
1946 } else {
1947 if (activeSource != AUDIO_SOURCE_HOTWORD) {
1948 ALOGW("startInput(%d) failed: other input %d already started",
1949 input, activeDesc->mIoHandle);
1950 return INVALID_OPERATION;
1951 }
1952 }
1953 }
1954
Chris Thornton2b864642017-06-27 21:26:07 -07001955 // We only need to check if the sound trigger session supports concurrent capture if the
1956 // input is also a sound trigger input. Otherwise, we should preempt any hotword stream
1957 // that's running.
1958 const bool allowConcurrentWithSoundTrigger =
1959 inputDesc->isSoundTrigger() ? soundTriggerSupportsConcurrentCapture() : false;
1960
Eric Laurent74708e72017-04-07 17:13:42 -07001961 // if capture is allowed, preempt currently active HOTWORD captures
1962 for (size_t i = 0; i < activeInputs.size(); i++) {
1963 sp<AudioInputDescriptor> activeDesc = activeInputs[i];
1964
1965 if (is_virtual_input_device(activeDesc->mDevice)) {
1966 continue;
1967 }
1968
Chris Thornton2b864642017-06-27 21:26:07 -07001969 if (allowConcurrentWithSoundTrigger && activeDesc->isSoundTrigger()) {
1970 continue;
1971 }
1972
Eric Laurent74708e72017-04-07 17:13:42 -07001973 audio_source_t activeSource = activeDesc->inputSource(true);
1974 if (activeSource == AUDIO_SOURCE_HOTWORD) {
1975 AudioSessionCollection activeSessions =
1976 activeDesc->getAudioSessions(true /*activeOnly*/);
1977 audio_session_t activeSession = activeSessions.keyAt(0);
1978 audio_io_handle_t activeHandle = activeDesc->mIoHandle;
1979 SortedVector<audio_session_t> sessions = activeDesc->getPreemptedSessions();
1980 sessions.add(activeSession);
1981 inputDesc->setPreemptedSessions(sessions);
1982 stopInput(activeHandle, activeSession);
1983 releaseInput(activeHandle, activeSession);
1984 ALOGV("startInput(%d) for HOTWORD preempting HOTWORD input %d",
1985 input, activeDesc->mIoHandle);
1986 }
1987 }
1988 }
1989#endif
Eric Laurente552edb2014-03-10 17:42:56 -07001990
Eric Laurent313d1e72016-01-29 09:56:57 -08001991 // increment activity count before calling getNewInputDevice() below as only active sessions
1992 // are considered for device selection
1993 audioSession->changeActiveCount(1);
1994
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001995 // Routing?
1996 mInputRoutes.incRouteActivity(session);
1997
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001998 if (audioSession->activeCount() == 1 || mInputRoutes.hasRouteChanged(session)) {
Eric Laurent271a93e2016-09-29 14:47:06 -07001999 // indicate active capture to sound trigger service if starting capture from a mic on
2000 // primary HW module
Eric Laurentf7c50102016-09-30 15:19:43 -07002001 audio_devices_t device = getNewInputDevice(inputDesc);
Eric Laurent271a93e2016-09-29 14:47:06 -07002002 setInputDevice(input, device, true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07002003
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07002004 if (inputDesc->getAudioSessionCount(true/*activeOnly*/) == 1) {
2005 // if input maps to a dynamic policy with an activity listener, notify of state change
2006 if ((inputDesc->mPolicyMix != NULL)
2007 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
2008 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
2009 MIX_STATE_MIXING);
Eric Laurentc722f302014-12-10 11:21:49 -08002010 }
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07002011
Eric Laurentf7c50102016-09-30 15:19:43 -07002012 audio_devices_t primaryInputDevices = availablePrimaryInputDevices();
2013 if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) &&
Eric Laurent05b345e2016-11-18 12:36:27 -08002014 mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) {
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07002015 SoundTrigger::setCaptureState(true);
2016 }
2017
2018 // automatically enable the remote submix output when input is started if not
2019 // used by a policy mix of type MIX_TYPE_RECORDERS
2020 // For remote submix (a virtual device), we open only one input per capture request.
2021 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
2022 String8 address = String8("");
2023 if (inputDesc->mPolicyMix == NULL) {
2024 address = String8("0");
2025 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
2026 address = inputDesc->mPolicyMix->mDeviceAddress;
2027 }
2028 if (address != "") {
2029 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2030 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2031 address, "remote-submix");
2032 }
Eric Laurentc722f302014-12-10 11:21:49 -08002033 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07002034 }
Eric Laurente552edb2014-03-10 17:42:56 -07002035 }
2036
Eric Laurent599c7582015-12-07 18:05:55 -08002037 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Eric Laurente552edb2014-03-10 17:42:56 -07002038
Eric Laurente552edb2014-03-10 17:42:56 -07002039 return NO_ERROR;
2040}
2041
Eric Laurent4dc68062014-07-28 17:26:49 -07002042status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
2043 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07002044{
2045 ALOGV("stopInput() input %d", input);
2046 ssize_t index = mInputs.indexOfKey(input);
2047 if (index < 0) {
2048 ALOGW("stopInput() unknown input %d", input);
2049 return BAD_VALUE;
2050 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07002051 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07002052
Eric Laurent599c7582015-12-07 18:05:55 -08002053 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07002054 if (index < 0) {
2055 ALOGW("stopInput() unknown session %d on input %d", session, input);
2056 return BAD_VALUE;
2057 }
2058
Eric Laurent599c7582015-12-07 18:05:55 -08002059 if (audioSession->activeCount() == 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07002060 ALOGW("stopInput() input %d already stopped", input);
2061 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002062 }
2063
Eric Laurent599c7582015-12-07 18:05:55 -08002064 audioSession->changeActiveCount(-1);
Paul McLean466dc8e2015-04-17 13:15:36 -06002065
2066 // Routing?
2067 mInputRoutes.decRouteActivity(session);
2068
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07002069 if (audioSession->activeCount() == 0) {
Eric Laurent84332aa2016-01-28 22:19:18 +00002070
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07002071 if (inputDesc->isActive()) {
2072 setInputDevice(input, getNewInputDevice(inputDesc), false /* force */);
2073 } else {
2074 // if input maps to a dynamic policy with an activity listener, notify of state change
2075 if ((inputDesc->mPolicyMix != NULL)
2076 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
2077 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
2078 MIX_STATE_IDLE);
Eric Laurent84332aa2016-01-28 22:19:18 +00002079 }
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07002080
2081 // automatically disable the remote submix output when input is stopped if not
2082 // used by a policy mix of type MIX_TYPE_RECORDERS
2083 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
2084 String8 address = String8("");
2085 if (inputDesc->mPolicyMix == NULL) {
2086 address = String8("0");
2087 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
2088 address = inputDesc->mPolicyMix->mDeviceAddress;
2089 }
2090 if (address != "") {
2091 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2092 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2093 address, "remote-submix");
2094 }
Eric Laurent84332aa2016-01-28 22:19:18 +00002095 }
Eric Laurent84332aa2016-01-28 22:19:18 +00002096
Eric Laurentf7c50102016-09-30 15:19:43 -07002097 audio_devices_t device = inputDesc->mDevice;
Eric Laurentfb66dd92016-01-28 18:32:03 -08002098 resetInputDevice(input);
Eric Laurent84332aa2016-01-28 22:19:18 +00002099
Eric Laurentf7c50102016-09-30 15:19:43 -07002100 // indicate inactive capture to sound trigger service if stopping capture from a mic on
2101 // primary HW module
2102 audio_devices_t primaryInputDevices = availablePrimaryInputDevices();
2103 if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) &&
2104 mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) {
Eric Laurentfb66dd92016-01-28 18:32:03 -08002105 SoundTrigger::setCaptureState(false);
2106 }
2107 inputDesc->clearPreemptedSessions();
Eric Laurent84332aa2016-01-28 22:19:18 +00002108 }
Eric Laurente552edb2014-03-10 17:42:56 -07002109 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002110 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07002111}
2112
Eric Laurent4dc68062014-07-28 17:26:49 -07002113void AudioPolicyManager::releaseInput(audio_io_handle_t input,
2114 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07002115{
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002116
Eric Laurente552edb2014-03-10 17:42:56 -07002117 ALOGV("releaseInput() %d", input);
2118 ssize_t index = mInputs.indexOfKey(input);
2119 if (index < 0) {
2120 ALOGW("releaseInput() releasing unknown input %d", input);
2121 return;
2122 }
Paul McLean466dc8e2015-04-17 13:15:36 -06002123
2124 // Routing
2125 mInputRoutes.removeRoute(session);
2126
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002127 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
2128 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07002129
Eric Laurent599c7582015-12-07 18:05:55 -08002130 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
vivek mehta587b8df2017-01-31 14:58:44 -08002131 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07002132 ALOGW("releaseInput() unknown session %d on input %d", session, input);
2133 return;
2134 }
Eric Laurent599c7582015-12-07 18:05:55 -08002135
2136 if (audioSession->openCount() == 0) {
2137 ALOGW("releaseInput() invalid open count %d on session %d",
2138 audioSession->openCount(), session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002139 return;
2140 }
Eric Laurent599c7582015-12-07 18:05:55 -08002141
2142 if (audioSession->changeOpenCount(-1) == 0) {
2143 inputDesc->removeAudioSession(session);
2144 }
2145
Jean-Michel Trivi65bfe912015-12-04 15:56:47 -08002146 if (inputDesc->getOpenRefCount() > 0) {
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002147 ALOGV("releaseInput() exit > 0");
2148 return;
2149 }
2150
Eric Laurent05b90f82014-08-27 15:32:29 -07002151 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07002152 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07002153 ALOGV("releaseInput() exit");
2154}
2155
Eric Laurentd4692962014-05-05 18:13:44 -07002156void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07002157 bool patchRemoved = false;
2158
Eric Laurentd4692962014-05-05 18:13:44 -07002159 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07002160 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08002161 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07002162 if (patch_index >= 0) {
2163 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07002164 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07002165 mAudioPatches.removeItemsAt(patch_index);
2166 patchRemoved = true;
2167 }
Eric Laurentd4692962014-05-05 18:13:44 -07002168 mpClientInterface->closeInput(mInputs.keyAt(input_index));
2169 }
2170 mInputs.clear();
Chris Thornton52785f32016-02-09 17:28:49 -08002171 SoundTrigger::setCaptureState(false);
Eric Laurent6a94d692014-05-20 11:18:06 -07002172 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07002173
2174 if (patchRemoved) {
2175 mpClientInterface->onAudioPatchListUpdate();
2176 }
Eric Laurentd4692962014-05-05 18:13:44 -07002177}
2178
Eric Laurente0720872014-03-11 09:30:41 -07002179void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07002180 int indexMin,
2181 int indexMax)
2182{
2183 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002184 mVolumeCurves->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent28d09f02016-03-08 10:43:05 -08002185
2186 // initialize other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08002187 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
2188 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002189 continue;
2190 }
2191 mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08002192 }
Eric Laurente552edb2014-03-10 17:42:56 -07002193}
2194
Eric Laurente0720872014-03-11 09:30:41 -07002195status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01002196 int index,
2197 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07002198{
2199
François Gaffied1ab2bd2015-12-02 18:20:06 +01002200 if ((index < mVolumeCurves->getVolumeIndexMin(stream)) ||
2201 (index > mVolumeCurves->getVolumeIndexMax(stream))) {
Eric Laurente552edb2014-03-10 17:42:56 -07002202 return BAD_VALUE;
2203 }
2204 if (!audio_is_output_device(device)) {
2205 return BAD_VALUE;
2206 }
2207
2208 // Force max volume if stream cannot be muted
François Gaffied1ab2bd2015-12-02 18:20:06 +01002209 if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07002210
Eric Laurent1fd372e2016-04-06 14:23:53 -07002211 ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d",
Eric Laurente552edb2014-03-10 17:42:56 -07002212 stream, device, index);
2213
Eric Laurent28d09f02016-03-08 10:43:05 -08002214 // update other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08002215 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
2216 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002217 continue;
2218 }
2219 mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index);
2220 }
Eric Laurente552edb2014-03-10 17:42:56 -07002221
Eric Laurent1fd372e2016-04-06 14:23:53 -07002222 // update volume on all outputs and streams matching the following:
2223 // - The requested stream (or a stream matching for volume control) is active on the output
2224 // - The device (or devices) selected by the strategy corresponding to this stream includes
2225 // the requested device
2226 // - For non default requested device, currently selected device on the output is either the
2227 // requested device or one of the devices selected by the strategy
Eric Laurent5a2b6292016-04-14 18:05:57 -07002228 // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if
2229 // no specific device volume value exists for currently selected device.
Eric Laurente552edb2014-03-10 17:42:56 -07002230 status_t status = NO_ERROR;
2231 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002232 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
2233 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent794fde22016-03-11 09:50:45 -08002234 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
2235 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002236 continue;
Eric Laurente552edb2014-03-10 17:42:56 -07002237 }
Eric Laurentd3926fe2016-04-15 18:10:07 -07002238 if (!(desc->isStreamActive((audio_stream_type_t)curStream) ||
2239 (isInCall() && (curStream == AUDIO_STREAM_VOICE_CALL)))) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07002240 continue;
2241 }
Eric Laurent794fde22016-03-11 09:50:45 -08002242 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Jean-Michel Trivib7fdce62017-06-27 19:38:42 -07002243 audio_devices_t curStreamDevice = Volume::getDeviceForVolume(getDeviceForStrategy(
2244 curStrategy, false /*fromCache*/));
Eric Laurente76f29c2016-09-14 17:17:53 -07002245 if ((device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) &&
2246 ((curStreamDevice & device) == 0)) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07002247 continue;
2248 }
Eric Laurente76f29c2016-09-14 17:17:53 -07002249 bool applyVolume;
Eric Laurent5a2b6292016-04-14 18:05:57 -07002250 if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07002251 curStreamDevice |= device;
Eric Laurente76f29c2016-09-14 17:17:53 -07002252 applyVolume = (curDevice & curStreamDevice) != 0;
2253 } else {
2254 applyVolume = !mVolumeCurves->hasVolumeIndexForDevice(
Jean-Michel Trivib7fdce62017-06-27 19:38:42 -07002255 stream, curStreamDevice);
Eric Laurent1fd372e2016-04-06 14:23:53 -07002256 }
Eric Laurent28d09f02016-03-08 10:43:05 -08002257
Eric Laurente76f29c2016-09-14 17:17:53 -07002258 if (applyVolume) {
Eric Laurentdc462862016-07-19 12:29:53 -07002259 //FIXME: workaround for truncated touch sounds
2260 // delayed volume change for system stream to be removed when the problem is
2261 // handled by system UI
Eric Laurent28d09f02016-03-08 10:43:05 -08002262 status_t volStatus =
Eric Laurentdc462862016-07-19 12:29:53 -07002263 checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice,
2264 (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0);
Eric Laurent28d09f02016-03-08 10:43:05 -08002265 if (volStatus != NO_ERROR) {
2266 status = volStatus;
2267 }
2268 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08002269 }
Eric Laurente552edb2014-03-10 17:42:56 -07002270 }
2271 return status;
2272}
2273
Eric Laurente0720872014-03-11 09:30:41 -07002274status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07002275 int *index,
2276 audio_devices_t device)
2277{
2278 if (index == NULL) {
2279 return BAD_VALUE;
2280 }
2281 if (!audio_is_output_device(device)) {
2282 return BAD_VALUE;
2283 }
Eric Laurent5a2b6292016-04-14 18:05:57 -07002284 // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device corresponding to
Eric Laurente552edb2014-03-10 17:42:56 -07002285 // the strategy the stream belongs to.
Eric Laurent5a2b6292016-04-14 18:05:57 -07002286 if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurente552edb2014-03-10 17:42:56 -07002287 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
2288 }
François Gaffiedfd74092015-03-19 12:10:59 +01002289 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07002290
François Gaffied1ab2bd2015-12-02 18:20:06 +01002291 *index = mVolumeCurves->getVolumeIndex(stream, device);
Eric Laurente552edb2014-03-10 17:42:56 -07002292 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
2293 return NO_ERROR;
2294}
2295
Eric Laurent36829f92017-04-07 19:04:42 -07002296audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects()
Eric Laurente552edb2014-03-10 17:42:56 -07002297{
2298 // select one output among several suitable for global effects.
2299 // The priority is as follows:
2300 // 1: An offloaded output. If the effect ends up not being offloadable,
2301 // AudioFlinger will invalidate the track and the offloaded output
2302 // will be closed causing the effect to be moved to a PCM output.
2303 // 2: A deep buffer output
Eric Laurent36829f92017-04-07 19:04:42 -07002304 // 3: The primary output
2305 // 4: the first output in the list
Eric Laurente552edb2014-03-10 17:42:56 -07002306
Eric Laurent3b73df72014-03-11 09:06:29 -07002307 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07002308 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent36829f92017-04-07 19:04:42 -07002309 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
Eric Laurente552edb2014-03-10 17:42:56 -07002310
Eric Laurent36829f92017-04-07 19:04:42 -07002311 if (outputs.size() == 0) {
2312 return AUDIO_IO_HANDLE_NONE;
2313 }
Eric Laurente552edb2014-03-10 17:42:56 -07002314
Eric Laurent36829f92017-04-07 19:04:42 -07002315 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
2316 bool activeOnly = true;
2317
2318 while (output == AUDIO_IO_HANDLE_NONE) {
2319 audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE;
2320 audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE;
2321 audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE;
2322
2323 for (size_t i = 0; i < outputs.size(); i++) {
2324 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
2325 if (activeOnly && !desc->isStreamActive(AUDIO_STREAM_MUSIC)) {
2326 continue;
2327 }
2328 ALOGV("selectOutputForMusicEffects activeOnly %d outputs[%zu] flags 0x%08x",
2329 activeOnly, i, desc->mFlags);
2330 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
2331 outputOffloaded = outputs[i];
2332 }
2333 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
2334 outputDeepBuffer = outputs[i];
2335 }
2336 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) {
2337 outputPrimary = outputs[i];
2338 }
2339 }
2340 if (outputOffloaded != AUDIO_IO_HANDLE_NONE) {
2341 output = outputOffloaded;
2342 } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) {
2343 output = outputDeepBuffer;
2344 } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) {
2345 output = outputPrimary;
2346 } else {
2347 output = outputs[0];
2348 }
2349 activeOnly = false;
2350 }
2351
2352 if (output != mMusicEffectOutput) {
2353 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output);
2354 mMusicEffectOutput = output;
2355 }
2356
2357 ALOGV("selectOutputForMusicEffects selected output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -07002358 return output;
2359}
2360
Eric Laurent36829f92017-04-07 19:04:42 -07002361audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused)
2362{
2363 return selectOutputForMusicEffects();
2364}
2365
Eric Laurente0720872014-03-11 09:30:41 -07002366status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07002367 audio_io_handle_t io,
2368 uint32_t strategy,
2369 int session,
2370 int id)
2371{
2372 ssize_t index = mOutputs.indexOfKey(io);
2373 if (index < 0) {
2374 index = mInputs.indexOfKey(io);
2375 if (index < 0) {
2376 ALOGW("registerEffect() unknown io %d", io);
2377 return INVALID_OPERATION;
2378 }
2379 }
François Gaffie45ed3b02015-03-19 10:35:14 +01002380 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07002381}
2382
Eric Laurentc75307b2015-03-17 15:29:32 -07002383bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
2384{
Eric Laurent28d09f02016-03-08 10:43:05 -08002385 bool active = false;
Eric Laurent794fde22016-03-11 09:50:45 -08002386 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) {
2387 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002388 continue;
2389 }
2390 active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs);
2391 }
Eric Laurent28d09f02016-03-08 10:43:05 -08002392 return active;
Eric Laurentc75307b2015-03-17 15:29:32 -07002393}
2394
2395bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
2396{
2397 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
2398}
2399
Eric Laurente0720872014-03-11 09:30:41 -07002400bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07002401{
2402 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002403 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002404 if (inputDescriptor->isSourceActive(source)) {
Eric Laurente552edb2014-03-10 17:42:56 -07002405 return true;
2406 }
2407 }
2408 return false;
2409}
2410
Eric Laurent275e8e92014-11-30 15:14:47 -08002411// Register a list of custom mixes with their attributes and format.
2412// When a mix is registered, corresponding input and output profiles are
2413// added to the remote submix hw module. The profile contains only the
2414// parameters (sampling rate, format...) specified by the mix.
2415// The corresponding input remote submix device is also connected.
2416//
2417// When a remote submix device is connected, the address is checked to select the
2418// appropriate profile and the corresponding input or output stream is opened.
2419//
2420// When capture starts, getInputForAttr() will:
2421// - 1 look for a mix matching the address passed in attribtutes tags if any
2422// - 2 if none found, getDeviceForInputSource() will:
2423// - 2.1 look for a mix matching the attributes source
2424// - 2.2 if none found, default to device selection by policy rules
2425// At this time, the corresponding output remote submix device is also connected
2426// and active playback use cases can be transferred to this mix if needed when reconnecting
2427// after AudioTracks are invalidated
2428//
2429// When playback starts, getOutputForAttr() will:
2430// - 1 look for a mix matching the address passed in attribtutes tags if any
2431// - 2 if none found, look for a mix matching the attributes usage
2432// - 3 if none found, default to device and output selection by policy rules.
2433
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07002434status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes)
Eric Laurent275e8e92014-11-30 15:14:47 -08002435{
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002436 ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size());
2437 status_t res = NO_ERROR;
2438
2439 sp<HwModule> rSubmixModule;
2440 // examine each mix's route type
2441 for (size_t i = 0; i < mixes.size(); i++) {
2442 // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination
2443 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) {
2444 res = INVALID_OPERATION;
Eric Laurent275e8e92014-11-30 15:14:47 -08002445 break;
2446 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002447 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
2448 // Loop back through "remote submix"
2449 if (rSubmixModule == 0) {
2450 for (size_t j = 0; i < mHwModules.size(); j++) {
2451 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2452 && mHwModules[j]->mHandle != 0) {
2453 rSubmixModule = mHwModules[j];
2454 break;
2455 }
2456 }
2457 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002458
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002459 ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size());
Eric Laurent275e8e92014-11-30 15:14:47 -08002460
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002461 if (rSubmixModule == 0) {
2462 ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", i);
2463 res = INVALID_OPERATION;
2464 break;
2465 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002466
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002467 String8 address = mixes[i].mDeviceAddress;
François Gaffie036e1e92015-03-19 10:16:24 +01002468
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002469 if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) {
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002470 ALOGE(" Error registering mix %zu for address %s", i, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002471 res = INVALID_OPERATION;
2472 break;
2473 }
2474 audio_config_t outputConfig = mixes[i].mFormat;
2475 audio_config_t inputConfig = mixes[i].mFormat;
2476 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
2477 // stereo and let audio flinger do the channel conversion if needed.
2478 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2479 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
2480 rSubmixModule->addOutputProfile(address, &outputConfig,
2481 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
2482 rSubmixModule->addInputProfile(address, &inputConfig,
2483 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01002484
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002485 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
2486 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2487 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2488 address.string(), "remote-submix");
2489 } else {
2490 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2491 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2492 address.string(), "remote-submix");
2493 }
2494 } else if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002495 String8 address = mixes[i].mDeviceAddress;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002496 audio_devices_t device = mixes[i].mDeviceType;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002497 ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s",
2498 i, mixes.size(), device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002499
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002500 bool foundOutput = false;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002501 for (size_t j = 0 ; j < mOutputs.size() ; j++) {
2502 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j);
2503 sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle());
2504 if ((patch != 0) && (patch->mPatch.num_sinks != 0)
2505 && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE)
2506 && (patch->mPatch.sinks[0].ext.device.type == device)
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002507 && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(),
2508 AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002509 if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) {
2510 res = INVALID_OPERATION;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002511 } else {
2512 foundOutput = true;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002513 }
2514 break;
2515 }
2516 }
2517
2518 if (res != NO_ERROR) {
2519 ALOGE(" Error registering mix %zu for device 0x%X addr %s",
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002520 i, device, address.string());
2521 res = INVALID_OPERATION;
2522 break;
2523 } else if (!foundOutput) {
2524 ALOGE(" Output not found for mix %zu for device 0x%X addr %s",
2525 i, device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002526 res = INVALID_OPERATION;
2527 break;
2528 }
Eric Laurentc722f302014-12-10 11:21:49 -08002529 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002530 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002531 if (res != NO_ERROR) {
2532 unregisterPolicyMixes(mixes);
2533 }
2534 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002535}
2536
2537status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
2538{
Eric Laurent7b279bb2015-12-14 10:18:23 -08002539 ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002540 status_t res = NO_ERROR;
2541 sp<HwModule> rSubmixModule;
2542 // examine each mix's route type
Eric Laurent275e8e92014-11-30 15:14:47 -08002543 for (size_t i = 0; i < mixes.size(); i++) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002544 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
François Gaffie036e1e92015-03-19 10:16:24 +01002545
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002546 if (rSubmixModule == 0) {
2547 for (size_t j = 0; i < mHwModules.size(); j++) {
2548 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2549 && mHwModules[j]->mHandle != 0) {
2550 rSubmixModule = mHwModules[j];
2551 break;
2552 }
2553 }
2554 }
2555 if (rSubmixModule == 0) {
2556 res = INVALID_OPERATION;
2557 continue;
2558 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002559
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002560 String8 address = mixes[i].mDeviceAddress;
Eric Laurent275e8e92014-11-30 15:14:47 -08002561
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002562 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
2563 res = INVALID_OPERATION;
2564 continue;
2565 }
2566
2567 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
2568 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2569 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2570 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2571 address.string(), "remote-submix");
2572 }
2573 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
2574 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2575 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2576 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2577 address.string(), "remote-submix");
2578 }
2579 rSubmixModule->removeOutputProfile(address);
2580 rSubmixModule->removeInputProfile(address);
2581
2582 } if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
2583 if (mPolicyMixes.unregisterMix(mixes[i].mDeviceAddress) != NO_ERROR) {
2584 res = INVALID_OPERATION;
2585 continue;
2586 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002587 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002588 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002589 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002590}
2591
Eric Laurente552edb2014-03-10 17:42:56 -07002592
Eric Laurente0720872014-03-11 09:30:41 -07002593status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07002594{
2595 const size_t SIZE = 256;
2596 char buffer[SIZE];
2597 String8 result;
2598
2599 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
2600 result.append(buffer);
2601
Eric Laurent87ffa392015-05-22 10:32:38 -07002602 snprintf(buffer, SIZE, " Primary Output: %d\n",
2603 hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE);
Eric Laurente552edb2014-03-10 17:42:56 -07002604 result.append(buffer);
Mikhail Naganov0d6a0332016-04-19 17:12:38 -07002605 std::string stateLiteral;
2606 AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral);
2607 snprintf(buffer, SIZE, " Phone state: %s\n", stateLiteral.c_str());
Eric Laurente552edb2014-03-10 17:42:56 -07002608 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002609 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002610 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
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 media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
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 record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07002615 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002616 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07002617 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002618 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07002619 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002620 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002621 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002622 result.append(buffer);
Phil Burk09bc4612016-02-24 15:58:15 -08002623 snprintf(buffer, SIZE, " Force use for encoded surround output %d\n",
2624 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND));
2625 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002626 snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available");
2627 result.append(buffer);
Andy Hung2ddee192015-12-18 17:34:44 -08002628 snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off");
2629 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002630
François Gaffie2110e042015-03-24 08:41:51 +01002631 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07002632
François Gaffie112b0af2015-11-19 16:13:25 +01002633 mAvailableOutputDevices.dump(fd, String8("Available output"));
2634 mAvailableInputDevices.dump(fd, String8("Available input"));
François Gaffie53615e22015-03-19 09:24:12 +01002635 mHwModules.dump(fd);
2636 mOutputs.dump(fd);
2637 mInputs.dump(fd);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002638 mVolumeCurves->dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01002639 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002640 mAudioPatches.dump(fd);
Mikhail Naganov44344b02016-12-13 11:21:02 -08002641 mPolicyMixes.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07002642
2643 return NO_ERROR;
2644}
2645
2646// This function checks for the parameters which can be offloaded.
2647// This can be enhanced depending on the capability of the DSP and policy
2648// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002649bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002650{
2651 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002652 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002653 offloadInfo.sample_rate, offloadInfo.channel_mask,
2654 offloadInfo.format,
2655 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2656 offloadInfo.has_video);
2657
Andy Hung2ddee192015-12-18 17:34:44 -08002658 if (mMasterMono) {
2659 return false; // no offloading if mono is set.
2660 }
2661
Eric Laurente552edb2014-03-10 17:42:56 -07002662 // Check if offload has been disabled
2663 char propValue[PROPERTY_VALUE_MAX];
2664 if (property_get("audio.offload.disable", propValue, "0")) {
2665 if (atoi(propValue) != 0) {
2666 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2667 return false;
2668 }
2669 }
2670
2671 // Check if stream type is music, then only allow offload as of now.
2672 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2673 {
2674 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2675 return false;
2676 }
2677
2678 //TODO: enable audio offloading with video when ready
Andy Hung08945c42015-05-31 21:36:46 -07002679 const bool allowOffloadWithVideo =
2680 property_get_bool("audio.offload.video", false /* default_value */);
2681 if (offloadInfo.has_video && !allowOffloadWithVideo) {
Eric Laurente552edb2014-03-10 17:42:56 -07002682 ALOGV("isOffloadSupported: has_video == true, returning false");
2683 return false;
2684 }
2685
2686 //If duration is less than minimum value defined in property, return false
2687 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2688 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2689 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2690 return false;
2691 }
2692 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2693 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2694 return false;
2695 }
2696
2697 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2698 // creating an offloaded track and tearing it down immediately after start when audioflinger
2699 // detects there is an active non offloadable effect.
2700 // FIXME: We should check the audio session here but we do not have it in this context.
2701 // This may prevent offloading in rare situations where effects are left active by apps
2702 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002703 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002704 return false;
2705 }
2706
2707 // See if there is a profile to support this.
2708 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002709 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002710 offloadInfo.sample_rate,
2711 offloadInfo.format,
2712 offloadInfo.channel_mask,
2713 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002714 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2715 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002716}
2717
Eric Laurent6a94d692014-05-20 11:18:06 -07002718status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2719 audio_port_type_t type,
2720 unsigned int *num_ports,
2721 struct audio_port *ports,
2722 unsigned int *generation)
2723{
2724 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2725 generation == NULL) {
2726 return BAD_VALUE;
2727 }
2728 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2729 if (ports == NULL) {
2730 *num_ports = 0;
2731 }
2732
2733 size_t portsWritten = 0;
2734 size_t portsMax = *num_ports;
2735 *num_ports = 0;
2736 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002737 // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB
2738 // as they are used by stub HALs by convention
Eric Laurent6a94d692014-05-20 11:18:06 -07002739 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002740 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
2741 if (mAvailableOutputDevices[i]->type() == AUDIO_DEVICE_OUT_STUB) {
2742 continue;
2743 }
2744 if (portsWritten < portsMax) {
2745 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2746 }
2747 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002748 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002749 }
2750 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002751 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
2752 if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_STUB) {
2753 continue;
2754 }
2755 if (portsWritten < portsMax) {
2756 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2757 }
2758 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002759 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002760 }
2761 }
2762 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2763 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2764 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2765 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2766 }
2767 *num_ports += mInputs.size();
2768 }
2769 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002770 size_t numOutputs = 0;
2771 for (size_t i = 0; i < mOutputs.size(); i++) {
2772 if (!mOutputs[i]->isDuplicated()) {
2773 numOutputs++;
2774 if (portsWritten < portsMax) {
2775 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2776 }
2777 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002778 }
Eric Laurent84c70242014-06-23 08:46:27 -07002779 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002780 }
2781 }
2782 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002783 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002784 return NO_ERROR;
2785}
2786
2787status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2788{
2789 return NO_ERROR;
2790}
2791
Eric Laurent6a94d692014-05-20 11:18:06 -07002792status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2793 audio_patch_handle_t *handle,
2794 uid_t uid)
2795{
2796 ALOGV("createAudioPatch()");
2797
2798 if (handle == NULL || patch == NULL) {
2799 return BAD_VALUE;
2800 }
2801 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2802
Eric Laurent874c42872014-08-08 15:13:39 -07002803 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2804 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2805 return BAD_VALUE;
2806 }
2807 // only one source per audio patch supported for now
2808 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002809 return INVALID_OPERATION;
2810 }
Eric Laurent874c42872014-08-08 15:13:39 -07002811
2812 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002813 return INVALID_OPERATION;
2814 }
Eric Laurent874c42872014-08-08 15:13:39 -07002815 for (size_t i = 0; i < patch->num_sinks; i++) {
2816 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2817 return INVALID_OPERATION;
2818 }
2819 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002820
2821 sp<AudioPatch> patchDesc;
2822 ssize_t index = mAudioPatches.indexOfKey(*handle);
2823
Eric Laurent6a94d692014-05-20 11:18:06 -07002824 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2825 patch->sources[0].role,
2826 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002827#if LOG_NDEBUG == 0
2828 for (size_t i = 0; i < patch->num_sinks; i++) {
Eric Laurent7b279bb2015-12-14 10:18:23 -08002829 ALOGV("createAudioPatch sink %zu: id %d role %d type %d", i, patch->sinks[i].id,
Eric Laurent874c42872014-08-08 15:13:39 -07002830 patch->sinks[i].role,
2831 patch->sinks[i].type);
2832 }
2833#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002834
2835 if (index >= 0) {
2836 patchDesc = mAudioPatches.valueAt(index);
2837 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2838 mUidCached, patchDesc->mUid, uid);
2839 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2840 return INVALID_OPERATION;
2841 }
2842 } else {
Glenn Kastena13cde92016-03-28 15:26:02 -07002843 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07002844 }
2845
2846 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002847 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002848 if (outputDesc == NULL) {
2849 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2850 return BAD_VALUE;
2851 }
Eric Laurent84c70242014-06-23 08:46:27 -07002852 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2853 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002854 if (patchDesc != 0) {
2855 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2856 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2857 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2858 return BAD_VALUE;
2859 }
2860 }
Eric Laurent874c42872014-08-08 15:13:39 -07002861 DeviceVector devices;
2862 for (size_t i = 0; i < patch->num_sinks; i++) {
2863 // Only support mix to devices connection
2864 // TODO add support for mix to mix connection
2865 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2866 ALOGV("createAudioPatch() source mix but sink is not a device");
2867 return INVALID_OPERATION;
2868 }
2869 sp<DeviceDescriptor> devDesc =
2870 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2871 if (devDesc == 0) {
2872 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2873 return BAD_VALUE;
2874 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002875
François Gaffie53615e22015-03-19 09:24:12 +01002876 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002877 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002878 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002879 NULL, // updatedSamplingRate
2880 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002881 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002882 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002883 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002884 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002885 ALOGV("createAudioPatch() profile not supported for device %08x",
2886 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002887 return INVALID_OPERATION;
2888 }
2889 devices.add(devDesc);
2890 }
2891 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002892 return INVALID_OPERATION;
2893 }
Eric Laurent874c42872014-08-08 15:13:39 -07002894
Eric Laurent6a94d692014-05-20 11:18:06 -07002895 // TODO: reconfigure output format and channels here
2896 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002897 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002898 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002899 index = mAudioPatches.indexOfKey(*handle);
2900 if (index >= 0) {
2901 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2902 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2903 }
2904 patchDesc = mAudioPatches.valueAt(index);
2905 patchDesc->mUid = uid;
2906 ALOGV("createAudioPatch() success");
2907 } else {
2908 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2909 return INVALID_OPERATION;
2910 }
2911 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2912 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2913 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002914 // only one sink supported when connecting an input device to a mix
2915 if (patch->num_sinks > 1) {
2916 return INVALID_OPERATION;
2917 }
François Gaffie53615e22015-03-19 09:24:12 +01002918 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002919 if (inputDesc == NULL) {
2920 return BAD_VALUE;
2921 }
2922 if (patchDesc != 0) {
2923 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2924 return BAD_VALUE;
2925 }
2926 }
2927 sp<DeviceDescriptor> devDesc =
2928 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2929 if (devDesc == 0) {
2930 return BAD_VALUE;
2931 }
2932
François Gaffie53615e22015-03-19 09:24:12 +01002933 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002934 devDesc->mAddress,
2935 patch->sinks[0].sample_rate,
2936 NULL, /*updatedSampleRate*/
2937 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002938 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002939 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002940 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002941 // FIXME for the parameter type,
2942 // and the NONE
2943 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002944 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002945 return INVALID_OPERATION;
2946 }
2947 // TODO: reconfigure output format and channels here
2948 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002949 devDesc->type(), inputDesc->mIoHandle);
2950 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002951 index = mAudioPatches.indexOfKey(*handle);
2952 if (index >= 0) {
2953 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2954 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2955 }
2956 patchDesc = mAudioPatches.valueAt(index);
2957 patchDesc->mUid = uid;
2958 ALOGV("createAudioPatch() success");
2959 } else {
2960 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2961 return INVALID_OPERATION;
2962 }
2963 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2964 // device to device connection
2965 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002966 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002967 return BAD_VALUE;
2968 }
2969 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002970 sp<DeviceDescriptor> srcDeviceDesc =
2971 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002972 if (srcDeviceDesc == 0) {
2973 return BAD_VALUE;
2974 }
Eric Laurent874c42872014-08-08 15:13:39 -07002975
Eric Laurent6a94d692014-05-20 11:18:06 -07002976 //update source and sink with our own data as the data passed in the patch may
2977 // be incomplete.
2978 struct audio_patch newPatch = *patch;
2979 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002980
Eric Laurent874c42872014-08-08 15:13:39 -07002981 for (size_t i = 0; i < patch->num_sinks; i++) {
2982 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2983 ALOGV("createAudioPatch() source device but one sink is not a device");
2984 return INVALID_OPERATION;
2985 }
2986
2987 sp<DeviceDescriptor> sinkDeviceDesc =
2988 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2989 if (sinkDeviceDesc == 0) {
2990 return BAD_VALUE;
2991 }
2992 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2993
Eric Laurent3bcf8592015-04-03 12:13:24 -07002994 // create a software bridge in PatchPanel if:
2995 // - source and sink devices are on differnt HW modules OR
2996 // - audio HAL version is < 3.0
Eric Laurentfb66dd92016-01-28 18:32:03 -08002997 if (!srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) ||
Mikhail Naganov9ee05402016-10-13 15:58:17 -07002998 (srcDeviceDesc->mModule->getHalVersionMajor() < 3)) {
Eric Laurent3bcf8592015-04-03 12:13:24 -07002999 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07003000 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07003001 return INVALID_OPERATION;
3002 }
Eric Laurent874c42872014-08-08 15:13:39 -07003003 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01003004 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07003005 // if the sink device is reachable via an opened output stream, request to go via
3006 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07003007 audio_io_handle_t output = selectOutput(outputs,
3008 AUDIO_OUTPUT_FLAG_NONE,
3009 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07003010 if (output != AUDIO_IO_HANDLE_NONE) {
3011 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
3012 if (outputDesc->isDuplicated()) {
3013 return INVALID_OPERATION;
3014 }
3015 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07003016 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07003017 newPatch.num_sources = 2;
3018 }
Eric Laurent83b88082014-06-20 18:31:16 -07003019 }
Eric Laurent6a94d692014-05-20 11:18:06 -07003020 }
3021 // TODO: check from routing capabilities in config file and other conflicting patches
3022
3023 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3024 if (index >= 0) {
3025 afPatchHandle = patchDesc->mAfPatchHandle;
3026 }
3027
3028 status_t status = mpClientInterface->createAudioPatch(&newPatch,
3029 &afPatchHandle,
3030 0);
3031 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
3032 status, afPatchHandle);
3033 if (status == NO_ERROR) {
3034 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01003035 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07003036 addAudioPatch(patchDesc->mHandle, patchDesc);
3037 } else {
3038 patchDesc->mPatch = newPatch;
3039 }
3040 patchDesc->mAfPatchHandle = afPatchHandle;
3041 *handle = patchDesc->mHandle;
3042 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07003043 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07003044 } else {
3045 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
3046 status);
3047 return INVALID_OPERATION;
3048 }
3049 } else {
3050 return BAD_VALUE;
3051 }
3052 } else {
3053 return BAD_VALUE;
3054 }
3055 return NO_ERROR;
3056}
3057
3058status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
3059 uid_t uid)
3060{
3061 ALOGV("releaseAudioPatch() patch %d", handle);
3062
3063 ssize_t index = mAudioPatches.indexOfKey(handle);
3064
3065 if (index < 0) {
3066 return BAD_VALUE;
3067 }
3068 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3069 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
3070 mUidCached, patchDesc->mUid, uid);
3071 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
3072 return INVALID_OPERATION;
3073 }
3074
3075 struct audio_patch *patch = &patchDesc->mPatch;
3076 patchDesc->mUid = mUidCached;
3077 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003078 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07003079 if (outputDesc == NULL) {
3080 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
3081 return BAD_VALUE;
3082 }
3083
Eric Laurentc75307b2015-03-17 15:29:32 -07003084 setOutputDevice(outputDesc,
3085 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07003086 true,
3087 0,
3088 NULL);
3089 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
3090 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01003091 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07003092 if (inputDesc == NULL) {
3093 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
3094 return BAD_VALUE;
3095 }
3096 setInputDevice(inputDesc->mIoHandle,
Eric Laurentfb66dd92016-01-28 18:32:03 -08003097 getNewInputDevice(inputDesc),
Eric Laurent6a94d692014-05-20 11:18:06 -07003098 true,
3099 NULL);
3100 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003101 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3102 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
3103 status, patchDesc->mAfPatchHandle);
3104 removeAudioPatch(patchDesc->mHandle);
3105 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07003106 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07003107 } else {
3108 return BAD_VALUE;
3109 }
3110 } else {
3111 return BAD_VALUE;
3112 }
3113 return NO_ERROR;
3114}
3115
3116status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
3117 struct audio_patch *patches,
3118 unsigned int *generation)
3119{
François Gaffie53615e22015-03-19 09:24:12 +01003120 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003121 return BAD_VALUE;
3122 }
Eric Laurent6a94d692014-05-20 11:18:06 -07003123 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01003124 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07003125}
3126
Eric Laurente1715a42014-05-20 11:30:42 -07003127status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07003128{
Eric Laurente1715a42014-05-20 11:30:42 -07003129 ALOGV("setAudioPortConfig()");
3130
3131 if (config == NULL) {
3132 return BAD_VALUE;
3133 }
3134 ALOGV("setAudioPortConfig() on port handle %d", config->id);
3135 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07003136 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
3137 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07003138 }
3139
Eric Laurenta121f902014-06-03 13:32:54 -07003140 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07003141 if (config->type == AUDIO_PORT_TYPE_MIX) {
3142 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003143 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07003144 if (outputDesc == NULL) {
3145 return BAD_VALUE;
3146 }
Eric Laurent84c70242014-06-23 08:46:27 -07003147 ALOG_ASSERT(!outputDesc->isDuplicated(),
3148 "setAudioPortConfig() called on duplicated output %d",
3149 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07003150 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07003151 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01003152 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07003153 if (inputDesc == NULL) {
3154 return BAD_VALUE;
3155 }
Eric Laurenta121f902014-06-03 13:32:54 -07003156 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07003157 } else {
3158 return BAD_VALUE;
3159 }
3160 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
3161 sp<DeviceDescriptor> deviceDesc;
3162 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
3163 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
3164 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
3165 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
3166 } else {
3167 return BAD_VALUE;
3168 }
3169 if (deviceDesc == NULL) {
3170 return BAD_VALUE;
3171 }
Eric Laurenta121f902014-06-03 13:32:54 -07003172 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07003173 } else {
3174 return BAD_VALUE;
3175 }
3176
Eric Laurenta121f902014-06-03 13:32:54 -07003177 struct audio_port_config backupConfig;
3178 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
3179 if (status == NO_ERROR) {
3180 struct audio_port_config newConfig;
3181 audioPortConfig->toAudioPortConfig(&newConfig, config);
3182 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07003183 }
Eric Laurenta121f902014-06-03 13:32:54 -07003184 if (status != NO_ERROR) {
3185 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07003186 }
Eric Laurente1715a42014-05-20 11:30:42 -07003187
3188 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07003189}
3190
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003191void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
3192{
Eric Laurentd60560a2015-04-10 11:31:20 -07003193 clearAudioSources(uid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003194 clearAudioPatches(uid);
3195 clearSessionRoutes(uid);
3196}
3197
Eric Laurent6a94d692014-05-20 11:18:06 -07003198void AudioPolicyManager::clearAudioPatches(uid_t uid)
3199{
Eric Laurent0add0fd2014-12-04 18:58:14 -08003200 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003201 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
3202 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08003203 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07003204 }
3205 }
3206}
3207
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003208void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
3209 audio_io_handle_t ouptutToSkip)
3210{
3211 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
3212 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
3213 for (size_t j = 0; j < mOutputs.size(); j++) {
3214 if (mOutputs.keyAt(j) == ouptutToSkip) {
3215 continue;
3216 }
3217 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
3218 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
3219 continue;
3220 }
3221 // If the default device for this strategy is on another output mix,
3222 // invalidate all tracks in this strategy to force re connection.
3223 // Otherwise select new device on the output mix.
3224 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
Eric Laurent794fde22016-03-11 09:50:45 -08003225 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003226 if (getStrategy((audio_stream_type_t)stream) == strategy) {
3227 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
3228 }
3229 }
3230 } else {
3231 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
3232 setOutputDevice(outputDesc, newDevice, false);
3233 }
3234 }
3235}
3236
3237void AudioPolicyManager::clearSessionRoutes(uid_t uid)
3238{
3239 // remove output routes associated with this uid
3240 SortedVector<routing_strategy> affectedStrategies;
3241 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
3242 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
3243 if (route->mUid == uid) {
3244 mOutputRoutes.removeItemsAt(i);
3245 if (route->mDeviceDescriptor != 0) {
3246 affectedStrategies.add(getStrategy(route->mStreamType));
3247 }
3248 }
3249 }
3250 // reroute outputs if necessary
3251 for (size_t i = 0; i < affectedStrategies.size(); i++) {
3252 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
3253 }
3254
3255 // remove input routes associated with this uid
3256 SortedVector<audio_source_t> affectedSources;
3257 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
3258 sp<SessionRoute> route = mInputRoutes.valueAt(i);
3259 if (route->mUid == uid) {
3260 mInputRoutes.removeItemsAt(i);
3261 if (route->mDeviceDescriptor != 0) {
3262 affectedSources.add(route->mSource);
3263 }
3264 }
3265 }
3266 // reroute inputs if necessary
3267 SortedVector<audio_io_handle_t> inputsToClose;
3268 for (size_t i = 0; i < mInputs.size(); i++) {
3269 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08003270 if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003271 inputsToClose.add(inputDesc->mIoHandle);
3272 }
3273 }
3274 for (size_t i = 0; i < inputsToClose.size(); i++) {
3275 closeInput(inputsToClose[i]);
3276 }
3277}
3278
Eric Laurentd60560a2015-04-10 11:31:20 -07003279void AudioPolicyManager::clearAudioSources(uid_t uid)
3280{
3281 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
3282 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3283 if (sourceDesc->mUid == uid) {
3284 stopAudioSource(mAudioSources.keyAt(i));
3285 }
3286 }
3287}
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003288
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07003289status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
3290 audio_io_handle_t *ioHandle,
3291 audio_devices_t *device)
3292{
Glenn Kastenf0c6d7d2016-02-26 10:44:04 -08003293 *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
3294 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
Eric Laurentc73ca6e2014-12-12 14:34:22 -08003295 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07003296
François Gaffiedf372692015-03-19 10:43:27 +01003297 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07003298}
3299
Eric Laurentd60560a2015-04-10 11:31:20 -07003300status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source,
3301 const audio_attributes_t *attributes,
Glenn Kasten559d4392016-03-29 13:42:57 -07003302 audio_patch_handle_t *handle,
Eric Laurentd60560a2015-04-10 11:31:20 -07003303 uid_t uid)
Eric Laurent554a2772015-04-10 11:29:24 -07003304{
Eric Laurentd60560a2015-04-10 11:31:20 -07003305 ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle);
3306 if (source == NULL || attributes == NULL || handle == NULL) {
3307 return BAD_VALUE;
3308 }
3309
Glenn Kasten559d4392016-03-29 13:42:57 -07003310 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentd60560a2015-04-10 11:31:20 -07003311
3312 if (source->role != AUDIO_PORT_ROLE_SOURCE ||
3313 source->type != AUDIO_PORT_TYPE_DEVICE) {
3314 ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type);
3315 return INVALID_OPERATION;
3316 }
3317
3318 sp<DeviceDescriptor> srcDeviceDesc =
3319 mAvailableInputDevices.getDevice(source->ext.device.type,
3320 String8(source->ext.device.address));
3321 if (srcDeviceDesc == 0) {
3322 ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type);
3323 return BAD_VALUE;
3324 }
3325 sp<AudioSourceDescriptor> sourceDesc =
3326 new AudioSourceDescriptor(srcDeviceDesc, attributes, uid);
3327
3328 struct audio_patch dummyPatch;
3329 sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid);
3330 sourceDesc->mPatchDesc = patchDesc;
3331
3332 status_t status = connectAudioSource(sourceDesc);
3333 if (status == NO_ERROR) {
3334 mAudioSources.add(sourceDesc->getHandle(), sourceDesc);
3335 *handle = sourceDesc->getHandle();
3336 }
3337 return status;
3338}
3339
3340status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3341{
3342 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3343
3344 // make sure we only have one patch per source.
3345 disconnectAudioSource(sourceDesc);
3346
3347 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
Eric Laurent28d09f02016-03-08 10:43:05 -08003348 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003349 sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice;
3350
3351 audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true);
3352 sp<DeviceDescriptor> sinkDeviceDesc =
3353 mAvailableOutputDevices.getDevice(sinkDevice, String8(""));
3354
3355 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3356 struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch;
3357
3358 if (srcDeviceDesc->getAudioPort()->mModule->getHandle() ==
3359 sinkDeviceDesc->getAudioPort()->mModule->getHandle() &&
Mikhail Naganov9ee05402016-10-13 15:58:17 -07003360 srcDeviceDesc->getAudioPort()->mModule->getHalVersionMajor() >= 3 &&
Eric Laurentd60560a2015-04-10 11:31:20 -07003361 srcDeviceDesc->getAudioPort()->mGains.size() > 0) {
3362 ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__);
3363 // create patch between src device and output device
3364 // create Hwoutput and add to mHwOutputs
3365 } else {
3366 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs);
3367 audio_io_handle_t output =
3368 selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID);
3369 if (output == AUDIO_IO_HANDLE_NONE) {
3370 ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice);
3371 return INVALID_OPERATION;
3372 }
3373 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
3374 if (outputDesc->isDuplicated()) {
3375 ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice);
3376 return INVALID_OPERATION;
3377 }
3378 // create a special patch with no sink and two sources:
3379 // - the second source indicates to PatchPanel through which output mix this patch should
3380 // be connected as well as the stream type for volume control
3381 // - the sink is defined by whatever output device is currently selected for the output
3382 // though which this patch is routed.
3383 patch->num_sinks = 0;
3384 patch->num_sources = 2;
3385 srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL);
3386 outputDesc->toAudioPortConfig(&patch->sources[1], NULL);
3387 patch->sources[1].ext.mix.usecase.stream = stream;
3388 status_t status = mpClientInterface->createAudioPatch(patch,
3389 &afPatchHandle,
3390 0);
3391 ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__,
3392 status, afPatchHandle);
3393 if (status != NO_ERROR) {
3394 ALOGW("%s patch panel could not connect device patch, error %d",
3395 __FUNCTION__, status);
3396 return INVALID_OPERATION;
3397 }
3398 uint32_t delayMs = 0;
Eric Laurentc40d9692016-04-13 19:14:13 -07003399 status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs);
Eric Laurentd60560a2015-04-10 11:31:20 -07003400
3401 if (status != NO_ERROR) {
3402 mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0);
3403 return status;
3404 }
3405 sourceDesc->mSwOutput = outputDesc;
3406 if (delayMs != 0) {
3407 usleep(delayMs * 1000);
3408 }
3409 }
3410
3411 sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle;
3412 addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc);
3413
3414 return NO_ERROR;
Eric Laurent554a2772015-04-10 11:29:24 -07003415}
3416
Glenn Kasten559d4392016-03-29 13:42:57 -07003417status_t AudioPolicyManager::stopAudioSource(audio_patch_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07003418{
Eric Laurentd60560a2015-04-10 11:31:20 -07003419 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle);
3420 ALOGV("%s handle %d", __FUNCTION__, handle);
3421 if (sourceDesc == 0) {
3422 ALOGW("%s unknown source for handle %d", __FUNCTION__, handle);
3423 return BAD_VALUE;
3424 }
3425 status_t status = disconnectAudioSource(sourceDesc);
3426
3427 mAudioSources.removeItem(handle);
3428 return status;
3429}
3430
Andy Hung2ddee192015-12-18 17:34:44 -08003431status_t AudioPolicyManager::setMasterMono(bool mono)
3432{
3433 if (mMasterMono == mono) {
3434 return NO_ERROR;
3435 }
3436 mMasterMono = mono;
3437 // if enabling mono we close all offloaded devices, which will invalidate the
3438 // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible
3439 // for recreating the new AudioTrack as non-offloaded PCM.
3440 //
3441 // If disabling mono, we leave all tracks as is: we don't know which clients
3442 // and tracks are able to be recreated as offloaded. The next "song" should
3443 // play back offloaded.
3444 if (mMasterMono) {
3445 Vector<audio_io_handle_t> offloaded;
3446 for (size_t i = 0; i < mOutputs.size(); ++i) {
3447 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
3448 if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
3449 offloaded.push(desc->mIoHandle);
3450 }
3451 }
3452 for (size_t i = 0; i < offloaded.size(); ++i) {
3453 closeOutput(offloaded[i]);
3454 }
3455 }
3456 // update master mono for all remaining outputs
3457 for (size_t i = 0; i < mOutputs.size(); ++i) {
3458 updateMono(mOutputs.keyAt(i));
3459 }
3460 return NO_ERROR;
3461}
3462
3463status_t AudioPolicyManager::getMasterMono(bool *mono)
3464{
3465 *mono = mMasterMono;
3466 return NO_ERROR;
3467}
3468
Eric Laurentac9cef52017-06-09 15:46:26 -07003469float AudioPolicyManager::getStreamVolumeDB(
3470 audio_stream_type_t stream, int index, audio_devices_t device)
3471{
3472 return computeVolume(stream, index, device);
3473}
3474
Eric Laurentd60560a2015-04-10 11:31:20 -07003475status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3476{
3477 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3478
3479 sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle);
3480 if (patchDesc == 0) {
3481 ALOGW("%s source has no patch with handle %d", __FUNCTION__,
3482 sourceDesc->mPatchDesc->mHandle);
3483 return BAD_VALUE;
3484 }
3485 removeAudioPatch(sourceDesc->mPatchDesc->mHandle);
3486
Eric Laurent28d09f02016-03-08 10:43:05 -08003487 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003488 sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote();
3489 if (swOutputDesc != 0) {
3490 stopSource(swOutputDesc, stream, false);
3491 mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3492 } else {
3493 sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote();
3494 if (hwOutputDesc != 0) {
3495 // release patch between src device and output device
3496 // close Hwoutput and remove from mHwOutputs
3497 } else {
3498 ALOGW("%s source has neither SW nor HW output", __FUNCTION__);
3499 }
3500 }
3501 return NO_ERROR;
3502}
3503
3504sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput(
3505 audio_io_handle_t output, routing_strategy strategy)
3506{
3507 sp<AudioSourceDescriptor> source;
3508 for (size_t i = 0; i < mAudioSources.size(); i++) {
3509 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3510 routing_strategy sourceStrategy =
3511 (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
3512 sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote();
3513 if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) {
3514 source = sourceDesc;
3515 break;
3516 }
3517 }
3518 return source;
Eric Laurent554a2772015-04-10 11:29:24 -07003519}
3520
Eric Laurente552edb2014-03-10 17:42:56 -07003521// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07003522// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07003523// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07003524uint32_t AudioPolicyManager::nextAudioPortGeneration()
3525{
3526 return android_atomic_inc(&mAudioPortGeneration);
3527}
3528
Jaekyun Seok0d4a6af2017-02-17 17:10:17 +09003529#ifdef USE_XML_AUDIO_POLICY_CONF
3530// Treblized audio policy xml config will be located in /odm/etc or /vendor/etc.
3531static const char *kConfigLocationList[] =
3532 {"/odm/etc", "/vendor/etc", "/system/etc"};
3533static const int kConfigLocationListSize =
3534 (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0]));
3535
3536static status_t deserializeAudioPolicyXmlConfig(AudioPolicyConfig &config) {
3537 char audioPolicyXmlConfigFile[AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH];
3538 status_t ret;
3539
3540 for (int i = 0; i < kConfigLocationListSize; i++) {
3541 PolicySerializer serializer;
3542 snprintf(audioPolicyXmlConfigFile,
3543 sizeof(audioPolicyXmlConfigFile),
3544 "%s/%s",
3545 kConfigLocationList[i],
3546 AUDIO_POLICY_XML_CONFIG_FILE_NAME);
3547 ret = serializer.deserialize(audioPolicyXmlConfigFile, config);
3548 if (ret == NO_ERROR) {
3549 break;
3550 }
3551 }
3552 return ret;
3553}
3554#endif
3555
Eric Laurente0720872014-03-11 09:30:41 -07003556AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07003557 :
Eric Laurente552edb2014-03-10 17:42:56 -07003558 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07003559 mA2dpSuspended(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003560 mAudioPortGeneration(1),
3561 mBeaconMuteRefCount(0),
3562 mBeaconPlayingRefCount(0),
Eric Laurent9459fb02015-08-12 18:36:32 -07003563 mBeaconMuted(false),
Andy Hung2ddee192015-12-18 17:34:44 -08003564 mTtsOutputAvailable(false),
Eric Laurent36829f92017-04-07 19:04:42 -07003565 mMasterMono(false),
Chris Thornton2b864642017-06-27 21:26:07 -07003566 mMusicEffectOutput(AUDIO_IO_HANDLE_NONE),
3567 mHasComputedSoundTriggerSupportsConcurrentCapture(false)
Eric Laurente552edb2014-03-10 17:42:56 -07003568{
François Gaffied1ab2bd2015-12-02 18:20:06 +01003569 mUidCached = getuid();
3570 mpClientInterface = clientInterface;
3571
3572 // TODO: remove when legacy conf file is removed. true on devices that use DRC on the
3573 // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly.
3574 // Note: remove also speaker_drc_enabled from global configuration of XML config file.
3575 bool speakerDrcEnabled = false;
3576
3577#ifdef USE_XML_AUDIO_POLICY_CONF
3578 mVolumeCurves = new VolumeCurvesCollection();
3579 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3580 mDefaultOutputDevice, speakerDrcEnabled,
3581 static_cast<VolumeCurvesCollection *>(mVolumeCurves));
Jaekyun Seok0d4a6af2017-02-17 17:10:17 +09003582 if (deserializeAudioPolicyXmlConfig(config) != NO_ERROR) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01003583#else
3584 mVolumeCurves = new StreamDescriptorCollection();
3585 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3586 mDefaultOutputDevice, speakerDrcEnabled);
3587 if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) &&
3588 (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) {
3589#endif
3590 ALOGE("could not load audio policy configuration file, setting defaults");
3591 config.setDefault();
3592 }
3593 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
3594 mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled);
3595
3596 // Once policy config has been parsed, retrieve an instance of the engine and initialize it.
François Gaffie2110e042015-03-24 08:41:51 +01003597 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
3598 if (!engineInstance) {
3599 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
3600 return;
3601 }
3602 // Retrieve the Policy Manager Interface
3603 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
3604 if (mEngine == NULL) {
3605 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
3606 return;
3607 }
3608 mEngine->setObserver(this);
3609 status_t status = mEngine->initCheck();
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07003610 (void) status;
François Gaffie2110e042015-03-24 08:41:51 +01003611 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
3612
Eric Laurent3a4311c2014-03-17 12:00:47 -07003613 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07003614 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07003615 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
3616 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07003617 for (size_t i = 0; i < mHwModules.size(); i++) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003618 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003619 if (mHwModules[i]->mHandle == 0) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003620 ALOGW("could not open HW module %s", mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003621 continue;
3622 }
3623 // open all output streams needed to access attached devices
3624 // except for direct output streams that are only opened when they are actually
3625 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07003626 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07003627 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3628 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003629 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003630
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003631 if (!outProfile->hasSupportedDevices()) {
3632 ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003633 continue;
3634 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003635 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) {
Eric Laurent9459fb02015-08-12 18:36:32 -07003636 mTtsOutputAvailable = true;
3637 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003638
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003639 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
Eric Laurentd78f1532014-09-16 16:38:20 -07003640 continue;
3641 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003642 audio_devices_t profileType = outProfile->getSupportedDevicesType();
François Gaffie53615e22015-03-19 09:24:12 +01003643 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
3644 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07003645 } else {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003646 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003647 // outputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003648 profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes);
Eric Laurente552edb2014-03-10 17:42:56 -07003649 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003650 if ((profileType & outputDeviceTypes) == 0) {
3651 continue;
3652 }
Eric Laurentc75307b2015-03-17 15:29:32 -07003653 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
3654 mpClientInterface);
Eric Laurentc40d9692016-04-13 19:14:13 -07003655 const DeviceVector &supportedDevices = outProfile->getSupportedDevices();
3656 const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType);
3657 String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress
3658 : String8("");
Eric Laurentd78f1532014-09-16 16:38:20 -07003659
3660 outputDesc->mDevice = profileType;
3661 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3662 config.sample_rate = outputDesc->mSamplingRate;
3663 config.channel_mask = outputDesc->mChannelMask;
3664 config.format = outputDesc->mFormat;
3665 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003666 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003667 &output,
3668 &config,
3669 &outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003670 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003671 &outputDesc->mLatency,
3672 outputDesc->mFlags);
3673
3674 if (status != NO_ERROR) {
3675 ALOGW("Cannot open output stream for device %08x on hw module %s",
3676 outputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003677 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003678 } else {
3679 outputDesc->mSamplingRate = config.sample_rate;
3680 outputDesc->mChannelMask = config.channel_mask;
3681 outputDesc->mFormat = config.format;
3682
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003683 for (size_t k = 0; k < supportedDevices.size(); k++) {
3684 ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003685 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08003686 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
3687 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003688 }
3689 }
3690 if (mPrimaryOutput == 0 &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003691 outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003692 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07003693 }
3694 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003695 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07003696 outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003697 true,
3698 0,
3699 NULL,
3700 address.string());
Eric Laurentd78f1532014-09-16 16:38:20 -07003701 }
Eric Laurente552edb2014-03-10 17:42:56 -07003702 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003703 // open input streams needed to access attached devices to validate
3704 // mAvailableInputDevices list
3705 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
3706 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003707 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003708
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003709 if (!inProfile->hasSupportedDevices()) {
3710 ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003711 continue;
3712 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003713 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003714 // inputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003715 audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes);
3716
Eric Laurentd78f1532014-09-16 16:38:20 -07003717 if ((profileType & inputDeviceTypes) == 0) {
3718 continue;
3719 }
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08003720 sp<AudioInputDescriptor> inputDesc =
3721 new AudioInputDescriptor(inProfile);
Eric Laurentd78f1532014-09-16 16:38:20 -07003722
Eric Laurentd78f1532014-09-16 16:38:20 -07003723 inputDesc->mDevice = profileType;
3724
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003725 // find the address
3726 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
3727 // the inputs vector must be of size 1, but we don't want to crash here
3728 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
3729 : String8("");
3730 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
3731 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
3732
Eric Laurentd78f1532014-09-16 16:38:20 -07003733 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3734 config.sample_rate = inputDesc->mSamplingRate;
3735 config.channel_mask = inputDesc->mChannelMask;
3736 config.format = inputDesc->mFormat;
3737 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003738 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003739 &input,
3740 &config,
3741 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003742 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003743 AUDIO_SOURCE_MIC,
3744 AUDIO_INPUT_FLAG_NONE);
3745
3746 if (status == NO_ERROR) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003747 const DeviceVector &supportedDevices = inProfile->getSupportedDevices();
3748 for (size_t k = 0; k < supportedDevices.size(); k++) {
3749 ssize_t index = mAvailableInputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003750 // give a valid ID to an attached device once confirmed it is reachable
Eric Laurent45aabc32015-08-06 09:11:13 -07003751 if (index >= 0) {
3752 sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index];
3753 if (!devDesc->isAttached()) {
3754 devDesc->attach(mHwModules[i]);
Eric Laurent83efe1c2017-07-09 16:51:08 -07003755 devDesc->importAudioPort(inProfile, true);
Eric Laurent45aabc32015-08-06 09:11:13 -07003756 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003757 }
3758 }
3759 mpClientInterface->closeInput(input);
3760 } else {
3761 ALOGW("Cannot open input stream for device %08x on hw module %s",
3762 inputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003763 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003764 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003765 }
3766 }
3767 // make sure all attached devices have been allocated a unique ID
3768 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003769 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003770 ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003771 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
3772 continue;
3773 }
François Gaffie2110e042015-03-24 08:41:51 +01003774 // The device is now validated and can be appended to the available devices of the engine
3775 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
3776 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003777 i++;
3778 }
3779 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003780 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01003781 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003782 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
3783 continue;
3784 }
François Gaffie2110e042015-03-24 08:41:51 +01003785 // The device is now validated and can be appended to the available devices of the engine
3786 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
3787 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003788 i++;
3789 }
3790 // make sure default device is reachable
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003791 if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01003792 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003793 }
Eric Laurente552edb2014-03-10 17:42:56 -07003794
3795 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
3796
3797 updateDevicesAndOutputs();
Eric Laurente552edb2014-03-10 17:42:56 -07003798}
3799
Eric Laurente0720872014-03-11 09:30:41 -07003800AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07003801{
Eric Laurente552edb2014-03-10 17:42:56 -07003802 for (size_t i = 0; i < mOutputs.size(); i++) {
3803 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003804 }
3805 for (size_t i = 0; i < mInputs.size(); i++) {
3806 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003807 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003808 mAvailableOutputDevices.clear();
3809 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003810 mOutputs.clear();
3811 mInputs.clear();
3812 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07003813}
3814
Eric Laurente0720872014-03-11 09:30:41 -07003815status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07003816{
Eric Laurent87ffa392015-05-22 10:32:38 -07003817 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003818}
3819
Eric Laurente552edb2014-03-10 17:42:56 -07003820// ---
3821
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003822void AudioPolicyManager::addOutput(audio_io_handle_t output, const sp<SwAudioOutputDescriptor>& outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003823{
François Gaffie98cc1912015-03-18 17:52:40 +01003824 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003825 mOutputs.add(output, outputDesc);
Andy Hung2ddee192015-12-18 17:34:44 -08003826 updateMono(output); // update mono status when adding to output list
Eric Laurent36829f92017-04-07 19:04:42 -07003827 selectOutputForMusicEffects();
Eric Laurent6a94d692014-05-20 11:18:06 -07003828 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003829}
3830
François Gaffie53615e22015-03-19 09:24:12 +01003831void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3832{
3833 mOutputs.removeItem(output);
Eric Laurent36829f92017-04-07 19:04:42 -07003834 selectOutputForMusicEffects();
François Gaffie53615e22015-03-19 09:24:12 +01003835}
3836
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003837void AudioPolicyManager::addInput(audio_io_handle_t input, const sp<AudioInputDescriptor>& inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003838{
François Gaffie98cc1912015-03-18 17:52:40 +01003839 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003840 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003841 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003842}
Eric Laurente552edb2014-03-10 17:42:56 -07003843
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003844void AudioPolicyManager::findIoHandlesByAddress(const sp<SwAudioOutputDescriptor>& desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003845 const audio_devices_t device /*in*/,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003846 const String8& address /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003847 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003848 sp<DeviceDescriptor> devDesc =
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003849 desc->mProfile->getSupportedDeviceByAddress(device, address);
keunyoung3190e672014-12-30 13:00:52 -08003850 if (devDesc != 0) {
3851 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3852 desc->mIoHandle, address.string());
3853 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003854 }
3855}
3856
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003857status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003858 audio_policy_dev_state_t state,
3859 SortedVector<audio_io_handle_t>& outputs,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003860 const String8& address)
Eric Laurente552edb2014-03-10 17:42:56 -07003861{
François Gaffie53615e22015-03-19 09:24:12 +01003862 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003863 sp<SwAudioOutputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003864
3865 if (audio_device_is_digital(device)) {
3866 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003867 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003868 }
Eric Laurente552edb2014-03-10 17:42:56 -07003869
Eric Laurent3b73df72014-03-11 09:06:29 -07003870 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003871 // first list already open outputs that can be routed to this device
3872 for (size_t i = 0; i < mOutputs.size(); i++) {
3873 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003874 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003875 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003876 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3877 outputs.add(mOutputs.keyAt(i));
3878 } else {
3879 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003880 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003881 }
Eric Laurente552edb2014-03-10 17:42:56 -07003882 }
3883 }
3884 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003885 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003886 for (size_t i = 0; i < mHwModules.size(); i++)
3887 {
3888 if (mHwModules[i]->mHandle == 0) {
3889 continue;
3890 }
3891 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3892 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003893 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003894 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003895 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003896 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003897 profiles.add(profile);
3898 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3899 }
Eric Laurente552edb2014-03-10 17:42:56 -07003900 }
3901 }
3902 }
3903
Eric Laurent7b279bb2015-12-14 10:18:23 -08003904 ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size());
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003905
Eric Laurente552edb2014-03-10 17:42:56 -07003906 if (profiles.isEmpty() && outputs.isEmpty()) {
3907 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3908 return BAD_VALUE;
3909 }
3910
3911 // open outputs for matching profiles if needed. Direct outputs are also opened to
3912 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3913 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003914 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003915
3916 // nothing to do if one output is already opened for this profile
3917 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003918 for (j = 0; j < outputs.size(); j++) {
3919 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003920 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003921 // matching profile: save the sample rates, format and channel masks supported
3922 // by the profile in our device descriptor
Paul McLean9080a4c2015-06-18 08:24:02 -07003923 if (audio_device_is_digital(device)) {
3924 devDesc->importAudioPort(profile);
3925 }
Eric Laurente552edb2014-03-10 17:42:56 -07003926 break;
3927 }
3928 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003929 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003930 continue;
3931 }
3932
Eric Laurent83efe1c2017-07-09 16:51:08 -07003933 ALOGV("opening output for device %08x with params %s profile %p name %s",
3934 device, address.string(), profile.get(), profile->getName().string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003935 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003936 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003937 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3938 config.sample_rate = desc->mSamplingRate;
3939 config.channel_mask = desc->mChannelMask;
3940 config.format = desc->mFormat;
3941 config.offload_info.sample_rate = desc->mSamplingRate;
3942 config.offload_info.channel_mask = desc->mChannelMask;
3943 config.offload_info.format = desc->mFormat;
3944 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003945 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003946 &output,
3947 &config,
3948 &desc->mDevice,
3949 address,
3950 &desc->mLatency,
3951 desc->mFlags);
3952 if (status == NO_ERROR) {
3953 desc->mSamplingRate = config.sample_rate;
3954 desc->mChannelMask = config.channel_mask;
3955 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003956
Eric Laurentd4692962014-05-05 18:13:44 -07003957 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003958 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003959 char *param = audio_device_address_to_parameter(device, address);
3960 mpClientInterface->setParameters(output, String8(param));
3961 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003962 }
Phil Burk00eeb322016-03-31 12:41:00 -07003963 updateAudioProfiles(device, output, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01003964 if (!profile->hasValidAudioProfile()) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003965 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003966 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003967 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003968 } else if (profile->hasDynamicAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07003969 mpClientInterface->closeOutput(output);
Phil Burk702b1052016-03-02 16:38:26 -08003970 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003971 profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003972 config.offload_info.sample_rate = config.sample_rate;
3973 config.offload_info.channel_mask = config.channel_mask;
3974 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07003975 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003976 &output,
3977 &config,
3978 &desc->mDevice,
3979 address,
3980 &desc->mLatency,
3981 desc->mFlags);
3982 if (status == NO_ERROR) {
3983 desc->mSamplingRate = config.sample_rate;
3984 desc->mChannelMask = config.channel_mask;
3985 desc->mFormat = config.format;
3986 } else {
3987 output = AUDIO_IO_HANDLE_NONE;
3988 }
Eric Laurentd4692962014-05-05 18:13:44 -07003989 }
3990
Eric Laurentcf2c0212014-07-25 16:20:43 -07003991 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003992 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003993 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003994 sp<AudioPolicyMix> policyMix;
3995 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003996 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3997 address.string());
3998 }
François Gaffie036e1e92015-03-19 10:16:24 +01003999 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07004000 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01004001
Eric Laurent87ffa392015-05-22 10:32:38 -07004002 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
4003 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08004004 // no duplicated output for direct outputs and
4005 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07004006 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07004007
Eric Laurentd4692962014-05-05 18:13:44 -07004008 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07004009 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07004010
Eric Laurentd4692962014-05-05 18:13:44 -07004011 //TODO: configure audio effect output stage here
4012
4013 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07004014 duplicatedOutput =
4015 mpClientInterface->openDuplicateOutput(output,
4016 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004017 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07004018 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07004019 sp<SwAudioOutputDescriptor> dupOutputDesc =
4020 new SwAudioOutputDescriptor(NULL, mpClientInterface);
4021 dupOutputDesc->mOutput1 = mPrimaryOutput;
4022 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07004023 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
4024 dupOutputDesc->mFormat = desc->mFormat;
4025 dupOutputDesc->mChannelMask = desc->mChannelMask;
4026 dupOutputDesc->mLatency = desc->mLatency;
4027 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07004028 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07004029 } else {
4030 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07004031 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07004032 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01004033 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07004034 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07004035 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07004036 }
Eric Laurente552edb2014-03-10 17:42:56 -07004037 }
4038 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07004039 } else {
4040 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07004041 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07004042 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07004043 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004044 profiles.removeAt(profile_index);
4045 profile_index--;
4046 } else {
4047 outputs.add(output);
Paul McLean9080a4c2015-06-18 08:24:02 -07004048 // Load digital format info only for digital devices
4049 if (audio_device_is_digital(device)) {
4050 devDesc->importAudioPort(profile);
4051 }
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07004052
François Gaffie53615e22015-03-19 09:24:12 +01004053 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004054 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
4055 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07004056 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004057 NULL/*patch handle*/, address.string());
4058 }
Eric Laurente552edb2014-03-10 17:42:56 -07004059 ALOGV("checkOutputsForDevice(): adding output %d", output);
4060 }
4061 }
4062
4063 if (profiles.isEmpty()) {
4064 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
4065 return BAD_VALUE;
4066 }
Eric Laurentd4692962014-05-05 18:13:44 -07004067 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07004068 // check if one opened output is not needed any more after disconnecting one device
4069 for (size_t i = 0; i < mOutputs.size(); i++) {
4070 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004071 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004072 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01004073 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07004074 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08004075 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07004076 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004077 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
4078 mOutputs.keyAt(i));
4079 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004080 }
Eric Laurente552edb2014-03-10 17:42:56 -07004081 }
4082 }
Eric Laurentd4692962014-05-05 18:13:44 -07004083 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07004084 for (size_t i = 0; i < mHwModules.size(); i++)
4085 {
4086 if (mHwModules[i]->mHandle == 0) {
4087 continue;
4088 }
4089 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
4090 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004091 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004092 if (profile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07004093 ALOGV("checkOutputsForDevice(): "
4094 "clearing direct output profile %zu on module %zu", j, i);
François Gaffie112b0af2015-11-19 16:13:25 +01004095 profile->clearAudioProfiles();
Eric Laurente552edb2014-03-10 17:42:56 -07004096 }
4097 }
4098 }
4099 }
4100 return NO_ERROR;
4101}
4102
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004103status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01004104 audio_policy_dev_state_t state,
4105 SortedVector<audio_io_handle_t>& inputs,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004106 const String8& address)
Eric Laurentd4692962014-05-05 18:13:44 -07004107{
Paul McLean9080a4c2015-06-18 08:24:02 -07004108 audio_devices_t device = devDesc->type();
Eric Laurent1f2f2232014-06-02 12:01:23 -07004109 sp<AudioInputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07004110
4111 if (audio_device_is_digital(device)) {
4112 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08004113 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07004114 }
4115
Eric Laurentd4692962014-05-05 18:13:44 -07004116 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
4117 // first list already open inputs that can be routed to this device
4118 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
4119 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004120 if (desc->mProfile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07004121 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
4122 inputs.add(mInputs.keyAt(input_index));
4123 }
4124 }
4125
4126 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07004127 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07004128 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
4129 {
4130 if (mHwModules[module_idx]->mHandle == 0) {
4131 continue;
4132 }
4133 for (size_t profile_index = 0;
4134 profile_index < mHwModules[module_idx]->mInputProfiles.size();
4135 profile_index++)
4136 {
Eric Laurent275e8e92014-11-30 15:14:47 -08004137 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
4138
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004139 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01004140 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004141 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004142 profiles.add(profile);
4143 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
4144 profile_index, module_idx);
4145 }
Eric Laurentd4692962014-05-05 18:13:44 -07004146 }
4147 }
4148 }
4149
4150 if (profiles.isEmpty() && inputs.isEmpty()) {
4151 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
4152 return BAD_VALUE;
4153 }
4154
4155 // open inputs for matching profiles if needed. Direct inputs are also opened to
4156 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
4157 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
4158
Eric Laurent1c333e22014-05-20 10:48:17 -07004159 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07004160 // nothing to do if one input is already opened for this profile
4161 size_t input_index;
4162 for (input_index = 0; input_index < mInputs.size(); input_index++) {
4163 desc = mInputs.valueAt(input_index);
4164 if (desc->mProfile == profile) {
Paul McLean9080a4c2015-06-18 08:24:02 -07004165 if (audio_device_is_digital(device)) {
4166 devDesc->importAudioPort(profile);
4167 }
Eric Laurentd4692962014-05-05 18:13:44 -07004168 break;
4169 }
4170 }
4171 if (input_index != mInputs.size()) {
4172 continue;
4173 }
4174
4175 ALOGV("opening input for device 0x%X with params %s", device, address.string());
4176 desc = new AudioInputDescriptor(profile);
4177 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07004178 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
4179 config.sample_rate = desc->mSamplingRate;
4180 config.channel_mask = desc->mChannelMask;
4181 config.format = desc->mFormat;
4182 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent83efe1c2017-07-09 16:51:08 -07004183
4184 ALOGV("opening inputput for device %08x with params %s profile %p name %s",
4185 desc->mDevice, address.string(), profile.get(), profile->getName().string());
4186
Eric Laurent322b4d22015-04-03 15:57:54 -07004187 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07004188 &input,
4189 &config,
4190 &desc->mDevice,
4191 address,
4192 AUDIO_SOURCE_MIC,
4193 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07004194
Eric Laurentcf2c0212014-07-25 16:20:43 -07004195 if (status == NO_ERROR) {
4196 desc->mSamplingRate = config.sample_rate;
4197 desc->mChannelMask = config.channel_mask;
4198 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07004199
Eric Laurentd4692962014-05-05 18:13:44 -07004200 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07004201 char *param = audio_device_address_to_parameter(device, address);
4202 mpClientInterface->setParameters(input, String8(param));
4203 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07004204 }
Phil Burk00eeb322016-03-31 12:41:00 -07004205 updateAudioProfiles(device, input, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01004206 if (!profile->hasValidAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07004207 ALOGW("checkInputsForDevice() direct input missing param");
4208 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004209 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07004210 }
4211
4212 if (input != 0) {
4213 addInput(input, desc);
4214 }
4215 } // endif input != 0
4216
Eric Laurentcf2c0212014-07-25 16:20:43 -07004217 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07004218 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07004219 profiles.removeAt(profile_index);
4220 profile_index--;
4221 } else {
4222 inputs.add(input);
Paul McLean9080a4c2015-06-18 08:24:02 -07004223 if (audio_device_is_digital(device)) {
4224 devDesc->importAudioPort(profile);
4225 }
Eric Laurentd4692962014-05-05 18:13:44 -07004226 ALOGV("checkInputsForDevice(): adding input %d", input);
4227 }
4228 } // end scan profiles
4229
4230 if (profiles.isEmpty()) {
4231 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
4232 return BAD_VALUE;
4233 }
4234 } else {
4235 // Disconnect
4236 // check if one opened input is not needed any more after disconnecting one device
4237 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
4238 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004239 if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) {
Eric Laurentd4692962014-05-05 18:13:44 -07004240 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
4241 mInputs.keyAt(input_index));
4242 inputs.add(mInputs.keyAt(input_index));
4243 }
4244 }
4245 // Clear any profiles associated with the disconnected device.
4246 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
4247 if (mHwModules[module_index]->mHandle == 0) {
4248 continue;
4249 }
4250 for (size_t profile_index = 0;
4251 profile_index < mHwModules[module_index]->mInputProfiles.size();
4252 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004253 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004254 if (profile->supportDevice(device)) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07004255 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07004256 profile_index, module_index);
François Gaffie112b0af2015-11-19 16:13:25 +01004257 profile->clearAudioProfiles();
Eric Laurentd4692962014-05-05 18:13:44 -07004258 }
4259 }
4260 }
4261 } // end disconnect
4262
4263 return NO_ERROR;
4264}
4265
4266
Eric Laurente0720872014-03-11 09:30:41 -07004267void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07004268{
4269 ALOGV("closeOutput(%d)", output);
4270
Eric Laurentc75307b2015-03-17 15:29:32 -07004271 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004272 if (outputDesc == NULL) {
4273 ALOGW("closeOutput() unknown output %d", output);
4274 return;
4275 }
François Gaffie036e1e92015-03-19 10:16:24 +01004276 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08004277
Eric Laurente552edb2014-03-10 17:42:56 -07004278 // look for duplicated outputs connected to the output being removed.
4279 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004280 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07004281 if (dupOutputDesc->isDuplicated() &&
4282 (dupOutputDesc->mOutput1 == outputDesc ||
4283 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004284 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07004285 if (dupOutputDesc->mOutput1 == outputDesc) {
4286 outputDesc2 = dupOutputDesc->mOutput2;
4287 } else {
4288 outputDesc2 = dupOutputDesc->mOutput1;
4289 }
4290 // As all active tracks on duplicated output will be deleted,
4291 // and as they were also referenced on the other output, the reference
4292 // count for their stream type must be adjusted accordingly on
4293 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07004294 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07004295 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07004296 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07004297 }
4298 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
4299 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
4300
4301 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01004302 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07004303 }
4304 }
4305
Eric Laurent05b90f82014-08-27 15:32:29 -07004306 nextAudioPortGeneration();
4307
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004308 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004309 if (index >= 0) {
4310 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07004311 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07004312 mAudioPatches.removeItemsAt(index);
4313 mpClientInterface->onAudioPatchListUpdate();
4314 }
4315
Eric Laurente552edb2014-03-10 17:42:56 -07004316 AudioParameter param;
4317 param.add(String8("closing"), String8("true"));
4318 mpClientInterface->setParameters(output, param.toString());
4319
4320 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01004321 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004322 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07004323}
4324
4325void AudioPolicyManager::closeInput(audio_io_handle_t input)
4326{
4327 ALOGV("closeInput(%d)", input);
4328
4329 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
4330 if (inputDesc == NULL) {
4331 ALOGW("closeInput() unknown input %d", input);
4332 return;
4333 }
4334
Eric Laurent6a94d692014-05-20 11:18:06 -07004335 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07004336
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004337 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004338 if (index >= 0) {
4339 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07004340 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07004341 mAudioPatches.removeItemsAt(index);
4342 mpClientInterface->onAudioPatchListUpdate();
4343 }
4344
4345 mpClientInterface->closeInput(input);
4346 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07004347}
4348
Eric Laurentc75307b2015-03-17 15:29:32 -07004349SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
4350 audio_devices_t device,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004351 const SwAudioOutputCollection& openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07004352{
4353 SortedVector<audio_io_handle_t> outputs;
4354
4355 ALOGVV("getOutputsForDevice() device %04x", device);
4356 for (size_t i = 0; i < openOutputs.size(); i++) {
Eric Laurent37ddbb42016-08-10 16:19:14 -07004357 ALOGVV("output %zu isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004358 i, openOutputs.valueAt(i)->isDuplicated(),
4359 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004360 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
4361 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
4362 outputs.add(openOutputs.keyAt(i));
4363 }
4364 }
4365 return outputs;
4366}
4367
Eric Laurente0720872014-03-11 09:30:41 -07004368bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01004369 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07004370{
4371 if (outputs1.size() != outputs2.size()) {
4372 return false;
4373 }
4374 for (size_t i = 0; i < outputs1.size(); i++) {
4375 if (outputs1[i] != outputs2[i]) {
4376 return false;
4377 }
4378 }
4379 return true;
4380}
4381
Eric Laurente0720872014-03-11 09:30:41 -07004382void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07004383{
4384 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
4385 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
4386 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
4387 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
4388
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004389 // also take into account external policy-related changes: add all outputs which are
4390 // associated with policies in the "before" and "after" output vectors
4391 ALOGVV("checkOutputForStrategy(): policy related outputs");
4392 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004393 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004394 if (desc != 0 && desc->mPolicyMix != NULL) {
4395 srcOutputs.add(desc->mIoHandle);
4396 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
4397 }
4398 }
4399 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004400 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004401 if (desc != 0 && desc->mPolicyMix != NULL) {
4402 dstOutputs.add(desc->mIoHandle);
4403 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
4404 }
4405 }
4406
Eric Laurente552edb2014-03-10 17:42:56 -07004407 if (!vectorsEqual(srcOutputs,dstOutputs)) {
4408 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
4409 strategy, srcOutputs[0], dstOutputs[0]);
4410 // mute strategy while moving tracks from one output to another
4411 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004412 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01004413 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004414 setStrategyMute(strategy, true, desc);
4415 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004416 }
Eric Laurentd60560a2015-04-10 11:31:20 -07004417 sp<AudioSourceDescriptor> source =
4418 getSourceForStrategyOnOutput(srcOutputs[i], strategy);
4419 if (source != 0){
4420 connectAudioSource(source);
4421 }
Eric Laurente552edb2014-03-10 17:42:56 -07004422 }
4423
4424 // Move effects associated to this strategy from previous output to new output
4425 if (strategy == STRATEGY_MEDIA) {
Eric Laurent36829f92017-04-07 19:04:42 -07004426 selectOutputForMusicEffects();
Eric Laurente552edb2014-03-10 17:42:56 -07004427 }
4428 // Move tracks associated to this strategy from previous output to new output
Eric Laurent794fde22016-03-11 09:50:45 -08004429 for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004430 if (getStrategy((audio_stream_type_t)i) == strategy) {
4431 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07004432 }
4433 }
4434 }
4435}
4436
Eric Laurente0720872014-03-11 09:30:41 -07004437void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07004438{
François Gaffie2110e042015-03-24 08:41:51 +01004439 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004440 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004441 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01004442 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004443 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004444 checkOutputForStrategy(STRATEGY_SONIFICATION);
4445 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004446 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07004447 checkOutputForStrategy(STRATEGY_MEDIA);
4448 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004449 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07004450}
4451
Eric Laurente0720872014-03-11 09:30:41 -07004452void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07004453{
François Gaffie53615e22015-03-19 09:24:12 +01004454 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07004455 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004456 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07004457 return;
4458 }
4459
Eric Laurent3a4311c2014-03-17 12:00:47 -07004460 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08004461 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
4462 ~AUDIO_DEVICE_BIT_IN) != 0) ||
4463 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurentf732e072016-08-03 19:30:28 -07004464
4465 // if suspended, restore A2DP output if:
4466 // ((SCO device is NOT connected) ||
4467 // ((forced usage communication is NOT SCO) && (forced usage for record is NOT SCO) &&
4468 // (phone state is NOT in call) && (phone state is NOT ringing)))
Eric Laurente552edb2014-03-10 17:42:56 -07004469 //
Eric Laurentf732e072016-08-03 19:30:28 -07004470 // if not suspended, suspend A2DP output if:
4471 // (SCO device is connected) &&
4472 // ((forced usage for communication is SCO) || (forced usage for record is SCO) ||
4473 // ((phone state is in call) || (phone state is ringing)))
Eric Laurente552edb2014-03-10 17:42:56 -07004474 //
4475 if (mA2dpSuspended) {
Eric Laurentf732e072016-08-03 19:30:28 -07004476 if (!isScoConnected ||
4477 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) !=
4478 AUDIO_POLICY_FORCE_BT_SCO) &&
4479 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) !=
4480 AUDIO_POLICY_FORCE_BT_SCO) &&
4481 (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
François Gaffie2110e042015-03-24 08:41:51 +01004482 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004483
4484 mpClientInterface->restoreOutput(a2dpOutput);
4485 mA2dpSuspended = false;
4486 }
4487 } else {
Eric Laurentf732e072016-08-03 19:30:28 -07004488 if (isScoConnected &&
4489 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ==
4490 AUDIO_POLICY_FORCE_BT_SCO) ||
4491 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) ==
4492 AUDIO_POLICY_FORCE_BT_SCO) ||
4493 (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
François Gaffie2110e042015-03-24 08:41:51 +01004494 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004495
4496 mpClientInterface->suspendOutput(a2dpOutput);
4497 mA2dpSuspended = true;
4498 }
4499 }
4500}
4501
Eric Laurentc75307b2015-03-17 15:29:32 -07004502audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
4503 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004504{
4505 audio_devices_t device = AUDIO_DEVICE_NONE;
4506
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004507 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004508 if (index >= 0) {
4509 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4510 if (patchDesc->mUid != mUidCached) {
4511 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004512 outputDesc->device(), outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004513 return outputDesc->device();
4514 }
4515 }
4516
Eric Laurente552edb2014-03-10 17:42:56 -07004517 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05004518 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004519 // use device for strategy enforced audible
4520 // 2: we are in call or the strategy phone is active on the output:
4521 // use device for strategy phone
Jean-Michel Trivi178683b2017-08-30 18:07:06 -07004522 // 3: the strategy sonification is active on the output:
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004523 // use device for strategy sonification
Jean-Michel Trivi178683b2017-08-30 18:07:06 -07004524 // 4: the strategy for enforced audible is active but not enforced on the output:
4525 // use the device for strategy enforced audible
Eric Laurent28d09f02016-03-08 10:43:05 -08004526 // 5: the strategy accessibility is active on the output:
4527 // use device for strategy accessibility
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004528 // 6: the strategy "respectful" sonification is active on the output:
4529 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08004530 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004531 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08004532 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004533 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08004534 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004535 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01004536 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004537 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07004538 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
4539 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01004540 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004541 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004542 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004543 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Jean-Michel Trivi178683b2017-08-30 18:07:06 -07004544 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
4545 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
Eric Laurent28d09f02016-03-08 10:43:05 -08004546 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
4547 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004548 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004549 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004550 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004551 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004552 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004553 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004554 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004555 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004556 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004557 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07004558 }
4559
Eric Laurent1c333e22014-05-20 10:48:17 -07004560 ALOGV("getNewOutputDevice() selected device %x", device);
4561 return device;
4562}
4563
Eric Laurentfb66dd92016-01-28 18:32:03 -08004564audio_devices_t AudioPolicyManager::getNewInputDevice(const sp<AudioInputDescriptor>& inputDesc)
Eric Laurent1c333e22014-05-20 10:48:17 -07004565{
Eric Laurentfb66dd92016-01-28 18:32:03 -08004566 audio_devices_t device = AUDIO_DEVICE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07004567
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004568 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004569 if (index >= 0) {
4570 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4571 if (patchDesc->mUid != mUidCached) {
4572 ALOGV("getNewInputDevice() device %08x forced by patch %d",
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004573 inputDesc->mDevice, inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004574 return inputDesc->mDevice;
4575 }
4576 }
4577
Eric Laurentfb66dd92016-01-28 18:32:03 -08004578 audio_source_t source = inputDesc->getHighestPrioritySource(true /*activeOnly*/);
4579 if (isInCall()) {
4580 device = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
4581 } else if (source != AUDIO_SOURCE_DEFAULT) {
4582 device = getDeviceAndMixForInputSource(source);
4583 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004584
Eric Laurente552edb2014-03-10 17:42:56 -07004585 return device;
4586}
4587
Eric Laurent794fde22016-03-11 09:50:45 -08004588bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1,
4589 audio_stream_type_t stream2) {
Jean-Michel Trivi99bb2f92016-11-23 15:52:07 -08004590 return (stream1 == stream2);
Eric Laurent28d09f02016-03-08 10:43:05 -08004591}
4592
Eric Laurente0720872014-03-11 09:30:41 -07004593uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004594 return (uint32_t)getStrategy(stream);
4595}
4596
Eric Laurente0720872014-03-11 09:30:41 -07004597audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004598 // By checking the range of stream before calling getStrategy, we avoid
4599 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
4600 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08004601 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004602 return AUDIO_DEVICE_NONE;
4603 }
Eric Laurent28d09f02016-03-08 10:43:05 -08004604 audio_devices_t devices = AUDIO_DEVICE_NONE;
Eric Laurent794fde22016-03-11 09:50:45 -08004605 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
4606 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004607 continue;
Eric Laurent6a94d692014-05-20 11:18:06 -07004608 }
Eric Laurent794fde22016-03-11 09:50:45 -08004609 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08004610 audio_devices_t curDevices =
Eric Laurent447a87b2016-07-07 17:32:10 -07004611 getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/);
Eric Laurent28d09f02016-03-08 10:43:05 -08004612 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs);
4613 for (size_t i = 0; i < outputs.size(); i++) {
4614 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurent794fde22016-03-11 09:50:45 -08004615 if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004616 curDevices |= outputDesc->device();
4617 }
4618 }
4619 devices |= curDevices;
Eric Laurente552edb2014-03-10 17:42:56 -07004620 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05004621
4622 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
4623 and doesn't really need to.*/
4624 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
4625 devices |= AUDIO_DEVICE_OUT_SPEAKER;
4626 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4627 }
Eric Laurente552edb2014-03-10 17:42:56 -07004628 return devices;
4629}
4630
François Gaffie2110e042015-03-24 08:41:51 +01004631routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
4632{
Eric Laurent223fd5c2014-11-11 13:43:36 -08004633 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01004634 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004635}
4636
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004637uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4638 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004639 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4640 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4641 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004642 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4643 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4644 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004645 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01004646 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004647}
4648
Eric Laurente0720872014-03-11 09:30:41 -07004649void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004650 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004651 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004652 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4653 updateDevicesAndOutputs();
4654 break;
4655 default:
4656 break;
4657 }
4658}
4659
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004660uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
Eric Laurent9459fb02015-08-12 18:36:32 -07004661
4662 // skip beacon mute management if a dedicated TTS output is available
4663 if (mTtsOutputAvailable) {
4664 return 0;
4665 }
4666
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004667 switch(event) {
4668 case STARTING_OUTPUT:
4669 mBeaconMuteRefCount++;
4670 break;
4671 case STOPPING_OUTPUT:
4672 if (mBeaconMuteRefCount > 0) {
4673 mBeaconMuteRefCount--;
4674 }
4675 break;
4676 case STARTING_BEACON:
4677 mBeaconPlayingRefCount++;
4678 break;
4679 case STOPPING_BEACON:
4680 if (mBeaconPlayingRefCount > 0) {
4681 mBeaconPlayingRefCount--;
4682 }
4683 break;
4684 }
4685
4686 if (mBeaconMuteRefCount > 0) {
4687 // any playback causes beacon to be muted
4688 return setBeaconMute(true);
4689 } else {
4690 // no other playback: unmute when beacon starts playing, mute when it stops
4691 return setBeaconMute(mBeaconPlayingRefCount == 0);
4692 }
4693}
4694
4695uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4696 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4697 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4698 // keep track of muted state to avoid repeating mute/unmute operations
4699 if (mBeaconMuted != mute) {
4700 // mute/unmute AUDIO_STREAM_TTS on all outputs
4701 ALOGV("\t muting %d", mute);
4702 uint32_t maxLatency = 0;
4703 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004704 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004705 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004706 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004707 0 /*delay*/, AUDIO_DEVICE_NONE);
4708 const uint32_t latency = desc->latency() * 2;
4709 if (latency > maxLatency) {
4710 maxLatency = latency;
4711 }
4712 }
4713 mBeaconMuted = mute;
4714 return maxLatency;
4715 }
4716 return 0;
4717}
4718
Eric Laurente0720872014-03-11 09:30:41 -07004719audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004720 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004721{
Paul McLeanaa981192015-03-21 09:55:15 -07004722 // Routing
4723 // see if we have an explicit route
4724 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4725 // (getStrategy(stream)).
4726 // if the strategy from the stream type in the RouteMap is the same as the argument above,
Eric Laurentad2e7b92017-09-14 20:06:42 -07004727 // and activity count is non-zero and the device in the route descriptor is available
4728 // then select this device.
Paul McLeanaa981192015-03-21 09:55:15 -07004729 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4730 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
Eric Laurent28d09f02016-03-08 10:43:05 -08004731 routing_strategy routeStrategy = getStrategy(route->mStreamType);
Eric Laurentad2e7b92017-09-14 20:06:42 -07004732 if ((routeStrategy == strategy) && route->isActive() &&
4733 (mAvailableOutputDevices.indexOf(route->mDeviceDescriptor) >= 0)) {
Paul McLeanaa981192015-03-21 09:55:15 -07004734 return route->mDeviceDescriptor->type();
4735 }
4736 }
4737
Eric Laurente552edb2014-03-10 17:42:56 -07004738 if (fromCache) {
4739 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4740 strategy, mDeviceForStrategy[strategy]);
4741 return mDeviceForStrategy[strategy];
4742 }
François Gaffie2110e042015-03-24 08:41:51 +01004743 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004744}
4745
Eric Laurente0720872014-03-11 09:30:41 -07004746void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004747{
4748 for (int i = 0; i < NUM_STRATEGIES; i++) {
4749 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4750 }
4751 mPreviousOutputs = mOutputs;
4752}
4753
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004754uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004755 audio_devices_t prevDevice,
4756 uint32_t delayMs)
4757{
4758 // mute/unmute strategies using an incompatible device combination
4759 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4760 // if unmuting, unmute only after the specified delay
4761 if (outputDesc->isDuplicated()) {
4762 return 0;
4763 }
4764
4765 uint32_t muteWaitMs = 0;
4766 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004767 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004768
4769 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4770 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004771 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004772 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4773 bool doMute = false;
4774
4775 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4776 doMute = true;
4777 outputDesc->mStrategyMutedByDevice[i] = true;
4778 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4779 doMute = true;
4780 outputDesc->mStrategyMutedByDevice[i] = false;
4781 }
Eric Laurent99401132014-05-07 19:48:15 -07004782 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004783 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004784 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004785 // skip output if it does not share any device with current output
4786 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4787 == AUDIO_DEVICE_NONE) {
4788 continue;
4789 }
Eric Laurent37ddbb42016-08-10 16:19:14 -07004790 ALOGVV("checkDeviceMuteStrategies() %s strategy %zu (curDevice %04x)",
Eric Laurentc75307b2015-03-17 15:29:32 -07004791 mute ? "muting" : "unmuting", i, curDevice);
4792 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004793 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004794 if (mute) {
4795 // FIXME: should not need to double latency if volume could be applied
4796 // immediately by the audioflinger mixer. We must account for the delay
4797 // between now and the next time the audioflinger thread for this output
4798 // will process a buffer (which corresponds to one buffer size,
4799 // usually 1/2 or 1/4 of the latency).
4800 if (muteWaitMs < desc->latency() * 2) {
4801 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004802 }
4803 }
4804 }
4805 }
4806 }
4807 }
4808
Eric Laurent99401132014-05-07 19:48:15 -07004809 // temporary mute output if device selection changes to avoid volume bursts due to
4810 // different per device volumes
4811 if (outputDesc->isActive() && (device != prevDevice)) {
Eric Laurentdc462862016-07-19 12:29:53 -07004812 uint32_t tempMuteWaitMs = outputDesc->latency() * 2;
4813 // temporary mute duration is conservatively set to 4 times the reported latency
4814 uint32_t tempMuteDurationMs = outputDesc->latency() * 4;
4815 if (muteWaitMs < tempMuteWaitMs) {
4816 muteWaitMs = tempMuteWaitMs;
Eric Laurent99401132014-05-07 19:48:15 -07004817 }
Eric Laurentdc462862016-07-19 12:29:53 -07004818
Eric Laurent99401132014-05-07 19:48:15 -07004819 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004820 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentdc462862016-07-19 12:29:53 -07004821 // make sure that we do not start the temporary mute period too early in case of
4822 // delayed device change
4823 setStrategyMute((routing_strategy)i, true, outputDesc, delayMs);
Eric Laurentc75307b2015-03-17 15:29:32 -07004824 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurentdc462862016-07-19 12:29:53 -07004825 delayMs + tempMuteDurationMs, device);
Eric Laurent99401132014-05-07 19:48:15 -07004826 }
4827 }
4828 }
4829
Eric Laurente552edb2014-03-10 17:42:56 -07004830 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4831 if (muteWaitMs > delayMs) {
4832 muteWaitMs -= delayMs;
4833 usleep(muteWaitMs * 1000);
4834 return muteWaitMs;
4835 }
4836 return 0;
4837}
4838
Eric Laurentc75307b2015-03-17 15:29:32 -07004839uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004840 audio_devices_t device,
4841 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004842 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004843 audio_patch_handle_t *patchHandle,
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00004844 const char *address,
4845 bool requiresMuteCheck)
Eric Laurente552edb2014-03-10 17:42:56 -07004846{
Eric Laurentc75307b2015-03-17 15:29:32 -07004847 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004848 AudioParameter param;
4849 uint32_t muteWaitMs;
4850
4851 if (outputDesc->isDuplicated()) {
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00004852 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs,
4853 nullptr /* patchHandle */, nullptr /* address */, requiresMuteCheck);
4854 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs,
4855 nullptr /* patchHandle */, nullptr /* address */, requiresMuteCheck);
Eric Laurente552edb2014-03-10 17:42:56 -07004856 return muteWaitMs;
4857 }
4858 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4859 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004860 if ((device != AUDIO_DEVICE_NONE) &&
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00004861 ((device & outputDesc->supportedDevices()) == AUDIO_DEVICE_NONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004862 return 0;
4863 }
4864
4865 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004866 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004867
4868 audio_devices_t prevDevice = outputDesc->mDevice;
4869
Paul McLeanaa981192015-03-21 09:55:15 -07004870 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004871
4872 if (device != AUDIO_DEVICE_NONE) {
4873 outputDesc->mDevice = device;
4874 }
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00004875
4876 // if the outputs are not materially active, there is no need to mute.
4877 if (requiresMuteCheck) {
4878 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4879 } else {
4880 ALOGV("%s: suppressing checkDeviceMuteStrategies", __func__);
4881 muteWaitMs = 0;
4882 }
Eric Laurente552edb2014-03-10 17:42:56 -07004883
4884 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004885 // the requested device is AUDIO_DEVICE_NONE
4886 // OR the requested device is the same as current device
4887 // AND force is not specified
4888 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004889 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004890 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4891 !force &&
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004892 outputDesc->getPatchHandle() != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004893 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004894 return muteWaitMs;
4895 }
4896
4897 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004898
Eric Laurente552edb2014-03-10 17:42:56 -07004899 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004900 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004901 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004902 } else {
Eric Laurentc40d9692016-04-13 19:14:13 -07004903 DeviceVector deviceList;
4904 if ((address == NULL) || (strlen(address) == 0)) {
4905 deviceList = mAvailableOutputDevices.getDevicesFromType(device);
4906 } else {
4907 deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
4908 }
4909
Eric Laurent1c333e22014-05-20 10:48:17 -07004910 if (!deviceList.isEmpty()) {
4911 struct audio_patch patch;
4912 outputDesc->toAudioPortConfig(&patch.sources[0]);
4913 patch.num_sources = 1;
4914 patch.num_sinks = 0;
4915 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4916 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004917 patch.num_sinks++;
4918 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004919 ssize_t index;
4920 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4921 index = mAudioPatches.indexOfKey(*patchHandle);
4922 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004923 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004924 }
4925 sp< AudioPatch> patchDesc;
4926 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4927 if (index >= 0) {
4928 patchDesc = mAudioPatches.valueAt(index);
4929 afPatchHandle = patchDesc->mAfPatchHandle;
4930 }
4931
Eric Laurent1c333e22014-05-20 10:48:17 -07004932 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004933 &afPatchHandle,
4934 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004935 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4936 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004937 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004938 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004939 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004940 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004941 addAudioPatch(patchDesc->mHandle, patchDesc);
4942 } else {
4943 patchDesc->mPatch = patch;
4944 }
4945 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004946 if (patchHandle) {
4947 *patchHandle = patchDesc->mHandle;
4948 }
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004949 outputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004950 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004951 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004952 }
4953 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004954
4955 // inform all input as well
4956 for (size_t i = 0; i < mInputs.size(); i++) {
4957 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004958 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004959 AudioParameter inputCmd = AudioParameter();
4960 ALOGV("%s: inform input %d of device:%d", __func__,
4961 inputDescriptor->mIoHandle, device);
4962 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4963 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4964 inputCmd.toString(),
4965 delayMs);
4966 }
4967 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004968 }
Eric Laurente552edb2014-03-10 17:42:56 -07004969
4970 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004971 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004972
4973 return muteWaitMs;
4974}
4975
Eric Laurentc75307b2015-03-17 15:29:32 -07004976status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004977 int delayMs,
4978 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004979{
Eric Laurent6a94d692014-05-20 11:18:06 -07004980 ssize_t index;
4981 if (patchHandle) {
4982 index = mAudioPatches.indexOfKey(*patchHandle);
4983 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004984 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004985 }
4986 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004987 return INVALID_OPERATION;
4988 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004989 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4990 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004991 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07004992 outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07004993 removeAudioPatch(patchDesc->mHandle);
4994 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004995 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004996 return status;
4997}
4998
4999status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
5000 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07005001 bool force,
5002 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07005003{
5004 status_t status = NO_ERROR;
5005
Eric Laurent1f2f2232014-06-02 12:01:23 -07005006 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07005007 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
5008 inputDesc->mDevice = device;
5009
5010 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
5011 if (!deviceList.isEmpty()) {
5012 struct audio_patch patch;
5013 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07005014 // AUDIO_SOURCE_HOTWORD is for internal use only:
5015 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07005016 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
Eric Laurent599c7582015-12-07 18:05:55 -08005017 !inputDesc->isSoundTrigger()) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07005018 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
5019 }
Eric Laurent1c333e22014-05-20 10:48:17 -07005020 patch.num_sinks = 1;
5021 //only one input device for now
5022 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07005023 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07005024 ssize_t index;
5025 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
5026 index = mAudioPatches.indexOfKey(*patchHandle);
5027 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08005028 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07005029 }
5030 sp< AudioPatch> patchDesc;
5031 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
5032 if (index >= 0) {
5033 patchDesc = mAudioPatches.valueAt(index);
5034 afPatchHandle = patchDesc->mAfPatchHandle;
5035 }
5036
Eric Laurent1c333e22014-05-20 10:48:17 -07005037 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07005038 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07005039 0);
5040 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07005041 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07005042 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07005043 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01005044 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07005045 addAudioPatch(patchDesc->mHandle, patchDesc);
5046 } else {
5047 patchDesc->mPatch = patch;
5048 }
5049 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07005050 if (patchHandle) {
5051 *patchHandle = patchDesc->mHandle;
5052 }
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08005053 inputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07005054 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07005055 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07005056 }
5057 }
5058 }
5059 return status;
5060}
5061
Eric Laurent6a94d692014-05-20 11:18:06 -07005062status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
5063 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07005064{
Eric Laurent1f2f2232014-06-02 12:01:23 -07005065 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07005066 ssize_t index;
5067 if (patchHandle) {
5068 index = mAudioPatches.indexOfKey(*patchHandle);
5069 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08005070 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07005071 }
5072 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07005073 return INVALID_OPERATION;
5074 }
Eric Laurent6a94d692014-05-20 11:18:06 -07005075 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
5076 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07005077 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07005078 inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07005079 removeAudioPatch(patchDesc->mHandle);
5080 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07005081 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07005082 return status;
5083}
5084
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08005085sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07005086 const String8& address,
François Gaffie53615e22015-03-19 09:24:12 +01005087 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07005088 audio_format_t& format,
5089 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01005090 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07005091{
5092 // Choose an input profile based on the requested capture parameters: select the first available
5093 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07005094 //
5095 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
5096 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07005097
5098 for (size_t i = 0; i < mHwModules.size(); i++)
5099 {
5100 if (mHwModules[i]->mHandle == 0) {
5101 continue;
5102 }
5103 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
5104 {
Eric Laurent1c333e22014-05-20 10:48:17 -07005105 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07005106 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08005107 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07005108 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07005109 format,
5110 &format /*updatedFormat*/,
5111 channelMask,
5112 &channelMask /*updatedChannelMask*/,
5113 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08005114
Eric Laurente552edb2014-03-10 17:42:56 -07005115 return profile;
5116 }
5117 }
5118 }
5119 return NULL;
5120}
5121
Eric Laurentc73ca6e2014-12-12 14:34:22 -08005122
5123audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01005124 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07005125{
François Gaffie036e1e92015-03-19 10:16:24 +01005126 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
5127 audio_devices_t selectedDeviceFromMix =
5128 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08005129
François Gaffie036e1e92015-03-19 10:16:24 +01005130 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
5131 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08005132 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08005133 return getDeviceForInputSource(inputSource);
5134}
5135
5136audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
5137{
Eric Laurentad2e7b92017-09-14 20:06:42 -07005138 // Routing
5139 // Scan the whole RouteMap to see if we have an explicit route:
5140 // if the input source in the RouteMap is the same as the argument above,
5141 // and activity count is non-zero and the device in the route descriptor is available
5142 // then select this device.
Paul McLean466dc8e2015-04-17 13:15:36 -06005143 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
5144 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurentad2e7b92017-09-14 20:06:42 -07005145 if ((inputSource == route->mSource) && route->isActive() &&
5146 (mAvailableInputDevices.indexOf(route->mDeviceDescriptor) >= 0)) {
Paul McLean466dc8e2015-04-17 13:15:36 -06005147 return route->mDeviceDescriptor->type();
5148 }
5149 }
5150
5151 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07005152}
5153
Eric Laurente0720872014-03-11 09:30:41 -07005154float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005155 int index,
5156 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005157{
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005158 float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index);
Jean-Michel Trivi3d8b4a42016-09-14 18:37:46 -07005159
5160 // handle the case of accessibility active while a ringtone is playing: if the ringtone is much
5161 // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch
5162 // exploration of the dialer UI. In this situation, bring the accessibility volume closer to
5163 // the ringtone volume
5164 if ((stream == AUDIO_STREAM_ACCESSIBILITY)
5165 && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState())
5166 && isStreamActive(AUDIO_STREAM_RING, 0)) {
5167 const float ringVolumeDB = computeVolume(AUDIO_STREAM_RING, index, device);
5168 return ringVolumeDB - 4 > volumeDB ? ringVolumeDB - 4 : volumeDB;
5169 }
5170
Jean-Michel Trivi719a9872017-08-05 13:51:35 -07005171 // in-call: always cap earpiece volume by voice volume + some low headroom
5172 if ((stream != AUDIO_STREAM_VOICE_CALL) && (device & AUDIO_DEVICE_OUT_EARPIECE) && isInCall()) {
5173 switch (stream) {
5174 case AUDIO_STREAM_SYSTEM:
5175 case AUDIO_STREAM_RING:
5176 case AUDIO_STREAM_MUSIC:
5177 case AUDIO_STREAM_ALARM:
5178 case AUDIO_STREAM_NOTIFICATION:
5179 case AUDIO_STREAM_ENFORCED_AUDIBLE:
5180 case AUDIO_STREAM_DTMF:
5181 case AUDIO_STREAM_ACCESSIBILITY: {
5182 const float maxVoiceVolDb = computeVolume(AUDIO_STREAM_VOICE_CALL, index, device)
5183 + IN_CALL_EARPIECE_HEADROOM_DB;
5184 if (volumeDB > maxVoiceVolDb) {
5185 ALOGV("computeVolume() stream %d at vol=%f overriden by stream %d at vol=%f",
5186 stream, volumeDB, AUDIO_STREAM_VOICE_CALL, maxVoiceVolDb);
5187 volumeDB = maxVoiceVolDb;
5188 }
5189 } break;
5190 default:
5191 break;
5192 }
5193 }
5194
Eric Laurente552edb2014-03-10 17:42:56 -07005195 // if a headset is connected, apply the following rules to ring tones and notifications
5196 // to avoid sound level bursts in user's ears:
Eric Laurent6af1c1d2016-04-14 11:20:44 -07005197 // - always attenuate notifications volume by 6dB
5198 // - attenuate ring tones volume by 6dB unless music is not playing and
5199 // speaker is part of the select devices
Eric Laurente552edb2014-03-10 17:42:56 -07005200 // - if music is playing, always limit the volume to current music volume,
5201 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07005202 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005203 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
5204 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
5205 AUDIO_DEVICE_OUT_WIRED_HEADSET |
Eric Laurent904d6322017-03-17 17:20:47 -07005206 AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
5207 AUDIO_DEVICE_OUT_USB_HEADSET)) &&
Eric Laurente552edb2014-03-10 17:42:56 -07005208 ((stream_strategy == STRATEGY_SONIFICATION)
5209 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07005210 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07005211 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01005212 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
François Gaffied1ab2bd2015-12-02 18:20:06 +01005213 mVolumeCurves->canBeMuted(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005214 // when the phone is ringing we must consider that music could have been paused just before
5215 // by the music application and behave as if music was active if the last music track was
5216 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07005217 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07005218 mLimitRingtoneVolume) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005219 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07005220 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07005221 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005222 mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC,
5223 musicDevice),
5224 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07005225 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
5226 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005227 if (volumeDB > minVolDB) {
5228 volumeDB = minVolDB;
Eric Laurentffbc80f2015-03-18 18:30:19 -07005229 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07005230 }
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005231 if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
5232 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) {
5233 // on A2DP, also ensure notification volume is not too low compared to media when
5234 // intended to be played
5235 if ((volumeDB > -96.0f) &&
5236 (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) {
5237 ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f",
5238 stream, device,
5239 volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB);
5240 volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB;
5241 }
5242 }
Eric Laurent6af1c1d2016-04-14 11:20:44 -07005243 } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) ||
5244 stream_strategy != STRATEGY_SONIFICATION) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005245 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07005246 }
5247 }
5248
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005249 return volumeDB;
Eric Laurente552edb2014-03-10 17:42:56 -07005250}
5251
Eric Laurente0720872014-03-11 09:30:41 -07005252status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005253 int index,
5254 const sp<AudioOutputDescriptor>& outputDesc,
5255 audio_devices_t device,
5256 int delayMs,
5257 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005258{
Eric Laurente552edb2014-03-10 17:42:56 -07005259 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07005260 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07005261 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07005262 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07005263 return NO_ERROR;
5264 }
François Gaffie2110e042015-03-24 08:41:51 +01005265 audio_policy_forced_cfg_t forceUseForComm =
5266 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07005267 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01005268 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
5269 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005270 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01005271 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07005272 return INVALID_OPERATION;
5273 }
5274
Eric Laurentc75307b2015-03-17 15:29:32 -07005275 if (device == AUDIO_DEVICE_NONE) {
5276 device = outputDesc->device();
5277 }
Eric Laurent275e8e92014-11-30 15:14:47 -08005278
Eric Laurentffbc80f2015-03-18 18:30:19 -07005279 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07005280 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07005281 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08005282 }
Eric Laurentc75307b2015-03-17 15:29:32 -07005283
Eric Laurentffbc80f2015-03-18 18:30:19 -07005284 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07005285
Eric Laurent3b73df72014-03-11 09:06:29 -07005286 if (stream == AUDIO_STREAM_VOICE_CALL ||
5287 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07005288 float voiceVolume;
5289 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07005290 if (stream == AUDIO_STREAM_VOICE_CALL) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005291 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005292 } else {
5293 voiceVolume = 1.0;
5294 }
5295
Eric Laurent18fba842016-03-31 14:41:26 -07005296 if (voiceVolume != mLastVoiceVolume) {
Eric Laurente552edb2014-03-10 17:42:56 -07005297 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
5298 mLastVoiceVolume = voiceVolume;
5299 }
5300 }
5301
5302 return NO_ERROR;
5303}
5304
Eric Laurentc75307b2015-03-17 15:29:32 -07005305void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
5306 audio_devices_t device,
5307 int delayMs,
5308 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005309{
Eric Laurentc75307b2015-03-17 15:29:32 -07005310 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07005311
Eric Laurent794fde22016-03-11 09:50:45 -08005312 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005313 checkAndSetVolume((audio_stream_type_t)stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005314 mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005315 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005316 device,
5317 delayMs,
5318 force);
5319 }
5320}
5321
Eric Laurente0720872014-03-11 09:30:41 -07005322void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07005323 bool on,
5324 const sp<AudioOutputDescriptor>& outputDesc,
5325 int delayMs,
5326 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005327{
Eric Laurent72249d52015-06-08 11:12:15 -07005328 ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d",
5329 strategy, on, outputDesc->getId());
Eric Laurent794fde22016-03-11 09:50:45 -08005330 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005331 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005332 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07005333 }
5334 }
5335}
5336
Eric Laurente0720872014-03-11 09:30:41 -07005337void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005338 bool on,
5339 const sp<AudioOutputDescriptor>& outputDesc,
5340 int delayMs,
5341 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005342{
Eric Laurente552edb2014-03-10 17:42:56 -07005343 if (device == AUDIO_DEVICE_NONE) {
5344 device = outputDesc->device();
5345 }
5346
Eric Laurentc75307b2015-03-17 15:29:32 -07005347 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
5348 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07005349
5350 if (on) {
5351 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005352 if (mVolumeCurves->canBeMuted(stream) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07005353 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01005354 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005355 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005356 }
5357 }
5358 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
5359 outputDesc->mMuteCount[stream]++;
5360 } else {
5361 if (outputDesc->mMuteCount[stream] == 0) {
5362 ALOGV("setStreamMute() unmuting non muted stream!");
5363 return;
5364 }
5365 if (--outputDesc->mMuteCount[stream] == 0) {
5366 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005367 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005368 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005369 device,
5370 delayMs);
5371 }
5372 }
5373}
5374
Eric Laurente0720872014-03-11 09:30:41 -07005375void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07005376 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07005377{
Eric Laurent87ffa392015-05-22 10:32:38 -07005378 if(!hasPrimaryOutput()) {
5379 return;
5380 }
5381
Eric Laurente552edb2014-03-10 17:42:56 -07005382 // if the stream pertains to sonification strategy and we are in call we must
5383 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
5384 // in the device used for phone strategy and play the tone if the selected device does not
5385 // interfere with the device used for phone strategy
5386 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
5387 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07005388 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005389 if ((stream_strategy == STRATEGY_SONIFICATION) ||
5390 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005391 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07005392 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
5393 stream, starting, outputDesc->mDevice, stateChange);
5394 if (outputDesc->mRefCount[stream]) {
5395 int muteCount = 1;
5396 if (stateChange) {
5397 muteCount = outputDesc->mRefCount[stream];
5398 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005399 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005400 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
5401 for (int i = 0; i < muteCount; i++) {
5402 setStreamMute(stream, starting, mPrimaryOutput);
5403 }
5404 } else {
5405 ALOGV("handleIncallSonification() high visibility");
5406 if (outputDesc->device() &
5407 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
5408 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
5409 for (int i = 0; i < muteCount; i++) {
5410 setStreamMute(stream, starting, mPrimaryOutput);
5411 }
5412 }
5413 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005414 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
5415 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07005416 } else {
5417 mpClientInterface->stopTone();
5418 }
5419 }
5420 }
5421 }
5422}
5423
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005424audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
5425{
5426 // flags to stream type mapping
5427 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
5428 return AUDIO_STREAM_ENFORCED_AUDIBLE;
5429 }
5430 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
5431 return AUDIO_STREAM_BLUETOOTH_SCO;
5432 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08005433 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
5434 return AUDIO_STREAM_TTS;
5435 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005436
5437 // usage to stream type mapping
5438 switch (attr->usage) {
5439 case AUDIO_USAGE_MEDIA:
5440 case AUDIO_USAGE_GAME:
Jean-Michel Trivi36867762016-12-29 12:03:28 -08005441 case AUDIO_USAGE_ASSISTANT:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005442 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5443 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08005444 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5445 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005446 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5447 return AUDIO_STREAM_SYSTEM;
5448 case AUDIO_USAGE_VOICE_COMMUNICATION:
5449 return AUDIO_STREAM_VOICE_CALL;
5450
5451 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5452 return AUDIO_STREAM_DTMF;
5453
5454 case AUDIO_USAGE_ALARM:
5455 return AUDIO_STREAM_ALARM;
5456 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5457 return AUDIO_STREAM_RING;
5458
5459 case AUDIO_USAGE_NOTIFICATION:
5460 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5461 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5462 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5463 case AUDIO_USAGE_NOTIFICATION_EVENT:
5464 return AUDIO_STREAM_NOTIFICATION;
5465
5466 case AUDIO_USAGE_UNKNOWN:
5467 default:
5468 return AUDIO_STREAM_MUSIC;
5469 }
5470}
Eric Laurente83b55d2014-11-14 10:06:21 -08005471
François Gaffie53615e22015-03-19 09:24:12 +01005472bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
5473{
Eric Laurente83b55d2014-11-14 10:06:21 -08005474 // has flags that map to a strategy?
5475 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
5476 return true;
5477 }
5478
5479 // has known usage?
5480 switch (paa->usage) {
5481 case AUDIO_USAGE_UNKNOWN:
5482 case AUDIO_USAGE_MEDIA:
5483 case AUDIO_USAGE_VOICE_COMMUNICATION:
5484 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5485 case AUDIO_USAGE_ALARM:
5486 case AUDIO_USAGE_NOTIFICATION:
5487 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5488 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5489 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5490 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5491 case AUDIO_USAGE_NOTIFICATION_EVENT:
5492 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5493 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5494 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5495 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08005496 case AUDIO_USAGE_VIRTUAL_SOURCE:
Jean-Michel Trivi36867762016-12-29 12:03:28 -08005497 case AUDIO_USAGE_ASSISTANT:
Eric Laurente83b55d2014-11-14 10:06:21 -08005498 break;
5499 default:
5500 return false;
5501 }
5502 return true;
5503}
5504
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07005505bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor>& outputDesc,
François Gaffiead3183e2015-03-18 16:55:35 +01005506 routing_strategy strategy, uint32_t inPastMs,
5507 nsecs_t sysTime) const
5508{
5509 if ((sysTime == 0) && (inPastMs != 0)) {
5510 sysTime = systemTime();
5511 }
Eric Laurent794fde22016-03-11 09:50:45 -08005512 for (int i = 0; i < (int)AUDIO_STREAM_FOR_POLICY_CNT; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01005513 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
5514 (NUM_STRATEGIES == strategy)) &&
5515 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
5516 return true;
5517 }
5518 }
5519 return false;
5520}
5521
François Gaffie2110e042015-03-24 08:41:51 +01005522audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
5523{
5524 return mEngine->getForceUse(usage);
5525}
5526
5527bool AudioPolicyManager::isInCall()
5528{
5529 return isStateInCall(mEngine->getPhoneState());
5530}
5531
5532bool AudioPolicyManager::isStateInCall(int state)
5533{
5534 return is_state_in_call(state);
5535}
5536
Eric Laurentd60560a2015-04-10 11:31:20 -07005537void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc)
5538{
5539 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
5540 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
5541 if (sourceDesc->mDevice->equals(deviceDesc)) {
5542 ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle());
5543 stopAudioSource(sourceDesc->getHandle());
5544 }
5545 }
5546
5547 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
5548 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
5549 bool release = false;
5550 for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) {
5551 const struct audio_port_config *source = &patchDesc->mPatch.sources[j];
5552 if (source->type == AUDIO_PORT_TYPE_DEVICE &&
5553 source->ext.device.type == deviceDesc->type()) {
5554 release = true;
5555 }
5556 }
5557 for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) {
5558 const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j];
5559 if (sink->type == AUDIO_PORT_TYPE_DEVICE &&
5560 sink->ext.device.type == deviceDesc->type()) {
5561 release = true;
5562 }
5563 }
5564 if (release) {
5565 ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle);
5566 releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid);
5567 }
5568 }
5569}
5570
Phil Burk09bc4612016-02-24 15:58:15 -08005571// Modify the list of surround sound formats supported.
Phil Burk0709b0a2016-03-31 12:54:57 -07005572void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) {
5573 FormatVector &formats = *formatsPtr;
Phil Burk07ac1142016-03-25 13:39:29 -07005574 // TODO Set this based on Config properties.
5575 const bool alwaysForceAC3 = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005576
5577 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5578 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
Phil Burk0709b0a2016-03-31 12:54:57 -07005579 ALOGD("%s: forced use = %d", __FUNCTION__, forceUse);
Phil Burk09bc4612016-02-24 15:58:15 -08005580
5581 // Analyze original support for various formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005582 bool supportsAC3 = false;
5583 bool supportsOtherSurround = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005584 bool supportsIEC61937 = false;
5585 for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) {
5586 audio_format_t format = formats[formatIndex];
Phil Burk09bc4612016-02-24 15:58:15 -08005587 switch (format) {
5588 case AUDIO_FORMAT_AC3:
Phil Burk07ac1142016-03-25 13:39:29 -07005589 supportsAC3 = true;
5590 break;
Phil Burk09bc4612016-02-24 15:58:15 -08005591 case AUDIO_FORMAT_E_AC3:
5592 case AUDIO_FORMAT_DTS:
5593 case AUDIO_FORMAT_DTS_HD:
Phil Burk07ac1142016-03-25 13:39:29 -07005594 supportsOtherSurround = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005595 break;
5596 case AUDIO_FORMAT_IEC61937:
5597 supportsIEC61937 = true;
5598 break;
5599 default:
5600 break;
5601 }
5602 }
Phil Burk09bc4612016-02-24 15:58:15 -08005603
5604 // Modify formats based on surround preferences.
5605 // If NEVER, remove support for surround formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005606 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5607 if (supportsAC3 || supportsOtherSurround || supportsIEC61937) {
5608 // Remove surround sound related formats.
5609 for (size_t formatIndex = 0; formatIndex < formats.size(); ) {
5610 audio_format_t format = formats[formatIndex];
5611 switch(format) {
5612 case AUDIO_FORMAT_AC3:
5613 case AUDIO_FORMAT_E_AC3:
5614 case AUDIO_FORMAT_DTS:
5615 case AUDIO_FORMAT_DTS_HD:
5616 case AUDIO_FORMAT_IEC61937:
Phil Burk07ac1142016-03-25 13:39:29 -07005617 formats.removeAt(formatIndex);
5618 break;
5619 default:
5620 formatIndex++; // keep it
5621 break;
5622 }
Phil Burk09bc4612016-02-24 15:58:15 -08005623 }
Phil Burk07ac1142016-03-25 13:39:29 -07005624 supportsAC3 = false;
5625 supportsOtherSurround = false;
5626 supportsIEC61937 = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005627 }
Phil Burk07ac1142016-03-25 13:39:29 -07005628 } else { // AUTO or ALWAYS
5629 // Most TVs support AC3 even if they do not report it in the EDID.
5630 if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS))
5631 && !supportsAC3) {
5632 formats.add(AUDIO_FORMAT_AC3);
5633 supportsAC3 = true;
5634 }
5635
5636 // If ALWAYS, add support for raw surround formats if all are missing.
5637 // This assumes that if any of these formats are reported by the HAL
5638 // then the report is valid and should not be modified.
5639 if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)
5640 && !supportsOtherSurround) {
5641 formats.add(AUDIO_FORMAT_E_AC3);
5642 formats.add(AUDIO_FORMAT_DTS);
5643 formats.add(AUDIO_FORMAT_DTS_HD);
5644 supportsOtherSurround = true;
5645 }
5646
5647 // Add support for IEC61937 if any raw surround supported.
5648 // The HAL could do this but add it here, just in case.
5649 if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) {
5650 formats.add(AUDIO_FORMAT_IEC61937);
5651 supportsIEC61937 = true;
5652 }
Phil Burk09bc4612016-02-24 15:58:15 -08005653 }
Phil Burk0709b0a2016-03-31 12:54:57 -07005654}
5655
5656// Modify the list of channel masks supported.
5657void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) {
5658 ChannelsVector &channelMasks = *channelMasksPtr;
5659 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5660 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
5661
5662 // If NEVER, then remove support for channelMasks > stereo.
5663 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5664 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) {
5665 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5666 if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) {
5667 ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask);
5668 channelMasks.removeAt(maskIndex);
5669 } else {
5670 maskIndex++;
5671 }
5672 }
5673 // If ALWAYS, then make sure we at least support 5.1
5674 } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) {
5675 bool supports5dot1 = false;
5676 // Are there any channel masks that can be considered "surround"?
5677 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); maskIndex++) {
5678 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5679 if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) {
5680 supports5dot1 = true;
5681 break;
5682 }
5683 }
5684 // If not then add 5.1 support.
5685 if (!supports5dot1) {
5686 channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1);
5687 ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__);
5688 }
Phil Burk09bc4612016-02-24 15:58:15 -08005689 }
5690}
5691
Phil Burk00eeb322016-03-31 12:41:00 -07005692void AudioPolicyManager::updateAudioProfiles(audio_devices_t device,
5693 audio_io_handle_t ioHandle,
François Gaffie112b0af2015-11-19 16:13:25 +01005694 AudioProfileVector &profiles)
5695{
5696 String8 reply;
Phil Burk0709b0a2016-03-31 12:54:57 -07005697
François Gaffie112b0af2015-11-19 16:13:25 +01005698 // Format MUST be checked first to update the list of AudioProfile
5699 if (profiles.hasDynamicFormat()) {
Mikhail Naganov388360c2016-10-17 17:09:41 -07005700 reply = mpClientInterface->getParameters(
5701 ioHandle, String8(AudioParameter::keyStreamSupportedFormats));
Phil Burk0709b0a2016-03-31 12:54:57 -07005702 ALOGV("%s: supported formats %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005703 AudioParameter repliedParameters(reply);
5704 if (repliedParameters.get(
Mikhail Naganov388360c2016-10-17 17:09:41 -07005705 String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) {
François Gaffie112b0af2015-11-19 16:13:25 +01005706 ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__);
5707 return;
5708 }
Phil Burk09bc4612016-02-24 15:58:15 -08005709 FormatVector formats = formatsFromString(reply.string());
Phil Burk00eeb322016-03-31 12:41:00 -07005710 if (device == AUDIO_DEVICE_OUT_HDMI) {
Phil Burk0709b0a2016-03-31 12:54:57 -07005711 filterSurroundFormats(&formats);
Phil Burk00eeb322016-03-31 12:41:00 -07005712 }
Phil Burk09bc4612016-02-24 15:58:15 -08005713 profiles.setFormats(formats);
François Gaffie112b0af2015-11-19 16:13:25 +01005714 }
5715 const FormatVector &supportedFormats = profiles.getSupportedFormats();
5716
Eric Laurent20eb3a42016-01-26 18:39:17 -08005717 for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) {
François Gaffie112b0af2015-11-19 16:13:25 +01005718 audio_format_t format = supportedFormats[formatIndex];
Eric Laurent20eb3a42016-01-26 18:39:17 -08005719 ChannelsVector channelMasks;
5720 SampleRateVector samplingRates;
François Gaffie112b0af2015-11-19 16:13:25 +01005721 AudioParameter requestedParameters;
Mikhail Naganov388360c2016-10-17 17:09:41 -07005722 requestedParameters.addInt(String8(AudioParameter::keyFormat), format);
François Gaffie112b0af2015-11-19 16:13:25 +01005723
5724 if (profiles.hasDynamicRateFor(format)) {
Mikhail Naganov388360c2016-10-17 17:09:41 -07005725 reply = mpClientInterface->getParameters(
5726 ioHandle,
5727 requestedParameters.toString() + ";" +
5728 AudioParameter::keyStreamSupportedSamplingRates);
François Gaffie112b0af2015-11-19 16:13:25 +01005729 ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005730 AudioParameter repliedParameters(reply);
5731 if (repliedParameters.get(
Mikhail Naganov388360c2016-10-17 17:09:41 -07005732 String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) {
Eric Laurent62e4bc52016-02-02 18:37:28 -08005733 samplingRates = samplingRatesFromString(reply.string());
François Gaffie112b0af2015-11-19 16:13:25 +01005734 }
5735 }
5736 if (profiles.hasDynamicChannelsFor(format)) {
5737 reply = mpClientInterface->getParameters(ioHandle,
5738 requestedParameters.toString() + ";" +
Mikhail Naganov388360c2016-10-17 17:09:41 -07005739 AudioParameter::keyStreamSupportedChannels);
François Gaffie112b0af2015-11-19 16:13:25 +01005740 ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005741 AudioParameter repliedParameters(reply);
5742 if (repliedParameters.get(
Mikhail Naganov388360c2016-10-17 17:09:41 -07005743 String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) {
Eric Laurent62e4bc52016-02-02 18:37:28 -08005744 channelMasks = channelMasksFromString(reply.string());
Phil Burk0709b0a2016-03-31 12:54:57 -07005745 if (device == AUDIO_DEVICE_OUT_HDMI) {
5746 filterSurroundChannelMasks(&channelMasks);
5747 }
François Gaffie112b0af2015-11-19 16:13:25 +01005748 }
5749 }
Eric Laurent20eb3a42016-01-26 18:39:17 -08005750 profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates));
François Gaffie112b0af2015-11-19 16:13:25 +01005751 }
5752}
Eric Laurentd60560a2015-04-10 11:31:20 -07005753
Eric Laurente552edb2014-03-10 17:42:56 -07005754}; // namespace android