blob: 732f711d75db7e2d2cc242ed7b25030ed980f1de [file] [log] [blame]
Eric Laurente552edb2014-03-10 17:42:56 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -070017#define LOG_TAG "APM_AudioPolicyManager"
Eric Laurente552edb2014-03-10 17:42:56 -070018//#define LOG_NDEBUG 0
19
20//#define VERY_VERBOSE_LOGGING
21#ifdef VERY_VERBOSE_LOGGING
22#define ALOGVV ALOGV
23#else
24#define ALOGVV(a...) do { } while(0)
25#endif
26
Jaekyun Seok0d4a6af2017-02-17 17:10:17 +090027#define AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH 128
28#define AUDIO_POLICY_XML_CONFIG_FILE_NAME "audio_policy_configuration.xml"
François Gaffief4ad6e52015-11-19 16:59:57 +010029
Eric Laurentd4692962014-05-05 18:13:44 -070030#include <inttypes.h>
Eric Laurente552edb2014-03-10 17:42:56 -070031#include <math.h>
Eric Laurentd4692962014-05-05 18:13:44 -070032
François Gaffie2110e042015-03-24 08:41:51 +010033#include <AudioPolicyManagerInterface.h>
34#include <AudioPolicyEngineInstance.h>
Mathias Agopian05d19b02017-02-28 16:28:19 -080035#include <cutils/atomic.h>
Eric Laurente552edb2014-03-10 17:42:56 -070036#include <cutils/properties.h>
Eric Laurentd4692962014-05-05 18:13:44 -070037#include <utils/Log.h>
Eric Laurent3b73df72014-03-11 09:06:29 -070038#include <media/AudioParameter.h>
Eric Laurente83b55d2014-11-14 10:06:21 -080039#include <media/AudioPolicyHelper.h>
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070040#include <soundtrigger/SoundTrigger.h>
Mikhail Naganovcbc8f612016-10-11 18:05:13 -070041#include <system/audio.h>
Mikhail Naganov9ee05402016-10-13 15:58:17 -070042#include <audio_policy_conf.h>
Eric Laurentd4692962014-05-05 18:13:44 -070043#include "AudioPolicyManager.h"
François Gaffied1ab2bd2015-12-02 18:20:06 +010044#ifndef USE_XML_AUDIO_POLICY_CONF
François Gaffie53615e22015-03-19 09:24:12 +010045#include <ConfigParsingUtils.h>
François Gaffied1ab2bd2015-12-02 18:20:06 +010046#include <StreamDescriptor.h>
François Gaffief4ad6e52015-11-19 16:59:57 +010047#endif
François Gaffied1ab2bd2015-12-02 18:20:06 +010048#include <Serializer.h>
François Gaffiea8ecc2c2015-11-09 16:10:40 +010049#include "TypeConverter.h"
François Gaffie53615e22015-03-19 09:24:12 +010050#include <policy.h>
Eric Laurente552edb2014-03-10 17:42:56 -070051
Eric Laurent3b73df72014-03-11 09:06:29 -070052namespace android {
Eric Laurente552edb2014-03-10 17:42:56 -070053
Eric Laurentdc462862016-07-19 12:29:53 -070054//FIXME: workaround for truncated touch sounds
55// to be removed when the problem is handled by system UI
56#define TOUCH_SOUND_FIXED_DELAY_MS 100
Eric Laurente552edb2014-03-10 17:42:56 -070057// ----------------------------------------------------------------------------
58// AudioPolicyInterface implementation
59// ----------------------------------------------------------------------------
60
Eric Laurente0720872014-03-11 09:30:41 -070061status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
Paul McLeane743a472015-01-28 11:07:31 -080062 audio_policy_dev_state_t state,
63 const char *device_address,
64 const char *device_name)
Eric Laurente552edb2014-03-10 17:42:56 -070065{
Paul McLeane743a472015-01-28 11:07:31 -080066 return setDeviceConnectionStateInt(device, state, device_address, device_name);
Eric Laurentc73ca6e2014-12-12 14:34:22 -080067}
68
François Gaffie44481e72016-04-20 07:49:57 +020069void AudioPolicyManager::broadcastDeviceConnectionState(audio_devices_t device,
70 audio_policy_dev_state_t state,
71 const String8 &device_address)
72{
73 AudioParameter param(device_address);
74 const String8 key(state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE ?
Mikhail Naganov388360c2016-10-17 17:09:41 -070075 AudioParameter::keyStreamConnect : AudioParameter::keyStreamDisconnect);
François Gaffie44481e72016-04-20 07:49:57 +020076 param.addInt(key, device);
77 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
78}
79
Eric Laurentc73ca6e2014-12-12 14:34:22 -080080status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device,
Eric Laurenta1d525f2015-01-29 13:36:45 -080081 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -080082 const char *device_address,
83 const char *device_name)
Eric Laurentc73ca6e2014-12-12 14:34:22 -080084{
Paul McLeane743a472015-01-28 11:07:31 -080085 ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
86- device, state, device_address, device_name);
Eric Laurente552edb2014-03-10 17:42:56 -070087
88 // connect/disconnect only 1 device at a time
89 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
90
François Gaffie53615e22015-03-19 09:24:12 +010091 sp<DeviceDescriptor> devDesc =
92 mHwModules.getDeviceDescriptor(device, device_address, device_name);
Paul McLeane743a472015-01-28 11:07:31 -080093
Eric Laurente552edb2014-03-10 17:42:56 -070094 // handle output devices
95 if (audio_is_output_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -070096 SortedVector <audio_io_handle_t> outputs;
97
Eric Laurent3a4311c2014-03-17 12:00:47 -070098 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
99
Eric Laurente552edb2014-03-10 17:42:56 -0700100 // save a copy of the opened output descriptors before any output is opened or closed
101 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
102 mPreviousOutputs = mOutputs;
Eric Laurente552edb2014-03-10 17:42:56 -0700103 switch (state)
104 {
105 // handle output device connection
Eric Laurent3ae5f312015-02-03 17:12:08 -0800106 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700107 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700108 ALOGW("setDeviceConnectionState() device already connected: %x", device);
109 return INVALID_OPERATION;
110 }
111 ALOGV("setDeviceConnectionState() connecting device %x", device);
112
Eric Laurente552edb2014-03-10 17:42:56 -0700113 // register new device as available
Eric Laurent3a4311c2014-03-17 12:00:47 -0700114 index = mAvailableOutputDevices.add(devDesc);
115 if (index >= 0) {
François Gaffie53615e22015-03-19 09:24:12 +0100116 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurentcf817a22014-08-04 20:36:31 -0700117 if (module == 0) {
118 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
119 device);
120 mAvailableOutputDevices.remove(devDesc);
121 return INVALID_OPERATION;
122 }
Paul McLeane743a472015-01-28 11:07:31 -0800123 mAvailableOutputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700124 } else {
125 return NO_MEMORY;
Eric Laurente552edb2014-03-10 17:42:56 -0700126 }
127
François Gaffie44481e72016-04-20 07:49:57 +0200128 // Before checking outputs, broadcast connect event to allow HAL to retrieve dynamic
129 // parameters on newly connected devices (instead of opening the outputs...)
130 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
131
Eric Laurenta1d525f2015-01-29 13:36:45 -0800132 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700133 mAvailableOutputDevices.remove(devDesc);
François Gaffie44481e72016-04-20 07:49:57 +0200134
135 broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
136 devDesc->mAddress);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700137 return INVALID_OPERATION;
138 }
François Gaffie2110e042015-03-24 08:41:51 +0100139 // Propagate device availability to Engine
140 mEngine->setDeviceConnectionState(devDesc, state);
141
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700142 // outputs should never be empty here
143 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
144 "checkOutputsForDevice() returned no outputs but status OK");
145 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
146 outputs.size());
Eric Laurent3ae5f312015-02-03 17:12:08 -0800147
Eric Laurent3ae5f312015-02-03 17:12:08 -0800148 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700149 // handle output device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700150 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700151 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700152 ALOGW("setDeviceConnectionState() device not connected: %x", device);
153 return INVALID_OPERATION;
154 }
155
Paul McLean5c477aa2014-08-20 16:47:57 -0700156 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
157
Paul McLeane743a472015-01-28 11:07:31 -0800158 // Send Disconnect to HALs
François Gaffie44481e72016-04-20 07:49:57 +0200159 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700160
Eric Laurente552edb2014-03-10 17:42:56 -0700161 // remove device from available output devices
Eric Laurent3a4311c2014-03-17 12:00:47 -0700162 mAvailableOutputDevices.remove(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700163
Eric Laurenta1d525f2015-01-29 13:36:45 -0800164 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
François Gaffie2110e042015-03-24 08:41:51 +0100165
166 // Propagate device availability to Engine
167 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurente552edb2014-03-10 17:42:56 -0700168 } break;
169
170 default:
171 ALOGE("setDeviceConnectionState() invalid state: %x", state);
172 return BAD_VALUE;
173 }
174
Eric Laurent3a4311c2014-03-17 12:00:47 -0700175 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
176 // output is suspended before any tracks are moved to it
Eric Laurente552edb2014-03-10 17:42:56 -0700177 checkA2dpSuspend();
178 checkOutputForAllStrategies();
179 // outputs must be closed after checkOutputForAllStrategies() is executed
180 if (!outputs.isEmpty()) {
181 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700182 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700183 // close unused outputs after device disconnection or direct outputs that have been
184 // opened by checkOutputsForDevice() to query dynamic parameters
Eric Laurent3b73df72014-03-11 09:06:29 -0700185 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Eric Laurente552edb2014-03-10 17:42:56 -0700186 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
187 (desc->mDirectOpenCount == 0))) {
188 closeOutput(outputs[i]);
189 }
190 }
Eric Laurent3a4311c2014-03-17 12:00:47 -0700191 // check again after closing A2DP output to reset mA2dpSuspended if needed
192 checkA2dpSuspend();
Eric Laurente552edb2014-03-10 17:42:56 -0700193 }
194
195 updateDevicesAndOutputs();
Eric Laurent87ffa392015-05-22 10:32:38 -0700196 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700197 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
198 updateCallRouting(newDevice);
199 }
Eric Laurente552edb2014-03-10 17:42:56 -0700200 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700201 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
202 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
203 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700204 // do not force device change on duplicated output because if device is 0, it will
205 // also force a device 0 for the two outputs it is duplicated to which may override
206 // a valid device selection on those outputs.
Eric Laurentc75307b2015-03-17 15:29:32 -0700207 bool force = !desc->isDuplicated()
François Gaffie53615e22015-03-19 09:24:12 +0100208 && (!device_distinguishes_on_address(device)
Eric Laurentc2730ba2014-07-20 15:47:07 -0700209 // always force when disconnecting (a non-duplicated device)
210 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Eric Laurentc75307b2015-03-17 15:29:32 -0700211 setOutputDevice(desc, newDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700212 }
Eric Laurente552edb2014-03-10 17:42:56 -0700213 }
214
Eric Laurentd60560a2015-04-10 11:31:20 -0700215 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
216 cleanUpForDevice(devDesc);
217 }
218
Eric Laurent72aa32f2014-05-30 18:51:48 -0700219 mpClientInterface->onAudioPortListUpdate();
Eric Laurentb71e58b2014-05-29 16:08:11 -0700220 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700221 } // end if is output device
222
Eric Laurente552edb2014-03-10 17:42:56 -0700223 // handle input devices
224 if (audio_is_input_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700225 SortedVector <audio_io_handle_t> inputs;
226
Eric Laurent3a4311c2014-03-17 12:00:47 -0700227 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700228 switch (state)
229 {
230 // handle input device connection
Eric Laurent3b73df72014-03-11 09:06:29 -0700231 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700232 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700233 ALOGW("setDeviceConnectionState() device already connected: %d", device);
234 return INVALID_OPERATION;
235 }
François Gaffie53615e22015-03-19 09:24:12 +0100236 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurent6a94d692014-05-20 11:18:06 -0700237 if (module == NULL) {
238 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
239 device);
240 return INVALID_OPERATION;
241 }
François Gaffie44481e72016-04-20 07:49:57 +0200242
243 // Before checking intputs, broadcast connect event to allow HAL to retrieve dynamic
244 // parameters on newly connected devices (instead of opening the inputs...)
245 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
246
Paul McLean9080a4c2015-06-18 08:24:02 -0700247 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
François Gaffie44481e72016-04-20 07:49:57 +0200248 broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
249 devDesc->mAddress);
Eric Laurentd4692962014-05-05 18:13:44 -0700250 return INVALID_OPERATION;
251 }
252
Eric Laurent3a4311c2014-03-17 12:00:47 -0700253 index = mAvailableInputDevices.add(devDesc);
254 if (index >= 0) {
Paul McLeane743a472015-01-28 11:07:31 -0800255 mAvailableInputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700256 } else {
257 return NO_MEMORY;
258 }
Eric Laurent3ae5f312015-02-03 17:12:08 -0800259
François Gaffie2110e042015-03-24 08:41:51 +0100260 // Propagate device availability to Engine
261 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700262 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700263
264 // handle input device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700265 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700266 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700267 ALOGW("setDeviceConnectionState() device not connected: %d", device);
268 return INVALID_OPERATION;
269 }
Paul McLean5c477aa2014-08-20 16:47:57 -0700270
271 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
272
273 // Set Disconnect to HALs
François Gaffie44481e72016-04-20 07:49:57 +0200274 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700275
Paul McLean9080a4c2015-06-18 08:24:02 -0700276 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700277 mAvailableInputDevices.remove(devDesc);
Paul McLean5c477aa2014-08-20 16:47:57 -0700278
François Gaffie2110e042015-03-24 08:41:51 +0100279 // Propagate device availability to Engine
280 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700281 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700282
283 default:
284 ALOGE("setDeviceConnectionState() invalid state: %x", state);
285 return BAD_VALUE;
286 }
287
Eric Laurentd4692962014-05-05 18:13:44 -0700288 closeAllInputs();
Eric Laurent5f5fca52016-08-04 11:48:57 -0700289 // As the input device list can impact the output device selection, update
290 // getDeviceForStrategy() cache
291 updateDevicesAndOutputs();
Eric Laurente552edb2014-03-10 17:42:56 -0700292
Eric Laurent87ffa392015-05-22 10:32:38 -0700293 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700294 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
295 updateCallRouting(newDevice);
296 }
297
Eric Laurentd60560a2015-04-10 11:31:20 -0700298 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
299 cleanUpForDevice(devDesc);
300 }
301
Eric Laurentb52c1522014-05-20 11:27:36 -0700302 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700303 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700304 } // end if is input device
Eric Laurente552edb2014-03-10 17:42:56 -0700305
306 ALOGW("setDeviceConnectionState() invalid device: %x", device);
307 return BAD_VALUE;
308}
309
Eric Laurente0720872014-03-11 09:30:41 -0700310audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +0100311 const char *device_address)
Eric Laurente552edb2014-03-10 17:42:56 -0700312{
Eric Laurent634b7142016-04-20 13:48:02 -0700313 sp<DeviceDescriptor> devDesc =
314 mHwModules.getDeviceDescriptor(device, device_address, "",
315 (strlen(device_address) != 0)/*matchAddress*/);
316
317 if (devDesc == 0) {
318 ALOGW("getDeviceConnectionState() undeclared device, type %08x, address: %s",
319 device, device_address);
320 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
321 }
François Gaffie53615e22015-03-19 09:24:12 +0100322
Eric Laurent3a4311c2014-03-17 12:00:47 -0700323 DeviceVector *deviceVector;
324
Eric Laurente552edb2014-03-10 17:42:56 -0700325 if (audio_is_output_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700326 deviceVector = &mAvailableOutputDevices;
Eric Laurente552edb2014-03-10 17:42:56 -0700327 } else if (audio_is_input_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700328 deviceVector = &mAvailableInputDevices;
329 } else {
330 ALOGW("getDeviceConnectionState() invalid device type %08x", device);
331 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700332 }
Eric Laurent634b7142016-04-20 13:48:02 -0700333
334 return (deviceVector->getDevice(device, String8(device_address)) != 0) ?
335 AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurenta1d525f2015-01-29 13:36:45 -0800336}
337
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800338status_t AudioPolicyManager::handleDeviceConfigChange(audio_devices_t device,
339 const char *device_address,
340 const char *device_name)
341{
342 status_t status;
343
344 ALOGV("handleDeviceConfigChange(() device: 0x%X, address %s name %s",
345 device, device_address, device_name);
346
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -0800347 // connect/disconnect only 1 device at a time
348 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
349
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800350 // Check if the device is currently connected
351 sp<DeviceDescriptor> devDesc =
352 mHwModules.getDeviceDescriptor(device, device_address, device_name);
353 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
354 if (index < 0) {
355 // Nothing to do: device is not connected
356 return NO_ERROR;
357 }
358
359 // Toggle the device state: UNAVAILABLE -> AVAILABLE
360 // This will force reading again the device configuration
361 status = setDeviceConnectionState(device,
362 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
363 device_address, device_name);
364 if (status != NO_ERROR) {
365 ALOGW("handleDeviceConfigChange() error disabling connection state: %d",
366 status);
367 return status;
368 }
369
370 status = setDeviceConnectionState(device,
371 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
372 device_address, device_name);
373 if (status != NO_ERROR) {
374 ALOGW("handleDeviceConfigChange() error enabling connection state: %d",
375 status);
376 return status;
377 }
378
379 return NO_ERROR;
380}
381
Eric Laurentdc462862016-07-19 12:29:53 -0700382uint32_t AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, uint32_t delayMs)
Eric Laurentc2730ba2014-07-20 15:47:07 -0700383{
384 bool createTxPatch = false;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700385 status_t status;
386 audio_patch_handle_t afPatchHandle;
387 DeviceVector deviceList;
Eric Laurentdc462862016-07-19 12:29:53 -0700388 uint32_t muteWaitMs = 0;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700389
Andy Hunga76c7de2016-12-13 19:14:31 -0800390 if(!hasPrimaryOutput() || mPrimaryOutput->device() == AUDIO_DEVICE_OUT_STUB) {
Eric Laurentdc462862016-07-19 12:29:53 -0700391 return muteWaitMs;
Eric Laurent87ffa392015-05-22 10:32:38 -0700392 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -0800393 audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700394 ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice);
395
396 // release existing RX patch if any
397 if (mCallRxPatch != 0) {
398 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
399 mCallRxPatch.clear();
400 }
401 // release TX patch if any
402 if (mCallTxPatch != 0) {
403 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
404 mCallTxPatch.clear();
405 }
406
407 // If the RX device is on the primary HW module, then use legacy routing method for voice calls
408 // via setOutputDevice() on primary output.
409 // Otherwise, create two audio patches for TX and RX path.
410 if (availablePrimaryOutputDevices() & rxDevice) {
Eric Laurentdc462862016-07-19 12:29:53 -0700411 muteWaitMs = setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700412 // If the TX device is also on the primary HW module, setOutputDevice() will take care
413 // of it due to legacy implementation. If not, create a patch.
414 if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN)
415 == AUDIO_DEVICE_NONE) {
416 createTxPatch = true;
417 }
Eric Laurent8ae73122016-04-12 10:13:29 -0700418 } else { // create RX path audio patch
419 struct audio_patch patch;
420
421 patch.num_sources = 1;
422 patch.num_sinks = 1;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700423 deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice);
424 ALOG_ASSERT(!deviceList.isEmpty(),
425 "updateCallRouting() selected device not in output device list");
426 sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0);
427 deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX);
428 ALOG_ASSERT(!deviceList.isEmpty(),
429 "updateCallRouting() no telephony RX device");
430 sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0);
431
432 rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
433 rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
434
435 // request to reuse existing output stream if one is already opened to reach the RX device
436 SortedVector<audio_io_handle_t> outputs =
437 getOutputsForDevice(rxDevice, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700438 audio_io_handle_t output = selectOutput(outputs,
439 AUDIO_OUTPUT_FLAG_NONE,
440 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700441 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700442 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700443 ALOG_ASSERT(!outputDesc->isDuplicated(),
444 "updateCallRouting() RX device output is duplicated");
445 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700446 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700447 patch.num_sources = 2;
448 }
449
450 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentdc462862016-07-19 12:29:53 -0700451 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700452 ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch",
453 status);
454 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100455 mCallRxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700456 mCallRxPatch->mAfPatchHandle = afPatchHandle;
457 mCallRxPatch->mUid = mUidCached;
458 }
459 createTxPatch = true;
460 }
Eric Laurent8ae73122016-04-12 10:13:29 -0700461 if (createTxPatch) { // create TX path audio patch
Eric Laurentc2730ba2014-07-20 15:47:07 -0700462 struct audio_patch patch;
Eric Laurent8ae73122016-04-12 10:13:29 -0700463
Eric Laurentc2730ba2014-07-20 15:47:07 -0700464 patch.num_sources = 1;
465 patch.num_sinks = 1;
466 deviceList = mAvailableInputDevices.getDevicesFromType(txDevice);
467 ALOG_ASSERT(!deviceList.isEmpty(),
468 "updateCallRouting() selected device not in input device list");
469 sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0);
470 txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
471 deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
472 ALOG_ASSERT(!deviceList.isEmpty(),
473 "updateCallRouting() no telephony TX device");
474 sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0);
475 txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
476
477 SortedVector<audio_io_handle_t> outputs =
478 getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700479 audio_io_handle_t output = selectOutput(outputs,
480 AUDIO_OUTPUT_FLAG_NONE,
481 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700482 // request to reuse existing output stream if one is already opened to reach the TX
483 // path output device
484 if (output != AUDIO_IO_HANDLE_NONE) {
485 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
486 ALOG_ASSERT(!outputDesc->isDuplicated(),
487 "updateCallRouting() RX device output is duplicated");
488 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700489 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700490 patch.num_sources = 2;
491 }
492
Eric Laurentc0a889f2015-10-14 14:36:34 -0700493 // terminate active capture if on the same HW module as the call TX source device
494 // FIXME: would be better to refine to only inputs whose profile connects to the
495 // call TX device but this information is not in the audio patch and logic here must be
496 // symmetric to the one in startInput()
Eric Laurentfb66dd92016-01-28 18:32:03 -0800497 Vector<sp <AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
498 for (size_t i = 0; i < activeInputs.size(); i++) {
499 sp<AudioInputDescriptor> activeDesc = activeInputs[i];
500 if (activeDesc->hasSameHwModuleAs(txSourceDeviceDesc)) {
501 AudioSessionCollection activeSessions =
502 activeDesc->getAudioSessions(true /*activeOnly*/);
503 for (size_t j = 0; j < activeSessions.size(); j++) {
504 audio_session_t activeSession = activeSessions.keyAt(j);
505 stopInput(activeDesc->mIoHandle, activeSession);
506 releaseInput(activeDesc->mIoHandle, activeSession);
507 }
Eric Laurentc0a889f2015-10-14 14:36:34 -0700508 }
509 }
510
Eric Laurentc2730ba2014-07-20 15:47:07 -0700511 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentdc462862016-07-19 12:29:53 -0700512 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700513 ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch",
514 status);
515 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100516 mCallTxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700517 mCallTxPatch->mAfPatchHandle = afPatchHandle;
518 mCallTxPatch->mUid = mUidCached;
519 }
520 }
Eric Laurentdc462862016-07-19 12:29:53 -0700521
522 return muteWaitMs;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700523}
524
Eric Laurente0720872014-03-11 09:30:41 -0700525void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700526{
527 ALOGV("setPhoneState() state %d", state);
François Gaffie2110e042015-03-24 08:41:51 +0100528 // store previous phone state for management of sonification strategy below
529 int oldState = mEngine->getPhoneState();
530
531 if (mEngine->setPhoneState(state) != NO_ERROR) {
532 ALOGW("setPhoneState() invalid or same state %d", state);
Eric Laurente552edb2014-03-10 17:42:56 -0700533 return;
534 }
François Gaffie2110e042015-03-24 08:41:51 +0100535 /// Opens: can these line be executed after the switch of volume curves???
Eric Laurente552edb2014-03-10 17:42:56 -0700536 // if leaving call state, handle special case of active streams
537 // pertaining to sonification strategy see handleIncallSonification()
Eric Laurent63dea1d2015-07-02 17:10:28 -0700538 if (isStateInCall(oldState)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700539 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent794fde22016-03-11 09:50:45 -0800540 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700541 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700542 }
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800543
Eric Laurent63dea1d2015-07-02 17:10:28 -0700544 // force reevaluating accessibility routing when call stops
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800545 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700546 }
547
François Gaffie2110e042015-03-24 08:41:51 +0100548 /**
549 * Switching to or from incall state or switching between telephony and VoIP lead to force
550 * routing command.
551 */
552 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
553 || (is_state_in_call(state) && (state != oldState)));
Eric Laurente552edb2014-03-10 17:42:56 -0700554
555 // check for device and output changes triggered by new phone state
Eric Laurente552edb2014-03-10 17:42:56 -0700556 checkA2dpSuspend();
557 checkOutputForAllStrategies();
558 updateDevicesAndOutputs();
559
Eric Laurente552edb2014-03-10 17:42:56 -0700560 int delayMs = 0;
561 if (isStateInCall(state)) {
562 nsecs_t sysTime = systemTime();
563 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700564 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700565 // mute media and sonification strategies and delay device switch by the largest
566 // latency of any output where either strategy is active.
567 // This avoid sending the ring tone or music tail into the earpiece or headset.
François Gaffiead3183e2015-03-18 16:55:35 +0100568 if ((isStrategyActive(desc, STRATEGY_MEDIA,
569 SONIFICATION_HEADSET_MUSIC_DELAY,
570 sysTime) ||
571 isStrategyActive(desc, STRATEGY_SONIFICATION,
572 SONIFICATION_HEADSET_MUSIC_DELAY,
573 sysTime)) &&
Eric Laurentc75307b2015-03-17 15:29:32 -0700574 (delayMs < (int)desc->latency()*2)) {
575 delayMs = desc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -0700576 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700577 setStrategyMute(STRATEGY_MEDIA, true, desc);
578 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700579 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Eric Laurentc75307b2015-03-17 15:29:32 -0700580 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
581 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700582 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
583 }
584 }
585
Eric Laurent87ffa392015-05-22 10:32:38 -0700586 if (hasPrimaryOutput()) {
587 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
588 // the device returned is not necessarily reachable via this output
589 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
590 // force routing command to audio hardware when ending call
591 // even if no device change is needed
592 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
593 rxDevice = mPrimaryOutput->device();
594 }
Eric Laurente552edb2014-03-10 17:42:56 -0700595
Eric Laurent87ffa392015-05-22 10:32:38 -0700596 if (state == AUDIO_MODE_IN_CALL) {
597 updateCallRouting(rxDevice, delayMs);
598 } else if (oldState == AUDIO_MODE_IN_CALL) {
599 if (mCallRxPatch != 0) {
600 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
601 mCallRxPatch.clear();
602 }
603 if (mCallTxPatch != 0) {
604 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
605 mCallTxPatch.clear();
606 }
607 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
608 } else {
609 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700610 }
Eric Laurentc2730ba2014-07-20 15:47:07 -0700611 }
Eric Laurente552edb2014-03-10 17:42:56 -0700612 // if entering in call state, handle special case of active streams
613 // pertaining to sonification strategy see handleIncallSonification()
614 if (isStateInCall(state)) {
615 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent794fde22016-03-11 09:50:45 -0800616 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700617 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700618 }
Eric Laurent63dea1d2015-07-02 17:10:28 -0700619
620 // force reevaluating accessibility routing when call starts
621 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700622 }
623
624 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700625 if (state == AUDIO_MODE_RINGTONE &&
626 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700627 mLimitRingtoneVolume = true;
628 } else {
629 mLimitRingtoneVolume = false;
630 }
631}
632
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700633audio_mode_t AudioPolicyManager::getPhoneState() {
634 return mEngine->getPhoneState();
635}
636
Eric Laurente0720872014-03-11 09:30:41 -0700637void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700638 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700639{
François Gaffie2110e042015-03-24 08:41:51 +0100640 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Eric Laurent8dc87a62017-05-16 19:00:40 -0700641 if (config == mEngine->getForceUse(usage)) {
642 return;
643 }
Eric Laurente552edb2014-03-10 17:42:56 -0700644
François Gaffie2110e042015-03-24 08:41:51 +0100645 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
646 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
647 return;
Eric Laurente552edb2014-03-10 17:42:56 -0700648 }
François Gaffie2110e042015-03-24 08:41:51 +0100649 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
650 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
651 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
Eric Laurente552edb2014-03-10 17:42:56 -0700652
653 // check for device and output changes triggered by new force usage
654 checkA2dpSuspend();
655 checkOutputForAllStrategies();
656 updateDevicesAndOutputs();
Phil Burk09bc4612016-02-24 15:58:15 -0800657
Eric Laurentdc462862016-07-19 12:29:53 -0700658 //FIXME: workaround for truncated touch sounds
659 // to be removed when the problem is handled by system UI
660 uint32_t delayMs = 0;
661 uint32_t waitMs = 0;
662 if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) {
663 delayMs = TOUCH_SOUND_FIXED_DELAY_MS;
664 }
Eric Laurent87ffa392015-05-22 10:32:38 -0700665 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700666 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
Eric Laurentdc462862016-07-19 12:29:53 -0700667 waitMs = updateCallRouting(newDevice, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700668 }
Eric Laurente552edb2014-03-10 17:42:56 -0700669 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700670 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
671 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
672 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
Eric Laurentdc462862016-07-19 12:29:53 -0700673 waitMs = setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE),
674 delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700675 }
Eric Laurente552edb2014-03-10 17:42:56 -0700676 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
Eric Laurentdc462862016-07-19 12:29:53 -0700677 applyStreamVolumes(outputDesc, newDevice, waitMs, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700678 }
679 }
680
Eric Laurentfb66dd92016-01-28 18:32:03 -0800681 Vector<sp <AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
682 for (size_t i = 0; i < activeInputs.size(); i++) {
683 sp<AudioInputDescriptor> activeDesc = activeInputs[i];
684 audio_devices_t newDevice = getNewInputDevice(activeDesc);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700685 // Force new input selection if the new device can not be reached via current input
Eric Laurentfb66dd92016-01-28 18:32:03 -0800686 if (activeDesc->mProfile->getSupportedDevices().types() &
687 (newDevice & ~AUDIO_DEVICE_BIT_IN)) {
688 setInputDevice(activeDesc->mIoHandle, newDevice);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700689 } else {
Eric Laurentfb66dd92016-01-28 18:32:03 -0800690 closeInput(activeDesc->mIoHandle);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700691 }
Eric Laurente552edb2014-03-10 17:42:56 -0700692 }
Eric Laurente552edb2014-03-10 17:42:56 -0700693}
694
Eric Laurente0720872014-03-11 09:30:41 -0700695void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700696{
697 ALOGV("setSystemProperty() property %s, value %s", property, value);
698}
699
700// Find a direct output profile compatible with the parameters passed, even if the input flags do
701// not explicitly request a direct output
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800702sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700703 audio_devices_t device,
704 uint32_t samplingRate,
705 audio_format_t format,
706 audio_channel_mask_t channelMask,
707 audio_output_flags_t flags)
708{
Eric Laurent861a6282015-05-18 15:40:16 -0700709 // only retain flags that will drive the direct output profile selection
710 // if explicitly requested
711 static const uint32_t kRelevantFlags =
Haynes Mathew George84c621e2017-04-25 11:41:50 -0700712 (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD |
713 AUDIO_OUTPUT_FLAG_VOIP_RX);
Eric Laurent861a6282015-05-18 15:40:16 -0700714 flags =
715 (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT);
716
717 sp<IOProfile> profile;
718
Eric Laurente552edb2014-03-10 17:42:56 -0700719 for (size_t i = 0; i < mHwModules.size(); i++) {
720 if (mHwModules[i]->mHandle == 0) {
721 continue;
722 }
723 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent861a6282015-05-18 15:40:16 -0700724 sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j];
725 if (!curProfile->isCompatibleProfile(device, String8(""),
Andy Hungf129b032015-04-07 13:45:50 -0700726 samplingRate, NULL /*updatedSamplingRate*/,
727 format, NULL /*updatedFormat*/,
728 channelMask, NULL /*updatedChannelMask*/,
Eric Laurent861a6282015-05-18 15:40:16 -0700729 flags)) {
730 continue;
731 }
732 // reject profiles not corresponding to a device currently available
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100733 if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) {
Eric Laurent861a6282015-05-18 15:40:16 -0700734 continue;
735 }
736 // if several profiles are compatible, give priority to one with offload capability
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100737 if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) {
Eric Laurent861a6282015-05-18 15:40:16 -0700738 continue;
739 }
740 profile = curProfile;
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100741 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent861a6282015-05-18 15:40:16 -0700742 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700743 }
Eric Laurente552edb2014-03-10 17:42:56 -0700744 }
745 }
Eric Laurent861a6282015-05-18 15:40:16 -0700746 return profile;
Eric Laurente552edb2014-03-10 17:42:56 -0700747}
748
Eric Laurente0720872014-03-11 09:30:41 -0700749audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +0100750 uint32_t samplingRate,
751 audio_format_t format,
752 audio_channel_mask_t channelMask,
753 audio_output_flags_t flags,
754 const audio_offload_info_t *offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -0700755{
Eric Laurent3b73df72014-03-11 09:06:29 -0700756 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700757 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
758 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
759 device, stream, samplingRate, format, channelMask, flags);
760
Kevin Rocard169753c2017-03-06 14:18:23 -0800761 return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE, stream, samplingRate, format,
762 channelMask, flags, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700763}
764
Eric Laurente83b55d2014-11-14 10:06:21 -0800765status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
766 audio_io_handle_t *output,
767 audio_session_t session,
768 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700769 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800770 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800771 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700772 audio_port_handle_t selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800773 audio_port_handle_t *portId)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700774{
Eric Laurente83b55d2014-11-14 10:06:21 -0800775 audio_attributes_t attributes;
776 if (attr != NULL) {
777 if (!isValidAttributes(attr)) {
778 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
779 attr->usage, attr->content_type, attr->flags,
780 attr->tags);
781 return BAD_VALUE;
782 }
783 attributes = *attr;
784 } else {
785 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
786 ALOGE("getOutputForAttr(): invalid stream type");
787 return BAD_VALUE;
788 }
789 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700790 }
Eric Laurent20b9ef02016-12-05 11:03:16 -0800791
792 // TODO: check for existing client for this port ID
793 if (*portId == AUDIO_PORT_HANDLE_NONE) {
794 *portId = AudioPort::getNextUniqueId();
795 }
796
Eric Laurentc75307b2015-03-17 15:29:32 -0700797 sp<SwAudioOutputDescriptor> desc;
Jean-Michel Trivie8deced2016-02-11 12:50:39 -0800798 if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) {
François Gaffie036e1e92015-03-19 10:16:24 +0100799 ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr");
Eric Laurent20b9ef02016-12-05 11:03:16 -0800800 if (!audio_has_proportional_frames(config->format)) {
François Gaffie036e1e92015-03-19 10:16:24 +0100801 return BAD_VALUE;
Eric Laurent275e8e92014-11-30 15:14:47 -0800802 }
François Gaffie036e1e92015-03-19 10:16:24 +0100803 *stream = streamTypefromAttributesInt(&attributes);
804 *output = desc->mIoHandle;
805 ALOGV("getOutputForAttr() returns output %d", *output);
806 return NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -0800807 }
808 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
809 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
810 return BAD_VALUE;
811 }
812
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700813 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x"
814 " session %d selectedDeviceId %d",
815 attributes.usage, attributes.content_type, attributes.tags, attributes.flags,
816 session, selectedDeviceId);
817
818 *stream = streamTypefromAttributesInt(&attributes);
819
820 // Explicit routing?
821 sp<DeviceDescriptor> deviceDesc;
822 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
823 if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) {
824 deviceDesc = mAvailableOutputDevices[i];
825 break;
826 }
827 }
828 mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700829
Eric Laurente83b55d2014-11-14 10:06:21 -0800830 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700831 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700832
Eric Laurente83b55d2014-11-14 10:06:21 -0800833 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700834 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
835 }
836
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700837 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Eric Laurent20b9ef02016-12-05 11:03:16 -0800838 device, config->sample_rate, config->format, config->channel_mask, flags);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700839
Kevin Rocard169753c2017-03-06 14:18:23 -0800840 *output = getOutputForDevice(device, session, *stream,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800841 config->sample_rate, config->format, config->channel_mask,
842 flags, &config->offload_info);
Eric Laurente83b55d2014-11-14 10:06:21 -0800843 if (*output == AUDIO_IO_HANDLE_NONE) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700844 mOutputRoutes.removeRoute(session);
Eric Laurente83b55d2014-11-14 10:06:21 -0800845 return INVALID_OPERATION;
846 }
Paul McLeanaa981192015-03-21 09:55:15 -0700847
Eric Laurente83b55d2014-11-14 10:06:21 -0800848 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700849}
850
851audio_io_handle_t AudioPolicyManager::getOutputForDevice(
852 audio_devices_t device,
Kevin Rocard169753c2017-03-06 14:18:23 -0800853 audio_session_t session,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700854 audio_stream_type_t stream,
855 uint32_t samplingRate,
856 audio_format_t format,
857 audio_channel_mask_t channelMask,
858 audio_output_flags_t flags,
859 const audio_offload_info_t *offloadInfo)
860{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700861 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700862 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700863
Eric Laurente552edb2014-03-10 17:42:56 -0700864#ifdef AUDIO_POLICY_TEST
865 if (mCurOutput != 0) {
866 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
867 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
868
869 if (mTestOutputs[mCurOutput] == 0) {
870 ALOGV("getOutput() opening test output");
Eric Laurentc75307b2015-03-17 15:29:32 -0700871 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
872 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700873 outputDesc->mDevice = mTestDevice;
Eric Laurente552edb2014-03-10 17:42:56 -0700874 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700875 outputDesc->mFlags =
876 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700877 outputDesc->mRefCount[stream] = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700878 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
879 config.sample_rate = mTestSamplingRate;
880 config.channel_mask = mTestChannels;
881 config.format = mTestFormat;
Phil Burk77cce802014-08-04 16:18:15 -0700882 if (offloadInfo != NULL) {
883 config.offload_info = *offloadInfo;
884 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700885 status = mpClientInterface->openOutput(0,
886 &mTestOutputs[mCurOutput],
887 &config,
888 &outputDesc->mDevice,
889 String8(""),
890 &outputDesc->mLatency,
891 outputDesc->mFlags);
892 if (status == NO_ERROR) {
893 outputDesc->mSamplingRate = config.sample_rate;
894 outputDesc->mFormat = config.format;
895 outputDesc->mChannelMask = config.channel_mask;
Eric Laurente552edb2014-03-10 17:42:56 -0700896 AudioParameter outputCmd = AudioParameter();
897 outputCmd.addInt(String8("set_id"),mCurOutput);
898 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
899 addOutput(mTestOutputs[mCurOutput], outputDesc);
900 }
901 }
902 return mTestOutputs[mCurOutput];
903 }
904#endif //AUDIO_POLICY_TEST
905
906 // open a direct output if required by specified parameters
907 //force direct flag if offload flag is set: offloading implies a direct output stream
908 // and all common behaviors are driven by checking only the direct flag
909 // this should normally be set appropriately in the policy configuration file
910 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700911 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700912 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700913 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
914 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
915 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800916 // only allow deep buffering for music stream type
917 if (stream != AUDIO_STREAM_MUSIC) {
918 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Ravi Kumar Alamanda439e4ed2015-04-03 12:13:21 -0700919 } else if (/* stream == AUDIO_STREAM_MUSIC && */
920 flags == AUDIO_OUTPUT_FLAG_NONE &&
921 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
922 // use DEEP_BUFFER as default output for music stream type
923 flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Eric Laurente83b55d2014-11-14 10:06:21 -0800924 }
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700925 if (stream == AUDIO_STREAM_TTS) {
926 flags = AUDIO_OUTPUT_FLAG_TTS;
Haynes Mathew George84c621e2017-04-25 11:41:50 -0700927 } else if (stream == AUDIO_STREAM_VOICE_CALL &&
928 getPhoneState() == AUDIO_MODE_IN_COMMUNICATION &&
929 audio_is_linear_pcm(format)) {
930 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX |
931 AUDIO_OUTPUT_FLAG_DIRECT);
932 ALOGV("Set VoIP and Direct output flags for PCM format");
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700933 }
Eric Laurente552edb2014-03-10 17:42:56 -0700934
Eric Laurentb732cf52014-09-24 19:08:21 -0700935 sp<IOProfile> profile;
936
937 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700938 // and not explicitly requested
939 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
Glenn Kasten05ddca52016-02-11 08:17:12 -0800940 audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700941 audio_channel_count_from_out_mask(channelMask) <= 2) {
942 goto non_direct_output;
943 }
944
Andy Hung2ddee192015-12-18 17:34:44 -0800945 // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled.
946 // This prevents creating an offloaded track and tearing it down immediately after start
947 // when audioflinger detects there is an active non offloadable effect.
Eric Laurente552edb2014-03-10 17:42:56 -0700948 // FIXME: We should check the audio session here but we do not have it in this context.
949 // This may prevent offloading in rare situations where effects are left active by apps
950 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700951
Eric Laurente552edb2014-03-10 17:42:56 -0700952 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
Andy Hung2ddee192015-12-18 17:34:44 -0800953 !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700954 profile = getProfileForDirectOutput(device,
955 samplingRate,
956 format,
957 channelMask,
958 (audio_output_flags_t)flags);
959 }
960
Eric Laurent1c333e22014-05-20 10:48:17 -0700961 if (profile != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700962 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700963
964 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700965 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700966 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
967 outputDesc = desc;
Kevin Rocard551061a2017-02-06 15:59:29 -0800968 // reuse direct output if currently open by the same client
969 // and configured with same parameters
Eric Laurente552edb2014-03-10 17:42:56 -0700970 if ((samplingRate == outputDesc->mSamplingRate) &&
Kevin Rocard551061a2017-02-06 15:59:29 -0800971 audio_formats_match(format, outputDesc->mFormat) &&
972 (channelMask == outputDesc->mChannelMask)) {
Kevin Rocard169753c2017-03-06 14:18:23 -0800973 if (session == outputDesc->mDirectClientSession) {
Kevin Rocard551061a2017-02-06 15:59:29 -0800974 outputDesc->mDirectOpenCount++;
Kevin Rocard169753c2017-03-06 14:18:23 -0800975 ALOGV("getOutput() reusing direct output %d for session %d",
976 mOutputs.keyAt(i), session);
Kevin Rocard551061a2017-02-06 15:59:29 -0800977 return mOutputs.keyAt(i);
978 } else {
Kevin Rocard169753c2017-03-06 14:18:23 -0800979 ALOGV("getOutput() do not reuse direct output because current client (%d) "
980 "is not the same as requesting client (%d)",
981 outputDesc->mDirectClientSession, session);
Kevin Rocard551061a2017-02-06 15:59:29 -0800982 goto non_direct_output;
983 }
Eric Laurente552edb2014-03-10 17:42:56 -0700984 }
985 }
986 }
987 // close direct output if currently open and configured with different parameters
988 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700989 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700990 }
Eric Laurent861a6282015-05-18 15:40:16 -0700991
992 // if the selected profile is offloaded and no offload info was specified,
993 // create a default one
994 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100995 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
Eric Laurent861a6282015-05-18 15:40:16 -0700996 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
997 defaultOffloadInfo.sample_rate = samplingRate;
998 defaultOffloadInfo.channel_mask = channelMask;
999 defaultOffloadInfo.format = format;
1000 defaultOffloadInfo.stream_type = stream;
1001 defaultOffloadInfo.bit_rate = 0;
1002 defaultOffloadInfo.duration_us = -1;
1003 defaultOffloadInfo.has_video = true; // conservative
1004 defaultOffloadInfo.is_streaming = true; // likely
1005 offloadInfo = &defaultOffloadInfo;
1006 }
1007
Eric Laurentc75307b2015-03-17 15:29:32 -07001008 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07001009 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -07001010 outputDesc->mLatency = 0;
Eric Laurent861a6282015-05-18 15:40:16 -07001011 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001012 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1013 config.sample_rate = samplingRate;
1014 config.channel_mask = channelMask;
1015 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -07001016 if (offloadInfo != NULL) {
1017 config.offload_info = *offloadInfo;
1018 }
Eric Laurente3014102017-05-03 11:15:43 -07001019 DeviceVector outputDevices = mAvailableOutputDevices.getDevicesFromType(device);
1020 String8 address = outputDevices.size() > 0 ? outputDevices.itemAt(0)->mAddress
1021 : String8("");
Eric Laurent322b4d22015-04-03 15:57:54 -07001022 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07001023 &output,
1024 &config,
1025 &outputDesc->mDevice,
Eric Laurente3014102017-05-03 11:15:43 -07001026 address,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001027 &outputDesc->mLatency,
1028 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001029
1030 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -07001031 if (status != NO_ERROR ||
1032 (samplingRate != 0 && samplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -08001033 (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) ||
Eric Laurentcf2c0212014-07-25 16:20:43 -07001034 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -07001035 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1036 "format %d %d, channelMask %04x %04x", output, samplingRate,
1037 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1038 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001039 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07001040 mpClientInterface->closeOutput(output);
1041 }
Eric Laurenta82797f2015-01-30 11:49:43 -08001042 // fall back to mixer output if possible when the direct output could not be open
Glenn Kasten05ddca52016-02-11 08:17:12 -08001043 if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) {
Eric Laurenta82797f2015-01-30 11:49:43 -08001044 goto non_direct_output;
1045 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001046 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001047 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001048 outputDesc->mSamplingRate = config.sample_rate;
1049 outputDesc->mChannelMask = config.channel_mask;
1050 outputDesc->mFormat = config.format;
1051 outputDesc->mRefCount[stream] = 0;
1052 outputDesc->mStopTime[stream] = 0;
1053 outputDesc->mDirectOpenCount = 1;
Kevin Rocard169753c2017-03-06 14:18:23 -08001054 outputDesc->mDirectClientSession = session;
1055
Eric Laurente552edb2014-03-10 17:42:56 -07001056 addOutput(output, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07001057 mPreviousOutputs = mOutputs;
1058 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -07001059 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001060 return output;
1061 }
1062
Eric Laurentb732cf52014-09-24 19:08:21 -07001063non_direct_output:
Eric Laurent14cbfca2016-03-17 09:42:16 -07001064
1065 // A request for HW A/V sync cannot fallback to a mixed output because time
1066 // stamps are embedded in audio data
1067 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1068 return AUDIO_IO_HANDLE_NONE;
1069 }
1070
Eric Laurente552edb2014-03-10 17:42:56 -07001071 // ignoring channel mask due to downmix capability in mixer
1072
1073 // open a non direct output
1074
1075 // for non direct outputs, only PCM is supported
1076 if (audio_is_linear_pcm(format)) {
1077 // get which output is suitable for the specified stream. The actual
1078 // routing change will happen when startOutput() will be called
1079 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1080
Eric Laurent8838a382014-09-08 16:44:28 -07001081 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1082 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1083 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -07001084 }
1085 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1086 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1087
Paul McLeanaa981192015-03-21 09:55:15 -07001088 ALOGV(" getOutputForDevice() returns output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -07001089
1090 return output;
1091}
1092
Eric Laurente0720872014-03-11 09:30:41 -07001093audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -07001094 audio_output_flags_t flags,
1095 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -07001096{
1097 // select one output among several that provide a path to a particular device or set of
1098 // devices (the list was previously build by getOutputsForDevice()).
1099 // The priority is as follows:
1100 // 1: the output with the highest number of requested policy flags
Eric Laurente6930022016-02-11 10:20:40 -08001101 // 2: the output with the bit depth the closest to the requested one
1102 // 3: the primary output
1103 // 4: the first output in the list
Eric Laurente552edb2014-03-10 17:42:56 -07001104
1105 if (outputs.size() == 0) {
1106 return 0;
1107 }
1108 if (outputs.size() == 1) {
1109 return outputs[0];
1110 }
1111
1112 int maxCommonFlags = 0;
Eric Laurente6930022016-02-11 10:20:40 -08001113 audio_io_handle_t outputForFlags = 0;
1114 audio_io_handle_t outputForPrimary = 0;
1115 audio_io_handle_t outputForFormat = 0;
1116 audio_format_t bestFormat = AUDIO_FORMAT_INVALID;
1117 audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07001118
1119 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001120 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -07001121 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -07001122 // if a valid format is specified, skip output if not compatible
1123 if (format != AUDIO_FORMAT_INVALID) {
1124 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente6930022016-02-11 10:20:40 -08001125 if (!audio_formats_match(format, outputDesc->mFormat)) {
Eric Laurent8838a382014-09-08 16:44:28 -07001126 continue;
1127 }
1128 } else if (!audio_is_linear_pcm(format)) {
1129 continue;
1130 }
Eric Laurente6930022016-02-11 10:20:40 -08001131 if (AudioPort::isBetterFormatMatch(
1132 outputDesc->mFormat, bestFormat, format)) {
1133 outputForFormat = outputs[i];
1134 bestFormat = outputDesc->mFormat;
1135 }
Eric Laurent8838a382014-09-08 16:44:28 -07001136 }
1137
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001138 int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags);
Eric Laurente6930022016-02-11 10:20:40 -08001139 if (commonFlags >= maxCommonFlags) {
1140 if (commonFlags == maxCommonFlags) {
1141 if (AudioPort::isBetterFormatMatch(
1142 outputDesc->mFormat, bestFormatForFlags, format)) {
1143 outputForFlags = outputs[i];
1144 bestFormatForFlags = outputDesc->mFormat;
1145 }
1146 } else {
1147 outputForFlags = outputs[i];
1148 maxCommonFlags = commonFlags;
1149 bestFormatForFlags = outputDesc->mFormat;
1150 }
Eric Laurente552edb2014-03-10 17:42:56 -07001151 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
1152 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001153 if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurente6930022016-02-11 10:20:40 -08001154 outputForPrimary = outputs[i];
Eric Laurente552edb2014-03-10 17:42:56 -07001155 }
1156 }
1157 }
1158
Eric Laurente6930022016-02-11 10:20:40 -08001159 if (outputForFlags != 0) {
1160 return outputForFlags;
Eric Laurente552edb2014-03-10 17:42:56 -07001161 }
Eric Laurente6930022016-02-11 10:20:40 -08001162 if (outputForFormat != 0) {
1163 return outputForFormat;
1164 }
1165 if (outputForPrimary != 0) {
1166 return outputForPrimary;
Eric Laurente552edb2014-03-10 17:42:56 -07001167 }
1168
1169 return outputs[0];
1170}
1171
Eric Laurente0720872014-03-11 09:30:41 -07001172status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001173 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001174 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001175{
Paul McLeanaa981192015-03-21 09:55:15 -07001176 ALOGV("startOutput() output %d, stream %d, session %d",
1177 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -07001178 ssize_t index = mOutputs.indexOfKey(output);
1179 if (index < 0) {
1180 ALOGW("startOutput() unknown output %d", output);
1181 return BAD_VALUE;
1182 }
1183
Eric Laurentc75307b2015-03-17 15:29:32 -07001184 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
1185
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001186 // Routing?
1187 mOutputRoutes.incRouteActivity(session);
1188
Eric Laurentc75307b2015-03-17 15:29:32 -07001189 audio_devices_t newDevice;
Eric Laurentc40d9692016-04-13 19:14:13 -07001190 AudioMix *policyMix = NULL;
1191 const char *address = NULL;
Eric Laurentc75307b2015-03-17 15:29:32 -07001192 if (outputDesc->mPolicyMix != NULL) {
Eric Laurentc40d9692016-04-13 19:14:13 -07001193 policyMix = outputDesc->mPolicyMix;
1194 address = policyMix->mDeviceAddress.string();
1195 if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
1196 newDevice = policyMix->mDeviceType;
1197 } else {
1198 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
1199 }
Eric Laurent493404d2015-04-21 15:07:36 -07001200 } else if (mOutputRoutes.hasRouteChanged(session)) {
1201 newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001202 checkStrategyRoute(getStrategy(stream), output);
Eric Laurentc75307b2015-03-17 15:29:32 -07001203 } else {
1204 newDevice = AUDIO_DEVICE_NONE;
1205 }
1206
1207 uint32_t delayMs = 0;
1208
Eric Laurentc40d9692016-04-13 19:14:13 -07001209 status_t status = startSource(outputDesc, stream, newDevice, address, &delayMs);
Eric Laurentc75307b2015-03-17 15:29:32 -07001210
1211 if (status != NO_ERROR) {
1212 mOutputRoutes.decRouteActivity(session);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001213 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -07001214 }
1215 // Automatically enable the remote submix input when output is started on a re routing mix
1216 // of type MIX_TYPE_RECORDERS
Eric Laurentc40d9692016-04-13 19:14:13 -07001217 if (audio_is_remote_submix_device(newDevice) && policyMix != NULL &&
1218 policyMix->mMixType == MIX_TYPE_RECORDERS) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001219 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1220 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Eric Laurentc40d9692016-04-13 19:14:13 -07001221 address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001222 "remote-submix");
1223 }
1224
1225 if (delayMs != 0) {
1226 usleep(delayMs * 1000);
1227 }
1228
1229 return status;
1230}
1231
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001232status_t AudioPolicyManager::startSource(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurentc75307b2015-03-17 15:29:32 -07001233 audio_stream_type_t stream,
1234 audio_devices_t device,
Eric Laurentc40d9692016-04-13 19:14:13 -07001235 const char *address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001236 uint32_t *delayMs)
1237{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001238 // cannot start playback of STREAM_TTS if any other output is being used
1239 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001240
1241 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001242 if (stream == AUDIO_STREAM_TTS) {
1243 ALOGV("\t found BEACON stream");
Eric Laurent9459fb02015-08-12 18:36:32 -07001244 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001245 return INVALID_OPERATION;
1246 } else {
1247 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1248 }
1249 } else {
1250 // some playback other than beacon starts
1251 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1252 }
1253
Eric Laurent77305a62016-07-25 16:39:22 -07001254 // force device change if the output is inactive and no audio patch is already present.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001255 // check active before incrementing usage count
Eric Laurent77305a62016-07-25 16:39:22 -07001256 bool force = !outputDesc->isActive() &&
1257 (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001258
Eric Laurente552edb2014-03-10 17:42:56 -07001259 // increment usage count for this stream on the requested output:
1260 // NOTE that the usage count is the same for duplicated output and hardware output which is
1261 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1262 outputDesc->changeRefCount(stream, 1);
1263
Eric Laurent36829f92017-04-07 19:04:42 -07001264 if (stream == AUDIO_STREAM_MUSIC) {
1265 selectOutputForMusicEffects();
1266 }
1267
Eric Laurent493404d2015-04-21 15:07:36 -07001268 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001269 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001270 if (device == AUDIO_DEVICE_NONE) {
1271 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001272 }
Eric Laurent36829f92017-04-07 19:04:42 -07001273
Eric Laurente552edb2014-03-10 17:42:56 -07001274 routing_strategy strategy = getStrategy(stream);
1275 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001276 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1277 (beaconMuteLatency > 0);
1278 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001279 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001280 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001281 if (desc != outputDesc) {
Eric Laurent77305a62016-07-25 16:39:22 -07001282 // force a device change if any other output is:
1283 // - managed by the same hw module
1284 // - has a current device selection that differs from selected device.
1285 // - supports currently selected device
1286 // - has an active audio patch
Eric Laurente552edb2014-03-10 17:42:56 -07001287 // In this case, the audio HAL must receive the new device selection so that it can
1288 // change the device currently selected by the other active output.
1289 if (outputDesc->sharesHwModuleWith(desc) &&
Eric Laurent77305a62016-07-25 16:39:22 -07001290 desc->device() != device &&
1291 desc->supportedDevices() & device &&
1292 desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07001293 force = true;
1294 }
1295 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001296 // a notification so that audio focus effect can propagate, or that a mute/unmute
1297 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001298 uint32_t latency = desc->latency();
1299 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1300 waitMs = latency;
1301 }
1302 }
1303 }
Eric Laurentdc462862016-07-19 12:29:53 -07001304 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force, 0, NULL, address);
Eric Laurente552edb2014-03-10 17:42:56 -07001305
1306 // handle special case for sonification while in call
1307 if (isInCall()) {
1308 handleIncallSonification(stream, true, false);
1309 }
1310
1311 // apply volume rules for current stream and device if necessary
1312 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01001313 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07001314 outputDesc,
1315 device);
Eric Laurente552edb2014-03-10 17:42:56 -07001316
1317 // update the outputs if starting an output with a stream that can affect notification
1318 // routing
1319 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001320
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001321 // force reevaluating accessibility routing when ringtone or alarm starts
1322 if (strategy == STRATEGY_SONIFICATION) {
1323 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1324 }
Eric Laurentdc462862016-07-19 12:29:53 -07001325
1326 if (waitMs > muteWaitMs) {
1327 *delayMs = waitMs - muteWaitMs;
1328 }
Eric Laurente552edb2014-03-10 17:42:56 -07001329 }
Eric Laurentdc462862016-07-19 12:29:53 -07001330
Eric Laurente552edb2014-03-10 17:42:56 -07001331 return NO_ERROR;
1332}
1333
1334
Eric Laurente0720872014-03-11 09:30:41 -07001335status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001336 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001337 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001338{
1339 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1340 ssize_t index = mOutputs.indexOfKey(output);
1341 if (index < 0) {
1342 ALOGW("stopOutput() unknown output %d", output);
1343 return BAD_VALUE;
1344 }
1345
Eric Laurentc75307b2015-03-17 15:29:32 -07001346 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001347
Eric Laurentc75307b2015-03-17 15:29:32 -07001348 if (outputDesc->mRefCount[stream] == 1) {
1349 // Automatically disable the remote submix input when output is stopped on a
1350 // re routing mix of type MIX_TYPE_RECORDERS
1351 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1352 outputDesc->mPolicyMix != NULL &&
1353 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1354 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1355 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001356 outputDesc->mPolicyMix->mDeviceAddress,
Eric Laurentc75307b2015-03-17 15:29:32 -07001357 "remote-submix");
1358 }
1359 }
1360
1361 // Routing?
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001362 bool forceDeviceUpdate = false;
Eric Laurentc75307b2015-03-17 15:29:32 -07001363 if (outputDesc->mRefCount[stream] > 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001364 int activityCount = mOutputRoutes.decRouteActivity(session);
1365 forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0));
1366
1367 if (forceDeviceUpdate) {
1368 checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE);
1369 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001370 }
1371
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001372 return stopSource(outputDesc, stream, forceDeviceUpdate);
Eric Laurentc75307b2015-03-17 15:29:32 -07001373}
1374
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001375status_t AudioPolicyManager::stopSource(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001376 audio_stream_type_t stream,
1377 bool forceDeviceUpdate)
Eric Laurentc75307b2015-03-17 15:29:32 -07001378{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001379 // always handle stream stop, check which stream type is stopping
1380 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1381
Eric Laurente552edb2014-03-10 17:42:56 -07001382 // handle special case for sonification while in call
1383 if (isInCall()) {
1384 handleIncallSonification(stream, false, false);
1385 }
1386
1387 if (outputDesc->mRefCount[stream] > 0) {
1388 // decrement usage count of this stream on the output
1389 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001390
Eric Laurente552edb2014-03-10 17:42:56 -07001391 // store time at which the stream was stopped - see isStreamActive()
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001392 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
Eric Laurente552edb2014-03-10 17:42:56 -07001393 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001394 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001395 // delay the device switch by twice the latency because stopOutput() is executed when
1396 // the track stop() command is received and at that time the audio track buffer can
1397 // still contain data that needs to be drained. The latency only covers the audio HAL
1398 // and kernel buffers. Also the latency does not always include additional delay in the
1399 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001400 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001401
1402 // force restoring the device selection on other active outputs if it differs from the
1403 // one being selected for this output
Eric Laurent57de36c2016-09-28 16:59:11 -07001404 uint32_t delayMs = outputDesc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -07001405 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001406 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001407 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001408 desc->isActive() &&
1409 outputDesc->sharesHwModuleWith(desc) &&
1410 (newDevice != desc->device())) {
Haynes Mathew George11c499a2016-08-26 12:08:25 -07001411 audio_devices_t newDevice2 = getNewOutputDevice(desc, false /*fromCache*/);
1412 bool force = desc->device() != newDevice2;
Eric Laurentc75307b2015-03-17 15:29:32 -07001413 setOutputDevice(desc,
Haynes Mathew George11c499a2016-08-26 12:08:25 -07001414 newDevice2,
1415 force,
Eric Laurent57de36c2016-09-28 16:59:11 -07001416 delayMs);
1417 // re-apply device specific volume if not done by setOutputDevice()
1418 if (!force) {
1419 applyStreamVolumes(desc, newDevice2, delayMs);
1420 }
Eric Laurente552edb2014-03-10 17:42:56 -07001421 }
1422 }
1423 // update the outputs if stopping one with a stream that can affect notification routing
1424 handleNotificationRoutingForStream(stream);
1425 }
Eric Laurent36829f92017-04-07 19:04:42 -07001426 if (stream == AUDIO_STREAM_MUSIC) {
1427 selectOutputForMusicEffects();
1428 }
Eric Laurente552edb2014-03-10 17:42:56 -07001429 return NO_ERROR;
1430 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001431 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001432 return INVALID_OPERATION;
1433 }
1434}
1435
Eric Laurente83b55d2014-11-14 10:06:21 -08001436void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001437 audio_stream_type_t stream __unused,
1438 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001439{
1440 ALOGV("releaseOutput() %d", output);
1441 ssize_t index = mOutputs.indexOfKey(output);
1442 if (index < 0) {
1443 ALOGW("releaseOutput() releasing unknown output %d", output);
1444 return;
1445 }
1446
1447#ifdef AUDIO_POLICY_TEST
1448 int testIndex = testOutputIndex(output);
1449 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001450 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001451 if (outputDesc->isActive()) {
1452 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001453 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001454 mTestOutputs[testIndex] = 0;
1455 }
1456 return;
1457 }
1458#endif //AUDIO_POLICY_TEST
1459
Paul McLeanaa981192015-03-21 09:55:15 -07001460 // Routing
1461 mOutputRoutes.removeRoute(session);
1462
Eric Laurentc75307b2015-03-17 15:29:32 -07001463 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001464 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001465 if (desc->mDirectOpenCount <= 0) {
1466 ALOGW("releaseOutput() invalid open count %d for output %d",
1467 desc->mDirectOpenCount, output);
1468 return;
1469 }
1470 if (--desc->mDirectOpenCount == 0) {
1471 closeOutput(output);
Eric Laurentb52c1522014-05-20 11:27:36 -07001472 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001473 }
1474 }
1475}
1476
1477
Eric Laurentcaf7f482014-11-25 17:50:47 -08001478status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1479 audio_io_handle_t *input,
1480 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001481 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001482 const audio_config_base_t *config,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001483 audio_input_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -06001484 audio_port_handle_t selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001485 input_type_t *inputType,
1486 audio_port_handle_t *portId)
Eric Laurente552edb2014-03-10 17:42:56 -07001487{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001488 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1489 "session %d, flags %#x",
Eric Laurent20b9ef02016-12-05 11:03:16 -08001490 attr->source, config->sample_rate, config->format, config->channel_mask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001491
Eric Laurentcaf7f482014-11-25 17:50:47 -08001492 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001493 *inputType = API_INPUT_INVALID;
Eric Laurentfb66dd92016-01-28 18:32:03 -08001494
Eric Laurent275e8e92014-11-30 15:14:47 -08001495 audio_devices_t device;
1496 // handle legacy remote submix case where the address was not always specified
1497 String8 address = String8("");
Eric Laurentc447ded2015-01-06 08:47:05 -08001498 audio_source_t inputSource = attr->source;
1499 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001500 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001501
Eric Laurentc447ded2015-01-06 08:47:05 -08001502 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1503 inputSource = AUDIO_SOURCE_MIC;
1504 }
1505 halInputSource = inputSource;
1506
Eric Laurent20b9ef02016-12-05 11:03:16 -08001507 // TODO: check for existing client for this port ID
1508 if (*portId == AUDIO_PORT_HANDLE_NONE) {
1509 *portId = AudioPort::getNextUniqueId();
1510 }
1511
Paul McLean466dc8e2015-04-17 13:15:36 -06001512 // Explicit routing?
1513 sp<DeviceDescriptor> deviceDesc;
1514 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1515 if (mAvailableInputDevices[i]->getId() == selectedDeviceId) {
1516 deviceDesc = mAvailableInputDevices[i];
1517 break;
1518 }
1519 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001520 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001521
Eric Laurentc447ded2015-01-06 08:47:05 -08001522 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001523 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001524 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
François Gaffie036e1e92015-03-19 10:16:24 +01001525 if (ret != NO_ERROR) {
1526 return ret;
1527 }
1528 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001529 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1530 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001531 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001532 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001533 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001534 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001535 return BAD_VALUE;
1536 }
Eric Laurentc722f302014-12-10 11:21:49 -08001537 if (policyMix != NULL) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001538 address = policyMix->mDeviceAddress;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001539 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1540 // there is an external policy, but this input is attached to a mix of recorders,
1541 // meaning it receives audio injected into the framework, so the recorder doesn't
1542 // know about it and is therefore considered "legacy"
1543 *inputType = API_INPUT_LEGACY;
1544 } else {
1545 // recording a mix of players defined by an external policy, we're rerouting for
1546 // an external policy
1547 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1548 }
Eric Laurentc722f302014-12-10 11:21:49 -08001549 } else if (audio_is_remote_submix_device(device)) {
1550 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001551 *inputType = API_INPUT_MIX_CAPTURE;
Eric Laurent82db2692015-08-07 13:59:42 -07001552 } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
1553 *inputType = API_INPUT_TELEPHONY_RX;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001554 } else {
1555 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001556 }
Ravi Kumar Alamandab367f5b2015-08-25 08:21:37 -07001557
Eric Laurent599c7582015-12-07 18:05:55 -08001558 }
1559
1560 *input = getInputForDevice(device, address, session, uid, inputSource,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001561 config->sample_rate, config->format, config->channel_mask, flags,
Eric Laurent599c7582015-12-07 18:05:55 -08001562 policyMix);
1563 if (*input == AUDIO_IO_HANDLE_NONE) {
1564 mInputRoutes.removeRoute(session);
1565 return INVALID_OPERATION;
1566 }
Eric Laurent20b9ef02016-12-05 11:03:16 -08001567
Eric Laurent599c7582015-12-07 18:05:55 -08001568 ALOGV("getInputForAttr() returns input type = %d", *inputType);
1569 return NO_ERROR;
1570}
1571
1572
1573audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device,
1574 String8 address,
1575 audio_session_t session,
1576 uid_t uid,
1577 audio_source_t inputSource,
1578 uint32_t samplingRate,
1579 audio_format_t format,
1580 audio_channel_mask_t channelMask,
1581 audio_input_flags_t flags,
1582 AudioMix *policyMix)
1583{
1584 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
1585 audio_source_t halInputSource = inputSource;
1586 bool isSoundTrigger = false;
1587
1588 if (inputSource == AUDIO_SOURCE_HOTWORD) {
1589 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1590 if (index >= 0) {
1591 input = mSoundTriggerSessions.valueFor(session);
1592 isSoundTrigger = true;
1593 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1594 ALOGV("SoundTrigger capture on session %d input %d", session, input);
1595 } else {
1596 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001597 }
1598 }
1599
Andy Hungf129b032015-04-07 13:45:50 -07001600 // find a compatible input profile (not necessarily identical in parameters)
1601 sp<IOProfile> profile;
1602 // samplingRate and flags may be updated by getInputProfile
Glenn Kasten05ddca52016-02-11 08:17:12 -08001603 uint32_t profileSamplingRate = (samplingRate == 0) ? SAMPLE_RATE_HZ_DEFAULT : samplingRate;
Andy Hungf129b032015-04-07 13:45:50 -07001604 audio_format_t profileFormat = format;
1605 audio_channel_mask_t profileChannelMask = channelMask;
1606 audio_input_flags_t profileFlags = flags;
1607 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001608 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001609 profileSamplingRate, profileFormat, profileChannelMask,
1610 profileFlags);
1611 if (profile != 0) {
1612 break; // success
Eric Laurent05067782016-06-01 18:27:28 -07001613 } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) {
1614 profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry
Andy Hungf129b032015-04-07 13:45:50 -07001615 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1616 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1617 } else { // fail
Eric Laurent599c7582015-12-07 18:05:55 -08001618 ALOGW("getInputForDevice() could not find profile for device 0x%X,"
1619 "samplingRate %u, format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001620 device, samplingRate, format, channelMask, flags);
Eric Laurent599c7582015-12-07 18:05:55 -08001621 return input;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001622 }
Eric Laurente552edb2014-03-10 17:42:56 -07001623 }
Glenn Kasten05ddca52016-02-11 08:17:12 -08001624 // Pick input sampling rate if not specified by client
1625 if (samplingRate == 0) {
1626 samplingRate = profileSamplingRate;
1627 }
Eric Laurente552edb2014-03-10 17:42:56 -07001628
Eric Laurent322b4d22015-04-03 15:57:54 -07001629 if (profile->getModuleHandle() == 0) {
1630 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurent599c7582015-12-07 18:05:55 -08001631 return input;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001632 }
1633
Eric Laurent599c7582015-12-07 18:05:55 -08001634 sp<AudioSession> audioSession = new AudioSession(session,
1635 inputSource,
1636 format,
1637 samplingRate,
1638 channelMask,
1639 flags,
1640 uid,
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001641 isSoundTrigger,
1642 policyMix, mpClientInterface);
Eric Laurent599c7582015-12-07 18:05:55 -08001643
Eric Laurent74708e72017-04-07 17:13:42 -07001644// FIXME: disable concurrent capture until UI is ready
1645#if 0
Eric Laurent599c7582015-12-07 18:05:55 -08001646 // reuse an open input if possible
Eric Laurent555530a2017-02-07 18:17:24 -08001647 sp<AudioInputDescriptor> reusedInputDesc;
Eric Laurent599c7582015-12-07 18:05:55 -08001648 for (size_t i = 0; i < mInputs.size(); i++) {
1649 sp<AudioInputDescriptor> desc = mInputs.valueAt(i);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001650 // reuse input if:
1651 // - it shares the same profile
1652 // AND
1653 // - it is not a reroute submix input
1654 // AND
1655 // - it is: not used for sound trigger
1656 // OR
1657 // used for sound trigger and all clients use the same session ID
1658 //
1659 if ((profile == desc->mProfile) &&
1660 (isSoundTrigger == desc->isSoundTrigger()) &&
1661 !is_virtual_input_device(device)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001662
1663 sp<AudioSession> as = desc->getAudioSession(session);
1664 if (as != 0) {
1665 // do not allow unmatching properties on same session
1666 if (as->matches(audioSession)) {
1667 as->changeOpenCount(1);
1668 } else {
1669 ALOGW("getInputForDevice() record with different attributes"
1670 " exists for session %d", session);
Eric Laurent555530a2017-02-07 18:17:24 -08001671 continue;
Eric Laurent599c7582015-12-07 18:05:55 -08001672 }
Eric Laurentfb66dd92016-01-28 18:32:03 -08001673 } else if (isSoundTrigger) {
Eric Laurent555530a2017-02-07 18:17:24 -08001674 continue;
Eric Laurentfb66dd92016-01-28 18:32:03 -08001675 }
Eric Laurentbb948092017-01-23 18:33:30 -08001676
Eric Laurent555530a2017-02-07 18:17:24 -08001677 // Reuse the already opened input stream on this profile if:
1678 // - the new capture source is background OR
1679 // - the path requested configurations match OR
1680 // - the new source priority is less than the highest source priority on this input
1681 // If the input stream cannot be reused, close it before opening a new stream
1682 // on the same profile for the new client so that the requested path configuration
1683 // can be selected.
1684 if (!isConcurrentSource(inputSource) &&
Eric Laurentbb948092017-01-23 18:33:30 -08001685 ((desc->mSamplingRate != samplingRate ||
Eric Laurentfb66dd92016-01-28 18:32:03 -08001686 desc->mChannelMask != channelMask ||
Eric Laurente6930022016-02-11 10:20:40 -08001687 !audio_formats_match(desc->mFormat, format)) &&
Eric Laurentfb66dd92016-01-28 18:32:03 -08001688 (source_priority(desc->getHighestPrioritySource(false /*activeOnly*/)) <
Eric Laurentbb948092017-01-23 18:33:30 -08001689 source_priority(inputSource)))) {
Eric Laurent555530a2017-02-07 18:17:24 -08001690 reusedInputDesc = desc;
1691 continue;
Eric Laurent599c7582015-12-07 18:05:55 -08001692 } else {
1693 desc->addAudioSession(session, audioSession);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001694 ALOGV("%s: reusing input %d", __FUNCTION__, mInputs.keyAt(i));
1695 return mInputs.keyAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08001696 }
Eric Laurent599c7582015-12-07 18:05:55 -08001697 }
1698 }
Eric Laurent599c7582015-12-07 18:05:55 -08001699
Eric Laurent555530a2017-02-07 18:17:24 -08001700 if (reusedInputDesc != 0) {
1701 AudioSessionCollection sessions = reusedInputDesc->getAudioSessions(false /*activeOnly*/);
1702 for (size_t j = 0; j < sessions.size(); j++) {
1703 audio_session_t currentSession = sessions.keyAt(j);
1704 stopInput(reusedInputDesc->mIoHandle, currentSession);
1705 releaseInput(reusedInputDesc->mIoHandle, currentSession);
1706 }
1707 }
Eric Laurent74708e72017-04-07 17:13:42 -07001708#endif
Eric Laurent555530a2017-02-07 18:17:24 -08001709
Eric Laurentcf2c0212014-07-25 16:20:43 -07001710 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001711 config.sample_rate = profileSamplingRate;
1712 config.channel_mask = profileChannelMask;
1713 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001714
Eric Laurente3014102017-05-03 11:15:43 -07001715 if (address == "") {
1716 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(device);
1717 // the inputs vector must be of size 1, but we don't want to crash here
1718 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress : String8("");
1719 }
1720
Eric Laurent322b4d22015-04-03 15:57:54 -07001721 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurent599c7582015-12-07 18:05:55 -08001722 &input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001723 &config,
1724 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001725 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001726 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001727 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001728
1729 // only accept input with the exact requested set of parameters
Eric Laurent599c7582015-12-07 18:05:55 -08001730 if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001731 (profileSamplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -08001732 !audio_formats_match(profileFormat, config.format) ||
Andy Hungf129b032015-04-07 13:45:50 -07001733 (profileChannelMask != config.channel_mask)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001734 ALOGW("getInputForAttr() failed opening input: samplingRate %d"
1735 ", format %d, channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001736 samplingRate, format, channelMask);
Eric Laurent599c7582015-12-07 18:05:55 -08001737 if (input != AUDIO_IO_HANDLE_NONE) {
1738 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001739 }
Eric Laurent599c7582015-12-07 18:05:55 -08001740 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001741 }
1742
Eric Laurent1f2f2232014-06-02 12:01:23 -07001743 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Andy Hungf129b032015-04-07 13:45:50 -07001744 inputDesc->mSamplingRate = profileSamplingRate;
1745 inputDesc->mFormat = profileFormat;
1746 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001747 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001748 inputDesc->mPolicyMix = policyMix;
Eric Laurent599c7582015-12-07 18:05:55 -08001749 inputDesc->addAudioSession(session, audioSession);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001750
Eric Laurent599c7582015-12-07 18:05:55 -08001751 addInput(input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001752 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001753
Eric Laurent599c7582015-12-07 18:05:55 -08001754 return input;
Eric Laurente552edb2014-03-10 17:42:56 -07001755}
1756
Eric Laurentbb948092017-01-23 18:33:30 -08001757//static
1758bool AudioPolicyManager::isConcurrentSource(audio_source_t source)
1759{
1760 return (source == AUDIO_SOURCE_HOTWORD) ||
1761 (source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
1762 (source == AUDIO_SOURCE_FM_TUNER);
1763}
1764
Eric Laurentfb66dd92016-01-28 18:32:03 -08001765bool AudioPolicyManager::isConcurentCaptureAllowed(const sp<AudioInputDescriptor>& inputDesc,
1766 const sp<AudioSession>& audioSession)
1767{
1768 // Do not allow capture if an active voice call is using a software patch and
1769 // the call TX source device is on the same HW module.
1770 // FIXME: would be better to refine to only inputs whose profile connects to the
1771 // call TX device but this information is not in the audio patch
1772 if (mCallTxPatch != 0 &&
1773 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1774 return false;
1775 }
1776
1777 // starting concurrent capture is enabled if:
1778 // 1) capturing for re-routing
1779 // 2) capturing for HOTWORD source
1780 // 3) capturing for FM TUNER source
1781 // 3) All other active captures are either for re-routing or HOTWORD
1782
1783 if (is_virtual_input_device(inputDesc->mDevice) ||
Eric Laurentbb948092017-01-23 18:33:30 -08001784 isConcurrentSource(audioSession->inputSource())) {
Eric Laurentfb66dd92016-01-28 18:32:03 -08001785 return true;
1786 }
1787
1788 Vector< sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
1789 for (size_t i = 0; i < activeInputs.size(); i++) {
1790 sp<AudioInputDescriptor> activeInput = activeInputs[i];
Eric Laurentbb948092017-01-23 18:33:30 -08001791 if (!isConcurrentSource(activeInput->inputSource(true)) &&
Eric Laurentfb66dd92016-01-28 18:32:03 -08001792 !is_virtual_input_device(activeInput->mDevice)) {
1793 return false;
1794 }
1795 }
1796
1797 return true;
1798}
1799
1800
Eric Laurent4dc68062014-07-28 17:26:49 -07001801status_t AudioPolicyManager::startInput(audio_io_handle_t input,
Eric Laurentfb66dd92016-01-28 18:32:03 -08001802 audio_session_t session,
1803 concurrency_type__mask_t *concurrency)
Eric Laurente552edb2014-03-10 17:42:56 -07001804{
1805 ALOGV("startInput() input %d", input);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001806 *concurrency = API_INPUT_CONCURRENCY_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001807 ssize_t index = mInputs.indexOfKey(input);
1808 if (index < 0) {
1809 ALOGW("startInput() unknown input %d", input);
1810 return BAD_VALUE;
1811 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001812 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001813
Eric Laurent599c7582015-12-07 18:05:55 -08001814 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1815 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07001816 ALOGW("startInput() unknown session %d on input %d", session, input);
1817 return BAD_VALUE;
1818 }
1819
Eric Laurent74708e72017-04-07 17:13:42 -07001820// FIXME: disable concurrent capture until UI is ready
1821#if 0
Eric Laurentfb66dd92016-01-28 18:32:03 -08001822 if (!isConcurentCaptureAllowed(inputDesc, audioSession)) {
1823 ALOGW("startInput(%d) failed: other input already started", input);
1824 return INVALID_OPERATION;
1825 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001826
Eric Laurentfb66dd92016-01-28 18:32:03 -08001827 if (isInCall()) {
1828 *concurrency |= API_INPUT_CONCURRENCY_CALL;
1829 }
Eric Laurentf7c50102016-09-30 15:19:43 -07001830 if (mInputs.activeInputsCountOnDevices() != 0) {
Eric Laurentfb66dd92016-01-28 18:32:03 -08001831 *concurrency |= API_INPUT_CONCURRENCY_CAPTURE;
Eric Laurente552edb2014-03-10 17:42:56 -07001832 }
Eric Laurent74708e72017-04-07 17:13:42 -07001833#else
1834 if (!is_virtual_input_device(inputDesc->mDevice)) {
1835 if (mCallTxPatch != 0 &&
1836 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1837 ALOGW("startInput(%d) failed: call in progress", input);
1838 return INVALID_OPERATION;
1839 }
1840
1841 Vector< sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
1842 for (size_t i = 0; i < activeInputs.size(); i++) {
1843 sp<AudioInputDescriptor> activeDesc = activeInputs[i];
1844
1845 if (is_virtual_input_device(activeDesc->mDevice)) {
1846 continue;
1847 }
1848
1849 audio_source_t activeSource = activeDesc->inputSource(true);
1850 if (audioSession->inputSource() == AUDIO_SOURCE_HOTWORD) {
1851 if (activeSource == AUDIO_SOURCE_HOTWORD) {
1852 if (activeDesc->hasPreemptedSession(session)) {
1853 ALOGW("startInput(%d) failed for HOTWORD: "
1854 "other input %d already started for HOTWORD",
1855 input, activeDesc->mIoHandle);
1856 return INVALID_OPERATION;
1857 }
1858 } else {
1859 ALOGV("startInput(%d) failed for HOTWORD: other input %d already started",
1860 input, activeDesc->mIoHandle);
1861 return INVALID_OPERATION;
1862 }
1863 } else {
1864 if (activeSource != AUDIO_SOURCE_HOTWORD) {
1865 ALOGW("startInput(%d) failed: other input %d already started",
1866 input, activeDesc->mIoHandle);
1867 return INVALID_OPERATION;
1868 }
1869 }
1870 }
1871
1872 // if capture is allowed, preempt currently active HOTWORD captures
1873 for (size_t i = 0; i < activeInputs.size(); i++) {
1874 sp<AudioInputDescriptor> activeDesc = activeInputs[i];
1875
1876 if (is_virtual_input_device(activeDesc->mDevice)) {
1877 continue;
1878 }
1879
1880 audio_source_t activeSource = activeDesc->inputSource(true);
1881 if (activeSource == AUDIO_SOURCE_HOTWORD) {
1882 AudioSessionCollection activeSessions =
1883 activeDesc->getAudioSessions(true /*activeOnly*/);
1884 audio_session_t activeSession = activeSessions.keyAt(0);
1885 audio_io_handle_t activeHandle = activeDesc->mIoHandle;
1886 SortedVector<audio_session_t> sessions = activeDesc->getPreemptedSessions();
1887 sessions.add(activeSession);
1888 inputDesc->setPreemptedSessions(sessions);
1889 stopInput(activeHandle, activeSession);
1890 releaseInput(activeHandle, activeSession);
1891 ALOGV("startInput(%d) for HOTWORD preempting HOTWORD input %d",
1892 input, activeDesc->mIoHandle);
1893 }
1894 }
1895 }
1896#endif
Eric Laurente552edb2014-03-10 17:42:56 -07001897
Eric Laurent313d1e72016-01-29 09:56:57 -08001898 // increment activity count before calling getNewInputDevice() below as only active sessions
1899 // are considered for device selection
1900 audioSession->changeActiveCount(1);
1901
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001902 // Routing?
1903 mInputRoutes.incRouteActivity(session);
1904
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001905 if (audioSession->activeCount() == 1 || mInputRoutes.hasRouteChanged(session)) {
Eric Laurent271a93e2016-09-29 14:47:06 -07001906 // indicate active capture to sound trigger service if starting capture from a mic on
1907 // primary HW module
Eric Laurentf7c50102016-09-30 15:19:43 -07001908 audio_devices_t device = getNewInputDevice(inputDesc);
Eric Laurent271a93e2016-09-29 14:47:06 -07001909 setInputDevice(input, device, true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001910
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001911 if (inputDesc->getAudioSessionCount(true/*activeOnly*/) == 1) {
1912 // if input maps to a dynamic policy with an activity listener, notify of state change
1913 if ((inputDesc->mPolicyMix != NULL)
1914 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1915 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
1916 MIX_STATE_MIXING);
Eric Laurentc722f302014-12-10 11:21:49 -08001917 }
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001918
Eric Laurentf7c50102016-09-30 15:19:43 -07001919 audio_devices_t primaryInputDevices = availablePrimaryInputDevices();
1920 if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) &&
Eric Laurent05b345e2016-11-18 12:36:27 -08001921 mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) {
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001922 SoundTrigger::setCaptureState(true);
1923 }
1924
1925 // automatically enable the remote submix output when input is started if not
1926 // used by a policy mix of type MIX_TYPE_RECORDERS
1927 // For remote submix (a virtual device), we open only one input per capture request.
1928 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1929 String8 address = String8("");
1930 if (inputDesc->mPolicyMix == NULL) {
1931 address = String8("0");
1932 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1933 address = inputDesc->mPolicyMix->mDeviceAddress;
1934 }
1935 if (address != "") {
1936 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1937 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1938 address, "remote-submix");
1939 }
Eric Laurentc722f302014-12-10 11:21:49 -08001940 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001941 }
Eric Laurente552edb2014-03-10 17:42:56 -07001942 }
1943
Eric Laurent599c7582015-12-07 18:05:55 -08001944 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Eric Laurente552edb2014-03-10 17:42:56 -07001945
Eric Laurente552edb2014-03-10 17:42:56 -07001946 return NO_ERROR;
1947}
1948
Eric Laurent4dc68062014-07-28 17:26:49 -07001949status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1950 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001951{
1952 ALOGV("stopInput() input %d", input);
1953 ssize_t index = mInputs.indexOfKey(input);
1954 if (index < 0) {
1955 ALOGW("stopInput() unknown input %d", input);
1956 return BAD_VALUE;
1957 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001958 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001959
Eric Laurent599c7582015-12-07 18:05:55 -08001960 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001961 if (index < 0) {
1962 ALOGW("stopInput() unknown session %d on input %d", session, input);
1963 return BAD_VALUE;
1964 }
1965
Eric Laurent599c7582015-12-07 18:05:55 -08001966 if (audioSession->activeCount() == 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07001967 ALOGW("stopInput() input %d already stopped", input);
1968 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001969 }
1970
Eric Laurent599c7582015-12-07 18:05:55 -08001971 audioSession->changeActiveCount(-1);
Paul McLean466dc8e2015-04-17 13:15:36 -06001972
1973 // Routing?
1974 mInputRoutes.decRouteActivity(session);
1975
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001976 if (audioSession->activeCount() == 0) {
Eric Laurent84332aa2016-01-28 22:19:18 +00001977
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001978 if (inputDesc->isActive()) {
1979 setInputDevice(input, getNewInputDevice(inputDesc), false /* force */);
1980 } else {
1981 // if input maps to a dynamic policy with an activity listener, notify of state change
1982 if ((inputDesc->mPolicyMix != NULL)
1983 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1984 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
1985 MIX_STATE_IDLE);
Eric Laurent84332aa2016-01-28 22:19:18 +00001986 }
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001987
1988 // automatically disable the remote submix output when input is stopped if not
1989 // used by a policy mix of type MIX_TYPE_RECORDERS
1990 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1991 String8 address = String8("");
1992 if (inputDesc->mPolicyMix == NULL) {
1993 address = String8("0");
1994 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1995 address = inputDesc->mPolicyMix->mDeviceAddress;
1996 }
1997 if (address != "") {
1998 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1999 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2000 address, "remote-submix");
2001 }
Eric Laurent84332aa2016-01-28 22:19:18 +00002002 }
Eric Laurent84332aa2016-01-28 22:19:18 +00002003
Eric Laurentf7c50102016-09-30 15:19:43 -07002004 audio_devices_t device = inputDesc->mDevice;
Eric Laurentfb66dd92016-01-28 18:32:03 -08002005 resetInputDevice(input);
Eric Laurent84332aa2016-01-28 22:19:18 +00002006
Eric Laurentf7c50102016-09-30 15:19:43 -07002007 // indicate inactive capture to sound trigger service if stopping capture from a mic on
2008 // primary HW module
2009 audio_devices_t primaryInputDevices = availablePrimaryInputDevices();
2010 if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) &&
2011 mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) {
Eric Laurentfb66dd92016-01-28 18:32:03 -08002012 SoundTrigger::setCaptureState(false);
2013 }
2014 inputDesc->clearPreemptedSessions();
Eric Laurent84332aa2016-01-28 22:19:18 +00002015 }
Eric Laurente552edb2014-03-10 17:42:56 -07002016 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002017 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07002018}
2019
Eric Laurent4dc68062014-07-28 17:26:49 -07002020void AudioPolicyManager::releaseInput(audio_io_handle_t input,
2021 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07002022{
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002023
Eric Laurente552edb2014-03-10 17:42:56 -07002024 ALOGV("releaseInput() %d", input);
2025 ssize_t index = mInputs.indexOfKey(input);
2026 if (index < 0) {
2027 ALOGW("releaseInput() releasing unknown input %d", input);
2028 return;
2029 }
Paul McLean466dc8e2015-04-17 13:15:36 -06002030
2031 // Routing
2032 mInputRoutes.removeRoute(session);
2033
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002034 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
2035 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07002036
Eric Laurent599c7582015-12-07 18:05:55 -08002037 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
vivek mehta587b8df2017-01-31 14:58:44 -08002038 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07002039 ALOGW("releaseInput() unknown session %d on input %d", session, input);
2040 return;
2041 }
Eric Laurent599c7582015-12-07 18:05:55 -08002042
2043 if (audioSession->openCount() == 0) {
2044 ALOGW("releaseInput() invalid open count %d on session %d",
2045 audioSession->openCount(), session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002046 return;
2047 }
Eric Laurent599c7582015-12-07 18:05:55 -08002048
2049 if (audioSession->changeOpenCount(-1) == 0) {
2050 inputDesc->removeAudioSession(session);
2051 }
2052
Jean-Michel Trivi65bfe912015-12-04 15:56:47 -08002053 if (inputDesc->getOpenRefCount() > 0) {
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002054 ALOGV("releaseInput() exit > 0");
2055 return;
2056 }
2057
Eric Laurent05b90f82014-08-27 15:32:29 -07002058 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07002059 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07002060 ALOGV("releaseInput() exit");
2061}
2062
Eric Laurentd4692962014-05-05 18:13:44 -07002063void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07002064 bool patchRemoved = false;
2065
Eric Laurentd4692962014-05-05 18:13:44 -07002066 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07002067 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08002068 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07002069 if (patch_index >= 0) {
2070 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07002071 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07002072 mAudioPatches.removeItemsAt(patch_index);
2073 patchRemoved = true;
2074 }
Eric Laurentd4692962014-05-05 18:13:44 -07002075 mpClientInterface->closeInput(mInputs.keyAt(input_index));
2076 }
2077 mInputs.clear();
Chris Thornton52785f32016-02-09 17:28:49 -08002078 SoundTrigger::setCaptureState(false);
Eric Laurent6a94d692014-05-20 11:18:06 -07002079 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07002080
2081 if (patchRemoved) {
2082 mpClientInterface->onAudioPatchListUpdate();
2083 }
Eric Laurentd4692962014-05-05 18:13:44 -07002084}
2085
Eric Laurente0720872014-03-11 09:30:41 -07002086void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07002087 int indexMin,
2088 int indexMax)
2089{
2090 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002091 mVolumeCurves->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent28d09f02016-03-08 10:43:05 -08002092
2093 // initialize other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08002094 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
2095 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002096 continue;
2097 }
2098 mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08002099 }
Eric Laurente552edb2014-03-10 17:42:56 -07002100}
2101
Eric Laurente0720872014-03-11 09:30:41 -07002102status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01002103 int index,
2104 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07002105{
2106
François Gaffied1ab2bd2015-12-02 18:20:06 +01002107 if ((index < mVolumeCurves->getVolumeIndexMin(stream)) ||
2108 (index > mVolumeCurves->getVolumeIndexMax(stream))) {
Eric Laurente552edb2014-03-10 17:42:56 -07002109 return BAD_VALUE;
2110 }
2111 if (!audio_is_output_device(device)) {
2112 return BAD_VALUE;
2113 }
2114
2115 // Force max volume if stream cannot be muted
François Gaffied1ab2bd2015-12-02 18:20:06 +01002116 if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07002117
Eric Laurent1fd372e2016-04-06 14:23:53 -07002118 ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d",
Eric Laurente552edb2014-03-10 17:42:56 -07002119 stream, device, index);
2120
Eric Laurent28d09f02016-03-08 10:43:05 -08002121 // update other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08002122 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
2123 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002124 continue;
2125 }
2126 mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index);
2127 }
Eric Laurente552edb2014-03-10 17:42:56 -07002128
Eric Laurent1fd372e2016-04-06 14:23:53 -07002129 // update volume on all outputs and streams matching the following:
2130 // - The requested stream (or a stream matching for volume control) is active on the output
2131 // - The device (or devices) selected by the strategy corresponding to this stream includes
2132 // the requested device
2133 // - For non default requested device, currently selected device on the output is either the
2134 // requested device or one of the devices selected by the strategy
Eric Laurent5a2b6292016-04-14 18:05:57 -07002135 // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if
2136 // no specific device volume value exists for currently selected device.
Eric Laurente552edb2014-03-10 17:42:56 -07002137 status_t status = NO_ERROR;
2138 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002139 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
2140 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent794fde22016-03-11 09:50:45 -08002141 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
2142 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002143 continue;
Eric Laurente552edb2014-03-10 17:42:56 -07002144 }
Eric Laurentd3926fe2016-04-15 18:10:07 -07002145 if (!(desc->isStreamActive((audio_stream_type_t)curStream) ||
2146 (isInCall() && (curStream == AUDIO_STREAM_VOICE_CALL)))) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07002147 continue;
2148 }
Eric Laurent794fde22016-03-11 09:50:45 -08002149 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurente76f29c2016-09-14 17:17:53 -07002150 audio_devices_t curStreamDevice = getDeviceForStrategy(curStrategy, false /*fromCache*/);
2151 if ((device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) &&
2152 ((curStreamDevice & device) == 0)) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07002153 continue;
2154 }
Eric Laurente76f29c2016-09-14 17:17:53 -07002155 bool applyVolume;
Eric Laurent5a2b6292016-04-14 18:05:57 -07002156 if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07002157 curStreamDevice |= device;
Eric Laurente76f29c2016-09-14 17:17:53 -07002158 applyVolume = (curDevice & curStreamDevice) != 0;
2159 } else {
2160 applyVolume = !mVolumeCurves->hasVolumeIndexForDevice(
2161 stream, Volume::getDeviceForVolume(curStreamDevice));
Eric Laurent1fd372e2016-04-06 14:23:53 -07002162 }
Eric Laurent28d09f02016-03-08 10:43:05 -08002163
Eric Laurente76f29c2016-09-14 17:17:53 -07002164 if (applyVolume) {
Eric Laurentdc462862016-07-19 12:29:53 -07002165 //FIXME: workaround for truncated touch sounds
2166 // delayed volume change for system stream to be removed when the problem is
2167 // handled by system UI
Eric Laurent28d09f02016-03-08 10:43:05 -08002168 status_t volStatus =
Eric Laurentdc462862016-07-19 12:29:53 -07002169 checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice,
2170 (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0);
Eric Laurent28d09f02016-03-08 10:43:05 -08002171 if (volStatus != NO_ERROR) {
2172 status = volStatus;
2173 }
2174 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08002175 }
Eric Laurente552edb2014-03-10 17:42:56 -07002176 }
2177 return status;
2178}
2179
Eric Laurente0720872014-03-11 09:30:41 -07002180status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07002181 int *index,
2182 audio_devices_t device)
2183{
2184 if (index == NULL) {
2185 return BAD_VALUE;
2186 }
2187 if (!audio_is_output_device(device)) {
2188 return BAD_VALUE;
2189 }
Eric Laurent5a2b6292016-04-14 18:05:57 -07002190 // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device corresponding to
Eric Laurente552edb2014-03-10 17:42:56 -07002191 // the strategy the stream belongs to.
Eric Laurent5a2b6292016-04-14 18:05:57 -07002192 if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurente552edb2014-03-10 17:42:56 -07002193 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
2194 }
François Gaffiedfd74092015-03-19 12:10:59 +01002195 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07002196
François Gaffied1ab2bd2015-12-02 18:20:06 +01002197 *index = mVolumeCurves->getVolumeIndex(stream, device);
Eric Laurente552edb2014-03-10 17:42:56 -07002198 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
2199 return NO_ERROR;
2200}
2201
Eric Laurent36829f92017-04-07 19:04:42 -07002202audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects()
Eric Laurente552edb2014-03-10 17:42:56 -07002203{
2204 // select one output among several suitable for global effects.
2205 // The priority is as follows:
2206 // 1: An offloaded output. If the effect ends up not being offloadable,
2207 // AudioFlinger will invalidate the track and the offloaded output
2208 // will be closed causing the effect to be moved to a PCM output.
2209 // 2: A deep buffer output
Eric Laurent36829f92017-04-07 19:04:42 -07002210 // 3: The primary output
2211 // 4: the first output in the list
Eric Laurente552edb2014-03-10 17:42:56 -07002212
Eric Laurent3b73df72014-03-11 09:06:29 -07002213 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07002214 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent36829f92017-04-07 19:04:42 -07002215 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
Eric Laurente552edb2014-03-10 17:42:56 -07002216
Eric Laurent36829f92017-04-07 19:04:42 -07002217 if (outputs.size() == 0) {
2218 return AUDIO_IO_HANDLE_NONE;
2219 }
Eric Laurente552edb2014-03-10 17:42:56 -07002220
Eric Laurent36829f92017-04-07 19:04:42 -07002221 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
2222 bool activeOnly = true;
2223
2224 while (output == AUDIO_IO_HANDLE_NONE) {
2225 audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE;
2226 audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE;
2227 audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE;
2228
2229 for (size_t i = 0; i < outputs.size(); i++) {
2230 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
2231 if (activeOnly && !desc->isStreamActive(AUDIO_STREAM_MUSIC)) {
2232 continue;
2233 }
2234 ALOGV("selectOutputForMusicEffects activeOnly %d outputs[%zu] flags 0x%08x",
2235 activeOnly, i, desc->mFlags);
2236 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
2237 outputOffloaded = outputs[i];
2238 }
2239 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
2240 outputDeepBuffer = outputs[i];
2241 }
2242 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) {
2243 outputPrimary = outputs[i];
2244 }
2245 }
2246 if (outputOffloaded != AUDIO_IO_HANDLE_NONE) {
2247 output = outputOffloaded;
2248 } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) {
2249 output = outputDeepBuffer;
2250 } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) {
2251 output = outputPrimary;
2252 } else {
2253 output = outputs[0];
2254 }
2255 activeOnly = false;
2256 }
2257
2258 if (output != mMusicEffectOutput) {
2259 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output);
2260 mMusicEffectOutput = output;
2261 }
2262
2263 ALOGV("selectOutputForMusicEffects selected output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -07002264 return output;
2265}
2266
Eric Laurent36829f92017-04-07 19:04:42 -07002267audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused)
2268{
2269 return selectOutputForMusicEffects();
2270}
2271
Eric Laurente0720872014-03-11 09:30:41 -07002272status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07002273 audio_io_handle_t io,
2274 uint32_t strategy,
2275 int session,
2276 int id)
2277{
2278 ssize_t index = mOutputs.indexOfKey(io);
2279 if (index < 0) {
2280 index = mInputs.indexOfKey(io);
2281 if (index < 0) {
2282 ALOGW("registerEffect() unknown io %d", io);
2283 return INVALID_OPERATION;
2284 }
2285 }
François Gaffie45ed3b02015-03-19 10:35:14 +01002286 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07002287}
2288
Eric Laurentc75307b2015-03-17 15:29:32 -07002289bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
2290{
Eric Laurent28d09f02016-03-08 10:43:05 -08002291 bool active = false;
Eric Laurent794fde22016-03-11 09:50:45 -08002292 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) {
2293 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002294 continue;
2295 }
2296 active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs);
2297 }
Eric Laurent28d09f02016-03-08 10:43:05 -08002298 return active;
Eric Laurentc75307b2015-03-17 15:29:32 -07002299}
2300
2301bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
2302{
2303 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
2304}
2305
Eric Laurente0720872014-03-11 09:30:41 -07002306bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07002307{
2308 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002309 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002310 if (inputDescriptor->isSourceActive(source)) {
Eric Laurente552edb2014-03-10 17:42:56 -07002311 return true;
2312 }
2313 }
2314 return false;
2315}
2316
Eric Laurent275e8e92014-11-30 15:14:47 -08002317// Register a list of custom mixes with their attributes and format.
2318// When a mix is registered, corresponding input and output profiles are
2319// added to the remote submix hw module. The profile contains only the
2320// parameters (sampling rate, format...) specified by the mix.
2321// The corresponding input remote submix device is also connected.
2322//
2323// When a remote submix device is connected, the address is checked to select the
2324// appropriate profile and the corresponding input or output stream is opened.
2325//
2326// When capture starts, getInputForAttr() will:
2327// - 1 look for a mix matching the address passed in attribtutes tags if any
2328// - 2 if none found, getDeviceForInputSource() will:
2329// - 2.1 look for a mix matching the attributes source
2330// - 2.2 if none found, default to device selection by policy rules
2331// At this time, the corresponding output remote submix device is also connected
2332// and active playback use cases can be transferred to this mix if needed when reconnecting
2333// after AudioTracks are invalidated
2334//
2335// When playback starts, getOutputForAttr() will:
2336// - 1 look for a mix matching the address passed in attribtutes tags if any
2337// - 2 if none found, look for a mix matching the attributes usage
2338// - 3 if none found, default to device and output selection by policy rules.
2339
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07002340status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes)
Eric Laurent275e8e92014-11-30 15:14:47 -08002341{
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002342 ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size());
2343 status_t res = NO_ERROR;
2344
2345 sp<HwModule> rSubmixModule;
2346 // examine each mix's route type
2347 for (size_t i = 0; i < mixes.size(); i++) {
2348 // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination
2349 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) {
2350 res = INVALID_OPERATION;
Eric Laurent275e8e92014-11-30 15:14:47 -08002351 break;
2352 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002353 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
2354 // Loop back through "remote submix"
2355 if (rSubmixModule == 0) {
2356 for (size_t j = 0; i < mHwModules.size(); j++) {
2357 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2358 && mHwModules[j]->mHandle != 0) {
2359 rSubmixModule = mHwModules[j];
2360 break;
2361 }
2362 }
2363 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002364
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002365 ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size());
Eric Laurent275e8e92014-11-30 15:14:47 -08002366
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002367 if (rSubmixModule == 0) {
2368 ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", i);
2369 res = INVALID_OPERATION;
2370 break;
2371 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002372
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002373 String8 address = mixes[i].mDeviceAddress;
François Gaffie036e1e92015-03-19 10:16:24 +01002374
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002375 if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) {
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002376 ALOGE(" Error registering mix %zu for address %s", i, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002377 res = INVALID_OPERATION;
2378 break;
2379 }
2380 audio_config_t outputConfig = mixes[i].mFormat;
2381 audio_config_t inputConfig = mixes[i].mFormat;
2382 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
2383 // stereo and let audio flinger do the channel conversion if needed.
2384 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2385 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
2386 rSubmixModule->addOutputProfile(address, &outputConfig,
2387 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
2388 rSubmixModule->addInputProfile(address, &inputConfig,
2389 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01002390
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002391 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
2392 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2393 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2394 address.string(), "remote-submix");
2395 } else {
2396 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2397 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2398 address.string(), "remote-submix");
2399 }
2400 } else if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002401 String8 address = mixes[i].mDeviceAddress;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002402 audio_devices_t device = mixes[i].mDeviceType;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002403 ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s",
2404 i, mixes.size(), device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002405
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002406 bool foundOutput = false;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002407 for (size_t j = 0 ; j < mOutputs.size() ; j++) {
2408 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j);
2409 sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle());
2410 if ((patch != 0) && (patch->mPatch.num_sinks != 0)
2411 && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE)
2412 && (patch->mPatch.sinks[0].ext.device.type == device)
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002413 && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(),
2414 AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002415 if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) {
2416 res = INVALID_OPERATION;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002417 } else {
2418 foundOutput = true;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002419 }
2420 break;
2421 }
2422 }
2423
2424 if (res != NO_ERROR) {
2425 ALOGE(" Error registering mix %zu for device 0x%X addr %s",
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002426 i, device, address.string());
2427 res = INVALID_OPERATION;
2428 break;
2429 } else if (!foundOutput) {
2430 ALOGE(" Output not found for mix %zu for device 0x%X addr %s",
2431 i, device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002432 res = INVALID_OPERATION;
2433 break;
2434 }
Eric Laurentc722f302014-12-10 11:21:49 -08002435 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002436 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002437 if (res != NO_ERROR) {
2438 unregisterPolicyMixes(mixes);
2439 }
2440 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002441}
2442
2443status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
2444{
Eric Laurent7b279bb2015-12-14 10:18:23 -08002445 ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002446 status_t res = NO_ERROR;
2447 sp<HwModule> rSubmixModule;
2448 // examine each mix's route type
Eric Laurent275e8e92014-11-30 15:14:47 -08002449 for (size_t i = 0; i < mixes.size(); i++) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002450 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
François Gaffie036e1e92015-03-19 10:16:24 +01002451
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002452 if (rSubmixModule == 0) {
2453 for (size_t j = 0; i < mHwModules.size(); j++) {
2454 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2455 && mHwModules[j]->mHandle != 0) {
2456 rSubmixModule = mHwModules[j];
2457 break;
2458 }
2459 }
2460 }
2461 if (rSubmixModule == 0) {
2462 res = INVALID_OPERATION;
2463 continue;
2464 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002465
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002466 String8 address = mixes[i].mDeviceAddress;
Eric Laurent275e8e92014-11-30 15:14:47 -08002467
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002468 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
2469 res = INVALID_OPERATION;
2470 continue;
2471 }
2472
2473 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
2474 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2475 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2476 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2477 address.string(), "remote-submix");
2478 }
2479 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
2480 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2481 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2482 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2483 address.string(), "remote-submix");
2484 }
2485 rSubmixModule->removeOutputProfile(address);
2486 rSubmixModule->removeInputProfile(address);
2487
2488 } if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
2489 if (mPolicyMixes.unregisterMix(mixes[i].mDeviceAddress) != NO_ERROR) {
2490 res = INVALID_OPERATION;
2491 continue;
2492 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002493 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002494 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002495 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002496}
2497
Eric Laurente552edb2014-03-10 17:42:56 -07002498
Eric Laurente0720872014-03-11 09:30:41 -07002499status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07002500{
2501 const size_t SIZE = 256;
2502 char buffer[SIZE];
2503 String8 result;
2504
2505 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
2506 result.append(buffer);
2507
Eric Laurent87ffa392015-05-22 10:32:38 -07002508 snprintf(buffer, SIZE, " Primary Output: %d\n",
2509 hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE);
Eric Laurente552edb2014-03-10 17:42:56 -07002510 result.append(buffer);
Mikhail Naganov0d6a0332016-04-19 17:12:38 -07002511 std::string stateLiteral;
2512 AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral);
2513 snprintf(buffer, SIZE, " Phone state: %s\n", stateLiteral.c_str());
Eric Laurente552edb2014-03-10 17:42:56 -07002514 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002515 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002516 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07002517 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002518 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07002519 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002520 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07002521 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002522 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07002523 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002524 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07002525 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002526 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002527 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002528 result.append(buffer);
Phil Burk09bc4612016-02-24 15:58:15 -08002529 snprintf(buffer, SIZE, " Force use for encoded surround output %d\n",
2530 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND));
2531 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002532 snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available");
2533 result.append(buffer);
Andy Hung2ddee192015-12-18 17:34:44 -08002534 snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off");
2535 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002536
François Gaffie2110e042015-03-24 08:41:51 +01002537 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07002538
François Gaffie112b0af2015-11-19 16:13:25 +01002539 mAvailableOutputDevices.dump(fd, String8("Available output"));
2540 mAvailableInputDevices.dump(fd, String8("Available input"));
François Gaffie53615e22015-03-19 09:24:12 +01002541 mHwModules.dump(fd);
2542 mOutputs.dump(fd);
2543 mInputs.dump(fd);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002544 mVolumeCurves->dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01002545 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002546 mAudioPatches.dump(fd);
Mikhail Naganov44344b02016-12-13 11:21:02 -08002547 mPolicyMixes.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07002548
2549 return NO_ERROR;
2550}
2551
2552// This function checks for the parameters which can be offloaded.
2553// This can be enhanced depending on the capability of the DSP and policy
2554// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002555bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002556{
2557 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002558 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002559 offloadInfo.sample_rate, offloadInfo.channel_mask,
2560 offloadInfo.format,
2561 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2562 offloadInfo.has_video);
2563
Andy Hung2ddee192015-12-18 17:34:44 -08002564 if (mMasterMono) {
2565 return false; // no offloading if mono is set.
2566 }
2567
Eric Laurente552edb2014-03-10 17:42:56 -07002568 // Check if offload has been disabled
2569 char propValue[PROPERTY_VALUE_MAX];
2570 if (property_get("audio.offload.disable", propValue, "0")) {
2571 if (atoi(propValue) != 0) {
2572 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2573 return false;
2574 }
2575 }
2576
2577 // Check if stream type is music, then only allow offload as of now.
2578 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2579 {
2580 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2581 return false;
2582 }
2583
2584 //TODO: enable audio offloading with video when ready
Andy Hung08945c42015-05-31 21:36:46 -07002585 const bool allowOffloadWithVideo =
2586 property_get_bool("audio.offload.video", false /* default_value */);
2587 if (offloadInfo.has_video && !allowOffloadWithVideo) {
Eric Laurente552edb2014-03-10 17:42:56 -07002588 ALOGV("isOffloadSupported: has_video == true, returning false");
2589 return false;
2590 }
2591
2592 //If duration is less than minimum value defined in property, return false
2593 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2594 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2595 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2596 return false;
2597 }
2598 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2599 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2600 return false;
2601 }
2602
2603 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2604 // creating an offloaded track and tearing it down immediately after start when audioflinger
2605 // detects there is an active non offloadable effect.
2606 // FIXME: We should check the audio session here but we do not have it in this context.
2607 // This may prevent offloading in rare situations where effects are left active by apps
2608 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002609 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002610 return false;
2611 }
2612
2613 // See if there is a profile to support this.
2614 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002615 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002616 offloadInfo.sample_rate,
2617 offloadInfo.format,
2618 offloadInfo.channel_mask,
2619 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002620 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2621 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002622}
2623
Eric Laurent6a94d692014-05-20 11:18:06 -07002624status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2625 audio_port_type_t type,
2626 unsigned int *num_ports,
2627 struct audio_port *ports,
2628 unsigned int *generation)
2629{
2630 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2631 generation == NULL) {
2632 return BAD_VALUE;
2633 }
2634 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2635 if (ports == NULL) {
2636 *num_ports = 0;
2637 }
2638
2639 size_t portsWritten = 0;
2640 size_t portsMax = *num_ports;
2641 *num_ports = 0;
2642 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002643 // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB
2644 // as they are used by stub HALs by convention
Eric Laurent6a94d692014-05-20 11:18:06 -07002645 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002646 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
2647 if (mAvailableOutputDevices[i]->type() == AUDIO_DEVICE_OUT_STUB) {
2648 continue;
2649 }
2650 if (portsWritten < portsMax) {
2651 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2652 }
2653 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002654 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002655 }
2656 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002657 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
2658 if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_STUB) {
2659 continue;
2660 }
2661 if (portsWritten < portsMax) {
2662 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2663 }
2664 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002665 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002666 }
2667 }
2668 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2669 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2670 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2671 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2672 }
2673 *num_ports += mInputs.size();
2674 }
2675 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002676 size_t numOutputs = 0;
2677 for (size_t i = 0; i < mOutputs.size(); i++) {
2678 if (!mOutputs[i]->isDuplicated()) {
2679 numOutputs++;
2680 if (portsWritten < portsMax) {
2681 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2682 }
2683 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002684 }
Eric Laurent84c70242014-06-23 08:46:27 -07002685 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002686 }
2687 }
2688 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002689 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002690 return NO_ERROR;
2691}
2692
2693status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2694{
2695 return NO_ERROR;
2696}
2697
Eric Laurent6a94d692014-05-20 11:18:06 -07002698status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2699 audio_patch_handle_t *handle,
2700 uid_t uid)
2701{
2702 ALOGV("createAudioPatch()");
2703
2704 if (handle == NULL || patch == NULL) {
2705 return BAD_VALUE;
2706 }
2707 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2708
Eric Laurent874c42872014-08-08 15:13:39 -07002709 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2710 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2711 return BAD_VALUE;
2712 }
2713 // only one source per audio patch supported for now
2714 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002715 return INVALID_OPERATION;
2716 }
Eric Laurent874c42872014-08-08 15:13:39 -07002717
2718 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002719 return INVALID_OPERATION;
2720 }
Eric Laurent874c42872014-08-08 15:13:39 -07002721 for (size_t i = 0; i < patch->num_sinks; i++) {
2722 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2723 return INVALID_OPERATION;
2724 }
2725 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002726
2727 sp<AudioPatch> patchDesc;
2728 ssize_t index = mAudioPatches.indexOfKey(*handle);
2729
Eric Laurent6a94d692014-05-20 11:18:06 -07002730 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2731 patch->sources[0].role,
2732 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002733#if LOG_NDEBUG == 0
2734 for (size_t i = 0; i < patch->num_sinks; i++) {
Eric Laurent7b279bb2015-12-14 10:18:23 -08002735 ALOGV("createAudioPatch sink %zu: id %d role %d type %d", i, patch->sinks[i].id,
Eric Laurent874c42872014-08-08 15:13:39 -07002736 patch->sinks[i].role,
2737 patch->sinks[i].type);
2738 }
2739#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002740
2741 if (index >= 0) {
2742 patchDesc = mAudioPatches.valueAt(index);
2743 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2744 mUidCached, patchDesc->mUid, uid);
2745 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2746 return INVALID_OPERATION;
2747 }
2748 } else {
Glenn Kastena13cde92016-03-28 15:26:02 -07002749 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07002750 }
2751
2752 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002753 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002754 if (outputDesc == NULL) {
2755 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2756 return BAD_VALUE;
2757 }
Eric Laurent84c70242014-06-23 08:46:27 -07002758 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2759 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002760 if (patchDesc != 0) {
2761 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2762 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2763 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2764 return BAD_VALUE;
2765 }
2766 }
Eric Laurent874c42872014-08-08 15:13:39 -07002767 DeviceVector devices;
2768 for (size_t i = 0; i < patch->num_sinks; i++) {
2769 // Only support mix to devices connection
2770 // TODO add support for mix to mix connection
2771 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2772 ALOGV("createAudioPatch() source mix but sink is not a device");
2773 return INVALID_OPERATION;
2774 }
2775 sp<DeviceDescriptor> devDesc =
2776 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2777 if (devDesc == 0) {
2778 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2779 return BAD_VALUE;
2780 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002781
François Gaffie53615e22015-03-19 09:24:12 +01002782 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002783 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002784 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002785 NULL, // updatedSamplingRate
2786 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002787 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002788 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002789 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002790 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002791 ALOGV("createAudioPatch() profile not supported for device %08x",
2792 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002793 return INVALID_OPERATION;
2794 }
2795 devices.add(devDesc);
2796 }
2797 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002798 return INVALID_OPERATION;
2799 }
Eric Laurent874c42872014-08-08 15:13:39 -07002800
Eric Laurent6a94d692014-05-20 11:18:06 -07002801 // TODO: reconfigure output format and channels here
2802 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002803 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002804 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002805 index = mAudioPatches.indexOfKey(*handle);
2806 if (index >= 0) {
2807 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2808 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2809 }
2810 patchDesc = mAudioPatches.valueAt(index);
2811 patchDesc->mUid = uid;
2812 ALOGV("createAudioPatch() success");
2813 } else {
2814 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2815 return INVALID_OPERATION;
2816 }
2817 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2818 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2819 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002820 // only one sink supported when connecting an input device to a mix
2821 if (patch->num_sinks > 1) {
2822 return INVALID_OPERATION;
2823 }
François Gaffie53615e22015-03-19 09:24:12 +01002824 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002825 if (inputDesc == NULL) {
2826 return BAD_VALUE;
2827 }
2828 if (patchDesc != 0) {
2829 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2830 return BAD_VALUE;
2831 }
2832 }
2833 sp<DeviceDescriptor> devDesc =
2834 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2835 if (devDesc == 0) {
2836 return BAD_VALUE;
2837 }
2838
François Gaffie53615e22015-03-19 09:24:12 +01002839 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002840 devDesc->mAddress,
2841 patch->sinks[0].sample_rate,
2842 NULL, /*updatedSampleRate*/
2843 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002844 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002845 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002846 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002847 // FIXME for the parameter type,
2848 // and the NONE
2849 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002850 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002851 return INVALID_OPERATION;
2852 }
2853 // TODO: reconfigure output format and channels here
2854 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002855 devDesc->type(), inputDesc->mIoHandle);
2856 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002857 index = mAudioPatches.indexOfKey(*handle);
2858 if (index >= 0) {
2859 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2860 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2861 }
2862 patchDesc = mAudioPatches.valueAt(index);
2863 patchDesc->mUid = uid;
2864 ALOGV("createAudioPatch() success");
2865 } else {
2866 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2867 return INVALID_OPERATION;
2868 }
2869 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2870 // device to device connection
2871 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002872 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002873 return BAD_VALUE;
2874 }
2875 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002876 sp<DeviceDescriptor> srcDeviceDesc =
2877 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002878 if (srcDeviceDesc == 0) {
2879 return BAD_VALUE;
2880 }
Eric Laurent874c42872014-08-08 15:13:39 -07002881
Eric Laurent6a94d692014-05-20 11:18:06 -07002882 //update source and sink with our own data as the data passed in the patch may
2883 // be incomplete.
2884 struct audio_patch newPatch = *patch;
2885 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002886
Eric Laurent874c42872014-08-08 15:13:39 -07002887 for (size_t i = 0; i < patch->num_sinks; i++) {
2888 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2889 ALOGV("createAudioPatch() source device but one sink is not a device");
2890 return INVALID_OPERATION;
2891 }
2892
2893 sp<DeviceDescriptor> sinkDeviceDesc =
2894 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2895 if (sinkDeviceDesc == 0) {
2896 return BAD_VALUE;
2897 }
2898 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2899
Eric Laurent3bcf8592015-04-03 12:13:24 -07002900 // create a software bridge in PatchPanel if:
2901 // - source and sink devices are on differnt HW modules OR
2902 // - audio HAL version is < 3.0
Eric Laurentfb66dd92016-01-28 18:32:03 -08002903 if (!srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) ||
Mikhail Naganov9ee05402016-10-13 15:58:17 -07002904 (srcDeviceDesc->mModule->getHalVersionMajor() < 3)) {
Eric Laurent3bcf8592015-04-03 12:13:24 -07002905 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002906 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002907 return INVALID_OPERATION;
2908 }
Eric Laurent874c42872014-08-08 15:13:39 -07002909 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002910 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002911 // if the sink device is reachable via an opened output stream, request to go via
2912 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002913 audio_io_handle_t output = selectOutput(outputs,
2914 AUDIO_OUTPUT_FLAG_NONE,
2915 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002916 if (output != AUDIO_IO_HANDLE_NONE) {
2917 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2918 if (outputDesc->isDuplicated()) {
2919 return INVALID_OPERATION;
2920 }
2921 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002922 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002923 newPatch.num_sources = 2;
2924 }
Eric Laurent83b88082014-06-20 18:31:16 -07002925 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002926 }
2927 // TODO: check from routing capabilities in config file and other conflicting patches
2928
2929 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2930 if (index >= 0) {
2931 afPatchHandle = patchDesc->mAfPatchHandle;
2932 }
2933
2934 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2935 &afPatchHandle,
2936 0);
2937 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2938 status, afPatchHandle);
2939 if (status == NO_ERROR) {
2940 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002941 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002942 addAudioPatch(patchDesc->mHandle, patchDesc);
2943 } else {
2944 patchDesc->mPatch = newPatch;
2945 }
2946 patchDesc->mAfPatchHandle = afPatchHandle;
2947 *handle = patchDesc->mHandle;
2948 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002949 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002950 } else {
2951 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2952 status);
2953 return INVALID_OPERATION;
2954 }
2955 } else {
2956 return BAD_VALUE;
2957 }
2958 } else {
2959 return BAD_VALUE;
2960 }
2961 return NO_ERROR;
2962}
2963
2964status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2965 uid_t uid)
2966{
2967 ALOGV("releaseAudioPatch() patch %d", handle);
2968
2969 ssize_t index = mAudioPatches.indexOfKey(handle);
2970
2971 if (index < 0) {
2972 return BAD_VALUE;
2973 }
2974 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2975 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2976 mUidCached, patchDesc->mUid, uid);
2977 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2978 return INVALID_OPERATION;
2979 }
2980
2981 struct audio_patch *patch = &patchDesc->mPatch;
2982 patchDesc->mUid = mUidCached;
2983 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002984 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002985 if (outputDesc == NULL) {
2986 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2987 return BAD_VALUE;
2988 }
2989
Eric Laurentc75307b2015-03-17 15:29:32 -07002990 setOutputDevice(outputDesc,
2991 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002992 true,
2993 0,
2994 NULL);
2995 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2996 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002997 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002998 if (inputDesc == NULL) {
2999 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
3000 return BAD_VALUE;
3001 }
3002 setInputDevice(inputDesc->mIoHandle,
Eric Laurentfb66dd92016-01-28 18:32:03 -08003003 getNewInputDevice(inputDesc),
Eric Laurent6a94d692014-05-20 11:18:06 -07003004 true,
3005 NULL);
3006 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003007 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3008 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
3009 status, patchDesc->mAfPatchHandle);
3010 removeAudioPatch(patchDesc->mHandle);
3011 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07003012 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07003013 } else {
3014 return BAD_VALUE;
3015 }
3016 } else {
3017 return BAD_VALUE;
3018 }
3019 return NO_ERROR;
3020}
3021
3022status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
3023 struct audio_patch *patches,
3024 unsigned int *generation)
3025{
François Gaffie53615e22015-03-19 09:24:12 +01003026 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003027 return BAD_VALUE;
3028 }
Eric Laurent6a94d692014-05-20 11:18:06 -07003029 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01003030 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07003031}
3032
Eric Laurente1715a42014-05-20 11:30:42 -07003033status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07003034{
Eric Laurente1715a42014-05-20 11:30:42 -07003035 ALOGV("setAudioPortConfig()");
3036
3037 if (config == NULL) {
3038 return BAD_VALUE;
3039 }
3040 ALOGV("setAudioPortConfig() on port handle %d", config->id);
3041 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07003042 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
3043 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07003044 }
3045
Eric Laurenta121f902014-06-03 13:32:54 -07003046 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07003047 if (config->type == AUDIO_PORT_TYPE_MIX) {
3048 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003049 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07003050 if (outputDesc == NULL) {
3051 return BAD_VALUE;
3052 }
Eric Laurent84c70242014-06-23 08:46:27 -07003053 ALOG_ASSERT(!outputDesc->isDuplicated(),
3054 "setAudioPortConfig() called on duplicated output %d",
3055 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07003056 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07003057 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01003058 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07003059 if (inputDesc == NULL) {
3060 return BAD_VALUE;
3061 }
Eric Laurenta121f902014-06-03 13:32:54 -07003062 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07003063 } else {
3064 return BAD_VALUE;
3065 }
3066 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
3067 sp<DeviceDescriptor> deviceDesc;
3068 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
3069 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
3070 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
3071 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
3072 } else {
3073 return BAD_VALUE;
3074 }
3075 if (deviceDesc == NULL) {
3076 return BAD_VALUE;
3077 }
Eric Laurenta121f902014-06-03 13:32:54 -07003078 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07003079 } else {
3080 return BAD_VALUE;
3081 }
3082
Eric Laurenta121f902014-06-03 13:32:54 -07003083 struct audio_port_config backupConfig;
3084 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
3085 if (status == NO_ERROR) {
3086 struct audio_port_config newConfig;
3087 audioPortConfig->toAudioPortConfig(&newConfig, config);
3088 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07003089 }
Eric Laurenta121f902014-06-03 13:32:54 -07003090 if (status != NO_ERROR) {
3091 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07003092 }
Eric Laurente1715a42014-05-20 11:30:42 -07003093
3094 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07003095}
3096
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003097void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
3098{
Eric Laurentd60560a2015-04-10 11:31:20 -07003099 clearAudioSources(uid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003100 clearAudioPatches(uid);
3101 clearSessionRoutes(uid);
3102}
3103
Eric Laurent6a94d692014-05-20 11:18:06 -07003104void AudioPolicyManager::clearAudioPatches(uid_t uid)
3105{
Eric Laurent0add0fd2014-12-04 18:58:14 -08003106 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003107 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
3108 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08003109 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07003110 }
3111 }
3112}
3113
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003114void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
3115 audio_io_handle_t ouptutToSkip)
3116{
3117 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
3118 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
3119 for (size_t j = 0; j < mOutputs.size(); j++) {
3120 if (mOutputs.keyAt(j) == ouptutToSkip) {
3121 continue;
3122 }
3123 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
3124 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
3125 continue;
3126 }
3127 // If the default device for this strategy is on another output mix,
3128 // invalidate all tracks in this strategy to force re connection.
3129 // Otherwise select new device on the output mix.
3130 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
Eric Laurent794fde22016-03-11 09:50:45 -08003131 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003132 if (getStrategy((audio_stream_type_t)stream) == strategy) {
3133 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
3134 }
3135 }
3136 } else {
3137 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
3138 setOutputDevice(outputDesc, newDevice, false);
3139 }
3140 }
3141}
3142
3143void AudioPolicyManager::clearSessionRoutes(uid_t uid)
3144{
3145 // remove output routes associated with this uid
3146 SortedVector<routing_strategy> affectedStrategies;
3147 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
3148 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
3149 if (route->mUid == uid) {
3150 mOutputRoutes.removeItemsAt(i);
3151 if (route->mDeviceDescriptor != 0) {
3152 affectedStrategies.add(getStrategy(route->mStreamType));
3153 }
3154 }
3155 }
3156 // reroute outputs if necessary
3157 for (size_t i = 0; i < affectedStrategies.size(); i++) {
3158 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
3159 }
3160
3161 // remove input routes associated with this uid
3162 SortedVector<audio_source_t> affectedSources;
3163 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
3164 sp<SessionRoute> route = mInputRoutes.valueAt(i);
3165 if (route->mUid == uid) {
3166 mInputRoutes.removeItemsAt(i);
3167 if (route->mDeviceDescriptor != 0) {
3168 affectedSources.add(route->mSource);
3169 }
3170 }
3171 }
3172 // reroute inputs if necessary
3173 SortedVector<audio_io_handle_t> inputsToClose;
3174 for (size_t i = 0; i < mInputs.size(); i++) {
3175 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08003176 if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003177 inputsToClose.add(inputDesc->mIoHandle);
3178 }
3179 }
3180 for (size_t i = 0; i < inputsToClose.size(); i++) {
3181 closeInput(inputsToClose[i]);
3182 }
3183}
3184
Eric Laurentd60560a2015-04-10 11:31:20 -07003185void AudioPolicyManager::clearAudioSources(uid_t uid)
3186{
3187 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
3188 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3189 if (sourceDesc->mUid == uid) {
3190 stopAudioSource(mAudioSources.keyAt(i));
3191 }
3192 }
3193}
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003194
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07003195status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
3196 audio_io_handle_t *ioHandle,
3197 audio_devices_t *device)
3198{
Glenn Kastenf0c6d7d2016-02-26 10:44:04 -08003199 *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
3200 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
Eric Laurentc73ca6e2014-12-12 14:34:22 -08003201 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07003202
François Gaffiedf372692015-03-19 10:43:27 +01003203 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07003204}
3205
Eric Laurentd60560a2015-04-10 11:31:20 -07003206status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source,
3207 const audio_attributes_t *attributes,
Glenn Kasten559d4392016-03-29 13:42:57 -07003208 audio_patch_handle_t *handle,
Eric Laurentd60560a2015-04-10 11:31:20 -07003209 uid_t uid)
Eric Laurent554a2772015-04-10 11:29:24 -07003210{
Eric Laurentd60560a2015-04-10 11:31:20 -07003211 ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle);
3212 if (source == NULL || attributes == NULL || handle == NULL) {
3213 return BAD_VALUE;
3214 }
3215
Glenn Kasten559d4392016-03-29 13:42:57 -07003216 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentd60560a2015-04-10 11:31:20 -07003217
3218 if (source->role != AUDIO_PORT_ROLE_SOURCE ||
3219 source->type != AUDIO_PORT_TYPE_DEVICE) {
3220 ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type);
3221 return INVALID_OPERATION;
3222 }
3223
3224 sp<DeviceDescriptor> srcDeviceDesc =
3225 mAvailableInputDevices.getDevice(source->ext.device.type,
3226 String8(source->ext.device.address));
3227 if (srcDeviceDesc == 0) {
3228 ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type);
3229 return BAD_VALUE;
3230 }
3231 sp<AudioSourceDescriptor> sourceDesc =
3232 new AudioSourceDescriptor(srcDeviceDesc, attributes, uid);
3233
3234 struct audio_patch dummyPatch;
3235 sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid);
3236 sourceDesc->mPatchDesc = patchDesc;
3237
3238 status_t status = connectAudioSource(sourceDesc);
3239 if (status == NO_ERROR) {
3240 mAudioSources.add(sourceDesc->getHandle(), sourceDesc);
3241 *handle = sourceDesc->getHandle();
3242 }
3243 return status;
3244}
3245
3246status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3247{
3248 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3249
3250 // make sure we only have one patch per source.
3251 disconnectAudioSource(sourceDesc);
3252
3253 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
Eric Laurent28d09f02016-03-08 10:43:05 -08003254 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003255 sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice;
3256
3257 audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true);
3258 sp<DeviceDescriptor> sinkDeviceDesc =
3259 mAvailableOutputDevices.getDevice(sinkDevice, String8(""));
3260
3261 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3262 struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch;
3263
3264 if (srcDeviceDesc->getAudioPort()->mModule->getHandle() ==
3265 sinkDeviceDesc->getAudioPort()->mModule->getHandle() &&
Mikhail Naganov9ee05402016-10-13 15:58:17 -07003266 srcDeviceDesc->getAudioPort()->mModule->getHalVersionMajor() >= 3 &&
Eric Laurentd60560a2015-04-10 11:31:20 -07003267 srcDeviceDesc->getAudioPort()->mGains.size() > 0) {
3268 ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__);
3269 // create patch between src device and output device
3270 // create Hwoutput and add to mHwOutputs
3271 } else {
3272 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs);
3273 audio_io_handle_t output =
3274 selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID);
3275 if (output == AUDIO_IO_HANDLE_NONE) {
3276 ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice);
3277 return INVALID_OPERATION;
3278 }
3279 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
3280 if (outputDesc->isDuplicated()) {
3281 ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice);
3282 return INVALID_OPERATION;
3283 }
3284 // create a special patch with no sink and two sources:
3285 // - the second source indicates to PatchPanel through which output mix this patch should
3286 // be connected as well as the stream type for volume control
3287 // - the sink is defined by whatever output device is currently selected for the output
3288 // though which this patch is routed.
3289 patch->num_sinks = 0;
3290 patch->num_sources = 2;
3291 srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL);
3292 outputDesc->toAudioPortConfig(&patch->sources[1], NULL);
3293 patch->sources[1].ext.mix.usecase.stream = stream;
3294 status_t status = mpClientInterface->createAudioPatch(patch,
3295 &afPatchHandle,
3296 0);
3297 ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__,
3298 status, afPatchHandle);
3299 if (status != NO_ERROR) {
3300 ALOGW("%s patch panel could not connect device patch, error %d",
3301 __FUNCTION__, status);
3302 return INVALID_OPERATION;
3303 }
3304 uint32_t delayMs = 0;
Eric Laurentc40d9692016-04-13 19:14:13 -07003305 status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs);
Eric Laurentd60560a2015-04-10 11:31:20 -07003306
3307 if (status != NO_ERROR) {
3308 mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0);
3309 return status;
3310 }
3311 sourceDesc->mSwOutput = outputDesc;
3312 if (delayMs != 0) {
3313 usleep(delayMs * 1000);
3314 }
3315 }
3316
3317 sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle;
3318 addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc);
3319
3320 return NO_ERROR;
Eric Laurent554a2772015-04-10 11:29:24 -07003321}
3322
Glenn Kasten559d4392016-03-29 13:42:57 -07003323status_t AudioPolicyManager::stopAudioSource(audio_patch_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07003324{
Eric Laurentd60560a2015-04-10 11:31:20 -07003325 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle);
3326 ALOGV("%s handle %d", __FUNCTION__, handle);
3327 if (sourceDesc == 0) {
3328 ALOGW("%s unknown source for handle %d", __FUNCTION__, handle);
3329 return BAD_VALUE;
3330 }
3331 status_t status = disconnectAudioSource(sourceDesc);
3332
3333 mAudioSources.removeItem(handle);
3334 return status;
3335}
3336
Andy Hung2ddee192015-12-18 17:34:44 -08003337status_t AudioPolicyManager::setMasterMono(bool mono)
3338{
3339 if (mMasterMono == mono) {
3340 return NO_ERROR;
3341 }
3342 mMasterMono = mono;
3343 // if enabling mono we close all offloaded devices, which will invalidate the
3344 // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible
3345 // for recreating the new AudioTrack as non-offloaded PCM.
3346 //
3347 // If disabling mono, we leave all tracks as is: we don't know which clients
3348 // and tracks are able to be recreated as offloaded. The next "song" should
3349 // play back offloaded.
3350 if (mMasterMono) {
3351 Vector<audio_io_handle_t> offloaded;
3352 for (size_t i = 0; i < mOutputs.size(); ++i) {
3353 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
3354 if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
3355 offloaded.push(desc->mIoHandle);
3356 }
3357 }
3358 for (size_t i = 0; i < offloaded.size(); ++i) {
3359 closeOutput(offloaded[i]);
3360 }
3361 }
3362 // update master mono for all remaining outputs
3363 for (size_t i = 0; i < mOutputs.size(); ++i) {
3364 updateMono(mOutputs.keyAt(i));
3365 }
3366 return NO_ERROR;
3367}
3368
3369status_t AudioPolicyManager::getMasterMono(bool *mono)
3370{
3371 *mono = mMasterMono;
3372 return NO_ERROR;
3373}
3374
Eric Laurentac9cef52017-06-09 15:46:26 -07003375float AudioPolicyManager::getStreamVolumeDB(
3376 audio_stream_type_t stream, int index, audio_devices_t device)
3377{
3378 return computeVolume(stream, index, device);
3379}
3380
Eric Laurentd60560a2015-04-10 11:31:20 -07003381status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3382{
3383 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3384
3385 sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle);
3386 if (patchDesc == 0) {
3387 ALOGW("%s source has no patch with handle %d", __FUNCTION__,
3388 sourceDesc->mPatchDesc->mHandle);
3389 return BAD_VALUE;
3390 }
3391 removeAudioPatch(sourceDesc->mPatchDesc->mHandle);
3392
Eric Laurent28d09f02016-03-08 10:43:05 -08003393 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003394 sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote();
3395 if (swOutputDesc != 0) {
3396 stopSource(swOutputDesc, stream, false);
3397 mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3398 } else {
3399 sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote();
3400 if (hwOutputDesc != 0) {
3401 // release patch between src device and output device
3402 // close Hwoutput and remove from mHwOutputs
3403 } else {
3404 ALOGW("%s source has neither SW nor HW output", __FUNCTION__);
3405 }
3406 }
3407 return NO_ERROR;
3408}
3409
3410sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput(
3411 audio_io_handle_t output, routing_strategy strategy)
3412{
3413 sp<AudioSourceDescriptor> source;
3414 for (size_t i = 0; i < mAudioSources.size(); i++) {
3415 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3416 routing_strategy sourceStrategy =
3417 (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
3418 sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote();
3419 if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) {
3420 source = sourceDesc;
3421 break;
3422 }
3423 }
3424 return source;
Eric Laurent554a2772015-04-10 11:29:24 -07003425}
3426
Eric Laurente552edb2014-03-10 17:42:56 -07003427// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07003428// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07003429// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07003430uint32_t AudioPolicyManager::nextAudioPortGeneration()
3431{
3432 return android_atomic_inc(&mAudioPortGeneration);
3433}
3434
Jaekyun Seok0d4a6af2017-02-17 17:10:17 +09003435#ifdef USE_XML_AUDIO_POLICY_CONF
3436// Treblized audio policy xml config will be located in /odm/etc or /vendor/etc.
3437static const char *kConfigLocationList[] =
3438 {"/odm/etc", "/vendor/etc", "/system/etc"};
3439static const int kConfigLocationListSize =
3440 (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0]));
3441
3442static status_t deserializeAudioPolicyXmlConfig(AudioPolicyConfig &config) {
3443 char audioPolicyXmlConfigFile[AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH];
3444 status_t ret;
3445
3446 for (int i = 0; i < kConfigLocationListSize; i++) {
3447 PolicySerializer serializer;
3448 snprintf(audioPolicyXmlConfigFile,
3449 sizeof(audioPolicyXmlConfigFile),
3450 "%s/%s",
3451 kConfigLocationList[i],
3452 AUDIO_POLICY_XML_CONFIG_FILE_NAME);
3453 ret = serializer.deserialize(audioPolicyXmlConfigFile, config);
3454 if (ret == NO_ERROR) {
3455 break;
3456 }
3457 }
3458 return ret;
3459}
3460#endif
3461
Eric Laurente0720872014-03-11 09:30:41 -07003462AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07003463 :
3464#ifdef AUDIO_POLICY_TEST
3465 Thread(false),
3466#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07003467 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07003468 mA2dpSuspended(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003469 mAudioPortGeneration(1),
3470 mBeaconMuteRefCount(0),
3471 mBeaconPlayingRefCount(0),
Eric Laurent9459fb02015-08-12 18:36:32 -07003472 mBeaconMuted(false),
Andy Hung2ddee192015-12-18 17:34:44 -08003473 mTtsOutputAvailable(false),
Eric Laurent36829f92017-04-07 19:04:42 -07003474 mMasterMono(false),
3475 mMusicEffectOutput(AUDIO_IO_HANDLE_NONE)
Eric Laurente552edb2014-03-10 17:42:56 -07003476{
François Gaffied1ab2bd2015-12-02 18:20:06 +01003477 mUidCached = getuid();
3478 mpClientInterface = clientInterface;
3479
3480 // TODO: remove when legacy conf file is removed. true on devices that use DRC on the
3481 // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly.
3482 // Note: remove also speaker_drc_enabled from global configuration of XML config file.
3483 bool speakerDrcEnabled = false;
3484
3485#ifdef USE_XML_AUDIO_POLICY_CONF
3486 mVolumeCurves = new VolumeCurvesCollection();
3487 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3488 mDefaultOutputDevice, speakerDrcEnabled,
3489 static_cast<VolumeCurvesCollection *>(mVolumeCurves));
Jaekyun Seok0d4a6af2017-02-17 17:10:17 +09003490 if (deserializeAudioPolicyXmlConfig(config) != NO_ERROR) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01003491#else
3492 mVolumeCurves = new StreamDescriptorCollection();
3493 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3494 mDefaultOutputDevice, speakerDrcEnabled);
3495 if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) &&
3496 (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) {
3497#endif
3498 ALOGE("could not load audio policy configuration file, setting defaults");
3499 config.setDefault();
3500 }
3501 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
3502 mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled);
3503
3504 // Once policy config has been parsed, retrieve an instance of the engine and initialize it.
François Gaffie2110e042015-03-24 08:41:51 +01003505 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
3506 if (!engineInstance) {
3507 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
3508 return;
3509 }
3510 // Retrieve the Policy Manager Interface
3511 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
3512 if (mEngine == NULL) {
3513 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
3514 return;
3515 }
3516 mEngine->setObserver(this);
3517 status_t status = mEngine->initCheck();
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07003518 (void) status;
François Gaffie2110e042015-03-24 08:41:51 +01003519 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
3520
Eric Laurent3a4311c2014-03-17 12:00:47 -07003521 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07003522 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07003523 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
3524 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07003525 for (size_t i = 0; i < mHwModules.size(); i++) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003526 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003527 if (mHwModules[i]->mHandle == 0) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003528 ALOGW("could not open HW module %s", mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003529 continue;
3530 }
3531 // open all output streams needed to access attached devices
3532 // except for direct output streams that are only opened when they are actually
3533 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07003534 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07003535 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3536 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003537 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003538
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003539 if (!outProfile->hasSupportedDevices()) {
3540 ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003541 continue;
3542 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003543 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) {
Eric Laurent9459fb02015-08-12 18:36:32 -07003544 mTtsOutputAvailable = true;
3545 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003546
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003547 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
Eric Laurentd78f1532014-09-16 16:38:20 -07003548 continue;
3549 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003550 audio_devices_t profileType = outProfile->getSupportedDevicesType();
François Gaffie53615e22015-03-19 09:24:12 +01003551 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
3552 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07003553 } else {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003554 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003555 // outputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003556 profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes);
Eric Laurente552edb2014-03-10 17:42:56 -07003557 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003558 if ((profileType & outputDeviceTypes) == 0) {
3559 continue;
3560 }
Eric Laurentc75307b2015-03-17 15:29:32 -07003561 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
3562 mpClientInterface);
Eric Laurentc40d9692016-04-13 19:14:13 -07003563 const DeviceVector &supportedDevices = outProfile->getSupportedDevices();
3564 const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType);
3565 String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress
3566 : String8("");
Eric Laurentd78f1532014-09-16 16:38:20 -07003567
3568 outputDesc->mDevice = profileType;
3569 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3570 config.sample_rate = outputDesc->mSamplingRate;
3571 config.channel_mask = outputDesc->mChannelMask;
3572 config.format = outputDesc->mFormat;
3573 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003574 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003575 &output,
3576 &config,
3577 &outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003578 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003579 &outputDesc->mLatency,
3580 outputDesc->mFlags);
3581
3582 if (status != NO_ERROR) {
3583 ALOGW("Cannot open output stream for device %08x on hw module %s",
3584 outputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003585 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003586 } else {
3587 outputDesc->mSamplingRate = config.sample_rate;
3588 outputDesc->mChannelMask = config.channel_mask;
3589 outputDesc->mFormat = config.format;
3590
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003591 for (size_t k = 0; k < supportedDevices.size(); k++) {
3592 ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003593 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08003594 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
3595 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003596 }
3597 }
3598 if (mPrimaryOutput == 0 &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003599 outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003600 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07003601 }
3602 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003603 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07003604 outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003605 true,
3606 0,
3607 NULL,
3608 address.string());
Eric Laurentd78f1532014-09-16 16:38:20 -07003609 }
Eric Laurente552edb2014-03-10 17:42:56 -07003610 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003611 // open input streams needed to access attached devices to validate
3612 // mAvailableInputDevices list
3613 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
3614 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003615 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003616
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003617 if (!inProfile->hasSupportedDevices()) {
3618 ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003619 continue;
3620 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003621 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003622 // inputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003623 audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes);
3624
Eric Laurentd78f1532014-09-16 16:38:20 -07003625 if ((profileType & inputDeviceTypes) == 0) {
3626 continue;
3627 }
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08003628 sp<AudioInputDescriptor> inputDesc =
3629 new AudioInputDescriptor(inProfile);
Eric Laurentd78f1532014-09-16 16:38:20 -07003630
Eric Laurentd78f1532014-09-16 16:38:20 -07003631 inputDesc->mDevice = profileType;
3632
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003633 // find the address
3634 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
3635 // the inputs vector must be of size 1, but we don't want to crash here
3636 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
3637 : String8("");
3638 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
3639 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
3640
Eric Laurentd78f1532014-09-16 16:38:20 -07003641 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3642 config.sample_rate = inputDesc->mSamplingRate;
3643 config.channel_mask = inputDesc->mChannelMask;
3644 config.format = inputDesc->mFormat;
3645 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003646 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003647 &input,
3648 &config,
3649 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003650 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003651 AUDIO_SOURCE_MIC,
3652 AUDIO_INPUT_FLAG_NONE);
3653
3654 if (status == NO_ERROR) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003655 const DeviceVector &supportedDevices = inProfile->getSupportedDevices();
3656 for (size_t k = 0; k < supportedDevices.size(); k++) {
3657 ssize_t index = mAvailableInputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003658 // give a valid ID to an attached device once confirmed it is reachable
Eric Laurent45aabc32015-08-06 09:11:13 -07003659 if (index >= 0) {
3660 sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index];
3661 if (!devDesc->isAttached()) {
3662 devDesc->attach(mHwModules[i]);
3663 devDesc->importAudioPort(inProfile);
3664 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003665 }
3666 }
3667 mpClientInterface->closeInput(input);
3668 } else {
3669 ALOGW("Cannot open input stream for device %08x on hw module %s",
3670 inputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003671 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003672 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003673 }
3674 }
3675 // make sure all attached devices have been allocated a unique ID
3676 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003677 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003678 ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003679 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
3680 continue;
3681 }
François Gaffie2110e042015-03-24 08:41:51 +01003682 // The device is now validated and can be appended to the available devices of the engine
3683 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
3684 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003685 i++;
3686 }
3687 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003688 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01003689 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003690 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
3691 continue;
3692 }
François Gaffie2110e042015-03-24 08:41:51 +01003693 // The device is now validated and can be appended to the available devices of the engine
3694 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
3695 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003696 i++;
3697 }
3698 // make sure default device is reachable
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003699 if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01003700 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003701 }
Eric Laurente552edb2014-03-10 17:42:56 -07003702
3703 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
3704
3705 updateDevicesAndOutputs();
3706
3707#ifdef AUDIO_POLICY_TEST
3708 if (mPrimaryOutput != 0) {
3709 AudioParameter outputCmd = AudioParameter();
3710 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003711 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07003712
3713 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
3714 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07003715 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
3716 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003717 mTestLatencyMs = 0;
3718 mCurOutput = 0;
3719 mDirectOutput = false;
3720 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3721 mTestOutputs[i] = 0;
3722 }
3723
3724 const size_t SIZE = 256;
3725 char buffer[SIZE];
3726 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
3727 run(buffer, ANDROID_PRIORITY_AUDIO);
3728 }
3729#endif //AUDIO_POLICY_TEST
3730}
3731
Eric Laurente0720872014-03-11 09:30:41 -07003732AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07003733{
3734#ifdef AUDIO_POLICY_TEST
3735 exit();
3736#endif //AUDIO_POLICY_TEST
3737 for (size_t i = 0; i < mOutputs.size(); i++) {
3738 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003739 }
3740 for (size_t i = 0; i < mInputs.size(); i++) {
3741 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003742 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003743 mAvailableOutputDevices.clear();
3744 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003745 mOutputs.clear();
3746 mInputs.clear();
3747 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07003748}
3749
Eric Laurente0720872014-03-11 09:30:41 -07003750status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07003751{
Eric Laurent87ffa392015-05-22 10:32:38 -07003752 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003753}
3754
3755#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07003756bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07003757{
3758 ALOGV("entering threadLoop()");
3759 while (!exitPending())
3760 {
3761 String8 command;
3762 int valueInt;
3763 String8 value;
3764
3765 Mutex::Autolock _l(mLock);
3766 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
3767
3768 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
3769 AudioParameter param = AudioParameter(command);
3770
3771 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
3772 valueInt != 0) {
3773 ALOGV("Test command %s received", command.string());
3774 String8 target;
3775 if (param.get(String8("target"), target) != NO_ERROR) {
3776 target = "Manager";
3777 }
3778 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
3779 param.remove(String8("test_cmd_policy_output"));
3780 mCurOutput = valueInt;
3781 }
3782 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
3783 param.remove(String8("test_cmd_policy_direct"));
3784 if (value == "false") {
3785 mDirectOutput = false;
3786 } else if (value == "true") {
3787 mDirectOutput = true;
3788 }
3789 }
3790 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
3791 param.remove(String8("test_cmd_policy_input"));
3792 mTestInput = valueInt;
3793 }
3794
3795 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
3796 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07003797 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07003798 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003799 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003800 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003801 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003802 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003803 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07003804 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003805 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07003806 if (target == "Manager") {
3807 mTestFormat = format;
3808 } else if (mTestOutputs[mCurOutput] != 0) {
3809 AudioParameter outputParam = AudioParameter();
Mikhail Naganov388360c2016-10-17 17:09:41 -07003810 outputParam.addInt(String8(AudioParameter::keyStreamSupportedFormats), format);
Eric Laurente552edb2014-03-10 17:42:56 -07003811 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3812 }
3813 }
3814 }
3815 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
3816 param.remove(String8("test_cmd_policy_channels"));
3817 int channels = 0;
3818
3819 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003820 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003821 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003822 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07003823 }
3824 if (channels != 0) {
3825 if (target == "Manager") {
3826 mTestChannels = channels;
3827 } else if (mTestOutputs[mCurOutput] != 0) {
3828 AudioParameter outputParam = AudioParameter();
Mikhail Naganov388360c2016-10-17 17:09:41 -07003829 outputParam.addInt(String8(AudioParameter::keyStreamSupportedChannels), channels);
Eric Laurente552edb2014-03-10 17:42:56 -07003830 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3831 }
3832 }
3833 }
3834 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
3835 param.remove(String8("test_cmd_policy_sampleRate"));
3836 if (valueInt >= 0 && valueInt <= 96000) {
3837 int samplingRate = valueInt;
3838 if (target == "Manager") {
3839 mTestSamplingRate = samplingRate;
3840 } else if (mTestOutputs[mCurOutput] != 0) {
3841 AudioParameter outputParam = AudioParameter();
Mikhail Naganov388360c2016-10-17 17:09:41 -07003842 outputParam.addInt(String8(AudioParameter::keyStreamSupportedSamplingRates), samplingRate);
Eric Laurente552edb2014-03-10 17:42:56 -07003843 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3844 }
3845 }
3846 }
3847
3848 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
3849 param.remove(String8("test_cmd_policy_reopen"));
3850
Eric Laurentc75307b2015-03-17 15:29:32 -07003851 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07003852
Eric Laurentc75307b2015-03-17 15:29:32 -07003853 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07003854
Eric Laurentc75307b2015-03-17 15:29:32 -07003855 removeOutput(mPrimaryOutput->mIoHandle);
3856 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
3857 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003858 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003859 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3860 config.sample_rate = outputDesc->mSamplingRate;
3861 config.channel_mask = outputDesc->mChannelMask;
3862 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07003863 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003864 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07003865 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07003866 &config,
3867 &outputDesc->mDevice,
3868 String8(""),
3869 &outputDesc->mLatency,
3870 outputDesc->mFlags);
3871 if (status != NO_ERROR) {
3872 ALOGE("Failed to reopen hardware output stream, "
3873 "samplingRate: %d, format %d, channels %d",
3874 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003875 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003876 outputDesc->mSamplingRate = config.sample_rate;
3877 outputDesc->mChannelMask = config.channel_mask;
3878 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07003879 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07003880 AudioParameter outputCmd = AudioParameter();
3881 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003882 mpClientInterface->setParameters(handle, outputCmd.toString());
3883 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07003884 }
3885 }
3886
3887
3888 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3889 }
3890 }
3891 return false;
3892}
3893
Eric Laurente0720872014-03-11 09:30:41 -07003894void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003895{
3896 {
3897 AutoMutex _l(mLock);
3898 requestExit();
3899 mWaitWorkCV.signal();
3900 }
3901 requestExitAndWait();
3902}
3903
Eric Laurente0720872014-03-11 09:30:41 -07003904int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003905{
3906 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3907 if (output == mTestOutputs[i]) return i;
3908 }
3909 return 0;
3910}
3911#endif //AUDIO_POLICY_TEST
3912
3913// ---
3914
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003915void AudioPolicyManager::addOutput(audio_io_handle_t output, const sp<SwAudioOutputDescriptor>& outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003916{
François Gaffie98cc1912015-03-18 17:52:40 +01003917 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003918 mOutputs.add(output, outputDesc);
Andy Hung2ddee192015-12-18 17:34:44 -08003919 updateMono(output); // update mono status when adding to output list
Eric Laurent36829f92017-04-07 19:04:42 -07003920 selectOutputForMusicEffects();
Eric Laurent6a94d692014-05-20 11:18:06 -07003921 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003922}
3923
François Gaffie53615e22015-03-19 09:24:12 +01003924void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3925{
3926 mOutputs.removeItem(output);
Eric Laurent36829f92017-04-07 19:04:42 -07003927 selectOutputForMusicEffects();
François Gaffie53615e22015-03-19 09:24:12 +01003928}
3929
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003930void AudioPolicyManager::addInput(audio_io_handle_t input, const sp<AudioInputDescriptor>& inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003931{
François Gaffie98cc1912015-03-18 17:52:40 +01003932 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003933 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003934 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003935}
Eric Laurente552edb2014-03-10 17:42:56 -07003936
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003937void AudioPolicyManager::findIoHandlesByAddress(const sp<SwAudioOutputDescriptor>& desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003938 const audio_devices_t device /*in*/,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003939 const String8& address /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003940 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003941 sp<DeviceDescriptor> devDesc =
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003942 desc->mProfile->getSupportedDeviceByAddress(device, address);
keunyoung3190e672014-12-30 13:00:52 -08003943 if (devDesc != 0) {
3944 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3945 desc->mIoHandle, address.string());
3946 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003947 }
3948}
3949
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003950status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003951 audio_policy_dev_state_t state,
3952 SortedVector<audio_io_handle_t>& outputs,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003953 const String8& address)
Eric Laurente552edb2014-03-10 17:42:56 -07003954{
François Gaffie53615e22015-03-19 09:24:12 +01003955 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003956 sp<SwAudioOutputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003957
3958 if (audio_device_is_digital(device)) {
3959 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003960 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003961 }
Eric Laurente552edb2014-03-10 17:42:56 -07003962
Eric Laurent3b73df72014-03-11 09:06:29 -07003963 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003964 // first list already open outputs that can be routed to this device
3965 for (size_t i = 0; i < mOutputs.size(); i++) {
3966 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003967 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003968 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003969 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3970 outputs.add(mOutputs.keyAt(i));
3971 } else {
3972 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003973 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003974 }
Eric Laurente552edb2014-03-10 17:42:56 -07003975 }
3976 }
3977 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003978 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003979 for (size_t i = 0; i < mHwModules.size(); i++)
3980 {
3981 if (mHwModules[i]->mHandle == 0) {
3982 continue;
3983 }
3984 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3985 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003986 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003987 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003988 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003989 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003990 profiles.add(profile);
3991 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3992 }
Eric Laurente552edb2014-03-10 17:42:56 -07003993 }
3994 }
3995 }
3996
Eric Laurent7b279bb2015-12-14 10:18:23 -08003997 ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size());
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003998
Eric Laurente552edb2014-03-10 17:42:56 -07003999 if (profiles.isEmpty() && outputs.isEmpty()) {
4000 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
4001 return BAD_VALUE;
4002 }
4003
4004 // open outputs for matching profiles if needed. Direct outputs are also opened to
4005 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
4006 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004007 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07004008
4009 // nothing to do if one output is already opened for this profile
4010 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004011 for (j = 0; j < outputs.size(); j++) {
4012 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07004013 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07004014 // matching profile: save the sample rates, format and channel masks supported
4015 // by the profile in our device descriptor
Paul McLean9080a4c2015-06-18 08:24:02 -07004016 if (audio_device_is_digital(device)) {
4017 devDesc->importAudioPort(profile);
4018 }
Eric Laurente552edb2014-03-10 17:42:56 -07004019 break;
4020 }
4021 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004022 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07004023 continue;
4024 }
4025
Eric Laurent83b88082014-06-20 18:31:16 -07004026 ALOGV("opening output for device %08x with params %s profile %p",
4027 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07004028 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07004029 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07004030 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
4031 config.sample_rate = desc->mSamplingRate;
4032 config.channel_mask = desc->mChannelMask;
4033 config.format = desc->mFormat;
4034 config.offload_info.sample_rate = desc->mSamplingRate;
4035 config.offload_info.channel_mask = desc->mChannelMask;
4036 config.offload_info.format = desc->mFormat;
4037 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07004038 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07004039 &output,
4040 &config,
4041 &desc->mDevice,
4042 address,
4043 &desc->mLatency,
4044 desc->mFlags);
4045 if (status == NO_ERROR) {
4046 desc->mSamplingRate = config.sample_rate;
4047 desc->mChannelMask = config.channel_mask;
4048 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07004049
Eric Laurentd4692962014-05-05 18:13:44 -07004050 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07004051 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07004052 char *param = audio_device_address_to_parameter(device, address);
4053 mpClientInterface->setParameters(output, String8(param));
4054 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07004055 }
Phil Burk00eeb322016-03-31 12:41:00 -07004056 updateAudioProfiles(device, output, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01004057 if (!profile->hasValidAudioProfile()) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004058 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07004059 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004060 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01004061 } else if (profile->hasDynamicAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07004062 mpClientInterface->closeOutput(output);
Phil Burk702b1052016-03-02 16:38:26 -08004063 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01004064 profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004065 config.offload_info.sample_rate = config.sample_rate;
4066 config.offload_info.channel_mask = config.channel_mask;
4067 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07004068 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07004069 &output,
4070 &config,
4071 &desc->mDevice,
4072 address,
4073 &desc->mLatency,
4074 desc->mFlags);
4075 if (status == NO_ERROR) {
4076 desc->mSamplingRate = config.sample_rate;
4077 desc->mChannelMask = config.channel_mask;
4078 desc->mFormat = config.format;
4079 } else {
4080 output = AUDIO_IO_HANDLE_NONE;
4081 }
Eric Laurentd4692962014-05-05 18:13:44 -07004082 }
4083
Eric Laurentcf2c0212014-07-25 16:20:43 -07004084 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07004085 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01004086 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01004087 sp<AudioPolicyMix> policyMix;
4088 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004089 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
4090 address.string());
4091 }
François Gaffie036e1e92015-03-19 10:16:24 +01004092 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07004093 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01004094
Eric Laurent87ffa392015-05-22 10:32:38 -07004095 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
4096 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08004097 // no duplicated output for direct outputs and
4098 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07004099 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07004100
Eric Laurentd4692962014-05-05 18:13:44 -07004101 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07004102 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07004103
Eric Laurentd4692962014-05-05 18:13:44 -07004104 //TODO: configure audio effect output stage here
4105
4106 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07004107 duplicatedOutput =
4108 mpClientInterface->openDuplicateOutput(output,
4109 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004110 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07004111 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07004112 sp<SwAudioOutputDescriptor> dupOutputDesc =
4113 new SwAudioOutputDescriptor(NULL, mpClientInterface);
4114 dupOutputDesc->mOutput1 = mPrimaryOutput;
4115 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07004116 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
4117 dupOutputDesc->mFormat = desc->mFormat;
4118 dupOutputDesc->mChannelMask = desc->mChannelMask;
4119 dupOutputDesc->mLatency = desc->mLatency;
4120 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07004121 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07004122 } else {
4123 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07004124 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07004125 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01004126 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07004127 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07004128 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07004129 }
Eric Laurente552edb2014-03-10 17:42:56 -07004130 }
4131 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07004132 } else {
4133 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07004134 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07004135 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07004136 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004137 profiles.removeAt(profile_index);
4138 profile_index--;
4139 } else {
4140 outputs.add(output);
Paul McLean9080a4c2015-06-18 08:24:02 -07004141 // Load digital format info only for digital devices
4142 if (audio_device_is_digital(device)) {
4143 devDesc->importAudioPort(profile);
4144 }
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07004145
François Gaffie53615e22015-03-19 09:24:12 +01004146 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004147 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
4148 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07004149 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004150 NULL/*patch handle*/, address.string());
4151 }
Eric Laurente552edb2014-03-10 17:42:56 -07004152 ALOGV("checkOutputsForDevice(): adding output %d", output);
4153 }
4154 }
4155
4156 if (profiles.isEmpty()) {
4157 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
4158 return BAD_VALUE;
4159 }
Eric Laurentd4692962014-05-05 18:13:44 -07004160 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07004161 // check if one opened output is not needed any more after disconnecting one device
4162 for (size_t i = 0; i < mOutputs.size(); i++) {
4163 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004164 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004165 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01004166 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07004167 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08004168 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07004169 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004170 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
4171 mOutputs.keyAt(i));
4172 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004173 }
Eric Laurente552edb2014-03-10 17:42:56 -07004174 }
4175 }
Eric Laurentd4692962014-05-05 18:13:44 -07004176 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07004177 for (size_t i = 0; i < mHwModules.size(); i++)
4178 {
4179 if (mHwModules[i]->mHandle == 0) {
4180 continue;
4181 }
4182 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
4183 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004184 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004185 if (profile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07004186 ALOGV("checkOutputsForDevice(): "
4187 "clearing direct output profile %zu on module %zu", j, i);
François Gaffie112b0af2015-11-19 16:13:25 +01004188 profile->clearAudioProfiles();
Eric Laurente552edb2014-03-10 17:42:56 -07004189 }
4190 }
4191 }
4192 }
4193 return NO_ERROR;
4194}
4195
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004196status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01004197 audio_policy_dev_state_t state,
4198 SortedVector<audio_io_handle_t>& inputs,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004199 const String8& address)
Eric Laurentd4692962014-05-05 18:13:44 -07004200{
Paul McLean9080a4c2015-06-18 08:24:02 -07004201 audio_devices_t device = devDesc->type();
Eric Laurent1f2f2232014-06-02 12:01:23 -07004202 sp<AudioInputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07004203
4204 if (audio_device_is_digital(device)) {
4205 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08004206 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07004207 }
4208
Eric Laurentd4692962014-05-05 18:13:44 -07004209 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
4210 // first list already open inputs that can be routed to this device
4211 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
4212 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004213 if (desc->mProfile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07004214 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
4215 inputs.add(mInputs.keyAt(input_index));
4216 }
4217 }
4218
4219 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07004220 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07004221 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
4222 {
4223 if (mHwModules[module_idx]->mHandle == 0) {
4224 continue;
4225 }
4226 for (size_t profile_index = 0;
4227 profile_index < mHwModules[module_idx]->mInputProfiles.size();
4228 profile_index++)
4229 {
Eric Laurent275e8e92014-11-30 15:14:47 -08004230 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
4231
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004232 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01004233 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004234 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004235 profiles.add(profile);
4236 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
4237 profile_index, module_idx);
4238 }
Eric Laurentd4692962014-05-05 18:13:44 -07004239 }
4240 }
4241 }
4242
4243 if (profiles.isEmpty() && inputs.isEmpty()) {
4244 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
4245 return BAD_VALUE;
4246 }
4247
4248 // open inputs for matching profiles if needed. Direct inputs are also opened to
4249 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
4250 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
4251
Eric Laurent1c333e22014-05-20 10:48:17 -07004252 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07004253 // nothing to do if one input is already opened for this profile
4254 size_t input_index;
4255 for (input_index = 0; input_index < mInputs.size(); input_index++) {
4256 desc = mInputs.valueAt(input_index);
4257 if (desc->mProfile == profile) {
Paul McLean9080a4c2015-06-18 08:24:02 -07004258 if (audio_device_is_digital(device)) {
4259 devDesc->importAudioPort(profile);
4260 }
Eric Laurentd4692962014-05-05 18:13:44 -07004261 break;
4262 }
4263 }
4264 if (input_index != mInputs.size()) {
4265 continue;
4266 }
4267
4268 ALOGV("opening input for device 0x%X with params %s", device, address.string());
4269 desc = new AudioInputDescriptor(profile);
4270 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07004271 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
4272 config.sample_rate = desc->mSamplingRate;
4273 config.channel_mask = desc->mChannelMask;
4274 config.format = desc->mFormat;
4275 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07004276 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07004277 &input,
4278 &config,
4279 &desc->mDevice,
4280 address,
4281 AUDIO_SOURCE_MIC,
4282 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07004283
Eric Laurentcf2c0212014-07-25 16:20:43 -07004284 if (status == NO_ERROR) {
4285 desc->mSamplingRate = config.sample_rate;
4286 desc->mChannelMask = config.channel_mask;
4287 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07004288
Eric Laurentd4692962014-05-05 18:13:44 -07004289 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07004290 char *param = audio_device_address_to_parameter(device, address);
4291 mpClientInterface->setParameters(input, String8(param));
4292 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07004293 }
Phil Burk00eeb322016-03-31 12:41:00 -07004294 updateAudioProfiles(device, input, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01004295 if (!profile->hasValidAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07004296 ALOGW("checkInputsForDevice() direct input missing param");
4297 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004298 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07004299 }
4300
4301 if (input != 0) {
4302 addInput(input, desc);
4303 }
4304 } // endif input != 0
4305
Eric Laurentcf2c0212014-07-25 16:20:43 -07004306 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07004307 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07004308 profiles.removeAt(profile_index);
4309 profile_index--;
4310 } else {
4311 inputs.add(input);
Paul McLean9080a4c2015-06-18 08:24:02 -07004312 if (audio_device_is_digital(device)) {
4313 devDesc->importAudioPort(profile);
4314 }
Eric Laurentd4692962014-05-05 18:13:44 -07004315 ALOGV("checkInputsForDevice(): adding input %d", input);
4316 }
4317 } // end scan profiles
4318
4319 if (profiles.isEmpty()) {
4320 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
4321 return BAD_VALUE;
4322 }
4323 } else {
4324 // Disconnect
4325 // check if one opened input is not needed any more after disconnecting one device
4326 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
4327 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004328 if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) {
Eric Laurentd4692962014-05-05 18:13:44 -07004329 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
4330 mInputs.keyAt(input_index));
4331 inputs.add(mInputs.keyAt(input_index));
4332 }
4333 }
4334 // Clear any profiles associated with the disconnected device.
4335 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
4336 if (mHwModules[module_index]->mHandle == 0) {
4337 continue;
4338 }
4339 for (size_t profile_index = 0;
4340 profile_index < mHwModules[module_index]->mInputProfiles.size();
4341 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004342 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004343 if (profile->supportDevice(device)) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07004344 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07004345 profile_index, module_index);
François Gaffie112b0af2015-11-19 16:13:25 +01004346 profile->clearAudioProfiles();
Eric Laurentd4692962014-05-05 18:13:44 -07004347 }
4348 }
4349 }
4350 } // end disconnect
4351
4352 return NO_ERROR;
4353}
4354
4355
Eric Laurente0720872014-03-11 09:30:41 -07004356void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07004357{
4358 ALOGV("closeOutput(%d)", output);
4359
Eric Laurentc75307b2015-03-17 15:29:32 -07004360 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004361 if (outputDesc == NULL) {
4362 ALOGW("closeOutput() unknown output %d", output);
4363 return;
4364 }
François Gaffie036e1e92015-03-19 10:16:24 +01004365 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08004366
Eric Laurente552edb2014-03-10 17:42:56 -07004367 // look for duplicated outputs connected to the output being removed.
4368 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004369 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07004370 if (dupOutputDesc->isDuplicated() &&
4371 (dupOutputDesc->mOutput1 == outputDesc ||
4372 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004373 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07004374 if (dupOutputDesc->mOutput1 == outputDesc) {
4375 outputDesc2 = dupOutputDesc->mOutput2;
4376 } else {
4377 outputDesc2 = dupOutputDesc->mOutput1;
4378 }
4379 // As all active tracks on duplicated output will be deleted,
4380 // and as they were also referenced on the other output, the reference
4381 // count for their stream type must be adjusted accordingly on
4382 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07004383 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07004384 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07004385 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07004386 }
4387 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
4388 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
4389
4390 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01004391 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07004392 }
4393 }
4394
Eric Laurent05b90f82014-08-27 15:32:29 -07004395 nextAudioPortGeneration();
4396
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004397 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004398 if (index >= 0) {
4399 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07004400 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07004401 mAudioPatches.removeItemsAt(index);
4402 mpClientInterface->onAudioPatchListUpdate();
4403 }
4404
Eric Laurente552edb2014-03-10 17:42:56 -07004405 AudioParameter param;
4406 param.add(String8("closing"), String8("true"));
4407 mpClientInterface->setParameters(output, param.toString());
4408
4409 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01004410 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004411 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07004412}
4413
4414void AudioPolicyManager::closeInput(audio_io_handle_t input)
4415{
4416 ALOGV("closeInput(%d)", input);
4417
4418 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
4419 if (inputDesc == NULL) {
4420 ALOGW("closeInput() unknown input %d", input);
4421 return;
4422 }
4423
Eric Laurent6a94d692014-05-20 11:18:06 -07004424 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07004425
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004426 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004427 if (index >= 0) {
4428 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07004429 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07004430 mAudioPatches.removeItemsAt(index);
4431 mpClientInterface->onAudioPatchListUpdate();
4432 }
4433
4434 mpClientInterface->closeInput(input);
4435 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07004436}
4437
Eric Laurentc75307b2015-03-17 15:29:32 -07004438SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
4439 audio_devices_t device,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004440 const SwAudioOutputCollection& openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07004441{
4442 SortedVector<audio_io_handle_t> outputs;
4443
4444 ALOGVV("getOutputsForDevice() device %04x", device);
4445 for (size_t i = 0; i < openOutputs.size(); i++) {
Eric Laurent37ddbb42016-08-10 16:19:14 -07004446 ALOGVV("output %zu isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004447 i, openOutputs.valueAt(i)->isDuplicated(),
4448 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004449 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
4450 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
4451 outputs.add(openOutputs.keyAt(i));
4452 }
4453 }
4454 return outputs;
4455}
4456
Eric Laurente0720872014-03-11 09:30:41 -07004457bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01004458 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07004459{
4460 if (outputs1.size() != outputs2.size()) {
4461 return false;
4462 }
4463 for (size_t i = 0; i < outputs1.size(); i++) {
4464 if (outputs1[i] != outputs2[i]) {
4465 return false;
4466 }
4467 }
4468 return true;
4469}
4470
Eric Laurente0720872014-03-11 09:30:41 -07004471void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07004472{
4473 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
4474 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
4475 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
4476 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
4477
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004478 // also take into account external policy-related changes: add all outputs which are
4479 // associated with policies in the "before" and "after" output vectors
4480 ALOGVV("checkOutputForStrategy(): policy related outputs");
4481 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004482 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004483 if (desc != 0 && desc->mPolicyMix != NULL) {
4484 srcOutputs.add(desc->mIoHandle);
4485 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
4486 }
4487 }
4488 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004489 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004490 if (desc != 0 && desc->mPolicyMix != NULL) {
4491 dstOutputs.add(desc->mIoHandle);
4492 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
4493 }
4494 }
4495
Eric Laurente552edb2014-03-10 17:42:56 -07004496 if (!vectorsEqual(srcOutputs,dstOutputs)) {
4497 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
4498 strategy, srcOutputs[0], dstOutputs[0]);
4499 // mute strategy while moving tracks from one output to another
4500 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004501 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01004502 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004503 setStrategyMute(strategy, true, desc);
4504 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004505 }
Eric Laurentd60560a2015-04-10 11:31:20 -07004506 sp<AudioSourceDescriptor> source =
4507 getSourceForStrategyOnOutput(srcOutputs[i], strategy);
4508 if (source != 0){
4509 connectAudioSource(source);
4510 }
Eric Laurente552edb2014-03-10 17:42:56 -07004511 }
4512
4513 // Move effects associated to this strategy from previous output to new output
4514 if (strategy == STRATEGY_MEDIA) {
Eric Laurent36829f92017-04-07 19:04:42 -07004515 selectOutputForMusicEffects();
Eric Laurente552edb2014-03-10 17:42:56 -07004516 }
4517 // Move tracks associated to this strategy from previous output to new output
Eric Laurent794fde22016-03-11 09:50:45 -08004518 for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004519 if (getStrategy((audio_stream_type_t)i) == strategy) {
4520 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07004521 }
4522 }
4523 }
4524}
4525
Eric Laurente0720872014-03-11 09:30:41 -07004526void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07004527{
François Gaffie2110e042015-03-24 08:41:51 +01004528 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004529 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004530 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01004531 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004532 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004533 checkOutputForStrategy(STRATEGY_SONIFICATION);
4534 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004535 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07004536 checkOutputForStrategy(STRATEGY_MEDIA);
4537 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004538 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07004539}
4540
Eric Laurente0720872014-03-11 09:30:41 -07004541void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07004542{
François Gaffie53615e22015-03-19 09:24:12 +01004543 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07004544 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004545 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07004546 return;
4547 }
4548
Eric Laurent3a4311c2014-03-17 12:00:47 -07004549 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08004550 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
4551 ~AUDIO_DEVICE_BIT_IN) != 0) ||
4552 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurentf732e072016-08-03 19:30:28 -07004553
4554 // if suspended, restore A2DP output if:
4555 // ((SCO device is NOT connected) ||
4556 // ((forced usage communication is NOT SCO) && (forced usage for record is NOT SCO) &&
4557 // (phone state is NOT in call) && (phone state is NOT ringing)))
Eric Laurente552edb2014-03-10 17:42:56 -07004558 //
Eric Laurentf732e072016-08-03 19:30:28 -07004559 // if not suspended, suspend A2DP output if:
4560 // (SCO device is connected) &&
4561 // ((forced usage for communication is SCO) || (forced usage for record is SCO) ||
4562 // ((phone state is in call) || (phone state is ringing)))
Eric Laurente552edb2014-03-10 17:42:56 -07004563 //
4564 if (mA2dpSuspended) {
Eric Laurentf732e072016-08-03 19:30:28 -07004565 if (!isScoConnected ||
4566 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) !=
4567 AUDIO_POLICY_FORCE_BT_SCO) &&
4568 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) !=
4569 AUDIO_POLICY_FORCE_BT_SCO) &&
4570 (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
François Gaffie2110e042015-03-24 08:41:51 +01004571 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004572
4573 mpClientInterface->restoreOutput(a2dpOutput);
4574 mA2dpSuspended = false;
4575 }
4576 } else {
Eric Laurentf732e072016-08-03 19:30:28 -07004577 if (isScoConnected &&
4578 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ==
4579 AUDIO_POLICY_FORCE_BT_SCO) ||
4580 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) ==
4581 AUDIO_POLICY_FORCE_BT_SCO) ||
4582 (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
François Gaffie2110e042015-03-24 08:41:51 +01004583 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004584
4585 mpClientInterface->suspendOutput(a2dpOutput);
4586 mA2dpSuspended = true;
4587 }
4588 }
4589}
4590
Eric Laurentc75307b2015-03-17 15:29:32 -07004591audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
4592 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004593{
4594 audio_devices_t device = AUDIO_DEVICE_NONE;
4595
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004596 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004597 if (index >= 0) {
4598 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4599 if (patchDesc->mUid != mUidCached) {
4600 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004601 outputDesc->device(), outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004602 return outputDesc->device();
4603 }
4604 }
4605
Eric Laurente552edb2014-03-10 17:42:56 -07004606 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05004607 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004608 // use device for strategy enforced audible
4609 // 2: we are in call or the strategy phone is active on the output:
4610 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05004611 // 3: the strategy for enforced audible is active but not enforced on the output:
4612 // use the device for strategy enforced audible
Eric Laurent28d09f02016-03-08 10:43:05 -08004613 // 4: the strategy sonification is active on the output:
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004614 // use device for strategy sonification
Eric Laurent28d09f02016-03-08 10:43:05 -08004615 // 5: the strategy accessibility is active on the output:
4616 // use device for strategy accessibility
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004617 // 6: the strategy "respectful" sonification is active on the output:
4618 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08004619 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004620 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08004621 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004622 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08004623 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004624 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01004625 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004626 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07004627 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
4628 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01004629 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004630 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004631 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05004632 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004633 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004634 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Eric Laurent28d09f02016-03-08 10:43:05 -08004635 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
4636 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004637 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004638 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004639 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004640 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004641 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004642 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004643 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004644 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004645 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004646 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07004647 }
4648
Eric Laurent1c333e22014-05-20 10:48:17 -07004649 ALOGV("getNewOutputDevice() selected device %x", device);
4650 return device;
4651}
4652
Eric Laurentfb66dd92016-01-28 18:32:03 -08004653audio_devices_t AudioPolicyManager::getNewInputDevice(const sp<AudioInputDescriptor>& inputDesc)
Eric Laurent1c333e22014-05-20 10:48:17 -07004654{
Eric Laurentfb66dd92016-01-28 18:32:03 -08004655 audio_devices_t device = AUDIO_DEVICE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07004656
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004657 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004658 if (index >= 0) {
4659 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4660 if (patchDesc->mUid != mUidCached) {
4661 ALOGV("getNewInputDevice() device %08x forced by patch %d",
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004662 inputDesc->mDevice, inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004663 return inputDesc->mDevice;
4664 }
4665 }
4666
Eric Laurentfb66dd92016-01-28 18:32:03 -08004667 audio_source_t source = inputDesc->getHighestPrioritySource(true /*activeOnly*/);
4668 if (isInCall()) {
4669 device = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
4670 } else if (source != AUDIO_SOURCE_DEFAULT) {
4671 device = getDeviceAndMixForInputSource(source);
4672 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004673
Eric Laurente552edb2014-03-10 17:42:56 -07004674 return device;
4675}
4676
Eric Laurent794fde22016-03-11 09:50:45 -08004677bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1,
4678 audio_stream_type_t stream2) {
Jean-Michel Trivi99bb2f92016-11-23 15:52:07 -08004679 return (stream1 == stream2);
Eric Laurent28d09f02016-03-08 10:43:05 -08004680}
4681
Eric Laurente0720872014-03-11 09:30:41 -07004682uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004683 return (uint32_t)getStrategy(stream);
4684}
4685
Eric Laurente0720872014-03-11 09:30:41 -07004686audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004687 // By checking the range of stream before calling getStrategy, we avoid
4688 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
4689 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08004690 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004691 return AUDIO_DEVICE_NONE;
4692 }
Eric Laurent28d09f02016-03-08 10:43:05 -08004693 audio_devices_t devices = AUDIO_DEVICE_NONE;
Eric Laurent794fde22016-03-11 09:50:45 -08004694 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
4695 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004696 continue;
Eric Laurent6a94d692014-05-20 11:18:06 -07004697 }
Eric Laurent794fde22016-03-11 09:50:45 -08004698 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08004699 audio_devices_t curDevices =
Eric Laurent447a87b2016-07-07 17:32:10 -07004700 getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/);
Eric Laurent28d09f02016-03-08 10:43:05 -08004701 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs);
4702 for (size_t i = 0; i < outputs.size(); i++) {
4703 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurent794fde22016-03-11 09:50:45 -08004704 if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004705 curDevices |= outputDesc->device();
4706 }
4707 }
4708 devices |= curDevices;
Eric Laurente552edb2014-03-10 17:42:56 -07004709 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05004710
4711 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
4712 and doesn't really need to.*/
4713 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
4714 devices |= AUDIO_DEVICE_OUT_SPEAKER;
4715 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4716 }
Eric Laurente552edb2014-03-10 17:42:56 -07004717 return devices;
4718}
4719
François Gaffie2110e042015-03-24 08:41:51 +01004720routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
4721{
Eric Laurent223fd5c2014-11-11 13:43:36 -08004722 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01004723 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004724}
4725
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004726uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4727 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004728 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4729 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4730 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004731 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4732 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4733 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004734 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01004735 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004736}
4737
Eric Laurente0720872014-03-11 09:30:41 -07004738void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004739 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004740 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004741 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4742 updateDevicesAndOutputs();
4743 break;
4744 default:
4745 break;
4746 }
4747}
4748
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004749uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
Eric Laurent9459fb02015-08-12 18:36:32 -07004750
4751 // skip beacon mute management if a dedicated TTS output is available
4752 if (mTtsOutputAvailable) {
4753 return 0;
4754 }
4755
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004756 switch(event) {
4757 case STARTING_OUTPUT:
4758 mBeaconMuteRefCount++;
4759 break;
4760 case STOPPING_OUTPUT:
4761 if (mBeaconMuteRefCount > 0) {
4762 mBeaconMuteRefCount--;
4763 }
4764 break;
4765 case STARTING_BEACON:
4766 mBeaconPlayingRefCount++;
4767 break;
4768 case STOPPING_BEACON:
4769 if (mBeaconPlayingRefCount > 0) {
4770 mBeaconPlayingRefCount--;
4771 }
4772 break;
4773 }
4774
4775 if (mBeaconMuteRefCount > 0) {
4776 // any playback causes beacon to be muted
4777 return setBeaconMute(true);
4778 } else {
4779 // no other playback: unmute when beacon starts playing, mute when it stops
4780 return setBeaconMute(mBeaconPlayingRefCount == 0);
4781 }
4782}
4783
4784uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4785 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4786 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4787 // keep track of muted state to avoid repeating mute/unmute operations
4788 if (mBeaconMuted != mute) {
4789 // mute/unmute AUDIO_STREAM_TTS on all outputs
4790 ALOGV("\t muting %d", mute);
4791 uint32_t maxLatency = 0;
4792 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004793 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004794 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004795 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004796 0 /*delay*/, AUDIO_DEVICE_NONE);
4797 const uint32_t latency = desc->latency() * 2;
4798 if (latency > maxLatency) {
4799 maxLatency = latency;
4800 }
4801 }
4802 mBeaconMuted = mute;
4803 return maxLatency;
4804 }
4805 return 0;
4806}
4807
Eric Laurente0720872014-03-11 09:30:41 -07004808audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004809 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004810{
Paul McLeanaa981192015-03-21 09:55:15 -07004811 // Routing
4812 // see if we have an explicit route
4813 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4814 // (getStrategy(stream)).
4815 // if the strategy from the stream type in the RouteMap is the same as the argument above,
4816 // and activity count is non-zero
4817 // the device = the device from the descriptor in the RouteMap, and exit.
4818 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4819 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
Eric Laurent28d09f02016-03-08 10:43:05 -08004820 routing_strategy routeStrategy = getStrategy(route->mStreamType);
4821 if ((routeStrategy == strategy) && route->isActive()) {
Paul McLeanaa981192015-03-21 09:55:15 -07004822 return route->mDeviceDescriptor->type();
4823 }
4824 }
4825
Eric Laurente552edb2014-03-10 17:42:56 -07004826 if (fromCache) {
4827 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4828 strategy, mDeviceForStrategy[strategy]);
4829 return mDeviceForStrategy[strategy];
4830 }
François Gaffie2110e042015-03-24 08:41:51 +01004831 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004832}
4833
Eric Laurente0720872014-03-11 09:30:41 -07004834void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004835{
4836 for (int i = 0; i < NUM_STRATEGIES; i++) {
4837 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4838 }
4839 mPreviousOutputs = mOutputs;
4840}
4841
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004842uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004843 audio_devices_t prevDevice,
4844 uint32_t delayMs)
4845{
4846 // mute/unmute strategies using an incompatible device combination
4847 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4848 // if unmuting, unmute only after the specified delay
4849 if (outputDesc->isDuplicated()) {
4850 return 0;
4851 }
4852
4853 uint32_t muteWaitMs = 0;
4854 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004855 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004856
4857 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4858 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004859 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004860 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4861 bool doMute = false;
4862
4863 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4864 doMute = true;
4865 outputDesc->mStrategyMutedByDevice[i] = true;
4866 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4867 doMute = true;
4868 outputDesc->mStrategyMutedByDevice[i] = false;
4869 }
Eric Laurent99401132014-05-07 19:48:15 -07004870 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004871 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004872 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004873 // skip output if it does not share any device with current output
4874 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4875 == AUDIO_DEVICE_NONE) {
4876 continue;
4877 }
Eric Laurent37ddbb42016-08-10 16:19:14 -07004878 ALOGVV("checkDeviceMuteStrategies() %s strategy %zu (curDevice %04x)",
Eric Laurentc75307b2015-03-17 15:29:32 -07004879 mute ? "muting" : "unmuting", i, curDevice);
4880 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004881 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004882 if (mute) {
4883 // FIXME: should not need to double latency if volume could be applied
4884 // immediately by the audioflinger mixer. We must account for the delay
4885 // between now and the next time the audioflinger thread for this output
4886 // will process a buffer (which corresponds to one buffer size,
4887 // usually 1/2 or 1/4 of the latency).
4888 if (muteWaitMs < desc->latency() * 2) {
4889 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004890 }
4891 }
4892 }
4893 }
4894 }
4895 }
4896
Eric Laurent99401132014-05-07 19:48:15 -07004897 // temporary mute output if device selection changes to avoid volume bursts due to
4898 // different per device volumes
4899 if (outputDesc->isActive() && (device != prevDevice)) {
Eric Laurentdc462862016-07-19 12:29:53 -07004900 uint32_t tempMuteWaitMs = outputDesc->latency() * 2;
4901 // temporary mute duration is conservatively set to 4 times the reported latency
4902 uint32_t tempMuteDurationMs = outputDesc->latency() * 4;
4903 if (muteWaitMs < tempMuteWaitMs) {
4904 muteWaitMs = tempMuteWaitMs;
Eric Laurent99401132014-05-07 19:48:15 -07004905 }
Eric Laurentdc462862016-07-19 12:29:53 -07004906
Eric Laurent99401132014-05-07 19:48:15 -07004907 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004908 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentdc462862016-07-19 12:29:53 -07004909 // make sure that we do not start the temporary mute period too early in case of
4910 // delayed device change
4911 setStrategyMute((routing_strategy)i, true, outputDesc, delayMs);
Eric Laurentc75307b2015-03-17 15:29:32 -07004912 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurentdc462862016-07-19 12:29:53 -07004913 delayMs + tempMuteDurationMs, device);
Eric Laurent99401132014-05-07 19:48:15 -07004914 }
4915 }
4916 }
4917
Eric Laurente552edb2014-03-10 17:42:56 -07004918 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4919 if (muteWaitMs > delayMs) {
4920 muteWaitMs -= delayMs;
4921 usleep(muteWaitMs * 1000);
4922 return muteWaitMs;
4923 }
4924 return 0;
4925}
4926
Eric Laurentc75307b2015-03-17 15:29:32 -07004927uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004928 audio_devices_t device,
4929 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004930 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004931 audio_patch_handle_t *patchHandle,
4932 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004933{
Eric Laurentc75307b2015-03-17 15:29:32 -07004934 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004935 AudioParameter param;
4936 uint32_t muteWaitMs;
4937
4938 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004939 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4940 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004941 return muteWaitMs;
4942 }
4943 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4944 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004945 if ((device != AUDIO_DEVICE_NONE) &&
4946 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004947 return 0;
4948 }
4949
4950 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004951 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004952
4953 audio_devices_t prevDevice = outputDesc->mDevice;
4954
Paul McLeanaa981192015-03-21 09:55:15 -07004955 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004956
4957 if (device != AUDIO_DEVICE_NONE) {
4958 outputDesc->mDevice = device;
4959 }
4960 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4961
4962 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004963 // the requested device is AUDIO_DEVICE_NONE
4964 // OR the requested device is the same as current device
4965 // AND force is not specified
4966 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004967 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004968 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4969 !force &&
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004970 outputDesc->getPatchHandle() != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004971 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004972 return muteWaitMs;
4973 }
4974
4975 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004976
Eric Laurente552edb2014-03-10 17:42:56 -07004977 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004978 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004979 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004980 } else {
Eric Laurentc40d9692016-04-13 19:14:13 -07004981 DeviceVector deviceList;
4982 if ((address == NULL) || (strlen(address) == 0)) {
4983 deviceList = mAvailableOutputDevices.getDevicesFromType(device);
4984 } else {
4985 deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
4986 }
4987
Eric Laurent1c333e22014-05-20 10:48:17 -07004988 if (!deviceList.isEmpty()) {
4989 struct audio_patch patch;
4990 outputDesc->toAudioPortConfig(&patch.sources[0]);
4991 patch.num_sources = 1;
4992 patch.num_sinks = 0;
4993 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4994 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004995 patch.num_sinks++;
4996 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004997 ssize_t index;
4998 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4999 index = mAudioPatches.indexOfKey(*patchHandle);
5000 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08005001 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07005002 }
5003 sp< AudioPatch> patchDesc;
5004 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
5005 if (index >= 0) {
5006 patchDesc = mAudioPatches.valueAt(index);
5007 afPatchHandle = patchDesc->mAfPatchHandle;
5008 }
5009
Eric Laurent1c333e22014-05-20 10:48:17 -07005010 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07005011 &afPatchHandle,
5012 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07005013 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
5014 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07005015 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07005016 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07005017 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01005018 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07005019 addAudioPatch(patchDesc->mHandle, patchDesc);
5020 } else {
5021 patchDesc->mPatch = patch;
5022 }
5023 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07005024 if (patchHandle) {
5025 *patchHandle = patchDesc->mHandle;
5026 }
Jean-Michel Triviff155c62016-02-26 12:07:16 -08005027 outputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07005028 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07005029 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07005030 }
5031 }
bryant_liuf5e7e792014-08-19 20:07:05 +08005032
5033 // inform all input as well
5034 for (size_t i = 0; i < mInputs.size(); i++) {
5035 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01005036 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08005037 AudioParameter inputCmd = AudioParameter();
5038 ALOGV("%s: inform input %d of device:%d", __func__,
5039 inputDescriptor->mIoHandle, device);
5040 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
5041 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
5042 inputCmd.toString(),
5043 delayMs);
5044 }
5045 }
Eric Laurent1c333e22014-05-20 10:48:17 -07005046 }
Eric Laurente552edb2014-03-10 17:42:56 -07005047
5048 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07005049 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005050
5051 return muteWaitMs;
5052}
5053
Eric Laurentc75307b2015-03-17 15:29:32 -07005054status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07005055 int delayMs,
5056 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07005057{
Eric Laurent6a94d692014-05-20 11:18:06 -07005058 ssize_t index;
5059 if (patchHandle) {
5060 index = mAudioPatches.indexOfKey(*patchHandle);
5061 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08005062 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07005063 }
5064 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07005065 return INVALID_OPERATION;
5066 }
Eric Laurent6a94d692014-05-20 11:18:06 -07005067 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
5068 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07005069 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07005070 outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07005071 removeAudioPatch(patchDesc->mHandle);
5072 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07005073 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07005074 return status;
5075}
5076
5077status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
5078 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07005079 bool force,
5080 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07005081{
5082 status_t status = NO_ERROR;
5083
Eric Laurent1f2f2232014-06-02 12:01:23 -07005084 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07005085 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
5086 inputDesc->mDevice = device;
5087
5088 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
5089 if (!deviceList.isEmpty()) {
5090 struct audio_patch patch;
5091 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07005092 // AUDIO_SOURCE_HOTWORD is for internal use only:
5093 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07005094 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
Eric Laurent599c7582015-12-07 18:05:55 -08005095 !inputDesc->isSoundTrigger()) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07005096 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
5097 }
Eric Laurent1c333e22014-05-20 10:48:17 -07005098 patch.num_sinks = 1;
5099 //only one input device for now
5100 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07005101 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07005102 ssize_t index;
5103 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
5104 index = mAudioPatches.indexOfKey(*patchHandle);
5105 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08005106 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07005107 }
5108 sp< AudioPatch> patchDesc;
5109 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
5110 if (index >= 0) {
5111 patchDesc = mAudioPatches.valueAt(index);
5112 afPatchHandle = patchDesc->mAfPatchHandle;
5113 }
5114
Eric Laurent1c333e22014-05-20 10:48:17 -07005115 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07005116 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07005117 0);
5118 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07005119 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07005120 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07005121 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01005122 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07005123 addAudioPatch(patchDesc->mHandle, patchDesc);
5124 } else {
5125 patchDesc->mPatch = patch;
5126 }
5127 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07005128 if (patchHandle) {
5129 *patchHandle = patchDesc->mHandle;
5130 }
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08005131 inputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07005132 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07005133 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07005134 }
5135 }
5136 }
5137 return status;
5138}
5139
Eric Laurent6a94d692014-05-20 11:18:06 -07005140status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
5141 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07005142{
Eric Laurent1f2f2232014-06-02 12:01:23 -07005143 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07005144 ssize_t index;
5145 if (patchHandle) {
5146 index = mAudioPatches.indexOfKey(*patchHandle);
5147 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08005148 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07005149 }
5150 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07005151 return INVALID_OPERATION;
5152 }
Eric Laurent6a94d692014-05-20 11:18:06 -07005153 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
5154 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07005155 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07005156 inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07005157 removeAudioPatch(patchDesc->mHandle);
5158 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07005159 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07005160 return status;
5161}
5162
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08005163sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07005164 const String8& address,
François Gaffie53615e22015-03-19 09:24:12 +01005165 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07005166 audio_format_t& format,
5167 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01005168 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07005169{
5170 // Choose an input profile based on the requested capture parameters: select the first available
5171 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07005172 //
5173 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
5174 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07005175
5176 for (size_t i = 0; i < mHwModules.size(); i++)
5177 {
5178 if (mHwModules[i]->mHandle == 0) {
5179 continue;
5180 }
5181 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
5182 {
Eric Laurent1c333e22014-05-20 10:48:17 -07005183 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07005184 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08005185 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07005186 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07005187 format,
5188 &format /*updatedFormat*/,
5189 channelMask,
5190 &channelMask /*updatedChannelMask*/,
5191 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08005192
Eric Laurente552edb2014-03-10 17:42:56 -07005193 return profile;
5194 }
5195 }
5196 }
5197 return NULL;
5198}
5199
Eric Laurentc73ca6e2014-12-12 14:34:22 -08005200
5201audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01005202 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07005203{
François Gaffie036e1e92015-03-19 10:16:24 +01005204 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
5205 audio_devices_t selectedDeviceFromMix =
5206 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08005207
François Gaffie036e1e92015-03-19 10:16:24 +01005208 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
5209 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08005210 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08005211 return getDeviceForInputSource(inputSource);
5212}
5213
5214audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
5215{
Paul McLean466dc8e2015-04-17 13:15:36 -06005216 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
5217 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07005218 if (inputSource == route->mSource && route->isActive()) {
Paul McLean466dc8e2015-04-17 13:15:36 -06005219 return route->mDeviceDescriptor->type();
5220 }
5221 }
5222
5223 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07005224}
5225
Eric Laurente0720872014-03-11 09:30:41 -07005226float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005227 int index,
5228 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005229{
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005230 float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index);
Jean-Michel Trivi3d8b4a42016-09-14 18:37:46 -07005231
5232 // handle the case of accessibility active while a ringtone is playing: if the ringtone is much
5233 // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch
5234 // exploration of the dialer UI. In this situation, bring the accessibility volume closer to
5235 // the ringtone volume
5236 if ((stream == AUDIO_STREAM_ACCESSIBILITY)
5237 && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState())
5238 && isStreamActive(AUDIO_STREAM_RING, 0)) {
5239 const float ringVolumeDB = computeVolume(AUDIO_STREAM_RING, index, device);
5240 return ringVolumeDB - 4 > volumeDB ? ringVolumeDB - 4 : volumeDB;
5241 }
5242
Eric Laurente552edb2014-03-10 17:42:56 -07005243 // if a headset is connected, apply the following rules to ring tones and notifications
5244 // to avoid sound level bursts in user's ears:
Eric Laurent6af1c1d2016-04-14 11:20:44 -07005245 // - always attenuate notifications volume by 6dB
5246 // - attenuate ring tones volume by 6dB unless music is not playing and
5247 // speaker is part of the select devices
Eric Laurente552edb2014-03-10 17:42:56 -07005248 // - if music is playing, always limit the volume to current music volume,
5249 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07005250 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005251 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
5252 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
5253 AUDIO_DEVICE_OUT_WIRED_HEADSET |
Eric Laurent904d6322017-03-17 17:20:47 -07005254 AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
5255 AUDIO_DEVICE_OUT_USB_HEADSET)) &&
Eric Laurente552edb2014-03-10 17:42:56 -07005256 ((stream_strategy == STRATEGY_SONIFICATION)
5257 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07005258 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07005259 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01005260 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
François Gaffied1ab2bd2015-12-02 18:20:06 +01005261 mVolumeCurves->canBeMuted(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005262 // when the phone is ringing we must consider that music could have been paused just before
5263 // by the music application and behave as if music was active if the last music track was
5264 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07005265 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07005266 mLimitRingtoneVolume) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005267 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07005268 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07005269 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005270 mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC,
5271 musicDevice),
5272 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07005273 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
5274 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005275 if (volumeDB > minVolDB) {
5276 volumeDB = minVolDB;
Eric Laurentffbc80f2015-03-18 18:30:19 -07005277 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07005278 }
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005279 if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
5280 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) {
5281 // on A2DP, also ensure notification volume is not too low compared to media when
5282 // intended to be played
5283 if ((volumeDB > -96.0f) &&
5284 (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) {
5285 ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f",
5286 stream, device,
5287 volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB);
5288 volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB;
5289 }
5290 }
Eric Laurent6af1c1d2016-04-14 11:20:44 -07005291 } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) ||
5292 stream_strategy != STRATEGY_SONIFICATION) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005293 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07005294 }
5295 }
5296
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005297 return volumeDB;
Eric Laurente552edb2014-03-10 17:42:56 -07005298}
5299
Eric Laurente0720872014-03-11 09:30:41 -07005300status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005301 int index,
5302 const sp<AudioOutputDescriptor>& outputDesc,
5303 audio_devices_t device,
5304 int delayMs,
5305 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005306{
Eric Laurente552edb2014-03-10 17:42:56 -07005307 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07005308 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07005309 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07005310 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07005311 return NO_ERROR;
5312 }
François Gaffie2110e042015-03-24 08:41:51 +01005313 audio_policy_forced_cfg_t forceUseForComm =
5314 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07005315 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01005316 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
5317 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005318 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01005319 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07005320 return INVALID_OPERATION;
5321 }
5322
Eric Laurentc75307b2015-03-17 15:29:32 -07005323 if (device == AUDIO_DEVICE_NONE) {
5324 device = outputDesc->device();
5325 }
Eric Laurent275e8e92014-11-30 15:14:47 -08005326
Eric Laurentffbc80f2015-03-18 18:30:19 -07005327 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07005328 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07005329 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08005330 }
Eric Laurentc75307b2015-03-17 15:29:32 -07005331
Eric Laurentffbc80f2015-03-18 18:30:19 -07005332 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07005333
Eric Laurent3b73df72014-03-11 09:06:29 -07005334 if (stream == AUDIO_STREAM_VOICE_CALL ||
5335 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07005336 float voiceVolume;
5337 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07005338 if (stream == AUDIO_STREAM_VOICE_CALL) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005339 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005340 } else {
5341 voiceVolume = 1.0;
5342 }
5343
Eric Laurent18fba842016-03-31 14:41:26 -07005344 if (voiceVolume != mLastVoiceVolume) {
Eric Laurente552edb2014-03-10 17:42:56 -07005345 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
5346 mLastVoiceVolume = voiceVolume;
5347 }
5348 }
5349
5350 return NO_ERROR;
5351}
5352
Eric Laurentc75307b2015-03-17 15:29:32 -07005353void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
5354 audio_devices_t device,
5355 int delayMs,
5356 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005357{
Eric Laurentc75307b2015-03-17 15:29:32 -07005358 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07005359
Eric Laurent794fde22016-03-11 09:50:45 -08005360 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005361 checkAndSetVolume((audio_stream_type_t)stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005362 mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005363 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005364 device,
5365 delayMs,
5366 force);
5367 }
5368}
5369
Eric Laurente0720872014-03-11 09:30:41 -07005370void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07005371 bool on,
5372 const sp<AudioOutputDescriptor>& outputDesc,
5373 int delayMs,
5374 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005375{
Eric Laurent72249d52015-06-08 11:12:15 -07005376 ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d",
5377 strategy, on, outputDesc->getId());
Eric Laurent794fde22016-03-11 09:50:45 -08005378 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005379 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005380 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07005381 }
5382 }
5383}
5384
Eric Laurente0720872014-03-11 09:30:41 -07005385void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005386 bool on,
5387 const sp<AudioOutputDescriptor>& outputDesc,
5388 int delayMs,
5389 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005390{
Eric Laurente552edb2014-03-10 17:42:56 -07005391 if (device == AUDIO_DEVICE_NONE) {
5392 device = outputDesc->device();
5393 }
5394
Eric Laurentc75307b2015-03-17 15:29:32 -07005395 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
5396 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07005397
5398 if (on) {
5399 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005400 if (mVolumeCurves->canBeMuted(stream) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07005401 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01005402 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005403 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005404 }
5405 }
5406 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
5407 outputDesc->mMuteCount[stream]++;
5408 } else {
5409 if (outputDesc->mMuteCount[stream] == 0) {
5410 ALOGV("setStreamMute() unmuting non muted stream!");
5411 return;
5412 }
5413 if (--outputDesc->mMuteCount[stream] == 0) {
5414 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005415 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005416 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005417 device,
5418 delayMs);
5419 }
5420 }
5421}
5422
Eric Laurente0720872014-03-11 09:30:41 -07005423void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07005424 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07005425{
Eric Laurent87ffa392015-05-22 10:32:38 -07005426 if(!hasPrimaryOutput()) {
5427 return;
5428 }
5429
Eric Laurente552edb2014-03-10 17:42:56 -07005430 // if the stream pertains to sonification strategy and we are in call we must
5431 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
5432 // in the device used for phone strategy and play the tone if the selected device does not
5433 // interfere with the device used for phone strategy
5434 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
5435 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07005436 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005437 if ((stream_strategy == STRATEGY_SONIFICATION) ||
5438 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005439 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07005440 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
5441 stream, starting, outputDesc->mDevice, stateChange);
5442 if (outputDesc->mRefCount[stream]) {
5443 int muteCount = 1;
5444 if (stateChange) {
5445 muteCount = outputDesc->mRefCount[stream];
5446 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005447 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005448 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
5449 for (int i = 0; i < muteCount; i++) {
5450 setStreamMute(stream, starting, mPrimaryOutput);
5451 }
5452 } else {
5453 ALOGV("handleIncallSonification() high visibility");
5454 if (outputDesc->device() &
5455 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
5456 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
5457 for (int i = 0; i < muteCount; i++) {
5458 setStreamMute(stream, starting, mPrimaryOutput);
5459 }
5460 }
5461 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005462 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
5463 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07005464 } else {
5465 mpClientInterface->stopTone();
5466 }
5467 }
5468 }
5469 }
5470}
5471
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005472audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
5473{
5474 // flags to stream type mapping
5475 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
5476 return AUDIO_STREAM_ENFORCED_AUDIBLE;
5477 }
5478 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
5479 return AUDIO_STREAM_BLUETOOTH_SCO;
5480 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08005481 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
5482 return AUDIO_STREAM_TTS;
5483 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005484
5485 // usage to stream type mapping
5486 switch (attr->usage) {
5487 case AUDIO_USAGE_MEDIA:
5488 case AUDIO_USAGE_GAME:
Jean-Michel Trivi36867762016-12-29 12:03:28 -08005489 case AUDIO_USAGE_ASSISTANT:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005490 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5491 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08005492 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5493 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005494 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5495 return AUDIO_STREAM_SYSTEM;
5496 case AUDIO_USAGE_VOICE_COMMUNICATION:
5497 return AUDIO_STREAM_VOICE_CALL;
5498
5499 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5500 return AUDIO_STREAM_DTMF;
5501
5502 case AUDIO_USAGE_ALARM:
5503 return AUDIO_STREAM_ALARM;
5504 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5505 return AUDIO_STREAM_RING;
5506
5507 case AUDIO_USAGE_NOTIFICATION:
5508 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5509 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5510 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5511 case AUDIO_USAGE_NOTIFICATION_EVENT:
5512 return AUDIO_STREAM_NOTIFICATION;
5513
5514 case AUDIO_USAGE_UNKNOWN:
5515 default:
5516 return AUDIO_STREAM_MUSIC;
5517 }
5518}
Eric Laurente83b55d2014-11-14 10:06:21 -08005519
François Gaffie53615e22015-03-19 09:24:12 +01005520bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
5521{
Eric Laurente83b55d2014-11-14 10:06:21 -08005522 // has flags that map to a strategy?
5523 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
5524 return true;
5525 }
5526
5527 // has known usage?
5528 switch (paa->usage) {
5529 case AUDIO_USAGE_UNKNOWN:
5530 case AUDIO_USAGE_MEDIA:
5531 case AUDIO_USAGE_VOICE_COMMUNICATION:
5532 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5533 case AUDIO_USAGE_ALARM:
5534 case AUDIO_USAGE_NOTIFICATION:
5535 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5536 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5537 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5538 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5539 case AUDIO_USAGE_NOTIFICATION_EVENT:
5540 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5541 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5542 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5543 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08005544 case AUDIO_USAGE_VIRTUAL_SOURCE:
Jean-Michel Trivi36867762016-12-29 12:03:28 -08005545 case AUDIO_USAGE_ASSISTANT:
Eric Laurente83b55d2014-11-14 10:06:21 -08005546 break;
5547 default:
5548 return false;
5549 }
5550 return true;
5551}
5552
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07005553bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor>& outputDesc,
François Gaffiead3183e2015-03-18 16:55:35 +01005554 routing_strategy strategy, uint32_t inPastMs,
5555 nsecs_t sysTime) const
5556{
5557 if ((sysTime == 0) && (inPastMs != 0)) {
5558 sysTime = systemTime();
5559 }
Eric Laurent794fde22016-03-11 09:50:45 -08005560 for (int i = 0; i < (int)AUDIO_STREAM_FOR_POLICY_CNT; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01005561 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
5562 (NUM_STRATEGIES == strategy)) &&
5563 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
5564 return true;
5565 }
5566 }
5567 return false;
5568}
5569
François Gaffie2110e042015-03-24 08:41:51 +01005570audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
5571{
5572 return mEngine->getForceUse(usage);
5573}
5574
5575bool AudioPolicyManager::isInCall()
5576{
5577 return isStateInCall(mEngine->getPhoneState());
5578}
5579
5580bool AudioPolicyManager::isStateInCall(int state)
5581{
5582 return is_state_in_call(state);
5583}
5584
Eric Laurentd60560a2015-04-10 11:31:20 -07005585void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc)
5586{
5587 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
5588 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
5589 if (sourceDesc->mDevice->equals(deviceDesc)) {
5590 ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle());
5591 stopAudioSource(sourceDesc->getHandle());
5592 }
5593 }
5594
5595 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
5596 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
5597 bool release = false;
5598 for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) {
5599 const struct audio_port_config *source = &patchDesc->mPatch.sources[j];
5600 if (source->type == AUDIO_PORT_TYPE_DEVICE &&
5601 source->ext.device.type == deviceDesc->type()) {
5602 release = true;
5603 }
5604 }
5605 for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) {
5606 const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j];
5607 if (sink->type == AUDIO_PORT_TYPE_DEVICE &&
5608 sink->ext.device.type == deviceDesc->type()) {
5609 release = true;
5610 }
5611 }
5612 if (release) {
5613 ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle);
5614 releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid);
5615 }
5616 }
5617}
5618
Phil Burk09bc4612016-02-24 15:58:15 -08005619// Modify the list of surround sound formats supported.
Phil Burk0709b0a2016-03-31 12:54:57 -07005620void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) {
5621 FormatVector &formats = *formatsPtr;
Phil Burk07ac1142016-03-25 13:39:29 -07005622 // TODO Set this based on Config properties.
5623 const bool alwaysForceAC3 = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005624
5625 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5626 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
Phil Burk0709b0a2016-03-31 12:54:57 -07005627 ALOGD("%s: forced use = %d", __FUNCTION__, forceUse);
Phil Burk09bc4612016-02-24 15:58:15 -08005628
5629 // Analyze original support for various formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005630 bool supportsAC3 = false;
5631 bool supportsOtherSurround = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005632 bool supportsIEC61937 = false;
5633 for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) {
5634 audio_format_t format = formats[formatIndex];
Phil Burk09bc4612016-02-24 15:58:15 -08005635 switch (format) {
5636 case AUDIO_FORMAT_AC3:
Phil Burk07ac1142016-03-25 13:39:29 -07005637 supportsAC3 = true;
5638 break;
Phil Burk09bc4612016-02-24 15:58:15 -08005639 case AUDIO_FORMAT_E_AC3:
5640 case AUDIO_FORMAT_DTS:
5641 case AUDIO_FORMAT_DTS_HD:
Phil Burk07ac1142016-03-25 13:39:29 -07005642 supportsOtherSurround = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005643 break;
5644 case AUDIO_FORMAT_IEC61937:
5645 supportsIEC61937 = true;
5646 break;
5647 default:
5648 break;
5649 }
5650 }
Phil Burk09bc4612016-02-24 15:58:15 -08005651
5652 // Modify formats based on surround preferences.
5653 // If NEVER, remove support for surround formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005654 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5655 if (supportsAC3 || supportsOtherSurround || supportsIEC61937) {
5656 // Remove surround sound related formats.
5657 for (size_t formatIndex = 0; formatIndex < formats.size(); ) {
5658 audio_format_t format = formats[formatIndex];
5659 switch(format) {
5660 case AUDIO_FORMAT_AC3:
5661 case AUDIO_FORMAT_E_AC3:
5662 case AUDIO_FORMAT_DTS:
5663 case AUDIO_FORMAT_DTS_HD:
5664 case AUDIO_FORMAT_IEC61937:
Phil Burk07ac1142016-03-25 13:39:29 -07005665 formats.removeAt(formatIndex);
5666 break;
5667 default:
5668 formatIndex++; // keep it
5669 break;
5670 }
Phil Burk09bc4612016-02-24 15:58:15 -08005671 }
Phil Burk07ac1142016-03-25 13:39:29 -07005672 supportsAC3 = false;
5673 supportsOtherSurround = false;
5674 supportsIEC61937 = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005675 }
Phil Burk07ac1142016-03-25 13:39:29 -07005676 } else { // AUTO or ALWAYS
5677 // Most TVs support AC3 even if they do not report it in the EDID.
5678 if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS))
5679 && !supportsAC3) {
5680 formats.add(AUDIO_FORMAT_AC3);
5681 supportsAC3 = true;
5682 }
5683
5684 // If ALWAYS, add support for raw surround formats if all are missing.
5685 // This assumes that if any of these formats are reported by the HAL
5686 // then the report is valid and should not be modified.
5687 if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)
5688 && !supportsOtherSurround) {
5689 formats.add(AUDIO_FORMAT_E_AC3);
5690 formats.add(AUDIO_FORMAT_DTS);
5691 formats.add(AUDIO_FORMAT_DTS_HD);
5692 supportsOtherSurround = true;
5693 }
5694
5695 // Add support for IEC61937 if any raw surround supported.
5696 // The HAL could do this but add it here, just in case.
5697 if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) {
5698 formats.add(AUDIO_FORMAT_IEC61937);
5699 supportsIEC61937 = true;
5700 }
Phil Burk09bc4612016-02-24 15:58:15 -08005701 }
Phil Burk0709b0a2016-03-31 12:54:57 -07005702}
5703
5704// Modify the list of channel masks supported.
5705void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) {
5706 ChannelsVector &channelMasks = *channelMasksPtr;
5707 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5708 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
5709
5710 // If NEVER, then remove support for channelMasks > stereo.
5711 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5712 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) {
5713 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5714 if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) {
5715 ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask);
5716 channelMasks.removeAt(maskIndex);
5717 } else {
5718 maskIndex++;
5719 }
5720 }
5721 // If ALWAYS, then make sure we at least support 5.1
5722 } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) {
5723 bool supports5dot1 = false;
5724 // Are there any channel masks that can be considered "surround"?
5725 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); maskIndex++) {
5726 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5727 if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) {
5728 supports5dot1 = true;
5729 break;
5730 }
5731 }
5732 // If not then add 5.1 support.
5733 if (!supports5dot1) {
5734 channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1);
5735 ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__);
5736 }
Phil Burk09bc4612016-02-24 15:58:15 -08005737 }
5738}
5739
Phil Burk00eeb322016-03-31 12:41:00 -07005740void AudioPolicyManager::updateAudioProfiles(audio_devices_t device,
5741 audio_io_handle_t ioHandle,
François Gaffie112b0af2015-11-19 16:13:25 +01005742 AudioProfileVector &profiles)
5743{
5744 String8 reply;
Phil Burk0709b0a2016-03-31 12:54:57 -07005745
François Gaffie112b0af2015-11-19 16:13:25 +01005746 // Format MUST be checked first to update the list of AudioProfile
5747 if (profiles.hasDynamicFormat()) {
Mikhail Naganov388360c2016-10-17 17:09:41 -07005748 reply = mpClientInterface->getParameters(
5749 ioHandle, String8(AudioParameter::keyStreamSupportedFormats));
Phil Burk0709b0a2016-03-31 12:54:57 -07005750 ALOGV("%s: supported formats %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005751 AudioParameter repliedParameters(reply);
5752 if (repliedParameters.get(
Mikhail Naganov388360c2016-10-17 17:09:41 -07005753 String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) {
François Gaffie112b0af2015-11-19 16:13:25 +01005754 ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__);
5755 return;
5756 }
Phil Burk09bc4612016-02-24 15:58:15 -08005757 FormatVector formats = formatsFromString(reply.string());
Phil Burk00eeb322016-03-31 12:41:00 -07005758 if (device == AUDIO_DEVICE_OUT_HDMI) {
Phil Burk0709b0a2016-03-31 12:54:57 -07005759 filterSurroundFormats(&formats);
Phil Burk00eeb322016-03-31 12:41:00 -07005760 }
Phil Burk09bc4612016-02-24 15:58:15 -08005761 profiles.setFormats(formats);
François Gaffie112b0af2015-11-19 16:13:25 +01005762 }
5763 const FormatVector &supportedFormats = profiles.getSupportedFormats();
5764
Eric Laurent20eb3a42016-01-26 18:39:17 -08005765 for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) {
François Gaffie112b0af2015-11-19 16:13:25 +01005766 audio_format_t format = supportedFormats[formatIndex];
Eric Laurent20eb3a42016-01-26 18:39:17 -08005767 ChannelsVector channelMasks;
5768 SampleRateVector samplingRates;
François Gaffie112b0af2015-11-19 16:13:25 +01005769 AudioParameter requestedParameters;
Mikhail Naganov388360c2016-10-17 17:09:41 -07005770 requestedParameters.addInt(String8(AudioParameter::keyFormat), format);
François Gaffie112b0af2015-11-19 16:13:25 +01005771
5772 if (profiles.hasDynamicRateFor(format)) {
Mikhail Naganov388360c2016-10-17 17:09:41 -07005773 reply = mpClientInterface->getParameters(
5774 ioHandle,
5775 requestedParameters.toString() + ";" +
5776 AudioParameter::keyStreamSupportedSamplingRates);
François Gaffie112b0af2015-11-19 16:13:25 +01005777 ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005778 AudioParameter repliedParameters(reply);
5779 if (repliedParameters.get(
Mikhail Naganov388360c2016-10-17 17:09:41 -07005780 String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) {
Eric Laurent62e4bc52016-02-02 18:37:28 -08005781 samplingRates = samplingRatesFromString(reply.string());
François Gaffie112b0af2015-11-19 16:13:25 +01005782 }
5783 }
5784 if (profiles.hasDynamicChannelsFor(format)) {
5785 reply = mpClientInterface->getParameters(ioHandle,
5786 requestedParameters.toString() + ";" +
Mikhail Naganov388360c2016-10-17 17:09:41 -07005787 AudioParameter::keyStreamSupportedChannels);
François Gaffie112b0af2015-11-19 16:13:25 +01005788 ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005789 AudioParameter repliedParameters(reply);
5790 if (repliedParameters.get(
Mikhail Naganov388360c2016-10-17 17:09:41 -07005791 String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) {
Eric Laurent62e4bc52016-02-02 18:37:28 -08005792 channelMasks = channelMasksFromString(reply.string());
Phil Burk0709b0a2016-03-31 12:54:57 -07005793 if (device == AUDIO_DEVICE_OUT_HDMI) {
5794 filterSurroundChannelMasks(&channelMasks);
5795 }
François Gaffie112b0af2015-11-19 16:13:25 +01005796 }
5797 }
Eric Laurent20eb3a42016-01-26 18:39:17 -08005798 profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates));
François Gaffie112b0af2015-11-19 16:13:25 +01005799 }
5800}
Eric Laurentd60560a2015-04-10 11:31:20 -07005801
Eric Laurente552edb2014-03-10 17:42:56 -07005802}; // namespace android