blob: 42b85e38acd874a2297891c30b8f120855806aad [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 Laurente552edb2014-03-10 17:42:56 -0700641
François Gaffie2110e042015-03-24 08:41:51 +0100642 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
643 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
644 return;
Eric Laurente552edb2014-03-10 17:42:56 -0700645 }
François Gaffie2110e042015-03-24 08:41:51 +0100646 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
647 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
648 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
Eric Laurente552edb2014-03-10 17:42:56 -0700649
650 // check for device and output changes triggered by new force usage
651 checkA2dpSuspend();
652 checkOutputForAllStrategies();
653 updateDevicesAndOutputs();
Phil Burk09bc4612016-02-24 15:58:15 -0800654
Eric Laurentdc462862016-07-19 12:29:53 -0700655 //FIXME: workaround for truncated touch sounds
656 // to be removed when the problem is handled by system UI
657 uint32_t delayMs = 0;
658 uint32_t waitMs = 0;
659 if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) {
660 delayMs = TOUCH_SOUND_FIXED_DELAY_MS;
661 }
Eric Laurent87ffa392015-05-22 10:32:38 -0700662 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700663 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
Eric Laurentdc462862016-07-19 12:29:53 -0700664 waitMs = updateCallRouting(newDevice, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700665 }
Eric Laurente552edb2014-03-10 17:42:56 -0700666 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700667 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
668 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
669 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
Eric Laurentdc462862016-07-19 12:29:53 -0700670 waitMs = setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE),
671 delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700672 }
Eric Laurente552edb2014-03-10 17:42:56 -0700673 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
Eric Laurentdc462862016-07-19 12:29:53 -0700674 applyStreamVolumes(outputDesc, newDevice, waitMs, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700675 }
676 }
677
Eric Laurentfb66dd92016-01-28 18:32:03 -0800678 Vector<sp <AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
679 for (size_t i = 0; i < activeInputs.size(); i++) {
680 sp<AudioInputDescriptor> activeDesc = activeInputs[i];
681 audio_devices_t newDevice = getNewInputDevice(activeDesc);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700682 // Force new input selection if the new device can not be reached via current input
Eric Laurentfb66dd92016-01-28 18:32:03 -0800683 if (activeDesc->mProfile->getSupportedDevices().types() &
684 (newDevice & ~AUDIO_DEVICE_BIT_IN)) {
685 setInputDevice(activeDesc->mIoHandle, newDevice);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700686 } else {
Eric Laurentfb66dd92016-01-28 18:32:03 -0800687 closeInput(activeDesc->mIoHandle);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700688 }
Eric Laurente552edb2014-03-10 17:42:56 -0700689 }
Eric Laurente552edb2014-03-10 17:42:56 -0700690}
691
Eric Laurente0720872014-03-11 09:30:41 -0700692void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700693{
694 ALOGV("setSystemProperty() property %s, value %s", property, value);
695}
696
697// Find a direct output profile compatible with the parameters passed, even if the input flags do
698// not explicitly request a direct output
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800699sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700700 audio_devices_t device,
701 uint32_t samplingRate,
702 audio_format_t format,
703 audio_channel_mask_t channelMask,
704 audio_output_flags_t flags)
705{
Eric Laurent861a6282015-05-18 15:40:16 -0700706 // only retain flags that will drive the direct output profile selection
707 // if explicitly requested
708 static const uint32_t kRelevantFlags =
709 (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
710 flags =
711 (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT);
712
713 sp<IOProfile> profile;
714
Eric Laurente552edb2014-03-10 17:42:56 -0700715 for (size_t i = 0; i < mHwModules.size(); i++) {
716 if (mHwModules[i]->mHandle == 0) {
717 continue;
718 }
719 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent861a6282015-05-18 15:40:16 -0700720 sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j];
721 if (!curProfile->isCompatibleProfile(device, String8(""),
Andy Hungf129b032015-04-07 13:45:50 -0700722 samplingRate, NULL /*updatedSamplingRate*/,
723 format, NULL /*updatedFormat*/,
724 channelMask, NULL /*updatedChannelMask*/,
Eric Laurent861a6282015-05-18 15:40:16 -0700725 flags)) {
726 continue;
727 }
728 // reject profiles not corresponding to a device currently available
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100729 if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) {
Eric Laurent861a6282015-05-18 15:40:16 -0700730 continue;
731 }
732 // if several profiles are compatible, give priority to one with offload capability
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100733 if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) {
Eric Laurent861a6282015-05-18 15:40:16 -0700734 continue;
735 }
736 profile = curProfile;
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100737 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent861a6282015-05-18 15:40:16 -0700738 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700739 }
Eric Laurente552edb2014-03-10 17:42:56 -0700740 }
741 }
Eric Laurent861a6282015-05-18 15:40:16 -0700742 return profile;
Eric Laurente552edb2014-03-10 17:42:56 -0700743}
744
Eric Laurente0720872014-03-11 09:30:41 -0700745audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +0100746 uint32_t samplingRate,
747 audio_format_t format,
748 audio_channel_mask_t channelMask,
749 audio_output_flags_t flags,
750 const audio_offload_info_t *offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -0700751{
Eric Laurent3b73df72014-03-11 09:06:29 -0700752 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700753 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
754 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
755 device, stream, samplingRate, format, channelMask, flags);
756
Kevin Rocard551061a2017-02-06 15:59:29 -0800757 return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE, uid_t{0} /*Invalid uid*/,
Eric Laurente83b55d2014-11-14 10:06:21 -0800758 stream, samplingRate,format, channelMask,
759 flags, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700760}
761
Eric Laurente83b55d2014-11-14 10:06:21 -0800762status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
763 audio_io_handle_t *output,
764 audio_session_t session,
765 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700766 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800767 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800768 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700769 audio_port_handle_t selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800770 audio_port_handle_t *portId)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700771{
Eric Laurente83b55d2014-11-14 10:06:21 -0800772 audio_attributes_t attributes;
773 if (attr != NULL) {
774 if (!isValidAttributes(attr)) {
775 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
776 attr->usage, attr->content_type, attr->flags,
777 attr->tags);
778 return BAD_VALUE;
779 }
780 attributes = *attr;
781 } else {
782 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
783 ALOGE("getOutputForAttr(): invalid stream type");
784 return BAD_VALUE;
785 }
786 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700787 }
Eric Laurent20b9ef02016-12-05 11:03:16 -0800788
789 // TODO: check for existing client for this port ID
790 if (*portId == AUDIO_PORT_HANDLE_NONE) {
791 *portId = AudioPort::getNextUniqueId();
792 }
793
Eric Laurentc75307b2015-03-17 15:29:32 -0700794 sp<SwAudioOutputDescriptor> desc;
Jean-Michel Trivie8deced2016-02-11 12:50:39 -0800795 if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) {
François Gaffie036e1e92015-03-19 10:16:24 +0100796 ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr");
Eric Laurent20b9ef02016-12-05 11:03:16 -0800797 if (!audio_has_proportional_frames(config->format)) {
François Gaffie036e1e92015-03-19 10:16:24 +0100798 return BAD_VALUE;
Eric Laurent275e8e92014-11-30 15:14:47 -0800799 }
François Gaffie036e1e92015-03-19 10:16:24 +0100800 *stream = streamTypefromAttributesInt(&attributes);
801 *output = desc->mIoHandle;
802 ALOGV("getOutputForAttr() returns output %d", *output);
803 return NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -0800804 }
805 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
806 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
807 return BAD_VALUE;
808 }
809
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700810 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x"
811 " session %d selectedDeviceId %d",
812 attributes.usage, attributes.content_type, attributes.tags, attributes.flags,
813 session, selectedDeviceId);
814
815 *stream = streamTypefromAttributesInt(&attributes);
816
817 // Explicit routing?
818 sp<DeviceDescriptor> deviceDesc;
819 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
820 if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) {
821 deviceDesc = mAvailableOutputDevices[i];
822 break;
823 }
824 }
825 mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700826
Eric Laurente83b55d2014-11-14 10:06:21 -0800827 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700828 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700829
Eric Laurente83b55d2014-11-14 10:06:21 -0800830 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700831 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
832 }
833
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700834 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Eric Laurent20b9ef02016-12-05 11:03:16 -0800835 device, config->sample_rate, config->format, config->channel_mask, flags);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700836
Kevin Rocard551061a2017-02-06 15:59:29 -0800837 *output = getOutputForDevice(device, session, uid, *stream,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800838 config->sample_rate, config->format, config->channel_mask,
839 flags, &config->offload_info);
Eric Laurente83b55d2014-11-14 10:06:21 -0800840 if (*output == AUDIO_IO_HANDLE_NONE) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700841 mOutputRoutes.removeRoute(session);
Eric Laurente83b55d2014-11-14 10:06:21 -0800842 return INVALID_OPERATION;
843 }
Paul McLeanaa981192015-03-21 09:55:15 -0700844
Eric Laurente83b55d2014-11-14 10:06:21 -0800845 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700846}
847
848audio_io_handle_t AudioPolicyManager::getOutputForDevice(
849 audio_devices_t device,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800850 audio_session_t session __unused,
Kevin Rocard551061a2017-02-06 15:59:29 -0800851 uid_t clientUid,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700852 audio_stream_type_t stream,
853 uint32_t samplingRate,
854 audio_format_t format,
855 audio_channel_mask_t channelMask,
856 audio_output_flags_t flags,
857 const audio_offload_info_t *offloadInfo)
858{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700859 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700860 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700861
Eric Laurente552edb2014-03-10 17:42:56 -0700862#ifdef AUDIO_POLICY_TEST
863 if (mCurOutput != 0) {
864 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
865 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
866
867 if (mTestOutputs[mCurOutput] == 0) {
868 ALOGV("getOutput() opening test output");
Eric Laurentc75307b2015-03-17 15:29:32 -0700869 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
870 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700871 outputDesc->mDevice = mTestDevice;
Eric Laurente552edb2014-03-10 17:42:56 -0700872 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700873 outputDesc->mFlags =
874 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700875 outputDesc->mRefCount[stream] = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700876 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
877 config.sample_rate = mTestSamplingRate;
878 config.channel_mask = mTestChannels;
879 config.format = mTestFormat;
Phil Burk77cce802014-08-04 16:18:15 -0700880 if (offloadInfo != NULL) {
881 config.offload_info = *offloadInfo;
882 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700883 status = mpClientInterface->openOutput(0,
884 &mTestOutputs[mCurOutput],
885 &config,
886 &outputDesc->mDevice,
887 String8(""),
888 &outputDesc->mLatency,
889 outputDesc->mFlags);
890 if (status == NO_ERROR) {
891 outputDesc->mSamplingRate = config.sample_rate;
892 outputDesc->mFormat = config.format;
893 outputDesc->mChannelMask = config.channel_mask;
Eric Laurente552edb2014-03-10 17:42:56 -0700894 AudioParameter outputCmd = AudioParameter();
895 outputCmd.addInt(String8("set_id"),mCurOutput);
896 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
897 addOutput(mTestOutputs[mCurOutput], outputDesc);
898 }
899 }
900 return mTestOutputs[mCurOutput];
901 }
902#endif //AUDIO_POLICY_TEST
903
904 // open a direct output if required by specified parameters
905 //force direct flag if offload flag is set: offloading implies a direct output stream
906 // and all common behaviors are driven by checking only the direct flag
907 // this should normally be set appropriately in the policy configuration file
908 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700909 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700910 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700911 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
912 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
913 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800914 // only allow deep buffering for music stream type
915 if (stream != AUDIO_STREAM_MUSIC) {
916 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Ravi Kumar Alamanda439e4ed2015-04-03 12:13:21 -0700917 } else if (/* stream == AUDIO_STREAM_MUSIC && */
918 flags == AUDIO_OUTPUT_FLAG_NONE &&
919 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
920 // use DEEP_BUFFER as default output for music stream type
921 flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Eric Laurente83b55d2014-11-14 10:06:21 -0800922 }
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700923 if (stream == AUDIO_STREAM_TTS) {
924 flags = AUDIO_OUTPUT_FLAG_TTS;
925 }
Eric Laurente552edb2014-03-10 17:42:56 -0700926
Eric Laurentb732cf52014-09-24 19:08:21 -0700927 sp<IOProfile> profile;
928
929 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700930 // and not explicitly requested
931 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
Glenn Kasten05ddca52016-02-11 08:17:12 -0800932 audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700933 audio_channel_count_from_out_mask(channelMask) <= 2) {
934 goto non_direct_output;
935 }
936
Andy Hung2ddee192015-12-18 17:34:44 -0800937 // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled.
938 // This prevents creating an offloaded track and tearing it down immediately after start
939 // when audioflinger detects there is an active non offloadable effect.
Eric Laurente552edb2014-03-10 17:42:56 -0700940 // FIXME: We should check the audio session here but we do not have it in this context.
941 // This may prevent offloading in rare situations where effects are left active by apps
942 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700943
Eric Laurente552edb2014-03-10 17:42:56 -0700944 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
Andy Hung2ddee192015-12-18 17:34:44 -0800945 !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700946 profile = getProfileForDirectOutput(device,
947 samplingRate,
948 format,
949 channelMask,
950 (audio_output_flags_t)flags);
951 }
952
Eric Laurent1c333e22014-05-20 10:48:17 -0700953 if (profile != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700954 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700955
956 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700957 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700958 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
959 outputDesc = desc;
Kevin Rocard551061a2017-02-06 15:59:29 -0800960 // reuse direct output if currently open by the same client
961 // and configured with same parameters
Eric Laurente552edb2014-03-10 17:42:56 -0700962 if ((samplingRate == outputDesc->mSamplingRate) &&
Kevin Rocard551061a2017-02-06 15:59:29 -0800963 audio_formats_match(format, outputDesc->mFormat) &&
964 (channelMask == outputDesc->mChannelMask)) {
965 if (clientUid == outputDesc->mDirectClientUid) {
966 outputDesc->mDirectOpenCount++;
967 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
968 return mOutputs.keyAt(i);
969 } else {
970 ALOGV("getOutput() do not reuse direct output because current client (%ld) "
971 "is not the same as requesting client (%ld)",
972 (long)outputDesc->mDirectClientUid, (long)clientUid);
973 goto non_direct_output;
974 }
Eric Laurente552edb2014-03-10 17:42:56 -0700975 }
976 }
977 }
978 // close direct output if currently open and configured with different parameters
979 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700980 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700981 }
Eric Laurent861a6282015-05-18 15:40:16 -0700982
983 // if the selected profile is offloaded and no offload info was specified,
984 // create a default one
985 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100986 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
Eric Laurent861a6282015-05-18 15:40:16 -0700987 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
988 defaultOffloadInfo.sample_rate = samplingRate;
989 defaultOffloadInfo.channel_mask = channelMask;
990 defaultOffloadInfo.format = format;
991 defaultOffloadInfo.stream_type = stream;
992 defaultOffloadInfo.bit_rate = 0;
993 defaultOffloadInfo.duration_us = -1;
994 defaultOffloadInfo.has_video = true; // conservative
995 defaultOffloadInfo.is_streaming = true; // likely
996 offloadInfo = &defaultOffloadInfo;
997 }
998
Eric Laurentc75307b2015-03-17 15:29:32 -0700999 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07001000 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -07001001 outputDesc->mLatency = 0;
Eric Laurent861a6282015-05-18 15:40:16 -07001002 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001003 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1004 config.sample_rate = samplingRate;
1005 config.channel_mask = channelMask;
1006 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -07001007 if (offloadInfo != NULL) {
1008 config.offload_info = *offloadInfo;
1009 }
Eric Laurent322b4d22015-04-03 15:57:54 -07001010 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07001011 &output,
1012 &config,
1013 &outputDesc->mDevice,
1014 String8(""),
1015 &outputDesc->mLatency,
1016 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001017
1018 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -07001019 if (status != NO_ERROR ||
1020 (samplingRate != 0 && samplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -08001021 (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) ||
Eric Laurentcf2c0212014-07-25 16:20:43 -07001022 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -07001023 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1024 "format %d %d, channelMask %04x %04x", output, samplingRate,
1025 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1026 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001027 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07001028 mpClientInterface->closeOutput(output);
1029 }
Eric Laurenta82797f2015-01-30 11:49:43 -08001030 // fall back to mixer output if possible when the direct output could not be open
Glenn Kasten05ddca52016-02-11 08:17:12 -08001031 if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) {
Eric Laurenta82797f2015-01-30 11:49:43 -08001032 goto non_direct_output;
1033 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001034 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001035 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001036 outputDesc->mSamplingRate = config.sample_rate;
1037 outputDesc->mChannelMask = config.channel_mask;
1038 outputDesc->mFormat = config.format;
1039 outputDesc->mRefCount[stream] = 0;
1040 outputDesc->mStopTime[stream] = 0;
1041 outputDesc->mDirectOpenCount = 1;
Kevin Rocard551061a2017-02-06 15:59:29 -08001042 outputDesc->mDirectClientUid = clientUid;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001043
Eric Laurente552edb2014-03-10 17:42:56 -07001044 audio_io_handle_t srcOutput = getOutputForEffect();
1045 addOutput(output, outputDesc);
1046 audio_io_handle_t dstOutput = getOutputForEffect();
1047 if (dstOutput == output) {
1048 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1049 }
1050 mPreviousOutputs = mOutputs;
1051 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -07001052 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001053 return output;
1054 }
1055
Eric Laurentb732cf52014-09-24 19:08:21 -07001056non_direct_output:
Eric Laurent14cbfca2016-03-17 09:42:16 -07001057
1058 // A request for HW A/V sync cannot fallback to a mixed output because time
1059 // stamps are embedded in audio data
1060 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1061 return AUDIO_IO_HANDLE_NONE;
1062 }
1063
Eric Laurente552edb2014-03-10 17:42:56 -07001064 // ignoring channel mask due to downmix capability in mixer
1065
1066 // open a non direct output
1067
1068 // for non direct outputs, only PCM is supported
1069 if (audio_is_linear_pcm(format)) {
1070 // get which output is suitable for the specified stream. The actual
1071 // routing change will happen when startOutput() will be called
1072 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1073
Eric Laurent8838a382014-09-08 16:44:28 -07001074 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1075 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1076 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -07001077 }
1078 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1079 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1080
Paul McLeanaa981192015-03-21 09:55:15 -07001081 ALOGV(" getOutputForDevice() returns output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -07001082
1083 return output;
1084}
1085
Eric Laurente0720872014-03-11 09:30:41 -07001086audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -07001087 audio_output_flags_t flags,
1088 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -07001089{
1090 // select one output among several that provide a path to a particular device or set of
1091 // devices (the list was previously build by getOutputsForDevice()).
1092 // The priority is as follows:
1093 // 1: the output with the highest number of requested policy flags
Eric Laurente6930022016-02-11 10:20:40 -08001094 // 2: the output with the bit depth the closest to the requested one
1095 // 3: the primary output
1096 // 4: the first output in the list
Eric Laurente552edb2014-03-10 17:42:56 -07001097
1098 if (outputs.size() == 0) {
1099 return 0;
1100 }
1101 if (outputs.size() == 1) {
1102 return outputs[0];
1103 }
1104
1105 int maxCommonFlags = 0;
Eric Laurente6930022016-02-11 10:20:40 -08001106 audio_io_handle_t outputForFlags = 0;
1107 audio_io_handle_t outputForPrimary = 0;
1108 audio_io_handle_t outputForFormat = 0;
1109 audio_format_t bestFormat = AUDIO_FORMAT_INVALID;
1110 audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07001111
1112 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001113 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -07001114 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -07001115 // if a valid format is specified, skip output if not compatible
1116 if (format != AUDIO_FORMAT_INVALID) {
1117 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente6930022016-02-11 10:20:40 -08001118 if (!audio_formats_match(format, outputDesc->mFormat)) {
Eric Laurent8838a382014-09-08 16:44:28 -07001119 continue;
1120 }
1121 } else if (!audio_is_linear_pcm(format)) {
1122 continue;
1123 }
Eric Laurente6930022016-02-11 10:20:40 -08001124 if (AudioPort::isBetterFormatMatch(
1125 outputDesc->mFormat, bestFormat, format)) {
1126 outputForFormat = outputs[i];
1127 bestFormat = outputDesc->mFormat;
1128 }
Eric Laurent8838a382014-09-08 16:44:28 -07001129 }
1130
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001131 int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags);
Eric Laurente6930022016-02-11 10:20:40 -08001132 if (commonFlags >= maxCommonFlags) {
1133 if (commonFlags == maxCommonFlags) {
1134 if (AudioPort::isBetterFormatMatch(
1135 outputDesc->mFormat, bestFormatForFlags, format)) {
1136 outputForFlags = outputs[i];
1137 bestFormatForFlags = outputDesc->mFormat;
1138 }
1139 } else {
1140 outputForFlags = outputs[i];
1141 maxCommonFlags = commonFlags;
1142 bestFormatForFlags = outputDesc->mFormat;
1143 }
Eric Laurente552edb2014-03-10 17:42:56 -07001144 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
1145 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001146 if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurente6930022016-02-11 10:20:40 -08001147 outputForPrimary = outputs[i];
Eric Laurente552edb2014-03-10 17:42:56 -07001148 }
1149 }
1150 }
1151
Eric Laurente6930022016-02-11 10:20:40 -08001152 if (outputForFlags != 0) {
1153 return outputForFlags;
Eric Laurente552edb2014-03-10 17:42:56 -07001154 }
Eric Laurente6930022016-02-11 10:20:40 -08001155 if (outputForFormat != 0) {
1156 return outputForFormat;
1157 }
1158 if (outputForPrimary != 0) {
1159 return outputForPrimary;
Eric Laurente552edb2014-03-10 17:42:56 -07001160 }
1161
1162 return outputs[0];
1163}
1164
Eric Laurente0720872014-03-11 09:30:41 -07001165status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001166 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001167 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001168{
Paul McLeanaa981192015-03-21 09:55:15 -07001169 ALOGV("startOutput() output %d, stream %d, session %d",
1170 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -07001171 ssize_t index = mOutputs.indexOfKey(output);
1172 if (index < 0) {
1173 ALOGW("startOutput() unknown output %d", output);
1174 return BAD_VALUE;
1175 }
1176
Eric Laurentc75307b2015-03-17 15:29:32 -07001177 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
1178
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001179 // Routing?
1180 mOutputRoutes.incRouteActivity(session);
1181
Eric Laurentc75307b2015-03-17 15:29:32 -07001182 audio_devices_t newDevice;
Eric Laurentc40d9692016-04-13 19:14:13 -07001183 AudioMix *policyMix = NULL;
1184 const char *address = NULL;
Eric Laurentc75307b2015-03-17 15:29:32 -07001185 if (outputDesc->mPolicyMix != NULL) {
Eric Laurentc40d9692016-04-13 19:14:13 -07001186 policyMix = outputDesc->mPolicyMix;
1187 address = policyMix->mDeviceAddress.string();
1188 if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
1189 newDevice = policyMix->mDeviceType;
1190 } else {
1191 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
1192 }
Eric Laurent493404d2015-04-21 15:07:36 -07001193 } else if (mOutputRoutes.hasRouteChanged(session)) {
1194 newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001195 checkStrategyRoute(getStrategy(stream), output);
Eric Laurentc75307b2015-03-17 15:29:32 -07001196 } else {
1197 newDevice = AUDIO_DEVICE_NONE;
1198 }
1199
1200 uint32_t delayMs = 0;
1201
Eric Laurentc40d9692016-04-13 19:14:13 -07001202 status_t status = startSource(outputDesc, stream, newDevice, address, &delayMs);
Eric Laurentc75307b2015-03-17 15:29:32 -07001203
1204 if (status != NO_ERROR) {
1205 mOutputRoutes.decRouteActivity(session);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001206 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -07001207 }
1208 // Automatically enable the remote submix input when output is started on a re routing mix
1209 // of type MIX_TYPE_RECORDERS
Eric Laurentc40d9692016-04-13 19:14:13 -07001210 if (audio_is_remote_submix_device(newDevice) && policyMix != NULL &&
1211 policyMix->mMixType == MIX_TYPE_RECORDERS) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001212 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1213 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Eric Laurentc40d9692016-04-13 19:14:13 -07001214 address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001215 "remote-submix");
1216 }
1217
1218 if (delayMs != 0) {
1219 usleep(delayMs * 1000);
1220 }
1221
1222 return status;
1223}
1224
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001225status_t AudioPolicyManager::startSource(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurentc75307b2015-03-17 15:29:32 -07001226 audio_stream_type_t stream,
1227 audio_devices_t device,
Eric Laurentc40d9692016-04-13 19:14:13 -07001228 const char *address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001229 uint32_t *delayMs)
1230{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001231 // cannot start playback of STREAM_TTS if any other output is being used
1232 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001233
1234 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001235 if (stream == AUDIO_STREAM_TTS) {
1236 ALOGV("\t found BEACON stream");
Eric Laurent9459fb02015-08-12 18:36:32 -07001237 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001238 return INVALID_OPERATION;
1239 } else {
1240 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1241 }
1242 } else {
1243 // some playback other than beacon starts
1244 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1245 }
1246
Eric Laurent77305a62016-07-25 16:39:22 -07001247 // force device change if the output is inactive and no audio patch is already present.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001248 // check active before incrementing usage count
Eric Laurent77305a62016-07-25 16:39:22 -07001249 bool force = !outputDesc->isActive() &&
1250 (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001251
Eric Laurente552edb2014-03-10 17:42:56 -07001252 // increment usage count for this stream on the requested output:
1253 // NOTE that the usage count is the same for duplicated output and hardware output which is
1254 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1255 outputDesc->changeRefCount(stream, 1);
1256
Eric Laurent493404d2015-04-21 15:07:36 -07001257 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001258 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001259 if (device == AUDIO_DEVICE_NONE) {
1260 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001261 }
Eric Laurente552edb2014-03-10 17:42:56 -07001262 routing_strategy strategy = getStrategy(stream);
1263 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001264 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1265 (beaconMuteLatency > 0);
1266 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001267 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001268 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001269 if (desc != outputDesc) {
Eric Laurent77305a62016-07-25 16:39:22 -07001270 // force a device change if any other output is:
1271 // - managed by the same hw module
1272 // - has a current device selection that differs from selected device.
1273 // - supports currently selected device
1274 // - has an active audio patch
Eric Laurente552edb2014-03-10 17:42:56 -07001275 // In this case, the audio HAL must receive the new device selection so that it can
1276 // change the device currently selected by the other active output.
1277 if (outputDesc->sharesHwModuleWith(desc) &&
Eric Laurent77305a62016-07-25 16:39:22 -07001278 desc->device() != device &&
1279 desc->supportedDevices() & device &&
1280 desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07001281 force = true;
1282 }
1283 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001284 // a notification so that audio focus effect can propagate, or that a mute/unmute
1285 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001286 uint32_t latency = desc->latency();
1287 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1288 waitMs = latency;
1289 }
1290 }
1291 }
Eric Laurentdc462862016-07-19 12:29:53 -07001292 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force, 0, NULL, address);
Eric Laurente552edb2014-03-10 17:42:56 -07001293
1294 // handle special case for sonification while in call
1295 if (isInCall()) {
1296 handleIncallSonification(stream, true, false);
1297 }
1298
1299 // apply volume rules for current stream and device if necessary
1300 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01001301 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07001302 outputDesc,
1303 device);
Eric Laurente552edb2014-03-10 17:42:56 -07001304
1305 // update the outputs if starting an output with a stream that can affect notification
1306 // routing
1307 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001308
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001309 // force reevaluating accessibility routing when ringtone or alarm starts
1310 if (strategy == STRATEGY_SONIFICATION) {
1311 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1312 }
Eric Laurentdc462862016-07-19 12:29:53 -07001313
1314 if (waitMs > muteWaitMs) {
1315 *delayMs = waitMs - muteWaitMs;
1316 }
Eric Laurente552edb2014-03-10 17:42:56 -07001317 }
Eric Laurentdc462862016-07-19 12:29:53 -07001318
Eric Laurente552edb2014-03-10 17:42:56 -07001319 return NO_ERROR;
1320}
1321
1322
Eric Laurente0720872014-03-11 09:30:41 -07001323status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001324 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001325 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001326{
1327 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1328 ssize_t index = mOutputs.indexOfKey(output);
1329 if (index < 0) {
1330 ALOGW("stopOutput() unknown output %d", output);
1331 return BAD_VALUE;
1332 }
1333
Eric Laurentc75307b2015-03-17 15:29:32 -07001334 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001335
Eric Laurentc75307b2015-03-17 15:29:32 -07001336 if (outputDesc->mRefCount[stream] == 1) {
1337 // Automatically disable the remote submix input when output is stopped on a
1338 // re routing mix of type MIX_TYPE_RECORDERS
1339 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1340 outputDesc->mPolicyMix != NULL &&
1341 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1342 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1343 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001344 outputDesc->mPolicyMix->mDeviceAddress,
Eric Laurentc75307b2015-03-17 15:29:32 -07001345 "remote-submix");
1346 }
1347 }
1348
1349 // Routing?
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001350 bool forceDeviceUpdate = false;
Eric Laurentc75307b2015-03-17 15:29:32 -07001351 if (outputDesc->mRefCount[stream] > 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001352 int activityCount = mOutputRoutes.decRouteActivity(session);
1353 forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0));
1354
1355 if (forceDeviceUpdate) {
1356 checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE);
1357 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001358 }
1359
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001360 return stopSource(outputDesc, stream, forceDeviceUpdate);
Eric Laurentc75307b2015-03-17 15:29:32 -07001361}
1362
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001363status_t AudioPolicyManager::stopSource(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001364 audio_stream_type_t stream,
1365 bool forceDeviceUpdate)
Eric Laurentc75307b2015-03-17 15:29:32 -07001366{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001367 // always handle stream stop, check which stream type is stopping
1368 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1369
Eric Laurente552edb2014-03-10 17:42:56 -07001370 // handle special case for sonification while in call
1371 if (isInCall()) {
1372 handleIncallSonification(stream, false, false);
1373 }
1374
1375 if (outputDesc->mRefCount[stream] > 0) {
1376 // decrement usage count of this stream on the output
1377 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001378
Eric Laurente552edb2014-03-10 17:42:56 -07001379 // store time at which the stream was stopped - see isStreamActive()
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001380 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
Eric Laurente552edb2014-03-10 17:42:56 -07001381 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001382 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001383 // delay the device switch by twice the latency because stopOutput() is executed when
1384 // the track stop() command is received and at that time the audio track buffer can
1385 // still contain data that needs to be drained. The latency only covers the audio HAL
1386 // and kernel buffers. Also the latency does not always include additional delay in the
1387 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001388 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001389
1390 // force restoring the device selection on other active outputs if it differs from the
1391 // one being selected for this output
Eric Laurent57de36c2016-09-28 16:59:11 -07001392 uint32_t delayMs = outputDesc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -07001393 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001394 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001395 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001396 desc->isActive() &&
1397 outputDesc->sharesHwModuleWith(desc) &&
1398 (newDevice != desc->device())) {
Haynes Mathew George11c499a2016-08-26 12:08:25 -07001399 audio_devices_t newDevice2 = getNewOutputDevice(desc, false /*fromCache*/);
1400 bool force = desc->device() != newDevice2;
Eric Laurentc75307b2015-03-17 15:29:32 -07001401 setOutputDevice(desc,
Haynes Mathew George11c499a2016-08-26 12:08:25 -07001402 newDevice2,
1403 force,
Eric Laurent57de36c2016-09-28 16:59:11 -07001404 delayMs);
1405 // re-apply device specific volume if not done by setOutputDevice()
1406 if (!force) {
1407 applyStreamVolumes(desc, newDevice2, delayMs);
1408 }
Eric Laurente552edb2014-03-10 17:42:56 -07001409 }
1410 }
1411 // update the outputs if stopping one with a stream that can affect notification routing
1412 handleNotificationRoutingForStream(stream);
1413 }
1414 return NO_ERROR;
1415 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001416 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001417 return INVALID_OPERATION;
1418 }
1419}
1420
Eric Laurente83b55d2014-11-14 10:06:21 -08001421void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001422 audio_stream_type_t stream __unused,
1423 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001424{
1425 ALOGV("releaseOutput() %d", output);
1426 ssize_t index = mOutputs.indexOfKey(output);
1427 if (index < 0) {
1428 ALOGW("releaseOutput() releasing unknown output %d", output);
1429 return;
1430 }
1431
1432#ifdef AUDIO_POLICY_TEST
1433 int testIndex = testOutputIndex(output);
1434 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001435 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001436 if (outputDesc->isActive()) {
1437 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001438 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001439 mTestOutputs[testIndex] = 0;
1440 }
1441 return;
1442 }
1443#endif //AUDIO_POLICY_TEST
1444
Paul McLeanaa981192015-03-21 09:55:15 -07001445 // Routing
1446 mOutputRoutes.removeRoute(session);
1447
Eric Laurentc75307b2015-03-17 15:29:32 -07001448 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001449 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001450 if (desc->mDirectOpenCount <= 0) {
1451 ALOGW("releaseOutput() invalid open count %d for output %d",
1452 desc->mDirectOpenCount, output);
1453 return;
1454 }
1455 if (--desc->mDirectOpenCount == 0) {
1456 closeOutput(output);
1457 // If effects where present on the output, audioflinger moved them to the primary
1458 // output by default: move them back to the appropriate output.
1459 audio_io_handle_t dstOutput = getOutputForEffect();
Eric Laurent87ffa392015-05-22 10:32:38 -07001460 if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001461 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
1462 mPrimaryOutput->mIoHandle, dstOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07001463 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001464 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001465 }
1466 }
1467}
1468
1469
Eric Laurentcaf7f482014-11-25 17:50:47 -08001470status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1471 audio_io_handle_t *input,
1472 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001473 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001474 const audio_config_base_t *config,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001475 audio_input_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -06001476 audio_port_handle_t selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001477 input_type_t *inputType,
1478 audio_port_handle_t *portId)
Eric Laurente552edb2014-03-10 17:42:56 -07001479{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001480 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1481 "session %d, flags %#x",
Eric Laurent20b9ef02016-12-05 11:03:16 -08001482 attr->source, config->sample_rate, config->format, config->channel_mask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001483
Eric Laurentcaf7f482014-11-25 17:50:47 -08001484 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001485 *inputType = API_INPUT_INVALID;
Eric Laurentfb66dd92016-01-28 18:32:03 -08001486
Eric Laurent275e8e92014-11-30 15:14:47 -08001487 audio_devices_t device;
1488 // handle legacy remote submix case where the address was not always specified
1489 String8 address = String8("");
Eric Laurentc447ded2015-01-06 08:47:05 -08001490 audio_source_t inputSource = attr->source;
1491 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001492 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001493
Eric Laurentc447ded2015-01-06 08:47:05 -08001494 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1495 inputSource = AUDIO_SOURCE_MIC;
1496 }
1497 halInputSource = inputSource;
1498
Eric Laurent20b9ef02016-12-05 11:03:16 -08001499 // TODO: check for existing client for this port ID
1500 if (*portId == AUDIO_PORT_HANDLE_NONE) {
1501 *portId = AudioPort::getNextUniqueId();
1502 }
1503
Paul McLean466dc8e2015-04-17 13:15:36 -06001504 // Explicit routing?
1505 sp<DeviceDescriptor> deviceDesc;
1506 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1507 if (mAvailableInputDevices[i]->getId() == selectedDeviceId) {
1508 deviceDesc = mAvailableInputDevices[i];
1509 break;
1510 }
1511 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001512 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001513
Eric Laurentc447ded2015-01-06 08:47:05 -08001514 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001515 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001516 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
François Gaffie036e1e92015-03-19 10:16:24 +01001517 if (ret != NO_ERROR) {
1518 return ret;
1519 }
1520 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001521 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1522 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001523 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001524 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001525 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001526 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001527 return BAD_VALUE;
1528 }
Eric Laurentc722f302014-12-10 11:21:49 -08001529 if (policyMix != NULL) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001530 address = policyMix->mDeviceAddress;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001531 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1532 // there is an external policy, but this input is attached to a mix of recorders,
1533 // meaning it receives audio injected into the framework, so the recorder doesn't
1534 // know about it and is therefore considered "legacy"
1535 *inputType = API_INPUT_LEGACY;
1536 } else {
1537 // recording a mix of players defined by an external policy, we're rerouting for
1538 // an external policy
1539 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1540 }
Eric Laurentc722f302014-12-10 11:21:49 -08001541 } else if (audio_is_remote_submix_device(device)) {
1542 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001543 *inputType = API_INPUT_MIX_CAPTURE;
Eric Laurent82db2692015-08-07 13:59:42 -07001544 } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
1545 *inputType = API_INPUT_TELEPHONY_RX;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001546 } else {
1547 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001548 }
Ravi Kumar Alamandab367f5b2015-08-25 08:21:37 -07001549
Eric Laurent599c7582015-12-07 18:05:55 -08001550 }
1551
1552 *input = getInputForDevice(device, address, session, uid, inputSource,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001553 config->sample_rate, config->format, config->channel_mask, flags,
Eric Laurent599c7582015-12-07 18:05:55 -08001554 policyMix);
1555 if (*input == AUDIO_IO_HANDLE_NONE) {
1556 mInputRoutes.removeRoute(session);
1557 return INVALID_OPERATION;
1558 }
Eric Laurent20b9ef02016-12-05 11:03:16 -08001559
Eric Laurent599c7582015-12-07 18:05:55 -08001560 ALOGV("getInputForAttr() returns input type = %d", *inputType);
1561 return NO_ERROR;
1562}
1563
1564
1565audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device,
1566 String8 address,
1567 audio_session_t session,
1568 uid_t uid,
1569 audio_source_t inputSource,
1570 uint32_t samplingRate,
1571 audio_format_t format,
1572 audio_channel_mask_t channelMask,
1573 audio_input_flags_t flags,
1574 AudioMix *policyMix)
1575{
1576 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
1577 audio_source_t halInputSource = inputSource;
1578 bool isSoundTrigger = false;
1579
1580 if (inputSource == AUDIO_SOURCE_HOTWORD) {
1581 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1582 if (index >= 0) {
1583 input = mSoundTriggerSessions.valueFor(session);
1584 isSoundTrigger = true;
1585 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1586 ALOGV("SoundTrigger capture on session %d input %d", session, input);
1587 } else {
1588 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001589 }
1590 }
1591
Andy Hungf129b032015-04-07 13:45:50 -07001592 // find a compatible input profile (not necessarily identical in parameters)
1593 sp<IOProfile> profile;
1594 // samplingRate and flags may be updated by getInputProfile
Glenn Kasten05ddca52016-02-11 08:17:12 -08001595 uint32_t profileSamplingRate = (samplingRate == 0) ? SAMPLE_RATE_HZ_DEFAULT : samplingRate;
Andy Hungf129b032015-04-07 13:45:50 -07001596 audio_format_t profileFormat = format;
1597 audio_channel_mask_t profileChannelMask = channelMask;
1598 audio_input_flags_t profileFlags = flags;
1599 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001600 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001601 profileSamplingRate, profileFormat, profileChannelMask,
1602 profileFlags);
1603 if (profile != 0) {
1604 break; // success
Eric Laurent05067782016-06-01 18:27:28 -07001605 } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) {
1606 profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry
Andy Hungf129b032015-04-07 13:45:50 -07001607 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1608 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1609 } else { // fail
Eric Laurent599c7582015-12-07 18:05:55 -08001610 ALOGW("getInputForDevice() could not find profile for device 0x%X,"
1611 "samplingRate %u, format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001612 device, samplingRate, format, channelMask, flags);
Eric Laurent599c7582015-12-07 18:05:55 -08001613 return input;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001614 }
Eric Laurente552edb2014-03-10 17:42:56 -07001615 }
Glenn Kasten05ddca52016-02-11 08:17:12 -08001616 // Pick input sampling rate if not specified by client
1617 if (samplingRate == 0) {
1618 samplingRate = profileSamplingRate;
1619 }
Eric Laurente552edb2014-03-10 17:42:56 -07001620
Eric Laurent322b4d22015-04-03 15:57:54 -07001621 if (profile->getModuleHandle() == 0) {
1622 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurent599c7582015-12-07 18:05:55 -08001623 return input;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001624 }
1625
Eric Laurent599c7582015-12-07 18:05:55 -08001626 sp<AudioSession> audioSession = new AudioSession(session,
1627 inputSource,
1628 format,
1629 samplingRate,
1630 channelMask,
1631 flags,
1632 uid,
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001633 isSoundTrigger,
1634 policyMix, mpClientInterface);
Eric Laurent599c7582015-12-07 18:05:55 -08001635
Eric Laurent74708e72017-04-07 17:13:42 -07001636// FIXME: disable concurrent capture until UI is ready
1637#if 0
Eric Laurent599c7582015-12-07 18:05:55 -08001638 // reuse an open input if possible
Eric Laurent555530a2017-02-07 18:17:24 -08001639 sp<AudioInputDescriptor> reusedInputDesc;
Eric Laurent599c7582015-12-07 18:05:55 -08001640 for (size_t i = 0; i < mInputs.size(); i++) {
1641 sp<AudioInputDescriptor> desc = mInputs.valueAt(i);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001642 // reuse input if:
1643 // - it shares the same profile
1644 // AND
1645 // - it is not a reroute submix input
1646 // AND
1647 // - it is: not used for sound trigger
1648 // OR
1649 // used for sound trigger and all clients use the same session ID
1650 //
1651 if ((profile == desc->mProfile) &&
1652 (isSoundTrigger == desc->isSoundTrigger()) &&
1653 !is_virtual_input_device(device)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001654
1655 sp<AudioSession> as = desc->getAudioSession(session);
1656 if (as != 0) {
1657 // do not allow unmatching properties on same session
1658 if (as->matches(audioSession)) {
1659 as->changeOpenCount(1);
1660 } else {
1661 ALOGW("getInputForDevice() record with different attributes"
1662 " exists for session %d", session);
Eric Laurent555530a2017-02-07 18:17:24 -08001663 continue;
Eric Laurent599c7582015-12-07 18:05:55 -08001664 }
Eric Laurentfb66dd92016-01-28 18:32:03 -08001665 } else if (isSoundTrigger) {
Eric Laurent555530a2017-02-07 18:17:24 -08001666 continue;
Eric Laurentfb66dd92016-01-28 18:32:03 -08001667 }
Eric Laurentbb948092017-01-23 18:33:30 -08001668
Eric Laurent555530a2017-02-07 18:17:24 -08001669 // Reuse the already opened input stream on this profile if:
1670 // - the new capture source is background OR
1671 // - the path requested configurations match OR
1672 // - the new source priority is less than the highest source priority on this input
1673 // If the input stream cannot be reused, close it before opening a new stream
1674 // on the same profile for the new client so that the requested path configuration
1675 // can be selected.
1676 if (!isConcurrentSource(inputSource) &&
Eric Laurentbb948092017-01-23 18:33:30 -08001677 ((desc->mSamplingRate != samplingRate ||
Eric Laurentfb66dd92016-01-28 18:32:03 -08001678 desc->mChannelMask != channelMask ||
Eric Laurente6930022016-02-11 10:20:40 -08001679 !audio_formats_match(desc->mFormat, format)) &&
Eric Laurentfb66dd92016-01-28 18:32:03 -08001680 (source_priority(desc->getHighestPrioritySource(false /*activeOnly*/)) <
Eric Laurentbb948092017-01-23 18:33:30 -08001681 source_priority(inputSource)))) {
Eric Laurent555530a2017-02-07 18:17:24 -08001682 reusedInputDesc = desc;
1683 continue;
Eric Laurent599c7582015-12-07 18:05:55 -08001684 } else {
1685 desc->addAudioSession(session, audioSession);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001686 ALOGV("%s: reusing input %d", __FUNCTION__, mInputs.keyAt(i));
1687 return mInputs.keyAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08001688 }
Eric Laurent599c7582015-12-07 18:05:55 -08001689 }
1690 }
Eric Laurent599c7582015-12-07 18:05:55 -08001691
Eric Laurent555530a2017-02-07 18:17:24 -08001692 if (reusedInputDesc != 0) {
1693 AudioSessionCollection sessions = reusedInputDesc->getAudioSessions(false /*activeOnly*/);
1694 for (size_t j = 0; j < sessions.size(); j++) {
1695 audio_session_t currentSession = sessions.keyAt(j);
1696 stopInput(reusedInputDesc->mIoHandle, currentSession);
1697 releaseInput(reusedInputDesc->mIoHandle, currentSession);
1698 }
1699 }
Eric Laurent74708e72017-04-07 17:13:42 -07001700#endif
Eric Laurent555530a2017-02-07 18:17:24 -08001701
Eric Laurentcf2c0212014-07-25 16:20:43 -07001702 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001703 config.sample_rate = profileSamplingRate;
1704 config.channel_mask = profileChannelMask;
1705 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001706
Eric Laurent322b4d22015-04-03 15:57:54 -07001707 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurent599c7582015-12-07 18:05:55 -08001708 &input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001709 &config,
1710 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001711 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001712 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001713 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001714
1715 // only accept input with the exact requested set of parameters
Eric Laurent599c7582015-12-07 18:05:55 -08001716 if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001717 (profileSamplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -08001718 !audio_formats_match(profileFormat, config.format) ||
Andy Hungf129b032015-04-07 13:45:50 -07001719 (profileChannelMask != config.channel_mask)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001720 ALOGW("getInputForAttr() failed opening input: samplingRate %d"
1721 ", format %d, channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001722 samplingRate, format, channelMask);
Eric Laurent599c7582015-12-07 18:05:55 -08001723 if (input != AUDIO_IO_HANDLE_NONE) {
1724 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001725 }
Eric Laurent599c7582015-12-07 18:05:55 -08001726 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001727 }
1728
Eric Laurent1f2f2232014-06-02 12:01:23 -07001729 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Andy Hungf129b032015-04-07 13:45:50 -07001730 inputDesc->mSamplingRate = profileSamplingRate;
1731 inputDesc->mFormat = profileFormat;
1732 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001733 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001734 inputDesc->mPolicyMix = policyMix;
Eric Laurent599c7582015-12-07 18:05:55 -08001735 inputDesc->addAudioSession(session, audioSession);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001736
Eric Laurent599c7582015-12-07 18:05:55 -08001737 addInput(input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001738 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001739
Eric Laurent599c7582015-12-07 18:05:55 -08001740 return input;
Eric Laurente552edb2014-03-10 17:42:56 -07001741}
1742
Eric Laurentbb948092017-01-23 18:33:30 -08001743//static
1744bool AudioPolicyManager::isConcurrentSource(audio_source_t source)
1745{
1746 return (source == AUDIO_SOURCE_HOTWORD) ||
1747 (source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
1748 (source == AUDIO_SOURCE_FM_TUNER);
1749}
1750
Eric Laurentfb66dd92016-01-28 18:32:03 -08001751bool AudioPolicyManager::isConcurentCaptureAllowed(const sp<AudioInputDescriptor>& inputDesc,
1752 const sp<AudioSession>& audioSession)
1753{
1754 // Do not allow capture if an active voice call is using a software patch and
1755 // the call TX source device is on the same HW module.
1756 // FIXME: would be better to refine to only inputs whose profile connects to the
1757 // call TX device but this information is not in the audio patch
1758 if (mCallTxPatch != 0 &&
1759 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1760 return false;
1761 }
1762
1763 // starting concurrent capture is enabled if:
1764 // 1) capturing for re-routing
1765 // 2) capturing for HOTWORD source
1766 // 3) capturing for FM TUNER source
1767 // 3) All other active captures are either for re-routing or HOTWORD
1768
1769 if (is_virtual_input_device(inputDesc->mDevice) ||
Eric Laurentbb948092017-01-23 18:33:30 -08001770 isConcurrentSource(audioSession->inputSource())) {
Eric Laurentfb66dd92016-01-28 18:32:03 -08001771 return true;
1772 }
1773
1774 Vector< sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
1775 for (size_t i = 0; i < activeInputs.size(); i++) {
1776 sp<AudioInputDescriptor> activeInput = activeInputs[i];
Eric Laurentbb948092017-01-23 18:33:30 -08001777 if (!isConcurrentSource(activeInput->inputSource(true)) &&
Eric Laurentfb66dd92016-01-28 18:32:03 -08001778 !is_virtual_input_device(activeInput->mDevice)) {
1779 return false;
1780 }
1781 }
1782
1783 return true;
1784}
1785
1786
Eric Laurent4dc68062014-07-28 17:26:49 -07001787status_t AudioPolicyManager::startInput(audio_io_handle_t input,
Eric Laurentfb66dd92016-01-28 18:32:03 -08001788 audio_session_t session,
1789 concurrency_type__mask_t *concurrency)
Eric Laurente552edb2014-03-10 17:42:56 -07001790{
1791 ALOGV("startInput() input %d", input);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001792 *concurrency = API_INPUT_CONCURRENCY_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001793 ssize_t index = mInputs.indexOfKey(input);
1794 if (index < 0) {
1795 ALOGW("startInput() unknown input %d", input);
1796 return BAD_VALUE;
1797 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001798 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001799
Eric Laurent599c7582015-12-07 18:05:55 -08001800 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1801 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07001802 ALOGW("startInput() unknown session %d on input %d", session, input);
1803 return BAD_VALUE;
1804 }
1805
Eric Laurent74708e72017-04-07 17:13:42 -07001806// FIXME: disable concurrent capture until UI is ready
1807#if 0
Eric Laurentfb66dd92016-01-28 18:32:03 -08001808 if (!isConcurentCaptureAllowed(inputDesc, audioSession)) {
1809 ALOGW("startInput(%d) failed: other input already started", input);
1810 return INVALID_OPERATION;
1811 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001812
Eric Laurentfb66dd92016-01-28 18:32:03 -08001813 if (isInCall()) {
1814 *concurrency |= API_INPUT_CONCURRENCY_CALL;
1815 }
Eric Laurentf7c50102016-09-30 15:19:43 -07001816 if (mInputs.activeInputsCountOnDevices() != 0) {
Eric Laurentfb66dd92016-01-28 18:32:03 -08001817 *concurrency |= API_INPUT_CONCURRENCY_CAPTURE;
Eric Laurente552edb2014-03-10 17:42:56 -07001818 }
Eric Laurent74708e72017-04-07 17:13:42 -07001819#else
1820 if (!is_virtual_input_device(inputDesc->mDevice)) {
1821 if (mCallTxPatch != 0 &&
1822 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1823 ALOGW("startInput(%d) failed: call in progress", input);
1824 return INVALID_OPERATION;
1825 }
1826
1827 Vector< sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
1828 for (size_t i = 0; i < activeInputs.size(); i++) {
1829 sp<AudioInputDescriptor> activeDesc = activeInputs[i];
1830
1831 if (is_virtual_input_device(activeDesc->mDevice)) {
1832 continue;
1833 }
1834
1835 audio_source_t activeSource = activeDesc->inputSource(true);
1836 if (audioSession->inputSource() == AUDIO_SOURCE_HOTWORD) {
1837 if (activeSource == AUDIO_SOURCE_HOTWORD) {
1838 if (activeDesc->hasPreemptedSession(session)) {
1839 ALOGW("startInput(%d) failed for HOTWORD: "
1840 "other input %d already started for HOTWORD",
1841 input, activeDesc->mIoHandle);
1842 return INVALID_OPERATION;
1843 }
1844 } else {
1845 ALOGV("startInput(%d) failed for HOTWORD: other input %d already started",
1846 input, activeDesc->mIoHandle);
1847 return INVALID_OPERATION;
1848 }
1849 } else {
1850 if (activeSource != AUDIO_SOURCE_HOTWORD) {
1851 ALOGW("startInput(%d) failed: other input %d already started",
1852 input, activeDesc->mIoHandle);
1853 return INVALID_OPERATION;
1854 }
1855 }
1856 }
1857
1858 // if capture is allowed, preempt currently active HOTWORD captures
1859 for (size_t i = 0; i < activeInputs.size(); i++) {
1860 sp<AudioInputDescriptor> activeDesc = activeInputs[i];
1861
1862 if (is_virtual_input_device(activeDesc->mDevice)) {
1863 continue;
1864 }
1865
1866 audio_source_t activeSource = activeDesc->inputSource(true);
1867 if (activeSource == AUDIO_SOURCE_HOTWORD) {
1868 AudioSessionCollection activeSessions =
1869 activeDesc->getAudioSessions(true /*activeOnly*/);
1870 audio_session_t activeSession = activeSessions.keyAt(0);
1871 audio_io_handle_t activeHandle = activeDesc->mIoHandle;
1872 SortedVector<audio_session_t> sessions = activeDesc->getPreemptedSessions();
1873 sessions.add(activeSession);
1874 inputDesc->setPreemptedSessions(sessions);
1875 stopInput(activeHandle, activeSession);
1876 releaseInput(activeHandle, activeSession);
1877 ALOGV("startInput(%d) for HOTWORD preempting HOTWORD input %d",
1878 input, activeDesc->mIoHandle);
1879 }
1880 }
1881 }
1882#endif
Eric Laurente552edb2014-03-10 17:42:56 -07001883
Eric Laurent313d1e72016-01-29 09:56:57 -08001884 // increment activity count before calling getNewInputDevice() below as only active sessions
1885 // are considered for device selection
1886 audioSession->changeActiveCount(1);
1887
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001888 // Routing?
1889 mInputRoutes.incRouteActivity(session);
1890
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001891 if (audioSession->activeCount() == 1 || mInputRoutes.hasRouteChanged(session)) {
Eric Laurent271a93e2016-09-29 14:47:06 -07001892 // indicate active capture to sound trigger service if starting capture from a mic on
1893 // primary HW module
Eric Laurentf7c50102016-09-30 15:19:43 -07001894 audio_devices_t device = getNewInputDevice(inputDesc);
Eric Laurent271a93e2016-09-29 14:47:06 -07001895 setInputDevice(input, device, true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001896
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001897 if (inputDesc->getAudioSessionCount(true/*activeOnly*/) == 1) {
1898 // if input maps to a dynamic policy with an activity listener, notify of state change
1899 if ((inputDesc->mPolicyMix != NULL)
1900 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1901 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
1902 MIX_STATE_MIXING);
Eric Laurentc722f302014-12-10 11:21:49 -08001903 }
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001904
Eric Laurentf7c50102016-09-30 15:19:43 -07001905 audio_devices_t primaryInputDevices = availablePrimaryInputDevices();
1906 if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) &&
Eric Laurent05b345e2016-11-18 12:36:27 -08001907 mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) {
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001908 SoundTrigger::setCaptureState(true);
1909 }
1910
1911 // automatically enable the remote submix output when input is started if not
1912 // used by a policy mix of type MIX_TYPE_RECORDERS
1913 // For remote submix (a virtual device), we open only one input per capture request.
1914 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1915 String8 address = String8("");
1916 if (inputDesc->mPolicyMix == NULL) {
1917 address = String8("0");
1918 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1919 address = inputDesc->mPolicyMix->mDeviceAddress;
1920 }
1921 if (address != "") {
1922 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1923 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1924 address, "remote-submix");
1925 }
Eric Laurentc722f302014-12-10 11:21:49 -08001926 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001927 }
Eric Laurente552edb2014-03-10 17:42:56 -07001928 }
1929
Eric Laurent599c7582015-12-07 18:05:55 -08001930 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Eric Laurente552edb2014-03-10 17:42:56 -07001931
Eric Laurente552edb2014-03-10 17:42:56 -07001932 return NO_ERROR;
1933}
1934
Eric Laurent4dc68062014-07-28 17:26:49 -07001935status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1936 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001937{
1938 ALOGV("stopInput() input %d", input);
1939 ssize_t index = mInputs.indexOfKey(input);
1940 if (index < 0) {
1941 ALOGW("stopInput() unknown input %d", input);
1942 return BAD_VALUE;
1943 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001944 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001945
Eric Laurent599c7582015-12-07 18:05:55 -08001946 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001947 if (index < 0) {
1948 ALOGW("stopInput() unknown session %d on input %d", session, input);
1949 return BAD_VALUE;
1950 }
1951
Eric Laurent599c7582015-12-07 18:05:55 -08001952 if (audioSession->activeCount() == 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07001953 ALOGW("stopInput() input %d already stopped", input);
1954 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001955 }
1956
Eric Laurent599c7582015-12-07 18:05:55 -08001957 audioSession->changeActiveCount(-1);
Paul McLean466dc8e2015-04-17 13:15:36 -06001958
1959 // Routing?
1960 mInputRoutes.decRouteActivity(session);
1961
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001962 if (audioSession->activeCount() == 0) {
Eric Laurent84332aa2016-01-28 22:19:18 +00001963
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001964 if (inputDesc->isActive()) {
1965 setInputDevice(input, getNewInputDevice(inputDesc), false /* force */);
1966 } else {
1967 // if input maps to a dynamic policy with an activity listener, notify of state change
1968 if ((inputDesc->mPolicyMix != NULL)
1969 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1970 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
1971 MIX_STATE_IDLE);
Eric Laurent84332aa2016-01-28 22:19:18 +00001972 }
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001973
1974 // automatically disable the remote submix output when input is stopped if not
1975 // used by a policy mix of type MIX_TYPE_RECORDERS
1976 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1977 String8 address = String8("");
1978 if (inputDesc->mPolicyMix == NULL) {
1979 address = String8("0");
1980 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1981 address = inputDesc->mPolicyMix->mDeviceAddress;
1982 }
1983 if (address != "") {
1984 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1985 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1986 address, "remote-submix");
1987 }
Eric Laurent84332aa2016-01-28 22:19:18 +00001988 }
Eric Laurent84332aa2016-01-28 22:19:18 +00001989
Eric Laurentf7c50102016-09-30 15:19:43 -07001990 audio_devices_t device = inputDesc->mDevice;
Eric Laurentfb66dd92016-01-28 18:32:03 -08001991 resetInputDevice(input);
Eric Laurent84332aa2016-01-28 22:19:18 +00001992
Eric Laurentf7c50102016-09-30 15:19:43 -07001993 // indicate inactive capture to sound trigger service if stopping capture from a mic on
1994 // primary HW module
1995 audio_devices_t primaryInputDevices = availablePrimaryInputDevices();
1996 if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) &&
1997 mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) {
Eric Laurentfb66dd92016-01-28 18:32:03 -08001998 SoundTrigger::setCaptureState(false);
1999 }
2000 inputDesc->clearPreemptedSessions();
Eric Laurent84332aa2016-01-28 22:19:18 +00002001 }
Eric Laurente552edb2014-03-10 17:42:56 -07002002 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002003 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07002004}
2005
Eric Laurent4dc68062014-07-28 17:26:49 -07002006void AudioPolicyManager::releaseInput(audio_io_handle_t input,
2007 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07002008{
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002009
Eric Laurente552edb2014-03-10 17:42:56 -07002010 ALOGV("releaseInput() %d", input);
2011 ssize_t index = mInputs.indexOfKey(input);
2012 if (index < 0) {
2013 ALOGW("releaseInput() releasing unknown input %d", input);
2014 return;
2015 }
Paul McLean466dc8e2015-04-17 13:15:36 -06002016
2017 // Routing
2018 mInputRoutes.removeRoute(session);
2019
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002020 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
2021 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07002022
Eric Laurent599c7582015-12-07 18:05:55 -08002023 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
vivek mehta587b8df2017-01-31 14:58:44 -08002024 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07002025 ALOGW("releaseInput() unknown session %d on input %d", session, input);
2026 return;
2027 }
Eric Laurent599c7582015-12-07 18:05:55 -08002028
2029 if (audioSession->openCount() == 0) {
2030 ALOGW("releaseInput() invalid open count %d on session %d",
2031 audioSession->openCount(), session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002032 return;
2033 }
Eric Laurent599c7582015-12-07 18:05:55 -08002034
2035 if (audioSession->changeOpenCount(-1) == 0) {
2036 inputDesc->removeAudioSession(session);
2037 }
2038
Jean-Michel Trivi65bfe912015-12-04 15:56:47 -08002039 if (inputDesc->getOpenRefCount() > 0) {
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002040 ALOGV("releaseInput() exit > 0");
2041 return;
2042 }
2043
Eric Laurent05b90f82014-08-27 15:32:29 -07002044 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07002045 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07002046 ALOGV("releaseInput() exit");
2047}
2048
Eric Laurentd4692962014-05-05 18:13:44 -07002049void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07002050 bool patchRemoved = false;
2051
Eric Laurentd4692962014-05-05 18:13:44 -07002052 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07002053 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08002054 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07002055 if (patch_index >= 0) {
2056 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07002057 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07002058 mAudioPatches.removeItemsAt(patch_index);
2059 patchRemoved = true;
2060 }
Eric Laurentd4692962014-05-05 18:13:44 -07002061 mpClientInterface->closeInput(mInputs.keyAt(input_index));
2062 }
2063 mInputs.clear();
Chris Thornton52785f32016-02-09 17:28:49 -08002064 SoundTrigger::setCaptureState(false);
Eric Laurent6a94d692014-05-20 11:18:06 -07002065 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07002066
2067 if (patchRemoved) {
2068 mpClientInterface->onAudioPatchListUpdate();
2069 }
Eric Laurentd4692962014-05-05 18:13:44 -07002070}
2071
Eric Laurente0720872014-03-11 09:30:41 -07002072void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07002073 int indexMin,
2074 int indexMax)
2075{
2076 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002077 mVolumeCurves->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent28d09f02016-03-08 10:43:05 -08002078
2079 // initialize other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08002080 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
2081 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002082 continue;
2083 }
2084 mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08002085 }
Eric Laurente552edb2014-03-10 17:42:56 -07002086}
2087
Eric Laurente0720872014-03-11 09:30:41 -07002088status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01002089 int index,
2090 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07002091{
2092
François Gaffied1ab2bd2015-12-02 18:20:06 +01002093 if ((index < mVolumeCurves->getVolumeIndexMin(stream)) ||
2094 (index > mVolumeCurves->getVolumeIndexMax(stream))) {
Eric Laurente552edb2014-03-10 17:42:56 -07002095 return BAD_VALUE;
2096 }
2097 if (!audio_is_output_device(device)) {
2098 return BAD_VALUE;
2099 }
2100
2101 // Force max volume if stream cannot be muted
François Gaffied1ab2bd2015-12-02 18:20:06 +01002102 if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07002103
Eric Laurent1fd372e2016-04-06 14:23:53 -07002104 ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d",
Eric Laurente552edb2014-03-10 17:42:56 -07002105 stream, device, index);
2106
Eric Laurent28d09f02016-03-08 10:43:05 -08002107 // update other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08002108 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
2109 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002110 continue;
2111 }
2112 mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index);
2113 }
Eric Laurente552edb2014-03-10 17:42:56 -07002114
Eric Laurent1fd372e2016-04-06 14:23:53 -07002115 // update volume on all outputs and streams matching the following:
2116 // - The requested stream (or a stream matching for volume control) is active on the output
2117 // - The device (or devices) selected by the strategy corresponding to this stream includes
2118 // the requested device
2119 // - For non default requested device, currently selected device on the output is either the
2120 // requested device or one of the devices selected by the strategy
Eric Laurent5a2b6292016-04-14 18:05:57 -07002121 // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if
2122 // no specific device volume value exists for currently selected device.
Eric Laurente552edb2014-03-10 17:42:56 -07002123 status_t status = NO_ERROR;
2124 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002125 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
2126 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent794fde22016-03-11 09:50:45 -08002127 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
2128 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002129 continue;
Eric Laurente552edb2014-03-10 17:42:56 -07002130 }
Eric Laurentd3926fe2016-04-15 18:10:07 -07002131 if (!(desc->isStreamActive((audio_stream_type_t)curStream) ||
2132 (isInCall() && (curStream == AUDIO_STREAM_VOICE_CALL)))) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07002133 continue;
2134 }
Eric Laurent794fde22016-03-11 09:50:45 -08002135 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurente76f29c2016-09-14 17:17:53 -07002136 audio_devices_t curStreamDevice = getDeviceForStrategy(curStrategy, false /*fromCache*/);
2137 if ((device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) &&
2138 ((curStreamDevice & device) == 0)) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07002139 continue;
2140 }
Eric Laurente76f29c2016-09-14 17:17:53 -07002141 bool applyVolume;
Eric Laurent5a2b6292016-04-14 18:05:57 -07002142 if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07002143 curStreamDevice |= device;
Eric Laurente76f29c2016-09-14 17:17:53 -07002144 applyVolume = (curDevice & curStreamDevice) != 0;
2145 } else {
2146 applyVolume = !mVolumeCurves->hasVolumeIndexForDevice(
2147 stream, Volume::getDeviceForVolume(curStreamDevice));
Eric Laurent1fd372e2016-04-06 14:23:53 -07002148 }
Eric Laurent28d09f02016-03-08 10:43:05 -08002149
Eric Laurente76f29c2016-09-14 17:17:53 -07002150 if (applyVolume) {
Eric Laurentdc462862016-07-19 12:29:53 -07002151 //FIXME: workaround for truncated touch sounds
2152 // delayed volume change for system stream to be removed when the problem is
2153 // handled by system UI
Eric Laurent28d09f02016-03-08 10:43:05 -08002154 status_t volStatus =
Eric Laurentdc462862016-07-19 12:29:53 -07002155 checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice,
2156 (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0);
Eric Laurent28d09f02016-03-08 10:43:05 -08002157 if (volStatus != NO_ERROR) {
2158 status = volStatus;
2159 }
2160 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08002161 }
Eric Laurente552edb2014-03-10 17:42:56 -07002162 }
2163 return status;
2164}
2165
Eric Laurente0720872014-03-11 09:30:41 -07002166status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07002167 int *index,
2168 audio_devices_t device)
2169{
2170 if (index == NULL) {
2171 return BAD_VALUE;
2172 }
2173 if (!audio_is_output_device(device)) {
2174 return BAD_VALUE;
2175 }
Eric Laurent5a2b6292016-04-14 18:05:57 -07002176 // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device corresponding to
Eric Laurente552edb2014-03-10 17:42:56 -07002177 // the strategy the stream belongs to.
Eric Laurent5a2b6292016-04-14 18:05:57 -07002178 if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurente552edb2014-03-10 17:42:56 -07002179 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
2180 }
François Gaffiedfd74092015-03-19 12:10:59 +01002181 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07002182
François Gaffied1ab2bd2015-12-02 18:20:06 +01002183 *index = mVolumeCurves->getVolumeIndex(stream, device);
Eric Laurente552edb2014-03-10 17:42:56 -07002184 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
2185 return NO_ERROR;
2186}
2187
Eric Laurente0720872014-03-11 09:30:41 -07002188audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07002189 const SortedVector<audio_io_handle_t>& outputs)
2190{
2191 // select one output among several suitable for global effects.
2192 // The priority is as follows:
2193 // 1: An offloaded output. If the effect ends up not being offloadable,
2194 // AudioFlinger will invalidate the track and the offloaded output
2195 // will be closed causing the effect to be moved to a PCM output.
2196 // 2: A deep buffer output
2197 // 3: the first output in the list
2198
2199 if (outputs.size() == 0) {
2200 return 0;
2201 }
2202
2203 audio_io_handle_t outputOffloaded = 0;
2204 audio_io_handle_t outputDeepBuffer = 0;
2205
2206 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002207 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07002208 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07002209 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
2210 outputOffloaded = outputs[i];
2211 }
2212 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
2213 outputDeepBuffer = outputs[i];
2214 }
2215 }
2216
2217 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
2218 outputOffloaded, outputDeepBuffer);
2219 if (outputOffloaded != 0) {
2220 return outputOffloaded;
2221 }
2222 if (outputDeepBuffer != 0) {
2223 return outputDeepBuffer;
2224 }
2225
2226 return outputs[0];
2227}
2228
Eric Laurente0720872014-03-11 09:30:41 -07002229audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07002230{
2231 // apply simple rule where global effects are attached to the same output as MUSIC streams
2232
Eric Laurent3b73df72014-03-11 09:06:29 -07002233 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07002234 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2235 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
2236
2237 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
2238 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
2239 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
2240
2241 return output;
2242}
2243
Eric Laurente0720872014-03-11 09:30:41 -07002244status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07002245 audio_io_handle_t io,
2246 uint32_t strategy,
2247 int session,
2248 int id)
2249{
2250 ssize_t index = mOutputs.indexOfKey(io);
2251 if (index < 0) {
2252 index = mInputs.indexOfKey(io);
2253 if (index < 0) {
2254 ALOGW("registerEffect() unknown io %d", io);
2255 return INVALID_OPERATION;
2256 }
2257 }
François Gaffie45ed3b02015-03-19 10:35:14 +01002258 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07002259}
2260
Eric Laurentc75307b2015-03-17 15:29:32 -07002261bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
2262{
Eric Laurent28d09f02016-03-08 10:43:05 -08002263 bool active = false;
Eric Laurent794fde22016-03-11 09:50:45 -08002264 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) {
2265 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002266 continue;
2267 }
2268 active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs);
2269 }
Eric Laurent28d09f02016-03-08 10:43:05 -08002270 return active;
Eric Laurentc75307b2015-03-17 15:29:32 -07002271}
2272
2273bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
2274{
2275 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
2276}
2277
Eric Laurente0720872014-03-11 09:30:41 -07002278bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07002279{
2280 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002281 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002282 if (inputDescriptor->isSourceActive(source)) {
Eric Laurente552edb2014-03-10 17:42:56 -07002283 return true;
2284 }
2285 }
2286 return false;
2287}
2288
Eric Laurent275e8e92014-11-30 15:14:47 -08002289// Register a list of custom mixes with their attributes and format.
2290// When a mix is registered, corresponding input and output profiles are
2291// added to the remote submix hw module. The profile contains only the
2292// parameters (sampling rate, format...) specified by the mix.
2293// The corresponding input remote submix device is also connected.
2294//
2295// When a remote submix device is connected, the address is checked to select the
2296// appropriate profile and the corresponding input or output stream is opened.
2297//
2298// When capture starts, getInputForAttr() will:
2299// - 1 look for a mix matching the address passed in attribtutes tags if any
2300// - 2 if none found, getDeviceForInputSource() will:
2301// - 2.1 look for a mix matching the attributes source
2302// - 2.2 if none found, default to device selection by policy rules
2303// At this time, the corresponding output remote submix device is also connected
2304// and active playback use cases can be transferred to this mix if needed when reconnecting
2305// after AudioTracks are invalidated
2306//
2307// When playback starts, getOutputForAttr() will:
2308// - 1 look for a mix matching the address passed in attribtutes tags if any
2309// - 2 if none found, look for a mix matching the attributes usage
2310// - 3 if none found, default to device and output selection by policy rules.
2311
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07002312status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes)
Eric Laurent275e8e92014-11-30 15:14:47 -08002313{
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002314 ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size());
2315 status_t res = NO_ERROR;
2316
2317 sp<HwModule> rSubmixModule;
2318 // examine each mix's route type
2319 for (size_t i = 0; i < mixes.size(); i++) {
2320 // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination
2321 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) {
2322 res = INVALID_OPERATION;
Eric Laurent275e8e92014-11-30 15:14:47 -08002323 break;
2324 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002325 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
2326 // Loop back through "remote submix"
2327 if (rSubmixModule == 0) {
2328 for (size_t j = 0; i < mHwModules.size(); j++) {
2329 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2330 && mHwModules[j]->mHandle != 0) {
2331 rSubmixModule = mHwModules[j];
2332 break;
2333 }
2334 }
2335 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002336
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002337 ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size());
Eric Laurent275e8e92014-11-30 15:14:47 -08002338
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002339 if (rSubmixModule == 0) {
2340 ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", i);
2341 res = INVALID_OPERATION;
2342 break;
2343 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002344
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002345 String8 address = mixes[i].mDeviceAddress;
François Gaffie036e1e92015-03-19 10:16:24 +01002346
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002347 if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) {
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002348 ALOGE(" Error registering mix %zu for address %s", i, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002349 res = INVALID_OPERATION;
2350 break;
2351 }
2352 audio_config_t outputConfig = mixes[i].mFormat;
2353 audio_config_t inputConfig = mixes[i].mFormat;
2354 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
2355 // stereo and let audio flinger do the channel conversion if needed.
2356 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2357 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
2358 rSubmixModule->addOutputProfile(address, &outputConfig,
2359 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
2360 rSubmixModule->addInputProfile(address, &inputConfig,
2361 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01002362
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002363 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
2364 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2365 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2366 address.string(), "remote-submix");
2367 } else {
2368 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2369 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2370 address.string(), "remote-submix");
2371 }
2372 } else if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002373 String8 address = mixes[i].mDeviceAddress;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002374 audio_devices_t device = mixes[i].mDeviceType;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002375 ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s",
2376 i, mixes.size(), device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002377
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002378 bool foundOutput = false;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002379 for (size_t j = 0 ; j < mOutputs.size() ; j++) {
2380 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j);
2381 sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle());
2382 if ((patch != 0) && (patch->mPatch.num_sinks != 0)
2383 && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE)
2384 && (patch->mPatch.sinks[0].ext.device.type == device)
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002385 && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(),
2386 AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002387 if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) {
2388 res = INVALID_OPERATION;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002389 } else {
2390 foundOutput = true;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002391 }
2392 break;
2393 }
2394 }
2395
2396 if (res != NO_ERROR) {
2397 ALOGE(" Error registering mix %zu for device 0x%X addr %s",
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002398 i, device, address.string());
2399 res = INVALID_OPERATION;
2400 break;
2401 } else if (!foundOutput) {
2402 ALOGE(" Output not found for mix %zu for device 0x%X addr %s",
2403 i, device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002404 res = INVALID_OPERATION;
2405 break;
2406 }
Eric Laurentc722f302014-12-10 11:21:49 -08002407 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002408 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002409 if (res != NO_ERROR) {
2410 unregisterPolicyMixes(mixes);
2411 }
2412 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002413}
2414
2415status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
2416{
Eric Laurent7b279bb2015-12-14 10:18:23 -08002417 ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002418 status_t res = NO_ERROR;
2419 sp<HwModule> rSubmixModule;
2420 // examine each mix's route type
Eric Laurent275e8e92014-11-30 15:14:47 -08002421 for (size_t i = 0; i < mixes.size(); i++) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002422 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
François Gaffie036e1e92015-03-19 10:16:24 +01002423
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002424 if (rSubmixModule == 0) {
2425 for (size_t j = 0; i < mHwModules.size(); j++) {
2426 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2427 && mHwModules[j]->mHandle != 0) {
2428 rSubmixModule = mHwModules[j];
2429 break;
2430 }
2431 }
2432 }
2433 if (rSubmixModule == 0) {
2434 res = INVALID_OPERATION;
2435 continue;
2436 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002437
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002438 String8 address = mixes[i].mDeviceAddress;
Eric Laurent275e8e92014-11-30 15:14:47 -08002439
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002440 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
2441 res = INVALID_OPERATION;
2442 continue;
2443 }
2444
2445 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
2446 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2447 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2448 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2449 address.string(), "remote-submix");
2450 }
2451 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
2452 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2453 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2454 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2455 address.string(), "remote-submix");
2456 }
2457 rSubmixModule->removeOutputProfile(address);
2458 rSubmixModule->removeInputProfile(address);
2459
2460 } if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
2461 if (mPolicyMixes.unregisterMix(mixes[i].mDeviceAddress) != NO_ERROR) {
2462 res = INVALID_OPERATION;
2463 continue;
2464 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002465 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002466 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002467 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002468}
2469
Eric Laurente552edb2014-03-10 17:42:56 -07002470
Eric Laurente0720872014-03-11 09:30:41 -07002471status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07002472{
2473 const size_t SIZE = 256;
2474 char buffer[SIZE];
2475 String8 result;
2476
2477 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
2478 result.append(buffer);
2479
Eric Laurent87ffa392015-05-22 10:32:38 -07002480 snprintf(buffer, SIZE, " Primary Output: %d\n",
2481 hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE);
Eric Laurente552edb2014-03-10 17:42:56 -07002482 result.append(buffer);
Mikhail Naganov0d6a0332016-04-19 17:12:38 -07002483 std::string stateLiteral;
2484 AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral);
2485 snprintf(buffer, SIZE, " Phone state: %s\n", stateLiteral.c_str());
Eric Laurente552edb2014-03-10 17:42:56 -07002486 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002487 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002488 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07002489 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002490 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07002491 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002492 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07002493 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002494 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07002495 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002496 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07002497 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002498 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002499 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002500 result.append(buffer);
Phil Burk09bc4612016-02-24 15:58:15 -08002501 snprintf(buffer, SIZE, " Force use for encoded surround output %d\n",
2502 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND));
2503 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002504 snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available");
2505 result.append(buffer);
Andy Hung2ddee192015-12-18 17:34:44 -08002506 snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off");
2507 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002508
François Gaffie2110e042015-03-24 08:41:51 +01002509 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07002510
François Gaffie112b0af2015-11-19 16:13:25 +01002511 mAvailableOutputDevices.dump(fd, String8("Available output"));
2512 mAvailableInputDevices.dump(fd, String8("Available input"));
François Gaffie53615e22015-03-19 09:24:12 +01002513 mHwModules.dump(fd);
2514 mOutputs.dump(fd);
2515 mInputs.dump(fd);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002516 mVolumeCurves->dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01002517 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002518 mAudioPatches.dump(fd);
Mikhail Naganov44344b02016-12-13 11:21:02 -08002519 mPolicyMixes.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07002520
2521 return NO_ERROR;
2522}
2523
2524// This function checks for the parameters which can be offloaded.
2525// This can be enhanced depending on the capability of the DSP and policy
2526// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002527bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002528{
2529 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002530 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002531 offloadInfo.sample_rate, offloadInfo.channel_mask,
2532 offloadInfo.format,
2533 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2534 offloadInfo.has_video);
2535
Andy Hung2ddee192015-12-18 17:34:44 -08002536 if (mMasterMono) {
2537 return false; // no offloading if mono is set.
2538 }
2539
Eric Laurente552edb2014-03-10 17:42:56 -07002540 // Check if offload has been disabled
2541 char propValue[PROPERTY_VALUE_MAX];
2542 if (property_get("audio.offload.disable", propValue, "0")) {
2543 if (atoi(propValue) != 0) {
2544 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2545 return false;
2546 }
2547 }
2548
2549 // Check if stream type is music, then only allow offload as of now.
2550 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2551 {
2552 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2553 return false;
2554 }
2555
2556 //TODO: enable audio offloading with video when ready
Andy Hung08945c42015-05-31 21:36:46 -07002557 const bool allowOffloadWithVideo =
2558 property_get_bool("audio.offload.video", false /* default_value */);
2559 if (offloadInfo.has_video && !allowOffloadWithVideo) {
Eric Laurente552edb2014-03-10 17:42:56 -07002560 ALOGV("isOffloadSupported: has_video == true, returning false");
2561 return false;
2562 }
2563
2564 //If duration is less than minimum value defined in property, return false
2565 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2566 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2567 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2568 return false;
2569 }
2570 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2571 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2572 return false;
2573 }
2574
2575 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2576 // creating an offloaded track and tearing it down immediately after start when audioflinger
2577 // detects there is an active non offloadable effect.
2578 // FIXME: We should check the audio session here but we do not have it in this context.
2579 // This may prevent offloading in rare situations where effects are left active by apps
2580 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002581 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002582 return false;
2583 }
2584
2585 // See if there is a profile to support this.
2586 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002587 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002588 offloadInfo.sample_rate,
2589 offloadInfo.format,
2590 offloadInfo.channel_mask,
2591 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002592 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2593 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002594}
2595
Eric Laurent6a94d692014-05-20 11:18:06 -07002596status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2597 audio_port_type_t type,
2598 unsigned int *num_ports,
2599 struct audio_port *ports,
2600 unsigned int *generation)
2601{
2602 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2603 generation == NULL) {
2604 return BAD_VALUE;
2605 }
2606 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2607 if (ports == NULL) {
2608 *num_ports = 0;
2609 }
2610
2611 size_t portsWritten = 0;
2612 size_t portsMax = *num_ports;
2613 *num_ports = 0;
2614 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002615 // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB
2616 // as they are used by stub HALs by convention
Eric Laurent6a94d692014-05-20 11:18:06 -07002617 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002618 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
2619 if (mAvailableOutputDevices[i]->type() == AUDIO_DEVICE_OUT_STUB) {
2620 continue;
2621 }
2622 if (portsWritten < portsMax) {
2623 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2624 }
2625 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002626 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002627 }
2628 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002629 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
2630 if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_STUB) {
2631 continue;
2632 }
2633 if (portsWritten < portsMax) {
2634 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2635 }
2636 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002637 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002638 }
2639 }
2640 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2641 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2642 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2643 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2644 }
2645 *num_ports += mInputs.size();
2646 }
2647 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002648 size_t numOutputs = 0;
2649 for (size_t i = 0; i < mOutputs.size(); i++) {
2650 if (!mOutputs[i]->isDuplicated()) {
2651 numOutputs++;
2652 if (portsWritten < portsMax) {
2653 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2654 }
2655 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002656 }
Eric Laurent84c70242014-06-23 08:46:27 -07002657 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002658 }
2659 }
2660 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002661 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002662 return NO_ERROR;
2663}
2664
2665status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2666{
2667 return NO_ERROR;
2668}
2669
Eric Laurent6a94d692014-05-20 11:18:06 -07002670status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2671 audio_patch_handle_t *handle,
2672 uid_t uid)
2673{
2674 ALOGV("createAudioPatch()");
2675
2676 if (handle == NULL || patch == NULL) {
2677 return BAD_VALUE;
2678 }
2679 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2680
Eric Laurent874c42872014-08-08 15:13:39 -07002681 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2682 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2683 return BAD_VALUE;
2684 }
2685 // only one source per audio patch supported for now
2686 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002687 return INVALID_OPERATION;
2688 }
Eric Laurent874c42872014-08-08 15:13:39 -07002689
2690 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002691 return INVALID_OPERATION;
2692 }
Eric Laurent874c42872014-08-08 15:13:39 -07002693 for (size_t i = 0; i < patch->num_sinks; i++) {
2694 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2695 return INVALID_OPERATION;
2696 }
2697 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002698
2699 sp<AudioPatch> patchDesc;
2700 ssize_t index = mAudioPatches.indexOfKey(*handle);
2701
Eric Laurent6a94d692014-05-20 11:18:06 -07002702 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2703 patch->sources[0].role,
2704 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002705#if LOG_NDEBUG == 0
2706 for (size_t i = 0; i < patch->num_sinks; i++) {
Eric Laurent7b279bb2015-12-14 10:18:23 -08002707 ALOGV("createAudioPatch sink %zu: id %d role %d type %d", i, patch->sinks[i].id,
Eric Laurent874c42872014-08-08 15:13:39 -07002708 patch->sinks[i].role,
2709 patch->sinks[i].type);
2710 }
2711#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002712
2713 if (index >= 0) {
2714 patchDesc = mAudioPatches.valueAt(index);
2715 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2716 mUidCached, patchDesc->mUid, uid);
2717 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2718 return INVALID_OPERATION;
2719 }
2720 } else {
Glenn Kastena13cde92016-03-28 15:26:02 -07002721 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07002722 }
2723
2724 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002725 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002726 if (outputDesc == NULL) {
2727 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2728 return BAD_VALUE;
2729 }
Eric Laurent84c70242014-06-23 08:46:27 -07002730 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2731 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002732 if (patchDesc != 0) {
2733 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2734 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2735 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2736 return BAD_VALUE;
2737 }
2738 }
Eric Laurent874c42872014-08-08 15:13:39 -07002739 DeviceVector devices;
2740 for (size_t i = 0; i < patch->num_sinks; i++) {
2741 // Only support mix to devices connection
2742 // TODO add support for mix to mix connection
2743 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2744 ALOGV("createAudioPatch() source mix but sink is not a device");
2745 return INVALID_OPERATION;
2746 }
2747 sp<DeviceDescriptor> devDesc =
2748 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2749 if (devDesc == 0) {
2750 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2751 return BAD_VALUE;
2752 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002753
François Gaffie53615e22015-03-19 09:24:12 +01002754 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002755 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002756 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002757 NULL, // updatedSamplingRate
2758 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002759 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002760 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002761 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002762 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002763 ALOGV("createAudioPatch() profile not supported for device %08x",
2764 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002765 return INVALID_OPERATION;
2766 }
2767 devices.add(devDesc);
2768 }
2769 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002770 return INVALID_OPERATION;
2771 }
Eric Laurent874c42872014-08-08 15:13:39 -07002772
Eric Laurent6a94d692014-05-20 11:18:06 -07002773 // TODO: reconfigure output format and channels here
2774 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002775 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002776 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002777 index = mAudioPatches.indexOfKey(*handle);
2778 if (index >= 0) {
2779 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2780 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2781 }
2782 patchDesc = mAudioPatches.valueAt(index);
2783 patchDesc->mUid = uid;
2784 ALOGV("createAudioPatch() success");
2785 } else {
2786 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2787 return INVALID_OPERATION;
2788 }
2789 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2790 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2791 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002792 // only one sink supported when connecting an input device to a mix
2793 if (patch->num_sinks > 1) {
2794 return INVALID_OPERATION;
2795 }
François Gaffie53615e22015-03-19 09:24:12 +01002796 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002797 if (inputDesc == NULL) {
2798 return BAD_VALUE;
2799 }
2800 if (patchDesc != 0) {
2801 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2802 return BAD_VALUE;
2803 }
2804 }
2805 sp<DeviceDescriptor> devDesc =
2806 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2807 if (devDesc == 0) {
2808 return BAD_VALUE;
2809 }
2810
François Gaffie53615e22015-03-19 09:24:12 +01002811 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002812 devDesc->mAddress,
2813 patch->sinks[0].sample_rate,
2814 NULL, /*updatedSampleRate*/
2815 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002816 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002817 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002818 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002819 // FIXME for the parameter type,
2820 // and the NONE
2821 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002822 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002823 return INVALID_OPERATION;
2824 }
2825 // TODO: reconfigure output format and channels here
2826 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002827 devDesc->type(), inputDesc->mIoHandle);
2828 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002829 index = mAudioPatches.indexOfKey(*handle);
2830 if (index >= 0) {
2831 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2832 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2833 }
2834 patchDesc = mAudioPatches.valueAt(index);
2835 patchDesc->mUid = uid;
2836 ALOGV("createAudioPatch() success");
2837 } else {
2838 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2839 return INVALID_OPERATION;
2840 }
2841 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2842 // device to device connection
2843 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002844 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002845 return BAD_VALUE;
2846 }
2847 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002848 sp<DeviceDescriptor> srcDeviceDesc =
2849 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002850 if (srcDeviceDesc == 0) {
2851 return BAD_VALUE;
2852 }
Eric Laurent874c42872014-08-08 15:13:39 -07002853
Eric Laurent6a94d692014-05-20 11:18:06 -07002854 //update source and sink with our own data as the data passed in the patch may
2855 // be incomplete.
2856 struct audio_patch newPatch = *patch;
2857 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002858
Eric Laurent874c42872014-08-08 15:13:39 -07002859 for (size_t i = 0; i < patch->num_sinks; i++) {
2860 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2861 ALOGV("createAudioPatch() source device but one sink is not a device");
2862 return INVALID_OPERATION;
2863 }
2864
2865 sp<DeviceDescriptor> sinkDeviceDesc =
2866 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2867 if (sinkDeviceDesc == 0) {
2868 return BAD_VALUE;
2869 }
2870 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2871
Eric Laurent3bcf8592015-04-03 12:13:24 -07002872 // create a software bridge in PatchPanel if:
2873 // - source and sink devices are on differnt HW modules OR
2874 // - audio HAL version is < 3.0
Eric Laurentfb66dd92016-01-28 18:32:03 -08002875 if (!srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) ||
Mikhail Naganov9ee05402016-10-13 15:58:17 -07002876 (srcDeviceDesc->mModule->getHalVersionMajor() < 3)) {
Eric Laurent3bcf8592015-04-03 12:13:24 -07002877 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002878 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002879 return INVALID_OPERATION;
2880 }
Eric Laurent874c42872014-08-08 15:13:39 -07002881 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002882 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002883 // if the sink device is reachable via an opened output stream, request to go via
2884 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002885 audio_io_handle_t output = selectOutput(outputs,
2886 AUDIO_OUTPUT_FLAG_NONE,
2887 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002888 if (output != AUDIO_IO_HANDLE_NONE) {
2889 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2890 if (outputDesc->isDuplicated()) {
2891 return INVALID_OPERATION;
2892 }
2893 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002894 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002895 newPatch.num_sources = 2;
2896 }
Eric Laurent83b88082014-06-20 18:31:16 -07002897 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002898 }
2899 // TODO: check from routing capabilities in config file and other conflicting patches
2900
2901 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2902 if (index >= 0) {
2903 afPatchHandle = patchDesc->mAfPatchHandle;
2904 }
2905
2906 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2907 &afPatchHandle,
2908 0);
2909 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2910 status, afPatchHandle);
2911 if (status == NO_ERROR) {
2912 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002913 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002914 addAudioPatch(patchDesc->mHandle, patchDesc);
2915 } else {
2916 patchDesc->mPatch = newPatch;
2917 }
2918 patchDesc->mAfPatchHandle = afPatchHandle;
2919 *handle = patchDesc->mHandle;
2920 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002921 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002922 } else {
2923 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2924 status);
2925 return INVALID_OPERATION;
2926 }
2927 } else {
2928 return BAD_VALUE;
2929 }
2930 } else {
2931 return BAD_VALUE;
2932 }
2933 return NO_ERROR;
2934}
2935
2936status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2937 uid_t uid)
2938{
2939 ALOGV("releaseAudioPatch() patch %d", handle);
2940
2941 ssize_t index = mAudioPatches.indexOfKey(handle);
2942
2943 if (index < 0) {
2944 return BAD_VALUE;
2945 }
2946 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2947 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2948 mUidCached, patchDesc->mUid, uid);
2949 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2950 return INVALID_OPERATION;
2951 }
2952
2953 struct audio_patch *patch = &patchDesc->mPatch;
2954 patchDesc->mUid = mUidCached;
2955 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002956 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002957 if (outputDesc == NULL) {
2958 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2959 return BAD_VALUE;
2960 }
2961
Eric Laurentc75307b2015-03-17 15:29:32 -07002962 setOutputDevice(outputDesc,
2963 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002964 true,
2965 0,
2966 NULL);
2967 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2968 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002969 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002970 if (inputDesc == NULL) {
2971 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2972 return BAD_VALUE;
2973 }
2974 setInputDevice(inputDesc->mIoHandle,
Eric Laurentfb66dd92016-01-28 18:32:03 -08002975 getNewInputDevice(inputDesc),
Eric Laurent6a94d692014-05-20 11:18:06 -07002976 true,
2977 NULL);
2978 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002979 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2980 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2981 status, patchDesc->mAfPatchHandle);
2982 removeAudioPatch(patchDesc->mHandle);
2983 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002984 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002985 } else {
2986 return BAD_VALUE;
2987 }
2988 } else {
2989 return BAD_VALUE;
2990 }
2991 return NO_ERROR;
2992}
2993
2994status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2995 struct audio_patch *patches,
2996 unsigned int *generation)
2997{
François Gaffie53615e22015-03-19 09:24:12 +01002998 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002999 return BAD_VALUE;
3000 }
Eric Laurent6a94d692014-05-20 11:18:06 -07003001 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01003002 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07003003}
3004
Eric Laurente1715a42014-05-20 11:30:42 -07003005status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07003006{
Eric Laurente1715a42014-05-20 11:30:42 -07003007 ALOGV("setAudioPortConfig()");
3008
3009 if (config == NULL) {
3010 return BAD_VALUE;
3011 }
3012 ALOGV("setAudioPortConfig() on port handle %d", config->id);
3013 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07003014 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
3015 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07003016 }
3017
Eric Laurenta121f902014-06-03 13:32:54 -07003018 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07003019 if (config->type == AUDIO_PORT_TYPE_MIX) {
3020 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003021 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07003022 if (outputDesc == NULL) {
3023 return BAD_VALUE;
3024 }
Eric Laurent84c70242014-06-23 08:46:27 -07003025 ALOG_ASSERT(!outputDesc->isDuplicated(),
3026 "setAudioPortConfig() called on duplicated output %d",
3027 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07003028 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07003029 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01003030 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07003031 if (inputDesc == NULL) {
3032 return BAD_VALUE;
3033 }
Eric Laurenta121f902014-06-03 13:32:54 -07003034 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07003035 } else {
3036 return BAD_VALUE;
3037 }
3038 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
3039 sp<DeviceDescriptor> deviceDesc;
3040 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
3041 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
3042 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
3043 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
3044 } else {
3045 return BAD_VALUE;
3046 }
3047 if (deviceDesc == NULL) {
3048 return BAD_VALUE;
3049 }
Eric Laurenta121f902014-06-03 13:32:54 -07003050 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07003051 } else {
3052 return BAD_VALUE;
3053 }
3054
Eric Laurenta121f902014-06-03 13:32:54 -07003055 struct audio_port_config backupConfig;
3056 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
3057 if (status == NO_ERROR) {
3058 struct audio_port_config newConfig;
3059 audioPortConfig->toAudioPortConfig(&newConfig, config);
3060 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07003061 }
Eric Laurenta121f902014-06-03 13:32:54 -07003062 if (status != NO_ERROR) {
3063 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07003064 }
Eric Laurente1715a42014-05-20 11:30:42 -07003065
3066 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07003067}
3068
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003069void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
3070{
Eric Laurentd60560a2015-04-10 11:31:20 -07003071 clearAudioSources(uid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003072 clearAudioPatches(uid);
3073 clearSessionRoutes(uid);
3074}
3075
Eric Laurent6a94d692014-05-20 11:18:06 -07003076void AudioPolicyManager::clearAudioPatches(uid_t uid)
3077{
Eric Laurent0add0fd2014-12-04 18:58:14 -08003078 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003079 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
3080 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08003081 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07003082 }
3083 }
3084}
3085
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003086void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
3087 audio_io_handle_t ouptutToSkip)
3088{
3089 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
3090 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
3091 for (size_t j = 0; j < mOutputs.size(); j++) {
3092 if (mOutputs.keyAt(j) == ouptutToSkip) {
3093 continue;
3094 }
3095 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
3096 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
3097 continue;
3098 }
3099 // If the default device for this strategy is on another output mix,
3100 // invalidate all tracks in this strategy to force re connection.
3101 // Otherwise select new device on the output mix.
3102 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
Eric Laurent794fde22016-03-11 09:50:45 -08003103 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003104 if (getStrategy((audio_stream_type_t)stream) == strategy) {
3105 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
3106 }
3107 }
3108 } else {
3109 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
3110 setOutputDevice(outputDesc, newDevice, false);
3111 }
3112 }
3113}
3114
3115void AudioPolicyManager::clearSessionRoutes(uid_t uid)
3116{
3117 // remove output routes associated with this uid
3118 SortedVector<routing_strategy> affectedStrategies;
3119 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
3120 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
3121 if (route->mUid == uid) {
3122 mOutputRoutes.removeItemsAt(i);
3123 if (route->mDeviceDescriptor != 0) {
3124 affectedStrategies.add(getStrategy(route->mStreamType));
3125 }
3126 }
3127 }
3128 // reroute outputs if necessary
3129 for (size_t i = 0; i < affectedStrategies.size(); i++) {
3130 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
3131 }
3132
3133 // remove input routes associated with this uid
3134 SortedVector<audio_source_t> affectedSources;
3135 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
3136 sp<SessionRoute> route = mInputRoutes.valueAt(i);
3137 if (route->mUid == uid) {
3138 mInputRoutes.removeItemsAt(i);
3139 if (route->mDeviceDescriptor != 0) {
3140 affectedSources.add(route->mSource);
3141 }
3142 }
3143 }
3144 // reroute inputs if necessary
3145 SortedVector<audio_io_handle_t> inputsToClose;
3146 for (size_t i = 0; i < mInputs.size(); i++) {
3147 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08003148 if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003149 inputsToClose.add(inputDesc->mIoHandle);
3150 }
3151 }
3152 for (size_t i = 0; i < inputsToClose.size(); i++) {
3153 closeInput(inputsToClose[i]);
3154 }
3155}
3156
Eric Laurentd60560a2015-04-10 11:31:20 -07003157void AudioPolicyManager::clearAudioSources(uid_t uid)
3158{
3159 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
3160 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3161 if (sourceDesc->mUid == uid) {
3162 stopAudioSource(mAudioSources.keyAt(i));
3163 }
3164 }
3165}
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003166
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07003167status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
3168 audio_io_handle_t *ioHandle,
3169 audio_devices_t *device)
3170{
Glenn Kastenf0c6d7d2016-02-26 10:44:04 -08003171 *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
3172 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
Eric Laurentc73ca6e2014-12-12 14:34:22 -08003173 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07003174
François Gaffiedf372692015-03-19 10:43:27 +01003175 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07003176}
3177
Eric Laurentd60560a2015-04-10 11:31:20 -07003178status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source,
3179 const audio_attributes_t *attributes,
Glenn Kasten559d4392016-03-29 13:42:57 -07003180 audio_patch_handle_t *handle,
Eric Laurentd60560a2015-04-10 11:31:20 -07003181 uid_t uid)
Eric Laurent554a2772015-04-10 11:29:24 -07003182{
Eric Laurentd60560a2015-04-10 11:31:20 -07003183 ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle);
3184 if (source == NULL || attributes == NULL || handle == NULL) {
3185 return BAD_VALUE;
3186 }
3187
Glenn Kasten559d4392016-03-29 13:42:57 -07003188 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentd60560a2015-04-10 11:31:20 -07003189
3190 if (source->role != AUDIO_PORT_ROLE_SOURCE ||
3191 source->type != AUDIO_PORT_TYPE_DEVICE) {
3192 ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type);
3193 return INVALID_OPERATION;
3194 }
3195
3196 sp<DeviceDescriptor> srcDeviceDesc =
3197 mAvailableInputDevices.getDevice(source->ext.device.type,
3198 String8(source->ext.device.address));
3199 if (srcDeviceDesc == 0) {
3200 ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type);
3201 return BAD_VALUE;
3202 }
3203 sp<AudioSourceDescriptor> sourceDesc =
3204 new AudioSourceDescriptor(srcDeviceDesc, attributes, uid);
3205
3206 struct audio_patch dummyPatch;
3207 sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid);
3208 sourceDesc->mPatchDesc = patchDesc;
3209
3210 status_t status = connectAudioSource(sourceDesc);
3211 if (status == NO_ERROR) {
3212 mAudioSources.add(sourceDesc->getHandle(), sourceDesc);
3213 *handle = sourceDesc->getHandle();
3214 }
3215 return status;
3216}
3217
3218status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3219{
3220 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3221
3222 // make sure we only have one patch per source.
3223 disconnectAudioSource(sourceDesc);
3224
3225 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
Eric Laurent28d09f02016-03-08 10:43:05 -08003226 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003227 sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice;
3228
3229 audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true);
3230 sp<DeviceDescriptor> sinkDeviceDesc =
3231 mAvailableOutputDevices.getDevice(sinkDevice, String8(""));
3232
3233 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3234 struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch;
3235
3236 if (srcDeviceDesc->getAudioPort()->mModule->getHandle() ==
3237 sinkDeviceDesc->getAudioPort()->mModule->getHandle() &&
Mikhail Naganov9ee05402016-10-13 15:58:17 -07003238 srcDeviceDesc->getAudioPort()->mModule->getHalVersionMajor() >= 3 &&
Eric Laurentd60560a2015-04-10 11:31:20 -07003239 srcDeviceDesc->getAudioPort()->mGains.size() > 0) {
3240 ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__);
3241 // create patch between src device and output device
3242 // create Hwoutput and add to mHwOutputs
3243 } else {
3244 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs);
3245 audio_io_handle_t output =
3246 selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID);
3247 if (output == AUDIO_IO_HANDLE_NONE) {
3248 ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice);
3249 return INVALID_OPERATION;
3250 }
3251 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
3252 if (outputDesc->isDuplicated()) {
3253 ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice);
3254 return INVALID_OPERATION;
3255 }
3256 // create a special patch with no sink and two sources:
3257 // - the second source indicates to PatchPanel through which output mix this patch should
3258 // be connected as well as the stream type for volume control
3259 // - the sink is defined by whatever output device is currently selected for the output
3260 // though which this patch is routed.
3261 patch->num_sinks = 0;
3262 patch->num_sources = 2;
3263 srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL);
3264 outputDesc->toAudioPortConfig(&patch->sources[1], NULL);
3265 patch->sources[1].ext.mix.usecase.stream = stream;
3266 status_t status = mpClientInterface->createAudioPatch(patch,
3267 &afPatchHandle,
3268 0);
3269 ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__,
3270 status, afPatchHandle);
3271 if (status != NO_ERROR) {
3272 ALOGW("%s patch panel could not connect device patch, error %d",
3273 __FUNCTION__, status);
3274 return INVALID_OPERATION;
3275 }
3276 uint32_t delayMs = 0;
Eric Laurentc40d9692016-04-13 19:14:13 -07003277 status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs);
Eric Laurentd60560a2015-04-10 11:31:20 -07003278
3279 if (status != NO_ERROR) {
3280 mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0);
3281 return status;
3282 }
3283 sourceDesc->mSwOutput = outputDesc;
3284 if (delayMs != 0) {
3285 usleep(delayMs * 1000);
3286 }
3287 }
3288
3289 sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle;
3290 addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc);
3291
3292 return NO_ERROR;
Eric Laurent554a2772015-04-10 11:29:24 -07003293}
3294
Glenn Kasten559d4392016-03-29 13:42:57 -07003295status_t AudioPolicyManager::stopAudioSource(audio_patch_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07003296{
Eric Laurentd60560a2015-04-10 11:31:20 -07003297 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle);
3298 ALOGV("%s handle %d", __FUNCTION__, handle);
3299 if (sourceDesc == 0) {
3300 ALOGW("%s unknown source for handle %d", __FUNCTION__, handle);
3301 return BAD_VALUE;
3302 }
3303 status_t status = disconnectAudioSource(sourceDesc);
3304
3305 mAudioSources.removeItem(handle);
3306 return status;
3307}
3308
Andy Hung2ddee192015-12-18 17:34:44 -08003309status_t AudioPolicyManager::setMasterMono(bool mono)
3310{
3311 if (mMasterMono == mono) {
3312 return NO_ERROR;
3313 }
3314 mMasterMono = mono;
3315 // if enabling mono we close all offloaded devices, which will invalidate the
3316 // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible
3317 // for recreating the new AudioTrack as non-offloaded PCM.
3318 //
3319 // If disabling mono, we leave all tracks as is: we don't know which clients
3320 // and tracks are able to be recreated as offloaded. The next "song" should
3321 // play back offloaded.
3322 if (mMasterMono) {
3323 Vector<audio_io_handle_t> offloaded;
3324 for (size_t i = 0; i < mOutputs.size(); ++i) {
3325 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
3326 if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
3327 offloaded.push(desc->mIoHandle);
3328 }
3329 }
3330 for (size_t i = 0; i < offloaded.size(); ++i) {
3331 closeOutput(offloaded[i]);
3332 }
3333 }
3334 // update master mono for all remaining outputs
3335 for (size_t i = 0; i < mOutputs.size(); ++i) {
3336 updateMono(mOutputs.keyAt(i));
3337 }
3338 return NO_ERROR;
3339}
3340
3341status_t AudioPolicyManager::getMasterMono(bool *mono)
3342{
3343 *mono = mMasterMono;
3344 return NO_ERROR;
3345}
3346
Eric Laurentd60560a2015-04-10 11:31:20 -07003347status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3348{
3349 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3350
3351 sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle);
3352 if (patchDesc == 0) {
3353 ALOGW("%s source has no patch with handle %d", __FUNCTION__,
3354 sourceDesc->mPatchDesc->mHandle);
3355 return BAD_VALUE;
3356 }
3357 removeAudioPatch(sourceDesc->mPatchDesc->mHandle);
3358
Eric Laurent28d09f02016-03-08 10:43:05 -08003359 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003360 sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote();
3361 if (swOutputDesc != 0) {
3362 stopSource(swOutputDesc, stream, false);
3363 mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3364 } else {
3365 sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote();
3366 if (hwOutputDesc != 0) {
3367 // release patch between src device and output device
3368 // close Hwoutput and remove from mHwOutputs
3369 } else {
3370 ALOGW("%s source has neither SW nor HW output", __FUNCTION__);
3371 }
3372 }
3373 return NO_ERROR;
3374}
3375
3376sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput(
3377 audio_io_handle_t output, routing_strategy strategy)
3378{
3379 sp<AudioSourceDescriptor> source;
3380 for (size_t i = 0; i < mAudioSources.size(); i++) {
3381 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3382 routing_strategy sourceStrategy =
3383 (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
3384 sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote();
3385 if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) {
3386 source = sourceDesc;
3387 break;
3388 }
3389 }
3390 return source;
Eric Laurent554a2772015-04-10 11:29:24 -07003391}
3392
Eric Laurente552edb2014-03-10 17:42:56 -07003393// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07003394// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07003395// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07003396uint32_t AudioPolicyManager::nextAudioPortGeneration()
3397{
3398 return android_atomic_inc(&mAudioPortGeneration);
3399}
3400
Jaekyun Seok0d4a6af2017-02-17 17:10:17 +09003401#ifdef USE_XML_AUDIO_POLICY_CONF
3402// Treblized audio policy xml config will be located in /odm/etc or /vendor/etc.
3403static const char *kConfigLocationList[] =
3404 {"/odm/etc", "/vendor/etc", "/system/etc"};
3405static const int kConfigLocationListSize =
3406 (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0]));
3407
3408static status_t deserializeAudioPolicyXmlConfig(AudioPolicyConfig &config) {
3409 char audioPolicyXmlConfigFile[AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH];
3410 status_t ret;
3411
3412 for (int i = 0; i < kConfigLocationListSize; i++) {
3413 PolicySerializer serializer;
3414 snprintf(audioPolicyXmlConfigFile,
3415 sizeof(audioPolicyXmlConfigFile),
3416 "%s/%s",
3417 kConfigLocationList[i],
3418 AUDIO_POLICY_XML_CONFIG_FILE_NAME);
3419 ret = serializer.deserialize(audioPolicyXmlConfigFile, config);
3420 if (ret == NO_ERROR) {
3421 break;
3422 }
3423 }
3424 return ret;
3425}
3426#endif
3427
Eric Laurente0720872014-03-11 09:30:41 -07003428AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07003429 :
3430#ifdef AUDIO_POLICY_TEST
3431 Thread(false),
3432#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07003433 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07003434 mA2dpSuspended(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003435 mAudioPortGeneration(1),
3436 mBeaconMuteRefCount(0),
3437 mBeaconPlayingRefCount(0),
Eric Laurent9459fb02015-08-12 18:36:32 -07003438 mBeaconMuted(false),
Andy Hung2ddee192015-12-18 17:34:44 -08003439 mTtsOutputAvailable(false),
3440 mMasterMono(false)
Eric Laurente552edb2014-03-10 17:42:56 -07003441{
François Gaffied1ab2bd2015-12-02 18:20:06 +01003442 mUidCached = getuid();
3443 mpClientInterface = clientInterface;
3444
3445 // TODO: remove when legacy conf file is removed. true on devices that use DRC on the
3446 // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly.
3447 // Note: remove also speaker_drc_enabled from global configuration of XML config file.
3448 bool speakerDrcEnabled = false;
3449
3450#ifdef USE_XML_AUDIO_POLICY_CONF
3451 mVolumeCurves = new VolumeCurvesCollection();
3452 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3453 mDefaultOutputDevice, speakerDrcEnabled,
3454 static_cast<VolumeCurvesCollection *>(mVolumeCurves));
Jaekyun Seok0d4a6af2017-02-17 17:10:17 +09003455 if (deserializeAudioPolicyXmlConfig(config) != NO_ERROR) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01003456#else
3457 mVolumeCurves = new StreamDescriptorCollection();
3458 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3459 mDefaultOutputDevice, speakerDrcEnabled);
3460 if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) &&
3461 (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) {
3462#endif
3463 ALOGE("could not load audio policy configuration file, setting defaults");
3464 config.setDefault();
3465 }
3466 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
3467 mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled);
3468
3469 // Once policy config has been parsed, retrieve an instance of the engine and initialize it.
François Gaffie2110e042015-03-24 08:41:51 +01003470 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
3471 if (!engineInstance) {
3472 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
3473 return;
3474 }
3475 // Retrieve the Policy Manager Interface
3476 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
3477 if (mEngine == NULL) {
3478 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
3479 return;
3480 }
3481 mEngine->setObserver(this);
3482 status_t status = mEngine->initCheck();
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07003483 (void) status;
François Gaffie2110e042015-03-24 08:41:51 +01003484 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
3485
Eric Laurent3a4311c2014-03-17 12:00:47 -07003486 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07003487 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07003488 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
3489 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07003490 for (size_t i = 0; i < mHwModules.size(); i++) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003491 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003492 if (mHwModules[i]->mHandle == 0) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003493 ALOGW("could not open HW module %s", mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003494 continue;
3495 }
3496 // open all output streams needed to access attached devices
3497 // except for direct output streams that are only opened when they are actually
3498 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07003499 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07003500 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3501 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003502 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003503
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003504 if (!outProfile->hasSupportedDevices()) {
3505 ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003506 continue;
3507 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003508 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) {
Eric Laurent9459fb02015-08-12 18:36:32 -07003509 mTtsOutputAvailable = true;
3510 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003511
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003512 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
Eric Laurentd78f1532014-09-16 16:38:20 -07003513 continue;
3514 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003515 audio_devices_t profileType = outProfile->getSupportedDevicesType();
François Gaffie53615e22015-03-19 09:24:12 +01003516 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
3517 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07003518 } else {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003519 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003520 // outputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003521 profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes);
Eric Laurente552edb2014-03-10 17:42:56 -07003522 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003523 if ((profileType & outputDeviceTypes) == 0) {
3524 continue;
3525 }
Eric Laurentc75307b2015-03-17 15:29:32 -07003526 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
3527 mpClientInterface);
Eric Laurentc40d9692016-04-13 19:14:13 -07003528 const DeviceVector &supportedDevices = outProfile->getSupportedDevices();
3529 const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType);
3530 String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress
3531 : String8("");
Eric Laurentd78f1532014-09-16 16:38:20 -07003532
3533 outputDesc->mDevice = profileType;
3534 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3535 config.sample_rate = outputDesc->mSamplingRate;
3536 config.channel_mask = outputDesc->mChannelMask;
3537 config.format = outputDesc->mFormat;
3538 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003539 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003540 &output,
3541 &config,
3542 &outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003543 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003544 &outputDesc->mLatency,
3545 outputDesc->mFlags);
3546
3547 if (status != NO_ERROR) {
3548 ALOGW("Cannot open output stream for device %08x on hw module %s",
3549 outputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003550 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003551 } else {
3552 outputDesc->mSamplingRate = config.sample_rate;
3553 outputDesc->mChannelMask = config.channel_mask;
3554 outputDesc->mFormat = config.format;
3555
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003556 for (size_t k = 0; k < supportedDevices.size(); k++) {
3557 ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003558 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08003559 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
3560 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003561 }
3562 }
3563 if (mPrimaryOutput == 0 &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003564 outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003565 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07003566 }
3567 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003568 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07003569 outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003570 true,
3571 0,
3572 NULL,
3573 address.string());
Eric Laurentd78f1532014-09-16 16:38:20 -07003574 }
Eric Laurente552edb2014-03-10 17:42:56 -07003575 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003576 // open input streams needed to access attached devices to validate
3577 // mAvailableInputDevices list
3578 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
3579 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003580 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003581
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003582 if (!inProfile->hasSupportedDevices()) {
3583 ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003584 continue;
3585 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003586 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003587 // inputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003588 audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes);
3589
Eric Laurentd78f1532014-09-16 16:38:20 -07003590 if ((profileType & inputDeviceTypes) == 0) {
3591 continue;
3592 }
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08003593 sp<AudioInputDescriptor> inputDesc =
3594 new AudioInputDescriptor(inProfile);
Eric Laurentd78f1532014-09-16 16:38:20 -07003595
Eric Laurentd78f1532014-09-16 16:38:20 -07003596 inputDesc->mDevice = profileType;
3597
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003598 // find the address
3599 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
3600 // the inputs vector must be of size 1, but we don't want to crash here
3601 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
3602 : String8("");
3603 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
3604 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
3605
Eric Laurentd78f1532014-09-16 16:38:20 -07003606 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3607 config.sample_rate = inputDesc->mSamplingRate;
3608 config.channel_mask = inputDesc->mChannelMask;
3609 config.format = inputDesc->mFormat;
3610 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003611 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003612 &input,
3613 &config,
3614 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003615 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003616 AUDIO_SOURCE_MIC,
3617 AUDIO_INPUT_FLAG_NONE);
3618
3619 if (status == NO_ERROR) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003620 const DeviceVector &supportedDevices = inProfile->getSupportedDevices();
3621 for (size_t k = 0; k < supportedDevices.size(); k++) {
3622 ssize_t index = mAvailableInputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003623 // give a valid ID to an attached device once confirmed it is reachable
Eric Laurent45aabc32015-08-06 09:11:13 -07003624 if (index >= 0) {
3625 sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index];
3626 if (!devDesc->isAttached()) {
3627 devDesc->attach(mHwModules[i]);
3628 devDesc->importAudioPort(inProfile);
3629 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003630 }
3631 }
3632 mpClientInterface->closeInput(input);
3633 } else {
3634 ALOGW("Cannot open input stream for device %08x on hw module %s",
3635 inputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003636 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003637 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003638 }
3639 }
3640 // make sure all attached devices have been allocated a unique ID
3641 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003642 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003643 ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003644 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
3645 continue;
3646 }
François Gaffie2110e042015-03-24 08:41:51 +01003647 // The device is now validated and can be appended to the available devices of the engine
3648 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
3649 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003650 i++;
3651 }
3652 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003653 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01003654 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003655 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
3656 continue;
3657 }
François Gaffie2110e042015-03-24 08:41:51 +01003658 // The device is now validated and can be appended to the available devices of the engine
3659 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
3660 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003661 i++;
3662 }
3663 // make sure default device is reachable
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003664 if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01003665 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003666 }
Eric Laurente552edb2014-03-10 17:42:56 -07003667
3668 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
3669
3670 updateDevicesAndOutputs();
3671
3672#ifdef AUDIO_POLICY_TEST
3673 if (mPrimaryOutput != 0) {
3674 AudioParameter outputCmd = AudioParameter();
3675 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003676 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07003677
3678 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
3679 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07003680 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
3681 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003682 mTestLatencyMs = 0;
3683 mCurOutput = 0;
3684 mDirectOutput = false;
3685 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3686 mTestOutputs[i] = 0;
3687 }
3688
3689 const size_t SIZE = 256;
3690 char buffer[SIZE];
3691 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
3692 run(buffer, ANDROID_PRIORITY_AUDIO);
3693 }
3694#endif //AUDIO_POLICY_TEST
3695}
3696
Eric Laurente0720872014-03-11 09:30:41 -07003697AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07003698{
3699#ifdef AUDIO_POLICY_TEST
3700 exit();
3701#endif //AUDIO_POLICY_TEST
3702 for (size_t i = 0; i < mOutputs.size(); i++) {
3703 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003704 }
3705 for (size_t i = 0; i < mInputs.size(); i++) {
3706 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003707 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003708 mAvailableOutputDevices.clear();
3709 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003710 mOutputs.clear();
3711 mInputs.clear();
3712 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07003713}
3714
Eric Laurente0720872014-03-11 09:30:41 -07003715status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07003716{
Eric Laurent87ffa392015-05-22 10:32:38 -07003717 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003718}
3719
3720#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07003721bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07003722{
3723 ALOGV("entering threadLoop()");
3724 while (!exitPending())
3725 {
3726 String8 command;
3727 int valueInt;
3728 String8 value;
3729
3730 Mutex::Autolock _l(mLock);
3731 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
3732
3733 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
3734 AudioParameter param = AudioParameter(command);
3735
3736 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
3737 valueInt != 0) {
3738 ALOGV("Test command %s received", command.string());
3739 String8 target;
3740 if (param.get(String8("target"), target) != NO_ERROR) {
3741 target = "Manager";
3742 }
3743 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
3744 param.remove(String8("test_cmd_policy_output"));
3745 mCurOutput = valueInt;
3746 }
3747 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
3748 param.remove(String8("test_cmd_policy_direct"));
3749 if (value == "false") {
3750 mDirectOutput = false;
3751 } else if (value == "true") {
3752 mDirectOutput = true;
3753 }
3754 }
3755 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
3756 param.remove(String8("test_cmd_policy_input"));
3757 mTestInput = valueInt;
3758 }
3759
3760 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
3761 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07003762 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07003763 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003764 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003765 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003766 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003767 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003768 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07003769 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003770 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07003771 if (target == "Manager") {
3772 mTestFormat = format;
3773 } else if (mTestOutputs[mCurOutput] != 0) {
3774 AudioParameter outputParam = AudioParameter();
Mikhail Naganov388360c2016-10-17 17:09:41 -07003775 outputParam.addInt(String8(AudioParameter::keyStreamSupportedFormats), format);
Eric Laurente552edb2014-03-10 17:42:56 -07003776 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3777 }
3778 }
3779 }
3780 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
3781 param.remove(String8("test_cmd_policy_channels"));
3782 int channels = 0;
3783
3784 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003785 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003786 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003787 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07003788 }
3789 if (channels != 0) {
3790 if (target == "Manager") {
3791 mTestChannels = channels;
3792 } else if (mTestOutputs[mCurOutput] != 0) {
3793 AudioParameter outputParam = AudioParameter();
Mikhail Naganov388360c2016-10-17 17:09:41 -07003794 outputParam.addInt(String8(AudioParameter::keyStreamSupportedChannels), channels);
Eric Laurente552edb2014-03-10 17:42:56 -07003795 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3796 }
3797 }
3798 }
3799 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
3800 param.remove(String8("test_cmd_policy_sampleRate"));
3801 if (valueInt >= 0 && valueInt <= 96000) {
3802 int samplingRate = valueInt;
3803 if (target == "Manager") {
3804 mTestSamplingRate = samplingRate;
3805 } else if (mTestOutputs[mCurOutput] != 0) {
3806 AudioParameter outputParam = AudioParameter();
Mikhail Naganov388360c2016-10-17 17:09:41 -07003807 outputParam.addInt(String8(AudioParameter::keyStreamSupportedSamplingRates), samplingRate);
Eric Laurente552edb2014-03-10 17:42:56 -07003808 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3809 }
3810 }
3811 }
3812
3813 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
3814 param.remove(String8("test_cmd_policy_reopen"));
3815
Eric Laurentc75307b2015-03-17 15:29:32 -07003816 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07003817
Eric Laurentc75307b2015-03-17 15:29:32 -07003818 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07003819
Eric Laurentc75307b2015-03-17 15:29:32 -07003820 removeOutput(mPrimaryOutput->mIoHandle);
3821 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
3822 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003823 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003824 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3825 config.sample_rate = outputDesc->mSamplingRate;
3826 config.channel_mask = outputDesc->mChannelMask;
3827 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07003828 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003829 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07003830 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07003831 &config,
3832 &outputDesc->mDevice,
3833 String8(""),
3834 &outputDesc->mLatency,
3835 outputDesc->mFlags);
3836 if (status != NO_ERROR) {
3837 ALOGE("Failed to reopen hardware output stream, "
3838 "samplingRate: %d, format %d, channels %d",
3839 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003840 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003841 outputDesc->mSamplingRate = config.sample_rate;
3842 outputDesc->mChannelMask = config.channel_mask;
3843 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07003844 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07003845 AudioParameter outputCmd = AudioParameter();
3846 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003847 mpClientInterface->setParameters(handle, outputCmd.toString());
3848 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07003849 }
3850 }
3851
3852
3853 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3854 }
3855 }
3856 return false;
3857}
3858
Eric Laurente0720872014-03-11 09:30:41 -07003859void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003860{
3861 {
3862 AutoMutex _l(mLock);
3863 requestExit();
3864 mWaitWorkCV.signal();
3865 }
3866 requestExitAndWait();
3867}
3868
Eric Laurente0720872014-03-11 09:30:41 -07003869int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003870{
3871 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3872 if (output == mTestOutputs[i]) return i;
3873 }
3874 return 0;
3875}
3876#endif //AUDIO_POLICY_TEST
3877
3878// ---
3879
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003880void AudioPolicyManager::addOutput(audio_io_handle_t output, const sp<SwAudioOutputDescriptor>& outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003881{
François Gaffie98cc1912015-03-18 17:52:40 +01003882 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003883 mOutputs.add(output, outputDesc);
Andy Hung2ddee192015-12-18 17:34:44 -08003884 updateMono(output); // update mono status when adding to output list
Eric Laurent6a94d692014-05-20 11:18:06 -07003885 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003886}
3887
François Gaffie53615e22015-03-19 09:24:12 +01003888void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3889{
3890 mOutputs.removeItem(output);
3891}
3892
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003893void AudioPolicyManager::addInput(audio_io_handle_t input, const sp<AudioInputDescriptor>& inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003894{
François Gaffie98cc1912015-03-18 17:52:40 +01003895 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003896 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003897 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003898}
Eric Laurente552edb2014-03-10 17:42:56 -07003899
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003900void AudioPolicyManager::findIoHandlesByAddress(const sp<SwAudioOutputDescriptor>& desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003901 const audio_devices_t device /*in*/,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003902 const String8& address /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003903 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003904 sp<DeviceDescriptor> devDesc =
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003905 desc->mProfile->getSupportedDeviceByAddress(device, address);
keunyoung3190e672014-12-30 13:00:52 -08003906 if (devDesc != 0) {
3907 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3908 desc->mIoHandle, address.string());
3909 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003910 }
3911}
3912
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003913status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003914 audio_policy_dev_state_t state,
3915 SortedVector<audio_io_handle_t>& outputs,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003916 const String8& address)
Eric Laurente552edb2014-03-10 17:42:56 -07003917{
François Gaffie53615e22015-03-19 09:24:12 +01003918 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003919 sp<SwAudioOutputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003920
3921 if (audio_device_is_digital(device)) {
3922 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003923 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003924 }
Eric Laurente552edb2014-03-10 17:42:56 -07003925
Eric Laurent3b73df72014-03-11 09:06:29 -07003926 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003927 // first list already open outputs that can be routed to this device
3928 for (size_t i = 0; i < mOutputs.size(); i++) {
3929 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003930 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003931 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003932 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3933 outputs.add(mOutputs.keyAt(i));
3934 } else {
3935 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003936 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003937 }
Eric Laurente552edb2014-03-10 17:42:56 -07003938 }
3939 }
3940 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003941 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003942 for (size_t i = 0; i < mHwModules.size(); i++)
3943 {
3944 if (mHwModules[i]->mHandle == 0) {
3945 continue;
3946 }
3947 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3948 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003949 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003950 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003951 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003952 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003953 profiles.add(profile);
3954 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3955 }
Eric Laurente552edb2014-03-10 17:42:56 -07003956 }
3957 }
3958 }
3959
Eric Laurent7b279bb2015-12-14 10:18:23 -08003960 ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size());
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003961
Eric Laurente552edb2014-03-10 17:42:56 -07003962 if (profiles.isEmpty() && outputs.isEmpty()) {
3963 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3964 return BAD_VALUE;
3965 }
3966
3967 // open outputs for matching profiles if needed. Direct outputs are also opened to
3968 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3969 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003970 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003971
3972 // nothing to do if one output is already opened for this profile
3973 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003974 for (j = 0; j < outputs.size(); j++) {
3975 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003976 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003977 // matching profile: save the sample rates, format and channel masks supported
3978 // by the profile in our device descriptor
Paul McLean9080a4c2015-06-18 08:24:02 -07003979 if (audio_device_is_digital(device)) {
3980 devDesc->importAudioPort(profile);
3981 }
Eric Laurente552edb2014-03-10 17:42:56 -07003982 break;
3983 }
3984 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003985 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003986 continue;
3987 }
3988
Eric Laurent83b88082014-06-20 18:31:16 -07003989 ALOGV("opening output for device %08x with params %s profile %p",
3990 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07003991 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003992 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003993 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3994 config.sample_rate = desc->mSamplingRate;
3995 config.channel_mask = desc->mChannelMask;
3996 config.format = desc->mFormat;
3997 config.offload_info.sample_rate = desc->mSamplingRate;
3998 config.offload_info.channel_mask = desc->mChannelMask;
3999 config.offload_info.format = desc->mFormat;
4000 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07004001 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07004002 &output,
4003 &config,
4004 &desc->mDevice,
4005 address,
4006 &desc->mLatency,
4007 desc->mFlags);
4008 if (status == NO_ERROR) {
4009 desc->mSamplingRate = config.sample_rate;
4010 desc->mChannelMask = config.channel_mask;
4011 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07004012
Eric Laurentd4692962014-05-05 18:13:44 -07004013 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07004014 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07004015 char *param = audio_device_address_to_parameter(device, address);
4016 mpClientInterface->setParameters(output, String8(param));
4017 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07004018 }
Phil Burk00eeb322016-03-31 12:41:00 -07004019 updateAudioProfiles(device, output, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01004020 if (!profile->hasValidAudioProfile()) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004021 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07004022 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004023 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01004024 } else if (profile->hasDynamicAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07004025 mpClientInterface->closeOutput(output);
Phil Burk702b1052016-03-02 16:38:26 -08004026 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01004027 profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004028 config.offload_info.sample_rate = config.sample_rate;
4029 config.offload_info.channel_mask = config.channel_mask;
4030 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07004031 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07004032 &output,
4033 &config,
4034 &desc->mDevice,
4035 address,
4036 &desc->mLatency,
4037 desc->mFlags);
4038 if (status == NO_ERROR) {
4039 desc->mSamplingRate = config.sample_rate;
4040 desc->mChannelMask = config.channel_mask;
4041 desc->mFormat = config.format;
4042 } else {
4043 output = AUDIO_IO_HANDLE_NONE;
4044 }
Eric Laurentd4692962014-05-05 18:13:44 -07004045 }
4046
Eric Laurentcf2c0212014-07-25 16:20:43 -07004047 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07004048 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01004049 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01004050 sp<AudioPolicyMix> policyMix;
4051 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004052 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
4053 address.string());
4054 }
François Gaffie036e1e92015-03-19 10:16:24 +01004055 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07004056 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01004057
Eric Laurent87ffa392015-05-22 10:32:38 -07004058 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
4059 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08004060 // no duplicated output for direct outputs and
4061 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07004062 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07004063
Eric Laurentd4692962014-05-05 18:13:44 -07004064 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07004065 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07004066
Eric Laurentd4692962014-05-05 18:13:44 -07004067 //TODO: configure audio effect output stage here
4068
4069 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07004070 duplicatedOutput =
4071 mpClientInterface->openDuplicateOutput(output,
4072 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004073 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07004074 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07004075 sp<SwAudioOutputDescriptor> dupOutputDesc =
4076 new SwAudioOutputDescriptor(NULL, mpClientInterface);
4077 dupOutputDesc->mOutput1 = mPrimaryOutput;
4078 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07004079 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
4080 dupOutputDesc->mFormat = desc->mFormat;
4081 dupOutputDesc->mChannelMask = desc->mChannelMask;
4082 dupOutputDesc->mLatency = desc->mLatency;
4083 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07004084 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07004085 } else {
4086 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07004087 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07004088 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01004089 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07004090 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07004091 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07004092 }
Eric Laurente552edb2014-03-10 17:42:56 -07004093 }
4094 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07004095 } else {
4096 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07004097 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07004098 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07004099 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004100 profiles.removeAt(profile_index);
4101 profile_index--;
4102 } else {
4103 outputs.add(output);
Paul McLean9080a4c2015-06-18 08:24:02 -07004104 // Load digital format info only for digital devices
4105 if (audio_device_is_digital(device)) {
4106 devDesc->importAudioPort(profile);
4107 }
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07004108
François Gaffie53615e22015-03-19 09:24:12 +01004109 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004110 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
4111 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07004112 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004113 NULL/*patch handle*/, address.string());
4114 }
Eric Laurente552edb2014-03-10 17:42:56 -07004115 ALOGV("checkOutputsForDevice(): adding output %d", output);
4116 }
4117 }
4118
4119 if (profiles.isEmpty()) {
4120 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
4121 return BAD_VALUE;
4122 }
Eric Laurentd4692962014-05-05 18:13:44 -07004123 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07004124 // check if one opened output is not needed any more after disconnecting one device
4125 for (size_t i = 0; i < mOutputs.size(); i++) {
4126 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004127 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004128 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01004129 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07004130 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08004131 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07004132 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004133 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
4134 mOutputs.keyAt(i));
4135 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004136 }
Eric Laurente552edb2014-03-10 17:42:56 -07004137 }
4138 }
Eric Laurentd4692962014-05-05 18:13:44 -07004139 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07004140 for (size_t i = 0; i < mHwModules.size(); i++)
4141 {
4142 if (mHwModules[i]->mHandle == 0) {
4143 continue;
4144 }
4145 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
4146 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004147 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004148 if (profile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07004149 ALOGV("checkOutputsForDevice(): "
4150 "clearing direct output profile %zu on module %zu", j, i);
François Gaffie112b0af2015-11-19 16:13:25 +01004151 profile->clearAudioProfiles();
Eric Laurente552edb2014-03-10 17:42:56 -07004152 }
4153 }
4154 }
4155 }
4156 return NO_ERROR;
4157}
4158
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004159status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01004160 audio_policy_dev_state_t state,
4161 SortedVector<audio_io_handle_t>& inputs,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004162 const String8& address)
Eric Laurentd4692962014-05-05 18:13:44 -07004163{
Paul McLean9080a4c2015-06-18 08:24:02 -07004164 audio_devices_t device = devDesc->type();
Eric Laurent1f2f2232014-06-02 12:01:23 -07004165 sp<AudioInputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07004166
4167 if (audio_device_is_digital(device)) {
4168 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08004169 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07004170 }
4171
Eric Laurentd4692962014-05-05 18:13:44 -07004172 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
4173 // first list already open inputs that can be routed to this device
4174 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
4175 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004176 if (desc->mProfile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07004177 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
4178 inputs.add(mInputs.keyAt(input_index));
4179 }
4180 }
4181
4182 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07004183 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07004184 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
4185 {
4186 if (mHwModules[module_idx]->mHandle == 0) {
4187 continue;
4188 }
4189 for (size_t profile_index = 0;
4190 profile_index < mHwModules[module_idx]->mInputProfiles.size();
4191 profile_index++)
4192 {
Eric Laurent275e8e92014-11-30 15:14:47 -08004193 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
4194
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004195 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01004196 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004197 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004198 profiles.add(profile);
4199 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
4200 profile_index, module_idx);
4201 }
Eric Laurentd4692962014-05-05 18:13:44 -07004202 }
4203 }
4204 }
4205
4206 if (profiles.isEmpty() && inputs.isEmpty()) {
4207 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
4208 return BAD_VALUE;
4209 }
4210
4211 // open inputs for matching profiles if needed. Direct inputs are also opened to
4212 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
4213 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
4214
Eric Laurent1c333e22014-05-20 10:48:17 -07004215 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07004216 // nothing to do if one input is already opened for this profile
4217 size_t input_index;
4218 for (input_index = 0; input_index < mInputs.size(); input_index++) {
4219 desc = mInputs.valueAt(input_index);
4220 if (desc->mProfile == profile) {
Paul McLean9080a4c2015-06-18 08:24:02 -07004221 if (audio_device_is_digital(device)) {
4222 devDesc->importAudioPort(profile);
4223 }
Eric Laurentd4692962014-05-05 18:13:44 -07004224 break;
4225 }
4226 }
4227 if (input_index != mInputs.size()) {
4228 continue;
4229 }
4230
4231 ALOGV("opening input for device 0x%X with params %s", device, address.string());
4232 desc = new AudioInputDescriptor(profile);
4233 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07004234 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
4235 config.sample_rate = desc->mSamplingRate;
4236 config.channel_mask = desc->mChannelMask;
4237 config.format = desc->mFormat;
4238 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07004239 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07004240 &input,
4241 &config,
4242 &desc->mDevice,
4243 address,
4244 AUDIO_SOURCE_MIC,
4245 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07004246
Eric Laurentcf2c0212014-07-25 16:20:43 -07004247 if (status == NO_ERROR) {
4248 desc->mSamplingRate = config.sample_rate;
4249 desc->mChannelMask = config.channel_mask;
4250 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07004251
Eric Laurentd4692962014-05-05 18:13:44 -07004252 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07004253 char *param = audio_device_address_to_parameter(device, address);
4254 mpClientInterface->setParameters(input, String8(param));
4255 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07004256 }
Phil Burk00eeb322016-03-31 12:41:00 -07004257 updateAudioProfiles(device, input, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01004258 if (!profile->hasValidAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07004259 ALOGW("checkInputsForDevice() direct input missing param");
4260 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004261 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07004262 }
4263
4264 if (input != 0) {
4265 addInput(input, desc);
4266 }
4267 } // endif input != 0
4268
Eric Laurentcf2c0212014-07-25 16:20:43 -07004269 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07004270 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07004271 profiles.removeAt(profile_index);
4272 profile_index--;
4273 } else {
4274 inputs.add(input);
Paul McLean9080a4c2015-06-18 08:24:02 -07004275 if (audio_device_is_digital(device)) {
4276 devDesc->importAudioPort(profile);
4277 }
Eric Laurentd4692962014-05-05 18:13:44 -07004278 ALOGV("checkInputsForDevice(): adding input %d", input);
4279 }
4280 } // end scan profiles
4281
4282 if (profiles.isEmpty()) {
4283 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
4284 return BAD_VALUE;
4285 }
4286 } else {
4287 // Disconnect
4288 // check if one opened input is not needed any more after disconnecting one device
4289 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
4290 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004291 if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) {
Eric Laurentd4692962014-05-05 18:13:44 -07004292 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
4293 mInputs.keyAt(input_index));
4294 inputs.add(mInputs.keyAt(input_index));
4295 }
4296 }
4297 // Clear any profiles associated with the disconnected device.
4298 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
4299 if (mHwModules[module_index]->mHandle == 0) {
4300 continue;
4301 }
4302 for (size_t profile_index = 0;
4303 profile_index < mHwModules[module_index]->mInputProfiles.size();
4304 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004305 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004306 if (profile->supportDevice(device)) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07004307 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07004308 profile_index, module_index);
François Gaffie112b0af2015-11-19 16:13:25 +01004309 profile->clearAudioProfiles();
Eric Laurentd4692962014-05-05 18:13:44 -07004310 }
4311 }
4312 }
4313 } // end disconnect
4314
4315 return NO_ERROR;
4316}
4317
4318
Eric Laurente0720872014-03-11 09:30:41 -07004319void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07004320{
4321 ALOGV("closeOutput(%d)", output);
4322
Eric Laurentc75307b2015-03-17 15:29:32 -07004323 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004324 if (outputDesc == NULL) {
4325 ALOGW("closeOutput() unknown output %d", output);
4326 return;
4327 }
François Gaffie036e1e92015-03-19 10:16:24 +01004328 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08004329
Eric Laurente552edb2014-03-10 17:42:56 -07004330 // look for duplicated outputs connected to the output being removed.
4331 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004332 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07004333 if (dupOutputDesc->isDuplicated() &&
4334 (dupOutputDesc->mOutput1 == outputDesc ||
4335 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004336 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07004337 if (dupOutputDesc->mOutput1 == outputDesc) {
4338 outputDesc2 = dupOutputDesc->mOutput2;
4339 } else {
4340 outputDesc2 = dupOutputDesc->mOutput1;
4341 }
4342 // As all active tracks on duplicated output will be deleted,
4343 // and as they were also referenced on the other output, the reference
4344 // count for their stream type must be adjusted accordingly on
4345 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07004346 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07004347 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07004348 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07004349 }
4350 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
4351 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
4352
4353 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01004354 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07004355 }
4356 }
4357
Eric Laurent05b90f82014-08-27 15:32:29 -07004358 nextAudioPortGeneration();
4359
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004360 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004361 if (index >= 0) {
4362 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07004363 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07004364 mAudioPatches.removeItemsAt(index);
4365 mpClientInterface->onAudioPatchListUpdate();
4366 }
4367
Eric Laurente552edb2014-03-10 17:42:56 -07004368 AudioParameter param;
4369 param.add(String8("closing"), String8("true"));
4370 mpClientInterface->setParameters(output, param.toString());
4371
4372 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01004373 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004374 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07004375}
4376
4377void AudioPolicyManager::closeInput(audio_io_handle_t input)
4378{
4379 ALOGV("closeInput(%d)", input);
4380
4381 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
4382 if (inputDesc == NULL) {
4383 ALOGW("closeInput() unknown input %d", input);
4384 return;
4385 }
4386
Eric Laurent6a94d692014-05-20 11:18:06 -07004387 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07004388
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004389 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004390 if (index >= 0) {
4391 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07004392 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07004393 mAudioPatches.removeItemsAt(index);
4394 mpClientInterface->onAudioPatchListUpdate();
4395 }
4396
4397 mpClientInterface->closeInput(input);
4398 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07004399}
4400
Eric Laurentc75307b2015-03-17 15:29:32 -07004401SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
4402 audio_devices_t device,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004403 const SwAudioOutputCollection& openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07004404{
4405 SortedVector<audio_io_handle_t> outputs;
4406
4407 ALOGVV("getOutputsForDevice() device %04x", device);
4408 for (size_t i = 0; i < openOutputs.size(); i++) {
Eric Laurent37ddbb42016-08-10 16:19:14 -07004409 ALOGVV("output %zu isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004410 i, openOutputs.valueAt(i)->isDuplicated(),
4411 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004412 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
4413 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
4414 outputs.add(openOutputs.keyAt(i));
4415 }
4416 }
4417 return outputs;
4418}
4419
Eric Laurente0720872014-03-11 09:30:41 -07004420bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01004421 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07004422{
4423 if (outputs1.size() != outputs2.size()) {
4424 return false;
4425 }
4426 for (size_t i = 0; i < outputs1.size(); i++) {
4427 if (outputs1[i] != outputs2[i]) {
4428 return false;
4429 }
4430 }
4431 return true;
4432}
4433
Eric Laurente0720872014-03-11 09:30:41 -07004434void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07004435{
4436 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
4437 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
4438 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
4439 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
4440
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004441 // also take into account external policy-related changes: add all outputs which are
4442 // associated with policies in the "before" and "after" output vectors
4443 ALOGVV("checkOutputForStrategy(): policy related outputs");
4444 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004445 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004446 if (desc != 0 && desc->mPolicyMix != NULL) {
4447 srcOutputs.add(desc->mIoHandle);
4448 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
4449 }
4450 }
4451 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004452 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004453 if (desc != 0 && desc->mPolicyMix != NULL) {
4454 dstOutputs.add(desc->mIoHandle);
4455 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
4456 }
4457 }
4458
Eric Laurente552edb2014-03-10 17:42:56 -07004459 if (!vectorsEqual(srcOutputs,dstOutputs)) {
4460 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
4461 strategy, srcOutputs[0], dstOutputs[0]);
4462 // mute strategy while moving tracks from one output to another
4463 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004464 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01004465 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004466 setStrategyMute(strategy, true, desc);
4467 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004468 }
Eric Laurentd60560a2015-04-10 11:31:20 -07004469 sp<AudioSourceDescriptor> source =
4470 getSourceForStrategyOnOutput(srcOutputs[i], strategy);
4471 if (source != 0){
4472 connectAudioSource(source);
4473 }
Eric Laurente552edb2014-03-10 17:42:56 -07004474 }
4475
4476 // Move effects associated to this strategy from previous output to new output
4477 if (strategy == STRATEGY_MEDIA) {
4478 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
4479 SortedVector<audio_io_handle_t> moved;
4480 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004481 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
4482 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
4483 effectDesc->mIo != fxOutput) {
4484 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004485 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
4486 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004487 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07004488 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004489 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07004490 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07004491 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004492 }
4493 }
4494 }
4495 // Move tracks associated to this strategy from previous output to new output
Eric Laurent794fde22016-03-11 09:50:45 -08004496 for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004497 if (getStrategy((audio_stream_type_t)i) == strategy) {
4498 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07004499 }
4500 }
4501 }
4502}
4503
Eric Laurente0720872014-03-11 09:30:41 -07004504void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07004505{
François Gaffie2110e042015-03-24 08:41:51 +01004506 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004507 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004508 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01004509 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004510 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004511 checkOutputForStrategy(STRATEGY_SONIFICATION);
4512 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004513 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07004514 checkOutputForStrategy(STRATEGY_MEDIA);
4515 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004516 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07004517}
4518
Eric Laurente0720872014-03-11 09:30:41 -07004519void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07004520{
François Gaffie53615e22015-03-19 09:24:12 +01004521 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07004522 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004523 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07004524 return;
4525 }
4526
Eric Laurent3a4311c2014-03-17 12:00:47 -07004527 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08004528 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
4529 ~AUDIO_DEVICE_BIT_IN) != 0) ||
4530 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurentf732e072016-08-03 19:30:28 -07004531
4532 // if suspended, restore A2DP output if:
4533 // ((SCO device is NOT connected) ||
4534 // ((forced usage communication is NOT SCO) && (forced usage for record is NOT SCO) &&
4535 // (phone state is NOT in call) && (phone state is NOT ringing)))
Eric Laurente552edb2014-03-10 17:42:56 -07004536 //
Eric Laurentf732e072016-08-03 19:30:28 -07004537 // if not suspended, suspend A2DP output if:
4538 // (SCO device is connected) &&
4539 // ((forced usage for communication is SCO) || (forced usage for record is SCO) ||
4540 // ((phone state is in call) || (phone state is ringing)))
Eric Laurente552edb2014-03-10 17:42:56 -07004541 //
4542 if (mA2dpSuspended) {
Eric Laurentf732e072016-08-03 19:30:28 -07004543 if (!isScoConnected ||
4544 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) !=
4545 AUDIO_POLICY_FORCE_BT_SCO) &&
4546 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) !=
4547 AUDIO_POLICY_FORCE_BT_SCO) &&
4548 (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
François Gaffie2110e042015-03-24 08:41:51 +01004549 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004550
4551 mpClientInterface->restoreOutput(a2dpOutput);
4552 mA2dpSuspended = false;
4553 }
4554 } else {
Eric Laurentf732e072016-08-03 19:30:28 -07004555 if (isScoConnected &&
4556 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ==
4557 AUDIO_POLICY_FORCE_BT_SCO) ||
4558 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) ==
4559 AUDIO_POLICY_FORCE_BT_SCO) ||
4560 (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
François Gaffie2110e042015-03-24 08:41:51 +01004561 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004562
4563 mpClientInterface->suspendOutput(a2dpOutput);
4564 mA2dpSuspended = true;
4565 }
4566 }
4567}
4568
Eric Laurentc75307b2015-03-17 15:29:32 -07004569audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
4570 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004571{
4572 audio_devices_t device = AUDIO_DEVICE_NONE;
4573
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004574 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004575 if (index >= 0) {
4576 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4577 if (patchDesc->mUid != mUidCached) {
4578 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004579 outputDesc->device(), outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004580 return outputDesc->device();
4581 }
4582 }
4583
Eric Laurente552edb2014-03-10 17:42:56 -07004584 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05004585 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004586 // use device for strategy enforced audible
4587 // 2: we are in call or the strategy phone is active on the output:
4588 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05004589 // 3: the strategy for enforced audible is active but not enforced on the output:
4590 // use the device for strategy enforced audible
Eric Laurent28d09f02016-03-08 10:43:05 -08004591 // 4: the strategy sonification is active on the output:
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004592 // use device for strategy sonification
Eric Laurent28d09f02016-03-08 10:43:05 -08004593 // 5: the strategy accessibility is active on the output:
4594 // use device for strategy accessibility
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004595 // 6: the strategy "respectful" sonification is active on the output:
4596 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08004597 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004598 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08004599 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004600 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08004601 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004602 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01004603 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004604 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07004605 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
4606 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01004607 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004608 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004609 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05004610 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004611 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004612 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Eric Laurent28d09f02016-03-08 10:43:05 -08004613 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
4614 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004615 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004616 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004617 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004618 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004619 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004620 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004621 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004622 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004623 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004624 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07004625 }
4626
Eric Laurent1c333e22014-05-20 10:48:17 -07004627 ALOGV("getNewOutputDevice() selected device %x", device);
4628 return device;
4629}
4630
Eric Laurentfb66dd92016-01-28 18:32:03 -08004631audio_devices_t AudioPolicyManager::getNewInputDevice(const sp<AudioInputDescriptor>& inputDesc)
Eric Laurent1c333e22014-05-20 10:48:17 -07004632{
Eric Laurentfb66dd92016-01-28 18:32:03 -08004633 audio_devices_t device = AUDIO_DEVICE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07004634
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004635 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004636 if (index >= 0) {
4637 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4638 if (patchDesc->mUid != mUidCached) {
4639 ALOGV("getNewInputDevice() device %08x forced by patch %d",
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004640 inputDesc->mDevice, inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004641 return inputDesc->mDevice;
4642 }
4643 }
4644
Eric Laurentfb66dd92016-01-28 18:32:03 -08004645 audio_source_t source = inputDesc->getHighestPrioritySource(true /*activeOnly*/);
4646 if (isInCall()) {
4647 device = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
4648 } else if (source != AUDIO_SOURCE_DEFAULT) {
4649 device = getDeviceAndMixForInputSource(source);
4650 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004651
Eric Laurente552edb2014-03-10 17:42:56 -07004652 return device;
4653}
4654
Eric Laurent794fde22016-03-11 09:50:45 -08004655bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1,
4656 audio_stream_type_t stream2) {
Jean-Michel Trivi99bb2f92016-11-23 15:52:07 -08004657 return (stream1 == stream2);
Eric Laurent28d09f02016-03-08 10:43:05 -08004658}
4659
Eric Laurente0720872014-03-11 09:30:41 -07004660uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004661 return (uint32_t)getStrategy(stream);
4662}
4663
Eric Laurente0720872014-03-11 09:30:41 -07004664audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004665 // By checking the range of stream before calling getStrategy, we avoid
4666 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
4667 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08004668 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004669 return AUDIO_DEVICE_NONE;
4670 }
Eric Laurent28d09f02016-03-08 10:43:05 -08004671 audio_devices_t devices = AUDIO_DEVICE_NONE;
Eric Laurent794fde22016-03-11 09:50:45 -08004672 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
4673 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004674 continue;
Eric Laurent6a94d692014-05-20 11:18:06 -07004675 }
Eric Laurent794fde22016-03-11 09:50:45 -08004676 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08004677 audio_devices_t curDevices =
Eric Laurent447a87b2016-07-07 17:32:10 -07004678 getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/);
Eric Laurent28d09f02016-03-08 10:43:05 -08004679 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs);
4680 for (size_t i = 0; i < outputs.size(); i++) {
4681 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurent794fde22016-03-11 09:50:45 -08004682 if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004683 curDevices |= outputDesc->device();
4684 }
4685 }
4686 devices |= curDevices;
Eric Laurente552edb2014-03-10 17:42:56 -07004687 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05004688
4689 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
4690 and doesn't really need to.*/
4691 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
4692 devices |= AUDIO_DEVICE_OUT_SPEAKER;
4693 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4694 }
Eric Laurente552edb2014-03-10 17:42:56 -07004695 return devices;
4696}
4697
François Gaffie2110e042015-03-24 08:41:51 +01004698routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
4699{
Eric Laurent223fd5c2014-11-11 13:43:36 -08004700 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01004701 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004702}
4703
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004704uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4705 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004706 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4707 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4708 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004709 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4710 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4711 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004712 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01004713 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004714}
4715
Eric Laurente0720872014-03-11 09:30:41 -07004716void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004717 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004718 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004719 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4720 updateDevicesAndOutputs();
4721 break;
4722 default:
4723 break;
4724 }
4725}
4726
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004727uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
Eric Laurent9459fb02015-08-12 18:36:32 -07004728
4729 // skip beacon mute management if a dedicated TTS output is available
4730 if (mTtsOutputAvailable) {
4731 return 0;
4732 }
4733
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004734 switch(event) {
4735 case STARTING_OUTPUT:
4736 mBeaconMuteRefCount++;
4737 break;
4738 case STOPPING_OUTPUT:
4739 if (mBeaconMuteRefCount > 0) {
4740 mBeaconMuteRefCount--;
4741 }
4742 break;
4743 case STARTING_BEACON:
4744 mBeaconPlayingRefCount++;
4745 break;
4746 case STOPPING_BEACON:
4747 if (mBeaconPlayingRefCount > 0) {
4748 mBeaconPlayingRefCount--;
4749 }
4750 break;
4751 }
4752
4753 if (mBeaconMuteRefCount > 0) {
4754 // any playback causes beacon to be muted
4755 return setBeaconMute(true);
4756 } else {
4757 // no other playback: unmute when beacon starts playing, mute when it stops
4758 return setBeaconMute(mBeaconPlayingRefCount == 0);
4759 }
4760}
4761
4762uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4763 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4764 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4765 // keep track of muted state to avoid repeating mute/unmute operations
4766 if (mBeaconMuted != mute) {
4767 // mute/unmute AUDIO_STREAM_TTS on all outputs
4768 ALOGV("\t muting %d", mute);
4769 uint32_t maxLatency = 0;
4770 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004771 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004772 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004773 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004774 0 /*delay*/, AUDIO_DEVICE_NONE);
4775 const uint32_t latency = desc->latency() * 2;
4776 if (latency > maxLatency) {
4777 maxLatency = latency;
4778 }
4779 }
4780 mBeaconMuted = mute;
4781 return maxLatency;
4782 }
4783 return 0;
4784}
4785
Eric Laurente0720872014-03-11 09:30:41 -07004786audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004787 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004788{
Paul McLeanaa981192015-03-21 09:55:15 -07004789 // Routing
4790 // see if we have an explicit route
4791 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4792 // (getStrategy(stream)).
4793 // if the strategy from the stream type in the RouteMap is the same as the argument above,
4794 // and activity count is non-zero
4795 // the device = the device from the descriptor in the RouteMap, and exit.
4796 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4797 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
Eric Laurent28d09f02016-03-08 10:43:05 -08004798 routing_strategy routeStrategy = getStrategy(route->mStreamType);
4799 if ((routeStrategy == strategy) && route->isActive()) {
Paul McLeanaa981192015-03-21 09:55:15 -07004800 return route->mDeviceDescriptor->type();
4801 }
4802 }
4803
Eric Laurente552edb2014-03-10 17:42:56 -07004804 if (fromCache) {
4805 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4806 strategy, mDeviceForStrategy[strategy]);
4807 return mDeviceForStrategy[strategy];
4808 }
François Gaffie2110e042015-03-24 08:41:51 +01004809 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004810}
4811
Eric Laurente0720872014-03-11 09:30:41 -07004812void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004813{
4814 for (int i = 0; i < NUM_STRATEGIES; i++) {
4815 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4816 }
4817 mPreviousOutputs = mOutputs;
4818}
4819
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004820uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004821 audio_devices_t prevDevice,
4822 uint32_t delayMs)
4823{
4824 // mute/unmute strategies using an incompatible device combination
4825 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4826 // if unmuting, unmute only after the specified delay
4827 if (outputDesc->isDuplicated()) {
4828 return 0;
4829 }
4830
4831 uint32_t muteWaitMs = 0;
4832 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004833 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004834
4835 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4836 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004837 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004838 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4839 bool doMute = false;
4840
4841 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4842 doMute = true;
4843 outputDesc->mStrategyMutedByDevice[i] = true;
4844 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4845 doMute = true;
4846 outputDesc->mStrategyMutedByDevice[i] = false;
4847 }
Eric Laurent99401132014-05-07 19:48:15 -07004848 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004849 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004850 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004851 // skip output if it does not share any device with current output
4852 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4853 == AUDIO_DEVICE_NONE) {
4854 continue;
4855 }
Eric Laurent37ddbb42016-08-10 16:19:14 -07004856 ALOGVV("checkDeviceMuteStrategies() %s strategy %zu (curDevice %04x)",
Eric Laurentc75307b2015-03-17 15:29:32 -07004857 mute ? "muting" : "unmuting", i, curDevice);
4858 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004859 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004860 if (mute) {
4861 // FIXME: should not need to double latency if volume could be applied
4862 // immediately by the audioflinger mixer. We must account for the delay
4863 // between now and the next time the audioflinger thread for this output
4864 // will process a buffer (which corresponds to one buffer size,
4865 // usually 1/2 or 1/4 of the latency).
4866 if (muteWaitMs < desc->latency() * 2) {
4867 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004868 }
4869 }
4870 }
4871 }
4872 }
4873 }
4874
Eric Laurent99401132014-05-07 19:48:15 -07004875 // temporary mute output if device selection changes to avoid volume bursts due to
4876 // different per device volumes
4877 if (outputDesc->isActive() && (device != prevDevice)) {
Eric Laurentdc462862016-07-19 12:29:53 -07004878 uint32_t tempMuteWaitMs = outputDesc->latency() * 2;
4879 // temporary mute duration is conservatively set to 4 times the reported latency
4880 uint32_t tempMuteDurationMs = outputDesc->latency() * 4;
4881 if (muteWaitMs < tempMuteWaitMs) {
4882 muteWaitMs = tempMuteWaitMs;
Eric Laurent99401132014-05-07 19:48:15 -07004883 }
Eric Laurentdc462862016-07-19 12:29:53 -07004884
Eric Laurent99401132014-05-07 19:48:15 -07004885 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004886 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentdc462862016-07-19 12:29:53 -07004887 // make sure that we do not start the temporary mute period too early in case of
4888 // delayed device change
4889 setStrategyMute((routing_strategy)i, true, outputDesc, delayMs);
Eric Laurentc75307b2015-03-17 15:29:32 -07004890 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurentdc462862016-07-19 12:29:53 -07004891 delayMs + tempMuteDurationMs, device);
Eric Laurent99401132014-05-07 19:48:15 -07004892 }
4893 }
4894 }
4895
Eric Laurente552edb2014-03-10 17:42:56 -07004896 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4897 if (muteWaitMs > delayMs) {
4898 muteWaitMs -= delayMs;
4899 usleep(muteWaitMs * 1000);
4900 return muteWaitMs;
4901 }
4902 return 0;
4903}
4904
Eric Laurentc75307b2015-03-17 15:29:32 -07004905uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004906 audio_devices_t device,
4907 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004908 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004909 audio_patch_handle_t *patchHandle,
4910 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004911{
Eric Laurentc75307b2015-03-17 15:29:32 -07004912 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004913 AudioParameter param;
4914 uint32_t muteWaitMs;
4915
4916 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004917 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4918 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004919 return muteWaitMs;
4920 }
4921 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4922 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004923 if ((device != AUDIO_DEVICE_NONE) &&
4924 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004925 return 0;
4926 }
4927
4928 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004929 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004930
4931 audio_devices_t prevDevice = outputDesc->mDevice;
4932
Paul McLeanaa981192015-03-21 09:55:15 -07004933 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004934
4935 if (device != AUDIO_DEVICE_NONE) {
4936 outputDesc->mDevice = device;
4937 }
4938 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4939
4940 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004941 // the requested device is AUDIO_DEVICE_NONE
4942 // OR the requested device is the same as current device
4943 // AND force is not specified
4944 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004945 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004946 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4947 !force &&
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004948 outputDesc->getPatchHandle() != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004949 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004950 return muteWaitMs;
4951 }
4952
4953 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004954
Eric Laurente552edb2014-03-10 17:42:56 -07004955 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004956 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004957 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004958 } else {
Eric Laurentc40d9692016-04-13 19:14:13 -07004959 DeviceVector deviceList;
4960 if ((address == NULL) || (strlen(address) == 0)) {
4961 deviceList = mAvailableOutputDevices.getDevicesFromType(device);
4962 } else {
4963 deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
4964 }
4965
Eric Laurent1c333e22014-05-20 10:48:17 -07004966 if (!deviceList.isEmpty()) {
4967 struct audio_patch patch;
4968 outputDesc->toAudioPortConfig(&patch.sources[0]);
4969 patch.num_sources = 1;
4970 patch.num_sinks = 0;
4971 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4972 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004973 patch.num_sinks++;
4974 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004975 ssize_t index;
4976 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4977 index = mAudioPatches.indexOfKey(*patchHandle);
4978 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004979 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004980 }
4981 sp< AudioPatch> patchDesc;
4982 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4983 if (index >= 0) {
4984 patchDesc = mAudioPatches.valueAt(index);
4985 afPatchHandle = patchDesc->mAfPatchHandle;
4986 }
4987
Eric Laurent1c333e22014-05-20 10:48:17 -07004988 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004989 &afPatchHandle,
4990 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004991 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4992 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004993 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004994 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004995 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004996 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004997 addAudioPatch(patchDesc->mHandle, patchDesc);
4998 } else {
4999 patchDesc->mPatch = patch;
5000 }
5001 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07005002 if (patchHandle) {
5003 *patchHandle = patchDesc->mHandle;
5004 }
Jean-Michel Triviff155c62016-02-26 12:07:16 -08005005 outputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07005006 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07005007 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07005008 }
5009 }
bryant_liuf5e7e792014-08-19 20:07:05 +08005010
5011 // inform all input as well
5012 for (size_t i = 0; i < mInputs.size(); i++) {
5013 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01005014 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08005015 AudioParameter inputCmd = AudioParameter();
5016 ALOGV("%s: inform input %d of device:%d", __func__,
5017 inputDescriptor->mIoHandle, device);
5018 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
5019 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
5020 inputCmd.toString(),
5021 delayMs);
5022 }
5023 }
Eric Laurent1c333e22014-05-20 10:48:17 -07005024 }
Eric Laurente552edb2014-03-10 17:42:56 -07005025
5026 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07005027 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005028
5029 return muteWaitMs;
5030}
5031
Eric Laurentc75307b2015-03-17 15:29:32 -07005032status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07005033 int delayMs,
5034 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07005035{
Eric Laurent6a94d692014-05-20 11:18:06 -07005036 ssize_t index;
5037 if (patchHandle) {
5038 index = mAudioPatches.indexOfKey(*patchHandle);
5039 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08005040 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07005041 }
5042 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07005043 return INVALID_OPERATION;
5044 }
Eric Laurent6a94d692014-05-20 11:18:06 -07005045 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
5046 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07005047 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07005048 outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07005049 removeAudioPatch(patchDesc->mHandle);
5050 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07005051 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07005052 return status;
5053}
5054
5055status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
5056 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07005057 bool force,
5058 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07005059{
5060 status_t status = NO_ERROR;
5061
Eric Laurent1f2f2232014-06-02 12:01:23 -07005062 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07005063 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
5064 inputDesc->mDevice = device;
5065
5066 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
5067 if (!deviceList.isEmpty()) {
5068 struct audio_patch patch;
5069 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07005070 // AUDIO_SOURCE_HOTWORD is for internal use only:
5071 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07005072 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
Eric Laurent599c7582015-12-07 18:05:55 -08005073 !inputDesc->isSoundTrigger()) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07005074 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
5075 }
Eric Laurent1c333e22014-05-20 10:48:17 -07005076 patch.num_sinks = 1;
5077 //only one input device for now
5078 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07005079 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07005080 ssize_t index;
5081 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
5082 index = mAudioPatches.indexOfKey(*patchHandle);
5083 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08005084 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07005085 }
5086 sp< AudioPatch> patchDesc;
5087 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
5088 if (index >= 0) {
5089 patchDesc = mAudioPatches.valueAt(index);
5090 afPatchHandle = patchDesc->mAfPatchHandle;
5091 }
5092
Eric Laurent1c333e22014-05-20 10:48:17 -07005093 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07005094 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07005095 0);
5096 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07005097 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07005098 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07005099 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01005100 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07005101 addAudioPatch(patchDesc->mHandle, patchDesc);
5102 } else {
5103 patchDesc->mPatch = patch;
5104 }
5105 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07005106 if (patchHandle) {
5107 *patchHandle = patchDesc->mHandle;
5108 }
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08005109 inputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07005110 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07005111 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07005112 }
5113 }
5114 }
5115 return status;
5116}
5117
Eric Laurent6a94d692014-05-20 11:18:06 -07005118status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
5119 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07005120{
Eric Laurent1f2f2232014-06-02 12:01:23 -07005121 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07005122 ssize_t index;
5123 if (patchHandle) {
5124 index = mAudioPatches.indexOfKey(*patchHandle);
5125 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08005126 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07005127 }
5128 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07005129 return INVALID_OPERATION;
5130 }
Eric Laurent6a94d692014-05-20 11:18:06 -07005131 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
5132 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07005133 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07005134 inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07005135 removeAudioPatch(patchDesc->mHandle);
5136 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07005137 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07005138 return status;
5139}
5140
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08005141sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07005142 const String8& address,
François Gaffie53615e22015-03-19 09:24:12 +01005143 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07005144 audio_format_t& format,
5145 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01005146 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07005147{
5148 // Choose an input profile based on the requested capture parameters: select the first available
5149 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07005150 //
5151 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
5152 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07005153
5154 for (size_t i = 0; i < mHwModules.size(); i++)
5155 {
5156 if (mHwModules[i]->mHandle == 0) {
5157 continue;
5158 }
5159 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
5160 {
Eric Laurent1c333e22014-05-20 10:48:17 -07005161 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07005162 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08005163 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07005164 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07005165 format,
5166 &format /*updatedFormat*/,
5167 channelMask,
5168 &channelMask /*updatedChannelMask*/,
5169 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08005170
Eric Laurente552edb2014-03-10 17:42:56 -07005171 return profile;
5172 }
5173 }
5174 }
5175 return NULL;
5176}
5177
Eric Laurentc73ca6e2014-12-12 14:34:22 -08005178
5179audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01005180 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07005181{
François Gaffie036e1e92015-03-19 10:16:24 +01005182 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
5183 audio_devices_t selectedDeviceFromMix =
5184 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08005185
François Gaffie036e1e92015-03-19 10:16:24 +01005186 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
5187 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08005188 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08005189 return getDeviceForInputSource(inputSource);
5190}
5191
5192audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
5193{
Paul McLean466dc8e2015-04-17 13:15:36 -06005194 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
5195 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07005196 if (inputSource == route->mSource && route->isActive()) {
Paul McLean466dc8e2015-04-17 13:15:36 -06005197 return route->mDeviceDescriptor->type();
5198 }
5199 }
5200
5201 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07005202}
5203
Eric Laurente0720872014-03-11 09:30:41 -07005204float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005205 int index,
5206 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005207{
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005208 float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index);
Jean-Michel Trivi3d8b4a42016-09-14 18:37:46 -07005209
5210 // handle the case of accessibility active while a ringtone is playing: if the ringtone is much
5211 // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch
5212 // exploration of the dialer UI. In this situation, bring the accessibility volume closer to
5213 // the ringtone volume
5214 if ((stream == AUDIO_STREAM_ACCESSIBILITY)
5215 && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState())
5216 && isStreamActive(AUDIO_STREAM_RING, 0)) {
5217 const float ringVolumeDB = computeVolume(AUDIO_STREAM_RING, index, device);
5218 return ringVolumeDB - 4 > volumeDB ? ringVolumeDB - 4 : volumeDB;
5219 }
5220
Eric Laurente552edb2014-03-10 17:42:56 -07005221 // if a headset is connected, apply the following rules to ring tones and notifications
5222 // to avoid sound level bursts in user's ears:
Eric Laurent6af1c1d2016-04-14 11:20:44 -07005223 // - always attenuate notifications volume by 6dB
5224 // - attenuate ring tones volume by 6dB unless music is not playing and
5225 // speaker is part of the select devices
Eric Laurente552edb2014-03-10 17:42:56 -07005226 // - if music is playing, always limit the volume to current music volume,
5227 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07005228 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005229 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
5230 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
5231 AUDIO_DEVICE_OUT_WIRED_HEADSET |
Eric Laurent904d6322017-03-17 17:20:47 -07005232 AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
5233 AUDIO_DEVICE_OUT_USB_HEADSET)) &&
Eric Laurente552edb2014-03-10 17:42:56 -07005234 ((stream_strategy == STRATEGY_SONIFICATION)
5235 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07005236 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07005237 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01005238 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
François Gaffied1ab2bd2015-12-02 18:20:06 +01005239 mVolumeCurves->canBeMuted(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005240 // when the phone is ringing we must consider that music could have been paused just before
5241 // by the music application and behave as if music was active if the last music track was
5242 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07005243 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07005244 mLimitRingtoneVolume) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005245 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07005246 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07005247 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005248 mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC,
5249 musicDevice),
5250 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07005251 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
5252 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005253 if (volumeDB > minVolDB) {
5254 volumeDB = minVolDB;
Eric Laurentffbc80f2015-03-18 18:30:19 -07005255 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07005256 }
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005257 if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
5258 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) {
5259 // on A2DP, also ensure notification volume is not too low compared to media when
5260 // intended to be played
5261 if ((volumeDB > -96.0f) &&
5262 (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) {
5263 ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f",
5264 stream, device,
5265 volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB);
5266 volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB;
5267 }
5268 }
Eric Laurent6af1c1d2016-04-14 11:20:44 -07005269 } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) ||
5270 stream_strategy != STRATEGY_SONIFICATION) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005271 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07005272 }
5273 }
5274
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005275 return volumeDB;
Eric Laurente552edb2014-03-10 17:42:56 -07005276}
5277
Eric Laurente0720872014-03-11 09:30:41 -07005278status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005279 int index,
5280 const sp<AudioOutputDescriptor>& outputDesc,
5281 audio_devices_t device,
5282 int delayMs,
5283 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005284{
Eric Laurente552edb2014-03-10 17:42:56 -07005285 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07005286 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07005287 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07005288 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07005289 return NO_ERROR;
5290 }
François Gaffie2110e042015-03-24 08:41:51 +01005291 audio_policy_forced_cfg_t forceUseForComm =
5292 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07005293 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01005294 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
5295 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005296 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01005297 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07005298 return INVALID_OPERATION;
5299 }
5300
Eric Laurentc75307b2015-03-17 15:29:32 -07005301 if (device == AUDIO_DEVICE_NONE) {
5302 device = outputDesc->device();
5303 }
Eric Laurent275e8e92014-11-30 15:14:47 -08005304
Eric Laurentffbc80f2015-03-18 18:30:19 -07005305 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07005306 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07005307 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08005308 }
Eric Laurentc75307b2015-03-17 15:29:32 -07005309
Eric Laurentffbc80f2015-03-18 18:30:19 -07005310 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07005311
Eric Laurent3b73df72014-03-11 09:06:29 -07005312 if (stream == AUDIO_STREAM_VOICE_CALL ||
5313 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07005314 float voiceVolume;
5315 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07005316 if (stream == AUDIO_STREAM_VOICE_CALL) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005317 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005318 } else {
5319 voiceVolume = 1.0;
5320 }
5321
Eric Laurent18fba842016-03-31 14:41:26 -07005322 if (voiceVolume != mLastVoiceVolume) {
Eric Laurente552edb2014-03-10 17:42:56 -07005323 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
5324 mLastVoiceVolume = voiceVolume;
5325 }
5326 }
5327
5328 return NO_ERROR;
5329}
5330
Eric Laurentc75307b2015-03-17 15:29:32 -07005331void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
5332 audio_devices_t device,
5333 int delayMs,
5334 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005335{
Eric Laurentc75307b2015-03-17 15:29:32 -07005336 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07005337
Eric Laurent794fde22016-03-11 09:50:45 -08005338 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005339 checkAndSetVolume((audio_stream_type_t)stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005340 mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005341 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005342 device,
5343 delayMs,
5344 force);
5345 }
5346}
5347
Eric Laurente0720872014-03-11 09:30:41 -07005348void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07005349 bool on,
5350 const sp<AudioOutputDescriptor>& outputDesc,
5351 int delayMs,
5352 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005353{
Eric Laurent72249d52015-06-08 11:12:15 -07005354 ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d",
5355 strategy, on, outputDesc->getId());
Eric Laurent794fde22016-03-11 09:50:45 -08005356 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005357 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005358 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07005359 }
5360 }
5361}
5362
Eric Laurente0720872014-03-11 09:30:41 -07005363void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005364 bool on,
5365 const sp<AudioOutputDescriptor>& outputDesc,
5366 int delayMs,
5367 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005368{
Eric Laurente552edb2014-03-10 17:42:56 -07005369 if (device == AUDIO_DEVICE_NONE) {
5370 device = outputDesc->device();
5371 }
5372
Eric Laurentc75307b2015-03-17 15:29:32 -07005373 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
5374 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07005375
5376 if (on) {
5377 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005378 if (mVolumeCurves->canBeMuted(stream) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07005379 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01005380 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005381 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005382 }
5383 }
5384 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
5385 outputDesc->mMuteCount[stream]++;
5386 } else {
5387 if (outputDesc->mMuteCount[stream] == 0) {
5388 ALOGV("setStreamMute() unmuting non muted stream!");
5389 return;
5390 }
5391 if (--outputDesc->mMuteCount[stream] == 0) {
5392 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005393 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005394 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005395 device,
5396 delayMs);
5397 }
5398 }
5399}
5400
Eric Laurente0720872014-03-11 09:30:41 -07005401void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07005402 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07005403{
Eric Laurent87ffa392015-05-22 10:32:38 -07005404 if(!hasPrimaryOutput()) {
5405 return;
5406 }
5407
Eric Laurente552edb2014-03-10 17:42:56 -07005408 // if the stream pertains to sonification strategy and we are in call we must
5409 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
5410 // in the device used for phone strategy and play the tone if the selected device does not
5411 // interfere with the device used for phone strategy
5412 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
5413 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07005414 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005415 if ((stream_strategy == STRATEGY_SONIFICATION) ||
5416 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005417 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07005418 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
5419 stream, starting, outputDesc->mDevice, stateChange);
5420 if (outputDesc->mRefCount[stream]) {
5421 int muteCount = 1;
5422 if (stateChange) {
5423 muteCount = outputDesc->mRefCount[stream];
5424 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005425 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005426 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
5427 for (int i = 0; i < muteCount; i++) {
5428 setStreamMute(stream, starting, mPrimaryOutput);
5429 }
5430 } else {
5431 ALOGV("handleIncallSonification() high visibility");
5432 if (outputDesc->device() &
5433 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
5434 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
5435 for (int i = 0; i < muteCount; i++) {
5436 setStreamMute(stream, starting, mPrimaryOutput);
5437 }
5438 }
5439 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005440 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
5441 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07005442 } else {
5443 mpClientInterface->stopTone();
5444 }
5445 }
5446 }
5447 }
5448}
5449
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005450audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
5451{
5452 // flags to stream type mapping
5453 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
5454 return AUDIO_STREAM_ENFORCED_AUDIBLE;
5455 }
5456 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
5457 return AUDIO_STREAM_BLUETOOTH_SCO;
5458 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08005459 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
5460 return AUDIO_STREAM_TTS;
5461 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005462
5463 // usage to stream type mapping
5464 switch (attr->usage) {
5465 case AUDIO_USAGE_MEDIA:
5466 case AUDIO_USAGE_GAME:
Jean-Michel Trivi36867762016-12-29 12:03:28 -08005467 case AUDIO_USAGE_ASSISTANT:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005468 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5469 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08005470 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5471 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005472 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5473 return AUDIO_STREAM_SYSTEM;
5474 case AUDIO_USAGE_VOICE_COMMUNICATION:
5475 return AUDIO_STREAM_VOICE_CALL;
5476
5477 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5478 return AUDIO_STREAM_DTMF;
5479
5480 case AUDIO_USAGE_ALARM:
5481 return AUDIO_STREAM_ALARM;
5482 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5483 return AUDIO_STREAM_RING;
5484
5485 case AUDIO_USAGE_NOTIFICATION:
5486 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5487 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5488 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5489 case AUDIO_USAGE_NOTIFICATION_EVENT:
5490 return AUDIO_STREAM_NOTIFICATION;
5491
5492 case AUDIO_USAGE_UNKNOWN:
5493 default:
5494 return AUDIO_STREAM_MUSIC;
5495 }
5496}
Eric Laurente83b55d2014-11-14 10:06:21 -08005497
François Gaffie53615e22015-03-19 09:24:12 +01005498bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
5499{
Eric Laurente83b55d2014-11-14 10:06:21 -08005500 // has flags that map to a strategy?
5501 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
5502 return true;
5503 }
5504
5505 // has known usage?
5506 switch (paa->usage) {
5507 case AUDIO_USAGE_UNKNOWN:
5508 case AUDIO_USAGE_MEDIA:
5509 case AUDIO_USAGE_VOICE_COMMUNICATION:
5510 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5511 case AUDIO_USAGE_ALARM:
5512 case AUDIO_USAGE_NOTIFICATION:
5513 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5514 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5515 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5516 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5517 case AUDIO_USAGE_NOTIFICATION_EVENT:
5518 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5519 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5520 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5521 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08005522 case AUDIO_USAGE_VIRTUAL_SOURCE:
Jean-Michel Trivi36867762016-12-29 12:03:28 -08005523 case AUDIO_USAGE_ASSISTANT:
Eric Laurente83b55d2014-11-14 10:06:21 -08005524 break;
5525 default:
5526 return false;
5527 }
5528 return true;
5529}
5530
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07005531bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor>& outputDesc,
François Gaffiead3183e2015-03-18 16:55:35 +01005532 routing_strategy strategy, uint32_t inPastMs,
5533 nsecs_t sysTime) const
5534{
5535 if ((sysTime == 0) && (inPastMs != 0)) {
5536 sysTime = systemTime();
5537 }
Eric Laurent794fde22016-03-11 09:50:45 -08005538 for (int i = 0; i < (int)AUDIO_STREAM_FOR_POLICY_CNT; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01005539 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
5540 (NUM_STRATEGIES == strategy)) &&
5541 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
5542 return true;
5543 }
5544 }
5545 return false;
5546}
5547
François Gaffie2110e042015-03-24 08:41:51 +01005548audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
5549{
5550 return mEngine->getForceUse(usage);
5551}
5552
5553bool AudioPolicyManager::isInCall()
5554{
5555 return isStateInCall(mEngine->getPhoneState());
5556}
5557
5558bool AudioPolicyManager::isStateInCall(int state)
5559{
5560 return is_state_in_call(state);
5561}
5562
Eric Laurentd60560a2015-04-10 11:31:20 -07005563void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc)
5564{
5565 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
5566 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
5567 if (sourceDesc->mDevice->equals(deviceDesc)) {
5568 ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle());
5569 stopAudioSource(sourceDesc->getHandle());
5570 }
5571 }
5572
5573 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
5574 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
5575 bool release = false;
5576 for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) {
5577 const struct audio_port_config *source = &patchDesc->mPatch.sources[j];
5578 if (source->type == AUDIO_PORT_TYPE_DEVICE &&
5579 source->ext.device.type == deviceDesc->type()) {
5580 release = true;
5581 }
5582 }
5583 for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) {
5584 const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j];
5585 if (sink->type == AUDIO_PORT_TYPE_DEVICE &&
5586 sink->ext.device.type == deviceDesc->type()) {
5587 release = true;
5588 }
5589 }
5590 if (release) {
5591 ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle);
5592 releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid);
5593 }
5594 }
5595}
5596
Phil Burk09bc4612016-02-24 15:58:15 -08005597// Modify the list of surround sound formats supported.
Phil Burk0709b0a2016-03-31 12:54:57 -07005598void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) {
5599 FormatVector &formats = *formatsPtr;
Phil Burk07ac1142016-03-25 13:39:29 -07005600 // TODO Set this based on Config properties.
5601 const bool alwaysForceAC3 = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005602
5603 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5604 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
Phil Burk0709b0a2016-03-31 12:54:57 -07005605 ALOGD("%s: forced use = %d", __FUNCTION__, forceUse);
Phil Burk09bc4612016-02-24 15:58:15 -08005606
5607 // Analyze original support for various formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005608 bool supportsAC3 = false;
5609 bool supportsOtherSurround = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005610 bool supportsIEC61937 = false;
5611 for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) {
5612 audio_format_t format = formats[formatIndex];
Phil Burk09bc4612016-02-24 15:58:15 -08005613 switch (format) {
5614 case AUDIO_FORMAT_AC3:
Phil Burk07ac1142016-03-25 13:39:29 -07005615 supportsAC3 = true;
5616 break;
Phil Burk09bc4612016-02-24 15:58:15 -08005617 case AUDIO_FORMAT_E_AC3:
5618 case AUDIO_FORMAT_DTS:
5619 case AUDIO_FORMAT_DTS_HD:
Phil Burk07ac1142016-03-25 13:39:29 -07005620 supportsOtherSurround = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005621 break;
5622 case AUDIO_FORMAT_IEC61937:
5623 supportsIEC61937 = true;
5624 break;
5625 default:
5626 break;
5627 }
5628 }
Phil Burk09bc4612016-02-24 15:58:15 -08005629
5630 // Modify formats based on surround preferences.
5631 // If NEVER, remove support for surround formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005632 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5633 if (supportsAC3 || supportsOtherSurround || supportsIEC61937) {
5634 // Remove surround sound related formats.
5635 for (size_t formatIndex = 0; formatIndex < formats.size(); ) {
5636 audio_format_t format = formats[formatIndex];
5637 switch(format) {
5638 case AUDIO_FORMAT_AC3:
5639 case AUDIO_FORMAT_E_AC3:
5640 case AUDIO_FORMAT_DTS:
5641 case AUDIO_FORMAT_DTS_HD:
5642 case AUDIO_FORMAT_IEC61937:
Phil Burk07ac1142016-03-25 13:39:29 -07005643 formats.removeAt(formatIndex);
5644 break;
5645 default:
5646 formatIndex++; // keep it
5647 break;
5648 }
Phil Burk09bc4612016-02-24 15:58:15 -08005649 }
Phil Burk07ac1142016-03-25 13:39:29 -07005650 supportsAC3 = false;
5651 supportsOtherSurround = false;
5652 supportsIEC61937 = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005653 }
Phil Burk07ac1142016-03-25 13:39:29 -07005654 } else { // AUTO or ALWAYS
5655 // Most TVs support AC3 even if they do not report it in the EDID.
5656 if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS))
5657 && !supportsAC3) {
5658 formats.add(AUDIO_FORMAT_AC3);
5659 supportsAC3 = true;
5660 }
5661
5662 // If ALWAYS, add support for raw surround formats if all are missing.
5663 // This assumes that if any of these formats are reported by the HAL
5664 // then the report is valid and should not be modified.
5665 if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)
5666 && !supportsOtherSurround) {
5667 formats.add(AUDIO_FORMAT_E_AC3);
5668 formats.add(AUDIO_FORMAT_DTS);
5669 formats.add(AUDIO_FORMAT_DTS_HD);
5670 supportsOtherSurround = true;
5671 }
5672
5673 // Add support for IEC61937 if any raw surround supported.
5674 // The HAL could do this but add it here, just in case.
5675 if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) {
5676 formats.add(AUDIO_FORMAT_IEC61937);
5677 supportsIEC61937 = true;
5678 }
Phil Burk09bc4612016-02-24 15:58:15 -08005679 }
Phil Burk0709b0a2016-03-31 12:54:57 -07005680}
5681
5682// Modify the list of channel masks supported.
5683void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) {
5684 ChannelsVector &channelMasks = *channelMasksPtr;
5685 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5686 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
5687
5688 // If NEVER, then remove support for channelMasks > stereo.
5689 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5690 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) {
5691 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5692 if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) {
5693 ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask);
5694 channelMasks.removeAt(maskIndex);
5695 } else {
5696 maskIndex++;
5697 }
5698 }
5699 // If ALWAYS, then make sure we at least support 5.1
5700 } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) {
5701 bool supports5dot1 = false;
5702 // Are there any channel masks that can be considered "surround"?
5703 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); maskIndex++) {
5704 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5705 if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) {
5706 supports5dot1 = true;
5707 break;
5708 }
5709 }
5710 // If not then add 5.1 support.
5711 if (!supports5dot1) {
5712 channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1);
5713 ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__);
5714 }
Phil Burk09bc4612016-02-24 15:58:15 -08005715 }
5716}
5717
Phil Burk00eeb322016-03-31 12:41:00 -07005718void AudioPolicyManager::updateAudioProfiles(audio_devices_t device,
5719 audio_io_handle_t ioHandle,
François Gaffie112b0af2015-11-19 16:13:25 +01005720 AudioProfileVector &profiles)
5721{
5722 String8 reply;
Phil Burk0709b0a2016-03-31 12:54:57 -07005723
François Gaffie112b0af2015-11-19 16:13:25 +01005724 // Format MUST be checked first to update the list of AudioProfile
5725 if (profiles.hasDynamicFormat()) {
Mikhail Naganov388360c2016-10-17 17:09:41 -07005726 reply = mpClientInterface->getParameters(
5727 ioHandle, String8(AudioParameter::keyStreamSupportedFormats));
Phil Burk0709b0a2016-03-31 12:54:57 -07005728 ALOGV("%s: supported formats %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005729 AudioParameter repliedParameters(reply);
5730 if (repliedParameters.get(
Mikhail Naganov388360c2016-10-17 17:09:41 -07005731 String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) {
François Gaffie112b0af2015-11-19 16:13:25 +01005732 ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__);
5733 return;
5734 }
Phil Burk09bc4612016-02-24 15:58:15 -08005735 FormatVector formats = formatsFromString(reply.string());
Phil Burk00eeb322016-03-31 12:41:00 -07005736 if (device == AUDIO_DEVICE_OUT_HDMI) {
Phil Burk0709b0a2016-03-31 12:54:57 -07005737 filterSurroundFormats(&formats);
Phil Burk00eeb322016-03-31 12:41:00 -07005738 }
Phil Burk09bc4612016-02-24 15:58:15 -08005739 profiles.setFormats(formats);
François Gaffie112b0af2015-11-19 16:13:25 +01005740 }
5741 const FormatVector &supportedFormats = profiles.getSupportedFormats();
5742
Eric Laurent20eb3a42016-01-26 18:39:17 -08005743 for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) {
François Gaffie112b0af2015-11-19 16:13:25 +01005744 audio_format_t format = supportedFormats[formatIndex];
Eric Laurent20eb3a42016-01-26 18:39:17 -08005745 ChannelsVector channelMasks;
5746 SampleRateVector samplingRates;
François Gaffie112b0af2015-11-19 16:13:25 +01005747 AudioParameter requestedParameters;
Mikhail Naganov388360c2016-10-17 17:09:41 -07005748 requestedParameters.addInt(String8(AudioParameter::keyFormat), format);
François Gaffie112b0af2015-11-19 16:13:25 +01005749
5750 if (profiles.hasDynamicRateFor(format)) {
Mikhail Naganov388360c2016-10-17 17:09:41 -07005751 reply = mpClientInterface->getParameters(
5752 ioHandle,
5753 requestedParameters.toString() + ";" +
5754 AudioParameter::keyStreamSupportedSamplingRates);
François Gaffie112b0af2015-11-19 16:13:25 +01005755 ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005756 AudioParameter repliedParameters(reply);
5757 if (repliedParameters.get(
Mikhail Naganov388360c2016-10-17 17:09:41 -07005758 String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) {
Eric Laurent62e4bc52016-02-02 18:37:28 -08005759 samplingRates = samplingRatesFromString(reply.string());
François Gaffie112b0af2015-11-19 16:13:25 +01005760 }
5761 }
5762 if (profiles.hasDynamicChannelsFor(format)) {
5763 reply = mpClientInterface->getParameters(ioHandle,
5764 requestedParameters.toString() + ";" +
Mikhail Naganov388360c2016-10-17 17:09:41 -07005765 AudioParameter::keyStreamSupportedChannels);
François Gaffie112b0af2015-11-19 16:13:25 +01005766 ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005767 AudioParameter repliedParameters(reply);
5768 if (repliedParameters.get(
Mikhail Naganov388360c2016-10-17 17:09:41 -07005769 String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) {
Eric Laurent62e4bc52016-02-02 18:37:28 -08005770 channelMasks = channelMasksFromString(reply.string());
Phil Burk0709b0a2016-03-31 12:54:57 -07005771 if (device == AUDIO_DEVICE_OUT_HDMI) {
5772 filterSurroundChannelMasks(&channelMasks);
5773 }
François Gaffie112b0af2015-11-19 16:13:25 +01005774 }
5775 }
Eric Laurent20eb3a42016-01-26 18:39:17 -08005776 profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates));
François Gaffie112b0af2015-11-19 16:13:25 +01005777 }
5778}
Eric Laurentd60560a2015-04-10 11:31:20 -07005779
Eric Laurente552edb2014-03-10 17:42:56 -07005780}; // namespace android