blob: aaa6134d0931c4e58d9c53a994002b2c32bd986d [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 Laurente3014102017-05-03 11:15:43 -07001010 DeviceVector outputDevices = mAvailableOutputDevices.getDevicesFromType(device);
1011 String8 address = outputDevices.size() > 0 ? outputDevices.itemAt(0)->mAddress
1012 : String8("");
Eric Laurent322b4d22015-04-03 15:57:54 -07001013 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07001014 &output,
1015 &config,
1016 &outputDesc->mDevice,
Eric Laurente3014102017-05-03 11:15:43 -07001017 address,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001018 &outputDesc->mLatency,
1019 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001020
1021 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -07001022 if (status != NO_ERROR ||
1023 (samplingRate != 0 && samplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -08001024 (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) ||
Eric Laurentcf2c0212014-07-25 16:20:43 -07001025 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -07001026 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1027 "format %d %d, channelMask %04x %04x", output, samplingRate,
1028 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1029 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001030 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07001031 mpClientInterface->closeOutput(output);
1032 }
Eric Laurenta82797f2015-01-30 11:49:43 -08001033 // fall back to mixer output if possible when the direct output could not be open
Glenn Kasten05ddca52016-02-11 08:17:12 -08001034 if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) {
Eric Laurenta82797f2015-01-30 11:49:43 -08001035 goto non_direct_output;
1036 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001037 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001038 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001039 outputDesc->mSamplingRate = config.sample_rate;
1040 outputDesc->mChannelMask = config.channel_mask;
1041 outputDesc->mFormat = config.format;
1042 outputDesc->mRefCount[stream] = 0;
1043 outputDesc->mStopTime[stream] = 0;
1044 outputDesc->mDirectOpenCount = 1;
Kevin Rocard551061a2017-02-06 15:59:29 -08001045 outputDesc->mDirectClientUid = clientUid;
Eric Laurente552edb2014-03-10 17:42:56 -07001046 addOutput(output, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07001047 mPreviousOutputs = mOutputs;
1048 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -07001049 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001050 return output;
1051 }
1052
Eric Laurentb732cf52014-09-24 19:08:21 -07001053non_direct_output:
Eric Laurent14cbfca2016-03-17 09:42:16 -07001054
1055 // A request for HW A/V sync cannot fallback to a mixed output because time
1056 // stamps are embedded in audio data
1057 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1058 return AUDIO_IO_HANDLE_NONE;
1059 }
1060
Eric Laurente552edb2014-03-10 17:42:56 -07001061 // ignoring channel mask due to downmix capability in mixer
1062
1063 // open a non direct output
1064
1065 // for non direct outputs, only PCM is supported
1066 if (audio_is_linear_pcm(format)) {
1067 // get which output is suitable for the specified stream. The actual
1068 // routing change will happen when startOutput() will be called
1069 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1070
Eric Laurent8838a382014-09-08 16:44:28 -07001071 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1072 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1073 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -07001074 }
1075 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1076 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1077
Paul McLeanaa981192015-03-21 09:55:15 -07001078 ALOGV(" getOutputForDevice() returns output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -07001079
1080 return output;
1081}
1082
Eric Laurente0720872014-03-11 09:30:41 -07001083audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -07001084 audio_output_flags_t flags,
1085 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -07001086{
1087 // select one output among several that provide a path to a particular device or set of
1088 // devices (the list was previously build by getOutputsForDevice()).
1089 // The priority is as follows:
1090 // 1: the output with the highest number of requested policy flags
Eric Laurente6930022016-02-11 10:20:40 -08001091 // 2: the output with the bit depth the closest to the requested one
1092 // 3: the primary output
1093 // 4: the first output in the list
Eric Laurente552edb2014-03-10 17:42:56 -07001094
1095 if (outputs.size() == 0) {
1096 return 0;
1097 }
1098 if (outputs.size() == 1) {
1099 return outputs[0];
1100 }
1101
1102 int maxCommonFlags = 0;
Eric Laurente6930022016-02-11 10:20:40 -08001103 audio_io_handle_t outputForFlags = 0;
1104 audio_io_handle_t outputForPrimary = 0;
1105 audio_io_handle_t outputForFormat = 0;
1106 audio_format_t bestFormat = AUDIO_FORMAT_INVALID;
1107 audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07001108
1109 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001110 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -07001111 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -07001112 // if a valid format is specified, skip output if not compatible
1113 if (format != AUDIO_FORMAT_INVALID) {
1114 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente6930022016-02-11 10:20:40 -08001115 if (!audio_formats_match(format, outputDesc->mFormat)) {
Eric Laurent8838a382014-09-08 16:44:28 -07001116 continue;
1117 }
1118 } else if (!audio_is_linear_pcm(format)) {
1119 continue;
1120 }
Eric Laurente6930022016-02-11 10:20:40 -08001121 if (AudioPort::isBetterFormatMatch(
1122 outputDesc->mFormat, bestFormat, format)) {
1123 outputForFormat = outputs[i];
1124 bestFormat = outputDesc->mFormat;
1125 }
Eric Laurent8838a382014-09-08 16:44:28 -07001126 }
1127
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001128 int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags);
Eric Laurente6930022016-02-11 10:20:40 -08001129 if (commonFlags >= maxCommonFlags) {
1130 if (commonFlags == maxCommonFlags) {
1131 if (AudioPort::isBetterFormatMatch(
1132 outputDesc->mFormat, bestFormatForFlags, format)) {
1133 outputForFlags = outputs[i];
1134 bestFormatForFlags = outputDesc->mFormat;
1135 }
1136 } else {
1137 outputForFlags = outputs[i];
1138 maxCommonFlags = commonFlags;
1139 bestFormatForFlags = outputDesc->mFormat;
1140 }
Eric Laurente552edb2014-03-10 17:42:56 -07001141 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
1142 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001143 if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurente6930022016-02-11 10:20:40 -08001144 outputForPrimary = outputs[i];
Eric Laurente552edb2014-03-10 17:42:56 -07001145 }
1146 }
1147 }
1148
Eric Laurente6930022016-02-11 10:20:40 -08001149 if (outputForFlags != 0) {
1150 return outputForFlags;
Eric Laurente552edb2014-03-10 17:42:56 -07001151 }
Eric Laurente6930022016-02-11 10:20:40 -08001152 if (outputForFormat != 0) {
1153 return outputForFormat;
1154 }
1155 if (outputForPrimary != 0) {
1156 return outputForPrimary;
Eric Laurente552edb2014-03-10 17:42:56 -07001157 }
1158
1159 return outputs[0];
1160}
1161
Eric Laurente0720872014-03-11 09:30:41 -07001162status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001163 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001164 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001165{
Paul McLeanaa981192015-03-21 09:55:15 -07001166 ALOGV("startOutput() output %d, stream %d, session %d",
1167 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -07001168 ssize_t index = mOutputs.indexOfKey(output);
1169 if (index < 0) {
1170 ALOGW("startOutput() unknown output %d", output);
1171 return BAD_VALUE;
1172 }
1173
Eric Laurentc75307b2015-03-17 15:29:32 -07001174 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
1175
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001176 // Routing?
1177 mOutputRoutes.incRouteActivity(session);
1178
Eric Laurentc75307b2015-03-17 15:29:32 -07001179 audio_devices_t newDevice;
Eric Laurentc40d9692016-04-13 19:14:13 -07001180 AudioMix *policyMix = NULL;
1181 const char *address = NULL;
Eric Laurentc75307b2015-03-17 15:29:32 -07001182 if (outputDesc->mPolicyMix != NULL) {
Eric Laurentc40d9692016-04-13 19:14:13 -07001183 policyMix = outputDesc->mPolicyMix;
1184 address = policyMix->mDeviceAddress.string();
1185 if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
1186 newDevice = policyMix->mDeviceType;
1187 } else {
1188 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
1189 }
Eric Laurent493404d2015-04-21 15:07:36 -07001190 } else if (mOutputRoutes.hasRouteChanged(session)) {
1191 newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001192 checkStrategyRoute(getStrategy(stream), output);
Eric Laurentc75307b2015-03-17 15:29:32 -07001193 } else {
1194 newDevice = AUDIO_DEVICE_NONE;
1195 }
1196
1197 uint32_t delayMs = 0;
1198
Eric Laurentc40d9692016-04-13 19:14:13 -07001199 status_t status = startSource(outputDesc, stream, newDevice, address, &delayMs);
Eric Laurentc75307b2015-03-17 15:29:32 -07001200
1201 if (status != NO_ERROR) {
1202 mOutputRoutes.decRouteActivity(session);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001203 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -07001204 }
1205 // Automatically enable the remote submix input when output is started on a re routing mix
1206 // of type MIX_TYPE_RECORDERS
Eric Laurentc40d9692016-04-13 19:14:13 -07001207 if (audio_is_remote_submix_device(newDevice) && policyMix != NULL &&
1208 policyMix->mMixType == MIX_TYPE_RECORDERS) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001209 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1210 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Eric Laurentc40d9692016-04-13 19:14:13 -07001211 address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001212 "remote-submix");
1213 }
1214
1215 if (delayMs != 0) {
1216 usleep(delayMs * 1000);
1217 }
1218
1219 return status;
1220}
1221
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001222status_t AudioPolicyManager::startSource(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurentc75307b2015-03-17 15:29:32 -07001223 audio_stream_type_t stream,
1224 audio_devices_t device,
Eric Laurentc40d9692016-04-13 19:14:13 -07001225 const char *address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001226 uint32_t *delayMs)
1227{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001228 // cannot start playback of STREAM_TTS if any other output is being used
1229 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001230
1231 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001232 if (stream == AUDIO_STREAM_TTS) {
1233 ALOGV("\t found BEACON stream");
Eric Laurent9459fb02015-08-12 18:36:32 -07001234 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001235 return INVALID_OPERATION;
1236 } else {
1237 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1238 }
1239 } else {
1240 // some playback other than beacon starts
1241 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1242 }
1243
Eric Laurent77305a62016-07-25 16:39:22 -07001244 // force device change if the output is inactive and no audio patch is already present.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001245 // check active before incrementing usage count
Eric Laurent77305a62016-07-25 16:39:22 -07001246 bool force = !outputDesc->isActive() &&
1247 (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001248
Eric Laurente552edb2014-03-10 17:42:56 -07001249 // increment usage count for this stream on the requested output:
1250 // NOTE that the usage count is the same for duplicated output and hardware output which is
1251 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1252 outputDesc->changeRefCount(stream, 1);
1253
Eric Laurent36829f92017-04-07 19:04:42 -07001254 if (stream == AUDIO_STREAM_MUSIC) {
1255 selectOutputForMusicEffects();
1256 }
1257
Eric Laurent493404d2015-04-21 15:07:36 -07001258 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001259 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001260 if (device == AUDIO_DEVICE_NONE) {
1261 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001262 }
Eric Laurent36829f92017-04-07 19:04:42 -07001263
Eric Laurente552edb2014-03-10 17:42:56 -07001264 routing_strategy strategy = getStrategy(stream);
1265 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001266 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1267 (beaconMuteLatency > 0);
1268 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001269 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001270 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001271 if (desc != outputDesc) {
Eric Laurent77305a62016-07-25 16:39:22 -07001272 // force a device change if any other output is:
1273 // - managed by the same hw module
1274 // - has a current device selection that differs from selected device.
1275 // - supports currently selected device
1276 // - has an active audio patch
Eric Laurente552edb2014-03-10 17:42:56 -07001277 // In this case, the audio HAL must receive the new device selection so that it can
1278 // change the device currently selected by the other active output.
1279 if (outputDesc->sharesHwModuleWith(desc) &&
Eric Laurent77305a62016-07-25 16:39:22 -07001280 desc->device() != device &&
1281 desc->supportedDevices() & device &&
1282 desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07001283 force = true;
1284 }
1285 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001286 // a notification so that audio focus effect can propagate, or that a mute/unmute
1287 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001288 uint32_t latency = desc->latency();
1289 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1290 waitMs = latency;
1291 }
1292 }
1293 }
Eric Laurentdc462862016-07-19 12:29:53 -07001294 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force, 0, NULL, address);
Eric Laurente552edb2014-03-10 17:42:56 -07001295
1296 // handle special case for sonification while in call
1297 if (isInCall()) {
1298 handleIncallSonification(stream, true, false);
1299 }
1300
1301 // apply volume rules for current stream and device if necessary
1302 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01001303 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07001304 outputDesc,
1305 device);
Eric Laurente552edb2014-03-10 17:42:56 -07001306
1307 // update the outputs if starting an output with a stream that can affect notification
1308 // routing
1309 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001310
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001311 // force reevaluating accessibility routing when ringtone or alarm starts
1312 if (strategy == STRATEGY_SONIFICATION) {
1313 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1314 }
Eric Laurentdc462862016-07-19 12:29:53 -07001315
1316 if (waitMs > muteWaitMs) {
1317 *delayMs = waitMs - muteWaitMs;
1318 }
Eric Laurente552edb2014-03-10 17:42:56 -07001319 }
Eric Laurentdc462862016-07-19 12:29:53 -07001320
Eric Laurente552edb2014-03-10 17:42:56 -07001321 return NO_ERROR;
1322}
1323
1324
Eric Laurente0720872014-03-11 09:30:41 -07001325status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001326 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001327 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001328{
1329 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1330 ssize_t index = mOutputs.indexOfKey(output);
1331 if (index < 0) {
1332 ALOGW("stopOutput() unknown output %d", output);
1333 return BAD_VALUE;
1334 }
1335
Eric Laurentc75307b2015-03-17 15:29:32 -07001336 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001337
Eric Laurentc75307b2015-03-17 15:29:32 -07001338 if (outputDesc->mRefCount[stream] == 1) {
1339 // Automatically disable the remote submix input when output is stopped on a
1340 // re routing mix of type MIX_TYPE_RECORDERS
1341 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1342 outputDesc->mPolicyMix != NULL &&
1343 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1344 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1345 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001346 outputDesc->mPolicyMix->mDeviceAddress,
Eric Laurentc75307b2015-03-17 15:29:32 -07001347 "remote-submix");
1348 }
1349 }
1350
1351 // Routing?
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001352 bool forceDeviceUpdate = false;
Eric Laurentc75307b2015-03-17 15:29:32 -07001353 if (outputDesc->mRefCount[stream] > 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001354 int activityCount = mOutputRoutes.decRouteActivity(session);
1355 forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0));
1356
1357 if (forceDeviceUpdate) {
1358 checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE);
1359 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001360 }
1361
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001362 return stopSource(outputDesc, stream, forceDeviceUpdate);
Eric Laurentc75307b2015-03-17 15:29:32 -07001363}
1364
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001365status_t AudioPolicyManager::stopSource(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001366 audio_stream_type_t stream,
1367 bool forceDeviceUpdate)
Eric Laurentc75307b2015-03-17 15:29:32 -07001368{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001369 // always handle stream stop, check which stream type is stopping
1370 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1371
Eric Laurente552edb2014-03-10 17:42:56 -07001372 // handle special case for sonification while in call
1373 if (isInCall()) {
1374 handleIncallSonification(stream, false, false);
1375 }
1376
1377 if (outputDesc->mRefCount[stream] > 0) {
1378 // decrement usage count of this stream on the output
1379 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001380
Eric Laurente552edb2014-03-10 17:42:56 -07001381 // store time at which the stream was stopped - see isStreamActive()
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001382 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
Eric Laurente552edb2014-03-10 17:42:56 -07001383 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001384 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001385 // delay the device switch by twice the latency because stopOutput() is executed when
1386 // the track stop() command is received and at that time the audio track buffer can
1387 // still contain data that needs to be drained. The latency only covers the audio HAL
1388 // and kernel buffers. Also the latency does not always include additional delay in the
1389 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001390 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001391
1392 // force restoring the device selection on other active outputs if it differs from the
1393 // one being selected for this output
Eric Laurent57de36c2016-09-28 16:59:11 -07001394 uint32_t delayMs = outputDesc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -07001395 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001396 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001397 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001398 desc->isActive() &&
1399 outputDesc->sharesHwModuleWith(desc) &&
1400 (newDevice != desc->device())) {
Haynes Mathew George11c499a2016-08-26 12:08:25 -07001401 audio_devices_t newDevice2 = getNewOutputDevice(desc, false /*fromCache*/);
1402 bool force = desc->device() != newDevice2;
Eric Laurentc75307b2015-03-17 15:29:32 -07001403 setOutputDevice(desc,
Haynes Mathew George11c499a2016-08-26 12:08:25 -07001404 newDevice2,
1405 force,
Eric Laurent57de36c2016-09-28 16:59:11 -07001406 delayMs);
1407 // re-apply device specific volume if not done by setOutputDevice()
1408 if (!force) {
1409 applyStreamVolumes(desc, newDevice2, delayMs);
1410 }
Eric Laurente552edb2014-03-10 17:42:56 -07001411 }
1412 }
1413 // update the outputs if stopping one with a stream that can affect notification routing
1414 handleNotificationRoutingForStream(stream);
1415 }
Eric Laurent36829f92017-04-07 19:04:42 -07001416 if (stream == AUDIO_STREAM_MUSIC) {
1417 selectOutputForMusicEffects();
1418 }
Eric Laurente552edb2014-03-10 17:42:56 -07001419 return NO_ERROR;
1420 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001421 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001422 return INVALID_OPERATION;
1423 }
1424}
1425
Eric Laurente83b55d2014-11-14 10:06:21 -08001426void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001427 audio_stream_type_t stream __unused,
1428 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001429{
1430 ALOGV("releaseOutput() %d", output);
1431 ssize_t index = mOutputs.indexOfKey(output);
1432 if (index < 0) {
1433 ALOGW("releaseOutput() releasing unknown output %d", output);
1434 return;
1435 }
1436
1437#ifdef AUDIO_POLICY_TEST
1438 int testIndex = testOutputIndex(output);
1439 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001440 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001441 if (outputDesc->isActive()) {
1442 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001443 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001444 mTestOutputs[testIndex] = 0;
1445 }
1446 return;
1447 }
1448#endif //AUDIO_POLICY_TEST
1449
Paul McLeanaa981192015-03-21 09:55:15 -07001450 // Routing
1451 mOutputRoutes.removeRoute(session);
1452
Eric Laurentc75307b2015-03-17 15:29:32 -07001453 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001454 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001455 if (desc->mDirectOpenCount <= 0) {
1456 ALOGW("releaseOutput() invalid open count %d for output %d",
1457 desc->mDirectOpenCount, output);
1458 return;
1459 }
1460 if (--desc->mDirectOpenCount == 0) {
1461 closeOutput(output);
Eric Laurentb52c1522014-05-20 11:27:36 -07001462 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001463 }
1464 }
1465}
1466
1467
Eric Laurentcaf7f482014-11-25 17:50:47 -08001468status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1469 audio_io_handle_t *input,
1470 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001471 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001472 const audio_config_base_t *config,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001473 audio_input_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -06001474 audio_port_handle_t selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001475 input_type_t *inputType,
1476 audio_port_handle_t *portId)
Eric Laurente552edb2014-03-10 17:42:56 -07001477{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001478 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1479 "session %d, flags %#x",
Eric Laurent20b9ef02016-12-05 11:03:16 -08001480 attr->source, config->sample_rate, config->format, config->channel_mask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001481
Eric Laurentcaf7f482014-11-25 17:50:47 -08001482 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001483 *inputType = API_INPUT_INVALID;
Eric Laurentfb66dd92016-01-28 18:32:03 -08001484
Eric Laurent275e8e92014-11-30 15:14:47 -08001485 audio_devices_t device;
1486 // handle legacy remote submix case where the address was not always specified
1487 String8 address = String8("");
Eric Laurentc447ded2015-01-06 08:47:05 -08001488 audio_source_t inputSource = attr->source;
1489 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001490 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001491
Eric Laurentc447ded2015-01-06 08:47:05 -08001492 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1493 inputSource = AUDIO_SOURCE_MIC;
1494 }
1495 halInputSource = inputSource;
1496
Eric Laurent20b9ef02016-12-05 11:03:16 -08001497 // TODO: check for existing client for this port ID
1498 if (*portId == AUDIO_PORT_HANDLE_NONE) {
1499 *portId = AudioPort::getNextUniqueId();
1500 }
1501
Paul McLean466dc8e2015-04-17 13:15:36 -06001502 // Explicit routing?
1503 sp<DeviceDescriptor> deviceDesc;
1504 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1505 if (mAvailableInputDevices[i]->getId() == selectedDeviceId) {
1506 deviceDesc = mAvailableInputDevices[i];
1507 break;
1508 }
1509 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001510 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001511
Eric Laurentc447ded2015-01-06 08:47:05 -08001512 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001513 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001514 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
François Gaffie036e1e92015-03-19 10:16:24 +01001515 if (ret != NO_ERROR) {
1516 return ret;
1517 }
1518 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001519 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1520 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001521 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001522 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001523 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001524 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001525 return BAD_VALUE;
1526 }
Eric Laurentc722f302014-12-10 11:21:49 -08001527 if (policyMix != NULL) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001528 address = policyMix->mDeviceAddress;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001529 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1530 // there is an external policy, but this input is attached to a mix of recorders,
1531 // meaning it receives audio injected into the framework, so the recorder doesn't
1532 // know about it and is therefore considered "legacy"
1533 *inputType = API_INPUT_LEGACY;
1534 } else {
1535 // recording a mix of players defined by an external policy, we're rerouting for
1536 // an external policy
1537 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1538 }
Eric Laurentc722f302014-12-10 11:21:49 -08001539 } else if (audio_is_remote_submix_device(device)) {
1540 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001541 *inputType = API_INPUT_MIX_CAPTURE;
Eric Laurent82db2692015-08-07 13:59:42 -07001542 } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
1543 *inputType = API_INPUT_TELEPHONY_RX;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001544 } else {
1545 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001546 }
Ravi Kumar Alamandab367f5b2015-08-25 08:21:37 -07001547
Eric Laurent599c7582015-12-07 18:05:55 -08001548 }
1549
1550 *input = getInputForDevice(device, address, session, uid, inputSource,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001551 config->sample_rate, config->format, config->channel_mask, flags,
Eric Laurent599c7582015-12-07 18:05:55 -08001552 policyMix);
1553 if (*input == AUDIO_IO_HANDLE_NONE) {
1554 mInputRoutes.removeRoute(session);
1555 return INVALID_OPERATION;
1556 }
Eric Laurent20b9ef02016-12-05 11:03:16 -08001557
Eric Laurent599c7582015-12-07 18:05:55 -08001558 ALOGV("getInputForAttr() returns input type = %d", *inputType);
1559 return NO_ERROR;
1560}
1561
1562
1563audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device,
1564 String8 address,
1565 audio_session_t session,
1566 uid_t uid,
1567 audio_source_t inputSource,
1568 uint32_t samplingRate,
1569 audio_format_t format,
1570 audio_channel_mask_t channelMask,
1571 audio_input_flags_t flags,
1572 AudioMix *policyMix)
1573{
1574 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
1575 audio_source_t halInputSource = inputSource;
1576 bool isSoundTrigger = false;
1577
1578 if (inputSource == AUDIO_SOURCE_HOTWORD) {
1579 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1580 if (index >= 0) {
1581 input = mSoundTriggerSessions.valueFor(session);
1582 isSoundTrigger = true;
1583 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1584 ALOGV("SoundTrigger capture on session %d input %d", session, input);
1585 } else {
1586 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001587 }
1588 }
1589
Andy Hungf129b032015-04-07 13:45:50 -07001590 // find a compatible input profile (not necessarily identical in parameters)
1591 sp<IOProfile> profile;
1592 // samplingRate and flags may be updated by getInputProfile
Glenn Kasten05ddca52016-02-11 08:17:12 -08001593 uint32_t profileSamplingRate = (samplingRate == 0) ? SAMPLE_RATE_HZ_DEFAULT : samplingRate;
Andy Hungf129b032015-04-07 13:45:50 -07001594 audio_format_t profileFormat = format;
1595 audio_channel_mask_t profileChannelMask = channelMask;
1596 audio_input_flags_t profileFlags = flags;
1597 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001598 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001599 profileSamplingRate, profileFormat, profileChannelMask,
1600 profileFlags);
1601 if (profile != 0) {
1602 break; // success
Eric Laurent05067782016-06-01 18:27:28 -07001603 } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) {
1604 profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry
Andy Hungf129b032015-04-07 13:45:50 -07001605 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1606 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1607 } else { // fail
Eric Laurent599c7582015-12-07 18:05:55 -08001608 ALOGW("getInputForDevice() could not find profile for device 0x%X,"
1609 "samplingRate %u, format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001610 device, samplingRate, format, channelMask, flags);
Eric Laurent599c7582015-12-07 18:05:55 -08001611 return input;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001612 }
Eric Laurente552edb2014-03-10 17:42:56 -07001613 }
Glenn Kasten05ddca52016-02-11 08:17:12 -08001614 // Pick input sampling rate if not specified by client
1615 if (samplingRate == 0) {
1616 samplingRate = profileSamplingRate;
1617 }
Eric Laurente552edb2014-03-10 17:42:56 -07001618
Eric Laurent322b4d22015-04-03 15:57:54 -07001619 if (profile->getModuleHandle() == 0) {
1620 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurent599c7582015-12-07 18:05:55 -08001621 return input;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001622 }
1623
Eric Laurent599c7582015-12-07 18:05:55 -08001624 sp<AudioSession> audioSession = new AudioSession(session,
1625 inputSource,
1626 format,
1627 samplingRate,
1628 channelMask,
1629 flags,
1630 uid,
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001631 isSoundTrigger,
1632 policyMix, mpClientInterface);
Eric Laurent599c7582015-12-07 18:05:55 -08001633
Eric Laurent74708e72017-04-07 17:13:42 -07001634// FIXME: disable concurrent capture until UI is ready
1635#if 0
Eric Laurent599c7582015-12-07 18:05:55 -08001636 // reuse an open input if possible
Eric Laurent555530a2017-02-07 18:17:24 -08001637 sp<AudioInputDescriptor> reusedInputDesc;
Eric Laurent599c7582015-12-07 18:05:55 -08001638 for (size_t i = 0; i < mInputs.size(); i++) {
1639 sp<AudioInputDescriptor> desc = mInputs.valueAt(i);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001640 // reuse input if:
1641 // - it shares the same profile
1642 // AND
1643 // - it is not a reroute submix input
1644 // AND
1645 // - it is: not used for sound trigger
1646 // OR
1647 // used for sound trigger and all clients use the same session ID
1648 //
1649 if ((profile == desc->mProfile) &&
1650 (isSoundTrigger == desc->isSoundTrigger()) &&
1651 !is_virtual_input_device(device)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001652
1653 sp<AudioSession> as = desc->getAudioSession(session);
1654 if (as != 0) {
1655 // do not allow unmatching properties on same session
1656 if (as->matches(audioSession)) {
1657 as->changeOpenCount(1);
1658 } else {
1659 ALOGW("getInputForDevice() record with different attributes"
1660 " exists for session %d", session);
Eric Laurent555530a2017-02-07 18:17:24 -08001661 continue;
Eric Laurent599c7582015-12-07 18:05:55 -08001662 }
Eric Laurentfb66dd92016-01-28 18:32:03 -08001663 } else if (isSoundTrigger) {
Eric Laurent555530a2017-02-07 18:17:24 -08001664 continue;
Eric Laurentfb66dd92016-01-28 18:32:03 -08001665 }
Eric Laurentbb948092017-01-23 18:33:30 -08001666
Eric Laurent555530a2017-02-07 18:17:24 -08001667 // Reuse the already opened input stream on this profile if:
1668 // - the new capture source is background OR
1669 // - the path requested configurations match OR
1670 // - the new source priority is less than the highest source priority on this input
1671 // If the input stream cannot be reused, close it before opening a new stream
1672 // on the same profile for the new client so that the requested path configuration
1673 // can be selected.
1674 if (!isConcurrentSource(inputSource) &&
Eric Laurentbb948092017-01-23 18:33:30 -08001675 ((desc->mSamplingRate != samplingRate ||
Eric Laurentfb66dd92016-01-28 18:32:03 -08001676 desc->mChannelMask != channelMask ||
Eric Laurente6930022016-02-11 10:20:40 -08001677 !audio_formats_match(desc->mFormat, format)) &&
Eric Laurentfb66dd92016-01-28 18:32:03 -08001678 (source_priority(desc->getHighestPrioritySource(false /*activeOnly*/)) <
Eric Laurentbb948092017-01-23 18:33:30 -08001679 source_priority(inputSource)))) {
Eric Laurent555530a2017-02-07 18:17:24 -08001680 reusedInputDesc = desc;
1681 continue;
Eric Laurent599c7582015-12-07 18:05:55 -08001682 } else {
1683 desc->addAudioSession(session, audioSession);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001684 ALOGV("%s: reusing input %d", __FUNCTION__, mInputs.keyAt(i));
1685 return mInputs.keyAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08001686 }
Eric Laurent599c7582015-12-07 18:05:55 -08001687 }
1688 }
Eric Laurent599c7582015-12-07 18:05:55 -08001689
Eric Laurent555530a2017-02-07 18:17:24 -08001690 if (reusedInputDesc != 0) {
1691 AudioSessionCollection sessions = reusedInputDesc->getAudioSessions(false /*activeOnly*/);
1692 for (size_t j = 0; j < sessions.size(); j++) {
1693 audio_session_t currentSession = sessions.keyAt(j);
1694 stopInput(reusedInputDesc->mIoHandle, currentSession);
1695 releaseInput(reusedInputDesc->mIoHandle, currentSession);
1696 }
1697 }
Eric Laurent74708e72017-04-07 17:13:42 -07001698#endif
Eric Laurent555530a2017-02-07 18:17:24 -08001699
Eric Laurentcf2c0212014-07-25 16:20:43 -07001700 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001701 config.sample_rate = profileSamplingRate;
1702 config.channel_mask = profileChannelMask;
1703 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001704
Eric Laurente3014102017-05-03 11:15:43 -07001705 if (address == "") {
1706 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(device);
1707 // the inputs vector must be of size 1, but we don't want to crash here
1708 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress : String8("");
1709 }
1710
Eric Laurent322b4d22015-04-03 15:57:54 -07001711 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurent599c7582015-12-07 18:05:55 -08001712 &input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001713 &config,
1714 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001715 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001716 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001717 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001718
1719 // only accept input with the exact requested set of parameters
Eric Laurent599c7582015-12-07 18:05:55 -08001720 if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001721 (profileSamplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -08001722 !audio_formats_match(profileFormat, config.format) ||
Andy Hungf129b032015-04-07 13:45:50 -07001723 (profileChannelMask != config.channel_mask)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001724 ALOGW("getInputForAttr() failed opening input: samplingRate %d"
1725 ", format %d, channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001726 samplingRate, format, channelMask);
Eric Laurent599c7582015-12-07 18:05:55 -08001727 if (input != AUDIO_IO_HANDLE_NONE) {
1728 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001729 }
Eric Laurent599c7582015-12-07 18:05:55 -08001730 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001731 }
1732
Eric Laurent1f2f2232014-06-02 12:01:23 -07001733 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Andy Hungf129b032015-04-07 13:45:50 -07001734 inputDesc->mSamplingRate = profileSamplingRate;
1735 inputDesc->mFormat = profileFormat;
1736 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001737 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001738 inputDesc->mPolicyMix = policyMix;
Eric Laurent599c7582015-12-07 18:05:55 -08001739 inputDesc->addAudioSession(session, audioSession);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001740
Eric Laurent599c7582015-12-07 18:05:55 -08001741 addInput(input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001742 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001743
Eric Laurent599c7582015-12-07 18:05:55 -08001744 return input;
Eric Laurente552edb2014-03-10 17:42:56 -07001745}
1746
Eric Laurentbb948092017-01-23 18:33:30 -08001747//static
1748bool AudioPolicyManager::isConcurrentSource(audio_source_t source)
1749{
1750 return (source == AUDIO_SOURCE_HOTWORD) ||
1751 (source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
1752 (source == AUDIO_SOURCE_FM_TUNER);
1753}
1754
Eric Laurentfb66dd92016-01-28 18:32:03 -08001755bool AudioPolicyManager::isConcurentCaptureAllowed(const sp<AudioInputDescriptor>& inputDesc,
1756 const sp<AudioSession>& audioSession)
1757{
1758 // Do not allow capture if an active voice call is using a software patch and
1759 // the call TX source device is on the same HW module.
1760 // FIXME: would be better to refine to only inputs whose profile connects to the
1761 // call TX device but this information is not in the audio patch
1762 if (mCallTxPatch != 0 &&
1763 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1764 return false;
1765 }
1766
1767 // starting concurrent capture is enabled if:
1768 // 1) capturing for re-routing
1769 // 2) capturing for HOTWORD source
1770 // 3) capturing for FM TUNER source
1771 // 3) All other active captures are either for re-routing or HOTWORD
1772
1773 if (is_virtual_input_device(inputDesc->mDevice) ||
Eric Laurentbb948092017-01-23 18:33:30 -08001774 isConcurrentSource(audioSession->inputSource())) {
Eric Laurentfb66dd92016-01-28 18:32:03 -08001775 return true;
1776 }
1777
1778 Vector< sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
1779 for (size_t i = 0; i < activeInputs.size(); i++) {
1780 sp<AudioInputDescriptor> activeInput = activeInputs[i];
Eric Laurentbb948092017-01-23 18:33:30 -08001781 if (!isConcurrentSource(activeInput->inputSource(true)) &&
Eric Laurentfb66dd92016-01-28 18:32:03 -08001782 !is_virtual_input_device(activeInput->mDevice)) {
1783 return false;
1784 }
1785 }
1786
1787 return true;
1788}
1789
1790
Eric Laurent4dc68062014-07-28 17:26:49 -07001791status_t AudioPolicyManager::startInput(audio_io_handle_t input,
Eric Laurentfb66dd92016-01-28 18:32:03 -08001792 audio_session_t session,
1793 concurrency_type__mask_t *concurrency)
Eric Laurente552edb2014-03-10 17:42:56 -07001794{
1795 ALOGV("startInput() input %d", input);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001796 *concurrency = API_INPUT_CONCURRENCY_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001797 ssize_t index = mInputs.indexOfKey(input);
1798 if (index < 0) {
1799 ALOGW("startInput() unknown input %d", input);
1800 return BAD_VALUE;
1801 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001802 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001803
Eric Laurent599c7582015-12-07 18:05:55 -08001804 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1805 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07001806 ALOGW("startInput() unknown session %d on input %d", session, input);
1807 return BAD_VALUE;
1808 }
1809
Eric Laurent74708e72017-04-07 17:13:42 -07001810// FIXME: disable concurrent capture until UI is ready
1811#if 0
Eric Laurentfb66dd92016-01-28 18:32:03 -08001812 if (!isConcurentCaptureAllowed(inputDesc, audioSession)) {
1813 ALOGW("startInput(%d) failed: other input already started", input);
1814 return INVALID_OPERATION;
1815 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001816
Eric Laurentfb66dd92016-01-28 18:32:03 -08001817 if (isInCall()) {
1818 *concurrency |= API_INPUT_CONCURRENCY_CALL;
1819 }
Eric Laurentf7c50102016-09-30 15:19:43 -07001820 if (mInputs.activeInputsCountOnDevices() != 0) {
Eric Laurentfb66dd92016-01-28 18:32:03 -08001821 *concurrency |= API_INPUT_CONCURRENCY_CAPTURE;
Eric Laurente552edb2014-03-10 17:42:56 -07001822 }
Eric Laurent74708e72017-04-07 17:13:42 -07001823#else
1824 if (!is_virtual_input_device(inputDesc->mDevice)) {
1825 if (mCallTxPatch != 0 &&
1826 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1827 ALOGW("startInput(%d) failed: call in progress", input);
1828 return INVALID_OPERATION;
1829 }
1830
1831 Vector< sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
1832 for (size_t i = 0; i < activeInputs.size(); i++) {
1833 sp<AudioInputDescriptor> activeDesc = activeInputs[i];
1834
1835 if (is_virtual_input_device(activeDesc->mDevice)) {
1836 continue;
1837 }
1838
1839 audio_source_t activeSource = activeDesc->inputSource(true);
1840 if (audioSession->inputSource() == AUDIO_SOURCE_HOTWORD) {
1841 if (activeSource == AUDIO_SOURCE_HOTWORD) {
1842 if (activeDesc->hasPreemptedSession(session)) {
1843 ALOGW("startInput(%d) failed for HOTWORD: "
1844 "other input %d already started for HOTWORD",
1845 input, activeDesc->mIoHandle);
1846 return INVALID_OPERATION;
1847 }
1848 } else {
1849 ALOGV("startInput(%d) failed for HOTWORD: other input %d already started",
1850 input, activeDesc->mIoHandle);
1851 return INVALID_OPERATION;
1852 }
1853 } else {
1854 if (activeSource != AUDIO_SOURCE_HOTWORD) {
1855 ALOGW("startInput(%d) failed: other input %d already started",
1856 input, activeDesc->mIoHandle);
1857 return INVALID_OPERATION;
1858 }
1859 }
1860 }
1861
1862 // if capture is allowed, preempt currently active HOTWORD captures
1863 for (size_t i = 0; i < activeInputs.size(); i++) {
1864 sp<AudioInputDescriptor> activeDesc = activeInputs[i];
1865
1866 if (is_virtual_input_device(activeDesc->mDevice)) {
1867 continue;
1868 }
1869
1870 audio_source_t activeSource = activeDesc->inputSource(true);
1871 if (activeSource == AUDIO_SOURCE_HOTWORD) {
1872 AudioSessionCollection activeSessions =
1873 activeDesc->getAudioSessions(true /*activeOnly*/);
1874 audio_session_t activeSession = activeSessions.keyAt(0);
1875 audio_io_handle_t activeHandle = activeDesc->mIoHandle;
1876 SortedVector<audio_session_t> sessions = activeDesc->getPreemptedSessions();
1877 sessions.add(activeSession);
1878 inputDesc->setPreemptedSessions(sessions);
1879 stopInput(activeHandle, activeSession);
1880 releaseInput(activeHandle, activeSession);
1881 ALOGV("startInput(%d) for HOTWORD preempting HOTWORD input %d",
1882 input, activeDesc->mIoHandle);
1883 }
1884 }
1885 }
1886#endif
Eric Laurente552edb2014-03-10 17:42:56 -07001887
Eric Laurent313d1e72016-01-29 09:56:57 -08001888 // increment activity count before calling getNewInputDevice() below as only active sessions
1889 // are considered for device selection
1890 audioSession->changeActiveCount(1);
1891
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001892 // Routing?
1893 mInputRoutes.incRouteActivity(session);
1894
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001895 if (audioSession->activeCount() == 1 || mInputRoutes.hasRouteChanged(session)) {
Eric Laurent271a93e2016-09-29 14:47:06 -07001896 // indicate active capture to sound trigger service if starting capture from a mic on
1897 // primary HW module
Eric Laurentf7c50102016-09-30 15:19:43 -07001898 audio_devices_t device = getNewInputDevice(inputDesc);
Eric Laurent271a93e2016-09-29 14:47:06 -07001899 setInputDevice(input, device, true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001900
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001901 if (inputDesc->getAudioSessionCount(true/*activeOnly*/) == 1) {
1902 // if input maps to a dynamic policy with an activity listener, notify of state change
1903 if ((inputDesc->mPolicyMix != NULL)
1904 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1905 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
1906 MIX_STATE_MIXING);
Eric Laurentc722f302014-12-10 11:21:49 -08001907 }
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001908
Eric Laurentf7c50102016-09-30 15:19:43 -07001909 audio_devices_t primaryInputDevices = availablePrimaryInputDevices();
1910 if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) &&
Eric Laurent05b345e2016-11-18 12:36:27 -08001911 mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) {
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001912 SoundTrigger::setCaptureState(true);
1913 }
1914
1915 // automatically enable the remote submix output when input is started if not
1916 // used by a policy mix of type MIX_TYPE_RECORDERS
1917 // For remote submix (a virtual device), we open only one input per capture request.
1918 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1919 String8 address = String8("");
1920 if (inputDesc->mPolicyMix == NULL) {
1921 address = String8("0");
1922 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1923 address = inputDesc->mPolicyMix->mDeviceAddress;
1924 }
1925 if (address != "") {
1926 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1927 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1928 address, "remote-submix");
1929 }
Eric Laurentc722f302014-12-10 11:21:49 -08001930 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001931 }
Eric Laurente552edb2014-03-10 17:42:56 -07001932 }
1933
Eric Laurent599c7582015-12-07 18:05:55 -08001934 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Eric Laurente552edb2014-03-10 17:42:56 -07001935
Eric Laurente552edb2014-03-10 17:42:56 -07001936 return NO_ERROR;
1937}
1938
Eric Laurent4dc68062014-07-28 17:26:49 -07001939status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1940 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001941{
1942 ALOGV("stopInput() input %d", input);
1943 ssize_t index = mInputs.indexOfKey(input);
1944 if (index < 0) {
1945 ALOGW("stopInput() unknown input %d", input);
1946 return BAD_VALUE;
1947 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001948 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001949
Eric Laurent599c7582015-12-07 18:05:55 -08001950 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001951 if (index < 0) {
1952 ALOGW("stopInput() unknown session %d on input %d", session, input);
1953 return BAD_VALUE;
1954 }
1955
Eric Laurent599c7582015-12-07 18:05:55 -08001956 if (audioSession->activeCount() == 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07001957 ALOGW("stopInput() input %d already stopped", input);
1958 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001959 }
1960
Eric Laurent599c7582015-12-07 18:05:55 -08001961 audioSession->changeActiveCount(-1);
Paul McLean466dc8e2015-04-17 13:15:36 -06001962
1963 // Routing?
1964 mInputRoutes.decRouteActivity(session);
1965
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001966 if (audioSession->activeCount() == 0) {
Eric Laurent84332aa2016-01-28 22:19:18 +00001967
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001968 if (inputDesc->isActive()) {
1969 setInputDevice(input, getNewInputDevice(inputDesc), false /* force */);
1970 } else {
1971 // if input maps to a dynamic policy with an activity listener, notify of state change
1972 if ((inputDesc->mPolicyMix != NULL)
1973 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1974 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
1975 MIX_STATE_IDLE);
Eric Laurent84332aa2016-01-28 22:19:18 +00001976 }
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001977
1978 // automatically disable the remote submix output when input is stopped if not
1979 // used by a policy mix of type MIX_TYPE_RECORDERS
1980 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1981 String8 address = String8("");
1982 if (inputDesc->mPolicyMix == NULL) {
1983 address = String8("0");
1984 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1985 address = inputDesc->mPolicyMix->mDeviceAddress;
1986 }
1987 if (address != "") {
1988 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1989 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1990 address, "remote-submix");
1991 }
Eric Laurent84332aa2016-01-28 22:19:18 +00001992 }
Eric Laurent84332aa2016-01-28 22:19:18 +00001993
Eric Laurentf7c50102016-09-30 15:19:43 -07001994 audio_devices_t device = inputDesc->mDevice;
Eric Laurentfb66dd92016-01-28 18:32:03 -08001995 resetInputDevice(input);
Eric Laurent84332aa2016-01-28 22:19:18 +00001996
Eric Laurentf7c50102016-09-30 15:19:43 -07001997 // indicate inactive capture to sound trigger service if stopping capture from a mic on
1998 // primary HW module
1999 audio_devices_t primaryInputDevices = availablePrimaryInputDevices();
2000 if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) &&
2001 mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) {
Eric Laurentfb66dd92016-01-28 18:32:03 -08002002 SoundTrigger::setCaptureState(false);
2003 }
2004 inputDesc->clearPreemptedSessions();
Eric Laurent84332aa2016-01-28 22:19:18 +00002005 }
Eric Laurente552edb2014-03-10 17:42:56 -07002006 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002007 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07002008}
2009
Eric Laurent4dc68062014-07-28 17:26:49 -07002010void AudioPolicyManager::releaseInput(audio_io_handle_t input,
2011 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07002012{
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002013
Eric Laurente552edb2014-03-10 17:42:56 -07002014 ALOGV("releaseInput() %d", input);
2015 ssize_t index = mInputs.indexOfKey(input);
2016 if (index < 0) {
2017 ALOGW("releaseInput() releasing unknown input %d", input);
2018 return;
2019 }
Paul McLean466dc8e2015-04-17 13:15:36 -06002020
2021 // Routing
2022 mInputRoutes.removeRoute(session);
2023
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002024 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
2025 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07002026
Eric Laurent599c7582015-12-07 18:05:55 -08002027 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
vivek mehta587b8df2017-01-31 14:58:44 -08002028 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07002029 ALOGW("releaseInput() unknown session %d on input %d", session, input);
2030 return;
2031 }
Eric Laurent599c7582015-12-07 18:05:55 -08002032
2033 if (audioSession->openCount() == 0) {
2034 ALOGW("releaseInput() invalid open count %d on session %d",
2035 audioSession->openCount(), session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002036 return;
2037 }
Eric Laurent599c7582015-12-07 18:05:55 -08002038
2039 if (audioSession->changeOpenCount(-1) == 0) {
2040 inputDesc->removeAudioSession(session);
2041 }
2042
Jean-Michel Trivi65bfe912015-12-04 15:56:47 -08002043 if (inputDesc->getOpenRefCount() > 0) {
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002044 ALOGV("releaseInput() exit > 0");
2045 return;
2046 }
2047
Eric Laurent05b90f82014-08-27 15:32:29 -07002048 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07002049 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07002050 ALOGV("releaseInput() exit");
2051}
2052
Eric Laurentd4692962014-05-05 18:13:44 -07002053void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07002054 bool patchRemoved = false;
2055
Eric Laurentd4692962014-05-05 18:13:44 -07002056 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07002057 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08002058 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07002059 if (patch_index >= 0) {
2060 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07002061 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07002062 mAudioPatches.removeItemsAt(patch_index);
2063 patchRemoved = true;
2064 }
Eric Laurentd4692962014-05-05 18:13:44 -07002065 mpClientInterface->closeInput(mInputs.keyAt(input_index));
2066 }
2067 mInputs.clear();
Chris Thornton52785f32016-02-09 17:28:49 -08002068 SoundTrigger::setCaptureState(false);
Eric Laurent6a94d692014-05-20 11:18:06 -07002069 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07002070
2071 if (patchRemoved) {
2072 mpClientInterface->onAudioPatchListUpdate();
2073 }
Eric Laurentd4692962014-05-05 18:13:44 -07002074}
2075
Eric Laurente0720872014-03-11 09:30:41 -07002076void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07002077 int indexMin,
2078 int indexMax)
2079{
2080 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002081 mVolumeCurves->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent28d09f02016-03-08 10:43:05 -08002082
2083 // initialize other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08002084 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
2085 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002086 continue;
2087 }
2088 mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08002089 }
Eric Laurente552edb2014-03-10 17:42:56 -07002090}
2091
Eric Laurente0720872014-03-11 09:30:41 -07002092status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01002093 int index,
2094 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07002095{
2096
François Gaffied1ab2bd2015-12-02 18:20:06 +01002097 if ((index < mVolumeCurves->getVolumeIndexMin(stream)) ||
2098 (index > mVolumeCurves->getVolumeIndexMax(stream))) {
Eric Laurente552edb2014-03-10 17:42:56 -07002099 return BAD_VALUE;
2100 }
2101 if (!audio_is_output_device(device)) {
2102 return BAD_VALUE;
2103 }
2104
2105 // Force max volume if stream cannot be muted
François Gaffied1ab2bd2015-12-02 18:20:06 +01002106 if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07002107
Eric Laurent1fd372e2016-04-06 14:23:53 -07002108 ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d",
Eric Laurente552edb2014-03-10 17:42:56 -07002109 stream, device, index);
2110
Eric Laurent28d09f02016-03-08 10:43:05 -08002111 // update other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08002112 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
2113 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002114 continue;
2115 }
2116 mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index);
2117 }
Eric Laurente552edb2014-03-10 17:42:56 -07002118
Eric Laurent1fd372e2016-04-06 14:23:53 -07002119 // update volume on all outputs and streams matching the following:
2120 // - The requested stream (or a stream matching for volume control) is active on the output
2121 // - The device (or devices) selected by the strategy corresponding to this stream includes
2122 // the requested device
2123 // - For non default requested device, currently selected device on the output is either the
2124 // requested device or one of the devices selected by the strategy
Eric Laurent5a2b6292016-04-14 18:05:57 -07002125 // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if
2126 // no specific device volume value exists for currently selected device.
Eric Laurente552edb2014-03-10 17:42:56 -07002127 status_t status = NO_ERROR;
2128 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002129 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
2130 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent794fde22016-03-11 09:50:45 -08002131 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
2132 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002133 continue;
Eric Laurente552edb2014-03-10 17:42:56 -07002134 }
Eric Laurentd3926fe2016-04-15 18:10:07 -07002135 if (!(desc->isStreamActive((audio_stream_type_t)curStream) ||
2136 (isInCall() && (curStream == AUDIO_STREAM_VOICE_CALL)))) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07002137 continue;
2138 }
Eric Laurent794fde22016-03-11 09:50:45 -08002139 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurente76f29c2016-09-14 17:17:53 -07002140 audio_devices_t curStreamDevice = getDeviceForStrategy(curStrategy, false /*fromCache*/);
2141 if ((device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) &&
2142 ((curStreamDevice & device) == 0)) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07002143 continue;
2144 }
Eric Laurente76f29c2016-09-14 17:17:53 -07002145 bool applyVolume;
Eric Laurent5a2b6292016-04-14 18:05:57 -07002146 if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07002147 curStreamDevice |= device;
Eric Laurente76f29c2016-09-14 17:17:53 -07002148 applyVolume = (curDevice & curStreamDevice) != 0;
2149 } else {
2150 applyVolume = !mVolumeCurves->hasVolumeIndexForDevice(
2151 stream, Volume::getDeviceForVolume(curStreamDevice));
Eric Laurent1fd372e2016-04-06 14:23:53 -07002152 }
Eric Laurent28d09f02016-03-08 10:43:05 -08002153
Eric Laurente76f29c2016-09-14 17:17:53 -07002154 if (applyVolume) {
Eric Laurentdc462862016-07-19 12:29:53 -07002155 //FIXME: workaround for truncated touch sounds
2156 // delayed volume change for system stream to be removed when the problem is
2157 // handled by system UI
Eric Laurent28d09f02016-03-08 10:43:05 -08002158 status_t volStatus =
Eric Laurentdc462862016-07-19 12:29:53 -07002159 checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice,
2160 (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0);
Eric Laurent28d09f02016-03-08 10:43:05 -08002161 if (volStatus != NO_ERROR) {
2162 status = volStatus;
2163 }
2164 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08002165 }
Eric Laurente552edb2014-03-10 17:42:56 -07002166 }
2167 return status;
2168}
2169
Eric Laurente0720872014-03-11 09:30:41 -07002170status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07002171 int *index,
2172 audio_devices_t device)
2173{
2174 if (index == NULL) {
2175 return BAD_VALUE;
2176 }
2177 if (!audio_is_output_device(device)) {
2178 return BAD_VALUE;
2179 }
Eric Laurent5a2b6292016-04-14 18:05:57 -07002180 // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device corresponding to
Eric Laurente552edb2014-03-10 17:42:56 -07002181 // the strategy the stream belongs to.
Eric Laurent5a2b6292016-04-14 18:05:57 -07002182 if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurente552edb2014-03-10 17:42:56 -07002183 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
2184 }
François Gaffiedfd74092015-03-19 12:10:59 +01002185 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07002186
François Gaffied1ab2bd2015-12-02 18:20:06 +01002187 *index = mVolumeCurves->getVolumeIndex(stream, device);
Eric Laurente552edb2014-03-10 17:42:56 -07002188 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
2189 return NO_ERROR;
2190}
2191
Eric Laurent36829f92017-04-07 19:04:42 -07002192audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects()
Eric Laurente552edb2014-03-10 17:42:56 -07002193{
2194 // select one output among several suitable for global effects.
2195 // The priority is as follows:
2196 // 1: An offloaded output. If the effect ends up not being offloadable,
2197 // AudioFlinger will invalidate the track and the offloaded output
2198 // will be closed causing the effect to be moved to a PCM output.
2199 // 2: A deep buffer output
Eric Laurent36829f92017-04-07 19:04:42 -07002200 // 3: The primary output
2201 // 4: the first output in the list
Eric Laurente552edb2014-03-10 17:42:56 -07002202
Eric Laurent3b73df72014-03-11 09:06:29 -07002203 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07002204 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent36829f92017-04-07 19:04:42 -07002205 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
Eric Laurente552edb2014-03-10 17:42:56 -07002206
Eric Laurent36829f92017-04-07 19:04:42 -07002207 if (outputs.size() == 0) {
2208 return AUDIO_IO_HANDLE_NONE;
2209 }
Eric Laurente552edb2014-03-10 17:42:56 -07002210
Eric Laurent36829f92017-04-07 19:04:42 -07002211 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
2212 bool activeOnly = true;
2213
2214 while (output == AUDIO_IO_HANDLE_NONE) {
2215 audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE;
2216 audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE;
2217 audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE;
2218
2219 for (size_t i = 0; i < outputs.size(); i++) {
2220 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
2221 if (activeOnly && !desc->isStreamActive(AUDIO_STREAM_MUSIC)) {
2222 continue;
2223 }
2224 ALOGV("selectOutputForMusicEffects activeOnly %d outputs[%zu] flags 0x%08x",
2225 activeOnly, i, desc->mFlags);
2226 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
2227 outputOffloaded = outputs[i];
2228 }
2229 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
2230 outputDeepBuffer = outputs[i];
2231 }
2232 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) {
2233 outputPrimary = outputs[i];
2234 }
2235 }
2236 if (outputOffloaded != AUDIO_IO_HANDLE_NONE) {
2237 output = outputOffloaded;
2238 } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) {
2239 output = outputDeepBuffer;
2240 } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) {
2241 output = outputPrimary;
2242 } else {
2243 output = outputs[0];
2244 }
2245 activeOnly = false;
2246 }
2247
2248 if (output != mMusicEffectOutput) {
2249 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output);
2250 mMusicEffectOutput = output;
2251 }
2252
2253 ALOGV("selectOutputForMusicEffects selected output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -07002254 return output;
2255}
2256
Eric Laurent36829f92017-04-07 19:04:42 -07002257audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused)
2258{
2259 return selectOutputForMusicEffects();
2260}
2261
Eric Laurente0720872014-03-11 09:30:41 -07002262status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07002263 audio_io_handle_t io,
2264 uint32_t strategy,
2265 int session,
2266 int id)
2267{
2268 ssize_t index = mOutputs.indexOfKey(io);
2269 if (index < 0) {
2270 index = mInputs.indexOfKey(io);
2271 if (index < 0) {
2272 ALOGW("registerEffect() unknown io %d", io);
2273 return INVALID_OPERATION;
2274 }
2275 }
François Gaffie45ed3b02015-03-19 10:35:14 +01002276 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07002277}
2278
Eric Laurentc75307b2015-03-17 15:29:32 -07002279bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
2280{
Eric Laurent28d09f02016-03-08 10:43:05 -08002281 bool active = false;
Eric Laurent794fde22016-03-11 09:50:45 -08002282 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) {
2283 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002284 continue;
2285 }
2286 active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs);
2287 }
Eric Laurent28d09f02016-03-08 10:43:05 -08002288 return active;
Eric Laurentc75307b2015-03-17 15:29:32 -07002289}
2290
2291bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
2292{
2293 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
2294}
2295
Eric Laurente0720872014-03-11 09:30:41 -07002296bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07002297{
2298 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002299 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002300 if (inputDescriptor->isSourceActive(source)) {
Eric Laurente552edb2014-03-10 17:42:56 -07002301 return true;
2302 }
2303 }
2304 return false;
2305}
2306
Eric Laurent275e8e92014-11-30 15:14:47 -08002307// Register a list of custom mixes with their attributes and format.
2308// When a mix is registered, corresponding input and output profiles are
2309// added to the remote submix hw module. The profile contains only the
2310// parameters (sampling rate, format...) specified by the mix.
2311// The corresponding input remote submix device is also connected.
2312//
2313// When a remote submix device is connected, the address is checked to select the
2314// appropriate profile and the corresponding input or output stream is opened.
2315//
2316// When capture starts, getInputForAttr() will:
2317// - 1 look for a mix matching the address passed in attribtutes tags if any
2318// - 2 if none found, getDeviceForInputSource() will:
2319// - 2.1 look for a mix matching the attributes source
2320// - 2.2 if none found, default to device selection by policy rules
2321// At this time, the corresponding output remote submix device is also connected
2322// and active playback use cases can be transferred to this mix if needed when reconnecting
2323// after AudioTracks are invalidated
2324//
2325// When playback starts, getOutputForAttr() will:
2326// - 1 look for a mix matching the address passed in attribtutes tags if any
2327// - 2 if none found, look for a mix matching the attributes usage
2328// - 3 if none found, default to device and output selection by policy rules.
2329
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07002330status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes)
Eric Laurent275e8e92014-11-30 15:14:47 -08002331{
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002332 ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size());
2333 status_t res = NO_ERROR;
2334
2335 sp<HwModule> rSubmixModule;
2336 // examine each mix's route type
2337 for (size_t i = 0; i < mixes.size(); i++) {
2338 // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination
2339 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) {
2340 res = INVALID_OPERATION;
Eric Laurent275e8e92014-11-30 15:14:47 -08002341 break;
2342 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002343 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
2344 // Loop back through "remote submix"
2345 if (rSubmixModule == 0) {
2346 for (size_t j = 0; i < mHwModules.size(); j++) {
2347 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2348 && mHwModules[j]->mHandle != 0) {
2349 rSubmixModule = mHwModules[j];
2350 break;
2351 }
2352 }
2353 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002354
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002355 ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size());
Eric Laurent275e8e92014-11-30 15:14:47 -08002356
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002357 if (rSubmixModule == 0) {
2358 ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", i);
2359 res = INVALID_OPERATION;
2360 break;
2361 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002362
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002363 String8 address = mixes[i].mDeviceAddress;
François Gaffie036e1e92015-03-19 10:16:24 +01002364
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002365 if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) {
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002366 ALOGE(" Error registering mix %zu for address %s", i, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002367 res = INVALID_OPERATION;
2368 break;
2369 }
2370 audio_config_t outputConfig = mixes[i].mFormat;
2371 audio_config_t inputConfig = mixes[i].mFormat;
2372 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
2373 // stereo and let audio flinger do the channel conversion if needed.
2374 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2375 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
2376 rSubmixModule->addOutputProfile(address, &outputConfig,
2377 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
2378 rSubmixModule->addInputProfile(address, &inputConfig,
2379 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01002380
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002381 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
2382 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2383 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2384 address.string(), "remote-submix");
2385 } else {
2386 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2387 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2388 address.string(), "remote-submix");
2389 }
2390 } else if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002391 String8 address = mixes[i].mDeviceAddress;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002392 audio_devices_t device = mixes[i].mDeviceType;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002393 ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s",
2394 i, mixes.size(), device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002395
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002396 bool foundOutput = false;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002397 for (size_t j = 0 ; j < mOutputs.size() ; j++) {
2398 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j);
2399 sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle());
2400 if ((patch != 0) && (patch->mPatch.num_sinks != 0)
2401 && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE)
2402 && (patch->mPatch.sinks[0].ext.device.type == device)
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002403 && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(),
2404 AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002405 if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) {
2406 res = INVALID_OPERATION;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002407 } else {
2408 foundOutput = true;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002409 }
2410 break;
2411 }
2412 }
2413
2414 if (res != NO_ERROR) {
2415 ALOGE(" Error registering mix %zu for device 0x%X addr %s",
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002416 i, device, address.string());
2417 res = INVALID_OPERATION;
2418 break;
2419 } else if (!foundOutput) {
2420 ALOGE(" Output not found for mix %zu for device 0x%X addr %s",
2421 i, device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002422 res = INVALID_OPERATION;
2423 break;
2424 }
Eric Laurentc722f302014-12-10 11:21:49 -08002425 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002426 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002427 if (res != NO_ERROR) {
2428 unregisterPolicyMixes(mixes);
2429 }
2430 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002431}
2432
2433status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
2434{
Eric Laurent7b279bb2015-12-14 10:18:23 -08002435 ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002436 status_t res = NO_ERROR;
2437 sp<HwModule> rSubmixModule;
2438 // examine each mix's route type
Eric Laurent275e8e92014-11-30 15:14:47 -08002439 for (size_t i = 0; i < mixes.size(); i++) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002440 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
François Gaffie036e1e92015-03-19 10:16:24 +01002441
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002442 if (rSubmixModule == 0) {
2443 for (size_t j = 0; i < mHwModules.size(); j++) {
2444 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2445 && mHwModules[j]->mHandle != 0) {
2446 rSubmixModule = mHwModules[j];
2447 break;
2448 }
2449 }
2450 }
2451 if (rSubmixModule == 0) {
2452 res = INVALID_OPERATION;
2453 continue;
2454 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002455
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002456 String8 address = mixes[i].mDeviceAddress;
Eric Laurent275e8e92014-11-30 15:14:47 -08002457
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002458 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
2459 res = INVALID_OPERATION;
2460 continue;
2461 }
2462
2463 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
2464 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2465 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2466 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2467 address.string(), "remote-submix");
2468 }
2469 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
2470 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2471 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2472 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2473 address.string(), "remote-submix");
2474 }
2475 rSubmixModule->removeOutputProfile(address);
2476 rSubmixModule->removeInputProfile(address);
2477
2478 } if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
2479 if (mPolicyMixes.unregisterMix(mixes[i].mDeviceAddress) != NO_ERROR) {
2480 res = INVALID_OPERATION;
2481 continue;
2482 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002483 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002484 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002485 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002486}
2487
Eric Laurente552edb2014-03-10 17:42:56 -07002488
Eric Laurente0720872014-03-11 09:30:41 -07002489status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07002490{
2491 const size_t SIZE = 256;
2492 char buffer[SIZE];
2493 String8 result;
2494
2495 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
2496 result.append(buffer);
2497
Eric Laurent87ffa392015-05-22 10:32:38 -07002498 snprintf(buffer, SIZE, " Primary Output: %d\n",
2499 hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE);
Eric Laurente552edb2014-03-10 17:42:56 -07002500 result.append(buffer);
Mikhail Naganov0d6a0332016-04-19 17:12:38 -07002501 std::string stateLiteral;
2502 AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral);
2503 snprintf(buffer, SIZE, " Phone state: %s\n", stateLiteral.c_str());
Eric Laurente552edb2014-03-10 17:42:56 -07002504 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002505 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002506 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07002507 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002508 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07002509 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002510 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07002511 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002512 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07002513 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002514 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07002515 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002516 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002517 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002518 result.append(buffer);
Phil Burk09bc4612016-02-24 15:58:15 -08002519 snprintf(buffer, SIZE, " Force use for encoded surround output %d\n",
2520 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND));
2521 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002522 snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available");
2523 result.append(buffer);
Andy Hung2ddee192015-12-18 17:34:44 -08002524 snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off");
2525 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002526
François Gaffie2110e042015-03-24 08:41:51 +01002527 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07002528
François Gaffie112b0af2015-11-19 16:13:25 +01002529 mAvailableOutputDevices.dump(fd, String8("Available output"));
2530 mAvailableInputDevices.dump(fd, String8("Available input"));
François Gaffie53615e22015-03-19 09:24:12 +01002531 mHwModules.dump(fd);
2532 mOutputs.dump(fd);
2533 mInputs.dump(fd);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002534 mVolumeCurves->dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01002535 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002536 mAudioPatches.dump(fd);
Mikhail Naganov44344b02016-12-13 11:21:02 -08002537 mPolicyMixes.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07002538
2539 return NO_ERROR;
2540}
2541
2542// This function checks for the parameters which can be offloaded.
2543// This can be enhanced depending on the capability of the DSP and policy
2544// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002545bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002546{
2547 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002548 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002549 offloadInfo.sample_rate, offloadInfo.channel_mask,
2550 offloadInfo.format,
2551 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2552 offloadInfo.has_video);
2553
Andy Hung2ddee192015-12-18 17:34:44 -08002554 if (mMasterMono) {
2555 return false; // no offloading if mono is set.
2556 }
2557
Eric Laurente552edb2014-03-10 17:42:56 -07002558 // Check if offload has been disabled
2559 char propValue[PROPERTY_VALUE_MAX];
2560 if (property_get("audio.offload.disable", propValue, "0")) {
2561 if (atoi(propValue) != 0) {
2562 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2563 return false;
2564 }
2565 }
2566
2567 // Check if stream type is music, then only allow offload as of now.
2568 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2569 {
2570 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2571 return false;
2572 }
2573
2574 //TODO: enable audio offloading with video when ready
Andy Hung08945c42015-05-31 21:36:46 -07002575 const bool allowOffloadWithVideo =
2576 property_get_bool("audio.offload.video", false /* default_value */);
2577 if (offloadInfo.has_video && !allowOffloadWithVideo) {
Eric Laurente552edb2014-03-10 17:42:56 -07002578 ALOGV("isOffloadSupported: has_video == true, returning false");
2579 return false;
2580 }
2581
2582 //If duration is less than minimum value defined in property, return false
2583 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2584 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2585 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2586 return false;
2587 }
2588 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2589 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2590 return false;
2591 }
2592
2593 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2594 // creating an offloaded track and tearing it down immediately after start when audioflinger
2595 // detects there is an active non offloadable effect.
2596 // FIXME: We should check the audio session here but we do not have it in this context.
2597 // This may prevent offloading in rare situations where effects are left active by apps
2598 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002599 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002600 return false;
2601 }
2602
2603 // See if there is a profile to support this.
2604 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002605 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002606 offloadInfo.sample_rate,
2607 offloadInfo.format,
2608 offloadInfo.channel_mask,
2609 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002610 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2611 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002612}
2613
Eric Laurent6a94d692014-05-20 11:18:06 -07002614status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2615 audio_port_type_t type,
2616 unsigned int *num_ports,
2617 struct audio_port *ports,
2618 unsigned int *generation)
2619{
2620 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2621 generation == NULL) {
2622 return BAD_VALUE;
2623 }
2624 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2625 if (ports == NULL) {
2626 *num_ports = 0;
2627 }
2628
2629 size_t portsWritten = 0;
2630 size_t portsMax = *num_ports;
2631 *num_ports = 0;
2632 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002633 // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB
2634 // as they are used by stub HALs by convention
Eric Laurent6a94d692014-05-20 11:18:06 -07002635 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002636 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
2637 if (mAvailableOutputDevices[i]->type() == AUDIO_DEVICE_OUT_STUB) {
2638 continue;
2639 }
2640 if (portsWritten < portsMax) {
2641 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2642 }
2643 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002644 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002645 }
2646 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002647 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
2648 if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_STUB) {
2649 continue;
2650 }
2651 if (portsWritten < portsMax) {
2652 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2653 }
2654 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002655 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002656 }
2657 }
2658 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2659 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2660 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2661 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2662 }
2663 *num_ports += mInputs.size();
2664 }
2665 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002666 size_t numOutputs = 0;
2667 for (size_t i = 0; i < mOutputs.size(); i++) {
2668 if (!mOutputs[i]->isDuplicated()) {
2669 numOutputs++;
2670 if (portsWritten < portsMax) {
2671 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2672 }
2673 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002674 }
Eric Laurent84c70242014-06-23 08:46:27 -07002675 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002676 }
2677 }
2678 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002679 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002680 return NO_ERROR;
2681}
2682
2683status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2684{
2685 return NO_ERROR;
2686}
2687
Eric Laurent6a94d692014-05-20 11:18:06 -07002688status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2689 audio_patch_handle_t *handle,
2690 uid_t uid)
2691{
2692 ALOGV("createAudioPatch()");
2693
2694 if (handle == NULL || patch == NULL) {
2695 return BAD_VALUE;
2696 }
2697 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2698
Eric Laurent874c42872014-08-08 15:13:39 -07002699 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2700 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2701 return BAD_VALUE;
2702 }
2703 // only one source per audio patch supported for now
2704 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002705 return INVALID_OPERATION;
2706 }
Eric Laurent874c42872014-08-08 15:13:39 -07002707
2708 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002709 return INVALID_OPERATION;
2710 }
Eric Laurent874c42872014-08-08 15:13:39 -07002711 for (size_t i = 0; i < patch->num_sinks; i++) {
2712 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2713 return INVALID_OPERATION;
2714 }
2715 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002716
2717 sp<AudioPatch> patchDesc;
2718 ssize_t index = mAudioPatches.indexOfKey(*handle);
2719
Eric Laurent6a94d692014-05-20 11:18:06 -07002720 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2721 patch->sources[0].role,
2722 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002723#if LOG_NDEBUG == 0
2724 for (size_t i = 0; i < patch->num_sinks; i++) {
Eric Laurent7b279bb2015-12-14 10:18:23 -08002725 ALOGV("createAudioPatch sink %zu: id %d role %d type %d", i, patch->sinks[i].id,
Eric Laurent874c42872014-08-08 15:13:39 -07002726 patch->sinks[i].role,
2727 patch->sinks[i].type);
2728 }
2729#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002730
2731 if (index >= 0) {
2732 patchDesc = mAudioPatches.valueAt(index);
2733 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2734 mUidCached, patchDesc->mUid, uid);
2735 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2736 return INVALID_OPERATION;
2737 }
2738 } else {
Glenn Kastena13cde92016-03-28 15:26:02 -07002739 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07002740 }
2741
2742 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002743 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002744 if (outputDesc == NULL) {
2745 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2746 return BAD_VALUE;
2747 }
Eric Laurent84c70242014-06-23 08:46:27 -07002748 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2749 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002750 if (patchDesc != 0) {
2751 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2752 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2753 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2754 return BAD_VALUE;
2755 }
2756 }
Eric Laurent874c42872014-08-08 15:13:39 -07002757 DeviceVector devices;
2758 for (size_t i = 0; i < patch->num_sinks; i++) {
2759 // Only support mix to devices connection
2760 // TODO add support for mix to mix connection
2761 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2762 ALOGV("createAudioPatch() source mix but sink is not a device");
2763 return INVALID_OPERATION;
2764 }
2765 sp<DeviceDescriptor> devDesc =
2766 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2767 if (devDesc == 0) {
2768 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2769 return BAD_VALUE;
2770 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002771
François Gaffie53615e22015-03-19 09:24:12 +01002772 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002773 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002774 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002775 NULL, // updatedSamplingRate
2776 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002777 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002778 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002779 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002780 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002781 ALOGV("createAudioPatch() profile not supported for device %08x",
2782 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002783 return INVALID_OPERATION;
2784 }
2785 devices.add(devDesc);
2786 }
2787 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002788 return INVALID_OPERATION;
2789 }
Eric Laurent874c42872014-08-08 15:13:39 -07002790
Eric Laurent6a94d692014-05-20 11:18:06 -07002791 // TODO: reconfigure output format and channels here
2792 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002793 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002794 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002795 index = mAudioPatches.indexOfKey(*handle);
2796 if (index >= 0) {
2797 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2798 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2799 }
2800 patchDesc = mAudioPatches.valueAt(index);
2801 patchDesc->mUid = uid;
2802 ALOGV("createAudioPatch() success");
2803 } else {
2804 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2805 return INVALID_OPERATION;
2806 }
2807 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2808 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2809 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002810 // only one sink supported when connecting an input device to a mix
2811 if (patch->num_sinks > 1) {
2812 return INVALID_OPERATION;
2813 }
François Gaffie53615e22015-03-19 09:24:12 +01002814 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002815 if (inputDesc == NULL) {
2816 return BAD_VALUE;
2817 }
2818 if (patchDesc != 0) {
2819 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2820 return BAD_VALUE;
2821 }
2822 }
2823 sp<DeviceDescriptor> devDesc =
2824 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2825 if (devDesc == 0) {
2826 return BAD_VALUE;
2827 }
2828
François Gaffie53615e22015-03-19 09:24:12 +01002829 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002830 devDesc->mAddress,
2831 patch->sinks[0].sample_rate,
2832 NULL, /*updatedSampleRate*/
2833 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002834 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002835 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002836 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002837 // FIXME for the parameter type,
2838 // and the NONE
2839 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002840 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002841 return INVALID_OPERATION;
2842 }
2843 // TODO: reconfigure output format and channels here
2844 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002845 devDesc->type(), inputDesc->mIoHandle);
2846 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002847 index = mAudioPatches.indexOfKey(*handle);
2848 if (index >= 0) {
2849 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2850 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2851 }
2852 patchDesc = mAudioPatches.valueAt(index);
2853 patchDesc->mUid = uid;
2854 ALOGV("createAudioPatch() success");
2855 } else {
2856 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2857 return INVALID_OPERATION;
2858 }
2859 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2860 // device to device connection
2861 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002862 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002863 return BAD_VALUE;
2864 }
2865 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002866 sp<DeviceDescriptor> srcDeviceDesc =
2867 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002868 if (srcDeviceDesc == 0) {
2869 return BAD_VALUE;
2870 }
Eric Laurent874c42872014-08-08 15:13:39 -07002871
Eric Laurent6a94d692014-05-20 11:18:06 -07002872 //update source and sink with our own data as the data passed in the patch may
2873 // be incomplete.
2874 struct audio_patch newPatch = *patch;
2875 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002876
Eric Laurent874c42872014-08-08 15:13:39 -07002877 for (size_t i = 0; i < patch->num_sinks; i++) {
2878 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2879 ALOGV("createAudioPatch() source device but one sink is not a device");
2880 return INVALID_OPERATION;
2881 }
2882
2883 sp<DeviceDescriptor> sinkDeviceDesc =
2884 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2885 if (sinkDeviceDesc == 0) {
2886 return BAD_VALUE;
2887 }
2888 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2889
Eric Laurent3bcf8592015-04-03 12:13:24 -07002890 // create a software bridge in PatchPanel if:
2891 // - source and sink devices are on differnt HW modules OR
2892 // - audio HAL version is < 3.0
Eric Laurentfb66dd92016-01-28 18:32:03 -08002893 if (!srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) ||
Mikhail Naganov9ee05402016-10-13 15:58:17 -07002894 (srcDeviceDesc->mModule->getHalVersionMajor() < 3)) {
Eric Laurent3bcf8592015-04-03 12:13:24 -07002895 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002896 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002897 return INVALID_OPERATION;
2898 }
Eric Laurent874c42872014-08-08 15:13:39 -07002899 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002900 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002901 // if the sink device is reachable via an opened output stream, request to go via
2902 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002903 audio_io_handle_t output = selectOutput(outputs,
2904 AUDIO_OUTPUT_FLAG_NONE,
2905 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002906 if (output != AUDIO_IO_HANDLE_NONE) {
2907 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2908 if (outputDesc->isDuplicated()) {
2909 return INVALID_OPERATION;
2910 }
2911 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002912 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002913 newPatch.num_sources = 2;
2914 }
Eric Laurent83b88082014-06-20 18:31:16 -07002915 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002916 }
2917 // TODO: check from routing capabilities in config file and other conflicting patches
2918
2919 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2920 if (index >= 0) {
2921 afPatchHandle = patchDesc->mAfPatchHandle;
2922 }
2923
2924 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2925 &afPatchHandle,
2926 0);
2927 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2928 status, afPatchHandle);
2929 if (status == NO_ERROR) {
2930 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002931 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002932 addAudioPatch(patchDesc->mHandle, patchDesc);
2933 } else {
2934 patchDesc->mPatch = newPatch;
2935 }
2936 patchDesc->mAfPatchHandle = afPatchHandle;
2937 *handle = patchDesc->mHandle;
2938 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002939 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002940 } else {
2941 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2942 status);
2943 return INVALID_OPERATION;
2944 }
2945 } else {
2946 return BAD_VALUE;
2947 }
2948 } else {
2949 return BAD_VALUE;
2950 }
2951 return NO_ERROR;
2952}
2953
2954status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2955 uid_t uid)
2956{
2957 ALOGV("releaseAudioPatch() patch %d", handle);
2958
2959 ssize_t index = mAudioPatches.indexOfKey(handle);
2960
2961 if (index < 0) {
2962 return BAD_VALUE;
2963 }
2964 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2965 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2966 mUidCached, patchDesc->mUid, uid);
2967 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2968 return INVALID_OPERATION;
2969 }
2970
2971 struct audio_patch *patch = &patchDesc->mPatch;
2972 patchDesc->mUid = mUidCached;
2973 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002974 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002975 if (outputDesc == NULL) {
2976 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2977 return BAD_VALUE;
2978 }
2979
Eric Laurentc75307b2015-03-17 15:29:32 -07002980 setOutputDevice(outputDesc,
2981 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002982 true,
2983 0,
2984 NULL);
2985 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2986 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002987 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002988 if (inputDesc == NULL) {
2989 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2990 return BAD_VALUE;
2991 }
2992 setInputDevice(inputDesc->mIoHandle,
Eric Laurentfb66dd92016-01-28 18:32:03 -08002993 getNewInputDevice(inputDesc),
Eric Laurent6a94d692014-05-20 11:18:06 -07002994 true,
2995 NULL);
2996 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002997 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2998 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2999 status, patchDesc->mAfPatchHandle);
3000 removeAudioPatch(patchDesc->mHandle);
3001 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07003002 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07003003 } else {
3004 return BAD_VALUE;
3005 }
3006 } else {
3007 return BAD_VALUE;
3008 }
3009 return NO_ERROR;
3010}
3011
3012status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
3013 struct audio_patch *patches,
3014 unsigned int *generation)
3015{
François Gaffie53615e22015-03-19 09:24:12 +01003016 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003017 return BAD_VALUE;
3018 }
Eric Laurent6a94d692014-05-20 11:18:06 -07003019 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01003020 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07003021}
3022
Eric Laurente1715a42014-05-20 11:30:42 -07003023status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07003024{
Eric Laurente1715a42014-05-20 11:30:42 -07003025 ALOGV("setAudioPortConfig()");
3026
3027 if (config == NULL) {
3028 return BAD_VALUE;
3029 }
3030 ALOGV("setAudioPortConfig() on port handle %d", config->id);
3031 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07003032 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
3033 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07003034 }
3035
Eric Laurenta121f902014-06-03 13:32:54 -07003036 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07003037 if (config->type == AUDIO_PORT_TYPE_MIX) {
3038 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003039 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07003040 if (outputDesc == NULL) {
3041 return BAD_VALUE;
3042 }
Eric Laurent84c70242014-06-23 08:46:27 -07003043 ALOG_ASSERT(!outputDesc->isDuplicated(),
3044 "setAudioPortConfig() called on duplicated output %d",
3045 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07003046 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07003047 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01003048 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07003049 if (inputDesc == NULL) {
3050 return BAD_VALUE;
3051 }
Eric Laurenta121f902014-06-03 13:32:54 -07003052 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07003053 } else {
3054 return BAD_VALUE;
3055 }
3056 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
3057 sp<DeviceDescriptor> deviceDesc;
3058 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
3059 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
3060 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
3061 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
3062 } else {
3063 return BAD_VALUE;
3064 }
3065 if (deviceDesc == NULL) {
3066 return BAD_VALUE;
3067 }
Eric Laurenta121f902014-06-03 13:32:54 -07003068 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07003069 } else {
3070 return BAD_VALUE;
3071 }
3072
Eric Laurenta121f902014-06-03 13:32:54 -07003073 struct audio_port_config backupConfig;
3074 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
3075 if (status == NO_ERROR) {
3076 struct audio_port_config newConfig;
3077 audioPortConfig->toAudioPortConfig(&newConfig, config);
3078 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07003079 }
Eric Laurenta121f902014-06-03 13:32:54 -07003080 if (status != NO_ERROR) {
3081 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07003082 }
Eric Laurente1715a42014-05-20 11:30:42 -07003083
3084 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07003085}
3086
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003087void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
3088{
Eric Laurentd60560a2015-04-10 11:31:20 -07003089 clearAudioSources(uid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003090 clearAudioPatches(uid);
3091 clearSessionRoutes(uid);
3092}
3093
Eric Laurent6a94d692014-05-20 11:18:06 -07003094void AudioPolicyManager::clearAudioPatches(uid_t uid)
3095{
Eric Laurent0add0fd2014-12-04 18:58:14 -08003096 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003097 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
3098 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08003099 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07003100 }
3101 }
3102}
3103
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003104void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
3105 audio_io_handle_t ouptutToSkip)
3106{
3107 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
3108 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
3109 for (size_t j = 0; j < mOutputs.size(); j++) {
3110 if (mOutputs.keyAt(j) == ouptutToSkip) {
3111 continue;
3112 }
3113 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
3114 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
3115 continue;
3116 }
3117 // If the default device for this strategy is on another output mix,
3118 // invalidate all tracks in this strategy to force re connection.
3119 // Otherwise select new device on the output mix.
3120 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
Eric Laurent794fde22016-03-11 09:50:45 -08003121 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003122 if (getStrategy((audio_stream_type_t)stream) == strategy) {
3123 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
3124 }
3125 }
3126 } else {
3127 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
3128 setOutputDevice(outputDesc, newDevice, false);
3129 }
3130 }
3131}
3132
3133void AudioPolicyManager::clearSessionRoutes(uid_t uid)
3134{
3135 // remove output routes associated with this uid
3136 SortedVector<routing_strategy> affectedStrategies;
3137 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
3138 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
3139 if (route->mUid == uid) {
3140 mOutputRoutes.removeItemsAt(i);
3141 if (route->mDeviceDescriptor != 0) {
3142 affectedStrategies.add(getStrategy(route->mStreamType));
3143 }
3144 }
3145 }
3146 // reroute outputs if necessary
3147 for (size_t i = 0; i < affectedStrategies.size(); i++) {
3148 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
3149 }
3150
3151 // remove input routes associated with this uid
3152 SortedVector<audio_source_t> affectedSources;
3153 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
3154 sp<SessionRoute> route = mInputRoutes.valueAt(i);
3155 if (route->mUid == uid) {
3156 mInputRoutes.removeItemsAt(i);
3157 if (route->mDeviceDescriptor != 0) {
3158 affectedSources.add(route->mSource);
3159 }
3160 }
3161 }
3162 // reroute inputs if necessary
3163 SortedVector<audio_io_handle_t> inputsToClose;
3164 for (size_t i = 0; i < mInputs.size(); i++) {
3165 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08003166 if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003167 inputsToClose.add(inputDesc->mIoHandle);
3168 }
3169 }
3170 for (size_t i = 0; i < inputsToClose.size(); i++) {
3171 closeInput(inputsToClose[i]);
3172 }
3173}
3174
Eric Laurentd60560a2015-04-10 11:31:20 -07003175void AudioPolicyManager::clearAudioSources(uid_t uid)
3176{
3177 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
3178 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3179 if (sourceDesc->mUid == uid) {
3180 stopAudioSource(mAudioSources.keyAt(i));
3181 }
3182 }
3183}
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003184
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07003185status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
3186 audio_io_handle_t *ioHandle,
3187 audio_devices_t *device)
3188{
Glenn Kastenf0c6d7d2016-02-26 10:44:04 -08003189 *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
3190 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
Eric Laurentc73ca6e2014-12-12 14:34:22 -08003191 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07003192
François Gaffiedf372692015-03-19 10:43:27 +01003193 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07003194}
3195
Eric Laurentd60560a2015-04-10 11:31:20 -07003196status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source,
3197 const audio_attributes_t *attributes,
Glenn Kasten559d4392016-03-29 13:42:57 -07003198 audio_patch_handle_t *handle,
Eric Laurentd60560a2015-04-10 11:31:20 -07003199 uid_t uid)
Eric Laurent554a2772015-04-10 11:29:24 -07003200{
Eric Laurentd60560a2015-04-10 11:31:20 -07003201 ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle);
3202 if (source == NULL || attributes == NULL || handle == NULL) {
3203 return BAD_VALUE;
3204 }
3205
Glenn Kasten559d4392016-03-29 13:42:57 -07003206 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentd60560a2015-04-10 11:31:20 -07003207
3208 if (source->role != AUDIO_PORT_ROLE_SOURCE ||
3209 source->type != AUDIO_PORT_TYPE_DEVICE) {
3210 ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type);
3211 return INVALID_OPERATION;
3212 }
3213
3214 sp<DeviceDescriptor> srcDeviceDesc =
3215 mAvailableInputDevices.getDevice(source->ext.device.type,
3216 String8(source->ext.device.address));
3217 if (srcDeviceDesc == 0) {
3218 ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type);
3219 return BAD_VALUE;
3220 }
3221 sp<AudioSourceDescriptor> sourceDesc =
3222 new AudioSourceDescriptor(srcDeviceDesc, attributes, uid);
3223
3224 struct audio_patch dummyPatch;
3225 sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid);
3226 sourceDesc->mPatchDesc = patchDesc;
3227
3228 status_t status = connectAudioSource(sourceDesc);
3229 if (status == NO_ERROR) {
3230 mAudioSources.add(sourceDesc->getHandle(), sourceDesc);
3231 *handle = sourceDesc->getHandle();
3232 }
3233 return status;
3234}
3235
3236status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3237{
3238 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3239
3240 // make sure we only have one patch per source.
3241 disconnectAudioSource(sourceDesc);
3242
3243 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
Eric Laurent28d09f02016-03-08 10:43:05 -08003244 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003245 sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice;
3246
3247 audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true);
3248 sp<DeviceDescriptor> sinkDeviceDesc =
3249 mAvailableOutputDevices.getDevice(sinkDevice, String8(""));
3250
3251 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3252 struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch;
3253
3254 if (srcDeviceDesc->getAudioPort()->mModule->getHandle() ==
3255 sinkDeviceDesc->getAudioPort()->mModule->getHandle() &&
Mikhail Naganov9ee05402016-10-13 15:58:17 -07003256 srcDeviceDesc->getAudioPort()->mModule->getHalVersionMajor() >= 3 &&
Eric Laurentd60560a2015-04-10 11:31:20 -07003257 srcDeviceDesc->getAudioPort()->mGains.size() > 0) {
3258 ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__);
3259 // create patch between src device and output device
3260 // create Hwoutput and add to mHwOutputs
3261 } else {
3262 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs);
3263 audio_io_handle_t output =
3264 selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID);
3265 if (output == AUDIO_IO_HANDLE_NONE) {
3266 ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice);
3267 return INVALID_OPERATION;
3268 }
3269 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
3270 if (outputDesc->isDuplicated()) {
3271 ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice);
3272 return INVALID_OPERATION;
3273 }
3274 // create a special patch with no sink and two sources:
3275 // - the second source indicates to PatchPanel through which output mix this patch should
3276 // be connected as well as the stream type for volume control
3277 // - the sink is defined by whatever output device is currently selected for the output
3278 // though which this patch is routed.
3279 patch->num_sinks = 0;
3280 patch->num_sources = 2;
3281 srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL);
3282 outputDesc->toAudioPortConfig(&patch->sources[1], NULL);
3283 patch->sources[1].ext.mix.usecase.stream = stream;
3284 status_t status = mpClientInterface->createAudioPatch(patch,
3285 &afPatchHandle,
3286 0);
3287 ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__,
3288 status, afPatchHandle);
3289 if (status != NO_ERROR) {
3290 ALOGW("%s patch panel could not connect device patch, error %d",
3291 __FUNCTION__, status);
3292 return INVALID_OPERATION;
3293 }
3294 uint32_t delayMs = 0;
Eric Laurentc40d9692016-04-13 19:14:13 -07003295 status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs);
Eric Laurentd60560a2015-04-10 11:31:20 -07003296
3297 if (status != NO_ERROR) {
3298 mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0);
3299 return status;
3300 }
3301 sourceDesc->mSwOutput = outputDesc;
3302 if (delayMs != 0) {
3303 usleep(delayMs * 1000);
3304 }
3305 }
3306
3307 sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle;
3308 addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc);
3309
3310 return NO_ERROR;
Eric Laurent554a2772015-04-10 11:29:24 -07003311}
3312
Glenn Kasten559d4392016-03-29 13:42:57 -07003313status_t AudioPolicyManager::stopAudioSource(audio_patch_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07003314{
Eric Laurentd60560a2015-04-10 11:31:20 -07003315 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle);
3316 ALOGV("%s handle %d", __FUNCTION__, handle);
3317 if (sourceDesc == 0) {
3318 ALOGW("%s unknown source for handle %d", __FUNCTION__, handle);
3319 return BAD_VALUE;
3320 }
3321 status_t status = disconnectAudioSource(sourceDesc);
3322
3323 mAudioSources.removeItem(handle);
3324 return status;
3325}
3326
Andy Hung2ddee192015-12-18 17:34:44 -08003327status_t AudioPolicyManager::setMasterMono(bool mono)
3328{
3329 if (mMasterMono == mono) {
3330 return NO_ERROR;
3331 }
3332 mMasterMono = mono;
3333 // if enabling mono we close all offloaded devices, which will invalidate the
3334 // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible
3335 // for recreating the new AudioTrack as non-offloaded PCM.
3336 //
3337 // If disabling mono, we leave all tracks as is: we don't know which clients
3338 // and tracks are able to be recreated as offloaded. The next "song" should
3339 // play back offloaded.
3340 if (mMasterMono) {
3341 Vector<audio_io_handle_t> offloaded;
3342 for (size_t i = 0; i < mOutputs.size(); ++i) {
3343 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
3344 if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
3345 offloaded.push(desc->mIoHandle);
3346 }
3347 }
3348 for (size_t i = 0; i < offloaded.size(); ++i) {
3349 closeOutput(offloaded[i]);
3350 }
3351 }
3352 // update master mono for all remaining outputs
3353 for (size_t i = 0; i < mOutputs.size(); ++i) {
3354 updateMono(mOutputs.keyAt(i));
3355 }
3356 return NO_ERROR;
3357}
3358
3359status_t AudioPolicyManager::getMasterMono(bool *mono)
3360{
3361 *mono = mMasterMono;
3362 return NO_ERROR;
3363}
3364
Eric Laurentd60560a2015-04-10 11:31:20 -07003365status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3366{
3367 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3368
3369 sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle);
3370 if (patchDesc == 0) {
3371 ALOGW("%s source has no patch with handle %d", __FUNCTION__,
3372 sourceDesc->mPatchDesc->mHandle);
3373 return BAD_VALUE;
3374 }
3375 removeAudioPatch(sourceDesc->mPatchDesc->mHandle);
3376
Eric Laurent28d09f02016-03-08 10:43:05 -08003377 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003378 sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote();
3379 if (swOutputDesc != 0) {
3380 stopSource(swOutputDesc, stream, false);
3381 mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3382 } else {
3383 sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote();
3384 if (hwOutputDesc != 0) {
3385 // release patch between src device and output device
3386 // close Hwoutput and remove from mHwOutputs
3387 } else {
3388 ALOGW("%s source has neither SW nor HW output", __FUNCTION__);
3389 }
3390 }
3391 return NO_ERROR;
3392}
3393
3394sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput(
3395 audio_io_handle_t output, routing_strategy strategy)
3396{
3397 sp<AudioSourceDescriptor> source;
3398 for (size_t i = 0; i < mAudioSources.size(); i++) {
3399 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3400 routing_strategy sourceStrategy =
3401 (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
3402 sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote();
3403 if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) {
3404 source = sourceDesc;
3405 break;
3406 }
3407 }
3408 return source;
Eric Laurent554a2772015-04-10 11:29:24 -07003409}
3410
Eric Laurente552edb2014-03-10 17:42:56 -07003411// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07003412// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07003413// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07003414uint32_t AudioPolicyManager::nextAudioPortGeneration()
3415{
3416 return android_atomic_inc(&mAudioPortGeneration);
3417}
3418
Jaekyun Seok0d4a6af2017-02-17 17:10:17 +09003419#ifdef USE_XML_AUDIO_POLICY_CONF
3420// Treblized audio policy xml config will be located in /odm/etc or /vendor/etc.
3421static const char *kConfigLocationList[] =
3422 {"/odm/etc", "/vendor/etc", "/system/etc"};
3423static const int kConfigLocationListSize =
3424 (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0]));
3425
3426static status_t deserializeAudioPolicyXmlConfig(AudioPolicyConfig &config) {
3427 char audioPolicyXmlConfigFile[AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH];
3428 status_t ret;
3429
3430 for (int i = 0; i < kConfigLocationListSize; i++) {
3431 PolicySerializer serializer;
3432 snprintf(audioPolicyXmlConfigFile,
3433 sizeof(audioPolicyXmlConfigFile),
3434 "%s/%s",
3435 kConfigLocationList[i],
3436 AUDIO_POLICY_XML_CONFIG_FILE_NAME);
3437 ret = serializer.deserialize(audioPolicyXmlConfigFile, config);
3438 if (ret == NO_ERROR) {
3439 break;
3440 }
3441 }
3442 return ret;
3443}
3444#endif
3445
Eric Laurente0720872014-03-11 09:30:41 -07003446AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07003447 :
3448#ifdef AUDIO_POLICY_TEST
3449 Thread(false),
3450#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07003451 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07003452 mA2dpSuspended(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003453 mAudioPortGeneration(1),
3454 mBeaconMuteRefCount(0),
3455 mBeaconPlayingRefCount(0),
Eric Laurent9459fb02015-08-12 18:36:32 -07003456 mBeaconMuted(false),
Andy Hung2ddee192015-12-18 17:34:44 -08003457 mTtsOutputAvailable(false),
Eric Laurent36829f92017-04-07 19:04:42 -07003458 mMasterMono(false),
3459 mMusicEffectOutput(AUDIO_IO_HANDLE_NONE)
Eric Laurente552edb2014-03-10 17:42:56 -07003460{
François Gaffied1ab2bd2015-12-02 18:20:06 +01003461 mUidCached = getuid();
3462 mpClientInterface = clientInterface;
3463
3464 // TODO: remove when legacy conf file is removed. true on devices that use DRC on the
3465 // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly.
3466 // Note: remove also speaker_drc_enabled from global configuration of XML config file.
3467 bool speakerDrcEnabled = false;
3468
3469#ifdef USE_XML_AUDIO_POLICY_CONF
3470 mVolumeCurves = new VolumeCurvesCollection();
3471 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3472 mDefaultOutputDevice, speakerDrcEnabled,
3473 static_cast<VolumeCurvesCollection *>(mVolumeCurves));
Jaekyun Seok0d4a6af2017-02-17 17:10:17 +09003474 if (deserializeAudioPolicyXmlConfig(config) != NO_ERROR) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01003475#else
3476 mVolumeCurves = new StreamDescriptorCollection();
3477 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3478 mDefaultOutputDevice, speakerDrcEnabled);
3479 if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) &&
3480 (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) {
3481#endif
3482 ALOGE("could not load audio policy configuration file, setting defaults");
3483 config.setDefault();
3484 }
3485 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
3486 mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled);
3487
3488 // Once policy config has been parsed, retrieve an instance of the engine and initialize it.
François Gaffie2110e042015-03-24 08:41:51 +01003489 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
3490 if (!engineInstance) {
3491 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
3492 return;
3493 }
3494 // Retrieve the Policy Manager Interface
3495 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
3496 if (mEngine == NULL) {
3497 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
3498 return;
3499 }
3500 mEngine->setObserver(this);
3501 status_t status = mEngine->initCheck();
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07003502 (void) status;
François Gaffie2110e042015-03-24 08:41:51 +01003503 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
3504
Eric Laurent3a4311c2014-03-17 12:00:47 -07003505 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07003506 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07003507 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
3508 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07003509 for (size_t i = 0; i < mHwModules.size(); i++) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003510 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003511 if (mHwModules[i]->mHandle == 0) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003512 ALOGW("could not open HW module %s", mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003513 continue;
3514 }
3515 // open all output streams needed to access attached devices
3516 // except for direct output streams that are only opened when they are actually
3517 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07003518 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07003519 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3520 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003521 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003522
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003523 if (!outProfile->hasSupportedDevices()) {
3524 ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003525 continue;
3526 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003527 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) {
Eric Laurent9459fb02015-08-12 18:36:32 -07003528 mTtsOutputAvailable = true;
3529 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003530
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003531 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
Eric Laurentd78f1532014-09-16 16:38:20 -07003532 continue;
3533 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003534 audio_devices_t profileType = outProfile->getSupportedDevicesType();
François Gaffie53615e22015-03-19 09:24:12 +01003535 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
3536 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07003537 } else {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003538 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003539 // outputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003540 profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes);
Eric Laurente552edb2014-03-10 17:42:56 -07003541 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003542 if ((profileType & outputDeviceTypes) == 0) {
3543 continue;
3544 }
Eric Laurentc75307b2015-03-17 15:29:32 -07003545 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
3546 mpClientInterface);
Eric Laurentc40d9692016-04-13 19:14:13 -07003547 const DeviceVector &supportedDevices = outProfile->getSupportedDevices();
3548 const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType);
3549 String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress
3550 : String8("");
Eric Laurentd78f1532014-09-16 16:38:20 -07003551
3552 outputDesc->mDevice = profileType;
3553 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3554 config.sample_rate = outputDesc->mSamplingRate;
3555 config.channel_mask = outputDesc->mChannelMask;
3556 config.format = outputDesc->mFormat;
3557 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003558 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003559 &output,
3560 &config,
3561 &outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003562 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003563 &outputDesc->mLatency,
3564 outputDesc->mFlags);
3565
3566 if (status != NO_ERROR) {
3567 ALOGW("Cannot open output stream for device %08x on hw module %s",
3568 outputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003569 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003570 } else {
3571 outputDesc->mSamplingRate = config.sample_rate;
3572 outputDesc->mChannelMask = config.channel_mask;
3573 outputDesc->mFormat = config.format;
3574
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003575 for (size_t k = 0; k < supportedDevices.size(); k++) {
3576 ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003577 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08003578 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
3579 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003580 }
3581 }
3582 if (mPrimaryOutput == 0 &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003583 outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003584 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07003585 }
3586 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003587 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07003588 outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003589 true,
3590 0,
3591 NULL,
3592 address.string());
Eric Laurentd78f1532014-09-16 16:38:20 -07003593 }
Eric Laurente552edb2014-03-10 17:42:56 -07003594 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003595 // open input streams needed to access attached devices to validate
3596 // mAvailableInputDevices list
3597 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
3598 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003599 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003600
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003601 if (!inProfile->hasSupportedDevices()) {
3602 ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003603 continue;
3604 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003605 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003606 // inputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003607 audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes);
3608
Eric Laurentd78f1532014-09-16 16:38:20 -07003609 if ((profileType & inputDeviceTypes) == 0) {
3610 continue;
3611 }
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08003612 sp<AudioInputDescriptor> inputDesc =
3613 new AudioInputDescriptor(inProfile);
Eric Laurentd78f1532014-09-16 16:38:20 -07003614
Eric Laurentd78f1532014-09-16 16:38:20 -07003615 inputDesc->mDevice = profileType;
3616
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003617 // find the address
3618 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
3619 // the inputs vector must be of size 1, but we don't want to crash here
3620 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
3621 : String8("");
3622 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
3623 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
3624
Eric Laurentd78f1532014-09-16 16:38:20 -07003625 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3626 config.sample_rate = inputDesc->mSamplingRate;
3627 config.channel_mask = inputDesc->mChannelMask;
3628 config.format = inputDesc->mFormat;
3629 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003630 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003631 &input,
3632 &config,
3633 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003634 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003635 AUDIO_SOURCE_MIC,
3636 AUDIO_INPUT_FLAG_NONE);
3637
3638 if (status == NO_ERROR) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003639 const DeviceVector &supportedDevices = inProfile->getSupportedDevices();
3640 for (size_t k = 0; k < supportedDevices.size(); k++) {
3641 ssize_t index = mAvailableInputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003642 // give a valid ID to an attached device once confirmed it is reachable
Eric Laurent45aabc32015-08-06 09:11:13 -07003643 if (index >= 0) {
3644 sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index];
3645 if (!devDesc->isAttached()) {
3646 devDesc->attach(mHwModules[i]);
3647 devDesc->importAudioPort(inProfile);
3648 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003649 }
3650 }
3651 mpClientInterface->closeInput(input);
3652 } else {
3653 ALOGW("Cannot open input stream for device %08x on hw module %s",
3654 inputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003655 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003656 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003657 }
3658 }
3659 // make sure all attached devices have been allocated a unique ID
3660 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003661 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003662 ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003663 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
3664 continue;
3665 }
François Gaffie2110e042015-03-24 08:41:51 +01003666 // The device is now validated and can be appended to the available devices of the engine
3667 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
3668 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003669 i++;
3670 }
3671 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003672 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01003673 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003674 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
3675 continue;
3676 }
François Gaffie2110e042015-03-24 08:41:51 +01003677 // The device is now validated and can be appended to the available devices of the engine
3678 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
3679 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003680 i++;
3681 }
3682 // make sure default device is reachable
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003683 if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01003684 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003685 }
Eric Laurente552edb2014-03-10 17:42:56 -07003686
3687 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
3688
3689 updateDevicesAndOutputs();
3690
3691#ifdef AUDIO_POLICY_TEST
3692 if (mPrimaryOutput != 0) {
3693 AudioParameter outputCmd = AudioParameter();
3694 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003695 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07003696
3697 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
3698 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07003699 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
3700 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003701 mTestLatencyMs = 0;
3702 mCurOutput = 0;
3703 mDirectOutput = false;
3704 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3705 mTestOutputs[i] = 0;
3706 }
3707
3708 const size_t SIZE = 256;
3709 char buffer[SIZE];
3710 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
3711 run(buffer, ANDROID_PRIORITY_AUDIO);
3712 }
3713#endif //AUDIO_POLICY_TEST
3714}
3715
Eric Laurente0720872014-03-11 09:30:41 -07003716AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07003717{
3718#ifdef AUDIO_POLICY_TEST
3719 exit();
3720#endif //AUDIO_POLICY_TEST
3721 for (size_t i = 0; i < mOutputs.size(); i++) {
3722 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003723 }
3724 for (size_t i = 0; i < mInputs.size(); i++) {
3725 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003726 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003727 mAvailableOutputDevices.clear();
3728 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003729 mOutputs.clear();
3730 mInputs.clear();
3731 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07003732}
3733
Eric Laurente0720872014-03-11 09:30:41 -07003734status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07003735{
Eric Laurent87ffa392015-05-22 10:32:38 -07003736 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003737}
3738
3739#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07003740bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07003741{
3742 ALOGV("entering threadLoop()");
3743 while (!exitPending())
3744 {
3745 String8 command;
3746 int valueInt;
3747 String8 value;
3748
3749 Mutex::Autolock _l(mLock);
3750 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
3751
3752 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
3753 AudioParameter param = AudioParameter(command);
3754
3755 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
3756 valueInt != 0) {
3757 ALOGV("Test command %s received", command.string());
3758 String8 target;
3759 if (param.get(String8("target"), target) != NO_ERROR) {
3760 target = "Manager";
3761 }
3762 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
3763 param.remove(String8("test_cmd_policy_output"));
3764 mCurOutput = valueInt;
3765 }
3766 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
3767 param.remove(String8("test_cmd_policy_direct"));
3768 if (value == "false") {
3769 mDirectOutput = false;
3770 } else if (value == "true") {
3771 mDirectOutput = true;
3772 }
3773 }
3774 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
3775 param.remove(String8("test_cmd_policy_input"));
3776 mTestInput = valueInt;
3777 }
3778
3779 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
3780 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07003781 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07003782 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003783 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003784 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003785 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003786 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003787 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07003788 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003789 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07003790 if (target == "Manager") {
3791 mTestFormat = format;
3792 } else if (mTestOutputs[mCurOutput] != 0) {
3793 AudioParameter outputParam = AudioParameter();
Mikhail Naganov388360c2016-10-17 17:09:41 -07003794 outputParam.addInt(String8(AudioParameter::keyStreamSupportedFormats), format);
Eric Laurente552edb2014-03-10 17:42:56 -07003795 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3796 }
3797 }
3798 }
3799 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
3800 param.remove(String8("test_cmd_policy_channels"));
3801 int channels = 0;
3802
3803 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003804 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003805 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003806 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07003807 }
3808 if (channels != 0) {
3809 if (target == "Manager") {
3810 mTestChannels = channels;
3811 } else if (mTestOutputs[mCurOutput] != 0) {
3812 AudioParameter outputParam = AudioParameter();
Mikhail Naganov388360c2016-10-17 17:09:41 -07003813 outputParam.addInt(String8(AudioParameter::keyStreamSupportedChannels), channels);
Eric Laurente552edb2014-03-10 17:42:56 -07003814 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3815 }
3816 }
3817 }
3818 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
3819 param.remove(String8("test_cmd_policy_sampleRate"));
3820 if (valueInt >= 0 && valueInt <= 96000) {
3821 int samplingRate = valueInt;
3822 if (target == "Manager") {
3823 mTestSamplingRate = samplingRate;
3824 } else if (mTestOutputs[mCurOutput] != 0) {
3825 AudioParameter outputParam = AudioParameter();
Mikhail Naganov388360c2016-10-17 17:09:41 -07003826 outputParam.addInt(String8(AudioParameter::keyStreamSupportedSamplingRates), samplingRate);
Eric Laurente552edb2014-03-10 17:42:56 -07003827 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3828 }
3829 }
3830 }
3831
3832 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
3833 param.remove(String8("test_cmd_policy_reopen"));
3834
Eric Laurentc75307b2015-03-17 15:29:32 -07003835 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07003836
Eric Laurentc75307b2015-03-17 15:29:32 -07003837 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07003838
Eric Laurentc75307b2015-03-17 15:29:32 -07003839 removeOutput(mPrimaryOutput->mIoHandle);
3840 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
3841 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003842 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003843 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3844 config.sample_rate = outputDesc->mSamplingRate;
3845 config.channel_mask = outputDesc->mChannelMask;
3846 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07003847 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003848 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07003849 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07003850 &config,
3851 &outputDesc->mDevice,
3852 String8(""),
3853 &outputDesc->mLatency,
3854 outputDesc->mFlags);
3855 if (status != NO_ERROR) {
3856 ALOGE("Failed to reopen hardware output stream, "
3857 "samplingRate: %d, format %d, channels %d",
3858 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003859 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003860 outputDesc->mSamplingRate = config.sample_rate;
3861 outputDesc->mChannelMask = config.channel_mask;
3862 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07003863 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07003864 AudioParameter outputCmd = AudioParameter();
3865 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003866 mpClientInterface->setParameters(handle, outputCmd.toString());
3867 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07003868 }
3869 }
3870
3871
3872 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3873 }
3874 }
3875 return false;
3876}
3877
Eric Laurente0720872014-03-11 09:30:41 -07003878void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003879{
3880 {
3881 AutoMutex _l(mLock);
3882 requestExit();
3883 mWaitWorkCV.signal();
3884 }
3885 requestExitAndWait();
3886}
3887
Eric Laurente0720872014-03-11 09:30:41 -07003888int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003889{
3890 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3891 if (output == mTestOutputs[i]) return i;
3892 }
3893 return 0;
3894}
3895#endif //AUDIO_POLICY_TEST
3896
3897// ---
3898
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003899void AudioPolicyManager::addOutput(audio_io_handle_t output, const sp<SwAudioOutputDescriptor>& outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003900{
François Gaffie98cc1912015-03-18 17:52:40 +01003901 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003902 mOutputs.add(output, outputDesc);
Andy Hung2ddee192015-12-18 17:34:44 -08003903 updateMono(output); // update mono status when adding to output list
Eric Laurent36829f92017-04-07 19:04:42 -07003904 selectOutputForMusicEffects();
Eric Laurent6a94d692014-05-20 11:18:06 -07003905 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003906}
3907
François Gaffie53615e22015-03-19 09:24:12 +01003908void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3909{
3910 mOutputs.removeItem(output);
Eric Laurent36829f92017-04-07 19:04:42 -07003911 selectOutputForMusicEffects();
François Gaffie53615e22015-03-19 09:24:12 +01003912}
3913
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003914void AudioPolicyManager::addInput(audio_io_handle_t input, const sp<AudioInputDescriptor>& inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003915{
François Gaffie98cc1912015-03-18 17:52:40 +01003916 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003917 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003918 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003919}
Eric Laurente552edb2014-03-10 17:42:56 -07003920
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003921void AudioPolicyManager::findIoHandlesByAddress(const sp<SwAudioOutputDescriptor>& desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003922 const audio_devices_t device /*in*/,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003923 const String8& address /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003924 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003925 sp<DeviceDescriptor> devDesc =
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003926 desc->mProfile->getSupportedDeviceByAddress(device, address);
keunyoung3190e672014-12-30 13:00:52 -08003927 if (devDesc != 0) {
3928 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3929 desc->mIoHandle, address.string());
3930 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003931 }
3932}
3933
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003934status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003935 audio_policy_dev_state_t state,
3936 SortedVector<audio_io_handle_t>& outputs,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003937 const String8& address)
Eric Laurente552edb2014-03-10 17:42:56 -07003938{
François Gaffie53615e22015-03-19 09:24:12 +01003939 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003940 sp<SwAudioOutputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003941
3942 if (audio_device_is_digital(device)) {
3943 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003944 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003945 }
Eric Laurente552edb2014-03-10 17:42:56 -07003946
Eric Laurent3b73df72014-03-11 09:06:29 -07003947 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003948 // first list already open outputs that can be routed to this device
3949 for (size_t i = 0; i < mOutputs.size(); i++) {
3950 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003951 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003952 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003953 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3954 outputs.add(mOutputs.keyAt(i));
3955 } else {
3956 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003957 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003958 }
Eric Laurente552edb2014-03-10 17:42:56 -07003959 }
3960 }
3961 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003962 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003963 for (size_t i = 0; i < mHwModules.size(); i++)
3964 {
3965 if (mHwModules[i]->mHandle == 0) {
3966 continue;
3967 }
3968 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3969 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003970 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003971 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003972 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003973 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003974 profiles.add(profile);
3975 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3976 }
Eric Laurente552edb2014-03-10 17:42:56 -07003977 }
3978 }
3979 }
3980
Eric Laurent7b279bb2015-12-14 10:18:23 -08003981 ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size());
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003982
Eric Laurente552edb2014-03-10 17:42:56 -07003983 if (profiles.isEmpty() && outputs.isEmpty()) {
3984 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3985 return BAD_VALUE;
3986 }
3987
3988 // open outputs for matching profiles if needed. Direct outputs are also opened to
3989 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3990 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003991 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003992
3993 // nothing to do if one output is already opened for this profile
3994 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003995 for (j = 0; j < outputs.size(); j++) {
3996 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003997 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003998 // matching profile: save the sample rates, format and channel masks supported
3999 // by the profile in our device descriptor
Paul McLean9080a4c2015-06-18 08:24:02 -07004000 if (audio_device_is_digital(device)) {
4001 devDesc->importAudioPort(profile);
4002 }
Eric Laurente552edb2014-03-10 17:42:56 -07004003 break;
4004 }
4005 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004006 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07004007 continue;
4008 }
4009
Eric Laurent83b88082014-06-20 18:31:16 -07004010 ALOGV("opening output for device %08x with params %s profile %p",
4011 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07004012 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07004013 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07004014 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
4015 config.sample_rate = desc->mSamplingRate;
4016 config.channel_mask = desc->mChannelMask;
4017 config.format = desc->mFormat;
4018 config.offload_info.sample_rate = desc->mSamplingRate;
4019 config.offload_info.channel_mask = desc->mChannelMask;
4020 config.offload_info.format = desc->mFormat;
4021 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07004022 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07004023 &output,
4024 &config,
4025 &desc->mDevice,
4026 address,
4027 &desc->mLatency,
4028 desc->mFlags);
4029 if (status == NO_ERROR) {
4030 desc->mSamplingRate = config.sample_rate;
4031 desc->mChannelMask = config.channel_mask;
4032 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07004033
Eric Laurentd4692962014-05-05 18:13:44 -07004034 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07004035 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07004036 char *param = audio_device_address_to_parameter(device, address);
4037 mpClientInterface->setParameters(output, String8(param));
4038 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07004039 }
Phil Burk00eeb322016-03-31 12:41:00 -07004040 updateAudioProfiles(device, output, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01004041 if (!profile->hasValidAudioProfile()) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004042 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07004043 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004044 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01004045 } else if (profile->hasDynamicAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07004046 mpClientInterface->closeOutput(output);
Phil Burk702b1052016-03-02 16:38:26 -08004047 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01004048 profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004049 config.offload_info.sample_rate = config.sample_rate;
4050 config.offload_info.channel_mask = config.channel_mask;
4051 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07004052 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07004053 &output,
4054 &config,
4055 &desc->mDevice,
4056 address,
4057 &desc->mLatency,
4058 desc->mFlags);
4059 if (status == NO_ERROR) {
4060 desc->mSamplingRate = config.sample_rate;
4061 desc->mChannelMask = config.channel_mask;
4062 desc->mFormat = config.format;
4063 } else {
4064 output = AUDIO_IO_HANDLE_NONE;
4065 }
Eric Laurentd4692962014-05-05 18:13:44 -07004066 }
4067
Eric Laurentcf2c0212014-07-25 16:20:43 -07004068 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07004069 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01004070 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01004071 sp<AudioPolicyMix> policyMix;
4072 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004073 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
4074 address.string());
4075 }
François Gaffie036e1e92015-03-19 10:16:24 +01004076 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07004077 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01004078
Eric Laurent87ffa392015-05-22 10:32:38 -07004079 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
4080 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08004081 // no duplicated output for direct outputs and
4082 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07004083 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07004084
Eric Laurentd4692962014-05-05 18:13:44 -07004085 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07004086 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07004087
Eric Laurentd4692962014-05-05 18:13:44 -07004088 //TODO: configure audio effect output stage here
4089
4090 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07004091 duplicatedOutput =
4092 mpClientInterface->openDuplicateOutput(output,
4093 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004094 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07004095 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07004096 sp<SwAudioOutputDescriptor> dupOutputDesc =
4097 new SwAudioOutputDescriptor(NULL, mpClientInterface);
4098 dupOutputDesc->mOutput1 = mPrimaryOutput;
4099 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07004100 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
4101 dupOutputDesc->mFormat = desc->mFormat;
4102 dupOutputDesc->mChannelMask = desc->mChannelMask;
4103 dupOutputDesc->mLatency = desc->mLatency;
4104 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07004105 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07004106 } else {
4107 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07004108 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07004109 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01004110 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07004111 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07004112 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07004113 }
Eric Laurente552edb2014-03-10 17:42:56 -07004114 }
4115 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07004116 } else {
4117 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07004118 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07004119 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07004120 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004121 profiles.removeAt(profile_index);
4122 profile_index--;
4123 } else {
4124 outputs.add(output);
Paul McLean9080a4c2015-06-18 08:24:02 -07004125 // Load digital format info only for digital devices
4126 if (audio_device_is_digital(device)) {
4127 devDesc->importAudioPort(profile);
4128 }
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07004129
François Gaffie53615e22015-03-19 09:24:12 +01004130 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004131 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
4132 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07004133 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004134 NULL/*patch handle*/, address.string());
4135 }
Eric Laurente552edb2014-03-10 17:42:56 -07004136 ALOGV("checkOutputsForDevice(): adding output %d", output);
4137 }
4138 }
4139
4140 if (profiles.isEmpty()) {
4141 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
4142 return BAD_VALUE;
4143 }
Eric Laurentd4692962014-05-05 18:13:44 -07004144 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07004145 // check if one opened output is not needed any more after disconnecting one device
4146 for (size_t i = 0; i < mOutputs.size(); i++) {
4147 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004148 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004149 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01004150 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07004151 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08004152 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07004153 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004154 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
4155 mOutputs.keyAt(i));
4156 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004157 }
Eric Laurente552edb2014-03-10 17:42:56 -07004158 }
4159 }
Eric Laurentd4692962014-05-05 18:13:44 -07004160 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07004161 for (size_t i = 0; i < mHwModules.size(); i++)
4162 {
4163 if (mHwModules[i]->mHandle == 0) {
4164 continue;
4165 }
4166 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
4167 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004168 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004169 if (profile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07004170 ALOGV("checkOutputsForDevice(): "
4171 "clearing direct output profile %zu on module %zu", j, i);
François Gaffie112b0af2015-11-19 16:13:25 +01004172 profile->clearAudioProfiles();
Eric Laurente552edb2014-03-10 17:42:56 -07004173 }
4174 }
4175 }
4176 }
4177 return NO_ERROR;
4178}
4179
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004180status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01004181 audio_policy_dev_state_t state,
4182 SortedVector<audio_io_handle_t>& inputs,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004183 const String8& address)
Eric Laurentd4692962014-05-05 18:13:44 -07004184{
Paul McLean9080a4c2015-06-18 08:24:02 -07004185 audio_devices_t device = devDesc->type();
Eric Laurent1f2f2232014-06-02 12:01:23 -07004186 sp<AudioInputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07004187
4188 if (audio_device_is_digital(device)) {
4189 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08004190 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07004191 }
4192
Eric Laurentd4692962014-05-05 18:13:44 -07004193 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
4194 // first list already open inputs that can be routed to this device
4195 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
4196 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004197 if (desc->mProfile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07004198 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
4199 inputs.add(mInputs.keyAt(input_index));
4200 }
4201 }
4202
4203 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07004204 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07004205 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
4206 {
4207 if (mHwModules[module_idx]->mHandle == 0) {
4208 continue;
4209 }
4210 for (size_t profile_index = 0;
4211 profile_index < mHwModules[module_idx]->mInputProfiles.size();
4212 profile_index++)
4213 {
Eric Laurent275e8e92014-11-30 15:14:47 -08004214 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
4215
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004216 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01004217 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004218 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004219 profiles.add(profile);
4220 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
4221 profile_index, module_idx);
4222 }
Eric Laurentd4692962014-05-05 18:13:44 -07004223 }
4224 }
4225 }
4226
4227 if (profiles.isEmpty() && inputs.isEmpty()) {
4228 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
4229 return BAD_VALUE;
4230 }
4231
4232 // open inputs for matching profiles if needed. Direct inputs are also opened to
4233 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
4234 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
4235
Eric Laurent1c333e22014-05-20 10:48:17 -07004236 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07004237 // nothing to do if one input is already opened for this profile
4238 size_t input_index;
4239 for (input_index = 0; input_index < mInputs.size(); input_index++) {
4240 desc = mInputs.valueAt(input_index);
4241 if (desc->mProfile == profile) {
Paul McLean9080a4c2015-06-18 08:24:02 -07004242 if (audio_device_is_digital(device)) {
4243 devDesc->importAudioPort(profile);
4244 }
Eric Laurentd4692962014-05-05 18:13:44 -07004245 break;
4246 }
4247 }
4248 if (input_index != mInputs.size()) {
4249 continue;
4250 }
4251
4252 ALOGV("opening input for device 0x%X with params %s", device, address.string());
4253 desc = new AudioInputDescriptor(profile);
4254 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07004255 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
4256 config.sample_rate = desc->mSamplingRate;
4257 config.channel_mask = desc->mChannelMask;
4258 config.format = desc->mFormat;
4259 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07004260 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07004261 &input,
4262 &config,
4263 &desc->mDevice,
4264 address,
4265 AUDIO_SOURCE_MIC,
4266 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07004267
Eric Laurentcf2c0212014-07-25 16:20:43 -07004268 if (status == NO_ERROR) {
4269 desc->mSamplingRate = config.sample_rate;
4270 desc->mChannelMask = config.channel_mask;
4271 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07004272
Eric Laurentd4692962014-05-05 18:13:44 -07004273 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07004274 char *param = audio_device_address_to_parameter(device, address);
4275 mpClientInterface->setParameters(input, String8(param));
4276 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07004277 }
Phil Burk00eeb322016-03-31 12:41:00 -07004278 updateAudioProfiles(device, input, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01004279 if (!profile->hasValidAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07004280 ALOGW("checkInputsForDevice() direct input missing param");
4281 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004282 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07004283 }
4284
4285 if (input != 0) {
4286 addInput(input, desc);
4287 }
4288 } // endif input != 0
4289
Eric Laurentcf2c0212014-07-25 16:20:43 -07004290 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07004291 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07004292 profiles.removeAt(profile_index);
4293 profile_index--;
4294 } else {
4295 inputs.add(input);
Paul McLean9080a4c2015-06-18 08:24:02 -07004296 if (audio_device_is_digital(device)) {
4297 devDesc->importAudioPort(profile);
4298 }
Eric Laurentd4692962014-05-05 18:13:44 -07004299 ALOGV("checkInputsForDevice(): adding input %d", input);
4300 }
4301 } // end scan profiles
4302
4303 if (profiles.isEmpty()) {
4304 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
4305 return BAD_VALUE;
4306 }
4307 } else {
4308 // Disconnect
4309 // check if one opened input is not needed any more after disconnecting one device
4310 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
4311 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004312 if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) {
Eric Laurentd4692962014-05-05 18:13:44 -07004313 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
4314 mInputs.keyAt(input_index));
4315 inputs.add(mInputs.keyAt(input_index));
4316 }
4317 }
4318 // Clear any profiles associated with the disconnected device.
4319 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
4320 if (mHwModules[module_index]->mHandle == 0) {
4321 continue;
4322 }
4323 for (size_t profile_index = 0;
4324 profile_index < mHwModules[module_index]->mInputProfiles.size();
4325 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004326 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004327 if (profile->supportDevice(device)) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07004328 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07004329 profile_index, module_index);
François Gaffie112b0af2015-11-19 16:13:25 +01004330 profile->clearAudioProfiles();
Eric Laurentd4692962014-05-05 18:13:44 -07004331 }
4332 }
4333 }
4334 } // end disconnect
4335
4336 return NO_ERROR;
4337}
4338
4339
Eric Laurente0720872014-03-11 09:30:41 -07004340void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07004341{
4342 ALOGV("closeOutput(%d)", output);
4343
Eric Laurentc75307b2015-03-17 15:29:32 -07004344 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004345 if (outputDesc == NULL) {
4346 ALOGW("closeOutput() unknown output %d", output);
4347 return;
4348 }
François Gaffie036e1e92015-03-19 10:16:24 +01004349 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08004350
Eric Laurente552edb2014-03-10 17:42:56 -07004351 // look for duplicated outputs connected to the output being removed.
4352 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004353 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07004354 if (dupOutputDesc->isDuplicated() &&
4355 (dupOutputDesc->mOutput1 == outputDesc ||
4356 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004357 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07004358 if (dupOutputDesc->mOutput1 == outputDesc) {
4359 outputDesc2 = dupOutputDesc->mOutput2;
4360 } else {
4361 outputDesc2 = dupOutputDesc->mOutput1;
4362 }
4363 // As all active tracks on duplicated output will be deleted,
4364 // and as they were also referenced on the other output, the reference
4365 // count for their stream type must be adjusted accordingly on
4366 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07004367 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07004368 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07004369 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07004370 }
4371 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
4372 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
4373
4374 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01004375 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07004376 }
4377 }
4378
Eric Laurent05b90f82014-08-27 15:32:29 -07004379 nextAudioPortGeneration();
4380
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004381 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004382 if (index >= 0) {
4383 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07004384 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07004385 mAudioPatches.removeItemsAt(index);
4386 mpClientInterface->onAudioPatchListUpdate();
4387 }
4388
Eric Laurente552edb2014-03-10 17:42:56 -07004389 AudioParameter param;
4390 param.add(String8("closing"), String8("true"));
4391 mpClientInterface->setParameters(output, param.toString());
4392
4393 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01004394 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004395 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07004396}
4397
4398void AudioPolicyManager::closeInput(audio_io_handle_t input)
4399{
4400 ALOGV("closeInput(%d)", input);
4401
4402 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
4403 if (inputDesc == NULL) {
4404 ALOGW("closeInput() unknown input %d", input);
4405 return;
4406 }
4407
Eric Laurent6a94d692014-05-20 11:18:06 -07004408 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07004409
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004410 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004411 if (index >= 0) {
4412 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07004413 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07004414 mAudioPatches.removeItemsAt(index);
4415 mpClientInterface->onAudioPatchListUpdate();
4416 }
4417
4418 mpClientInterface->closeInput(input);
4419 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07004420}
4421
Eric Laurentc75307b2015-03-17 15:29:32 -07004422SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
4423 audio_devices_t device,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004424 const SwAudioOutputCollection& openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07004425{
4426 SortedVector<audio_io_handle_t> outputs;
4427
4428 ALOGVV("getOutputsForDevice() device %04x", device);
4429 for (size_t i = 0; i < openOutputs.size(); i++) {
Eric Laurent37ddbb42016-08-10 16:19:14 -07004430 ALOGVV("output %zu isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004431 i, openOutputs.valueAt(i)->isDuplicated(),
4432 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004433 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
4434 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
4435 outputs.add(openOutputs.keyAt(i));
4436 }
4437 }
4438 return outputs;
4439}
4440
Eric Laurente0720872014-03-11 09:30:41 -07004441bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01004442 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07004443{
4444 if (outputs1.size() != outputs2.size()) {
4445 return false;
4446 }
4447 for (size_t i = 0; i < outputs1.size(); i++) {
4448 if (outputs1[i] != outputs2[i]) {
4449 return false;
4450 }
4451 }
4452 return true;
4453}
4454
Eric Laurente0720872014-03-11 09:30:41 -07004455void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07004456{
4457 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
4458 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
4459 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
4460 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
4461
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004462 // also take into account external policy-related changes: add all outputs which are
4463 // associated with policies in the "before" and "after" output vectors
4464 ALOGVV("checkOutputForStrategy(): policy related outputs");
4465 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004466 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004467 if (desc != 0 && desc->mPolicyMix != NULL) {
4468 srcOutputs.add(desc->mIoHandle);
4469 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
4470 }
4471 }
4472 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004473 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004474 if (desc != 0 && desc->mPolicyMix != NULL) {
4475 dstOutputs.add(desc->mIoHandle);
4476 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
4477 }
4478 }
4479
Eric Laurente552edb2014-03-10 17:42:56 -07004480 if (!vectorsEqual(srcOutputs,dstOutputs)) {
4481 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
4482 strategy, srcOutputs[0], dstOutputs[0]);
4483 // mute strategy while moving tracks from one output to another
4484 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004485 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01004486 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004487 setStrategyMute(strategy, true, desc);
4488 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004489 }
Eric Laurentd60560a2015-04-10 11:31:20 -07004490 sp<AudioSourceDescriptor> source =
4491 getSourceForStrategyOnOutput(srcOutputs[i], strategy);
4492 if (source != 0){
4493 connectAudioSource(source);
4494 }
Eric Laurente552edb2014-03-10 17:42:56 -07004495 }
4496
4497 // Move effects associated to this strategy from previous output to new output
4498 if (strategy == STRATEGY_MEDIA) {
Eric Laurent36829f92017-04-07 19:04:42 -07004499 selectOutputForMusicEffects();
Eric Laurente552edb2014-03-10 17:42:56 -07004500 }
4501 // Move tracks associated to this strategy from previous output to new output
Eric Laurent794fde22016-03-11 09:50:45 -08004502 for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004503 if (getStrategy((audio_stream_type_t)i) == strategy) {
4504 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07004505 }
4506 }
4507 }
4508}
4509
Eric Laurente0720872014-03-11 09:30:41 -07004510void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07004511{
François Gaffie2110e042015-03-24 08:41:51 +01004512 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004513 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004514 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01004515 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004516 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004517 checkOutputForStrategy(STRATEGY_SONIFICATION);
4518 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004519 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07004520 checkOutputForStrategy(STRATEGY_MEDIA);
4521 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004522 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07004523}
4524
Eric Laurente0720872014-03-11 09:30:41 -07004525void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07004526{
François Gaffie53615e22015-03-19 09:24:12 +01004527 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07004528 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004529 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07004530 return;
4531 }
4532
Eric Laurent3a4311c2014-03-17 12:00:47 -07004533 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08004534 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
4535 ~AUDIO_DEVICE_BIT_IN) != 0) ||
4536 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurentf732e072016-08-03 19:30:28 -07004537
4538 // if suspended, restore A2DP output if:
4539 // ((SCO device is NOT connected) ||
4540 // ((forced usage communication is NOT SCO) && (forced usage for record is NOT SCO) &&
4541 // (phone state is NOT in call) && (phone state is NOT ringing)))
Eric Laurente552edb2014-03-10 17:42:56 -07004542 //
Eric Laurentf732e072016-08-03 19:30:28 -07004543 // if not suspended, suspend A2DP output if:
4544 // (SCO device is connected) &&
4545 // ((forced usage for communication is SCO) || (forced usage for record is SCO) ||
4546 // ((phone state is in call) || (phone state is ringing)))
Eric Laurente552edb2014-03-10 17:42:56 -07004547 //
4548 if (mA2dpSuspended) {
Eric Laurentf732e072016-08-03 19:30:28 -07004549 if (!isScoConnected ||
4550 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) !=
4551 AUDIO_POLICY_FORCE_BT_SCO) &&
4552 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) !=
4553 AUDIO_POLICY_FORCE_BT_SCO) &&
4554 (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
François Gaffie2110e042015-03-24 08:41:51 +01004555 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004556
4557 mpClientInterface->restoreOutput(a2dpOutput);
4558 mA2dpSuspended = false;
4559 }
4560 } else {
Eric Laurentf732e072016-08-03 19:30:28 -07004561 if (isScoConnected &&
4562 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ==
4563 AUDIO_POLICY_FORCE_BT_SCO) ||
4564 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) ==
4565 AUDIO_POLICY_FORCE_BT_SCO) ||
4566 (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
François Gaffie2110e042015-03-24 08:41:51 +01004567 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004568
4569 mpClientInterface->suspendOutput(a2dpOutput);
4570 mA2dpSuspended = true;
4571 }
4572 }
4573}
4574
Eric Laurentc75307b2015-03-17 15:29:32 -07004575audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
4576 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004577{
4578 audio_devices_t device = AUDIO_DEVICE_NONE;
4579
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004580 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004581 if (index >= 0) {
4582 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4583 if (patchDesc->mUid != mUidCached) {
4584 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004585 outputDesc->device(), outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004586 return outputDesc->device();
4587 }
4588 }
4589
Eric Laurente552edb2014-03-10 17:42:56 -07004590 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05004591 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004592 // use device for strategy enforced audible
4593 // 2: we are in call or the strategy phone is active on the output:
4594 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05004595 // 3: the strategy for enforced audible is active but not enforced on the output:
4596 // use the device for strategy enforced audible
Eric Laurent28d09f02016-03-08 10:43:05 -08004597 // 4: the strategy sonification is active on the output:
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004598 // use device for strategy sonification
Eric Laurent28d09f02016-03-08 10:43:05 -08004599 // 5: the strategy accessibility is active on the output:
4600 // use device for strategy accessibility
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004601 // 6: the strategy "respectful" sonification is active on the output:
4602 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08004603 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004604 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08004605 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004606 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08004607 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004608 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01004609 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004610 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07004611 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
4612 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01004613 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004614 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004615 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05004616 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004617 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004618 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Eric Laurent28d09f02016-03-08 10:43:05 -08004619 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
4620 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004621 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004622 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004623 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004624 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004625 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004626 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004627 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004628 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004629 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004630 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07004631 }
4632
Eric Laurent1c333e22014-05-20 10:48:17 -07004633 ALOGV("getNewOutputDevice() selected device %x", device);
4634 return device;
4635}
4636
Eric Laurentfb66dd92016-01-28 18:32:03 -08004637audio_devices_t AudioPolicyManager::getNewInputDevice(const sp<AudioInputDescriptor>& inputDesc)
Eric Laurent1c333e22014-05-20 10:48:17 -07004638{
Eric Laurentfb66dd92016-01-28 18:32:03 -08004639 audio_devices_t device = AUDIO_DEVICE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07004640
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004641 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004642 if (index >= 0) {
4643 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4644 if (patchDesc->mUid != mUidCached) {
4645 ALOGV("getNewInputDevice() device %08x forced by patch %d",
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004646 inputDesc->mDevice, inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004647 return inputDesc->mDevice;
4648 }
4649 }
4650
Eric Laurentfb66dd92016-01-28 18:32:03 -08004651 audio_source_t source = inputDesc->getHighestPrioritySource(true /*activeOnly*/);
4652 if (isInCall()) {
4653 device = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
4654 } else if (source != AUDIO_SOURCE_DEFAULT) {
4655 device = getDeviceAndMixForInputSource(source);
4656 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004657
Eric Laurente552edb2014-03-10 17:42:56 -07004658 return device;
4659}
4660
Eric Laurent794fde22016-03-11 09:50:45 -08004661bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1,
4662 audio_stream_type_t stream2) {
Jean-Michel Trivi99bb2f92016-11-23 15:52:07 -08004663 return (stream1 == stream2);
Eric Laurent28d09f02016-03-08 10:43:05 -08004664}
4665
Eric Laurente0720872014-03-11 09:30:41 -07004666uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004667 return (uint32_t)getStrategy(stream);
4668}
4669
Eric Laurente0720872014-03-11 09:30:41 -07004670audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004671 // By checking the range of stream before calling getStrategy, we avoid
4672 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
4673 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08004674 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004675 return AUDIO_DEVICE_NONE;
4676 }
Eric Laurent28d09f02016-03-08 10:43:05 -08004677 audio_devices_t devices = AUDIO_DEVICE_NONE;
Eric Laurent794fde22016-03-11 09:50:45 -08004678 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
4679 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004680 continue;
Eric Laurent6a94d692014-05-20 11:18:06 -07004681 }
Eric Laurent794fde22016-03-11 09:50:45 -08004682 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08004683 audio_devices_t curDevices =
Eric Laurent447a87b2016-07-07 17:32:10 -07004684 getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/);
Eric Laurent28d09f02016-03-08 10:43:05 -08004685 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs);
4686 for (size_t i = 0; i < outputs.size(); i++) {
4687 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurent794fde22016-03-11 09:50:45 -08004688 if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004689 curDevices |= outputDesc->device();
4690 }
4691 }
4692 devices |= curDevices;
Eric Laurente552edb2014-03-10 17:42:56 -07004693 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05004694
4695 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
4696 and doesn't really need to.*/
4697 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
4698 devices |= AUDIO_DEVICE_OUT_SPEAKER;
4699 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4700 }
Eric Laurente552edb2014-03-10 17:42:56 -07004701 return devices;
4702}
4703
François Gaffie2110e042015-03-24 08:41:51 +01004704routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
4705{
Eric Laurent223fd5c2014-11-11 13:43:36 -08004706 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01004707 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004708}
4709
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004710uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4711 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004712 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4713 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4714 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004715 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4716 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4717 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004718 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01004719 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004720}
4721
Eric Laurente0720872014-03-11 09:30:41 -07004722void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004723 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004724 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004725 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4726 updateDevicesAndOutputs();
4727 break;
4728 default:
4729 break;
4730 }
4731}
4732
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004733uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
Eric Laurent9459fb02015-08-12 18:36:32 -07004734
4735 // skip beacon mute management if a dedicated TTS output is available
4736 if (mTtsOutputAvailable) {
4737 return 0;
4738 }
4739
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004740 switch(event) {
4741 case STARTING_OUTPUT:
4742 mBeaconMuteRefCount++;
4743 break;
4744 case STOPPING_OUTPUT:
4745 if (mBeaconMuteRefCount > 0) {
4746 mBeaconMuteRefCount--;
4747 }
4748 break;
4749 case STARTING_BEACON:
4750 mBeaconPlayingRefCount++;
4751 break;
4752 case STOPPING_BEACON:
4753 if (mBeaconPlayingRefCount > 0) {
4754 mBeaconPlayingRefCount--;
4755 }
4756 break;
4757 }
4758
4759 if (mBeaconMuteRefCount > 0) {
4760 // any playback causes beacon to be muted
4761 return setBeaconMute(true);
4762 } else {
4763 // no other playback: unmute when beacon starts playing, mute when it stops
4764 return setBeaconMute(mBeaconPlayingRefCount == 0);
4765 }
4766}
4767
4768uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4769 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4770 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4771 // keep track of muted state to avoid repeating mute/unmute operations
4772 if (mBeaconMuted != mute) {
4773 // mute/unmute AUDIO_STREAM_TTS on all outputs
4774 ALOGV("\t muting %d", mute);
4775 uint32_t maxLatency = 0;
4776 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004777 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004778 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004779 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004780 0 /*delay*/, AUDIO_DEVICE_NONE);
4781 const uint32_t latency = desc->latency() * 2;
4782 if (latency > maxLatency) {
4783 maxLatency = latency;
4784 }
4785 }
4786 mBeaconMuted = mute;
4787 return maxLatency;
4788 }
4789 return 0;
4790}
4791
Eric Laurente0720872014-03-11 09:30:41 -07004792audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004793 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004794{
Paul McLeanaa981192015-03-21 09:55:15 -07004795 // Routing
4796 // see if we have an explicit route
4797 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4798 // (getStrategy(stream)).
4799 // if the strategy from the stream type in the RouteMap is the same as the argument above,
4800 // and activity count is non-zero
4801 // the device = the device from the descriptor in the RouteMap, and exit.
4802 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4803 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
Eric Laurent28d09f02016-03-08 10:43:05 -08004804 routing_strategy routeStrategy = getStrategy(route->mStreamType);
4805 if ((routeStrategy == strategy) && route->isActive()) {
Paul McLeanaa981192015-03-21 09:55:15 -07004806 return route->mDeviceDescriptor->type();
4807 }
4808 }
4809
Eric Laurente552edb2014-03-10 17:42:56 -07004810 if (fromCache) {
4811 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4812 strategy, mDeviceForStrategy[strategy]);
4813 return mDeviceForStrategy[strategy];
4814 }
François Gaffie2110e042015-03-24 08:41:51 +01004815 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004816}
4817
Eric Laurente0720872014-03-11 09:30:41 -07004818void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004819{
4820 for (int i = 0; i < NUM_STRATEGIES; i++) {
4821 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4822 }
4823 mPreviousOutputs = mOutputs;
4824}
4825
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004826uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004827 audio_devices_t prevDevice,
4828 uint32_t delayMs)
4829{
4830 // mute/unmute strategies using an incompatible device combination
4831 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4832 // if unmuting, unmute only after the specified delay
4833 if (outputDesc->isDuplicated()) {
4834 return 0;
4835 }
4836
4837 uint32_t muteWaitMs = 0;
4838 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004839 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004840
4841 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4842 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004843 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004844 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4845 bool doMute = false;
4846
4847 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4848 doMute = true;
4849 outputDesc->mStrategyMutedByDevice[i] = true;
4850 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4851 doMute = true;
4852 outputDesc->mStrategyMutedByDevice[i] = false;
4853 }
Eric Laurent99401132014-05-07 19:48:15 -07004854 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004855 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004856 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004857 // skip output if it does not share any device with current output
4858 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4859 == AUDIO_DEVICE_NONE) {
4860 continue;
4861 }
Eric Laurent37ddbb42016-08-10 16:19:14 -07004862 ALOGVV("checkDeviceMuteStrategies() %s strategy %zu (curDevice %04x)",
Eric Laurentc75307b2015-03-17 15:29:32 -07004863 mute ? "muting" : "unmuting", i, curDevice);
4864 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004865 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004866 if (mute) {
4867 // FIXME: should not need to double latency if volume could be applied
4868 // immediately by the audioflinger mixer. We must account for the delay
4869 // between now and the next time the audioflinger thread for this output
4870 // will process a buffer (which corresponds to one buffer size,
4871 // usually 1/2 or 1/4 of the latency).
4872 if (muteWaitMs < desc->latency() * 2) {
4873 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004874 }
4875 }
4876 }
4877 }
4878 }
4879 }
4880
Eric Laurent99401132014-05-07 19:48:15 -07004881 // temporary mute output if device selection changes to avoid volume bursts due to
4882 // different per device volumes
4883 if (outputDesc->isActive() && (device != prevDevice)) {
Eric Laurentdc462862016-07-19 12:29:53 -07004884 uint32_t tempMuteWaitMs = outputDesc->latency() * 2;
4885 // temporary mute duration is conservatively set to 4 times the reported latency
4886 uint32_t tempMuteDurationMs = outputDesc->latency() * 4;
4887 if (muteWaitMs < tempMuteWaitMs) {
4888 muteWaitMs = tempMuteWaitMs;
Eric Laurent99401132014-05-07 19:48:15 -07004889 }
Eric Laurentdc462862016-07-19 12:29:53 -07004890
Eric Laurent99401132014-05-07 19:48:15 -07004891 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004892 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentdc462862016-07-19 12:29:53 -07004893 // make sure that we do not start the temporary mute period too early in case of
4894 // delayed device change
4895 setStrategyMute((routing_strategy)i, true, outputDesc, delayMs);
Eric Laurentc75307b2015-03-17 15:29:32 -07004896 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurentdc462862016-07-19 12:29:53 -07004897 delayMs + tempMuteDurationMs, device);
Eric Laurent99401132014-05-07 19:48:15 -07004898 }
4899 }
4900 }
4901
Eric Laurente552edb2014-03-10 17:42:56 -07004902 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4903 if (muteWaitMs > delayMs) {
4904 muteWaitMs -= delayMs;
4905 usleep(muteWaitMs * 1000);
4906 return muteWaitMs;
4907 }
4908 return 0;
4909}
4910
Eric Laurentc75307b2015-03-17 15:29:32 -07004911uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004912 audio_devices_t device,
4913 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004914 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004915 audio_patch_handle_t *patchHandle,
4916 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004917{
Eric Laurentc75307b2015-03-17 15:29:32 -07004918 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004919 AudioParameter param;
4920 uint32_t muteWaitMs;
4921
4922 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004923 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4924 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004925 return muteWaitMs;
4926 }
4927 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4928 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004929 if ((device != AUDIO_DEVICE_NONE) &&
4930 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004931 return 0;
4932 }
4933
4934 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004935 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004936
4937 audio_devices_t prevDevice = outputDesc->mDevice;
4938
Paul McLeanaa981192015-03-21 09:55:15 -07004939 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004940
4941 if (device != AUDIO_DEVICE_NONE) {
4942 outputDesc->mDevice = device;
4943 }
4944 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4945
4946 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004947 // the requested device is AUDIO_DEVICE_NONE
4948 // OR the requested device is the same as current device
4949 // AND force is not specified
4950 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004951 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004952 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4953 !force &&
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004954 outputDesc->getPatchHandle() != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004955 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004956 return muteWaitMs;
4957 }
4958
4959 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004960
Eric Laurente552edb2014-03-10 17:42:56 -07004961 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004962 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004963 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004964 } else {
Eric Laurentc40d9692016-04-13 19:14:13 -07004965 DeviceVector deviceList;
4966 if ((address == NULL) || (strlen(address) == 0)) {
4967 deviceList = mAvailableOutputDevices.getDevicesFromType(device);
4968 } else {
4969 deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
4970 }
4971
Eric Laurent1c333e22014-05-20 10:48:17 -07004972 if (!deviceList.isEmpty()) {
4973 struct audio_patch patch;
4974 outputDesc->toAudioPortConfig(&patch.sources[0]);
4975 patch.num_sources = 1;
4976 patch.num_sinks = 0;
4977 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4978 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004979 patch.num_sinks++;
4980 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004981 ssize_t index;
4982 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4983 index = mAudioPatches.indexOfKey(*patchHandle);
4984 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004985 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004986 }
4987 sp< AudioPatch> patchDesc;
4988 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4989 if (index >= 0) {
4990 patchDesc = mAudioPatches.valueAt(index);
4991 afPatchHandle = patchDesc->mAfPatchHandle;
4992 }
4993
Eric Laurent1c333e22014-05-20 10:48:17 -07004994 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004995 &afPatchHandle,
4996 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004997 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4998 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004999 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07005000 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07005001 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01005002 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07005003 addAudioPatch(patchDesc->mHandle, patchDesc);
5004 } else {
5005 patchDesc->mPatch = patch;
5006 }
5007 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07005008 if (patchHandle) {
5009 *patchHandle = patchDesc->mHandle;
5010 }
Jean-Michel Triviff155c62016-02-26 12:07:16 -08005011 outputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07005012 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07005013 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07005014 }
5015 }
bryant_liuf5e7e792014-08-19 20:07:05 +08005016
5017 // inform all input as well
5018 for (size_t i = 0; i < mInputs.size(); i++) {
5019 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01005020 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08005021 AudioParameter inputCmd = AudioParameter();
5022 ALOGV("%s: inform input %d of device:%d", __func__,
5023 inputDescriptor->mIoHandle, device);
5024 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
5025 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
5026 inputCmd.toString(),
5027 delayMs);
5028 }
5029 }
Eric Laurent1c333e22014-05-20 10:48:17 -07005030 }
Eric Laurente552edb2014-03-10 17:42:56 -07005031
5032 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07005033 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005034
5035 return muteWaitMs;
5036}
5037
Eric Laurentc75307b2015-03-17 15:29:32 -07005038status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07005039 int delayMs,
5040 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07005041{
Eric Laurent6a94d692014-05-20 11:18:06 -07005042 ssize_t index;
5043 if (patchHandle) {
5044 index = mAudioPatches.indexOfKey(*patchHandle);
5045 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08005046 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07005047 }
5048 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07005049 return INVALID_OPERATION;
5050 }
Eric Laurent6a94d692014-05-20 11:18:06 -07005051 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
5052 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07005053 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07005054 outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07005055 removeAudioPatch(patchDesc->mHandle);
5056 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07005057 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07005058 return status;
5059}
5060
5061status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
5062 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07005063 bool force,
5064 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07005065{
5066 status_t status = NO_ERROR;
5067
Eric Laurent1f2f2232014-06-02 12:01:23 -07005068 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07005069 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
5070 inputDesc->mDevice = device;
5071
5072 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
5073 if (!deviceList.isEmpty()) {
5074 struct audio_patch patch;
5075 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07005076 // AUDIO_SOURCE_HOTWORD is for internal use only:
5077 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07005078 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
Eric Laurent599c7582015-12-07 18:05:55 -08005079 !inputDesc->isSoundTrigger()) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07005080 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
5081 }
Eric Laurent1c333e22014-05-20 10:48:17 -07005082 patch.num_sinks = 1;
5083 //only one input device for now
5084 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07005085 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07005086 ssize_t index;
5087 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
5088 index = mAudioPatches.indexOfKey(*patchHandle);
5089 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08005090 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07005091 }
5092 sp< AudioPatch> patchDesc;
5093 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
5094 if (index >= 0) {
5095 patchDesc = mAudioPatches.valueAt(index);
5096 afPatchHandle = patchDesc->mAfPatchHandle;
5097 }
5098
Eric Laurent1c333e22014-05-20 10:48:17 -07005099 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07005100 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07005101 0);
5102 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07005103 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07005104 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07005105 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01005106 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07005107 addAudioPatch(patchDesc->mHandle, patchDesc);
5108 } else {
5109 patchDesc->mPatch = patch;
5110 }
5111 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07005112 if (patchHandle) {
5113 *patchHandle = patchDesc->mHandle;
5114 }
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08005115 inputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07005116 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07005117 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07005118 }
5119 }
5120 }
5121 return status;
5122}
5123
Eric Laurent6a94d692014-05-20 11:18:06 -07005124status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
5125 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07005126{
Eric Laurent1f2f2232014-06-02 12:01:23 -07005127 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07005128 ssize_t index;
5129 if (patchHandle) {
5130 index = mAudioPatches.indexOfKey(*patchHandle);
5131 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08005132 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07005133 }
5134 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07005135 return INVALID_OPERATION;
5136 }
Eric Laurent6a94d692014-05-20 11:18:06 -07005137 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
5138 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07005139 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07005140 inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07005141 removeAudioPatch(patchDesc->mHandle);
5142 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07005143 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07005144 return status;
5145}
5146
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08005147sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07005148 const String8& address,
François Gaffie53615e22015-03-19 09:24:12 +01005149 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07005150 audio_format_t& format,
5151 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01005152 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07005153{
5154 // Choose an input profile based on the requested capture parameters: select the first available
5155 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07005156 //
5157 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
5158 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07005159
5160 for (size_t i = 0; i < mHwModules.size(); i++)
5161 {
5162 if (mHwModules[i]->mHandle == 0) {
5163 continue;
5164 }
5165 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
5166 {
Eric Laurent1c333e22014-05-20 10:48:17 -07005167 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07005168 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08005169 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07005170 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07005171 format,
5172 &format /*updatedFormat*/,
5173 channelMask,
5174 &channelMask /*updatedChannelMask*/,
5175 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08005176
Eric Laurente552edb2014-03-10 17:42:56 -07005177 return profile;
5178 }
5179 }
5180 }
5181 return NULL;
5182}
5183
Eric Laurentc73ca6e2014-12-12 14:34:22 -08005184
5185audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01005186 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07005187{
François Gaffie036e1e92015-03-19 10:16:24 +01005188 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
5189 audio_devices_t selectedDeviceFromMix =
5190 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08005191
François Gaffie036e1e92015-03-19 10:16:24 +01005192 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
5193 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08005194 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08005195 return getDeviceForInputSource(inputSource);
5196}
5197
5198audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
5199{
Paul McLean466dc8e2015-04-17 13:15:36 -06005200 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
5201 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07005202 if (inputSource == route->mSource && route->isActive()) {
Paul McLean466dc8e2015-04-17 13:15:36 -06005203 return route->mDeviceDescriptor->type();
5204 }
5205 }
5206
5207 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07005208}
5209
Eric Laurente0720872014-03-11 09:30:41 -07005210float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005211 int index,
5212 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005213{
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005214 float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index);
Jean-Michel Trivi3d8b4a42016-09-14 18:37:46 -07005215
5216 // handle the case of accessibility active while a ringtone is playing: if the ringtone is much
5217 // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch
5218 // exploration of the dialer UI. In this situation, bring the accessibility volume closer to
5219 // the ringtone volume
5220 if ((stream == AUDIO_STREAM_ACCESSIBILITY)
5221 && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState())
5222 && isStreamActive(AUDIO_STREAM_RING, 0)) {
5223 const float ringVolumeDB = computeVolume(AUDIO_STREAM_RING, index, device);
5224 return ringVolumeDB - 4 > volumeDB ? ringVolumeDB - 4 : volumeDB;
5225 }
5226
Eric Laurente552edb2014-03-10 17:42:56 -07005227 // if a headset is connected, apply the following rules to ring tones and notifications
5228 // to avoid sound level bursts in user's ears:
Eric Laurent6af1c1d2016-04-14 11:20:44 -07005229 // - always attenuate notifications volume by 6dB
5230 // - attenuate ring tones volume by 6dB unless music is not playing and
5231 // speaker is part of the select devices
Eric Laurente552edb2014-03-10 17:42:56 -07005232 // - if music is playing, always limit the volume to current music volume,
5233 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07005234 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005235 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
5236 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
5237 AUDIO_DEVICE_OUT_WIRED_HEADSET |
Eric Laurent904d6322017-03-17 17:20:47 -07005238 AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
5239 AUDIO_DEVICE_OUT_USB_HEADSET)) &&
Eric Laurente552edb2014-03-10 17:42:56 -07005240 ((stream_strategy == STRATEGY_SONIFICATION)
5241 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07005242 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07005243 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01005244 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
François Gaffied1ab2bd2015-12-02 18:20:06 +01005245 mVolumeCurves->canBeMuted(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005246 // when the phone is ringing we must consider that music could have been paused just before
5247 // by the music application and behave as if music was active if the last music track was
5248 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07005249 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07005250 mLimitRingtoneVolume) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005251 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07005252 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07005253 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005254 mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC,
5255 musicDevice),
5256 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07005257 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
5258 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005259 if (volumeDB > minVolDB) {
5260 volumeDB = minVolDB;
Eric Laurentffbc80f2015-03-18 18:30:19 -07005261 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07005262 }
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005263 if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
5264 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) {
5265 // on A2DP, also ensure notification volume is not too low compared to media when
5266 // intended to be played
5267 if ((volumeDB > -96.0f) &&
5268 (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) {
5269 ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f",
5270 stream, device,
5271 volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB);
5272 volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB;
5273 }
5274 }
Eric Laurent6af1c1d2016-04-14 11:20:44 -07005275 } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) ||
5276 stream_strategy != STRATEGY_SONIFICATION) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005277 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07005278 }
5279 }
5280
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005281 return volumeDB;
Eric Laurente552edb2014-03-10 17:42:56 -07005282}
5283
Eric Laurente0720872014-03-11 09:30:41 -07005284status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005285 int index,
5286 const sp<AudioOutputDescriptor>& outputDesc,
5287 audio_devices_t device,
5288 int delayMs,
5289 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005290{
Eric Laurente552edb2014-03-10 17:42:56 -07005291 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07005292 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07005293 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07005294 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07005295 return NO_ERROR;
5296 }
François Gaffie2110e042015-03-24 08:41:51 +01005297 audio_policy_forced_cfg_t forceUseForComm =
5298 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07005299 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01005300 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
5301 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005302 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01005303 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07005304 return INVALID_OPERATION;
5305 }
5306
Eric Laurentc75307b2015-03-17 15:29:32 -07005307 if (device == AUDIO_DEVICE_NONE) {
5308 device = outputDesc->device();
5309 }
Eric Laurent275e8e92014-11-30 15:14:47 -08005310
Eric Laurentffbc80f2015-03-18 18:30:19 -07005311 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07005312 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07005313 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08005314 }
Eric Laurentc75307b2015-03-17 15:29:32 -07005315
Eric Laurentffbc80f2015-03-18 18:30:19 -07005316 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07005317
Eric Laurent3b73df72014-03-11 09:06:29 -07005318 if (stream == AUDIO_STREAM_VOICE_CALL ||
5319 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07005320 float voiceVolume;
5321 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07005322 if (stream == AUDIO_STREAM_VOICE_CALL) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005323 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005324 } else {
5325 voiceVolume = 1.0;
5326 }
5327
Eric Laurent18fba842016-03-31 14:41:26 -07005328 if (voiceVolume != mLastVoiceVolume) {
Eric Laurente552edb2014-03-10 17:42:56 -07005329 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
5330 mLastVoiceVolume = voiceVolume;
5331 }
5332 }
5333
5334 return NO_ERROR;
5335}
5336
Eric Laurentc75307b2015-03-17 15:29:32 -07005337void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
5338 audio_devices_t device,
5339 int delayMs,
5340 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005341{
Eric Laurentc75307b2015-03-17 15:29:32 -07005342 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07005343
Eric Laurent794fde22016-03-11 09:50:45 -08005344 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005345 checkAndSetVolume((audio_stream_type_t)stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005346 mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005347 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005348 device,
5349 delayMs,
5350 force);
5351 }
5352}
5353
Eric Laurente0720872014-03-11 09:30:41 -07005354void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07005355 bool on,
5356 const sp<AudioOutputDescriptor>& outputDesc,
5357 int delayMs,
5358 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005359{
Eric Laurent72249d52015-06-08 11:12:15 -07005360 ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d",
5361 strategy, on, outputDesc->getId());
Eric Laurent794fde22016-03-11 09:50:45 -08005362 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005363 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005364 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07005365 }
5366 }
5367}
5368
Eric Laurente0720872014-03-11 09:30:41 -07005369void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005370 bool on,
5371 const sp<AudioOutputDescriptor>& outputDesc,
5372 int delayMs,
5373 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005374{
Eric Laurente552edb2014-03-10 17:42:56 -07005375 if (device == AUDIO_DEVICE_NONE) {
5376 device = outputDesc->device();
5377 }
5378
Eric Laurentc75307b2015-03-17 15:29:32 -07005379 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
5380 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07005381
5382 if (on) {
5383 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005384 if (mVolumeCurves->canBeMuted(stream) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07005385 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01005386 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005387 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005388 }
5389 }
5390 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
5391 outputDesc->mMuteCount[stream]++;
5392 } else {
5393 if (outputDesc->mMuteCount[stream] == 0) {
5394 ALOGV("setStreamMute() unmuting non muted stream!");
5395 return;
5396 }
5397 if (--outputDesc->mMuteCount[stream] == 0) {
5398 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005399 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005400 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005401 device,
5402 delayMs);
5403 }
5404 }
5405}
5406
Eric Laurente0720872014-03-11 09:30:41 -07005407void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07005408 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07005409{
Eric Laurent87ffa392015-05-22 10:32:38 -07005410 if(!hasPrimaryOutput()) {
5411 return;
5412 }
5413
Eric Laurente552edb2014-03-10 17:42:56 -07005414 // if the stream pertains to sonification strategy and we are in call we must
5415 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
5416 // in the device used for phone strategy and play the tone if the selected device does not
5417 // interfere with the device used for phone strategy
5418 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
5419 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07005420 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005421 if ((stream_strategy == STRATEGY_SONIFICATION) ||
5422 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005423 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07005424 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
5425 stream, starting, outputDesc->mDevice, stateChange);
5426 if (outputDesc->mRefCount[stream]) {
5427 int muteCount = 1;
5428 if (stateChange) {
5429 muteCount = outputDesc->mRefCount[stream];
5430 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005431 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005432 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
5433 for (int i = 0; i < muteCount; i++) {
5434 setStreamMute(stream, starting, mPrimaryOutput);
5435 }
5436 } else {
5437 ALOGV("handleIncallSonification() high visibility");
5438 if (outputDesc->device() &
5439 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
5440 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
5441 for (int i = 0; i < muteCount; i++) {
5442 setStreamMute(stream, starting, mPrimaryOutput);
5443 }
5444 }
5445 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005446 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
5447 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07005448 } else {
5449 mpClientInterface->stopTone();
5450 }
5451 }
5452 }
5453 }
5454}
5455
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005456audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
5457{
5458 // flags to stream type mapping
5459 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
5460 return AUDIO_STREAM_ENFORCED_AUDIBLE;
5461 }
5462 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
5463 return AUDIO_STREAM_BLUETOOTH_SCO;
5464 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08005465 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
5466 return AUDIO_STREAM_TTS;
5467 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005468
5469 // usage to stream type mapping
5470 switch (attr->usage) {
5471 case AUDIO_USAGE_MEDIA:
5472 case AUDIO_USAGE_GAME:
Jean-Michel Trivi36867762016-12-29 12:03:28 -08005473 case AUDIO_USAGE_ASSISTANT:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005474 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5475 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08005476 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5477 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005478 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5479 return AUDIO_STREAM_SYSTEM;
5480 case AUDIO_USAGE_VOICE_COMMUNICATION:
5481 return AUDIO_STREAM_VOICE_CALL;
5482
5483 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5484 return AUDIO_STREAM_DTMF;
5485
5486 case AUDIO_USAGE_ALARM:
5487 return AUDIO_STREAM_ALARM;
5488 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5489 return AUDIO_STREAM_RING;
5490
5491 case AUDIO_USAGE_NOTIFICATION:
5492 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5493 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5494 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5495 case AUDIO_USAGE_NOTIFICATION_EVENT:
5496 return AUDIO_STREAM_NOTIFICATION;
5497
5498 case AUDIO_USAGE_UNKNOWN:
5499 default:
5500 return AUDIO_STREAM_MUSIC;
5501 }
5502}
Eric Laurente83b55d2014-11-14 10:06:21 -08005503
François Gaffie53615e22015-03-19 09:24:12 +01005504bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
5505{
Eric Laurente83b55d2014-11-14 10:06:21 -08005506 // has flags that map to a strategy?
5507 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
5508 return true;
5509 }
5510
5511 // has known usage?
5512 switch (paa->usage) {
5513 case AUDIO_USAGE_UNKNOWN:
5514 case AUDIO_USAGE_MEDIA:
5515 case AUDIO_USAGE_VOICE_COMMUNICATION:
5516 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5517 case AUDIO_USAGE_ALARM:
5518 case AUDIO_USAGE_NOTIFICATION:
5519 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5520 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5521 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5522 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5523 case AUDIO_USAGE_NOTIFICATION_EVENT:
5524 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5525 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5526 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5527 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08005528 case AUDIO_USAGE_VIRTUAL_SOURCE:
Jean-Michel Trivi36867762016-12-29 12:03:28 -08005529 case AUDIO_USAGE_ASSISTANT:
Eric Laurente83b55d2014-11-14 10:06:21 -08005530 break;
5531 default:
5532 return false;
5533 }
5534 return true;
5535}
5536
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07005537bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor>& outputDesc,
François Gaffiead3183e2015-03-18 16:55:35 +01005538 routing_strategy strategy, uint32_t inPastMs,
5539 nsecs_t sysTime) const
5540{
5541 if ((sysTime == 0) && (inPastMs != 0)) {
5542 sysTime = systemTime();
5543 }
Eric Laurent794fde22016-03-11 09:50:45 -08005544 for (int i = 0; i < (int)AUDIO_STREAM_FOR_POLICY_CNT; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01005545 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
5546 (NUM_STRATEGIES == strategy)) &&
5547 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
5548 return true;
5549 }
5550 }
5551 return false;
5552}
5553
François Gaffie2110e042015-03-24 08:41:51 +01005554audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
5555{
5556 return mEngine->getForceUse(usage);
5557}
5558
5559bool AudioPolicyManager::isInCall()
5560{
5561 return isStateInCall(mEngine->getPhoneState());
5562}
5563
5564bool AudioPolicyManager::isStateInCall(int state)
5565{
5566 return is_state_in_call(state);
5567}
5568
Eric Laurentd60560a2015-04-10 11:31:20 -07005569void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc)
5570{
5571 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
5572 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
5573 if (sourceDesc->mDevice->equals(deviceDesc)) {
5574 ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle());
5575 stopAudioSource(sourceDesc->getHandle());
5576 }
5577 }
5578
5579 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
5580 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
5581 bool release = false;
5582 for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) {
5583 const struct audio_port_config *source = &patchDesc->mPatch.sources[j];
5584 if (source->type == AUDIO_PORT_TYPE_DEVICE &&
5585 source->ext.device.type == deviceDesc->type()) {
5586 release = true;
5587 }
5588 }
5589 for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) {
5590 const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j];
5591 if (sink->type == AUDIO_PORT_TYPE_DEVICE &&
5592 sink->ext.device.type == deviceDesc->type()) {
5593 release = true;
5594 }
5595 }
5596 if (release) {
5597 ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle);
5598 releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid);
5599 }
5600 }
5601}
5602
Phil Burk09bc4612016-02-24 15:58:15 -08005603// Modify the list of surround sound formats supported.
Phil Burk0709b0a2016-03-31 12:54:57 -07005604void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) {
5605 FormatVector &formats = *formatsPtr;
Phil Burk07ac1142016-03-25 13:39:29 -07005606 // TODO Set this based on Config properties.
5607 const bool alwaysForceAC3 = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005608
5609 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5610 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
Phil Burk0709b0a2016-03-31 12:54:57 -07005611 ALOGD("%s: forced use = %d", __FUNCTION__, forceUse);
Phil Burk09bc4612016-02-24 15:58:15 -08005612
5613 // Analyze original support for various formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005614 bool supportsAC3 = false;
5615 bool supportsOtherSurround = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005616 bool supportsIEC61937 = false;
5617 for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) {
5618 audio_format_t format = formats[formatIndex];
Phil Burk09bc4612016-02-24 15:58:15 -08005619 switch (format) {
5620 case AUDIO_FORMAT_AC3:
Phil Burk07ac1142016-03-25 13:39:29 -07005621 supportsAC3 = true;
5622 break;
Phil Burk09bc4612016-02-24 15:58:15 -08005623 case AUDIO_FORMAT_E_AC3:
5624 case AUDIO_FORMAT_DTS:
5625 case AUDIO_FORMAT_DTS_HD:
Phil Burk07ac1142016-03-25 13:39:29 -07005626 supportsOtherSurround = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005627 break;
5628 case AUDIO_FORMAT_IEC61937:
5629 supportsIEC61937 = true;
5630 break;
5631 default:
5632 break;
5633 }
5634 }
Phil Burk09bc4612016-02-24 15:58:15 -08005635
5636 // Modify formats based on surround preferences.
5637 // If NEVER, remove support for surround formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005638 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5639 if (supportsAC3 || supportsOtherSurround || supportsIEC61937) {
5640 // Remove surround sound related formats.
5641 for (size_t formatIndex = 0; formatIndex < formats.size(); ) {
5642 audio_format_t format = formats[formatIndex];
5643 switch(format) {
5644 case AUDIO_FORMAT_AC3:
5645 case AUDIO_FORMAT_E_AC3:
5646 case AUDIO_FORMAT_DTS:
5647 case AUDIO_FORMAT_DTS_HD:
5648 case AUDIO_FORMAT_IEC61937:
Phil Burk07ac1142016-03-25 13:39:29 -07005649 formats.removeAt(formatIndex);
5650 break;
5651 default:
5652 formatIndex++; // keep it
5653 break;
5654 }
Phil Burk09bc4612016-02-24 15:58:15 -08005655 }
Phil Burk07ac1142016-03-25 13:39:29 -07005656 supportsAC3 = false;
5657 supportsOtherSurround = false;
5658 supportsIEC61937 = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005659 }
Phil Burk07ac1142016-03-25 13:39:29 -07005660 } else { // AUTO or ALWAYS
5661 // Most TVs support AC3 even if they do not report it in the EDID.
5662 if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS))
5663 && !supportsAC3) {
5664 formats.add(AUDIO_FORMAT_AC3);
5665 supportsAC3 = true;
5666 }
5667
5668 // If ALWAYS, add support for raw surround formats if all are missing.
5669 // This assumes that if any of these formats are reported by the HAL
5670 // then the report is valid and should not be modified.
5671 if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)
5672 && !supportsOtherSurround) {
5673 formats.add(AUDIO_FORMAT_E_AC3);
5674 formats.add(AUDIO_FORMAT_DTS);
5675 formats.add(AUDIO_FORMAT_DTS_HD);
5676 supportsOtherSurround = true;
5677 }
5678
5679 // Add support for IEC61937 if any raw surround supported.
5680 // The HAL could do this but add it here, just in case.
5681 if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) {
5682 formats.add(AUDIO_FORMAT_IEC61937);
5683 supportsIEC61937 = true;
5684 }
Phil Burk09bc4612016-02-24 15:58:15 -08005685 }
Phil Burk0709b0a2016-03-31 12:54:57 -07005686}
5687
5688// Modify the list of channel masks supported.
5689void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) {
5690 ChannelsVector &channelMasks = *channelMasksPtr;
5691 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5692 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
5693
5694 // If NEVER, then remove support for channelMasks > stereo.
5695 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5696 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) {
5697 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5698 if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) {
5699 ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask);
5700 channelMasks.removeAt(maskIndex);
5701 } else {
5702 maskIndex++;
5703 }
5704 }
5705 // If ALWAYS, then make sure we at least support 5.1
5706 } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) {
5707 bool supports5dot1 = false;
5708 // Are there any channel masks that can be considered "surround"?
5709 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); maskIndex++) {
5710 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5711 if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) {
5712 supports5dot1 = true;
5713 break;
5714 }
5715 }
5716 // If not then add 5.1 support.
5717 if (!supports5dot1) {
5718 channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1);
5719 ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__);
5720 }
Phil Burk09bc4612016-02-24 15:58:15 -08005721 }
5722}
5723
Phil Burk00eeb322016-03-31 12:41:00 -07005724void AudioPolicyManager::updateAudioProfiles(audio_devices_t device,
5725 audio_io_handle_t ioHandle,
François Gaffie112b0af2015-11-19 16:13:25 +01005726 AudioProfileVector &profiles)
5727{
5728 String8 reply;
Phil Burk0709b0a2016-03-31 12:54:57 -07005729
François Gaffie112b0af2015-11-19 16:13:25 +01005730 // Format MUST be checked first to update the list of AudioProfile
5731 if (profiles.hasDynamicFormat()) {
Mikhail Naganov388360c2016-10-17 17:09:41 -07005732 reply = mpClientInterface->getParameters(
5733 ioHandle, String8(AudioParameter::keyStreamSupportedFormats));
Phil Burk0709b0a2016-03-31 12:54:57 -07005734 ALOGV("%s: supported formats %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005735 AudioParameter repliedParameters(reply);
5736 if (repliedParameters.get(
Mikhail Naganov388360c2016-10-17 17:09:41 -07005737 String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) {
François Gaffie112b0af2015-11-19 16:13:25 +01005738 ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__);
5739 return;
5740 }
Phil Burk09bc4612016-02-24 15:58:15 -08005741 FormatVector formats = formatsFromString(reply.string());
Phil Burk00eeb322016-03-31 12:41:00 -07005742 if (device == AUDIO_DEVICE_OUT_HDMI) {
Phil Burk0709b0a2016-03-31 12:54:57 -07005743 filterSurroundFormats(&formats);
Phil Burk00eeb322016-03-31 12:41:00 -07005744 }
Phil Burk09bc4612016-02-24 15:58:15 -08005745 profiles.setFormats(formats);
François Gaffie112b0af2015-11-19 16:13:25 +01005746 }
5747 const FormatVector &supportedFormats = profiles.getSupportedFormats();
5748
Eric Laurent20eb3a42016-01-26 18:39:17 -08005749 for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) {
François Gaffie112b0af2015-11-19 16:13:25 +01005750 audio_format_t format = supportedFormats[formatIndex];
Eric Laurent20eb3a42016-01-26 18:39:17 -08005751 ChannelsVector channelMasks;
5752 SampleRateVector samplingRates;
François Gaffie112b0af2015-11-19 16:13:25 +01005753 AudioParameter requestedParameters;
Mikhail Naganov388360c2016-10-17 17:09:41 -07005754 requestedParameters.addInt(String8(AudioParameter::keyFormat), format);
François Gaffie112b0af2015-11-19 16:13:25 +01005755
5756 if (profiles.hasDynamicRateFor(format)) {
Mikhail Naganov388360c2016-10-17 17:09:41 -07005757 reply = mpClientInterface->getParameters(
5758 ioHandle,
5759 requestedParameters.toString() + ";" +
5760 AudioParameter::keyStreamSupportedSamplingRates);
François Gaffie112b0af2015-11-19 16:13:25 +01005761 ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005762 AudioParameter repliedParameters(reply);
5763 if (repliedParameters.get(
Mikhail Naganov388360c2016-10-17 17:09:41 -07005764 String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) {
Eric Laurent62e4bc52016-02-02 18:37:28 -08005765 samplingRates = samplingRatesFromString(reply.string());
François Gaffie112b0af2015-11-19 16:13:25 +01005766 }
5767 }
5768 if (profiles.hasDynamicChannelsFor(format)) {
5769 reply = mpClientInterface->getParameters(ioHandle,
5770 requestedParameters.toString() + ";" +
Mikhail Naganov388360c2016-10-17 17:09:41 -07005771 AudioParameter::keyStreamSupportedChannels);
François Gaffie112b0af2015-11-19 16:13:25 +01005772 ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005773 AudioParameter repliedParameters(reply);
5774 if (repliedParameters.get(
Mikhail Naganov388360c2016-10-17 17:09:41 -07005775 String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) {
Eric Laurent62e4bc52016-02-02 18:37:28 -08005776 channelMasks = channelMasksFromString(reply.string());
Phil Burk0709b0a2016-03-31 12:54:57 -07005777 if (device == AUDIO_DEVICE_OUT_HDMI) {
5778 filterSurroundChannelMasks(&channelMasks);
5779 }
François Gaffie112b0af2015-11-19 16:13:25 +01005780 }
5781 }
Eric Laurent20eb3a42016-01-26 18:39:17 -08005782 profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates));
François Gaffie112b0af2015-11-19 16:13:25 +01005783 }
5784}
Eric Laurentd60560a2015-04-10 11:31:20 -07005785
Eric Laurente552edb2014-03-10 17:42:56 -07005786}; // namespace android