blob: a0d0cf3ecc5c5e64411f9d64da288ba8c092e846 [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 =
Haynes Mathew George84c621e2017-04-25 11:41:50 -0700709 (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD |
710 AUDIO_OUTPUT_FLAG_VOIP_RX);
Eric Laurent861a6282015-05-18 15:40:16 -0700711 flags =
712 (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT);
713
714 sp<IOProfile> profile;
715
Eric Laurente552edb2014-03-10 17:42:56 -0700716 for (size_t i = 0; i < mHwModules.size(); i++) {
717 if (mHwModules[i]->mHandle == 0) {
718 continue;
719 }
720 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent861a6282015-05-18 15:40:16 -0700721 sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j];
722 if (!curProfile->isCompatibleProfile(device, String8(""),
Andy Hungf129b032015-04-07 13:45:50 -0700723 samplingRate, NULL /*updatedSamplingRate*/,
724 format, NULL /*updatedFormat*/,
725 channelMask, NULL /*updatedChannelMask*/,
Eric Laurent861a6282015-05-18 15:40:16 -0700726 flags)) {
727 continue;
728 }
729 // reject profiles not corresponding to a device currently available
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100730 if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) {
Eric Laurent861a6282015-05-18 15:40:16 -0700731 continue;
732 }
733 // if several profiles are compatible, give priority to one with offload capability
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100734 if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) {
Eric Laurent861a6282015-05-18 15:40:16 -0700735 continue;
736 }
737 profile = curProfile;
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100738 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent861a6282015-05-18 15:40:16 -0700739 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700740 }
Eric Laurente552edb2014-03-10 17:42:56 -0700741 }
742 }
Eric Laurent861a6282015-05-18 15:40:16 -0700743 return profile;
Eric Laurente552edb2014-03-10 17:42:56 -0700744}
745
Eric Laurente0720872014-03-11 09:30:41 -0700746audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +0100747 uint32_t samplingRate,
748 audio_format_t format,
749 audio_channel_mask_t channelMask,
750 audio_output_flags_t flags,
751 const audio_offload_info_t *offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -0700752{
Eric Laurent3b73df72014-03-11 09:06:29 -0700753 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700754 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
755 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
756 device, stream, samplingRate, format, channelMask, flags);
757
Kevin Rocard551061a2017-02-06 15:59:29 -0800758 return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE, uid_t{0} /*Invalid uid*/,
Eric Laurente83b55d2014-11-14 10:06:21 -0800759 stream, samplingRate,format, channelMask,
760 flags, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700761}
762
Eric Laurente83b55d2014-11-14 10:06:21 -0800763status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
764 audio_io_handle_t *output,
765 audio_session_t session,
766 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700767 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800768 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800769 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700770 audio_port_handle_t selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800771 audio_port_handle_t *portId)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700772{
Eric Laurente83b55d2014-11-14 10:06:21 -0800773 audio_attributes_t attributes;
774 if (attr != NULL) {
775 if (!isValidAttributes(attr)) {
776 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
777 attr->usage, attr->content_type, attr->flags,
778 attr->tags);
779 return BAD_VALUE;
780 }
781 attributes = *attr;
782 } else {
783 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
784 ALOGE("getOutputForAttr(): invalid stream type");
785 return BAD_VALUE;
786 }
787 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700788 }
Eric Laurent20b9ef02016-12-05 11:03:16 -0800789
790 // TODO: check for existing client for this port ID
791 if (*portId == AUDIO_PORT_HANDLE_NONE) {
792 *portId = AudioPort::getNextUniqueId();
793 }
794
Eric Laurentc75307b2015-03-17 15:29:32 -0700795 sp<SwAudioOutputDescriptor> desc;
Jean-Michel Trivie8deced2016-02-11 12:50:39 -0800796 if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) {
François Gaffie036e1e92015-03-19 10:16:24 +0100797 ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr");
Eric Laurent20b9ef02016-12-05 11:03:16 -0800798 if (!audio_has_proportional_frames(config->format)) {
François Gaffie036e1e92015-03-19 10:16:24 +0100799 return BAD_VALUE;
Eric Laurent275e8e92014-11-30 15:14:47 -0800800 }
François Gaffie036e1e92015-03-19 10:16:24 +0100801 *stream = streamTypefromAttributesInt(&attributes);
802 *output = desc->mIoHandle;
803 ALOGV("getOutputForAttr() returns output %d", *output);
804 return NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -0800805 }
806 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
807 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
808 return BAD_VALUE;
809 }
810
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700811 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x"
812 " session %d selectedDeviceId %d",
813 attributes.usage, attributes.content_type, attributes.tags, attributes.flags,
814 session, selectedDeviceId);
815
816 *stream = streamTypefromAttributesInt(&attributes);
817
818 // Explicit routing?
819 sp<DeviceDescriptor> deviceDesc;
820 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
821 if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) {
822 deviceDesc = mAvailableOutputDevices[i];
823 break;
824 }
825 }
826 mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700827
Eric Laurente83b55d2014-11-14 10:06:21 -0800828 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700829 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700830
Eric Laurente83b55d2014-11-14 10:06:21 -0800831 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700832 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
833 }
834
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700835 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Eric Laurent20b9ef02016-12-05 11:03:16 -0800836 device, config->sample_rate, config->format, config->channel_mask, flags);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700837
Kevin Rocard551061a2017-02-06 15:59:29 -0800838 *output = getOutputForDevice(device, session, uid, *stream,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800839 config->sample_rate, config->format, config->channel_mask,
840 flags, &config->offload_info);
Eric Laurente83b55d2014-11-14 10:06:21 -0800841 if (*output == AUDIO_IO_HANDLE_NONE) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700842 mOutputRoutes.removeRoute(session);
Eric Laurente83b55d2014-11-14 10:06:21 -0800843 return INVALID_OPERATION;
844 }
Paul McLeanaa981192015-03-21 09:55:15 -0700845
Eric Laurente83b55d2014-11-14 10:06:21 -0800846 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700847}
848
849audio_io_handle_t AudioPolicyManager::getOutputForDevice(
850 audio_devices_t device,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800851 audio_session_t session __unused,
Kevin Rocard551061a2017-02-06 15:59:29 -0800852 uid_t clientUid,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700853 audio_stream_type_t stream,
854 uint32_t samplingRate,
855 audio_format_t format,
856 audio_channel_mask_t channelMask,
857 audio_output_flags_t flags,
858 const audio_offload_info_t *offloadInfo)
859{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700860 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700861 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700862
Eric Laurente552edb2014-03-10 17:42:56 -0700863#ifdef AUDIO_POLICY_TEST
864 if (mCurOutput != 0) {
865 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
866 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
867
868 if (mTestOutputs[mCurOutput] == 0) {
869 ALOGV("getOutput() opening test output");
Eric Laurentc75307b2015-03-17 15:29:32 -0700870 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
871 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700872 outputDesc->mDevice = mTestDevice;
Eric Laurente552edb2014-03-10 17:42:56 -0700873 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700874 outputDesc->mFlags =
875 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700876 outputDesc->mRefCount[stream] = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700877 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
878 config.sample_rate = mTestSamplingRate;
879 config.channel_mask = mTestChannels;
880 config.format = mTestFormat;
Phil Burk77cce802014-08-04 16:18:15 -0700881 if (offloadInfo != NULL) {
882 config.offload_info = *offloadInfo;
883 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700884 status = mpClientInterface->openOutput(0,
885 &mTestOutputs[mCurOutput],
886 &config,
887 &outputDesc->mDevice,
888 String8(""),
889 &outputDesc->mLatency,
890 outputDesc->mFlags);
891 if (status == NO_ERROR) {
892 outputDesc->mSamplingRate = config.sample_rate;
893 outputDesc->mFormat = config.format;
894 outputDesc->mChannelMask = config.channel_mask;
Eric Laurente552edb2014-03-10 17:42:56 -0700895 AudioParameter outputCmd = AudioParameter();
896 outputCmd.addInt(String8("set_id"),mCurOutput);
897 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
898 addOutput(mTestOutputs[mCurOutput], outputDesc);
899 }
900 }
901 return mTestOutputs[mCurOutput];
902 }
903#endif //AUDIO_POLICY_TEST
904
905 // open a direct output if required by specified parameters
906 //force direct flag if offload flag is set: offloading implies a direct output stream
907 // and all common behaviors are driven by checking only the direct flag
908 // this should normally be set appropriately in the policy configuration file
909 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700910 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700911 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700912 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
913 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
914 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800915 // only allow deep buffering for music stream type
916 if (stream != AUDIO_STREAM_MUSIC) {
917 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Ravi Kumar Alamanda439e4ed2015-04-03 12:13:21 -0700918 } else if (/* stream == AUDIO_STREAM_MUSIC && */
919 flags == AUDIO_OUTPUT_FLAG_NONE &&
920 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
921 // use DEEP_BUFFER as default output for music stream type
922 flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Eric Laurente83b55d2014-11-14 10:06:21 -0800923 }
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700924 if (stream == AUDIO_STREAM_TTS) {
925 flags = AUDIO_OUTPUT_FLAG_TTS;
Haynes Mathew George84c621e2017-04-25 11:41:50 -0700926 } else if (stream == AUDIO_STREAM_VOICE_CALL &&
927 getPhoneState() == AUDIO_MODE_IN_COMMUNICATION &&
928 audio_is_linear_pcm(format)) {
929 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX |
930 AUDIO_OUTPUT_FLAG_DIRECT);
931 ALOGV("Set VoIP and Direct output flags for PCM format");
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700932 }
Eric Laurente552edb2014-03-10 17:42:56 -0700933
Eric Laurentb732cf52014-09-24 19:08:21 -0700934 sp<IOProfile> profile;
935
936 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700937 // and not explicitly requested
938 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
Glenn Kasten05ddca52016-02-11 08:17:12 -0800939 audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700940 audio_channel_count_from_out_mask(channelMask) <= 2) {
941 goto non_direct_output;
942 }
943
Andy Hung2ddee192015-12-18 17:34:44 -0800944 // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled.
945 // This prevents creating an offloaded track and tearing it down immediately after start
946 // when audioflinger detects there is an active non offloadable effect.
Eric Laurente552edb2014-03-10 17:42:56 -0700947 // FIXME: We should check the audio session here but we do not have it in this context.
948 // This may prevent offloading in rare situations where effects are left active by apps
949 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700950
Eric Laurente552edb2014-03-10 17:42:56 -0700951 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
Andy Hung2ddee192015-12-18 17:34:44 -0800952 !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700953 profile = getProfileForDirectOutput(device,
954 samplingRate,
955 format,
956 channelMask,
957 (audio_output_flags_t)flags);
958 }
959
Eric Laurent1c333e22014-05-20 10:48:17 -0700960 if (profile != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700961 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700962
963 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700964 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700965 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
966 outputDesc = desc;
Kevin Rocard551061a2017-02-06 15:59:29 -0800967 // reuse direct output if currently open by the same client
968 // and configured with same parameters
Eric Laurente552edb2014-03-10 17:42:56 -0700969 if ((samplingRate == outputDesc->mSamplingRate) &&
Kevin Rocard551061a2017-02-06 15:59:29 -0800970 audio_formats_match(format, outputDesc->mFormat) &&
971 (channelMask == outputDesc->mChannelMask)) {
972 if (clientUid == outputDesc->mDirectClientUid) {
973 outputDesc->mDirectOpenCount++;
974 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
975 return mOutputs.keyAt(i);
976 } else {
977 ALOGV("getOutput() do not reuse direct output because current client (%ld) "
978 "is not the same as requesting client (%ld)",
979 (long)outputDesc->mDirectClientUid, (long)clientUid);
980 goto non_direct_output;
981 }
Eric Laurente552edb2014-03-10 17:42:56 -0700982 }
983 }
984 }
985 // close direct output if currently open and configured with different parameters
986 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700987 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700988 }
Eric Laurent861a6282015-05-18 15:40:16 -0700989
990 // if the selected profile is offloaded and no offload info was specified,
991 // create a default one
992 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100993 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
Eric Laurent861a6282015-05-18 15:40:16 -0700994 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
995 defaultOffloadInfo.sample_rate = samplingRate;
996 defaultOffloadInfo.channel_mask = channelMask;
997 defaultOffloadInfo.format = format;
998 defaultOffloadInfo.stream_type = stream;
999 defaultOffloadInfo.bit_rate = 0;
1000 defaultOffloadInfo.duration_us = -1;
1001 defaultOffloadInfo.has_video = true; // conservative
1002 defaultOffloadInfo.is_streaming = true; // likely
1003 offloadInfo = &defaultOffloadInfo;
1004 }
1005
Eric Laurentc75307b2015-03-17 15:29:32 -07001006 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07001007 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -07001008 outputDesc->mLatency = 0;
Eric Laurent861a6282015-05-18 15:40:16 -07001009 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001010 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1011 config.sample_rate = samplingRate;
1012 config.channel_mask = channelMask;
1013 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -07001014 if (offloadInfo != NULL) {
1015 config.offload_info = *offloadInfo;
1016 }
Eric Laurente3014102017-05-03 11:15:43 -07001017 DeviceVector outputDevices = mAvailableOutputDevices.getDevicesFromType(device);
1018 String8 address = outputDevices.size() > 0 ? outputDevices.itemAt(0)->mAddress
1019 : String8("");
Eric Laurent322b4d22015-04-03 15:57:54 -07001020 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07001021 &output,
1022 &config,
1023 &outputDesc->mDevice,
Eric Laurente3014102017-05-03 11:15:43 -07001024 address,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001025 &outputDesc->mLatency,
1026 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001027
1028 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -07001029 if (status != NO_ERROR ||
1030 (samplingRate != 0 && samplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -08001031 (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) ||
Eric Laurentcf2c0212014-07-25 16:20:43 -07001032 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -07001033 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1034 "format %d %d, channelMask %04x %04x", output, samplingRate,
1035 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1036 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001037 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07001038 mpClientInterface->closeOutput(output);
1039 }
Eric Laurenta82797f2015-01-30 11:49:43 -08001040 // fall back to mixer output if possible when the direct output could not be open
Glenn Kasten05ddca52016-02-11 08:17:12 -08001041 if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) {
Eric Laurenta82797f2015-01-30 11:49:43 -08001042 goto non_direct_output;
1043 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001044 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001045 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001046 outputDesc->mSamplingRate = config.sample_rate;
1047 outputDesc->mChannelMask = config.channel_mask;
1048 outputDesc->mFormat = config.format;
1049 outputDesc->mRefCount[stream] = 0;
1050 outputDesc->mStopTime[stream] = 0;
1051 outputDesc->mDirectOpenCount = 1;
Kevin Rocard551061a2017-02-06 15:59:29 -08001052 outputDesc->mDirectClientUid = clientUid;
Eric Laurente552edb2014-03-10 17:42:56 -07001053 addOutput(output, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07001054 mPreviousOutputs = mOutputs;
1055 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -07001056 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001057 return output;
1058 }
1059
Eric Laurentb732cf52014-09-24 19:08:21 -07001060non_direct_output:
Eric Laurent14cbfca2016-03-17 09:42:16 -07001061
1062 // A request for HW A/V sync cannot fallback to a mixed output because time
1063 // stamps are embedded in audio data
1064 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1065 return AUDIO_IO_HANDLE_NONE;
1066 }
1067
Eric Laurente552edb2014-03-10 17:42:56 -07001068 // ignoring channel mask due to downmix capability in mixer
1069
1070 // open a non direct output
1071
1072 // for non direct outputs, only PCM is supported
1073 if (audio_is_linear_pcm(format)) {
1074 // get which output is suitable for the specified stream. The actual
1075 // routing change will happen when startOutput() will be called
1076 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1077
Eric Laurent8838a382014-09-08 16:44:28 -07001078 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1079 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1080 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -07001081 }
1082 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1083 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1084
Paul McLeanaa981192015-03-21 09:55:15 -07001085 ALOGV(" getOutputForDevice() returns output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -07001086
1087 return output;
1088}
1089
Eric Laurente0720872014-03-11 09:30:41 -07001090audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -07001091 audio_output_flags_t flags,
1092 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -07001093{
1094 // select one output among several that provide a path to a particular device or set of
1095 // devices (the list was previously build by getOutputsForDevice()).
1096 // The priority is as follows:
1097 // 1: the output with the highest number of requested policy flags
Eric Laurente6930022016-02-11 10:20:40 -08001098 // 2: the output with the bit depth the closest to the requested one
1099 // 3: the primary output
1100 // 4: the first output in the list
Eric Laurente552edb2014-03-10 17:42:56 -07001101
1102 if (outputs.size() == 0) {
1103 return 0;
1104 }
1105 if (outputs.size() == 1) {
1106 return outputs[0];
1107 }
1108
1109 int maxCommonFlags = 0;
Eric Laurente6930022016-02-11 10:20:40 -08001110 audio_io_handle_t outputForFlags = 0;
1111 audio_io_handle_t outputForPrimary = 0;
1112 audio_io_handle_t outputForFormat = 0;
1113 audio_format_t bestFormat = AUDIO_FORMAT_INVALID;
1114 audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07001115
1116 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001117 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -07001118 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -07001119 // if a valid format is specified, skip output if not compatible
1120 if (format != AUDIO_FORMAT_INVALID) {
1121 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente6930022016-02-11 10:20:40 -08001122 if (!audio_formats_match(format, outputDesc->mFormat)) {
Eric Laurent8838a382014-09-08 16:44:28 -07001123 continue;
1124 }
1125 } else if (!audio_is_linear_pcm(format)) {
1126 continue;
1127 }
Eric Laurente6930022016-02-11 10:20:40 -08001128 if (AudioPort::isBetterFormatMatch(
1129 outputDesc->mFormat, bestFormat, format)) {
1130 outputForFormat = outputs[i];
1131 bestFormat = outputDesc->mFormat;
1132 }
Eric Laurent8838a382014-09-08 16:44:28 -07001133 }
1134
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001135 int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags);
Eric Laurente6930022016-02-11 10:20:40 -08001136 if (commonFlags >= maxCommonFlags) {
1137 if (commonFlags == maxCommonFlags) {
1138 if (AudioPort::isBetterFormatMatch(
1139 outputDesc->mFormat, bestFormatForFlags, format)) {
1140 outputForFlags = outputs[i];
1141 bestFormatForFlags = outputDesc->mFormat;
1142 }
1143 } else {
1144 outputForFlags = outputs[i];
1145 maxCommonFlags = commonFlags;
1146 bestFormatForFlags = outputDesc->mFormat;
1147 }
Eric Laurente552edb2014-03-10 17:42:56 -07001148 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
1149 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001150 if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurente6930022016-02-11 10:20:40 -08001151 outputForPrimary = outputs[i];
Eric Laurente552edb2014-03-10 17:42:56 -07001152 }
1153 }
1154 }
1155
Eric Laurente6930022016-02-11 10:20:40 -08001156 if (outputForFlags != 0) {
1157 return outputForFlags;
Eric Laurente552edb2014-03-10 17:42:56 -07001158 }
Eric Laurente6930022016-02-11 10:20:40 -08001159 if (outputForFormat != 0) {
1160 return outputForFormat;
1161 }
1162 if (outputForPrimary != 0) {
1163 return outputForPrimary;
Eric Laurente552edb2014-03-10 17:42:56 -07001164 }
1165
1166 return outputs[0];
1167}
1168
Eric Laurente0720872014-03-11 09:30:41 -07001169status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001170 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001171 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001172{
Paul McLeanaa981192015-03-21 09:55:15 -07001173 ALOGV("startOutput() output %d, stream %d, session %d",
1174 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -07001175 ssize_t index = mOutputs.indexOfKey(output);
1176 if (index < 0) {
1177 ALOGW("startOutput() unknown output %d", output);
1178 return BAD_VALUE;
1179 }
1180
Eric Laurentc75307b2015-03-17 15:29:32 -07001181 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
1182
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001183 // Routing?
1184 mOutputRoutes.incRouteActivity(session);
1185
Eric Laurentc75307b2015-03-17 15:29:32 -07001186 audio_devices_t newDevice;
Eric Laurentc40d9692016-04-13 19:14:13 -07001187 AudioMix *policyMix = NULL;
1188 const char *address = NULL;
Eric Laurentc75307b2015-03-17 15:29:32 -07001189 if (outputDesc->mPolicyMix != NULL) {
Eric Laurentc40d9692016-04-13 19:14:13 -07001190 policyMix = outputDesc->mPolicyMix;
1191 address = policyMix->mDeviceAddress.string();
1192 if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
1193 newDevice = policyMix->mDeviceType;
1194 } else {
1195 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
1196 }
Eric Laurent493404d2015-04-21 15:07:36 -07001197 } else if (mOutputRoutes.hasRouteChanged(session)) {
1198 newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001199 checkStrategyRoute(getStrategy(stream), output);
Eric Laurentc75307b2015-03-17 15:29:32 -07001200 } else {
1201 newDevice = AUDIO_DEVICE_NONE;
1202 }
1203
1204 uint32_t delayMs = 0;
1205
Eric Laurentc40d9692016-04-13 19:14:13 -07001206 status_t status = startSource(outputDesc, stream, newDevice, address, &delayMs);
Eric Laurentc75307b2015-03-17 15:29:32 -07001207
1208 if (status != NO_ERROR) {
1209 mOutputRoutes.decRouteActivity(session);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001210 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -07001211 }
1212 // Automatically enable the remote submix input when output is started on a re routing mix
1213 // of type MIX_TYPE_RECORDERS
Eric Laurentc40d9692016-04-13 19:14:13 -07001214 if (audio_is_remote_submix_device(newDevice) && policyMix != NULL &&
1215 policyMix->mMixType == MIX_TYPE_RECORDERS) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001216 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1217 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Eric Laurentc40d9692016-04-13 19:14:13 -07001218 address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001219 "remote-submix");
1220 }
1221
1222 if (delayMs != 0) {
1223 usleep(delayMs * 1000);
1224 }
1225
1226 return status;
1227}
1228
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001229status_t AudioPolicyManager::startSource(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurentc75307b2015-03-17 15:29:32 -07001230 audio_stream_type_t stream,
1231 audio_devices_t device,
Eric Laurentc40d9692016-04-13 19:14:13 -07001232 const char *address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001233 uint32_t *delayMs)
1234{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001235 // cannot start playback of STREAM_TTS if any other output is being used
1236 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001237
1238 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001239 if (stream == AUDIO_STREAM_TTS) {
1240 ALOGV("\t found BEACON stream");
Eric Laurent9459fb02015-08-12 18:36:32 -07001241 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001242 return INVALID_OPERATION;
1243 } else {
1244 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1245 }
1246 } else {
1247 // some playback other than beacon starts
1248 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1249 }
1250
Eric Laurent77305a62016-07-25 16:39:22 -07001251 // force device change if the output is inactive and no audio patch is already present.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001252 // check active before incrementing usage count
Eric Laurent77305a62016-07-25 16:39:22 -07001253 bool force = !outputDesc->isActive() &&
1254 (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001255
Eric Laurente552edb2014-03-10 17:42:56 -07001256 // increment usage count for this stream on the requested output:
1257 // NOTE that the usage count is the same for duplicated output and hardware output which is
1258 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1259 outputDesc->changeRefCount(stream, 1);
1260
Eric Laurent36829f92017-04-07 19:04:42 -07001261 if (stream == AUDIO_STREAM_MUSIC) {
1262 selectOutputForMusicEffects();
1263 }
1264
Eric Laurent493404d2015-04-21 15:07:36 -07001265 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001266 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001267 if (device == AUDIO_DEVICE_NONE) {
1268 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001269 }
Eric Laurent36829f92017-04-07 19:04:42 -07001270
Eric Laurente552edb2014-03-10 17:42:56 -07001271 routing_strategy strategy = getStrategy(stream);
1272 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001273 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1274 (beaconMuteLatency > 0);
1275 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001276 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001277 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001278 if (desc != outputDesc) {
Eric Laurent77305a62016-07-25 16:39:22 -07001279 // force a device change if any other output is:
1280 // - managed by the same hw module
1281 // - has a current device selection that differs from selected device.
1282 // - supports currently selected device
1283 // - has an active audio patch
Eric Laurente552edb2014-03-10 17:42:56 -07001284 // In this case, the audio HAL must receive the new device selection so that it can
1285 // change the device currently selected by the other active output.
1286 if (outputDesc->sharesHwModuleWith(desc) &&
Eric Laurent77305a62016-07-25 16:39:22 -07001287 desc->device() != device &&
1288 desc->supportedDevices() & device &&
1289 desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07001290 force = true;
1291 }
1292 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001293 // a notification so that audio focus effect can propagate, or that a mute/unmute
1294 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001295 uint32_t latency = desc->latency();
1296 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1297 waitMs = latency;
1298 }
1299 }
1300 }
Eric Laurentdc462862016-07-19 12:29:53 -07001301 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force, 0, NULL, address);
Eric Laurente552edb2014-03-10 17:42:56 -07001302
1303 // handle special case for sonification while in call
1304 if (isInCall()) {
1305 handleIncallSonification(stream, true, false);
1306 }
1307
1308 // apply volume rules for current stream and device if necessary
1309 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01001310 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07001311 outputDesc,
1312 device);
Eric Laurente552edb2014-03-10 17:42:56 -07001313
1314 // update the outputs if starting an output with a stream that can affect notification
1315 // routing
1316 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001317
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001318 // force reevaluating accessibility routing when ringtone or alarm starts
1319 if (strategy == STRATEGY_SONIFICATION) {
1320 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1321 }
Eric Laurentdc462862016-07-19 12:29:53 -07001322
1323 if (waitMs > muteWaitMs) {
1324 *delayMs = waitMs - muteWaitMs;
1325 }
Eric Laurente552edb2014-03-10 17:42:56 -07001326 }
Eric Laurentdc462862016-07-19 12:29:53 -07001327
Eric Laurente552edb2014-03-10 17:42:56 -07001328 return NO_ERROR;
1329}
1330
1331
Eric Laurente0720872014-03-11 09:30:41 -07001332status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001333 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001334 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001335{
1336 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1337 ssize_t index = mOutputs.indexOfKey(output);
1338 if (index < 0) {
1339 ALOGW("stopOutput() unknown output %d", output);
1340 return BAD_VALUE;
1341 }
1342
Eric Laurentc75307b2015-03-17 15:29:32 -07001343 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001344
Eric Laurentc75307b2015-03-17 15:29:32 -07001345 if (outputDesc->mRefCount[stream] == 1) {
1346 // Automatically disable the remote submix input when output is stopped on a
1347 // re routing mix of type MIX_TYPE_RECORDERS
1348 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1349 outputDesc->mPolicyMix != NULL &&
1350 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1351 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1352 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001353 outputDesc->mPolicyMix->mDeviceAddress,
Eric Laurentc75307b2015-03-17 15:29:32 -07001354 "remote-submix");
1355 }
1356 }
1357
1358 // Routing?
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001359 bool forceDeviceUpdate = false;
Eric Laurentc75307b2015-03-17 15:29:32 -07001360 if (outputDesc->mRefCount[stream] > 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001361 int activityCount = mOutputRoutes.decRouteActivity(session);
1362 forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0));
1363
1364 if (forceDeviceUpdate) {
1365 checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE);
1366 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001367 }
1368
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001369 return stopSource(outputDesc, stream, forceDeviceUpdate);
Eric Laurentc75307b2015-03-17 15:29:32 -07001370}
1371
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001372status_t AudioPolicyManager::stopSource(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001373 audio_stream_type_t stream,
1374 bool forceDeviceUpdate)
Eric Laurentc75307b2015-03-17 15:29:32 -07001375{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001376 // always handle stream stop, check which stream type is stopping
1377 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1378
Eric Laurente552edb2014-03-10 17:42:56 -07001379 // handle special case for sonification while in call
1380 if (isInCall()) {
1381 handleIncallSonification(stream, false, false);
1382 }
1383
1384 if (outputDesc->mRefCount[stream] > 0) {
1385 // decrement usage count of this stream on the output
1386 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001387
Eric Laurente552edb2014-03-10 17:42:56 -07001388 // store time at which the stream was stopped - see isStreamActive()
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001389 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
Eric Laurente552edb2014-03-10 17:42:56 -07001390 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001391 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001392 // delay the device switch by twice the latency because stopOutput() is executed when
1393 // the track stop() command is received and at that time the audio track buffer can
1394 // still contain data that needs to be drained. The latency only covers the audio HAL
1395 // and kernel buffers. Also the latency does not always include additional delay in the
1396 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001397 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001398
1399 // force restoring the device selection on other active outputs if it differs from the
1400 // one being selected for this output
Eric Laurent57de36c2016-09-28 16:59:11 -07001401 uint32_t delayMs = outputDesc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -07001402 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001403 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001404 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001405 desc->isActive() &&
1406 outputDesc->sharesHwModuleWith(desc) &&
1407 (newDevice != desc->device())) {
Haynes Mathew George11c499a2016-08-26 12:08:25 -07001408 audio_devices_t newDevice2 = getNewOutputDevice(desc, false /*fromCache*/);
1409 bool force = desc->device() != newDevice2;
Eric Laurentc75307b2015-03-17 15:29:32 -07001410 setOutputDevice(desc,
Haynes Mathew George11c499a2016-08-26 12:08:25 -07001411 newDevice2,
1412 force,
Eric Laurent57de36c2016-09-28 16:59:11 -07001413 delayMs);
1414 // re-apply device specific volume if not done by setOutputDevice()
1415 if (!force) {
1416 applyStreamVolumes(desc, newDevice2, delayMs);
1417 }
Eric Laurente552edb2014-03-10 17:42:56 -07001418 }
1419 }
1420 // update the outputs if stopping one with a stream that can affect notification routing
1421 handleNotificationRoutingForStream(stream);
1422 }
Eric Laurent36829f92017-04-07 19:04:42 -07001423 if (stream == AUDIO_STREAM_MUSIC) {
1424 selectOutputForMusicEffects();
1425 }
Eric Laurente552edb2014-03-10 17:42:56 -07001426 return NO_ERROR;
1427 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001428 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001429 return INVALID_OPERATION;
1430 }
1431}
1432
Eric Laurente83b55d2014-11-14 10:06:21 -08001433void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001434 audio_stream_type_t stream __unused,
1435 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001436{
1437 ALOGV("releaseOutput() %d", output);
1438 ssize_t index = mOutputs.indexOfKey(output);
1439 if (index < 0) {
1440 ALOGW("releaseOutput() releasing unknown output %d", output);
1441 return;
1442 }
1443
1444#ifdef AUDIO_POLICY_TEST
1445 int testIndex = testOutputIndex(output);
1446 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001447 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001448 if (outputDesc->isActive()) {
1449 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001450 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001451 mTestOutputs[testIndex] = 0;
1452 }
1453 return;
1454 }
1455#endif //AUDIO_POLICY_TEST
1456
Paul McLeanaa981192015-03-21 09:55:15 -07001457 // Routing
1458 mOutputRoutes.removeRoute(session);
1459
Eric Laurentc75307b2015-03-17 15:29:32 -07001460 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001461 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001462 if (desc->mDirectOpenCount <= 0) {
1463 ALOGW("releaseOutput() invalid open count %d for output %d",
1464 desc->mDirectOpenCount, output);
1465 return;
1466 }
1467 if (--desc->mDirectOpenCount == 0) {
1468 closeOutput(output);
Eric Laurentb52c1522014-05-20 11:27:36 -07001469 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001470 }
1471 }
1472}
1473
1474
Eric Laurentcaf7f482014-11-25 17:50:47 -08001475status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1476 audio_io_handle_t *input,
1477 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001478 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001479 const audio_config_base_t *config,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001480 audio_input_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -06001481 audio_port_handle_t selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001482 input_type_t *inputType,
1483 audio_port_handle_t *portId)
Eric Laurente552edb2014-03-10 17:42:56 -07001484{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001485 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1486 "session %d, flags %#x",
Eric Laurent20b9ef02016-12-05 11:03:16 -08001487 attr->source, config->sample_rate, config->format, config->channel_mask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001488
Eric Laurentcaf7f482014-11-25 17:50:47 -08001489 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001490 *inputType = API_INPUT_INVALID;
Eric Laurentfb66dd92016-01-28 18:32:03 -08001491
Eric Laurent275e8e92014-11-30 15:14:47 -08001492 audio_devices_t device;
1493 // handle legacy remote submix case where the address was not always specified
1494 String8 address = String8("");
Eric Laurentc447ded2015-01-06 08:47:05 -08001495 audio_source_t inputSource = attr->source;
1496 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001497 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001498
Eric Laurentc447ded2015-01-06 08:47:05 -08001499 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1500 inputSource = AUDIO_SOURCE_MIC;
1501 }
1502 halInputSource = inputSource;
1503
Eric Laurent20b9ef02016-12-05 11:03:16 -08001504 // TODO: check for existing client for this port ID
1505 if (*portId == AUDIO_PORT_HANDLE_NONE) {
1506 *portId = AudioPort::getNextUniqueId();
1507 }
1508
Paul McLean466dc8e2015-04-17 13:15:36 -06001509 // Explicit routing?
1510 sp<DeviceDescriptor> deviceDesc;
1511 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1512 if (mAvailableInputDevices[i]->getId() == selectedDeviceId) {
1513 deviceDesc = mAvailableInputDevices[i];
1514 break;
1515 }
1516 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001517 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001518
Eric Laurentc447ded2015-01-06 08:47:05 -08001519 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001520 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001521 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
François Gaffie036e1e92015-03-19 10:16:24 +01001522 if (ret != NO_ERROR) {
1523 return ret;
1524 }
1525 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001526 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1527 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001528 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001529 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001530 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001531 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001532 return BAD_VALUE;
1533 }
Eric Laurentc722f302014-12-10 11:21:49 -08001534 if (policyMix != NULL) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001535 address = policyMix->mDeviceAddress;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001536 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1537 // there is an external policy, but this input is attached to a mix of recorders,
1538 // meaning it receives audio injected into the framework, so the recorder doesn't
1539 // know about it and is therefore considered "legacy"
1540 *inputType = API_INPUT_LEGACY;
1541 } else {
1542 // recording a mix of players defined by an external policy, we're rerouting for
1543 // an external policy
1544 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1545 }
Eric Laurentc722f302014-12-10 11:21:49 -08001546 } else if (audio_is_remote_submix_device(device)) {
1547 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001548 *inputType = API_INPUT_MIX_CAPTURE;
Eric Laurent82db2692015-08-07 13:59:42 -07001549 } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
1550 *inputType = API_INPUT_TELEPHONY_RX;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001551 } else {
1552 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001553 }
Ravi Kumar Alamandab367f5b2015-08-25 08:21:37 -07001554
Eric Laurent599c7582015-12-07 18:05:55 -08001555 }
1556
1557 *input = getInputForDevice(device, address, session, uid, inputSource,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001558 config->sample_rate, config->format, config->channel_mask, flags,
Eric Laurent599c7582015-12-07 18:05:55 -08001559 policyMix);
1560 if (*input == AUDIO_IO_HANDLE_NONE) {
1561 mInputRoutes.removeRoute(session);
1562 return INVALID_OPERATION;
1563 }
Eric Laurent20b9ef02016-12-05 11:03:16 -08001564
Eric Laurent599c7582015-12-07 18:05:55 -08001565 ALOGV("getInputForAttr() returns input type = %d", *inputType);
1566 return NO_ERROR;
1567}
1568
1569
1570audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device,
1571 String8 address,
1572 audio_session_t session,
1573 uid_t uid,
1574 audio_source_t inputSource,
1575 uint32_t samplingRate,
1576 audio_format_t format,
1577 audio_channel_mask_t channelMask,
1578 audio_input_flags_t flags,
1579 AudioMix *policyMix)
1580{
1581 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
1582 audio_source_t halInputSource = inputSource;
1583 bool isSoundTrigger = false;
1584
1585 if (inputSource == AUDIO_SOURCE_HOTWORD) {
1586 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1587 if (index >= 0) {
1588 input = mSoundTriggerSessions.valueFor(session);
1589 isSoundTrigger = true;
1590 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1591 ALOGV("SoundTrigger capture on session %d input %d", session, input);
1592 } else {
1593 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001594 }
1595 }
1596
Andy Hungf129b032015-04-07 13:45:50 -07001597 // find a compatible input profile (not necessarily identical in parameters)
1598 sp<IOProfile> profile;
1599 // samplingRate and flags may be updated by getInputProfile
Glenn Kasten05ddca52016-02-11 08:17:12 -08001600 uint32_t profileSamplingRate = (samplingRate == 0) ? SAMPLE_RATE_HZ_DEFAULT : samplingRate;
Andy Hungf129b032015-04-07 13:45:50 -07001601 audio_format_t profileFormat = format;
1602 audio_channel_mask_t profileChannelMask = channelMask;
1603 audio_input_flags_t profileFlags = flags;
1604 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001605 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001606 profileSamplingRate, profileFormat, profileChannelMask,
1607 profileFlags);
1608 if (profile != 0) {
1609 break; // success
Eric Laurent05067782016-06-01 18:27:28 -07001610 } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) {
1611 profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry
Andy Hungf129b032015-04-07 13:45:50 -07001612 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1613 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1614 } else { // fail
Eric Laurent599c7582015-12-07 18:05:55 -08001615 ALOGW("getInputForDevice() could not find profile for device 0x%X,"
1616 "samplingRate %u, format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001617 device, samplingRate, format, channelMask, flags);
Eric Laurent599c7582015-12-07 18:05:55 -08001618 return input;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001619 }
Eric Laurente552edb2014-03-10 17:42:56 -07001620 }
Glenn Kasten05ddca52016-02-11 08:17:12 -08001621 // Pick input sampling rate if not specified by client
1622 if (samplingRate == 0) {
1623 samplingRate = profileSamplingRate;
1624 }
Eric Laurente552edb2014-03-10 17:42:56 -07001625
Eric Laurent322b4d22015-04-03 15:57:54 -07001626 if (profile->getModuleHandle() == 0) {
1627 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurent599c7582015-12-07 18:05:55 -08001628 return input;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001629 }
1630
Eric Laurent599c7582015-12-07 18:05:55 -08001631 sp<AudioSession> audioSession = new AudioSession(session,
1632 inputSource,
1633 format,
1634 samplingRate,
1635 channelMask,
1636 flags,
1637 uid,
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001638 isSoundTrigger,
1639 policyMix, mpClientInterface);
Eric Laurent599c7582015-12-07 18:05:55 -08001640
Eric Laurent74708e72017-04-07 17:13:42 -07001641// FIXME: disable concurrent capture until UI is ready
1642#if 0
Eric Laurent599c7582015-12-07 18:05:55 -08001643 // reuse an open input if possible
Eric Laurent555530a2017-02-07 18:17:24 -08001644 sp<AudioInputDescriptor> reusedInputDesc;
Eric Laurent599c7582015-12-07 18:05:55 -08001645 for (size_t i = 0; i < mInputs.size(); i++) {
1646 sp<AudioInputDescriptor> desc = mInputs.valueAt(i);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001647 // reuse input if:
1648 // - it shares the same profile
1649 // AND
1650 // - it is not a reroute submix input
1651 // AND
1652 // - it is: not used for sound trigger
1653 // OR
1654 // used for sound trigger and all clients use the same session ID
1655 //
1656 if ((profile == desc->mProfile) &&
1657 (isSoundTrigger == desc->isSoundTrigger()) &&
1658 !is_virtual_input_device(device)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001659
1660 sp<AudioSession> as = desc->getAudioSession(session);
1661 if (as != 0) {
1662 // do not allow unmatching properties on same session
1663 if (as->matches(audioSession)) {
1664 as->changeOpenCount(1);
1665 } else {
1666 ALOGW("getInputForDevice() record with different attributes"
1667 " exists for session %d", session);
Eric Laurent555530a2017-02-07 18:17:24 -08001668 continue;
Eric Laurent599c7582015-12-07 18:05:55 -08001669 }
Eric Laurentfb66dd92016-01-28 18:32:03 -08001670 } else if (isSoundTrigger) {
Eric Laurent555530a2017-02-07 18:17:24 -08001671 continue;
Eric Laurentfb66dd92016-01-28 18:32:03 -08001672 }
Eric Laurentbb948092017-01-23 18:33:30 -08001673
Eric Laurent555530a2017-02-07 18:17:24 -08001674 // Reuse the already opened input stream on this profile if:
1675 // - the new capture source is background OR
1676 // - the path requested configurations match OR
1677 // - the new source priority is less than the highest source priority on this input
1678 // If the input stream cannot be reused, close it before opening a new stream
1679 // on the same profile for the new client so that the requested path configuration
1680 // can be selected.
1681 if (!isConcurrentSource(inputSource) &&
Eric Laurentbb948092017-01-23 18:33:30 -08001682 ((desc->mSamplingRate != samplingRate ||
Eric Laurentfb66dd92016-01-28 18:32:03 -08001683 desc->mChannelMask != channelMask ||
Eric Laurente6930022016-02-11 10:20:40 -08001684 !audio_formats_match(desc->mFormat, format)) &&
Eric Laurentfb66dd92016-01-28 18:32:03 -08001685 (source_priority(desc->getHighestPrioritySource(false /*activeOnly*/)) <
Eric Laurentbb948092017-01-23 18:33:30 -08001686 source_priority(inputSource)))) {
Eric Laurent555530a2017-02-07 18:17:24 -08001687 reusedInputDesc = desc;
1688 continue;
Eric Laurent599c7582015-12-07 18:05:55 -08001689 } else {
1690 desc->addAudioSession(session, audioSession);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001691 ALOGV("%s: reusing input %d", __FUNCTION__, mInputs.keyAt(i));
1692 return mInputs.keyAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08001693 }
Eric Laurent599c7582015-12-07 18:05:55 -08001694 }
1695 }
Eric Laurent599c7582015-12-07 18:05:55 -08001696
Eric Laurent555530a2017-02-07 18:17:24 -08001697 if (reusedInputDesc != 0) {
1698 AudioSessionCollection sessions = reusedInputDesc->getAudioSessions(false /*activeOnly*/);
1699 for (size_t j = 0; j < sessions.size(); j++) {
1700 audio_session_t currentSession = sessions.keyAt(j);
1701 stopInput(reusedInputDesc->mIoHandle, currentSession);
1702 releaseInput(reusedInputDesc->mIoHandle, currentSession);
1703 }
1704 }
Eric Laurent74708e72017-04-07 17:13:42 -07001705#endif
Eric Laurent555530a2017-02-07 18:17:24 -08001706
Eric Laurentcf2c0212014-07-25 16:20:43 -07001707 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001708 config.sample_rate = profileSamplingRate;
1709 config.channel_mask = profileChannelMask;
1710 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001711
Eric Laurente3014102017-05-03 11:15:43 -07001712 if (address == "") {
1713 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(device);
1714 // the inputs vector must be of size 1, but we don't want to crash here
1715 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress : String8("");
1716 }
1717
Eric Laurent322b4d22015-04-03 15:57:54 -07001718 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurent599c7582015-12-07 18:05:55 -08001719 &input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001720 &config,
1721 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001722 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001723 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001724 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001725
1726 // only accept input with the exact requested set of parameters
Eric Laurent599c7582015-12-07 18:05:55 -08001727 if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001728 (profileSamplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -08001729 !audio_formats_match(profileFormat, config.format) ||
Andy Hungf129b032015-04-07 13:45:50 -07001730 (profileChannelMask != config.channel_mask)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001731 ALOGW("getInputForAttr() failed opening input: samplingRate %d"
1732 ", format %d, channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001733 samplingRate, format, channelMask);
Eric Laurent599c7582015-12-07 18:05:55 -08001734 if (input != AUDIO_IO_HANDLE_NONE) {
1735 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001736 }
Eric Laurent599c7582015-12-07 18:05:55 -08001737 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001738 }
1739
Eric Laurent1f2f2232014-06-02 12:01:23 -07001740 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Andy Hungf129b032015-04-07 13:45:50 -07001741 inputDesc->mSamplingRate = profileSamplingRate;
1742 inputDesc->mFormat = profileFormat;
1743 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001744 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001745 inputDesc->mPolicyMix = policyMix;
Eric Laurent599c7582015-12-07 18:05:55 -08001746 inputDesc->addAudioSession(session, audioSession);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001747
Eric Laurent599c7582015-12-07 18:05:55 -08001748 addInput(input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001749 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001750
Eric Laurent599c7582015-12-07 18:05:55 -08001751 return input;
Eric Laurente552edb2014-03-10 17:42:56 -07001752}
1753
Eric Laurentbb948092017-01-23 18:33:30 -08001754//static
1755bool AudioPolicyManager::isConcurrentSource(audio_source_t source)
1756{
1757 return (source == AUDIO_SOURCE_HOTWORD) ||
1758 (source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
1759 (source == AUDIO_SOURCE_FM_TUNER);
1760}
1761
Eric Laurentfb66dd92016-01-28 18:32:03 -08001762bool AudioPolicyManager::isConcurentCaptureAllowed(const sp<AudioInputDescriptor>& inputDesc,
1763 const sp<AudioSession>& audioSession)
1764{
1765 // Do not allow capture if an active voice call is using a software patch and
1766 // the call TX source device is on the same HW module.
1767 // FIXME: would be better to refine to only inputs whose profile connects to the
1768 // call TX device but this information is not in the audio patch
1769 if (mCallTxPatch != 0 &&
1770 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1771 return false;
1772 }
1773
1774 // starting concurrent capture is enabled if:
1775 // 1) capturing for re-routing
1776 // 2) capturing for HOTWORD source
1777 // 3) capturing for FM TUNER source
1778 // 3) All other active captures are either for re-routing or HOTWORD
1779
1780 if (is_virtual_input_device(inputDesc->mDevice) ||
Eric Laurentbb948092017-01-23 18:33:30 -08001781 isConcurrentSource(audioSession->inputSource())) {
Eric Laurentfb66dd92016-01-28 18:32:03 -08001782 return true;
1783 }
1784
1785 Vector< sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
1786 for (size_t i = 0; i < activeInputs.size(); i++) {
1787 sp<AudioInputDescriptor> activeInput = activeInputs[i];
Eric Laurentbb948092017-01-23 18:33:30 -08001788 if (!isConcurrentSource(activeInput->inputSource(true)) &&
Eric Laurentfb66dd92016-01-28 18:32:03 -08001789 !is_virtual_input_device(activeInput->mDevice)) {
1790 return false;
1791 }
1792 }
1793
1794 return true;
1795}
1796
1797
Eric Laurent4dc68062014-07-28 17:26:49 -07001798status_t AudioPolicyManager::startInput(audio_io_handle_t input,
Eric Laurentfb66dd92016-01-28 18:32:03 -08001799 audio_session_t session,
1800 concurrency_type__mask_t *concurrency)
Eric Laurente552edb2014-03-10 17:42:56 -07001801{
1802 ALOGV("startInput() input %d", input);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001803 *concurrency = API_INPUT_CONCURRENCY_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001804 ssize_t index = mInputs.indexOfKey(input);
1805 if (index < 0) {
1806 ALOGW("startInput() unknown input %d", input);
1807 return BAD_VALUE;
1808 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001809 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001810
Eric Laurent599c7582015-12-07 18:05:55 -08001811 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1812 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07001813 ALOGW("startInput() unknown session %d on input %d", session, input);
1814 return BAD_VALUE;
1815 }
1816
Eric Laurent74708e72017-04-07 17:13:42 -07001817// FIXME: disable concurrent capture until UI is ready
1818#if 0
Eric Laurentfb66dd92016-01-28 18:32:03 -08001819 if (!isConcurentCaptureAllowed(inputDesc, audioSession)) {
1820 ALOGW("startInput(%d) failed: other input already started", input);
1821 return INVALID_OPERATION;
1822 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001823
Eric Laurentfb66dd92016-01-28 18:32:03 -08001824 if (isInCall()) {
1825 *concurrency |= API_INPUT_CONCURRENCY_CALL;
1826 }
Eric Laurentf7c50102016-09-30 15:19:43 -07001827 if (mInputs.activeInputsCountOnDevices() != 0) {
Eric Laurentfb66dd92016-01-28 18:32:03 -08001828 *concurrency |= API_INPUT_CONCURRENCY_CAPTURE;
Eric Laurente552edb2014-03-10 17:42:56 -07001829 }
Eric Laurent74708e72017-04-07 17:13:42 -07001830#else
1831 if (!is_virtual_input_device(inputDesc->mDevice)) {
1832 if (mCallTxPatch != 0 &&
1833 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1834 ALOGW("startInput(%d) failed: call in progress", input);
1835 return INVALID_OPERATION;
1836 }
1837
1838 Vector< sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
1839 for (size_t i = 0; i < activeInputs.size(); i++) {
1840 sp<AudioInputDescriptor> activeDesc = activeInputs[i];
1841
1842 if (is_virtual_input_device(activeDesc->mDevice)) {
1843 continue;
1844 }
1845
1846 audio_source_t activeSource = activeDesc->inputSource(true);
1847 if (audioSession->inputSource() == AUDIO_SOURCE_HOTWORD) {
1848 if (activeSource == AUDIO_SOURCE_HOTWORD) {
1849 if (activeDesc->hasPreemptedSession(session)) {
1850 ALOGW("startInput(%d) failed for HOTWORD: "
1851 "other input %d already started for HOTWORD",
1852 input, activeDesc->mIoHandle);
1853 return INVALID_OPERATION;
1854 }
1855 } else {
1856 ALOGV("startInput(%d) failed for HOTWORD: other input %d already started",
1857 input, activeDesc->mIoHandle);
1858 return INVALID_OPERATION;
1859 }
1860 } else {
1861 if (activeSource != AUDIO_SOURCE_HOTWORD) {
1862 ALOGW("startInput(%d) failed: other input %d already started",
1863 input, activeDesc->mIoHandle);
1864 return INVALID_OPERATION;
1865 }
1866 }
1867 }
1868
1869 // if capture is allowed, preempt currently active HOTWORD captures
1870 for (size_t i = 0; i < activeInputs.size(); i++) {
1871 sp<AudioInputDescriptor> activeDesc = activeInputs[i];
1872
1873 if (is_virtual_input_device(activeDesc->mDevice)) {
1874 continue;
1875 }
1876
1877 audio_source_t activeSource = activeDesc->inputSource(true);
1878 if (activeSource == AUDIO_SOURCE_HOTWORD) {
1879 AudioSessionCollection activeSessions =
1880 activeDesc->getAudioSessions(true /*activeOnly*/);
1881 audio_session_t activeSession = activeSessions.keyAt(0);
1882 audio_io_handle_t activeHandle = activeDesc->mIoHandle;
1883 SortedVector<audio_session_t> sessions = activeDesc->getPreemptedSessions();
1884 sessions.add(activeSession);
1885 inputDesc->setPreemptedSessions(sessions);
1886 stopInput(activeHandle, activeSession);
1887 releaseInput(activeHandle, activeSession);
1888 ALOGV("startInput(%d) for HOTWORD preempting HOTWORD input %d",
1889 input, activeDesc->mIoHandle);
1890 }
1891 }
1892 }
1893#endif
Eric Laurente552edb2014-03-10 17:42:56 -07001894
Eric Laurent313d1e72016-01-29 09:56:57 -08001895 // increment activity count before calling getNewInputDevice() below as only active sessions
1896 // are considered for device selection
1897 audioSession->changeActiveCount(1);
1898
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001899 // Routing?
1900 mInputRoutes.incRouteActivity(session);
1901
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001902 if (audioSession->activeCount() == 1 || mInputRoutes.hasRouteChanged(session)) {
Eric Laurent271a93e2016-09-29 14:47:06 -07001903 // indicate active capture to sound trigger service if starting capture from a mic on
1904 // primary HW module
Eric Laurentf7c50102016-09-30 15:19:43 -07001905 audio_devices_t device = getNewInputDevice(inputDesc);
Eric Laurent271a93e2016-09-29 14:47:06 -07001906 setInputDevice(input, device, true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001907
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001908 if (inputDesc->getAudioSessionCount(true/*activeOnly*/) == 1) {
1909 // if input maps to a dynamic policy with an activity listener, notify of state change
1910 if ((inputDesc->mPolicyMix != NULL)
1911 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1912 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
1913 MIX_STATE_MIXING);
Eric Laurentc722f302014-12-10 11:21:49 -08001914 }
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001915
Eric Laurentf7c50102016-09-30 15:19:43 -07001916 audio_devices_t primaryInputDevices = availablePrimaryInputDevices();
1917 if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) &&
Eric Laurent05b345e2016-11-18 12:36:27 -08001918 mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) {
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001919 SoundTrigger::setCaptureState(true);
1920 }
1921
1922 // automatically enable the remote submix output when input is started if not
1923 // used by a policy mix of type MIX_TYPE_RECORDERS
1924 // For remote submix (a virtual device), we open only one input per capture request.
1925 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1926 String8 address = String8("");
1927 if (inputDesc->mPolicyMix == NULL) {
1928 address = String8("0");
1929 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1930 address = inputDesc->mPolicyMix->mDeviceAddress;
1931 }
1932 if (address != "") {
1933 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1934 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1935 address, "remote-submix");
1936 }
Eric Laurentc722f302014-12-10 11:21:49 -08001937 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001938 }
Eric Laurente552edb2014-03-10 17:42:56 -07001939 }
1940
Eric Laurent599c7582015-12-07 18:05:55 -08001941 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Eric Laurente552edb2014-03-10 17:42:56 -07001942
Eric Laurente552edb2014-03-10 17:42:56 -07001943 return NO_ERROR;
1944}
1945
Eric Laurent4dc68062014-07-28 17:26:49 -07001946status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1947 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001948{
1949 ALOGV("stopInput() input %d", input);
1950 ssize_t index = mInputs.indexOfKey(input);
1951 if (index < 0) {
1952 ALOGW("stopInput() unknown input %d", input);
1953 return BAD_VALUE;
1954 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001955 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001956
Eric Laurent599c7582015-12-07 18:05:55 -08001957 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001958 if (index < 0) {
1959 ALOGW("stopInput() unknown session %d on input %d", session, input);
1960 return BAD_VALUE;
1961 }
1962
Eric Laurent599c7582015-12-07 18:05:55 -08001963 if (audioSession->activeCount() == 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07001964 ALOGW("stopInput() input %d already stopped", input);
1965 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001966 }
1967
Eric Laurent599c7582015-12-07 18:05:55 -08001968 audioSession->changeActiveCount(-1);
Paul McLean466dc8e2015-04-17 13:15:36 -06001969
1970 // Routing?
1971 mInputRoutes.decRouteActivity(session);
1972
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001973 if (audioSession->activeCount() == 0) {
Eric Laurent84332aa2016-01-28 22:19:18 +00001974
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001975 if (inputDesc->isActive()) {
1976 setInputDevice(input, getNewInputDevice(inputDesc), false /* force */);
1977 } else {
1978 // if input maps to a dynamic policy with an activity listener, notify of state change
1979 if ((inputDesc->mPolicyMix != NULL)
1980 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1981 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
1982 MIX_STATE_IDLE);
Eric Laurent84332aa2016-01-28 22:19:18 +00001983 }
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001984
1985 // automatically disable the remote submix output when input is stopped if not
1986 // used by a policy mix of type MIX_TYPE_RECORDERS
1987 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1988 String8 address = String8("");
1989 if (inputDesc->mPolicyMix == NULL) {
1990 address = String8("0");
1991 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1992 address = inputDesc->mPolicyMix->mDeviceAddress;
1993 }
1994 if (address != "") {
1995 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1996 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1997 address, "remote-submix");
1998 }
Eric Laurent84332aa2016-01-28 22:19:18 +00001999 }
Eric Laurent84332aa2016-01-28 22:19:18 +00002000
Eric Laurentf7c50102016-09-30 15:19:43 -07002001 audio_devices_t device = inputDesc->mDevice;
Eric Laurentfb66dd92016-01-28 18:32:03 -08002002 resetInputDevice(input);
Eric Laurent84332aa2016-01-28 22:19:18 +00002003
Eric Laurentf7c50102016-09-30 15:19:43 -07002004 // indicate inactive capture to sound trigger service if stopping capture from a mic on
2005 // primary HW module
2006 audio_devices_t primaryInputDevices = availablePrimaryInputDevices();
2007 if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) &&
2008 mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) {
Eric Laurentfb66dd92016-01-28 18:32:03 -08002009 SoundTrigger::setCaptureState(false);
2010 }
2011 inputDesc->clearPreemptedSessions();
Eric Laurent84332aa2016-01-28 22:19:18 +00002012 }
Eric Laurente552edb2014-03-10 17:42:56 -07002013 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002014 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07002015}
2016
Eric Laurent4dc68062014-07-28 17:26:49 -07002017void AudioPolicyManager::releaseInput(audio_io_handle_t input,
2018 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07002019{
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002020
Eric Laurente552edb2014-03-10 17:42:56 -07002021 ALOGV("releaseInput() %d", input);
2022 ssize_t index = mInputs.indexOfKey(input);
2023 if (index < 0) {
2024 ALOGW("releaseInput() releasing unknown input %d", input);
2025 return;
2026 }
Paul McLean466dc8e2015-04-17 13:15:36 -06002027
2028 // Routing
2029 mInputRoutes.removeRoute(session);
2030
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002031 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
2032 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07002033
Eric Laurent599c7582015-12-07 18:05:55 -08002034 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
vivek mehta587b8df2017-01-31 14:58:44 -08002035 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07002036 ALOGW("releaseInput() unknown session %d on input %d", session, input);
2037 return;
2038 }
Eric Laurent599c7582015-12-07 18:05:55 -08002039
2040 if (audioSession->openCount() == 0) {
2041 ALOGW("releaseInput() invalid open count %d on session %d",
2042 audioSession->openCount(), session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002043 return;
2044 }
Eric Laurent599c7582015-12-07 18:05:55 -08002045
2046 if (audioSession->changeOpenCount(-1) == 0) {
2047 inputDesc->removeAudioSession(session);
2048 }
2049
Jean-Michel Trivi65bfe912015-12-04 15:56:47 -08002050 if (inputDesc->getOpenRefCount() > 0) {
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002051 ALOGV("releaseInput() exit > 0");
2052 return;
2053 }
2054
Eric Laurent05b90f82014-08-27 15:32:29 -07002055 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07002056 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07002057 ALOGV("releaseInput() exit");
2058}
2059
Eric Laurentd4692962014-05-05 18:13:44 -07002060void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07002061 bool patchRemoved = false;
2062
Eric Laurentd4692962014-05-05 18:13:44 -07002063 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07002064 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08002065 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07002066 if (patch_index >= 0) {
2067 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07002068 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07002069 mAudioPatches.removeItemsAt(patch_index);
2070 patchRemoved = true;
2071 }
Eric Laurentd4692962014-05-05 18:13:44 -07002072 mpClientInterface->closeInput(mInputs.keyAt(input_index));
2073 }
2074 mInputs.clear();
Chris Thornton52785f32016-02-09 17:28:49 -08002075 SoundTrigger::setCaptureState(false);
Eric Laurent6a94d692014-05-20 11:18:06 -07002076 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07002077
2078 if (patchRemoved) {
2079 mpClientInterface->onAudioPatchListUpdate();
2080 }
Eric Laurentd4692962014-05-05 18:13:44 -07002081}
2082
Eric Laurente0720872014-03-11 09:30:41 -07002083void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07002084 int indexMin,
2085 int indexMax)
2086{
2087 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002088 mVolumeCurves->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent28d09f02016-03-08 10:43:05 -08002089
2090 // initialize other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08002091 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
2092 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002093 continue;
2094 }
2095 mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08002096 }
Eric Laurente552edb2014-03-10 17:42:56 -07002097}
2098
Eric Laurente0720872014-03-11 09:30:41 -07002099status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01002100 int index,
2101 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07002102{
2103
François Gaffied1ab2bd2015-12-02 18:20:06 +01002104 if ((index < mVolumeCurves->getVolumeIndexMin(stream)) ||
2105 (index > mVolumeCurves->getVolumeIndexMax(stream))) {
Eric Laurente552edb2014-03-10 17:42:56 -07002106 return BAD_VALUE;
2107 }
2108 if (!audio_is_output_device(device)) {
2109 return BAD_VALUE;
2110 }
2111
2112 // Force max volume if stream cannot be muted
François Gaffied1ab2bd2015-12-02 18:20:06 +01002113 if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07002114
Eric Laurent1fd372e2016-04-06 14:23:53 -07002115 ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d",
Eric Laurente552edb2014-03-10 17:42:56 -07002116 stream, device, index);
2117
Eric Laurent28d09f02016-03-08 10:43:05 -08002118 // update other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08002119 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
2120 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002121 continue;
2122 }
2123 mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index);
2124 }
Eric Laurente552edb2014-03-10 17:42:56 -07002125
Eric Laurent1fd372e2016-04-06 14:23:53 -07002126 // update volume on all outputs and streams matching the following:
2127 // - The requested stream (or a stream matching for volume control) is active on the output
2128 // - The device (or devices) selected by the strategy corresponding to this stream includes
2129 // the requested device
2130 // - For non default requested device, currently selected device on the output is either the
2131 // requested device or one of the devices selected by the strategy
Eric Laurent5a2b6292016-04-14 18:05:57 -07002132 // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if
2133 // no specific device volume value exists for currently selected device.
Eric Laurente552edb2014-03-10 17:42:56 -07002134 status_t status = NO_ERROR;
2135 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002136 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
2137 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent794fde22016-03-11 09:50:45 -08002138 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
2139 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002140 continue;
Eric Laurente552edb2014-03-10 17:42:56 -07002141 }
Eric Laurentd3926fe2016-04-15 18:10:07 -07002142 if (!(desc->isStreamActive((audio_stream_type_t)curStream) ||
2143 (isInCall() && (curStream == AUDIO_STREAM_VOICE_CALL)))) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07002144 continue;
2145 }
Eric Laurent794fde22016-03-11 09:50:45 -08002146 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurente76f29c2016-09-14 17:17:53 -07002147 audio_devices_t curStreamDevice = getDeviceForStrategy(curStrategy, false /*fromCache*/);
2148 if ((device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) &&
2149 ((curStreamDevice & device) == 0)) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07002150 continue;
2151 }
Eric Laurente76f29c2016-09-14 17:17:53 -07002152 bool applyVolume;
Eric Laurent5a2b6292016-04-14 18:05:57 -07002153 if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07002154 curStreamDevice |= device;
Eric Laurente76f29c2016-09-14 17:17:53 -07002155 applyVolume = (curDevice & curStreamDevice) != 0;
2156 } else {
2157 applyVolume = !mVolumeCurves->hasVolumeIndexForDevice(
2158 stream, Volume::getDeviceForVolume(curStreamDevice));
Eric Laurent1fd372e2016-04-06 14:23:53 -07002159 }
Eric Laurent28d09f02016-03-08 10:43:05 -08002160
Eric Laurente76f29c2016-09-14 17:17:53 -07002161 if (applyVolume) {
Eric Laurentdc462862016-07-19 12:29:53 -07002162 //FIXME: workaround for truncated touch sounds
2163 // delayed volume change for system stream to be removed when the problem is
2164 // handled by system UI
Eric Laurent28d09f02016-03-08 10:43:05 -08002165 status_t volStatus =
Eric Laurentdc462862016-07-19 12:29:53 -07002166 checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice,
2167 (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0);
Eric Laurent28d09f02016-03-08 10:43:05 -08002168 if (volStatus != NO_ERROR) {
2169 status = volStatus;
2170 }
2171 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08002172 }
Eric Laurente552edb2014-03-10 17:42:56 -07002173 }
2174 return status;
2175}
2176
Eric Laurente0720872014-03-11 09:30:41 -07002177status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07002178 int *index,
2179 audio_devices_t device)
2180{
2181 if (index == NULL) {
2182 return BAD_VALUE;
2183 }
2184 if (!audio_is_output_device(device)) {
2185 return BAD_VALUE;
2186 }
Eric Laurent5a2b6292016-04-14 18:05:57 -07002187 // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device corresponding to
Eric Laurente552edb2014-03-10 17:42:56 -07002188 // the strategy the stream belongs to.
Eric Laurent5a2b6292016-04-14 18:05:57 -07002189 if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurente552edb2014-03-10 17:42:56 -07002190 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
2191 }
François Gaffiedfd74092015-03-19 12:10:59 +01002192 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07002193
François Gaffied1ab2bd2015-12-02 18:20:06 +01002194 *index = mVolumeCurves->getVolumeIndex(stream, device);
Eric Laurente552edb2014-03-10 17:42:56 -07002195 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
2196 return NO_ERROR;
2197}
2198
Eric Laurent36829f92017-04-07 19:04:42 -07002199audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects()
Eric Laurente552edb2014-03-10 17:42:56 -07002200{
2201 // select one output among several suitable for global effects.
2202 // The priority is as follows:
2203 // 1: An offloaded output. If the effect ends up not being offloadable,
2204 // AudioFlinger will invalidate the track and the offloaded output
2205 // will be closed causing the effect to be moved to a PCM output.
2206 // 2: A deep buffer output
Eric Laurent36829f92017-04-07 19:04:42 -07002207 // 3: The primary output
2208 // 4: the first output in the list
Eric Laurente552edb2014-03-10 17:42:56 -07002209
Eric Laurent3b73df72014-03-11 09:06:29 -07002210 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07002211 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent36829f92017-04-07 19:04:42 -07002212 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
Eric Laurente552edb2014-03-10 17:42:56 -07002213
Eric Laurent36829f92017-04-07 19:04:42 -07002214 if (outputs.size() == 0) {
2215 return AUDIO_IO_HANDLE_NONE;
2216 }
Eric Laurente552edb2014-03-10 17:42:56 -07002217
Eric Laurent36829f92017-04-07 19:04:42 -07002218 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
2219 bool activeOnly = true;
2220
2221 while (output == AUDIO_IO_HANDLE_NONE) {
2222 audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE;
2223 audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE;
2224 audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE;
2225
2226 for (size_t i = 0; i < outputs.size(); i++) {
2227 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
2228 if (activeOnly && !desc->isStreamActive(AUDIO_STREAM_MUSIC)) {
2229 continue;
2230 }
2231 ALOGV("selectOutputForMusicEffects activeOnly %d outputs[%zu] flags 0x%08x",
2232 activeOnly, i, desc->mFlags);
2233 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
2234 outputOffloaded = outputs[i];
2235 }
2236 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
2237 outputDeepBuffer = outputs[i];
2238 }
2239 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) {
2240 outputPrimary = outputs[i];
2241 }
2242 }
2243 if (outputOffloaded != AUDIO_IO_HANDLE_NONE) {
2244 output = outputOffloaded;
2245 } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) {
2246 output = outputDeepBuffer;
2247 } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) {
2248 output = outputPrimary;
2249 } else {
2250 output = outputs[0];
2251 }
2252 activeOnly = false;
2253 }
2254
2255 if (output != mMusicEffectOutput) {
2256 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output);
2257 mMusicEffectOutput = output;
2258 }
2259
2260 ALOGV("selectOutputForMusicEffects selected output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -07002261 return output;
2262}
2263
Eric Laurent36829f92017-04-07 19:04:42 -07002264audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused)
2265{
2266 return selectOutputForMusicEffects();
2267}
2268
Eric Laurente0720872014-03-11 09:30:41 -07002269status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07002270 audio_io_handle_t io,
2271 uint32_t strategy,
2272 int session,
2273 int id)
2274{
2275 ssize_t index = mOutputs.indexOfKey(io);
2276 if (index < 0) {
2277 index = mInputs.indexOfKey(io);
2278 if (index < 0) {
2279 ALOGW("registerEffect() unknown io %d", io);
2280 return INVALID_OPERATION;
2281 }
2282 }
François Gaffie45ed3b02015-03-19 10:35:14 +01002283 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07002284}
2285
Eric Laurentc75307b2015-03-17 15:29:32 -07002286bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
2287{
Eric Laurent28d09f02016-03-08 10:43:05 -08002288 bool active = false;
Eric Laurent794fde22016-03-11 09:50:45 -08002289 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) {
2290 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002291 continue;
2292 }
2293 active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs);
2294 }
Eric Laurent28d09f02016-03-08 10:43:05 -08002295 return active;
Eric Laurentc75307b2015-03-17 15:29:32 -07002296}
2297
2298bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
2299{
2300 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
2301}
2302
Eric Laurente0720872014-03-11 09:30:41 -07002303bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07002304{
2305 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002306 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002307 if (inputDescriptor->isSourceActive(source)) {
Eric Laurente552edb2014-03-10 17:42:56 -07002308 return true;
2309 }
2310 }
2311 return false;
2312}
2313
Eric Laurent275e8e92014-11-30 15:14:47 -08002314// Register a list of custom mixes with their attributes and format.
2315// When a mix is registered, corresponding input and output profiles are
2316// added to the remote submix hw module. The profile contains only the
2317// parameters (sampling rate, format...) specified by the mix.
2318// The corresponding input remote submix device is also connected.
2319//
2320// When a remote submix device is connected, the address is checked to select the
2321// appropriate profile and the corresponding input or output stream is opened.
2322//
2323// When capture starts, getInputForAttr() will:
2324// - 1 look for a mix matching the address passed in attribtutes tags if any
2325// - 2 if none found, getDeviceForInputSource() will:
2326// - 2.1 look for a mix matching the attributes source
2327// - 2.2 if none found, default to device selection by policy rules
2328// At this time, the corresponding output remote submix device is also connected
2329// and active playback use cases can be transferred to this mix if needed when reconnecting
2330// after AudioTracks are invalidated
2331//
2332// When playback starts, getOutputForAttr() will:
2333// - 1 look for a mix matching the address passed in attribtutes tags if any
2334// - 2 if none found, look for a mix matching the attributes usage
2335// - 3 if none found, default to device and output selection by policy rules.
2336
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07002337status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes)
Eric Laurent275e8e92014-11-30 15:14:47 -08002338{
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002339 ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size());
2340 status_t res = NO_ERROR;
2341
2342 sp<HwModule> rSubmixModule;
2343 // examine each mix's route type
2344 for (size_t i = 0; i < mixes.size(); i++) {
2345 // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination
2346 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) {
2347 res = INVALID_OPERATION;
Eric Laurent275e8e92014-11-30 15:14:47 -08002348 break;
2349 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002350 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
2351 // Loop back through "remote submix"
2352 if (rSubmixModule == 0) {
2353 for (size_t j = 0; i < mHwModules.size(); j++) {
2354 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2355 && mHwModules[j]->mHandle != 0) {
2356 rSubmixModule = mHwModules[j];
2357 break;
2358 }
2359 }
2360 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002361
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002362 ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size());
Eric Laurent275e8e92014-11-30 15:14:47 -08002363
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002364 if (rSubmixModule == 0) {
2365 ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", i);
2366 res = INVALID_OPERATION;
2367 break;
2368 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002369
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002370 String8 address = mixes[i].mDeviceAddress;
François Gaffie036e1e92015-03-19 10:16:24 +01002371
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002372 if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) {
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002373 ALOGE(" Error registering mix %zu for address %s", i, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002374 res = INVALID_OPERATION;
2375 break;
2376 }
2377 audio_config_t outputConfig = mixes[i].mFormat;
2378 audio_config_t inputConfig = mixes[i].mFormat;
2379 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
2380 // stereo and let audio flinger do the channel conversion if needed.
2381 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2382 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
2383 rSubmixModule->addOutputProfile(address, &outputConfig,
2384 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
2385 rSubmixModule->addInputProfile(address, &inputConfig,
2386 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01002387
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002388 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
2389 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2390 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2391 address.string(), "remote-submix");
2392 } else {
2393 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2394 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2395 address.string(), "remote-submix");
2396 }
2397 } else if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002398 String8 address = mixes[i].mDeviceAddress;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002399 audio_devices_t device = mixes[i].mDeviceType;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002400 ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s",
2401 i, mixes.size(), device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002402
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002403 bool foundOutput = false;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002404 for (size_t j = 0 ; j < mOutputs.size() ; j++) {
2405 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j);
2406 sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle());
2407 if ((patch != 0) && (patch->mPatch.num_sinks != 0)
2408 && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE)
2409 && (patch->mPatch.sinks[0].ext.device.type == device)
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002410 && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(),
2411 AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002412 if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) {
2413 res = INVALID_OPERATION;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002414 } else {
2415 foundOutput = true;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002416 }
2417 break;
2418 }
2419 }
2420
2421 if (res != NO_ERROR) {
2422 ALOGE(" Error registering mix %zu for device 0x%X addr %s",
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002423 i, device, address.string());
2424 res = INVALID_OPERATION;
2425 break;
2426 } else if (!foundOutput) {
2427 ALOGE(" Output not found for mix %zu for device 0x%X addr %s",
2428 i, device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002429 res = INVALID_OPERATION;
2430 break;
2431 }
Eric Laurentc722f302014-12-10 11:21:49 -08002432 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002433 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002434 if (res != NO_ERROR) {
2435 unregisterPolicyMixes(mixes);
2436 }
2437 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002438}
2439
2440status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
2441{
Eric Laurent7b279bb2015-12-14 10:18:23 -08002442 ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002443 status_t res = NO_ERROR;
2444 sp<HwModule> rSubmixModule;
2445 // examine each mix's route type
Eric Laurent275e8e92014-11-30 15:14:47 -08002446 for (size_t i = 0; i < mixes.size(); i++) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002447 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
François Gaffie036e1e92015-03-19 10:16:24 +01002448
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002449 if (rSubmixModule == 0) {
2450 for (size_t j = 0; i < mHwModules.size(); j++) {
2451 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2452 && mHwModules[j]->mHandle != 0) {
2453 rSubmixModule = mHwModules[j];
2454 break;
2455 }
2456 }
2457 }
2458 if (rSubmixModule == 0) {
2459 res = INVALID_OPERATION;
2460 continue;
2461 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002462
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002463 String8 address = mixes[i].mDeviceAddress;
Eric Laurent275e8e92014-11-30 15:14:47 -08002464
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002465 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
2466 res = INVALID_OPERATION;
2467 continue;
2468 }
2469
2470 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
2471 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2472 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2473 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2474 address.string(), "remote-submix");
2475 }
2476 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
2477 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2478 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2479 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2480 address.string(), "remote-submix");
2481 }
2482 rSubmixModule->removeOutputProfile(address);
2483 rSubmixModule->removeInputProfile(address);
2484
2485 } if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
2486 if (mPolicyMixes.unregisterMix(mixes[i].mDeviceAddress) != NO_ERROR) {
2487 res = INVALID_OPERATION;
2488 continue;
2489 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002490 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002491 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002492 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002493}
2494
Eric Laurente552edb2014-03-10 17:42:56 -07002495
Eric Laurente0720872014-03-11 09:30:41 -07002496status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07002497{
2498 const size_t SIZE = 256;
2499 char buffer[SIZE];
2500 String8 result;
2501
2502 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
2503 result.append(buffer);
2504
Eric Laurent87ffa392015-05-22 10:32:38 -07002505 snprintf(buffer, SIZE, " Primary Output: %d\n",
2506 hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE);
Eric Laurente552edb2014-03-10 17:42:56 -07002507 result.append(buffer);
Mikhail Naganov0d6a0332016-04-19 17:12:38 -07002508 std::string stateLiteral;
2509 AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral);
2510 snprintf(buffer, SIZE, " Phone state: %s\n", stateLiteral.c_str());
Eric Laurente552edb2014-03-10 17:42:56 -07002511 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002512 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002513 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07002514 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002515 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07002516 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002517 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07002518 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002519 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07002520 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002521 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07002522 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002523 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002524 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002525 result.append(buffer);
Phil Burk09bc4612016-02-24 15:58:15 -08002526 snprintf(buffer, SIZE, " Force use for encoded surround output %d\n",
2527 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND));
2528 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002529 snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available");
2530 result.append(buffer);
Andy Hung2ddee192015-12-18 17:34:44 -08002531 snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off");
2532 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002533
François Gaffie2110e042015-03-24 08:41:51 +01002534 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07002535
François Gaffie112b0af2015-11-19 16:13:25 +01002536 mAvailableOutputDevices.dump(fd, String8("Available output"));
2537 mAvailableInputDevices.dump(fd, String8("Available input"));
François Gaffie53615e22015-03-19 09:24:12 +01002538 mHwModules.dump(fd);
2539 mOutputs.dump(fd);
2540 mInputs.dump(fd);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002541 mVolumeCurves->dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01002542 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002543 mAudioPatches.dump(fd);
Mikhail Naganov44344b02016-12-13 11:21:02 -08002544 mPolicyMixes.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07002545
2546 return NO_ERROR;
2547}
2548
2549// This function checks for the parameters which can be offloaded.
2550// This can be enhanced depending on the capability of the DSP and policy
2551// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002552bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002553{
2554 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002555 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002556 offloadInfo.sample_rate, offloadInfo.channel_mask,
2557 offloadInfo.format,
2558 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2559 offloadInfo.has_video);
2560
Andy Hung2ddee192015-12-18 17:34:44 -08002561 if (mMasterMono) {
2562 return false; // no offloading if mono is set.
2563 }
2564
Eric Laurente552edb2014-03-10 17:42:56 -07002565 // Check if offload has been disabled
2566 char propValue[PROPERTY_VALUE_MAX];
2567 if (property_get("audio.offload.disable", propValue, "0")) {
2568 if (atoi(propValue) != 0) {
2569 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2570 return false;
2571 }
2572 }
2573
2574 // Check if stream type is music, then only allow offload as of now.
2575 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2576 {
2577 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2578 return false;
2579 }
2580
2581 //TODO: enable audio offloading with video when ready
Andy Hung08945c42015-05-31 21:36:46 -07002582 const bool allowOffloadWithVideo =
2583 property_get_bool("audio.offload.video", false /* default_value */);
2584 if (offloadInfo.has_video && !allowOffloadWithVideo) {
Eric Laurente552edb2014-03-10 17:42:56 -07002585 ALOGV("isOffloadSupported: has_video == true, returning false");
2586 return false;
2587 }
2588
2589 //If duration is less than minimum value defined in property, return false
2590 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2591 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2592 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2593 return false;
2594 }
2595 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2596 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2597 return false;
2598 }
2599
2600 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2601 // creating an offloaded track and tearing it down immediately after start when audioflinger
2602 // detects there is an active non offloadable effect.
2603 // FIXME: We should check the audio session here but we do not have it in this context.
2604 // This may prevent offloading in rare situations where effects are left active by apps
2605 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002606 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002607 return false;
2608 }
2609
2610 // See if there is a profile to support this.
2611 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002612 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002613 offloadInfo.sample_rate,
2614 offloadInfo.format,
2615 offloadInfo.channel_mask,
2616 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002617 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2618 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002619}
2620
Eric Laurent6a94d692014-05-20 11:18:06 -07002621status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2622 audio_port_type_t type,
2623 unsigned int *num_ports,
2624 struct audio_port *ports,
2625 unsigned int *generation)
2626{
2627 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2628 generation == NULL) {
2629 return BAD_VALUE;
2630 }
2631 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2632 if (ports == NULL) {
2633 *num_ports = 0;
2634 }
2635
2636 size_t portsWritten = 0;
2637 size_t portsMax = *num_ports;
2638 *num_ports = 0;
2639 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002640 // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB
2641 // as they are used by stub HALs by convention
Eric Laurent6a94d692014-05-20 11:18:06 -07002642 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002643 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
2644 if (mAvailableOutputDevices[i]->type() == AUDIO_DEVICE_OUT_STUB) {
2645 continue;
2646 }
2647 if (portsWritten < portsMax) {
2648 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2649 }
2650 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002651 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002652 }
2653 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002654 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
2655 if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_STUB) {
2656 continue;
2657 }
2658 if (portsWritten < portsMax) {
2659 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2660 }
2661 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002662 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002663 }
2664 }
2665 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2666 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2667 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2668 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2669 }
2670 *num_ports += mInputs.size();
2671 }
2672 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002673 size_t numOutputs = 0;
2674 for (size_t i = 0; i < mOutputs.size(); i++) {
2675 if (!mOutputs[i]->isDuplicated()) {
2676 numOutputs++;
2677 if (portsWritten < portsMax) {
2678 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2679 }
2680 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002681 }
Eric Laurent84c70242014-06-23 08:46:27 -07002682 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002683 }
2684 }
2685 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002686 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002687 return NO_ERROR;
2688}
2689
2690status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2691{
2692 return NO_ERROR;
2693}
2694
Eric Laurent6a94d692014-05-20 11:18:06 -07002695status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2696 audio_patch_handle_t *handle,
2697 uid_t uid)
2698{
2699 ALOGV("createAudioPatch()");
2700
2701 if (handle == NULL || patch == NULL) {
2702 return BAD_VALUE;
2703 }
2704 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2705
Eric Laurent874c42872014-08-08 15:13:39 -07002706 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2707 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2708 return BAD_VALUE;
2709 }
2710 // only one source per audio patch supported for now
2711 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002712 return INVALID_OPERATION;
2713 }
Eric Laurent874c42872014-08-08 15:13:39 -07002714
2715 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002716 return INVALID_OPERATION;
2717 }
Eric Laurent874c42872014-08-08 15:13:39 -07002718 for (size_t i = 0; i < patch->num_sinks; i++) {
2719 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2720 return INVALID_OPERATION;
2721 }
2722 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002723
2724 sp<AudioPatch> patchDesc;
2725 ssize_t index = mAudioPatches.indexOfKey(*handle);
2726
Eric Laurent6a94d692014-05-20 11:18:06 -07002727 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2728 patch->sources[0].role,
2729 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002730#if LOG_NDEBUG == 0
2731 for (size_t i = 0; i < patch->num_sinks; i++) {
Eric Laurent7b279bb2015-12-14 10:18:23 -08002732 ALOGV("createAudioPatch sink %zu: id %d role %d type %d", i, patch->sinks[i].id,
Eric Laurent874c42872014-08-08 15:13:39 -07002733 patch->sinks[i].role,
2734 patch->sinks[i].type);
2735 }
2736#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002737
2738 if (index >= 0) {
2739 patchDesc = mAudioPatches.valueAt(index);
2740 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2741 mUidCached, patchDesc->mUid, uid);
2742 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2743 return INVALID_OPERATION;
2744 }
2745 } else {
Glenn Kastena13cde92016-03-28 15:26:02 -07002746 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07002747 }
2748
2749 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002750 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002751 if (outputDesc == NULL) {
2752 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2753 return BAD_VALUE;
2754 }
Eric Laurent84c70242014-06-23 08:46:27 -07002755 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2756 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002757 if (patchDesc != 0) {
2758 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2759 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2760 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2761 return BAD_VALUE;
2762 }
2763 }
Eric Laurent874c42872014-08-08 15:13:39 -07002764 DeviceVector devices;
2765 for (size_t i = 0; i < patch->num_sinks; i++) {
2766 // Only support mix to devices connection
2767 // TODO add support for mix to mix connection
2768 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2769 ALOGV("createAudioPatch() source mix but sink is not a device");
2770 return INVALID_OPERATION;
2771 }
2772 sp<DeviceDescriptor> devDesc =
2773 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2774 if (devDesc == 0) {
2775 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2776 return BAD_VALUE;
2777 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002778
François Gaffie53615e22015-03-19 09:24:12 +01002779 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002780 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002781 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002782 NULL, // updatedSamplingRate
2783 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002784 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002785 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002786 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002787 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002788 ALOGV("createAudioPatch() profile not supported for device %08x",
2789 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002790 return INVALID_OPERATION;
2791 }
2792 devices.add(devDesc);
2793 }
2794 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002795 return INVALID_OPERATION;
2796 }
Eric Laurent874c42872014-08-08 15:13:39 -07002797
Eric Laurent6a94d692014-05-20 11:18:06 -07002798 // TODO: reconfigure output format and channels here
2799 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002800 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002801 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002802 index = mAudioPatches.indexOfKey(*handle);
2803 if (index >= 0) {
2804 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2805 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2806 }
2807 patchDesc = mAudioPatches.valueAt(index);
2808 patchDesc->mUid = uid;
2809 ALOGV("createAudioPatch() success");
2810 } else {
2811 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2812 return INVALID_OPERATION;
2813 }
2814 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2815 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2816 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002817 // only one sink supported when connecting an input device to a mix
2818 if (patch->num_sinks > 1) {
2819 return INVALID_OPERATION;
2820 }
François Gaffie53615e22015-03-19 09:24:12 +01002821 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002822 if (inputDesc == NULL) {
2823 return BAD_VALUE;
2824 }
2825 if (patchDesc != 0) {
2826 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2827 return BAD_VALUE;
2828 }
2829 }
2830 sp<DeviceDescriptor> devDesc =
2831 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2832 if (devDesc == 0) {
2833 return BAD_VALUE;
2834 }
2835
François Gaffie53615e22015-03-19 09:24:12 +01002836 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002837 devDesc->mAddress,
2838 patch->sinks[0].sample_rate,
2839 NULL, /*updatedSampleRate*/
2840 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002841 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002842 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002843 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002844 // FIXME for the parameter type,
2845 // and the NONE
2846 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002847 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002848 return INVALID_OPERATION;
2849 }
2850 // TODO: reconfigure output format and channels here
2851 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002852 devDesc->type(), inputDesc->mIoHandle);
2853 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002854 index = mAudioPatches.indexOfKey(*handle);
2855 if (index >= 0) {
2856 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2857 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2858 }
2859 patchDesc = mAudioPatches.valueAt(index);
2860 patchDesc->mUid = uid;
2861 ALOGV("createAudioPatch() success");
2862 } else {
2863 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2864 return INVALID_OPERATION;
2865 }
2866 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2867 // device to device connection
2868 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002869 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002870 return BAD_VALUE;
2871 }
2872 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002873 sp<DeviceDescriptor> srcDeviceDesc =
2874 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002875 if (srcDeviceDesc == 0) {
2876 return BAD_VALUE;
2877 }
Eric Laurent874c42872014-08-08 15:13:39 -07002878
Eric Laurent6a94d692014-05-20 11:18:06 -07002879 //update source and sink with our own data as the data passed in the patch may
2880 // be incomplete.
2881 struct audio_patch newPatch = *patch;
2882 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002883
Eric Laurent874c42872014-08-08 15:13:39 -07002884 for (size_t i = 0; i < patch->num_sinks; i++) {
2885 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2886 ALOGV("createAudioPatch() source device but one sink is not a device");
2887 return INVALID_OPERATION;
2888 }
2889
2890 sp<DeviceDescriptor> sinkDeviceDesc =
2891 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2892 if (sinkDeviceDesc == 0) {
2893 return BAD_VALUE;
2894 }
2895 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2896
Eric Laurent3bcf8592015-04-03 12:13:24 -07002897 // create a software bridge in PatchPanel if:
2898 // - source and sink devices are on differnt HW modules OR
2899 // - audio HAL version is < 3.0
Eric Laurentfb66dd92016-01-28 18:32:03 -08002900 if (!srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) ||
Mikhail Naganov9ee05402016-10-13 15:58:17 -07002901 (srcDeviceDesc->mModule->getHalVersionMajor() < 3)) {
Eric Laurent3bcf8592015-04-03 12:13:24 -07002902 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002903 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002904 return INVALID_OPERATION;
2905 }
Eric Laurent874c42872014-08-08 15:13:39 -07002906 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002907 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002908 // if the sink device is reachable via an opened output stream, request to go via
2909 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002910 audio_io_handle_t output = selectOutput(outputs,
2911 AUDIO_OUTPUT_FLAG_NONE,
2912 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002913 if (output != AUDIO_IO_HANDLE_NONE) {
2914 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2915 if (outputDesc->isDuplicated()) {
2916 return INVALID_OPERATION;
2917 }
2918 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002919 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002920 newPatch.num_sources = 2;
2921 }
Eric Laurent83b88082014-06-20 18:31:16 -07002922 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002923 }
2924 // TODO: check from routing capabilities in config file and other conflicting patches
2925
2926 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2927 if (index >= 0) {
2928 afPatchHandle = patchDesc->mAfPatchHandle;
2929 }
2930
2931 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2932 &afPatchHandle,
2933 0);
2934 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2935 status, afPatchHandle);
2936 if (status == NO_ERROR) {
2937 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002938 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002939 addAudioPatch(patchDesc->mHandle, patchDesc);
2940 } else {
2941 patchDesc->mPatch = newPatch;
2942 }
2943 patchDesc->mAfPatchHandle = afPatchHandle;
2944 *handle = patchDesc->mHandle;
2945 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002946 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002947 } else {
2948 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2949 status);
2950 return INVALID_OPERATION;
2951 }
2952 } else {
2953 return BAD_VALUE;
2954 }
2955 } else {
2956 return BAD_VALUE;
2957 }
2958 return NO_ERROR;
2959}
2960
2961status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2962 uid_t uid)
2963{
2964 ALOGV("releaseAudioPatch() patch %d", handle);
2965
2966 ssize_t index = mAudioPatches.indexOfKey(handle);
2967
2968 if (index < 0) {
2969 return BAD_VALUE;
2970 }
2971 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2972 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2973 mUidCached, patchDesc->mUid, uid);
2974 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2975 return INVALID_OPERATION;
2976 }
2977
2978 struct audio_patch *patch = &patchDesc->mPatch;
2979 patchDesc->mUid = mUidCached;
2980 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002981 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002982 if (outputDesc == NULL) {
2983 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2984 return BAD_VALUE;
2985 }
2986
Eric Laurentc75307b2015-03-17 15:29:32 -07002987 setOutputDevice(outputDesc,
2988 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002989 true,
2990 0,
2991 NULL);
2992 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2993 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002994 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002995 if (inputDesc == NULL) {
2996 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2997 return BAD_VALUE;
2998 }
2999 setInputDevice(inputDesc->mIoHandle,
Eric Laurentfb66dd92016-01-28 18:32:03 -08003000 getNewInputDevice(inputDesc),
Eric Laurent6a94d692014-05-20 11:18:06 -07003001 true,
3002 NULL);
3003 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003004 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3005 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
3006 status, patchDesc->mAfPatchHandle);
3007 removeAudioPatch(patchDesc->mHandle);
3008 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07003009 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07003010 } else {
3011 return BAD_VALUE;
3012 }
3013 } else {
3014 return BAD_VALUE;
3015 }
3016 return NO_ERROR;
3017}
3018
3019status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
3020 struct audio_patch *patches,
3021 unsigned int *generation)
3022{
François Gaffie53615e22015-03-19 09:24:12 +01003023 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003024 return BAD_VALUE;
3025 }
Eric Laurent6a94d692014-05-20 11:18:06 -07003026 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01003027 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07003028}
3029
Eric Laurente1715a42014-05-20 11:30:42 -07003030status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07003031{
Eric Laurente1715a42014-05-20 11:30:42 -07003032 ALOGV("setAudioPortConfig()");
3033
3034 if (config == NULL) {
3035 return BAD_VALUE;
3036 }
3037 ALOGV("setAudioPortConfig() on port handle %d", config->id);
3038 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07003039 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
3040 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07003041 }
3042
Eric Laurenta121f902014-06-03 13:32:54 -07003043 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07003044 if (config->type == AUDIO_PORT_TYPE_MIX) {
3045 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003046 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07003047 if (outputDesc == NULL) {
3048 return BAD_VALUE;
3049 }
Eric Laurent84c70242014-06-23 08:46:27 -07003050 ALOG_ASSERT(!outputDesc->isDuplicated(),
3051 "setAudioPortConfig() called on duplicated output %d",
3052 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07003053 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07003054 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01003055 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07003056 if (inputDesc == NULL) {
3057 return BAD_VALUE;
3058 }
Eric Laurenta121f902014-06-03 13:32:54 -07003059 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07003060 } else {
3061 return BAD_VALUE;
3062 }
3063 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
3064 sp<DeviceDescriptor> deviceDesc;
3065 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
3066 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
3067 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
3068 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
3069 } else {
3070 return BAD_VALUE;
3071 }
3072 if (deviceDesc == NULL) {
3073 return BAD_VALUE;
3074 }
Eric Laurenta121f902014-06-03 13:32:54 -07003075 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07003076 } else {
3077 return BAD_VALUE;
3078 }
3079
Eric Laurenta121f902014-06-03 13:32:54 -07003080 struct audio_port_config backupConfig;
3081 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
3082 if (status == NO_ERROR) {
3083 struct audio_port_config newConfig;
3084 audioPortConfig->toAudioPortConfig(&newConfig, config);
3085 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07003086 }
Eric Laurenta121f902014-06-03 13:32:54 -07003087 if (status != NO_ERROR) {
3088 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07003089 }
Eric Laurente1715a42014-05-20 11:30:42 -07003090
3091 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07003092}
3093
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003094void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
3095{
Eric Laurentd60560a2015-04-10 11:31:20 -07003096 clearAudioSources(uid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003097 clearAudioPatches(uid);
3098 clearSessionRoutes(uid);
3099}
3100
Eric Laurent6a94d692014-05-20 11:18:06 -07003101void AudioPolicyManager::clearAudioPatches(uid_t uid)
3102{
Eric Laurent0add0fd2014-12-04 18:58:14 -08003103 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003104 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
3105 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08003106 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07003107 }
3108 }
3109}
3110
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003111void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
3112 audio_io_handle_t ouptutToSkip)
3113{
3114 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
3115 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
3116 for (size_t j = 0; j < mOutputs.size(); j++) {
3117 if (mOutputs.keyAt(j) == ouptutToSkip) {
3118 continue;
3119 }
3120 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
3121 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
3122 continue;
3123 }
3124 // If the default device for this strategy is on another output mix,
3125 // invalidate all tracks in this strategy to force re connection.
3126 // Otherwise select new device on the output mix.
3127 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
Eric Laurent794fde22016-03-11 09:50:45 -08003128 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003129 if (getStrategy((audio_stream_type_t)stream) == strategy) {
3130 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
3131 }
3132 }
3133 } else {
3134 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
3135 setOutputDevice(outputDesc, newDevice, false);
3136 }
3137 }
3138}
3139
3140void AudioPolicyManager::clearSessionRoutes(uid_t uid)
3141{
3142 // remove output routes associated with this uid
3143 SortedVector<routing_strategy> affectedStrategies;
3144 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
3145 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
3146 if (route->mUid == uid) {
3147 mOutputRoutes.removeItemsAt(i);
3148 if (route->mDeviceDescriptor != 0) {
3149 affectedStrategies.add(getStrategy(route->mStreamType));
3150 }
3151 }
3152 }
3153 // reroute outputs if necessary
3154 for (size_t i = 0; i < affectedStrategies.size(); i++) {
3155 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
3156 }
3157
3158 // remove input routes associated with this uid
3159 SortedVector<audio_source_t> affectedSources;
3160 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
3161 sp<SessionRoute> route = mInputRoutes.valueAt(i);
3162 if (route->mUid == uid) {
3163 mInputRoutes.removeItemsAt(i);
3164 if (route->mDeviceDescriptor != 0) {
3165 affectedSources.add(route->mSource);
3166 }
3167 }
3168 }
3169 // reroute inputs if necessary
3170 SortedVector<audio_io_handle_t> inputsToClose;
3171 for (size_t i = 0; i < mInputs.size(); i++) {
3172 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08003173 if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003174 inputsToClose.add(inputDesc->mIoHandle);
3175 }
3176 }
3177 for (size_t i = 0; i < inputsToClose.size(); i++) {
3178 closeInput(inputsToClose[i]);
3179 }
3180}
3181
Eric Laurentd60560a2015-04-10 11:31:20 -07003182void AudioPolicyManager::clearAudioSources(uid_t uid)
3183{
3184 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
3185 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3186 if (sourceDesc->mUid == uid) {
3187 stopAudioSource(mAudioSources.keyAt(i));
3188 }
3189 }
3190}
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003191
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07003192status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
3193 audio_io_handle_t *ioHandle,
3194 audio_devices_t *device)
3195{
Glenn Kastenf0c6d7d2016-02-26 10:44:04 -08003196 *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
3197 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
Eric Laurentc73ca6e2014-12-12 14:34:22 -08003198 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07003199
François Gaffiedf372692015-03-19 10:43:27 +01003200 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07003201}
3202
Eric Laurentd60560a2015-04-10 11:31:20 -07003203status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source,
3204 const audio_attributes_t *attributes,
Glenn Kasten559d4392016-03-29 13:42:57 -07003205 audio_patch_handle_t *handle,
Eric Laurentd60560a2015-04-10 11:31:20 -07003206 uid_t uid)
Eric Laurent554a2772015-04-10 11:29:24 -07003207{
Eric Laurentd60560a2015-04-10 11:31:20 -07003208 ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle);
3209 if (source == NULL || attributes == NULL || handle == NULL) {
3210 return BAD_VALUE;
3211 }
3212
Glenn Kasten559d4392016-03-29 13:42:57 -07003213 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentd60560a2015-04-10 11:31:20 -07003214
3215 if (source->role != AUDIO_PORT_ROLE_SOURCE ||
3216 source->type != AUDIO_PORT_TYPE_DEVICE) {
3217 ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type);
3218 return INVALID_OPERATION;
3219 }
3220
3221 sp<DeviceDescriptor> srcDeviceDesc =
3222 mAvailableInputDevices.getDevice(source->ext.device.type,
3223 String8(source->ext.device.address));
3224 if (srcDeviceDesc == 0) {
3225 ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type);
3226 return BAD_VALUE;
3227 }
3228 sp<AudioSourceDescriptor> sourceDesc =
3229 new AudioSourceDescriptor(srcDeviceDesc, attributes, uid);
3230
3231 struct audio_patch dummyPatch;
3232 sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid);
3233 sourceDesc->mPatchDesc = patchDesc;
3234
3235 status_t status = connectAudioSource(sourceDesc);
3236 if (status == NO_ERROR) {
3237 mAudioSources.add(sourceDesc->getHandle(), sourceDesc);
3238 *handle = sourceDesc->getHandle();
3239 }
3240 return status;
3241}
3242
3243status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3244{
3245 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3246
3247 // make sure we only have one patch per source.
3248 disconnectAudioSource(sourceDesc);
3249
3250 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
Eric Laurent28d09f02016-03-08 10:43:05 -08003251 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003252 sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice;
3253
3254 audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true);
3255 sp<DeviceDescriptor> sinkDeviceDesc =
3256 mAvailableOutputDevices.getDevice(sinkDevice, String8(""));
3257
3258 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3259 struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch;
3260
3261 if (srcDeviceDesc->getAudioPort()->mModule->getHandle() ==
3262 sinkDeviceDesc->getAudioPort()->mModule->getHandle() &&
Mikhail Naganov9ee05402016-10-13 15:58:17 -07003263 srcDeviceDesc->getAudioPort()->mModule->getHalVersionMajor() >= 3 &&
Eric Laurentd60560a2015-04-10 11:31:20 -07003264 srcDeviceDesc->getAudioPort()->mGains.size() > 0) {
3265 ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__);
3266 // create patch between src device and output device
3267 // create Hwoutput and add to mHwOutputs
3268 } else {
3269 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs);
3270 audio_io_handle_t output =
3271 selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID);
3272 if (output == AUDIO_IO_HANDLE_NONE) {
3273 ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice);
3274 return INVALID_OPERATION;
3275 }
3276 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
3277 if (outputDesc->isDuplicated()) {
3278 ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice);
3279 return INVALID_OPERATION;
3280 }
3281 // create a special patch with no sink and two sources:
3282 // - the second source indicates to PatchPanel through which output mix this patch should
3283 // be connected as well as the stream type for volume control
3284 // - the sink is defined by whatever output device is currently selected for the output
3285 // though which this patch is routed.
3286 patch->num_sinks = 0;
3287 patch->num_sources = 2;
3288 srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL);
3289 outputDesc->toAudioPortConfig(&patch->sources[1], NULL);
3290 patch->sources[1].ext.mix.usecase.stream = stream;
3291 status_t status = mpClientInterface->createAudioPatch(patch,
3292 &afPatchHandle,
3293 0);
3294 ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__,
3295 status, afPatchHandle);
3296 if (status != NO_ERROR) {
3297 ALOGW("%s patch panel could not connect device patch, error %d",
3298 __FUNCTION__, status);
3299 return INVALID_OPERATION;
3300 }
3301 uint32_t delayMs = 0;
Eric Laurentc40d9692016-04-13 19:14:13 -07003302 status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs);
Eric Laurentd60560a2015-04-10 11:31:20 -07003303
3304 if (status != NO_ERROR) {
3305 mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0);
3306 return status;
3307 }
3308 sourceDesc->mSwOutput = outputDesc;
3309 if (delayMs != 0) {
3310 usleep(delayMs * 1000);
3311 }
3312 }
3313
3314 sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle;
3315 addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc);
3316
3317 return NO_ERROR;
Eric Laurent554a2772015-04-10 11:29:24 -07003318}
3319
Glenn Kasten559d4392016-03-29 13:42:57 -07003320status_t AudioPolicyManager::stopAudioSource(audio_patch_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07003321{
Eric Laurentd60560a2015-04-10 11:31:20 -07003322 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle);
3323 ALOGV("%s handle %d", __FUNCTION__, handle);
3324 if (sourceDesc == 0) {
3325 ALOGW("%s unknown source for handle %d", __FUNCTION__, handle);
3326 return BAD_VALUE;
3327 }
3328 status_t status = disconnectAudioSource(sourceDesc);
3329
3330 mAudioSources.removeItem(handle);
3331 return status;
3332}
3333
Andy Hung2ddee192015-12-18 17:34:44 -08003334status_t AudioPolicyManager::setMasterMono(bool mono)
3335{
3336 if (mMasterMono == mono) {
3337 return NO_ERROR;
3338 }
3339 mMasterMono = mono;
3340 // if enabling mono we close all offloaded devices, which will invalidate the
3341 // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible
3342 // for recreating the new AudioTrack as non-offloaded PCM.
3343 //
3344 // If disabling mono, we leave all tracks as is: we don't know which clients
3345 // and tracks are able to be recreated as offloaded. The next "song" should
3346 // play back offloaded.
3347 if (mMasterMono) {
3348 Vector<audio_io_handle_t> offloaded;
3349 for (size_t i = 0; i < mOutputs.size(); ++i) {
3350 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
3351 if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
3352 offloaded.push(desc->mIoHandle);
3353 }
3354 }
3355 for (size_t i = 0; i < offloaded.size(); ++i) {
3356 closeOutput(offloaded[i]);
3357 }
3358 }
3359 // update master mono for all remaining outputs
3360 for (size_t i = 0; i < mOutputs.size(); ++i) {
3361 updateMono(mOutputs.keyAt(i));
3362 }
3363 return NO_ERROR;
3364}
3365
3366status_t AudioPolicyManager::getMasterMono(bool *mono)
3367{
3368 *mono = mMasterMono;
3369 return NO_ERROR;
3370}
3371
Eric Laurentd60560a2015-04-10 11:31:20 -07003372status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3373{
3374 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3375
3376 sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle);
3377 if (patchDesc == 0) {
3378 ALOGW("%s source has no patch with handle %d", __FUNCTION__,
3379 sourceDesc->mPatchDesc->mHandle);
3380 return BAD_VALUE;
3381 }
3382 removeAudioPatch(sourceDesc->mPatchDesc->mHandle);
3383
Eric Laurent28d09f02016-03-08 10:43:05 -08003384 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003385 sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote();
3386 if (swOutputDesc != 0) {
3387 stopSource(swOutputDesc, stream, false);
3388 mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3389 } else {
3390 sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote();
3391 if (hwOutputDesc != 0) {
3392 // release patch between src device and output device
3393 // close Hwoutput and remove from mHwOutputs
3394 } else {
3395 ALOGW("%s source has neither SW nor HW output", __FUNCTION__);
3396 }
3397 }
3398 return NO_ERROR;
3399}
3400
3401sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput(
3402 audio_io_handle_t output, routing_strategy strategy)
3403{
3404 sp<AudioSourceDescriptor> source;
3405 for (size_t i = 0; i < mAudioSources.size(); i++) {
3406 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3407 routing_strategy sourceStrategy =
3408 (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
3409 sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote();
3410 if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) {
3411 source = sourceDesc;
3412 break;
3413 }
3414 }
3415 return source;
Eric Laurent554a2772015-04-10 11:29:24 -07003416}
3417
Eric Laurente552edb2014-03-10 17:42:56 -07003418// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07003419// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07003420// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07003421uint32_t AudioPolicyManager::nextAudioPortGeneration()
3422{
3423 return android_atomic_inc(&mAudioPortGeneration);
3424}
3425
Jaekyun Seok0d4a6af2017-02-17 17:10:17 +09003426#ifdef USE_XML_AUDIO_POLICY_CONF
3427// Treblized audio policy xml config will be located in /odm/etc or /vendor/etc.
3428static const char *kConfigLocationList[] =
3429 {"/odm/etc", "/vendor/etc", "/system/etc"};
3430static const int kConfigLocationListSize =
3431 (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0]));
3432
3433static status_t deserializeAudioPolicyXmlConfig(AudioPolicyConfig &config) {
3434 char audioPolicyXmlConfigFile[AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH];
3435 status_t ret;
3436
3437 for (int i = 0; i < kConfigLocationListSize; i++) {
3438 PolicySerializer serializer;
3439 snprintf(audioPolicyXmlConfigFile,
3440 sizeof(audioPolicyXmlConfigFile),
3441 "%s/%s",
3442 kConfigLocationList[i],
3443 AUDIO_POLICY_XML_CONFIG_FILE_NAME);
3444 ret = serializer.deserialize(audioPolicyXmlConfigFile, config);
3445 if (ret == NO_ERROR) {
3446 break;
3447 }
3448 }
3449 return ret;
3450}
3451#endif
3452
Eric Laurente0720872014-03-11 09:30:41 -07003453AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07003454 :
3455#ifdef AUDIO_POLICY_TEST
3456 Thread(false),
3457#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07003458 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07003459 mA2dpSuspended(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003460 mAudioPortGeneration(1),
3461 mBeaconMuteRefCount(0),
3462 mBeaconPlayingRefCount(0),
Eric Laurent9459fb02015-08-12 18:36:32 -07003463 mBeaconMuted(false),
Andy Hung2ddee192015-12-18 17:34:44 -08003464 mTtsOutputAvailable(false),
Eric Laurent36829f92017-04-07 19:04:42 -07003465 mMasterMono(false),
3466 mMusicEffectOutput(AUDIO_IO_HANDLE_NONE)
Eric Laurente552edb2014-03-10 17:42:56 -07003467{
François Gaffied1ab2bd2015-12-02 18:20:06 +01003468 mUidCached = getuid();
3469 mpClientInterface = clientInterface;
3470
3471 // TODO: remove when legacy conf file is removed. true on devices that use DRC on the
3472 // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly.
3473 // Note: remove also speaker_drc_enabled from global configuration of XML config file.
3474 bool speakerDrcEnabled = false;
3475
3476#ifdef USE_XML_AUDIO_POLICY_CONF
3477 mVolumeCurves = new VolumeCurvesCollection();
3478 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3479 mDefaultOutputDevice, speakerDrcEnabled,
3480 static_cast<VolumeCurvesCollection *>(mVolumeCurves));
Jaekyun Seok0d4a6af2017-02-17 17:10:17 +09003481 if (deserializeAudioPolicyXmlConfig(config) != NO_ERROR) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01003482#else
3483 mVolumeCurves = new StreamDescriptorCollection();
3484 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3485 mDefaultOutputDevice, speakerDrcEnabled);
3486 if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) &&
3487 (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) {
3488#endif
3489 ALOGE("could not load audio policy configuration file, setting defaults");
3490 config.setDefault();
3491 }
3492 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
3493 mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled);
3494
3495 // Once policy config has been parsed, retrieve an instance of the engine and initialize it.
François Gaffie2110e042015-03-24 08:41:51 +01003496 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
3497 if (!engineInstance) {
3498 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
3499 return;
3500 }
3501 // Retrieve the Policy Manager Interface
3502 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
3503 if (mEngine == NULL) {
3504 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
3505 return;
3506 }
3507 mEngine->setObserver(this);
3508 status_t status = mEngine->initCheck();
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07003509 (void) status;
François Gaffie2110e042015-03-24 08:41:51 +01003510 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
3511
Eric Laurent3a4311c2014-03-17 12:00:47 -07003512 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07003513 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07003514 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
3515 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07003516 for (size_t i = 0; i < mHwModules.size(); i++) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003517 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003518 if (mHwModules[i]->mHandle == 0) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003519 ALOGW("could not open HW module %s", mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003520 continue;
3521 }
3522 // open all output streams needed to access attached devices
3523 // except for direct output streams that are only opened when they are actually
3524 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07003525 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07003526 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3527 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003528 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003529
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003530 if (!outProfile->hasSupportedDevices()) {
3531 ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003532 continue;
3533 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003534 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) {
Eric Laurent9459fb02015-08-12 18:36:32 -07003535 mTtsOutputAvailable = true;
3536 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003537
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003538 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
Eric Laurentd78f1532014-09-16 16:38:20 -07003539 continue;
3540 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003541 audio_devices_t profileType = outProfile->getSupportedDevicesType();
François Gaffie53615e22015-03-19 09:24:12 +01003542 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
3543 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07003544 } else {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003545 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003546 // outputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003547 profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes);
Eric Laurente552edb2014-03-10 17:42:56 -07003548 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003549 if ((profileType & outputDeviceTypes) == 0) {
3550 continue;
3551 }
Eric Laurentc75307b2015-03-17 15:29:32 -07003552 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
3553 mpClientInterface);
Eric Laurentc40d9692016-04-13 19:14:13 -07003554 const DeviceVector &supportedDevices = outProfile->getSupportedDevices();
3555 const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType);
3556 String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress
3557 : String8("");
Eric Laurentd78f1532014-09-16 16:38:20 -07003558
3559 outputDesc->mDevice = profileType;
3560 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3561 config.sample_rate = outputDesc->mSamplingRate;
3562 config.channel_mask = outputDesc->mChannelMask;
3563 config.format = outputDesc->mFormat;
3564 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003565 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003566 &output,
3567 &config,
3568 &outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003569 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003570 &outputDesc->mLatency,
3571 outputDesc->mFlags);
3572
3573 if (status != NO_ERROR) {
3574 ALOGW("Cannot open output stream for device %08x on hw module %s",
3575 outputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003576 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003577 } else {
3578 outputDesc->mSamplingRate = config.sample_rate;
3579 outputDesc->mChannelMask = config.channel_mask;
3580 outputDesc->mFormat = config.format;
3581
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003582 for (size_t k = 0; k < supportedDevices.size(); k++) {
3583 ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003584 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08003585 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
3586 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003587 }
3588 }
3589 if (mPrimaryOutput == 0 &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003590 outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003591 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07003592 }
3593 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003594 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07003595 outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003596 true,
3597 0,
3598 NULL,
3599 address.string());
Eric Laurentd78f1532014-09-16 16:38:20 -07003600 }
Eric Laurente552edb2014-03-10 17:42:56 -07003601 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003602 // open input streams needed to access attached devices to validate
3603 // mAvailableInputDevices list
3604 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
3605 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003606 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003607
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003608 if (!inProfile->hasSupportedDevices()) {
3609 ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003610 continue;
3611 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003612 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003613 // inputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003614 audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes);
3615
Eric Laurentd78f1532014-09-16 16:38:20 -07003616 if ((profileType & inputDeviceTypes) == 0) {
3617 continue;
3618 }
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08003619 sp<AudioInputDescriptor> inputDesc =
3620 new AudioInputDescriptor(inProfile);
Eric Laurentd78f1532014-09-16 16:38:20 -07003621
Eric Laurentd78f1532014-09-16 16:38:20 -07003622 inputDesc->mDevice = profileType;
3623
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003624 // find the address
3625 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
3626 // the inputs vector must be of size 1, but we don't want to crash here
3627 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
3628 : String8("");
3629 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
3630 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
3631
Eric Laurentd78f1532014-09-16 16:38:20 -07003632 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3633 config.sample_rate = inputDesc->mSamplingRate;
3634 config.channel_mask = inputDesc->mChannelMask;
3635 config.format = inputDesc->mFormat;
3636 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003637 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003638 &input,
3639 &config,
3640 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003641 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003642 AUDIO_SOURCE_MIC,
3643 AUDIO_INPUT_FLAG_NONE);
3644
3645 if (status == NO_ERROR) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003646 const DeviceVector &supportedDevices = inProfile->getSupportedDevices();
3647 for (size_t k = 0; k < supportedDevices.size(); k++) {
3648 ssize_t index = mAvailableInputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003649 // give a valid ID to an attached device once confirmed it is reachable
Eric Laurent45aabc32015-08-06 09:11:13 -07003650 if (index >= 0) {
3651 sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index];
3652 if (!devDesc->isAttached()) {
3653 devDesc->attach(mHwModules[i]);
3654 devDesc->importAudioPort(inProfile);
3655 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003656 }
3657 }
3658 mpClientInterface->closeInput(input);
3659 } else {
3660 ALOGW("Cannot open input stream for device %08x on hw module %s",
3661 inputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003662 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003663 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003664 }
3665 }
3666 // make sure all attached devices have been allocated a unique ID
3667 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003668 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003669 ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003670 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
3671 continue;
3672 }
François Gaffie2110e042015-03-24 08:41:51 +01003673 // The device is now validated and can be appended to the available devices of the engine
3674 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
3675 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003676 i++;
3677 }
3678 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003679 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01003680 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003681 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
3682 continue;
3683 }
François Gaffie2110e042015-03-24 08:41:51 +01003684 // The device is now validated and can be appended to the available devices of the engine
3685 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
3686 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003687 i++;
3688 }
3689 // make sure default device is reachable
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003690 if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01003691 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003692 }
Eric Laurente552edb2014-03-10 17:42:56 -07003693
3694 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
3695
3696 updateDevicesAndOutputs();
3697
3698#ifdef AUDIO_POLICY_TEST
3699 if (mPrimaryOutput != 0) {
3700 AudioParameter outputCmd = AudioParameter();
3701 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003702 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07003703
3704 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
3705 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07003706 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
3707 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003708 mTestLatencyMs = 0;
3709 mCurOutput = 0;
3710 mDirectOutput = false;
3711 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3712 mTestOutputs[i] = 0;
3713 }
3714
3715 const size_t SIZE = 256;
3716 char buffer[SIZE];
3717 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
3718 run(buffer, ANDROID_PRIORITY_AUDIO);
3719 }
3720#endif //AUDIO_POLICY_TEST
3721}
3722
Eric Laurente0720872014-03-11 09:30:41 -07003723AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07003724{
3725#ifdef AUDIO_POLICY_TEST
3726 exit();
3727#endif //AUDIO_POLICY_TEST
3728 for (size_t i = 0; i < mOutputs.size(); i++) {
3729 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003730 }
3731 for (size_t i = 0; i < mInputs.size(); i++) {
3732 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003733 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003734 mAvailableOutputDevices.clear();
3735 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003736 mOutputs.clear();
3737 mInputs.clear();
3738 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07003739}
3740
Eric Laurente0720872014-03-11 09:30:41 -07003741status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07003742{
Eric Laurent87ffa392015-05-22 10:32:38 -07003743 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003744}
3745
3746#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07003747bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07003748{
3749 ALOGV("entering threadLoop()");
3750 while (!exitPending())
3751 {
3752 String8 command;
3753 int valueInt;
3754 String8 value;
3755
3756 Mutex::Autolock _l(mLock);
3757 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
3758
3759 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
3760 AudioParameter param = AudioParameter(command);
3761
3762 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
3763 valueInt != 0) {
3764 ALOGV("Test command %s received", command.string());
3765 String8 target;
3766 if (param.get(String8("target"), target) != NO_ERROR) {
3767 target = "Manager";
3768 }
3769 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
3770 param.remove(String8("test_cmd_policy_output"));
3771 mCurOutput = valueInt;
3772 }
3773 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
3774 param.remove(String8("test_cmd_policy_direct"));
3775 if (value == "false") {
3776 mDirectOutput = false;
3777 } else if (value == "true") {
3778 mDirectOutput = true;
3779 }
3780 }
3781 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
3782 param.remove(String8("test_cmd_policy_input"));
3783 mTestInput = valueInt;
3784 }
3785
3786 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
3787 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07003788 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07003789 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003790 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003791 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003792 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003793 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003794 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07003795 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003796 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07003797 if (target == "Manager") {
3798 mTestFormat = format;
3799 } else if (mTestOutputs[mCurOutput] != 0) {
3800 AudioParameter outputParam = AudioParameter();
Mikhail Naganov388360c2016-10-17 17:09:41 -07003801 outputParam.addInt(String8(AudioParameter::keyStreamSupportedFormats), format);
Eric Laurente552edb2014-03-10 17:42:56 -07003802 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3803 }
3804 }
3805 }
3806 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
3807 param.remove(String8("test_cmd_policy_channels"));
3808 int channels = 0;
3809
3810 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003811 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003812 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003813 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07003814 }
3815 if (channels != 0) {
3816 if (target == "Manager") {
3817 mTestChannels = channels;
3818 } else if (mTestOutputs[mCurOutput] != 0) {
3819 AudioParameter outputParam = AudioParameter();
Mikhail Naganov388360c2016-10-17 17:09:41 -07003820 outputParam.addInt(String8(AudioParameter::keyStreamSupportedChannels), channels);
Eric Laurente552edb2014-03-10 17:42:56 -07003821 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3822 }
3823 }
3824 }
3825 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
3826 param.remove(String8("test_cmd_policy_sampleRate"));
3827 if (valueInt >= 0 && valueInt <= 96000) {
3828 int samplingRate = valueInt;
3829 if (target == "Manager") {
3830 mTestSamplingRate = samplingRate;
3831 } else if (mTestOutputs[mCurOutput] != 0) {
3832 AudioParameter outputParam = AudioParameter();
Mikhail Naganov388360c2016-10-17 17:09:41 -07003833 outputParam.addInt(String8(AudioParameter::keyStreamSupportedSamplingRates), samplingRate);
Eric Laurente552edb2014-03-10 17:42:56 -07003834 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3835 }
3836 }
3837 }
3838
3839 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
3840 param.remove(String8("test_cmd_policy_reopen"));
3841
Eric Laurentc75307b2015-03-17 15:29:32 -07003842 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07003843
Eric Laurentc75307b2015-03-17 15:29:32 -07003844 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07003845
Eric Laurentc75307b2015-03-17 15:29:32 -07003846 removeOutput(mPrimaryOutput->mIoHandle);
3847 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
3848 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003849 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003850 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3851 config.sample_rate = outputDesc->mSamplingRate;
3852 config.channel_mask = outputDesc->mChannelMask;
3853 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07003854 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003855 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07003856 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07003857 &config,
3858 &outputDesc->mDevice,
3859 String8(""),
3860 &outputDesc->mLatency,
3861 outputDesc->mFlags);
3862 if (status != NO_ERROR) {
3863 ALOGE("Failed to reopen hardware output stream, "
3864 "samplingRate: %d, format %d, channels %d",
3865 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003866 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003867 outputDesc->mSamplingRate = config.sample_rate;
3868 outputDesc->mChannelMask = config.channel_mask;
3869 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07003870 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07003871 AudioParameter outputCmd = AudioParameter();
3872 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003873 mpClientInterface->setParameters(handle, outputCmd.toString());
3874 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07003875 }
3876 }
3877
3878
3879 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3880 }
3881 }
3882 return false;
3883}
3884
Eric Laurente0720872014-03-11 09:30:41 -07003885void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003886{
3887 {
3888 AutoMutex _l(mLock);
3889 requestExit();
3890 mWaitWorkCV.signal();
3891 }
3892 requestExitAndWait();
3893}
3894
Eric Laurente0720872014-03-11 09:30:41 -07003895int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003896{
3897 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3898 if (output == mTestOutputs[i]) return i;
3899 }
3900 return 0;
3901}
3902#endif //AUDIO_POLICY_TEST
3903
3904// ---
3905
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003906void AudioPolicyManager::addOutput(audio_io_handle_t output, const sp<SwAudioOutputDescriptor>& outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003907{
François Gaffie98cc1912015-03-18 17:52:40 +01003908 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003909 mOutputs.add(output, outputDesc);
Andy Hung2ddee192015-12-18 17:34:44 -08003910 updateMono(output); // update mono status when adding to output list
Eric Laurent36829f92017-04-07 19:04:42 -07003911 selectOutputForMusicEffects();
Eric Laurent6a94d692014-05-20 11:18:06 -07003912 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003913}
3914
François Gaffie53615e22015-03-19 09:24:12 +01003915void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3916{
3917 mOutputs.removeItem(output);
Eric Laurent36829f92017-04-07 19:04:42 -07003918 selectOutputForMusicEffects();
François Gaffie53615e22015-03-19 09:24:12 +01003919}
3920
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003921void AudioPolicyManager::addInput(audio_io_handle_t input, const sp<AudioInputDescriptor>& inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003922{
François Gaffie98cc1912015-03-18 17:52:40 +01003923 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003924 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003925 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003926}
Eric Laurente552edb2014-03-10 17:42:56 -07003927
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003928void AudioPolicyManager::findIoHandlesByAddress(const sp<SwAudioOutputDescriptor>& desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003929 const audio_devices_t device /*in*/,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003930 const String8& address /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003931 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003932 sp<DeviceDescriptor> devDesc =
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003933 desc->mProfile->getSupportedDeviceByAddress(device, address);
keunyoung3190e672014-12-30 13:00:52 -08003934 if (devDesc != 0) {
3935 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3936 desc->mIoHandle, address.string());
3937 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003938 }
3939}
3940
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003941status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003942 audio_policy_dev_state_t state,
3943 SortedVector<audio_io_handle_t>& outputs,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003944 const String8& address)
Eric Laurente552edb2014-03-10 17:42:56 -07003945{
François Gaffie53615e22015-03-19 09:24:12 +01003946 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003947 sp<SwAudioOutputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003948
3949 if (audio_device_is_digital(device)) {
3950 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003951 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003952 }
Eric Laurente552edb2014-03-10 17:42:56 -07003953
Eric Laurent3b73df72014-03-11 09:06:29 -07003954 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003955 // first list already open outputs that can be routed to this device
3956 for (size_t i = 0; i < mOutputs.size(); i++) {
3957 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003958 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003959 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003960 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3961 outputs.add(mOutputs.keyAt(i));
3962 } else {
3963 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003964 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003965 }
Eric Laurente552edb2014-03-10 17:42:56 -07003966 }
3967 }
3968 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003969 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003970 for (size_t i = 0; i < mHwModules.size(); i++)
3971 {
3972 if (mHwModules[i]->mHandle == 0) {
3973 continue;
3974 }
3975 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3976 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003977 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003978 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003979 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003980 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003981 profiles.add(profile);
3982 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3983 }
Eric Laurente552edb2014-03-10 17:42:56 -07003984 }
3985 }
3986 }
3987
Eric Laurent7b279bb2015-12-14 10:18:23 -08003988 ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size());
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003989
Eric Laurente552edb2014-03-10 17:42:56 -07003990 if (profiles.isEmpty() && outputs.isEmpty()) {
3991 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3992 return BAD_VALUE;
3993 }
3994
3995 // open outputs for matching profiles if needed. Direct outputs are also opened to
3996 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3997 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003998 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003999
4000 // nothing to do if one output is already opened for this profile
4001 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004002 for (j = 0; j < outputs.size(); j++) {
4003 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07004004 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07004005 // matching profile: save the sample rates, format and channel masks supported
4006 // by the profile in our device descriptor
Paul McLean9080a4c2015-06-18 08:24:02 -07004007 if (audio_device_is_digital(device)) {
4008 devDesc->importAudioPort(profile);
4009 }
Eric Laurente552edb2014-03-10 17:42:56 -07004010 break;
4011 }
4012 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004013 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07004014 continue;
4015 }
4016
Eric Laurent83b88082014-06-20 18:31:16 -07004017 ALOGV("opening output for device %08x with params %s profile %p",
4018 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07004019 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07004020 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07004021 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
4022 config.sample_rate = desc->mSamplingRate;
4023 config.channel_mask = desc->mChannelMask;
4024 config.format = desc->mFormat;
4025 config.offload_info.sample_rate = desc->mSamplingRate;
4026 config.offload_info.channel_mask = desc->mChannelMask;
4027 config.offload_info.format = desc->mFormat;
4028 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07004029 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07004030 &output,
4031 &config,
4032 &desc->mDevice,
4033 address,
4034 &desc->mLatency,
4035 desc->mFlags);
4036 if (status == NO_ERROR) {
4037 desc->mSamplingRate = config.sample_rate;
4038 desc->mChannelMask = config.channel_mask;
4039 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07004040
Eric Laurentd4692962014-05-05 18:13:44 -07004041 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07004042 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07004043 char *param = audio_device_address_to_parameter(device, address);
4044 mpClientInterface->setParameters(output, String8(param));
4045 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07004046 }
Phil Burk00eeb322016-03-31 12:41:00 -07004047 updateAudioProfiles(device, output, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01004048 if (!profile->hasValidAudioProfile()) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004049 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07004050 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004051 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01004052 } else if (profile->hasDynamicAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07004053 mpClientInterface->closeOutput(output);
Phil Burk702b1052016-03-02 16:38:26 -08004054 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01004055 profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004056 config.offload_info.sample_rate = config.sample_rate;
4057 config.offload_info.channel_mask = config.channel_mask;
4058 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07004059 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07004060 &output,
4061 &config,
4062 &desc->mDevice,
4063 address,
4064 &desc->mLatency,
4065 desc->mFlags);
4066 if (status == NO_ERROR) {
4067 desc->mSamplingRate = config.sample_rate;
4068 desc->mChannelMask = config.channel_mask;
4069 desc->mFormat = config.format;
4070 } else {
4071 output = AUDIO_IO_HANDLE_NONE;
4072 }
Eric Laurentd4692962014-05-05 18:13:44 -07004073 }
4074
Eric Laurentcf2c0212014-07-25 16:20:43 -07004075 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07004076 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01004077 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01004078 sp<AudioPolicyMix> policyMix;
4079 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004080 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
4081 address.string());
4082 }
François Gaffie036e1e92015-03-19 10:16:24 +01004083 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07004084 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01004085
Eric Laurent87ffa392015-05-22 10:32:38 -07004086 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
4087 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08004088 // no duplicated output for direct outputs and
4089 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07004090 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07004091
Eric Laurentd4692962014-05-05 18:13:44 -07004092 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07004093 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07004094
Eric Laurentd4692962014-05-05 18:13:44 -07004095 //TODO: configure audio effect output stage here
4096
4097 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07004098 duplicatedOutput =
4099 mpClientInterface->openDuplicateOutput(output,
4100 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004101 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07004102 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07004103 sp<SwAudioOutputDescriptor> dupOutputDesc =
4104 new SwAudioOutputDescriptor(NULL, mpClientInterface);
4105 dupOutputDesc->mOutput1 = mPrimaryOutput;
4106 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07004107 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
4108 dupOutputDesc->mFormat = desc->mFormat;
4109 dupOutputDesc->mChannelMask = desc->mChannelMask;
4110 dupOutputDesc->mLatency = desc->mLatency;
4111 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07004112 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07004113 } else {
4114 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07004115 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07004116 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01004117 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07004118 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07004119 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07004120 }
Eric Laurente552edb2014-03-10 17:42:56 -07004121 }
4122 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07004123 } else {
4124 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07004125 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07004126 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07004127 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004128 profiles.removeAt(profile_index);
4129 profile_index--;
4130 } else {
4131 outputs.add(output);
Paul McLean9080a4c2015-06-18 08:24:02 -07004132 // Load digital format info only for digital devices
4133 if (audio_device_is_digital(device)) {
4134 devDesc->importAudioPort(profile);
4135 }
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07004136
François Gaffie53615e22015-03-19 09:24:12 +01004137 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004138 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
4139 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07004140 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004141 NULL/*patch handle*/, address.string());
4142 }
Eric Laurente552edb2014-03-10 17:42:56 -07004143 ALOGV("checkOutputsForDevice(): adding output %d", output);
4144 }
4145 }
4146
4147 if (profiles.isEmpty()) {
4148 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
4149 return BAD_VALUE;
4150 }
Eric Laurentd4692962014-05-05 18:13:44 -07004151 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07004152 // check if one opened output is not needed any more after disconnecting one device
4153 for (size_t i = 0; i < mOutputs.size(); i++) {
4154 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004155 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004156 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01004157 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07004158 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08004159 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07004160 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004161 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
4162 mOutputs.keyAt(i));
4163 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004164 }
Eric Laurente552edb2014-03-10 17:42:56 -07004165 }
4166 }
Eric Laurentd4692962014-05-05 18:13:44 -07004167 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07004168 for (size_t i = 0; i < mHwModules.size(); i++)
4169 {
4170 if (mHwModules[i]->mHandle == 0) {
4171 continue;
4172 }
4173 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
4174 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004175 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004176 if (profile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07004177 ALOGV("checkOutputsForDevice(): "
4178 "clearing direct output profile %zu on module %zu", j, i);
François Gaffie112b0af2015-11-19 16:13:25 +01004179 profile->clearAudioProfiles();
Eric Laurente552edb2014-03-10 17:42:56 -07004180 }
4181 }
4182 }
4183 }
4184 return NO_ERROR;
4185}
4186
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004187status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01004188 audio_policy_dev_state_t state,
4189 SortedVector<audio_io_handle_t>& inputs,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004190 const String8& address)
Eric Laurentd4692962014-05-05 18:13:44 -07004191{
Paul McLean9080a4c2015-06-18 08:24:02 -07004192 audio_devices_t device = devDesc->type();
Eric Laurent1f2f2232014-06-02 12:01:23 -07004193 sp<AudioInputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07004194
4195 if (audio_device_is_digital(device)) {
4196 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08004197 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07004198 }
4199
Eric Laurentd4692962014-05-05 18:13:44 -07004200 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
4201 // first list already open inputs that can be routed to this device
4202 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
4203 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004204 if (desc->mProfile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07004205 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
4206 inputs.add(mInputs.keyAt(input_index));
4207 }
4208 }
4209
4210 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07004211 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07004212 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
4213 {
4214 if (mHwModules[module_idx]->mHandle == 0) {
4215 continue;
4216 }
4217 for (size_t profile_index = 0;
4218 profile_index < mHwModules[module_idx]->mInputProfiles.size();
4219 profile_index++)
4220 {
Eric Laurent275e8e92014-11-30 15:14:47 -08004221 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
4222
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004223 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01004224 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004225 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004226 profiles.add(profile);
4227 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
4228 profile_index, module_idx);
4229 }
Eric Laurentd4692962014-05-05 18:13:44 -07004230 }
4231 }
4232 }
4233
4234 if (profiles.isEmpty() && inputs.isEmpty()) {
4235 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
4236 return BAD_VALUE;
4237 }
4238
4239 // open inputs for matching profiles if needed. Direct inputs are also opened to
4240 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
4241 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
4242
Eric Laurent1c333e22014-05-20 10:48:17 -07004243 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07004244 // nothing to do if one input is already opened for this profile
4245 size_t input_index;
4246 for (input_index = 0; input_index < mInputs.size(); input_index++) {
4247 desc = mInputs.valueAt(input_index);
4248 if (desc->mProfile == profile) {
Paul McLean9080a4c2015-06-18 08:24:02 -07004249 if (audio_device_is_digital(device)) {
4250 devDesc->importAudioPort(profile);
4251 }
Eric Laurentd4692962014-05-05 18:13:44 -07004252 break;
4253 }
4254 }
4255 if (input_index != mInputs.size()) {
4256 continue;
4257 }
4258
4259 ALOGV("opening input for device 0x%X with params %s", device, address.string());
4260 desc = new AudioInputDescriptor(profile);
4261 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07004262 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
4263 config.sample_rate = desc->mSamplingRate;
4264 config.channel_mask = desc->mChannelMask;
4265 config.format = desc->mFormat;
4266 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07004267 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07004268 &input,
4269 &config,
4270 &desc->mDevice,
4271 address,
4272 AUDIO_SOURCE_MIC,
4273 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07004274
Eric Laurentcf2c0212014-07-25 16:20:43 -07004275 if (status == NO_ERROR) {
4276 desc->mSamplingRate = config.sample_rate;
4277 desc->mChannelMask = config.channel_mask;
4278 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07004279
Eric Laurentd4692962014-05-05 18:13:44 -07004280 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07004281 char *param = audio_device_address_to_parameter(device, address);
4282 mpClientInterface->setParameters(input, String8(param));
4283 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07004284 }
Phil Burk00eeb322016-03-31 12:41:00 -07004285 updateAudioProfiles(device, input, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01004286 if (!profile->hasValidAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07004287 ALOGW("checkInputsForDevice() direct input missing param");
4288 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004289 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07004290 }
4291
4292 if (input != 0) {
4293 addInput(input, desc);
4294 }
4295 } // endif input != 0
4296
Eric Laurentcf2c0212014-07-25 16:20:43 -07004297 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07004298 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07004299 profiles.removeAt(profile_index);
4300 profile_index--;
4301 } else {
4302 inputs.add(input);
Paul McLean9080a4c2015-06-18 08:24:02 -07004303 if (audio_device_is_digital(device)) {
4304 devDesc->importAudioPort(profile);
4305 }
Eric Laurentd4692962014-05-05 18:13:44 -07004306 ALOGV("checkInputsForDevice(): adding input %d", input);
4307 }
4308 } // end scan profiles
4309
4310 if (profiles.isEmpty()) {
4311 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
4312 return BAD_VALUE;
4313 }
4314 } else {
4315 // Disconnect
4316 // check if one opened input is not needed any more after disconnecting one device
4317 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
4318 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004319 if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) {
Eric Laurentd4692962014-05-05 18:13:44 -07004320 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
4321 mInputs.keyAt(input_index));
4322 inputs.add(mInputs.keyAt(input_index));
4323 }
4324 }
4325 // Clear any profiles associated with the disconnected device.
4326 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
4327 if (mHwModules[module_index]->mHandle == 0) {
4328 continue;
4329 }
4330 for (size_t profile_index = 0;
4331 profile_index < mHwModules[module_index]->mInputProfiles.size();
4332 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004333 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004334 if (profile->supportDevice(device)) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07004335 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07004336 profile_index, module_index);
François Gaffie112b0af2015-11-19 16:13:25 +01004337 profile->clearAudioProfiles();
Eric Laurentd4692962014-05-05 18:13:44 -07004338 }
4339 }
4340 }
4341 } // end disconnect
4342
4343 return NO_ERROR;
4344}
4345
4346
Eric Laurente0720872014-03-11 09:30:41 -07004347void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07004348{
4349 ALOGV("closeOutput(%d)", output);
4350
Eric Laurentc75307b2015-03-17 15:29:32 -07004351 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004352 if (outputDesc == NULL) {
4353 ALOGW("closeOutput() unknown output %d", output);
4354 return;
4355 }
François Gaffie036e1e92015-03-19 10:16:24 +01004356 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08004357
Eric Laurente552edb2014-03-10 17:42:56 -07004358 // look for duplicated outputs connected to the output being removed.
4359 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004360 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07004361 if (dupOutputDesc->isDuplicated() &&
4362 (dupOutputDesc->mOutput1 == outputDesc ||
4363 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004364 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07004365 if (dupOutputDesc->mOutput1 == outputDesc) {
4366 outputDesc2 = dupOutputDesc->mOutput2;
4367 } else {
4368 outputDesc2 = dupOutputDesc->mOutput1;
4369 }
4370 // As all active tracks on duplicated output will be deleted,
4371 // and as they were also referenced on the other output, the reference
4372 // count for their stream type must be adjusted accordingly on
4373 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07004374 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07004375 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07004376 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07004377 }
4378 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
4379 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
4380
4381 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01004382 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07004383 }
4384 }
4385
Eric Laurent05b90f82014-08-27 15:32:29 -07004386 nextAudioPortGeneration();
4387
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004388 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004389 if (index >= 0) {
4390 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07004391 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07004392 mAudioPatches.removeItemsAt(index);
4393 mpClientInterface->onAudioPatchListUpdate();
4394 }
4395
Eric Laurente552edb2014-03-10 17:42:56 -07004396 AudioParameter param;
4397 param.add(String8("closing"), String8("true"));
4398 mpClientInterface->setParameters(output, param.toString());
4399
4400 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01004401 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004402 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07004403}
4404
4405void AudioPolicyManager::closeInput(audio_io_handle_t input)
4406{
4407 ALOGV("closeInput(%d)", input);
4408
4409 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
4410 if (inputDesc == NULL) {
4411 ALOGW("closeInput() unknown input %d", input);
4412 return;
4413 }
4414
Eric Laurent6a94d692014-05-20 11:18:06 -07004415 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07004416
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004417 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004418 if (index >= 0) {
4419 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07004420 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07004421 mAudioPatches.removeItemsAt(index);
4422 mpClientInterface->onAudioPatchListUpdate();
4423 }
4424
4425 mpClientInterface->closeInput(input);
4426 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07004427}
4428
Eric Laurentc75307b2015-03-17 15:29:32 -07004429SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
4430 audio_devices_t device,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004431 const SwAudioOutputCollection& openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07004432{
4433 SortedVector<audio_io_handle_t> outputs;
4434
4435 ALOGVV("getOutputsForDevice() device %04x", device);
4436 for (size_t i = 0; i < openOutputs.size(); i++) {
Eric Laurent37ddbb42016-08-10 16:19:14 -07004437 ALOGVV("output %zu isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004438 i, openOutputs.valueAt(i)->isDuplicated(),
4439 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004440 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
4441 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
4442 outputs.add(openOutputs.keyAt(i));
4443 }
4444 }
4445 return outputs;
4446}
4447
Eric Laurente0720872014-03-11 09:30:41 -07004448bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01004449 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07004450{
4451 if (outputs1.size() != outputs2.size()) {
4452 return false;
4453 }
4454 for (size_t i = 0; i < outputs1.size(); i++) {
4455 if (outputs1[i] != outputs2[i]) {
4456 return false;
4457 }
4458 }
4459 return true;
4460}
4461
Eric Laurente0720872014-03-11 09:30:41 -07004462void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07004463{
4464 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
4465 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
4466 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
4467 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
4468
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004469 // also take into account external policy-related changes: add all outputs which are
4470 // associated with policies in the "before" and "after" output vectors
4471 ALOGVV("checkOutputForStrategy(): policy related outputs");
4472 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004473 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004474 if (desc != 0 && desc->mPolicyMix != NULL) {
4475 srcOutputs.add(desc->mIoHandle);
4476 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
4477 }
4478 }
4479 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004480 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004481 if (desc != 0 && desc->mPolicyMix != NULL) {
4482 dstOutputs.add(desc->mIoHandle);
4483 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
4484 }
4485 }
4486
Eric Laurente552edb2014-03-10 17:42:56 -07004487 if (!vectorsEqual(srcOutputs,dstOutputs)) {
4488 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
4489 strategy, srcOutputs[0], dstOutputs[0]);
4490 // mute strategy while moving tracks from one output to another
4491 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004492 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01004493 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004494 setStrategyMute(strategy, true, desc);
4495 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004496 }
Eric Laurentd60560a2015-04-10 11:31:20 -07004497 sp<AudioSourceDescriptor> source =
4498 getSourceForStrategyOnOutput(srcOutputs[i], strategy);
4499 if (source != 0){
4500 connectAudioSource(source);
4501 }
Eric Laurente552edb2014-03-10 17:42:56 -07004502 }
4503
4504 // Move effects associated to this strategy from previous output to new output
4505 if (strategy == STRATEGY_MEDIA) {
Eric Laurent36829f92017-04-07 19:04:42 -07004506 selectOutputForMusicEffects();
Eric Laurente552edb2014-03-10 17:42:56 -07004507 }
4508 // Move tracks associated to this strategy from previous output to new output
Eric Laurent794fde22016-03-11 09:50:45 -08004509 for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004510 if (getStrategy((audio_stream_type_t)i) == strategy) {
4511 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07004512 }
4513 }
4514 }
4515}
4516
Eric Laurente0720872014-03-11 09:30:41 -07004517void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07004518{
François Gaffie2110e042015-03-24 08:41:51 +01004519 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004520 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004521 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01004522 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004523 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004524 checkOutputForStrategy(STRATEGY_SONIFICATION);
4525 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004526 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07004527 checkOutputForStrategy(STRATEGY_MEDIA);
4528 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004529 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07004530}
4531
Eric Laurente0720872014-03-11 09:30:41 -07004532void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07004533{
François Gaffie53615e22015-03-19 09:24:12 +01004534 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07004535 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004536 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07004537 return;
4538 }
4539
Eric Laurent3a4311c2014-03-17 12:00:47 -07004540 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08004541 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
4542 ~AUDIO_DEVICE_BIT_IN) != 0) ||
4543 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurentf732e072016-08-03 19:30:28 -07004544
4545 // if suspended, restore A2DP output if:
4546 // ((SCO device is NOT connected) ||
4547 // ((forced usage communication is NOT SCO) && (forced usage for record is NOT SCO) &&
4548 // (phone state is NOT in call) && (phone state is NOT ringing)))
Eric Laurente552edb2014-03-10 17:42:56 -07004549 //
Eric Laurentf732e072016-08-03 19:30:28 -07004550 // if not suspended, suspend A2DP output if:
4551 // (SCO device is connected) &&
4552 // ((forced usage for communication is SCO) || (forced usage for record is SCO) ||
4553 // ((phone state is in call) || (phone state is ringing)))
Eric Laurente552edb2014-03-10 17:42:56 -07004554 //
4555 if (mA2dpSuspended) {
Eric Laurentf732e072016-08-03 19:30:28 -07004556 if (!isScoConnected ||
4557 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) !=
4558 AUDIO_POLICY_FORCE_BT_SCO) &&
4559 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) !=
4560 AUDIO_POLICY_FORCE_BT_SCO) &&
4561 (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
François Gaffie2110e042015-03-24 08:41:51 +01004562 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004563
4564 mpClientInterface->restoreOutput(a2dpOutput);
4565 mA2dpSuspended = false;
4566 }
4567 } else {
Eric Laurentf732e072016-08-03 19:30:28 -07004568 if (isScoConnected &&
4569 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ==
4570 AUDIO_POLICY_FORCE_BT_SCO) ||
4571 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) ==
4572 AUDIO_POLICY_FORCE_BT_SCO) ||
4573 (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
François Gaffie2110e042015-03-24 08:41:51 +01004574 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004575
4576 mpClientInterface->suspendOutput(a2dpOutput);
4577 mA2dpSuspended = true;
4578 }
4579 }
4580}
4581
Eric Laurentc75307b2015-03-17 15:29:32 -07004582audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
4583 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004584{
4585 audio_devices_t device = AUDIO_DEVICE_NONE;
4586
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004587 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004588 if (index >= 0) {
4589 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4590 if (patchDesc->mUid != mUidCached) {
4591 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004592 outputDesc->device(), outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004593 return outputDesc->device();
4594 }
4595 }
4596
Eric Laurente552edb2014-03-10 17:42:56 -07004597 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05004598 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004599 // use device for strategy enforced audible
4600 // 2: we are in call or the strategy phone is active on the output:
4601 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05004602 // 3: the strategy for enforced audible is active but not enforced on the output:
4603 // use the device for strategy enforced audible
Eric Laurent28d09f02016-03-08 10:43:05 -08004604 // 4: the strategy sonification is active on the output:
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004605 // use device for strategy sonification
Eric Laurent28d09f02016-03-08 10:43:05 -08004606 // 5: the strategy accessibility is active on the output:
4607 // use device for strategy accessibility
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004608 // 6: the strategy "respectful" sonification is active on the output:
4609 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08004610 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004611 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08004612 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004613 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08004614 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004615 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01004616 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004617 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07004618 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
4619 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01004620 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004621 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004622 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05004623 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004624 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004625 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Eric Laurent28d09f02016-03-08 10:43:05 -08004626 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
4627 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004628 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004629 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004630 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004631 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004632 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004633 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004634 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004635 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004636 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004637 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07004638 }
4639
Eric Laurent1c333e22014-05-20 10:48:17 -07004640 ALOGV("getNewOutputDevice() selected device %x", device);
4641 return device;
4642}
4643
Eric Laurentfb66dd92016-01-28 18:32:03 -08004644audio_devices_t AudioPolicyManager::getNewInputDevice(const sp<AudioInputDescriptor>& inputDesc)
Eric Laurent1c333e22014-05-20 10:48:17 -07004645{
Eric Laurentfb66dd92016-01-28 18:32:03 -08004646 audio_devices_t device = AUDIO_DEVICE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07004647
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004648 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004649 if (index >= 0) {
4650 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4651 if (patchDesc->mUid != mUidCached) {
4652 ALOGV("getNewInputDevice() device %08x forced by patch %d",
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004653 inputDesc->mDevice, inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004654 return inputDesc->mDevice;
4655 }
4656 }
4657
Eric Laurentfb66dd92016-01-28 18:32:03 -08004658 audio_source_t source = inputDesc->getHighestPrioritySource(true /*activeOnly*/);
4659 if (isInCall()) {
4660 device = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
4661 } else if (source != AUDIO_SOURCE_DEFAULT) {
4662 device = getDeviceAndMixForInputSource(source);
4663 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004664
Eric Laurente552edb2014-03-10 17:42:56 -07004665 return device;
4666}
4667
Eric Laurent794fde22016-03-11 09:50:45 -08004668bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1,
4669 audio_stream_type_t stream2) {
Jean-Michel Trivi99bb2f92016-11-23 15:52:07 -08004670 return (stream1 == stream2);
Eric Laurent28d09f02016-03-08 10:43:05 -08004671}
4672
Eric Laurente0720872014-03-11 09:30:41 -07004673uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004674 return (uint32_t)getStrategy(stream);
4675}
4676
Eric Laurente0720872014-03-11 09:30:41 -07004677audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004678 // By checking the range of stream before calling getStrategy, we avoid
4679 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
4680 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08004681 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004682 return AUDIO_DEVICE_NONE;
4683 }
Eric Laurent28d09f02016-03-08 10:43:05 -08004684 audio_devices_t devices = AUDIO_DEVICE_NONE;
Eric Laurent794fde22016-03-11 09:50:45 -08004685 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
4686 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004687 continue;
Eric Laurent6a94d692014-05-20 11:18:06 -07004688 }
Eric Laurent794fde22016-03-11 09:50:45 -08004689 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08004690 audio_devices_t curDevices =
Eric Laurent447a87b2016-07-07 17:32:10 -07004691 getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/);
Eric Laurent28d09f02016-03-08 10:43:05 -08004692 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs);
4693 for (size_t i = 0; i < outputs.size(); i++) {
4694 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurent794fde22016-03-11 09:50:45 -08004695 if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004696 curDevices |= outputDesc->device();
4697 }
4698 }
4699 devices |= curDevices;
Eric Laurente552edb2014-03-10 17:42:56 -07004700 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05004701
4702 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
4703 and doesn't really need to.*/
4704 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
4705 devices |= AUDIO_DEVICE_OUT_SPEAKER;
4706 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4707 }
Eric Laurente552edb2014-03-10 17:42:56 -07004708 return devices;
4709}
4710
François Gaffie2110e042015-03-24 08:41:51 +01004711routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
4712{
Eric Laurent223fd5c2014-11-11 13:43:36 -08004713 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01004714 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004715}
4716
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004717uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4718 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004719 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4720 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4721 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004722 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4723 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4724 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004725 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01004726 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004727}
4728
Eric Laurente0720872014-03-11 09:30:41 -07004729void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004730 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004731 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004732 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4733 updateDevicesAndOutputs();
4734 break;
4735 default:
4736 break;
4737 }
4738}
4739
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004740uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
Eric Laurent9459fb02015-08-12 18:36:32 -07004741
4742 // skip beacon mute management if a dedicated TTS output is available
4743 if (mTtsOutputAvailable) {
4744 return 0;
4745 }
4746
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004747 switch(event) {
4748 case STARTING_OUTPUT:
4749 mBeaconMuteRefCount++;
4750 break;
4751 case STOPPING_OUTPUT:
4752 if (mBeaconMuteRefCount > 0) {
4753 mBeaconMuteRefCount--;
4754 }
4755 break;
4756 case STARTING_BEACON:
4757 mBeaconPlayingRefCount++;
4758 break;
4759 case STOPPING_BEACON:
4760 if (mBeaconPlayingRefCount > 0) {
4761 mBeaconPlayingRefCount--;
4762 }
4763 break;
4764 }
4765
4766 if (mBeaconMuteRefCount > 0) {
4767 // any playback causes beacon to be muted
4768 return setBeaconMute(true);
4769 } else {
4770 // no other playback: unmute when beacon starts playing, mute when it stops
4771 return setBeaconMute(mBeaconPlayingRefCount == 0);
4772 }
4773}
4774
4775uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4776 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4777 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4778 // keep track of muted state to avoid repeating mute/unmute operations
4779 if (mBeaconMuted != mute) {
4780 // mute/unmute AUDIO_STREAM_TTS on all outputs
4781 ALOGV("\t muting %d", mute);
4782 uint32_t maxLatency = 0;
4783 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004784 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004785 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004786 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004787 0 /*delay*/, AUDIO_DEVICE_NONE);
4788 const uint32_t latency = desc->latency() * 2;
4789 if (latency > maxLatency) {
4790 maxLatency = latency;
4791 }
4792 }
4793 mBeaconMuted = mute;
4794 return maxLatency;
4795 }
4796 return 0;
4797}
4798
Eric Laurente0720872014-03-11 09:30:41 -07004799audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004800 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004801{
Paul McLeanaa981192015-03-21 09:55:15 -07004802 // Routing
4803 // see if we have an explicit route
4804 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4805 // (getStrategy(stream)).
4806 // if the strategy from the stream type in the RouteMap is the same as the argument above,
4807 // and activity count is non-zero
4808 // the device = the device from the descriptor in the RouteMap, and exit.
4809 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4810 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
Eric Laurent28d09f02016-03-08 10:43:05 -08004811 routing_strategy routeStrategy = getStrategy(route->mStreamType);
4812 if ((routeStrategy == strategy) && route->isActive()) {
Paul McLeanaa981192015-03-21 09:55:15 -07004813 return route->mDeviceDescriptor->type();
4814 }
4815 }
4816
Eric Laurente552edb2014-03-10 17:42:56 -07004817 if (fromCache) {
4818 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4819 strategy, mDeviceForStrategy[strategy]);
4820 return mDeviceForStrategy[strategy];
4821 }
François Gaffie2110e042015-03-24 08:41:51 +01004822 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004823}
4824
Eric Laurente0720872014-03-11 09:30:41 -07004825void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004826{
4827 for (int i = 0; i < NUM_STRATEGIES; i++) {
4828 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4829 }
4830 mPreviousOutputs = mOutputs;
4831}
4832
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004833uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004834 audio_devices_t prevDevice,
4835 uint32_t delayMs)
4836{
4837 // mute/unmute strategies using an incompatible device combination
4838 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4839 // if unmuting, unmute only after the specified delay
4840 if (outputDesc->isDuplicated()) {
4841 return 0;
4842 }
4843
4844 uint32_t muteWaitMs = 0;
4845 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004846 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004847
4848 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4849 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004850 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004851 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4852 bool doMute = false;
4853
4854 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4855 doMute = true;
4856 outputDesc->mStrategyMutedByDevice[i] = true;
4857 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4858 doMute = true;
4859 outputDesc->mStrategyMutedByDevice[i] = false;
4860 }
Eric Laurent99401132014-05-07 19:48:15 -07004861 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004862 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004863 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004864 // skip output if it does not share any device with current output
4865 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4866 == AUDIO_DEVICE_NONE) {
4867 continue;
4868 }
Eric Laurent37ddbb42016-08-10 16:19:14 -07004869 ALOGVV("checkDeviceMuteStrategies() %s strategy %zu (curDevice %04x)",
Eric Laurentc75307b2015-03-17 15:29:32 -07004870 mute ? "muting" : "unmuting", i, curDevice);
4871 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004872 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004873 if (mute) {
4874 // FIXME: should not need to double latency if volume could be applied
4875 // immediately by the audioflinger mixer. We must account for the delay
4876 // between now and the next time the audioflinger thread for this output
4877 // will process a buffer (which corresponds to one buffer size,
4878 // usually 1/2 or 1/4 of the latency).
4879 if (muteWaitMs < desc->latency() * 2) {
4880 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004881 }
4882 }
4883 }
4884 }
4885 }
4886 }
4887
Eric Laurent99401132014-05-07 19:48:15 -07004888 // temporary mute output if device selection changes to avoid volume bursts due to
4889 // different per device volumes
4890 if (outputDesc->isActive() && (device != prevDevice)) {
Eric Laurentdc462862016-07-19 12:29:53 -07004891 uint32_t tempMuteWaitMs = outputDesc->latency() * 2;
4892 // temporary mute duration is conservatively set to 4 times the reported latency
4893 uint32_t tempMuteDurationMs = outputDesc->latency() * 4;
4894 if (muteWaitMs < tempMuteWaitMs) {
4895 muteWaitMs = tempMuteWaitMs;
Eric Laurent99401132014-05-07 19:48:15 -07004896 }
Eric Laurentdc462862016-07-19 12:29:53 -07004897
Eric Laurent99401132014-05-07 19:48:15 -07004898 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004899 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentdc462862016-07-19 12:29:53 -07004900 // make sure that we do not start the temporary mute period too early in case of
4901 // delayed device change
4902 setStrategyMute((routing_strategy)i, true, outputDesc, delayMs);
Eric Laurentc75307b2015-03-17 15:29:32 -07004903 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurentdc462862016-07-19 12:29:53 -07004904 delayMs + tempMuteDurationMs, device);
Eric Laurent99401132014-05-07 19:48:15 -07004905 }
4906 }
4907 }
4908
Eric Laurente552edb2014-03-10 17:42:56 -07004909 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4910 if (muteWaitMs > delayMs) {
4911 muteWaitMs -= delayMs;
4912 usleep(muteWaitMs * 1000);
4913 return muteWaitMs;
4914 }
4915 return 0;
4916}
4917
Eric Laurentc75307b2015-03-17 15:29:32 -07004918uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004919 audio_devices_t device,
4920 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004921 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004922 audio_patch_handle_t *patchHandle,
4923 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004924{
Eric Laurentc75307b2015-03-17 15:29:32 -07004925 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004926 AudioParameter param;
4927 uint32_t muteWaitMs;
4928
4929 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004930 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4931 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004932 return muteWaitMs;
4933 }
4934 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4935 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004936 if ((device != AUDIO_DEVICE_NONE) &&
4937 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004938 return 0;
4939 }
4940
4941 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004942 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004943
4944 audio_devices_t prevDevice = outputDesc->mDevice;
4945
Paul McLeanaa981192015-03-21 09:55:15 -07004946 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004947
4948 if (device != AUDIO_DEVICE_NONE) {
4949 outputDesc->mDevice = device;
4950 }
4951 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4952
4953 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004954 // the requested device is AUDIO_DEVICE_NONE
4955 // OR the requested device is the same as current device
4956 // AND force is not specified
4957 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004958 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004959 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4960 !force &&
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004961 outputDesc->getPatchHandle() != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004962 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004963 return muteWaitMs;
4964 }
4965
4966 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004967
Eric Laurente552edb2014-03-10 17:42:56 -07004968 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004969 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004970 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004971 } else {
Eric Laurentc40d9692016-04-13 19:14:13 -07004972 DeviceVector deviceList;
4973 if ((address == NULL) || (strlen(address) == 0)) {
4974 deviceList = mAvailableOutputDevices.getDevicesFromType(device);
4975 } else {
4976 deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
4977 }
4978
Eric Laurent1c333e22014-05-20 10:48:17 -07004979 if (!deviceList.isEmpty()) {
4980 struct audio_patch patch;
4981 outputDesc->toAudioPortConfig(&patch.sources[0]);
4982 patch.num_sources = 1;
4983 patch.num_sinks = 0;
4984 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4985 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004986 patch.num_sinks++;
4987 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004988 ssize_t index;
4989 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4990 index = mAudioPatches.indexOfKey(*patchHandle);
4991 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004992 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004993 }
4994 sp< AudioPatch> patchDesc;
4995 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4996 if (index >= 0) {
4997 patchDesc = mAudioPatches.valueAt(index);
4998 afPatchHandle = patchDesc->mAfPatchHandle;
4999 }
5000
Eric Laurent1c333e22014-05-20 10:48:17 -07005001 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07005002 &afPatchHandle,
5003 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07005004 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
5005 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07005006 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07005007 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07005008 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01005009 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07005010 addAudioPatch(patchDesc->mHandle, patchDesc);
5011 } else {
5012 patchDesc->mPatch = patch;
5013 }
5014 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07005015 if (patchHandle) {
5016 *patchHandle = patchDesc->mHandle;
5017 }
Jean-Michel Triviff155c62016-02-26 12:07:16 -08005018 outputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07005019 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07005020 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07005021 }
5022 }
bryant_liuf5e7e792014-08-19 20:07:05 +08005023
5024 // inform all input as well
5025 for (size_t i = 0; i < mInputs.size(); i++) {
5026 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01005027 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08005028 AudioParameter inputCmd = AudioParameter();
5029 ALOGV("%s: inform input %d of device:%d", __func__,
5030 inputDescriptor->mIoHandle, device);
5031 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
5032 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
5033 inputCmd.toString(),
5034 delayMs);
5035 }
5036 }
Eric Laurent1c333e22014-05-20 10:48:17 -07005037 }
Eric Laurente552edb2014-03-10 17:42:56 -07005038
5039 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07005040 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005041
5042 return muteWaitMs;
5043}
5044
Eric Laurentc75307b2015-03-17 15:29:32 -07005045status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07005046 int delayMs,
5047 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07005048{
Eric Laurent6a94d692014-05-20 11:18:06 -07005049 ssize_t index;
5050 if (patchHandle) {
5051 index = mAudioPatches.indexOfKey(*patchHandle);
5052 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08005053 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07005054 }
5055 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07005056 return INVALID_OPERATION;
5057 }
Eric Laurent6a94d692014-05-20 11:18:06 -07005058 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
5059 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07005060 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07005061 outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07005062 removeAudioPatch(patchDesc->mHandle);
5063 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07005064 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07005065 return status;
5066}
5067
5068status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
5069 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07005070 bool force,
5071 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07005072{
5073 status_t status = NO_ERROR;
5074
Eric Laurent1f2f2232014-06-02 12:01:23 -07005075 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07005076 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
5077 inputDesc->mDevice = device;
5078
5079 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
5080 if (!deviceList.isEmpty()) {
5081 struct audio_patch patch;
5082 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07005083 // AUDIO_SOURCE_HOTWORD is for internal use only:
5084 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07005085 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
Eric Laurent599c7582015-12-07 18:05:55 -08005086 !inputDesc->isSoundTrigger()) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07005087 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
5088 }
Eric Laurent1c333e22014-05-20 10:48:17 -07005089 patch.num_sinks = 1;
5090 //only one input device for now
5091 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07005092 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07005093 ssize_t index;
5094 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
5095 index = mAudioPatches.indexOfKey(*patchHandle);
5096 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08005097 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07005098 }
5099 sp< AudioPatch> patchDesc;
5100 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
5101 if (index >= 0) {
5102 patchDesc = mAudioPatches.valueAt(index);
5103 afPatchHandle = patchDesc->mAfPatchHandle;
5104 }
5105
Eric Laurent1c333e22014-05-20 10:48:17 -07005106 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07005107 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07005108 0);
5109 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07005110 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07005111 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07005112 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01005113 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07005114 addAudioPatch(patchDesc->mHandle, patchDesc);
5115 } else {
5116 patchDesc->mPatch = patch;
5117 }
5118 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07005119 if (patchHandle) {
5120 *patchHandle = patchDesc->mHandle;
5121 }
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08005122 inputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07005123 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07005124 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07005125 }
5126 }
5127 }
5128 return status;
5129}
5130
Eric Laurent6a94d692014-05-20 11:18:06 -07005131status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
5132 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07005133{
Eric Laurent1f2f2232014-06-02 12:01:23 -07005134 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07005135 ssize_t index;
5136 if (patchHandle) {
5137 index = mAudioPatches.indexOfKey(*patchHandle);
5138 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08005139 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07005140 }
5141 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07005142 return INVALID_OPERATION;
5143 }
Eric Laurent6a94d692014-05-20 11:18:06 -07005144 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
5145 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07005146 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07005147 inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07005148 removeAudioPatch(patchDesc->mHandle);
5149 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07005150 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07005151 return status;
5152}
5153
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08005154sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07005155 const String8& address,
François Gaffie53615e22015-03-19 09:24:12 +01005156 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07005157 audio_format_t& format,
5158 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01005159 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07005160{
5161 // Choose an input profile based on the requested capture parameters: select the first available
5162 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07005163 //
5164 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
5165 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07005166
5167 for (size_t i = 0; i < mHwModules.size(); i++)
5168 {
5169 if (mHwModules[i]->mHandle == 0) {
5170 continue;
5171 }
5172 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
5173 {
Eric Laurent1c333e22014-05-20 10:48:17 -07005174 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07005175 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08005176 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07005177 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07005178 format,
5179 &format /*updatedFormat*/,
5180 channelMask,
5181 &channelMask /*updatedChannelMask*/,
5182 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08005183
Eric Laurente552edb2014-03-10 17:42:56 -07005184 return profile;
5185 }
5186 }
5187 }
5188 return NULL;
5189}
5190
Eric Laurentc73ca6e2014-12-12 14:34:22 -08005191
5192audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01005193 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07005194{
François Gaffie036e1e92015-03-19 10:16:24 +01005195 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
5196 audio_devices_t selectedDeviceFromMix =
5197 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08005198
François Gaffie036e1e92015-03-19 10:16:24 +01005199 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
5200 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08005201 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08005202 return getDeviceForInputSource(inputSource);
5203}
5204
5205audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
5206{
Paul McLean466dc8e2015-04-17 13:15:36 -06005207 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
5208 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07005209 if (inputSource == route->mSource && route->isActive()) {
Paul McLean466dc8e2015-04-17 13:15:36 -06005210 return route->mDeviceDescriptor->type();
5211 }
5212 }
5213
5214 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07005215}
5216
Eric Laurente0720872014-03-11 09:30:41 -07005217float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005218 int index,
5219 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005220{
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005221 float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index);
Jean-Michel Trivi3d8b4a42016-09-14 18:37:46 -07005222
5223 // handle the case of accessibility active while a ringtone is playing: if the ringtone is much
5224 // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch
5225 // exploration of the dialer UI. In this situation, bring the accessibility volume closer to
5226 // the ringtone volume
5227 if ((stream == AUDIO_STREAM_ACCESSIBILITY)
5228 && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState())
5229 && isStreamActive(AUDIO_STREAM_RING, 0)) {
5230 const float ringVolumeDB = computeVolume(AUDIO_STREAM_RING, index, device);
5231 return ringVolumeDB - 4 > volumeDB ? ringVolumeDB - 4 : volumeDB;
5232 }
5233
Eric Laurente552edb2014-03-10 17:42:56 -07005234 // if a headset is connected, apply the following rules to ring tones and notifications
5235 // to avoid sound level bursts in user's ears:
Eric Laurent6af1c1d2016-04-14 11:20:44 -07005236 // - always attenuate notifications volume by 6dB
5237 // - attenuate ring tones volume by 6dB unless music is not playing and
5238 // speaker is part of the select devices
Eric Laurente552edb2014-03-10 17:42:56 -07005239 // - if music is playing, always limit the volume to current music volume,
5240 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07005241 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005242 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
5243 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
5244 AUDIO_DEVICE_OUT_WIRED_HEADSET |
Eric Laurent904d6322017-03-17 17:20:47 -07005245 AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
5246 AUDIO_DEVICE_OUT_USB_HEADSET)) &&
Eric Laurente552edb2014-03-10 17:42:56 -07005247 ((stream_strategy == STRATEGY_SONIFICATION)
5248 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07005249 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07005250 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01005251 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
François Gaffied1ab2bd2015-12-02 18:20:06 +01005252 mVolumeCurves->canBeMuted(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005253 // when the phone is ringing we must consider that music could have been paused just before
5254 // by the music application and behave as if music was active if the last music track was
5255 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07005256 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07005257 mLimitRingtoneVolume) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005258 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07005259 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07005260 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005261 mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC,
5262 musicDevice),
5263 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07005264 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
5265 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005266 if (volumeDB > minVolDB) {
5267 volumeDB = minVolDB;
Eric Laurentffbc80f2015-03-18 18:30:19 -07005268 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07005269 }
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005270 if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
5271 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) {
5272 // on A2DP, also ensure notification volume is not too low compared to media when
5273 // intended to be played
5274 if ((volumeDB > -96.0f) &&
5275 (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) {
5276 ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f",
5277 stream, device,
5278 volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB);
5279 volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB;
5280 }
5281 }
Eric Laurent6af1c1d2016-04-14 11:20:44 -07005282 } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) ||
5283 stream_strategy != STRATEGY_SONIFICATION) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005284 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07005285 }
5286 }
5287
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005288 return volumeDB;
Eric Laurente552edb2014-03-10 17:42:56 -07005289}
5290
Eric Laurente0720872014-03-11 09:30:41 -07005291status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005292 int index,
5293 const sp<AudioOutputDescriptor>& outputDesc,
5294 audio_devices_t device,
5295 int delayMs,
5296 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005297{
Eric Laurente552edb2014-03-10 17:42:56 -07005298 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07005299 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07005300 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07005301 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07005302 return NO_ERROR;
5303 }
François Gaffie2110e042015-03-24 08:41:51 +01005304 audio_policy_forced_cfg_t forceUseForComm =
5305 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07005306 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01005307 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
5308 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005309 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01005310 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07005311 return INVALID_OPERATION;
5312 }
5313
Eric Laurentc75307b2015-03-17 15:29:32 -07005314 if (device == AUDIO_DEVICE_NONE) {
5315 device = outputDesc->device();
5316 }
Eric Laurent275e8e92014-11-30 15:14:47 -08005317
Eric Laurentffbc80f2015-03-18 18:30:19 -07005318 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07005319 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07005320 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08005321 }
Eric Laurentc75307b2015-03-17 15:29:32 -07005322
Eric Laurentffbc80f2015-03-18 18:30:19 -07005323 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07005324
Eric Laurent3b73df72014-03-11 09:06:29 -07005325 if (stream == AUDIO_STREAM_VOICE_CALL ||
5326 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07005327 float voiceVolume;
5328 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07005329 if (stream == AUDIO_STREAM_VOICE_CALL) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005330 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005331 } else {
5332 voiceVolume = 1.0;
5333 }
5334
Eric Laurent18fba842016-03-31 14:41:26 -07005335 if (voiceVolume != mLastVoiceVolume) {
Eric Laurente552edb2014-03-10 17:42:56 -07005336 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
5337 mLastVoiceVolume = voiceVolume;
5338 }
5339 }
5340
5341 return NO_ERROR;
5342}
5343
Eric Laurentc75307b2015-03-17 15:29:32 -07005344void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
5345 audio_devices_t device,
5346 int delayMs,
5347 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005348{
Eric Laurentc75307b2015-03-17 15:29:32 -07005349 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07005350
Eric Laurent794fde22016-03-11 09:50:45 -08005351 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005352 checkAndSetVolume((audio_stream_type_t)stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005353 mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005354 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005355 device,
5356 delayMs,
5357 force);
5358 }
5359}
5360
Eric Laurente0720872014-03-11 09:30:41 -07005361void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07005362 bool on,
5363 const sp<AudioOutputDescriptor>& outputDesc,
5364 int delayMs,
5365 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005366{
Eric Laurent72249d52015-06-08 11:12:15 -07005367 ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d",
5368 strategy, on, outputDesc->getId());
Eric Laurent794fde22016-03-11 09:50:45 -08005369 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005370 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005371 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07005372 }
5373 }
5374}
5375
Eric Laurente0720872014-03-11 09:30:41 -07005376void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005377 bool on,
5378 const sp<AudioOutputDescriptor>& outputDesc,
5379 int delayMs,
5380 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005381{
Eric Laurente552edb2014-03-10 17:42:56 -07005382 if (device == AUDIO_DEVICE_NONE) {
5383 device = outputDesc->device();
5384 }
5385
Eric Laurentc75307b2015-03-17 15:29:32 -07005386 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
5387 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07005388
5389 if (on) {
5390 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005391 if (mVolumeCurves->canBeMuted(stream) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07005392 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01005393 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005394 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005395 }
5396 }
5397 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
5398 outputDesc->mMuteCount[stream]++;
5399 } else {
5400 if (outputDesc->mMuteCount[stream] == 0) {
5401 ALOGV("setStreamMute() unmuting non muted stream!");
5402 return;
5403 }
5404 if (--outputDesc->mMuteCount[stream] == 0) {
5405 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005406 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005407 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005408 device,
5409 delayMs);
5410 }
5411 }
5412}
5413
Eric Laurente0720872014-03-11 09:30:41 -07005414void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07005415 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07005416{
Eric Laurent87ffa392015-05-22 10:32:38 -07005417 if(!hasPrimaryOutput()) {
5418 return;
5419 }
5420
Eric Laurente552edb2014-03-10 17:42:56 -07005421 // if the stream pertains to sonification strategy and we are in call we must
5422 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
5423 // in the device used for phone strategy and play the tone if the selected device does not
5424 // interfere with the device used for phone strategy
5425 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
5426 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07005427 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005428 if ((stream_strategy == STRATEGY_SONIFICATION) ||
5429 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005430 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07005431 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
5432 stream, starting, outputDesc->mDevice, stateChange);
5433 if (outputDesc->mRefCount[stream]) {
5434 int muteCount = 1;
5435 if (stateChange) {
5436 muteCount = outputDesc->mRefCount[stream];
5437 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005438 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005439 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
5440 for (int i = 0; i < muteCount; i++) {
5441 setStreamMute(stream, starting, mPrimaryOutput);
5442 }
5443 } else {
5444 ALOGV("handleIncallSonification() high visibility");
5445 if (outputDesc->device() &
5446 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
5447 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
5448 for (int i = 0; i < muteCount; i++) {
5449 setStreamMute(stream, starting, mPrimaryOutput);
5450 }
5451 }
5452 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005453 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
5454 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07005455 } else {
5456 mpClientInterface->stopTone();
5457 }
5458 }
5459 }
5460 }
5461}
5462
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005463audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
5464{
5465 // flags to stream type mapping
5466 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
5467 return AUDIO_STREAM_ENFORCED_AUDIBLE;
5468 }
5469 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
5470 return AUDIO_STREAM_BLUETOOTH_SCO;
5471 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08005472 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
5473 return AUDIO_STREAM_TTS;
5474 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005475
5476 // usage to stream type mapping
5477 switch (attr->usage) {
5478 case AUDIO_USAGE_MEDIA:
5479 case AUDIO_USAGE_GAME:
Jean-Michel Trivi36867762016-12-29 12:03:28 -08005480 case AUDIO_USAGE_ASSISTANT:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005481 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5482 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08005483 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5484 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005485 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5486 return AUDIO_STREAM_SYSTEM;
5487 case AUDIO_USAGE_VOICE_COMMUNICATION:
5488 return AUDIO_STREAM_VOICE_CALL;
5489
5490 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5491 return AUDIO_STREAM_DTMF;
5492
5493 case AUDIO_USAGE_ALARM:
5494 return AUDIO_STREAM_ALARM;
5495 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5496 return AUDIO_STREAM_RING;
5497
5498 case AUDIO_USAGE_NOTIFICATION:
5499 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5500 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5501 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5502 case AUDIO_USAGE_NOTIFICATION_EVENT:
5503 return AUDIO_STREAM_NOTIFICATION;
5504
5505 case AUDIO_USAGE_UNKNOWN:
5506 default:
5507 return AUDIO_STREAM_MUSIC;
5508 }
5509}
Eric Laurente83b55d2014-11-14 10:06:21 -08005510
François Gaffie53615e22015-03-19 09:24:12 +01005511bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
5512{
Eric Laurente83b55d2014-11-14 10:06:21 -08005513 // has flags that map to a strategy?
5514 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
5515 return true;
5516 }
5517
5518 // has known usage?
5519 switch (paa->usage) {
5520 case AUDIO_USAGE_UNKNOWN:
5521 case AUDIO_USAGE_MEDIA:
5522 case AUDIO_USAGE_VOICE_COMMUNICATION:
5523 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5524 case AUDIO_USAGE_ALARM:
5525 case AUDIO_USAGE_NOTIFICATION:
5526 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5527 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5528 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5529 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5530 case AUDIO_USAGE_NOTIFICATION_EVENT:
5531 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5532 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5533 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5534 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08005535 case AUDIO_USAGE_VIRTUAL_SOURCE:
Jean-Michel Trivi36867762016-12-29 12:03:28 -08005536 case AUDIO_USAGE_ASSISTANT:
Eric Laurente83b55d2014-11-14 10:06:21 -08005537 break;
5538 default:
5539 return false;
5540 }
5541 return true;
5542}
5543
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07005544bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor>& outputDesc,
François Gaffiead3183e2015-03-18 16:55:35 +01005545 routing_strategy strategy, uint32_t inPastMs,
5546 nsecs_t sysTime) const
5547{
5548 if ((sysTime == 0) && (inPastMs != 0)) {
5549 sysTime = systemTime();
5550 }
Eric Laurent794fde22016-03-11 09:50:45 -08005551 for (int i = 0; i < (int)AUDIO_STREAM_FOR_POLICY_CNT; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01005552 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
5553 (NUM_STRATEGIES == strategy)) &&
5554 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
5555 return true;
5556 }
5557 }
5558 return false;
5559}
5560
François Gaffie2110e042015-03-24 08:41:51 +01005561audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
5562{
5563 return mEngine->getForceUse(usage);
5564}
5565
5566bool AudioPolicyManager::isInCall()
5567{
5568 return isStateInCall(mEngine->getPhoneState());
5569}
5570
5571bool AudioPolicyManager::isStateInCall(int state)
5572{
5573 return is_state_in_call(state);
5574}
5575
Eric Laurentd60560a2015-04-10 11:31:20 -07005576void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc)
5577{
5578 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
5579 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
5580 if (sourceDesc->mDevice->equals(deviceDesc)) {
5581 ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle());
5582 stopAudioSource(sourceDesc->getHandle());
5583 }
5584 }
5585
5586 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
5587 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
5588 bool release = false;
5589 for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) {
5590 const struct audio_port_config *source = &patchDesc->mPatch.sources[j];
5591 if (source->type == AUDIO_PORT_TYPE_DEVICE &&
5592 source->ext.device.type == deviceDesc->type()) {
5593 release = true;
5594 }
5595 }
5596 for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) {
5597 const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j];
5598 if (sink->type == AUDIO_PORT_TYPE_DEVICE &&
5599 sink->ext.device.type == deviceDesc->type()) {
5600 release = true;
5601 }
5602 }
5603 if (release) {
5604 ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle);
5605 releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid);
5606 }
5607 }
5608}
5609
Phil Burk09bc4612016-02-24 15:58:15 -08005610// Modify the list of surround sound formats supported.
Phil Burk0709b0a2016-03-31 12:54:57 -07005611void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) {
5612 FormatVector &formats = *formatsPtr;
Phil Burk07ac1142016-03-25 13:39:29 -07005613 // TODO Set this based on Config properties.
5614 const bool alwaysForceAC3 = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005615
5616 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5617 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
Phil Burk0709b0a2016-03-31 12:54:57 -07005618 ALOGD("%s: forced use = %d", __FUNCTION__, forceUse);
Phil Burk09bc4612016-02-24 15:58:15 -08005619
5620 // Analyze original support for various formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005621 bool supportsAC3 = false;
5622 bool supportsOtherSurround = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005623 bool supportsIEC61937 = false;
5624 for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) {
5625 audio_format_t format = formats[formatIndex];
Phil Burk09bc4612016-02-24 15:58:15 -08005626 switch (format) {
5627 case AUDIO_FORMAT_AC3:
Phil Burk07ac1142016-03-25 13:39:29 -07005628 supportsAC3 = true;
5629 break;
Phil Burk09bc4612016-02-24 15:58:15 -08005630 case AUDIO_FORMAT_E_AC3:
5631 case AUDIO_FORMAT_DTS:
5632 case AUDIO_FORMAT_DTS_HD:
Phil Burk07ac1142016-03-25 13:39:29 -07005633 supportsOtherSurround = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005634 break;
5635 case AUDIO_FORMAT_IEC61937:
5636 supportsIEC61937 = true;
5637 break;
5638 default:
5639 break;
5640 }
5641 }
Phil Burk09bc4612016-02-24 15:58:15 -08005642
5643 // Modify formats based on surround preferences.
5644 // If NEVER, remove support for surround formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005645 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5646 if (supportsAC3 || supportsOtherSurround || supportsIEC61937) {
5647 // Remove surround sound related formats.
5648 for (size_t formatIndex = 0; formatIndex < formats.size(); ) {
5649 audio_format_t format = formats[formatIndex];
5650 switch(format) {
5651 case AUDIO_FORMAT_AC3:
5652 case AUDIO_FORMAT_E_AC3:
5653 case AUDIO_FORMAT_DTS:
5654 case AUDIO_FORMAT_DTS_HD:
5655 case AUDIO_FORMAT_IEC61937:
Phil Burk07ac1142016-03-25 13:39:29 -07005656 formats.removeAt(formatIndex);
5657 break;
5658 default:
5659 formatIndex++; // keep it
5660 break;
5661 }
Phil Burk09bc4612016-02-24 15:58:15 -08005662 }
Phil Burk07ac1142016-03-25 13:39:29 -07005663 supportsAC3 = false;
5664 supportsOtherSurround = false;
5665 supportsIEC61937 = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005666 }
Phil Burk07ac1142016-03-25 13:39:29 -07005667 } else { // AUTO or ALWAYS
5668 // Most TVs support AC3 even if they do not report it in the EDID.
5669 if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS))
5670 && !supportsAC3) {
5671 formats.add(AUDIO_FORMAT_AC3);
5672 supportsAC3 = true;
5673 }
5674
5675 // If ALWAYS, add support for raw surround formats if all are missing.
5676 // This assumes that if any of these formats are reported by the HAL
5677 // then the report is valid and should not be modified.
5678 if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)
5679 && !supportsOtherSurround) {
5680 formats.add(AUDIO_FORMAT_E_AC3);
5681 formats.add(AUDIO_FORMAT_DTS);
5682 formats.add(AUDIO_FORMAT_DTS_HD);
5683 supportsOtherSurround = true;
5684 }
5685
5686 // Add support for IEC61937 if any raw surround supported.
5687 // The HAL could do this but add it here, just in case.
5688 if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) {
5689 formats.add(AUDIO_FORMAT_IEC61937);
5690 supportsIEC61937 = true;
5691 }
Phil Burk09bc4612016-02-24 15:58:15 -08005692 }
Phil Burk0709b0a2016-03-31 12:54:57 -07005693}
5694
5695// Modify the list of channel masks supported.
5696void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) {
5697 ChannelsVector &channelMasks = *channelMasksPtr;
5698 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5699 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
5700
5701 // If NEVER, then remove support for channelMasks > stereo.
5702 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5703 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) {
5704 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5705 if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) {
5706 ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask);
5707 channelMasks.removeAt(maskIndex);
5708 } else {
5709 maskIndex++;
5710 }
5711 }
5712 // If ALWAYS, then make sure we at least support 5.1
5713 } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) {
5714 bool supports5dot1 = false;
5715 // Are there any channel masks that can be considered "surround"?
5716 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); maskIndex++) {
5717 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5718 if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) {
5719 supports5dot1 = true;
5720 break;
5721 }
5722 }
5723 // If not then add 5.1 support.
5724 if (!supports5dot1) {
5725 channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1);
5726 ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__);
5727 }
Phil Burk09bc4612016-02-24 15:58:15 -08005728 }
5729}
5730
Phil Burk00eeb322016-03-31 12:41:00 -07005731void AudioPolicyManager::updateAudioProfiles(audio_devices_t device,
5732 audio_io_handle_t ioHandle,
François Gaffie112b0af2015-11-19 16:13:25 +01005733 AudioProfileVector &profiles)
5734{
5735 String8 reply;
Phil Burk0709b0a2016-03-31 12:54:57 -07005736
François Gaffie112b0af2015-11-19 16:13:25 +01005737 // Format MUST be checked first to update the list of AudioProfile
5738 if (profiles.hasDynamicFormat()) {
Mikhail Naganov388360c2016-10-17 17:09:41 -07005739 reply = mpClientInterface->getParameters(
5740 ioHandle, String8(AudioParameter::keyStreamSupportedFormats));
Phil Burk0709b0a2016-03-31 12:54:57 -07005741 ALOGV("%s: supported formats %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005742 AudioParameter repliedParameters(reply);
5743 if (repliedParameters.get(
Mikhail Naganov388360c2016-10-17 17:09:41 -07005744 String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) {
François Gaffie112b0af2015-11-19 16:13:25 +01005745 ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__);
5746 return;
5747 }
Phil Burk09bc4612016-02-24 15:58:15 -08005748 FormatVector formats = formatsFromString(reply.string());
Phil Burk00eeb322016-03-31 12:41:00 -07005749 if (device == AUDIO_DEVICE_OUT_HDMI) {
Phil Burk0709b0a2016-03-31 12:54:57 -07005750 filterSurroundFormats(&formats);
Phil Burk00eeb322016-03-31 12:41:00 -07005751 }
Phil Burk09bc4612016-02-24 15:58:15 -08005752 profiles.setFormats(formats);
François Gaffie112b0af2015-11-19 16:13:25 +01005753 }
5754 const FormatVector &supportedFormats = profiles.getSupportedFormats();
5755
Eric Laurent20eb3a42016-01-26 18:39:17 -08005756 for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) {
François Gaffie112b0af2015-11-19 16:13:25 +01005757 audio_format_t format = supportedFormats[formatIndex];
Eric Laurent20eb3a42016-01-26 18:39:17 -08005758 ChannelsVector channelMasks;
5759 SampleRateVector samplingRates;
François Gaffie112b0af2015-11-19 16:13:25 +01005760 AudioParameter requestedParameters;
Mikhail Naganov388360c2016-10-17 17:09:41 -07005761 requestedParameters.addInt(String8(AudioParameter::keyFormat), format);
François Gaffie112b0af2015-11-19 16:13:25 +01005762
5763 if (profiles.hasDynamicRateFor(format)) {
Mikhail Naganov388360c2016-10-17 17:09:41 -07005764 reply = mpClientInterface->getParameters(
5765 ioHandle,
5766 requestedParameters.toString() + ";" +
5767 AudioParameter::keyStreamSupportedSamplingRates);
François Gaffie112b0af2015-11-19 16:13:25 +01005768 ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005769 AudioParameter repliedParameters(reply);
5770 if (repliedParameters.get(
Mikhail Naganov388360c2016-10-17 17:09:41 -07005771 String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) {
Eric Laurent62e4bc52016-02-02 18:37:28 -08005772 samplingRates = samplingRatesFromString(reply.string());
François Gaffie112b0af2015-11-19 16:13:25 +01005773 }
5774 }
5775 if (profiles.hasDynamicChannelsFor(format)) {
5776 reply = mpClientInterface->getParameters(ioHandle,
5777 requestedParameters.toString() + ";" +
Mikhail Naganov388360c2016-10-17 17:09:41 -07005778 AudioParameter::keyStreamSupportedChannels);
François Gaffie112b0af2015-11-19 16:13:25 +01005779 ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005780 AudioParameter repliedParameters(reply);
5781 if (repliedParameters.get(
Mikhail Naganov388360c2016-10-17 17:09:41 -07005782 String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) {
Eric Laurent62e4bc52016-02-02 18:37:28 -08005783 channelMasks = channelMasksFromString(reply.string());
Phil Burk0709b0a2016-03-31 12:54:57 -07005784 if (device == AUDIO_DEVICE_OUT_HDMI) {
5785 filterSurroundChannelMasks(&channelMasks);
5786 }
François Gaffie112b0af2015-11-19 16:13:25 +01005787 }
5788 }
Eric Laurent20eb3a42016-01-26 18:39:17 -08005789 profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates));
François Gaffie112b0af2015-11-19 16:13:25 +01005790 }
5791}
Eric Laurentd60560a2015-04-10 11:31:20 -07005792
Eric Laurente552edb2014-03-10 17:42:56 -07005793}; // namespace android