blob: 80a8dc6c7767b4ac5f5e0e6218dd05eb90b66bf3 [file] [log] [blame]
Eric Laurente552edb2014-03-10 17:42:56 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -070017#define LOG_TAG "APM_AudioPolicyManager"
Eric Laurente552edb2014-03-10 17:42:56 -070018//#define LOG_NDEBUG 0
19
20//#define VERY_VERBOSE_LOGGING
21#ifdef VERY_VERBOSE_LOGGING
22#define ALOGVV ALOGV
23#else
24#define ALOGVV(a...) do { } while(0)
25#endif
26
Jaekyun Seok0d4a6af2017-02-17 17:10:17 +090027#define AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH 128
28#define AUDIO_POLICY_XML_CONFIG_FILE_NAME "audio_policy_configuration.xml"
François Gaffief4ad6e52015-11-19 16:59:57 +010029
Eric Laurentd4692962014-05-05 18:13:44 -070030#include <inttypes.h>
Eric Laurente552edb2014-03-10 17:42:56 -070031#include <math.h>
Eric Laurentd4692962014-05-05 18:13:44 -070032
François Gaffie2110e042015-03-24 08:41:51 +010033#include <AudioPolicyManagerInterface.h>
34#include <AudioPolicyEngineInstance.h>
Mathias Agopian05d19b02017-02-28 16:28:19 -080035#include <cutils/atomic.h>
Eric Laurente552edb2014-03-10 17:42:56 -070036#include <cutils/properties.h>
Eric Laurentd4692962014-05-05 18:13:44 -070037#include <utils/Log.h>
Eric Laurent3b73df72014-03-11 09:06:29 -070038#include <media/AudioParameter.h>
Eric Laurente83b55d2014-11-14 10:06:21 -080039#include <media/AudioPolicyHelper.h>
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070040#include <soundtrigger/SoundTrigger.h>
Mikhail Naganovcbc8f612016-10-11 18:05:13 -070041#include <system/audio.h>
Mikhail Naganov9ee05402016-10-13 15:58:17 -070042#include <audio_policy_conf.h>
Eric Laurentd4692962014-05-05 18:13:44 -070043#include "AudioPolicyManager.h"
François Gaffied1ab2bd2015-12-02 18:20:06 +010044#ifndef USE_XML_AUDIO_POLICY_CONF
François Gaffie53615e22015-03-19 09:24:12 +010045#include <ConfigParsingUtils.h>
François Gaffied1ab2bd2015-12-02 18:20:06 +010046#include <StreamDescriptor.h>
François Gaffief4ad6e52015-11-19 16:59:57 +010047#endif
François Gaffied1ab2bd2015-12-02 18:20:06 +010048#include <Serializer.h>
François Gaffiea8ecc2c2015-11-09 16:10:40 +010049#include "TypeConverter.h"
François Gaffie53615e22015-03-19 09:24:12 +010050#include <policy.h>
Eric Laurente552edb2014-03-10 17:42:56 -070051
Eric Laurent3b73df72014-03-11 09:06:29 -070052namespace android {
Eric Laurente552edb2014-03-10 17:42:56 -070053
Eric Laurentdc462862016-07-19 12:29:53 -070054//FIXME: workaround for truncated touch sounds
55// to be removed when the problem is handled by system UI
56#define TOUCH_SOUND_FIXED_DELAY_MS 100
Jean-Michel Trivi719a9872017-08-05 13:51:35 -070057
58// Largest difference in dB on earpiece in call between the voice volume and another
59// media / notification / system volume.
60constexpr float IN_CALL_EARPIECE_HEADROOM_DB = 3.f;
61
Eric Laurente552edb2014-03-10 17:42:56 -070062// ----------------------------------------------------------------------------
63// AudioPolicyInterface implementation
64// ----------------------------------------------------------------------------
65
Eric Laurente0720872014-03-11 09:30:41 -070066status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
Paul McLeane743a472015-01-28 11:07:31 -080067 audio_policy_dev_state_t state,
68 const char *device_address,
69 const char *device_name)
Eric Laurente552edb2014-03-10 17:42:56 -070070{
Paul McLeane743a472015-01-28 11:07:31 -080071 return setDeviceConnectionStateInt(device, state, device_address, device_name);
Eric Laurentc73ca6e2014-12-12 14:34:22 -080072}
73
François Gaffie44481e72016-04-20 07:49:57 +020074void AudioPolicyManager::broadcastDeviceConnectionState(audio_devices_t device,
75 audio_policy_dev_state_t state,
76 const String8 &device_address)
77{
78 AudioParameter param(device_address);
79 const String8 key(state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE ?
Mikhail Naganov388360c2016-10-17 17:09:41 -070080 AudioParameter::keyStreamConnect : AudioParameter::keyStreamDisconnect);
François Gaffie44481e72016-04-20 07:49:57 +020081 param.addInt(key, device);
82 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
83}
84
Eric Laurentc73ca6e2014-12-12 14:34:22 -080085status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device,
Eric Laurenta1d525f2015-01-29 13:36:45 -080086 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -080087 const char *device_address,
88 const char *device_name)
Eric Laurentc73ca6e2014-12-12 14:34:22 -080089{
Paul McLeane743a472015-01-28 11:07:31 -080090 ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
91- device, state, device_address, device_name);
Eric Laurente552edb2014-03-10 17:42:56 -070092
93 // connect/disconnect only 1 device at a time
94 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
95
François Gaffie53615e22015-03-19 09:24:12 +010096 sp<DeviceDescriptor> devDesc =
97 mHwModules.getDeviceDescriptor(device, device_address, device_name);
Paul McLeane743a472015-01-28 11:07:31 -080098
Eric Laurente552edb2014-03-10 17:42:56 -070099 // handle output devices
100 if (audio_is_output_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700101 SortedVector <audio_io_handle_t> outputs;
102
Eric Laurent3a4311c2014-03-17 12:00:47 -0700103 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
104
Eric Laurente552edb2014-03-10 17:42:56 -0700105 // save a copy of the opened output descriptors before any output is opened or closed
106 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
107 mPreviousOutputs = mOutputs;
Eric Laurente552edb2014-03-10 17:42:56 -0700108 switch (state)
109 {
110 // handle output device connection
Eric Laurent3ae5f312015-02-03 17:12:08 -0800111 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700112 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700113 ALOGW("setDeviceConnectionState() device already connected: %x", device);
114 return INVALID_OPERATION;
115 }
116 ALOGV("setDeviceConnectionState() connecting device %x", device);
117
Eric Laurente552edb2014-03-10 17:42:56 -0700118 // register new device as available
Eric Laurent3a4311c2014-03-17 12:00:47 -0700119 index = mAvailableOutputDevices.add(devDesc);
120 if (index >= 0) {
François Gaffie53615e22015-03-19 09:24:12 +0100121 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurentcf817a22014-08-04 20:36:31 -0700122 if (module == 0) {
123 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
124 device);
125 mAvailableOutputDevices.remove(devDesc);
126 return INVALID_OPERATION;
127 }
Paul McLeane743a472015-01-28 11:07:31 -0800128 mAvailableOutputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700129 } else {
130 return NO_MEMORY;
Eric Laurente552edb2014-03-10 17:42:56 -0700131 }
132
François Gaffie44481e72016-04-20 07:49:57 +0200133 // Before checking outputs, broadcast connect event to allow HAL to retrieve dynamic
134 // parameters on newly connected devices (instead of opening the outputs...)
135 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
136
Eric Laurenta1d525f2015-01-29 13:36:45 -0800137 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700138 mAvailableOutputDevices.remove(devDesc);
François Gaffie44481e72016-04-20 07:49:57 +0200139
140 broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
141 devDesc->mAddress);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700142 return INVALID_OPERATION;
143 }
François Gaffie2110e042015-03-24 08:41:51 +0100144 // Propagate device availability to Engine
145 mEngine->setDeviceConnectionState(devDesc, state);
146
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700147 // outputs should never be empty here
148 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
149 "checkOutputsForDevice() returned no outputs but status OK");
150 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
151 outputs.size());
Eric Laurent3ae5f312015-02-03 17:12:08 -0800152
Eric Laurent3ae5f312015-02-03 17:12:08 -0800153 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700154 // handle output device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700155 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700156 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700157 ALOGW("setDeviceConnectionState() device not connected: %x", device);
158 return INVALID_OPERATION;
159 }
160
Paul McLean5c477aa2014-08-20 16:47:57 -0700161 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
162
Paul McLeane743a472015-01-28 11:07:31 -0800163 // Send Disconnect to HALs
François Gaffie44481e72016-04-20 07:49:57 +0200164 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700165
Eric Laurente552edb2014-03-10 17:42:56 -0700166 // remove device from available output devices
Eric Laurent3a4311c2014-03-17 12:00:47 -0700167 mAvailableOutputDevices.remove(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700168
Eric Laurenta1d525f2015-01-29 13:36:45 -0800169 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
François Gaffie2110e042015-03-24 08:41:51 +0100170
171 // Propagate device availability to Engine
172 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurente552edb2014-03-10 17:42:56 -0700173 } break;
174
175 default:
176 ALOGE("setDeviceConnectionState() invalid state: %x", state);
177 return BAD_VALUE;
178 }
179
Eric Laurent3a4311c2014-03-17 12:00:47 -0700180 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
181 // output is suspended before any tracks are moved to it
Eric Laurente552edb2014-03-10 17:42:56 -0700182 checkA2dpSuspend();
183 checkOutputForAllStrategies();
184 // outputs must be closed after checkOutputForAllStrategies() is executed
185 if (!outputs.isEmpty()) {
186 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700187 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700188 // close unused outputs after device disconnection or direct outputs that have been
189 // opened by checkOutputsForDevice() to query dynamic parameters
Eric Laurent3b73df72014-03-11 09:06:29 -0700190 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Eric Laurente552edb2014-03-10 17:42:56 -0700191 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
192 (desc->mDirectOpenCount == 0))) {
193 closeOutput(outputs[i]);
194 }
195 }
Eric Laurent3a4311c2014-03-17 12:00:47 -0700196 // check again after closing A2DP output to reset mA2dpSuspended if needed
197 checkA2dpSuspend();
Eric Laurente552edb2014-03-10 17:42:56 -0700198 }
199
200 updateDevicesAndOutputs();
Eric Laurent87ffa392015-05-22 10:32:38 -0700201 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700202 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
203 updateCallRouting(newDevice);
204 }
Eric Laurente552edb2014-03-10 17:42:56 -0700205 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700206 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
207 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
208 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700209 // do not force device change on duplicated output because if device is 0, it will
210 // also force a device 0 for the two outputs it is duplicated to which may override
211 // a valid device selection on those outputs.
Eric Laurentc75307b2015-03-17 15:29:32 -0700212 bool force = !desc->isDuplicated()
François Gaffie53615e22015-03-19 09:24:12 +0100213 && (!device_distinguishes_on_address(device)
Eric Laurentc2730ba2014-07-20 15:47:07 -0700214 // always force when disconnecting (a non-duplicated device)
215 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Eric Laurentc75307b2015-03-17 15:29:32 -0700216 setOutputDevice(desc, newDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700217 }
Eric Laurente552edb2014-03-10 17:42:56 -0700218 }
219
Eric Laurentd60560a2015-04-10 11:31:20 -0700220 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
221 cleanUpForDevice(devDesc);
222 }
223
Eric Laurent72aa32f2014-05-30 18:51:48 -0700224 mpClientInterface->onAudioPortListUpdate();
Eric Laurentb71e58b2014-05-29 16:08:11 -0700225 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700226 } // end if is output device
227
Eric Laurente552edb2014-03-10 17:42:56 -0700228 // handle input devices
229 if (audio_is_input_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700230 SortedVector <audio_io_handle_t> inputs;
231
Eric Laurent3a4311c2014-03-17 12:00:47 -0700232 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700233 switch (state)
234 {
235 // handle input device connection
Eric Laurent3b73df72014-03-11 09:06:29 -0700236 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700237 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700238 ALOGW("setDeviceConnectionState() device already connected: %d", device);
239 return INVALID_OPERATION;
240 }
François Gaffie53615e22015-03-19 09:24:12 +0100241 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurent6a94d692014-05-20 11:18:06 -0700242 if (module == NULL) {
243 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
244 device);
245 return INVALID_OPERATION;
246 }
François Gaffie44481e72016-04-20 07:49:57 +0200247
248 // Before checking intputs, broadcast connect event to allow HAL to retrieve dynamic
249 // parameters on newly connected devices (instead of opening the inputs...)
250 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
251
Paul McLean9080a4c2015-06-18 08:24:02 -0700252 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
François Gaffie44481e72016-04-20 07:49:57 +0200253 broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
254 devDesc->mAddress);
Eric Laurentd4692962014-05-05 18:13:44 -0700255 return INVALID_OPERATION;
256 }
257
Eric Laurent3a4311c2014-03-17 12:00:47 -0700258 index = mAvailableInputDevices.add(devDesc);
259 if (index >= 0) {
Paul McLeane743a472015-01-28 11:07:31 -0800260 mAvailableInputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700261 } else {
262 return NO_MEMORY;
263 }
Eric Laurent3ae5f312015-02-03 17:12:08 -0800264
François Gaffie2110e042015-03-24 08:41:51 +0100265 // Propagate device availability to Engine
266 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700267 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700268
269 // handle input device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700270 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700271 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700272 ALOGW("setDeviceConnectionState() device not connected: %d", device);
273 return INVALID_OPERATION;
274 }
Paul McLean5c477aa2014-08-20 16:47:57 -0700275
276 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
277
278 // Set Disconnect to HALs
François Gaffie44481e72016-04-20 07:49:57 +0200279 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700280
Paul McLean9080a4c2015-06-18 08:24:02 -0700281 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700282 mAvailableInputDevices.remove(devDesc);
Paul McLean5c477aa2014-08-20 16:47:57 -0700283
François Gaffie2110e042015-03-24 08:41:51 +0100284 // Propagate device availability to Engine
285 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700286 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700287
288 default:
289 ALOGE("setDeviceConnectionState() invalid state: %x", state);
290 return BAD_VALUE;
291 }
292
Eric Laurentd4692962014-05-05 18:13:44 -0700293 closeAllInputs();
Eric Laurent5f5fca52016-08-04 11:48:57 -0700294 // As the input device list can impact the output device selection, update
295 // getDeviceForStrategy() cache
296 updateDevicesAndOutputs();
Eric Laurente552edb2014-03-10 17:42:56 -0700297
Eric Laurent87ffa392015-05-22 10:32:38 -0700298 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700299 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
300 updateCallRouting(newDevice);
301 }
302
Eric Laurentd60560a2015-04-10 11:31:20 -0700303 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
304 cleanUpForDevice(devDesc);
305 }
306
Eric Laurentb52c1522014-05-20 11:27:36 -0700307 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700308 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700309 } // end if is input device
Eric Laurente552edb2014-03-10 17:42:56 -0700310
311 ALOGW("setDeviceConnectionState() invalid device: %x", device);
312 return BAD_VALUE;
313}
314
Eric Laurente0720872014-03-11 09:30:41 -0700315audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +0100316 const char *device_address)
Eric Laurente552edb2014-03-10 17:42:56 -0700317{
Eric Laurent634b7142016-04-20 13:48:02 -0700318 sp<DeviceDescriptor> devDesc =
319 mHwModules.getDeviceDescriptor(device, device_address, "",
320 (strlen(device_address) != 0)/*matchAddress*/);
321
322 if (devDesc == 0) {
323 ALOGW("getDeviceConnectionState() undeclared device, type %08x, address: %s",
324 device, device_address);
325 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
326 }
François Gaffie53615e22015-03-19 09:24:12 +0100327
Eric Laurent3a4311c2014-03-17 12:00:47 -0700328 DeviceVector *deviceVector;
329
Eric Laurente552edb2014-03-10 17:42:56 -0700330 if (audio_is_output_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700331 deviceVector = &mAvailableOutputDevices;
Eric Laurente552edb2014-03-10 17:42:56 -0700332 } else if (audio_is_input_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700333 deviceVector = &mAvailableInputDevices;
334 } else {
335 ALOGW("getDeviceConnectionState() invalid device type %08x", device);
336 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700337 }
Eric Laurent634b7142016-04-20 13:48:02 -0700338
339 return (deviceVector->getDevice(device, String8(device_address)) != 0) ?
340 AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurenta1d525f2015-01-29 13:36:45 -0800341}
342
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800343status_t AudioPolicyManager::handleDeviceConfigChange(audio_devices_t device,
344 const char *device_address,
345 const char *device_name)
346{
347 status_t status;
348
349 ALOGV("handleDeviceConfigChange(() device: 0x%X, address %s name %s",
350 device, device_address, device_name);
351
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -0800352 // connect/disconnect only 1 device at a time
353 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
354
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800355 // Check if the device is currently connected
356 sp<DeviceDescriptor> devDesc =
357 mHwModules.getDeviceDescriptor(device, device_address, device_name);
358 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
359 if (index < 0) {
360 // Nothing to do: device is not connected
361 return NO_ERROR;
362 }
363
364 // Toggle the device state: UNAVAILABLE -> AVAILABLE
365 // This will force reading again the device configuration
366 status = setDeviceConnectionState(device,
367 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
368 device_address, device_name);
369 if (status != NO_ERROR) {
370 ALOGW("handleDeviceConfigChange() error disabling connection state: %d",
371 status);
372 return status;
373 }
374
375 status = setDeviceConnectionState(device,
376 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
377 device_address, device_name);
378 if (status != NO_ERROR) {
379 ALOGW("handleDeviceConfigChange() error enabling connection state: %d",
380 status);
381 return status;
382 }
383
384 return NO_ERROR;
385}
386
Eric Laurentdc462862016-07-19 12:29:53 -0700387uint32_t AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, uint32_t delayMs)
Eric Laurentc2730ba2014-07-20 15:47:07 -0700388{
389 bool createTxPatch = false;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700390 status_t status;
391 audio_patch_handle_t afPatchHandle;
392 DeviceVector deviceList;
Eric Laurentdc462862016-07-19 12:29:53 -0700393 uint32_t muteWaitMs = 0;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700394
Andy Hunga76c7de2016-12-13 19:14:31 -0800395 if(!hasPrimaryOutput() || mPrimaryOutput->device() == AUDIO_DEVICE_OUT_STUB) {
Eric Laurentdc462862016-07-19 12:29:53 -0700396 return muteWaitMs;
Eric Laurent87ffa392015-05-22 10:32:38 -0700397 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -0800398 audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700399 ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice);
400
401 // release existing RX patch if any
402 if (mCallRxPatch != 0) {
403 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
404 mCallRxPatch.clear();
405 }
406 // release TX patch if any
407 if (mCallTxPatch != 0) {
408 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
409 mCallTxPatch.clear();
410 }
411
412 // If the RX device is on the primary HW module, then use legacy routing method for voice calls
413 // via setOutputDevice() on primary output.
414 // Otherwise, create two audio patches for TX and RX path.
415 if (availablePrimaryOutputDevices() & rxDevice) {
Eric Laurentdc462862016-07-19 12:29:53 -0700416 muteWaitMs = setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700417 // If the TX device is also on the primary HW module, setOutputDevice() will take care
418 // of it due to legacy implementation. If not, create a patch.
419 if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN)
420 == AUDIO_DEVICE_NONE) {
421 createTxPatch = true;
422 }
Eric Laurent8ae73122016-04-12 10:13:29 -0700423 } else { // create RX path audio patch
424 struct audio_patch patch;
425
426 patch.num_sources = 1;
427 patch.num_sinks = 1;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700428 deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice);
429 ALOG_ASSERT(!deviceList.isEmpty(),
430 "updateCallRouting() selected device not in output device list");
431 sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0);
432 deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX);
433 ALOG_ASSERT(!deviceList.isEmpty(),
434 "updateCallRouting() no telephony RX device");
435 sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0);
436
437 rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
438 rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
439
440 // request to reuse existing output stream if one is already opened to reach the RX device
441 SortedVector<audio_io_handle_t> outputs =
442 getOutputsForDevice(rxDevice, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700443 audio_io_handle_t output = selectOutput(outputs,
444 AUDIO_OUTPUT_FLAG_NONE,
445 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700446 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700447 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700448 ALOG_ASSERT(!outputDesc->isDuplicated(),
449 "updateCallRouting() RX device output is duplicated");
450 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700451 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700452 patch.num_sources = 2;
453 }
454
455 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentdc462862016-07-19 12:29:53 -0700456 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700457 ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch",
458 status);
459 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100460 mCallRxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700461 mCallRxPatch->mAfPatchHandle = afPatchHandle;
462 mCallRxPatch->mUid = mUidCached;
463 }
464 createTxPatch = true;
465 }
Eric Laurent8ae73122016-04-12 10:13:29 -0700466 if (createTxPatch) { // create TX path audio patch
Eric Laurentc2730ba2014-07-20 15:47:07 -0700467 struct audio_patch patch;
Eric Laurent8ae73122016-04-12 10:13:29 -0700468
Eric Laurentc2730ba2014-07-20 15:47:07 -0700469 patch.num_sources = 1;
470 patch.num_sinks = 1;
471 deviceList = mAvailableInputDevices.getDevicesFromType(txDevice);
472 ALOG_ASSERT(!deviceList.isEmpty(),
473 "updateCallRouting() selected device not in input device list");
474 sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0);
475 txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
476 deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
477 ALOG_ASSERT(!deviceList.isEmpty(),
478 "updateCallRouting() no telephony TX device");
479 sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0);
480 txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
481
482 SortedVector<audio_io_handle_t> outputs =
483 getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700484 audio_io_handle_t output = selectOutput(outputs,
485 AUDIO_OUTPUT_FLAG_NONE,
486 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700487 // request to reuse existing output stream if one is already opened to reach the TX
488 // path output device
489 if (output != AUDIO_IO_HANDLE_NONE) {
490 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
491 ALOG_ASSERT(!outputDesc->isDuplicated(),
492 "updateCallRouting() RX device output is duplicated");
493 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700494 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700495 patch.num_sources = 2;
496 }
497
Eric Laurentc0a889f2015-10-14 14:36:34 -0700498 // terminate active capture if on the same HW module as the call TX source device
499 // FIXME: would be better to refine to only inputs whose profile connects to the
500 // call TX device but this information is not in the audio patch and logic here must be
501 // symmetric to the one in startInput()
Eric Laurentfb66dd92016-01-28 18:32:03 -0800502 Vector<sp <AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
503 for (size_t i = 0; i < activeInputs.size(); i++) {
504 sp<AudioInputDescriptor> activeDesc = activeInputs[i];
505 if (activeDesc->hasSameHwModuleAs(txSourceDeviceDesc)) {
506 AudioSessionCollection activeSessions =
507 activeDesc->getAudioSessions(true /*activeOnly*/);
508 for (size_t j = 0; j < activeSessions.size(); j++) {
509 audio_session_t activeSession = activeSessions.keyAt(j);
510 stopInput(activeDesc->mIoHandle, activeSession);
511 releaseInput(activeDesc->mIoHandle, activeSession);
512 }
Eric Laurentc0a889f2015-10-14 14:36:34 -0700513 }
514 }
515
Eric Laurentc2730ba2014-07-20 15:47:07 -0700516 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentdc462862016-07-19 12:29:53 -0700517 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700518 ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch",
519 status);
520 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100521 mCallTxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700522 mCallTxPatch->mAfPatchHandle = afPatchHandle;
523 mCallTxPatch->mUid = mUidCached;
524 }
525 }
Eric Laurentdc462862016-07-19 12:29:53 -0700526
527 return muteWaitMs;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700528}
529
Eric Laurente0720872014-03-11 09:30:41 -0700530void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700531{
532 ALOGV("setPhoneState() state %d", state);
François Gaffie2110e042015-03-24 08:41:51 +0100533 // store previous phone state for management of sonification strategy below
534 int oldState = mEngine->getPhoneState();
535
536 if (mEngine->setPhoneState(state) != NO_ERROR) {
537 ALOGW("setPhoneState() invalid or same state %d", state);
Eric Laurente552edb2014-03-10 17:42:56 -0700538 return;
539 }
François Gaffie2110e042015-03-24 08:41:51 +0100540 /// Opens: can these line be executed after the switch of volume curves???
Eric Laurente552edb2014-03-10 17:42:56 -0700541 // if leaving call state, handle special case of active streams
542 // pertaining to sonification strategy see handleIncallSonification()
Eric Laurent63dea1d2015-07-02 17:10:28 -0700543 if (isStateInCall(oldState)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700544 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent794fde22016-03-11 09:50:45 -0800545 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700546 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700547 }
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800548
Eric Laurent63dea1d2015-07-02 17:10:28 -0700549 // force reevaluating accessibility routing when call stops
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800550 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700551 }
552
François Gaffie2110e042015-03-24 08:41:51 +0100553 /**
554 * Switching to or from incall state or switching between telephony and VoIP lead to force
555 * routing command.
556 */
557 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
558 || (is_state_in_call(state) && (state != oldState)));
Eric Laurente552edb2014-03-10 17:42:56 -0700559
560 // check for device and output changes triggered by new phone state
Eric Laurente552edb2014-03-10 17:42:56 -0700561 checkA2dpSuspend();
562 checkOutputForAllStrategies();
563 updateDevicesAndOutputs();
564
Eric Laurente552edb2014-03-10 17:42:56 -0700565 int delayMs = 0;
566 if (isStateInCall(state)) {
567 nsecs_t sysTime = systemTime();
568 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700569 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700570 // mute media and sonification strategies and delay device switch by the largest
571 // latency of any output where either strategy is active.
572 // This avoid sending the ring tone or music tail into the earpiece or headset.
François Gaffiead3183e2015-03-18 16:55:35 +0100573 if ((isStrategyActive(desc, STRATEGY_MEDIA,
574 SONIFICATION_HEADSET_MUSIC_DELAY,
575 sysTime) ||
576 isStrategyActive(desc, STRATEGY_SONIFICATION,
577 SONIFICATION_HEADSET_MUSIC_DELAY,
578 sysTime)) &&
Eric Laurentc75307b2015-03-17 15:29:32 -0700579 (delayMs < (int)desc->latency()*2)) {
580 delayMs = desc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -0700581 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700582 setStrategyMute(STRATEGY_MEDIA, true, desc);
583 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700584 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Eric Laurentc75307b2015-03-17 15:29:32 -0700585 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
586 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700587 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
588 }
589 }
590
Eric Laurent87ffa392015-05-22 10:32:38 -0700591 if (hasPrimaryOutput()) {
592 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
593 // the device returned is not necessarily reachable via this output
594 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
595 // force routing command to audio hardware when ending call
596 // even if no device change is needed
597 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
598 rxDevice = mPrimaryOutput->device();
599 }
Eric Laurente552edb2014-03-10 17:42:56 -0700600
Eric Laurent87ffa392015-05-22 10:32:38 -0700601 if (state == AUDIO_MODE_IN_CALL) {
602 updateCallRouting(rxDevice, delayMs);
603 } else if (oldState == AUDIO_MODE_IN_CALL) {
604 if (mCallRxPatch != 0) {
605 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
606 mCallRxPatch.clear();
607 }
608 if (mCallTxPatch != 0) {
609 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
610 mCallTxPatch.clear();
611 }
612 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
613 } else {
614 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700615 }
Eric Laurentc2730ba2014-07-20 15:47:07 -0700616 }
Eric Laurente552edb2014-03-10 17:42:56 -0700617 // if entering in call state, handle special case of active streams
618 // pertaining to sonification strategy see handleIncallSonification()
619 if (isStateInCall(state)) {
620 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent794fde22016-03-11 09:50:45 -0800621 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700622 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700623 }
Eric Laurent63dea1d2015-07-02 17:10:28 -0700624
625 // force reevaluating accessibility routing when call starts
626 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700627 }
628
629 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700630 if (state == AUDIO_MODE_RINGTONE &&
631 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700632 mLimitRingtoneVolume = true;
633 } else {
634 mLimitRingtoneVolume = false;
635 }
636}
637
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700638audio_mode_t AudioPolicyManager::getPhoneState() {
639 return mEngine->getPhoneState();
640}
641
Eric Laurente0720872014-03-11 09:30:41 -0700642void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700643 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700644{
François Gaffie2110e042015-03-24 08:41:51 +0100645 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Eric Laurent8dc87a62017-05-16 19:00:40 -0700646 if (config == mEngine->getForceUse(usage)) {
647 return;
648 }
Eric Laurente552edb2014-03-10 17:42:56 -0700649
François Gaffie2110e042015-03-24 08:41:51 +0100650 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
651 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
652 return;
Eric Laurente552edb2014-03-10 17:42:56 -0700653 }
François Gaffie2110e042015-03-24 08:41:51 +0100654 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
655 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
656 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
Eric Laurente552edb2014-03-10 17:42:56 -0700657
658 // check for device and output changes triggered by new force usage
659 checkA2dpSuspend();
660 checkOutputForAllStrategies();
661 updateDevicesAndOutputs();
Phil Burk09bc4612016-02-24 15:58:15 -0800662
Eric Laurentdc462862016-07-19 12:29:53 -0700663 //FIXME: workaround for truncated touch sounds
664 // to be removed when the problem is handled by system UI
665 uint32_t delayMs = 0;
666 uint32_t waitMs = 0;
667 if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) {
668 delayMs = TOUCH_SOUND_FIXED_DELAY_MS;
669 }
Eric Laurent87ffa392015-05-22 10:32:38 -0700670 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700671 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
Eric Laurentdc462862016-07-19 12:29:53 -0700672 waitMs = updateCallRouting(newDevice, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700673 }
Eric Laurente552edb2014-03-10 17:42:56 -0700674 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700675 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
676 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
677 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
Eric Laurentdc462862016-07-19 12:29:53 -0700678 waitMs = setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE),
679 delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700680 }
Eric Laurente552edb2014-03-10 17:42:56 -0700681 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
Eric Laurentdc462862016-07-19 12:29:53 -0700682 applyStreamVolumes(outputDesc, newDevice, waitMs, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700683 }
684 }
685
Eric Laurentfb66dd92016-01-28 18:32:03 -0800686 Vector<sp <AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
687 for (size_t i = 0; i < activeInputs.size(); i++) {
688 sp<AudioInputDescriptor> activeDesc = activeInputs[i];
689 audio_devices_t newDevice = getNewInputDevice(activeDesc);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700690 // Force new input selection if the new device can not be reached via current input
Eric Laurentfb66dd92016-01-28 18:32:03 -0800691 if (activeDesc->mProfile->getSupportedDevices().types() &
692 (newDevice & ~AUDIO_DEVICE_BIT_IN)) {
693 setInputDevice(activeDesc->mIoHandle, newDevice);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700694 } else {
Eric Laurentfb66dd92016-01-28 18:32:03 -0800695 closeInput(activeDesc->mIoHandle);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700696 }
Eric Laurente552edb2014-03-10 17:42:56 -0700697 }
Eric Laurente552edb2014-03-10 17:42:56 -0700698}
699
Eric Laurente0720872014-03-11 09:30:41 -0700700void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700701{
702 ALOGV("setSystemProperty() property %s, value %s", property, value);
703}
704
705// Find a direct output profile compatible with the parameters passed, even if the input flags do
706// not explicitly request a direct output
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800707sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700708 audio_devices_t device,
709 uint32_t samplingRate,
710 audio_format_t format,
711 audio_channel_mask_t channelMask,
712 audio_output_flags_t flags)
713{
Eric Laurent861a6282015-05-18 15:40:16 -0700714 // only retain flags that will drive the direct output profile selection
715 // if explicitly requested
716 static const uint32_t kRelevantFlags =
Haynes Mathew George84c621e2017-04-25 11:41:50 -0700717 (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD |
718 AUDIO_OUTPUT_FLAG_VOIP_RX);
Eric Laurent861a6282015-05-18 15:40:16 -0700719 flags =
720 (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT);
721
722 sp<IOProfile> profile;
723
Eric Laurente552edb2014-03-10 17:42:56 -0700724 for (size_t i = 0; i < mHwModules.size(); i++) {
725 if (mHwModules[i]->mHandle == 0) {
726 continue;
727 }
728 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent861a6282015-05-18 15:40:16 -0700729 sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j];
730 if (!curProfile->isCompatibleProfile(device, String8(""),
Andy Hungf129b032015-04-07 13:45:50 -0700731 samplingRate, NULL /*updatedSamplingRate*/,
732 format, NULL /*updatedFormat*/,
733 channelMask, NULL /*updatedChannelMask*/,
Eric Laurent861a6282015-05-18 15:40:16 -0700734 flags)) {
735 continue;
736 }
737 // reject profiles not corresponding to a device currently available
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100738 if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) {
Eric Laurent861a6282015-05-18 15:40:16 -0700739 continue;
740 }
741 // if several profiles are compatible, give priority to one with offload capability
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100742 if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) {
Eric Laurent861a6282015-05-18 15:40:16 -0700743 continue;
744 }
745 profile = curProfile;
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100746 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent861a6282015-05-18 15:40:16 -0700747 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700748 }
Eric Laurente552edb2014-03-10 17:42:56 -0700749 }
750 }
Eric Laurent861a6282015-05-18 15:40:16 -0700751 return profile;
Eric Laurente552edb2014-03-10 17:42:56 -0700752}
753
Eric Laurentf4e63452017-11-06 19:31:46 +0000754audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream)
Eric Laurente552edb2014-03-10 17:42:56 -0700755{
Eric Laurent3b73df72014-03-11 09:06:29 -0700756 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700757 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Andy Hungc9901522017-11-10 20:07:54 -0800758
759 // Note that related method getOutputForAttr() uses getOutputForDevice() not selectOutput().
760 // We use selectOutput() here since we don't have the desired AudioTrack sample rate,
761 // format, flags, etc. This may result in some discrepancy for functions that utilize
762 // getOutput() solely on audio_stream_type such as AudioSystem::getOutputFrameCount()
763 // and AudioSystem::getOutputSamplingRate().
764
Eric Laurentf4e63452017-11-06 19:31:46 +0000765 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
766 audio_io_handle_t output = selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID);
Eric Laurente552edb2014-03-10 17:42:56 -0700767
Eric Laurentf4e63452017-11-06 19:31:46 +0000768 ALOGV("getOutput() stream %d selected device %08x, output %d", stream, device, output);
769 return output;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700770}
771
Eric Laurente83b55d2014-11-14 10:06:21 -0800772status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
773 audio_io_handle_t *output,
774 audio_session_t session,
775 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700776 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800777 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800778 audio_output_flags_t flags,
Eric Laurent2ac76942017-06-22 17:17:09 -0700779 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800780 audio_port_handle_t *portId)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700781{
Eric Laurente83b55d2014-11-14 10:06:21 -0800782 audio_attributes_t attributes;
783 if (attr != NULL) {
784 if (!isValidAttributes(attr)) {
785 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
786 attr->usage, attr->content_type, attr->flags,
787 attr->tags);
788 return BAD_VALUE;
789 }
790 attributes = *attr;
791 } else {
792 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
793 ALOGE("getOutputForAttr(): invalid stream type");
794 return BAD_VALUE;
795 }
796 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700797 }
Eric Laurent20b9ef02016-12-05 11:03:16 -0800798
799 // TODO: check for existing client for this port ID
800 if (*portId == AUDIO_PORT_HANDLE_NONE) {
801 *portId = AudioPort::getNextUniqueId();
802 }
803
Eric Laurentc75307b2015-03-17 15:29:32 -0700804 sp<SwAudioOutputDescriptor> desc;
Jean-Michel Trivie8deced2016-02-11 12:50:39 -0800805 if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) {
François Gaffie036e1e92015-03-19 10:16:24 +0100806 ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr");
Eric Laurent20b9ef02016-12-05 11:03:16 -0800807 if (!audio_has_proportional_frames(config->format)) {
François Gaffie036e1e92015-03-19 10:16:24 +0100808 return BAD_VALUE;
Eric Laurent275e8e92014-11-30 15:14:47 -0800809 }
François Gaffie036e1e92015-03-19 10:16:24 +0100810 *stream = streamTypefromAttributesInt(&attributes);
811 *output = desc->mIoHandle;
812 ALOGV("getOutputForAttr() returns output %d", *output);
813 return NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -0800814 }
815 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
816 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
817 return BAD_VALUE;
818 }
819
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700820 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x"
821 " session %d selectedDeviceId %d",
822 attributes.usage, attributes.content_type, attributes.tags, attributes.flags,
Eric Laurent2ac76942017-06-22 17:17:09 -0700823 session, *selectedDeviceId);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700824
825 *stream = streamTypefromAttributesInt(&attributes);
826
827 // Explicit routing?
828 sp<DeviceDescriptor> deviceDesc;
Eric Laurent2ac76942017-06-22 17:17:09 -0700829 if (*selectedDeviceId != AUDIO_PORT_HANDLE_NONE) {
830 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
831 if (mAvailableOutputDevices[i]->getId() == *selectedDeviceId) {
832 deviceDesc = mAvailableOutputDevices[i];
833 break;
834 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700835 }
836 }
837 mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700838
Eric Laurente83b55d2014-11-14 10:06:21 -0800839 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700840 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700841
Eric Laurente83b55d2014-11-14 10:06:21 -0800842 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700843 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
844 }
845
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700846 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Eric Laurent20b9ef02016-12-05 11:03:16 -0800847 device, config->sample_rate, config->format, config->channel_mask, flags);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700848
Kevin Rocard169753c2017-03-06 14:18:23 -0800849 *output = getOutputForDevice(device, session, *stream,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800850 config->sample_rate, config->format, config->channel_mask,
851 flags, &config->offload_info);
Eric Laurente83b55d2014-11-14 10:06:21 -0800852 if (*output == AUDIO_IO_HANDLE_NONE) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700853 mOutputRoutes.removeRoute(session);
Eric Laurente83b55d2014-11-14 10:06:21 -0800854 return INVALID_OPERATION;
855 }
Paul McLeanaa981192015-03-21 09:55:15 -0700856
Eric Laurent2ac76942017-06-22 17:17:09 -0700857 DeviceVector outputDevices = mAvailableOutputDevices.getDevicesFromType(device);
858 *selectedDeviceId = outputDevices.size() > 0 ? outputDevices.itemAt(0)->getId()
859 : AUDIO_PORT_HANDLE_NONE;
860
861 ALOGV(" getOutputForAttr() returns output %d selectedDeviceId %d", *output, *selectedDeviceId);
862
Eric Laurente83b55d2014-11-14 10:06:21 -0800863 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700864}
865
866audio_io_handle_t AudioPolicyManager::getOutputForDevice(
867 audio_devices_t device,
Kevin Rocard169753c2017-03-06 14:18:23 -0800868 audio_session_t session,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700869 audio_stream_type_t stream,
870 uint32_t samplingRate,
871 audio_format_t format,
872 audio_channel_mask_t channelMask,
873 audio_output_flags_t flags,
874 const audio_offload_info_t *offloadInfo)
875{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700876 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700877 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700878
Eric Laurente552edb2014-03-10 17:42:56 -0700879 // open a direct output if required by specified parameters
880 //force direct flag if offload flag is set: offloading implies a direct output stream
881 // and all common behaviors are driven by checking only the direct flag
882 // this should normally be set appropriately in the policy configuration file
883 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700884 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700885 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700886 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
887 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
888 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800889 // only allow deep buffering for music stream type
890 if (stream != AUDIO_STREAM_MUSIC) {
891 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Ravi Kumar Alamanda439e4ed2015-04-03 12:13:21 -0700892 } else if (/* stream == AUDIO_STREAM_MUSIC && */
893 flags == AUDIO_OUTPUT_FLAG_NONE &&
894 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
895 // use DEEP_BUFFER as default output for music stream type
896 flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Eric Laurente83b55d2014-11-14 10:06:21 -0800897 }
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700898 if (stream == AUDIO_STREAM_TTS) {
899 flags = AUDIO_OUTPUT_FLAG_TTS;
Haynes Mathew George84c621e2017-04-25 11:41:50 -0700900 } else if (stream == AUDIO_STREAM_VOICE_CALL &&
Haynes Mathew George84c621e2017-04-25 11:41:50 -0700901 audio_is_linear_pcm(format)) {
902 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX |
903 AUDIO_OUTPUT_FLAG_DIRECT);
904 ALOGV("Set VoIP and Direct output flags for PCM format");
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700905 }
Eric Laurente552edb2014-03-10 17:42:56 -0700906
Eric Laurentb732cf52014-09-24 19:08:21 -0700907 sp<IOProfile> profile;
908
909 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700910 // and not explicitly requested
911 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
Glenn Kasten05ddca52016-02-11 08:17:12 -0800912 audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700913 audio_channel_count_from_out_mask(channelMask) <= 2) {
914 goto non_direct_output;
915 }
916
Andy Hung2ddee192015-12-18 17:34:44 -0800917 // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled.
918 // This prevents creating an offloaded track and tearing it down immediately after start
919 // when audioflinger detects there is an active non offloadable effect.
Eric Laurente552edb2014-03-10 17:42:56 -0700920 // FIXME: We should check the audio session here but we do not have it in this context.
921 // This may prevent offloading in rare situations where effects are left active by apps
922 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700923
Eric Laurente552edb2014-03-10 17:42:56 -0700924 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
Andy Hung2ddee192015-12-18 17:34:44 -0800925 !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700926 profile = getProfileForDirectOutput(device,
927 samplingRate,
928 format,
929 channelMask,
930 (audio_output_flags_t)flags);
931 }
932
Eric Laurent1c333e22014-05-20 10:48:17 -0700933 if (profile != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700934 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700935
936 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700937 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700938 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
939 outputDesc = desc;
Kevin Rocard551061a2017-02-06 15:59:29 -0800940 // reuse direct output if currently open by the same client
941 // and configured with same parameters
Eric Laurente552edb2014-03-10 17:42:56 -0700942 if ((samplingRate == outputDesc->mSamplingRate) &&
Kevin Rocard551061a2017-02-06 15:59:29 -0800943 audio_formats_match(format, outputDesc->mFormat) &&
944 (channelMask == outputDesc->mChannelMask)) {
Kevin Rocard169753c2017-03-06 14:18:23 -0800945 if (session == outputDesc->mDirectClientSession) {
Kevin Rocard551061a2017-02-06 15:59:29 -0800946 outputDesc->mDirectOpenCount++;
Eric Laurentf4e63452017-11-06 19:31:46 +0000947 ALOGV("getOutputForDevice() reusing direct output %d for session %d",
Kevin Rocard169753c2017-03-06 14:18:23 -0800948 mOutputs.keyAt(i), session);
Kevin Rocard551061a2017-02-06 15:59:29 -0800949 return mOutputs.keyAt(i);
950 } else {
Eric Laurentf4e63452017-11-06 19:31:46 +0000951 ALOGV("getOutputForDevice() do not reuse direct output because"
952 "current client (%d) is not the same as requesting client (%d)",
Kevin Rocard169753c2017-03-06 14:18:23 -0800953 outputDesc->mDirectClientSession, session);
Kevin Rocard551061a2017-02-06 15:59:29 -0800954 goto non_direct_output;
955 }
Eric Laurente552edb2014-03-10 17:42:56 -0700956 }
957 }
958 }
959 // close direct output if currently open and configured with different parameters
960 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700961 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700962 }
Eric Laurent861a6282015-05-18 15:40:16 -0700963
964 // if the selected profile is offloaded and no offload info was specified,
965 // create a default one
966 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100967 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
Eric Laurent861a6282015-05-18 15:40:16 -0700968 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
969 defaultOffloadInfo.sample_rate = samplingRate;
970 defaultOffloadInfo.channel_mask = channelMask;
971 defaultOffloadInfo.format = format;
972 defaultOffloadInfo.stream_type = stream;
973 defaultOffloadInfo.bit_rate = 0;
974 defaultOffloadInfo.duration_us = -1;
975 defaultOffloadInfo.has_video = true; // conservative
976 defaultOffloadInfo.is_streaming = true; // likely
977 offloadInfo = &defaultOffloadInfo;
978 }
979
Eric Laurentc75307b2015-03-17 15:29:32 -0700980 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700981 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -0700982 outputDesc->mLatency = 0;
Eric Laurent861a6282015-05-18 15:40:16 -0700983 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700984 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
985 config.sample_rate = samplingRate;
986 config.channel_mask = channelMask;
987 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -0700988 if (offloadInfo != NULL) {
989 config.offload_info = *offloadInfo;
990 }
Eric Laurente3014102017-05-03 11:15:43 -0700991 DeviceVector outputDevices = mAvailableOutputDevices.getDevicesFromType(device);
992 String8 address = outputDevices.size() > 0 ? outputDevices.itemAt(0)->mAddress
993 : String8("");
Eric Laurent322b4d22015-04-03 15:57:54 -0700994 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -0700995 &output,
996 &config,
997 &outputDesc->mDevice,
Eric Laurente3014102017-05-03 11:15:43 -0700998 address,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700999 &outputDesc->mLatency,
1000 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001001
1002 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -07001003 if (status != NO_ERROR ||
1004 (samplingRate != 0 && samplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -08001005 (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) ||
Eric Laurentcf2c0212014-07-25 16:20:43 -07001006 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurentf4e63452017-11-06 19:31:46 +00001007 ALOGV("getOutputForDevice() failed opening direct output: output %d samplingRate %d %d,"
Eric Laurente552edb2014-03-10 17:42:56 -07001008 "format %d %d, channelMask %04x %04x", output, samplingRate,
1009 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1010 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001011 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07001012 mpClientInterface->closeOutput(output);
1013 }
Eric Laurenta82797f2015-01-30 11:49:43 -08001014 // fall back to mixer output if possible when the direct output could not be open
Glenn Kasten05ddca52016-02-11 08:17:12 -08001015 if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) {
Eric Laurenta82797f2015-01-30 11:49:43 -08001016 goto non_direct_output;
1017 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001018 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001019 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001020 outputDesc->mSamplingRate = config.sample_rate;
1021 outputDesc->mChannelMask = config.channel_mask;
1022 outputDesc->mFormat = config.format;
1023 outputDesc->mRefCount[stream] = 0;
1024 outputDesc->mStopTime[stream] = 0;
1025 outputDesc->mDirectOpenCount = 1;
Kevin Rocard169753c2017-03-06 14:18:23 -08001026 outputDesc->mDirectClientSession = session;
1027
Eric Laurente552edb2014-03-10 17:42:56 -07001028 addOutput(output, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07001029 mPreviousOutputs = mOutputs;
Eric Laurentf4e63452017-11-06 19:31:46 +00001030 ALOGV("getOutputForDevice() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -07001031 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001032 return output;
1033 }
1034
Eric Laurentb732cf52014-09-24 19:08:21 -07001035non_direct_output:
Eric Laurent14cbfca2016-03-17 09:42:16 -07001036
1037 // A request for HW A/V sync cannot fallback to a mixed output because time
1038 // stamps are embedded in audio data
1039 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1040 return AUDIO_IO_HANDLE_NONE;
1041 }
1042
Eric Laurente552edb2014-03-10 17:42:56 -07001043 // ignoring channel mask due to downmix capability in mixer
1044
1045 // open a non direct output
1046
1047 // for non direct outputs, only PCM is supported
1048 if (audio_is_linear_pcm(format)) {
1049 // get which output is suitable for the specified stream. The actual
1050 // routing change will happen when startOutput() will be called
1051 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1052
Eric Laurent8838a382014-09-08 16:44:28 -07001053 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1054 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1055 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -07001056 }
Eric Laurentf4e63452017-11-06 19:31:46 +00001057 ALOGW_IF((output == 0), "getOutputForDevice() could not find output for stream %d, "
1058 "samplingRate %d, format %d, channels %x, flags %x",
1059 stream, samplingRate, format, channelMask, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001060
Eric Laurente552edb2014-03-10 17:42:56 -07001061 return output;
1062}
1063
Eric Laurente0720872014-03-11 09:30:41 -07001064audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -07001065 audio_output_flags_t flags,
1066 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -07001067{
1068 // select one output among several that provide a path to a particular device or set of
1069 // devices (the list was previously build by getOutputsForDevice()).
1070 // The priority is as follows:
1071 // 1: the output with the highest number of requested policy flags
Eric Laurente6930022016-02-11 10:20:40 -08001072 // 2: the output with the bit depth the closest to the requested one
1073 // 3: the primary output
1074 // 4: the first output in the list
Eric Laurente552edb2014-03-10 17:42:56 -07001075
1076 if (outputs.size() == 0) {
1077 return 0;
1078 }
1079 if (outputs.size() == 1) {
1080 return outputs[0];
1081 }
1082
1083 int maxCommonFlags = 0;
Eric Laurente6930022016-02-11 10:20:40 -08001084 audio_io_handle_t outputForFlags = 0;
1085 audio_io_handle_t outputForPrimary = 0;
1086 audio_io_handle_t outputForFormat = 0;
1087 audio_format_t bestFormat = AUDIO_FORMAT_INVALID;
1088 audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07001089
1090 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001091 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -07001092 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -07001093 // if a valid format is specified, skip output if not compatible
1094 if (format != AUDIO_FORMAT_INVALID) {
1095 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente6930022016-02-11 10:20:40 -08001096 if (!audio_formats_match(format, outputDesc->mFormat)) {
Eric Laurent8838a382014-09-08 16:44:28 -07001097 continue;
1098 }
1099 } else if (!audio_is_linear_pcm(format)) {
1100 continue;
1101 }
Eric Laurente6930022016-02-11 10:20:40 -08001102 if (AudioPort::isBetterFormatMatch(
1103 outputDesc->mFormat, bestFormat, format)) {
1104 outputForFormat = outputs[i];
1105 bestFormat = outputDesc->mFormat;
1106 }
Eric Laurent8838a382014-09-08 16:44:28 -07001107 }
1108
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001109 int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags);
Eric Laurente6930022016-02-11 10:20:40 -08001110 if (commonFlags >= maxCommonFlags) {
1111 if (commonFlags == maxCommonFlags) {
Andy Hungc9901522017-11-10 20:07:54 -08001112 if (format != AUDIO_FORMAT_INVALID
1113 && AudioPort::isBetterFormatMatch(
1114 outputDesc->mFormat, bestFormatForFlags, format)) {
Eric Laurente6930022016-02-11 10:20:40 -08001115 outputForFlags = outputs[i];
1116 bestFormatForFlags = outputDesc->mFormat;
1117 }
1118 } else {
1119 outputForFlags = outputs[i];
1120 maxCommonFlags = commonFlags;
1121 bestFormatForFlags = outputDesc->mFormat;
1122 }
Eric Laurente552edb2014-03-10 17:42:56 -07001123 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
1124 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001125 if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurente6930022016-02-11 10:20:40 -08001126 outputForPrimary = outputs[i];
Eric Laurente552edb2014-03-10 17:42:56 -07001127 }
1128 }
1129 }
1130
Eric Laurente6930022016-02-11 10:20:40 -08001131 if (outputForFlags != 0) {
1132 return outputForFlags;
Eric Laurente552edb2014-03-10 17:42:56 -07001133 }
Eric Laurente6930022016-02-11 10:20:40 -08001134 if (outputForFormat != 0) {
1135 return outputForFormat;
1136 }
1137 if (outputForPrimary != 0) {
1138 return outputForPrimary;
Eric Laurente552edb2014-03-10 17:42:56 -07001139 }
1140
1141 return outputs[0];
1142}
1143
Eric Laurente0720872014-03-11 09:30:41 -07001144status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001145 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001146 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001147{
Paul McLeanaa981192015-03-21 09:55:15 -07001148 ALOGV("startOutput() output %d, stream %d, session %d",
1149 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -07001150 ssize_t index = mOutputs.indexOfKey(output);
1151 if (index < 0) {
1152 ALOGW("startOutput() unknown output %d", output);
1153 return BAD_VALUE;
1154 }
1155
Eric Laurentc75307b2015-03-17 15:29:32 -07001156 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
1157
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001158 // Routing?
1159 mOutputRoutes.incRouteActivity(session);
1160
Eric Laurentc75307b2015-03-17 15:29:32 -07001161 audio_devices_t newDevice;
Eric Laurentc40d9692016-04-13 19:14:13 -07001162 AudioMix *policyMix = NULL;
1163 const char *address = NULL;
Eric Laurentc75307b2015-03-17 15:29:32 -07001164 if (outputDesc->mPolicyMix != NULL) {
Eric Laurentc40d9692016-04-13 19:14:13 -07001165 policyMix = outputDesc->mPolicyMix;
1166 address = policyMix->mDeviceAddress.string();
1167 if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
1168 newDevice = policyMix->mDeviceType;
1169 } else {
1170 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
1171 }
Eric Laurent493404d2015-04-21 15:07:36 -07001172 } else if (mOutputRoutes.hasRouteChanged(session)) {
1173 newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001174 checkStrategyRoute(getStrategy(stream), output);
Eric Laurentc75307b2015-03-17 15:29:32 -07001175 } else {
1176 newDevice = AUDIO_DEVICE_NONE;
1177 }
1178
1179 uint32_t delayMs = 0;
1180
Eric Laurentc40d9692016-04-13 19:14:13 -07001181 status_t status = startSource(outputDesc, stream, newDevice, address, &delayMs);
Eric Laurentc75307b2015-03-17 15:29:32 -07001182
1183 if (status != NO_ERROR) {
1184 mOutputRoutes.decRouteActivity(session);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001185 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -07001186 }
1187 // Automatically enable the remote submix input when output is started on a re routing mix
1188 // of type MIX_TYPE_RECORDERS
Eric Laurentc40d9692016-04-13 19:14:13 -07001189 if (audio_is_remote_submix_device(newDevice) && policyMix != NULL &&
1190 policyMix->mMixType == MIX_TYPE_RECORDERS) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001191 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1192 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Eric Laurentc40d9692016-04-13 19:14:13 -07001193 address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001194 "remote-submix");
1195 }
1196
1197 if (delayMs != 0) {
1198 usleep(delayMs * 1000);
1199 }
1200
1201 return status;
1202}
1203
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001204status_t AudioPolicyManager::startSource(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurentc75307b2015-03-17 15:29:32 -07001205 audio_stream_type_t stream,
1206 audio_devices_t device,
Eric Laurentc40d9692016-04-13 19:14:13 -07001207 const char *address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001208 uint32_t *delayMs)
1209{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001210 // cannot start playback of STREAM_TTS if any other output is being used
1211 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001212
1213 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001214 if (stream == AUDIO_STREAM_TTS) {
1215 ALOGV("\t found BEACON stream");
Eric Laurent9459fb02015-08-12 18:36:32 -07001216 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001217 return INVALID_OPERATION;
1218 } else {
1219 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1220 }
1221 } else {
1222 // some playback other than beacon starts
1223 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1224 }
1225
Eric Laurent77305a62016-07-25 16:39:22 -07001226 // force device change if the output is inactive and no audio patch is already present.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001227 // check active before incrementing usage count
Eric Laurent77305a62016-07-25 16:39:22 -07001228 bool force = !outputDesc->isActive() &&
1229 (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001230
Andy Hung7c7124e2017-10-20 12:03:34 -07001231 // requiresMuteCheck is false when we can bypass mute strategy.
1232 // It covers a common case when there is no materially active audio
1233 // and muting would result in unnecessary delay and dropped audio.
1234 const uint32_t outputLatencyMs = outputDesc->latency();
1235 bool requiresMuteCheck = outputDesc->isActive(outputLatencyMs * 2); // account for drain
1236
Eric Laurente552edb2014-03-10 17:42:56 -07001237 // increment usage count for this stream on the requested output:
1238 // NOTE that the usage count is the same for duplicated output and hardware output which is
1239 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1240 outputDesc->changeRefCount(stream, 1);
1241
Eric Laurent36829f92017-04-07 19:04:42 -07001242 if (stream == AUDIO_STREAM_MUSIC) {
1243 selectOutputForMusicEffects();
1244 }
1245
Eric Laurent493404d2015-04-21 15:07:36 -07001246 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001247 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001248 if (device == AUDIO_DEVICE_NONE) {
1249 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001250 }
Eric Laurent36829f92017-04-07 19:04:42 -07001251
Eric Laurente552edb2014-03-10 17:42:56 -07001252 routing_strategy strategy = getStrategy(stream);
1253 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001254 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1255 (beaconMuteLatency > 0);
1256 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001257 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001258 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001259 if (desc != outputDesc) {
Andy Hung7c7124e2017-10-20 12:03:34 -07001260 // An output has a shared device if
1261 // - managed by the same hw module
1262 // - supports the currently selected device
1263 const bool sharedDevice = outputDesc->sharesHwModuleWith(desc)
1264 && (desc->supportedDevices() & device) != AUDIO_DEVICE_NONE;
1265
Eric Laurent77305a62016-07-25 16:39:22 -07001266 // force a device change if any other output is:
1267 // - managed by the same hw module
Eric Laurent77305a62016-07-25 16:39:22 -07001268 // - supports currently selected device
Andy Hung7c7124e2017-10-20 12:03:34 -07001269 // - has a current device selection that differs from selected device.
Eric Laurent77305a62016-07-25 16:39:22 -07001270 // - has an active audio patch
Eric Laurente552edb2014-03-10 17:42:56 -07001271 // In this case, the audio HAL must receive the new device selection so that it can
Andy Hung7c7124e2017-10-20 12:03:34 -07001272 // change the device currently selected by the other output.
1273 if (sharedDevice &&
Eric Laurent77305a62016-07-25 16:39:22 -07001274 desc->device() != device &&
Eric Laurent77305a62016-07-25 16:39:22 -07001275 desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07001276 force = true;
1277 }
1278 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001279 // a notification so that audio focus effect can propagate, or that a mute/unmute
1280 // event occurred for beacon
Andy Hung7c7124e2017-10-20 12:03:34 -07001281 const uint32_t latencyMs = desc->latency();
1282 const bool isActive = desc->isActive(latencyMs * 2); // account for drain
1283
1284 if (shouldWait && isActive && (waitMs < latencyMs)) {
1285 waitMs = latencyMs;
Eric Laurente552edb2014-03-10 17:42:56 -07001286 }
Andy Hung7c7124e2017-10-20 12:03:34 -07001287
1288 // Require mute check if another output is on a shared device
1289 // and currently active to have proper drain and avoid pops.
1290 // Note restoring AudioTracks onto this output needs to invoke
1291 // a volume ramp if there is no mute.
1292 requiresMuteCheck |= sharedDevice && isActive;
Eric Laurente552edb2014-03-10 17:42:56 -07001293 }
1294 }
Andy Hung7c7124e2017-10-20 12:03:34 -07001295
1296 const uint32_t muteWaitMs =
1297 setOutputDevice(outputDesc, device, force, 0, NULL, address, requiresMuteCheck);
Eric Laurente552edb2014-03-10 17:42:56 -07001298
1299 // handle special case for sonification while in call
1300 if (isInCall()) {
1301 handleIncallSonification(stream, true, false);
1302 }
1303
1304 // apply volume rules for current stream and device if necessary
1305 checkAndSetVolume(stream,
Shuhei Miyazaki6fe70332017-07-31 15:21:28 +09001306 mVolumeCurves->getVolumeIndex(stream, outputDesc->device()),
Eric Laurentc75307b2015-03-17 15:29:32 -07001307 outputDesc,
Shuhei Miyazaki6fe70332017-07-31 15:21:28 +09001308 outputDesc->device());
Eric Laurente552edb2014-03-10 17:42:56 -07001309
1310 // update the outputs if starting an output with a stream that can affect notification
1311 // routing
1312 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001313
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001314 // force reevaluating accessibility routing when ringtone or alarm starts
1315 if (strategy == STRATEGY_SONIFICATION) {
1316 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1317 }
Eric Laurentdc462862016-07-19 12:29:53 -07001318
1319 if (waitMs > muteWaitMs) {
1320 *delayMs = waitMs - muteWaitMs;
1321 }
Andy Hung7c7124e2017-10-20 12:03:34 -07001322
1323 // FIXME: A device change (muteWaitMs > 0) likely introduces a volume change.
1324 // A volume change enacted by APM with 0 delay is not synchronous, as it goes
1325 // via AudioCommandThread to AudioFlinger. Hence it is possible that the volume
1326 // change occurs after the MixerThread starts and causes a stream volume
1327 // glitch.
1328 //
1329 // We do not introduce additional delay here.
Eric Laurente552edb2014-03-10 17:42:56 -07001330 }
Eric Laurentdc462862016-07-19 12:29:53 -07001331
Eric Laurente552edb2014-03-10 17:42:56 -07001332 return NO_ERROR;
1333}
1334
1335
Eric Laurente0720872014-03-11 09:30:41 -07001336status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001337 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001338 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001339{
1340 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1341 ssize_t index = mOutputs.indexOfKey(output);
1342 if (index < 0) {
1343 ALOGW("stopOutput() unknown output %d", output);
1344 return BAD_VALUE;
1345 }
1346
Eric Laurentc75307b2015-03-17 15:29:32 -07001347 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001348
Eric Laurentc75307b2015-03-17 15:29:32 -07001349 if (outputDesc->mRefCount[stream] == 1) {
1350 // Automatically disable the remote submix input when output is stopped on a
1351 // re routing mix of type MIX_TYPE_RECORDERS
1352 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1353 outputDesc->mPolicyMix != NULL &&
1354 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1355 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1356 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001357 outputDesc->mPolicyMix->mDeviceAddress,
Eric Laurentc75307b2015-03-17 15:29:32 -07001358 "remote-submix");
1359 }
1360 }
1361
1362 // Routing?
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001363 bool forceDeviceUpdate = false;
Eric Laurentc75307b2015-03-17 15:29:32 -07001364 if (outputDesc->mRefCount[stream] > 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001365 int activityCount = mOutputRoutes.decRouteActivity(session);
1366 forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0));
1367
1368 if (forceDeviceUpdate) {
1369 checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE);
1370 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001371 }
1372
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001373 return stopSource(outputDesc, stream, forceDeviceUpdate);
Eric Laurentc75307b2015-03-17 15:29:32 -07001374}
1375
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001376status_t AudioPolicyManager::stopSource(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001377 audio_stream_type_t stream,
1378 bool forceDeviceUpdate)
Eric Laurentc75307b2015-03-17 15:29:32 -07001379{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001380 // always handle stream stop, check which stream type is stopping
1381 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1382
Eric Laurente552edb2014-03-10 17:42:56 -07001383 // handle special case for sonification while in call
1384 if (isInCall()) {
1385 handleIncallSonification(stream, false, false);
1386 }
1387
1388 if (outputDesc->mRefCount[stream] > 0) {
1389 // decrement usage count of this stream on the output
1390 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001391
Eric Laurente552edb2014-03-10 17:42:56 -07001392 // store time at which the stream was stopped - see isStreamActive()
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001393 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
Eric Laurente552edb2014-03-10 17:42:56 -07001394 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001395 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001396 // delay the device switch by twice the latency because stopOutput() is executed when
1397 // the track stop() command is received and at that time the audio track buffer can
1398 // still contain data that needs to be drained. The latency only covers the audio HAL
1399 // and kernel buffers. Also the latency does not always include additional delay in the
1400 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001401 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001402
1403 // force restoring the device selection on other active outputs if it differs from the
1404 // one being selected for this output
Eric Laurent57de36c2016-09-28 16:59:11 -07001405 uint32_t delayMs = outputDesc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -07001406 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001407 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001408 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001409 desc->isActive() &&
1410 outputDesc->sharesHwModuleWith(desc) &&
1411 (newDevice != desc->device())) {
Haynes Mathew George11c499a2016-08-26 12:08:25 -07001412 audio_devices_t newDevice2 = getNewOutputDevice(desc, false /*fromCache*/);
1413 bool force = desc->device() != newDevice2;
Eric Laurentc75307b2015-03-17 15:29:32 -07001414 setOutputDevice(desc,
Haynes Mathew George11c499a2016-08-26 12:08:25 -07001415 newDevice2,
1416 force,
Eric Laurent57de36c2016-09-28 16:59:11 -07001417 delayMs);
1418 // re-apply device specific volume if not done by setOutputDevice()
1419 if (!force) {
1420 applyStreamVolumes(desc, newDevice2, delayMs);
1421 }
Eric Laurente552edb2014-03-10 17:42:56 -07001422 }
1423 }
1424 // update the outputs if stopping one with a stream that can affect notification routing
1425 handleNotificationRoutingForStream(stream);
1426 }
Eric Laurent36829f92017-04-07 19:04:42 -07001427 if (stream == AUDIO_STREAM_MUSIC) {
1428 selectOutputForMusicEffects();
1429 }
Eric Laurente552edb2014-03-10 17:42:56 -07001430 return NO_ERROR;
1431 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001432 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001433 return INVALID_OPERATION;
1434 }
1435}
1436
Eric Laurente83b55d2014-11-14 10:06:21 -08001437void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001438 audio_stream_type_t stream __unused,
1439 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001440{
1441 ALOGV("releaseOutput() %d", output);
1442 ssize_t index = mOutputs.indexOfKey(output);
1443 if (index < 0) {
1444 ALOGW("releaseOutput() releasing unknown output %d", output);
1445 return;
1446 }
1447
Paul McLeanaa981192015-03-21 09:55:15 -07001448 // Routing
1449 mOutputRoutes.removeRoute(session);
1450
Eric Laurentc75307b2015-03-17 15:29:32 -07001451 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001452 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001453 if (desc->mDirectOpenCount <= 0) {
1454 ALOGW("releaseOutput() invalid open count %d for output %d",
1455 desc->mDirectOpenCount, output);
1456 return;
1457 }
1458 if (--desc->mDirectOpenCount == 0) {
1459 closeOutput(output);
Eric Laurentb52c1522014-05-20 11:27:36 -07001460 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001461 }
1462 }
1463}
1464
1465
Eric Laurentcaf7f482014-11-25 17:50:47 -08001466status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1467 audio_io_handle_t *input,
1468 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001469 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001470 const audio_config_base_t *config,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001471 audio_input_flags_t flags,
Eric Laurent2ac76942017-06-22 17:17:09 -07001472 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001473 input_type_t *inputType,
1474 audio_port_handle_t *portId)
Eric Laurente552edb2014-03-10 17:42:56 -07001475{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001476 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1477 "session %d, flags %#x",
Eric Laurent20b9ef02016-12-05 11:03:16 -08001478 attr->source, config->sample_rate, config->format, config->channel_mask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001479
Eric Laurentad2e7b92017-09-14 20:06:42 -07001480 status_t status = NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -08001481 // handle legacy remote submix case where the address was not always specified
1482 String8 address = String8("");
Eric Laurentc447ded2015-01-06 08:47:05 -08001483 audio_source_t halInputSource;
Eric Laurentad2e7b92017-09-14 20:06:42 -07001484 audio_source_t inputSource = attr->source;
Eric Laurentc722f302014-12-10 11:21:49 -08001485 AudioMix *policyMix = NULL;
Eric Laurentad2e7b92017-09-14 20:06:42 -07001486 DeviceVector inputDevices;
Eric Laurent20b9ef02016-12-05 11:03:16 -08001487
Paul McLean466dc8e2015-04-17 13:15:36 -06001488 // Explicit routing?
1489 sp<DeviceDescriptor> deviceDesc;
Eric Laurent2ac76942017-06-22 17:17:09 -07001490 if (*selectedDeviceId != AUDIO_PORT_HANDLE_NONE) {
1491 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1492 if (mAvailableInputDevices[i]->getId() == *selectedDeviceId) {
1493 deviceDesc = mAvailableInputDevices[i];
1494 break;
1495 }
Paul McLean466dc8e2015-04-17 13:15:36 -06001496 }
1497 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001498 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001499
Eric Laurentad2e7b92017-09-14 20:06:42 -07001500 // special case for mmap capture: if an input IO handle is specified, we reuse this input if
1501 // possible
1502 if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) == AUDIO_INPUT_FLAG_MMAP_NOIRQ &&
1503 *input != AUDIO_IO_HANDLE_NONE) {
1504 ssize_t index = mInputs.indexOfKey(*input);
1505 if (index < 0) {
1506 ALOGW("getInputForAttr() unknown MMAP input %d", *input);
1507 status = BAD_VALUE;
1508 goto error;
1509 }
1510 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1511 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1512 if (audioSession == 0) {
1513 ALOGW("getInputForAttr() unknown session %d on input %d", session, *input);
1514 status = BAD_VALUE;
1515 goto error;
1516 }
1517 // For MMAP mode, the first call to getInputForAttr() is made on behalf of audioflinger.
1518 // The second call is for the first active client and sets the UID. Any further call
1519 // corresponds to a new client and is only permitted from the same UId.
1520 if (audioSession->openCount() == 1) {
1521 audioSession->setUid(uid);
1522 } else if (audioSession->uid() != uid) {
1523 ALOGW("getInputForAttr() bad uid %d for session %d uid %d",
1524 uid, session, audioSession->uid());
1525 status = INVALID_OPERATION;
1526 goto error;
1527 }
1528 audioSession->changeOpenCount(1);
1529 *inputType = API_INPUT_LEGACY;
1530 if (*portId == AUDIO_PORT_HANDLE_NONE) {
1531 *portId = AudioPort::getNextUniqueId();
1532 }
1533 inputDevices = mAvailableInputDevices.getDevicesFromType(inputDesc->mDevice);
1534 *selectedDeviceId = inputDevices.size() > 0 ? inputDevices.itemAt(0)->getId()
1535 : AUDIO_PORT_HANDLE_NONE;
1536 ALOGI("%s reusing MMAP input %d for session %d", __FUNCTION__, *input, session);
1537
1538 return NO_ERROR;
1539 }
1540
1541 *input = AUDIO_IO_HANDLE_NONE;
1542 *inputType = API_INPUT_INVALID;
1543
1544 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1545 inputSource = AUDIO_SOURCE_MIC;
1546 }
1547 halInputSource = inputSource;
1548
1549 // TODO: check for existing client for this port ID
1550 if (*portId == AUDIO_PORT_HANDLE_NONE) {
1551 *portId = AudioPort::getNextUniqueId();
1552 }
1553
1554 audio_devices_t device;
1555
Eric Laurentc447ded2015-01-06 08:47:05 -08001556 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001557 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Eric Laurentad2e7b92017-09-14 20:06:42 -07001558 status = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
1559 if (status != NO_ERROR) {
1560 goto error;
François Gaffie036e1e92015-03-19 10:16:24 +01001561 }
1562 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001563 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1564 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001565 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001566 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001567 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001568 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurentad2e7b92017-09-14 20:06:42 -07001569 status = BAD_VALUE;
1570 goto error;
Eric Laurent275e8e92014-11-30 15:14:47 -08001571 }
Eric Laurentc722f302014-12-10 11:21:49 -08001572 if (policyMix != NULL) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001573 address = policyMix->mDeviceAddress;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001574 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1575 // there is an external policy, but this input is attached to a mix of recorders,
1576 // meaning it receives audio injected into the framework, so the recorder doesn't
1577 // know about it and is therefore considered "legacy"
1578 *inputType = API_INPUT_LEGACY;
1579 } else {
1580 // recording a mix of players defined by an external policy, we're rerouting for
1581 // an external policy
1582 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1583 }
Eric Laurentc722f302014-12-10 11:21:49 -08001584 } else if (audio_is_remote_submix_device(device)) {
1585 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001586 *inputType = API_INPUT_MIX_CAPTURE;
Eric Laurent82db2692015-08-07 13:59:42 -07001587 } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
1588 *inputType = API_INPUT_TELEPHONY_RX;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001589 } else {
1590 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001591 }
Ravi Kumar Alamandab367f5b2015-08-25 08:21:37 -07001592
Eric Laurent599c7582015-12-07 18:05:55 -08001593 }
1594
1595 *input = getInputForDevice(device, address, session, uid, inputSource,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001596 config->sample_rate, config->format, config->channel_mask, flags,
Eric Laurent599c7582015-12-07 18:05:55 -08001597 policyMix);
1598 if (*input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentad2e7b92017-09-14 20:06:42 -07001599 status = INVALID_OPERATION;
1600 goto error;
Eric Laurent599c7582015-12-07 18:05:55 -08001601 }
Eric Laurent20b9ef02016-12-05 11:03:16 -08001602
Eric Laurentad2e7b92017-09-14 20:06:42 -07001603 inputDevices = mAvailableInputDevices.getDevicesFromType(device);
Eric Laurent2ac76942017-06-22 17:17:09 -07001604 *selectedDeviceId = inputDevices.size() > 0 ? inputDevices.itemAt(0)->getId()
1605 : AUDIO_PORT_HANDLE_NONE;
1606
1607 ALOGV("getInputForAttr() returns input %d type %d selectedDeviceId %d",
1608 *input, *inputType, *selectedDeviceId);
1609
Eric Laurent599c7582015-12-07 18:05:55 -08001610 return NO_ERROR;
Eric Laurentad2e7b92017-09-14 20:06:42 -07001611
1612error:
1613 mInputRoutes.removeRoute(session);
1614 return status;
Eric Laurent599c7582015-12-07 18:05:55 -08001615}
1616
1617
1618audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device,
1619 String8 address,
1620 audio_session_t session,
1621 uid_t uid,
1622 audio_source_t inputSource,
1623 uint32_t samplingRate,
1624 audio_format_t format,
1625 audio_channel_mask_t channelMask,
1626 audio_input_flags_t flags,
1627 AudioMix *policyMix)
1628{
1629 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
1630 audio_source_t halInputSource = inputSource;
1631 bool isSoundTrigger = false;
1632
1633 if (inputSource == AUDIO_SOURCE_HOTWORD) {
1634 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1635 if (index >= 0) {
1636 input = mSoundTriggerSessions.valueFor(session);
1637 isSoundTrigger = true;
1638 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1639 ALOGV("SoundTrigger capture on session %d input %d", session, input);
1640 } else {
1641 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001642 }
Haynes Mathew George851d3ff2017-06-19 20:01:57 -07001643 } else if (inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION &&
Haynes Mathew George851d3ff2017-06-19 20:01:57 -07001644 audio_is_linear_pcm(format)) {
1645 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_VOIP_TX);
Eric Laurent5dbe4712014-09-19 19:04:57 -07001646 }
1647
Andy Hungf129b032015-04-07 13:45:50 -07001648 // find a compatible input profile (not necessarily identical in parameters)
1649 sp<IOProfile> profile;
1650 // samplingRate and flags may be updated by getInputProfile
Glenn Kasten05ddca52016-02-11 08:17:12 -08001651 uint32_t profileSamplingRate = (samplingRate == 0) ? SAMPLE_RATE_HZ_DEFAULT : samplingRate;
Andy Hungf129b032015-04-07 13:45:50 -07001652 audio_format_t profileFormat = format;
1653 audio_channel_mask_t profileChannelMask = channelMask;
1654 audio_input_flags_t profileFlags = flags;
1655 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001656 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001657 profileSamplingRate, profileFormat, profileChannelMask,
1658 profileFlags);
1659 if (profile != 0) {
1660 break; // success
Eric Laurent05067782016-06-01 18:27:28 -07001661 } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) {
1662 profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry
Andy Hungf129b032015-04-07 13:45:50 -07001663 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1664 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1665 } else { // fail
Glenn Kastenfaa10a62017-05-25 15:52:05 -07001666 ALOGW("getInputForDevice() could not find profile for device 0x%X, "
Eric Laurent599c7582015-12-07 18:05:55 -08001667 "samplingRate %u, format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001668 device, samplingRate, format, channelMask, flags);
Eric Laurent599c7582015-12-07 18:05:55 -08001669 return input;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001670 }
Eric Laurente552edb2014-03-10 17:42:56 -07001671 }
Glenn Kasten05ddca52016-02-11 08:17:12 -08001672 // Pick input sampling rate if not specified by client
1673 if (samplingRate == 0) {
1674 samplingRate = profileSamplingRate;
1675 }
Eric Laurente552edb2014-03-10 17:42:56 -07001676
Eric Laurent322b4d22015-04-03 15:57:54 -07001677 if (profile->getModuleHandle() == 0) {
1678 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurent599c7582015-12-07 18:05:55 -08001679 return input;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001680 }
1681
Eric Laurent599c7582015-12-07 18:05:55 -08001682 sp<AudioSession> audioSession = new AudioSession(session,
1683 inputSource,
1684 format,
1685 samplingRate,
1686 channelMask,
1687 flags,
1688 uid,
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001689 isSoundTrigger,
1690 policyMix, mpClientInterface);
Eric Laurent599c7582015-12-07 18:05:55 -08001691
Eric Laurent74708e72017-04-07 17:13:42 -07001692// FIXME: disable concurrent capture until UI is ready
1693#if 0
Eric Laurent599c7582015-12-07 18:05:55 -08001694 // reuse an open input if possible
Eric Laurent555530a2017-02-07 18:17:24 -08001695 sp<AudioInputDescriptor> reusedInputDesc;
Eric Laurent599c7582015-12-07 18:05:55 -08001696 for (size_t i = 0; i < mInputs.size(); i++) {
1697 sp<AudioInputDescriptor> desc = mInputs.valueAt(i);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001698 // reuse input if:
1699 // - it shares the same profile
1700 // AND
1701 // - it is not a reroute submix input
1702 // AND
1703 // - it is: not used for sound trigger
1704 // OR
1705 // used for sound trigger and all clients use the same session ID
1706 //
1707 if ((profile == desc->mProfile) &&
1708 (isSoundTrigger == desc->isSoundTrigger()) &&
1709 !is_virtual_input_device(device)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001710
1711 sp<AudioSession> as = desc->getAudioSession(session);
1712 if (as != 0) {
1713 // do not allow unmatching properties on same session
1714 if (as->matches(audioSession)) {
1715 as->changeOpenCount(1);
1716 } else {
1717 ALOGW("getInputForDevice() record with different attributes"
1718 " exists for session %d", session);
Eric Laurent555530a2017-02-07 18:17:24 -08001719 continue;
Eric Laurent599c7582015-12-07 18:05:55 -08001720 }
Eric Laurentfb66dd92016-01-28 18:32:03 -08001721 } else if (isSoundTrigger) {
Eric Laurent555530a2017-02-07 18:17:24 -08001722 continue;
Eric Laurentfb66dd92016-01-28 18:32:03 -08001723 }
Eric Laurentbb948092017-01-23 18:33:30 -08001724
Eric Laurent555530a2017-02-07 18:17:24 -08001725 // Reuse the already opened input stream on this profile if:
1726 // - the new capture source is background OR
1727 // - the path requested configurations match OR
1728 // - the new source priority is less than the highest source priority on this input
1729 // If the input stream cannot be reused, close it before opening a new stream
1730 // on the same profile for the new client so that the requested path configuration
1731 // can be selected.
1732 if (!isConcurrentSource(inputSource) &&
Eric Laurentbb948092017-01-23 18:33:30 -08001733 ((desc->mSamplingRate != samplingRate ||
Eric Laurentfb66dd92016-01-28 18:32:03 -08001734 desc->mChannelMask != channelMask ||
Eric Laurente6930022016-02-11 10:20:40 -08001735 !audio_formats_match(desc->mFormat, format)) &&
Eric Laurentfb66dd92016-01-28 18:32:03 -08001736 (source_priority(desc->getHighestPrioritySource(false /*activeOnly*/)) <
Eric Laurentbb948092017-01-23 18:33:30 -08001737 source_priority(inputSource)))) {
Eric Laurent555530a2017-02-07 18:17:24 -08001738 reusedInputDesc = desc;
1739 continue;
Eric Laurent599c7582015-12-07 18:05:55 -08001740 } else {
1741 desc->addAudioSession(session, audioSession);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001742 ALOGV("%s: reusing input %d", __FUNCTION__, mInputs.keyAt(i));
1743 return mInputs.keyAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08001744 }
Eric Laurent599c7582015-12-07 18:05:55 -08001745 }
1746 }
Eric Laurent599c7582015-12-07 18:05:55 -08001747
Eric Laurent555530a2017-02-07 18:17:24 -08001748 if (reusedInputDesc != 0) {
1749 AudioSessionCollection sessions = reusedInputDesc->getAudioSessions(false /*activeOnly*/);
1750 for (size_t j = 0; j < sessions.size(); j++) {
1751 audio_session_t currentSession = sessions.keyAt(j);
1752 stopInput(reusedInputDesc->mIoHandle, currentSession);
1753 releaseInput(reusedInputDesc->mIoHandle, currentSession);
1754 }
1755 }
Eric Laurent74708e72017-04-07 17:13:42 -07001756#endif
Eric Laurent555530a2017-02-07 18:17:24 -08001757
Eric Laurentcf2c0212014-07-25 16:20:43 -07001758 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001759 config.sample_rate = profileSamplingRate;
1760 config.channel_mask = profileChannelMask;
1761 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001762
Eric Laurente3014102017-05-03 11:15:43 -07001763 if (address == "") {
1764 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(device);
1765 // the inputs vector must be of size 1, but we don't want to crash here
1766 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress : String8("");
1767 }
1768
Eric Laurent322b4d22015-04-03 15:57:54 -07001769 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurent599c7582015-12-07 18:05:55 -08001770 &input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001771 &config,
1772 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001773 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001774 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001775 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001776
1777 // only accept input with the exact requested set of parameters
Eric Laurent599c7582015-12-07 18:05:55 -08001778 if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001779 (profileSamplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -08001780 !audio_formats_match(profileFormat, config.format) ||
Andy Hungf129b032015-04-07 13:45:50 -07001781 (profileChannelMask != config.channel_mask)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001782 ALOGW("getInputForAttr() failed opening input: samplingRate %d"
1783 ", format %d, channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001784 samplingRate, format, channelMask);
Eric Laurent599c7582015-12-07 18:05:55 -08001785 if (input != AUDIO_IO_HANDLE_NONE) {
1786 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001787 }
Eric Laurent599c7582015-12-07 18:05:55 -08001788 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001789 }
1790
Eric Laurent1f2f2232014-06-02 12:01:23 -07001791 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Andy Hungf129b032015-04-07 13:45:50 -07001792 inputDesc->mSamplingRate = profileSamplingRate;
1793 inputDesc->mFormat = profileFormat;
1794 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001795 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001796 inputDesc->mPolicyMix = policyMix;
Eric Laurent599c7582015-12-07 18:05:55 -08001797 inputDesc->addAudioSession(session, audioSession);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001798
Eric Laurent599c7582015-12-07 18:05:55 -08001799 addInput(input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001800 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001801
Eric Laurent599c7582015-12-07 18:05:55 -08001802 return input;
Eric Laurente552edb2014-03-10 17:42:56 -07001803}
1804
Eric Laurentbb948092017-01-23 18:33:30 -08001805//static
1806bool AudioPolicyManager::isConcurrentSource(audio_source_t source)
1807{
1808 return (source == AUDIO_SOURCE_HOTWORD) ||
1809 (source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
1810 (source == AUDIO_SOURCE_FM_TUNER);
1811}
1812
Eric Laurentfb66dd92016-01-28 18:32:03 -08001813bool AudioPolicyManager::isConcurentCaptureAllowed(const sp<AudioInputDescriptor>& inputDesc,
1814 const sp<AudioSession>& audioSession)
1815{
1816 // Do not allow capture if an active voice call is using a software patch and
1817 // the call TX source device is on the same HW module.
1818 // FIXME: would be better to refine to only inputs whose profile connects to the
1819 // call TX device but this information is not in the audio patch
1820 if (mCallTxPatch != 0 &&
1821 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1822 return false;
1823 }
1824
1825 // starting concurrent capture is enabled if:
1826 // 1) capturing for re-routing
1827 // 2) capturing for HOTWORD source
1828 // 3) capturing for FM TUNER source
1829 // 3) All other active captures are either for re-routing or HOTWORD
1830
1831 if (is_virtual_input_device(inputDesc->mDevice) ||
Eric Laurentbb948092017-01-23 18:33:30 -08001832 isConcurrentSource(audioSession->inputSource())) {
Eric Laurentfb66dd92016-01-28 18:32:03 -08001833 return true;
1834 }
1835
1836 Vector< sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
1837 for (size_t i = 0; i < activeInputs.size(); i++) {
1838 sp<AudioInputDescriptor> activeInput = activeInputs[i];
Eric Laurentbb948092017-01-23 18:33:30 -08001839 if (!isConcurrentSource(activeInput->inputSource(true)) &&
Eric Laurentfb66dd92016-01-28 18:32:03 -08001840 !is_virtual_input_device(activeInput->mDevice)) {
1841 return false;
1842 }
1843 }
1844
1845 return true;
1846}
1847
Chris Thornton2b864642017-06-27 21:26:07 -07001848// FIXME: remove when concurrent capture is ready. This is a hack to work around bug b/63083537.
1849bool AudioPolicyManager::soundTriggerSupportsConcurrentCapture() {
1850 if (!mHasComputedSoundTriggerSupportsConcurrentCapture) {
1851 bool soundTriggerSupportsConcurrentCapture = false;
1852 unsigned int numModules = 0;
1853 struct sound_trigger_module_descriptor* nModules = NULL;
1854
1855 status_t status = SoundTrigger::listModules(nModules, &numModules);
1856 if (status == NO_ERROR && numModules != 0) {
1857 nModules = (struct sound_trigger_module_descriptor*) calloc(
1858 numModules, sizeof(struct sound_trigger_module_descriptor));
1859 if (nModules == NULL) {
1860 // We failed to malloc the buffer, so just say no for now, and hope that we have more
1861 // ram the next time this function is called.
1862 ALOGE("Failed to allocate buffer for module descriptors");
1863 return false;
1864 }
1865
1866 status = SoundTrigger::listModules(nModules, &numModules);
1867 if (status == NO_ERROR) {
1868 soundTriggerSupportsConcurrentCapture = true;
1869 for (size_t i = 0; i < numModules; ++i) {
1870 soundTriggerSupportsConcurrentCapture &=
1871 nModules[i].properties.concurrent_capture;
1872 }
1873 }
1874 free(nModules);
1875 }
1876 mSoundTriggerSupportsConcurrentCapture = soundTriggerSupportsConcurrentCapture;
1877 mHasComputedSoundTriggerSupportsConcurrentCapture = true;
1878 }
1879 return mSoundTriggerSupportsConcurrentCapture;
1880}
1881
Eric Laurentfb66dd92016-01-28 18:32:03 -08001882
Eric Laurent4dc68062014-07-28 17:26:49 -07001883status_t AudioPolicyManager::startInput(audio_io_handle_t input,
Eric Laurentfb66dd92016-01-28 18:32:03 -08001884 audio_session_t session,
1885 concurrency_type__mask_t *concurrency)
Eric Laurente552edb2014-03-10 17:42:56 -07001886{
1887 ALOGV("startInput() input %d", input);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001888 *concurrency = API_INPUT_CONCURRENCY_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001889 ssize_t index = mInputs.indexOfKey(input);
1890 if (index < 0) {
1891 ALOGW("startInput() unknown input %d", input);
1892 return BAD_VALUE;
1893 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001894 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001895
Eric Laurent599c7582015-12-07 18:05:55 -08001896 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1897 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07001898 ALOGW("startInput() unknown session %d on input %d", session, input);
1899 return BAD_VALUE;
1900 }
1901
Eric Laurent74708e72017-04-07 17:13:42 -07001902// FIXME: disable concurrent capture until UI is ready
1903#if 0
Eric Laurentfb66dd92016-01-28 18:32:03 -08001904 if (!isConcurentCaptureAllowed(inputDesc, audioSession)) {
1905 ALOGW("startInput(%d) failed: other input already started", input);
1906 return INVALID_OPERATION;
1907 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001908
Eric Laurentfb66dd92016-01-28 18:32:03 -08001909 if (isInCall()) {
1910 *concurrency |= API_INPUT_CONCURRENCY_CALL;
1911 }
Eric Laurentf7c50102016-09-30 15:19:43 -07001912 if (mInputs.activeInputsCountOnDevices() != 0) {
Eric Laurentfb66dd92016-01-28 18:32:03 -08001913 *concurrency |= API_INPUT_CONCURRENCY_CAPTURE;
Eric Laurente552edb2014-03-10 17:42:56 -07001914 }
Eric Laurent74708e72017-04-07 17:13:42 -07001915#else
1916 if (!is_virtual_input_device(inputDesc->mDevice)) {
1917 if (mCallTxPatch != 0 &&
1918 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1919 ALOGW("startInput(%d) failed: call in progress", input);
1920 return INVALID_OPERATION;
1921 }
1922
1923 Vector< sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
1924 for (size_t i = 0; i < activeInputs.size(); i++) {
1925 sp<AudioInputDescriptor> activeDesc = activeInputs[i];
1926
1927 if (is_virtual_input_device(activeDesc->mDevice)) {
1928 continue;
1929 }
1930
Eric Laurentcb4dae22017-07-01 19:39:32 -07001931 if ((audioSession->flags() & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0 &&
1932 activeDesc->getId() == inputDesc->getId()) {
1933 continue;
1934 }
1935
Eric Laurent74708e72017-04-07 17:13:42 -07001936 audio_source_t activeSource = activeDesc->inputSource(true);
1937 if (audioSession->inputSource() == AUDIO_SOURCE_HOTWORD) {
1938 if (activeSource == AUDIO_SOURCE_HOTWORD) {
1939 if (activeDesc->hasPreemptedSession(session)) {
1940 ALOGW("startInput(%d) failed for HOTWORD: "
1941 "other input %d already started for HOTWORD",
1942 input, activeDesc->mIoHandle);
1943 return INVALID_OPERATION;
1944 }
1945 } else {
1946 ALOGV("startInput(%d) failed for HOTWORD: other input %d already started",
1947 input, activeDesc->mIoHandle);
1948 return INVALID_OPERATION;
1949 }
1950 } else {
1951 if (activeSource != AUDIO_SOURCE_HOTWORD) {
1952 ALOGW("startInput(%d) failed: other input %d already started",
1953 input, activeDesc->mIoHandle);
1954 return INVALID_OPERATION;
1955 }
1956 }
1957 }
1958
Chris Thornton2b864642017-06-27 21:26:07 -07001959 // We only need to check if the sound trigger session supports concurrent capture if the
1960 // input is also a sound trigger input. Otherwise, we should preempt any hotword stream
1961 // that's running.
1962 const bool allowConcurrentWithSoundTrigger =
1963 inputDesc->isSoundTrigger() ? soundTriggerSupportsConcurrentCapture() : false;
1964
Eric Laurent74708e72017-04-07 17:13:42 -07001965 // if capture is allowed, preempt currently active HOTWORD captures
1966 for (size_t i = 0; i < activeInputs.size(); i++) {
1967 sp<AudioInputDescriptor> activeDesc = activeInputs[i];
1968
1969 if (is_virtual_input_device(activeDesc->mDevice)) {
1970 continue;
1971 }
1972
Chris Thornton2b864642017-06-27 21:26:07 -07001973 if (allowConcurrentWithSoundTrigger && activeDesc->isSoundTrigger()) {
1974 continue;
1975 }
1976
Eric Laurent74708e72017-04-07 17:13:42 -07001977 audio_source_t activeSource = activeDesc->inputSource(true);
1978 if (activeSource == AUDIO_SOURCE_HOTWORD) {
1979 AudioSessionCollection activeSessions =
1980 activeDesc->getAudioSessions(true /*activeOnly*/);
1981 audio_session_t activeSession = activeSessions.keyAt(0);
1982 audio_io_handle_t activeHandle = activeDesc->mIoHandle;
1983 SortedVector<audio_session_t> sessions = activeDesc->getPreemptedSessions();
1984 sessions.add(activeSession);
1985 inputDesc->setPreemptedSessions(sessions);
1986 stopInput(activeHandle, activeSession);
1987 releaseInput(activeHandle, activeSession);
1988 ALOGV("startInput(%d) for HOTWORD preempting HOTWORD input %d",
1989 input, activeDesc->mIoHandle);
1990 }
1991 }
1992 }
1993#endif
Eric Laurente552edb2014-03-10 17:42:56 -07001994
Eric Laurent313d1e72016-01-29 09:56:57 -08001995 // increment activity count before calling getNewInputDevice() below as only active sessions
1996 // are considered for device selection
1997 audioSession->changeActiveCount(1);
1998
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001999 // Routing?
2000 mInputRoutes.incRouteActivity(session);
2001
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07002002 if (audioSession->activeCount() == 1 || mInputRoutes.hasRouteChanged(session)) {
Eric Laurent271a93e2016-09-29 14:47:06 -07002003 // indicate active capture to sound trigger service if starting capture from a mic on
2004 // primary HW module
Eric Laurentf7c50102016-09-30 15:19:43 -07002005 audio_devices_t device = getNewInputDevice(inputDesc);
Eric Laurent271a93e2016-09-29 14:47:06 -07002006 setInputDevice(input, device, true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07002007
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07002008 if (inputDesc->getAudioSessionCount(true/*activeOnly*/) == 1) {
2009 // if input maps to a dynamic policy with an activity listener, notify of state change
2010 if ((inputDesc->mPolicyMix != NULL)
2011 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
2012 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
2013 MIX_STATE_MIXING);
Eric Laurentc722f302014-12-10 11:21:49 -08002014 }
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07002015
Eric Laurentf7c50102016-09-30 15:19:43 -07002016 audio_devices_t primaryInputDevices = availablePrimaryInputDevices();
2017 if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) &&
Eric Laurent05b345e2016-11-18 12:36:27 -08002018 mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) {
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07002019 SoundTrigger::setCaptureState(true);
2020 }
2021
2022 // automatically enable the remote submix output when input is started if not
2023 // used by a policy mix of type MIX_TYPE_RECORDERS
2024 // For remote submix (a virtual device), we open only one input per capture request.
2025 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
2026 String8 address = String8("");
2027 if (inputDesc->mPolicyMix == NULL) {
2028 address = String8("0");
2029 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
2030 address = inputDesc->mPolicyMix->mDeviceAddress;
2031 }
2032 if (address != "") {
2033 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2034 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2035 address, "remote-submix");
2036 }
Eric Laurentc722f302014-12-10 11:21:49 -08002037 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07002038 }
Eric Laurente552edb2014-03-10 17:42:56 -07002039 }
2040
Eric Laurent599c7582015-12-07 18:05:55 -08002041 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Eric Laurente552edb2014-03-10 17:42:56 -07002042
Eric Laurente552edb2014-03-10 17:42:56 -07002043 return NO_ERROR;
2044}
2045
Eric Laurent4dc68062014-07-28 17:26:49 -07002046status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
2047 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07002048{
2049 ALOGV("stopInput() input %d", input);
2050 ssize_t index = mInputs.indexOfKey(input);
2051 if (index < 0) {
2052 ALOGW("stopInput() unknown input %d", input);
2053 return BAD_VALUE;
2054 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07002055 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07002056
Eric Laurent599c7582015-12-07 18:05:55 -08002057 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07002058 if (index < 0) {
2059 ALOGW("stopInput() unknown session %d on input %d", session, input);
2060 return BAD_VALUE;
2061 }
2062
Eric Laurent599c7582015-12-07 18:05:55 -08002063 if (audioSession->activeCount() == 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07002064 ALOGW("stopInput() input %d already stopped", input);
2065 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002066 }
2067
Eric Laurent599c7582015-12-07 18:05:55 -08002068 audioSession->changeActiveCount(-1);
Paul McLean466dc8e2015-04-17 13:15:36 -06002069
2070 // Routing?
2071 mInputRoutes.decRouteActivity(session);
2072
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07002073 if (audioSession->activeCount() == 0) {
Eric Laurent84332aa2016-01-28 22:19:18 +00002074
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07002075 if (inputDesc->isActive()) {
2076 setInputDevice(input, getNewInputDevice(inputDesc), false /* force */);
2077 } else {
2078 // if input maps to a dynamic policy with an activity listener, notify of state change
2079 if ((inputDesc->mPolicyMix != NULL)
2080 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
2081 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
2082 MIX_STATE_IDLE);
Eric Laurent84332aa2016-01-28 22:19:18 +00002083 }
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07002084
2085 // automatically disable the remote submix output when input is stopped if not
2086 // used by a policy mix of type MIX_TYPE_RECORDERS
2087 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
2088 String8 address = String8("");
2089 if (inputDesc->mPolicyMix == NULL) {
2090 address = String8("0");
2091 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
2092 address = inputDesc->mPolicyMix->mDeviceAddress;
2093 }
2094 if (address != "") {
2095 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2096 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2097 address, "remote-submix");
2098 }
Eric Laurent84332aa2016-01-28 22:19:18 +00002099 }
Eric Laurent84332aa2016-01-28 22:19:18 +00002100
Eric Laurentf7c50102016-09-30 15:19:43 -07002101 audio_devices_t device = inputDesc->mDevice;
Eric Laurentfb66dd92016-01-28 18:32:03 -08002102 resetInputDevice(input);
Eric Laurent84332aa2016-01-28 22:19:18 +00002103
Eric Laurentf7c50102016-09-30 15:19:43 -07002104 // indicate inactive capture to sound trigger service if stopping capture from a mic on
2105 // primary HW module
2106 audio_devices_t primaryInputDevices = availablePrimaryInputDevices();
2107 if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) &&
2108 mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) {
Eric Laurentfb66dd92016-01-28 18:32:03 -08002109 SoundTrigger::setCaptureState(false);
2110 }
2111 inputDesc->clearPreemptedSessions();
Eric Laurent84332aa2016-01-28 22:19:18 +00002112 }
Eric Laurente552edb2014-03-10 17:42:56 -07002113 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002114 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07002115}
2116
Eric Laurent4dc68062014-07-28 17:26:49 -07002117void AudioPolicyManager::releaseInput(audio_io_handle_t input,
2118 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07002119{
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002120
Eric Laurente552edb2014-03-10 17:42:56 -07002121 ALOGV("releaseInput() %d", input);
2122 ssize_t index = mInputs.indexOfKey(input);
2123 if (index < 0) {
2124 ALOGW("releaseInput() releasing unknown input %d", input);
2125 return;
2126 }
Paul McLean466dc8e2015-04-17 13:15:36 -06002127
2128 // Routing
2129 mInputRoutes.removeRoute(session);
2130
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002131 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
2132 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07002133
Eric Laurent599c7582015-12-07 18:05:55 -08002134 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
vivek mehta587b8df2017-01-31 14:58:44 -08002135 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07002136 ALOGW("releaseInput() unknown session %d on input %d", session, input);
2137 return;
2138 }
Eric Laurent599c7582015-12-07 18:05:55 -08002139
2140 if (audioSession->openCount() == 0) {
2141 ALOGW("releaseInput() invalid open count %d on session %d",
2142 audioSession->openCount(), session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002143 return;
2144 }
Eric Laurent599c7582015-12-07 18:05:55 -08002145
2146 if (audioSession->changeOpenCount(-1) == 0) {
2147 inputDesc->removeAudioSession(session);
2148 }
2149
Jean-Michel Trivi65bfe912015-12-04 15:56:47 -08002150 if (inputDesc->getOpenRefCount() > 0) {
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002151 ALOGV("releaseInput() exit > 0");
2152 return;
2153 }
2154
Eric Laurent05b90f82014-08-27 15:32:29 -07002155 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07002156 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07002157 ALOGV("releaseInput() exit");
2158}
2159
Eric Laurentd4692962014-05-05 18:13:44 -07002160void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07002161 bool patchRemoved = false;
2162
Eric Laurentd4692962014-05-05 18:13:44 -07002163 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07002164 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08002165 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07002166 if (patch_index >= 0) {
2167 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07002168 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07002169 mAudioPatches.removeItemsAt(patch_index);
2170 patchRemoved = true;
2171 }
Eric Laurentd4692962014-05-05 18:13:44 -07002172 mpClientInterface->closeInput(mInputs.keyAt(input_index));
2173 }
2174 mInputs.clear();
Chris Thornton52785f32016-02-09 17:28:49 -08002175 SoundTrigger::setCaptureState(false);
Eric Laurent6a94d692014-05-20 11:18:06 -07002176 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07002177
2178 if (patchRemoved) {
2179 mpClientInterface->onAudioPatchListUpdate();
2180 }
Eric Laurentd4692962014-05-05 18:13:44 -07002181}
2182
Eric Laurente0720872014-03-11 09:30:41 -07002183void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07002184 int indexMin,
2185 int indexMax)
2186{
2187 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002188 mVolumeCurves->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent28d09f02016-03-08 10:43:05 -08002189
2190 // initialize other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08002191 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
2192 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002193 continue;
2194 }
2195 mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08002196 }
Eric Laurente552edb2014-03-10 17:42:56 -07002197}
2198
Eric Laurente0720872014-03-11 09:30:41 -07002199status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01002200 int index,
2201 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07002202{
2203
François Gaffied1ab2bd2015-12-02 18:20:06 +01002204 if ((index < mVolumeCurves->getVolumeIndexMin(stream)) ||
2205 (index > mVolumeCurves->getVolumeIndexMax(stream))) {
Eric Laurente552edb2014-03-10 17:42:56 -07002206 return BAD_VALUE;
2207 }
2208 if (!audio_is_output_device(device)) {
2209 return BAD_VALUE;
2210 }
2211
2212 // Force max volume if stream cannot be muted
François Gaffied1ab2bd2015-12-02 18:20:06 +01002213 if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07002214
Eric Laurent1fd372e2016-04-06 14:23:53 -07002215 ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d",
Eric Laurente552edb2014-03-10 17:42:56 -07002216 stream, device, index);
2217
Eric Laurent28d09f02016-03-08 10:43:05 -08002218 // update other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08002219 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
2220 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002221 continue;
2222 }
2223 mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index);
2224 }
Eric Laurente552edb2014-03-10 17:42:56 -07002225
Eric Laurent1fd372e2016-04-06 14:23:53 -07002226 // update volume on all outputs and streams matching the following:
2227 // - The requested stream (or a stream matching for volume control) is active on the output
2228 // - The device (or devices) selected by the strategy corresponding to this stream includes
2229 // the requested device
2230 // - For non default requested device, currently selected device on the output is either the
2231 // requested device or one of the devices selected by the strategy
Eric Laurent5a2b6292016-04-14 18:05:57 -07002232 // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if
2233 // no specific device volume value exists for currently selected device.
Eric Laurente552edb2014-03-10 17:42:56 -07002234 status_t status = NO_ERROR;
2235 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002236 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
2237 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent794fde22016-03-11 09:50:45 -08002238 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
2239 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002240 continue;
Eric Laurente552edb2014-03-10 17:42:56 -07002241 }
Eric Laurentd3926fe2016-04-15 18:10:07 -07002242 if (!(desc->isStreamActive((audio_stream_type_t)curStream) ||
2243 (isInCall() && (curStream == AUDIO_STREAM_VOICE_CALL)))) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07002244 continue;
2245 }
Eric Laurent794fde22016-03-11 09:50:45 -08002246 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Jean-Michel Trivib7fdce62017-06-27 19:38:42 -07002247 audio_devices_t curStreamDevice = Volume::getDeviceForVolume(getDeviceForStrategy(
2248 curStrategy, false /*fromCache*/));
Eric Laurente76f29c2016-09-14 17:17:53 -07002249 if ((device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) &&
2250 ((curStreamDevice & device) == 0)) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07002251 continue;
2252 }
Eric Laurente76f29c2016-09-14 17:17:53 -07002253 bool applyVolume;
Eric Laurent5a2b6292016-04-14 18:05:57 -07002254 if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07002255 curStreamDevice |= device;
Eric Laurente76f29c2016-09-14 17:17:53 -07002256 applyVolume = (curDevice & curStreamDevice) != 0;
2257 } else {
2258 applyVolume = !mVolumeCurves->hasVolumeIndexForDevice(
Jean-Michel Trivib7fdce62017-06-27 19:38:42 -07002259 stream, curStreamDevice);
Eric Laurent1fd372e2016-04-06 14:23:53 -07002260 }
Eric Laurent28d09f02016-03-08 10:43:05 -08002261
Eric Laurente76f29c2016-09-14 17:17:53 -07002262 if (applyVolume) {
Eric Laurentdc462862016-07-19 12:29:53 -07002263 //FIXME: workaround for truncated touch sounds
2264 // delayed volume change for system stream to be removed when the problem is
2265 // handled by system UI
Eric Laurent28d09f02016-03-08 10:43:05 -08002266 status_t volStatus =
Eric Laurentdc462862016-07-19 12:29:53 -07002267 checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice,
2268 (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0);
Eric Laurent28d09f02016-03-08 10:43:05 -08002269 if (volStatus != NO_ERROR) {
2270 status = volStatus;
2271 }
2272 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08002273 }
Eric Laurente552edb2014-03-10 17:42:56 -07002274 }
2275 return status;
2276}
2277
Eric Laurente0720872014-03-11 09:30:41 -07002278status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07002279 int *index,
2280 audio_devices_t device)
2281{
2282 if (index == NULL) {
2283 return BAD_VALUE;
2284 }
2285 if (!audio_is_output_device(device)) {
2286 return BAD_VALUE;
2287 }
Eric Laurent5a2b6292016-04-14 18:05:57 -07002288 // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device corresponding to
Eric Laurente552edb2014-03-10 17:42:56 -07002289 // the strategy the stream belongs to.
Eric Laurent5a2b6292016-04-14 18:05:57 -07002290 if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurente552edb2014-03-10 17:42:56 -07002291 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
2292 }
François Gaffiedfd74092015-03-19 12:10:59 +01002293 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07002294
François Gaffied1ab2bd2015-12-02 18:20:06 +01002295 *index = mVolumeCurves->getVolumeIndex(stream, device);
Eric Laurente552edb2014-03-10 17:42:56 -07002296 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
2297 return NO_ERROR;
2298}
2299
Eric Laurent36829f92017-04-07 19:04:42 -07002300audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects()
Eric Laurente552edb2014-03-10 17:42:56 -07002301{
2302 // select one output among several suitable for global effects.
2303 // The priority is as follows:
2304 // 1: An offloaded output. If the effect ends up not being offloadable,
2305 // AudioFlinger will invalidate the track and the offloaded output
2306 // will be closed causing the effect to be moved to a PCM output.
2307 // 2: A deep buffer output
Eric Laurent36829f92017-04-07 19:04:42 -07002308 // 3: The primary output
2309 // 4: the first output in the list
Eric Laurente552edb2014-03-10 17:42:56 -07002310
Eric Laurent3b73df72014-03-11 09:06:29 -07002311 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07002312 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent36829f92017-04-07 19:04:42 -07002313 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
Eric Laurente552edb2014-03-10 17:42:56 -07002314
Eric Laurent36829f92017-04-07 19:04:42 -07002315 if (outputs.size() == 0) {
2316 return AUDIO_IO_HANDLE_NONE;
2317 }
Eric Laurente552edb2014-03-10 17:42:56 -07002318
Eric Laurent36829f92017-04-07 19:04:42 -07002319 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
2320 bool activeOnly = true;
2321
2322 while (output == AUDIO_IO_HANDLE_NONE) {
2323 audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE;
2324 audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE;
2325 audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE;
2326
2327 for (size_t i = 0; i < outputs.size(); i++) {
2328 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
2329 if (activeOnly && !desc->isStreamActive(AUDIO_STREAM_MUSIC)) {
2330 continue;
2331 }
2332 ALOGV("selectOutputForMusicEffects activeOnly %d outputs[%zu] flags 0x%08x",
2333 activeOnly, i, desc->mFlags);
2334 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
2335 outputOffloaded = outputs[i];
2336 }
2337 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
2338 outputDeepBuffer = outputs[i];
2339 }
2340 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) {
2341 outputPrimary = outputs[i];
2342 }
2343 }
2344 if (outputOffloaded != AUDIO_IO_HANDLE_NONE) {
2345 output = outputOffloaded;
2346 } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) {
2347 output = outputDeepBuffer;
2348 } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) {
2349 output = outputPrimary;
2350 } else {
2351 output = outputs[0];
2352 }
2353 activeOnly = false;
2354 }
2355
2356 if (output != mMusicEffectOutput) {
2357 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output);
2358 mMusicEffectOutput = output;
2359 }
2360
2361 ALOGV("selectOutputForMusicEffects selected output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -07002362 return output;
2363}
2364
Eric Laurent36829f92017-04-07 19:04:42 -07002365audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused)
2366{
2367 return selectOutputForMusicEffects();
2368}
2369
Eric Laurente0720872014-03-11 09:30:41 -07002370status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07002371 audio_io_handle_t io,
2372 uint32_t strategy,
2373 int session,
2374 int id)
2375{
2376 ssize_t index = mOutputs.indexOfKey(io);
2377 if (index < 0) {
2378 index = mInputs.indexOfKey(io);
2379 if (index < 0) {
2380 ALOGW("registerEffect() unknown io %d", io);
2381 return INVALID_OPERATION;
2382 }
2383 }
François Gaffie45ed3b02015-03-19 10:35:14 +01002384 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07002385}
2386
Eric Laurentc75307b2015-03-17 15:29:32 -07002387bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
2388{
Eric Laurent28d09f02016-03-08 10:43:05 -08002389 bool active = false;
Eric Laurent794fde22016-03-11 09:50:45 -08002390 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) {
2391 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002392 continue;
2393 }
2394 active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs);
2395 }
Eric Laurent28d09f02016-03-08 10:43:05 -08002396 return active;
Eric Laurentc75307b2015-03-17 15:29:32 -07002397}
2398
2399bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
2400{
2401 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
2402}
2403
Eric Laurente0720872014-03-11 09:30:41 -07002404bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07002405{
2406 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002407 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002408 if (inputDescriptor->isSourceActive(source)) {
Eric Laurente552edb2014-03-10 17:42:56 -07002409 return true;
2410 }
2411 }
2412 return false;
2413}
2414
Eric Laurent275e8e92014-11-30 15:14:47 -08002415// Register a list of custom mixes with their attributes and format.
2416// When a mix is registered, corresponding input and output profiles are
2417// added to the remote submix hw module. The profile contains only the
2418// parameters (sampling rate, format...) specified by the mix.
2419// The corresponding input remote submix device is also connected.
2420//
2421// When a remote submix device is connected, the address is checked to select the
2422// appropriate profile and the corresponding input or output stream is opened.
2423//
2424// When capture starts, getInputForAttr() will:
2425// - 1 look for a mix matching the address passed in attribtutes tags if any
2426// - 2 if none found, getDeviceForInputSource() will:
2427// - 2.1 look for a mix matching the attributes source
2428// - 2.2 if none found, default to device selection by policy rules
2429// At this time, the corresponding output remote submix device is also connected
2430// and active playback use cases can be transferred to this mix if needed when reconnecting
2431// after AudioTracks are invalidated
2432//
2433// When playback starts, getOutputForAttr() will:
2434// - 1 look for a mix matching the address passed in attribtutes tags if any
2435// - 2 if none found, look for a mix matching the attributes usage
2436// - 3 if none found, default to device and output selection by policy rules.
2437
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07002438status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes)
Eric Laurent275e8e92014-11-30 15:14:47 -08002439{
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002440 ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size());
2441 status_t res = NO_ERROR;
2442
2443 sp<HwModule> rSubmixModule;
2444 // examine each mix's route type
2445 for (size_t i = 0; i < mixes.size(); i++) {
2446 // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination
2447 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) {
2448 res = INVALID_OPERATION;
Eric Laurent275e8e92014-11-30 15:14:47 -08002449 break;
2450 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002451 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
2452 // Loop back through "remote submix"
2453 if (rSubmixModule == 0) {
2454 for (size_t j = 0; i < mHwModules.size(); j++) {
2455 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2456 && mHwModules[j]->mHandle != 0) {
2457 rSubmixModule = mHwModules[j];
2458 break;
2459 }
2460 }
2461 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002462
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002463 ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size());
Eric Laurent275e8e92014-11-30 15:14:47 -08002464
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002465 if (rSubmixModule == 0) {
2466 ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", i);
2467 res = INVALID_OPERATION;
2468 break;
2469 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002470
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002471 String8 address = mixes[i].mDeviceAddress;
François Gaffie036e1e92015-03-19 10:16:24 +01002472
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002473 if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) {
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002474 ALOGE(" Error registering mix %zu for address %s", i, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002475 res = INVALID_OPERATION;
2476 break;
2477 }
2478 audio_config_t outputConfig = mixes[i].mFormat;
2479 audio_config_t inputConfig = mixes[i].mFormat;
2480 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
2481 // stereo and let audio flinger do the channel conversion if needed.
2482 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2483 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
2484 rSubmixModule->addOutputProfile(address, &outputConfig,
2485 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
2486 rSubmixModule->addInputProfile(address, &inputConfig,
2487 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01002488
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002489 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
2490 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2491 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2492 address.string(), "remote-submix");
2493 } else {
2494 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2495 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2496 address.string(), "remote-submix");
2497 }
2498 } else if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002499 String8 address = mixes[i].mDeviceAddress;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002500 audio_devices_t device = mixes[i].mDeviceType;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002501 ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s",
2502 i, mixes.size(), device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002503
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002504 bool foundOutput = false;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002505 for (size_t j = 0 ; j < mOutputs.size() ; j++) {
2506 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j);
2507 sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle());
2508 if ((patch != 0) && (patch->mPatch.num_sinks != 0)
2509 && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE)
2510 && (patch->mPatch.sinks[0].ext.device.type == device)
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002511 && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(),
2512 AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002513 if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) {
2514 res = INVALID_OPERATION;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002515 } else {
2516 foundOutput = true;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002517 }
2518 break;
2519 }
2520 }
2521
2522 if (res != NO_ERROR) {
2523 ALOGE(" Error registering mix %zu for device 0x%X addr %s",
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002524 i, device, address.string());
2525 res = INVALID_OPERATION;
2526 break;
2527 } else if (!foundOutput) {
2528 ALOGE(" Output not found for mix %zu for device 0x%X addr %s",
2529 i, device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002530 res = INVALID_OPERATION;
2531 break;
2532 }
Eric Laurentc722f302014-12-10 11:21:49 -08002533 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002534 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002535 if (res != NO_ERROR) {
2536 unregisterPolicyMixes(mixes);
2537 }
2538 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002539}
2540
2541status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
2542{
Eric Laurent7b279bb2015-12-14 10:18:23 -08002543 ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002544 status_t res = NO_ERROR;
2545 sp<HwModule> rSubmixModule;
2546 // examine each mix's route type
Eric Laurent275e8e92014-11-30 15:14:47 -08002547 for (size_t i = 0; i < mixes.size(); i++) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002548 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
François Gaffie036e1e92015-03-19 10:16:24 +01002549
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002550 if (rSubmixModule == 0) {
2551 for (size_t j = 0; i < mHwModules.size(); j++) {
2552 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2553 && mHwModules[j]->mHandle != 0) {
2554 rSubmixModule = mHwModules[j];
2555 break;
2556 }
2557 }
2558 }
2559 if (rSubmixModule == 0) {
2560 res = INVALID_OPERATION;
2561 continue;
2562 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002563
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002564 String8 address = mixes[i].mDeviceAddress;
Eric Laurent275e8e92014-11-30 15:14:47 -08002565
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002566 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
2567 res = INVALID_OPERATION;
2568 continue;
2569 }
2570
2571 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
2572 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2573 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2574 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2575 address.string(), "remote-submix");
2576 }
2577 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
2578 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2579 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2580 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2581 address.string(), "remote-submix");
2582 }
2583 rSubmixModule->removeOutputProfile(address);
2584 rSubmixModule->removeInputProfile(address);
2585
2586 } if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
2587 if (mPolicyMixes.unregisterMix(mixes[i].mDeviceAddress) != NO_ERROR) {
2588 res = INVALID_OPERATION;
2589 continue;
2590 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002591 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002592 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002593 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002594}
2595
Eric Laurente552edb2014-03-10 17:42:56 -07002596
Eric Laurente0720872014-03-11 09:30:41 -07002597status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07002598{
2599 const size_t SIZE = 256;
2600 char buffer[SIZE];
2601 String8 result;
2602
2603 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
2604 result.append(buffer);
2605
Eric Laurent87ffa392015-05-22 10:32:38 -07002606 snprintf(buffer, SIZE, " Primary Output: %d\n",
2607 hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE);
Eric Laurente552edb2014-03-10 17:42:56 -07002608 result.append(buffer);
Mikhail Naganov0d6a0332016-04-19 17:12:38 -07002609 std::string stateLiteral;
2610 AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral);
2611 snprintf(buffer, SIZE, " Phone state: %s\n", stateLiteral.c_str());
Eric Laurente552edb2014-03-10 17:42:56 -07002612 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002613 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002614 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
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 media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
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 record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07002619 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002620 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07002621 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002622 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07002623 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002624 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002625 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002626 result.append(buffer);
Phil Burk09bc4612016-02-24 15:58:15 -08002627 snprintf(buffer, SIZE, " Force use for encoded surround output %d\n",
2628 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND));
2629 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002630 snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available");
2631 result.append(buffer);
Andy Hung2ddee192015-12-18 17:34:44 -08002632 snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off");
2633 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002634
François Gaffie2110e042015-03-24 08:41:51 +01002635 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07002636
François Gaffie112b0af2015-11-19 16:13:25 +01002637 mAvailableOutputDevices.dump(fd, String8("Available output"));
2638 mAvailableInputDevices.dump(fd, String8("Available input"));
François Gaffie53615e22015-03-19 09:24:12 +01002639 mHwModules.dump(fd);
2640 mOutputs.dump(fd);
2641 mInputs.dump(fd);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002642 mVolumeCurves->dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01002643 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002644 mAudioPatches.dump(fd);
Mikhail Naganov44344b02016-12-13 11:21:02 -08002645 mPolicyMixes.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07002646
2647 return NO_ERROR;
2648}
2649
2650// This function checks for the parameters which can be offloaded.
2651// This can be enhanced depending on the capability of the DSP and policy
2652// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002653bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002654{
2655 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002656 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002657 offloadInfo.sample_rate, offloadInfo.channel_mask,
2658 offloadInfo.format,
2659 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2660 offloadInfo.has_video);
2661
Andy Hung2ddee192015-12-18 17:34:44 -08002662 if (mMasterMono) {
2663 return false; // no offloading if mono is set.
2664 }
2665
Eric Laurente552edb2014-03-10 17:42:56 -07002666 // Check if offload has been disabled
2667 char propValue[PROPERTY_VALUE_MAX];
2668 if (property_get("audio.offload.disable", propValue, "0")) {
2669 if (atoi(propValue) != 0) {
2670 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2671 return false;
2672 }
2673 }
2674
2675 // Check if stream type is music, then only allow offload as of now.
2676 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2677 {
2678 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2679 return false;
2680 }
2681
2682 //TODO: enable audio offloading with video when ready
Andy Hung08945c42015-05-31 21:36:46 -07002683 const bool allowOffloadWithVideo =
2684 property_get_bool("audio.offload.video", false /* default_value */);
2685 if (offloadInfo.has_video && !allowOffloadWithVideo) {
Eric Laurente552edb2014-03-10 17:42:56 -07002686 ALOGV("isOffloadSupported: has_video == true, returning false");
2687 return false;
2688 }
2689
2690 //If duration is less than minimum value defined in property, return false
2691 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2692 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2693 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2694 return false;
2695 }
2696 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2697 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2698 return false;
2699 }
2700
2701 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2702 // creating an offloaded track and tearing it down immediately after start when audioflinger
2703 // detects there is an active non offloadable effect.
2704 // FIXME: We should check the audio session here but we do not have it in this context.
2705 // This may prevent offloading in rare situations where effects are left active by apps
2706 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002707 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002708 return false;
2709 }
2710
2711 // See if there is a profile to support this.
2712 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002713 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002714 offloadInfo.sample_rate,
2715 offloadInfo.format,
2716 offloadInfo.channel_mask,
2717 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002718 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2719 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002720}
2721
Eric Laurent6a94d692014-05-20 11:18:06 -07002722status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2723 audio_port_type_t type,
2724 unsigned int *num_ports,
2725 struct audio_port *ports,
2726 unsigned int *generation)
2727{
2728 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2729 generation == NULL) {
2730 return BAD_VALUE;
2731 }
2732 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2733 if (ports == NULL) {
2734 *num_ports = 0;
2735 }
2736
2737 size_t portsWritten = 0;
2738 size_t portsMax = *num_ports;
2739 *num_ports = 0;
2740 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002741 // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB
2742 // as they are used by stub HALs by convention
Eric Laurent6a94d692014-05-20 11:18:06 -07002743 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002744 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
2745 if (mAvailableOutputDevices[i]->type() == AUDIO_DEVICE_OUT_STUB) {
2746 continue;
2747 }
2748 if (portsWritten < portsMax) {
2749 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2750 }
2751 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002752 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002753 }
2754 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002755 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
2756 if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_STUB) {
2757 continue;
2758 }
2759 if (portsWritten < portsMax) {
2760 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2761 }
2762 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002763 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002764 }
2765 }
2766 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2767 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2768 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2769 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2770 }
2771 *num_ports += mInputs.size();
2772 }
2773 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002774 size_t numOutputs = 0;
2775 for (size_t i = 0; i < mOutputs.size(); i++) {
2776 if (!mOutputs[i]->isDuplicated()) {
2777 numOutputs++;
2778 if (portsWritten < portsMax) {
2779 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2780 }
2781 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002782 }
Eric Laurent84c70242014-06-23 08:46:27 -07002783 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002784 }
2785 }
2786 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002787 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002788 return NO_ERROR;
2789}
2790
2791status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2792{
2793 return NO_ERROR;
2794}
2795
Eric Laurent6a94d692014-05-20 11:18:06 -07002796status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2797 audio_patch_handle_t *handle,
2798 uid_t uid)
2799{
2800 ALOGV("createAudioPatch()");
2801
2802 if (handle == NULL || patch == NULL) {
2803 return BAD_VALUE;
2804 }
2805 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2806
Eric Laurent874c42872014-08-08 15:13:39 -07002807 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2808 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2809 return BAD_VALUE;
2810 }
2811 // only one source per audio patch supported for now
2812 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002813 return INVALID_OPERATION;
2814 }
Eric Laurent874c42872014-08-08 15:13:39 -07002815
2816 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002817 return INVALID_OPERATION;
2818 }
Eric Laurent874c42872014-08-08 15:13:39 -07002819 for (size_t i = 0; i < patch->num_sinks; i++) {
2820 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2821 return INVALID_OPERATION;
2822 }
2823 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002824
2825 sp<AudioPatch> patchDesc;
2826 ssize_t index = mAudioPatches.indexOfKey(*handle);
2827
Eric Laurent6a94d692014-05-20 11:18:06 -07002828 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2829 patch->sources[0].role,
2830 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002831#if LOG_NDEBUG == 0
2832 for (size_t i = 0; i < patch->num_sinks; i++) {
Eric Laurent7b279bb2015-12-14 10:18:23 -08002833 ALOGV("createAudioPatch sink %zu: id %d role %d type %d", i, patch->sinks[i].id,
Eric Laurent874c42872014-08-08 15:13:39 -07002834 patch->sinks[i].role,
2835 patch->sinks[i].type);
2836 }
2837#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002838
2839 if (index >= 0) {
2840 patchDesc = mAudioPatches.valueAt(index);
2841 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2842 mUidCached, patchDesc->mUid, uid);
2843 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2844 return INVALID_OPERATION;
2845 }
2846 } else {
Glenn Kastena13cde92016-03-28 15:26:02 -07002847 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07002848 }
2849
2850 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002851 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002852 if (outputDesc == NULL) {
2853 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2854 return BAD_VALUE;
2855 }
Eric Laurent84c70242014-06-23 08:46:27 -07002856 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2857 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002858 if (patchDesc != 0) {
2859 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2860 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2861 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2862 return BAD_VALUE;
2863 }
2864 }
Eric Laurent874c42872014-08-08 15:13:39 -07002865 DeviceVector devices;
2866 for (size_t i = 0; i < patch->num_sinks; i++) {
2867 // Only support mix to devices connection
2868 // TODO add support for mix to mix connection
2869 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2870 ALOGV("createAudioPatch() source mix but sink is not a device");
2871 return INVALID_OPERATION;
2872 }
2873 sp<DeviceDescriptor> devDesc =
2874 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2875 if (devDesc == 0) {
2876 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2877 return BAD_VALUE;
2878 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002879
François Gaffie53615e22015-03-19 09:24:12 +01002880 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002881 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002882 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002883 NULL, // updatedSamplingRate
2884 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002885 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002886 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002887 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002888 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002889 ALOGV("createAudioPatch() profile not supported for device %08x",
2890 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002891 return INVALID_OPERATION;
2892 }
2893 devices.add(devDesc);
2894 }
2895 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002896 return INVALID_OPERATION;
2897 }
Eric Laurent874c42872014-08-08 15:13:39 -07002898
Eric Laurent6a94d692014-05-20 11:18:06 -07002899 // TODO: reconfigure output format and channels here
2900 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002901 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002902 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002903 index = mAudioPatches.indexOfKey(*handle);
2904 if (index >= 0) {
2905 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2906 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2907 }
2908 patchDesc = mAudioPatches.valueAt(index);
2909 patchDesc->mUid = uid;
2910 ALOGV("createAudioPatch() success");
2911 } else {
2912 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2913 return INVALID_OPERATION;
2914 }
2915 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2916 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2917 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002918 // only one sink supported when connecting an input device to a mix
2919 if (patch->num_sinks > 1) {
2920 return INVALID_OPERATION;
2921 }
François Gaffie53615e22015-03-19 09:24:12 +01002922 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002923 if (inputDesc == NULL) {
2924 return BAD_VALUE;
2925 }
2926 if (patchDesc != 0) {
2927 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2928 return BAD_VALUE;
2929 }
2930 }
2931 sp<DeviceDescriptor> devDesc =
2932 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2933 if (devDesc == 0) {
2934 return BAD_VALUE;
2935 }
2936
François Gaffie53615e22015-03-19 09:24:12 +01002937 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002938 devDesc->mAddress,
2939 patch->sinks[0].sample_rate,
2940 NULL, /*updatedSampleRate*/
2941 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002942 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002943 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002944 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002945 // FIXME for the parameter type,
2946 // and the NONE
2947 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002948 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002949 return INVALID_OPERATION;
2950 }
2951 // TODO: reconfigure output format and channels here
2952 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002953 devDesc->type(), inputDesc->mIoHandle);
2954 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002955 index = mAudioPatches.indexOfKey(*handle);
2956 if (index >= 0) {
2957 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2958 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2959 }
2960 patchDesc = mAudioPatches.valueAt(index);
2961 patchDesc->mUid = uid;
2962 ALOGV("createAudioPatch() success");
2963 } else {
2964 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2965 return INVALID_OPERATION;
2966 }
2967 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2968 // device to device connection
2969 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002970 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002971 return BAD_VALUE;
2972 }
2973 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002974 sp<DeviceDescriptor> srcDeviceDesc =
2975 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002976 if (srcDeviceDesc == 0) {
2977 return BAD_VALUE;
2978 }
Eric Laurent874c42872014-08-08 15:13:39 -07002979
Eric Laurent6a94d692014-05-20 11:18:06 -07002980 //update source and sink with our own data as the data passed in the patch may
2981 // be incomplete.
2982 struct audio_patch newPatch = *patch;
2983 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002984
Eric Laurent874c42872014-08-08 15:13:39 -07002985 for (size_t i = 0; i < patch->num_sinks; i++) {
2986 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2987 ALOGV("createAudioPatch() source device but one sink is not a device");
2988 return INVALID_OPERATION;
2989 }
2990
2991 sp<DeviceDescriptor> sinkDeviceDesc =
2992 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2993 if (sinkDeviceDesc == 0) {
2994 return BAD_VALUE;
2995 }
2996 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2997
Eric Laurent3bcf8592015-04-03 12:13:24 -07002998 // create a software bridge in PatchPanel if:
2999 // - source and sink devices are on differnt HW modules OR
3000 // - audio HAL version is < 3.0
Eric Laurentfb66dd92016-01-28 18:32:03 -08003001 if (!srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) ||
Mikhail Naganov9ee05402016-10-13 15:58:17 -07003002 (srcDeviceDesc->mModule->getHalVersionMajor() < 3)) {
Eric Laurent3bcf8592015-04-03 12:13:24 -07003003 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07003004 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07003005 return INVALID_OPERATION;
3006 }
Eric Laurent874c42872014-08-08 15:13:39 -07003007 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01003008 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07003009 // if the sink device is reachable via an opened output stream, request to go via
3010 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07003011 audio_io_handle_t output = selectOutput(outputs,
3012 AUDIO_OUTPUT_FLAG_NONE,
3013 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07003014 if (output != AUDIO_IO_HANDLE_NONE) {
3015 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
3016 if (outputDesc->isDuplicated()) {
3017 return INVALID_OPERATION;
3018 }
3019 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07003020 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07003021 newPatch.num_sources = 2;
3022 }
Eric Laurent83b88082014-06-20 18:31:16 -07003023 }
Eric Laurent6a94d692014-05-20 11:18:06 -07003024 }
3025 // TODO: check from routing capabilities in config file and other conflicting patches
3026
3027 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3028 if (index >= 0) {
3029 afPatchHandle = patchDesc->mAfPatchHandle;
3030 }
3031
3032 status_t status = mpClientInterface->createAudioPatch(&newPatch,
3033 &afPatchHandle,
3034 0);
3035 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
3036 status, afPatchHandle);
3037 if (status == NO_ERROR) {
3038 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01003039 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07003040 addAudioPatch(patchDesc->mHandle, patchDesc);
3041 } else {
3042 patchDesc->mPatch = newPatch;
3043 }
3044 patchDesc->mAfPatchHandle = afPatchHandle;
3045 *handle = patchDesc->mHandle;
3046 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07003047 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07003048 } else {
3049 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
3050 status);
3051 return INVALID_OPERATION;
3052 }
3053 } else {
3054 return BAD_VALUE;
3055 }
3056 } else {
3057 return BAD_VALUE;
3058 }
3059 return NO_ERROR;
3060}
3061
3062status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
3063 uid_t uid)
3064{
3065 ALOGV("releaseAudioPatch() patch %d", handle);
3066
3067 ssize_t index = mAudioPatches.indexOfKey(handle);
3068
3069 if (index < 0) {
3070 return BAD_VALUE;
3071 }
3072 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3073 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
3074 mUidCached, patchDesc->mUid, uid);
3075 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
3076 return INVALID_OPERATION;
3077 }
3078
3079 struct audio_patch *patch = &patchDesc->mPatch;
3080 patchDesc->mUid = mUidCached;
3081 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003082 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07003083 if (outputDesc == NULL) {
3084 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
3085 return BAD_VALUE;
3086 }
3087
Eric Laurentc75307b2015-03-17 15:29:32 -07003088 setOutputDevice(outputDesc,
3089 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07003090 true,
3091 0,
3092 NULL);
3093 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
3094 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01003095 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07003096 if (inputDesc == NULL) {
3097 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
3098 return BAD_VALUE;
3099 }
3100 setInputDevice(inputDesc->mIoHandle,
Eric Laurentfb66dd92016-01-28 18:32:03 -08003101 getNewInputDevice(inputDesc),
Eric Laurent6a94d692014-05-20 11:18:06 -07003102 true,
3103 NULL);
3104 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003105 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3106 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
3107 status, patchDesc->mAfPatchHandle);
3108 removeAudioPatch(patchDesc->mHandle);
3109 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07003110 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07003111 } else {
3112 return BAD_VALUE;
3113 }
3114 } else {
3115 return BAD_VALUE;
3116 }
3117 return NO_ERROR;
3118}
3119
3120status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
3121 struct audio_patch *patches,
3122 unsigned int *generation)
3123{
François Gaffie53615e22015-03-19 09:24:12 +01003124 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003125 return BAD_VALUE;
3126 }
Eric Laurent6a94d692014-05-20 11:18:06 -07003127 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01003128 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07003129}
3130
Eric Laurente1715a42014-05-20 11:30:42 -07003131status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07003132{
Eric Laurente1715a42014-05-20 11:30:42 -07003133 ALOGV("setAudioPortConfig()");
3134
3135 if (config == NULL) {
3136 return BAD_VALUE;
3137 }
3138 ALOGV("setAudioPortConfig() on port handle %d", config->id);
3139 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07003140 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
3141 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07003142 }
3143
Eric Laurenta121f902014-06-03 13:32:54 -07003144 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07003145 if (config->type == AUDIO_PORT_TYPE_MIX) {
3146 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003147 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07003148 if (outputDesc == NULL) {
3149 return BAD_VALUE;
3150 }
Eric Laurent84c70242014-06-23 08:46:27 -07003151 ALOG_ASSERT(!outputDesc->isDuplicated(),
3152 "setAudioPortConfig() called on duplicated output %d",
3153 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07003154 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07003155 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01003156 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07003157 if (inputDesc == NULL) {
3158 return BAD_VALUE;
3159 }
Eric Laurenta121f902014-06-03 13:32:54 -07003160 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07003161 } else {
3162 return BAD_VALUE;
3163 }
3164 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
3165 sp<DeviceDescriptor> deviceDesc;
3166 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
3167 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
3168 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
3169 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
3170 } else {
3171 return BAD_VALUE;
3172 }
3173 if (deviceDesc == NULL) {
3174 return BAD_VALUE;
3175 }
Eric Laurenta121f902014-06-03 13:32:54 -07003176 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07003177 } else {
3178 return BAD_VALUE;
3179 }
3180
Eric Laurenta121f902014-06-03 13:32:54 -07003181 struct audio_port_config backupConfig;
3182 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
3183 if (status == NO_ERROR) {
3184 struct audio_port_config newConfig;
3185 audioPortConfig->toAudioPortConfig(&newConfig, config);
3186 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07003187 }
Eric Laurenta121f902014-06-03 13:32:54 -07003188 if (status != NO_ERROR) {
3189 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07003190 }
Eric Laurente1715a42014-05-20 11:30:42 -07003191
3192 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07003193}
3194
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003195void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
3196{
Eric Laurentd60560a2015-04-10 11:31:20 -07003197 clearAudioSources(uid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003198 clearAudioPatches(uid);
3199 clearSessionRoutes(uid);
3200}
3201
Eric Laurent6a94d692014-05-20 11:18:06 -07003202void AudioPolicyManager::clearAudioPatches(uid_t uid)
3203{
Eric Laurent0add0fd2014-12-04 18:58:14 -08003204 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003205 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
3206 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08003207 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07003208 }
3209 }
3210}
3211
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003212void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
3213 audio_io_handle_t ouptutToSkip)
3214{
3215 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
3216 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
3217 for (size_t j = 0; j < mOutputs.size(); j++) {
3218 if (mOutputs.keyAt(j) == ouptutToSkip) {
3219 continue;
3220 }
3221 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
3222 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
3223 continue;
3224 }
3225 // If the default device for this strategy is on another output mix,
3226 // invalidate all tracks in this strategy to force re connection.
3227 // Otherwise select new device on the output mix.
3228 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
Eric Laurent794fde22016-03-11 09:50:45 -08003229 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003230 if (getStrategy((audio_stream_type_t)stream) == strategy) {
3231 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
3232 }
3233 }
3234 } else {
3235 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
3236 setOutputDevice(outputDesc, newDevice, false);
3237 }
3238 }
3239}
3240
3241void AudioPolicyManager::clearSessionRoutes(uid_t uid)
3242{
3243 // remove output routes associated with this uid
3244 SortedVector<routing_strategy> affectedStrategies;
3245 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
3246 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
3247 if (route->mUid == uid) {
3248 mOutputRoutes.removeItemsAt(i);
3249 if (route->mDeviceDescriptor != 0) {
3250 affectedStrategies.add(getStrategy(route->mStreamType));
3251 }
3252 }
3253 }
3254 // reroute outputs if necessary
3255 for (size_t i = 0; i < affectedStrategies.size(); i++) {
3256 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
3257 }
3258
3259 // remove input routes associated with this uid
3260 SortedVector<audio_source_t> affectedSources;
3261 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
3262 sp<SessionRoute> route = mInputRoutes.valueAt(i);
3263 if (route->mUid == uid) {
3264 mInputRoutes.removeItemsAt(i);
3265 if (route->mDeviceDescriptor != 0) {
3266 affectedSources.add(route->mSource);
3267 }
3268 }
3269 }
3270 // reroute inputs if necessary
3271 SortedVector<audio_io_handle_t> inputsToClose;
3272 for (size_t i = 0; i < mInputs.size(); i++) {
3273 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08003274 if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003275 inputsToClose.add(inputDesc->mIoHandle);
3276 }
3277 }
3278 for (size_t i = 0; i < inputsToClose.size(); i++) {
3279 closeInput(inputsToClose[i]);
3280 }
3281}
3282
Eric Laurentd60560a2015-04-10 11:31:20 -07003283void AudioPolicyManager::clearAudioSources(uid_t uid)
3284{
3285 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
3286 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3287 if (sourceDesc->mUid == uid) {
3288 stopAudioSource(mAudioSources.keyAt(i));
3289 }
3290 }
3291}
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003292
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07003293status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
3294 audio_io_handle_t *ioHandle,
3295 audio_devices_t *device)
3296{
Glenn Kastenf0c6d7d2016-02-26 10:44:04 -08003297 *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
3298 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
Eric Laurentc73ca6e2014-12-12 14:34:22 -08003299 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07003300
François Gaffiedf372692015-03-19 10:43:27 +01003301 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07003302}
3303
Eric Laurentd60560a2015-04-10 11:31:20 -07003304status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source,
3305 const audio_attributes_t *attributes,
Glenn Kasten559d4392016-03-29 13:42:57 -07003306 audio_patch_handle_t *handle,
Eric Laurentd60560a2015-04-10 11:31:20 -07003307 uid_t uid)
Eric Laurent554a2772015-04-10 11:29:24 -07003308{
Eric Laurentd60560a2015-04-10 11:31:20 -07003309 ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle);
3310 if (source == NULL || attributes == NULL || handle == NULL) {
3311 return BAD_VALUE;
3312 }
3313
Glenn Kasten559d4392016-03-29 13:42:57 -07003314 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentd60560a2015-04-10 11:31:20 -07003315
3316 if (source->role != AUDIO_PORT_ROLE_SOURCE ||
3317 source->type != AUDIO_PORT_TYPE_DEVICE) {
3318 ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type);
3319 return INVALID_OPERATION;
3320 }
3321
3322 sp<DeviceDescriptor> srcDeviceDesc =
3323 mAvailableInputDevices.getDevice(source->ext.device.type,
3324 String8(source->ext.device.address));
3325 if (srcDeviceDesc == 0) {
3326 ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type);
3327 return BAD_VALUE;
3328 }
3329 sp<AudioSourceDescriptor> sourceDesc =
3330 new AudioSourceDescriptor(srcDeviceDesc, attributes, uid);
3331
3332 struct audio_patch dummyPatch;
3333 sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid);
3334 sourceDesc->mPatchDesc = patchDesc;
3335
3336 status_t status = connectAudioSource(sourceDesc);
3337 if (status == NO_ERROR) {
3338 mAudioSources.add(sourceDesc->getHandle(), sourceDesc);
3339 *handle = sourceDesc->getHandle();
3340 }
3341 return status;
3342}
3343
3344status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3345{
3346 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3347
3348 // make sure we only have one patch per source.
3349 disconnectAudioSource(sourceDesc);
3350
3351 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
Eric Laurent28d09f02016-03-08 10:43:05 -08003352 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003353 sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice;
3354
3355 audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true);
3356 sp<DeviceDescriptor> sinkDeviceDesc =
3357 mAvailableOutputDevices.getDevice(sinkDevice, String8(""));
3358
3359 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3360 struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch;
3361
3362 if (srcDeviceDesc->getAudioPort()->mModule->getHandle() ==
3363 sinkDeviceDesc->getAudioPort()->mModule->getHandle() &&
Mikhail Naganov9ee05402016-10-13 15:58:17 -07003364 srcDeviceDesc->getAudioPort()->mModule->getHalVersionMajor() >= 3 &&
Eric Laurentd60560a2015-04-10 11:31:20 -07003365 srcDeviceDesc->getAudioPort()->mGains.size() > 0) {
3366 ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__);
3367 // create patch between src device and output device
3368 // create Hwoutput and add to mHwOutputs
3369 } else {
3370 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs);
3371 audio_io_handle_t output =
3372 selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID);
3373 if (output == AUDIO_IO_HANDLE_NONE) {
3374 ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice);
3375 return INVALID_OPERATION;
3376 }
3377 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
3378 if (outputDesc->isDuplicated()) {
3379 ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice);
3380 return INVALID_OPERATION;
3381 }
3382 // create a special patch with no sink and two sources:
3383 // - the second source indicates to PatchPanel through which output mix this patch should
3384 // be connected as well as the stream type for volume control
3385 // - the sink is defined by whatever output device is currently selected for the output
3386 // though which this patch is routed.
3387 patch->num_sinks = 0;
3388 patch->num_sources = 2;
3389 srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL);
3390 outputDesc->toAudioPortConfig(&patch->sources[1], NULL);
3391 patch->sources[1].ext.mix.usecase.stream = stream;
3392 status_t status = mpClientInterface->createAudioPatch(patch,
3393 &afPatchHandle,
3394 0);
3395 ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__,
3396 status, afPatchHandle);
3397 if (status != NO_ERROR) {
3398 ALOGW("%s patch panel could not connect device patch, error %d",
3399 __FUNCTION__, status);
3400 return INVALID_OPERATION;
3401 }
3402 uint32_t delayMs = 0;
Eric Laurentc40d9692016-04-13 19:14:13 -07003403 status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs);
Eric Laurentd60560a2015-04-10 11:31:20 -07003404
3405 if (status != NO_ERROR) {
3406 mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0);
3407 return status;
3408 }
3409 sourceDesc->mSwOutput = outputDesc;
3410 if (delayMs != 0) {
3411 usleep(delayMs * 1000);
3412 }
3413 }
3414
3415 sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle;
3416 addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc);
3417
3418 return NO_ERROR;
Eric Laurent554a2772015-04-10 11:29:24 -07003419}
3420
Glenn Kasten559d4392016-03-29 13:42:57 -07003421status_t AudioPolicyManager::stopAudioSource(audio_patch_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07003422{
Eric Laurentd60560a2015-04-10 11:31:20 -07003423 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle);
3424 ALOGV("%s handle %d", __FUNCTION__, handle);
3425 if (sourceDesc == 0) {
3426 ALOGW("%s unknown source for handle %d", __FUNCTION__, handle);
3427 return BAD_VALUE;
3428 }
3429 status_t status = disconnectAudioSource(sourceDesc);
3430
3431 mAudioSources.removeItem(handle);
3432 return status;
3433}
3434
Andy Hung2ddee192015-12-18 17:34:44 -08003435status_t AudioPolicyManager::setMasterMono(bool mono)
3436{
3437 if (mMasterMono == mono) {
3438 return NO_ERROR;
3439 }
3440 mMasterMono = mono;
3441 // if enabling mono we close all offloaded devices, which will invalidate the
3442 // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible
3443 // for recreating the new AudioTrack as non-offloaded PCM.
3444 //
3445 // If disabling mono, we leave all tracks as is: we don't know which clients
3446 // and tracks are able to be recreated as offloaded. The next "song" should
3447 // play back offloaded.
3448 if (mMasterMono) {
3449 Vector<audio_io_handle_t> offloaded;
3450 for (size_t i = 0; i < mOutputs.size(); ++i) {
3451 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
3452 if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
3453 offloaded.push(desc->mIoHandle);
3454 }
3455 }
3456 for (size_t i = 0; i < offloaded.size(); ++i) {
3457 closeOutput(offloaded[i]);
3458 }
3459 }
3460 // update master mono for all remaining outputs
3461 for (size_t i = 0; i < mOutputs.size(); ++i) {
3462 updateMono(mOutputs.keyAt(i));
3463 }
3464 return NO_ERROR;
3465}
3466
3467status_t AudioPolicyManager::getMasterMono(bool *mono)
3468{
3469 *mono = mMasterMono;
3470 return NO_ERROR;
3471}
3472
Eric Laurentac9cef52017-06-09 15:46:26 -07003473float AudioPolicyManager::getStreamVolumeDB(
3474 audio_stream_type_t stream, int index, audio_devices_t device)
3475{
3476 return computeVolume(stream, index, device);
3477}
3478
Eric Laurentd60560a2015-04-10 11:31:20 -07003479status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3480{
3481 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3482
3483 sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle);
3484 if (patchDesc == 0) {
3485 ALOGW("%s source has no patch with handle %d", __FUNCTION__,
3486 sourceDesc->mPatchDesc->mHandle);
3487 return BAD_VALUE;
3488 }
3489 removeAudioPatch(sourceDesc->mPatchDesc->mHandle);
3490
Eric Laurent28d09f02016-03-08 10:43:05 -08003491 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003492 sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote();
3493 if (swOutputDesc != 0) {
3494 stopSource(swOutputDesc, stream, false);
3495 mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3496 } else {
3497 sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote();
3498 if (hwOutputDesc != 0) {
3499 // release patch between src device and output device
3500 // close Hwoutput and remove from mHwOutputs
3501 } else {
3502 ALOGW("%s source has neither SW nor HW output", __FUNCTION__);
3503 }
3504 }
3505 return NO_ERROR;
3506}
3507
3508sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput(
3509 audio_io_handle_t output, routing_strategy strategy)
3510{
3511 sp<AudioSourceDescriptor> source;
3512 for (size_t i = 0; i < mAudioSources.size(); i++) {
3513 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3514 routing_strategy sourceStrategy =
3515 (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
3516 sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote();
3517 if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) {
3518 source = sourceDesc;
3519 break;
3520 }
3521 }
3522 return source;
Eric Laurent554a2772015-04-10 11:29:24 -07003523}
3524
Eric Laurente552edb2014-03-10 17:42:56 -07003525// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07003526// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07003527// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07003528uint32_t AudioPolicyManager::nextAudioPortGeneration()
3529{
3530 return android_atomic_inc(&mAudioPortGeneration);
3531}
3532
Jaekyun Seok0d4a6af2017-02-17 17:10:17 +09003533#ifdef USE_XML_AUDIO_POLICY_CONF
3534// Treblized audio policy xml config will be located in /odm/etc or /vendor/etc.
3535static const char *kConfigLocationList[] =
3536 {"/odm/etc", "/vendor/etc", "/system/etc"};
3537static const int kConfigLocationListSize =
3538 (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0]));
3539
3540static status_t deserializeAudioPolicyXmlConfig(AudioPolicyConfig &config) {
3541 char audioPolicyXmlConfigFile[AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH];
3542 status_t ret;
3543
3544 for (int i = 0; i < kConfigLocationListSize; i++) {
3545 PolicySerializer serializer;
3546 snprintf(audioPolicyXmlConfigFile,
3547 sizeof(audioPolicyXmlConfigFile),
3548 "%s/%s",
3549 kConfigLocationList[i],
3550 AUDIO_POLICY_XML_CONFIG_FILE_NAME);
3551 ret = serializer.deserialize(audioPolicyXmlConfigFile, config);
3552 if (ret == NO_ERROR) {
3553 break;
3554 }
3555 }
3556 return ret;
3557}
3558#endif
3559
Eric Laurente0720872014-03-11 09:30:41 -07003560AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07003561 :
Eric Laurente552edb2014-03-10 17:42:56 -07003562 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07003563 mA2dpSuspended(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003564 mAudioPortGeneration(1),
3565 mBeaconMuteRefCount(0),
3566 mBeaconPlayingRefCount(0),
Eric Laurent9459fb02015-08-12 18:36:32 -07003567 mBeaconMuted(false),
Andy Hung2ddee192015-12-18 17:34:44 -08003568 mTtsOutputAvailable(false),
Eric Laurent36829f92017-04-07 19:04:42 -07003569 mMasterMono(false),
Chris Thornton2b864642017-06-27 21:26:07 -07003570 mMusicEffectOutput(AUDIO_IO_HANDLE_NONE),
3571 mHasComputedSoundTriggerSupportsConcurrentCapture(false)
Eric Laurente552edb2014-03-10 17:42:56 -07003572{
François Gaffied1ab2bd2015-12-02 18:20:06 +01003573 mUidCached = getuid();
3574 mpClientInterface = clientInterface;
3575
3576 // TODO: remove when legacy conf file is removed. true on devices that use DRC on the
3577 // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly.
3578 // Note: remove also speaker_drc_enabled from global configuration of XML config file.
3579 bool speakerDrcEnabled = false;
3580
3581#ifdef USE_XML_AUDIO_POLICY_CONF
3582 mVolumeCurves = new VolumeCurvesCollection();
3583 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3584 mDefaultOutputDevice, speakerDrcEnabled,
3585 static_cast<VolumeCurvesCollection *>(mVolumeCurves));
Jaekyun Seok0d4a6af2017-02-17 17:10:17 +09003586 if (deserializeAudioPolicyXmlConfig(config) != NO_ERROR) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01003587#else
3588 mVolumeCurves = new StreamDescriptorCollection();
3589 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3590 mDefaultOutputDevice, speakerDrcEnabled);
3591 if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) &&
3592 (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) {
3593#endif
3594 ALOGE("could not load audio policy configuration file, setting defaults");
3595 config.setDefault();
3596 }
3597 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
3598 mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled);
3599
3600 // Once policy config has been parsed, retrieve an instance of the engine and initialize it.
François Gaffie2110e042015-03-24 08:41:51 +01003601 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
3602 if (!engineInstance) {
3603 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
3604 return;
3605 }
3606 // Retrieve the Policy Manager Interface
3607 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
3608 if (mEngine == NULL) {
3609 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
3610 return;
3611 }
3612 mEngine->setObserver(this);
3613 status_t status = mEngine->initCheck();
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07003614 (void) status;
François Gaffie2110e042015-03-24 08:41:51 +01003615 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
3616
Eric Laurent3a4311c2014-03-17 12:00:47 -07003617 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07003618 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07003619 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
3620 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07003621 for (size_t i = 0; i < mHwModules.size(); i++) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003622 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003623 if (mHwModules[i]->mHandle == 0) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003624 ALOGW("could not open HW module %s", mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003625 continue;
3626 }
3627 // open all output streams needed to access attached devices
3628 // except for direct output streams that are only opened when they are actually
3629 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07003630 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07003631 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3632 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003633 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003634
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003635 if (!outProfile->hasSupportedDevices()) {
3636 ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003637 continue;
3638 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003639 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) {
Eric Laurent9459fb02015-08-12 18:36:32 -07003640 mTtsOutputAvailable = true;
3641 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003642
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003643 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
Eric Laurentd78f1532014-09-16 16:38:20 -07003644 continue;
3645 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003646 audio_devices_t profileType = outProfile->getSupportedDevicesType();
François Gaffie53615e22015-03-19 09:24:12 +01003647 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
3648 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07003649 } else {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003650 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003651 // outputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003652 profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes);
Eric Laurente552edb2014-03-10 17:42:56 -07003653 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003654 if ((profileType & outputDeviceTypes) == 0) {
3655 continue;
3656 }
Eric Laurentc75307b2015-03-17 15:29:32 -07003657 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
3658 mpClientInterface);
Eric Laurentc40d9692016-04-13 19:14:13 -07003659 const DeviceVector &supportedDevices = outProfile->getSupportedDevices();
3660 const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType);
3661 String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress
3662 : String8("");
Eric Laurentd78f1532014-09-16 16:38:20 -07003663
3664 outputDesc->mDevice = profileType;
3665 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3666 config.sample_rate = outputDesc->mSamplingRate;
3667 config.channel_mask = outputDesc->mChannelMask;
3668 config.format = outputDesc->mFormat;
3669 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003670 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003671 &output,
3672 &config,
3673 &outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003674 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003675 &outputDesc->mLatency,
3676 outputDesc->mFlags);
3677
3678 if (status != NO_ERROR) {
3679 ALOGW("Cannot open output stream for device %08x on hw module %s",
3680 outputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003681 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003682 } else {
3683 outputDesc->mSamplingRate = config.sample_rate;
3684 outputDesc->mChannelMask = config.channel_mask;
3685 outputDesc->mFormat = config.format;
3686
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003687 for (size_t k = 0; k < supportedDevices.size(); k++) {
3688 ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003689 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08003690 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
3691 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003692 }
3693 }
3694 if (mPrimaryOutput == 0 &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003695 outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003696 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07003697 }
3698 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003699 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07003700 outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003701 true,
3702 0,
3703 NULL,
3704 address.string());
Eric Laurentd78f1532014-09-16 16:38:20 -07003705 }
Eric Laurente552edb2014-03-10 17:42:56 -07003706 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003707 // open input streams needed to access attached devices to validate
3708 // mAvailableInputDevices list
3709 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
3710 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003711 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003712
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003713 if (!inProfile->hasSupportedDevices()) {
3714 ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003715 continue;
3716 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003717 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003718 // inputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003719 audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes);
3720
Eric Laurentd78f1532014-09-16 16:38:20 -07003721 if ((profileType & inputDeviceTypes) == 0) {
3722 continue;
3723 }
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08003724 sp<AudioInputDescriptor> inputDesc =
3725 new AudioInputDescriptor(inProfile);
Eric Laurentd78f1532014-09-16 16:38:20 -07003726
Eric Laurentd78f1532014-09-16 16:38:20 -07003727 inputDesc->mDevice = profileType;
3728
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003729 // find the address
3730 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
3731 // the inputs vector must be of size 1, but we don't want to crash here
3732 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
3733 : String8("");
3734 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
3735 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
3736
Eric Laurentd78f1532014-09-16 16:38:20 -07003737 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3738 config.sample_rate = inputDesc->mSamplingRate;
3739 config.channel_mask = inputDesc->mChannelMask;
3740 config.format = inputDesc->mFormat;
3741 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003742 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003743 &input,
3744 &config,
3745 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003746 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003747 AUDIO_SOURCE_MIC,
3748 AUDIO_INPUT_FLAG_NONE);
3749
3750 if (status == NO_ERROR) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003751 const DeviceVector &supportedDevices = inProfile->getSupportedDevices();
3752 for (size_t k = 0; k < supportedDevices.size(); k++) {
3753 ssize_t index = mAvailableInputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003754 // give a valid ID to an attached device once confirmed it is reachable
Eric Laurent45aabc32015-08-06 09:11:13 -07003755 if (index >= 0) {
3756 sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index];
3757 if (!devDesc->isAttached()) {
3758 devDesc->attach(mHwModules[i]);
Eric Laurent83efe1c2017-07-09 16:51:08 -07003759 devDesc->importAudioPort(inProfile, true);
Eric Laurent45aabc32015-08-06 09:11:13 -07003760 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003761 }
3762 }
3763 mpClientInterface->closeInput(input);
3764 } else {
3765 ALOGW("Cannot open input stream for device %08x on hw module %s",
3766 inputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003767 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003768 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003769 }
3770 }
3771 // make sure all attached devices have been allocated a unique ID
3772 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003773 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003774 ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003775 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
3776 continue;
3777 }
François Gaffie2110e042015-03-24 08:41:51 +01003778 // The device is now validated and can be appended to the available devices of the engine
3779 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
3780 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003781 i++;
3782 }
3783 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003784 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01003785 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003786 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
3787 continue;
3788 }
François Gaffie2110e042015-03-24 08:41:51 +01003789 // The device is now validated and can be appended to the available devices of the engine
3790 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
3791 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003792 i++;
3793 }
3794 // make sure default device is reachable
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003795 if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01003796 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003797 }
Eric Laurente552edb2014-03-10 17:42:56 -07003798
3799 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
3800
3801 updateDevicesAndOutputs();
Eric Laurente552edb2014-03-10 17:42:56 -07003802}
3803
Eric Laurente0720872014-03-11 09:30:41 -07003804AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07003805{
Eric Laurente552edb2014-03-10 17:42:56 -07003806 for (size_t i = 0; i < mOutputs.size(); i++) {
3807 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003808 }
3809 for (size_t i = 0; i < mInputs.size(); i++) {
3810 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003811 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003812 mAvailableOutputDevices.clear();
3813 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003814 mOutputs.clear();
3815 mInputs.clear();
3816 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07003817}
3818
Eric Laurente0720872014-03-11 09:30:41 -07003819status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07003820{
Eric Laurent87ffa392015-05-22 10:32:38 -07003821 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003822}
3823
Eric Laurente552edb2014-03-10 17:42:56 -07003824// ---
3825
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003826void AudioPolicyManager::addOutput(audio_io_handle_t output, const sp<SwAudioOutputDescriptor>& outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003827{
François Gaffie98cc1912015-03-18 17:52:40 +01003828 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003829 mOutputs.add(output, outputDesc);
Andy Hung2ddee192015-12-18 17:34:44 -08003830 updateMono(output); // update mono status when adding to output list
Eric Laurent36829f92017-04-07 19:04:42 -07003831 selectOutputForMusicEffects();
Eric Laurent6a94d692014-05-20 11:18:06 -07003832 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003833}
3834
François Gaffie53615e22015-03-19 09:24:12 +01003835void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3836{
3837 mOutputs.removeItem(output);
Eric Laurent36829f92017-04-07 19:04:42 -07003838 selectOutputForMusicEffects();
François Gaffie53615e22015-03-19 09:24:12 +01003839}
3840
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003841void AudioPolicyManager::addInput(audio_io_handle_t input, const sp<AudioInputDescriptor>& inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003842{
François Gaffie98cc1912015-03-18 17:52:40 +01003843 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003844 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003845 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003846}
Eric Laurente552edb2014-03-10 17:42:56 -07003847
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003848void AudioPolicyManager::findIoHandlesByAddress(const sp<SwAudioOutputDescriptor>& desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003849 const audio_devices_t device /*in*/,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003850 const String8& address /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003851 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003852 sp<DeviceDescriptor> devDesc =
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003853 desc->mProfile->getSupportedDeviceByAddress(device, address);
keunyoung3190e672014-12-30 13:00:52 -08003854 if (devDesc != 0) {
3855 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3856 desc->mIoHandle, address.string());
3857 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003858 }
3859}
3860
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003861status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003862 audio_policy_dev_state_t state,
3863 SortedVector<audio_io_handle_t>& outputs,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003864 const String8& address)
Eric Laurente552edb2014-03-10 17:42:56 -07003865{
François Gaffie53615e22015-03-19 09:24:12 +01003866 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003867 sp<SwAudioOutputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003868
3869 if (audio_device_is_digital(device)) {
3870 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003871 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003872 }
Eric Laurente552edb2014-03-10 17:42:56 -07003873
Eric Laurent3b73df72014-03-11 09:06:29 -07003874 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003875 // first list already open outputs that can be routed to this device
3876 for (size_t i = 0; i < mOutputs.size(); i++) {
3877 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003878 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003879 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003880 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3881 outputs.add(mOutputs.keyAt(i));
3882 } else {
3883 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003884 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003885 }
Eric Laurente552edb2014-03-10 17:42:56 -07003886 }
3887 }
3888 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003889 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003890 for (size_t i = 0; i < mHwModules.size(); i++)
3891 {
3892 if (mHwModules[i]->mHandle == 0) {
3893 continue;
3894 }
3895 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3896 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003897 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003898 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003899 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003900 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003901 profiles.add(profile);
3902 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3903 }
Eric Laurente552edb2014-03-10 17:42:56 -07003904 }
3905 }
3906 }
3907
Eric Laurent7b279bb2015-12-14 10:18:23 -08003908 ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size());
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003909
Eric Laurente552edb2014-03-10 17:42:56 -07003910 if (profiles.isEmpty() && outputs.isEmpty()) {
3911 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3912 return BAD_VALUE;
3913 }
3914
3915 // open outputs for matching profiles if needed. Direct outputs are also opened to
3916 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3917 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003918 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003919
3920 // nothing to do if one output is already opened for this profile
3921 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003922 for (j = 0; j < outputs.size(); j++) {
3923 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003924 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003925 // matching profile: save the sample rates, format and channel masks supported
3926 // by the profile in our device descriptor
Paul McLean9080a4c2015-06-18 08:24:02 -07003927 if (audio_device_is_digital(device)) {
3928 devDesc->importAudioPort(profile);
3929 }
Eric Laurente552edb2014-03-10 17:42:56 -07003930 break;
3931 }
3932 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003933 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003934 continue;
3935 }
3936
Eric Laurent83efe1c2017-07-09 16:51:08 -07003937 ALOGV("opening output for device %08x with params %s profile %p name %s",
3938 device, address.string(), profile.get(), profile->getName().string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003939 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003940 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003941 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3942 config.sample_rate = desc->mSamplingRate;
3943 config.channel_mask = desc->mChannelMask;
3944 config.format = desc->mFormat;
3945 config.offload_info.sample_rate = desc->mSamplingRate;
3946 config.offload_info.channel_mask = desc->mChannelMask;
3947 config.offload_info.format = desc->mFormat;
3948 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003949 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003950 &output,
3951 &config,
3952 &desc->mDevice,
3953 address,
3954 &desc->mLatency,
3955 desc->mFlags);
3956 if (status == NO_ERROR) {
3957 desc->mSamplingRate = config.sample_rate;
3958 desc->mChannelMask = config.channel_mask;
3959 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003960
Eric Laurentd4692962014-05-05 18:13:44 -07003961 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003962 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003963 char *param = audio_device_address_to_parameter(device, address);
3964 mpClientInterface->setParameters(output, String8(param));
3965 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003966 }
Phil Burk00eeb322016-03-31 12:41:00 -07003967 updateAudioProfiles(device, output, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01003968 if (!profile->hasValidAudioProfile()) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003969 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003970 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003971 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003972 } else if (profile->hasDynamicAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07003973 mpClientInterface->closeOutput(output);
Phil Burk702b1052016-03-02 16:38:26 -08003974 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003975 profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003976 config.offload_info.sample_rate = config.sample_rate;
3977 config.offload_info.channel_mask = config.channel_mask;
3978 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07003979 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003980 &output,
3981 &config,
3982 &desc->mDevice,
3983 address,
3984 &desc->mLatency,
3985 desc->mFlags);
3986 if (status == NO_ERROR) {
3987 desc->mSamplingRate = config.sample_rate;
3988 desc->mChannelMask = config.channel_mask;
3989 desc->mFormat = config.format;
3990 } else {
3991 output = AUDIO_IO_HANDLE_NONE;
3992 }
Eric Laurentd4692962014-05-05 18:13:44 -07003993 }
3994
Eric Laurentcf2c0212014-07-25 16:20:43 -07003995 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003996 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003997 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003998 sp<AudioPolicyMix> policyMix;
3999 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004000 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
4001 address.string());
4002 }
François Gaffie036e1e92015-03-19 10:16:24 +01004003 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07004004 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01004005
Eric Laurent87ffa392015-05-22 10:32:38 -07004006 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
4007 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08004008 // no duplicated output for direct outputs and
4009 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07004010 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07004011
Eric Laurentd4692962014-05-05 18:13:44 -07004012 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07004013 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07004014
Eric Laurentd4692962014-05-05 18:13:44 -07004015 //TODO: configure audio effect output stage here
4016
4017 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07004018 duplicatedOutput =
4019 mpClientInterface->openDuplicateOutput(output,
4020 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004021 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07004022 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07004023 sp<SwAudioOutputDescriptor> dupOutputDesc =
4024 new SwAudioOutputDescriptor(NULL, mpClientInterface);
4025 dupOutputDesc->mOutput1 = mPrimaryOutput;
4026 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07004027 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
4028 dupOutputDesc->mFormat = desc->mFormat;
4029 dupOutputDesc->mChannelMask = desc->mChannelMask;
4030 dupOutputDesc->mLatency = desc->mLatency;
4031 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07004032 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07004033 } else {
4034 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07004035 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07004036 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01004037 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07004038 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07004039 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07004040 }
Eric Laurente552edb2014-03-10 17:42:56 -07004041 }
4042 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07004043 } else {
4044 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07004045 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07004046 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07004047 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004048 profiles.removeAt(profile_index);
4049 profile_index--;
4050 } else {
4051 outputs.add(output);
Paul McLean9080a4c2015-06-18 08:24:02 -07004052 // Load digital format info only for digital devices
4053 if (audio_device_is_digital(device)) {
4054 devDesc->importAudioPort(profile);
4055 }
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07004056
François Gaffie53615e22015-03-19 09:24:12 +01004057 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004058 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
4059 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07004060 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004061 NULL/*patch handle*/, address.string());
4062 }
Eric Laurente552edb2014-03-10 17:42:56 -07004063 ALOGV("checkOutputsForDevice(): adding output %d", output);
4064 }
4065 }
4066
4067 if (profiles.isEmpty()) {
4068 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
4069 return BAD_VALUE;
4070 }
Eric Laurentd4692962014-05-05 18:13:44 -07004071 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07004072 // check if one opened output is not needed any more after disconnecting one device
4073 for (size_t i = 0; i < mOutputs.size(); i++) {
4074 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004075 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004076 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01004077 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07004078 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08004079 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07004080 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004081 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
4082 mOutputs.keyAt(i));
4083 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004084 }
Eric Laurente552edb2014-03-10 17:42:56 -07004085 }
4086 }
Eric Laurentd4692962014-05-05 18:13:44 -07004087 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07004088 for (size_t i = 0; i < mHwModules.size(); i++)
4089 {
4090 if (mHwModules[i]->mHandle == 0) {
4091 continue;
4092 }
4093 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
4094 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004095 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004096 if (profile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07004097 ALOGV("checkOutputsForDevice(): "
4098 "clearing direct output profile %zu on module %zu", j, i);
François Gaffie112b0af2015-11-19 16:13:25 +01004099 profile->clearAudioProfiles();
Eric Laurente552edb2014-03-10 17:42:56 -07004100 }
4101 }
4102 }
4103 }
4104 return NO_ERROR;
4105}
4106
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004107status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01004108 audio_policy_dev_state_t state,
4109 SortedVector<audio_io_handle_t>& inputs,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004110 const String8& address)
Eric Laurentd4692962014-05-05 18:13:44 -07004111{
Paul McLean9080a4c2015-06-18 08:24:02 -07004112 audio_devices_t device = devDesc->type();
Eric Laurent1f2f2232014-06-02 12:01:23 -07004113 sp<AudioInputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07004114
4115 if (audio_device_is_digital(device)) {
4116 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08004117 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07004118 }
4119
Eric Laurentd4692962014-05-05 18:13:44 -07004120 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
4121 // first list already open inputs that can be routed to this device
4122 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
4123 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004124 if (desc->mProfile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07004125 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
4126 inputs.add(mInputs.keyAt(input_index));
4127 }
4128 }
4129
4130 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07004131 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07004132 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
4133 {
4134 if (mHwModules[module_idx]->mHandle == 0) {
4135 continue;
4136 }
4137 for (size_t profile_index = 0;
4138 profile_index < mHwModules[module_idx]->mInputProfiles.size();
4139 profile_index++)
4140 {
Eric Laurent275e8e92014-11-30 15:14:47 -08004141 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
4142
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004143 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01004144 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004145 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004146 profiles.add(profile);
4147 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
4148 profile_index, module_idx);
4149 }
Eric Laurentd4692962014-05-05 18:13:44 -07004150 }
4151 }
4152 }
4153
4154 if (profiles.isEmpty() && inputs.isEmpty()) {
4155 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
4156 return BAD_VALUE;
4157 }
4158
4159 // open inputs for matching profiles if needed. Direct inputs are also opened to
4160 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
4161 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
4162
Eric Laurent1c333e22014-05-20 10:48:17 -07004163 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07004164 // nothing to do if one input is already opened for this profile
4165 size_t input_index;
4166 for (input_index = 0; input_index < mInputs.size(); input_index++) {
4167 desc = mInputs.valueAt(input_index);
4168 if (desc->mProfile == profile) {
Paul McLean9080a4c2015-06-18 08:24:02 -07004169 if (audio_device_is_digital(device)) {
4170 devDesc->importAudioPort(profile);
4171 }
Eric Laurentd4692962014-05-05 18:13:44 -07004172 break;
4173 }
4174 }
4175 if (input_index != mInputs.size()) {
4176 continue;
4177 }
4178
4179 ALOGV("opening input for device 0x%X with params %s", device, address.string());
4180 desc = new AudioInputDescriptor(profile);
4181 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07004182 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
4183 config.sample_rate = desc->mSamplingRate;
4184 config.channel_mask = desc->mChannelMask;
4185 config.format = desc->mFormat;
4186 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent83efe1c2017-07-09 16:51:08 -07004187
4188 ALOGV("opening inputput for device %08x with params %s profile %p name %s",
4189 desc->mDevice, address.string(), profile.get(), profile->getName().string());
4190
Eric Laurent322b4d22015-04-03 15:57:54 -07004191 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07004192 &input,
4193 &config,
4194 &desc->mDevice,
4195 address,
4196 AUDIO_SOURCE_MIC,
4197 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07004198
Eric Laurentcf2c0212014-07-25 16:20:43 -07004199 if (status == NO_ERROR) {
4200 desc->mSamplingRate = config.sample_rate;
4201 desc->mChannelMask = config.channel_mask;
4202 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07004203
Eric Laurentd4692962014-05-05 18:13:44 -07004204 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07004205 char *param = audio_device_address_to_parameter(device, address);
4206 mpClientInterface->setParameters(input, String8(param));
4207 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07004208 }
Phil Burk00eeb322016-03-31 12:41:00 -07004209 updateAudioProfiles(device, input, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01004210 if (!profile->hasValidAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07004211 ALOGW("checkInputsForDevice() direct input missing param");
4212 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004213 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07004214 }
4215
4216 if (input != 0) {
4217 addInput(input, desc);
4218 }
4219 } // endif input != 0
4220
Eric Laurentcf2c0212014-07-25 16:20:43 -07004221 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07004222 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07004223 profiles.removeAt(profile_index);
4224 profile_index--;
4225 } else {
4226 inputs.add(input);
Paul McLean9080a4c2015-06-18 08:24:02 -07004227 if (audio_device_is_digital(device)) {
4228 devDesc->importAudioPort(profile);
4229 }
Eric Laurentd4692962014-05-05 18:13:44 -07004230 ALOGV("checkInputsForDevice(): adding input %d", input);
4231 }
4232 } // end scan profiles
4233
4234 if (profiles.isEmpty()) {
4235 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
4236 return BAD_VALUE;
4237 }
4238 } else {
4239 // Disconnect
4240 // check if one opened input is not needed any more after disconnecting one device
4241 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
4242 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004243 if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) {
Eric Laurentd4692962014-05-05 18:13:44 -07004244 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
4245 mInputs.keyAt(input_index));
4246 inputs.add(mInputs.keyAt(input_index));
4247 }
4248 }
4249 // Clear any profiles associated with the disconnected device.
4250 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
4251 if (mHwModules[module_index]->mHandle == 0) {
4252 continue;
4253 }
4254 for (size_t profile_index = 0;
4255 profile_index < mHwModules[module_index]->mInputProfiles.size();
4256 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004257 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004258 if (profile->supportDevice(device)) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07004259 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07004260 profile_index, module_index);
François Gaffie112b0af2015-11-19 16:13:25 +01004261 profile->clearAudioProfiles();
Eric Laurentd4692962014-05-05 18:13:44 -07004262 }
4263 }
4264 }
4265 } // end disconnect
4266
4267 return NO_ERROR;
4268}
4269
4270
Eric Laurente0720872014-03-11 09:30:41 -07004271void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07004272{
4273 ALOGV("closeOutput(%d)", output);
4274
Eric Laurentc75307b2015-03-17 15:29:32 -07004275 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004276 if (outputDesc == NULL) {
4277 ALOGW("closeOutput() unknown output %d", output);
4278 return;
4279 }
François Gaffie036e1e92015-03-19 10:16:24 +01004280 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08004281
Eric Laurente552edb2014-03-10 17:42:56 -07004282 // look for duplicated outputs connected to the output being removed.
4283 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004284 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07004285 if (dupOutputDesc->isDuplicated() &&
4286 (dupOutputDesc->mOutput1 == outputDesc ||
4287 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004288 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07004289 if (dupOutputDesc->mOutput1 == outputDesc) {
4290 outputDesc2 = dupOutputDesc->mOutput2;
4291 } else {
4292 outputDesc2 = dupOutputDesc->mOutput1;
4293 }
4294 // As all active tracks on duplicated output will be deleted,
4295 // and as they were also referenced on the other output, the reference
4296 // count for their stream type must be adjusted accordingly on
4297 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07004298 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07004299 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07004300 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07004301 }
4302 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
4303 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
4304
4305 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01004306 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07004307 }
4308 }
4309
Eric Laurent05b90f82014-08-27 15:32:29 -07004310 nextAudioPortGeneration();
4311
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004312 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004313 if (index >= 0) {
4314 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07004315 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07004316 mAudioPatches.removeItemsAt(index);
4317 mpClientInterface->onAudioPatchListUpdate();
4318 }
4319
Eric Laurente552edb2014-03-10 17:42:56 -07004320 AudioParameter param;
4321 param.add(String8("closing"), String8("true"));
4322 mpClientInterface->setParameters(output, param.toString());
4323
4324 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01004325 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004326 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07004327}
4328
4329void AudioPolicyManager::closeInput(audio_io_handle_t input)
4330{
4331 ALOGV("closeInput(%d)", input);
4332
4333 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
4334 if (inputDesc == NULL) {
4335 ALOGW("closeInput() unknown input %d", input);
4336 return;
4337 }
4338
Eric Laurent6a94d692014-05-20 11:18:06 -07004339 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07004340
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004341 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004342 if (index >= 0) {
4343 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07004344 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07004345 mAudioPatches.removeItemsAt(index);
4346 mpClientInterface->onAudioPatchListUpdate();
4347 }
4348
4349 mpClientInterface->closeInput(input);
4350 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07004351}
4352
Eric Laurentc75307b2015-03-17 15:29:32 -07004353SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
4354 audio_devices_t device,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004355 const SwAudioOutputCollection& openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07004356{
4357 SortedVector<audio_io_handle_t> outputs;
4358
4359 ALOGVV("getOutputsForDevice() device %04x", device);
4360 for (size_t i = 0; i < openOutputs.size(); i++) {
Eric Laurent37ddbb42016-08-10 16:19:14 -07004361 ALOGVV("output %zu isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004362 i, openOutputs.valueAt(i)->isDuplicated(),
4363 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004364 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
4365 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
4366 outputs.add(openOutputs.keyAt(i));
4367 }
4368 }
4369 return outputs;
4370}
4371
Eric Laurente0720872014-03-11 09:30:41 -07004372bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01004373 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07004374{
4375 if (outputs1.size() != outputs2.size()) {
4376 return false;
4377 }
4378 for (size_t i = 0; i < outputs1.size(); i++) {
4379 if (outputs1[i] != outputs2[i]) {
4380 return false;
4381 }
4382 }
4383 return true;
4384}
4385
Eric Laurente0720872014-03-11 09:30:41 -07004386void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07004387{
4388 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
4389 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
4390 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
4391 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
4392
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004393 // also take into account external policy-related changes: add all outputs which are
4394 // associated with policies in the "before" and "after" output vectors
4395 ALOGVV("checkOutputForStrategy(): policy related outputs");
4396 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004397 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004398 if (desc != 0 && desc->mPolicyMix != NULL) {
4399 srcOutputs.add(desc->mIoHandle);
4400 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
4401 }
4402 }
4403 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004404 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004405 if (desc != 0 && desc->mPolicyMix != NULL) {
4406 dstOutputs.add(desc->mIoHandle);
4407 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
4408 }
4409 }
4410
Eric Laurente552edb2014-03-10 17:42:56 -07004411 if (!vectorsEqual(srcOutputs,dstOutputs)) {
4412 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
4413 strategy, srcOutputs[0], dstOutputs[0]);
4414 // mute strategy while moving tracks from one output to another
4415 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004416 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01004417 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004418 setStrategyMute(strategy, true, desc);
4419 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004420 }
Eric Laurentd60560a2015-04-10 11:31:20 -07004421 sp<AudioSourceDescriptor> source =
4422 getSourceForStrategyOnOutput(srcOutputs[i], strategy);
4423 if (source != 0){
4424 connectAudioSource(source);
4425 }
Eric Laurente552edb2014-03-10 17:42:56 -07004426 }
4427
4428 // Move effects associated to this strategy from previous output to new output
4429 if (strategy == STRATEGY_MEDIA) {
Eric Laurent36829f92017-04-07 19:04:42 -07004430 selectOutputForMusicEffects();
Eric Laurente552edb2014-03-10 17:42:56 -07004431 }
4432 // Move tracks associated to this strategy from previous output to new output
Eric Laurent794fde22016-03-11 09:50:45 -08004433 for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004434 if (getStrategy((audio_stream_type_t)i) == strategy) {
4435 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07004436 }
4437 }
4438 }
4439}
4440
Eric Laurente0720872014-03-11 09:30:41 -07004441void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07004442{
François Gaffie2110e042015-03-24 08:41:51 +01004443 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004444 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004445 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01004446 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004447 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004448 checkOutputForStrategy(STRATEGY_SONIFICATION);
4449 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004450 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07004451 checkOutputForStrategy(STRATEGY_MEDIA);
4452 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004453 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07004454}
4455
Eric Laurente0720872014-03-11 09:30:41 -07004456void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07004457{
François Gaffie53615e22015-03-19 09:24:12 +01004458 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07004459 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004460 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07004461 return;
4462 }
4463
Eric Laurent3a4311c2014-03-17 12:00:47 -07004464 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08004465 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
4466 ~AUDIO_DEVICE_BIT_IN) != 0) ||
4467 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurentf732e072016-08-03 19:30:28 -07004468
4469 // if suspended, restore A2DP output if:
4470 // ((SCO device is NOT connected) ||
4471 // ((forced usage communication is NOT SCO) && (forced usage for record is NOT SCO) &&
4472 // (phone state is NOT in call) && (phone state is NOT ringing)))
Eric Laurente552edb2014-03-10 17:42:56 -07004473 //
Eric Laurentf732e072016-08-03 19:30:28 -07004474 // if not suspended, suspend A2DP output if:
4475 // (SCO device is connected) &&
4476 // ((forced usage for communication is SCO) || (forced usage for record is SCO) ||
4477 // ((phone state is in call) || (phone state is ringing)))
Eric Laurente552edb2014-03-10 17:42:56 -07004478 //
4479 if (mA2dpSuspended) {
Eric Laurentf732e072016-08-03 19:30:28 -07004480 if (!isScoConnected ||
4481 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) !=
4482 AUDIO_POLICY_FORCE_BT_SCO) &&
4483 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) !=
4484 AUDIO_POLICY_FORCE_BT_SCO) &&
4485 (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
François Gaffie2110e042015-03-24 08:41:51 +01004486 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004487
4488 mpClientInterface->restoreOutput(a2dpOutput);
4489 mA2dpSuspended = false;
4490 }
4491 } else {
Eric Laurentf732e072016-08-03 19:30:28 -07004492 if (isScoConnected &&
4493 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ==
4494 AUDIO_POLICY_FORCE_BT_SCO) ||
4495 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) ==
4496 AUDIO_POLICY_FORCE_BT_SCO) ||
4497 (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
François Gaffie2110e042015-03-24 08:41:51 +01004498 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004499
4500 mpClientInterface->suspendOutput(a2dpOutput);
4501 mA2dpSuspended = true;
4502 }
4503 }
4504}
4505
Eric Laurentc75307b2015-03-17 15:29:32 -07004506audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
4507 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004508{
4509 audio_devices_t device = AUDIO_DEVICE_NONE;
4510
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004511 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004512 if (index >= 0) {
4513 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4514 if (patchDesc->mUid != mUidCached) {
4515 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004516 outputDesc->device(), outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004517 return outputDesc->device();
4518 }
4519 }
4520
Eric Laurente552edb2014-03-10 17:42:56 -07004521 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05004522 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004523 // use device for strategy enforced audible
4524 // 2: we are in call or the strategy phone is active on the output:
4525 // use device for strategy phone
Jean-Michel Trivi178683b2017-08-30 18:07:06 -07004526 // 3: the strategy sonification is active on the output:
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004527 // use device for strategy sonification
Jean-Michel Trivi178683b2017-08-30 18:07:06 -07004528 // 4: the strategy for enforced audible is active but not enforced on the output:
4529 // use the device for strategy enforced audible
Eric Laurent28d09f02016-03-08 10:43:05 -08004530 // 5: the strategy accessibility is active on the output:
4531 // use device for strategy accessibility
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004532 // 6: the strategy "respectful" sonification is active on the output:
4533 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08004534 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004535 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08004536 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004537 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08004538 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004539 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01004540 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004541 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07004542 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
4543 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01004544 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004545 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004546 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004547 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Jean-Michel Trivi178683b2017-08-30 18:07:06 -07004548 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
4549 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
Eric Laurent28d09f02016-03-08 10:43:05 -08004550 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
4551 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004552 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004553 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004554 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004555 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004556 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004557 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004558 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004559 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004560 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004561 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07004562 }
4563
Eric Laurent1c333e22014-05-20 10:48:17 -07004564 ALOGV("getNewOutputDevice() selected device %x", device);
4565 return device;
4566}
4567
Eric Laurentfb66dd92016-01-28 18:32:03 -08004568audio_devices_t AudioPolicyManager::getNewInputDevice(const sp<AudioInputDescriptor>& inputDesc)
Eric Laurent1c333e22014-05-20 10:48:17 -07004569{
Eric Laurentfb66dd92016-01-28 18:32:03 -08004570 audio_devices_t device = AUDIO_DEVICE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07004571
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004572 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004573 if (index >= 0) {
4574 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4575 if (patchDesc->mUid != mUidCached) {
4576 ALOGV("getNewInputDevice() device %08x forced by patch %d",
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004577 inputDesc->mDevice, inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004578 return inputDesc->mDevice;
4579 }
4580 }
4581
Eric Laurentfb66dd92016-01-28 18:32:03 -08004582 audio_source_t source = inputDesc->getHighestPrioritySource(true /*activeOnly*/);
4583 if (isInCall()) {
4584 device = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
4585 } else if (source != AUDIO_SOURCE_DEFAULT) {
4586 device = getDeviceAndMixForInputSource(source);
4587 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004588
Eric Laurente552edb2014-03-10 17:42:56 -07004589 return device;
4590}
4591
Eric Laurent794fde22016-03-11 09:50:45 -08004592bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1,
4593 audio_stream_type_t stream2) {
Jean-Michel Trivi99bb2f92016-11-23 15:52:07 -08004594 return (stream1 == stream2);
Eric Laurent28d09f02016-03-08 10:43:05 -08004595}
4596
Eric Laurente0720872014-03-11 09:30:41 -07004597uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004598 return (uint32_t)getStrategy(stream);
4599}
4600
Eric Laurente0720872014-03-11 09:30:41 -07004601audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004602 // By checking the range of stream before calling getStrategy, we avoid
4603 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
4604 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08004605 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004606 return AUDIO_DEVICE_NONE;
4607 }
Eric Laurent28d09f02016-03-08 10:43:05 -08004608 audio_devices_t devices = AUDIO_DEVICE_NONE;
Eric Laurent794fde22016-03-11 09:50:45 -08004609 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
4610 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004611 continue;
Eric Laurent6a94d692014-05-20 11:18:06 -07004612 }
Eric Laurent794fde22016-03-11 09:50:45 -08004613 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08004614 audio_devices_t curDevices =
Eric Laurent447a87b2016-07-07 17:32:10 -07004615 getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/);
Eric Laurent28d09f02016-03-08 10:43:05 -08004616 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs);
4617 for (size_t i = 0; i < outputs.size(); i++) {
4618 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurent794fde22016-03-11 09:50:45 -08004619 if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004620 curDevices |= outputDesc->device();
4621 }
4622 }
4623 devices |= curDevices;
Eric Laurente552edb2014-03-10 17:42:56 -07004624 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05004625
4626 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
4627 and doesn't really need to.*/
4628 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
4629 devices |= AUDIO_DEVICE_OUT_SPEAKER;
4630 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4631 }
Eric Laurente552edb2014-03-10 17:42:56 -07004632 return devices;
4633}
4634
François Gaffie2110e042015-03-24 08:41:51 +01004635routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
4636{
Eric Laurent223fd5c2014-11-11 13:43:36 -08004637 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01004638 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004639}
4640
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004641uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4642 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004643 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4644 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4645 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004646 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4647 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4648 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004649 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01004650 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004651}
4652
Eric Laurente0720872014-03-11 09:30:41 -07004653void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004654 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004655 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004656 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4657 updateDevicesAndOutputs();
4658 break;
4659 default:
4660 break;
4661 }
4662}
4663
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004664uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
Eric Laurent9459fb02015-08-12 18:36:32 -07004665
4666 // skip beacon mute management if a dedicated TTS output is available
4667 if (mTtsOutputAvailable) {
4668 return 0;
4669 }
4670
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004671 switch(event) {
4672 case STARTING_OUTPUT:
4673 mBeaconMuteRefCount++;
4674 break;
4675 case STOPPING_OUTPUT:
4676 if (mBeaconMuteRefCount > 0) {
4677 mBeaconMuteRefCount--;
4678 }
4679 break;
4680 case STARTING_BEACON:
4681 mBeaconPlayingRefCount++;
4682 break;
4683 case STOPPING_BEACON:
4684 if (mBeaconPlayingRefCount > 0) {
4685 mBeaconPlayingRefCount--;
4686 }
4687 break;
4688 }
4689
4690 if (mBeaconMuteRefCount > 0) {
4691 // any playback causes beacon to be muted
4692 return setBeaconMute(true);
4693 } else {
4694 // no other playback: unmute when beacon starts playing, mute when it stops
4695 return setBeaconMute(mBeaconPlayingRefCount == 0);
4696 }
4697}
4698
4699uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4700 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4701 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4702 // keep track of muted state to avoid repeating mute/unmute operations
4703 if (mBeaconMuted != mute) {
4704 // mute/unmute AUDIO_STREAM_TTS on all outputs
4705 ALOGV("\t muting %d", mute);
4706 uint32_t maxLatency = 0;
4707 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004708 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004709 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004710 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004711 0 /*delay*/, AUDIO_DEVICE_NONE);
4712 const uint32_t latency = desc->latency() * 2;
4713 if (latency > maxLatency) {
4714 maxLatency = latency;
4715 }
4716 }
4717 mBeaconMuted = mute;
4718 return maxLatency;
4719 }
4720 return 0;
4721}
4722
Eric Laurente0720872014-03-11 09:30:41 -07004723audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004724 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004725{
Paul McLeanaa981192015-03-21 09:55:15 -07004726 // Routing
4727 // see if we have an explicit route
4728 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4729 // (getStrategy(stream)).
4730 // if the strategy from the stream type in the RouteMap is the same as the argument above,
Eric Laurentad2e7b92017-09-14 20:06:42 -07004731 // and activity count is non-zero and the device in the route descriptor is available
4732 // then select this device.
Paul McLeanaa981192015-03-21 09:55:15 -07004733 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4734 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
Eric Laurent28d09f02016-03-08 10:43:05 -08004735 routing_strategy routeStrategy = getStrategy(route->mStreamType);
Eric Laurentad2e7b92017-09-14 20:06:42 -07004736 if ((routeStrategy == strategy) && route->isActive() &&
4737 (mAvailableOutputDevices.indexOf(route->mDeviceDescriptor) >= 0)) {
Paul McLeanaa981192015-03-21 09:55:15 -07004738 return route->mDeviceDescriptor->type();
4739 }
4740 }
4741
Eric Laurente552edb2014-03-10 17:42:56 -07004742 if (fromCache) {
4743 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4744 strategy, mDeviceForStrategy[strategy]);
4745 return mDeviceForStrategy[strategy];
4746 }
François Gaffie2110e042015-03-24 08:41:51 +01004747 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004748}
4749
Eric Laurente0720872014-03-11 09:30:41 -07004750void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004751{
4752 for (int i = 0; i < NUM_STRATEGIES; i++) {
4753 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4754 }
4755 mPreviousOutputs = mOutputs;
4756}
4757
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004758uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004759 audio_devices_t prevDevice,
4760 uint32_t delayMs)
4761{
4762 // mute/unmute strategies using an incompatible device combination
4763 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4764 // if unmuting, unmute only after the specified delay
4765 if (outputDesc->isDuplicated()) {
4766 return 0;
4767 }
4768
4769 uint32_t muteWaitMs = 0;
4770 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004771 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004772
4773 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4774 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004775 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004776 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4777 bool doMute = false;
4778
4779 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4780 doMute = true;
4781 outputDesc->mStrategyMutedByDevice[i] = true;
4782 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4783 doMute = true;
4784 outputDesc->mStrategyMutedByDevice[i] = false;
4785 }
Eric Laurent99401132014-05-07 19:48:15 -07004786 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004787 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004788 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004789 // skip output if it does not share any device with current output
4790 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4791 == AUDIO_DEVICE_NONE) {
4792 continue;
4793 }
Eric Laurent37ddbb42016-08-10 16:19:14 -07004794 ALOGVV("checkDeviceMuteStrategies() %s strategy %zu (curDevice %04x)",
Eric Laurentc75307b2015-03-17 15:29:32 -07004795 mute ? "muting" : "unmuting", i, curDevice);
4796 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004797 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004798 if (mute) {
4799 // FIXME: should not need to double latency if volume could be applied
4800 // immediately by the audioflinger mixer. We must account for the delay
4801 // between now and the next time the audioflinger thread for this output
4802 // will process a buffer (which corresponds to one buffer size,
4803 // usually 1/2 or 1/4 of the latency).
4804 if (muteWaitMs < desc->latency() * 2) {
4805 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004806 }
4807 }
4808 }
4809 }
4810 }
4811 }
4812
Eric Laurent99401132014-05-07 19:48:15 -07004813 // temporary mute output if device selection changes to avoid volume bursts due to
4814 // different per device volumes
4815 if (outputDesc->isActive() && (device != prevDevice)) {
Eric Laurentdc462862016-07-19 12:29:53 -07004816 uint32_t tempMuteWaitMs = outputDesc->latency() * 2;
4817 // temporary mute duration is conservatively set to 4 times the reported latency
4818 uint32_t tempMuteDurationMs = outputDesc->latency() * 4;
4819 if (muteWaitMs < tempMuteWaitMs) {
4820 muteWaitMs = tempMuteWaitMs;
Eric Laurent99401132014-05-07 19:48:15 -07004821 }
Eric Laurentdc462862016-07-19 12:29:53 -07004822
Eric Laurent99401132014-05-07 19:48:15 -07004823 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004824 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentdc462862016-07-19 12:29:53 -07004825 // make sure that we do not start the temporary mute period too early in case of
4826 // delayed device change
4827 setStrategyMute((routing_strategy)i, true, outputDesc, delayMs);
Eric Laurentc75307b2015-03-17 15:29:32 -07004828 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurentdc462862016-07-19 12:29:53 -07004829 delayMs + tempMuteDurationMs, device);
Eric Laurent99401132014-05-07 19:48:15 -07004830 }
4831 }
4832 }
4833
Eric Laurente552edb2014-03-10 17:42:56 -07004834 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4835 if (muteWaitMs > delayMs) {
4836 muteWaitMs -= delayMs;
4837 usleep(muteWaitMs * 1000);
4838 return muteWaitMs;
4839 }
4840 return 0;
4841}
4842
Eric Laurentc75307b2015-03-17 15:29:32 -07004843uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004844 audio_devices_t device,
4845 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004846 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004847 audio_patch_handle_t *patchHandle,
Andy Hung7c7124e2017-10-20 12:03:34 -07004848 const char *address,
4849 bool requiresMuteCheck)
Eric Laurente552edb2014-03-10 17:42:56 -07004850{
Eric Laurentc75307b2015-03-17 15:29:32 -07004851 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004852 AudioParameter param;
4853 uint32_t muteWaitMs;
4854
4855 if (outputDesc->isDuplicated()) {
Andy Hung7c7124e2017-10-20 12:03:34 -07004856 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs,
4857 nullptr /* patchHandle */, nullptr /* address */, requiresMuteCheck);
4858 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs,
4859 nullptr /* patchHandle */, nullptr /* address */, requiresMuteCheck);
Eric Laurente552edb2014-03-10 17:42:56 -07004860 return muteWaitMs;
4861 }
4862 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4863 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004864 if ((device != AUDIO_DEVICE_NONE) &&
Andy Hung7c7124e2017-10-20 12:03:34 -07004865 ((device & outputDesc->supportedDevices()) == AUDIO_DEVICE_NONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004866 return 0;
4867 }
4868
4869 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004870 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004871
4872 audio_devices_t prevDevice = outputDesc->mDevice;
4873
Paul McLeanaa981192015-03-21 09:55:15 -07004874 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004875
4876 if (device != AUDIO_DEVICE_NONE) {
4877 outputDesc->mDevice = device;
4878 }
Andy Hung7c7124e2017-10-20 12:03:34 -07004879
4880 // if the outputs are not materially active, there is no need to mute.
4881 if (requiresMuteCheck) {
4882 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4883 } else {
4884 ALOGV("%s: suppressing checkDeviceMuteStrategies", __func__);
4885 muteWaitMs = 0;
4886 }
Eric Laurente552edb2014-03-10 17:42:56 -07004887
4888 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004889 // the requested device is AUDIO_DEVICE_NONE
4890 // OR the requested device is the same as current device
4891 // AND force is not specified
4892 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004893 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004894 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4895 !force &&
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004896 outputDesc->getPatchHandle() != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004897 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004898 return muteWaitMs;
4899 }
4900
4901 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004902
Eric Laurente552edb2014-03-10 17:42:56 -07004903 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004904 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004905 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004906 } else {
Eric Laurentc40d9692016-04-13 19:14:13 -07004907 DeviceVector deviceList;
4908 if ((address == NULL) || (strlen(address) == 0)) {
4909 deviceList = mAvailableOutputDevices.getDevicesFromType(device);
4910 } else {
4911 deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
4912 }
4913
Eric Laurent1c333e22014-05-20 10:48:17 -07004914 if (!deviceList.isEmpty()) {
4915 struct audio_patch patch;
4916 outputDesc->toAudioPortConfig(&patch.sources[0]);
4917 patch.num_sources = 1;
4918 patch.num_sinks = 0;
4919 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4920 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004921 patch.num_sinks++;
4922 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004923 ssize_t index;
4924 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4925 index = mAudioPatches.indexOfKey(*patchHandle);
4926 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004927 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004928 }
4929 sp< AudioPatch> patchDesc;
4930 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4931 if (index >= 0) {
4932 patchDesc = mAudioPatches.valueAt(index);
4933 afPatchHandle = patchDesc->mAfPatchHandle;
4934 }
4935
Eric Laurent1c333e22014-05-20 10:48:17 -07004936 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004937 &afPatchHandle,
4938 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004939 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4940 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004941 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004942 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004943 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004944 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004945 addAudioPatch(patchDesc->mHandle, patchDesc);
4946 } else {
4947 patchDesc->mPatch = patch;
4948 }
4949 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004950 if (patchHandle) {
4951 *patchHandle = patchDesc->mHandle;
4952 }
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004953 outputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004954 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004955 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004956 }
4957 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004958
4959 // inform all input as well
4960 for (size_t i = 0; i < mInputs.size(); i++) {
4961 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004962 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004963 AudioParameter inputCmd = AudioParameter();
4964 ALOGV("%s: inform input %d of device:%d", __func__,
4965 inputDescriptor->mIoHandle, device);
4966 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4967 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4968 inputCmd.toString(),
4969 delayMs);
4970 }
4971 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004972 }
Eric Laurente552edb2014-03-10 17:42:56 -07004973
4974 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004975 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004976
4977 return muteWaitMs;
4978}
4979
Eric Laurentc75307b2015-03-17 15:29:32 -07004980status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004981 int delayMs,
4982 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004983{
Eric Laurent6a94d692014-05-20 11:18:06 -07004984 ssize_t index;
4985 if (patchHandle) {
4986 index = mAudioPatches.indexOfKey(*patchHandle);
4987 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004988 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004989 }
4990 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004991 return INVALID_OPERATION;
4992 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004993 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4994 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004995 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07004996 outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07004997 removeAudioPatch(patchDesc->mHandle);
4998 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004999 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07005000 return status;
5001}
5002
5003status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
5004 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07005005 bool force,
5006 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07005007{
5008 status_t status = NO_ERROR;
5009
Eric Laurent1f2f2232014-06-02 12:01:23 -07005010 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07005011 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
5012 inputDesc->mDevice = device;
5013
5014 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
5015 if (!deviceList.isEmpty()) {
5016 struct audio_patch patch;
5017 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07005018 // AUDIO_SOURCE_HOTWORD is for internal use only:
5019 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07005020 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
Eric Laurent599c7582015-12-07 18:05:55 -08005021 !inputDesc->isSoundTrigger()) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07005022 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
5023 }
Eric Laurent1c333e22014-05-20 10:48:17 -07005024 patch.num_sinks = 1;
5025 //only one input device for now
5026 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07005027 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07005028 ssize_t index;
5029 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
5030 index = mAudioPatches.indexOfKey(*patchHandle);
5031 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08005032 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07005033 }
5034 sp< AudioPatch> patchDesc;
5035 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
5036 if (index >= 0) {
5037 patchDesc = mAudioPatches.valueAt(index);
5038 afPatchHandle = patchDesc->mAfPatchHandle;
5039 }
5040
Eric Laurent1c333e22014-05-20 10:48:17 -07005041 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07005042 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07005043 0);
5044 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07005045 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07005046 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07005047 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01005048 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07005049 addAudioPatch(patchDesc->mHandle, patchDesc);
5050 } else {
5051 patchDesc->mPatch = patch;
5052 }
5053 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07005054 if (patchHandle) {
5055 *patchHandle = patchDesc->mHandle;
5056 }
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08005057 inputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07005058 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07005059 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07005060 }
5061 }
5062 }
5063 return status;
5064}
5065
Eric Laurent6a94d692014-05-20 11:18:06 -07005066status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
5067 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07005068{
Eric Laurent1f2f2232014-06-02 12:01:23 -07005069 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07005070 ssize_t index;
5071 if (patchHandle) {
5072 index = mAudioPatches.indexOfKey(*patchHandle);
5073 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08005074 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07005075 }
5076 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07005077 return INVALID_OPERATION;
5078 }
Eric Laurent6a94d692014-05-20 11:18:06 -07005079 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
5080 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07005081 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07005082 inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07005083 removeAudioPatch(patchDesc->mHandle);
5084 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07005085 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07005086 return status;
5087}
5088
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08005089sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07005090 const String8& address,
François Gaffie53615e22015-03-19 09:24:12 +01005091 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07005092 audio_format_t& format,
5093 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01005094 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07005095{
5096 // Choose an input profile based on the requested capture parameters: select the first available
5097 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07005098 //
5099 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
5100 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07005101
5102 for (size_t i = 0; i < mHwModules.size(); i++)
5103 {
5104 if (mHwModules[i]->mHandle == 0) {
5105 continue;
5106 }
5107 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
5108 {
Eric Laurent1c333e22014-05-20 10:48:17 -07005109 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07005110 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08005111 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07005112 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07005113 format,
5114 &format /*updatedFormat*/,
5115 channelMask,
5116 &channelMask /*updatedChannelMask*/,
5117 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08005118
Eric Laurente552edb2014-03-10 17:42:56 -07005119 return profile;
5120 }
5121 }
5122 }
5123 return NULL;
5124}
5125
Eric Laurentc73ca6e2014-12-12 14:34:22 -08005126
5127audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01005128 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07005129{
François Gaffie036e1e92015-03-19 10:16:24 +01005130 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
5131 audio_devices_t selectedDeviceFromMix =
5132 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08005133
François Gaffie036e1e92015-03-19 10:16:24 +01005134 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
5135 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08005136 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08005137 return getDeviceForInputSource(inputSource);
5138}
5139
5140audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
5141{
Eric Laurentad2e7b92017-09-14 20:06:42 -07005142 // Routing
5143 // Scan the whole RouteMap to see if we have an explicit route:
5144 // if the input source in the RouteMap is the same as the argument above,
5145 // and activity count is non-zero and the device in the route descriptor is available
5146 // then select this device.
Paul McLean466dc8e2015-04-17 13:15:36 -06005147 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
5148 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurentad2e7b92017-09-14 20:06:42 -07005149 if ((inputSource == route->mSource) && route->isActive() &&
5150 (mAvailableInputDevices.indexOf(route->mDeviceDescriptor) >= 0)) {
Paul McLean466dc8e2015-04-17 13:15:36 -06005151 return route->mDeviceDescriptor->type();
5152 }
5153 }
5154
5155 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07005156}
5157
Eric Laurente0720872014-03-11 09:30:41 -07005158float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005159 int index,
5160 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005161{
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005162 float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index);
Jean-Michel Trivi3d8b4a42016-09-14 18:37:46 -07005163
5164 // handle the case of accessibility active while a ringtone is playing: if the ringtone is much
5165 // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch
5166 // exploration of the dialer UI. In this situation, bring the accessibility volume closer to
5167 // the ringtone volume
5168 if ((stream == AUDIO_STREAM_ACCESSIBILITY)
5169 && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState())
5170 && isStreamActive(AUDIO_STREAM_RING, 0)) {
5171 const float ringVolumeDB = computeVolume(AUDIO_STREAM_RING, index, device);
5172 return ringVolumeDB - 4 > volumeDB ? ringVolumeDB - 4 : volumeDB;
5173 }
5174
Jean-Michel Trivi719a9872017-08-05 13:51:35 -07005175 // in-call: always cap earpiece volume by voice volume + some low headroom
5176 if ((stream != AUDIO_STREAM_VOICE_CALL) && (device & AUDIO_DEVICE_OUT_EARPIECE) && isInCall()) {
5177 switch (stream) {
5178 case AUDIO_STREAM_SYSTEM:
5179 case AUDIO_STREAM_RING:
5180 case AUDIO_STREAM_MUSIC:
5181 case AUDIO_STREAM_ALARM:
5182 case AUDIO_STREAM_NOTIFICATION:
5183 case AUDIO_STREAM_ENFORCED_AUDIBLE:
5184 case AUDIO_STREAM_DTMF:
5185 case AUDIO_STREAM_ACCESSIBILITY: {
5186 const float maxVoiceVolDb = computeVolume(AUDIO_STREAM_VOICE_CALL, index, device)
5187 + IN_CALL_EARPIECE_HEADROOM_DB;
5188 if (volumeDB > maxVoiceVolDb) {
5189 ALOGV("computeVolume() stream %d at vol=%f overriden by stream %d at vol=%f",
5190 stream, volumeDB, AUDIO_STREAM_VOICE_CALL, maxVoiceVolDb);
5191 volumeDB = maxVoiceVolDb;
5192 }
5193 } break;
5194 default:
5195 break;
5196 }
5197 }
5198
Eric Laurente552edb2014-03-10 17:42:56 -07005199 // if a headset is connected, apply the following rules to ring tones and notifications
5200 // to avoid sound level bursts in user's ears:
Eric Laurent6af1c1d2016-04-14 11:20:44 -07005201 // - always attenuate notifications volume by 6dB
5202 // - attenuate ring tones volume by 6dB unless music is not playing and
5203 // speaker is part of the select devices
Eric Laurente552edb2014-03-10 17:42:56 -07005204 // - if music is playing, always limit the volume to current music volume,
5205 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07005206 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005207 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
5208 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
5209 AUDIO_DEVICE_OUT_WIRED_HEADSET |
Eric Laurent904d6322017-03-17 17:20:47 -07005210 AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
5211 AUDIO_DEVICE_OUT_USB_HEADSET)) &&
Eric Laurente552edb2014-03-10 17:42:56 -07005212 ((stream_strategy == STRATEGY_SONIFICATION)
5213 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07005214 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07005215 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01005216 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
François Gaffied1ab2bd2015-12-02 18:20:06 +01005217 mVolumeCurves->canBeMuted(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005218 // when the phone is ringing we must consider that music could have been paused just before
5219 // by the music application and behave as if music was active if the last music track was
5220 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07005221 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07005222 mLimitRingtoneVolume) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005223 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07005224 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07005225 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005226 mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC,
5227 musicDevice),
5228 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07005229 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
5230 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005231 if (volumeDB > minVolDB) {
5232 volumeDB = minVolDB;
Eric Laurentffbc80f2015-03-18 18:30:19 -07005233 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07005234 }
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005235 if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
5236 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) {
5237 // on A2DP, also ensure notification volume is not too low compared to media when
5238 // intended to be played
5239 if ((volumeDB > -96.0f) &&
5240 (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) {
5241 ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f",
5242 stream, device,
5243 volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB);
5244 volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB;
5245 }
5246 }
Eric Laurent6af1c1d2016-04-14 11:20:44 -07005247 } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) ||
5248 stream_strategy != STRATEGY_SONIFICATION) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005249 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07005250 }
5251 }
5252
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005253 return volumeDB;
Eric Laurente552edb2014-03-10 17:42:56 -07005254}
5255
Eric Laurente0720872014-03-11 09:30:41 -07005256status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005257 int index,
5258 const sp<AudioOutputDescriptor>& outputDesc,
5259 audio_devices_t device,
5260 int delayMs,
5261 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005262{
Eric Laurente552edb2014-03-10 17:42:56 -07005263 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07005264 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07005265 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07005266 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07005267 return NO_ERROR;
5268 }
François Gaffie2110e042015-03-24 08:41:51 +01005269 audio_policy_forced_cfg_t forceUseForComm =
5270 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07005271 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01005272 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
5273 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005274 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01005275 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07005276 return INVALID_OPERATION;
5277 }
5278
Eric Laurentc75307b2015-03-17 15:29:32 -07005279 if (device == AUDIO_DEVICE_NONE) {
5280 device = outputDesc->device();
5281 }
Eric Laurent275e8e92014-11-30 15:14:47 -08005282
Eric Laurentffbc80f2015-03-18 18:30:19 -07005283 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07005284 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07005285 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08005286 }
Eric Laurentc75307b2015-03-17 15:29:32 -07005287
Eric Laurentffbc80f2015-03-18 18:30:19 -07005288 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07005289
Eric Laurent3b73df72014-03-11 09:06:29 -07005290 if (stream == AUDIO_STREAM_VOICE_CALL ||
5291 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07005292 float voiceVolume;
5293 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07005294 if (stream == AUDIO_STREAM_VOICE_CALL) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005295 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005296 } else {
5297 voiceVolume = 1.0;
5298 }
5299
Eric Laurent18fba842016-03-31 14:41:26 -07005300 if (voiceVolume != mLastVoiceVolume) {
Eric Laurente552edb2014-03-10 17:42:56 -07005301 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
5302 mLastVoiceVolume = voiceVolume;
5303 }
5304 }
5305
5306 return NO_ERROR;
5307}
5308
Eric Laurentc75307b2015-03-17 15:29:32 -07005309void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
5310 audio_devices_t device,
5311 int delayMs,
5312 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005313{
Eric Laurentc75307b2015-03-17 15:29:32 -07005314 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07005315
Eric Laurent794fde22016-03-11 09:50:45 -08005316 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005317 checkAndSetVolume((audio_stream_type_t)stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005318 mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005319 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005320 device,
5321 delayMs,
5322 force);
5323 }
5324}
5325
Eric Laurente0720872014-03-11 09:30:41 -07005326void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07005327 bool on,
5328 const sp<AudioOutputDescriptor>& outputDesc,
5329 int delayMs,
5330 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005331{
Eric Laurent72249d52015-06-08 11:12:15 -07005332 ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d",
5333 strategy, on, outputDesc->getId());
Eric Laurent794fde22016-03-11 09:50:45 -08005334 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005335 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005336 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07005337 }
5338 }
5339}
5340
Eric Laurente0720872014-03-11 09:30:41 -07005341void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005342 bool on,
5343 const sp<AudioOutputDescriptor>& outputDesc,
5344 int delayMs,
5345 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005346{
Eric Laurente552edb2014-03-10 17:42:56 -07005347 if (device == AUDIO_DEVICE_NONE) {
5348 device = outputDesc->device();
5349 }
5350
Eric Laurentc75307b2015-03-17 15:29:32 -07005351 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
5352 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07005353
5354 if (on) {
5355 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005356 if (mVolumeCurves->canBeMuted(stream) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07005357 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01005358 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005359 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005360 }
5361 }
5362 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
5363 outputDesc->mMuteCount[stream]++;
5364 } else {
5365 if (outputDesc->mMuteCount[stream] == 0) {
5366 ALOGV("setStreamMute() unmuting non muted stream!");
5367 return;
5368 }
5369 if (--outputDesc->mMuteCount[stream] == 0) {
5370 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005371 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005372 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005373 device,
5374 delayMs);
5375 }
5376 }
5377}
5378
Eric Laurente0720872014-03-11 09:30:41 -07005379void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07005380 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07005381{
Eric Laurent87ffa392015-05-22 10:32:38 -07005382 if(!hasPrimaryOutput()) {
5383 return;
5384 }
5385
Eric Laurente552edb2014-03-10 17:42:56 -07005386 // if the stream pertains to sonification strategy and we are in call we must
5387 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
5388 // in the device used for phone strategy and play the tone if the selected device does not
5389 // interfere with the device used for phone strategy
5390 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
5391 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07005392 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005393 if ((stream_strategy == STRATEGY_SONIFICATION) ||
5394 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005395 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07005396 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
5397 stream, starting, outputDesc->mDevice, stateChange);
5398 if (outputDesc->mRefCount[stream]) {
5399 int muteCount = 1;
5400 if (stateChange) {
5401 muteCount = outputDesc->mRefCount[stream];
5402 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005403 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005404 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
5405 for (int i = 0; i < muteCount; i++) {
5406 setStreamMute(stream, starting, mPrimaryOutput);
5407 }
5408 } else {
5409 ALOGV("handleIncallSonification() high visibility");
5410 if (outputDesc->device() &
5411 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
5412 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
5413 for (int i = 0; i < muteCount; i++) {
5414 setStreamMute(stream, starting, mPrimaryOutput);
5415 }
5416 }
5417 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005418 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
5419 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07005420 } else {
5421 mpClientInterface->stopTone();
5422 }
5423 }
5424 }
5425 }
5426}
5427
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005428audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
5429{
5430 // flags to stream type mapping
5431 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
5432 return AUDIO_STREAM_ENFORCED_AUDIBLE;
5433 }
5434 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
5435 return AUDIO_STREAM_BLUETOOTH_SCO;
5436 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08005437 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
5438 return AUDIO_STREAM_TTS;
5439 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005440
5441 // usage to stream type mapping
5442 switch (attr->usage) {
5443 case AUDIO_USAGE_MEDIA:
5444 case AUDIO_USAGE_GAME:
Jean-Michel Trivi36867762016-12-29 12:03:28 -08005445 case AUDIO_USAGE_ASSISTANT:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005446 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5447 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08005448 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5449 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005450 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5451 return AUDIO_STREAM_SYSTEM;
5452 case AUDIO_USAGE_VOICE_COMMUNICATION:
5453 return AUDIO_STREAM_VOICE_CALL;
5454
5455 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5456 return AUDIO_STREAM_DTMF;
5457
5458 case AUDIO_USAGE_ALARM:
5459 return AUDIO_STREAM_ALARM;
5460 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5461 return AUDIO_STREAM_RING;
5462
5463 case AUDIO_USAGE_NOTIFICATION:
5464 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5465 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5466 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5467 case AUDIO_USAGE_NOTIFICATION_EVENT:
5468 return AUDIO_STREAM_NOTIFICATION;
5469
5470 case AUDIO_USAGE_UNKNOWN:
5471 default:
5472 return AUDIO_STREAM_MUSIC;
5473 }
5474}
Eric Laurente83b55d2014-11-14 10:06:21 -08005475
François Gaffie53615e22015-03-19 09:24:12 +01005476bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
5477{
Eric Laurente83b55d2014-11-14 10:06:21 -08005478 // has flags that map to a strategy?
5479 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
5480 return true;
5481 }
5482
5483 // has known usage?
5484 switch (paa->usage) {
5485 case AUDIO_USAGE_UNKNOWN:
5486 case AUDIO_USAGE_MEDIA:
5487 case AUDIO_USAGE_VOICE_COMMUNICATION:
5488 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5489 case AUDIO_USAGE_ALARM:
5490 case AUDIO_USAGE_NOTIFICATION:
5491 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5492 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5493 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5494 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5495 case AUDIO_USAGE_NOTIFICATION_EVENT:
5496 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5497 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5498 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5499 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08005500 case AUDIO_USAGE_VIRTUAL_SOURCE:
Jean-Michel Trivi36867762016-12-29 12:03:28 -08005501 case AUDIO_USAGE_ASSISTANT:
Eric Laurente83b55d2014-11-14 10:06:21 -08005502 break;
5503 default:
5504 return false;
5505 }
5506 return true;
5507}
5508
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07005509bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor>& outputDesc,
François Gaffiead3183e2015-03-18 16:55:35 +01005510 routing_strategy strategy, uint32_t inPastMs,
5511 nsecs_t sysTime) const
5512{
5513 if ((sysTime == 0) && (inPastMs != 0)) {
5514 sysTime = systemTime();
5515 }
Eric Laurent794fde22016-03-11 09:50:45 -08005516 for (int i = 0; i < (int)AUDIO_STREAM_FOR_POLICY_CNT; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01005517 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
5518 (NUM_STRATEGIES == strategy)) &&
5519 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
5520 return true;
5521 }
5522 }
5523 return false;
5524}
5525
François Gaffie2110e042015-03-24 08:41:51 +01005526audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
5527{
5528 return mEngine->getForceUse(usage);
5529}
5530
5531bool AudioPolicyManager::isInCall()
5532{
5533 return isStateInCall(mEngine->getPhoneState());
5534}
5535
5536bool AudioPolicyManager::isStateInCall(int state)
5537{
5538 return is_state_in_call(state);
5539}
5540
Eric Laurentd60560a2015-04-10 11:31:20 -07005541void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc)
5542{
5543 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
5544 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
5545 if (sourceDesc->mDevice->equals(deviceDesc)) {
5546 ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle());
5547 stopAudioSource(sourceDesc->getHandle());
5548 }
5549 }
5550
5551 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
5552 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
5553 bool release = false;
5554 for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) {
5555 const struct audio_port_config *source = &patchDesc->mPatch.sources[j];
5556 if (source->type == AUDIO_PORT_TYPE_DEVICE &&
5557 source->ext.device.type == deviceDesc->type()) {
5558 release = true;
5559 }
5560 }
5561 for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) {
5562 const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j];
5563 if (sink->type == AUDIO_PORT_TYPE_DEVICE &&
5564 sink->ext.device.type == deviceDesc->type()) {
5565 release = true;
5566 }
5567 }
5568 if (release) {
5569 ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle);
5570 releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid);
5571 }
5572 }
5573}
5574
Phil Burk09bc4612016-02-24 15:58:15 -08005575// Modify the list of surround sound formats supported.
Phil Burk0709b0a2016-03-31 12:54:57 -07005576void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) {
5577 FormatVector &formats = *formatsPtr;
Phil Burk07ac1142016-03-25 13:39:29 -07005578 // TODO Set this based on Config properties.
5579 const bool alwaysForceAC3 = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005580
5581 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5582 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
Phil Burk0709b0a2016-03-31 12:54:57 -07005583 ALOGD("%s: forced use = %d", __FUNCTION__, forceUse);
Phil Burk09bc4612016-02-24 15:58:15 -08005584
5585 // Analyze original support for various formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005586 bool supportsAC3 = false;
5587 bool supportsOtherSurround = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005588 bool supportsIEC61937 = false;
5589 for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) {
5590 audio_format_t format = formats[formatIndex];
Phil Burk09bc4612016-02-24 15:58:15 -08005591 switch (format) {
5592 case AUDIO_FORMAT_AC3:
Phil Burk07ac1142016-03-25 13:39:29 -07005593 supportsAC3 = true;
5594 break;
Phil Burk09bc4612016-02-24 15:58:15 -08005595 case AUDIO_FORMAT_E_AC3:
5596 case AUDIO_FORMAT_DTS:
5597 case AUDIO_FORMAT_DTS_HD:
Phil Burk07ac1142016-03-25 13:39:29 -07005598 supportsOtherSurround = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005599 break;
5600 case AUDIO_FORMAT_IEC61937:
5601 supportsIEC61937 = true;
5602 break;
5603 default:
5604 break;
5605 }
5606 }
Phil Burk09bc4612016-02-24 15:58:15 -08005607
5608 // Modify formats based on surround preferences.
5609 // If NEVER, remove support for surround formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005610 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5611 if (supportsAC3 || supportsOtherSurround || supportsIEC61937) {
5612 // Remove surround sound related formats.
5613 for (size_t formatIndex = 0; formatIndex < formats.size(); ) {
5614 audio_format_t format = formats[formatIndex];
5615 switch(format) {
5616 case AUDIO_FORMAT_AC3:
5617 case AUDIO_FORMAT_E_AC3:
5618 case AUDIO_FORMAT_DTS:
5619 case AUDIO_FORMAT_DTS_HD:
5620 case AUDIO_FORMAT_IEC61937:
Phil Burk07ac1142016-03-25 13:39:29 -07005621 formats.removeAt(formatIndex);
5622 break;
5623 default:
5624 formatIndex++; // keep it
5625 break;
5626 }
Phil Burk09bc4612016-02-24 15:58:15 -08005627 }
Phil Burk07ac1142016-03-25 13:39:29 -07005628 supportsAC3 = false;
5629 supportsOtherSurround = false;
5630 supportsIEC61937 = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005631 }
Phil Burk07ac1142016-03-25 13:39:29 -07005632 } else { // AUTO or ALWAYS
5633 // Most TVs support AC3 even if they do not report it in the EDID.
5634 if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS))
5635 && !supportsAC3) {
5636 formats.add(AUDIO_FORMAT_AC3);
5637 supportsAC3 = true;
5638 }
5639
5640 // If ALWAYS, add support for raw surround formats if all are missing.
5641 // This assumes that if any of these formats are reported by the HAL
5642 // then the report is valid and should not be modified.
5643 if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)
5644 && !supportsOtherSurround) {
5645 formats.add(AUDIO_FORMAT_E_AC3);
5646 formats.add(AUDIO_FORMAT_DTS);
5647 formats.add(AUDIO_FORMAT_DTS_HD);
5648 supportsOtherSurround = true;
5649 }
5650
5651 // Add support for IEC61937 if any raw surround supported.
5652 // The HAL could do this but add it here, just in case.
5653 if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) {
5654 formats.add(AUDIO_FORMAT_IEC61937);
5655 supportsIEC61937 = true;
5656 }
Phil Burk09bc4612016-02-24 15:58:15 -08005657 }
Phil Burk0709b0a2016-03-31 12:54:57 -07005658}
5659
5660// Modify the list of channel masks supported.
5661void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) {
5662 ChannelsVector &channelMasks = *channelMasksPtr;
5663 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5664 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
5665
5666 // If NEVER, then remove support for channelMasks > stereo.
5667 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5668 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) {
5669 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5670 if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) {
5671 ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask);
5672 channelMasks.removeAt(maskIndex);
5673 } else {
5674 maskIndex++;
5675 }
5676 }
5677 // If ALWAYS, then make sure we at least support 5.1
5678 } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) {
5679 bool supports5dot1 = false;
5680 // Are there any channel masks that can be considered "surround"?
5681 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); maskIndex++) {
5682 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5683 if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) {
5684 supports5dot1 = true;
5685 break;
5686 }
5687 }
5688 // If not then add 5.1 support.
5689 if (!supports5dot1) {
5690 channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1);
5691 ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__);
5692 }
Phil Burk09bc4612016-02-24 15:58:15 -08005693 }
5694}
5695
Phil Burk00eeb322016-03-31 12:41:00 -07005696void AudioPolicyManager::updateAudioProfiles(audio_devices_t device,
5697 audio_io_handle_t ioHandle,
François Gaffie112b0af2015-11-19 16:13:25 +01005698 AudioProfileVector &profiles)
5699{
5700 String8 reply;
Phil Burk0709b0a2016-03-31 12:54:57 -07005701
François Gaffie112b0af2015-11-19 16:13:25 +01005702 // Format MUST be checked first to update the list of AudioProfile
5703 if (profiles.hasDynamicFormat()) {
Mikhail Naganov388360c2016-10-17 17:09:41 -07005704 reply = mpClientInterface->getParameters(
5705 ioHandle, String8(AudioParameter::keyStreamSupportedFormats));
Phil Burk0709b0a2016-03-31 12:54:57 -07005706 ALOGV("%s: supported formats %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005707 AudioParameter repliedParameters(reply);
5708 if (repliedParameters.get(
Mikhail Naganov388360c2016-10-17 17:09:41 -07005709 String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) {
François Gaffie112b0af2015-11-19 16:13:25 +01005710 ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__);
5711 return;
5712 }
Phil Burk09bc4612016-02-24 15:58:15 -08005713 FormatVector formats = formatsFromString(reply.string());
Phil Burk00eeb322016-03-31 12:41:00 -07005714 if (device == AUDIO_DEVICE_OUT_HDMI) {
Phil Burk0709b0a2016-03-31 12:54:57 -07005715 filterSurroundFormats(&formats);
Phil Burk00eeb322016-03-31 12:41:00 -07005716 }
Phil Burk09bc4612016-02-24 15:58:15 -08005717 profiles.setFormats(formats);
François Gaffie112b0af2015-11-19 16:13:25 +01005718 }
5719 const FormatVector &supportedFormats = profiles.getSupportedFormats();
5720
Eric Laurent20eb3a42016-01-26 18:39:17 -08005721 for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) {
François Gaffie112b0af2015-11-19 16:13:25 +01005722 audio_format_t format = supportedFormats[formatIndex];
Eric Laurent20eb3a42016-01-26 18:39:17 -08005723 ChannelsVector channelMasks;
5724 SampleRateVector samplingRates;
François Gaffie112b0af2015-11-19 16:13:25 +01005725 AudioParameter requestedParameters;
Mikhail Naganov388360c2016-10-17 17:09:41 -07005726 requestedParameters.addInt(String8(AudioParameter::keyFormat), format);
François Gaffie112b0af2015-11-19 16:13:25 +01005727
5728 if (profiles.hasDynamicRateFor(format)) {
Mikhail Naganov388360c2016-10-17 17:09:41 -07005729 reply = mpClientInterface->getParameters(
5730 ioHandle,
5731 requestedParameters.toString() + ";" +
5732 AudioParameter::keyStreamSupportedSamplingRates);
François Gaffie112b0af2015-11-19 16:13:25 +01005733 ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005734 AudioParameter repliedParameters(reply);
5735 if (repliedParameters.get(
Mikhail Naganov388360c2016-10-17 17:09:41 -07005736 String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) {
Eric Laurent62e4bc52016-02-02 18:37:28 -08005737 samplingRates = samplingRatesFromString(reply.string());
François Gaffie112b0af2015-11-19 16:13:25 +01005738 }
5739 }
5740 if (profiles.hasDynamicChannelsFor(format)) {
5741 reply = mpClientInterface->getParameters(ioHandle,
5742 requestedParameters.toString() + ";" +
Mikhail Naganov388360c2016-10-17 17:09:41 -07005743 AudioParameter::keyStreamSupportedChannels);
François Gaffie112b0af2015-11-19 16:13:25 +01005744 ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005745 AudioParameter repliedParameters(reply);
5746 if (repliedParameters.get(
Mikhail Naganov388360c2016-10-17 17:09:41 -07005747 String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) {
Eric Laurent62e4bc52016-02-02 18:37:28 -08005748 channelMasks = channelMasksFromString(reply.string());
Phil Burk0709b0a2016-03-31 12:54:57 -07005749 if (device == AUDIO_DEVICE_OUT_HDMI) {
5750 filterSurroundChannelMasks(&channelMasks);
5751 }
François Gaffie112b0af2015-11-19 16:13:25 +01005752 }
5753 }
Eric Laurent20eb3a42016-01-26 18:39:17 -08005754 profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates));
François Gaffie112b0af2015-11-19 16:13:25 +01005755 }
5756}
Eric Laurentd60560a2015-04-10 11:31:20 -07005757
Eric Laurente552edb2014-03-10 17:42:56 -07005758}; // namespace android