blob: f8982e43a8ffc817fa589e70da137cbe9cfd17b1 [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"
Petri Gyntherf497f292018-04-17 18:46:10 -070029#define AUDIO_POLICY_A2DP_OFFLOAD_DISABLED_XML_CONFIG_FILE_NAME \
30 "audio_policy_configuration_a2dp_offload_disabled.xml"
François Gaffief4ad6e52015-11-19 16:59:57 +010031
Eric Laurentd4692962014-05-05 18:13:44 -070032#include <inttypes.h>
Eric Laurente552edb2014-03-10 17:42:56 -070033#include <math.h>
Eric Laurentd4692962014-05-05 18:13:44 -070034
François Gaffie2110e042015-03-24 08:41:51 +010035#include <AudioPolicyManagerInterface.h>
36#include <AudioPolicyEngineInstance.h>
Eric Laurente552edb2014-03-10 17:42:56 -070037#include <cutils/properties.h>
Eric Laurentd4692962014-05-05 18:13:44 -070038#include <utils/Log.h>
Eric Laurent3b73df72014-03-11 09:06:29 -070039#include <media/AudioParameter.h>
Eric Laurente83b55d2014-11-14 10:06:21 -080040#include <media/AudioPolicyHelper.h>
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070041#include <soundtrigger/SoundTrigger.h>
Mikhail Naganovcbc8f612016-10-11 18:05:13 -070042#include <system/audio.h>
Mikhail Naganov9ee05402016-10-13 15:58:17 -070043#include <audio_policy_conf.h>
Eric Laurentd4692962014-05-05 18:13:44 -070044#include "AudioPolicyManager.h"
François Gaffied1ab2bd2015-12-02 18:20:06 +010045#ifndef USE_XML_AUDIO_POLICY_CONF
François Gaffie53615e22015-03-19 09:24:12 +010046#include <ConfigParsingUtils.h>
François Gaffied1ab2bd2015-12-02 18:20:06 +010047#include <StreamDescriptor.h>
François Gaffief4ad6e52015-11-19 16:59:57 +010048#endif
François Gaffied1ab2bd2015-12-02 18:20:06 +010049#include <Serializer.h>
François Gaffiea8ecc2c2015-11-09 16:10:40 +010050#include "TypeConverter.h"
François Gaffie53615e22015-03-19 09:24:12 +010051#include <policy.h>
Eric Laurente552edb2014-03-10 17:42:56 -070052
Eric Laurent3b73df72014-03-11 09:06:29 -070053namespace android {
Eric Laurente552edb2014-03-10 17:42:56 -070054
Eric Laurentdc462862016-07-19 12:29:53 -070055//FIXME: workaround for truncated touch sounds
56// to be removed when the problem is handled by system UI
57#define TOUCH_SOUND_FIXED_DELAY_MS 100
Jean-Michel Trivi719a9872017-08-05 13:51:35 -070058
59// Largest difference in dB on earpiece in call between the voice volume and another
60// media / notification / system volume.
61constexpr float IN_CALL_EARPIECE_HEADROOM_DB = 3.f;
62
jiabin81772902018-04-02 17:52:27 -070063#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
64// Array of all surround formats.
65static const audio_format_t SURROUND_FORMATS[] = {
66 AUDIO_FORMAT_AC3,
67 AUDIO_FORMAT_E_AC3,
68 AUDIO_FORMAT_DTS,
69 AUDIO_FORMAT_DTS_HD,
70 AUDIO_FORMAT_AAC_LC,
71 AUDIO_FORMAT_DOLBY_TRUEHD,
72 AUDIO_FORMAT_E_AC3_JOC,
73};
74// Array of all AAC formats. When AAC is enabled by users, all AAC formats should be enabled.
75static const audio_format_t AAC_FORMATS[] = {
76 AUDIO_FORMAT_AAC_LC,
77 AUDIO_FORMAT_AAC_HE_V1,
78 AUDIO_FORMAT_AAC_HE_V2,
79 AUDIO_FORMAT_AAC_ELD,
80 AUDIO_FORMAT_AAC_XHE,
81};
82
Eric Laurente552edb2014-03-10 17:42:56 -070083// ----------------------------------------------------------------------------
84// AudioPolicyInterface implementation
85// ----------------------------------------------------------------------------
86
Eric Laurente0720872014-03-11 09:30:41 -070087status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
Paul McLeane743a472015-01-28 11:07:31 -080088 audio_policy_dev_state_t state,
89 const char *device_address,
90 const char *device_name)
Eric Laurente552edb2014-03-10 17:42:56 -070091{
jiabina7b43792018-02-15 16:04:46 -080092 status_t status = setDeviceConnectionStateInt(device, state, device_address, device_name);
93 nextAudioPortGeneration();
94 return status;
Eric Laurentc73ca6e2014-12-12 14:34:22 -080095}
96
François Gaffie44481e72016-04-20 07:49:57 +020097void AudioPolicyManager::broadcastDeviceConnectionState(audio_devices_t device,
98 audio_policy_dev_state_t state,
99 const String8 &device_address)
100{
101 AudioParameter param(device_address);
102 const String8 key(state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE ?
Mikhail Naganov388360c2016-10-17 17:09:41 -0700103 AudioParameter::keyStreamConnect : AudioParameter::keyStreamDisconnect);
François Gaffie44481e72016-04-20 07:49:57 +0200104 param.addInt(key, device);
105 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
106}
107
Eric Laurentc73ca6e2014-12-12 14:34:22 -0800108status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device,
Eric Laurenta1d525f2015-01-29 13:36:45 -0800109 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -0800110 const char *device_address,
111 const char *device_name)
Eric Laurentc73ca6e2014-12-12 14:34:22 -0800112{
Paul McLeane743a472015-01-28 11:07:31 -0800113 ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
Mikhail Naganov7e22e942017-12-07 10:04:29 -0800114 device, state, device_address, device_name);
Eric Laurente552edb2014-03-10 17:42:56 -0700115
116 // connect/disconnect only 1 device at a time
117 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
118
François Gaffie53615e22015-03-19 09:24:12 +0100119 sp<DeviceDescriptor> devDesc =
120 mHwModules.getDeviceDescriptor(device, device_address, device_name);
Paul McLeane743a472015-01-28 11:07:31 -0800121
Eric Laurente552edb2014-03-10 17:42:56 -0700122 // handle output devices
123 if (audio_is_output_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700124 SortedVector <audio_io_handle_t> outputs;
125
Eric Laurent3a4311c2014-03-17 12:00:47 -0700126 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
127
Eric Laurente552edb2014-03-10 17:42:56 -0700128 // save a copy of the opened output descriptors before any output is opened or closed
129 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
130 mPreviousOutputs = mOutputs;
Eric Laurente552edb2014-03-10 17:42:56 -0700131 switch (state)
132 {
133 // handle output device connection
Eric Laurent3ae5f312015-02-03 17:12:08 -0800134 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700135 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700136 ALOGW("setDeviceConnectionState() device already connected: %x", device);
137 return INVALID_OPERATION;
138 }
139 ALOGV("setDeviceConnectionState() connecting device %x", device);
140
Eric Laurente552edb2014-03-10 17:42:56 -0700141 // register new device as available
Eric Laurent3a4311c2014-03-17 12:00:47 -0700142 index = mAvailableOutputDevices.add(devDesc);
143 if (index >= 0) {
François Gaffie53615e22015-03-19 09:24:12 +0100144 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurentcf817a22014-08-04 20:36:31 -0700145 if (module == 0) {
146 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
147 device);
148 mAvailableOutputDevices.remove(devDesc);
149 return INVALID_OPERATION;
150 }
Paul McLeane743a472015-01-28 11:07:31 -0800151 mAvailableOutputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700152 } else {
153 return NO_MEMORY;
Eric Laurente552edb2014-03-10 17:42:56 -0700154 }
155
François Gaffie44481e72016-04-20 07:49:57 +0200156 // Before checking outputs, broadcast connect event to allow HAL to retrieve dynamic
157 // parameters on newly connected devices (instead of opening the outputs...)
158 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
159
Eric Laurenta1d525f2015-01-29 13:36:45 -0800160 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700161 mAvailableOutputDevices.remove(devDesc);
François Gaffie44481e72016-04-20 07:49:57 +0200162
163 broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
164 devDesc->mAddress);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700165 return INVALID_OPERATION;
166 }
François Gaffie2110e042015-03-24 08:41:51 +0100167 // Propagate device availability to Engine
168 mEngine->setDeviceConnectionState(devDesc, state);
169
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700170 // outputs should never be empty here
171 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
172 "checkOutputsForDevice() returned no outputs but status OK");
173 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
174 outputs.size());
Eric Laurent3ae5f312015-02-03 17:12:08 -0800175
Eric Laurent3ae5f312015-02-03 17:12:08 -0800176 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700177 // handle output device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700178 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700179 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700180 ALOGW("setDeviceConnectionState() device not connected: %x", device);
181 return INVALID_OPERATION;
182 }
183
Paul McLean5c477aa2014-08-20 16:47:57 -0700184 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
185
Paul McLeane743a472015-01-28 11:07:31 -0800186 // Send Disconnect to HALs
François Gaffie44481e72016-04-20 07:49:57 +0200187 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700188
Eric Laurente552edb2014-03-10 17:42:56 -0700189 // remove device from available output devices
Eric Laurent3a4311c2014-03-17 12:00:47 -0700190 mAvailableOutputDevices.remove(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700191
Eric Laurenta1d525f2015-01-29 13:36:45 -0800192 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
François Gaffie2110e042015-03-24 08:41:51 +0100193
194 // Propagate device availability to Engine
195 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurente552edb2014-03-10 17:42:56 -0700196 } break;
197
198 default:
199 ALOGE("setDeviceConnectionState() invalid state: %x", state);
200 return BAD_VALUE;
201 }
202
Eric Laurent3a4311c2014-03-17 12:00:47 -0700203 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
204 // output is suspended before any tracks are moved to it
Eric Laurente552edb2014-03-10 17:42:56 -0700205 checkA2dpSuspend();
206 checkOutputForAllStrategies();
207 // outputs must be closed after checkOutputForAllStrategies() is executed
208 if (!outputs.isEmpty()) {
Mikhail Naganovcf84e592017-12-07 11:25:11 -0800209 for (audio_io_handle_t output : outputs) {
210 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -0700211 // close unused outputs after device disconnection or direct outputs that have been
212 // opened by checkOutputsForDevice() to query dynamic parameters
Eric Laurent3b73df72014-03-11 09:06:29 -0700213 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Eric Laurente552edb2014-03-10 17:42:56 -0700214 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
215 (desc->mDirectOpenCount == 0))) {
Mikhail Naganovcf84e592017-12-07 11:25:11 -0800216 closeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -0700217 }
218 }
Eric Laurent3a4311c2014-03-17 12:00:47 -0700219 // check again after closing A2DP output to reset mA2dpSuspended if needed
220 checkA2dpSuspend();
Eric Laurente552edb2014-03-10 17:42:56 -0700221 }
222
223 updateDevicesAndOutputs();
Eric Laurent87ffa392015-05-22 10:32:38 -0700224 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700225 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
226 updateCallRouting(newDevice);
227 }
Eric Laurente552edb2014-03-10 17:42:56 -0700228 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700229 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
230 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
231 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700232 // do not force device change on duplicated output because if device is 0, it will
233 // also force a device 0 for the two outputs it is duplicated to which may override
234 // a valid device selection on those outputs.
Eric Laurentc75307b2015-03-17 15:29:32 -0700235 bool force = !desc->isDuplicated()
François Gaffie53615e22015-03-19 09:24:12 +0100236 && (!device_distinguishes_on_address(device)
Eric Laurentc2730ba2014-07-20 15:47:07 -0700237 // always force when disconnecting (a non-duplicated device)
238 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Eric Laurentc75307b2015-03-17 15:29:32 -0700239 setOutputDevice(desc, newDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700240 }
Eric Laurente552edb2014-03-10 17:42:56 -0700241 }
242
Eric Laurentd60560a2015-04-10 11:31:20 -0700243 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
244 cleanUpForDevice(devDesc);
245 }
246
Eric Laurent72aa32f2014-05-30 18:51:48 -0700247 mpClientInterface->onAudioPortListUpdate();
Eric Laurentb71e58b2014-05-29 16:08:11 -0700248 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700249 } // end if is output device
250
Eric Laurente552edb2014-03-10 17:42:56 -0700251 // handle input devices
252 if (audio_is_input_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700253 SortedVector <audio_io_handle_t> inputs;
254
Eric Laurent3a4311c2014-03-17 12:00:47 -0700255 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700256 switch (state)
257 {
258 // handle input device connection
Eric Laurent3b73df72014-03-11 09:06:29 -0700259 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700260 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700261 ALOGW("setDeviceConnectionState() device already connected: %d", device);
262 return INVALID_OPERATION;
263 }
François Gaffie53615e22015-03-19 09:24:12 +0100264 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurent6a94d692014-05-20 11:18:06 -0700265 if (module == NULL) {
266 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
267 device);
268 return INVALID_OPERATION;
269 }
François Gaffie44481e72016-04-20 07:49:57 +0200270
271 // Before checking intputs, broadcast connect event to allow HAL to retrieve dynamic
272 // parameters on newly connected devices (instead of opening the inputs...)
273 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
274
Paul McLean9080a4c2015-06-18 08:24:02 -0700275 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
François Gaffie44481e72016-04-20 07:49:57 +0200276 broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
277 devDesc->mAddress);
Eric Laurentd4692962014-05-05 18:13:44 -0700278 return INVALID_OPERATION;
279 }
280
Eric Laurent3a4311c2014-03-17 12:00:47 -0700281 index = mAvailableInputDevices.add(devDesc);
282 if (index >= 0) {
Paul McLeane743a472015-01-28 11:07:31 -0800283 mAvailableInputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700284 } else {
285 return NO_MEMORY;
286 }
Eric Laurent3ae5f312015-02-03 17:12:08 -0800287
François Gaffie2110e042015-03-24 08:41:51 +0100288 // Propagate device availability to Engine
289 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700290 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700291
292 // handle input device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700293 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700294 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700295 ALOGW("setDeviceConnectionState() device not connected: %d", device);
296 return INVALID_OPERATION;
297 }
Paul McLean5c477aa2014-08-20 16:47:57 -0700298
299 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
300
301 // Set Disconnect to HALs
François Gaffie44481e72016-04-20 07:49:57 +0200302 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700303
Paul McLean9080a4c2015-06-18 08:24:02 -0700304 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700305 mAvailableInputDevices.remove(devDesc);
Paul McLean5c477aa2014-08-20 16:47:57 -0700306
François Gaffie2110e042015-03-24 08:41:51 +0100307 // Propagate device availability to Engine
308 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700309 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700310
311 default:
312 ALOGE("setDeviceConnectionState() invalid state: %x", state);
313 return BAD_VALUE;
314 }
315
Eric Laurentd4692962014-05-05 18:13:44 -0700316 closeAllInputs();
Eric Laurent5f5fca52016-08-04 11:48:57 -0700317 // As the input device list can impact the output device selection, update
318 // getDeviceForStrategy() cache
319 updateDevicesAndOutputs();
Eric Laurente552edb2014-03-10 17:42:56 -0700320
Eric Laurent87ffa392015-05-22 10:32:38 -0700321 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700322 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
323 updateCallRouting(newDevice);
324 }
325
Eric Laurentd60560a2015-04-10 11:31:20 -0700326 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
327 cleanUpForDevice(devDesc);
328 }
329
Eric Laurentb52c1522014-05-20 11:27:36 -0700330 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700331 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700332 } // end if is input device
Eric Laurente552edb2014-03-10 17:42:56 -0700333
334 ALOGW("setDeviceConnectionState() invalid device: %x", device);
335 return BAD_VALUE;
336}
337
Eric Laurente0720872014-03-11 09:30:41 -0700338audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +0100339 const char *device_address)
Eric Laurente552edb2014-03-10 17:42:56 -0700340{
Eric Laurent634b7142016-04-20 13:48:02 -0700341 sp<DeviceDescriptor> devDesc =
342 mHwModules.getDeviceDescriptor(device, device_address, "",
343 (strlen(device_address) != 0)/*matchAddress*/);
344
345 if (devDesc == 0) {
346 ALOGW("getDeviceConnectionState() undeclared device, type %08x, address: %s",
347 device, device_address);
348 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
349 }
François Gaffie53615e22015-03-19 09:24:12 +0100350
Eric Laurent3a4311c2014-03-17 12:00:47 -0700351 DeviceVector *deviceVector;
352
Eric Laurente552edb2014-03-10 17:42:56 -0700353 if (audio_is_output_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700354 deviceVector = &mAvailableOutputDevices;
Eric Laurente552edb2014-03-10 17:42:56 -0700355 } else if (audio_is_input_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700356 deviceVector = &mAvailableInputDevices;
357 } else {
358 ALOGW("getDeviceConnectionState() invalid device type %08x", device);
359 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700360 }
Eric Laurent634b7142016-04-20 13:48:02 -0700361
362 return (deviceVector->getDevice(device, String8(device_address)) != 0) ?
363 AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurenta1d525f2015-01-29 13:36:45 -0800364}
365
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800366status_t AudioPolicyManager::handleDeviceConfigChange(audio_devices_t device,
367 const char *device_address,
368 const char *device_name)
369{
370 status_t status;
371
372 ALOGV("handleDeviceConfigChange(() device: 0x%X, address %s name %s",
373 device, device_address, device_name);
374
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -0800375 // connect/disconnect only 1 device at a time
376 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
377
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800378 // Check if the device is currently connected
379 sp<DeviceDescriptor> devDesc =
380 mHwModules.getDeviceDescriptor(device, device_address, device_name);
381 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
382 if (index < 0) {
383 // Nothing to do: device is not connected
384 return NO_ERROR;
385 }
386
387 // Toggle the device state: UNAVAILABLE -> AVAILABLE
388 // This will force reading again the device configuration
389 status = setDeviceConnectionState(device,
390 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
391 device_address, device_name);
392 if (status != NO_ERROR) {
393 ALOGW("handleDeviceConfigChange() error disabling connection state: %d",
394 status);
395 return status;
396 }
397
398 status = setDeviceConnectionState(device,
399 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
400 device_address, device_name);
401 if (status != NO_ERROR) {
402 ALOGW("handleDeviceConfigChange() error enabling connection state: %d",
403 status);
404 return status;
405 }
406
407 return NO_ERROR;
408}
409
Eric Laurentdc462862016-07-19 12:29:53 -0700410uint32_t AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, uint32_t delayMs)
Eric Laurentc2730ba2014-07-20 15:47:07 -0700411{
412 bool createTxPatch = false;
Eric Laurentdc462862016-07-19 12:29:53 -0700413 uint32_t muteWaitMs = 0;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700414
Andy Hunga76c7de2016-12-13 19:14:31 -0800415 if(!hasPrimaryOutput() || mPrimaryOutput->device() == AUDIO_DEVICE_OUT_STUB) {
Eric Laurentdc462862016-07-19 12:29:53 -0700416 return muteWaitMs;
Eric Laurent87ffa392015-05-22 10:32:38 -0700417 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -0800418 audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700419 ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice);
420
421 // release existing RX patch if any
422 if (mCallRxPatch != 0) {
423 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
424 mCallRxPatch.clear();
425 }
426 // release TX patch if any
427 if (mCallTxPatch != 0) {
428 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
429 mCallTxPatch.clear();
430 }
431
432 // If the RX device is on the primary HW module, then use legacy routing method for voice calls
433 // via setOutputDevice() on primary output.
434 // Otherwise, create two audio patches for TX and RX path.
435 if (availablePrimaryOutputDevices() & rxDevice) {
Eric Laurentdc462862016-07-19 12:29:53 -0700436 muteWaitMs = setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700437 // If the TX device is also on the primary HW module, setOutputDevice() will take care
438 // of it due to legacy implementation. If not, create a patch.
439 if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN)
440 == AUDIO_DEVICE_NONE) {
441 createTxPatch = true;
442 }
Eric Laurent8ae73122016-04-12 10:13:29 -0700443 } else { // create RX path audio patch
Mikhail Naganovb567ba02017-12-08 11:16:27 -0800444 mCallRxPatch = createTelephonyPatch(true /*isRx*/, rxDevice, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700445 createTxPatch = true;
446 }
Eric Laurent8ae73122016-04-12 10:13:29 -0700447 if (createTxPatch) { // create TX path audio patch
Mikhail Naganovb567ba02017-12-08 11:16:27 -0800448 mCallTxPatch = createTelephonyPatch(false /*isRx*/, txDevice, delayMs);
449 }
Eric Laurent8ae73122016-04-12 10:13:29 -0700450
Mikhail Naganovb567ba02017-12-08 11:16:27 -0800451 return muteWaitMs;
452}
Eric Laurentc2730ba2014-07-20 15:47:07 -0700453
Mikhail Naganovb567ba02017-12-08 11:16:27 -0800454sp<AudioPatch> AudioPolicyManager::createTelephonyPatch(
455 bool isRx, audio_devices_t device, uint32_t delayMs) {
456 struct audio_patch patch;
457 patch.num_sources = 1;
458 patch.num_sinks = 1;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700459
Mikhail Naganovb567ba02017-12-08 11:16:27 -0800460 sp<DeviceDescriptor> txSourceDeviceDesc;
461 if (isRx) {
462 fillAudioPortConfigForDevice(mAvailableOutputDevices, device, &patch.sinks[0]);
463 fillAudioPortConfigForDevice(
464 mAvailableInputDevices, AUDIO_DEVICE_IN_TELEPHONY_RX, &patch.sources[0]);
465 } else {
466 txSourceDeviceDesc = fillAudioPortConfigForDevice(
467 mAvailableInputDevices, device, &patch.sources[0]);
468 fillAudioPortConfigForDevice(
469 mAvailableOutputDevices, AUDIO_DEVICE_OUT_TELEPHONY_TX, &patch.sinks[0]);
470 }
471
472 audio_devices_t outputDevice = isRx ? device : AUDIO_DEVICE_OUT_TELEPHONY_TX;
473 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(outputDevice, mOutputs);
474 audio_io_handle_t output = selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID);
475 // request to reuse existing output stream if one is already opened to reach the target device
476 if (output != AUDIO_IO_HANDLE_NONE) {
477 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
478 ALOG_ASSERT(!outputDesc->isDuplicated(),
479 "%s() %#x device output %d is duplicated", __func__, outputDevice, output);
480 outputDesc->toAudioPortConfig(&patch.sources[1]);
481 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
482 patch.num_sources = 2;
483 }
484
485 if (!isRx) {
Eric Laurentc0a889f2015-10-14 14:36:34 -0700486 // terminate active capture if on the same HW module as the call TX source device
487 // FIXME: would be better to refine to only inputs whose profile connects to the
488 // call TX device but this information is not in the audio patch and logic here must be
489 // symmetric to the one in startInput()
Mikhail Naganovcf84e592017-12-07 11:25:11 -0800490 for (const auto& activeDesc : mInputs.getActiveInputs()) {
Eric Laurentfb66dd92016-01-28 18:32:03 -0800491 if (activeDesc->hasSameHwModuleAs(txSourceDeviceDesc)) {
492 AudioSessionCollection activeSessions =
493 activeDesc->getAudioSessions(true /*activeOnly*/);
494 for (size_t j = 0; j < activeSessions.size(); j++) {
495 audio_session_t activeSession = activeSessions.keyAt(j);
496 stopInput(activeDesc->mIoHandle, activeSession);
497 releaseInput(activeDesc->mIoHandle, activeSession);
498 }
Eric Laurentc0a889f2015-10-14 14:36:34 -0700499 }
500 }
Eric Laurentc2730ba2014-07-20 15:47:07 -0700501 }
Eric Laurentdc462862016-07-19 12:29:53 -0700502
Mikhail Naganovb567ba02017-12-08 11:16:27 -0800503 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
504 status_t status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs);
505 ALOGW_IF(status != NO_ERROR,
506 "%s() error %d creating %s audio patch", __func__, status, isRx ? "RX" : "TX");
507 sp<AudioPatch> audioPatch;
508 if (status == NO_ERROR) {
509 audioPatch = new AudioPatch(&patch, mUidCached);
510 audioPatch->mAfPatchHandle = afPatchHandle;
511 audioPatch->mUid = mUidCached;
512 }
513 return audioPatch;
514}
515
516sp<DeviceDescriptor> AudioPolicyManager::fillAudioPortConfigForDevice(
517 const DeviceVector& devices, audio_devices_t device, audio_port_config *config) {
518 DeviceVector deviceList = devices.getDevicesFromType(device);
519 ALOG_ASSERT(!deviceList.isEmpty(),
520 "%s() selected device type %#x is not in devices list", __func__, device);
521 sp<DeviceDescriptor> deviceDesc = deviceList.itemAt(0);
522 deviceDesc->toAudioPortConfig(config);
523 return deviceDesc;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700524}
525
Eric Laurente0720872014-03-11 09:30:41 -0700526void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700527{
528 ALOGV("setPhoneState() state %d", state);
François Gaffie2110e042015-03-24 08:41:51 +0100529 // store previous phone state for management of sonification strategy below
530 int oldState = mEngine->getPhoneState();
531
532 if (mEngine->setPhoneState(state) != NO_ERROR) {
533 ALOGW("setPhoneState() invalid or same state %d", state);
Eric Laurente552edb2014-03-10 17:42:56 -0700534 return;
535 }
François Gaffie2110e042015-03-24 08:41:51 +0100536 /// Opens: can these line be executed after the switch of volume curves???
Eric Laurente552edb2014-03-10 17:42:56 -0700537 // if leaving call state, handle special case of active streams
538 // pertaining to sonification strategy see handleIncallSonification()
Eric Laurent63dea1d2015-07-02 17:10:28 -0700539 if (isStateInCall(oldState)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700540 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent794fde22016-03-11 09:50:45 -0800541 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700542 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700543 }
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800544
Eric Laurent63dea1d2015-07-02 17:10:28 -0700545 // force reevaluating accessibility routing when call stops
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800546 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700547 }
548
François Gaffie2110e042015-03-24 08:41:51 +0100549 /**
550 * Switching to or from incall state or switching between telephony and VoIP lead to force
551 * routing command.
552 */
553 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
554 || (is_state_in_call(state) && (state != oldState)));
Eric Laurente552edb2014-03-10 17:42:56 -0700555
556 // check for device and output changes triggered by new phone state
Eric Laurente552edb2014-03-10 17:42:56 -0700557 checkA2dpSuspend();
558 checkOutputForAllStrategies();
559 updateDevicesAndOutputs();
560
Eric Laurente552edb2014-03-10 17:42:56 -0700561 int delayMs = 0;
562 if (isStateInCall(state)) {
563 nsecs_t sysTime = systemTime();
564 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700565 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700566 // mute media and sonification strategies and delay device switch by the largest
567 // latency of any output where either strategy is active.
568 // This avoid sending the ring tone or music tail into the earpiece or headset.
François Gaffiead3183e2015-03-18 16:55:35 +0100569 if ((isStrategyActive(desc, STRATEGY_MEDIA,
570 SONIFICATION_HEADSET_MUSIC_DELAY,
571 sysTime) ||
572 isStrategyActive(desc, STRATEGY_SONIFICATION,
573 SONIFICATION_HEADSET_MUSIC_DELAY,
574 sysTime)) &&
Eric Laurentc75307b2015-03-17 15:29:32 -0700575 (delayMs < (int)desc->latency()*2)) {
576 delayMs = desc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -0700577 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700578 setStrategyMute(STRATEGY_MEDIA, true, desc);
579 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700580 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Eric Laurentc75307b2015-03-17 15:29:32 -0700581 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
582 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700583 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
584 }
585 }
586
Eric Laurent87ffa392015-05-22 10:32:38 -0700587 if (hasPrimaryOutput()) {
588 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
589 // the device returned is not necessarily reachable via this output
590 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
591 // force routing command to audio hardware when ending call
592 // even if no device change is needed
593 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
594 rxDevice = mPrimaryOutput->device();
595 }
Eric Laurente552edb2014-03-10 17:42:56 -0700596
Eric Laurent87ffa392015-05-22 10:32:38 -0700597 if (state == AUDIO_MODE_IN_CALL) {
598 updateCallRouting(rxDevice, delayMs);
599 } else if (oldState == AUDIO_MODE_IN_CALL) {
600 if (mCallRxPatch != 0) {
601 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
602 mCallRxPatch.clear();
603 }
604 if (mCallTxPatch != 0) {
605 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
606 mCallTxPatch.clear();
607 }
608 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
609 } else {
610 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700611 }
Eric Laurentc2730ba2014-07-20 15:47:07 -0700612 }
Eric Laurent2e2a8a92018-04-20 16:21:33 -0700613
614 // reevaluate routing on all outputs in case tracks have been started during the call
615 for (size_t i = 0; i < mOutputs.size(); i++) {
616 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
617 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
618 if (state != AUDIO_MODE_IN_CALL || desc != mPrimaryOutput) {
Yung Ti Suf60c8242018-05-10 18:07:26 +0800619 setOutputDevice(desc, newDevice, (newDevice != AUDIO_DEVICE_NONE), 0 /*delayMs*/);
Eric Laurent2e2a8a92018-04-20 16:21:33 -0700620 }
621 }
622
Eric Laurente552edb2014-03-10 17:42:56 -0700623 // if entering in call state, handle special case of active streams
624 // pertaining to sonification strategy see handleIncallSonification()
625 if (isStateInCall(state)) {
626 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent794fde22016-03-11 09:50:45 -0800627 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700628 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700629 }
Eric Laurent63dea1d2015-07-02 17:10:28 -0700630
631 // force reevaluating accessibility routing when call starts
632 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700633 }
634
635 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700636 if (state == AUDIO_MODE_RINGTONE &&
637 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700638 mLimitRingtoneVolume = true;
639 } else {
640 mLimitRingtoneVolume = false;
641 }
642}
643
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700644audio_mode_t AudioPolicyManager::getPhoneState() {
645 return mEngine->getPhoneState();
646}
647
Eric Laurente0720872014-03-11 09:30:41 -0700648void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700649 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700650{
François Gaffie2110e042015-03-24 08:41:51 +0100651 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Eric Laurent8dc87a62017-05-16 19:00:40 -0700652 if (config == mEngine->getForceUse(usage)) {
653 return;
654 }
Eric Laurente552edb2014-03-10 17:42:56 -0700655
François Gaffie2110e042015-03-24 08:41:51 +0100656 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
657 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
658 return;
Eric Laurente552edb2014-03-10 17:42:56 -0700659 }
François Gaffie2110e042015-03-24 08:41:51 +0100660 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
661 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
662 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
Eric Laurente552edb2014-03-10 17:42:56 -0700663
664 // check for device and output changes triggered by new force usage
665 checkA2dpSuspend();
666 checkOutputForAllStrategies();
667 updateDevicesAndOutputs();
Phil Burk09bc4612016-02-24 15:58:15 -0800668
Eric Laurentdc462862016-07-19 12:29:53 -0700669 //FIXME: workaround for truncated touch sounds
670 // to be removed when the problem is handled by system UI
671 uint32_t delayMs = 0;
672 uint32_t waitMs = 0;
673 if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) {
674 delayMs = TOUCH_SOUND_FIXED_DELAY_MS;
675 }
Eric Laurent87ffa392015-05-22 10:32:38 -0700676 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700677 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
Eric Laurentdc462862016-07-19 12:29:53 -0700678 waitMs = updateCallRouting(newDevice, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700679 }
Eric Laurente552edb2014-03-10 17:42:56 -0700680 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700681 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
682 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
683 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
Eric Laurentdc462862016-07-19 12:29:53 -0700684 waitMs = setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE),
685 delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700686 }
Eric Laurente552edb2014-03-10 17:42:56 -0700687 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
Eric Laurentdc462862016-07-19 12:29:53 -0700688 applyStreamVolumes(outputDesc, newDevice, waitMs, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700689 }
690 }
691
Mikhail Naganovcf84e592017-12-07 11:25:11 -0800692 for (const auto& activeDesc : mInputs.getActiveInputs()) {
Eric Laurentfb66dd92016-01-28 18:32:03 -0800693 audio_devices_t newDevice = getNewInputDevice(activeDesc);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700694 // Force new input selection if the new device can not be reached via current input
Eric Laurentfb66dd92016-01-28 18:32:03 -0800695 if (activeDesc->mProfile->getSupportedDevices().types() &
696 (newDevice & ~AUDIO_DEVICE_BIT_IN)) {
697 setInputDevice(activeDesc->mIoHandle, newDevice);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700698 } else {
Eric Laurentfb66dd92016-01-28 18:32:03 -0800699 closeInput(activeDesc->mIoHandle);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700700 }
Eric Laurente552edb2014-03-10 17:42:56 -0700701 }
Eric Laurente552edb2014-03-10 17:42:56 -0700702}
703
Eric Laurente0720872014-03-11 09:30:41 -0700704void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700705{
706 ALOGV("setSystemProperty() property %s, value %s", property, value);
707}
708
709// Find a direct output profile compatible with the parameters passed, even if the input flags do
710// not explicitly request a direct output
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800711sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700712 audio_devices_t device,
713 uint32_t samplingRate,
714 audio_format_t format,
715 audio_channel_mask_t channelMask,
716 audio_output_flags_t flags)
717{
Eric Laurent861a6282015-05-18 15:40:16 -0700718 // only retain flags that will drive the direct output profile selection
719 // if explicitly requested
720 static const uint32_t kRelevantFlags =
Haynes Mathew George84c621e2017-04-25 11:41:50 -0700721 (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD |
722 AUDIO_OUTPUT_FLAG_VOIP_RX);
Eric Laurent861a6282015-05-18 15:40:16 -0700723 flags =
724 (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT);
725
726 sp<IOProfile> profile;
727
Mikhail Naganovd4120142017-12-06 15:49:22 -0800728 for (const auto& hwModule : mHwModules) {
Mikhail Naganova5e165d2017-12-07 17:08:02 -0800729 for (const auto& curProfile : hwModule->getOutputProfiles()) {
Eric Laurent861a6282015-05-18 15:40:16 -0700730 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,
Nadav Bar766fb022018-01-07 12:18:03 +0200778 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) {
Mikhail Naganovcf84e592017-12-07 11:25:11 -0800830 deviceDesc = mAvailableOutputDevices.getDeviceFromId(*selectedDeviceId);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700831 }
832 mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700833
Eric Laurente83b55d2014-11-14 10:06:21 -0800834 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700835 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700836
Eric Laurente83b55d2014-11-14 10:06:21 -0800837 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Nadav Bar766fb022018-01-07 12:18:03 +0200838 *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
Eric Laurent93c3d412014-08-01 14:48:35 -0700839 }
840
Glenn Kasten49f36ba2017-12-06 13:02:02 -0800841 ALOGV("getOutputForAttr() device 0x%x, sampling rate %d, format %#x, channel mask %#x, "
842 "flags %#x",
Nadav Bar766fb022018-01-07 12:18:03 +0200843 device, config->sample_rate, config->format, config->channel_mask, *flags);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700844
Andy Hungc88b0642018-04-27 15:42:35 -0700845 *output = getOutputForDevice(device, session, *stream, config, flags);
Eric Laurente83b55d2014-11-14 10:06:21 -0800846 if (*output == AUDIO_IO_HANDLE_NONE) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700847 mOutputRoutes.removeRoute(session);
Eric Laurente83b55d2014-11-14 10:06:21 -0800848 return INVALID_OPERATION;
849 }
Paul McLeanaa981192015-03-21 09:55:15 -0700850
Eric Laurent2ac76942017-06-22 17:17:09 -0700851 DeviceVector outputDevices = mAvailableOutputDevices.getDevicesFromType(device);
852 *selectedDeviceId = outputDevices.size() > 0 ? outputDevices.itemAt(0)->getId()
853 : AUDIO_PORT_HANDLE_NONE;
854
855 ALOGV(" getOutputForAttr() returns output %d selectedDeviceId %d", *output, *selectedDeviceId);
856
Eric Laurente83b55d2014-11-14 10:06:21 -0800857 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700858}
859
860audio_io_handle_t AudioPolicyManager::getOutputForDevice(
861 audio_devices_t device,
Kevin Rocard169753c2017-03-06 14:18:23 -0800862 audio_session_t session,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700863 audio_stream_type_t stream,
Eric Laurentfe231122017-11-17 17:48:06 -0800864 const audio_config_t *config,
Nadav Bar766fb022018-01-07 12:18:03 +0200865 audio_output_flags_t *flags)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700866{
Andy Hungc88b0642018-04-27 15:42:35 -0700867 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700868 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700869
Eric Laurente552edb2014-03-10 17:42:56 -0700870 // open a direct output if required by specified parameters
871 //force direct flag if offload flag is set: offloading implies a direct output stream
872 // and all common behaviors are driven by checking only the direct flag
873 // this should normally be set appropriately in the policy configuration file
Nadav Bar766fb022018-01-07 12:18:03 +0200874 if ((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
875 *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700876 }
Nadav Bar766fb022018-01-07 12:18:03 +0200877 if ((*flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
878 *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurent93c3d412014-08-01 14:48:35 -0700879 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800880 // only allow deep buffering for music stream type
881 if (stream != AUDIO_STREAM_MUSIC) {
Nadav Bar766fb022018-01-07 12:18:03 +0200882 *flags = (audio_output_flags_t)(*flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Ravi Kumar Alamanda439e4ed2015-04-03 12:13:21 -0700883 } else if (/* stream == AUDIO_STREAM_MUSIC && */
Nadav Bar766fb022018-01-07 12:18:03 +0200884 *flags == AUDIO_OUTPUT_FLAG_NONE &&
Ravi Kumar Alamanda439e4ed2015-04-03 12:13:21 -0700885 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
886 // use DEEP_BUFFER as default output for music stream type
Nadav Bar766fb022018-01-07 12:18:03 +0200887 *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Eric Laurente83b55d2014-11-14 10:06:21 -0800888 }
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700889 if (stream == AUDIO_STREAM_TTS) {
Nadav Bar766fb022018-01-07 12:18:03 +0200890 *flags = AUDIO_OUTPUT_FLAG_TTS;
Haynes Mathew George84c621e2017-04-25 11:41:50 -0700891 } else if (stream == AUDIO_STREAM_VOICE_CALL &&
Eric Laurentfe231122017-11-17 17:48:06 -0800892 audio_is_linear_pcm(config->format)) {
Nadav Bar766fb022018-01-07 12:18:03 +0200893 *flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX |
Haynes Mathew George84c621e2017-04-25 11:41:50 -0700894 AUDIO_OUTPUT_FLAG_DIRECT);
895 ALOGV("Set VoIP and Direct output flags for PCM format");
Nadav Bar766fb022018-01-07 12:18:03 +0200896 } else if (device == AUDIO_DEVICE_OUT_TELEPHONY_TX &&
897 stream == AUDIO_STREAM_MUSIC &&
898 audio_is_linear_pcm(config->format) &&
899 isInCall()) {
900 *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INCALL_MUSIC;
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700901 }
Eric Laurente552edb2014-03-10 17:42:56 -0700902
Nadav Bar766fb022018-01-07 12:18:03 +0200903
Eric Laurentb732cf52014-09-24 19:08:21 -0700904 sp<IOProfile> profile;
905
906 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700907 // and not explicitly requested
Nadav Bar766fb022018-01-07 12:18:03 +0200908 if (((*flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
Eric Laurentfe231122017-11-17 17:48:06 -0800909 audio_is_linear_pcm(config->format) && config->sample_rate <= SAMPLE_RATE_HZ_MAX &&
910 audio_channel_count_from_out_mask(config->channel_mask) <= 2) {
Eric Laurentb732cf52014-09-24 19:08:21 -0700911 goto non_direct_output;
912 }
913
Andy Hung2ddee192015-12-18 17:34:44 -0800914 // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled.
915 // This prevents creating an offloaded track and tearing it down immediately after start
916 // when audioflinger detects there is an active non offloadable effect.
Eric Laurente552edb2014-03-10 17:42:56 -0700917 // FIXME: We should check the audio session here but we do not have it in this context.
918 // This may prevent offloading in rare situations where effects are left active by apps
919 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700920
Nadav Bar766fb022018-01-07 12:18:03 +0200921 if (((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
Andy Hung2ddee192015-12-18 17:34:44 -0800922 !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700923 profile = getProfileForDirectOutput(device,
Eric Laurentfe231122017-11-17 17:48:06 -0800924 config->sample_rate,
925 config->format,
926 config->channel_mask,
Nadav Bar766fb022018-01-07 12:18:03 +0200927 (audio_output_flags_t)*flags);
Eric Laurente552edb2014-03-10 17:42:56 -0700928 }
929
Eric Laurent1c333e22014-05-20 10:48:17 -0700930 if (profile != 0) {
Andy Hungc88b0642018-04-27 15:42:35 -0700931 // exclusive outputs for MMAP and Offload are enforced by different session ids.
932 for (size_t i = 0; i < mOutputs.size(); i++) {
933 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
934 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
935 // reuse direct output if currently open by the same client
936 // and configured with same parameters
937 if ((config->sample_rate == desc->mSamplingRate) &&
Andy Hung5659ed52018-04-30 10:31:26 -0700938 (config->format == desc->mFormat) &&
Andy Hungc88b0642018-04-27 15:42:35 -0700939 (config->channel_mask == desc->mChannelMask) &&
940 (session == desc->mDirectClientSession)) {
941 desc->mDirectOpenCount++;
942 ALOGI("getOutputForDevice() reusing direct output %d for session %d",
943 mOutputs.keyAt(i), session);
944 return mOutputs.keyAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700945 }
946 }
947 }
Eric Laurent3974e3b2017-12-07 17:58:43 -0800948
949 if (!profile->canOpenNewIo()) {
950 goto non_direct_output;
Eric Laurente552edb2014-03-10 17:42:56 -0700951 }
Eric Laurent861a6282015-05-18 15:40:16 -0700952
Eric Laurent3974e3b2017-12-07 17:58:43 -0800953 sp<SwAudioOutputDescriptor> outputDesc =
954 new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurent53b810e2017-12-10 17:25:10 -0800955
956 DeviceVector outputDevices = mAvailableOutputDevices.getDevicesFromType(device);
957 String8 address = outputDevices.size() > 0 ? outputDevices.itemAt(0)->mAddress
958 : String8("");
959
Nadav Bar766fb022018-01-07 12:18:03 +0200960 status = outputDesc->open(config, device, address, stream, *flags, &output);
Eric Laurente552edb2014-03-10 17:42:56 -0700961
962 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -0700963 if (status != NO_ERROR ||
Eric Laurentfe231122017-11-17 17:48:06 -0800964 (config->sample_rate != 0 && config->sample_rate != outputDesc->mSamplingRate) ||
Andy Hung5659ed52018-04-30 10:31:26 -0700965 (config->format != AUDIO_FORMAT_DEFAULT && config->format != outputDesc->mFormat) ||
Eric Laurentfe231122017-11-17 17:48:06 -0800966 (config->channel_mask != 0 && config->channel_mask != outputDesc->mChannelMask)) {
967 ALOGV("getOutputForDevice() failed opening direct output: output %d sample rate %d %d,"
968 "format %d %d, channel mask %04x %04x", output, config->sample_rate,
969 outputDesc->mSamplingRate, config->format, outputDesc->mFormat,
970 config->channel_mask, outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700971 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurentfe231122017-11-17 17:48:06 -0800972 outputDesc->close();
Eric Laurente552edb2014-03-10 17:42:56 -0700973 }
Eric Laurenta82797f2015-01-30 11:49:43 -0800974 // fall back to mixer output if possible when the direct output could not be open
Eric Laurentfe231122017-11-17 17:48:06 -0800975 if (audio_is_linear_pcm(config->format) &&
976 config->sample_rate <= SAMPLE_RATE_HZ_MAX) {
Eric Laurenta82797f2015-01-30 11:49:43 -0800977 goto non_direct_output;
978 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700979 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -0700980 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700981 outputDesc->mRefCount[stream] = 0;
982 outputDesc->mStopTime[stream] = 0;
983 outputDesc->mDirectOpenCount = 1;
Kevin Rocard169753c2017-03-06 14:18:23 -0800984 outputDesc->mDirectClientSession = session;
985
Eric Laurente552edb2014-03-10 17:42:56 -0700986 addOutput(output, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700987 mPreviousOutputs = mOutputs;
Eric Laurentf4e63452017-11-06 19:31:46 +0000988 ALOGV("getOutputForDevice() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -0700989 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700990 return output;
991 }
992
Eric Laurentb732cf52014-09-24 19:08:21 -0700993non_direct_output:
Eric Laurent14cbfca2016-03-17 09:42:16 -0700994
995 // A request for HW A/V sync cannot fallback to a mixed output because time
996 // stamps are embedded in audio data
Phil Burk2d059932018-02-15 15:55:11 -0800997 if ((*flags & (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_MMAP_NOIRQ)) != 0) {
Eric Laurent14cbfca2016-03-17 09:42:16 -0700998 return AUDIO_IO_HANDLE_NONE;
999 }
1000
Eric Laurente552edb2014-03-10 17:42:56 -07001001 // ignoring channel mask due to downmix capability in mixer
1002
1003 // open a non direct output
1004
1005 // for non direct outputs, only PCM is supported
Eric Laurentfe231122017-11-17 17:48:06 -08001006 if (audio_is_linear_pcm(config->format)) {
Eric Laurente552edb2014-03-10 17:42:56 -07001007 // get which output is suitable for the specified stream. The actual
1008 // routing change will happen when startOutput() will be called
1009 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1010
Eric Laurent8838a382014-09-08 16:44:28 -07001011 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
Nadav Bar766fb022018-01-07 12:18:03 +02001012 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1013 output = selectOutput(outputs, *flags, config->format);
Eric Laurente552edb2014-03-10 17:42:56 -07001014 }
Eric Laurentf4e63452017-11-06 19:31:46 +00001015 ALOGW_IF((output == 0), "getOutputForDevice() could not find output for stream %d, "
Glenn Kasten49f36ba2017-12-06 13:02:02 -08001016 "sampling rate %d, format %#x, channels %#x, flags %#x",
Nadav Bar766fb022018-01-07 12:18:03 +02001017 stream, config->sample_rate, config->format, config->channel_mask, *flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001018
Eric Laurente552edb2014-03-10 17:42:56 -07001019 return output;
1020}
1021
Eric Laurente0720872014-03-11 09:30:41 -07001022audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -07001023 audio_output_flags_t flags,
1024 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -07001025{
1026 // select one output among several that provide a path to a particular device or set of
1027 // devices (the list was previously build by getOutputsForDevice()).
1028 // The priority is as follows:
1029 // 1: the output with the highest number of requested policy flags
Eric Laurente6930022016-02-11 10:20:40 -08001030 // 2: the output with the bit depth the closest to the requested one
1031 // 3: the primary output
1032 // 4: the first output in the list
Eric Laurente552edb2014-03-10 17:42:56 -07001033
1034 if (outputs.size() == 0) {
Mikhail Naganovcf84e592017-12-07 11:25:11 -08001035 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001036 }
1037 if (outputs.size() == 1) {
1038 return outputs[0];
1039 }
1040
1041 int maxCommonFlags = 0;
Mikhail Naganovcf84e592017-12-07 11:25:11 -08001042 audio_io_handle_t outputForFlags = AUDIO_IO_HANDLE_NONE;
1043 audio_io_handle_t outputForPrimary = AUDIO_IO_HANDLE_NONE;
1044 audio_io_handle_t outputForFormat = AUDIO_IO_HANDLE_NONE;
Eric Laurente6930022016-02-11 10:20:40 -08001045 audio_format_t bestFormat = AUDIO_FORMAT_INVALID;
1046 audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07001047
Mikhail Naganovcf84e592017-12-07 11:25:11 -08001048 for (audio_io_handle_t output : outputs) {
1049 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001050 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -07001051 // if a valid format is specified, skip output if not compatible
1052 if (format != AUDIO_FORMAT_INVALID) {
1053 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Andy Hung5659ed52018-04-30 10:31:26 -07001054 if (format != outputDesc->mFormat) {
Eric Laurent8838a382014-09-08 16:44:28 -07001055 continue;
1056 }
1057 } else if (!audio_is_linear_pcm(format)) {
1058 continue;
1059 }
Eric Laurente6930022016-02-11 10:20:40 -08001060 if (AudioPort::isBetterFormatMatch(
1061 outputDesc->mFormat, bestFormat, format)) {
Mikhail Naganovcf84e592017-12-07 11:25:11 -08001062 outputForFormat = output;
Eric Laurente6930022016-02-11 10:20:40 -08001063 bestFormat = outputDesc->mFormat;
1064 }
Eric Laurent8838a382014-09-08 16:44:28 -07001065 }
1066
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001067 int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags);
Eric Laurente6930022016-02-11 10:20:40 -08001068 if (commonFlags >= maxCommonFlags) {
1069 if (commonFlags == maxCommonFlags) {
Andy Hungc9901522017-11-10 20:07:54 -08001070 if (format != AUDIO_FORMAT_INVALID
1071 && AudioPort::isBetterFormatMatch(
1072 outputDesc->mFormat, bestFormatForFlags, format)) {
Mikhail Naganovcf84e592017-12-07 11:25:11 -08001073 outputForFlags = output;
Eric Laurente6930022016-02-11 10:20:40 -08001074 bestFormatForFlags = outputDesc->mFormat;
1075 }
1076 } else {
Mikhail Naganovcf84e592017-12-07 11:25:11 -08001077 outputForFlags = output;
Eric Laurente6930022016-02-11 10:20:40 -08001078 maxCommonFlags = commonFlags;
1079 bestFormatForFlags = outputDesc->mFormat;
1080 }
Mikhail Naganovcf84e592017-12-07 11:25:11 -08001081 ALOGV("selectOutput() commonFlags for output %d, %04x", output, commonFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001082 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001083 if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Mikhail Naganovcf84e592017-12-07 11:25:11 -08001084 outputForPrimary = output;
Eric Laurente552edb2014-03-10 17:42:56 -07001085 }
1086 }
1087 }
1088
Mikhail Naganovcf84e592017-12-07 11:25:11 -08001089 if (outputForFlags != AUDIO_IO_HANDLE_NONE) {
Eric Laurente6930022016-02-11 10:20:40 -08001090 return outputForFlags;
Eric Laurente552edb2014-03-10 17:42:56 -07001091 }
Mikhail Naganovcf84e592017-12-07 11:25:11 -08001092 if (outputForFormat != AUDIO_IO_HANDLE_NONE) {
Eric Laurente6930022016-02-11 10:20:40 -08001093 return outputForFormat;
1094 }
Mikhail Naganovcf84e592017-12-07 11:25:11 -08001095 if (outputForPrimary != AUDIO_IO_HANDLE_NONE) {
Eric Laurente6930022016-02-11 10:20:40 -08001096 return outputForPrimary;
Eric Laurente552edb2014-03-10 17:42:56 -07001097 }
1098
1099 return outputs[0];
1100}
1101
Eric Laurente0720872014-03-11 09:30:41 -07001102status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001103 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001104 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001105{
Paul McLeanaa981192015-03-21 09:55:15 -07001106 ALOGV("startOutput() output %d, stream %d, session %d",
1107 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -07001108 ssize_t index = mOutputs.indexOfKey(output);
1109 if (index < 0) {
1110 ALOGW("startOutput() unknown output %d", output);
1111 return BAD_VALUE;
1112 }
1113
Eric Laurentc75307b2015-03-17 15:29:32 -07001114 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
1115
Eric Laurent733ce942017-12-07 12:18:25 -08001116 status_t status = outputDesc->start();
1117 if (status != NO_ERROR) {
1118 return status;
Eric Laurent3974e3b2017-12-07 17:58:43 -08001119 }
1120
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001121 // Routing?
1122 mOutputRoutes.incRouteActivity(session);
1123
Eric Laurentc75307b2015-03-17 15:29:32 -07001124 audio_devices_t newDevice;
Eric Laurentc40d9692016-04-13 19:14:13 -07001125 AudioMix *policyMix = NULL;
1126 const char *address = NULL;
Eric Laurentc75307b2015-03-17 15:29:32 -07001127 if (outputDesc->mPolicyMix != NULL) {
Eric Laurentc40d9692016-04-13 19:14:13 -07001128 policyMix = outputDesc->mPolicyMix;
1129 address = policyMix->mDeviceAddress.string();
1130 if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
1131 newDevice = policyMix->mDeviceType;
1132 } else {
1133 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
1134 }
Eric Laurent2157f5b2018-04-25 18:33:02 -07001135 } else if (mOutputRoutes.getAndClearRouteChanged(session)) {
Eric Laurent493404d2015-04-21 15:07:36 -07001136 newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001137 checkStrategyRoute(getStrategy(stream), output);
Eric Laurentc75307b2015-03-17 15:29:32 -07001138 } else {
1139 newDevice = AUDIO_DEVICE_NONE;
1140 }
1141
1142 uint32_t delayMs = 0;
1143
Eric Laurent733ce942017-12-07 12:18:25 -08001144 status = startSource(outputDesc, stream, newDevice, address, &delayMs);
Eric Laurentc75307b2015-03-17 15:29:32 -07001145
1146 if (status != NO_ERROR) {
1147 mOutputRoutes.decRouteActivity(session);
Eric Laurent733ce942017-12-07 12:18:25 -08001148 outputDesc->stop();
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001149 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -07001150 }
1151 // Automatically enable the remote submix input when output is started on a re routing mix
1152 // of type MIX_TYPE_RECORDERS
Eric Laurentc40d9692016-04-13 19:14:13 -07001153 if (audio_is_remote_submix_device(newDevice) && policyMix != NULL &&
1154 policyMix->mMixType == MIX_TYPE_RECORDERS) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001155 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1156 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Eric Laurentc40d9692016-04-13 19:14:13 -07001157 address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001158 "remote-submix");
1159 }
1160
1161 if (delayMs != 0) {
1162 usleep(delayMs * 1000);
1163 }
1164
1165 return status;
1166}
1167
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001168status_t AudioPolicyManager::startSource(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurentc75307b2015-03-17 15:29:32 -07001169 audio_stream_type_t stream,
1170 audio_devices_t device,
Eric Laurentc40d9692016-04-13 19:14:13 -07001171 const char *address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001172 uint32_t *delayMs)
1173{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001174 // cannot start playback of STREAM_TTS if any other output is being used
1175 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001176
1177 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001178 if (stream == AUDIO_STREAM_TTS) {
1179 ALOGV("\t found BEACON stream");
Eric Laurent9459fb02015-08-12 18:36:32 -07001180 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001181 return INVALID_OPERATION;
1182 } else {
1183 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1184 }
1185 } else {
1186 // some playback other than beacon starts
1187 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1188 }
1189
Eric Laurent77305a62016-07-25 16:39:22 -07001190 // force device change if the output is inactive and no audio patch is already present.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001191 // check active before incrementing usage count
Eric Laurent77305a62016-07-25 16:39:22 -07001192 bool force = !outputDesc->isActive() &&
1193 (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001194
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00001195 // requiresMuteCheck is false when we can bypass mute strategy.
1196 // It covers a common case when there is no materially active audio
1197 // and muting would result in unnecessary delay and dropped audio.
1198 const uint32_t outputLatencyMs = outputDesc->latency();
1199 bool requiresMuteCheck = outputDesc->isActive(outputLatencyMs * 2); // account for drain
1200
Eric Laurente552edb2014-03-10 17:42:56 -07001201 // increment usage count for this stream on the requested output:
1202 // NOTE that the usage count is the same for duplicated output and hardware output which is
1203 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1204 outputDesc->changeRefCount(stream, 1);
1205
Eric Laurent36829f92017-04-07 19:04:42 -07001206 if (stream == AUDIO_STREAM_MUSIC) {
1207 selectOutputForMusicEffects();
1208 }
1209
Eric Laurent493404d2015-04-21 15:07:36 -07001210 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001211 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001212 if (device == AUDIO_DEVICE_NONE) {
1213 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001214 }
Eric Laurent36829f92017-04-07 19:04:42 -07001215
Eric Laurente552edb2014-03-10 17:42:56 -07001216 routing_strategy strategy = getStrategy(stream);
1217 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001218 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1219 (beaconMuteLatency > 0);
1220 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001221 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001222 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001223 if (desc != outputDesc) {
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00001224 // An output has a shared device if
1225 // - managed by the same hw module
1226 // - supports the currently selected device
1227 const bool sharedDevice = outputDesc->sharesHwModuleWith(desc)
1228 && (desc->supportedDevices() & device) != AUDIO_DEVICE_NONE;
1229
Eric Laurent77305a62016-07-25 16:39:22 -07001230 // force a device change if any other output is:
1231 // - managed by the same hw module
Jean-Michel Trivi4a5b4812018-02-08 17:22:32 +00001232 // - supports currently selected device
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00001233 // - has a current device selection that differs from selected device.
Eric Laurent77305a62016-07-25 16:39:22 -07001234 // - has an active audio patch
Eric Laurente552edb2014-03-10 17:42:56 -07001235 // In this case, the audio HAL must receive the new device selection so that it can
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00001236 // change the device currently selected by the other output.
1237 if (sharedDevice &&
Eric Laurent77305a62016-07-25 16:39:22 -07001238 desc->device() != device &&
Eric Laurent77305a62016-07-25 16:39:22 -07001239 desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07001240 force = true;
1241 }
1242 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001243 // a notification so that audio focus effect can propagate, or that a mute/unmute
1244 // event occurred for beacon
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00001245 const uint32_t latencyMs = desc->latency();
1246 const bool isActive = desc->isActive(latencyMs * 2); // account for drain
1247
1248 if (shouldWait && isActive && (waitMs < latencyMs)) {
1249 waitMs = latencyMs;
Eric Laurente552edb2014-03-10 17:42:56 -07001250 }
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00001251
1252 // Require mute check if another output is on a shared device
1253 // and currently active to have proper drain and avoid pops.
1254 // Note restoring AudioTracks onto this output needs to invoke
1255 // a volume ramp if there is no mute.
1256 requiresMuteCheck |= sharedDevice && isActive;
Eric Laurente552edb2014-03-10 17:42:56 -07001257 }
1258 }
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00001259
1260 const uint32_t muteWaitMs =
1261 setOutputDevice(outputDesc, device, force, 0, NULL, address, requiresMuteCheck);
Eric Laurente552edb2014-03-10 17:42:56 -07001262
1263 // handle special case for sonification while in call
1264 if (isInCall()) {
1265 handleIncallSonification(stream, true, false);
1266 }
1267
1268 // apply volume rules for current stream and device if necessary
1269 checkAndSetVolume(stream,
Shuhei Miyazaki6fe70332017-07-31 15:21:28 +09001270 mVolumeCurves->getVolumeIndex(stream, outputDesc->device()),
Eric Laurentc75307b2015-03-17 15:29:32 -07001271 outputDesc,
Shuhei Miyazaki6fe70332017-07-31 15:21:28 +09001272 outputDesc->device());
Eric Laurente552edb2014-03-10 17:42:56 -07001273
1274 // update the outputs if starting an output with a stream that can affect notification
1275 // routing
1276 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001277
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001278 // force reevaluating accessibility routing when ringtone or alarm starts
1279 if (strategy == STRATEGY_SONIFICATION) {
1280 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1281 }
Eric Laurentdc462862016-07-19 12:29:53 -07001282
1283 if (waitMs > muteWaitMs) {
1284 *delayMs = waitMs - muteWaitMs;
1285 }
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00001286
1287 // FIXME: A device change (muteWaitMs > 0) likely introduces a volume change.
1288 // A volume change enacted by APM with 0 delay is not synchronous, as it goes
1289 // via AudioCommandThread to AudioFlinger. Hence it is possible that the volume
1290 // change occurs after the MixerThread starts and causes a stream volume
1291 // glitch.
1292 //
1293 // We do not introduce additional delay here.
Eric Laurente552edb2014-03-10 17:42:56 -07001294 }
Eric Laurentdc462862016-07-19 12:29:53 -07001295
Tomoharu Kasaharab62d78b2018-01-18 20:55:02 +09001296 if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE &&
1297 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
1298 setStrategyMute(STRATEGY_SONIFICATION, true, outputDesc);
1299 }
1300
Tomoharu Kasaharaa81f0982018-01-18 20:55:02 +09001301 if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE &&
1302 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
1303 setStrategyMute(STRATEGY_SONIFICATION, true, outputDesc);
1304 }
1305
Eric Laurente552edb2014-03-10 17:42:56 -07001306 return NO_ERROR;
1307}
1308
1309
Eric Laurente0720872014-03-11 09:30:41 -07001310status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001311 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001312 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001313{
1314 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1315 ssize_t index = mOutputs.indexOfKey(output);
1316 if (index < 0) {
1317 ALOGW("stopOutput() unknown output %d", output);
1318 return BAD_VALUE;
1319 }
1320
Eric Laurentc75307b2015-03-17 15:29:32 -07001321 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001322
Eric Laurentc75307b2015-03-17 15:29:32 -07001323 if (outputDesc->mRefCount[stream] == 1) {
1324 // Automatically disable the remote submix input when output is stopped on a
1325 // re routing mix of type MIX_TYPE_RECORDERS
1326 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1327 outputDesc->mPolicyMix != NULL &&
1328 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1329 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1330 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001331 outputDesc->mPolicyMix->mDeviceAddress,
Eric Laurentc75307b2015-03-17 15:29:32 -07001332 "remote-submix");
1333 }
1334 }
1335
1336 // Routing?
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001337 bool forceDeviceUpdate = false;
Eric Laurentc75307b2015-03-17 15:29:32 -07001338 if (outputDesc->mRefCount[stream] > 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001339 int activityCount = mOutputRoutes.decRouteActivity(session);
1340 forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0));
1341
1342 if (forceDeviceUpdate) {
1343 checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE);
1344 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001345 }
1346
Eric Laurent3974e3b2017-12-07 17:58:43 -08001347 status_t status = stopSource(outputDesc, stream, forceDeviceUpdate);
1348
Eric Laurent733ce942017-12-07 12:18:25 -08001349 if (status == NO_ERROR ) {
1350 outputDesc->stop();
Eric Laurent3974e3b2017-12-07 17:58:43 -08001351 }
1352 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -07001353}
1354
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001355status_t AudioPolicyManager::stopSource(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001356 audio_stream_type_t stream,
1357 bool forceDeviceUpdate)
Eric Laurentc75307b2015-03-17 15:29:32 -07001358{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001359 // always handle stream stop, check which stream type is stopping
1360 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1361
Eric Laurente552edb2014-03-10 17:42:56 -07001362 // handle special case for sonification while in call
1363 if (isInCall()) {
1364 handleIncallSonification(stream, false, false);
1365 }
1366
1367 if (outputDesc->mRefCount[stream] > 0) {
1368 // decrement usage count of this stream on the output
1369 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001370
Eric Laurente552edb2014-03-10 17:42:56 -07001371 // store time at which the stream was stopped - see isStreamActive()
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001372 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
Eric Laurente552edb2014-03-10 17:42:56 -07001373 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001374 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001375 // delay the device switch by twice the latency because stopOutput() is executed when
1376 // the track stop() command is received and at that time the audio track buffer can
1377 // still contain data that needs to be drained. The latency only covers the audio HAL
1378 // and kernel buffers. Also the latency does not always include additional delay in the
1379 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001380 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001381
1382 // force restoring the device selection on other active outputs if it differs from the
1383 // one being selected for this output
Eric Laurent57de36c2016-09-28 16:59:11 -07001384 uint32_t delayMs = outputDesc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -07001385 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001386 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001387 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001388 desc->isActive() &&
1389 outputDesc->sharesHwModuleWith(desc) &&
1390 (newDevice != desc->device())) {
Haynes Mathew George11c499a2016-08-26 12:08:25 -07001391 audio_devices_t newDevice2 = getNewOutputDevice(desc, false /*fromCache*/);
1392 bool force = desc->device() != newDevice2;
Eric Laurentc75307b2015-03-17 15:29:32 -07001393 setOutputDevice(desc,
Haynes Mathew George11c499a2016-08-26 12:08:25 -07001394 newDevice2,
1395 force,
Eric Laurent57de36c2016-09-28 16:59:11 -07001396 delayMs);
1397 // re-apply device specific volume if not done by setOutputDevice()
1398 if (!force) {
1399 applyStreamVolumes(desc, newDevice2, delayMs);
1400 }
Eric Laurente552edb2014-03-10 17:42:56 -07001401 }
1402 }
1403 // update the outputs if stopping one with a stream that can affect notification routing
1404 handleNotificationRoutingForStream(stream);
1405 }
Tomoharu Kasaharab62d78b2018-01-18 20:55:02 +09001406
1407 if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE &&
1408 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
1409 setStrategyMute(STRATEGY_SONIFICATION, false, outputDesc);
1410 }
1411
Eric Laurent36829f92017-04-07 19:04:42 -07001412 if (stream == AUDIO_STREAM_MUSIC) {
1413 selectOutputForMusicEffects();
1414 }
Eric Laurente552edb2014-03-10 17:42:56 -07001415 return NO_ERROR;
1416 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001417 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001418 return INVALID_OPERATION;
1419 }
1420}
1421
Eric Laurente83b55d2014-11-14 10:06:21 -08001422void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001423 audio_stream_type_t stream __unused,
1424 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001425{
1426 ALOGV("releaseOutput() %d", output);
1427 ssize_t index = mOutputs.indexOfKey(output);
1428 if (index < 0) {
1429 ALOGW("releaseOutput() releasing unknown output %d", output);
1430 return;
1431 }
1432
Paul McLeanaa981192015-03-21 09:55:15 -07001433 // Routing
1434 mOutputRoutes.removeRoute(session);
1435
Eric Laurentc75307b2015-03-17 15:29:32 -07001436 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001437 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001438 if (desc->mDirectOpenCount <= 0) {
1439 ALOGW("releaseOutput() invalid open count %d for output %d",
1440 desc->mDirectOpenCount, output);
1441 return;
1442 }
1443 if (--desc->mDirectOpenCount == 0) {
1444 closeOutput(output);
Eric Laurentb52c1522014-05-20 11:27:36 -07001445 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001446 }
1447 }
1448}
1449
1450
Eric Laurentcaf7f482014-11-25 17:50:47 -08001451status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1452 audio_io_handle_t *input,
1453 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001454 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001455 const audio_config_base_t *config,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001456 audio_input_flags_t flags,
Eric Laurent2ac76942017-06-22 17:17:09 -07001457 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001458 input_type_t *inputType,
1459 audio_port_handle_t *portId)
Eric Laurente552edb2014-03-10 17:42:56 -07001460{
Glenn Kasten49f36ba2017-12-06 13:02:02 -08001461 ALOGV("getInputForAttr() source %d, sampling rate %d, format %#x, channel mask %#x,"
Eric Laurentcaf7f482014-11-25 17:50:47 -08001462 "session %d, flags %#x",
Eric Laurent20b9ef02016-12-05 11:03:16 -08001463 attr->source, config->sample_rate, config->format, config->channel_mask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001464
Eric Laurentad2e7b92017-09-14 20:06:42 -07001465 status_t status = NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -08001466 // handle legacy remote submix case where the address was not always specified
1467 String8 address = String8("");
Eric Laurentc447ded2015-01-06 08:47:05 -08001468 audio_source_t halInputSource;
Eric Laurentad2e7b92017-09-14 20:06:42 -07001469 audio_source_t inputSource = attr->source;
Eric Laurentc722f302014-12-10 11:21:49 -08001470 AudioMix *policyMix = NULL;
Eric Laurentad2e7b92017-09-14 20:06:42 -07001471 DeviceVector inputDevices;
Eric Laurent20b9ef02016-12-05 11:03:16 -08001472
Eric Laurentfe231122017-11-17 17:48:06 -08001473 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1474 inputSource = AUDIO_SOURCE_MIC;
1475 }
1476
Paul McLean466dc8e2015-04-17 13:15:36 -06001477 // Explicit routing?
1478 sp<DeviceDescriptor> deviceDesc;
Eric Laurent2ac76942017-06-22 17:17:09 -07001479 if (*selectedDeviceId != AUDIO_PORT_HANDLE_NONE) {
jiabinc0c831a2018-01-29 17:55:15 -08001480 deviceDesc = mAvailableInputDevices.getDeviceFromId(*selectedDeviceId);
Paul McLean466dc8e2015-04-17 13:15:36 -06001481 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001482 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001483
Eric Laurentad2e7b92017-09-14 20:06:42 -07001484 // special case for mmap capture: if an input IO handle is specified, we reuse this input if
1485 // possible
1486 if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) == AUDIO_INPUT_FLAG_MMAP_NOIRQ &&
1487 *input != AUDIO_IO_HANDLE_NONE) {
1488 ssize_t index = mInputs.indexOfKey(*input);
1489 if (index < 0) {
1490 ALOGW("getInputForAttr() unknown MMAP input %d", *input);
1491 status = BAD_VALUE;
1492 goto error;
1493 }
1494 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1495 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1496 if (audioSession == 0) {
1497 ALOGW("getInputForAttr() unknown session %d on input %d", session, *input);
1498 status = BAD_VALUE;
1499 goto error;
1500 }
1501 // For MMAP mode, the first call to getInputForAttr() is made on behalf of audioflinger.
1502 // The second call is for the first active client and sets the UID. Any further call
Eric Laurent331679c2018-04-16 17:03:16 -07001503 // corresponds to a new client and is only permitted from the same UID.
1504 // If the first UID is silenced, allow a new UID connection and replace with new UID
Eric Laurentad2e7b92017-09-14 20:06:42 -07001505 if (audioSession->openCount() == 1) {
1506 audioSession->setUid(uid);
1507 } else if (audioSession->uid() != uid) {
Eric Laurent331679c2018-04-16 17:03:16 -07001508 if (!audioSession->isSilenced()) {
1509 ALOGW("getInputForAttr() bad uid %d for session %d uid %d",
1510 uid, session, audioSession->uid());
1511 status = INVALID_OPERATION;
1512 goto error;
1513 }
1514 audioSession->setUid(uid);
1515 audioSession->setSilenced(false);
Eric Laurentad2e7b92017-09-14 20:06:42 -07001516 }
1517 audioSession->changeOpenCount(1);
1518 *inputType = API_INPUT_LEGACY;
1519 if (*portId == AUDIO_PORT_HANDLE_NONE) {
1520 *portId = AudioPort::getNextUniqueId();
1521 }
1522 inputDevices = mAvailableInputDevices.getDevicesFromType(inputDesc->mDevice);
1523 *selectedDeviceId = inputDevices.size() > 0 ? inputDevices.itemAt(0)->getId()
1524 : AUDIO_PORT_HANDLE_NONE;
1525 ALOGI("%s reusing MMAP input %d for session %d", __FUNCTION__, *input, session);
1526
1527 return NO_ERROR;
1528 }
1529
1530 *input = AUDIO_IO_HANDLE_NONE;
1531 *inputType = API_INPUT_INVALID;
1532
Eric Laurentad2e7b92017-09-14 20:06:42 -07001533 halInputSource = inputSource;
1534
1535 // TODO: check for existing client for this port ID
1536 if (*portId == AUDIO_PORT_HANDLE_NONE) {
1537 *portId = AudioPort::getNextUniqueId();
1538 }
1539
1540 audio_devices_t device;
1541
Eric Laurentc447ded2015-01-06 08:47:05 -08001542 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001543 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Eric Laurentad2e7b92017-09-14 20:06:42 -07001544 status = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
1545 if (status != NO_ERROR) {
1546 goto error;
François Gaffie036e1e92015-03-19 10:16:24 +01001547 }
1548 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001549 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1550 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001551 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001552 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001553 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001554 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurentad2e7b92017-09-14 20:06:42 -07001555 status = BAD_VALUE;
1556 goto error;
Eric Laurent275e8e92014-11-30 15:14:47 -08001557 }
Eric Laurentc722f302014-12-10 11:21:49 -08001558 if (policyMix != NULL) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001559 address = policyMix->mDeviceAddress;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001560 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1561 // there is an external policy, but this input is attached to a mix of recorders,
1562 // meaning it receives audio injected into the framework, so the recorder doesn't
1563 // know about it and is therefore considered "legacy"
1564 *inputType = API_INPUT_LEGACY;
1565 } else {
1566 // recording a mix of players defined by an external policy, we're rerouting for
1567 // an external policy
1568 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1569 }
Eric Laurentc722f302014-12-10 11:21:49 -08001570 } else if (audio_is_remote_submix_device(device)) {
1571 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001572 *inputType = API_INPUT_MIX_CAPTURE;
Eric Laurent82db2692015-08-07 13:59:42 -07001573 } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
1574 *inputType = API_INPUT_TELEPHONY_RX;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001575 } else {
1576 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001577 }
Ravi Kumar Alamandab367f5b2015-08-25 08:21:37 -07001578
Eric Laurent599c7582015-12-07 18:05:55 -08001579 }
1580
1581 *input = getInputForDevice(device, address, session, uid, inputSource,
Eric Laurentfe231122017-11-17 17:48:06 -08001582 config, flags,
Eric Laurent599c7582015-12-07 18:05:55 -08001583 policyMix);
1584 if (*input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentad2e7b92017-09-14 20:06:42 -07001585 status = INVALID_OPERATION;
1586 goto error;
Eric Laurent599c7582015-12-07 18:05:55 -08001587 }
Eric Laurent20b9ef02016-12-05 11:03:16 -08001588
Eric Laurentad2e7b92017-09-14 20:06:42 -07001589 inputDevices = mAvailableInputDevices.getDevicesFromType(device);
Eric Laurent2ac76942017-06-22 17:17:09 -07001590 *selectedDeviceId = inputDevices.size() > 0 ? inputDevices.itemAt(0)->getId()
1591 : AUDIO_PORT_HANDLE_NONE;
1592
1593 ALOGV("getInputForAttr() returns input %d type %d selectedDeviceId %d",
1594 *input, *inputType, *selectedDeviceId);
1595
Eric Laurent599c7582015-12-07 18:05:55 -08001596 return NO_ERROR;
Eric Laurentad2e7b92017-09-14 20:06:42 -07001597
1598error:
1599 mInputRoutes.removeRoute(session);
1600 return status;
Eric Laurent599c7582015-12-07 18:05:55 -08001601}
1602
1603
1604audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device,
1605 String8 address,
1606 audio_session_t session,
1607 uid_t uid,
1608 audio_source_t inputSource,
Eric Laurentfe231122017-11-17 17:48:06 -08001609 const audio_config_base_t *config,
Eric Laurent599c7582015-12-07 18:05:55 -08001610 audio_input_flags_t flags,
1611 AudioMix *policyMix)
1612{
1613 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
1614 audio_source_t halInputSource = inputSource;
1615 bool isSoundTrigger = false;
1616
1617 if (inputSource == AUDIO_SOURCE_HOTWORD) {
1618 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1619 if (index >= 0) {
1620 input = mSoundTriggerSessions.valueFor(session);
1621 isSoundTrigger = true;
1622 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1623 ALOGV("SoundTrigger capture on session %d input %d", session, input);
1624 } else {
1625 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001626 }
Haynes Mathew George851d3ff2017-06-19 20:01:57 -07001627 } else if (inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION &&
Eric Laurentfe231122017-11-17 17:48:06 -08001628 audio_is_linear_pcm(config->format)) {
Haynes Mathew George851d3ff2017-06-19 20:01:57 -07001629 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_VOIP_TX);
Eric Laurent5dbe4712014-09-19 19:04:57 -07001630 }
1631
Andy Hungf129b032015-04-07 13:45:50 -07001632 // find a compatible input profile (not necessarily identical in parameters)
1633 sp<IOProfile> profile;
Eric Laurentfe231122017-11-17 17:48:06 -08001634 // sampling rate and flags may be updated by getInputProfile
1635 uint32_t profileSamplingRate = (config->sample_rate == 0) ?
1636 SAMPLE_RATE_HZ_DEFAULT : config->sample_rate;
Glenn Kasten730b9262018-03-29 15:01:26 -07001637 audio_format_t profileFormat;
Eric Laurentfe231122017-11-17 17:48:06 -08001638 audio_channel_mask_t profileChannelMask = config->channel_mask;
Andy Hungf129b032015-04-07 13:45:50 -07001639 audio_input_flags_t profileFlags = flags;
1640 for (;;) {
Glenn Kasten730b9262018-03-29 15:01:26 -07001641 profileFormat = config->format; // reset each time through loop, in case it is updated
Eric Laurent275e8e92014-11-30 15:14:47 -08001642 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001643 profileSamplingRate, profileFormat, profileChannelMask,
1644 profileFlags);
1645 if (profile != 0) {
1646 break; // success
Eric Laurent05067782016-06-01 18:27:28 -07001647 } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) {
1648 profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry
Andy Hungf129b032015-04-07 13:45:50 -07001649 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1650 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1651 } else { // fail
Glenn Kastenfaa10a62017-05-25 15:52:05 -07001652 ALOGW("getInputForDevice() could not find profile for device 0x%X, "
Eric Laurentfe231122017-11-17 17:48:06 -08001653 "sampling rate %u, format %#x, channel mask 0x%X, flags %#x",
1654 device, config->sample_rate, config->format, config->channel_mask, flags);
Eric Laurent599c7582015-12-07 18:05:55 -08001655 return input;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001656 }
Eric Laurente552edb2014-03-10 17:42:56 -07001657 }
Glenn Kasten05ddca52016-02-11 08:17:12 -08001658 // Pick input sampling rate if not specified by client
Eric Laurentfe231122017-11-17 17:48:06 -08001659 uint32_t samplingRate = config->sample_rate;
Glenn Kasten05ddca52016-02-11 08:17:12 -08001660 if (samplingRate == 0) {
1661 samplingRate = profileSamplingRate;
1662 }
Eric Laurente552edb2014-03-10 17:42:56 -07001663
Eric Laurent322b4d22015-04-03 15:57:54 -07001664 if (profile->getModuleHandle() == 0) {
1665 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurent599c7582015-12-07 18:05:55 -08001666 return input;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001667 }
1668
Eric Laurent599c7582015-12-07 18:05:55 -08001669 sp<AudioSession> audioSession = new AudioSession(session,
Eric Laurentfe231122017-11-17 17:48:06 -08001670 inputSource,
1671 config->format,
1672 samplingRate,
1673 config->channel_mask,
1674 flags,
1675 uid,
1676 isSoundTrigger,
1677 policyMix, mpClientInterface);
Eric Laurent599c7582015-12-07 18:05:55 -08001678
Eric Laurent74708e72017-04-07 17:13:42 -07001679// FIXME: disable concurrent capture until UI is ready
1680#if 0
Eric Laurent599c7582015-12-07 18:05:55 -08001681 // reuse an open input if possible
Eric Laurent555530a2017-02-07 18:17:24 -08001682 sp<AudioInputDescriptor> reusedInputDesc;
Eric Laurent599c7582015-12-07 18:05:55 -08001683 for (size_t i = 0; i < mInputs.size(); i++) {
1684 sp<AudioInputDescriptor> desc = mInputs.valueAt(i);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001685 // reuse input if:
1686 // - it shares the same profile
1687 // AND
1688 // - it is not a reroute submix input
1689 // AND
1690 // - it is: not used for sound trigger
1691 // OR
1692 // used for sound trigger and all clients use the same session ID
1693 //
1694 if ((profile == desc->mProfile) &&
1695 (isSoundTrigger == desc->isSoundTrigger()) &&
1696 !is_virtual_input_device(device)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001697
1698 sp<AudioSession> as = desc->getAudioSession(session);
1699 if (as != 0) {
1700 // do not allow unmatching properties on same session
1701 if (as->matches(audioSession)) {
1702 as->changeOpenCount(1);
1703 } else {
1704 ALOGW("getInputForDevice() record with different attributes"
1705 " exists for session %d", session);
Eric Laurent555530a2017-02-07 18:17:24 -08001706 continue;
Eric Laurent599c7582015-12-07 18:05:55 -08001707 }
Eric Laurentfb66dd92016-01-28 18:32:03 -08001708 } else if (isSoundTrigger) {
Eric Laurent555530a2017-02-07 18:17:24 -08001709 continue;
Eric Laurentfb66dd92016-01-28 18:32:03 -08001710 }
Eric Laurentbb948092017-01-23 18:33:30 -08001711
Eric Laurent555530a2017-02-07 18:17:24 -08001712 // Reuse the already opened input stream on this profile if:
1713 // - the new capture source is background OR
1714 // - the path requested configurations match OR
1715 // - the new source priority is less than the highest source priority on this input
1716 // If the input stream cannot be reused, close it before opening a new stream
1717 // on the same profile for the new client so that the requested path configuration
1718 // can be selected.
1719 if (!isConcurrentSource(inputSource) &&
Eric Laurentbb948092017-01-23 18:33:30 -08001720 ((desc->mSamplingRate != samplingRate ||
Eric Laurentfe231122017-11-17 17:48:06 -08001721 desc->mChannelMask != config->channel_mask ||
1722 !audio_formats_match(desc->mFormat, config->format)) &&
Eric Laurentfb66dd92016-01-28 18:32:03 -08001723 (source_priority(desc->getHighestPrioritySource(false /*activeOnly*/)) <
Eric Laurentbb948092017-01-23 18:33:30 -08001724 source_priority(inputSource)))) {
Eric Laurent555530a2017-02-07 18:17:24 -08001725 reusedInputDesc = desc;
1726 continue;
Eric Laurent599c7582015-12-07 18:05:55 -08001727 } else {
1728 desc->addAudioSession(session, audioSession);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001729 ALOGV("%s: reusing input %d", __FUNCTION__, mInputs.keyAt(i));
1730 return mInputs.keyAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08001731 }
Eric Laurent599c7582015-12-07 18:05:55 -08001732 }
1733 }
Eric Laurent599c7582015-12-07 18:05:55 -08001734
Eric Laurent555530a2017-02-07 18:17:24 -08001735 if (reusedInputDesc != 0) {
1736 AudioSessionCollection sessions = reusedInputDesc->getAudioSessions(false /*activeOnly*/);
1737 for (size_t j = 0; j < sessions.size(); j++) {
1738 audio_session_t currentSession = sessions.keyAt(j);
1739 stopInput(reusedInputDesc->mIoHandle, currentSession);
1740 releaseInput(reusedInputDesc->mIoHandle, currentSession);
1741 }
1742 }
Eric Laurent74708e72017-04-07 17:13:42 -07001743#endif
Eric Laurent555530a2017-02-07 18:17:24 -08001744
Eric Laurent3974e3b2017-12-07 17:58:43 -08001745 if (!profile->canOpenNewIo()) {
1746 return AUDIO_IO_HANDLE_NONE;
1747 }
1748
Eric Laurentfe231122017-11-17 17:48:06 -08001749 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile, mpClientInterface);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001750
Eric Laurentfe231122017-11-17 17:48:06 -08001751 audio_config_t lConfig = AUDIO_CONFIG_INITIALIZER;
1752 lConfig.sample_rate = profileSamplingRate;
1753 lConfig.channel_mask = profileChannelMask;
1754 lConfig.format = profileFormat;
Eric Laurente3014102017-05-03 11:15:43 -07001755
Eric Laurent53b810e2017-12-10 17:25:10 -08001756 if (address == "") {
1757 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(device);
1758 // the inputs vector must be of size >= 1, but we don't want to crash here
1759 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress : String8("");
1760 }
1761
Eric Laurentfe231122017-11-17 17:48:06 -08001762 status_t status = inputDesc->open(&lConfig, device, address,
1763 halInputSource, profileFlags, &input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001764
1765 // only accept input with the exact requested set of parameters
Eric Laurent599c7582015-12-07 18:05:55 -08001766 if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE ||
Eric Laurentfe231122017-11-17 17:48:06 -08001767 (profileSamplingRate != lConfig.sample_rate) ||
1768 !audio_formats_match(profileFormat, lConfig.format) ||
1769 (profileChannelMask != lConfig.channel_mask)) {
1770 ALOGW("getInputForAttr() failed opening input: sampling rate %d"
Glenn Kasten49f36ba2017-12-06 13:02:02 -08001771 ", format %#x, channel mask %#x",
Eric Laurentfe231122017-11-17 17:48:06 -08001772 profileSamplingRate, profileFormat, profileChannelMask);
Eric Laurent599c7582015-12-07 18:05:55 -08001773 if (input != AUDIO_IO_HANDLE_NONE) {
Eric Laurentfe231122017-11-17 17:48:06 -08001774 inputDesc->close();
Eric Laurentcf2c0212014-07-25 16:20:43 -07001775 }
Eric Laurent599c7582015-12-07 18:05:55 -08001776 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001777 }
1778
Eric Laurentc722f302014-12-10 11:21:49 -08001779 inputDesc->mPolicyMix = policyMix;
Eric Laurent599c7582015-12-07 18:05:55 -08001780 inputDesc->addAudioSession(session, audioSession);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001781
Eric Laurent599c7582015-12-07 18:05:55 -08001782 addInput(input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001783 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001784
Eric Laurent599c7582015-12-07 18:05:55 -08001785 return input;
Eric Laurente552edb2014-03-10 17:42:56 -07001786}
1787
Eric Laurentbb948092017-01-23 18:33:30 -08001788//static
1789bool AudioPolicyManager::isConcurrentSource(audio_source_t source)
1790{
1791 return (source == AUDIO_SOURCE_HOTWORD) ||
1792 (source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
1793 (source == AUDIO_SOURCE_FM_TUNER);
1794}
1795
Eric Laurentfb66dd92016-01-28 18:32:03 -08001796bool AudioPolicyManager::isConcurentCaptureAllowed(const sp<AudioInputDescriptor>& inputDesc,
1797 const sp<AudioSession>& audioSession)
1798{
1799 // Do not allow capture if an active voice call is using a software patch and
1800 // the call TX source device is on the same HW module.
1801 // FIXME: would be better to refine to only inputs whose profile connects to the
1802 // call TX device but this information is not in the audio patch
1803 if (mCallTxPatch != 0 &&
1804 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1805 return false;
1806 }
1807
1808 // starting concurrent capture is enabled if:
1809 // 1) capturing for re-routing
1810 // 2) capturing for HOTWORD source
1811 // 3) capturing for FM TUNER source
1812 // 3) All other active captures are either for re-routing or HOTWORD
1813
1814 if (is_virtual_input_device(inputDesc->mDevice) ||
Eric Laurentbb948092017-01-23 18:33:30 -08001815 isConcurrentSource(audioSession->inputSource())) {
Eric Laurentfb66dd92016-01-28 18:32:03 -08001816 return true;
1817 }
1818
Mikhail Naganovcf84e592017-12-07 11:25:11 -08001819 for (const auto& activeInput : mInputs.getActiveInputs()) {
Eric Laurentbb948092017-01-23 18:33:30 -08001820 if (!isConcurrentSource(activeInput->inputSource(true)) &&
Eric Laurentfb66dd92016-01-28 18:32:03 -08001821 !is_virtual_input_device(activeInput->mDevice)) {
1822 return false;
1823 }
1824 }
1825
1826 return true;
1827}
1828
Chris Thornton2b864642017-06-27 21:26:07 -07001829// FIXME: remove when concurrent capture is ready. This is a hack to work around bug b/63083537.
1830bool AudioPolicyManager::soundTriggerSupportsConcurrentCapture() {
1831 if (!mHasComputedSoundTriggerSupportsConcurrentCapture) {
1832 bool soundTriggerSupportsConcurrentCapture = false;
1833 unsigned int numModules = 0;
1834 struct sound_trigger_module_descriptor* nModules = NULL;
1835
1836 status_t status = SoundTrigger::listModules(nModules, &numModules);
1837 if (status == NO_ERROR && numModules != 0) {
1838 nModules = (struct sound_trigger_module_descriptor*) calloc(
1839 numModules, sizeof(struct sound_trigger_module_descriptor));
1840 if (nModules == NULL) {
1841 // We failed to malloc the buffer, so just say no for now, and hope that we have more
1842 // ram the next time this function is called.
1843 ALOGE("Failed to allocate buffer for module descriptors");
1844 return false;
1845 }
1846
1847 status = SoundTrigger::listModules(nModules, &numModules);
1848 if (status == NO_ERROR) {
1849 soundTriggerSupportsConcurrentCapture = true;
1850 for (size_t i = 0; i < numModules; ++i) {
1851 soundTriggerSupportsConcurrentCapture &=
1852 nModules[i].properties.concurrent_capture;
1853 }
1854 }
1855 free(nModules);
1856 }
1857 mSoundTriggerSupportsConcurrentCapture = soundTriggerSupportsConcurrentCapture;
1858 mHasComputedSoundTriggerSupportsConcurrentCapture = true;
1859 }
1860 return mSoundTriggerSupportsConcurrentCapture;
1861}
1862
Eric Laurentfb66dd92016-01-28 18:32:03 -08001863
Eric Laurent4dc68062014-07-28 17:26:49 -07001864status_t AudioPolicyManager::startInput(audio_io_handle_t input,
Eric Laurentfb66dd92016-01-28 18:32:03 -08001865 audio_session_t session,
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001866 bool silenced,
Eric Laurentfb66dd92016-01-28 18:32:03 -08001867 concurrency_type__mask_t *concurrency)
Eric Laurente552edb2014-03-10 17:42:56 -07001868{
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001869
1870 ALOGV("AudioPolicyManager::startInput(input:%d, session:%d, silenced:%d, concurrency:%d)",
1871 input, session, silenced, *concurrency);
1872
Eric Laurentfb66dd92016-01-28 18:32:03 -08001873 *concurrency = API_INPUT_CONCURRENCY_NONE;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001874
Eric Laurente552edb2014-03-10 17:42:56 -07001875 ssize_t index = mInputs.indexOfKey(input);
1876 if (index < 0) {
1877 ALOGW("startInput() unknown input %d", input);
1878 return BAD_VALUE;
1879 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001880 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001881
Eric Laurent599c7582015-12-07 18:05:55 -08001882 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1883 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07001884 ALOGW("startInput() unknown session %d on input %d", session, input);
1885 return BAD_VALUE;
1886 }
1887
Eric Laurent74708e72017-04-07 17:13:42 -07001888// FIXME: disable concurrent capture until UI is ready
1889#if 0
Eric Laurentfb66dd92016-01-28 18:32:03 -08001890 if (!isConcurentCaptureAllowed(inputDesc, audioSession)) {
1891 ALOGW("startInput(%d) failed: other input already started", input);
1892 return INVALID_OPERATION;
1893 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001894
Eric Laurentfb66dd92016-01-28 18:32:03 -08001895 if (isInCall()) {
1896 *concurrency |= API_INPUT_CONCURRENCY_CALL;
1897 }
Eric Laurentf7c50102016-09-30 15:19:43 -07001898 if (mInputs.activeInputsCountOnDevices() != 0) {
Eric Laurentfb66dd92016-01-28 18:32:03 -08001899 *concurrency |= API_INPUT_CONCURRENCY_CAPTURE;
Eric Laurente552edb2014-03-10 17:42:56 -07001900 }
Eric Laurent74708e72017-04-07 17:13:42 -07001901#else
1902 if (!is_virtual_input_device(inputDesc->mDevice)) {
1903 if (mCallTxPatch != 0 &&
1904 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1905 ALOGW("startInput(%d) failed: call in progress", input);
Ray Essick84e84a52018-05-03 18:45:07 -07001906 *concurrency |= API_INPUT_CONCURRENCY_CALL;
Eric Laurent74708e72017-04-07 17:13:42 -07001907 return INVALID_OPERATION;
1908 }
1909
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001910 Vector<sp<AudioInputDescriptor>> activeInputs = mInputs.getActiveInputs();
Eric Laurent74708e72017-04-07 17:13:42 -07001911
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001912 // If a UID is idle and records silence and another not silenced recording starts
1913 // from another UID (idle or active) we stop the current idle UID recording in
1914 // favor of the new one - "There can be only one" TM
1915 if (!silenced) {
1916 for (const auto& activeDesc : activeInputs) {
1917 if ((audioSession->flags() & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0 &&
1918 activeDesc->getId() == inputDesc->getId()) {
1919 continue;
1920 }
1921
1922 AudioSessionCollection activeSessions = activeDesc->getAudioSessions(
1923 true /*activeOnly*/);
1924 sp<AudioSession> activeSession = activeSessions.valueAt(0);
1925 if (activeSession->isSilenced()) {
1926 audio_io_handle_t activeInput = activeDesc->mIoHandle;
1927 audio_session_t activeSessionId = activeSession->session();
1928 stopInput(activeInput, activeSessionId);
1929 releaseInput(activeInput, activeSessionId);
1930 ALOGV("startInput(%d) stopping silenced input %d", input, activeInput);
1931 activeInputs = mInputs.getActiveInputs();
1932 }
1933 }
1934 }
1935
1936 for (const auto& activeDesc : activeInputs) {
Eric Laurentcb4dae22017-07-01 19:39:32 -07001937 if ((audioSession->flags() & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0 &&
1938 activeDesc->getId() == inputDesc->getId()) {
1939 continue;
1940 }
1941
Eric Laurent74708e72017-04-07 17:13:42 -07001942 audio_source_t activeSource = activeDesc->inputSource(true);
1943 if (audioSession->inputSource() == AUDIO_SOURCE_HOTWORD) {
1944 if (activeSource == AUDIO_SOURCE_HOTWORD) {
1945 if (activeDesc->hasPreemptedSession(session)) {
1946 ALOGW("startInput(%d) failed for HOTWORD: "
1947 "other input %d already started for HOTWORD",
1948 input, activeDesc->mIoHandle);
Ray Essick84e84a52018-05-03 18:45:07 -07001949 *concurrency |= API_INPUT_CONCURRENCY_HOTWORD;
Eric Laurent74708e72017-04-07 17:13:42 -07001950 return INVALID_OPERATION;
1951 }
1952 } else {
1953 ALOGV("startInput(%d) failed for HOTWORD: other input %d already started",
1954 input, activeDesc->mIoHandle);
Ray Essick84e84a52018-05-03 18:45:07 -07001955 *concurrency |= API_INPUT_CONCURRENCY_CAPTURE;
Eric Laurent74708e72017-04-07 17:13:42 -07001956 return INVALID_OPERATION;
1957 }
1958 } else {
1959 if (activeSource != AUDIO_SOURCE_HOTWORD) {
1960 ALOGW("startInput(%d) failed: other input %d already started",
1961 input, activeDesc->mIoHandle);
Ray Essick84e84a52018-05-03 18:45:07 -07001962 *concurrency |= API_INPUT_CONCURRENCY_CAPTURE;
Eric Laurent74708e72017-04-07 17:13:42 -07001963 return INVALID_OPERATION;
1964 }
1965 }
1966 }
1967
Chris Thornton2b864642017-06-27 21:26:07 -07001968 // We only need to check if the sound trigger session supports concurrent capture if the
1969 // input is also a sound trigger input. Otherwise, we should preempt any hotword stream
1970 // that's running.
1971 const bool allowConcurrentWithSoundTrigger =
1972 inputDesc->isSoundTrigger() ? soundTriggerSupportsConcurrentCapture() : false;
1973
Eric Laurent74708e72017-04-07 17:13:42 -07001974 // if capture is allowed, preempt currently active HOTWORD captures
Mikhail Naganovcf84e592017-12-07 11:25:11 -08001975 for (const auto& activeDesc : activeInputs) {
Chris Thornton2b864642017-06-27 21:26:07 -07001976 if (allowConcurrentWithSoundTrigger && activeDesc->isSoundTrigger()) {
1977 continue;
1978 }
1979
Eric Laurent74708e72017-04-07 17:13:42 -07001980 audio_source_t activeSource = activeDesc->inputSource(true);
1981 if (activeSource == AUDIO_SOURCE_HOTWORD) {
1982 AudioSessionCollection activeSessions =
1983 activeDesc->getAudioSessions(true /*activeOnly*/);
1984 audio_session_t activeSession = activeSessions.keyAt(0);
1985 audio_io_handle_t activeHandle = activeDesc->mIoHandle;
1986 SortedVector<audio_session_t> sessions = activeDesc->getPreemptedSessions();
Ray Essick84e84a52018-05-03 18:45:07 -07001987 *concurrency |= API_INPUT_CONCURRENCY_PREEMPT;
Eric Laurent74708e72017-04-07 17:13:42 -07001988 sessions.add(activeSession);
1989 inputDesc->setPreemptedSessions(sessions);
1990 stopInput(activeHandle, activeSession);
1991 releaseInput(activeHandle, activeSession);
1992 ALOGV("startInput(%d) for HOTWORD preempting HOTWORD input %d",
1993 input, activeDesc->mIoHandle);
1994 }
1995 }
1996 }
1997#endif
Eric Laurente552edb2014-03-10 17:42:56 -07001998
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001999 // Make sure we start with the correct silence state
2000 audioSession->setSilenced(silenced);
2001
Eric Laurent313d1e72016-01-29 09:56:57 -08002002 // increment activity count before calling getNewInputDevice() below as only active sessions
2003 // are considered for device selection
2004 audioSession->changeActiveCount(1);
2005
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002006 // Routing?
2007 mInputRoutes.incRouteActivity(session);
2008
Eric Laurent2157f5b2018-04-25 18:33:02 -07002009 if (audioSession->activeCount() == 1 || mInputRoutes.getAndClearRouteChanged(session)) {
Eric Laurent271a93e2016-09-29 14:47:06 -07002010 // indicate active capture to sound trigger service if starting capture from a mic on
2011 // primary HW module
Eric Laurentf7c50102016-09-30 15:19:43 -07002012 audio_devices_t device = getNewInputDevice(inputDesc);
Eric Laurent271a93e2016-09-29 14:47:06 -07002013 setInputDevice(input, device, true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07002014
Eric Laurent733ce942017-12-07 12:18:25 -08002015 status_t status = inputDesc->start();
2016 if (status != NO_ERROR) {
2017 mInputRoutes.decRouteActivity(session);
2018 audioSession->changeActiveCount(-1);
2019 return status;
2020 }
Eric Laurent3974e3b2017-12-07 17:58:43 -08002021
Eric Laurent733ce942017-12-07 12:18:25 -08002022 if (inputDesc->getAudioSessionCount(true/*activeOnly*/) == 1) {
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07002023 // if input maps to a dynamic policy with an activity listener, notify of state change
2024 if ((inputDesc->mPolicyMix != NULL)
2025 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
2026 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
2027 MIX_STATE_MIXING);
Eric Laurentc722f302014-12-10 11:21:49 -08002028 }
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07002029
Eric Laurentf7c50102016-09-30 15:19:43 -07002030 audio_devices_t primaryInputDevices = availablePrimaryInputDevices();
2031 if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) &&
Eric Laurent05b345e2016-11-18 12:36:27 -08002032 mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) {
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07002033 SoundTrigger::setCaptureState(true);
2034 }
2035
2036 // automatically enable the remote submix output when input is started if not
2037 // used by a policy mix of type MIX_TYPE_RECORDERS
2038 // For remote submix (a virtual device), we open only one input per capture request.
2039 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
2040 String8 address = String8("");
2041 if (inputDesc->mPolicyMix == NULL) {
2042 address = String8("0");
2043 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
2044 address = inputDesc->mPolicyMix->mDeviceAddress;
2045 }
2046 if (address != "") {
2047 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2048 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2049 address, "remote-submix");
2050 }
Eric Laurentc722f302014-12-10 11:21:49 -08002051 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07002052 }
Eric Laurente552edb2014-03-10 17:42:56 -07002053 }
2054
Eric Laurent599c7582015-12-07 18:05:55 -08002055 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Eric Laurente552edb2014-03-10 17:42:56 -07002056
Eric Laurente552edb2014-03-10 17:42:56 -07002057 return NO_ERROR;
2058}
2059
Eric Laurent4dc68062014-07-28 17:26:49 -07002060status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
2061 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07002062{
2063 ALOGV("stopInput() input %d", input);
2064 ssize_t index = mInputs.indexOfKey(input);
2065 if (index < 0) {
2066 ALOGW("stopInput() unknown input %d", input);
2067 return BAD_VALUE;
2068 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07002069 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07002070
Eric Laurent599c7582015-12-07 18:05:55 -08002071 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07002072 if (index < 0) {
2073 ALOGW("stopInput() unknown session %d on input %d", session, input);
2074 return BAD_VALUE;
2075 }
2076
Eric Laurent599c7582015-12-07 18:05:55 -08002077 if (audioSession->activeCount() == 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07002078 ALOGW("stopInput() input %d already stopped", input);
2079 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002080 }
2081
Eric Laurent599c7582015-12-07 18:05:55 -08002082 audioSession->changeActiveCount(-1);
Paul McLean466dc8e2015-04-17 13:15:36 -06002083
2084 // Routing?
2085 mInputRoutes.decRouteActivity(session);
2086
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07002087 if (audioSession->activeCount() == 0) {
Eric Laurent733ce942017-12-07 12:18:25 -08002088 inputDesc->stop();
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07002089 if (inputDesc->isActive()) {
2090 setInputDevice(input, getNewInputDevice(inputDesc), false /* force */);
2091 } else {
2092 // if input maps to a dynamic policy with an activity listener, notify of state change
2093 if ((inputDesc->mPolicyMix != NULL)
2094 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
2095 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
2096 MIX_STATE_IDLE);
Eric Laurent84332aa2016-01-28 22:19:18 +00002097 }
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07002098
2099 // automatically disable the remote submix output when input is stopped if not
2100 // used by a policy mix of type MIX_TYPE_RECORDERS
2101 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
2102 String8 address = String8("");
2103 if (inputDesc->mPolicyMix == NULL) {
2104 address = String8("0");
2105 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
2106 address = inputDesc->mPolicyMix->mDeviceAddress;
2107 }
2108 if (address != "") {
2109 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2110 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2111 address, "remote-submix");
2112 }
Eric Laurent84332aa2016-01-28 22:19:18 +00002113 }
Eric Laurent84332aa2016-01-28 22:19:18 +00002114
Eric Laurentf7c50102016-09-30 15:19:43 -07002115 audio_devices_t device = inputDesc->mDevice;
Eric Laurentfb66dd92016-01-28 18:32:03 -08002116 resetInputDevice(input);
Eric Laurent84332aa2016-01-28 22:19:18 +00002117
Eric Laurentf7c50102016-09-30 15:19:43 -07002118 // indicate inactive capture to sound trigger service if stopping capture from a mic on
2119 // primary HW module
2120 audio_devices_t primaryInputDevices = availablePrimaryInputDevices();
2121 if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) &&
2122 mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) {
Eric Laurentfb66dd92016-01-28 18:32:03 -08002123 SoundTrigger::setCaptureState(false);
2124 }
2125 inputDesc->clearPreemptedSessions();
Eric Laurent84332aa2016-01-28 22:19:18 +00002126 }
Eric Laurente552edb2014-03-10 17:42:56 -07002127 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002128 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07002129}
2130
Eric Laurent4dc68062014-07-28 17:26:49 -07002131void AudioPolicyManager::releaseInput(audio_io_handle_t input,
2132 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07002133{
2134 ALOGV("releaseInput() %d", input);
2135 ssize_t index = mInputs.indexOfKey(input);
2136 if (index < 0) {
2137 ALOGW("releaseInput() releasing unknown input %d", input);
2138 return;
2139 }
Paul McLean466dc8e2015-04-17 13:15:36 -06002140
2141 // Routing
2142 mInputRoutes.removeRoute(session);
2143
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002144 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
2145 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07002146
Eric Laurent599c7582015-12-07 18:05:55 -08002147 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
vivek mehta587b8df2017-01-31 14:58:44 -08002148 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07002149 ALOGW("releaseInput() unknown session %d on input %d", session, input);
2150 return;
2151 }
Eric Laurent599c7582015-12-07 18:05:55 -08002152
2153 if (audioSession->openCount() == 0) {
2154 ALOGW("releaseInput() invalid open count %d on session %d",
2155 audioSession->openCount(), session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002156 return;
2157 }
Eric Laurent599c7582015-12-07 18:05:55 -08002158
2159 if (audioSession->changeOpenCount(-1) == 0) {
2160 inputDesc->removeAudioSession(session);
2161 }
2162
Jean-Michel Trivi65bfe912015-12-04 15:56:47 -08002163 if (inputDesc->getOpenRefCount() > 0) {
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002164 ALOGV("releaseInput() exit > 0");
2165 return;
2166 }
2167
Eric Laurent05b90f82014-08-27 15:32:29 -07002168 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07002169 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07002170 ALOGV("releaseInput() exit");
2171}
2172
Eric Laurentd4692962014-05-05 18:13:44 -07002173void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07002174 bool patchRemoved = false;
2175
Mikhail Naganov7e22e942017-12-07 10:04:29 -08002176 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07002177 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08002178 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07002179 if (patch_index >= 0) {
2180 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07002181 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07002182 mAudioPatches.removeItemsAt(patch_index);
2183 patchRemoved = true;
2184 }
Eric Laurentfe231122017-11-17 17:48:06 -08002185 inputDesc->close();
Eric Laurentd4692962014-05-05 18:13:44 -07002186 }
jiabin829a00b2018-04-04 16:28:32 -07002187 mInputRoutes.clear();
Eric Laurentd4692962014-05-05 18:13:44 -07002188 mInputs.clear();
Chris Thornton52785f32016-02-09 17:28:49 -08002189 SoundTrigger::setCaptureState(false);
Eric Laurent6a94d692014-05-20 11:18:06 -07002190 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07002191
2192 if (patchRemoved) {
2193 mpClientInterface->onAudioPatchListUpdate();
2194 }
Eric Laurentd4692962014-05-05 18:13:44 -07002195}
2196
Eric Laurente0720872014-03-11 09:30:41 -07002197void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07002198 int indexMin,
2199 int indexMax)
2200{
2201 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002202 mVolumeCurves->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent28d09f02016-03-08 10:43:05 -08002203
2204 // initialize other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08002205 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
2206 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002207 continue;
2208 }
2209 mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08002210 }
Eric Laurente552edb2014-03-10 17:42:56 -07002211}
2212
Eric Laurente0720872014-03-11 09:30:41 -07002213status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01002214 int index,
2215 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07002216{
2217
Nadav Bar7c605f62017-12-25 00:01:52 +02002218 // VOICE_CALL stream has minVolumeIndex > 0 but can be muted directly by an
2219 // app that has MODIFY_PHONE_STATE permission.
2220 if (((index < mVolumeCurves->getVolumeIndexMin(stream)) &&
2221 !(stream == AUDIO_STREAM_VOICE_CALL && index == 0)) ||
François Gaffied1ab2bd2015-12-02 18:20:06 +01002222 (index > mVolumeCurves->getVolumeIndexMax(stream))) {
Eric Laurente552edb2014-03-10 17:42:56 -07002223 return BAD_VALUE;
2224 }
2225 if (!audio_is_output_device(device)) {
2226 return BAD_VALUE;
2227 }
2228
2229 // Force max volume if stream cannot be muted
François Gaffied1ab2bd2015-12-02 18:20:06 +01002230 if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07002231
Eric Laurent1fd372e2016-04-06 14:23:53 -07002232 ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d",
Eric Laurente552edb2014-03-10 17:42:56 -07002233 stream, device, index);
2234
Eric Laurent28d09f02016-03-08 10:43:05 -08002235 // update other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08002236 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
2237 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002238 continue;
2239 }
2240 mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index);
2241 }
Eric Laurente552edb2014-03-10 17:42:56 -07002242
Eric Laurent1fd372e2016-04-06 14:23:53 -07002243 // update volume on all outputs and streams matching the following:
2244 // - The requested stream (or a stream matching for volume control) is active on the output
2245 // - The device (or devices) selected by the strategy corresponding to this stream includes
2246 // the requested device
2247 // - For non default requested device, currently selected device on the output is either the
2248 // requested device or one of the devices selected by the strategy
Eric Laurent5a2b6292016-04-14 18:05:57 -07002249 // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if
2250 // no specific device volume value exists for currently selected device.
Eric Laurente552edb2014-03-10 17:42:56 -07002251 status_t status = NO_ERROR;
2252 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002253 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
2254 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent794fde22016-03-11 09:50:45 -08002255 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
2256 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002257 continue;
Eric Laurente552edb2014-03-10 17:42:56 -07002258 }
Eric Laurentd3926fe2016-04-15 18:10:07 -07002259 if (!(desc->isStreamActive((audio_stream_type_t)curStream) ||
2260 (isInCall() && (curStream == AUDIO_STREAM_VOICE_CALL)))) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07002261 continue;
2262 }
Eric Laurent794fde22016-03-11 09:50:45 -08002263 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Jean-Michel Trivib7fdce62017-06-27 19:38:42 -07002264 audio_devices_t curStreamDevice = Volume::getDeviceForVolume(getDeviceForStrategy(
2265 curStrategy, false /*fromCache*/));
Eric Laurente76f29c2016-09-14 17:17:53 -07002266 if ((device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) &&
2267 ((curStreamDevice & device) == 0)) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07002268 continue;
2269 }
Eric Laurente76f29c2016-09-14 17:17:53 -07002270 bool applyVolume;
Eric Laurent5a2b6292016-04-14 18:05:57 -07002271 if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07002272 curStreamDevice |= device;
Eric Laurente76f29c2016-09-14 17:17:53 -07002273 applyVolume = (curDevice & curStreamDevice) != 0;
2274 } else {
2275 applyVolume = !mVolumeCurves->hasVolumeIndexForDevice(
Jean-Michel Trivib7fdce62017-06-27 19:38:42 -07002276 stream, curStreamDevice);
Eric Laurent1fd372e2016-04-06 14:23:53 -07002277 }
Eric Laurent28d09f02016-03-08 10:43:05 -08002278
Eric Laurente76f29c2016-09-14 17:17:53 -07002279 if (applyVolume) {
Eric Laurentdc462862016-07-19 12:29:53 -07002280 //FIXME: workaround for truncated touch sounds
2281 // delayed volume change for system stream to be removed when the problem is
2282 // handled by system UI
Eric Laurent28d09f02016-03-08 10:43:05 -08002283 status_t volStatus =
Eric Laurentdc462862016-07-19 12:29:53 -07002284 checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice,
2285 (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0);
Eric Laurent28d09f02016-03-08 10:43:05 -08002286 if (volStatus != NO_ERROR) {
2287 status = volStatus;
2288 }
2289 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08002290 }
Eric Laurente552edb2014-03-10 17:42:56 -07002291 }
2292 return status;
2293}
2294
Eric Laurente0720872014-03-11 09:30:41 -07002295status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07002296 int *index,
2297 audio_devices_t device)
2298{
2299 if (index == NULL) {
2300 return BAD_VALUE;
2301 }
2302 if (!audio_is_output_device(device)) {
2303 return BAD_VALUE;
2304 }
Eric Laurent5a2b6292016-04-14 18:05:57 -07002305 // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device corresponding to
Eric Laurente552edb2014-03-10 17:42:56 -07002306 // the strategy the stream belongs to.
Eric Laurent5a2b6292016-04-14 18:05:57 -07002307 if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurente552edb2014-03-10 17:42:56 -07002308 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
2309 }
François Gaffiedfd74092015-03-19 12:10:59 +01002310 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07002311
François Gaffied1ab2bd2015-12-02 18:20:06 +01002312 *index = mVolumeCurves->getVolumeIndex(stream, device);
Eric Laurente552edb2014-03-10 17:42:56 -07002313 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
2314 return NO_ERROR;
2315}
2316
Eric Laurent36829f92017-04-07 19:04:42 -07002317audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects()
Eric Laurente552edb2014-03-10 17:42:56 -07002318{
2319 // select one output among several suitable for global effects.
2320 // The priority is as follows:
2321 // 1: An offloaded output. If the effect ends up not being offloadable,
2322 // AudioFlinger will invalidate the track and the offloaded output
2323 // will be closed causing the effect to be moved to a PCM output.
2324 // 2: A deep buffer output
Eric Laurent36829f92017-04-07 19:04:42 -07002325 // 3: The primary output
2326 // 4: the first output in the list
Eric Laurente552edb2014-03-10 17:42:56 -07002327
Eric Laurent3b73df72014-03-11 09:06:29 -07002328 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07002329 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent36829f92017-04-07 19:04:42 -07002330 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
Eric Laurente552edb2014-03-10 17:42:56 -07002331
Eric Laurent36829f92017-04-07 19:04:42 -07002332 if (outputs.size() == 0) {
2333 return AUDIO_IO_HANDLE_NONE;
2334 }
Eric Laurente552edb2014-03-10 17:42:56 -07002335
Eric Laurent36829f92017-04-07 19:04:42 -07002336 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
2337 bool activeOnly = true;
2338
2339 while (output == AUDIO_IO_HANDLE_NONE) {
2340 audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE;
2341 audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE;
2342 audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE;
2343
Mikhail Naganovcf84e592017-12-07 11:25:11 -08002344 for (audio_io_handle_t output : outputs) {
2345 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output);
Eric Laurent36829f92017-04-07 19:04:42 -07002346 if (activeOnly && !desc->isStreamActive(AUDIO_STREAM_MUSIC)) {
2347 continue;
2348 }
Mikhail Naganovcf84e592017-12-07 11:25:11 -08002349 ALOGV("selectOutputForMusicEffects activeOnly %d output %d flags 0x%08x",
2350 activeOnly, output, desc->mFlags);
Eric Laurent36829f92017-04-07 19:04:42 -07002351 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Mikhail Naganovcf84e592017-12-07 11:25:11 -08002352 outputOffloaded = output;
Eric Laurent36829f92017-04-07 19:04:42 -07002353 }
2354 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
Mikhail Naganovcf84e592017-12-07 11:25:11 -08002355 outputDeepBuffer = output;
Eric Laurent36829f92017-04-07 19:04:42 -07002356 }
2357 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) {
Mikhail Naganovcf84e592017-12-07 11:25:11 -08002358 outputPrimary = output;
Eric Laurent36829f92017-04-07 19:04:42 -07002359 }
2360 }
2361 if (outputOffloaded != AUDIO_IO_HANDLE_NONE) {
2362 output = outputOffloaded;
2363 } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) {
2364 output = outputDeepBuffer;
2365 } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) {
2366 output = outputPrimary;
2367 } else {
2368 output = outputs[0];
2369 }
2370 activeOnly = false;
2371 }
2372
2373 if (output != mMusicEffectOutput) {
2374 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output);
2375 mMusicEffectOutput = output;
2376 }
2377
2378 ALOGV("selectOutputForMusicEffects selected output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -07002379 return output;
2380}
2381
Eric Laurent36829f92017-04-07 19:04:42 -07002382audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused)
2383{
2384 return selectOutputForMusicEffects();
2385}
2386
Eric Laurente0720872014-03-11 09:30:41 -07002387status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07002388 audio_io_handle_t io,
2389 uint32_t strategy,
2390 int session,
2391 int id)
2392{
2393 ssize_t index = mOutputs.indexOfKey(io);
2394 if (index < 0) {
2395 index = mInputs.indexOfKey(io);
2396 if (index < 0) {
2397 ALOGW("registerEffect() unknown io %d", io);
2398 return INVALID_OPERATION;
2399 }
2400 }
François Gaffie45ed3b02015-03-19 10:35:14 +01002401 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07002402}
2403
Eric Laurentc75307b2015-03-17 15:29:32 -07002404bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
2405{
Eric Laurent28d09f02016-03-08 10:43:05 -08002406 bool active = false;
Eric Laurent794fde22016-03-11 09:50:45 -08002407 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) {
2408 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002409 continue;
2410 }
2411 active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs);
2412 }
Eric Laurent28d09f02016-03-08 10:43:05 -08002413 return active;
Eric Laurentc75307b2015-03-17 15:29:32 -07002414}
2415
2416bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
2417{
2418 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
2419}
2420
Eric Laurente0720872014-03-11 09:30:41 -07002421bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07002422{
2423 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002424 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002425 if (inputDescriptor->isSourceActive(source)) {
Eric Laurente552edb2014-03-10 17:42:56 -07002426 return true;
2427 }
2428 }
2429 return false;
2430}
2431
Eric Laurent275e8e92014-11-30 15:14:47 -08002432// Register a list of custom mixes with their attributes and format.
2433// When a mix is registered, corresponding input and output profiles are
2434// added to the remote submix hw module. The profile contains only the
2435// parameters (sampling rate, format...) specified by the mix.
2436// The corresponding input remote submix device is also connected.
2437//
2438// When a remote submix device is connected, the address is checked to select the
2439// appropriate profile and the corresponding input or output stream is opened.
2440//
2441// When capture starts, getInputForAttr() will:
2442// - 1 look for a mix matching the address passed in attribtutes tags if any
2443// - 2 if none found, getDeviceForInputSource() will:
2444// - 2.1 look for a mix matching the attributes source
2445// - 2.2 if none found, default to device selection by policy rules
2446// At this time, the corresponding output remote submix device is also connected
2447// and active playback use cases can be transferred to this mix if needed when reconnecting
2448// after AudioTracks are invalidated
2449//
2450// When playback starts, getOutputForAttr() will:
2451// - 1 look for a mix matching the address passed in attribtutes tags if any
2452// - 2 if none found, look for a mix matching the attributes usage
2453// - 3 if none found, default to device and output selection by policy rules.
2454
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07002455status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes)
Eric Laurent275e8e92014-11-30 15:14:47 -08002456{
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002457 ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size());
2458 status_t res = NO_ERROR;
2459
2460 sp<HwModule> rSubmixModule;
2461 // examine each mix's route type
2462 for (size_t i = 0; i < mixes.size(); i++) {
2463 // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination
2464 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) {
2465 res = INVALID_OPERATION;
Eric Laurent275e8e92014-11-30 15:14:47 -08002466 break;
2467 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002468 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002469 ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002470 if (rSubmixModule == 0) {
Mikhail Naganovd4120142017-12-06 15:49:22 -08002471 rSubmixModule = mHwModules.getModuleFromName(
2472 AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX);
2473 if (rSubmixModule == 0) {
2474 ALOGE(" Unable to find audio module for submix, aborting mix %zu registration",
2475 i);
2476 res = INVALID_OPERATION;
2477 break;
2478 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002479 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002480
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002481 String8 address = mixes[i].mDeviceAddress;
François Gaffie036e1e92015-03-19 10:16:24 +01002482
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002483 if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) {
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002484 ALOGE(" Error registering mix %zu for address %s", i, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002485 res = INVALID_OPERATION;
2486 break;
2487 }
2488 audio_config_t outputConfig = mixes[i].mFormat;
2489 audio_config_t inputConfig = mixes[i].mFormat;
2490 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
2491 // stereo and let audio flinger do the channel conversion if needed.
2492 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2493 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
2494 rSubmixModule->addOutputProfile(address, &outputConfig,
2495 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
2496 rSubmixModule->addInputProfile(address, &inputConfig,
2497 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01002498
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002499 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
2500 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2501 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2502 address.string(), "remote-submix");
2503 } else {
2504 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2505 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2506 address.string(), "remote-submix");
2507 }
2508 } else if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002509 String8 address = mixes[i].mDeviceAddress;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002510 audio_devices_t device = mixes[i].mDeviceType;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002511 ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s",
2512 i, mixes.size(), device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002513
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002514 bool foundOutput = false;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002515 for (size_t j = 0 ; j < mOutputs.size() ; j++) {
2516 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j);
2517 sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle());
2518 if ((patch != 0) && (patch->mPatch.num_sinks != 0)
2519 && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE)
2520 && (patch->mPatch.sinks[0].ext.device.type == device)
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002521 && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(),
2522 AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002523 if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) {
2524 res = INVALID_OPERATION;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002525 } else {
2526 foundOutput = true;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002527 }
2528 break;
2529 }
2530 }
2531
2532 if (res != NO_ERROR) {
2533 ALOGE(" Error registering mix %zu for device 0x%X addr %s",
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002534 i, device, address.string());
2535 res = INVALID_OPERATION;
2536 break;
2537 } else if (!foundOutput) {
2538 ALOGE(" Output not found for mix %zu for device 0x%X addr %s",
2539 i, device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002540 res = INVALID_OPERATION;
2541 break;
2542 }
Eric Laurentc722f302014-12-10 11:21:49 -08002543 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002544 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002545 if (res != NO_ERROR) {
2546 unregisterPolicyMixes(mixes);
2547 }
2548 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002549}
2550
2551status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
2552{
Eric Laurent7b279bb2015-12-14 10:18:23 -08002553 ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002554 status_t res = NO_ERROR;
2555 sp<HwModule> rSubmixModule;
2556 // examine each mix's route type
Mikhail Naganovcf84e592017-12-07 11:25:11 -08002557 for (const auto& mix : mixes) {
2558 if ((mix.mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
François Gaffie036e1e92015-03-19 10:16:24 +01002559
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002560 if (rSubmixModule == 0) {
Mikhail Naganovd4120142017-12-06 15:49:22 -08002561 rSubmixModule = mHwModules.getModuleFromName(
2562 AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX);
2563 if (rSubmixModule == 0) {
2564 res = INVALID_OPERATION;
2565 continue;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002566 }
2567 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002568
Mikhail Naganovcf84e592017-12-07 11:25:11 -08002569 String8 address = mix.mDeviceAddress;
Eric Laurent275e8e92014-11-30 15:14:47 -08002570
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002571 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
2572 res = INVALID_OPERATION;
2573 continue;
2574 }
2575
2576 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
2577 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2578 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2579 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2580 address.string(), "remote-submix");
2581 }
2582 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
2583 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2584 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2585 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2586 address.string(), "remote-submix");
2587 }
2588 rSubmixModule->removeOutputProfile(address);
2589 rSubmixModule->removeInputProfile(address);
2590
Mikhail Naganovcf84e592017-12-07 11:25:11 -08002591 } if ((mix.mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
2592 if (mPolicyMixes.unregisterMix(mix.mDeviceAddress) != NO_ERROR) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002593 res = INVALID_OPERATION;
2594 continue;
2595 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002596 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002597 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002598 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002599}
2600
Eric Laurente552edb2014-03-10 17:42:56 -07002601
Eric Laurente0720872014-03-11 09:30:41 -07002602status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07002603{
Eric Laurente552edb2014-03-10 17:42:56 -07002604 String8 result;
Mikhail Naganov2e5167e12018-04-19 13:41:22 -07002605 result.appendFormat("\nAudioPolicyManager Dump: %p\n", this);
2606 result.appendFormat(" Primary Output: %d\n",
Eric Laurent87ffa392015-05-22 10:32:38 -07002607 hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE);
Mikhail Naganov0d6a0332016-04-19 17:12:38 -07002608 std::string stateLiteral;
2609 AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral);
Mikhail Naganov2e5167e12018-04-19 13:41:22 -07002610 result.appendFormat(" Phone state: %s\n", stateLiteral.c_str());
2611 const char* forceUses[AUDIO_POLICY_FORCE_USE_CNT] = {
2612 "communications", "media", "record", "dock", "system",
2613 "HDMI system audio", "encoded surround output", "vibrate ringing" };
2614 for (audio_policy_force_use_t i = AUDIO_POLICY_FORCE_FOR_COMMUNICATION;
2615 i < AUDIO_POLICY_FORCE_USE_CNT; i = (audio_policy_force_use_t)((int)i + 1)) {
2616 result.appendFormat(" Force use for %s: %d\n",
2617 forceUses[i], mEngine->getForceUse(i));
2618 }
2619 result.appendFormat(" TTS output %savailable\n", mTtsOutputAvailable ? "" : "not ");
2620 result.appendFormat(" Master mono: %s\n", mMasterMono ? "on" : "off");
2621 result.appendFormat(" Config source: %s\n", getConfig().getSource().c_str());
François Gaffie2110e042015-03-24 08:41:51 +01002622 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07002623
François Gaffie112b0af2015-11-19 16:13:25 +01002624 mAvailableOutputDevices.dump(fd, String8("Available output"));
2625 mAvailableInputDevices.dump(fd, String8("Available input"));
Mikhail Naganovd4120142017-12-06 15:49:22 -08002626 mHwModulesAll.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002627 mOutputs.dump(fd);
2628 mInputs.dump(fd);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002629 mVolumeCurves->dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01002630 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002631 mAudioPatches.dump(fd);
Mikhail Naganov44344b02016-12-13 11:21:02 -08002632 mPolicyMixes.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07002633
2634 return NO_ERROR;
2635}
2636
2637// This function checks for the parameters which can be offloaded.
2638// This can be enhanced depending on the capability of the DSP and policy
2639// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002640bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002641{
2642 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002643 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002644 offloadInfo.sample_rate, offloadInfo.channel_mask,
2645 offloadInfo.format,
2646 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2647 offloadInfo.has_video);
2648
Andy Hung2ddee192015-12-18 17:34:44 -08002649 if (mMasterMono) {
2650 return false; // no offloading if mono is set.
2651 }
2652
Eric Laurente552edb2014-03-10 17:42:56 -07002653 // Check if offload has been disabled
2654 char propValue[PROPERTY_VALUE_MAX];
2655 if (property_get("audio.offload.disable", propValue, "0")) {
2656 if (atoi(propValue) != 0) {
2657 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2658 return false;
2659 }
2660 }
2661
2662 // Check if stream type is music, then only allow offload as of now.
2663 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2664 {
2665 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2666 return false;
2667 }
2668
2669 //TODO: enable audio offloading with video when ready
Andy Hung08945c42015-05-31 21:36:46 -07002670 const bool allowOffloadWithVideo =
2671 property_get_bool("audio.offload.video", false /* default_value */);
2672 if (offloadInfo.has_video && !allowOffloadWithVideo) {
Eric Laurente552edb2014-03-10 17:42:56 -07002673 ALOGV("isOffloadSupported: has_video == true, returning false");
2674 return false;
2675 }
2676
2677 //If duration is less than minimum value defined in property, return false
2678 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2679 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2680 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2681 return false;
2682 }
2683 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2684 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2685 return false;
2686 }
2687
2688 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2689 // creating an offloaded track and tearing it down immediately after start when audioflinger
2690 // detects there is an active non offloadable effect.
2691 // FIXME: We should check the audio session here but we do not have it in this context.
2692 // This may prevent offloading in rare situations where effects are left active by apps
2693 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002694 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002695 return false;
2696 }
2697
2698 // See if there is a profile to support this.
2699 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002700 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002701 offloadInfo.sample_rate,
2702 offloadInfo.format,
2703 offloadInfo.channel_mask,
2704 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002705 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2706 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002707}
2708
Eric Laurent6a94d692014-05-20 11:18:06 -07002709status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2710 audio_port_type_t type,
2711 unsigned int *num_ports,
2712 struct audio_port *ports,
2713 unsigned int *generation)
2714{
2715 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2716 generation == NULL) {
2717 return BAD_VALUE;
2718 }
2719 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2720 if (ports == NULL) {
2721 *num_ports = 0;
2722 }
2723
2724 size_t portsWritten = 0;
2725 size_t portsMax = *num_ports;
2726 *num_ports = 0;
2727 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002728 // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB
2729 // as they are used by stub HALs by convention
Eric Laurent6a94d692014-05-20 11:18:06 -07002730 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
Mikhail Naganovcf84e592017-12-07 11:25:11 -08002731 for (const auto& dev : mAvailableOutputDevices) {
2732 if (dev->type() == AUDIO_DEVICE_OUT_STUB) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002733 continue;
2734 }
2735 if (portsWritten < portsMax) {
Mikhail Naganovcf84e592017-12-07 11:25:11 -08002736 dev->toAudioPort(&ports[portsWritten++]);
Eric Laurent5a2b6292016-04-14 18:05:57 -07002737 }
2738 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002739 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002740 }
2741 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Mikhail Naganovcf84e592017-12-07 11:25:11 -08002742 for (const auto& dev : mAvailableInputDevices) {
2743 if (dev->type() == AUDIO_DEVICE_IN_STUB) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002744 continue;
2745 }
2746 if (portsWritten < portsMax) {
Mikhail Naganovcf84e592017-12-07 11:25:11 -08002747 dev->toAudioPort(&ports[portsWritten++]);
Eric Laurent5a2b6292016-04-14 18:05:57 -07002748 }
2749 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002750 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002751 }
2752 }
2753 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2754 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2755 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2756 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2757 }
2758 *num_ports += mInputs.size();
2759 }
2760 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002761 size_t numOutputs = 0;
2762 for (size_t i = 0; i < mOutputs.size(); i++) {
2763 if (!mOutputs[i]->isDuplicated()) {
2764 numOutputs++;
2765 if (portsWritten < portsMax) {
2766 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2767 }
2768 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002769 }
Eric Laurent84c70242014-06-23 08:46:27 -07002770 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002771 }
2772 }
2773 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002774 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002775 return NO_ERROR;
2776}
2777
2778status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2779{
2780 return NO_ERROR;
2781}
2782
Eric Laurent6a94d692014-05-20 11:18:06 -07002783status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2784 audio_patch_handle_t *handle,
2785 uid_t uid)
2786{
2787 ALOGV("createAudioPatch()");
2788
2789 if (handle == NULL || patch == NULL) {
2790 return BAD_VALUE;
2791 }
2792 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2793
Eric Laurent874c42872014-08-08 15:13:39 -07002794 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2795 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2796 return BAD_VALUE;
2797 }
2798 // only one source per audio patch supported for now
2799 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002800 return INVALID_OPERATION;
2801 }
Eric Laurent874c42872014-08-08 15:13:39 -07002802
2803 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002804 return INVALID_OPERATION;
2805 }
Eric Laurent874c42872014-08-08 15:13:39 -07002806 for (size_t i = 0; i < patch->num_sinks; i++) {
2807 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2808 return INVALID_OPERATION;
2809 }
2810 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002811
2812 sp<AudioPatch> patchDesc;
2813 ssize_t index = mAudioPatches.indexOfKey(*handle);
2814
Eric Laurent6a94d692014-05-20 11:18:06 -07002815 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2816 patch->sources[0].role,
2817 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002818#if LOG_NDEBUG == 0
2819 for (size_t i = 0; i < patch->num_sinks; i++) {
Eric Laurent7b279bb2015-12-14 10:18:23 -08002820 ALOGV("createAudioPatch sink %zu: id %d role %d type %d", i, patch->sinks[i].id,
Eric Laurent874c42872014-08-08 15:13:39 -07002821 patch->sinks[i].role,
2822 patch->sinks[i].type);
2823 }
2824#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002825
2826 if (index >= 0) {
2827 patchDesc = mAudioPatches.valueAt(index);
2828 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2829 mUidCached, patchDesc->mUid, uid);
2830 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2831 return INVALID_OPERATION;
2832 }
2833 } else {
Glenn Kastena13cde92016-03-28 15:26:02 -07002834 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07002835 }
2836
2837 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002838 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002839 if (outputDesc == NULL) {
2840 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2841 return BAD_VALUE;
2842 }
Eric Laurent84c70242014-06-23 08:46:27 -07002843 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2844 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002845 if (patchDesc != 0) {
2846 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2847 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2848 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2849 return BAD_VALUE;
2850 }
2851 }
Eric Laurent874c42872014-08-08 15:13:39 -07002852 DeviceVector devices;
2853 for (size_t i = 0; i < patch->num_sinks; i++) {
2854 // Only support mix to devices connection
2855 // TODO add support for mix to mix connection
2856 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2857 ALOGV("createAudioPatch() source mix but sink is not a device");
2858 return INVALID_OPERATION;
2859 }
2860 sp<DeviceDescriptor> devDesc =
2861 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2862 if (devDesc == 0) {
2863 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2864 return BAD_VALUE;
2865 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002866
François Gaffie53615e22015-03-19 09:24:12 +01002867 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002868 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002869 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002870 NULL, // updatedSamplingRate
2871 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002872 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002873 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002874 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002875 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002876 ALOGV("createAudioPatch() profile not supported for device %08x",
2877 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002878 return INVALID_OPERATION;
2879 }
2880 devices.add(devDesc);
2881 }
2882 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002883 return INVALID_OPERATION;
2884 }
Eric Laurent874c42872014-08-08 15:13:39 -07002885
Eric Laurent6a94d692014-05-20 11:18:06 -07002886 // TODO: reconfigure output format and channels here
2887 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002888 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002889 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002890 index = mAudioPatches.indexOfKey(*handle);
2891 if (index >= 0) {
2892 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2893 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2894 }
2895 patchDesc = mAudioPatches.valueAt(index);
2896 patchDesc->mUid = uid;
2897 ALOGV("createAudioPatch() success");
2898 } else {
2899 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2900 return INVALID_OPERATION;
2901 }
2902 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2903 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2904 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002905 // only one sink supported when connecting an input device to a mix
2906 if (patch->num_sinks > 1) {
2907 return INVALID_OPERATION;
2908 }
François Gaffie53615e22015-03-19 09:24:12 +01002909 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002910 if (inputDesc == NULL) {
2911 return BAD_VALUE;
2912 }
2913 if (patchDesc != 0) {
2914 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2915 return BAD_VALUE;
2916 }
2917 }
2918 sp<DeviceDescriptor> devDesc =
2919 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2920 if (devDesc == 0) {
2921 return BAD_VALUE;
2922 }
2923
François Gaffie53615e22015-03-19 09:24:12 +01002924 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002925 devDesc->mAddress,
2926 patch->sinks[0].sample_rate,
2927 NULL, /*updatedSampleRate*/
2928 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002929 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002930 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002931 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002932 // FIXME for the parameter type,
2933 // and the NONE
2934 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002935 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002936 return INVALID_OPERATION;
2937 }
2938 // TODO: reconfigure output format and channels here
2939 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002940 devDesc->type(), inputDesc->mIoHandle);
2941 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002942 index = mAudioPatches.indexOfKey(*handle);
2943 if (index >= 0) {
2944 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2945 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2946 }
2947 patchDesc = mAudioPatches.valueAt(index);
2948 patchDesc->mUid = uid;
2949 ALOGV("createAudioPatch() success");
2950 } else {
2951 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2952 return INVALID_OPERATION;
2953 }
2954 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2955 // device to device connection
2956 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002957 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002958 return BAD_VALUE;
2959 }
2960 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002961 sp<DeviceDescriptor> srcDeviceDesc =
2962 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002963 if (srcDeviceDesc == 0) {
2964 return BAD_VALUE;
2965 }
Eric Laurent874c42872014-08-08 15:13:39 -07002966
Eric Laurent6a94d692014-05-20 11:18:06 -07002967 //update source and sink with our own data as the data passed in the patch may
2968 // be incomplete.
2969 struct audio_patch newPatch = *patch;
2970 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002971
Eric Laurent874c42872014-08-08 15:13:39 -07002972 for (size_t i = 0; i < patch->num_sinks; i++) {
2973 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2974 ALOGV("createAudioPatch() source device but one sink is not a device");
2975 return INVALID_OPERATION;
2976 }
2977
2978 sp<DeviceDescriptor> sinkDeviceDesc =
2979 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2980 if (sinkDeviceDesc == 0) {
2981 return BAD_VALUE;
2982 }
2983 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2984
Eric Laurent3bcf8592015-04-03 12:13:24 -07002985 // create a software bridge in PatchPanel if:
Scott Randolphf3172402018-01-23 17:06:53 -08002986 // - source and sink devices are on different HW modules OR
Eric Laurent3bcf8592015-04-03 12:13:24 -07002987 // - audio HAL version is < 3.0
Eric Laurentfb66dd92016-01-28 18:32:03 -08002988 if (!srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) ||
Mikhail Naganov9ee05402016-10-13 15:58:17 -07002989 (srcDeviceDesc->mModule->getHalVersionMajor() < 3)) {
Eric Laurent3bcf8592015-04-03 12:13:24 -07002990 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002991 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002992 return INVALID_OPERATION;
2993 }
Eric Laurent874c42872014-08-08 15:13:39 -07002994 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002995 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002996 // if the sink device is reachable via an opened output stream, request to go via
2997 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002998 audio_io_handle_t output = selectOutput(outputs,
2999 AUDIO_OUTPUT_FLAG_NONE,
3000 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07003001 if (output != AUDIO_IO_HANDLE_NONE) {
3002 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
3003 if (outputDesc->isDuplicated()) {
3004 return INVALID_OPERATION;
3005 }
3006 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07003007 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07003008 newPatch.num_sources = 2;
3009 }
Eric Laurent83b88082014-06-20 18:31:16 -07003010 }
Eric Laurent6a94d692014-05-20 11:18:06 -07003011 }
3012 // TODO: check from routing capabilities in config file and other conflicting patches
3013
3014 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3015 if (index >= 0) {
3016 afPatchHandle = patchDesc->mAfPatchHandle;
3017 }
3018
3019 status_t status = mpClientInterface->createAudioPatch(&newPatch,
3020 &afPatchHandle,
3021 0);
3022 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
3023 status, afPatchHandle);
3024 if (status == NO_ERROR) {
3025 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01003026 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07003027 addAudioPatch(patchDesc->mHandle, patchDesc);
3028 } else {
3029 patchDesc->mPatch = newPatch;
3030 }
3031 patchDesc->mAfPatchHandle = afPatchHandle;
3032 *handle = patchDesc->mHandle;
3033 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07003034 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07003035 } else {
3036 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
3037 status);
3038 return INVALID_OPERATION;
3039 }
3040 } else {
3041 return BAD_VALUE;
3042 }
3043 } else {
3044 return BAD_VALUE;
3045 }
3046 return NO_ERROR;
3047}
3048
3049status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
3050 uid_t uid)
3051{
3052 ALOGV("releaseAudioPatch() patch %d", handle);
3053
3054 ssize_t index = mAudioPatches.indexOfKey(handle);
3055
3056 if (index < 0) {
3057 return BAD_VALUE;
3058 }
3059 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3060 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
3061 mUidCached, patchDesc->mUid, uid);
3062 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
3063 return INVALID_OPERATION;
3064 }
3065
3066 struct audio_patch *patch = &patchDesc->mPatch;
3067 patchDesc->mUid = mUidCached;
3068 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003069 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07003070 if (outputDesc == NULL) {
3071 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
3072 return BAD_VALUE;
3073 }
3074
Eric Laurentc75307b2015-03-17 15:29:32 -07003075 setOutputDevice(outputDesc,
3076 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07003077 true,
3078 0,
3079 NULL);
3080 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
3081 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01003082 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07003083 if (inputDesc == NULL) {
3084 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
3085 return BAD_VALUE;
3086 }
3087 setInputDevice(inputDesc->mIoHandle,
Eric Laurentfb66dd92016-01-28 18:32:03 -08003088 getNewInputDevice(inputDesc),
Eric Laurent6a94d692014-05-20 11:18:06 -07003089 true,
3090 NULL);
3091 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003092 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3093 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
3094 status, patchDesc->mAfPatchHandle);
3095 removeAudioPatch(patchDesc->mHandle);
3096 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07003097 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07003098 } else {
3099 return BAD_VALUE;
3100 }
3101 } else {
3102 return BAD_VALUE;
3103 }
3104 return NO_ERROR;
3105}
3106
3107status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
3108 struct audio_patch *patches,
3109 unsigned int *generation)
3110{
François Gaffie53615e22015-03-19 09:24:12 +01003111 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003112 return BAD_VALUE;
3113 }
Eric Laurent6a94d692014-05-20 11:18:06 -07003114 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01003115 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07003116}
3117
Eric Laurente1715a42014-05-20 11:30:42 -07003118status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07003119{
Eric Laurente1715a42014-05-20 11:30:42 -07003120 ALOGV("setAudioPortConfig()");
3121
3122 if (config == NULL) {
3123 return BAD_VALUE;
3124 }
3125 ALOGV("setAudioPortConfig() on port handle %d", config->id);
3126 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07003127 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
3128 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07003129 }
3130
Eric Laurenta121f902014-06-03 13:32:54 -07003131 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07003132 if (config->type == AUDIO_PORT_TYPE_MIX) {
3133 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003134 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07003135 if (outputDesc == NULL) {
3136 return BAD_VALUE;
3137 }
Eric Laurent84c70242014-06-23 08:46:27 -07003138 ALOG_ASSERT(!outputDesc->isDuplicated(),
3139 "setAudioPortConfig() called on duplicated output %d",
3140 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07003141 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07003142 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01003143 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07003144 if (inputDesc == NULL) {
3145 return BAD_VALUE;
3146 }
Eric Laurenta121f902014-06-03 13:32:54 -07003147 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07003148 } else {
3149 return BAD_VALUE;
3150 }
3151 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
3152 sp<DeviceDescriptor> deviceDesc;
3153 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
3154 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
3155 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
3156 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
3157 } else {
3158 return BAD_VALUE;
3159 }
3160 if (deviceDesc == NULL) {
3161 return BAD_VALUE;
3162 }
Eric Laurenta121f902014-06-03 13:32:54 -07003163 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07003164 } else {
3165 return BAD_VALUE;
3166 }
3167
Eric Laurenta121f902014-06-03 13:32:54 -07003168 struct audio_port_config backupConfig;
3169 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
3170 if (status == NO_ERROR) {
3171 struct audio_port_config newConfig;
3172 audioPortConfig->toAudioPortConfig(&newConfig, config);
3173 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07003174 }
Eric Laurenta121f902014-06-03 13:32:54 -07003175 if (status != NO_ERROR) {
3176 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07003177 }
Eric Laurente1715a42014-05-20 11:30:42 -07003178
3179 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07003180}
3181
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003182void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
3183{
Eric Laurentd60560a2015-04-10 11:31:20 -07003184 clearAudioSources(uid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003185 clearAudioPatches(uid);
3186 clearSessionRoutes(uid);
3187}
3188
Eric Laurent6a94d692014-05-20 11:18:06 -07003189void AudioPolicyManager::clearAudioPatches(uid_t uid)
3190{
Eric Laurent0add0fd2014-12-04 18:58:14 -08003191 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003192 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
3193 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08003194 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07003195 }
3196 }
3197}
3198
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003199void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
3200 audio_io_handle_t ouptutToSkip)
3201{
3202 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
3203 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
3204 for (size_t j = 0; j < mOutputs.size(); j++) {
3205 if (mOutputs.keyAt(j) == ouptutToSkip) {
3206 continue;
3207 }
3208 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
3209 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
3210 continue;
3211 }
3212 // If the default device for this strategy is on another output mix,
3213 // invalidate all tracks in this strategy to force re connection.
3214 // Otherwise select new device on the output mix.
3215 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
Eric Laurent794fde22016-03-11 09:50:45 -08003216 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003217 if (getStrategy((audio_stream_type_t)stream) == strategy) {
3218 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
3219 }
3220 }
3221 } else {
3222 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
3223 setOutputDevice(outputDesc, newDevice, false);
3224 }
3225 }
3226}
3227
3228void AudioPolicyManager::clearSessionRoutes(uid_t uid)
3229{
3230 // remove output routes associated with this uid
3231 SortedVector<routing_strategy> affectedStrategies;
3232 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
3233 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
3234 if (route->mUid == uid) {
3235 mOutputRoutes.removeItemsAt(i);
3236 if (route->mDeviceDescriptor != 0) {
3237 affectedStrategies.add(getStrategy(route->mStreamType));
3238 }
3239 }
3240 }
3241 // reroute outputs if necessary
Mikhail Naganovcf84e592017-12-07 11:25:11 -08003242 for (const auto& strategy : affectedStrategies) {
3243 checkStrategyRoute(strategy, AUDIO_IO_HANDLE_NONE);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003244 }
3245
3246 // remove input routes associated with this uid
3247 SortedVector<audio_source_t> affectedSources;
3248 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
3249 sp<SessionRoute> route = mInputRoutes.valueAt(i);
3250 if (route->mUid == uid) {
3251 mInputRoutes.removeItemsAt(i);
3252 if (route->mDeviceDescriptor != 0) {
3253 affectedSources.add(route->mSource);
3254 }
3255 }
3256 }
3257 // reroute inputs if necessary
3258 SortedVector<audio_io_handle_t> inputsToClose;
3259 for (size_t i = 0; i < mInputs.size(); i++) {
3260 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08003261 if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003262 inputsToClose.add(inputDesc->mIoHandle);
3263 }
3264 }
Mikhail Naganovcf84e592017-12-07 11:25:11 -08003265 for (const auto& input : inputsToClose) {
3266 closeInput(input);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003267 }
3268}
3269
Eric Laurentd60560a2015-04-10 11:31:20 -07003270void AudioPolicyManager::clearAudioSources(uid_t uid)
3271{
3272 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
3273 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3274 if (sourceDesc->mUid == uid) {
3275 stopAudioSource(mAudioSources.keyAt(i));
3276 }
3277 }
3278}
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003279
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07003280status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
3281 audio_io_handle_t *ioHandle,
3282 audio_devices_t *device)
3283{
Glenn Kastenf0c6d7d2016-02-26 10:44:04 -08003284 *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
3285 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
Eric Laurentc73ca6e2014-12-12 14:34:22 -08003286 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07003287
François Gaffiedf372692015-03-19 10:43:27 +01003288 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07003289}
3290
Eric Laurentd60560a2015-04-10 11:31:20 -07003291status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source,
3292 const audio_attributes_t *attributes,
Glenn Kasten559d4392016-03-29 13:42:57 -07003293 audio_patch_handle_t *handle,
Eric Laurentd60560a2015-04-10 11:31:20 -07003294 uid_t uid)
Eric Laurent554a2772015-04-10 11:29:24 -07003295{
Eric Laurentd60560a2015-04-10 11:31:20 -07003296 ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle);
3297 if (source == NULL || attributes == NULL || handle == NULL) {
3298 return BAD_VALUE;
3299 }
3300
Glenn Kasten559d4392016-03-29 13:42:57 -07003301 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentd60560a2015-04-10 11:31:20 -07003302
3303 if (source->role != AUDIO_PORT_ROLE_SOURCE ||
3304 source->type != AUDIO_PORT_TYPE_DEVICE) {
3305 ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type);
3306 return INVALID_OPERATION;
3307 }
3308
3309 sp<DeviceDescriptor> srcDeviceDesc =
3310 mAvailableInputDevices.getDevice(source->ext.device.type,
3311 String8(source->ext.device.address));
3312 if (srcDeviceDesc == 0) {
3313 ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type);
3314 return BAD_VALUE;
3315 }
3316 sp<AudioSourceDescriptor> sourceDesc =
3317 new AudioSourceDescriptor(srcDeviceDesc, attributes, uid);
3318
3319 struct audio_patch dummyPatch;
3320 sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid);
3321 sourceDesc->mPatchDesc = patchDesc;
3322
3323 status_t status = connectAudioSource(sourceDesc);
3324 if (status == NO_ERROR) {
3325 mAudioSources.add(sourceDesc->getHandle(), sourceDesc);
3326 *handle = sourceDesc->getHandle();
3327 }
3328 return status;
3329}
3330
3331status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3332{
3333 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3334
3335 // make sure we only have one patch per source.
3336 disconnectAudioSource(sourceDesc);
3337
3338 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
Eric Laurent28d09f02016-03-08 10:43:05 -08003339 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003340 sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice;
3341
3342 audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true);
3343 sp<DeviceDescriptor> sinkDeviceDesc =
3344 mAvailableOutputDevices.getDevice(sinkDevice, String8(""));
3345
3346 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3347 struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch;
3348
3349 if (srcDeviceDesc->getAudioPort()->mModule->getHandle() ==
3350 sinkDeviceDesc->getAudioPort()->mModule->getHandle() &&
Mikhail Naganov9ee05402016-10-13 15:58:17 -07003351 srcDeviceDesc->getAudioPort()->mModule->getHalVersionMajor() >= 3 &&
Eric Laurentd60560a2015-04-10 11:31:20 -07003352 srcDeviceDesc->getAudioPort()->mGains.size() > 0) {
3353 ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__);
3354 // create patch between src device and output device
3355 // create Hwoutput and add to mHwOutputs
3356 } else {
3357 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs);
3358 audio_io_handle_t output =
3359 selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID);
3360 if (output == AUDIO_IO_HANDLE_NONE) {
3361 ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice);
3362 return INVALID_OPERATION;
3363 }
3364 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
3365 if (outputDesc->isDuplicated()) {
3366 ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice);
3367 return INVALID_OPERATION;
3368 }
Eric Laurent733ce942017-12-07 12:18:25 -08003369 status_t status = outputDesc->start();
3370 if (status != NO_ERROR) {
3371 return status;
3372 }
3373
Eric Laurentd60560a2015-04-10 11:31:20 -07003374 // create a special patch with no sink and two sources:
3375 // - the second source indicates to PatchPanel through which output mix this patch should
3376 // be connected as well as the stream type for volume control
3377 // - the sink is defined by whatever output device is currently selected for the output
3378 // though which this patch is routed.
3379 patch->num_sinks = 0;
3380 patch->num_sources = 2;
3381 srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL);
3382 outputDesc->toAudioPortConfig(&patch->sources[1], NULL);
3383 patch->sources[1].ext.mix.usecase.stream = stream;
Eric Laurent733ce942017-12-07 12:18:25 -08003384 status = mpClientInterface->createAudioPatch(patch,
Eric Laurentd60560a2015-04-10 11:31:20 -07003385 &afPatchHandle,
3386 0);
3387 ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__,
3388 status, afPatchHandle);
3389 if (status != NO_ERROR) {
3390 ALOGW("%s patch panel could not connect device patch, error %d",
3391 __FUNCTION__, status);
3392 return INVALID_OPERATION;
3393 }
3394 uint32_t delayMs = 0;
Eric Laurentc40d9692016-04-13 19:14:13 -07003395 status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs);
Eric Laurentd60560a2015-04-10 11:31:20 -07003396
3397 if (status != NO_ERROR) {
3398 mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0);
3399 return status;
3400 }
3401 sourceDesc->mSwOutput = outputDesc;
3402 if (delayMs != 0) {
3403 usleep(delayMs * 1000);
3404 }
3405 }
3406
3407 sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle;
3408 addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc);
3409
3410 return NO_ERROR;
Eric Laurent554a2772015-04-10 11:29:24 -07003411}
3412
Glenn Kasten559d4392016-03-29 13:42:57 -07003413status_t AudioPolicyManager::stopAudioSource(audio_patch_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07003414{
Eric Laurentd60560a2015-04-10 11:31:20 -07003415 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle);
3416 ALOGV("%s handle %d", __FUNCTION__, handle);
3417 if (sourceDesc == 0) {
3418 ALOGW("%s unknown source for handle %d", __FUNCTION__, handle);
3419 return BAD_VALUE;
3420 }
3421 status_t status = disconnectAudioSource(sourceDesc);
3422
3423 mAudioSources.removeItem(handle);
3424 return status;
3425}
3426
Andy Hung2ddee192015-12-18 17:34:44 -08003427status_t AudioPolicyManager::setMasterMono(bool mono)
3428{
3429 if (mMasterMono == mono) {
3430 return NO_ERROR;
3431 }
3432 mMasterMono = mono;
3433 // if enabling mono we close all offloaded devices, which will invalidate the
3434 // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible
3435 // for recreating the new AudioTrack as non-offloaded PCM.
3436 //
3437 // If disabling mono, we leave all tracks as is: we don't know which clients
3438 // and tracks are able to be recreated as offloaded. The next "song" should
3439 // play back offloaded.
3440 if (mMasterMono) {
3441 Vector<audio_io_handle_t> offloaded;
3442 for (size_t i = 0; i < mOutputs.size(); ++i) {
3443 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
3444 if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
3445 offloaded.push(desc->mIoHandle);
3446 }
3447 }
Mikhail Naganovcf84e592017-12-07 11:25:11 -08003448 for (const auto& handle : offloaded) {
3449 closeOutput(handle);
Andy Hung2ddee192015-12-18 17:34:44 -08003450 }
3451 }
3452 // update master mono for all remaining outputs
3453 for (size_t i = 0; i < mOutputs.size(); ++i) {
3454 updateMono(mOutputs.keyAt(i));
3455 }
3456 return NO_ERROR;
3457}
3458
3459status_t AudioPolicyManager::getMasterMono(bool *mono)
3460{
3461 *mono = mMasterMono;
3462 return NO_ERROR;
3463}
3464
Eric Laurentac9cef52017-06-09 15:46:26 -07003465float AudioPolicyManager::getStreamVolumeDB(
3466 audio_stream_type_t stream, int index, audio_devices_t device)
3467{
3468 return computeVolume(stream, index, device);
3469}
3470
jiabin81772902018-04-02 17:52:27 -07003471status_t AudioPolicyManager::getSupportedFormats(audio_io_handle_t ioHandle,
3472 FormatVector& formats) {
3473 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
3474 return BAD_VALUE;
3475 }
3476 String8 reply;
3477 reply = mpClientInterface->getParameters(
3478 ioHandle, String8(AudioParameter::keyStreamSupportedFormats));
3479 ALOGV("%s: supported formats %s", __FUNCTION__, reply.string());
3480 AudioParameter repliedParameters(reply);
3481 if (repliedParameters.get(
3482 String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) {
3483 ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__);
3484 return BAD_VALUE;
3485 }
3486 for (auto format : formatsFromString(reply.string())) {
3487 // Only AUDIO_FORMAT_AAC_LC will be used in Settings UI for all AAC formats.
3488 for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) {
3489 if (format == AAC_FORMATS[i]) {
3490 format = AUDIO_FORMAT_AAC_LC;
3491 break;
3492 }
3493 }
3494 bool exist = false;
3495 for (size_t i = 0; i < formats.size(); i++) {
3496 if (format == formats[i]) {
3497 exist = true;
3498 break;
3499 }
3500 }
3501 bool isSurroundFormat = false;
3502 for (size_t i = 0; i < ARRAY_SIZE(SURROUND_FORMATS); i++) {
3503 if (SURROUND_FORMATS[i] == format) {
3504 isSurroundFormat = true;
3505 break;
3506 }
3507 }
3508 if (!exist && isSurroundFormat) {
3509 formats.add(format);
3510 }
3511 }
3512 return NO_ERROR;
3513}
3514
3515status_t AudioPolicyManager::getSurroundFormats(unsigned int *numSurroundFormats,
3516 audio_format_t *surroundFormats,
3517 bool *surroundFormatsEnabled,
3518 bool reported)
3519{
3520 if (numSurroundFormats == NULL || (*numSurroundFormats != 0 &&
3521 (surroundFormats == NULL || surroundFormatsEnabled == NULL))) {
3522 return BAD_VALUE;
3523 }
3524 ALOGV("getSurroundFormats() numSurroundFormats %d surroundFormats %p surroundFormatsEnabled %p",
3525 *numSurroundFormats, surroundFormats, surroundFormatsEnabled);
3526
3527 // Only return value if there is HDMI output.
3528 if ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_HDMI) == 0) {
3529 return INVALID_OPERATION;
3530 }
3531
3532 size_t formatsWritten = 0;
3533 size_t formatsMax = *numSurroundFormats;
3534 *numSurroundFormats = 0;
3535 FormatVector formats;
3536 if (reported) {
3537 // Only get surround formats which are reported by device.
3538 // First list already open outputs that can be routed to this device
3539 audio_devices_t device = AUDIO_DEVICE_OUT_HDMI;
3540 SortedVector<audio_io_handle_t> outputs;
3541 bool reportedFormatFound = false;
3542 status_t status;
3543 sp<SwAudioOutputDescriptor> desc;
3544 for (size_t i = 0; i < mOutputs.size(); i++) {
3545 desc = mOutputs.valueAt(i);
3546 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
3547 outputs.add(mOutputs.keyAt(i));
3548 }
3549 }
3550 // Open an output to query dynamic parameters.
3551 DeviceVector hdmiOutputDevices = mAvailableOutputDevices.getDevicesFromType(
3552 AUDIO_DEVICE_OUT_HDMI);
3553 for (size_t i = 0; i < hdmiOutputDevices.size(); i++) {
3554 String8 address = hdmiOutputDevices[i]->mAddress;
3555 for (const auto& hwModule : mHwModules) {
3556 for (size_t i = 0; i < hwModule->getOutputProfiles().size(); i++) {
3557 sp<IOProfile> profile = hwModule->getOutputProfiles()[i];
3558 if (profile->supportDevice(AUDIO_DEVICE_OUT_HDMI) &&
3559 profile->supportDeviceAddress(address)) {
3560 size_t j;
3561 for (j = 0; j < outputs.size(); j++) {
3562 desc = mOutputs.valueFor(outputs.itemAt(j));
3563 if (!desc->isDuplicated() && desc->mProfile == profile) {
3564 break;
3565 }
3566 }
3567 if (j != outputs.size()) {
3568 status = getSupportedFormats(outputs.itemAt(j), formats);
3569 reportedFormatFound |= (status == NO_ERROR);
3570 continue;
3571 }
3572
3573 if (!profile->canOpenNewIo()) {
3574 ALOGW("Max Output number %u already opened for this profile %s",
3575 profile->maxOpenCount, profile->getTagName().c_str());
3576 continue;
3577 }
3578
3579 ALOGV("opening output for device %08x with params %s profile %p name %s",
3580 device, address.string(), profile.get(), profile->getName().string());
3581 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
3582 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
3583 status_t status = desc->open(nullptr, device, address,
3584 AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE,
3585 &output);
3586
3587 if (status == NO_ERROR) {
3588 status = getSupportedFormats(output, formats);
3589 reportedFormatFound |= (status == NO_ERROR);
3590 desc->close();
3591 output = AUDIO_IO_HANDLE_NONE;
3592 }
3593 }
3594 }
3595 }
3596 }
3597
3598 if (!reportedFormatFound) {
3599 return UNKNOWN_ERROR;
3600 }
3601 } else {
3602 for (size_t i = 0; i < ARRAY_SIZE(SURROUND_FORMATS); i++) {
3603 formats.add(SURROUND_FORMATS[i]);
3604 }
3605 }
3606 for (size_t i = 0; i < formats.size(); i++) {
3607 if (formatsWritten < formatsMax) {
3608 surroundFormats[formatsWritten] = formats[i];
3609 bool formatEnabled = false;
3610 if (formats[i] == AUDIO_FORMAT_AAC_LC) {
3611 for (size_t j = 0; j < ARRAY_SIZE(AAC_FORMATS); j++) {
3612 formatEnabled =
3613 mSurroundFormats.find(AAC_FORMATS[i]) != mSurroundFormats.end();
3614 break;
3615 }
3616 } else {
3617 formatEnabled = mSurroundFormats.find(formats[i]) != mSurroundFormats.end();
3618 }
3619 surroundFormatsEnabled[formatsWritten++] = formatEnabled;
3620 }
3621 (*numSurroundFormats)++;
3622 }
3623 return NO_ERROR;
3624}
3625
3626status_t AudioPolicyManager::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled)
3627{
3628 // Check if audio format is a surround formats.
3629 bool isSurroundFormat = false;
3630 for (size_t i = 0; i < ARRAY_SIZE(SURROUND_FORMATS); i++) {
3631 if (audioFormat == SURROUND_FORMATS[i]) {
3632 isSurroundFormat = true;
3633 break;
3634 }
3635 }
3636 if (!isSurroundFormat) {
3637 return BAD_VALUE;
3638 }
3639
3640 // Should only be called when MANUAL.
3641 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
3642 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
3643 if (forceUse != AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) {
3644 return INVALID_OPERATION;
3645 }
3646
3647 if ((mSurroundFormats.find(audioFormat) != mSurroundFormats.end() && enabled)
3648 || (mSurroundFormats.find(audioFormat) == mSurroundFormats.end() && !enabled)) {
3649 return NO_ERROR;
3650 }
3651
3652 // The operation is valid only when there is HDMI output available.
3653 if ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_HDMI) == 0) {
3654 return INVALID_OPERATION;
3655 }
3656
3657 if (enabled) {
3658 if (audioFormat == AUDIO_FORMAT_AAC_LC) {
3659 for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) {
3660 mSurroundFormats.insert(AAC_FORMATS[i]);
3661 }
3662 } else {
3663 mSurroundFormats.insert(audioFormat);
3664 }
3665 } else {
3666 if (audioFormat == AUDIO_FORMAT_AAC_LC) {
3667 for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) {
3668 mSurroundFormats.erase(AAC_FORMATS[i]);
3669 }
3670 } else {
3671 mSurroundFormats.erase(audioFormat);
3672 }
3673 }
3674
3675 sp<SwAudioOutputDescriptor> outputDesc;
3676 bool profileUpdated = false;
3677 DeviceVector hdmiOutputDevices = mAvailableOutputDevices.getDevicesFromType(
3678 AUDIO_DEVICE_OUT_HDMI);
3679 for (size_t i = 0; i < hdmiOutputDevices.size(); i++) {
3680 // Simulate reconnection to update enabled surround sound formats.
3681 String8 address = hdmiOutputDevices[i]->mAddress;
3682 String8 name = hdmiOutputDevices[i]->getName();
3683 status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI,
3684 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
3685 address.c_str(),
3686 name.c_str());
3687 if (status != NO_ERROR) {
3688 continue;
3689 }
3690 status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI,
3691 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
3692 address.c_str(),
3693 name.c_str());
3694 profileUpdated |= (status == NO_ERROR);
3695 }
3696 DeviceVector hdmiInputDevices = mAvailableInputDevices.getDevicesFromType(
3697 AUDIO_DEVICE_IN_HDMI);
3698 for (size_t i = 0; i < hdmiInputDevices.size(); i++) {
3699 // Simulate reconnection to update enabled surround sound formats.
3700 String8 address = hdmiInputDevices[i]->mAddress;
3701 String8 name = hdmiInputDevices[i]->getName();
3702 status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI,
3703 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
3704 address.c_str(),
3705 name.c_str());
3706 if (status != NO_ERROR) {
3707 continue;
3708 }
3709 status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI,
3710 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
3711 address.c_str(),
3712 name.c_str());
3713 profileUpdated |= (status == NO_ERROR);
3714 }
3715
3716 // Undo the surround formats change due to no audio profiles updated.
3717 if (!profileUpdated) {
3718 if (enabled) {
3719 if (audioFormat == AUDIO_FORMAT_AAC_LC) {
3720 for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) {
3721 mSurroundFormats.erase(AAC_FORMATS[i]);
3722 }
3723 } else {
3724 mSurroundFormats.erase(audioFormat);
3725 }
3726 } else {
3727 if (audioFormat == AUDIO_FORMAT_AAC_LC) {
3728 for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) {
3729 mSurroundFormats.insert(AAC_FORMATS[i]);
3730 }
3731 } else {
3732 mSurroundFormats.insert(audioFormat);
3733 }
3734 }
3735 }
3736
3737 return profileUpdated ? NO_ERROR : INVALID_OPERATION;
3738}
3739
Svet Ganovf4ddfef2018-01-16 07:37:58 -08003740void AudioPolicyManager::setRecordSilenced(uid_t uid, bool silenced)
3741{
3742 ALOGV("AudioPolicyManager:setRecordSilenced(uid:%d, silenced:%d)", uid, silenced);
3743
3744 Vector<sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
3745 for (size_t i = 0; i < activeInputs.size(); i++) {
3746 sp<AudioInputDescriptor> activeDesc = activeInputs[i];
3747 AudioSessionCollection activeSessions = activeDesc->getAudioSessions(true);
3748 for (size_t j = 0; j < activeSessions.size(); j++) {
3749 sp<AudioSession> activeSession = activeSessions.valueAt(j);
3750 if (activeSession->uid() == uid) {
3751 activeSession->setSilenced(silenced);
3752 }
3753 }
3754 }
3755}
3756
Eric Laurentd60560a2015-04-10 11:31:20 -07003757status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3758{
3759 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3760
3761 sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle);
3762 if (patchDesc == 0) {
3763 ALOGW("%s source has no patch with handle %d", __FUNCTION__,
3764 sourceDesc->mPatchDesc->mHandle);
3765 return BAD_VALUE;
3766 }
3767 removeAudioPatch(sourceDesc->mPatchDesc->mHandle);
3768
Eric Laurent28d09f02016-03-08 10:43:05 -08003769 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003770 sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote();
3771 if (swOutputDesc != 0) {
Eric Laurent733ce942017-12-07 12:18:25 -08003772 status_t status = stopSource(swOutputDesc, stream, false);
3773 if (status == NO_ERROR) {
3774 swOutputDesc->stop();
3775 }
Eric Laurentd60560a2015-04-10 11:31:20 -07003776 mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3777 } else {
3778 sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote();
3779 if (hwOutputDesc != 0) {
3780 // release patch between src device and output device
3781 // close Hwoutput and remove from mHwOutputs
3782 } else {
3783 ALOGW("%s source has neither SW nor HW output", __FUNCTION__);
3784 }
3785 }
3786 return NO_ERROR;
3787}
3788
3789sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput(
3790 audio_io_handle_t output, routing_strategy strategy)
3791{
3792 sp<AudioSourceDescriptor> source;
3793 for (size_t i = 0; i < mAudioSources.size(); i++) {
3794 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3795 routing_strategy sourceStrategy =
3796 (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
3797 sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote();
3798 if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) {
3799 source = sourceDesc;
3800 break;
3801 }
3802 }
3803 return source;
Eric Laurent554a2772015-04-10 11:29:24 -07003804}
3805
Eric Laurente552edb2014-03-10 17:42:56 -07003806// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07003807// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07003808// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07003809uint32_t AudioPolicyManager::nextAudioPortGeneration()
3810{
Mikhail Naganov2773dd72017-12-08 10:12:11 -08003811 return mAudioPortGeneration++;
Eric Laurent6a94d692014-05-20 11:18:06 -07003812}
3813
Jaekyun Seok0d4a6af2017-02-17 17:10:17 +09003814#ifdef USE_XML_AUDIO_POLICY_CONF
3815// Treblized audio policy xml config will be located in /odm/etc or /vendor/etc.
3816static const char *kConfigLocationList[] =
3817 {"/odm/etc", "/vendor/etc", "/system/etc"};
3818static const int kConfigLocationListSize =
3819 (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0]));
3820
3821static status_t deserializeAudioPolicyXmlConfig(AudioPolicyConfig &config) {
3822 char audioPolicyXmlConfigFile[AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH];
Petri Gyntherf497f292018-04-17 18:46:10 -07003823 std::vector<const char*> fileNames;
Jaekyun Seok0d4a6af2017-02-17 17:10:17 +09003824 status_t ret;
3825
Petri Gyntherf497f292018-04-17 18:46:10 -07003826 if (property_get_bool("ro.bluetooth.a2dp_offload.supported", false) &&
3827 property_get_bool("persist.bluetooth.a2dp_offload.disabled", false)) {
3828 // A2DP offload supported but disabled: try to use special XML file
3829 fileNames.push_back(AUDIO_POLICY_A2DP_OFFLOAD_DISABLED_XML_CONFIG_FILE_NAME);
3830 }
3831 fileNames.push_back(AUDIO_POLICY_XML_CONFIG_FILE_NAME);
3832
3833 for (const char* fileName : fileNames) {
3834 for (int i = 0; i < kConfigLocationListSize; i++) {
3835 PolicySerializer serializer;
3836 snprintf(audioPolicyXmlConfigFile, sizeof(audioPolicyXmlConfigFile),
3837 "%s/%s", kConfigLocationList[i], fileName);
3838 ret = serializer.deserialize(audioPolicyXmlConfigFile, config);
3839 if (ret == NO_ERROR) {
Mikhail Naganov2e5167e12018-04-19 13:41:22 -07003840 config.setSource(audioPolicyXmlConfigFile);
Petri Gyntherf497f292018-04-17 18:46:10 -07003841 return ret;
3842 }
Jaekyun Seok0d4a6af2017-02-17 17:10:17 +09003843 }
3844 }
3845 return ret;
3846}
3847#endif
3848
Mikhail Naganovad3f8a12017-12-12 13:24:23 -08003849AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface,
3850 bool /*forTesting*/)
Eric Laurente552edb2014-03-10 17:42:56 -07003851 :
Mikhail Naganovad3f8a12017-12-12 13:24:23 -08003852 mUidCached(getuid()),
3853 mpClientInterface(clientInterface),
Eric Laurente552edb2014-03-10 17:42:56 -07003854 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07003855 mA2dpSuspended(false),
Mikhail Naganovad3f8a12017-12-12 13:24:23 -08003856#ifdef USE_XML_AUDIO_POLICY_CONF
3857 mVolumeCurves(new VolumeCurvesCollection()),
3858 mConfig(mHwModulesAll, mAvailableOutputDevices, mAvailableInputDevices,
Mikhail Naganovbcbcb1b2017-12-13 13:03:35 -08003859 mDefaultOutputDevice, static_cast<VolumeCurvesCollection*>(mVolumeCurves.get())),
Mikhail Naganovad3f8a12017-12-12 13:24:23 -08003860#else
3861 mVolumeCurves(new StreamDescriptorCollection()),
3862 mConfig(mHwModulesAll, mAvailableOutputDevices, mAvailableInputDevices,
3863 mDefaultOutputDevice),
3864#endif
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003865 mAudioPortGeneration(1),
3866 mBeaconMuteRefCount(0),
3867 mBeaconPlayingRefCount(0),
Eric Laurent9459fb02015-08-12 18:36:32 -07003868 mBeaconMuted(false),
Andy Hung2ddee192015-12-18 17:34:44 -08003869 mTtsOutputAvailable(false),
Eric Laurent36829f92017-04-07 19:04:42 -07003870 mMasterMono(false),
Chris Thornton2b864642017-06-27 21:26:07 -07003871 mMusicEffectOutput(AUDIO_IO_HANDLE_NONE),
3872 mHasComputedSoundTriggerSupportsConcurrentCapture(false)
Eric Laurente552edb2014-03-10 17:42:56 -07003873{
Mikhail Naganovad3f8a12017-12-12 13:24:23 -08003874}
François Gaffied1ab2bd2015-12-02 18:20:06 +01003875
Mikhail Naganovad3f8a12017-12-12 13:24:23 -08003876AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
3877 : AudioPolicyManager(clientInterface, false /*forTesting*/)
3878{
3879 loadConfig();
3880 initialize();
3881}
François Gaffied1ab2bd2015-12-02 18:20:06 +01003882
Mikhail Naganovad3f8a12017-12-12 13:24:23 -08003883void AudioPolicyManager::loadConfig() {
François Gaffied1ab2bd2015-12-02 18:20:06 +01003884#ifdef USE_XML_AUDIO_POLICY_CONF
Mikhail Naganovad3f8a12017-12-12 13:24:23 -08003885 if (deserializeAudioPolicyXmlConfig(getConfig()) != NO_ERROR) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01003886#else
Mikhail Naganovad3f8a12017-12-12 13:24:23 -08003887 if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, getConfig()) != NO_ERROR)
3888 && (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, getConfig()) != NO_ERROR)) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01003889#endif
3890 ALOGE("could not load audio policy configuration file, setting defaults");
Mikhail Naganovad3f8a12017-12-12 13:24:23 -08003891 getConfig().setDefault();
François Gaffied1ab2bd2015-12-02 18:20:06 +01003892 }
Mikhail Naganovad3f8a12017-12-12 13:24:23 -08003893}
3894
3895status_t AudioPolicyManager::initialize() {
3896 mVolumeCurves->initializeVolumeCurves(getConfig().isSpeakerDrcEnabled());
François Gaffied1ab2bd2015-12-02 18:20:06 +01003897
3898 // Once policy config has been parsed, retrieve an instance of the engine and initialize it.
François Gaffie2110e042015-03-24 08:41:51 +01003899 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
3900 if (!engineInstance) {
3901 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
Mikhail Naganovad3f8a12017-12-12 13:24:23 -08003902 return NO_INIT;
François Gaffie2110e042015-03-24 08:41:51 +01003903 }
3904 // Retrieve the Policy Manager Interface
3905 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
3906 if (mEngine == NULL) {
3907 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
Mikhail Naganovad3f8a12017-12-12 13:24:23 -08003908 return NO_INIT;
François Gaffie2110e042015-03-24 08:41:51 +01003909 }
3910 mEngine->setObserver(this);
3911 status_t status = mEngine->initCheck();
Mikhail Naganovad3f8a12017-12-12 13:24:23 -08003912 if (status != NO_ERROR) {
3913 LOG_FATAL("Policy engine not initialized(err=%d)", status);
3914 return status;
3915 }
François Gaffie2110e042015-03-24 08:41:51 +01003916
Eric Laurent3a4311c2014-03-17 12:00:47 -07003917 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07003918 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07003919 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
3920 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Mikhail Naganovd4120142017-12-06 15:49:22 -08003921 for (const auto& hwModule : mHwModulesAll) {
Mikhail Naganova5e165d2017-12-07 17:08:02 -08003922 hwModule->setHandle(mpClientInterface->loadHwModule(hwModule->getName()));
Mikhail Naganovd4120142017-12-06 15:49:22 -08003923 if (hwModule->getHandle() == AUDIO_MODULE_HANDLE_NONE) {
3924 ALOGW("could not open HW module %s", hwModule->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003925 continue;
3926 }
Mikhail Naganovd4120142017-12-06 15:49:22 -08003927 mHwModules.push_back(hwModule);
Eric Laurente552edb2014-03-10 17:42:56 -07003928 // open all output streams needed to access attached devices
3929 // except for direct output streams that are only opened when they are actually
3930 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07003931 // This also validates mAvailableOutputDevices list
Mikhail Naganova5e165d2017-12-07 17:08:02 -08003932 for (const auto& outProfile : hwModule->getOutputProfiles()) {
Eric Laurent3974e3b2017-12-07 17:58:43 -08003933 if (!outProfile->canOpenNewIo()) {
3934 ALOGE("Invalid Output profile max open count %u for profile %s",
3935 outProfile->maxOpenCount, outProfile->getTagName().c_str());
3936 continue;
3937 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003938 if (!outProfile->hasSupportedDevices()) {
Mikhail Naganovd4120142017-12-06 15:49:22 -08003939 ALOGW("Output profile contains no device on module %s", hwModule->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003940 continue;
3941 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003942 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) {
Eric Laurent9459fb02015-08-12 18:36:32 -07003943 mTtsOutputAvailable = true;
3944 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003945
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003946 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
Eric Laurentd78f1532014-09-16 16:38:20 -07003947 continue;
3948 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003949 audio_devices_t profileType = outProfile->getSupportedDevicesType();
François Gaffie53615e22015-03-19 09:24:12 +01003950 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
3951 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07003952 } else {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003953 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003954 // outputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003955 profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes);
Eric Laurente552edb2014-03-10 17:42:56 -07003956 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003957 if ((profileType & outputDeviceTypes) == 0) {
3958 continue;
3959 }
Eric Laurentc75307b2015-03-17 15:29:32 -07003960 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
3961 mpClientInterface);
Eric Laurentc40d9692016-04-13 19:14:13 -07003962 const DeviceVector &supportedDevices = outProfile->getSupportedDevices();
3963 const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType);
3964 String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress
3965 : String8("");
Eric Laurentd78f1532014-09-16 16:38:20 -07003966 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurentfe231122017-11-17 17:48:06 -08003967 status_t status = outputDesc->open(nullptr, profileType, address,
3968 AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output);
Eric Laurentd78f1532014-09-16 16:38:20 -07003969
3970 if (status != NO_ERROR) {
3971 ALOGW("Cannot open output stream for device %08x on hw module %s",
3972 outputDesc->mDevice,
Mikhail Naganovd4120142017-12-06 15:49:22 -08003973 hwModule->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003974 } else {
Mikhail Naganovcf84e592017-12-07 11:25:11 -08003975 for (const auto& dev : supportedDevices) {
3976 ssize_t index = mAvailableOutputDevices.indexOf(dev);
Eric Laurentd78f1532014-09-16 16:38:20 -07003977 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08003978 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
Mikhail Naganovd4120142017-12-06 15:49:22 -08003979 mAvailableOutputDevices[index]->attach(hwModule);
Eric Laurentd78f1532014-09-16 16:38:20 -07003980 }
3981 }
3982 if (mPrimaryOutput == 0 &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003983 outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003984 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07003985 }
3986 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003987 setOutputDevice(outputDesc,
Eric Laurentfe231122017-11-17 17:48:06 -08003988 profileType,
Eric Laurentc40d9692016-04-13 19:14:13 -07003989 true,
3990 0,
3991 NULL,
Eric Laurentfe231122017-11-17 17:48:06 -08003992 address);
Eric Laurentd78f1532014-09-16 16:38:20 -07003993 }
Eric Laurente552edb2014-03-10 17:42:56 -07003994 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003995 // open input streams needed to access attached devices to validate
3996 // mAvailableInputDevices list
Mikhail Naganova5e165d2017-12-07 17:08:02 -08003997 for (const auto& inProfile : hwModule->getInputProfiles()) {
Eric Laurent3974e3b2017-12-07 17:58:43 -08003998 if (!inProfile->canOpenNewIo()) {
3999 ALOGE("Invalid Input profile max open count %u for profile %s",
4000 inProfile->maxOpenCount, inProfile->getTagName().c_str());
4001 continue;
4002 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004003 if (!inProfile->hasSupportedDevices()) {
Mikhail Naganovd4120142017-12-06 15:49:22 -08004004 ALOGW("Input profile contains no device on module %s", hwModule->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07004005 continue;
4006 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004007 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07004008 // inputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004009 audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes);
4010
Eric Laurentd78f1532014-09-16 16:38:20 -07004011 if ((profileType & inputDeviceTypes) == 0) {
4012 continue;
4013 }
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08004014 sp<AudioInputDescriptor> inputDesc =
Eric Laurentfe231122017-11-17 17:48:06 -08004015 new AudioInputDescriptor(inProfile, mpClientInterface);
Eric Laurentd78f1532014-09-16 16:38:20 -07004016
Eric Laurent53b810e2017-12-10 17:25:10 -08004017 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
4018 // the inputs vector must be of size >= 1, but we don't want to crash here
4019 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
4020 : String8("");
4021 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
4022 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
4023
Eric Laurentd78f1532014-09-16 16:38:20 -07004024 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurentfe231122017-11-17 17:48:06 -08004025 status_t status = inputDesc->open(nullptr,
4026 profileType,
Eric Laurent53b810e2017-12-10 17:25:10 -08004027 address,
Eric Laurentfe231122017-11-17 17:48:06 -08004028 AUDIO_SOURCE_MIC,
4029 AUDIO_INPUT_FLAG_NONE,
4030 &input);
Eric Laurentd78f1532014-09-16 16:38:20 -07004031
4032 if (status == NO_ERROR) {
Mikhail Naganovcf84e592017-12-07 11:25:11 -08004033 for (const auto& dev : inProfile->getSupportedDevices()) {
4034 ssize_t index = mAvailableInputDevices.indexOf(dev);
Eric Laurentd78f1532014-09-16 16:38:20 -07004035 // give a valid ID to an attached device once confirmed it is reachable
Eric Laurent45aabc32015-08-06 09:11:13 -07004036 if (index >= 0) {
4037 sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index];
4038 if (!devDesc->isAttached()) {
Mikhail Naganovd4120142017-12-06 15:49:22 -08004039 devDesc->attach(hwModule);
Eric Laurent83efe1c2017-07-09 16:51:08 -07004040 devDesc->importAudioPort(inProfile, true);
Eric Laurent45aabc32015-08-06 09:11:13 -07004041 }
Eric Laurentd78f1532014-09-16 16:38:20 -07004042 }
4043 }
Eric Laurentfe231122017-11-17 17:48:06 -08004044 inputDesc->close();
Eric Laurentd78f1532014-09-16 16:38:20 -07004045 } else {
4046 ALOGW("Cannot open input stream for device %08x on hw module %s",
Eric Laurentfe231122017-11-17 17:48:06 -08004047 profileType,
Mikhail Naganovd4120142017-12-06 15:49:22 -08004048 hwModule->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07004049 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07004050 }
4051 }
4052 // make sure all attached devices have been allocated a unique ID
4053 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08004054 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004055 ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07004056 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
4057 continue;
4058 }
François Gaffie2110e042015-03-24 08:41:51 +01004059 // The device is now validated and can be appended to the available devices of the engine
4060 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
4061 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004062 i++;
4063 }
4064 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08004065 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01004066 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07004067 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
4068 continue;
4069 }
François Gaffie2110e042015-03-24 08:41:51 +01004070 // The device is now validated and can be appended to the available devices of the engine
4071 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
4072 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004073 i++;
4074 }
4075 // make sure default device is reachable
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004076 if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01004077 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Mikhail Naganovad3f8a12017-12-12 13:24:23 -08004078 status = NO_INIT;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004079 }
jiabin9ff780e2018-03-19 18:19:52 -07004080 // If microphones address is empty, set it according to device type
4081 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
4082 if (mAvailableInputDevices[i]->mAddress.isEmpty()) {
4083 if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BUILTIN_MIC) {
4084 mAvailableInputDevices[i]->mAddress = String8(AUDIO_BOTTOM_MICROPHONE_ADDRESS);
4085 } else if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BACK_MIC) {
4086 mAvailableInputDevices[i]->mAddress = String8(AUDIO_BACK_MICROPHONE_ADDRESS);
4087 }
4088 }
4089 }
Eric Laurente552edb2014-03-10 17:42:56 -07004090
Mikhail Naganovad3f8a12017-12-12 13:24:23 -08004091 if (mPrimaryOutput == 0) {
4092 ALOGE("Failed to open primary output");
4093 status = NO_INIT;
4094 }
Eric Laurente552edb2014-03-10 17:42:56 -07004095
4096 updateDevicesAndOutputs();
Mikhail Naganovad3f8a12017-12-12 13:24:23 -08004097 return status;
Eric Laurente552edb2014-03-10 17:42:56 -07004098}
4099
Eric Laurente0720872014-03-11 09:30:41 -07004100AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07004101{
Eric Laurente552edb2014-03-10 17:42:56 -07004102 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentfe231122017-11-17 17:48:06 -08004103 mOutputs.valueAt(i)->close();
Eric Laurente552edb2014-03-10 17:42:56 -07004104 }
4105 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurentfe231122017-11-17 17:48:06 -08004106 mInputs.valueAt(i)->close();
Eric Laurente552edb2014-03-10 17:42:56 -07004107 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07004108 mAvailableOutputDevices.clear();
4109 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07004110 mOutputs.clear();
4111 mInputs.clear();
4112 mHwModules.clear();
Mikhail Naganovd4120142017-12-06 15:49:22 -08004113 mHwModulesAll.clear();
jiabin81772902018-04-02 17:52:27 -07004114 mSurroundFormats.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07004115}
4116
Eric Laurente0720872014-03-11 09:30:41 -07004117status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07004118{
Eric Laurent87ffa392015-05-22 10:32:38 -07004119 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07004120}
4121
Eric Laurente552edb2014-03-10 17:42:56 -07004122// ---
4123
Eric Laurent98e38192018-02-15 18:31:53 -08004124void AudioPolicyManager::addOutput(audio_io_handle_t output,
4125 const sp<SwAudioOutputDescriptor>& outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07004126{
Eric Laurent1c333e22014-05-20 10:48:17 -07004127 mOutputs.add(output, outputDesc);
Eric Laurent98e38192018-02-15 18:31:53 -08004128 applyStreamVolumes(outputDesc, AUDIO_DEVICE_NONE, 0 /* delayMs */, true /* force */);
Andy Hung2ddee192015-12-18 17:34:44 -08004129 updateMono(output); // update mono status when adding to output list
Eric Laurent36829f92017-04-07 19:04:42 -07004130 selectOutputForMusicEffects();
Eric Laurent6a94d692014-05-20 11:18:06 -07004131 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07004132}
4133
François Gaffie53615e22015-03-19 09:24:12 +01004134void AudioPolicyManager::removeOutput(audio_io_handle_t output)
4135{
4136 mOutputs.removeItem(output);
Eric Laurent36829f92017-04-07 19:04:42 -07004137 selectOutputForMusicEffects();
François Gaffie53615e22015-03-19 09:24:12 +01004138}
4139
Eric Laurent98e38192018-02-15 18:31:53 -08004140void AudioPolicyManager::addInput(audio_io_handle_t input,
4141 const sp<AudioInputDescriptor>& inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07004142{
Eric Laurent1c333e22014-05-20 10:48:17 -07004143 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07004144 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07004145}
Eric Laurente552edb2014-03-10 17:42:56 -07004146
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004147void AudioPolicyManager::findIoHandlesByAddress(const sp<SwAudioOutputDescriptor>& desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08004148 const audio_devices_t device /*in*/,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004149 const String8& address /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004150 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08004151 sp<DeviceDescriptor> devDesc =
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004152 desc->mProfile->getSupportedDeviceByAddress(device, address);
keunyoung3190e672014-12-30 13:00:52 -08004153 if (devDesc != 0) {
4154 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
4155 desc->mIoHandle, address.string());
4156 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004157 }
4158}
4159
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004160status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01004161 audio_policy_dev_state_t state,
4162 SortedVector<audio_io_handle_t>& outputs,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004163 const String8& address)
Eric Laurente552edb2014-03-10 17:42:56 -07004164{
François Gaffie53615e22015-03-19 09:24:12 +01004165 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07004166 sp<SwAudioOutputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07004167
4168 if (audio_device_is_digital(device)) {
4169 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08004170 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07004171 }
Eric Laurente552edb2014-03-10 17:42:56 -07004172
Eric Laurent3b73df72014-03-11 09:06:29 -07004173 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07004174 // first list already open outputs that can be routed to this device
4175 for (size_t i = 0; i < mOutputs.size(); i++) {
4176 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07004177 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01004178 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004179 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
4180 outputs.add(mOutputs.keyAt(i));
4181 } else {
4182 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08004183 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004184 }
Eric Laurente552edb2014-03-10 17:42:56 -07004185 }
4186 }
4187 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07004188 SortedVector< sp<IOProfile> > profiles;
Mikhail Naganov7e22e942017-12-07 10:04:29 -08004189 for (const auto& hwModule : mHwModules) {
Mikhail Naganova5e165d2017-12-07 17:08:02 -08004190 for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) {
4191 sp<IOProfile> profile = hwModule->getOutputProfiles()[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004192 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01004193 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004194 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004195 profiles.add(profile);
Mikhail Naganovd4120142017-12-06 15:49:22 -08004196 ALOGV("checkOutputsForDevice(): adding profile %zu from module %s",
4197 j, hwModule->getName());
Eric Laurent275e8e92014-11-30 15:14:47 -08004198 }
Eric Laurente552edb2014-03-10 17:42:56 -07004199 }
4200 }
4201 }
4202
Eric Laurent7b279bb2015-12-14 10:18:23 -08004203 ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size());
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004204
Eric Laurente552edb2014-03-10 17:42:56 -07004205 if (profiles.isEmpty() && outputs.isEmpty()) {
4206 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
4207 return BAD_VALUE;
4208 }
4209
4210 // open outputs for matching profiles if needed. Direct outputs are also opened to
4211 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
4212 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004213 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07004214
4215 // nothing to do if one output is already opened for this profile
4216 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004217 for (j = 0; j < outputs.size(); j++) {
4218 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07004219 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07004220 // matching profile: save the sample rates, format and channel masks supported
4221 // by the profile in our device descriptor
Paul McLean9080a4c2015-06-18 08:24:02 -07004222 if (audio_device_is_digital(device)) {
4223 devDesc->importAudioPort(profile);
4224 }
Eric Laurente552edb2014-03-10 17:42:56 -07004225 break;
4226 }
4227 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004228 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07004229 continue;
4230 }
4231
Eric Laurent3974e3b2017-12-07 17:58:43 -08004232 if (!profile->canOpenNewIo()) {
4233 ALOGW("Max Output number %u already opened for this profile %s",
4234 profile->maxOpenCount, profile->getTagName().c_str());
4235 continue;
4236 }
4237
Eric Laurent83efe1c2017-07-09 16:51:08 -07004238 ALOGV("opening output for device %08x with params %s profile %p name %s",
4239 device, address.string(), profile.get(), profile->getName().string());
Eric Laurentc75307b2015-03-17 15:29:32 -07004240 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004241 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurentfe231122017-11-17 17:48:06 -08004242 status_t status = desc->open(nullptr, device, address,
4243 AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output);
Eric Laurente552edb2014-03-10 17:42:56 -07004244
Eric Laurentfe231122017-11-17 17:48:06 -08004245 if (status == NO_ERROR) {
Eric Laurentd4692962014-05-05 18:13:44 -07004246 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07004247 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07004248 char *param = audio_device_address_to_parameter(device, address);
4249 mpClientInterface->setParameters(output, String8(param));
4250 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07004251 }
Phil Burk00eeb322016-03-31 12:41:00 -07004252 updateAudioProfiles(device, output, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01004253 if (!profile->hasValidAudioProfile()) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004254 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentfe231122017-11-17 17:48:06 -08004255 desc->close();
Eric Laurentcf2c0212014-07-25 16:20:43 -07004256 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01004257 } else if (profile->hasDynamicAudioProfile()) {
Eric Laurentfe231122017-11-17 17:48:06 -08004258 desc->close();
Phil Burk702b1052016-03-02 16:38:26 -08004259 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentfe231122017-11-17 17:48:06 -08004260 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
4261 profile->pickAudioProfile(
4262 config.sample_rate, config.channel_mask, config.format);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004263 config.offload_info.sample_rate = config.sample_rate;
4264 config.offload_info.channel_mask = config.channel_mask;
4265 config.offload_info.format = config.format;
Eric Laurentfe231122017-11-17 17:48:06 -08004266
4267 status_t status = desc->open(&config, device, address, AUDIO_STREAM_DEFAULT,
4268 AUDIO_OUTPUT_FLAG_NONE, &output);
4269 if (status != NO_ERROR) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07004270 output = AUDIO_IO_HANDLE_NONE;
4271 }
Eric Laurentd4692962014-05-05 18:13:44 -07004272 }
4273
Eric Laurentcf2c0212014-07-25 16:20:43 -07004274 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07004275 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01004276 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01004277 sp<AudioPolicyMix> policyMix;
4278 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004279 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
4280 address.string());
4281 }
François Gaffie036e1e92015-03-19 10:16:24 +01004282 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07004283 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01004284
Eric Laurent87ffa392015-05-22 10:32:38 -07004285 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
4286 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08004287 // no duplicated output for direct outputs and
4288 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07004289 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07004290
Eric Laurentd4692962014-05-05 18:13:44 -07004291 //TODO: configure audio effect output stage here
4292
4293 // open a duplicating output thread for the new output and the primary output
Eric Laurent5babc4f2018-02-15 12:33:44 -08004294 sp<SwAudioOutputDescriptor> dupOutputDesc =
4295 new SwAudioOutputDescriptor(NULL, mpClientInterface);
4296 status_t status = dupOutputDesc->openDuplicating(mPrimaryOutput, desc,
4297 &duplicatedOutput);
4298 if (status == NO_ERROR) {
Eric Laurentd4692962014-05-05 18:13:44 -07004299 // add duplicated output descriptor
Eric Laurentd4692962014-05-05 18:13:44 -07004300 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentd4692962014-05-05 18:13:44 -07004301 } else {
4302 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07004303 mPrimaryOutput->mIoHandle, output);
Eric Laurentfe231122017-11-17 17:48:06 -08004304 desc->close();
François Gaffie53615e22015-03-19 09:24:12 +01004305 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07004306 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07004307 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07004308 }
Eric Laurente552edb2014-03-10 17:42:56 -07004309 }
4310 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07004311 } else {
4312 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07004313 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07004314 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07004315 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004316 profiles.removeAt(profile_index);
4317 profile_index--;
4318 } else {
4319 outputs.add(output);
Paul McLean9080a4c2015-06-18 08:24:02 -07004320 // Load digital format info only for digital devices
4321 if (audio_device_is_digital(device)) {
4322 devDesc->importAudioPort(profile);
4323 }
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07004324
François Gaffie53615e22015-03-19 09:24:12 +01004325 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004326 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
4327 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07004328 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004329 NULL/*patch handle*/, address.string());
4330 }
Eric Laurente552edb2014-03-10 17:42:56 -07004331 ALOGV("checkOutputsForDevice(): adding output %d", output);
4332 }
4333 }
4334
4335 if (profiles.isEmpty()) {
4336 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
4337 return BAD_VALUE;
4338 }
Eric Laurentd4692962014-05-05 18:13:44 -07004339 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07004340 // check if one opened output is not needed any more after disconnecting one device
4341 for (size_t i = 0; i < mOutputs.size(); i++) {
4342 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004343 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004344 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01004345 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07004346 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08004347 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07004348 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004349 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
4350 mOutputs.keyAt(i));
4351 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004352 }
Eric Laurente552edb2014-03-10 17:42:56 -07004353 }
4354 }
Eric Laurentd4692962014-05-05 18:13:44 -07004355 // Clear any profiles associated with the disconnected device.
Mikhail Naganov7e22e942017-12-07 10:04:29 -08004356 for (const auto& hwModule : mHwModules) {
Mikhail Naganova5e165d2017-12-07 17:08:02 -08004357 for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) {
4358 sp<IOProfile> profile = hwModule->getOutputProfiles()[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004359 if (profile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07004360 ALOGV("checkOutputsForDevice(): "
Mikhail Naganovd4120142017-12-06 15:49:22 -08004361 "clearing direct output profile %zu on module %s",
4362 j, hwModule->getName());
François Gaffie112b0af2015-11-19 16:13:25 +01004363 profile->clearAudioProfiles();
Eric Laurente552edb2014-03-10 17:42:56 -07004364 }
4365 }
4366 }
4367 }
4368 return NO_ERROR;
4369}
4370
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004371status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01004372 audio_policy_dev_state_t state,
4373 SortedVector<audio_io_handle_t>& inputs,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004374 const String8& address)
Eric Laurentd4692962014-05-05 18:13:44 -07004375{
Paul McLean9080a4c2015-06-18 08:24:02 -07004376 audio_devices_t device = devDesc->type();
Eric Laurent1f2f2232014-06-02 12:01:23 -07004377 sp<AudioInputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07004378
4379 if (audio_device_is_digital(device)) {
4380 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08004381 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07004382 }
4383
Eric Laurentd4692962014-05-05 18:13:44 -07004384 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
4385 // first list already open inputs that can be routed to this device
4386 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
4387 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004388 if (desc->mProfile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07004389 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
4390 inputs.add(mInputs.keyAt(input_index));
4391 }
4392 }
4393
4394 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07004395 SortedVector< sp<IOProfile> > profiles;
Mikhail Naganov7e22e942017-12-07 10:04:29 -08004396 for (const auto& hwModule : mHwModules) {
Eric Laurentd4692962014-05-05 18:13:44 -07004397 for (size_t profile_index = 0;
Mikhail Naganova5e165d2017-12-07 17:08:02 -08004398 profile_index < hwModule->getInputProfiles().size();
Mikhail Naganov7e22e942017-12-07 10:04:29 -08004399 profile_index++) {
Mikhail Naganova5e165d2017-12-07 17:08:02 -08004400 sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index];
Eric Laurent275e8e92014-11-30 15:14:47 -08004401
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004402 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01004403 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004404 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004405 profiles.add(profile);
Mikhail Naganovd4120142017-12-06 15:49:22 -08004406 ALOGV("checkInputsForDevice(): adding profile %zu from module %s",
4407 profile_index, hwModule->getName());
Eric Laurent275e8e92014-11-30 15:14:47 -08004408 }
Eric Laurentd4692962014-05-05 18:13:44 -07004409 }
4410 }
4411 }
4412
4413 if (profiles.isEmpty() && inputs.isEmpty()) {
4414 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
4415 return BAD_VALUE;
4416 }
4417
4418 // open inputs for matching profiles if needed. Direct inputs are also opened to
4419 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
4420 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
4421
Eric Laurent1c333e22014-05-20 10:48:17 -07004422 sp<IOProfile> profile = profiles[profile_index];
Eric Laurent3974e3b2017-12-07 17:58:43 -08004423
Eric Laurentd4692962014-05-05 18:13:44 -07004424 // nothing to do if one input is already opened for this profile
4425 size_t input_index;
4426 for (input_index = 0; input_index < mInputs.size(); input_index++) {
4427 desc = mInputs.valueAt(input_index);
4428 if (desc->mProfile == profile) {
Paul McLean9080a4c2015-06-18 08:24:02 -07004429 if (audio_device_is_digital(device)) {
4430 devDesc->importAudioPort(profile);
4431 }
Eric Laurentd4692962014-05-05 18:13:44 -07004432 break;
4433 }
4434 }
4435 if (input_index != mInputs.size()) {
4436 continue;
4437 }
4438
Eric Laurent3974e3b2017-12-07 17:58:43 -08004439 if (!profile->canOpenNewIo()) {
4440 ALOGW("Max Input number %u already opened for this profile %s",
4441 profile->maxOpenCount, profile->getTagName().c_str());
4442 continue;
4443 }
4444
Eric Laurentfe231122017-11-17 17:48:06 -08004445 desc = new AudioInputDescriptor(profile, mpClientInterface);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004446 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurentfe231122017-11-17 17:48:06 -08004447 status_t status = desc->open(nullptr,
4448 device,
4449 address,
4450 AUDIO_SOURCE_MIC,
4451 AUDIO_INPUT_FLAG_NONE,
4452 &input);
Eric Laurentd4692962014-05-05 18:13:44 -07004453
Eric Laurentcf2c0212014-07-25 16:20:43 -07004454 if (status == NO_ERROR) {
Eric Laurentd4692962014-05-05 18:13:44 -07004455 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07004456 char *param = audio_device_address_to_parameter(device, address);
4457 mpClientInterface->setParameters(input, String8(param));
4458 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07004459 }
Phil Burk00eeb322016-03-31 12:41:00 -07004460 updateAudioProfiles(device, input, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01004461 if (!profile->hasValidAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07004462 ALOGW("checkInputsForDevice() direct input missing param");
Eric Laurentfe231122017-11-17 17:48:06 -08004463 desc->close();
Eric Laurentcf2c0212014-07-25 16:20:43 -07004464 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07004465 }
4466
4467 if (input != 0) {
4468 addInput(input, desc);
4469 }
4470 } // endif input != 0
4471
Eric Laurentcf2c0212014-07-25 16:20:43 -07004472 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07004473 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07004474 profiles.removeAt(profile_index);
4475 profile_index--;
4476 } else {
4477 inputs.add(input);
Paul McLean9080a4c2015-06-18 08:24:02 -07004478 if (audio_device_is_digital(device)) {
4479 devDesc->importAudioPort(profile);
4480 }
Eric Laurentd4692962014-05-05 18:13:44 -07004481 ALOGV("checkInputsForDevice(): adding input %d", input);
4482 }
4483 } // end scan profiles
4484
4485 if (profiles.isEmpty()) {
4486 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
4487 return BAD_VALUE;
4488 }
4489 } else {
4490 // Disconnect
4491 // check if one opened input is not needed any more after disconnecting one device
4492 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
4493 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004494 if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) {
Eric Laurentd4692962014-05-05 18:13:44 -07004495 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
4496 mInputs.keyAt(input_index));
4497 inputs.add(mInputs.keyAt(input_index));
4498 }
4499 }
4500 // Clear any profiles associated with the disconnected device.
Mikhail Naganovd4120142017-12-06 15:49:22 -08004501 for (const auto& hwModule : mHwModules) {
Eric Laurentd4692962014-05-05 18:13:44 -07004502 for (size_t profile_index = 0;
Mikhail Naganova5e165d2017-12-07 17:08:02 -08004503 profile_index < hwModule->getInputProfiles().size();
Eric Laurentd4692962014-05-05 18:13:44 -07004504 profile_index++) {
Mikhail Naganova5e165d2017-12-07 17:08:02 -08004505 sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004506 if (profile->supportDevice(device)) {
Mikhail Naganovd4120142017-12-06 15:49:22 -08004507 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %s",
4508 profile_index, hwModule->getName());
François Gaffie112b0af2015-11-19 16:13:25 +01004509 profile->clearAudioProfiles();
Eric Laurentd4692962014-05-05 18:13:44 -07004510 }
4511 }
4512 }
4513 } // end disconnect
4514
4515 return NO_ERROR;
4516}
4517
4518
Eric Laurente0720872014-03-11 09:30:41 -07004519void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07004520{
4521 ALOGV("closeOutput(%d)", output);
4522
Eric Laurentc75307b2015-03-17 15:29:32 -07004523 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004524 if (outputDesc == NULL) {
4525 ALOGW("closeOutput() unknown output %d", output);
4526 return;
4527 }
François Gaffie036e1e92015-03-19 10:16:24 +01004528 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08004529
Eric Laurente552edb2014-03-10 17:42:56 -07004530 // look for duplicated outputs connected to the output being removed.
4531 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004532 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07004533 if (dupOutputDesc->isDuplicated() &&
4534 (dupOutputDesc->mOutput1 == outputDesc ||
4535 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent733ce942017-12-07 12:18:25 -08004536 sp<SwAudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07004537 if (dupOutputDesc->mOutput1 == outputDesc) {
4538 outputDesc2 = dupOutputDesc->mOutput2;
4539 } else {
4540 outputDesc2 = dupOutputDesc->mOutput1;
4541 }
4542 // As all active tracks on duplicated output will be deleted,
4543 // and as they were also referenced on the other output, the reference
4544 // count for their stream type must be adjusted accordingly on
4545 // the other output.
Eric Laurent733ce942017-12-07 12:18:25 -08004546 bool wasActive = outputDesc2->isActive();
Eric Laurent3b73df72014-03-11 09:06:29 -07004547 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07004548 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07004549 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07004550 }
Eric Laurent733ce942017-12-07 12:18:25 -08004551 // stop() will be a no op if the output is still active but is needed in case all
4552 // active streams refcounts where cleared above
4553 if (wasActive) {
4554 outputDesc2->stop();
4555 }
Eric Laurente552edb2014-03-10 17:42:56 -07004556 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
4557 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
4558
4559 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01004560 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07004561 }
4562 }
4563
Eric Laurent05b90f82014-08-27 15:32:29 -07004564 nextAudioPortGeneration();
4565
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004566 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004567 if (index >= 0) {
4568 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07004569 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07004570 mAudioPatches.removeItemsAt(index);
4571 mpClientInterface->onAudioPatchListUpdate();
4572 }
4573
Eric Laurentfe231122017-11-17 17:48:06 -08004574 outputDesc->close();
Eric Laurente552edb2014-03-10 17:42:56 -07004575
François Gaffie53615e22015-03-19 09:24:12 +01004576 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004577 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07004578}
4579
4580void AudioPolicyManager::closeInput(audio_io_handle_t input)
4581{
4582 ALOGV("closeInput(%d)", input);
4583
4584 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
4585 if (inputDesc == NULL) {
4586 ALOGW("closeInput() unknown input %d", input);
4587 return;
4588 }
4589
Eric Laurent6a94d692014-05-20 11:18:06 -07004590 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07004591
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004592 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004593 if (index >= 0) {
4594 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07004595 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07004596 mAudioPatches.removeItemsAt(index);
4597 mpClientInterface->onAudioPatchListUpdate();
4598 }
4599
Eric Laurentfe231122017-11-17 17:48:06 -08004600 inputDesc->close();
Eric Laurent05b90f82014-08-27 15:32:29 -07004601 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07004602}
4603
Eric Laurentc75307b2015-03-17 15:29:32 -07004604SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
4605 audio_devices_t device,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004606 const SwAudioOutputCollection& openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07004607{
4608 SortedVector<audio_io_handle_t> outputs;
4609
4610 ALOGVV("getOutputsForDevice() device %04x", device);
4611 for (size_t i = 0; i < openOutputs.size(); i++) {
Eric Laurent37ddbb42016-08-10 16:19:14 -07004612 ALOGVV("output %zu isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004613 i, openOutputs.valueAt(i)->isDuplicated(),
4614 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004615 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
4616 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
4617 outputs.add(openOutputs.keyAt(i));
4618 }
4619 }
4620 return outputs;
4621}
4622
Eric Laurente0720872014-03-11 09:30:41 -07004623bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01004624 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07004625{
4626 if (outputs1.size() != outputs2.size()) {
4627 return false;
4628 }
4629 for (size_t i = 0; i < outputs1.size(); i++) {
4630 if (outputs1[i] != outputs2[i]) {
4631 return false;
4632 }
4633 }
4634 return true;
4635}
4636
Eric Laurente0720872014-03-11 09:30:41 -07004637void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07004638{
4639 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
4640 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
4641 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
4642 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
4643
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004644 // also take into account external policy-related changes: add all outputs which are
4645 // associated with policies in the "before" and "after" output vectors
4646 ALOGVV("checkOutputForStrategy(): policy related outputs");
4647 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004648 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004649 if (desc != 0 && desc->mPolicyMix != NULL) {
4650 srcOutputs.add(desc->mIoHandle);
4651 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
4652 }
4653 }
4654 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004655 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004656 if (desc != 0 && desc->mPolicyMix != NULL) {
4657 dstOutputs.add(desc->mIoHandle);
4658 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
4659 }
4660 }
4661
Eric Laurente552edb2014-03-10 17:42:56 -07004662 if (!vectorsEqual(srcOutputs,dstOutputs)) {
Eric Laurentac3a6902018-05-11 16:39:10 -07004663 // get maximum latency of all source outputs to determine the minimum mute time guaranteeing
4664 // audio from invalidated tracks will be rendered when unmuting
4665 uint32_t maxLatency = 0;
4666 for (audio_io_handle_t srcOut : srcOutputs) {
4667 sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut);
4668 if (desc != 0 && maxLatency < desc->latency()) {
4669 maxLatency = desc->latency();
4670 }
4671 }
Eric Laurente552edb2014-03-10 17:42:56 -07004672 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
4673 strategy, srcOutputs[0], dstOutputs[0]);
4674 // mute strategy while moving tracks from one output to another
Mikhail Naganovcf84e592017-12-07 11:25:11 -08004675 for (audio_io_handle_t srcOut : srcOutputs) {
Eric Laurentac3a6902018-05-11 16:39:10 -07004676 sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut);
4677 if (desc != 0 && isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004678 setStrategyMute(strategy, true, desc);
Eric Laurentac3a6902018-05-11 16:39:10 -07004679 setStrategyMute(strategy, false, desc, maxLatency * LATENCY_MUTE_FACTOR, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004680 }
Eric Laurentd60560a2015-04-10 11:31:20 -07004681 sp<AudioSourceDescriptor> source =
Mikhail Naganovcf84e592017-12-07 11:25:11 -08004682 getSourceForStrategyOnOutput(srcOut, strategy);
Eric Laurentd60560a2015-04-10 11:31:20 -07004683 if (source != 0){
4684 connectAudioSource(source);
4685 }
Eric Laurente552edb2014-03-10 17:42:56 -07004686 }
4687
4688 // Move effects associated to this strategy from previous output to new output
4689 if (strategy == STRATEGY_MEDIA) {
Eric Laurent36829f92017-04-07 19:04:42 -07004690 selectOutputForMusicEffects();
Eric Laurente552edb2014-03-10 17:42:56 -07004691 }
4692 // Move tracks associated to this strategy from previous output to new output
Eric Laurent794fde22016-03-11 09:50:45 -08004693 for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004694 if (getStrategy((audio_stream_type_t)i) == strategy) {
4695 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07004696 }
4697 }
4698 }
4699}
4700
Eric Laurente0720872014-03-11 09:30:41 -07004701void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07004702{
François Gaffie2110e042015-03-24 08:41:51 +01004703 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004704 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004705 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01004706 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004707 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004708 checkOutputForStrategy(STRATEGY_SONIFICATION);
4709 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004710 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07004711 checkOutputForStrategy(STRATEGY_MEDIA);
4712 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004713 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07004714}
4715
Eric Laurente0720872014-03-11 09:30:41 -07004716void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07004717{
François Gaffie53615e22015-03-19 09:24:12 +01004718 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Aniket Kumar Lataa8ee9962018-01-31 20:24:23 -08004719 if (a2dpOutput == 0 || mOutputs.isA2dpOffloadedOnPrimary()) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004720 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07004721 return;
4722 }
4723
Eric Laurent3a4311c2014-03-17 12:00:47 -07004724 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08004725 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
4726 ~AUDIO_DEVICE_BIT_IN) != 0) ||
4727 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurentf732e072016-08-03 19:30:28 -07004728
4729 // if suspended, restore A2DP output if:
4730 // ((SCO device is NOT connected) ||
4731 // ((forced usage communication is NOT SCO) && (forced usage for record is NOT SCO) &&
4732 // (phone state is NOT in call) && (phone state is NOT ringing)))
Eric Laurente552edb2014-03-10 17:42:56 -07004733 //
Eric Laurentf732e072016-08-03 19:30:28 -07004734 // if not suspended, suspend A2DP output if:
4735 // (SCO device is connected) &&
4736 // ((forced usage for communication is SCO) || (forced usage for record is SCO) ||
4737 // ((phone state is in call) || (phone state is ringing)))
Eric Laurente552edb2014-03-10 17:42:56 -07004738 //
4739 if (mA2dpSuspended) {
Eric Laurentf732e072016-08-03 19:30:28 -07004740 if (!isScoConnected ||
4741 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) !=
4742 AUDIO_POLICY_FORCE_BT_SCO) &&
4743 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) !=
4744 AUDIO_POLICY_FORCE_BT_SCO) &&
4745 (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
François Gaffie2110e042015-03-24 08:41:51 +01004746 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004747
4748 mpClientInterface->restoreOutput(a2dpOutput);
4749 mA2dpSuspended = false;
4750 }
4751 } else {
Eric Laurentf732e072016-08-03 19:30:28 -07004752 if (isScoConnected &&
4753 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ==
4754 AUDIO_POLICY_FORCE_BT_SCO) ||
4755 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) ==
4756 AUDIO_POLICY_FORCE_BT_SCO) ||
4757 (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
François Gaffie2110e042015-03-24 08:41:51 +01004758 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004759
4760 mpClientInterface->suspendOutput(a2dpOutput);
4761 mA2dpSuspended = true;
4762 }
4763 }
4764}
4765
Eric Laurentc75307b2015-03-17 15:29:32 -07004766audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
4767 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004768{
4769 audio_devices_t device = AUDIO_DEVICE_NONE;
4770
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004771 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004772 if (index >= 0) {
4773 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4774 if (patchDesc->mUid != mUidCached) {
4775 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004776 outputDesc->device(), outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004777 return outputDesc->device();
4778 }
4779 }
4780
Eric Laurente552edb2014-03-10 17:42:56 -07004781 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05004782 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004783 // use device for strategy enforced audible
4784 // 2: we are in call or the strategy phone is active on the output:
4785 // use device for strategy phone
Jean-Michel Trivi178683b2017-08-30 18:07:06 -07004786 // 3: the strategy sonification is active on the output:
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004787 // use device for strategy sonification
Jean-Michel Trivi178683b2017-08-30 18:07:06 -07004788 // 4: the strategy for enforced audible is active but not enforced on the output:
4789 // use the device for strategy enforced audible
Eric Laurent28d09f02016-03-08 10:43:05 -08004790 // 5: the strategy accessibility is active on the output:
4791 // use device for strategy accessibility
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004792 // 6: the strategy "respectful" sonification is active on the output:
4793 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08004794 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004795 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08004796 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004797 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08004798 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004799 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01004800 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004801 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07004802 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
4803 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01004804 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004805 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004806 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004807 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Jean-Michel Trivi178683b2017-08-30 18:07:06 -07004808 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
4809 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
Eric Laurent28d09f02016-03-08 10:43:05 -08004810 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
4811 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004812 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004813 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004814 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004815 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004816 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004817 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004818 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004819 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004820 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004821 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07004822 }
4823
Eric Laurent1c333e22014-05-20 10:48:17 -07004824 ALOGV("getNewOutputDevice() selected device %x", device);
4825 return device;
4826}
4827
Eric Laurentfb66dd92016-01-28 18:32:03 -08004828audio_devices_t AudioPolicyManager::getNewInputDevice(const sp<AudioInputDescriptor>& inputDesc)
Eric Laurent1c333e22014-05-20 10:48:17 -07004829{
Eric Laurentfb66dd92016-01-28 18:32:03 -08004830 audio_devices_t device = AUDIO_DEVICE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07004831
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004832 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004833 if (index >= 0) {
4834 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4835 if (patchDesc->mUid != mUidCached) {
4836 ALOGV("getNewInputDevice() device %08x forced by patch %d",
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004837 inputDesc->mDevice, inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004838 return inputDesc->mDevice;
4839 }
4840 }
4841
Eric Laurentdc95a252018-04-12 12:46:56 -07004842 // If we are not in call and no client is active on this input, this methods returns
4843 // AUDIO_DEVICE_NONE, causing the patch on the input stream to be released.
Eric Laurentfb66dd92016-01-28 18:32:03 -08004844 audio_source_t source = inputDesc->getHighestPrioritySource(true /*activeOnly*/);
Eric Laurentdc95a252018-04-12 12:46:56 -07004845 if (source == AUDIO_SOURCE_DEFAULT && isInCall()) {
4846 source = AUDIO_SOURCE_VOICE_COMMUNICATION;
4847 }
4848 if (source != AUDIO_SOURCE_DEFAULT) {
Eric Laurentfb66dd92016-01-28 18:32:03 -08004849 device = getDeviceAndMixForInputSource(source);
4850 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004851
Eric Laurente552edb2014-03-10 17:42:56 -07004852 return device;
4853}
4854
Eric Laurent794fde22016-03-11 09:50:45 -08004855bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1,
4856 audio_stream_type_t stream2) {
Jean-Michel Trivi99bb2f92016-11-23 15:52:07 -08004857 return (stream1 == stream2);
Eric Laurent28d09f02016-03-08 10:43:05 -08004858}
4859
Eric Laurente0720872014-03-11 09:30:41 -07004860uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004861 return (uint32_t)getStrategy(stream);
4862}
4863
Eric Laurente0720872014-03-11 09:30:41 -07004864audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004865 // By checking the range of stream before calling getStrategy, we avoid
4866 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
4867 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08004868 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004869 return AUDIO_DEVICE_NONE;
4870 }
Eric Laurent28d09f02016-03-08 10:43:05 -08004871 audio_devices_t devices = AUDIO_DEVICE_NONE;
Eric Laurent794fde22016-03-11 09:50:45 -08004872 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
4873 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004874 continue;
Eric Laurent6a94d692014-05-20 11:18:06 -07004875 }
Eric Laurent794fde22016-03-11 09:50:45 -08004876 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08004877 audio_devices_t curDevices =
Eric Laurent447a87b2016-07-07 17:32:10 -07004878 getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/);
Mikhail Naganovcf84e592017-12-07 11:25:11 -08004879 for (audio_io_handle_t output : getOutputsForDevice(curDevices, mOutputs)) {
4880 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurent794fde22016-03-11 09:50:45 -08004881 if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004882 curDevices |= outputDesc->device();
4883 }
4884 }
4885 devices |= curDevices;
Eric Laurente552edb2014-03-10 17:42:56 -07004886 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05004887
4888 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
4889 and doesn't really need to.*/
4890 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
4891 devices |= AUDIO_DEVICE_OUT_SPEAKER;
4892 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4893 }
Eric Laurente552edb2014-03-10 17:42:56 -07004894 return devices;
4895}
4896
François Gaffie2110e042015-03-24 08:41:51 +01004897routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
4898{
Eric Laurent223fd5c2014-11-11 13:43:36 -08004899 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01004900 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004901}
4902
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004903uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4904 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004905 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4906 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4907 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004908 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4909 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4910 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004911 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01004912 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004913}
4914
Eric Laurente0720872014-03-11 09:30:41 -07004915void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004916 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004917 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004918 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4919 updateDevicesAndOutputs();
4920 break;
4921 default:
4922 break;
4923 }
4924}
4925
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004926uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
Eric Laurent9459fb02015-08-12 18:36:32 -07004927
4928 // skip beacon mute management if a dedicated TTS output is available
4929 if (mTtsOutputAvailable) {
4930 return 0;
4931 }
4932
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004933 switch(event) {
4934 case STARTING_OUTPUT:
4935 mBeaconMuteRefCount++;
4936 break;
4937 case STOPPING_OUTPUT:
4938 if (mBeaconMuteRefCount > 0) {
4939 mBeaconMuteRefCount--;
4940 }
4941 break;
4942 case STARTING_BEACON:
4943 mBeaconPlayingRefCount++;
4944 break;
4945 case STOPPING_BEACON:
4946 if (mBeaconPlayingRefCount > 0) {
4947 mBeaconPlayingRefCount--;
4948 }
4949 break;
4950 }
4951
4952 if (mBeaconMuteRefCount > 0) {
4953 // any playback causes beacon to be muted
4954 return setBeaconMute(true);
4955 } else {
4956 // no other playback: unmute when beacon starts playing, mute when it stops
4957 return setBeaconMute(mBeaconPlayingRefCount == 0);
4958 }
4959}
4960
4961uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4962 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4963 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4964 // keep track of muted state to avoid repeating mute/unmute operations
4965 if (mBeaconMuted != mute) {
4966 // mute/unmute AUDIO_STREAM_TTS on all outputs
4967 ALOGV("\t muting %d", mute);
4968 uint32_t maxLatency = 0;
4969 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004970 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004971 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004972 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004973 0 /*delay*/, AUDIO_DEVICE_NONE);
4974 const uint32_t latency = desc->latency() * 2;
4975 if (latency > maxLatency) {
4976 maxLatency = latency;
4977 }
4978 }
4979 mBeaconMuted = mute;
4980 return maxLatency;
4981 }
4982 return 0;
4983}
4984
Eric Laurente0720872014-03-11 09:30:41 -07004985audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004986 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004987{
Paul McLeanaa981192015-03-21 09:55:15 -07004988 // Routing
4989 // see if we have an explicit route
4990 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4991 // (getStrategy(stream)).
4992 // if the strategy from the stream type in the RouteMap is the same as the argument above,
Eric Laurentad2e7b92017-09-14 20:06:42 -07004993 // and activity count is non-zero and the device in the route descriptor is available
4994 // then select this device.
Paul McLeanaa981192015-03-21 09:55:15 -07004995 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4996 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
Eric Laurent28d09f02016-03-08 10:43:05 -08004997 routing_strategy routeStrategy = getStrategy(route->mStreamType);
Eric Laurent2157f5b2018-04-25 18:33:02 -07004998 if ((routeStrategy == strategy) && route->isActiveOrChanged() &&
Eric Laurentad2e7b92017-09-14 20:06:42 -07004999 (mAvailableOutputDevices.indexOf(route->mDeviceDescriptor) >= 0)) {
Paul McLeanaa981192015-03-21 09:55:15 -07005000 return route->mDeviceDescriptor->type();
5001 }
5002 }
5003
Eric Laurente552edb2014-03-10 17:42:56 -07005004 if (fromCache) {
5005 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
5006 strategy, mDeviceForStrategy[strategy]);
5007 return mDeviceForStrategy[strategy];
5008 }
François Gaffie2110e042015-03-24 08:41:51 +01005009 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07005010}
5011
Eric Laurente0720872014-03-11 09:30:41 -07005012void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07005013{
5014 for (int i = 0; i < NUM_STRATEGIES; i++) {
5015 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
5016 }
5017 mPreviousOutputs = mOutputs;
5018}
5019
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07005020uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005021 audio_devices_t prevDevice,
5022 uint32_t delayMs)
5023{
5024 // mute/unmute strategies using an incompatible device combination
5025 // if muting, wait for the audio in pcm buffer to be drained before proceeding
5026 // if unmuting, unmute only after the specified delay
5027 if (outputDesc->isDuplicated()) {
5028 return 0;
5029 }
5030
5031 uint32_t muteWaitMs = 0;
5032 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07005033 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07005034
5035 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
5036 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07005037 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07005038 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
5039 bool doMute = false;
5040
5041 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
5042 doMute = true;
5043 outputDesc->mStrategyMutedByDevice[i] = true;
5044 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
5045 doMute = true;
5046 outputDesc->mStrategyMutedByDevice[i] = false;
5047 }
Eric Laurent99401132014-05-07 19:48:15 -07005048 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07005049 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07005050 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07005051 // skip output if it does not share any device with current output
5052 if ((desc->supportedDevices() & outputDesc->supportedDevices())
5053 == AUDIO_DEVICE_NONE) {
5054 continue;
5055 }
Eric Laurent37ddbb42016-08-10 16:19:14 -07005056 ALOGVV("checkDeviceMuteStrategies() %s strategy %zu (curDevice %04x)",
Eric Laurentc75307b2015-03-17 15:29:32 -07005057 mute ? "muting" : "unmuting", i, curDevice);
5058 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01005059 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07005060 if (mute) {
5061 // FIXME: should not need to double latency if volume could be applied
5062 // immediately by the audioflinger mixer. We must account for the delay
5063 // between now and the next time the audioflinger thread for this output
5064 // will process a buffer (which corresponds to one buffer size,
5065 // usually 1/2 or 1/4 of the latency).
5066 if (muteWaitMs < desc->latency() * 2) {
5067 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07005068 }
5069 }
5070 }
5071 }
5072 }
5073 }
5074
Eric Laurent99401132014-05-07 19:48:15 -07005075 // temporary mute output if device selection changes to avoid volume bursts due to
5076 // different per device volumes
5077 if (outputDesc->isActive() && (device != prevDevice)) {
Eric Laurentdc462862016-07-19 12:29:53 -07005078 uint32_t tempMuteWaitMs = outputDesc->latency() * 2;
5079 // temporary mute duration is conservatively set to 4 times the reported latency
5080 uint32_t tempMuteDurationMs = outputDesc->latency() * 4;
5081 if (muteWaitMs < tempMuteWaitMs) {
5082 muteWaitMs = tempMuteWaitMs;
Eric Laurent99401132014-05-07 19:48:15 -07005083 }
Eric Laurentdc462862016-07-19 12:29:53 -07005084
Eric Laurent99401132014-05-07 19:48:15 -07005085 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01005086 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentdc462862016-07-19 12:29:53 -07005087 // make sure that we do not start the temporary mute period too early in case of
5088 // delayed device change
5089 setStrategyMute((routing_strategy)i, true, outputDesc, delayMs);
Eric Laurentc75307b2015-03-17 15:29:32 -07005090 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurentdc462862016-07-19 12:29:53 -07005091 delayMs + tempMuteDurationMs, device);
Eric Laurent99401132014-05-07 19:48:15 -07005092 }
5093 }
5094 }
5095
Eric Laurente552edb2014-03-10 17:42:56 -07005096 // wait for the PCM output buffers to empty before proceeding with the rest of the command
5097 if (muteWaitMs > delayMs) {
5098 muteWaitMs -= delayMs;
5099 usleep(muteWaitMs * 1000);
5100 return muteWaitMs;
5101 }
5102 return 0;
5103}
5104
Eric Laurentc75307b2015-03-17 15:29:32 -07005105uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005106 audio_devices_t device,
5107 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07005108 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07005109 audio_patch_handle_t *patchHandle,
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00005110 const char *address,
5111 bool requiresMuteCheck)
Eric Laurente552edb2014-03-10 17:42:56 -07005112{
Eric Laurentc75307b2015-03-17 15:29:32 -07005113 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005114 AudioParameter param;
5115 uint32_t muteWaitMs;
5116
5117 if (outputDesc->isDuplicated()) {
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00005118 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs,
5119 nullptr /* patchHandle */, nullptr /* address */, requiresMuteCheck);
5120 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs,
5121 nullptr /* patchHandle */, nullptr /* address */, requiresMuteCheck);
Eric Laurente552edb2014-03-10 17:42:56 -07005122 return muteWaitMs;
5123 }
5124 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
5125 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07005126 if ((device != AUDIO_DEVICE_NONE) &&
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00005127 ((device & outputDesc->supportedDevices()) == AUDIO_DEVICE_NONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005128 return 0;
5129 }
5130
5131 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07005132 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07005133
5134 audio_devices_t prevDevice = outputDesc->mDevice;
5135
Paul McLeanaa981192015-03-21 09:55:15 -07005136 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07005137
5138 if (device != AUDIO_DEVICE_NONE) {
5139 outputDesc->mDevice = device;
5140 }
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00005141
5142 // if the outputs are not materially active, there is no need to mute.
5143 if (requiresMuteCheck) {
5144 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
5145 } else {
5146 ALOGV("%s: suppressing checkDeviceMuteStrategies", __func__);
5147 muteWaitMs = 0;
5148 }
Eric Laurente552edb2014-03-10 17:42:56 -07005149
5150 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07005151 // the requested device is AUDIO_DEVICE_NONE
5152 // OR the requested device is the same as current device
5153 // AND force is not specified
5154 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07005155 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07005156 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
5157 !force &&
Jean-Michel Triviff155c62016-02-26 12:07:16 -08005158 outputDesc->getPatchHandle() != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005159 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07005160 return muteWaitMs;
5161 }
5162
5163 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07005164
Eric Laurente552edb2014-03-10 17:42:56 -07005165 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07005166 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005167 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07005168 } else {
Eric Laurentc40d9692016-04-13 19:14:13 -07005169 DeviceVector deviceList;
5170 if ((address == NULL) || (strlen(address) == 0)) {
5171 deviceList = mAvailableOutputDevices.getDevicesFromType(device);
5172 } else {
5173 deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
5174 }
5175
Eric Laurent1c333e22014-05-20 10:48:17 -07005176 if (!deviceList.isEmpty()) {
5177 struct audio_patch patch;
5178 outputDesc->toAudioPortConfig(&patch.sources[0]);
5179 patch.num_sources = 1;
5180 patch.num_sinks = 0;
5181 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
5182 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07005183 patch.num_sinks++;
5184 }
Eric Laurent6a94d692014-05-20 11:18:06 -07005185 ssize_t index;
5186 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
5187 index = mAudioPatches.indexOfKey(*patchHandle);
5188 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08005189 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07005190 }
5191 sp< AudioPatch> patchDesc;
5192 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
5193 if (index >= 0) {
5194 patchDesc = mAudioPatches.valueAt(index);
5195 afPatchHandle = patchDesc->mAfPatchHandle;
5196 }
5197
Eric Laurent1c333e22014-05-20 10:48:17 -07005198 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07005199 &afPatchHandle,
5200 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07005201 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
5202 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07005203 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07005204 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07005205 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01005206 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07005207 addAudioPatch(patchDesc->mHandle, patchDesc);
5208 } else {
5209 patchDesc->mPatch = patch;
5210 }
5211 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07005212 if (patchHandle) {
5213 *patchHandle = patchDesc->mHandle;
5214 }
Jean-Michel Triviff155c62016-02-26 12:07:16 -08005215 outputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07005216 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07005217 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07005218 }
5219 }
bryant_liuf5e7e792014-08-19 20:07:05 +08005220
5221 // inform all input as well
5222 for (size_t i = 0; i < mInputs.size(); i++) {
5223 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01005224 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08005225 AudioParameter inputCmd = AudioParameter();
5226 ALOGV("%s: inform input %d of device:%d", __func__,
5227 inputDescriptor->mIoHandle, device);
5228 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
5229 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
5230 inputCmd.toString(),
5231 delayMs);
5232 }
5233 }
Eric Laurent1c333e22014-05-20 10:48:17 -07005234 }
Eric Laurente552edb2014-03-10 17:42:56 -07005235
5236 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07005237 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005238
5239 return muteWaitMs;
5240}
5241
Eric Laurentc75307b2015-03-17 15:29:32 -07005242status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07005243 int delayMs,
5244 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07005245{
Eric Laurent6a94d692014-05-20 11:18:06 -07005246 ssize_t index;
5247 if (patchHandle) {
5248 index = mAudioPatches.indexOfKey(*patchHandle);
5249 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08005250 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07005251 }
5252 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07005253 return INVALID_OPERATION;
5254 }
Eric Laurent6a94d692014-05-20 11:18:06 -07005255 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
5256 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07005257 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07005258 outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07005259 removeAudioPatch(patchDesc->mHandle);
5260 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07005261 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07005262 return status;
5263}
5264
5265status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
5266 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07005267 bool force,
5268 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07005269{
5270 status_t status = NO_ERROR;
5271
Eric Laurent1f2f2232014-06-02 12:01:23 -07005272 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07005273 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
5274 inputDesc->mDevice = device;
5275
5276 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
5277 if (!deviceList.isEmpty()) {
5278 struct audio_patch patch;
5279 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07005280 // AUDIO_SOURCE_HOTWORD is for internal use only:
5281 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07005282 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
Eric Laurent599c7582015-12-07 18:05:55 -08005283 !inputDesc->isSoundTrigger()) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07005284 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
5285 }
Eric Laurent1c333e22014-05-20 10:48:17 -07005286 patch.num_sinks = 1;
5287 //only one input device for now
5288 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07005289 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07005290 ssize_t index;
5291 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
5292 index = mAudioPatches.indexOfKey(*patchHandle);
5293 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08005294 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07005295 }
5296 sp< AudioPatch> patchDesc;
5297 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
5298 if (index >= 0) {
5299 patchDesc = mAudioPatches.valueAt(index);
5300 afPatchHandle = patchDesc->mAfPatchHandle;
5301 }
5302
Eric Laurent1c333e22014-05-20 10:48:17 -07005303 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07005304 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07005305 0);
5306 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07005307 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07005308 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07005309 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01005310 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07005311 addAudioPatch(patchDesc->mHandle, patchDesc);
5312 } else {
5313 patchDesc->mPatch = patch;
5314 }
5315 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07005316 if (patchHandle) {
5317 *patchHandle = patchDesc->mHandle;
5318 }
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08005319 inputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07005320 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07005321 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07005322 }
5323 }
5324 }
5325 return status;
5326}
5327
Eric Laurent6a94d692014-05-20 11:18:06 -07005328status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
5329 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07005330{
Eric Laurent1f2f2232014-06-02 12:01:23 -07005331 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07005332 ssize_t index;
5333 if (patchHandle) {
5334 index = mAudioPatches.indexOfKey(*patchHandle);
5335 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08005336 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07005337 }
5338 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07005339 return INVALID_OPERATION;
5340 }
Eric Laurent6a94d692014-05-20 11:18:06 -07005341 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
5342 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07005343 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07005344 inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07005345 removeAudioPatch(patchDesc->mHandle);
5346 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07005347 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07005348 return status;
5349}
5350
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08005351sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07005352 const String8& address,
François Gaffie53615e22015-03-19 09:24:12 +01005353 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07005354 audio_format_t& format,
5355 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01005356 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07005357{
5358 // Choose an input profile based on the requested capture parameters: select the first available
5359 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07005360 //
5361 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
5362 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07005363
Glenn Kasten730b9262018-03-29 15:01:26 -07005364 sp<IOProfile> firstInexact;
5365 uint32_t updatedSamplingRate = 0;
5366 audio_format_t updatedFormat = AUDIO_FORMAT_INVALID;
5367 audio_channel_mask_t updatedChannelMask = AUDIO_CHANNEL_INVALID;
Mikhail Naganov7e22e942017-12-07 10:04:29 -08005368 for (const auto& hwModule : mHwModules) {
Mikhail Naganova5e165d2017-12-07 17:08:02 -08005369 for (const auto& profile : hwModule->getInputProfiles()) {
Eric Laurentd4692962014-05-05 18:13:44 -07005370 // profile->log();
Glenn Kasten730b9262018-03-29 15:01:26 -07005371 //updatedFormat = format;
Eric Laurent275e8e92014-11-30 15:14:47 -08005372 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kasten730b9262018-03-29 15:01:26 -07005373 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07005374 format,
Glenn Kasten730b9262018-03-29 15:01:26 -07005375 &format, /*updatedFormat*/
Andy Hungf129b032015-04-07 13:45:50 -07005376 channelMask,
Glenn Kasten730b9262018-03-29 15:01:26 -07005377 &channelMask /*updatedChannelMask*/,
5378 // FIXME ugly cast
5379 (audio_output_flags_t) flags,
5380 true /*exactMatchRequiredForInputFlags*/)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005381 return profile;
5382 }
Glenn Kasten730b9262018-03-29 15:01:26 -07005383 if (firstInexact == nullptr && profile->isCompatibleProfile(device, address,
5384 samplingRate,
5385 &updatedSamplingRate,
5386 format,
5387 &updatedFormat,
5388 channelMask,
5389 &updatedChannelMask,
5390 // FIXME ugly cast
5391 (audio_output_flags_t) flags,
5392 false /*exactMatchRequiredForInputFlags*/)) {
5393 firstInexact = profile;
5394 }
5395
Eric Laurente552edb2014-03-10 17:42:56 -07005396 }
5397 }
Glenn Kasten730b9262018-03-29 15:01:26 -07005398 if (firstInexact != nullptr) {
5399 samplingRate = updatedSamplingRate;
5400 format = updatedFormat;
5401 channelMask = updatedChannelMask;
5402 return firstInexact;
5403 }
Eric Laurente552edb2014-03-10 17:42:56 -07005404 return NULL;
5405}
5406
Eric Laurentc73ca6e2014-12-12 14:34:22 -08005407
5408audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01005409 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07005410{
François Gaffie036e1e92015-03-19 10:16:24 +01005411 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
5412 audio_devices_t selectedDeviceFromMix =
5413 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08005414
François Gaffie036e1e92015-03-19 10:16:24 +01005415 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
5416 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08005417 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08005418 return getDeviceForInputSource(inputSource);
5419}
5420
5421audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
5422{
Eric Laurentad2e7b92017-09-14 20:06:42 -07005423 // Routing
5424 // Scan the whole RouteMap to see if we have an explicit route:
5425 // if the input source in the RouteMap is the same as the argument above,
5426 // and activity count is non-zero and the device in the route descriptor is available
5427 // then select this device.
Paul McLean466dc8e2015-04-17 13:15:36 -06005428 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
5429 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurent2157f5b2018-04-25 18:33:02 -07005430 if ((inputSource == route->mSource) && route->isActiveOrChanged() &&
Eric Laurentad2e7b92017-09-14 20:06:42 -07005431 (mAvailableInputDevices.indexOf(route->mDeviceDescriptor) >= 0)) {
Paul McLean466dc8e2015-04-17 13:15:36 -06005432 return route->mDeviceDescriptor->type();
5433 }
5434 }
5435
5436 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07005437}
5438
Eric Laurente0720872014-03-11 09:30:41 -07005439float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005440 int index,
5441 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005442{
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005443 float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index);
Jean-Michel Trivi3d8b4a42016-09-14 18:37:46 -07005444
5445 // handle the case of accessibility active while a ringtone is playing: if the ringtone is much
5446 // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch
5447 // exploration of the dialer UI. In this situation, bring the accessibility volume closer to
5448 // the ringtone volume
5449 if ((stream == AUDIO_STREAM_ACCESSIBILITY)
5450 && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState())
5451 && isStreamActive(AUDIO_STREAM_RING, 0)) {
5452 const float ringVolumeDB = computeVolume(AUDIO_STREAM_RING, index, device);
5453 return ringVolumeDB - 4 > volumeDB ? ringVolumeDB - 4 : volumeDB;
5454 }
5455
Jean-Michel Trivi719a9872017-08-05 13:51:35 -07005456 // in-call: always cap earpiece volume by voice volume + some low headroom
Eric Laurent7731b5a2018-04-06 15:47:22 -07005457 if ((stream != AUDIO_STREAM_VOICE_CALL) && (device & AUDIO_DEVICE_OUT_EARPIECE) &&
5458 (isInCall() || mOutputs.isStreamActiveLocally(AUDIO_STREAM_VOICE_CALL))) {
Jean-Michel Trivi719a9872017-08-05 13:51:35 -07005459 switch (stream) {
5460 case AUDIO_STREAM_SYSTEM:
5461 case AUDIO_STREAM_RING:
5462 case AUDIO_STREAM_MUSIC:
5463 case AUDIO_STREAM_ALARM:
5464 case AUDIO_STREAM_NOTIFICATION:
5465 case AUDIO_STREAM_ENFORCED_AUDIBLE:
5466 case AUDIO_STREAM_DTMF:
5467 case AUDIO_STREAM_ACCESSIBILITY: {
Eric Laurent7731b5a2018-04-06 15:47:22 -07005468 int voiceVolumeIndex =
5469 mVolumeCurves->getVolumeIndex(AUDIO_STREAM_VOICE_CALL, AUDIO_DEVICE_OUT_EARPIECE);
5470 const float maxVoiceVolDb =
5471 computeVolume(AUDIO_STREAM_VOICE_CALL, voiceVolumeIndex, AUDIO_DEVICE_OUT_EARPIECE)
5472 + IN_CALL_EARPIECE_HEADROOM_DB;
Jean-Michel Trivi719a9872017-08-05 13:51:35 -07005473 if (volumeDB > maxVoiceVolDb) {
5474 ALOGV("computeVolume() stream %d at vol=%f overriden by stream %d at vol=%f",
5475 stream, volumeDB, AUDIO_STREAM_VOICE_CALL, maxVoiceVolDb);
5476 volumeDB = maxVoiceVolDb;
5477 }
5478 } break;
5479 default:
5480 break;
5481 }
5482 }
5483
Eric Laurente552edb2014-03-10 17:42:56 -07005484 // if a headset is connected, apply the following rules to ring tones and notifications
5485 // to avoid sound level bursts in user's ears:
Eric Laurent6af1c1d2016-04-14 11:20:44 -07005486 // - always attenuate notifications volume by 6dB
5487 // - attenuate ring tones volume by 6dB unless music is not playing and
5488 // speaker is part of the select devices
Eric Laurente552edb2014-03-10 17:42:56 -07005489 // - if music is playing, always limit the volume to current music volume,
5490 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07005491 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005492 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
5493 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
5494 AUDIO_DEVICE_OUT_WIRED_HEADSET |
Eric Laurent904d6322017-03-17 17:20:47 -07005495 AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
Jakub Pawlowski00f928c2018-03-22 13:20:03 -07005496 AUDIO_DEVICE_OUT_USB_HEADSET |
5497 AUDIO_DEVICE_OUT_HEARING_AID)) &&
Eric Laurente552edb2014-03-10 17:42:56 -07005498 ((stream_strategy == STRATEGY_SONIFICATION)
5499 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07005500 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07005501 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01005502 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
François Gaffied1ab2bd2015-12-02 18:20:06 +01005503 mVolumeCurves->canBeMuted(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005504 // when the phone is ringing we must consider that music could have been paused just before
5505 // by the music application and behave as if music was active if the last music track was
5506 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07005507 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07005508 mLimitRingtoneVolume) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005509 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07005510 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07005511 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005512 mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC,
5513 musicDevice),
5514 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07005515 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
5516 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005517 if (volumeDB > minVolDB) {
5518 volumeDB = minVolDB;
Eric Laurentffbc80f2015-03-18 18:30:19 -07005519 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07005520 }
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005521 if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
5522 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) {
5523 // on A2DP, also ensure notification volume is not too low compared to media when
5524 // intended to be played
5525 if ((volumeDB > -96.0f) &&
5526 (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) {
5527 ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f",
5528 stream, device,
5529 volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB);
5530 volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB;
5531 }
5532 }
Eric Laurent6af1c1d2016-04-14 11:20:44 -07005533 } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) ||
5534 stream_strategy != STRATEGY_SONIFICATION) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005535 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07005536 }
5537 }
5538
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005539 return volumeDB;
Eric Laurente552edb2014-03-10 17:42:56 -07005540}
5541
Eric Laurente0720872014-03-11 09:30:41 -07005542status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005543 int index,
5544 const sp<AudioOutputDescriptor>& outputDesc,
5545 audio_devices_t device,
5546 int delayMs,
5547 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005548{
Eric Laurente552edb2014-03-10 17:42:56 -07005549 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07005550 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07005551 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07005552 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07005553 return NO_ERROR;
5554 }
François Gaffie2110e042015-03-24 08:41:51 +01005555 audio_policy_forced_cfg_t forceUseForComm =
5556 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07005557 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01005558 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
5559 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005560 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01005561 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07005562 return INVALID_OPERATION;
5563 }
5564
Eric Laurentc75307b2015-03-17 15:29:32 -07005565 if (device == AUDIO_DEVICE_NONE) {
5566 device = outputDesc->device();
5567 }
Eric Laurent275e8e92014-11-30 15:14:47 -08005568
Eric Laurentffbc80f2015-03-18 18:30:19 -07005569 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07005570 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07005571 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08005572 }
Eric Laurentc75307b2015-03-17 15:29:32 -07005573
Eric Laurentffbc80f2015-03-18 18:30:19 -07005574 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07005575
Eric Laurent3b73df72014-03-11 09:06:29 -07005576 if (stream == AUDIO_STREAM_VOICE_CALL ||
5577 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07005578 float voiceVolume;
5579 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07005580 if (stream == AUDIO_STREAM_VOICE_CALL) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005581 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005582 } else {
5583 voiceVolume = 1.0;
5584 }
5585
Eric Laurent18fba842016-03-31 14:41:26 -07005586 if (voiceVolume != mLastVoiceVolume) {
Eric Laurente552edb2014-03-10 17:42:56 -07005587 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
5588 mLastVoiceVolume = voiceVolume;
5589 }
5590 }
5591
5592 return NO_ERROR;
5593}
5594
Eric Laurentc75307b2015-03-17 15:29:32 -07005595void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
5596 audio_devices_t device,
5597 int delayMs,
5598 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005599{
Eric Laurentc75307b2015-03-17 15:29:32 -07005600 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07005601
Eric Laurent794fde22016-03-11 09:50:45 -08005602 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005603 checkAndSetVolume((audio_stream_type_t)stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005604 mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005605 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005606 device,
5607 delayMs,
5608 force);
5609 }
5610}
5611
Eric Laurente0720872014-03-11 09:30:41 -07005612void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07005613 bool on,
5614 const sp<AudioOutputDescriptor>& outputDesc,
5615 int delayMs,
5616 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005617{
Eric Laurent72249d52015-06-08 11:12:15 -07005618 ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d",
5619 strategy, on, outputDesc->getId());
Eric Laurent794fde22016-03-11 09:50:45 -08005620 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005621 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005622 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07005623 }
5624 }
5625}
5626
Eric Laurente0720872014-03-11 09:30:41 -07005627void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005628 bool on,
5629 const sp<AudioOutputDescriptor>& outputDesc,
5630 int delayMs,
5631 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005632{
Eric Laurente552edb2014-03-10 17:42:56 -07005633 if (device == AUDIO_DEVICE_NONE) {
5634 device = outputDesc->device();
5635 }
5636
Eric Laurentc75307b2015-03-17 15:29:32 -07005637 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
5638 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07005639
5640 if (on) {
5641 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005642 if (mVolumeCurves->canBeMuted(stream) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07005643 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01005644 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005645 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005646 }
5647 }
5648 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
5649 outputDesc->mMuteCount[stream]++;
5650 } else {
5651 if (outputDesc->mMuteCount[stream] == 0) {
5652 ALOGV("setStreamMute() unmuting non muted stream!");
5653 return;
5654 }
5655 if (--outputDesc->mMuteCount[stream] == 0) {
5656 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005657 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005658 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005659 device,
5660 delayMs);
5661 }
5662 }
5663}
5664
Eric Laurente0720872014-03-11 09:30:41 -07005665void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07005666 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07005667{
Eric Laurent87ffa392015-05-22 10:32:38 -07005668 if(!hasPrimaryOutput()) {
5669 return;
5670 }
5671
Eric Laurente552edb2014-03-10 17:42:56 -07005672 // if the stream pertains to sonification strategy and we are in call we must
5673 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
5674 // in the device used for phone strategy and play the tone if the selected device does not
5675 // interfere with the device used for phone strategy
5676 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
5677 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07005678 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005679 if ((stream_strategy == STRATEGY_SONIFICATION) ||
5680 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005681 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07005682 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
5683 stream, starting, outputDesc->mDevice, stateChange);
5684 if (outputDesc->mRefCount[stream]) {
5685 int muteCount = 1;
5686 if (stateChange) {
5687 muteCount = outputDesc->mRefCount[stream];
5688 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005689 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005690 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
5691 for (int i = 0; i < muteCount; i++) {
5692 setStreamMute(stream, starting, mPrimaryOutput);
5693 }
5694 } else {
5695 ALOGV("handleIncallSonification() high visibility");
5696 if (outputDesc->device() &
5697 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
5698 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
5699 for (int i = 0; i < muteCount; i++) {
5700 setStreamMute(stream, starting, mPrimaryOutput);
5701 }
5702 }
5703 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005704 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
5705 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07005706 } else {
5707 mpClientInterface->stopTone();
5708 }
5709 }
5710 }
5711 }
5712}
5713
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005714audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
5715{
5716 // flags to stream type mapping
5717 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
5718 return AUDIO_STREAM_ENFORCED_AUDIBLE;
5719 }
5720 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
5721 return AUDIO_STREAM_BLUETOOTH_SCO;
5722 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08005723 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
5724 return AUDIO_STREAM_TTS;
5725 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005726
5727 // usage to stream type mapping
5728 switch (attr->usage) {
5729 case AUDIO_USAGE_MEDIA:
5730 case AUDIO_USAGE_GAME:
Jean-Michel Trivi36867762016-12-29 12:03:28 -08005731 case AUDIO_USAGE_ASSISTANT:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005732 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5733 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08005734 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5735 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005736 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5737 return AUDIO_STREAM_SYSTEM;
5738 case AUDIO_USAGE_VOICE_COMMUNICATION:
5739 return AUDIO_STREAM_VOICE_CALL;
5740
5741 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5742 return AUDIO_STREAM_DTMF;
5743
5744 case AUDIO_USAGE_ALARM:
5745 return AUDIO_STREAM_ALARM;
5746 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5747 return AUDIO_STREAM_RING;
5748
5749 case AUDIO_USAGE_NOTIFICATION:
5750 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5751 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5752 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5753 case AUDIO_USAGE_NOTIFICATION_EVENT:
5754 return AUDIO_STREAM_NOTIFICATION;
5755
5756 case AUDIO_USAGE_UNKNOWN:
5757 default:
5758 return AUDIO_STREAM_MUSIC;
5759 }
5760}
Eric Laurente83b55d2014-11-14 10:06:21 -08005761
François Gaffie53615e22015-03-19 09:24:12 +01005762bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
5763{
Eric Laurente83b55d2014-11-14 10:06:21 -08005764 // has flags that map to a strategy?
5765 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
5766 return true;
5767 }
5768
5769 // has known usage?
5770 switch (paa->usage) {
5771 case AUDIO_USAGE_UNKNOWN:
5772 case AUDIO_USAGE_MEDIA:
5773 case AUDIO_USAGE_VOICE_COMMUNICATION:
5774 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5775 case AUDIO_USAGE_ALARM:
5776 case AUDIO_USAGE_NOTIFICATION:
5777 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5778 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5779 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5780 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5781 case AUDIO_USAGE_NOTIFICATION_EVENT:
5782 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5783 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5784 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5785 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08005786 case AUDIO_USAGE_VIRTUAL_SOURCE:
Jean-Michel Trivi36867762016-12-29 12:03:28 -08005787 case AUDIO_USAGE_ASSISTANT:
Eric Laurente83b55d2014-11-14 10:06:21 -08005788 break;
5789 default:
5790 return false;
5791 }
5792 return true;
5793}
5794
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07005795bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor>& outputDesc,
François Gaffiead3183e2015-03-18 16:55:35 +01005796 routing_strategy strategy, uint32_t inPastMs,
5797 nsecs_t sysTime) const
5798{
5799 if ((sysTime == 0) && (inPastMs != 0)) {
5800 sysTime = systemTime();
5801 }
Eric Laurent794fde22016-03-11 09:50:45 -08005802 for (int i = 0; i < (int)AUDIO_STREAM_FOR_POLICY_CNT; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01005803 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
5804 (NUM_STRATEGIES == strategy)) &&
5805 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
5806 return true;
5807 }
5808 }
5809 return false;
5810}
5811
François Gaffie2110e042015-03-24 08:41:51 +01005812audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
5813{
5814 return mEngine->getForceUse(usage);
5815}
5816
5817bool AudioPolicyManager::isInCall()
5818{
5819 return isStateInCall(mEngine->getPhoneState());
5820}
5821
5822bool AudioPolicyManager::isStateInCall(int state)
5823{
5824 return is_state_in_call(state);
5825}
5826
Eric Laurentd60560a2015-04-10 11:31:20 -07005827void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc)
5828{
5829 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
5830 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
5831 if (sourceDesc->mDevice->equals(deviceDesc)) {
5832 ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle());
5833 stopAudioSource(sourceDesc->getHandle());
5834 }
5835 }
5836
5837 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
5838 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
5839 bool release = false;
5840 for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) {
5841 const struct audio_port_config *source = &patchDesc->mPatch.sources[j];
5842 if (source->type == AUDIO_PORT_TYPE_DEVICE &&
5843 source->ext.device.type == deviceDesc->type()) {
5844 release = true;
5845 }
5846 }
5847 for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) {
5848 const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j];
5849 if (sink->type == AUDIO_PORT_TYPE_DEVICE &&
5850 sink->ext.device.type == deviceDesc->type()) {
5851 release = true;
5852 }
5853 }
5854 if (release) {
5855 ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle);
5856 releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid);
5857 }
5858 }
5859}
5860
Phil Burk09bc4612016-02-24 15:58:15 -08005861// Modify the list of surround sound formats supported.
Phil Burk0709b0a2016-03-31 12:54:57 -07005862void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) {
5863 FormatVector &formats = *formatsPtr;
Phil Burk07ac1142016-03-25 13:39:29 -07005864 // TODO Set this based on Config properties.
5865 const bool alwaysForceAC3 = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005866
5867 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5868 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
Phil Burk0709b0a2016-03-31 12:54:57 -07005869 ALOGD("%s: forced use = %d", __FUNCTION__, forceUse);
Phil Burk09bc4612016-02-24 15:58:15 -08005870
jiabin81772902018-04-02 17:52:27 -07005871 // If MANUAL, keep the supported surround sound formats as current enabled ones.
5872 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) {
5873 formats.clear();
5874 for (auto it = mSurroundFormats.begin(); it != mSurroundFormats.end(); it++) {
5875 formats.add(*it);
Phil Burk09bc4612016-02-24 15:58:15 -08005876 }
jiabin81772902018-04-02 17:52:27 -07005877 // Always enable IEC61937 when in MANUAL mode.
5878 formats.add(AUDIO_FORMAT_IEC61937);
5879 } else { // NEVER, AUTO or ALWAYS
5880 // Analyze original support for various formats.
5881 bool supportsAC3 = false;
5882 bool supportsOtherSurround = false;
5883 bool supportsIEC61937 = false;
5884 mSurroundFormats.clear();
5885 for (ssize_t formatIndex = 0; formatIndex < (ssize_t)formats.size(); formatIndex++) {
5886 audio_format_t format = formats[formatIndex];
5887 switch (format) {
5888 case AUDIO_FORMAT_AC3:
5889 supportsAC3 = true;
5890 break;
5891 case AUDIO_FORMAT_E_AC3:
5892 case AUDIO_FORMAT_DTS:
5893 case AUDIO_FORMAT_DTS_HD:
5894 // If ALWAYS, remove all other surround formats here
5895 // since we will add them later.
5896 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) {
5897 formats.removeAt(formatIndex);
5898 formatIndex--;
5899 }
5900 supportsOtherSurround = true;
5901 break;
5902 case AUDIO_FORMAT_IEC61937:
5903 supportsIEC61937 = true;
5904 break;
5905 default:
5906 break;
5907 }
5908 }
Phil Burk09bc4612016-02-24 15:58:15 -08005909
jiabin81772902018-04-02 17:52:27 -07005910 // Modify formats based on surround preferences.
5911 // If NEVER, remove support for surround formats.
5912 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5913 if (supportsAC3 || supportsOtherSurround || supportsIEC61937) {
5914 // Remove surround sound related formats.
5915 for (size_t formatIndex = 0; formatIndex < formats.size(); ) {
5916 audio_format_t format = formats[formatIndex];
5917 switch(format) {
5918 case AUDIO_FORMAT_AC3:
5919 case AUDIO_FORMAT_E_AC3:
5920 case AUDIO_FORMAT_DTS:
5921 case AUDIO_FORMAT_DTS_HD:
5922 case AUDIO_FORMAT_IEC61937:
5923 formats.removeAt(formatIndex);
5924 break;
5925 default:
5926 formatIndex++; // keep it
5927 break;
5928 }
5929 }
5930 supportsAC3 = false;
5931 supportsOtherSurround = false;
5932 supportsIEC61937 = false;
5933 }
5934 } else { // AUTO or ALWAYS
5935 // Most TVs support AC3 even if they do not report it in the EDID.
5936 if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS))
5937 && !supportsAC3) {
5938 formats.add(AUDIO_FORMAT_AC3);
5939 supportsAC3 = true;
5940 }
5941
5942 // If ALWAYS, add support for raw surround formats if all are missing.
5943 // This assumes that if any of these formats are reported by the HAL
5944 // then the report is valid and should not be modified.
5945 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) {
5946 formats.add(AUDIO_FORMAT_E_AC3);
5947 formats.add(AUDIO_FORMAT_DTS);
5948 formats.add(AUDIO_FORMAT_DTS_HD);
5949 supportsOtherSurround = true;
5950 }
5951
5952 // Add support for IEC61937 if any raw surround supported.
5953 // The HAL could do this but add it here, just in case.
5954 if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) {
5955 formats.add(AUDIO_FORMAT_IEC61937);
5956 supportsIEC61937 = true;
5957 }
5958
5959 // Add reported surround sound formats to enabled surround formats.
5960 for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) {
Phil Burk07ac1142016-03-25 13:39:29 -07005961 audio_format_t format = formats[formatIndex];
5962 switch(format) {
5963 case AUDIO_FORMAT_AC3:
5964 case AUDIO_FORMAT_E_AC3:
5965 case AUDIO_FORMAT_DTS:
5966 case AUDIO_FORMAT_DTS_HD:
jiabin81772902018-04-02 17:52:27 -07005967 case AUDIO_FORMAT_AAC_LC:
5968 case AUDIO_FORMAT_DOLBY_TRUEHD:
5969 case AUDIO_FORMAT_E_AC3_JOC:
5970 mSurroundFormats.insert(format);
Phil Burk07ac1142016-03-25 13:39:29 -07005971 default:
Phil Burk07ac1142016-03-25 13:39:29 -07005972 break;
5973 }
Phil Burk09bc4612016-02-24 15:58:15 -08005974 }
Phil Burk07ac1142016-03-25 13:39:29 -07005975 }
Phil Burk09bc4612016-02-24 15:58:15 -08005976 }
Phil Burk0709b0a2016-03-31 12:54:57 -07005977}
5978
5979// Modify the list of channel masks supported.
5980void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) {
5981 ChannelsVector &channelMasks = *channelMasksPtr;
5982 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5983 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
5984
5985 // If NEVER, then remove support for channelMasks > stereo.
5986 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5987 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) {
5988 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5989 if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) {
5990 ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask);
5991 channelMasks.removeAt(maskIndex);
5992 } else {
5993 maskIndex++;
5994 }
5995 }
jiabin81772902018-04-02 17:52:27 -07005996 // If ALWAYS or MANUAL, then make sure we at least support 5.1
5997 } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS
5998 || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) {
Phil Burk0709b0a2016-03-31 12:54:57 -07005999 bool supports5dot1 = false;
6000 // Are there any channel masks that can be considered "surround"?
Mikhail Naganovcf84e592017-12-07 11:25:11 -08006001 for (audio_channel_mask_t channelMask : channelMasks) {
Phil Burk0709b0a2016-03-31 12:54:57 -07006002 if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) {
6003 supports5dot1 = true;
6004 break;
6005 }
6006 }
6007 // If not then add 5.1 support.
6008 if (!supports5dot1) {
6009 channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1);
6010 ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__);
6011 }
Phil Burk09bc4612016-02-24 15:58:15 -08006012 }
6013}
6014
Phil Burk00eeb322016-03-31 12:41:00 -07006015void AudioPolicyManager::updateAudioProfiles(audio_devices_t device,
6016 audio_io_handle_t ioHandle,
François Gaffie112b0af2015-11-19 16:13:25 +01006017 AudioProfileVector &profiles)
6018{
6019 String8 reply;
Phil Burk0709b0a2016-03-31 12:54:57 -07006020
François Gaffie112b0af2015-11-19 16:13:25 +01006021 // Format MUST be checked first to update the list of AudioProfile
6022 if (profiles.hasDynamicFormat()) {
Mikhail Naganov388360c2016-10-17 17:09:41 -07006023 reply = mpClientInterface->getParameters(
6024 ioHandle, String8(AudioParameter::keyStreamSupportedFormats));
jiabin81772902018-04-02 17:52:27 -07006025 ALOGV("%s: supported formats %d, %s", __FUNCTION__, ioHandle, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08006026 AudioParameter repliedParameters(reply);
6027 if (repliedParameters.get(
Mikhail Naganov388360c2016-10-17 17:09:41 -07006028 String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) {
François Gaffie112b0af2015-11-19 16:13:25 +01006029 ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__);
6030 return;
6031 }
Phil Burk09bc4612016-02-24 15:58:15 -08006032 FormatVector formats = formatsFromString(reply.string());
Phil Burk00eeb322016-03-31 12:41:00 -07006033 if (device == AUDIO_DEVICE_OUT_HDMI) {
Phil Burk0709b0a2016-03-31 12:54:57 -07006034 filterSurroundFormats(&formats);
Phil Burk00eeb322016-03-31 12:41:00 -07006035 }
Phil Burk09bc4612016-02-24 15:58:15 -08006036 profiles.setFormats(formats);
François Gaffie112b0af2015-11-19 16:13:25 +01006037 }
François Gaffie112b0af2015-11-19 16:13:25 +01006038
Mikhail Naganovcf84e592017-12-07 11:25:11 -08006039 for (audio_format_t format : profiles.getSupportedFormats()) {
Eric Laurent20eb3a42016-01-26 18:39:17 -08006040 ChannelsVector channelMasks;
6041 SampleRateVector samplingRates;
François Gaffie112b0af2015-11-19 16:13:25 +01006042 AudioParameter requestedParameters;
Mikhail Naganov388360c2016-10-17 17:09:41 -07006043 requestedParameters.addInt(String8(AudioParameter::keyFormat), format);
François Gaffie112b0af2015-11-19 16:13:25 +01006044
6045 if (profiles.hasDynamicRateFor(format)) {
Mikhail Naganov388360c2016-10-17 17:09:41 -07006046 reply = mpClientInterface->getParameters(
6047 ioHandle,
6048 requestedParameters.toString() + ";" +
6049 AudioParameter::keyStreamSupportedSamplingRates);
François Gaffie112b0af2015-11-19 16:13:25 +01006050 ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08006051 AudioParameter repliedParameters(reply);
6052 if (repliedParameters.get(
Mikhail Naganov388360c2016-10-17 17:09:41 -07006053 String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) {
Eric Laurent62e4bc52016-02-02 18:37:28 -08006054 samplingRates = samplingRatesFromString(reply.string());
François Gaffie112b0af2015-11-19 16:13:25 +01006055 }
6056 }
6057 if (profiles.hasDynamicChannelsFor(format)) {
6058 reply = mpClientInterface->getParameters(ioHandle,
6059 requestedParameters.toString() + ";" +
Mikhail Naganov388360c2016-10-17 17:09:41 -07006060 AudioParameter::keyStreamSupportedChannels);
François Gaffie112b0af2015-11-19 16:13:25 +01006061 ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08006062 AudioParameter repliedParameters(reply);
6063 if (repliedParameters.get(
Mikhail Naganov388360c2016-10-17 17:09:41 -07006064 String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) {
Eric Laurent62e4bc52016-02-02 18:37:28 -08006065 channelMasks = channelMasksFromString(reply.string());
Phil Burk0709b0a2016-03-31 12:54:57 -07006066 if (device == AUDIO_DEVICE_OUT_HDMI) {
6067 filterSurroundChannelMasks(&channelMasks);
6068 }
François Gaffie112b0af2015-11-19 16:13:25 +01006069 }
6070 }
Eric Laurent20eb3a42016-01-26 18:39:17 -08006071 profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates));
François Gaffie112b0af2015-11-19 16:13:25 +01006072 }
6073}
Eric Laurentd60560a2015-04-10 11:31:20 -07006074
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08006075} // namespace android