blob: 6c86d519e48541b911253261b6dd3df81c41170e [file] [log] [blame]
Eric Laurente552edb2014-03-10 17:42:56 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -070017#define LOG_TAG "APM_AudioPolicyManager"
Eric Laurente552edb2014-03-10 17:42:56 -070018//#define LOG_NDEBUG 0
19
20//#define VERY_VERBOSE_LOGGING
21#ifdef VERY_VERBOSE_LOGGING
22#define ALOGVV ALOGV
23#else
24#define ALOGVV(a...) do { } while(0)
25#endif
26
Jaekyun Seok0d4a6af2017-02-17 17:10:17 +090027#define AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH 128
28#define AUDIO_POLICY_XML_CONFIG_FILE_NAME "audio_policy_configuration.xml"
François Gaffief4ad6e52015-11-19 16:59:57 +010029
Eric Laurentd4692962014-05-05 18:13:44 -070030#include <inttypes.h>
Eric Laurente552edb2014-03-10 17:42:56 -070031#include <math.h>
Eric Laurentd4692962014-05-05 18:13:44 -070032
François Gaffie2110e042015-03-24 08:41:51 +010033#include <AudioPolicyManagerInterface.h>
34#include <AudioPolicyEngineInstance.h>
Mathias Agopian05d19b02017-02-28 16:28:19 -080035#include <cutils/atomic.h>
Eric Laurente552edb2014-03-10 17:42:56 -070036#include <cutils/properties.h>
Eric Laurentd4692962014-05-05 18:13:44 -070037#include <utils/Log.h>
Eric Laurent3b73df72014-03-11 09:06:29 -070038#include <media/AudioParameter.h>
Eric Laurente83b55d2014-11-14 10:06:21 -080039#include <media/AudioPolicyHelper.h>
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070040#include <soundtrigger/SoundTrigger.h>
Mikhail Naganovcbc8f612016-10-11 18:05:13 -070041#include <system/audio.h>
Mikhail Naganov9ee05402016-10-13 15:58:17 -070042#include <audio_policy_conf.h>
Eric Laurentd4692962014-05-05 18:13:44 -070043#include "AudioPolicyManager.h"
François Gaffied1ab2bd2015-12-02 18:20:06 +010044#ifndef USE_XML_AUDIO_POLICY_CONF
François Gaffie53615e22015-03-19 09:24:12 +010045#include <ConfigParsingUtils.h>
François Gaffied1ab2bd2015-12-02 18:20:06 +010046#include <StreamDescriptor.h>
François Gaffief4ad6e52015-11-19 16:59:57 +010047#endif
François Gaffied1ab2bd2015-12-02 18:20:06 +010048#include <Serializer.h>
François Gaffiea8ecc2c2015-11-09 16:10:40 +010049#include "TypeConverter.h"
François Gaffie53615e22015-03-19 09:24:12 +010050#include <policy.h>
Eric Laurente552edb2014-03-10 17:42:56 -070051
Eric Laurent3b73df72014-03-11 09:06:29 -070052namespace android {
Eric Laurente552edb2014-03-10 17:42:56 -070053
Eric Laurentdc462862016-07-19 12:29:53 -070054//FIXME: workaround for truncated touch sounds
55// to be removed when the problem is handled by system UI
56#define TOUCH_SOUND_FIXED_DELAY_MS 100
Eric Laurente552edb2014-03-10 17:42:56 -070057// ----------------------------------------------------------------------------
58// AudioPolicyInterface implementation
59// ----------------------------------------------------------------------------
60
Eric Laurente0720872014-03-11 09:30:41 -070061status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
Paul McLeane743a472015-01-28 11:07:31 -080062 audio_policy_dev_state_t state,
63 const char *device_address,
64 const char *device_name)
Eric Laurente552edb2014-03-10 17:42:56 -070065{
Paul McLeane743a472015-01-28 11:07:31 -080066 return setDeviceConnectionStateInt(device, state, device_address, device_name);
Eric Laurentc73ca6e2014-12-12 14:34:22 -080067}
68
François Gaffie44481e72016-04-20 07:49:57 +020069void AudioPolicyManager::broadcastDeviceConnectionState(audio_devices_t device,
70 audio_policy_dev_state_t state,
71 const String8 &device_address)
72{
73 AudioParameter param(device_address);
74 const String8 key(state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE ?
Mikhail Naganov388360c2016-10-17 17:09:41 -070075 AudioParameter::keyStreamConnect : AudioParameter::keyStreamDisconnect);
François Gaffie44481e72016-04-20 07:49:57 +020076 param.addInt(key, device);
77 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
78}
79
Eric Laurentc73ca6e2014-12-12 14:34:22 -080080status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device,
Eric Laurenta1d525f2015-01-29 13:36:45 -080081 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -080082 const char *device_address,
83 const char *device_name)
Eric Laurentc73ca6e2014-12-12 14:34:22 -080084{
Paul McLeane743a472015-01-28 11:07:31 -080085 ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
86- device, state, device_address, device_name);
Eric Laurente552edb2014-03-10 17:42:56 -070087
88 // connect/disconnect only 1 device at a time
89 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
90
François Gaffie53615e22015-03-19 09:24:12 +010091 sp<DeviceDescriptor> devDesc =
92 mHwModules.getDeviceDescriptor(device, device_address, device_name);
Paul McLeane743a472015-01-28 11:07:31 -080093
Eric Laurente552edb2014-03-10 17:42:56 -070094 // handle output devices
95 if (audio_is_output_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -070096 SortedVector <audio_io_handle_t> outputs;
97
Eric Laurent3a4311c2014-03-17 12:00:47 -070098 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
99
Eric Laurente552edb2014-03-10 17:42:56 -0700100 // save a copy of the opened output descriptors before any output is opened or closed
101 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
102 mPreviousOutputs = mOutputs;
Eric Laurente552edb2014-03-10 17:42:56 -0700103 switch (state)
104 {
105 // handle output device connection
Eric Laurent3ae5f312015-02-03 17:12:08 -0800106 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700107 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700108 ALOGW("setDeviceConnectionState() device already connected: %x", device);
109 return INVALID_OPERATION;
110 }
111 ALOGV("setDeviceConnectionState() connecting device %x", device);
112
Eric Laurente552edb2014-03-10 17:42:56 -0700113 // register new device as available
Eric Laurent3a4311c2014-03-17 12:00:47 -0700114 index = mAvailableOutputDevices.add(devDesc);
115 if (index >= 0) {
François Gaffie53615e22015-03-19 09:24:12 +0100116 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurentcf817a22014-08-04 20:36:31 -0700117 if (module == 0) {
118 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
119 device);
120 mAvailableOutputDevices.remove(devDesc);
121 return INVALID_OPERATION;
122 }
Paul McLeane743a472015-01-28 11:07:31 -0800123 mAvailableOutputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700124 } else {
125 return NO_MEMORY;
Eric Laurente552edb2014-03-10 17:42:56 -0700126 }
127
François Gaffie44481e72016-04-20 07:49:57 +0200128 // Before checking outputs, broadcast connect event to allow HAL to retrieve dynamic
129 // parameters on newly connected devices (instead of opening the outputs...)
130 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
131
Eric Laurenta1d525f2015-01-29 13:36:45 -0800132 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700133 mAvailableOutputDevices.remove(devDesc);
François Gaffie44481e72016-04-20 07:49:57 +0200134
135 broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
136 devDesc->mAddress);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700137 return INVALID_OPERATION;
138 }
François Gaffie2110e042015-03-24 08:41:51 +0100139 // Propagate device availability to Engine
140 mEngine->setDeviceConnectionState(devDesc, state);
141
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700142 // outputs should never be empty here
143 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
144 "checkOutputsForDevice() returned no outputs but status OK");
145 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
146 outputs.size());
Eric Laurent3ae5f312015-02-03 17:12:08 -0800147
Eric Laurent3ae5f312015-02-03 17:12:08 -0800148 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700149 // handle output device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700150 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700151 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700152 ALOGW("setDeviceConnectionState() device not connected: %x", device);
153 return INVALID_OPERATION;
154 }
155
Paul McLean5c477aa2014-08-20 16:47:57 -0700156 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
157
Paul McLeane743a472015-01-28 11:07:31 -0800158 // Send Disconnect to HALs
François Gaffie44481e72016-04-20 07:49:57 +0200159 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700160
Eric Laurente552edb2014-03-10 17:42:56 -0700161 // remove device from available output devices
Eric Laurent3a4311c2014-03-17 12:00:47 -0700162 mAvailableOutputDevices.remove(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700163
Eric Laurenta1d525f2015-01-29 13:36:45 -0800164 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
François Gaffie2110e042015-03-24 08:41:51 +0100165
166 // Propagate device availability to Engine
167 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurente552edb2014-03-10 17:42:56 -0700168 } break;
169
170 default:
171 ALOGE("setDeviceConnectionState() invalid state: %x", state);
172 return BAD_VALUE;
173 }
174
Eric Laurent3a4311c2014-03-17 12:00:47 -0700175 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
176 // output is suspended before any tracks are moved to it
Eric Laurente552edb2014-03-10 17:42:56 -0700177 checkA2dpSuspend();
178 checkOutputForAllStrategies();
179 // outputs must be closed after checkOutputForAllStrategies() is executed
180 if (!outputs.isEmpty()) {
181 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700182 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700183 // close unused outputs after device disconnection or direct outputs that have been
184 // opened by checkOutputsForDevice() to query dynamic parameters
Eric Laurent3b73df72014-03-11 09:06:29 -0700185 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Eric Laurente552edb2014-03-10 17:42:56 -0700186 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
187 (desc->mDirectOpenCount == 0))) {
188 closeOutput(outputs[i]);
189 }
190 }
Eric Laurent3a4311c2014-03-17 12:00:47 -0700191 // check again after closing A2DP output to reset mA2dpSuspended if needed
192 checkA2dpSuspend();
Eric Laurente552edb2014-03-10 17:42:56 -0700193 }
194
195 updateDevicesAndOutputs();
Eric Laurent87ffa392015-05-22 10:32:38 -0700196 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700197 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
198 updateCallRouting(newDevice);
199 }
Eric Laurente552edb2014-03-10 17:42:56 -0700200 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700201 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
202 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
203 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700204 // do not force device change on duplicated output because if device is 0, it will
205 // also force a device 0 for the two outputs it is duplicated to which may override
206 // a valid device selection on those outputs.
Eric Laurentc75307b2015-03-17 15:29:32 -0700207 bool force = !desc->isDuplicated()
François Gaffie53615e22015-03-19 09:24:12 +0100208 && (!device_distinguishes_on_address(device)
Eric Laurentc2730ba2014-07-20 15:47:07 -0700209 // always force when disconnecting (a non-duplicated device)
210 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Eric Laurentc75307b2015-03-17 15:29:32 -0700211 setOutputDevice(desc, newDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700212 }
Eric Laurente552edb2014-03-10 17:42:56 -0700213 }
214
Eric Laurentd60560a2015-04-10 11:31:20 -0700215 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
216 cleanUpForDevice(devDesc);
217 }
218
Eric Laurent72aa32f2014-05-30 18:51:48 -0700219 mpClientInterface->onAudioPortListUpdate();
Eric Laurentb71e58b2014-05-29 16:08:11 -0700220 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700221 } // end if is output device
222
Eric Laurente552edb2014-03-10 17:42:56 -0700223 // handle input devices
224 if (audio_is_input_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700225 SortedVector <audio_io_handle_t> inputs;
226
Eric Laurent3a4311c2014-03-17 12:00:47 -0700227 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700228 switch (state)
229 {
230 // handle input device connection
Eric Laurent3b73df72014-03-11 09:06:29 -0700231 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700232 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700233 ALOGW("setDeviceConnectionState() device already connected: %d", device);
234 return INVALID_OPERATION;
235 }
François Gaffie53615e22015-03-19 09:24:12 +0100236 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurent6a94d692014-05-20 11:18:06 -0700237 if (module == NULL) {
238 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
239 device);
240 return INVALID_OPERATION;
241 }
François Gaffie44481e72016-04-20 07:49:57 +0200242
243 // Before checking intputs, broadcast connect event to allow HAL to retrieve dynamic
244 // parameters on newly connected devices (instead of opening the inputs...)
245 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
246
Paul McLean9080a4c2015-06-18 08:24:02 -0700247 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
François Gaffie44481e72016-04-20 07:49:57 +0200248 broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
249 devDesc->mAddress);
Eric Laurentd4692962014-05-05 18:13:44 -0700250 return INVALID_OPERATION;
251 }
252
Eric Laurent3a4311c2014-03-17 12:00:47 -0700253 index = mAvailableInputDevices.add(devDesc);
254 if (index >= 0) {
Paul McLeane743a472015-01-28 11:07:31 -0800255 mAvailableInputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700256 } else {
257 return NO_MEMORY;
258 }
Eric Laurent3ae5f312015-02-03 17:12:08 -0800259
François Gaffie2110e042015-03-24 08:41:51 +0100260 // Propagate device availability to Engine
261 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700262 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700263
264 // handle input device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700265 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700266 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700267 ALOGW("setDeviceConnectionState() device not connected: %d", device);
268 return INVALID_OPERATION;
269 }
Paul McLean5c477aa2014-08-20 16:47:57 -0700270
271 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
272
273 // Set Disconnect to HALs
François Gaffie44481e72016-04-20 07:49:57 +0200274 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700275
Paul McLean9080a4c2015-06-18 08:24:02 -0700276 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700277 mAvailableInputDevices.remove(devDesc);
Paul McLean5c477aa2014-08-20 16:47:57 -0700278
François Gaffie2110e042015-03-24 08:41:51 +0100279 // Propagate device availability to Engine
280 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700281 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700282
283 default:
284 ALOGE("setDeviceConnectionState() invalid state: %x", state);
285 return BAD_VALUE;
286 }
287
Eric Laurentd4692962014-05-05 18:13:44 -0700288 closeAllInputs();
Eric Laurent5f5fca52016-08-04 11:48:57 -0700289 // As the input device list can impact the output device selection, update
290 // getDeviceForStrategy() cache
291 updateDevicesAndOutputs();
Eric Laurente552edb2014-03-10 17:42:56 -0700292
Eric Laurent87ffa392015-05-22 10:32:38 -0700293 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700294 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
295 updateCallRouting(newDevice);
296 }
297
Eric Laurentd60560a2015-04-10 11:31:20 -0700298 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
299 cleanUpForDevice(devDesc);
300 }
301
Eric Laurentb52c1522014-05-20 11:27:36 -0700302 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700303 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700304 } // end if is input device
Eric Laurente552edb2014-03-10 17:42:56 -0700305
306 ALOGW("setDeviceConnectionState() invalid device: %x", device);
307 return BAD_VALUE;
308}
309
Eric Laurente0720872014-03-11 09:30:41 -0700310audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +0100311 const char *device_address)
Eric Laurente552edb2014-03-10 17:42:56 -0700312{
Eric Laurent634b7142016-04-20 13:48:02 -0700313 sp<DeviceDescriptor> devDesc =
314 mHwModules.getDeviceDescriptor(device, device_address, "",
315 (strlen(device_address) != 0)/*matchAddress*/);
316
317 if (devDesc == 0) {
318 ALOGW("getDeviceConnectionState() undeclared device, type %08x, address: %s",
319 device, device_address);
320 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
321 }
François Gaffie53615e22015-03-19 09:24:12 +0100322
Eric Laurent3a4311c2014-03-17 12:00:47 -0700323 DeviceVector *deviceVector;
324
Eric Laurente552edb2014-03-10 17:42:56 -0700325 if (audio_is_output_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700326 deviceVector = &mAvailableOutputDevices;
Eric Laurente552edb2014-03-10 17:42:56 -0700327 } else if (audio_is_input_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700328 deviceVector = &mAvailableInputDevices;
329 } else {
330 ALOGW("getDeviceConnectionState() invalid device type %08x", device);
331 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700332 }
Eric Laurent634b7142016-04-20 13:48:02 -0700333
334 return (deviceVector->getDevice(device, String8(device_address)) != 0) ?
335 AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurenta1d525f2015-01-29 13:36:45 -0800336}
337
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800338status_t AudioPolicyManager::handleDeviceConfigChange(audio_devices_t device,
339 const char *device_address,
340 const char *device_name)
341{
342 status_t status;
343
344 ALOGV("handleDeviceConfigChange(() device: 0x%X, address %s name %s",
345 device, device_address, device_name);
346
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -0800347 // connect/disconnect only 1 device at a time
348 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
349
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800350 // Check if the device is currently connected
351 sp<DeviceDescriptor> devDesc =
352 mHwModules.getDeviceDescriptor(device, device_address, device_name);
353 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
354 if (index < 0) {
355 // Nothing to do: device is not connected
356 return NO_ERROR;
357 }
358
359 // Toggle the device state: UNAVAILABLE -> AVAILABLE
360 // This will force reading again the device configuration
361 status = setDeviceConnectionState(device,
362 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
363 device_address, device_name);
364 if (status != NO_ERROR) {
365 ALOGW("handleDeviceConfigChange() error disabling connection state: %d",
366 status);
367 return status;
368 }
369
370 status = setDeviceConnectionState(device,
371 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
372 device_address, device_name);
373 if (status != NO_ERROR) {
374 ALOGW("handleDeviceConfigChange() error enabling connection state: %d",
375 status);
376 return status;
377 }
378
379 return NO_ERROR;
380}
381
Eric Laurentdc462862016-07-19 12:29:53 -0700382uint32_t AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, uint32_t delayMs)
Eric Laurentc2730ba2014-07-20 15:47:07 -0700383{
384 bool createTxPatch = false;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700385 status_t status;
386 audio_patch_handle_t afPatchHandle;
387 DeviceVector deviceList;
Eric Laurentdc462862016-07-19 12:29:53 -0700388 uint32_t muteWaitMs = 0;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700389
Andy Hunga76c7de2016-12-13 19:14:31 -0800390 if(!hasPrimaryOutput() || mPrimaryOutput->device() == AUDIO_DEVICE_OUT_STUB) {
Eric Laurentdc462862016-07-19 12:29:53 -0700391 return muteWaitMs;
Eric Laurent87ffa392015-05-22 10:32:38 -0700392 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -0800393 audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700394 ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice);
395
396 // release existing RX patch if any
397 if (mCallRxPatch != 0) {
398 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
399 mCallRxPatch.clear();
400 }
401 // release TX patch if any
402 if (mCallTxPatch != 0) {
403 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
404 mCallTxPatch.clear();
405 }
406
407 // If the RX device is on the primary HW module, then use legacy routing method for voice calls
408 // via setOutputDevice() on primary output.
409 // Otherwise, create two audio patches for TX and RX path.
410 if (availablePrimaryOutputDevices() & rxDevice) {
Eric Laurentdc462862016-07-19 12:29:53 -0700411 muteWaitMs = setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700412 // If the TX device is also on the primary HW module, setOutputDevice() will take care
413 // of it due to legacy implementation. If not, create a patch.
414 if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN)
415 == AUDIO_DEVICE_NONE) {
416 createTxPatch = true;
417 }
Eric Laurent8ae73122016-04-12 10:13:29 -0700418 } else { // create RX path audio patch
419 struct audio_patch patch;
420
421 patch.num_sources = 1;
422 patch.num_sinks = 1;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700423 deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice);
424 ALOG_ASSERT(!deviceList.isEmpty(),
425 "updateCallRouting() selected device not in output device list");
426 sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0);
427 deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX);
428 ALOG_ASSERT(!deviceList.isEmpty(),
429 "updateCallRouting() no telephony RX device");
430 sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0);
431
432 rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
433 rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
434
435 // request to reuse existing output stream if one is already opened to reach the RX device
436 SortedVector<audio_io_handle_t> outputs =
437 getOutputsForDevice(rxDevice, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700438 audio_io_handle_t output = selectOutput(outputs,
439 AUDIO_OUTPUT_FLAG_NONE,
440 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700441 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700442 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700443 ALOG_ASSERT(!outputDesc->isDuplicated(),
444 "updateCallRouting() RX device output is duplicated");
445 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700446 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700447 patch.num_sources = 2;
448 }
449
450 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentdc462862016-07-19 12:29:53 -0700451 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700452 ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch",
453 status);
454 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100455 mCallRxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700456 mCallRxPatch->mAfPatchHandle = afPatchHandle;
457 mCallRxPatch->mUid = mUidCached;
458 }
459 createTxPatch = true;
460 }
Eric Laurent8ae73122016-04-12 10:13:29 -0700461 if (createTxPatch) { // create TX path audio patch
Eric Laurentc2730ba2014-07-20 15:47:07 -0700462 struct audio_patch patch;
Eric Laurent8ae73122016-04-12 10:13:29 -0700463
Eric Laurentc2730ba2014-07-20 15:47:07 -0700464 patch.num_sources = 1;
465 patch.num_sinks = 1;
466 deviceList = mAvailableInputDevices.getDevicesFromType(txDevice);
467 ALOG_ASSERT(!deviceList.isEmpty(),
468 "updateCallRouting() selected device not in input device list");
469 sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0);
470 txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
471 deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
472 ALOG_ASSERT(!deviceList.isEmpty(),
473 "updateCallRouting() no telephony TX device");
474 sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0);
475 txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
476
477 SortedVector<audio_io_handle_t> outputs =
478 getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700479 audio_io_handle_t output = selectOutput(outputs,
480 AUDIO_OUTPUT_FLAG_NONE,
481 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700482 // request to reuse existing output stream if one is already opened to reach the TX
483 // path output device
484 if (output != AUDIO_IO_HANDLE_NONE) {
485 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
486 ALOG_ASSERT(!outputDesc->isDuplicated(),
487 "updateCallRouting() RX device output is duplicated");
488 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700489 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700490 patch.num_sources = 2;
491 }
492
Eric Laurentc0a889f2015-10-14 14:36:34 -0700493 // terminate active capture if on the same HW module as the call TX source device
494 // FIXME: would be better to refine to only inputs whose profile connects to the
495 // call TX device but this information is not in the audio patch and logic here must be
496 // symmetric to the one in startInput()
Eric Laurentfb66dd92016-01-28 18:32:03 -0800497 Vector<sp <AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
498 for (size_t i = 0; i < activeInputs.size(); i++) {
499 sp<AudioInputDescriptor> activeDesc = activeInputs[i];
500 if (activeDesc->hasSameHwModuleAs(txSourceDeviceDesc)) {
501 AudioSessionCollection activeSessions =
502 activeDesc->getAudioSessions(true /*activeOnly*/);
503 for (size_t j = 0; j < activeSessions.size(); j++) {
504 audio_session_t activeSession = activeSessions.keyAt(j);
505 stopInput(activeDesc->mIoHandle, activeSession);
506 releaseInput(activeDesc->mIoHandle, activeSession);
507 }
Eric Laurentc0a889f2015-10-14 14:36:34 -0700508 }
509 }
510
Eric Laurentc2730ba2014-07-20 15:47:07 -0700511 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentdc462862016-07-19 12:29:53 -0700512 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700513 ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch",
514 status);
515 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100516 mCallTxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700517 mCallTxPatch->mAfPatchHandle = afPatchHandle;
518 mCallTxPatch->mUid = mUidCached;
519 }
520 }
Eric Laurentdc462862016-07-19 12:29:53 -0700521
522 return muteWaitMs;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700523}
524
Eric Laurente0720872014-03-11 09:30:41 -0700525void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700526{
527 ALOGV("setPhoneState() state %d", state);
François Gaffie2110e042015-03-24 08:41:51 +0100528 // store previous phone state for management of sonification strategy below
529 int oldState = mEngine->getPhoneState();
530
531 if (mEngine->setPhoneState(state) != NO_ERROR) {
532 ALOGW("setPhoneState() invalid or same state %d", state);
Eric Laurente552edb2014-03-10 17:42:56 -0700533 return;
534 }
François Gaffie2110e042015-03-24 08:41:51 +0100535 /// Opens: can these line be executed after the switch of volume curves???
Eric Laurente552edb2014-03-10 17:42:56 -0700536 // if leaving call state, handle special case of active streams
537 // pertaining to sonification strategy see handleIncallSonification()
Eric Laurent63dea1d2015-07-02 17:10:28 -0700538 if (isStateInCall(oldState)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700539 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent794fde22016-03-11 09:50:45 -0800540 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700541 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700542 }
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800543
Eric Laurent63dea1d2015-07-02 17:10:28 -0700544 // force reevaluating accessibility routing when call stops
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800545 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700546 }
547
François Gaffie2110e042015-03-24 08:41:51 +0100548 /**
549 * Switching to or from incall state or switching between telephony and VoIP lead to force
550 * routing command.
551 */
552 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
553 || (is_state_in_call(state) && (state != oldState)));
Eric Laurente552edb2014-03-10 17:42:56 -0700554
555 // check for device and output changes triggered by new phone state
Eric Laurente552edb2014-03-10 17:42:56 -0700556 checkA2dpSuspend();
557 checkOutputForAllStrategies();
558 updateDevicesAndOutputs();
559
Eric Laurente552edb2014-03-10 17:42:56 -0700560 int delayMs = 0;
561 if (isStateInCall(state)) {
562 nsecs_t sysTime = systemTime();
563 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700564 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700565 // mute media and sonification strategies and delay device switch by the largest
566 // latency of any output where either strategy is active.
567 // This avoid sending the ring tone or music tail into the earpiece or headset.
François Gaffiead3183e2015-03-18 16:55:35 +0100568 if ((isStrategyActive(desc, STRATEGY_MEDIA,
569 SONIFICATION_HEADSET_MUSIC_DELAY,
570 sysTime) ||
571 isStrategyActive(desc, STRATEGY_SONIFICATION,
572 SONIFICATION_HEADSET_MUSIC_DELAY,
573 sysTime)) &&
Eric Laurentc75307b2015-03-17 15:29:32 -0700574 (delayMs < (int)desc->latency()*2)) {
575 delayMs = desc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -0700576 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700577 setStrategyMute(STRATEGY_MEDIA, true, desc);
578 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700579 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Eric Laurentc75307b2015-03-17 15:29:32 -0700580 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
581 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700582 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
583 }
584 }
585
Eric Laurent87ffa392015-05-22 10:32:38 -0700586 if (hasPrimaryOutput()) {
587 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
588 // the device returned is not necessarily reachable via this output
589 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
590 // force routing command to audio hardware when ending call
591 // even if no device change is needed
592 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
593 rxDevice = mPrimaryOutput->device();
594 }
Eric Laurente552edb2014-03-10 17:42:56 -0700595
Eric Laurent87ffa392015-05-22 10:32:38 -0700596 if (state == AUDIO_MODE_IN_CALL) {
597 updateCallRouting(rxDevice, delayMs);
598 } else if (oldState == AUDIO_MODE_IN_CALL) {
599 if (mCallRxPatch != 0) {
600 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
601 mCallRxPatch.clear();
602 }
603 if (mCallTxPatch != 0) {
604 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
605 mCallTxPatch.clear();
606 }
607 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
608 } else {
609 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700610 }
Eric Laurentc2730ba2014-07-20 15:47:07 -0700611 }
Eric Laurente552edb2014-03-10 17:42:56 -0700612 // if entering in call state, handle special case of active streams
613 // pertaining to sonification strategy see handleIncallSonification()
614 if (isStateInCall(state)) {
615 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent794fde22016-03-11 09:50:45 -0800616 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700617 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700618 }
Eric Laurent63dea1d2015-07-02 17:10:28 -0700619
620 // force reevaluating accessibility routing when call starts
621 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700622 }
623
624 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700625 if (state == AUDIO_MODE_RINGTONE &&
626 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700627 mLimitRingtoneVolume = true;
628 } else {
629 mLimitRingtoneVolume = false;
630 }
631}
632
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700633audio_mode_t AudioPolicyManager::getPhoneState() {
634 return mEngine->getPhoneState();
635}
636
Eric Laurente0720872014-03-11 09:30:41 -0700637void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700638 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700639{
François Gaffie2110e042015-03-24 08:41:51 +0100640 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Eric Laurent8dc87a62017-05-16 19:00:40 -0700641 if (config == mEngine->getForceUse(usage)) {
642 return;
643 }
Eric Laurente552edb2014-03-10 17:42:56 -0700644
François Gaffie2110e042015-03-24 08:41:51 +0100645 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
646 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
647 return;
Eric Laurente552edb2014-03-10 17:42:56 -0700648 }
François Gaffie2110e042015-03-24 08:41:51 +0100649 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
650 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
651 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
Eric Laurente552edb2014-03-10 17:42:56 -0700652
653 // check for device and output changes triggered by new force usage
654 checkA2dpSuspend();
655 checkOutputForAllStrategies();
656 updateDevicesAndOutputs();
Phil Burk09bc4612016-02-24 15:58:15 -0800657
Eric Laurentdc462862016-07-19 12:29:53 -0700658 //FIXME: workaround for truncated touch sounds
659 // to be removed when the problem is handled by system UI
660 uint32_t delayMs = 0;
661 uint32_t waitMs = 0;
662 if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) {
663 delayMs = TOUCH_SOUND_FIXED_DELAY_MS;
664 }
Eric Laurent87ffa392015-05-22 10:32:38 -0700665 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700666 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
Eric Laurentdc462862016-07-19 12:29:53 -0700667 waitMs = updateCallRouting(newDevice, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700668 }
Eric Laurente552edb2014-03-10 17:42:56 -0700669 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700670 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
671 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
672 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
Eric Laurentdc462862016-07-19 12:29:53 -0700673 waitMs = setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE),
674 delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700675 }
Eric Laurente552edb2014-03-10 17:42:56 -0700676 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
Eric Laurentdc462862016-07-19 12:29:53 -0700677 applyStreamVolumes(outputDesc, newDevice, waitMs, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700678 }
679 }
680
Eric Laurentfb66dd92016-01-28 18:32:03 -0800681 Vector<sp <AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
682 for (size_t i = 0; i < activeInputs.size(); i++) {
683 sp<AudioInputDescriptor> activeDesc = activeInputs[i];
684 audio_devices_t newDevice = getNewInputDevice(activeDesc);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700685 // Force new input selection if the new device can not be reached via current input
Eric Laurentfb66dd92016-01-28 18:32:03 -0800686 if (activeDesc->mProfile->getSupportedDevices().types() &
687 (newDevice & ~AUDIO_DEVICE_BIT_IN)) {
688 setInputDevice(activeDesc->mIoHandle, newDevice);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700689 } else {
Eric Laurentfb66dd92016-01-28 18:32:03 -0800690 closeInput(activeDesc->mIoHandle);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700691 }
Eric Laurente552edb2014-03-10 17:42:56 -0700692 }
Eric Laurente552edb2014-03-10 17:42:56 -0700693}
694
Eric Laurente0720872014-03-11 09:30:41 -0700695void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700696{
697 ALOGV("setSystemProperty() property %s, value %s", property, value);
698}
699
700// Find a direct output profile compatible with the parameters passed, even if the input flags do
701// not explicitly request a direct output
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800702sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700703 audio_devices_t device,
704 uint32_t samplingRate,
705 audio_format_t format,
706 audio_channel_mask_t channelMask,
707 audio_output_flags_t flags)
708{
Eric Laurent861a6282015-05-18 15:40:16 -0700709 // only retain flags that will drive the direct output profile selection
710 // if explicitly requested
711 static const uint32_t kRelevantFlags =
Haynes Mathew George84c621e2017-04-25 11:41:50 -0700712 (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD |
713 AUDIO_OUTPUT_FLAG_VOIP_RX);
Eric Laurent861a6282015-05-18 15:40:16 -0700714 flags =
715 (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT);
716
717 sp<IOProfile> profile;
718
Eric Laurente552edb2014-03-10 17:42:56 -0700719 for (size_t i = 0; i < mHwModules.size(); i++) {
720 if (mHwModules[i]->mHandle == 0) {
721 continue;
722 }
723 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent861a6282015-05-18 15:40:16 -0700724 sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j];
725 if (!curProfile->isCompatibleProfile(device, String8(""),
Andy Hungf129b032015-04-07 13:45:50 -0700726 samplingRate, NULL /*updatedSamplingRate*/,
727 format, NULL /*updatedFormat*/,
728 channelMask, NULL /*updatedChannelMask*/,
Eric Laurent861a6282015-05-18 15:40:16 -0700729 flags)) {
730 continue;
731 }
732 // reject profiles not corresponding to a device currently available
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100733 if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) {
Eric Laurent861a6282015-05-18 15:40:16 -0700734 continue;
735 }
736 // if several profiles are compatible, give priority to one with offload capability
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100737 if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) {
Eric Laurent861a6282015-05-18 15:40:16 -0700738 continue;
739 }
740 profile = curProfile;
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100741 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent861a6282015-05-18 15:40:16 -0700742 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700743 }
Eric Laurente552edb2014-03-10 17:42:56 -0700744 }
745 }
Eric Laurent861a6282015-05-18 15:40:16 -0700746 return profile;
Eric Laurente552edb2014-03-10 17:42:56 -0700747}
748
Eric Laurente0720872014-03-11 09:30:41 -0700749audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +0100750 uint32_t samplingRate,
751 audio_format_t format,
752 audio_channel_mask_t channelMask,
753 audio_output_flags_t flags,
754 const audio_offload_info_t *offloadInfo)
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*/);
758 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
759 device, stream, samplingRate, format, channelMask, flags);
760
Kevin Rocard169753c2017-03-06 14:18:23 -0800761 return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE, stream, samplingRate, format,
762 channelMask, flags, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700763}
764
Eric Laurente83b55d2014-11-14 10:06:21 -0800765status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
766 audio_io_handle_t *output,
767 audio_session_t session,
768 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700769 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800770 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800771 audio_output_flags_t flags,
Eric Laurent2ac76942017-06-22 17:17:09 -0700772 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800773 audio_port_handle_t *portId)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700774{
Eric Laurente83b55d2014-11-14 10:06:21 -0800775 audio_attributes_t attributes;
776 if (attr != NULL) {
777 if (!isValidAttributes(attr)) {
778 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
779 attr->usage, attr->content_type, attr->flags,
780 attr->tags);
781 return BAD_VALUE;
782 }
783 attributes = *attr;
784 } else {
785 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
786 ALOGE("getOutputForAttr(): invalid stream type");
787 return BAD_VALUE;
788 }
789 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700790 }
Eric Laurent20b9ef02016-12-05 11:03:16 -0800791
792 // TODO: check for existing client for this port ID
793 if (*portId == AUDIO_PORT_HANDLE_NONE) {
794 *portId = AudioPort::getNextUniqueId();
795 }
796
Eric Laurentc75307b2015-03-17 15:29:32 -0700797 sp<SwAudioOutputDescriptor> desc;
Jean-Michel Trivie8deced2016-02-11 12:50:39 -0800798 if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) {
François Gaffie036e1e92015-03-19 10:16:24 +0100799 ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr");
Eric Laurent20b9ef02016-12-05 11:03:16 -0800800 if (!audio_has_proportional_frames(config->format)) {
François Gaffie036e1e92015-03-19 10:16:24 +0100801 return BAD_VALUE;
Eric Laurent275e8e92014-11-30 15:14:47 -0800802 }
François Gaffie036e1e92015-03-19 10:16:24 +0100803 *stream = streamTypefromAttributesInt(&attributes);
804 *output = desc->mIoHandle;
805 ALOGV("getOutputForAttr() returns output %d", *output);
806 return NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -0800807 }
808 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
809 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
810 return BAD_VALUE;
811 }
812
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700813 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x"
814 " session %d selectedDeviceId %d",
815 attributes.usage, attributes.content_type, attributes.tags, attributes.flags,
Eric Laurent2ac76942017-06-22 17:17:09 -0700816 session, *selectedDeviceId);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700817
818 *stream = streamTypefromAttributesInt(&attributes);
819
820 // Explicit routing?
821 sp<DeviceDescriptor> deviceDesc;
Eric Laurent2ac76942017-06-22 17:17:09 -0700822 if (*selectedDeviceId != AUDIO_PORT_HANDLE_NONE) {
823 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
824 if (mAvailableOutputDevices[i]->getId() == *selectedDeviceId) {
825 deviceDesc = mAvailableOutputDevices[i];
826 break;
827 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700828 }
829 }
830 mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700831
Eric Laurente83b55d2014-11-14 10:06:21 -0800832 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700833 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700834
Eric Laurente83b55d2014-11-14 10:06:21 -0800835 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700836 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
837 }
838
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700839 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Eric Laurent20b9ef02016-12-05 11:03:16 -0800840 device, config->sample_rate, config->format, config->channel_mask, flags);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700841
Kevin Rocard169753c2017-03-06 14:18:23 -0800842 *output = getOutputForDevice(device, session, *stream,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800843 config->sample_rate, config->format, config->channel_mask,
844 flags, &config->offload_info);
Eric Laurente83b55d2014-11-14 10:06:21 -0800845 if (*output == AUDIO_IO_HANDLE_NONE) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700846 mOutputRoutes.removeRoute(session);
Eric Laurente83b55d2014-11-14 10:06:21 -0800847 return INVALID_OPERATION;
848 }
Paul McLeanaa981192015-03-21 09:55:15 -0700849
Eric Laurent2ac76942017-06-22 17:17:09 -0700850 DeviceVector outputDevices = mAvailableOutputDevices.getDevicesFromType(device);
851 *selectedDeviceId = outputDevices.size() > 0 ? outputDevices.itemAt(0)->getId()
852 : AUDIO_PORT_HANDLE_NONE;
853
854 ALOGV(" getOutputForAttr() returns output %d selectedDeviceId %d", *output, *selectedDeviceId);
855
Eric Laurente83b55d2014-11-14 10:06:21 -0800856 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700857}
858
859audio_io_handle_t AudioPolicyManager::getOutputForDevice(
860 audio_devices_t device,
Kevin Rocard169753c2017-03-06 14:18:23 -0800861 audio_session_t session,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700862 audio_stream_type_t stream,
863 uint32_t samplingRate,
864 audio_format_t format,
865 audio_channel_mask_t channelMask,
866 audio_output_flags_t flags,
867 const audio_offload_info_t *offloadInfo)
868{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700869 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700870 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700871
Eric Laurente552edb2014-03-10 17:42:56 -0700872#ifdef AUDIO_POLICY_TEST
873 if (mCurOutput != 0) {
874 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
875 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
876
877 if (mTestOutputs[mCurOutput] == 0) {
878 ALOGV("getOutput() opening test output");
Eric Laurentc75307b2015-03-17 15:29:32 -0700879 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
880 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700881 outputDesc->mDevice = mTestDevice;
Eric Laurente552edb2014-03-10 17:42:56 -0700882 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700883 outputDesc->mFlags =
884 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700885 outputDesc->mRefCount[stream] = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700886 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
887 config.sample_rate = mTestSamplingRate;
888 config.channel_mask = mTestChannels;
889 config.format = mTestFormat;
Phil Burk77cce802014-08-04 16:18:15 -0700890 if (offloadInfo != NULL) {
891 config.offload_info = *offloadInfo;
892 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700893 status = mpClientInterface->openOutput(0,
894 &mTestOutputs[mCurOutput],
895 &config,
896 &outputDesc->mDevice,
897 String8(""),
898 &outputDesc->mLatency,
899 outputDesc->mFlags);
900 if (status == NO_ERROR) {
901 outputDesc->mSamplingRate = config.sample_rate;
902 outputDesc->mFormat = config.format;
903 outputDesc->mChannelMask = config.channel_mask;
Eric Laurente552edb2014-03-10 17:42:56 -0700904 AudioParameter outputCmd = AudioParameter();
905 outputCmd.addInt(String8("set_id"),mCurOutput);
906 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
907 addOutput(mTestOutputs[mCurOutput], outputDesc);
908 }
909 }
910 return mTestOutputs[mCurOutput];
911 }
912#endif //AUDIO_POLICY_TEST
913
914 // open a direct output if required by specified parameters
915 //force direct flag if offload flag is set: offloading implies a direct output stream
916 // and all common behaviors are driven by checking only the direct flag
917 // this should normally be set appropriately in the policy configuration file
918 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700919 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700920 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700921 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
922 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
923 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800924 // only allow deep buffering for music stream type
925 if (stream != AUDIO_STREAM_MUSIC) {
926 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Ravi Kumar Alamanda439e4ed2015-04-03 12:13:21 -0700927 } else if (/* stream == AUDIO_STREAM_MUSIC && */
928 flags == AUDIO_OUTPUT_FLAG_NONE &&
929 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
930 // use DEEP_BUFFER as default output for music stream type
931 flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Eric Laurente83b55d2014-11-14 10:06:21 -0800932 }
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700933 if (stream == AUDIO_STREAM_TTS) {
934 flags = AUDIO_OUTPUT_FLAG_TTS;
Haynes Mathew George84c621e2017-04-25 11:41:50 -0700935 } else if (stream == AUDIO_STREAM_VOICE_CALL &&
936 getPhoneState() == AUDIO_MODE_IN_COMMUNICATION &&
937 audio_is_linear_pcm(format)) {
938 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX |
939 AUDIO_OUTPUT_FLAG_DIRECT);
940 ALOGV("Set VoIP and Direct output flags for PCM format");
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700941 }
Eric Laurente552edb2014-03-10 17:42:56 -0700942
Eric Laurentb732cf52014-09-24 19:08:21 -0700943 sp<IOProfile> profile;
944
945 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700946 // and not explicitly requested
947 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
Glenn Kasten05ddca52016-02-11 08:17:12 -0800948 audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700949 audio_channel_count_from_out_mask(channelMask) <= 2) {
950 goto non_direct_output;
951 }
952
Andy Hung2ddee192015-12-18 17:34:44 -0800953 // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled.
954 // This prevents creating an offloaded track and tearing it down immediately after start
955 // when audioflinger detects there is an active non offloadable effect.
Eric Laurente552edb2014-03-10 17:42:56 -0700956 // FIXME: We should check the audio session here but we do not have it in this context.
957 // This may prevent offloading in rare situations where effects are left active by apps
958 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700959
Eric Laurente552edb2014-03-10 17:42:56 -0700960 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
Andy Hung2ddee192015-12-18 17:34:44 -0800961 !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700962 profile = getProfileForDirectOutput(device,
963 samplingRate,
964 format,
965 channelMask,
966 (audio_output_flags_t)flags);
967 }
968
Eric Laurent1c333e22014-05-20 10:48:17 -0700969 if (profile != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700970 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700971
972 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700973 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700974 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
975 outputDesc = desc;
Kevin Rocard551061a2017-02-06 15:59:29 -0800976 // reuse direct output if currently open by the same client
977 // and configured with same parameters
Eric Laurente552edb2014-03-10 17:42:56 -0700978 if ((samplingRate == outputDesc->mSamplingRate) &&
Kevin Rocard551061a2017-02-06 15:59:29 -0800979 audio_formats_match(format, outputDesc->mFormat) &&
980 (channelMask == outputDesc->mChannelMask)) {
Kevin Rocard169753c2017-03-06 14:18:23 -0800981 if (session == outputDesc->mDirectClientSession) {
Kevin Rocard551061a2017-02-06 15:59:29 -0800982 outputDesc->mDirectOpenCount++;
Kevin Rocard169753c2017-03-06 14:18:23 -0800983 ALOGV("getOutput() reusing direct output %d for session %d",
984 mOutputs.keyAt(i), session);
Kevin Rocard551061a2017-02-06 15:59:29 -0800985 return mOutputs.keyAt(i);
986 } else {
Kevin Rocard169753c2017-03-06 14:18:23 -0800987 ALOGV("getOutput() do not reuse direct output because current client (%d) "
988 "is not the same as requesting client (%d)",
989 outputDesc->mDirectClientSession, session);
Kevin Rocard551061a2017-02-06 15:59:29 -0800990 goto non_direct_output;
991 }
Eric Laurente552edb2014-03-10 17:42:56 -0700992 }
993 }
994 }
995 // close direct output if currently open and configured with different parameters
996 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700997 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700998 }
Eric Laurent861a6282015-05-18 15:40:16 -0700999
1000 // if the selected profile is offloaded and no offload info was specified,
1001 // create a default one
1002 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001003 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
Eric Laurent861a6282015-05-18 15:40:16 -07001004 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1005 defaultOffloadInfo.sample_rate = samplingRate;
1006 defaultOffloadInfo.channel_mask = channelMask;
1007 defaultOffloadInfo.format = format;
1008 defaultOffloadInfo.stream_type = stream;
1009 defaultOffloadInfo.bit_rate = 0;
1010 defaultOffloadInfo.duration_us = -1;
1011 defaultOffloadInfo.has_video = true; // conservative
1012 defaultOffloadInfo.is_streaming = true; // likely
1013 offloadInfo = &defaultOffloadInfo;
1014 }
1015
Eric Laurentc75307b2015-03-17 15:29:32 -07001016 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07001017 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -07001018 outputDesc->mLatency = 0;
Eric Laurent861a6282015-05-18 15:40:16 -07001019 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001020 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1021 config.sample_rate = samplingRate;
1022 config.channel_mask = channelMask;
1023 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -07001024 if (offloadInfo != NULL) {
1025 config.offload_info = *offloadInfo;
1026 }
Eric Laurente3014102017-05-03 11:15:43 -07001027 DeviceVector outputDevices = mAvailableOutputDevices.getDevicesFromType(device);
1028 String8 address = outputDevices.size() > 0 ? outputDevices.itemAt(0)->mAddress
1029 : String8("");
Eric Laurent322b4d22015-04-03 15:57:54 -07001030 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07001031 &output,
1032 &config,
1033 &outputDesc->mDevice,
Eric Laurente3014102017-05-03 11:15:43 -07001034 address,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001035 &outputDesc->mLatency,
1036 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001037
1038 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -07001039 if (status != NO_ERROR ||
1040 (samplingRate != 0 && samplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -08001041 (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) ||
Eric Laurentcf2c0212014-07-25 16:20:43 -07001042 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -07001043 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1044 "format %d %d, channelMask %04x %04x", output, samplingRate,
1045 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1046 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001047 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07001048 mpClientInterface->closeOutput(output);
1049 }
Eric Laurenta82797f2015-01-30 11:49:43 -08001050 // fall back to mixer output if possible when the direct output could not be open
Glenn Kasten05ddca52016-02-11 08:17:12 -08001051 if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) {
Eric Laurenta82797f2015-01-30 11:49:43 -08001052 goto non_direct_output;
1053 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001054 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001055 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001056 outputDesc->mSamplingRate = config.sample_rate;
1057 outputDesc->mChannelMask = config.channel_mask;
1058 outputDesc->mFormat = config.format;
1059 outputDesc->mRefCount[stream] = 0;
1060 outputDesc->mStopTime[stream] = 0;
1061 outputDesc->mDirectOpenCount = 1;
Kevin Rocard169753c2017-03-06 14:18:23 -08001062 outputDesc->mDirectClientSession = session;
1063
Eric Laurente552edb2014-03-10 17:42:56 -07001064 addOutput(output, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07001065 mPreviousOutputs = mOutputs;
1066 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -07001067 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001068 return output;
1069 }
1070
Eric Laurentb732cf52014-09-24 19:08:21 -07001071non_direct_output:
Eric Laurent14cbfca2016-03-17 09:42:16 -07001072
1073 // A request for HW A/V sync cannot fallback to a mixed output because time
1074 // stamps are embedded in audio data
1075 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1076 return AUDIO_IO_HANDLE_NONE;
1077 }
1078
Eric Laurente552edb2014-03-10 17:42:56 -07001079 // ignoring channel mask due to downmix capability in mixer
1080
1081 // open a non direct output
1082
1083 // for non direct outputs, only PCM is supported
1084 if (audio_is_linear_pcm(format)) {
1085 // get which output is suitable for the specified stream. The actual
1086 // routing change will happen when startOutput() will be called
1087 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1088
Eric Laurent8838a382014-09-08 16:44:28 -07001089 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1090 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1091 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -07001092 }
1093 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1094 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1095
Eric Laurente552edb2014-03-10 17:42:56 -07001096 return output;
1097}
1098
Eric Laurente0720872014-03-11 09:30:41 -07001099audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -07001100 audio_output_flags_t flags,
1101 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -07001102{
1103 // select one output among several that provide a path to a particular device or set of
1104 // devices (the list was previously build by getOutputsForDevice()).
1105 // The priority is as follows:
1106 // 1: the output with the highest number of requested policy flags
Eric Laurente6930022016-02-11 10:20:40 -08001107 // 2: the output with the bit depth the closest to the requested one
1108 // 3: the primary output
1109 // 4: the first output in the list
Eric Laurente552edb2014-03-10 17:42:56 -07001110
1111 if (outputs.size() == 0) {
1112 return 0;
1113 }
1114 if (outputs.size() == 1) {
1115 return outputs[0];
1116 }
1117
1118 int maxCommonFlags = 0;
Eric Laurente6930022016-02-11 10:20:40 -08001119 audio_io_handle_t outputForFlags = 0;
1120 audio_io_handle_t outputForPrimary = 0;
1121 audio_io_handle_t outputForFormat = 0;
1122 audio_format_t bestFormat = AUDIO_FORMAT_INVALID;
1123 audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07001124
1125 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001126 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -07001127 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -07001128 // if a valid format is specified, skip output if not compatible
1129 if (format != AUDIO_FORMAT_INVALID) {
1130 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente6930022016-02-11 10:20:40 -08001131 if (!audio_formats_match(format, outputDesc->mFormat)) {
Eric Laurent8838a382014-09-08 16:44:28 -07001132 continue;
1133 }
1134 } else if (!audio_is_linear_pcm(format)) {
1135 continue;
1136 }
Eric Laurente6930022016-02-11 10:20:40 -08001137 if (AudioPort::isBetterFormatMatch(
1138 outputDesc->mFormat, bestFormat, format)) {
1139 outputForFormat = outputs[i];
1140 bestFormat = outputDesc->mFormat;
1141 }
Eric Laurent8838a382014-09-08 16:44:28 -07001142 }
1143
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001144 int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags);
Eric Laurente6930022016-02-11 10:20:40 -08001145 if (commonFlags >= maxCommonFlags) {
1146 if (commonFlags == maxCommonFlags) {
1147 if (AudioPort::isBetterFormatMatch(
1148 outputDesc->mFormat, bestFormatForFlags, format)) {
1149 outputForFlags = outputs[i];
1150 bestFormatForFlags = outputDesc->mFormat;
1151 }
1152 } else {
1153 outputForFlags = outputs[i];
1154 maxCommonFlags = commonFlags;
1155 bestFormatForFlags = outputDesc->mFormat;
1156 }
Eric Laurente552edb2014-03-10 17:42:56 -07001157 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
1158 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001159 if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurente6930022016-02-11 10:20:40 -08001160 outputForPrimary = outputs[i];
Eric Laurente552edb2014-03-10 17:42:56 -07001161 }
1162 }
1163 }
1164
Eric Laurente6930022016-02-11 10:20:40 -08001165 if (outputForFlags != 0) {
1166 return outputForFlags;
Eric Laurente552edb2014-03-10 17:42:56 -07001167 }
Eric Laurente6930022016-02-11 10:20:40 -08001168 if (outputForFormat != 0) {
1169 return outputForFormat;
1170 }
1171 if (outputForPrimary != 0) {
1172 return outputForPrimary;
Eric Laurente552edb2014-03-10 17:42:56 -07001173 }
1174
1175 return outputs[0];
1176}
1177
Eric Laurente0720872014-03-11 09:30:41 -07001178status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001179 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001180 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001181{
Paul McLeanaa981192015-03-21 09:55:15 -07001182 ALOGV("startOutput() output %d, stream %d, session %d",
1183 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -07001184 ssize_t index = mOutputs.indexOfKey(output);
1185 if (index < 0) {
1186 ALOGW("startOutput() unknown output %d", output);
1187 return BAD_VALUE;
1188 }
1189
Eric Laurentc75307b2015-03-17 15:29:32 -07001190 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
1191
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001192 // Routing?
1193 mOutputRoutes.incRouteActivity(session);
1194
Eric Laurentc75307b2015-03-17 15:29:32 -07001195 audio_devices_t newDevice;
Eric Laurentc40d9692016-04-13 19:14:13 -07001196 AudioMix *policyMix = NULL;
1197 const char *address = NULL;
Eric Laurentc75307b2015-03-17 15:29:32 -07001198 if (outputDesc->mPolicyMix != NULL) {
Eric Laurentc40d9692016-04-13 19:14:13 -07001199 policyMix = outputDesc->mPolicyMix;
1200 address = policyMix->mDeviceAddress.string();
1201 if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
1202 newDevice = policyMix->mDeviceType;
1203 } else {
1204 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
1205 }
Eric Laurent493404d2015-04-21 15:07:36 -07001206 } else if (mOutputRoutes.hasRouteChanged(session)) {
1207 newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001208 checkStrategyRoute(getStrategy(stream), output);
Eric Laurentc75307b2015-03-17 15:29:32 -07001209 } else {
1210 newDevice = AUDIO_DEVICE_NONE;
1211 }
1212
1213 uint32_t delayMs = 0;
1214
Eric Laurentc40d9692016-04-13 19:14:13 -07001215 status_t status = startSource(outputDesc, stream, newDevice, address, &delayMs);
Eric Laurentc75307b2015-03-17 15:29:32 -07001216
1217 if (status != NO_ERROR) {
1218 mOutputRoutes.decRouteActivity(session);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001219 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -07001220 }
1221 // Automatically enable the remote submix input when output is started on a re routing mix
1222 // of type MIX_TYPE_RECORDERS
Eric Laurentc40d9692016-04-13 19:14:13 -07001223 if (audio_is_remote_submix_device(newDevice) && policyMix != NULL &&
1224 policyMix->mMixType == MIX_TYPE_RECORDERS) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001225 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1226 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Eric Laurentc40d9692016-04-13 19:14:13 -07001227 address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001228 "remote-submix");
1229 }
1230
1231 if (delayMs != 0) {
1232 usleep(delayMs * 1000);
1233 }
1234
1235 return status;
1236}
1237
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001238status_t AudioPolicyManager::startSource(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurentc75307b2015-03-17 15:29:32 -07001239 audio_stream_type_t stream,
1240 audio_devices_t device,
Eric Laurentc40d9692016-04-13 19:14:13 -07001241 const char *address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001242 uint32_t *delayMs)
1243{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001244 // cannot start playback of STREAM_TTS if any other output is being used
1245 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001246
1247 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001248 if (stream == AUDIO_STREAM_TTS) {
1249 ALOGV("\t found BEACON stream");
Eric Laurent9459fb02015-08-12 18:36:32 -07001250 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001251 return INVALID_OPERATION;
1252 } else {
1253 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1254 }
1255 } else {
1256 // some playback other than beacon starts
1257 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1258 }
1259
Eric Laurent77305a62016-07-25 16:39:22 -07001260 // force device change if the output is inactive and no audio patch is already present.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001261 // check active before incrementing usage count
Eric Laurent77305a62016-07-25 16:39:22 -07001262 bool force = !outputDesc->isActive() &&
1263 (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001264
Eric Laurente552edb2014-03-10 17:42:56 -07001265 // increment usage count for this stream on the requested output:
1266 // NOTE that the usage count is the same for duplicated output and hardware output which is
1267 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1268 outputDesc->changeRefCount(stream, 1);
1269
Eric Laurent36829f92017-04-07 19:04:42 -07001270 if (stream == AUDIO_STREAM_MUSIC) {
1271 selectOutputForMusicEffects();
1272 }
1273
Eric Laurent493404d2015-04-21 15:07:36 -07001274 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001275 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001276 if (device == AUDIO_DEVICE_NONE) {
1277 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001278 }
Eric Laurent36829f92017-04-07 19:04:42 -07001279
Eric Laurente552edb2014-03-10 17:42:56 -07001280 routing_strategy strategy = getStrategy(stream);
1281 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001282 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1283 (beaconMuteLatency > 0);
1284 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001285 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001286 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001287 if (desc != outputDesc) {
Eric Laurent77305a62016-07-25 16:39:22 -07001288 // force a device change if any other output is:
1289 // - managed by the same hw module
1290 // - has a current device selection that differs from selected device.
1291 // - supports currently selected device
1292 // - has an active audio patch
Eric Laurente552edb2014-03-10 17:42:56 -07001293 // In this case, the audio HAL must receive the new device selection so that it can
1294 // change the device currently selected by the other active output.
1295 if (outputDesc->sharesHwModuleWith(desc) &&
Eric Laurent77305a62016-07-25 16:39:22 -07001296 desc->device() != device &&
1297 desc->supportedDevices() & device &&
1298 desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07001299 force = true;
1300 }
1301 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001302 // a notification so that audio focus effect can propagate, or that a mute/unmute
1303 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001304 uint32_t latency = desc->latency();
1305 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1306 waitMs = latency;
1307 }
1308 }
1309 }
Eric Laurentdc462862016-07-19 12:29:53 -07001310 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force, 0, NULL, address);
Eric Laurente552edb2014-03-10 17:42:56 -07001311
1312 // handle special case for sonification while in call
1313 if (isInCall()) {
1314 handleIncallSonification(stream, true, false);
1315 }
1316
1317 // apply volume rules for current stream and device if necessary
1318 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01001319 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07001320 outputDesc,
1321 device);
Eric Laurente552edb2014-03-10 17:42:56 -07001322
1323 // update the outputs if starting an output with a stream that can affect notification
1324 // routing
1325 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001326
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001327 // force reevaluating accessibility routing when ringtone or alarm starts
1328 if (strategy == STRATEGY_SONIFICATION) {
1329 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1330 }
Eric Laurentdc462862016-07-19 12:29:53 -07001331
1332 if (waitMs > muteWaitMs) {
1333 *delayMs = waitMs - muteWaitMs;
1334 }
Eric Laurente552edb2014-03-10 17:42:56 -07001335 }
Eric Laurentdc462862016-07-19 12:29:53 -07001336
Eric Laurente552edb2014-03-10 17:42:56 -07001337 return NO_ERROR;
1338}
1339
1340
Eric Laurente0720872014-03-11 09:30:41 -07001341status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001342 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001343 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001344{
1345 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1346 ssize_t index = mOutputs.indexOfKey(output);
1347 if (index < 0) {
1348 ALOGW("stopOutput() unknown output %d", output);
1349 return BAD_VALUE;
1350 }
1351
Eric Laurentc75307b2015-03-17 15:29:32 -07001352 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001353
Eric Laurentc75307b2015-03-17 15:29:32 -07001354 if (outputDesc->mRefCount[stream] == 1) {
1355 // Automatically disable the remote submix input when output is stopped on a
1356 // re routing mix of type MIX_TYPE_RECORDERS
1357 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1358 outputDesc->mPolicyMix != NULL &&
1359 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1360 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1361 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001362 outputDesc->mPolicyMix->mDeviceAddress,
Eric Laurentc75307b2015-03-17 15:29:32 -07001363 "remote-submix");
1364 }
1365 }
1366
1367 // Routing?
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001368 bool forceDeviceUpdate = false;
Eric Laurentc75307b2015-03-17 15:29:32 -07001369 if (outputDesc->mRefCount[stream] > 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001370 int activityCount = mOutputRoutes.decRouteActivity(session);
1371 forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0));
1372
1373 if (forceDeviceUpdate) {
1374 checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE);
1375 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001376 }
1377
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001378 return stopSource(outputDesc, stream, forceDeviceUpdate);
Eric Laurentc75307b2015-03-17 15:29:32 -07001379}
1380
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001381status_t AudioPolicyManager::stopSource(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001382 audio_stream_type_t stream,
1383 bool forceDeviceUpdate)
Eric Laurentc75307b2015-03-17 15:29:32 -07001384{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001385 // always handle stream stop, check which stream type is stopping
1386 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1387
Eric Laurente552edb2014-03-10 17:42:56 -07001388 // handle special case for sonification while in call
1389 if (isInCall()) {
1390 handleIncallSonification(stream, false, false);
1391 }
1392
1393 if (outputDesc->mRefCount[stream] > 0) {
1394 // decrement usage count of this stream on the output
1395 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001396
Eric Laurente552edb2014-03-10 17:42:56 -07001397 // store time at which the stream was stopped - see isStreamActive()
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001398 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
Eric Laurente552edb2014-03-10 17:42:56 -07001399 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001400 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001401 // delay the device switch by twice the latency because stopOutput() is executed when
1402 // the track stop() command is received and at that time the audio track buffer can
1403 // still contain data that needs to be drained. The latency only covers the audio HAL
1404 // and kernel buffers. Also the latency does not always include additional delay in the
1405 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001406 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001407
1408 // force restoring the device selection on other active outputs if it differs from the
1409 // one being selected for this output
Eric Laurent57de36c2016-09-28 16:59:11 -07001410 uint32_t delayMs = outputDesc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -07001411 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001412 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001413 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001414 desc->isActive() &&
1415 outputDesc->sharesHwModuleWith(desc) &&
1416 (newDevice != desc->device())) {
Haynes Mathew George11c499a2016-08-26 12:08:25 -07001417 audio_devices_t newDevice2 = getNewOutputDevice(desc, false /*fromCache*/);
1418 bool force = desc->device() != newDevice2;
Eric Laurentc75307b2015-03-17 15:29:32 -07001419 setOutputDevice(desc,
Haynes Mathew George11c499a2016-08-26 12:08:25 -07001420 newDevice2,
1421 force,
Eric Laurent57de36c2016-09-28 16:59:11 -07001422 delayMs);
1423 // re-apply device specific volume if not done by setOutputDevice()
1424 if (!force) {
1425 applyStreamVolumes(desc, newDevice2, delayMs);
1426 }
Eric Laurente552edb2014-03-10 17:42:56 -07001427 }
1428 }
1429 // update the outputs if stopping one with a stream that can affect notification routing
1430 handleNotificationRoutingForStream(stream);
1431 }
Eric Laurent36829f92017-04-07 19:04:42 -07001432 if (stream == AUDIO_STREAM_MUSIC) {
1433 selectOutputForMusicEffects();
1434 }
Eric Laurente552edb2014-03-10 17:42:56 -07001435 return NO_ERROR;
1436 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001437 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001438 return INVALID_OPERATION;
1439 }
1440}
1441
Eric Laurente83b55d2014-11-14 10:06:21 -08001442void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001443 audio_stream_type_t stream __unused,
1444 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001445{
1446 ALOGV("releaseOutput() %d", output);
1447 ssize_t index = mOutputs.indexOfKey(output);
1448 if (index < 0) {
1449 ALOGW("releaseOutput() releasing unknown output %d", output);
1450 return;
1451 }
1452
1453#ifdef AUDIO_POLICY_TEST
1454 int testIndex = testOutputIndex(output);
1455 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001456 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001457 if (outputDesc->isActive()) {
1458 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001459 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001460 mTestOutputs[testIndex] = 0;
1461 }
1462 return;
1463 }
1464#endif //AUDIO_POLICY_TEST
1465
Paul McLeanaa981192015-03-21 09:55:15 -07001466 // Routing
1467 mOutputRoutes.removeRoute(session);
1468
Eric Laurentc75307b2015-03-17 15:29:32 -07001469 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001470 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001471 if (desc->mDirectOpenCount <= 0) {
1472 ALOGW("releaseOutput() invalid open count %d for output %d",
1473 desc->mDirectOpenCount, output);
1474 return;
1475 }
1476 if (--desc->mDirectOpenCount == 0) {
1477 closeOutput(output);
Eric Laurentb52c1522014-05-20 11:27:36 -07001478 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001479 }
1480 }
1481}
1482
1483
Eric Laurentcaf7f482014-11-25 17:50:47 -08001484status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1485 audio_io_handle_t *input,
1486 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001487 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001488 const audio_config_base_t *config,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001489 audio_input_flags_t flags,
Eric Laurent2ac76942017-06-22 17:17:09 -07001490 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001491 input_type_t *inputType,
1492 audio_port_handle_t *portId)
Eric Laurente552edb2014-03-10 17:42:56 -07001493{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001494 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1495 "session %d, flags %#x",
Eric Laurent20b9ef02016-12-05 11:03:16 -08001496 attr->source, config->sample_rate, config->format, config->channel_mask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001497
Eric Laurentcaf7f482014-11-25 17:50:47 -08001498 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001499 *inputType = API_INPUT_INVALID;
Eric Laurentfb66dd92016-01-28 18:32:03 -08001500
Eric Laurent275e8e92014-11-30 15:14:47 -08001501 audio_devices_t device;
1502 // handle legacy remote submix case where the address was not always specified
1503 String8 address = String8("");
Eric Laurentc447ded2015-01-06 08:47:05 -08001504 audio_source_t inputSource = attr->source;
1505 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001506 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001507
Eric Laurentc447ded2015-01-06 08:47:05 -08001508 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1509 inputSource = AUDIO_SOURCE_MIC;
1510 }
1511 halInputSource = inputSource;
1512
Eric Laurent20b9ef02016-12-05 11:03:16 -08001513 // TODO: check for existing client for this port ID
1514 if (*portId == AUDIO_PORT_HANDLE_NONE) {
1515 *portId = AudioPort::getNextUniqueId();
1516 }
1517
Paul McLean466dc8e2015-04-17 13:15:36 -06001518 // Explicit routing?
1519 sp<DeviceDescriptor> deviceDesc;
Eric Laurent2ac76942017-06-22 17:17:09 -07001520 if (*selectedDeviceId != AUDIO_PORT_HANDLE_NONE) {
1521 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1522 if (mAvailableInputDevices[i]->getId() == *selectedDeviceId) {
1523 deviceDesc = mAvailableInputDevices[i];
1524 break;
1525 }
Paul McLean466dc8e2015-04-17 13:15:36 -06001526 }
1527 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001528 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001529
Eric Laurentc447ded2015-01-06 08:47:05 -08001530 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001531 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001532 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
François Gaffie036e1e92015-03-19 10:16:24 +01001533 if (ret != NO_ERROR) {
1534 return ret;
1535 }
1536 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001537 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1538 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001539 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001540 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001541 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001542 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001543 return BAD_VALUE;
1544 }
Eric Laurentc722f302014-12-10 11:21:49 -08001545 if (policyMix != NULL) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001546 address = policyMix->mDeviceAddress;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001547 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1548 // there is an external policy, but this input is attached to a mix of recorders,
1549 // meaning it receives audio injected into the framework, so the recorder doesn't
1550 // know about it and is therefore considered "legacy"
1551 *inputType = API_INPUT_LEGACY;
1552 } else {
1553 // recording a mix of players defined by an external policy, we're rerouting for
1554 // an external policy
1555 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1556 }
Eric Laurentc722f302014-12-10 11:21:49 -08001557 } else if (audio_is_remote_submix_device(device)) {
1558 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001559 *inputType = API_INPUT_MIX_CAPTURE;
Eric Laurent82db2692015-08-07 13:59:42 -07001560 } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
1561 *inputType = API_INPUT_TELEPHONY_RX;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001562 } else {
1563 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001564 }
Ravi Kumar Alamandab367f5b2015-08-25 08:21:37 -07001565
Eric Laurent599c7582015-12-07 18:05:55 -08001566 }
1567
1568 *input = getInputForDevice(device, address, session, uid, inputSource,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001569 config->sample_rate, config->format, config->channel_mask, flags,
Eric Laurent599c7582015-12-07 18:05:55 -08001570 policyMix);
1571 if (*input == AUDIO_IO_HANDLE_NONE) {
1572 mInputRoutes.removeRoute(session);
1573 return INVALID_OPERATION;
1574 }
Eric Laurent20b9ef02016-12-05 11:03:16 -08001575
Eric Laurent2ac76942017-06-22 17:17:09 -07001576 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(device);
1577 *selectedDeviceId = inputDevices.size() > 0 ? inputDevices.itemAt(0)->getId()
1578 : AUDIO_PORT_HANDLE_NONE;
1579
1580 ALOGV("getInputForAttr() returns input %d type %d selectedDeviceId %d",
1581 *input, *inputType, *selectedDeviceId);
1582
Eric Laurent599c7582015-12-07 18:05:55 -08001583 return NO_ERROR;
1584}
1585
1586
1587audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device,
1588 String8 address,
1589 audio_session_t session,
1590 uid_t uid,
1591 audio_source_t inputSource,
1592 uint32_t samplingRate,
1593 audio_format_t format,
1594 audio_channel_mask_t channelMask,
1595 audio_input_flags_t flags,
1596 AudioMix *policyMix)
1597{
1598 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
1599 audio_source_t halInputSource = inputSource;
1600 bool isSoundTrigger = false;
1601
1602 if (inputSource == AUDIO_SOURCE_HOTWORD) {
1603 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1604 if (index >= 0) {
1605 input = mSoundTriggerSessions.valueFor(session);
1606 isSoundTrigger = true;
1607 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1608 ALOGV("SoundTrigger capture on session %d input %d", session, input);
1609 } else {
1610 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001611 }
Haynes Mathew George851d3ff2017-06-19 20:01:57 -07001612 } else if (inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION &&
1613 getPhoneState() == AUDIO_MODE_IN_COMMUNICATION &&
1614 audio_is_linear_pcm(format)) {
1615 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_VOIP_TX);
Eric Laurent5dbe4712014-09-19 19:04:57 -07001616 }
1617
Andy Hungf129b032015-04-07 13:45:50 -07001618 // find a compatible input profile (not necessarily identical in parameters)
1619 sp<IOProfile> profile;
1620 // samplingRate and flags may be updated by getInputProfile
Glenn Kasten05ddca52016-02-11 08:17:12 -08001621 uint32_t profileSamplingRate = (samplingRate == 0) ? SAMPLE_RATE_HZ_DEFAULT : samplingRate;
Andy Hungf129b032015-04-07 13:45:50 -07001622 audio_format_t profileFormat = format;
1623 audio_channel_mask_t profileChannelMask = channelMask;
1624 audio_input_flags_t profileFlags = flags;
1625 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001626 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001627 profileSamplingRate, profileFormat, profileChannelMask,
1628 profileFlags);
1629 if (profile != 0) {
1630 break; // success
Eric Laurent05067782016-06-01 18:27:28 -07001631 } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) {
1632 profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry
Andy Hungf129b032015-04-07 13:45:50 -07001633 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1634 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1635 } else { // fail
Eric Laurent599c7582015-12-07 18:05:55 -08001636 ALOGW("getInputForDevice() could not find profile for device 0x%X,"
1637 "samplingRate %u, format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001638 device, samplingRate, format, channelMask, flags);
Eric Laurent599c7582015-12-07 18:05:55 -08001639 return input;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001640 }
Eric Laurente552edb2014-03-10 17:42:56 -07001641 }
Glenn Kasten05ddca52016-02-11 08:17:12 -08001642 // Pick input sampling rate if not specified by client
1643 if (samplingRate == 0) {
1644 samplingRate = profileSamplingRate;
1645 }
Eric Laurente552edb2014-03-10 17:42:56 -07001646
Eric Laurent322b4d22015-04-03 15:57:54 -07001647 if (profile->getModuleHandle() == 0) {
1648 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurent599c7582015-12-07 18:05:55 -08001649 return input;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001650 }
1651
Eric Laurent599c7582015-12-07 18:05:55 -08001652 sp<AudioSession> audioSession = new AudioSession(session,
1653 inputSource,
1654 format,
1655 samplingRate,
1656 channelMask,
1657 flags,
1658 uid,
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001659 isSoundTrigger,
1660 policyMix, mpClientInterface);
Eric Laurent599c7582015-12-07 18:05:55 -08001661
Eric Laurent74708e72017-04-07 17:13:42 -07001662// FIXME: disable concurrent capture until UI is ready
1663#if 0
Eric Laurent599c7582015-12-07 18:05:55 -08001664 // reuse an open input if possible
Eric Laurent555530a2017-02-07 18:17:24 -08001665 sp<AudioInputDescriptor> reusedInputDesc;
Eric Laurent599c7582015-12-07 18:05:55 -08001666 for (size_t i = 0; i < mInputs.size(); i++) {
1667 sp<AudioInputDescriptor> desc = mInputs.valueAt(i);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001668 // reuse input if:
1669 // - it shares the same profile
1670 // AND
1671 // - it is not a reroute submix input
1672 // AND
1673 // - it is: not used for sound trigger
1674 // OR
1675 // used for sound trigger and all clients use the same session ID
1676 //
1677 if ((profile == desc->mProfile) &&
1678 (isSoundTrigger == desc->isSoundTrigger()) &&
1679 !is_virtual_input_device(device)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001680
1681 sp<AudioSession> as = desc->getAudioSession(session);
1682 if (as != 0) {
1683 // do not allow unmatching properties on same session
1684 if (as->matches(audioSession)) {
1685 as->changeOpenCount(1);
1686 } else {
1687 ALOGW("getInputForDevice() record with different attributes"
1688 " exists for session %d", session);
Eric Laurent555530a2017-02-07 18:17:24 -08001689 continue;
Eric Laurent599c7582015-12-07 18:05:55 -08001690 }
Eric Laurentfb66dd92016-01-28 18:32:03 -08001691 } else if (isSoundTrigger) {
Eric Laurent555530a2017-02-07 18:17:24 -08001692 continue;
Eric Laurentfb66dd92016-01-28 18:32:03 -08001693 }
Eric Laurentbb948092017-01-23 18:33:30 -08001694
Eric Laurent555530a2017-02-07 18:17:24 -08001695 // Reuse the already opened input stream on this profile if:
1696 // - the new capture source is background OR
1697 // - the path requested configurations match OR
1698 // - the new source priority is less than the highest source priority on this input
1699 // If the input stream cannot be reused, close it before opening a new stream
1700 // on the same profile for the new client so that the requested path configuration
1701 // can be selected.
1702 if (!isConcurrentSource(inputSource) &&
Eric Laurentbb948092017-01-23 18:33:30 -08001703 ((desc->mSamplingRate != samplingRate ||
Eric Laurentfb66dd92016-01-28 18:32:03 -08001704 desc->mChannelMask != channelMask ||
Eric Laurente6930022016-02-11 10:20:40 -08001705 !audio_formats_match(desc->mFormat, format)) &&
Eric Laurentfb66dd92016-01-28 18:32:03 -08001706 (source_priority(desc->getHighestPrioritySource(false /*activeOnly*/)) <
Eric Laurentbb948092017-01-23 18:33:30 -08001707 source_priority(inputSource)))) {
Eric Laurent555530a2017-02-07 18:17:24 -08001708 reusedInputDesc = desc;
1709 continue;
Eric Laurent599c7582015-12-07 18:05:55 -08001710 } else {
1711 desc->addAudioSession(session, audioSession);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001712 ALOGV("%s: reusing input %d", __FUNCTION__, mInputs.keyAt(i));
1713 return mInputs.keyAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08001714 }
Eric Laurent599c7582015-12-07 18:05:55 -08001715 }
1716 }
Eric Laurent599c7582015-12-07 18:05:55 -08001717
Eric Laurent555530a2017-02-07 18:17:24 -08001718 if (reusedInputDesc != 0) {
1719 AudioSessionCollection sessions = reusedInputDesc->getAudioSessions(false /*activeOnly*/);
1720 for (size_t j = 0; j < sessions.size(); j++) {
1721 audio_session_t currentSession = sessions.keyAt(j);
1722 stopInput(reusedInputDesc->mIoHandle, currentSession);
1723 releaseInput(reusedInputDesc->mIoHandle, currentSession);
1724 }
1725 }
Eric Laurent74708e72017-04-07 17:13:42 -07001726#endif
Eric Laurent555530a2017-02-07 18:17:24 -08001727
Eric Laurentcf2c0212014-07-25 16:20:43 -07001728 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001729 config.sample_rate = profileSamplingRate;
1730 config.channel_mask = profileChannelMask;
1731 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001732
Eric Laurente3014102017-05-03 11:15:43 -07001733 if (address == "") {
1734 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(device);
1735 // the inputs vector must be of size 1, but we don't want to crash here
1736 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress : String8("");
1737 }
1738
Eric Laurent322b4d22015-04-03 15:57:54 -07001739 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurent599c7582015-12-07 18:05:55 -08001740 &input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001741 &config,
1742 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001743 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001744 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001745 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001746
1747 // only accept input with the exact requested set of parameters
Eric Laurent599c7582015-12-07 18:05:55 -08001748 if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001749 (profileSamplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -08001750 !audio_formats_match(profileFormat, config.format) ||
Andy Hungf129b032015-04-07 13:45:50 -07001751 (profileChannelMask != config.channel_mask)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001752 ALOGW("getInputForAttr() failed opening input: samplingRate %d"
1753 ", format %d, channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001754 samplingRate, format, channelMask);
Eric Laurent599c7582015-12-07 18:05:55 -08001755 if (input != AUDIO_IO_HANDLE_NONE) {
1756 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001757 }
Eric Laurent599c7582015-12-07 18:05:55 -08001758 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001759 }
1760
Eric Laurent1f2f2232014-06-02 12:01:23 -07001761 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Andy Hungf129b032015-04-07 13:45:50 -07001762 inputDesc->mSamplingRate = profileSamplingRate;
1763 inputDesc->mFormat = profileFormat;
1764 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001765 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001766 inputDesc->mPolicyMix = policyMix;
Eric Laurent599c7582015-12-07 18:05:55 -08001767 inputDesc->addAudioSession(session, audioSession);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001768
Eric Laurent599c7582015-12-07 18:05:55 -08001769 addInput(input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001770 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001771
Eric Laurent599c7582015-12-07 18:05:55 -08001772 return input;
Eric Laurente552edb2014-03-10 17:42:56 -07001773}
1774
Eric Laurentbb948092017-01-23 18:33:30 -08001775//static
1776bool AudioPolicyManager::isConcurrentSource(audio_source_t source)
1777{
1778 return (source == AUDIO_SOURCE_HOTWORD) ||
1779 (source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
1780 (source == AUDIO_SOURCE_FM_TUNER);
1781}
1782
Eric Laurentfb66dd92016-01-28 18:32:03 -08001783bool AudioPolicyManager::isConcurentCaptureAllowed(const sp<AudioInputDescriptor>& inputDesc,
1784 const sp<AudioSession>& audioSession)
1785{
1786 // Do not allow capture if an active voice call is using a software patch and
1787 // the call TX source device is on the same HW module.
1788 // FIXME: would be better to refine to only inputs whose profile connects to the
1789 // call TX device but this information is not in the audio patch
1790 if (mCallTxPatch != 0 &&
1791 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1792 return false;
1793 }
1794
1795 // starting concurrent capture is enabled if:
1796 // 1) capturing for re-routing
1797 // 2) capturing for HOTWORD source
1798 // 3) capturing for FM TUNER source
1799 // 3) All other active captures are either for re-routing or HOTWORD
1800
1801 if (is_virtual_input_device(inputDesc->mDevice) ||
Eric Laurentbb948092017-01-23 18:33:30 -08001802 isConcurrentSource(audioSession->inputSource())) {
Eric Laurentfb66dd92016-01-28 18:32:03 -08001803 return true;
1804 }
1805
1806 Vector< sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
1807 for (size_t i = 0; i < activeInputs.size(); i++) {
1808 sp<AudioInputDescriptor> activeInput = activeInputs[i];
Eric Laurentbb948092017-01-23 18:33:30 -08001809 if (!isConcurrentSource(activeInput->inputSource(true)) &&
Eric Laurentfb66dd92016-01-28 18:32:03 -08001810 !is_virtual_input_device(activeInput->mDevice)) {
1811 return false;
1812 }
1813 }
1814
1815 return true;
1816}
1817
1818
Eric Laurent4dc68062014-07-28 17:26:49 -07001819status_t AudioPolicyManager::startInput(audio_io_handle_t input,
Eric Laurentfb66dd92016-01-28 18:32:03 -08001820 audio_session_t session,
1821 concurrency_type__mask_t *concurrency)
Eric Laurente552edb2014-03-10 17:42:56 -07001822{
1823 ALOGV("startInput() input %d", input);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001824 *concurrency = API_INPUT_CONCURRENCY_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001825 ssize_t index = mInputs.indexOfKey(input);
1826 if (index < 0) {
1827 ALOGW("startInput() unknown input %d", input);
1828 return BAD_VALUE;
1829 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001830 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001831
Eric Laurent599c7582015-12-07 18:05:55 -08001832 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1833 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07001834 ALOGW("startInput() unknown session %d on input %d", session, input);
1835 return BAD_VALUE;
1836 }
1837
Eric Laurent74708e72017-04-07 17:13:42 -07001838// FIXME: disable concurrent capture until UI is ready
1839#if 0
Eric Laurentfb66dd92016-01-28 18:32:03 -08001840 if (!isConcurentCaptureAllowed(inputDesc, audioSession)) {
1841 ALOGW("startInput(%d) failed: other input already started", input);
1842 return INVALID_OPERATION;
1843 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001844
Eric Laurentfb66dd92016-01-28 18:32:03 -08001845 if (isInCall()) {
1846 *concurrency |= API_INPUT_CONCURRENCY_CALL;
1847 }
Eric Laurentf7c50102016-09-30 15:19:43 -07001848 if (mInputs.activeInputsCountOnDevices() != 0) {
Eric Laurentfb66dd92016-01-28 18:32:03 -08001849 *concurrency |= API_INPUT_CONCURRENCY_CAPTURE;
Eric Laurente552edb2014-03-10 17:42:56 -07001850 }
Eric Laurent74708e72017-04-07 17:13:42 -07001851#else
1852 if (!is_virtual_input_device(inputDesc->mDevice)) {
1853 if (mCallTxPatch != 0 &&
1854 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1855 ALOGW("startInput(%d) failed: call in progress", input);
1856 return INVALID_OPERATION;
1857 }
1858
1859 Vector< sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
1860 for (size_t i = 0; i < activeInputs.size(); i++) {
1861 sp<AudioInputDescriptor> activeDesc = activeInputs[i];
1862
1863 if (is_virtual_input_device(activeDesc->mDevice)) {
1864 continue;
1865 }
1866
1867 audio_source_t activeSource = activeDesc->inputSource(true);
1868 if (audioSession->inputSource() == AUDIO_SOURCE_HOTWORD) {
1869 if (activeSource == AUDIO_SOURCE_HOTWORD) {
1870 if (activeDesc->hasPreemptedSession(session)) {
1871 ALOGW("startInput(%d) failed for HOTWORD: "
1872 "other input %d already started for HOTWORD",
1873 input, activeDesc->mIoHandle);
1874 return INVALID_OPERATION;
1875 }
1876 } else {
1877 ALOGV("startInput(%d) failed for HOTWORD: other input %d already started",
1878 input, activeDesc->mIoHandle);
1879 return INVALID_OPERATION;
1880 }
1881 } else {
1882 if (activeSource != AUDIO_SOURCE_HOTWORD) {
1883 ALOGW("startInput(%d) failed: other input %d already started",
1884 input, activeDesc->mIoHandle);
1885 return INVALID_OPERATION;
1886 }
1887 }
1888 }
1889
1890 // if capture is allowed, preempt currently active HOTWORD captures
1891 for (size_t i = 0; i < activeInputs.size(); i++) {
1892 sp<AudioInputDescriptor> activeDesc = activeInputs[i];
1893
1894 if (is_virtual_input_device(activeDesc->mDevice)) {
1895 continue;
1896 }
1897
1898 audio_source_t activeSource = activeDesc->inputSource(true);
1899 if (activeSource == AUDIO_SOURCE_HOTWORD) {
1900 AudioSessionCollection activeSessions =
1901 activeDesc->getAudioSessions(true /*activeOnly*/);
1902 audio_session_t activeSession = activeSessions.keyAt(0);
1903 audio_io_handle_t activeHandle = activeDesc->mIoHandle;
1904 SortedVector<audio_session_t> sessions = activeDesc->getPreemptedSessions();
1905 sessions.add(activeSession);
1906 inputDesc->setPreemptedSessions(sessions);
1907 stopInput(activeHandle, activeSession);
1908 releaseInput(activeHandle, activeSession);
1909 ALOGV("startInput(%d) for HOTWORD preempting HOTWORD input %d",
1910 input, activeDesc->mIoHandle);
1911 }
1912 }
1913 }
1914#endif
Eric Laurente552edb2014-03-10 17:42:56 -07001915
Eric Laurent313d1e72016-01-29 09:56:57 -08001916 // increment activity count before calling getNewInputDevice() below as only active sessions
1917 // are considered for device selection
1918 audioSession->changeActiveCount(1);
1919
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001920 // Routing?
1921 mInputRoutes.incRouteActivity(session);
1922
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001923 if (audioSession->activeCount() == 1 || mInputRoutes.hasRouteChanged(session)) {
Eric Laurent271a93e2016-09-29 14:47:06 -07001924 // indicate active capture to sound trigger service if starting capture from a mic on
1925 // primary HW module
Eric Laurentf7c50102016-09-30 15:19:43 -07001926 audio_devices_t device = getNewInputDevice(inputDesc);
Eric Laurent271a93e2016-09-29 14:47:06 -07001927 setInputDevice(input, device, true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001928
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001929 if (inputDesc->getAudioSessionCount(true/*activeOnly*/) == 1) {
1930 // if input maps to a dynamic policy with an activity listener, notify of state change
1931 if ((inputDesc->mPolicyMix != NULL)
1932 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1933 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
1934 MIX_STATE_MIXING);
Eric Laurentc722f302014-12-10 11:21:49 -08001935 }
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001936
Eric Laurentf7c50102016-09-30 15:19:43 -07001937 audio_devices_t primaryInputDevices = availablePrimaryInputDevices();
1938 if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) &&
Eric Laurent05b345e2016-11-18 12:36:27 -08001939 mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) {
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001940 SoundTrigger::setCaptureState(true);
1941 }
1942
1943 // automatically enable the remote submix output when input is started if not
1944 // used by a policy mix of type MIX_TYPE_RECORDERS
1945 // For remote submix (a virtual device), we open only one input per capture request.
1946 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1947 String8 address = String8("");
1948 if (inputDesc->mPolicyMix == NULL) {
1949 address = String8("0");
1950 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1951 address = inputDesc->mPolicyMix->mDeviceAddress;
1952 }
1953 if (address != "") {
1954 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1955 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1956 address, "remote-submix");
1957 }
Eric Laurentc722f302014-12-10 11:21:49 -08001958 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001959 }
Eric Laurente552edb2014-03-10 17:42:56 -07001960 }
1961
Eric Laurent599c7582015-12-07 18:05:55 -08001962 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Eric Laurente552edb2014-03-10 17:42:56 -07001963
Eric Laurente552edb2014-03-10 17:42:56 -07001964 return NO_ERROR;
1965}
1966
Eric Laurent4dc68062014-07-28 17:26:49 -07001967status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1968 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001969{
1970 ALOGV("stopInput() input %d", input);
1971 ssize_t index = mInputs.indexOfKey(input);
1972 if (index < 0) {
1973 ALOGW("stopInput() unknown input %d", input);
1974 return BAD_VALUE;
1975 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001976 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001977
Eric Laurent599c7582015-12-07 18:05:55 -08001978 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001979 if (index < 0) {
1980 ALOGW("stopInput() unknown session %d on input %d", session, input);
1981 return BAD_VALUE;
1982 }
1983
Eric Laurent599c7582015-12-07 18:05:55 -08001984 if (audioSession->activeCount() == 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07001985 ALOGW("stopInput() input %d already stopped", input);
1986 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001987 }
1988
Eric Laurent599c7582015-12-07 18:05:55 -08001989 audioSession->changeActiveCount(-1);
Paul McLean466dc8e2015-04-17 13:15:36 -06001990
1991 // Routing?
1992 mInputRoutes.decRouteActivity(session);
1993
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001994 if (audioSession->activeCount() == 0) {
Eric Laurent84332aa2016-01-28 22:19:18 +00001995
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001996 if (inputDesc->isActive()) {
1997 setInputDevice(input, getNewInputDevice(inputDesc), false /* force */);
1998 } else {
1999 // if input maps to a dynamic policy with an activity listener, notify of state change
2000 if ((inputDesc->mPolicyMix != NULL)
2001 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
2002 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
2003 MIX_STATE_IDLE);
Eric Laurent84332aa2016-01-28 22:19:18 +00002004 }
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07002005
2006 // automatically disable the remote submix output when input is stopped if not
2007 // used by a policy mix of type MIX_TYPE_RECORDERS
2008 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
2009 String8 address = String8("");
2010 if (inputDesc->mPolicyMix == NULL) {
2011 address = String8("0");
2012 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
2013 address = inputDesc->mPolicyMix->mDeviceAddress;
2014 }
2015 if (address != "") {
2016 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2017 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2018 address, "remote-submix");
2019 }
Eric Laurent84332aa2016-01-28 22:19:18 +00002020 }
Eric Laurent84332aa2016-01-28 22:19:18 +00002021
Eric Laurentf7c50102016-09-30 15:19:43 -07002022 audio_devices_t device = inputDesc->mDevice;
Eric Laurentfb66dd92016-01-28 18:32:03 -08002023 resetInputDevice(input);
Eric Laurent84332aa2016-01-28 22:19:18 +00002024
Eric Laurentf7c50102016-09-30 15:19:43 -07002025 // indicate inactive capture to sound trigger service if stopping capture from a mic on
2026 // primary HW module
2027 audio_devices_t primaryInputDevices = availablePrimaryInputDevices();
2028 if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) &&
2029 mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) {
Eric Laurentfb66dd92016-01-28 18:32:03 -08002030 SoundTrigger::setCaptureState(false);
2031 }
2032 inputDesc->clearPreemptedSessions();
Eric Laurent84332aa2016-01-28 22:19:18 +00002033 }
Eric Laurente552edb2014-03-10 17:42:56 -07002034 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002035 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07002036}
2037
Eric Laurent4dc68062014-07-28 17:26:49 -07002038void AudioPolicyManager::releaseInput(audio_io_handle_t input,
2039 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07002040{
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002041
Eric Laurente552edb2014-03-10 17:42:56 -07002042 ALOGV("releaseInput() %d", input);
2043 ssize_t index = mInputs.indexOfKey(input);
2044 if (index < 0) {
2045 ALOGW("releaseInput() releasing unknown input %d", input);
2046 return;
2047 }
Paul McLean466dc8e2015-04-17 13:15:36 -06002048
2049 // Routing
2050 mInputRoutes.removeRoute(session);
2051
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002052 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
2053 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07002054
Eric Laurent599c7582015-12-07 18:05:55 -08002055 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
vivek mehta587b8df2017-01-31 14:58:44 -08002056 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07002057 ALOGW("releaseInput() unknown session %d on input %d", session, input);
2058 return;
2059 }
Eric Laurent599c7582015-12-07 18:05:55 -08002060
2061 if (audioSession->openCount() == 0) {
2062 ALOGW("releaseInput() invalid open count %d on session %d",
2063 audioSession->openCount(), session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002064 return;
2065 }
Eric Laurent599c7582015-12-07 18:05:55 -08002066
2067 if (audioSession->changeOpenCount(-1) == 0) {
2068 inputDesc->removeAudioSession(session);
2069 }
2070
Jean-Michel Trivi65bfe912015-12-04 15:56:47 -08002071 if (inputDesc->getOpenRefCount() > 0) {
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002072 ALOGV("releaseInput() exit > 0");
2073 return;
2074 }
2075
Eric Laurent05b90f82014-08-27 15:32:29 -07002076 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07002077 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07002078 ALOGV("releaseInput() exit");
2079}
2080
Eric Laurentd4692962014-05-05 18:13:44 -07002081void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07002082 bool patchRemoved = false;
2083
Eric Laurentd4692962014-05-05 18:13:44 -07002084 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07002085 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08002086 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07002087 if (patch_index >= 0) {
2088 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07002089 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07002090 mAudioPatches.removeItemsAt(patch_index);
2091 patchRemoved = true;
2092 }
Eric Laurentd4692962014-05-05 18:13:44 -07002093 mpClientInterface->closeInput(mInputs.keyAt(input_index));
2094 }
2095 mInputs.clear();
Chris Thornton52785f32016-02-09 17:28:49 -08002096 SoundTrigger::setCaptureState(false);
Eric Laurent6a94d692014-05-20 11:18:06 -07002097 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07002098
2099 if (patchRemoved) {
2100 mpClientInterface->onAudioPatchListUpdate();
2101 }
Eric Laurentd4692962014-05-05 18:13:44 -07002102}
2103
Eric Laurente0720872014-03-11 09:30:41 -07002104void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07002105 int indexMin,
2106 int indexMax)
2107{
2108 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002109 mVolumeCurves->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent28d09f02016-03-08 10:43:05 -08002110
2111 // initialize other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08002112 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
2113 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002114 continue;
2115 }
2116 mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08002117 }
Eric Laurente552edb2014-03-10 17:42:56 -07002118}
2119
Eric Laurente0720872014-03-11 09:30:41 -07002120status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01002121 int index,
2122 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07002123{
2124
François Gaffied1ab2bd2015-12-02 18:20:06 +01002125 if ((index < mVolumeCurves->getVolumeIndexMin(stream)) ||
2126 (index > mVolumeCurves->getVolumeIndexMax(stream))) {
Eric Laurente552edb2014-03-10 17:42:56 -07002127 return BAD_VALUE;
2128 }
2129 if (!audio_is_output_device(device)) {
2130 return BAD_VALUE;
2131 }
2132
2133 // Force max volume if stream cannot be muted
François Gaffied1ab2bd2015-12-02 18:20:06 +01002134 if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07002135
Eric Laurent1fd372e2016-04-06 14:23:53 -07002136 ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d",
Eric Laurente552edb2014-03-10 17:42:56 -07002137 stream, device, index);
2138
Eric Laurent28d09f02016-03-08 10:43:05 -08002139 // update other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08002140 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
2141 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002142 continue;
2143 }
2144 mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index);
2145 }
Eric Laurente552edb2014-03-10 17:42:56 -07002146
Eric Laurent1fd372e2016-04-06 14:23:53 -07002147 // update volume on all outputs and streams matching the following:
2148 // - The requested stream (or a stream matching for volume control) is active on the output
2149 // - The device (or devices) selected by the strategy corresponding to this stream includes
2150 // the requested device
2151 // - For non default requested device, currently selected device on the output is either the
2152 // requested device or one of the devices selected by the strategy
Eric Laurent5a2b6292016-04-14 18:05:57 -07002153 // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if
2154 // no specific device volume value exists for currently selected device.
Eric Laurente552edb2014-03-10 17:42:56 -07002155 status_t status = NO_ERROR;
2156 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002157 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
2158 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent794fde22016-03-11 09:50:45 -08002159 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
2160 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002161 continue;
Eric Laurente552edb2014-03-10 17:42:56 -07002162 }
Eric Laurentd3926fe2016-04-15 18:10:07 -07002163 if (!(desc->isStreamActive((audio_stream_type_t)curStream) ||
2164 (isInCall() && (curStream == AUDIO_STREAM_VOICE_CALL)))) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07002165 continue;
2166 }
Eric Laurent794fde22016-03-11 09:50:45 -08002167 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Jean-Michel Trivib7fdce62017-06-27 19:38:42 -07002168 audio_devices_t curStreamDevice = Volume::getDeviceForVolume(getDeviceForStrategy(
2169 curStrategy, false /*fromCache*/));
Eric Laurente76f29c2016-09-14 17:17:53 -07002170 if ((device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) &&
2171 ((curStreamDevice & device) == 0)) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07002172 continue;
2173 }
Eric Laurente76f29c2016-09-14 17:17:53 -07002174 bool applyVolume;
Eric Laurent5a2b6292016-04-14 18:05:57 -07002175 if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07002176 curStreamDevice |= device;
Eric Laurente76f29c2016-09-14 17:17:53 -07002177 applyVolume = (curDevice & curStreamDevice) != 0;
2178 } else {
2179 applyVolume = !mVolumeCurves->hasVolumeIndexForDevice(
Jean-Michel Trivib7fdce62017-06-27 19:38:42 -07002180 stream, curStreamDevice);
Eric Laurent1fd372e2016-04-06 14:23:53 -07002181 }
Eric Laurent28d09f02016-03-08 10:43:05 -08002182
Eric Laurente76f29c2016-09-14 17:17:53 -07002183 if (applyVolume) {
Eric Laurentdc462862016-07-19 12:29:53 -07002184 //FIXME: workaround for truncated touch sounds
2185 // delayed volume change for system stream to be removed when the problem is
2186 // handled by system UI
Eric Laurent28d09f02016-03-08 10:43:05 -08002187 status_t volStatus =
Eric Laurentdc462862016-07-19 12:29:53 -07002188 checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice,
2189 (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0);
Eric Laurent28d09f02016-03-08 10:43:05 -08002190 if (volStatus != NO_ERROR) {
2191 status = volStatus;
2192 }
2193 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08002194 }
Eric Laurente552edb2014-03-10 17:42:56 -07002195 }
2196 return status;
2197}
2198
Eric Laurente0720872014-03-11 09:30:41 -07002199status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07002200 int *index,
2201 audio_devices_t device)
2202{
2203 if (index == NULL) {
2204 return BAD_VALUE;
2205 }
2206 if (!audio_is_output_device(device)) {
2207 return BAD_VALUE;
2208 }
Eric Laurent5a2b6292016-04-14 18:05:57 -07002209 // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device corresponding to
Eric Laurente552edb2014-03-10 17:42:56 -07002210 // the strategy the stream belongs to.
Eric Laurent5a2b6292016-04-14 18:05:57 -07002211 if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurente552edb2014-03-10 17:42:56 -07002212 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
2213 }
François Gaffiedfd74092015-03-19 12:10:59 +01002214 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07002215
François Gaffied1ab2bd2015-12-02 18:20:06 +01002216 *index = mVolumeCurves->getVolumeIndex(stream, device);
Eric Laurente552edb2014-03-10 17:42:56 -07002217 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
2218 return NO_ERROR;
2219}
2220
Eric Laurent36829f92017-04-07 19:04:42 -07002221audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects()
Eric Laurente552edb2014-03-10 17:42:56 -07002222{
2223 // select one output among several suitable for global effects.
2224 // The priority is as follows:
2225 // 1: An offloaded output. If the effect ends up not being offloadable,
2226 // AudioFlinger will invalidate the track and the offloaded output
2227 // will be closed causing the effect to be moved to a PCM output.
2228 // 2: A deep buffer output
Eric Laurent36829f92017-04-07 19:04:42 -07002229 // 3: The primary output
2230 // 4: the first output in the list
Eric Laurente552edb2014-03-10 17:42:56 -07002231
Eric Laurent3b73df72014-03-11 09:06:29 -07002232 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07002233 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent36829f92017-04-07 19:04:42 -07002234 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
Eric Laurente552edb2014-03-10 17:42:56 -07002235
Eric Laurent36829f92017-04-07 19:04:42 -07002236 if (outputs.size() == 0) {
2237 return AUDIO_IO_HANDLE_NONE;
2238 }
Eric Laurente552edb2014-03-10 17:42:56 -07002239
Eric Laurent36829f92017-04-07 19:04:42 -07002240 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
2241 bool activeOnly = true;
2242
2243 while (output == AUDIO_IO_HANDLE_NONE) {
2244 audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE;
2245 audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE;
2246 audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE;
2247
2248 for (size_t i = 0; i < outputs.size(); i++) {
2249 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
2250 if (activeOnly && !desc->isStreamActive(AUDIO_STREAM_MUSIC)) {
2251 continue;
2252 }
2253 ALOGV("selectOutputForMusicEffects activeOnly %d outputs[%zu] flags 0x%08x",
2254 activeOnly, i, desc->mFlags);
2255 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
2256 outputOffloaded = outputs[i];
2257 }
2258 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
2259 outputDeepBuffer = outputs[i];
2260 }
2261 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) {
2262 outputPrimary = outputs[i];
2263 }
2264 }
2265 if (outputOffloaded != AUDIO_IO_HANDLE_NONE) {
2266 output = outputOffloaded;
2267 } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) {
2268 output = outputDeepBuffer;
2269 } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) {
2270 output = outputPrimary;
2271 } else {
2272 output = outputs[0];
2273 }
2274 activeOnly = false;
2275 }
2276
2277 if (output != mMusicEffectOutput) {
2278 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output);
2279 mMusicEffectOutput = output;
2280 }
2281
2282 ALOGV("selectOutputForMusicEffects selected output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -07002283 return output;
2284}
2285
Eric Laurent36829f92017-04-07 19:04:42 -07002286audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused)
2287{
2288 return selectOutputForMusicEffects();
2289}
2290
Eric Laurente0720872014-03-11 09:30:41 -07002291status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07002292 audio_io_handle_t io,
2293 uint32_t strategy,
2294 int session,
2295 int id)
2296{
2297 ssize_t index = mOutputs.indexOfKey(io);
2298 if (index < 0) {
2299 index = mInputs.indexOfKey(io);
2300 if (index < 0) {
2301 ALOGW("registerEffect() unknown io %d", io);
2302 return INVALID_OPERATION;
2303 }
2304 }
François Gaffie45ed3b02015-03-19 10:35:14 +01002305 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07002306}
2307
Eric Laurentc75307b2015-03-17 15:29:32 -07002308bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
2309{
Eric Laurent28d09f02016-03-08 10:43:05 -08002310 bool active = false;
Eric Laurent794fde22016-03-11 09:50:45 -08002311 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) {
2312 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002313 continue;
2314 }
2315 active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs);
2316 }
Eric Laurent28d09f02016-03-08 10:43:05 -08002317 return active;
Eric Laurentc75307b2015-03-17 15:29:32 -07002318}
2319
2320bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
2321{
2322 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
2323}
2324
Eric Laurente0720872014-03-11 09:30:41 -07002325bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07002326{
2327 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002328 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002329 if (inputDescriptor->isSourceActive(source)) {
Eric Laurente552edb2014-03-10 17:42:56 -07002330 return true;
2331 }
2332 }
2333 return false;
2334}
2335
Eric Laurent275e8e92014-11-30 15:14:47 -08002336// Register a list of custom mixes with their attributes and format.
2337// When a mix is registered, corresponding input and output profiles are
2338// added to the remote submix hw module. The profile contains only the
2339// parameters (sampling rate, format...) specified by the mix.
2340// The corresponding input remote submix device is also connected.
2341//
2342// When a remote submix device is connected, the address is checked to select the
2343// appropriate profile and the corresponding input or output stream is opened.
2344//
2345// When capture starts, getInputForAttr() will:
2346// - 1 look for a mix matching the address passed in attribtutes tags if any
2347// - 2 if none found, getDeviceForInputSource() will:
2348// - 2.1 look for a mix matching the attributes source
2349// - 2.2 if none found, default to device selection by policy rules
2350// At this time, the corresponding output remote submix device is also connected
2351// and active playback use cases can be transferred to this mix if needed when reconnecting
2352// after AudioTracks are invalidated
2353//
2354// When playback starts, getOutputForAttr() will:
2355// - 1 look for a mix matching the address passed in attribtutes tags if any
2356// - 2 if none found, look for a mix matching the attributes usage
2357// - 3 if none found, default to device and output selection by policy rules.
2358
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07002359status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes)
Eric Laurent275e8e92014-11-30 15:14:47 -08002360{
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002361 ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size());
2362 status_t res = NO_ERROR;
2363
2364 sp<HwModule> rSubmixModule;
2365 // examine each mix's route type
2366 for (size_t i = 0; i < mixes.size(); i++) {
2367 // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination
2368 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) {
2369 res = INVALID_OPERATION;
Eric Laurent275e8e92014-11-30 15:14:47 -08002370 break;
2371 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002372 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
2373 // Loop back through "remote submix"
2374 if (rSubmixModule == 0) {
2375 for (size_t j = 0; i < mHwModules.size(); j++) {
2376 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2377 && mHwModules[j]->mHandle != 0) {
2378 rSubmixModule = mHwModules[j];
2379 break;
2380 }
2381 }
2382 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002383
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002384 ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size());
Eric Laurent275e8e92014-11-30 15:14:47 -08002385
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002386 if (rSubmixModule == 0) {
2387 ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", i);
2388 res = INVALID_OPERATION;
2389 break;
2390 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002391
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002392 String8 address = mixes[i].mDeviceAddress;
François Gaffie036e1e92015-03-19 10:16:24 +01002393
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002394 if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) {
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002395 ALOGE(" Error registering mix %zu for address %s", i, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002396 res = INVALID_OPERATION;
2397 break;
2398 }
2399 audio_config_t outputConfig = mixes[i].mFormat;
2400 audio_config_t inputConfig = mixes[i].mFormat;
2401 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
2402 // stereo and let audio flinger do the channel conversion if needed.
2403 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2404 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
2405 rSubmixModule->addOutputProfile(address, &outputConfig,
2406 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
2407 rSubmixModule->addInputProfile(address, &inputConfig,
2408 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01002409
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002410 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
2411 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2412 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2413 address.string(), "remote-submix");
2414 } else {
2415 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2416 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2417 address.string(), "remote-submix");
2418 }
2419 } else if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002420 String8 address = mixes[i].mDeviceAddress;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002421 audio_devices_t device = mixes[i].mDeviceType;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002422 ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s",
2423 i, mixes.size(), device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002424
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002425 bool foundOutput = false;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002426 for (size_t j = 0 ; j < mOutputs.size() ; j++) {
2427 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j);
2428 sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle());
2429 if ((patch != 0) && (patch->mPatch.num_sinks != 0)
2430 && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE)
2431 && (patch->mPatch.sinks[0].ext.device.type == device)
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002432 && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(),
2433 AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002434 if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) {
2435 res = INVALID_OPERATION;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002436 } else {
2437 foundOutput = true;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002438 }
2439 break;
2440 }
2441 }
2442
2443 if (res != NO_ERROR) {
2444 ALOGE(" Error registering mix %zu for device 0x%X addr %s",
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002445 i, device, address.string());
2446 res = INVALID_OPERATION;
2447 break;
2448 } else if (!foundOutput) {
2449 ALOGE(" Output not found for mix %zu for device 0x%X addr %s",
2450 i, device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002451 res = INVALID_OPERATION;
2452 break;
2453 }
Eric Laurentc722f302014-12-10 11:21:49 -08002454 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002455 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002456 if (res != NO_ERROR) {
2457 unregisterPolicyMixes(mixes);
2458 }
2459 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002460}
2461
2462status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
2463{
Eric Laurent7b279bb2015-12-14 10:18:23 -08002464 ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002465 status_t res = NO_ERROR;
2466 sp<HwModule> rSubmixModule;
2467 // examine each mix's route type
Eric Laurent275e8e92014-11-30 15:14:47 -08002468 for (size_t i = 0; i < mixes.size(); i++) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002469 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
François Gaffie036e1e92015-03-19 10:16:24 +01002470
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002471 if (rSubmixModule == 0) {
2472 for (size_t j = 0; i < mHwModules.size(); j++) {
2473 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2474 && mHwModules[j]->mHandle != 0) {
2475 rSubmixModule = mHwModules[j];
2476 break;
2477 }
2478 }
2479 }
2480 if (rSubmixModule == 0) {
2481 res = INVALID_OPERATION;
2482 continue;
2483 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002484
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002485 String8 address = mixes[i].mDeviceAddress;
Eric Laurent275e8e92014-11-30 15:14:47 -08002486
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002487 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
2488 res = INVALID_OPERATION;
2489 continue;
2490 }
2491
2492 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
2493 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2494 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2495 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2496 address.string(), "remote-submix");
2497 }
2498 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
2499 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2500 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2501 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2502 address.string(), "remote-submix");
2503 }
2504 rSubmixModule->removeOutputProfile(address);
2505 rSubmixModule->removeInputProfile(address);
2506
2507 } if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
2508 if (mPolicyMixes.unregisterMix(mixes[i].mDeviceAddress) != NO_ERROR) {
2509 res = INVALID_OPERATION;
2510 continue;
2511 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002512 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002513 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002514 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002515}
2516
Eric Laurente552edb2014-03-10 17:42:56 -07002517
Eric Laurente0720872014-03-11 09:30:41 -07002518status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07002519{
2520 const size_t SIZE = 256;
2521 char buffer[SIZE];
2522 String8 result;
2523
2524 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
2525 result.append(buffer);
2526
Eric Laurent87ffa392015-05-22 10:32:38 -07002527 snprintf(buffer, SIZE, " Primary Output: %d\n",
2528 hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE);
Eric Laurente552edb2014-03-10 17:42:56 -07002529 result.append(buffer);
Mikhail Naganov0d6a0332016-04-19 17:12:38 -07002530 std::string stateLiteral;
2531 AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral);
2532 snprintf(buffer, SIZE, " Phone state: %s\n", stateLiteral.c_str());
Eric Laurente552edb2014-03-10 17:42:56 -07002533 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002534 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002535 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07002536 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002537 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07002538 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002539 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07002540 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002541 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07002542 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002543 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07002544 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002545 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002546 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002547 result.append(buffer);
Phil Burk09bc4612016-02-24 15:58:15 -08002548 snprintf(buffer, SIZE, " Force use for encoded surround output %d\n",
2549 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND));
2550 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002551 snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available");
2552 result.append(buffer);
Andy Hung2ddee192015-12-18 17:34:44 -08002553 snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off");
2554 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002555
François Gaffie2110e042015-03-24 08:41:51 +01002556 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07002557
François Gaffie112b0af2015-11-19 16:13:25 +01002558 mAvailableOutputDevices.dump(fd, String8("Available output"));
2559 mAvailableInputDevices.dump(fd, String8("Available input"));
François Gaffie53615e22015-03-19 09:24:12 +01002560 mHwModules.dump(fd);
2561 mOutputs.dump(fd);
2562 mInputs.dump(fd);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002563 mVolumeCurves->dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01002564 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002565 mAudioPatches.dump(fd);
Mikhail Naganov44344b02016-12-13 11:21:02 -08002566 mPolicyMixes.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07002567
2568 return NO_ERROR;
2569}
2570
2571// This function checks for the parameters which can be offloaded.
2572// This can be enhanced depending on the capability of the DSP and policy
2573// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002574bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002575{
2576 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002577 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002578 offloadInfo.sample_rate, offloadInfo.channel_mask,
2579 offloadInfo.format,
2580 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2581 offloadInfo.has_video);
2582
Andy Hung2ddee192015-12-18 17:34:44 -08002583 if (mMasterMono) {
2584 return false; // no offloading if mono is set.
2585 }
2586
Eric Laurente552edb2014-03-10 17:42:56 -07002587 // Check if offload has been disabled
2588 char propValue[PROPERTY_VALUE_MAX];
2589 if (property_get("audio.offload.disable", propValue, "0")) {
2590 if (atoi(propValue) != 0) {
2591 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2592 return false;
2593 }
2594 }
2595
2596 // Check if stream type is music, then only allow offload as of now.
2597 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2598 {
2599 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2600 return false;
2601 }
2602
2603 //TODO: enable audio offloading with video when ready
Andy Hung08945c42015-05-31 21:36:46 -07002604 const bool allowOffloadWithVideo =
2605 property_get_bool("audio.offload.video", false /* default_value */);
2606 if (offloadInfo.has_video && !allowOffloadWithVideo) {
Eric Laurente552edb2014-03-10 17:42:56 -07002607 ALOGV("isOffloadSupported: has_video == true, returning false");
2608 return false;
2609 }
2610
2611 //If duration is less than minimum value defined in property, return false
2612 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2613 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2614 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2615 return false;
2616 }
2617 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2618 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2619 return false;
2620 }
2621
2622 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2623 // creating an offloaded track and tearing it down immediately after start when audioflinger
2624 // detects there is an active non offloadable effect.
2625 // FIXME: We should check the audio session here but we do not have it in this context.
2626 // This may prevent offloading in rare situations where effects are left active by apps
2627 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002628 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002629 return false;
2630 }
2631
2632 // See if there is a profile to support this.
2633 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002634 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002635 offloadInfo.sample_rate,
2636 offloadInfo.format,
2637 offloadInfo.channel_mask,
2638 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002639 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2640 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002641}
2642
Eric Laurent6a94d692014-05-20 11:18:06 -07002643status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2644 audio_port_type_t type,
2645 unsigned int *num_ports,
2646 struct audio_port *ports,
2647 unsigned int *generation)
2648{
2649 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2650 generation == NULL) {
2651 return BAD_VALUE;
2652 }
2653 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2654 if (ports == NULL) {
2655 *num_ports = 0;
2656 }
2657
2658 size_t portsWritten = 0;
2659 size_t portsMax = *num_ports;
2660 *num_ports = 0;
2661 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002662 // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB
2663 // as they are used by stub HALs by convention
Eric Laurent6a94d692014-05-20 11:18:06 -07002664 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002665 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
2666 if (mAvailableOutputDevices[i]->type() == AUDIO_DEVICE_OUT_STUB) {
2667 continue;
2668 }
2669 if (portsWritten < portsMax) {
2670 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2671 }
2672 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002673 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002674 }
2675 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002676 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
2677 if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_STUB) {
2678 continue;
2679 }
2680 if (portsWritten < portsMax) {
2681 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2682 }
2683 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002684 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002685 }
2686 }
2687 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2688 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2689 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2690 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2691 }
2692 *num_ports += mInputs.size();
2693 }
2694 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002695 size_t numOutputs = 0;
2696 for (size_t i = 0; i < mOutputs.size(); i++) {
2697 if (!mOutputs[i]->isDuplicated()) {
2698 numOutputs++;
2699 if (portsWritten < portsMax) {
2700 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2701 }
2702 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002703 }
Eric Laurent84c70242014-06-23 08:46:27 -07002704 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002705 }
2706 }
2707 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002708 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002709 return NO_ERROR;
2710}
2711
2712status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2713{
2714 return NO_ERROR;
2715}
2716
Eric Laurent6a94d692014-05-20 11:18:06 -07002717status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2718 audio_patch_handle_t *handle,
2719 uid_t uid)
2720{
2721 ALOGV("createAudioPatch()");
2722
2723 if (handle == NULL || patch == NULL) {
2724 return BAD_VALUE;
2725 }
2726 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2727
Eric Laurent874c42872014-08-08 15:13:39 -07002728 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2729 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2730 return BAD_VALUE;
2731 }
2732 // only one source per audio patch supported for now
2733 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002734 return INVALID_OPERATION;
2735 }
Eric Laurent874c42872014-08-08 15:13:39 -07002736
2737 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002738 return INVALID_OPERATION;
2739 }
Eric Laurent874c42872014-08-08 15:13:39 -07002740 for (size_t i = 0; i < patch->num_sinks; i++) {
2741 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2742 return INVALID_OPERATION;
2743 }
2744 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002745
2746 sp<AudioPatch> patchDesc;
2747 ssize_t index = mAudioPatches.indexOfKey(*handle);
2748
Eric Laurent6a94d692014-05-20 11:18:06 -07002749 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2750 patch->sources[0].role,
2751 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002752#if LOG_NDEBUG == 0
2753 for (size_t i = 0; i < patch->num_sinks; i++) {
Eric Laurent7b279bb2015-12-14 10:18:23 -08002754 ALOGV("createAudioPatch sink %zu: id %d role %d type %d", i, patch->sinks[i].id,
Eric Laurent874c42872014-08-08 15:13:39 -07002755 patch->sinks[i].role,
2756 patch->sinks[i].type);
2757 }
2758#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002759
2760 if (index >= 0) {
2761 patchDesc = mAudioPatches.valueAt(index);
2762 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2763 mUidCached, patchDesc->mUid, uid);
2764 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2765 return INVALID_OPERATION;
2766 }
2767 } else {
Glenn Kastena13cde92016-03-28 15:26:02 -07002768 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07002769 }
2770
2771 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002772 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002773 if (outputDesc == NULL) {
2774 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2775 return BAD_VALUE;
2776 }
Eric Laurent84c70242014-06-23 08:46:27 -07002777 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2778 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002779 if (patchDesc != 0) {
2780 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2781 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2782 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2783 return BAD_VALUE;
2784 }
2785 }
Eric Laurent874c42872014-08-08 15:13:39 -07002786 DeviceVector devices;
2787 for (size_t i = 0; i < patch->num_sinks; i++) {
2788 // Only support mix to devices connection
2789 // TODO add support for mix to mix connection
2790 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2791 ALOGV("createAudioPatch() source mix but sink is not a device");
2792 return INVALID_OPERATION;
2793 }
2794 sp<DeviceDescriptor> devDesc =
2795 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2796 if (devDesc == 0) {
2797 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2798 return BAD_VALUE;
2799 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002800
François Gaffie53615e22015-03-19 09:24:12 +01002801 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002802 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002803 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002804 NULL, // updatedSamplingRate
2805 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002806 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002807 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002808 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002809 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002810 ALOGV("createAudioPatch() profile not supported for device %08x",
2811 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002812 return INVALID_OPERATION;
2813 }
2814 devices.add(devDesc);
2815 }
2816 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002817 return INVALID_OPERATION;
2818 }
Eric Laurent874c42872014-08-08 15:13:39 -07002819
Eric Laurent6a94d692014-05-20 11:18:06 -07002820 // TODO: reconfigure output format and channels here
2821 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002822 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002823 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002824 index = mAudioPatches.indexOfKey(*handle);
2825 if (index >= 0) {
2826 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2827 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2828 }
2829 patchDesc = mAudioPatches.valueAt(index);
2830 patchDesc->mUid = uid;
2831 ALOGV("createAudioPatch() success");
2832 } else {
2833 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2834 return INVALID_OPERATION;
2835 }
2836 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2837 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2838 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002839 // only one sink supported when connecting an input device to a mix
2840 if (patch->num_sinks > 1) {
2841 return INVALID_OPERATION;
2842 }
François Gaffie53615e22015-03-19 09:24:12 +01002843 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002844 if (inputDesc == NULL) {
2845 return BAD_VALUE;
2846 }
2847 if (patchDesc != 0) {
2848 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2849 return BAD_VALUE;
2850 }
2851 }
2852 sp<DeviceDescriptor> devDesc =
2853 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2854 if (devDesc == 0) {
2855 return BAD_VALUE;
2856 }
2857
François Gaffie53615e22015-03-19 09:24:12 +01002858 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002859 devDesc->mAddress,
2860 patch->sinks[0].sample_rate,
2861 NULL, /*updatedSampleRate*/
2862 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002863 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002864 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002865 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002866 // FIXME for the parameter type,
2867 // and the NONE
2868 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002869 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002870 return INVALID_OPERATION;
2871 }
2872 // TODO: reconfigure output format and channels here
2873 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002874 devDesc->type(), inputDesc->mIoHandle);
2875 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002876 index = mAudioPatches.indexOfKey(*handle);
2877 if (index >= 0) {
2878 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2879 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2880 }
2881 patchDesc = mAudioPatches.valueAt(index);
2882 patchDesc->mUid = uid;
2883 ALOGV("createAudioPatch() success");
2884 } else {
2885 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2886 return INVALID_OPERATION;
2887 }
2888 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2889 // device to device connection
2890 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002891 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002892 return BAD_VALUE;
2893 }
2894 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002895 sp<DeviceDescriptor> srcDeviceDesc =
2896 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002897 if (srcDeviceDesc == 0) {
2898 return BAD_VALUE;
2899 }
Eric Laurent874c42872014-08-08 15:13:39 -07002900
Eric Laurent6a94d692014-05-20 11:18:06 -07002901 //update source and sink with our own data as the data passed in the patch may
2902 // be incomplete.
2903 struct audio_patch newPatch = *patch;
2904 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002905
Eric Laurent874c42872014-08-08 15:13:39 -07002906 for (size_t i = 0; i < patch->num_sinks; i++) {
2907 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2908 ALOGV("createAudioPatch() source device but one sink is not a device");
2909 return INVALID_OPERATION;
2910 }
2911
2912 sp<DeviceDescriptor> sinkDeviceDesc =
2913 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2914 if (sinkDeviceDesc == 0) {
2915 return BAD_VALUE;
2916 }
2917 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2918
Eric Laurent3bcf8592015-04-03 12:13:24 -07002919 // create a software bridge in PatchPanel if:
2920 // - source and sink devices are on differnt HW modules OR
2921 // - audio HAL version is < 3.0
Eric Laurentfb66dd92016-01-28 18:32:03 -08002922 if (!srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) ||
Mikhail Naganov9ee05402016-10-13 15:58:17 -07002923 (srcDeviceDesc->mModule->getHalVersionMajor() < 3)) {
Eric Laurent3bcf8592015-04-03 12:13:24 -07002924 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002925 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002926 return INVALID_OPERATION;
2927 }
Eric Laurent874c42872014-08-08 15:13:39 -07002928 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002929 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002930 // if the sink device is reachable via an opened output stream, request to go via
2931 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002932 audio_io_handle_t output = selectOutput(outputs,
2933 AUDIO_OUTPUT_FLAG_NONE,
2934 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002935 if (output != AUDIO_IO_HANDLE_NONE) {
2936 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2937 if (outputDesc->isDuplicated()) {
2938 return INVALID_OPERATION;
2939 }
2940 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002941 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002942 newPatch.num_sources = 2;
2943 }
Eric Laurent83b88082014-06-20 18:31:16 -07002944 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002945 }
2946 // TODO: check from routing capabilities in config file and other conflicting patches
2947
2948 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2949 if (index >= 0) {
2950 afPatchHandle = patchDesc->mAfPatchHandle;
2951 }
2952
2953 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2954 &afPatchHandle,
2955 0);
2956 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2957 status, afPatchHandle);
2958 if (status == NO_ERROR) {
2959 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002960 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002961 addAudioPatch(patchDesc->mHandle, patchDesc);
2962 } else {
2963 patchDesc->mPatch = newPatch;
2964 }
2965 patchDesc->mAfPatchHandle = afPatchHandle;
2966 *handle = patchDesc->mHandle;
2967 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002968 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002969 } else {
2970 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2971 status);
2972 return INVALID_OPERATION;
2973 }
2974 } else {
2975 return BAD_VALUE;
2976 }
2977 } else {
2978 return BAD_VALUE;
2979 }
2980 return NO_ERROR;
2981}
2982
2983status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2984 uid_t uid)
2985{
2986 ALOGV("releaseAudioPatch() patch %d", handle);
2987
2988 ssize_t index = mAudioPatches.indexOfKey(handle);
2989
2990 if (index < 0) {
2991 return BAD_VALUE;
2992 }
2993 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2994 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2995 mUidCached, patchDesc->mUid, uid);
2996 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2997 return INVALID_OPERATION;
2998 }
2999
3000 struct audio_patch *patch = &patchDesc->mPatch;
3001 patchDesc->mUid = mUidCached;
3002 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003003 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07003004 if (outputDesc == NULL) {
3005 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
3006 return BAD_VALUE;
3007 }
3008
Eric Laurentc75307b2015-03-17 15:29:32 -07003009 setOutputDevice(outputDesc,
3010 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07003011 true,
3012 0,
3013 NULL);
3014 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
3015 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01003016 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07003017 if (inputDesc == NULL) {
3018 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
3019 return BAD_VALUE;
3020 }
3021 setInputDevice(inputDesc->mIoHandle,
Eric Laurentfb66dd92016-01-28 18:32:03 -08003022 getNewInputDevice(inputDesc),
Eric Laurent6a94d692014-05-20 11:18:06 -07003023 true,
3024 NULL);
3025 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003026 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3027 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
3028 status, patchDesc->mAfPatchHandle);
3029 removeAudioPatch(patchDesc->mHandle);
3030 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07003031 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07003032 } else {
3033 return BAD_VALUE;
3034 }
3035 } else {
3036 return BAD_VALUE;
3037 }
3038 return NO_ERROR;
3039}
3040
3041status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
3042 struct audio_patch *patches,
3043 unsigned int *generation)
3044{
François Gaffie53615e22015-03-19 09:24:12 +01003045 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003046 return BAD_VALUE;
3047 }
Eric Laurent6a94d692014-05-20 11:18:06 -07003048 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01003049 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07003050}
3051
Eric Laurente1715a42014-05-20 11:30:42 -07003052status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07003053{
Eric Laurente1715a42014-05-20 11:30:42 -07003054 ALOGV("setAudioPortConfig()");
3055
3056 if (config == NULL) {
3057 return BAD_VALUE;
3058 }
3059 ALOGV("setAudioPortConfig() on port handle %d", config->id);
3060 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07003061 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
3062 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07003063 }
3064
Eric Laurenta121f902014-06-03 13:32:54 -07003065 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07003066 if (config->type == AUDIO_PORT_TYPE_MIX) {
3067 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003068 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07003069 if (outputDesc == NULL) {
3070 return BAD_VALUE;
3071 }
Eric Laurent84c70242014-06-23 08:46:27 -07003072 ALOG_ASSERT(!outputDesc->isDuplicated(),
3073 "setAudioPortConfig() called on duplicated output %d",
3074 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07003075 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07003076 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01003077 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07003078 if (inputDesc == NULL) {
3079 return BAD_VALUE;
3080 }
Eric Laurenta121f902014-06-03 13:32:54 -07003081 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07003082 } else {
3083 return BAD_VALUE;
3084 }
3085 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
3086 sp<DeviceDescriptor> deviceDesc;
3087 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
3088 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
3089 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
3090 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
3091 } else {
3092 return BAD_VALUE;
3093 }
3094 if (deviceDesc == NULL) {
3095 return BAD_VALUE;
3096 }
Eric Laurenta121f902014-06-03 13:32:54 -07003097 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07003098 } else {
3099 return BAD_VALUE;
3100 }
3101
Eric Laurenta121f902014-06-03 13:32:54 -07003102 struct audio_port_config backupConfig;
3103 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
3104 if (status == NO_ERROR) {
3105 struct audio_port_config newConfig;
3106 audioPortConfig->toAudioPortConfig(&newConfig, config);
3107 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07003108 }
Eric Laurenta121f902014-06-03 13:32:54 -07003109 if (status != NO_ERROR) {
3110 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07003111 }
Eric Laurente1715a42014-05-20 11:30:42 -07003112
3113 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07003114}
3115
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003116void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
3117{
Eric Laurentd60560a2015-04-10 11:31:20 -07003118 clearAudioSources(uid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003119 clearAudioPatches(uid);
3120 clearSessionRoutes(uid);
3121}
3122
Eric Laurent6a94d692014-05-20 11:18:06 -07003123void AudioPolicyManager::clearAudioPatches(uid_t uid)
3124{
Eric Laurent0add0fd2014-12-04 18:58:14 -08003125 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003126 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
3127 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08003128 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07003129 }
3130 }
3131}
3132
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003133void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
3134 audio_io_handle_t ouptutToSkip)
3135{
3136 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
3137 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
3138 for (size_t j = 0; j < mOutputs.size(); j++) {
3139 if (mOutputs.keyAt(j) == ouptutToSkip) {
3140 continue;
3141 }
3142 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
3143 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
3144 continue;
3145 }
3146 // If the default device for this strategy is on another output mix,
3147 // invalidate all tracks in this strategy to force re connection.
3148 // Otherwise select new device on the output mix.
3149 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
Eric Laurent794fde22016-03-11 09:50:45 -08003150 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003151 if (getStrategy((audio_stream_type_t)stream) == strategy) {
3152 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
3153 }
3154 }
3155 } else {
3156 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
3157 setOutputDevice(outputDesc, newDevice, false);
3158 }
3159 }
3160}
3161
3162void AudioPolicyManager::clearSessionRoutes(uid_t uid)
3163{
3164 // remove output routes associated with this uid
3165 SortedVector<routing_strategy> affectedStrategies;
3166 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
3167 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
3168 if (route->mUid == uid) {
3169 mOutputRoutes.removeItemsAt(i);
3170 if (route->mDeviceDescriptor != 0) {
3171 affectedStrategies.add(getStrategy(route->mStreamType));
3172 }
3173 }
3174 }
3175 // reroute outputs if necessary
3176 for (size_t i = 0; i < affectedStrategies.size(); i++) {
3177 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
3178 }
3179
3180 // remove input routes associated with this uid
3181 SortedVector<audio_source_t> affectedSources;
3182 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
3183 sp<SessionRoute> route = mInputRoutes.valueAt(i);
3184 if (route->mUid == uid) {
3185 mInputRoutes.removeItemsAt(i);
3186 if (route->mDeviceDescriptor != 0) {
3187 affectedSources.add(route->mSource);
3188 }
3189 }
3190 }
3191 // reroute inputs if necessary
3192 SortedVector<audio_io_handle_t> inputsToClose;
3193 for (size_t i = 0; i < mInputs.size(); i++) {
3194 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08003195 if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003196 inputsToClose.add(inputDesc->mIoHandle);
3197 }
3198 }
3199 for (size_t i = 0; i < inputsToClose.size(); i++) {
3200 closeInput(inputsToClose[i]);
3201 }
3202}
3203
Eric Laurentd60560a2015-04-10 11:31:20 -07003204void AudioPolicyManager::clearAudioSources(uid_t uid)
3205{
3206 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
3207 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3208 if (sourceDesc->mUid == uid) {
3209 stopAudioSource(mAudioSources.keyAt(i));
3210 }
3211 }
3212}
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003213
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07003214status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
3215 audio_io_handle_t *ioHandle,
3216 audio_devices_t *device)
3217{
Glenn Kastenf0c6d7d2016-02-26 10:44:04 -08003218 *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
3219 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
Eric Laurentc73ca6e2014-12-12 14:34:22 -08003220 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07003221
François Gaffiedf372692015-03-19 10:43:27 +01003222 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07003223}
3224
Eric Laurentd60560a2015-04-10 11:31:20 -07003225status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source,
3226 const audio_attributes_t *attributes,
Glenn Kasten559d4392016-03-29 13:42:57 -07003227 audio_patch_handle_t *handle,
Eric Laurentd60560a2015-04-10 11:31:20 -07003228 uid_t uid)
Eric Laurent554a2772015-04-10 11:29:24 -07003229{
Eric Laurentd60560a2015-04-10 11:31:20 -07003230 ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle);
3231 if (source == NULL || attributes == NULL || handle == NULL) {
3232 return BAD_VALUE;
3233 }
3234
Glenn Kasten559d4392016-03-29 13:42:57 -07003235 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentd60560a2015-04-10 11:31:20 -07003236
3237 if (source->role != AUDIO_PORT_ROLE_SOURCE ||
3238 source->type != AUDIO_PORT_TYPE_DEVICE) {
3239 ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type);
3240 return INVALID_OPERATION;
3241 }
3242
3243 sp<DeviceDescriptor> srcDeviceDesc =
3244 mAvailableInputDevices.getDevice(source->ext.device.type,
3245 String8(source->ext.device.address));
3246 if (srcDeviceDesc == 0) {
3247 ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type);
3248 return BAD_VALUE;
3249 }
3250 sp<AudioSourceDescriptor> sourceDesc =
3251 new AudioSourceDescriptor(srcDeviceDesc, attributes, uid);
3252
3253 struct audio_patch dummyPatch;
3254 sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid);
3255 sourceDesc->mPatchDesc = patchDesc;
3256
3257 status_t status = connectAudioSource(sourceDesc);
3258 if (status == NO_ERROR) {
3259 mAudioSources.add(sourceDesc->getHandle(), sourceDesc);
3260 *handle = sourceDesc->getHandle();
3261 }
3262 return status;
3263}
3264
3265status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3266{
3267 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3268
3269 // make sure we only have one patch per source.
3270 disconnectAudioSource(sourceDesc);
3271
3272 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
Eric Laurent28d09f02016-03-08 10:43:05 -08003273 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003274 sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice;
3275
3276 audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true);
3277 sp<DeviceDescriptor> sinkDeviceDesc =
3278 mAvailableOutputDevices.getDevice(sinkDevice, String8(""));
3279
3280 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3281 struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch;
3282
3283 if (srcDeviceDesc->getAudioPort()->mModule->getHandle() ==
3284 sinkDeviceDesc->getAudioPort()->mModule->getHandle() &&
Mikhail Naganov9ee05402016-10-13 15:58:17 -07003285 srcDeviceDesc->getAudioPort()->mModule->getHalVersionMajor() >= 3 &&
Eric Laurentd60560a2015-04-10 11:31:20 -07003286 srcDeviceDesc->getAudioPort()->mGains.size() > 0) {
3287 ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__);
3288 // create patch between src device and output device
3289 // create Hwoutput and add to mHwOutputs
3290 } else {
3291 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs);
3292 audio_io_handle_t output =
3293 selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID);
3294 if (output == AUDIO_IO_HANDLE_NONE) {
3295 ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice);
3296 return INVALID_OPERATION;
3297 }
3298 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
3299 if (outputDesc->isDuplicated()) {
3300 ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice);
3301 return INVALID_OPERATION;
3302 }
3303 // create a special patch with no sink and two sources:
3304 // - the second source indicates to PatchPanel through which output mix this patch should
3305 // be connected as well as the stream type for volume control
3306 // - the sink is defined by whatever output device is currently selected for the output
3307 // though which this patch is routed.
3308 patch->num_sinks = 0;
3309 patch->num_sources = 2;
3310 srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL);
3311 outputDesc->toAudioPortConfig(&patch->sources[1], NULL);
3312 patch->sources[1].ext.mix.usecase.stream = stream;
3313 status_t status = mpClientInterface->createAudioPatch(patch,
3314 &afPatchHandle,
3315 0);
3316 ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__,
3317 status, afPatchHandle);
3318 if (status != NO_ERROR) {
3319 ALOGW("%s patch panel could not connect device patch, error %d",
3320 __FUNCTION__, status);
3321 return INVALID_OPERATION;
3322 }
3323 uint32_t delayMs = 0;
Eric Laurentc40d9692016-04-13 19:14:13 -07003324 status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs);
Eric Laurentd60560a2015-04-10 11:31:20 -07003325
3326 if (status != NO_ERROR) {
3327 mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0);
3328 return status;
3329 }
3330 sourceDesc->mSwOutput = outputDesc;
3331 if (delayMs != 0) {
3332 usleep(delayMs * 1000);
3333 }
3334 }
3335
3336 sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle;
3337 addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc);
3338
3339 return NO_ERROR;
Eric Laurent554a2772015-04-10 11:29:24 -07003340}
3341
Glenn Kasten559d4392016-03-29 13:42:57 -07003342status_t AudioPolicyManager::stopAudioSource(audio_patch_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07003343{
Eric Laurentd60560a2015-04-10 11:31:20 -07003344 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle);
3345 ALOGV("%s handle %d", __FUNCTION__, handle);
3346 if (sourceDesc == 0) {
3347 ALOGW("%s unknown source for handle %d", __FUNCTION__, handle);
3348 return BAD_VALUE;
3349 }
3350 status_t status = disconnectAudioSource(sourceDesc);
3351
3352 mAudioSources.removeItem(handle);
3353 return status;
3354}
3355
Andy Hung2ddee192015-12-18 17:34:44 -08003356status_t AudioPolicyManager::setMasterMono(bool mono)
3357{
3358 if (mMasterMono == mono) {
3359 return NO_ERROR;
3360 }
3361 mMasterMono = mono;
3362 // if enabling mono we close all offloaded devices, which will invalidate the
3363 // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible
3364 // for recreating the new AudioTrack as non-offloaded PCM.
3365 //
3366 // If disabling mono, we leave all tracks as is: we don't know which clients
3367 // and tracks are able to be recreated as offloaded. The next "song" should
3368 // play back offloaded.
3369 if (mMasterMono) {
3370 Vector<audio_io_handle_t> offloaded;
3371 for (size_t i = 0; i < mOutputs.size(); ++i) {
3372 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
3373 if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
3374 offloaded.push(desc->mIoHandle);
3375 }
3376 }
3377 for (size_t i = 0; i < offloaded.size(); ++i) {
3378 closeOutput(offloaded[i]);
3379 }
3380 }
3381 // update master mono for all remaining outputs
3382 for (size_t i = 0; i < mOutputs.size(); ++i) {
3383 updateMono(mOutputs.keyAt(i));
3384 }
3385 return NO_ERROR;
3386}
3387
3388status_t AudioPolicyManager::getMasterMono(bool *mono)
3389{
3390 *mono = mMasterMono;
3391 return NO_ERROR;
3392}
3393
Eric Laurentac9cef52017-06-09 15:46:26 -07003394float AudioPolicyManager::getStreamVolumeDB(
3395 audio_stream_type_t stream, int index, audio_devices_t device)
3396{
3397 return computeVolume(stream, index, device);
3398}
3399
Eric Laurentd60560a2015-04-10 11:31:20 -07003400status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3401{
3402 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3403
3404 sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle);
3405 if (patchDesc == 0) {
3406 ALOGW("%s source has no patch with handle %d", __FUNCTION__,
3407 sourceDesc->mPatchDesc->mHandle);
3408 return BAD_VALUE;
3409 }
3410 removeAudioPatch(sourceDesc->mPatchDesc->mHandle);
3411
Eric Laurent28d09f02016-03-08 10:43:05 -08003412 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003413 sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote();
3414 if (swOutputDesc != 0) {
3415 stopSource(swOutputDesc, stream, false);
3416 mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3417 } else {
3418 sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote();
3419 if (hwOutputDesc != 0) {
3420 // release patch between src device and output device
3421 // close Hwoutput and remove from mHwOutputs
3422 } else {
3423 ALOGW("%s source has neither SW nor HW output", __FUNCTION__);
3424 }
3425 }
3426 return NO_ERROR;
3427}
3428
3429sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput(
3430 audio_io_handle_t output, routing_strategy strategy)
3431{
3432 sp<AudioSourceDescriptor> source;
3433 for (size_t i = 0; i < mAudioSources.size(); i++) {
3434 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3435 routing_strategy sourceStrategy =
3436 (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
3437 sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote();
3438 if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) {
3439 source = sourceDesc;
3440 break;
3441 }
3442 }
3443 return source;
Eric Laurent554a2772015-04-10 11:29:24 -07003444}
3445
Eric Laurente552edb2014-03-10 17:42:56 -07003446// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07003447// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07003448// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07003449uint32_t AudioPolicyManager::nextAudioPortGeneration()
3450{
3451 return android_atomic_inc(&mAudioPortGeneration);
3452}
3453
Jaekyun Seok0d4a6af2017-02-17 17:10:17 +09003454#ifdef USE_XML_AUDIO_POLICY_CONF
3455// Treblized audio policy xml config will be located in /odm/etc or /vendor/etc.
3456static const char *kConfigLocationList[] =
3457 {"/odm/etc", "/vendor/etc", "/system/etc"};
3458static const int kConfigLocationListSize =
3459 (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0]));
3460
3461static status_t deserializeAudioPolicyXmlConfig(AudioPolicyConfig &config) {
3462 char audioPolicyXmlConfigFile[AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH];
3463 status_t ret;
3464
3465 for (int i = 0; i < kConfigLocationListSize; i++) {
3466 PolicySerializer serializer;
3467 snprintf(audioPolicyXmlConfigFile,
3468 sizeof(audioPolicyXmlConfigFile),
3469 "%s/%s",
3470 kConfigLocationList[i],
3471 AUDIO_POLICY_XML_CONFIG_FILE_NAME);
3472 ret = serializer.deserialize(audioPolicyXmlConfigFile, config);
3473 if (ret == NO_ERROR) {
3474 break;
3475 }
3476 }
3477 return ret;
3478}
3479#endif
3480
Eric Laurente0720872014-03-11 09:30:41 -07003481AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07003482 :
3483#ifdef AUDIO_POLICY_TEST
3484 Thread(false),
3485#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07003486 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07003487 mA2dpSuspended(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003488 mAudioPortGeneration(1),
3489 mBeaconMuteRefCount(0),
3490 mBeaconPlayingRefCount(0),
Eric Laurent9459fb02015-08-12 18:36:32 -07003491 mBeaconMuted(false),
Andy Hung2ddee192015-12-18 17:34:44 -08003492 mTtsOutputAvailable(false),
Eric Laurent36829f92017-04-07 19:04:42 -07003493 mMasterMono(false),
3494 mMusicEffectOutput(AUDIO_IO_HANDLE_NONE)
Eric Laurente552edb2014-03-10 17:42:56 -07003495{
François Gaffied1ab2bd2015-12-02 18:20:06 +01003496 mUidCached = getuid();
3497 mpClientInterface = clientInterface;
3498
3499 // TODO: remove when legacy conf file is removed. true on devices that use DRC on the
3500 // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly.
3501 // Note: remove also speaker_drc_enabled from global configuration of XML config file.
3502 bool speakerDrcEnabled = false;
3503
3504#ifdef USE_XML_AUDIO_POLICY_CONF
3505 mVolumeCurves = new VolumeCurvesCollection();
3506 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3507 mDefaultOutputDevice, speakerDrcEnabled,
3508 static_cast<VolumeCurvesCollection *>(mVolumeCurves));
Jaekyun Seok0d4a6af2017-02-17 17:10:17 +09003509 if (deserializeAudioPolicyXmlConfig(config) != NO_ERROR) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01003510#else
3511 mVolumeCurves = new StreamDescriptorCollection();
3512 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3513 mDefaultOutputDevice, speakerDrcEnabled);
3514 if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) &&
3515 (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) {
3516#endif
3517 ALOGE("could not load audio policy configuration file, setting defaults");
3518 config.setDefault();
3519 }
3520 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
3521 mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled);
3522
3523 // Once policy config has been parsed, retrieve an instance of the engine and initialize it.
François Gaffie2110e042015-03-24 08:41:51 +01003524 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
3525 if (!engineInstance) {
3526 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
3527 return;
3528 }
3529 // Retrieve the Policy Manager Interface
3530 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
3531 if (mEngine == NULL) {
3532 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
3533 return;
3534 }
3535 mEngine->setObserver(this);
3536 status_t status = mEngine->initCheck();
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07003537 (void) status;
François Gaffie2110e042015-03-24 08:41:51 +01003538 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
3539
Eric Laurent3a4311c2014-03-17 12:00:47 -07003540 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07003541 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07003542 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
3543 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07003544 for (size_t i = 0; i < mHwModules.size(); i++) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003545 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003546 if (mHwModules[i]->mHandle == 0) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003547 ALOGW("could not open HW module %s", mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003548 continue;
3549 }
3550 // open all output streams needed to access attached devices
3551 // except for direct output streams that are only opened when they are actually
3552 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07003553 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07003554 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3555 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003556 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003557
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003558 if (!outProfile->hasSupportedDevices()) {
3559 ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003560 continue;
3561 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003562 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) {
Eric Laurent9459fb02015-08-12 18:36:32 -07003563 mTtsOutputAvailable = true;
3564 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003565
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003566 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
Eric Laurentd78f1532014-09-16 16:38:20 -07003567 continue;
3568 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003569 audio_devices_t profileType = outProfile->getSupportedDevicesType();
François Gaffie53615e22015-03-19 09:24:12 +01003570 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
3571 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07003572 } else {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003573 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003574 // outputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003575 profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes);
Eric Laurente552edb2014-03-10 17:42:56 -07003576 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003577 if ((profileType & outputDeviceTypes) == 0) {
3578 continue;
3579 }
Eric Laurentc75307b2015-03-17 15:29:32 -07003580 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
3581 mpClientInterface);
Eric Laurentc40d9692016-04-13 19:14:13 -07003582 const DeviceVector &supportedDevices = outProfile->getSupportedDevices();
3583 const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType);
3584 String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress
3585 : String8("");
Eric Laurentd78f1532014-09-16 16:38:20 -07003586
3587 outputDesc->mDevice = profileType;
3588 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3589 config.sample_rate = outputDesc->mSamplingRate;
3590 config.channel_mask = outputDesc->mChannelMask;
3591 config.format = outputDesc->mFormat;
3592 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003593 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003594 &output,
3595 &config,
3596 &outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003597 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003598 &outputDesc->mLatency,
3599 outputDesc->mFlags);
3600
3601 if (status != NO_ERROR) {
3602 ALOGW("Cannot open output stream for device %08x on hw module %s",
3603 outputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003604 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003605 } else {
3606 outputDesc->mSamplingRate = config.sample_rate;
3607 outputDesc->mChannelMask = config.channel_mask;
3608 outputDesc->mFormat = config.format;
3609
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003610 for (size_t k = 0; k < supportedDevices.size(); k++) {
3611 ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003612 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08003613 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
3614 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003615 }
3616 }
3617 if (mPrimaryOutput == 0 &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003618 outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003619 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07003620 }
3621 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003622 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07003623 outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003624 true,
3625 0,
3626 NULL,
3627 address.string());
Eric Laurentd78f1532014-09-16 16:38:20 -07003628 }
Eric Laurente552edb2014-03-10 17:42:56 -07003629 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003630 // open input streams needed to access attached devices to validate
3631 // mAvailableInputDevices list
3632 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
3633 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003634 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003635
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003636 if (!inProfile->hasSupportedDevices()) {
3637 ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003638 continue;
3639 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003640 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003641 // inputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003642 audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes);
3643
Eric Laurentd78f1532014-09-16 16:38:20 -07003644 if ((profileType & inputDeviceTypes) == 0) {
3645 continue;
3646 }
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08003647 sp<AudioInputDescriptor> inputDesc =
3648 new AudioInputDescriptor(inProfile);
Eric Laurentd78f1532014-09-16 16:38:20 -07003649
Eric Laurentd78f1532014-09-16 16:38:20 -07003650 inputDesc->mDevice = profileType;
3651
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003652 // find the address
3653 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
3654 // the inputs vector must be of size 1, but we don't want to crash here
3655 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
3656 : String8("");
3657 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
3658 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
3659
Eric Laurentd78f1532014-09-16 16:38:20 -07003660 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3661 config.sample_rate = inputDesc->mSamplingRate;
3662 config.channel_mask = inputDesc->mChannelMask;
3663 config.format = inputDesc->mFormat;
3664 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003665 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003666 &input,
3667 &config,
3668 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003669 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003670 AUDIO_SOURCE_MIC,
3671 AUDIO_INPUT_FLAG_NONE);
3672
3673 if (status == NO_ERROR) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003674 const DeviceVector &supportedDevices = inProfile->getSupportedDevices();
3675 for (size_t k = 0; k < supportedDevices.size(); k++) {
3676 ssize_t index = mAvailableInputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003677 // give a valid ID to an attached device once confirmed it is reachable
Eric Laurent45aabc32015-08-06 09:11:13 -07003678 if (index >= 0) {
3679 sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index];
3680 if (!devDesc->isAttached()) {
3681 devDesc->attach(mHwModules[i]);
3682 devDesc->importAudioPort(inProfile);
3683 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003684 }
3685 }
3686 mpClientInterface->closeInput(input);
3687 } else {
3688 ALOGW("Cannot open input stream for device %08x on hw module %s",
3689 inputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003690 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003691 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003692 }
3693 }
3694 // make sure all attached devices have been allocated a unique ID
3695 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003696 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003697 ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003698 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
3699 continue;
3700 }
François Gaffie2110e042015-03-24 08:41:51 +01003701 // The device is now validated and can be appended to the available devices of the engine
3702 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
3703 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003704 i++;
3705 }
3706 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003707 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01003708 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003709 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
3710 continue;
3711 }
François Gaffie2110e042015-03-24 08:41:51 +01003712 // The device is now validated and can be appended to the available devices of the engine
3713 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
3714 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003715 i++;
3716 }
3717 // make sure default device is reachable
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003718 if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01003719 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003720 }
Eric Laurente552edb2014-03-10 17:42:56 -07003721
3722 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
3723
3724 updateDevicesAndOutputs();
3725
3726#ifdef AUDIO_POLICY_TEST
3727 if (mPrimaryOutput != 0) {
3728 AudioParameter outputCmd = AudioParameter();
3729 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003730 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07003731
3732 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
3733 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07003734 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
3735 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003736 mTestLatencyMs = 0;
3737 mCurOutput = 0;
3738 mDirectOutput = false;
3739 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3740 mTestOutputs[i] = 0;
3741 }
3742
3743 const size_t SIZE = 256;
3744 char buffer[SIZE];
3745 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
3746 run(buffer, ANDROID_PRIORITY_AUDIO);
3747 }
3748#endif //AUDIO_POLICY_TEST
3749}
3750
Eric Laurente0720872014-03-11 09:30:41 -07003751AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07003752{
3753#ifdef AUDIO_POLICY_TEST
3754 exit();
3755#endif //AUDIO_POLICY_TEST
3756 for (size_t i = 0; i < mOutputs.size(); i++) {
3757 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003758 }
3759 for (size_t i = 0; i < mInputs.size(); i++) {
3760 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003761 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003762 mAvailableOutputDevices.clear();
3763 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003764 mOutputs.clear();
3765 mInputs.clear();
3766 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07003767}
3768
Eric Laurente0720872014-03-11 09:30:41 -07003769status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07003770{
Eric Laurent87ffa392015-05-22 10:32:38 -07003771 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003772}
3773
3774#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07003775bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07003776{
3777 ALOGV("entering threadLoop()");
3778 while (!exitPending())
3779 {
3780 String8 command;
3781 int valueInt;
3782 String8 value;
3783
3784 Mutex::Autolock _l(mLock);
3785 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
3786
3787 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
3788 AudioParameter param = AudioParameter(command);
3789
3790 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
3791 valueInt != 0) {
3792 ALOGV("Test command %s received", command.string());
3793 String8 target;
3794 if (param.get(String8("target"), target) != NO_ERROR) {
3795 target = "Manager";
3796 }
3797 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
3798 param.remove(String8("test_cmd_policy_output"));
3799 mCurOutput = valueInt;
3800 }
3801 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
3802 param.remove(String8("test_cmd_policy_direct"));
3803 if (value == "false") {
3804 mDirectOutput = false;
3805 } else if (value == "true") {
3806 mDirectOutput = true;
3807 }
3808 }
3809 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
3810 param.remove(String8("test_cmd_policy_input"));
3811 mTestInput = valueInt;
3812 }
3813
3814 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
3815 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07003816 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07003817 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003818 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003819 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003820 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003821 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003822 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07003823 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003824 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07003825 if (target == "Manager") {
3826 mTestFormat = format;
3827 } else if (mTestOutputs[mCurOutput] != 0) {
3828 AudioParameter outputParam = AudioParameter();
Mikhail Naganov388360c2016-10-17 17:09:41 -07003829 outputParam.addInt(String8(AudioParameter::keyStreamSupportedFormats), format);
Eric Laurente552edb2014-03-10 17:42:56 -07003830 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3831 }
3832 }
3833 }
3834 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
3835 param.remove(String8("test_cmd_policy_channels"));
3836 int channels = 0;
3837
3838 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003839 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003840 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003841 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07003842 }
3843 if (channels != 0) {
3844 if (target == "Manager") {
3845 mTestChannels = channels;
3846 } else if (mTestOutputs[mCurOutput] != 0) {
3847 AudioParameter outputParam = AudioParameter();
Mikhail Naganov388360c2016-10-17 17:09:41 -07003848 outputParam.addInt(String8(AudioParameter::keyStreamSupportedChannels), channels);
Eric Laurente552edb2014-03-10 17:42:56 -07003849 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3850 }
3851 }
3852 }
3853 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
3854 param.remove(String8("test_cmd_policy_sampleRate"));
3855 if (valueInt >= 0 && valueInt <= 96000) {
3856 int samplingRate = valueInt;
3857 if (target == "Manager") {
3858 mTestSamplingRate = samplingRate;
3859 } else if (mTestOutputs[mCurOutput] != 0) {
3860 AudioParameter outputParam = AudioParameter();
Mikhail Naganov388360c2016-10-17 17:09:41 -07003861 outputParam.addInt(String8(AudioParameter::keyStreamSupportedSamplingRates), samplingRate);
Eric Laurente552edb2014-03-10 17:42:56 -07003862 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3863 }
3864 }
3865 }
3866
3867 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
3868 param.remove(String8("test_cmd_policy_reopen"));
3869
Eric Laurentc75307b2015-03-17 15:29:32 -07003870 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07003871
Eric Laurentc75307b2015-03-17 15:29:32 -07003872 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07003873
Eric Laurentc75307b2015-03-17 15:29:32 -07003874 removeOutput(mPrimaryOutput->mIoHandle);
3875 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
3876 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003877 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003878 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3879 config.sample_rate = outputDesc->mSamplingRate;
3880 config.channel_mask = outputDesc->mChannelMask;
3881 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07003882 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003883 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07003884 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07003885 &config,
3886 &outputDesc->mDevice,
3887 String8(""),
3888 &outputDesc->mLatency,
3889 outputDesc->mFlags);
3890 if (status != NO_ERROR) {
3891 ALOGE("Failed to reopen hardware output stream, "
3892 "samplingRate: %d, format %d, channels %d",
3893 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003894 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003895 outputDesc->mSamplingRate = config.sample_rate;
3896 outputDesc->mChannelMask = config.channel_mask;
3897 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07003898 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07003899 AudioParameter outputCmd = AudioParameter();
3900 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003901 mpClientInterface->setParameters(handle, outputCmd.toString());
3902 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07003903 }
3904 }
3905
3906
3907 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3908 }
3909 }
3910 return false;
3911}
3912
Eric Laurente0720872014-03-11 09:30:41 -07003913void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003914{
3915 {
3916 AutoMutex _l(mLock);
3917 requestExit();
3918 mWaitWorkCV.signal();
3919 }
3920 requestExitAndWait();
3921}
3922
Eric Laurente0720872014-03-11 09:30:41 -07003923int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003924{
3925 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3926 if (output == mTestOutputs[i]) return i;
3927 }
3928 return 0;
3929}
3930#endif //AUDIO_POLICY_TEST
3931
3932// ---
3933
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003934void AudioPolicyManager::addOutput(audio_io_handle_t output, const sp<SwAudioOutputDescriptor>& outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003935{
François Gaffie98cc1912015-03-18 17:52:40 +01003936 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003937 mOutputs.add(output, outputDesc);
Andy Hung2ddee192015-12-18 17:34:44 -08003938 updateMono(output); // update mono status when adding to output list
Eric Laurent36829f92017-04-07 19:04:42 -07003939 selectOutputForMusicEffects();
Eric Laurent6a94d692014-05-20 11:18:06 -07003940 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003941}
3942
François Gaffie53615e22015-03-19 09:24:12 +01003943void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3944{
3945 mOutputs.removeItem(output);
Eric Laurent36829f92017-04-07 19:04:42 -07003946 selectOutputForMusicEffects();
François Gaffie53615e22015-03-19 09:24:12 +01003947}
3948
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003949void AudioPolicyManager::addInput(audio_io_handle_t input, const sp<AudioInputDescriptor>& inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003950{
François Gaffie98cc1912015-03-18 17:52:40 +01003951 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003952 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003953 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003954}
Eric Laurente552edb2014-03-10 17:42:56 -07003955
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003956void AudioPolicyManager::findIoHandlesByAddress(const sp<SwAudioOutputDescriptor>& desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003957 const audio_devices_t device /*in*/,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003958 const String8& address /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003959 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003960 sp<DeviceDescriptor> devDesc =
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003961 desc->mProfile->getSupportedDeviceByAddress(device, address);
keunyoung3190e672014-12-30 13:00:52 -08003962 if (devDesc != 0) {
3963 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3964 desc->mIoHandle, address.string());
3965 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003966 }
3967}
3968
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003969status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003970 audio_policy_dev_state_t state,
3971 SortedVector<audio_io_handle_t>& outputs,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003972 const String8& address)
Eric Laurente552edb2014-03-10 17:42:56 -07003973{
François Gaffie53615e22015-03-19 09:24:12 +01003974 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003975 sp<SwAudioOutputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003976
3977 if (audio_device_is_digital(device)) {
3978 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003979 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003980 }
Eric Laurente552edb2014-03-10 17:42:56 -07003981
Eric Laurent3b73df72014-03-11 09:06:29 -07003982 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003983 // first list already open outputs that can be routed to this device
3984 for (size_t i = 0; i < mOutputs.size(); i++) {
3985 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003986 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003987 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003988 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3989 outputs.add(mOutputs.keyAt(i));
3990 } else {
3991 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003992 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003993 }
Eric Laurente552edb2014-03-10 17:42:56 -07003994 }
3995 }
3996 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003997 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003998 for (size_t i = 0; i < mHwModules.size(); i++)
3999 {
4000 if (mHwModules[i]->mHandle == 0) {
4001 continue;
4002 }
4003 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
4004 {
Eric Laurent275e8e92014-11-30 15:14:47 -08004005 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004006 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01004007 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004008 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004009 profiles.add(profile);
4010 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
4011 }
Eric Laurente552edb2014-03-10 17:42:56 -07004012 }
4013 }
4014 }
4015
Eric Laurent7b279bb2015-12-14 10:18:23 -08004016 ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size());
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004017
Eric Laurente552edb2014-03-10 17:42:56 -07004018 if (profiles.isEmpty() && outputs.isEmpty()) {
4019 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
4020 return BAD_VALUE;
4021 }
4022
4023 // open outputs for matching profiles if needed. Direct outputs are also opened to
4024 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
4025 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004026 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07004027
4028 // nothing to do if one output is already opened for this profile
4029 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004030 for (j = 0; j < outputs.size(); j++) {
4031 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07004032 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07004033 // matching profile: save the sample rates, format and channel masks supported
4034 // by the profile in our device descriptor
Paul McLean9080a4c2015-06-18 08:24:02 -07004035 if (audio_device_is_digital(device)) {
4036 devDesc->importAudioPort(profile);
4037 }
Eric Laurente552edb2014-03-10 17:42:56 -07004038 break;
4039 }
4040 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004041 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07004042 continue;
4043 }
4044
Eric Laurent83b88082014-06-20 18:31:16 -07004045 ALOGV("opening output for device %08x with params %s profile %p",
4046 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07004047 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07004048 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07004049 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
4050 config.sample_rate = desc->mSamplingRate;
4051 config.channel_mask = desc->mChannelMask;
4052 config.format = desc->mFormat;
4053 config.offload_info.sample_rate = desc->mSamplingRate;
4054 config.offload_info.channel_mask = desc->mChannelMask;
4055 config.offload_info.format = desc->mFormat;
4056 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07004057 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07004058 &output,
4059 &config,
4060 &desc->mDevice,
4061 address,
4062 &desc->mLatency,
4063 desc->mFlags);
4064 if (status == NO_ERROR) {
4065 desc->mSamplingRate = config.sample_rate;
4066 desc->mChannelMask = config.channel_mask;
4067 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07004068
Eric Laurentd4692962014-05-05 18:13:44 -07004069 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07004070 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07004071 char *param = audio_device_address_to_parameter(device, address);
4072 mpClientInterface->setParameters(output, String8(param));
4073 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07004074 }
Phil Burk00eeb322016-03-31 12:41:00 -07004075 updateAudioProfiles(device, output, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01004076 if (!profile->hasValidAudioProfile()) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004077 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07004078 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004079 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01004080 } else if (profile->hasDynamicAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07004081 mpClientInterface->closeOutput(output);
Phil Burk702b1052016-03-02 16:38:26 -08004082 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01004083 profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004084 config.offload_info.sample_rate = config.sample_rate;
4085 config.offload_info.channel_mask = config.channel_mask;
4086 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07004087 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07004088 &output,
4089 &config,
4090 &desc->mDevice,
4091 address,
4092 &desc->mLatency,
4093 desc->mFlags);
4094 if (status == NO_ERROR) {
4095 desc->mSamplingRate = config.sample_rate;
4096 desc->mChannelMask = config.channel_mask;
4097 desc->mFormat = config.format;
4098 } else {
4099 output = AUDIO_IO_HANDLE_NONE;
4100 }
Eric Laurentd4692962014-05-05 18:13:44 -07004101 }
4102
Eric Laurentcf2c0212014-07-25 16:20:43 -07004103 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07004104 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01004105 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01004106 sp<AudioPolicyMix> policyMix;
4107 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004108 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
4109 address.string());
4110 }
François Gaffie036e1e92015-03-19 10:16:24 +01004111 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07004112 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01004113
Eric Laurent87ffa392015-05-22 10:32:38 -07004114 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
4115 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08004116 // no duplicated output for direct outputs and
4117 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07004118 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07004119
Eric Laurentd4692962014-05-05 18:13:44 -07004120 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07004121 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07004122
Eric Laurentd4692962014-05-05 18:13:44 -07004123 //TODO: configure audio effect output stage here
4124
4125 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07004126 duplicatedOutput =
4127 mpClientInterface->openDuplicateOutput(output,
4128 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004129 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07004130 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07004131 sp<SwAudioOutputDescriptor> dupOutputDesc =
4132 new SwAudioOutputDescriptor(NULL, mpClientInterface);
4133 dupOutputDesc->mOutput1 = mPrimaryOutput;
4134 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07004135 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
4136 dupOutputDesc->mFormat = desc->mFormat;
4137 dupOutputDesc->mChannelMask = desc->mChannelMask;
4138 dupOutputDesc->mLatency = desc->mLatency;
4139 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07004140 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07004141 } else {
4142 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07004143 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07004144 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01004145 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07004146 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07004147 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07004148 }
Eric Laurente552edb2014-03-10 17:42:56 -07004149 }
4150 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07004151 } else {
4152 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07004153 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07004154 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07004155 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004156 profiles.removeAt(profile_index);
4157 profile_index--;
4158 } else {
4159 outputs.add(output);
Paul McLean9080a4c2015-06-18 08:24:02 -07004160 // Load digital format info only for digital devices
4161 if (audio_device_is_digital(device)) {
4162 devDesc->importAudioPort(profile);
4163 }
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07004164
François Gaffie53615e22015-03-19 09:24:12 +01004165 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004166 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
4167 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07004168 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004169 NULL/*patch handle*/, address.string());
4170 }
Eric Laurente552edb2014-03-10 17:42:56 -07004171 ALOGV("checkOutputsForDevice(): adding output %d", output);
4172 }
4173 }
4174
4175 if (profiles.isEmpty()) {
4176 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
4177 return BAD_VALUE;
4178 }
Eric Laurentd4692962014-05-05 18:13:44 -07004179 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07004180 // check if one opened output is not needed any more after disconnecting one device
4181 for (size_t i = 0; i < mOutputs.size(); i++) {
4182 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004183 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004184 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01004185 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07004186 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08004187 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07004188 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004189 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
4190 mOutputs.keyAt(i));
4191 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004192 }
Eric Laurente552edb2014-03-10 17:42:56 -07004193 }
4194 }
Eric Laurentd4692962014-05-05 18:13:44 -07004195 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07004196 for (size_t i = 0; i < mHwModules.size(); i++)
4197 {
4198 if (mHwModules[i]->mHandle == 0) {
4199 continue;
4200 }
4201 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
4202 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004203 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004204 if (profile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07004205 ALOGV("checkOutputsForDevice(): "
4206 "clearing direct output profile %zu on module %zu", j, i);
François Gaffie112b0af2015-11-19 16:13:25 +01004207 profile->clearAudioProfiles();
Eric Laurente552edb2014-03-10 17:42:56 -07004208 }
4209 }
4210 }
4211 }
4212 return NO_ERROR;
4213}
4214
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004215status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01004216 audio_policy_dev_state_t state,
4217 SortedVector<audio_io_handle_t>& inputs,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004218 const String8& address)
Eric Laurentd4692962014-05-05 18:13:44 -07004219{
Paul McLean9080a4c2015-06-18 08:24:02 -07004220 audio_devices_t device = devDesc->type();
Eric Laurent1f2f2232014-06-02 12:01:23 -07004221 sp<AudioInputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07004222
4223 if (audio_device_is_digital(device)) {
4224 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08004225 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07004226 }
4227
Eric Laurentd4692962014-05-05 18:13:44 -07004228 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
4229 // first list already open inputs that can be routed to this device
4230 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
4231 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004232 if (desc->mProfile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07004233 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
4234 inputs.add(mInputs.keyAt(input_index));
4235 }
4236 }
4237
4238 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07004239 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07004240 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
4241 {
4242 if (mHwModules[module_idx]->mHandle == 0) {
4243 continue;
4244 }
4245 for (size_t profile_index = 0;
4246 profile_index < mHwModules[module_idx]->mInputProfiles.size();
4247 profile_index++)
4248 {
Eric Laurent275e8e92014-11-30 15:14:47 -08004249 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
4250
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004251 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01004252 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004253 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004254 profiles.add(profile);
4255 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
4256 profile_index, module_idx);
4257 }
Eric Laurentd4692962014-05-05 18:13:44 -07004258 }
4259 }
4260 }
4261
4262 if (profiles.isEmpty() && inputs.isEmpty()) {
4263 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
4264 return BAD_VALUE;
4265 }
4266
4267 // open inputs for matching profiles if needed. Direct inputs are also opened to
4268 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
4269 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
4270
Eric Laurent1c333e22014-05-20 10:48:17 -07004271 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07004272 // nothing to do if one input is already opened for this profile
4273 size_t input_index;
4274 for (input_index = 0; input_index < mInputs.size(); input_index++) {
4275 desc = mInputs.valueAt(input_index);
4276 if (desc->mProfile == profile) {
Paul McLean9080a4c2015-06-18 08:24:02 -07004277 if (audio_device_is_digital(device)) {
4278 devDesc->importAudioPort(profile);
4279 }
Eric Laurentd4692962014-05-05 18:13:44 -07004280 break;
4281 }
4282 }
4283 if (input_index != mInputs.size()) {
4284 continue;
4285 }
4286
4287 ALOGV("opening input for device 0x%X with params %s", device, address.string());
4288 desc = new AudioInputDescriptor(profile);
4289 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07004290 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
4291 config.sample_rate = desc->mSamplingRate;
4292 config.channel_mask = desc->mChannelMask;
4293 config.format = desc->mFormat;
4294 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07004295 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07004296 &input,
4297 &config,
4298 &desc->mDevice,
4299 address,
4300 AUDIO_SOURCE_MIC,
4301 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07004302
Eric Laurentcf2c0212014-07-25 16:20:43 -07004303 if (status == NO_ERROR) {
4304 desc->mSamplingRate = config.sample_rate;
4305 desc->mChannelMask = config.channel_mask;
4306 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07004307
Eric Laurentd4692962014-05-05 18:13:44 -07004308 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07004309 char *param = audio_device_address_to_parameter(device, address);
4310 mpClientInterface->setParameters(input, String8(param));
4311 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07004312 }
Phil Burk00eeb322016-03-31 12:41:00 -07004313 updateAudioProfiles(device, input, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01004314 if (!profile->hasValidAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07004315 ALOGW("checkInputsForDevice() direct input missing param");
4316 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004317 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07004318 }
4319
4320 if (input != 0) {
4321 addInput(input, desc);
4322 }
4323 } // endif input != 0
4324
Eric Laurentcf2c0212014-07-25 16:20:43 -07004325 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07004326 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07004327 profiles.removeAt(profile_index);
4328 profile_index--;
4329 } else {
4330 inputs.add(input);
Paul McLean9080a4c2015-06-18 08:24:02 -07004331 if (audio_device_is_digital(device)) {
4332 devDesc->importAudioPort(profile);
4333 }
Eric Laurentd4692962014-05-05 18:13:44 -07004334 ALOGV("checkInputsForDevice(): adding input %d", input);
4335 }
4336 } // end scan profiles
4337
4338 if (profiles.isEmpty()) {
4339 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
4340 return BAD_VALUE;
4341 }
4342 } else {
4343 // Disconnect
4344 // check if one opened input is not needed any more after disconnecting one device
4345 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
4346 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004347 if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) {
Eric Laurentd4692962014-05-05 18:13:44 -07004348 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
4349 mInputs.keyAt(input_index));
4350 inputs.add(mInputs.keyAt(input_index));
4351 }
4352 }
4353 // Clear any profiles associated with the disconnected device.
4354 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
4355 if (mHwModules[module_index]->mHandle == 0) {
4356 continue;
4357 }
4358 for (size_t profile_index = 0;
4359 profile_index < mHwModules[module_index]->mInputProfiles.size();
4360 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004361 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004362 if (profile->supportDevice(device)) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07004363 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07004364 profile_index, module_index);
François Gaffie112b0af2015-11-19 16:13:25 +01004365 profile->clearAudioProfiles();
Eric Laurentd4692962014-05-05 18:13:44 -07004366 }
4367 }
4368 }
4369 } // end disconnect
4370
4371 return NO_ERROR;
4372}
4373
4374
Eric Laurente0720872014-03-11 09:30:41 -07004375void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07004376{
4377 ALOGV("closeOutput(%d)", output);
4378
Eric Laurentc75307b2015-03-17 15:29:32 -07004379 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004380 if (outputDesc == NULL) {
4381 ALOGW("closeOutput() unknown output %d", output);
4382 return;
4383 }
François Gaffie036e1e92015-03-19 10:16:24 +01004384 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08004385
Eric Laurente552edb2014-03-10 17:42:56 -07004386 // look for duplicated outputs connected to the output being removed.
4387 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004388 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07004389 if (dupOutputDesc->isDuplicated() &&
4390 (dupOutputDesc->mOutput1 == outputDesc ||
4391 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004392 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07004393 if (dupOutputDesc->mOutput1 == outputDesc) {
4394 outputDesc2 = dupOutputDesc->mOutput2;
4395 } else {
4396 outputDesc2 = dupOutputDesc->mOutput1;
4397 }
4398 // As all active tracks on duplicated output will be deleted,
4399 // and as they were also referenced on the other output, the reference
4400 // count for their stream type must be adjusted accordingly on
4401 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07004402 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07004403 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07004404 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07004405 }
4406 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
4407 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
4408
4409 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01004410 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07004411 }
4412 }
4413
Eric Laurent05b90f82014-08-27 15:32:29 -07004414 nextAudioPortGeneration();
4415
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004416 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004417 if (index >= 0) {
4418 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07004419 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07004420 mAudioPatches.removeItemsAt(index);
4421 mpClientInterface->onAudioPatchListUpdate();
4422 }
4423
Eric Laurente552edb2014-03-10 17:42:56 -07004424 AudioParameter param;
4425 param.add(String8("closing"), String8("true"));
4426 mpClientInterface->setParameters(output, param.toString());
4427
4428 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01004429 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004430 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07004431}
4432
4433void AudioPolicyManager::closeInput(audio_io_handle_t input)
4434{
4435 ALOGV("closeInput(%d)", input);
4436
4437 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
4438 if (inputDesc == NULL) {
4439 ALOGW("closeInput() unknown input %d", input);
4440 return;
4441 }
4442
Eric Laurent6a94d692014-05-20 11:18:06 -07004443 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07004444
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004445 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004446 if (index >= 0) {
4447 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07004448 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07004449 mAudioPatches.removeItemsAt(index);
4450 mpClientInterface->onAudioPatchListUpdate();
4451 }
4452
4453 mpClientInterface->closeInput(input);
4454 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07004455}
4456
Eric Laurentc75307b2015-03-17 15:29:32 -07004457SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
4458 audio_devices_t device,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004459 const SwAudioOutputCollection& openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07004460{
4461 SortedVector<audio_io_handle_t> outputs;
4462
4463 ALOGVV("getOutputsForDevice() device %04x", device);
4464 for (size_t i = 0; i < openOutputs.size(); i++) {
Eric Laurent37ddbb42016-08-10 16:19:14 -07004465 ALOGVV("output %zu isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004466 i, openOutputs.valueAt(i)->isDuplicated(),
4467 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004468 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
4469 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
4470 outputs.add(openOutputs.keyAt(i));
4471 }
4472 }
4473 return outputs;
4474}
4475
Eric Laurente0720872014-03-11 09:30:41 -07004476bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01004477 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07004478{
4479 if (outputs1.size() != outputs2.size()) {
4480 return false;
4481 }
4482 for (size_t i = 0; i < outputs1.size(); i++) {
4483 if (outputs1[i] != outputs2[i]) {
4484 return false;
4485 }
4486 }
4487 return true;
4488}
4489
Eric Laurente0720872014-03-11 09:30:41 -07004490void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07004491{
4492 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
4493 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
4494 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
4495 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
4496
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004497 // also take into account external policy-related changes: add all outputs which are
4498 // associated with policies in the "before" and "after" output vectors
4499 ALOGVV("checkOutputForStrategy(): policy related outputs");
4500 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004501 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004502 if (desc != 0 && desc->mPolicyMix != NULL) {
4503 srcOutputs.add(desc->mIoHandle);
4504 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
4505 }
4506 }
4507 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004508 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004509 if (desc != 0 && desc->mPolicyMix != NULL) {
4510 dstOutputs.add(desc->mIoHandle);
4511 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
4512 }
4513 }
4514
Eric Laurente552edb2014-03-10 17:42:56 -07004515 if (!vectorsEqual(srcOutputs,dstOutputs)) {
4516 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
4517 strategy, srcOutputs[0], dstOutputs[0]);
4518 // mute strategy while moving tracks from one output to another
4519 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004520 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01004521 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004522 setStrategyMute(strategy, true, desc);
4523 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004524 }
Eric Laurentd60560a2015-04-10 11:31:20 -07004525 sp<AudioSourceDescriptor> source =
4526 getSourceForStrategyOnOutput(srcOutputs[i], strategy);
4527 if (source != 0){
4528 connectAudioSource(source);
4529 }
Eric Laurente552edb2014-03-10 17:42:56 -07004530 }
4531
4532 // Move effects associated to this strategy from previous output to new output
4533 if (strategy == STRATEGY_MEDIA) {
Eric Laurent36829f92017-04-07 19:04:42 -07004534 selectOutputForMusicEffects();
Eric Laurente552edb2014-03-10 17:42:56 -07004535 }
4536 // Move tracks associated to this strategy from previous output to new output
Eric Laurent794fde22016-03-11 09:50:45 -08004537 for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004538 if (getStrategy((audio_stream_type_t)i) == strategy) {
4539 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07004540 }
4541 }
4542 }
4543}
4544
Eric Laurente0720872014-03-11 09:30:41 -07004545void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07004546{
François Gaffie2110e042015-03-24 08:41:51 +01004547 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004548 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004549 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01004550 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004551 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004552 checkOutputForStrategy(STRATEGY_SONIFICATION);
4553 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004554 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07004555 checkOutputForStrategy(STRATEGY_MEDIA);
4556 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004557 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07004558}
4559
Eric Laurente0720872014-03-11 09:30:41 -07004560void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07004561{
François Gaffie53615e22015-03-19 09:24:12 +01004562 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07004563 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004564 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07004565 return;
4566 }
4567
Eric Laurent3a4311c2014-03-17 12:00:47 -07004568 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08004569 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
4570 ~AUDIO_DEVICE_BIT_IN) != 0) ||
4571 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurentf732e072016-08-03 19:30:28 -07004572
4573 // if suspended, restore A2DP output if:
4574 // ((SCO device is NOT connected) ||
4575 // ((forced usage communication is NOT SCO) && (forced usage for record is NOT SCO) &&
4576 // (phone state is NOT in call) && (phone state is NOT ringing)))
Eric Laurente552edb2014-03-10 17:42:56 -07004577 //
Eric Laurentf732e072016-08-03 19:30:28 -07004578 // if not suspended, suspend A2DP output if:
4579 // (SCO device is connected) &&
4580 // ((forced usage for communication is SCO) || (forced usage for record is SCO) ||
4581 // ((phone state is in call) || (phone state is ringing)))
Eric Laurente552edb2014-03-10 17:42:56 -07004582 //
4583 if (mA2dpSuspended) {
Eric Laurentf732e072016-08-03 19:30:28 -07004584 if (!isScoConnected ||
4585 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) !=
4586 AUDIO_POLICY_FORCE_BT_SCO) &&
4587 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) !=
4588 AUDIO_POLICY_FORCE_BT_SCO) &&
4589 (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
François Gaffie2110e042015-03-24 08:41:51 +01004590 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004591
4592 mpClientInterface->restoreOutput(a2dpOutput);
4593 mA2dpSuspended = false;
4594 }
4595 } else {
Eric Laurentf732e072016-08-03 19:30:28 -07004596 if (isScoConnected &&
4597 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ==
4598 AUDIO_POLICY_FORCE_BT_SCO) ||
4599 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) ==
4600 AUDIO_POLICY_FORCE_BT_SCO) ||
4601 (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
François Gaffie2110e042015-03-24 08:41:51 +01004602 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004603
4604 mpClientInterface->suspendOutput(a2dpOutput);
4605 mA2dpSuspended = true;
4606 }
4607 }
4608}
4609
Eric Laurentc75307b2015-03-17 15:29:32 -07004610audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
4611 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004612{
4613 audio_devices_t device = AUDIO_DEVICE_NONE;
4614
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004615 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004616 if (index >= 0) {
4617 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4618 if (patchDesc->mUid != mUidCached) {
4619 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004620 outputDesc->device(), outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004621 return outputDesc->device();
4622 }
4623 }
4624
Eric Laurente552edb2014-03-10 17:42:56 -07004625 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05004626 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004627 // use device for strategy enforced audible
4628 // 2: we are in call or the strategy phone is active on the output:
4629 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05004630 // 3: the strategy for enforced audible is active but not enforced on the output:
4631 // use the device for strategy enforced audible
Eric Laurent28d09f02016-03-08 10:43:05 -08004632 // 4: the strategy sonification is active on the output:
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004633 // use device for strategy sonification
Eric Laurent28d09f02016-03-08 10:43:05 -08004634 // 5: the strategy accessibility is active on the output:
4635 // use device for strategy accessibility
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004636 // 6: the strategy "respectful" sonification is active on the output:
4637 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08004638 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004639 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08004640 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004641 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08004642 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004643 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01004644 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004645 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07004646 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
4647 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01004648 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004649 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004650 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05004651 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004652 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004653 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Eric Laurent28d09f02016-03-08 10:43:05 -08004654 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
4655 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004656 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004657 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004658 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004659 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004660 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004661 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004662 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004663 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004664 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004665 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07004666 }
4667
Eric Laurent1c333e22014-05-20 10:48:17 -07004668 ALOGV("getNewOutputDevice() selected device %x", device);
4669 return device;
4670}
4671
Eric Laurentfb66dd92016-01-28 18:32:03 -08004672audio_devices_t AudioPolicyManager::getNewInputDevice(const sp<AudioInputDescriptor>& inputDesc)
Eric Laurent1c333e22014-05-20 10:48:17 -07004673{
Eric Laurentfb66dd92016-01-28 18:32:03 -08004674 audio_devices_t device = AUDIO_DEVICE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07004675
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004676 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004677 if (index >= 0) {
4678 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4679 if (patchDesc->mUid != mUidCached) {
4680 ALOGV("getNewInputDevice() device %08x forced by patch %d",
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004681 inputDesc->mDevice, inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004682 return inputDesc->mDevice;
4683 }
4684 }
4685
Eric Laurentfb66dd92016-01-28 18:32:03 -08004686 audio_source_t source = inputDesc->getHighestPrioritySource(true /*activeOnly*/);
4687 if (isInCall()) {
4688 device = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
4689 } else if (source != AUDIO_SOURCE_DEFAULT) {
4690 device = getDeviceAndMixForInputSource(source);
4691 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004692
Eric Laurente552edb2014-03-10 17:42:56 -07004693 return device;
4694}
4695
Eric Laurent794fde22016-03-11 09:50:45 -08004696bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1,
4697 audio_stream_type_t stream2) {
Jean-Michel Trivi99bb2f92016-11-23 15:52:07 -08004698 return (stream1 == stream2);
Eric Laurent28d09f02016-03-08 10:43:05 -08004699}
4700
Eric Laurente0720872014-03-11 09:30:41 -07004701uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004702 return (uint32_t)getStrategy(stream);
4703}
4704
Eric Laurente0720872014-03-11 09:30:41 -07004705audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004706 // By checking the range of stream before calling getStrategy, we avoid
4707 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
4708 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08004709 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004710 return AUDIO_DEVICE_NONE;
4711 }
Eric Laurent28d09f02016-03-08 10:43:05 -08004712 audio_devices_t devices = AUDIO_DEVICE_NONE;
Eric Laurent794fde22016-03-11 09:50:45 -08004713 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
4714 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004715 continue;
Eric Laurent6a94d692014-05-20 11:18:06 -07004716 }
Eric Laurent794fde22016-03-11 09:50:45 -08004717 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08004718 audio_devices_t curDevices =
Eric Laurent447a87b2016-07-07 17:32:10 -07004719 getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/);
Eric Laurent28d09f02016-03-08 10:43:05 -08004720 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs);
4721 for (size_t i = 0; i < outputs.size(); i++) {
4722 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurent794fde22016-03-11 09:50:45 -08004723 if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004724 curDevices |= outputDesc->device();
4725 }
4726 }
4727 devices |= curDevices;
Eric Laurente552edb2014-03-10 17:42:56 -07004728 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05004729
4730 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
4731 and doesn't really need to.*/
4732 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
4733 devices |= AUDIO_DEVICE_OUT_SPEAKER;
4734 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4735 }
Eric Laurente552edb2014-03-10 17:42:56 -07004736 return devices;
4737}
4738
François Gaffie2110e042015-03-24 08:41:51 +01004739routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
4740{
Eric Laurent223fd5c2014-11-11 13:43:36 -08004741 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01004742 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004743}
4744
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004745uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4746 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004747 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4748 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4749 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004750 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4751 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4752 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004753 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01004754 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004755}
4756
Eric Laurente0720872014-03-11 09:30:41 -07004757void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004758 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004759 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004760 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4761 updateDevicesAndOutputs();
4762 break;
4763 default:
4764 break;
4765 }
4766}
4767
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004768uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
Eric Laurent9459fb02015-08-12 18:36:32 -07004769
4770 // skip beacon mute management if a dedicated TTS output is available
4771 if (mTtsOutputAvailable) {
4772 return 0;
4773 }
4774
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004775 switch(event) {
4776 case STARTING_OUTPUT:
4777 mBeaconMuteRefCount++;
4778 break;
4779 case STOPPING_OUTPUT:
4780 if (mBeaconMuteRefCount > 0) {
4781 mBeaconMuteRefCount--;
4782 }
4783 break;
4784 case STARTING_BEACON:
4785 mBeaconPlayingRefCount++;
4786 break;
4787 case STOPPING_BEACON:
4788 if (mBeaconPlayingRefCount > 0) {
4789 mBeaconPlayingRefCount--;
4790 }
4791 break;
4792 }
4793
4794 if (mBeaconMuteRefCount > 0) {
4795 // any playback causes beacon to be muted
4796 return setBeaconMute(true);
4797 } else {
4798 // no other playback: unmute when beacon starts playing, mute when it stops
4799 return setBeaconMute(mBeaconPlayingRefCount == 0);
4800 }
4801}
4802
4803uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4804 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4805 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4806 // keep track of muted state to avoid repeating mute/unmute operations
4807 if (mBeaconMuted != mute) {
4808 // mute/unmute AUDIO_STREAM_TTS on all outputs
4809 ALOGV("\t muting %d", mute);
4810 uint32_t maxLatency = 0;
4811 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004812 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004813 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004814 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004815 0 /*delay*/, AUDIO_DEVICE_NONE);
4816 const uint32_t latency = desc->latency() * 2;
4817 if (latency > maxLatency) {
4818 maxLatency = latency;
4819 }
4820 }
4821 mBeaconMuted = mute;
4822 return maxLatency;
4823 }
4824 return 0;
4825}
4826
Eric Laurente0720872014-03-11 09:30:41 -07004827audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004828 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004829{
Paul McLeanaa981192015-03-21 09:55:15 -07004830 // Routing
4831 // see if we have an explicit route
4832 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4833 // (getStrategy(stream)).
4834 // if the strategy from the stream type in the RouteMap is the same as the argument above,
4835 // and activity count is non-zero
4836 // the device = the device from the descriptor in the RouteMap, and exit.
4837 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4838 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
Eric Laurent28d09f02016-03-08 10:43:05 -08004839 routing_strategy routeStrategy = getStrategy(route->mStreamType);
4840 if ((routeStrategy == strategy) && route->isActive()) {
Paul McLeanaa981192015-03-21 09:55:15 -07004841 return route->mDeviceDescriptor->type();
4842 }
4843 }
4844
Eric Laurente552edb2014-03-10 17:42:56 -07004845 if (fromCache) {
4846 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4847 strategy, mDeviceForStrategy[strategy]);
4848 return mDeviceForStrategy[strategy];
4849 }
François Gaffie2110e042015-03-24 08:41:51 +01004850 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004851}
4852
Eric Laurente0720872014-03-11 09:30:41 -07004853void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004854{
4855 for (int i = 0; i < NUM_STRATEGIES; i++) {
4856 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4857 }
4858 mPreviousOutputs = mOutputs;
4859}
4860
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004861uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004862 audio_devices_t prevDevice,
4863 uint32_t delayMs)
4864{
4865 // mute/unmute strategies using an incompatible device combination
4866 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4867 // if unmuting, unmute only after the specified delay
4868 if (outputDesc->isDuplicated()) {
4869 return 0;
4870 }
4871
4872 uint32_t muteWaitMs = 0;
4873 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004874 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004875
4876 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4877 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004878 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004879 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4880 bool doMute = false;
4881
4882 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4883 doMute = true;
4884 outputDesc->mStrategyMutedByDevice[i] = true;
4885 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4886 doMute = true;
4887 outputDesc->mStrategyMutedByDevice[i] = false;
4888 }
Eric Laurent99401132014-05-07 19:48:15 -07004889 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004890 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004891 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004892 // skip output if it does not share any device with current output
4893 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4894 == AUDIO_DEVICE_NONE) {
4895 continue;
4896 }
Eric Laurent37ddbb42016-08-10 16:19:14 -07004897 ALOGVV("checkDeviceMuteStrategies() %s strategy %zu (curDevice %04x)",
Eric Laurentc75307b2015-03-17 15:29:32 -07004898 mute ? "muting" : "unmuting", i, curDevice);
4899 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004900 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004901 if (mute) {
4902 // FIXME: should not need to double latency if volume could be applied
4903 // immediately by the audioflinger mixer. We must account for the delay
4904 // between now and the next time the audioflinger thread for this output
4905 // will process a buffer (which corresponds to one buffer size,
4906 // usually 1/2 or 1/4 of the latency).
4907 if (muteWaitMs < desc->latency() * 2) {
4908 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004909 }
4910 }
4911 }
4912 }
4913 }
4914 }
4915
Eric Laurent99401132014-05-07 19:48:15 -07004916 // temporary mute output if device selection changes to avoid volume bursts due to
4917 // different per device volumes
4918 if (outputDesc->isActive() && (device != prevDevice)) {
Eric Laurentdc462862016-07-19 12:29:53 -07004919 uint32_t tempMuteWaitMs = outputDesc->latency() * 2;
4920 // temporary mute duration is conservatively set to 4 times the reported latency
4921 uint32_t tempMuteDurationMs = outputDesc->latency() * 4;
4922 if (muteWaitMs < tempMuteWaitMs) {
4923 muteWaitMs = tempMuteWaitMs;
Eric Laurent99401132014-05-07 19:48:15 -07004924 }
Eric Laurentdc462862016-07-19 12:29:53 -07004925
Eric Laurent99401132014-05-07 19:48:15 -07004926 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004927 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentdc462862016-07-19 12:29:53 -07004928 // make sure that we do not start the temporary mute period too early in case of
4929 // delayed device change
4930 setStrategyMute((routing_strategy)i, true, outputDesc, delayMs);
Eric Laurentc75307b2015-03-17 15:29:32 -07004931 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurentdc462862016-07-19 12:29:53 -07004932 delayMs + tempMuteDurationMs, device);
Eric Laurent99401132014-05-07 19:48:15 -07004933 }
4934 }
4935 }
4936
Eric Laurente552edb2014-03-10 17:42:56 -07004937 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4938 if (muteWaitMs > delayMs) {
4939 muteWaitMs -= delayMs;
4940 usleep(muteWaitMs * 1000);
4941 return muteWaitMs;
4942 }
4943 return 0;
4944}
4945
Eric Laurentc75307b2015-03-17 15:29:32 -07004946uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004947 audio_devices_t device,
4948 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004949 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004950 audio_patch_handle_t *patchHandle,
4951 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004952{
Eric Laurentc75307b2015-03-17 15:29:32 -07004953 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004954 AudioParameter param;
4955 uint32_t muteWaitMs;
4956
4957 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004958 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4959 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004960 return muteWaitMs;
4961 }
4962 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4963 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004964 if ((device != AUDIO_DEVICE_NONE) &&
4965 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004966 return 0;
4967 }
4968
4969 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004970 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004971
4972 audio_devices_t prevDevice = outputDesc->mDevice;
4973
Paul McLeanaa981192015-03-21 09:55:15 -07004974 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004975
4976 if (device != AUDIO_DEVICE_NONE) {
4977 outputDesc->mDevice = device;
4978 }
4979 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4980
4981 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004982 // the requested device is AUDIO_DEVICE_NONE
4983 // OR the requested device is the same as current device
4984 // AND force is not specified
4985 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004986 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004987 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4988 !force &&
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004989 outputDesc->getPatchHandle() != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004990 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004991 return muteWaitMs;
4992 }
4993
4994 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004995
Eric Laurente552edb2014-03-10 17:42:56 -07004996 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004997 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004998 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004999 } else {
Eric Laurentc40d9692016-04-13 19:14:13 -07005000 DeviceVector deviceList;
5001 if ((address == NULL) || (strlen(address) == 0)) {
5002 deviceList = mAvailableOutputDevices.getDevicesFromType(device);
5003 } else {
5004 deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
5005 }
5006
Eric Laurent1c333e22014-05-20 10:48:17 -07005007 if (!deviceList.isEmpty()) {
5008 struct audio_patch patch;
5009 outputDesc->toAudioPortConfig(&patch.sources[0]);
5010 patch.num_sources = 1;
5011 patch.num_sinks = 0;
5012 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
5013 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07005014 patch.num_sinks++;
5015 }
Eric Laurent6a94d692014-05-20 11:18:06 -07005016 ssize_t index;
5017 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
5018 index = mAudioPatches.indexOfKey(*patchHandle);
5019 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08005020 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07005021 }
5022 sp< AudioPatch> patchDesc;
5023 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
5024 if (index >= 0) {
5025 patchDesc = mAudioPatches.valueAt(index);
5026 afPatchHandle = patchDesc->mAfPatchHandle;
5027 }
5028
Eric Laurent1c333e22014-05-20 10:48:17 -07005029 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07005030 &afPatchHandle,
5031 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07005032 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
5033 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07005034 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07005035 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07005036 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01005037 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07005038 addAudioPatch(patchDesc->mHandle, patchDesc);
5039 } else {
5040 patchDesc->mPatch = patch;
5041 }
5042 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07005043 if (patchHandle) {
5044 *patchHandle = patchDesc->mHandle;
5045 }
Jean-Michel Triviff155c62016-02-26 12:07:16 -08005046 outputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07005047 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07005048 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07005049 }
5050 }
bryant_liuf5e7e792014-08-19 20:07:05 +08005051
5052 // inform all input as well
5053 for (size_t i = 0; i < mInputs.size(); i++) {
5054 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01005055 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08005056 AudioParameter inputCmd = AudioParameter();
5057 ALOGV("%s: inform input %d of device:%d", __func__,
5058 inputDescriptor->mIoHandle, device);
5059 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
5060 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
5061 inputCmd.toString(),
5062 delayMs);
5063 }
5064 }
Eric Laurent1c333e22014-05-20 10:48:17 -07005065 }
Eric Laurente552edb2014-03-10 17:42:56 -07005066
5067 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07005068 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005069
5070 return muteWaitMs;
5071}
5072
Eric Laurentc75307b2015-03-17 15:29:32 -07005073status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07005074 int delayMs,
5075 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07005076{
Eric Laurent6a94d692014-05-20 11:18:06 -07005077 ssize_t index;
5078 if (patchHandle) {
5079 index = mAudioPatches.indexOfKey(*patchHandle);
5080 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08005081 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07005082 }
5083 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07005084 return INVALID_OPERATION;
5085 }
Eric Laurent6a94d692014-05-20 11:18:06 -07005086 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
5087 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07005088 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07005089 outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07005090 removeAudioPatch(patchDesc->mHandle);
5091 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07005092 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07005093 return status;
5094}
5095
5096status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
5097 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07005098 bool force,
5099 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07005100{
5101 status_t status = NO_ERROR;
5102
Eric Laurent1f2f2232014-06-02 12:01:23 -07005103 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07005104 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
5105 inputDesc->mDevice = device;
5106
5107 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
5108 if (!deviceList.isEmpty()) {
5109 struct audio_patch patch;
5110 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07005111 // AUDIO_SOURCE_HOTWORD is for internal use only:
5112 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07005113 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
Eric Laurent599c7582015-12-07 18:05:55 -08005114 !inputDesc->isSoundTrigger()) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07005115 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
5116 }
Eric Laurent1c333e22014-05-20 10:48:17 -07005117 patch.num_sinks = 1;
5118 //only one input device for now
5119 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07005120 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07005121 ssize_t index;
5122 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
5123 index = mAudioPatches.indexOfKey(*patchHandle);
5124 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08005125 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07005126 }
5127 sp< AudioPatch> patchDesc;
5128 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
5129 if (index >= 0) {
5130 patchDesc = mAudioPatches.valueAt(index);
5131 afPatchHandle = patchDesc->mAfPatchHandle;
5132 }
5133
Eric Laurent1c333e22014-05-20 10:48:17 -07005134 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07005135 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07005136 0);
5137 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07005138 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07005139 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07005140 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01005141 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07005142 addAudioPatch(patchDesc->mHandle, patchDesc);
5143 } else {
5144 patchDesc->mPatch = patch;
5145 }
5146 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07005147 if (patchHandle) {
5148 *patchHandle = patchDesc->mHandle;
5149 }
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08005150 inputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07005151 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07005152 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07005153 }
5154 }
5155 }
5156 return status;
5157}
5158
Eric Laurent6a94d692014-05-20 11:18:06 -07005159status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
5160 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07005161{
Eric Laurent1f2f2232014-06-02 12:01:23 -07005162 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07005163 ssize_t index;
5164 if (patchHandle) {
5165 index = mAudioPatches.indexOfKey(*patchHandle);
5166 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08005167 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07005168 }
5169 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07005170 return INVALID_OPERATION;
5171 }
Eric Laurent6a94d692014-05-20 11:18:06 -07005172 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
5173 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07005174 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07005175 inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07005176 removeAudioPatch(patchDesc->mHandle);
5177 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07005178 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07005179 return status;
5180}
5181
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08005182sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07005183 const String8& address,
François Gaffie53615e22015-03-19 09:24:12 +01005184 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07005185 audio_format_t& format,
5186 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01005187 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07005188{
5189 // Choose an input profile based on the requested capture parameters: select the first available
5190 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07005191 //
5192 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
5193 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07005194
5195 for (size_t i = 0; i < mHwModules.size(); i++)
5196 {
5197 if (mHwModules[i]->mHandle == 0) {
5198 continue;
5199 }
5200 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
5201 {
Eric Laurent1c333e22014-05-20 10:48:17 -07005202 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07005203 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08005204 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07005205 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07005206 format,
5207 &format /*updatedFormat*/,
5208 channelMask,
5209 &channelMask /*updatedChannelMask*/,
5210 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08005211
Eric Laurente552edb2014-03-10 17:42:56 -07005212 return profile;
5213 }
5214 }
5215 }
5216 return NULL;
5217}
5218
Eric Laurentc73ca6e2014-12-12 14:34:22 -08005219
5220audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01005221 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07005222{
François Gaffie036e1e92015-03-19 10:16:24 +01005223 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
5224 audio_devices_t selectedDeviceFromMix =
5225 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08005226
François Gaffie036e1e92015-03-19 10:16:24 +01005227 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
5228 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08005229 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08005230 return getDeviceForInputSource(inputSource);
5231}
5232
5233audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
5234{
Paul McLean466dc8e2015-04-17 13:15:36 -06005235 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
5236 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07005237 if (inputSource == route->mSource && route->isActive()) {
Paul McLean466dc8e2015-04-17 13:15:36 -06005238 return route->mDeviceDescriptor->type();
5239 }
5240 }
5241
5242 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07005243}
5244
Eric Laurente0720872014-03-11 09:30:41 -07005245float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005246 int index,
5247 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005248{
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005249 float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index);
Jean-Michel Trivi3d8b4a42016-09-14 18:37:46 -07005250
5251 // handle the case of accessibility active while a ringtone is playing: if the ringtone is much
5252 // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch
5253 // exploration of the dialer UI. In this situation, bring the accessibility volume closer to
5254 // the ringtone volume
5255 if ((stream == AUDIO_STREAM_ACCESSIBILITY)
5256 && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState())
5257 && isStreamActive(AUDIO_STREAM_RING, 0)) {
5258 const float ringVolumeDB = computeVolume(AUDIO_STREAM_RING, index, device);
5259 return ringVolumeDB - 4 > volumeDB ? ringVolumeDB - 4 : volumeDB;
5260 }
5261
Eric Laurente552edb2014-03-10 17:42:56 -07005262 // if a headset is connected, apply the following rules to ring tones and notifications
5263 // to avoid sound level bursts in user's ears:
Eric Laurent6af1c1d2016-04-14 11:20:44 -07005264 // - always attenuate notifications volume by 6dB
5265 // - attenuate ring tones volume by 6dB unless music is not playing and
5266 // speaker is part of the select devices
Eric Laurente552edb2014-03-10 17:42:56 -07005267 // - if music is playing, always limit the volume to current music volume,
5268 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07005269 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005270 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
5271 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
5272 AUDIO_DEVICE_OUT_WIRED_HEADSET |
Eric Laurent904d6322017-03-17 17:20:47 -07005273 AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
5274 AUDIO_DEVICE_OUT_USB_HEADSET)) &&
Eric Laurente552edb2014-03-10 17:42:56 -07005275 ((stream_strategy == STRATEGY_SONIFICATION)
5276 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07005277 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07005278 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01005279 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
François Gaffied1ab2bd2015-12-02 18:20:06 +01005280 mVolumeCurves->canBeMuted(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005281 // when the phone is ringing we must consider that music could have been paused just before
5282 // by the music application and behave as if music was active if the last music track was
5283 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07005284 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07005285 mLimitRingtoneVolume) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005286 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07005287 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07005288 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005289 mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC,
5290 musicDevice),
5291 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07005292 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
5293 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005294 if (volumeDB > minVolDB) {
5295 volumeDB = minVolDB;
Eric Laurentffbc80f2015-03-18 18:30:19 -07005296 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07005297 }
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005298 if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
5299 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) {
5300 // on A2DP, also ensure notification volume is not too low compared to media when
5301 // intended to be played
5302 if ((volumeDB > -96.0f) &&
5303 (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) {
5304 ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f",
5305 stream, device,
5306 volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB);
5307 volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB;
5308 }
5309 }
Eric Laurent6af1c1d2016-04-14 11:20:44 -07005310 } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) ||
5311 stream_strategy != STRATEGY_SONIFICATION) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005312 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07005313 }
5314 }
5315
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005316 return volumeDB;
Eric Laurente552edb2014-03-10 17:42:56 -07005317}
5318
Eric Laurente0720872014-03-11 09:30:41 -07005319status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005320 int index,
5321 const sp<AudioOutputDescriptor>& outputDesc,
5322 audio_devices_t device,
5323 int delayMs,
5324 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005325{
Eric Laurente552edb2014-03-10 17:42:56 -07005326 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07005327 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07005328 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07005329 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07005330 return NO_ERROR;
5331 }
François Gaffie2110e042015-03-24 08:41:51 +01005332 audio_policy_forced_cfg_t forceUseForComm =
5333 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07005334 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01005335 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
5336 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005337 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01005338 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07005339 return INVALID_OPERATION;
5340 }
5341
Eric Laurentc75307b2015-03-17 15:29:32 -07005342 if (device == AUDIO_DEVICE_NONE) {
5343 device = outputDesc->device();
5344 }
Eric Laurent275e8e92014-11-30 15:14:47 -08005345
Eric Laurentffbc80f2015-03-18 18:30:19 -07005346 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07005347 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07005348 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08005349 }
Eric Laurentc75307b2015-03-17 15:29:32 -07005350
Eric Laurentffbc80f2015-03-18 18:30:19 -07005351 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07005352
Eric Laurent3b73df72014-03-11 09:06:29 -07005353 if (stream == AUDIO_STREAM_VOICE_CALL ||
5354 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07005355 float voiceVolume;
5356 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07005357 if (stream == AUDIO_STREAM_VOICE_CALL) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005358 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005359 } else {
5360 voiceVolume = 1.0;
5361 }
5362
Eric Laurent18fba842016-03-31 14:41:26 -07005363 if (voiceVolume != mLastVoiceVolume) {
Eric Laurente552edb2014-03-10 17:42:56 -07005364 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
5365 mLastVoiceVolume = voiceVolume;
5366 }
5367 }
5368
5369 return NO_ERROR;
5370}
5371
Eric Laurentc75307b2015-03-17 15:29:32 -07005372void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
5373 audio_devices_t device,
5374 int delayMs,
5375 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005376{
Eric Laurentc75307b2015-03-17 15:29:32 -07005377 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07005378
Eric Laurent794fde22016-03-11 09:50:45 -08005379 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005380 checkAndSetVolume((audio_stream_type_t)stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005381 mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005382 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005383 device,
5384 delayMs,
5385 force);
5386 }
5387}
5388
Eric Laurente0720872014-03-11 09:30:41 -07005389void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07005390 bool on,
5391 const sp<AudioOutputDescriptor>& outputDesc,
5392 int delayMs,
5393 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005394{
Eric Laurent72249d52015-06-08 11:12:15 -07005395 ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d",
5396 strategy, on, outputDesc->getId());
Eric Laurent794fde22016-03-11 09:50:45 -08005397 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005398 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005399 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07005400 }
5401 }
5402}
5403
Eric Laurente0720872014-03-11 09:30:41 -07005404void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005405 bool on,
5406 const sp<AudioOutputDescriptor>& outputDesc,
5407 int delayMs,
5408 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005409{
Eric Laurente552edb2014-03-10 17:42:56 -07005410 if (device == AUDIO_DEVICE_NONE) {
5411 device = outputDesc->device();
5412 }
5413
Eric Laurentc75307b2015-03-17 15:29:32 -07005414 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
5415 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07005416
5417 if (on) {
5418 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005419 if (mVolumeCurves->canBeMuted(stream) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07005420 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01005421 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005422 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005423 }
5424 }
5425 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
5426 outputDesc->mMuteCount[stream]++;
5427 } else {
5428 if (outputDesc->mMuteCount[stream] == 0) {
5429 ALOGV("setStreamMute() unmuting non muted stream!");
5430 return;
5431 }
5432 if (--outputDesc->mMuteCount[stream] == 0) {
5433 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005434 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005435 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005436 device,
5437 delayMs);
5438 }
5439 }
5440}
5441
Eric Laurente0720872014-03-11 09:30:41 -07005442void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07005443 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07005444{
Eric Laurent87ffa392015-05-22 10:32:38 -07005445 if(!hasPrimaryOutput()) {
5446 return;
5447 }
5448
Eric Laurente552edb2014-03-10 17:42:56 -07005449 // if the stream pertains to sonification strategy and we are in call we must
5450 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
5451 // in the device used for phone strategy and play the tone if the selected device does not
5452 // interfere with the device used for phone strategy
5453 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
5454 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07005455 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005456 if ((stream_strategy == STRATEGY_SONIFICATION) ||
5457 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005458 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07005459 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
5460 stream, starting, outputDesc->mDevice, stateChange);
5461 if (outputDesc->mRefCount[stream]) {
5462 int muteCount = 1;
5463 if (stateChange) {
5464 muteCount = outputDesc->mRefCount[stream];
5465 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005466 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005467 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
5468 for (int i = 0; i < muteCount; i++) {
5469 setStreamMute(stream, starting, mPrimaryOutput);
5470 }
5471 } else {
5472 ALOGV("handleIncallSonification() high visibility");
5473 if (outputDesc->device() &
5474 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
5475 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
5476 for (int i = 0; i < muteCount; i++) {
5477 setStreamMute(stream, starting, mPrimaryOutput);
5478 }
5479 }
5480 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005481 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
5482 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07005483 } else {
5484 mpClientInterface->stopTone();
5485 }
5486 }
5487 }
5488 }
5489}
5490
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005491audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
5492{
5493 // flags to stream type mapping
5494 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
5495 return AUDIO_STREAM_ENFORCED_AUDIBLE;
5496 }
5497 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
5498 return AUDIO_STREAM_BLUETOOTH_SCO;
5499 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08005500 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
5501 return AUDIO_STREAM_TTS;
5502 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005503
5504 // usage to stream type mapping
5505 switch (attr->usage) {
5506 case AUDIO_USAGE_MEDIA:
5507 case AUDIO_USAGE_GAME:
Jean-Michel Trivi36867762016-12-29 12:03:28 -08005508 case AUDIO_USAGE_ASSISTANT:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005509 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5510 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08005511 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5512 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005513 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5514 return AUDIO_STREAM_SYSTEM;
5515 case AUDIO_USAGE_VOICE_COMMUNICATION:
5516 return AUDIO_STREAM_VOICE_CALL;
5517
5518 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5519 return AUDIO_STREAM_DTMF;
5520
5521 case AUDIO_USAGE_ALARM:
5522 return AUDIO_STREAM_ALARM;
5523 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5524 return AUDIO_STREAM_RING;
5525
5526 case AUDIO_USAGE_NOTIFICATION:
5527 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5528 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5529 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5530 case AUDIO_USAGE_NOTIFICATION_EVENT:
5531 return AUDIO_STREAM_NOTIFICATION;
5532
5533 case AUDIO_USAGE_UNKNOWN:
5534 default:
5535 return AUDIO_STREAM_MUSIC;
5536 }
5537}
Eric Laurente83b55d2014-11-14 10:06:21 -08005538
François Gaffie53615e22015-03-19 09:24:12 +01005539bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
5540{
Eric Laurente83b55d2014-11-14 10:06:21 -08005541 // has flags that map to a strategy?
5542 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
5543 return true;
5544 }
5545
5546 // has known usage?
5547 switch (paa->usage) {
5548 case AUDIO_USAGE_UNKNOWN:
5549 case AUDIO_USAGE_MEDIA:
5550 case AUDIO_USAGE_VOICE_COMMUNICATION:
5551 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5552 case AUDIO_USAGE_ALARM:
5553 case AUDIO_USAGE_NOTIFICATION:
5554 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5555 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5556 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5557 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5558 case AUDIO_USAGE_NOTIFICATION_EVENT:
5559 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5560 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5561 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5562 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08005563 case AUDIO_USAGE_VIRTUAL_SOURCE:
Jean-Michel Trivi36867762016-12-29 12:03:28 -08005564 case AUDIO_USAGE_ASSISTANT:
Eric Laurente83b55d2014-11-14 10:06:21 -08005565 break;
5566 default:
5567 return false;
5568 }
5569 return true;
5570}
5571
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07005572bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor>& outputDesc,
François Gaffiead3183e2015-03-18 16:55:35 +01005573 routing_strategy strategy, uint32_t inPastMs,
5574 nsecs_t sysTime) const
5575{
5576 if ((sysTime == 0) && (inPastMs != 0)) {
5577 sysTime = systemTime();
5578 }
Eric Laurent794fde22016-03-11 09:50:45 -08005579 for (int i = 0; i < (int)AUDIO_STREAM_FOR_POLICY_CNT; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01005580 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
5581 (NUM_STRATEGIES == strategy)) &&
5582 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
5583 return true;
5584 }
5585 }
5586 return false;
5587}
5588
François Gaffie2110e042015-03-24 08:41:51 +01005589audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
5590{
5591 return mEngine->getForceUse(usage);
5592}
5593
5594bool AudioPolicyManager::isInCall()
5595{
5596 return isStateInCall(mEngine->getPhoneState());
5597}
5598
5599bool AudioPolicyManager::isStateInCall(int state)
5600{
5601 return is_state_in_call(state);
5602}
5603
Eric Laurentd60560a2015-04-10 11:31:20 -07005604void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc)
5605{
5606 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
5607 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
5608 if (sourceDesc->mDevice->equals(deviceDesc)) {
5609 ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle());
5610 stopAudioSource(sourceDesc->getHandle());
5611 }
5612 }
5613
5614 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
5615 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
5616 bool release = false;
5617 for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) {
5618 const struct audio_port_config *source = &patchDesc->mPatch.sources[j];
5619 if (source->type == AUDIO_PORT_TYPE_DEVICE &&
5620 source->ext.device.type == deviceDesc->type()) {
5621 release = true;
5622 }
5623 }
5624 for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) {
5625 const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j];
5626 if (sink->type == AUDIO_PORT_TYPE_DEVICE &&
5627 sink->ext.device.type == deviceDesc->type()) {
5628 release = true;
5629 }
5630 }
5631 if (release) {
5632 ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle);
5633 releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid);
5634 }
5635 }
5636}
5637
Phil Burk09bc4612016-02-24 15:58:15 -08005638// Modify the list of surround sound formats supported.
Phil Burk0709b0a2016-03-31 12:54:57 -07005639void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) {
5640 FormatVector &formats = *formatsPtr;
Phil Burk07ac1142016-03-25 13:39:29 -07005641 // TODO Set this based on Config properties.
5642 const bool alwaysForceAC3 = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005643
5644 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5645 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
Phil Burk0709b0a2016-03-31 12:54:57 -07005646 ALOGD("%s: forced use = %d", __FUNCTION__, forceUse);
Phil Burk09bc4612016-02-24 15:58:15 -08005647
5648 // Analyze original support for various formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005649 bool supportsAC3 = false;
5650 bool supportsOtherSurround = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005651 bool supportsIEC61937 = false;
5652 for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) {
5653 audio_format_t format = formats[formatIndex];
Phil Burk09bc4612016-02-24 15:58:15 -08005654 switch (format) {
5655 case AUDIO_FORMAT_AC3:
Phil Burk07ac1142016-03-25 13:39:29 -07005656 supportsAC3 = true;
5657 break;
Phil Burk09bc4612016-02-24 15:58:15 -08005658 case AUDIO_FORMAT_E_AC3:
5659 case AUDIO_FORMAT_DTS:
5660 case AUDIO_FORMAT_DTS_HD:
Phil Burk07ac1142016-03-25 13:39:29 -07005661 supportsOtherSurround = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005662 break;
5663 case AUDIO_FORMAT_IEC61937:
5664 supportsIEC61937 = true;
5665 break;
5666 default:
5667 break;
5668 }
5669 }
Phil Burk09bc4612016-02-24 15:58:15 -08005670
5671 // Modify formats based on surround preferences.
5672 // If NEVER, remove support for surround formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005673 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5674 if (supportsAC3 || supportsOtherSurround || supportsIEC61937) {
5675 // Remove surround sound related formats.
5676 for (size_t formatIndex = 0; formatIndex < formats.size(); ) {
5677 audio_format_t format = formats[formatIndex];
5678 switch(format) {
5679 case AUDIO_FORMAT_AC3:
5680 case AUDIO_FORMAT_E_AC3:
5681 case AUDIO_FORMAT_DTS:
5682 case AUDIO_FORMAT_DTS_HD:
5683 case AUDIO_FORMAT_IEC61937:
Phil Burk07ac1142016-03-25 13:39:29 -07005684 formats.removeAt(formatIndex);
5685 break;
5686 default:
5687 formatIndex++; // keep it
5688 break;
5689 }
Phil Burk09bc4612016-02-24 15:58:15 -08005690 }
Phil Burk07ac1142016-03-25 13:39:29 -07005691 supportsAC3 = false;
5692 supportsOtherSurround = false;
5693 supportsIEC61937 = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005694 }
Phil Burk07ac1142016-03-25 13:39:29 -07005695 } else { // AUTO or ALWAYS
5696 // Most TVs support AC3 even if they do not report it in the EDID.
5697 if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS))
5698 && !supportsAC3) {
5699 formats.add(AUDIO_FORMAT_AC3);
5700 supportsAC3 = true;
5701 }
5702
5703 // If ALWAYS, add support for raw surround formats if all are missing.
5704 // This assumes that if any of these formats are reported by the HAL
5705 // then the report is valid and should not be modified.
5706 if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)
5707 && !supportsOtherSurround) {
5708 formats.add(AUDIO_FORMAT_E_AC3);
5709 formats.add(AUDIO_FORMAT_DTS);
5710 formats.add(AUDIO_FORMAT_DTS_HD);
5711 supportsOtherSurround = true;
5712 }
5713
5714 // Add support for IEC61937 if any raw surround supported.
5715 // The HAL could do this but add it here, just in case.
5716 if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) {
5717 formats.add(AUDIO_FORMAT_IEC61937);
5718 supportsIEC61937 = true;
5719 }
Phil Burk09bc4612016-02-24 15:58:15 -08005720 }
Phil Burk0709b0a2016-03-31 12:54:57 -07005721}
5722
5723// Modify the list of channel masks supported.
5724void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) {
5725 ChannelsVector &channelMasks = *channelMasksPtr;
5726 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5727 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
5728
5729 // If NEVER, then remove support for channelMasks > stereo.
5730 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5731 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) {
5732 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5733 if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) {
5734 ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask);
5735 channelMasks.removeAt(maskIndex);
5736 } else {
5737 maskIndex++;
5738 }
5739 }
5740 // If ALWAYS, then make sure we at least support 5.1
5741 } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) {
5742 bool supports5dot1 = false;
5743 // Are there any channel masks that can be considered "surround"?
5744 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); maskIndex++) {
5745 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5746 if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) {
5747 supports5dot1 = true;
5748 break;
5749 }
5750 }
5751 // If not then add 5.1 support.
5752 if (!supports5dot1) {
5753 channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1);
5754 ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__);
5755 }
Phil Burk09bc4612016-02-24 15:58:15 -08005756 }
5757}
5758
Phil Burk00eeb322016-03-31 12:41:00 -07005759void AudioPolicyManager::updateAudioProfiles(audio_devices_t device,
5760 audio_io_handle_t ioHandle,
François Gaffie112b0af2015-11-19 16:13:25 +01005761 AudioProfileVector &profiles)
5762{
5763 String8 reply;
Phil Burk0709b0a2016-03-31 12:54:57 -07005764
François Gaffie112b0af2015-11-19 16:13:25 +01005765 // Format MUST be checked first to update the list of AudioProfile
5766 if (profiles.hasDynamicFormat()) {
Mikhail Naganov388360c2016-10-17 17:09:41 -07005767 reply = mpClientInterface->getParameters(
5768 ioHandle, String8(AudioParameter::keyStreamSupportedFormats));
Phil Burk0709b0a2016-03-31 12:54:57 -07005769 ALOGV("%s: supported formats %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005770 AudioParameter repliedParameters(reply);
5771 if (repliedParameters.get(
Mikhail Naganov388360c2016-10-17 17:09:41 -07005772 String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) {
François Gaffie112b0af2015-11-19 16:13:25 +01005773 ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__);
5774 return;
5775 }
Phil Burk09bc4612016-02-24 15:58:15 -08005776 FormatVector formats = formatsFromString(reply.string());
Phil Burk00eeb322016-03-31 12:41:00 -07005777 if (device == AUDIO_DEVICE_OUT_HDMI) {
Phil Burk0709b0a2016-03-31 12:54:57 -07005778 filterSurroundFormats(&formats);
Phil Burk00eeb322016-03-31 12:41:00 -07005779 }
Phil Burk09bc4612016-02-24 15:58:15 -08005780 profiles.setFormats(formats);
François Gaffie112b0af2015-11-19 16:13:25 +01005781 }
5782 const FormatVector &supportedFormats = profiles.getSupportedFormats();
5783
Eric Laurent20eb3a42016-01-26 18:39:17 -08005784 for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) {
François Gaffie112b0af2015-11-19 16:13:25 +01005785 audio_format_t format = supportedFormats[formatIndex];
Eric Laurent20eb3a42016-01-26 18:39:17 -08005786 ChannelsVector channelMasks;
5787 SampleRateVector samplingRates;
François Gaffie112b0af2015-11-19 16:13:25 +01005788 AudioParameter requestedParameters;
Mikhail Naganov388360c2016-10-17 17:09:41 -07005789 requestedParameters.addInt(String8(AudioParameter::keyFormat), format);
François Gaffie112b0af2015-11-19 16:13:25 +01005790
5791 if (profiles.hasDynamicRateFor(format)) {
Mikhail Naganov388360c2016-10-17 17:09:41 -07005792 reply = mpClientInterface->getParameters(
5793 ioHandle,
5794 requestedParameters.toString() + ";" +
5795 AudioParameter::keyStreamSupportedSamplingRates);
François Gaffie112b0af2015-11-19 16:13:25 +01005796 ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005797 AudioParameter repliedParameters(reply);
5798 if (repliedParameters.get(
Mikhail Naganov388360c2016-10-17 17:09:41 -07005799 String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) {
Eric Laurent62e4bc52016-02-02 18:37:28 -08005800 samplingRates = samplingRatesFromString(reply.string());
François Gaffie112b0af2015-11-19 16:13:25 +01005801 }
5802 }
5803 if (profiles.hasDynamicChannelsFor(format)) {
5804 reply = mpClientInterface->getParameters(ioHandle,
5805 requestedParameters.toString() + ";" +
Mikhail Naganov388360c2016-10-17 17:09:41 -07005806 AudioParameter::keyStreamSupportedChannels);
François Gaffie112b0af2015-11-19 16:13:25 +01005807 ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005808 AudioParameter repliedParameters(reply);
5809 if (repliedParameters.get(
Mikhail Naganov388360c2016-10-17 17:09:41 -07005810 String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) {
Eric Laurent62e4bc52016-02-02 18:37:28 -08005811 channelMasks = channelMasksFromString(reply.string());
Phil Burk0709b0a2016-03-31 12:54:57 -07005812 if (device == AUDIO_DEVICE_OUT_HDMI) {
5813 filterSurroundChannelMasks(&channelMasks);
5814 }
François Gaffie112b0af2015-11-19 16:13:25 +01005815 }
5816 }
Eric Laurent20eb3a42016-01-26 18:39:17 -08005817 profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates));
François Gaffie112b0af2015-11-19 16:13:25 +01005818 }
5819}
Eric Laurentd60560a2015-04-10 11:31:20 -07005820
Eric Laurente552edb2014-03-10 17:42:56 -07005821}; // namespace android