blob: 8f9008033556142627bc95dff0597ce1a94d6c53 [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
François Gaffief4ad6e52015-11-19 16:59:57 +010027#define AUDIO_POLICY_XML_CONFIG_FILE "/system/etc/audio_policy_configuration.xml"
28
Eric Laurentd4692962014-05-05 18:13:44 -070029#include <inttypes.h>
Eric Laurente552edb2014-03-10 17:42:56 -070030#include <math.h>
Eric Laurentd4692962014-05-05 18:13:44 -070031
François Gaffie2110e042015-03-24 08:41:51 +010032#include <AudioPolicyManagerInterface.h>
33#include <AudioPolicyEngineInstance.h>
Eric Laurente552edb2014-03-10 17:42:56 -070034#include <cutils/properties.h>
Eric Laurentd4692962014-05-05 18:13:44 -070035#include <utils/Log.h>
36#include <hardware/audio.h>
37#include <hardware/audio_effect.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>
Eric Laurentd4692962014-05-05 18:13:44 -070041#include "AudioPolicyManager.h"
François Gaffied1ab2bd2015-12-02 18:20:06 +010042#ifndef USE_XML_AUDIO_POLICY_CONF
François Gaffie53615e22015-03-19 09:24:12 +010043#include <ConfigParsingUtils.h>
François Gaffied1ab2bd2015-12-02 18:20:06 +010044#include <StreamDescriptor.h>
François Gaffief4ad6e52015-11-19 16:59:57 +010045#endif
François Gaffied1ab2bd2015-12-02 18:20:06 +010046#include <Serializer.h>
François Gaffiea8ecc2c2015-11-09 16:10:40 +010047#include "TypeConverter.h"
François Gaffie53615e22015-03-19 09:24:12 +010048#include <policy.h>
Eric Laurente552edb2014-03-10 17:42:56 -070049
Eric Laurent3b73df72014-03-11 09:06:29 -070050namespace android {
Eric Laurente552edb2014-03-10 17:42:56 -070051
Eric Laurentdc462862016-07-19 12:29:53 -070052//FIXME: workaround for truncated touch sounds
53// to be removed when the problem is handled by system UI
54#define TOUCH_SOUND_FIXED_DELAY_MS 100
Eric Laurente552edb2014-03-10 17:42:56 -070055// ----------------------------------------------------------------------------
56// AudioPolicyInterface implementation
57// ----------------------------------------------------------------------------
58
Eric Laurente0720872014-03-11 09:30:41 -070059status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
Paul McLeane743a472015-01-28 11:07:31 -080060 audio_policy_dev_state_t state,
61 const char *device_address,
62 const char *device_name)
Eric Laurente552edb2014-03-10 17:42:56 -070063{
Paul McLeane743a472015-01-28 11:07:31 -080064 return setDeviceConnectionStateInt(device, state, device_address, device_name);
Eric Laurentc73ca6e2014-12-12 14:34:22 -080065}
66
François Gaffie44481e72016-04-20 07:49:57 +020067void AudioPolicyManager::broadcastDeviceConnectionState(audio_devices_t device,
68 audio_policy_dev_state_t state,
69 const String8 &device_address)
70{
71 AudioParameter param(device_address);
72 const String8 key(state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE ?
73 AUDIO_PARAMETER_DEVICE_CONNECT : AUDIO_PARAMETER_DEVICE_DISCONNECT);
74 param.addInt(key, device);
75 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
76}
77
Eric Laurentc73ca6e2014-12-12 14:34:22 -080078status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device,
Eric Laurenta1d525f2015-01-29 13:36:45 -080079 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -080080 const char *device_address,
81 const char *device_name)
Eric Laurentc73ca6e2014-12-12 14:34:22 -080082{
Paul McLeane743a472015-01-28 11:07:31 -080083 ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
84- device, state, device_address, device_name);
Eric Laurente552edb2014-03-10 17:42:56 -070085
86 // connect/disconnect only 1 device at a time
87 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
88
François Gaffie53615e22015-03-19 09:24:12 +010089 sp<DeviceDescriptor> devDesc =
90 mHwModules.getDeviceDescriptor(device, device_address, device_name);
Paul McLeane743a472015-01-28 11:07:31 -080091
Eric Laurente552edb2014-03-10 17:42:56 -070092 // handle output devices
93 if (audio_is_output_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -070094 SortedVector <audio_io_handle_t> outputs;
95
Eric Laurent3a4311c2014-03-17 12:00:47 -070096 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
97
Eric Laurente552edb2014-03-10 17:42:56 -070098 // save a copy of the opened output descriptors before any output is opened or closed
99 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
100 mPreviousOutputs = mOutputs;
Eric Laurente552edb2014-03-10 17:42:56 -0700101 switch (state)
102 {
103 // handle output device connection
Eric Laurent3ae5f312015-02-03 17:12:08 -0800104 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700105 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700106 ALOGW("setDeviceConnectionState() device already connected: %x", device);
107 return INVALID_OPERATION;
108 }
109 ALOGV("setDeviceConnectionState() connecting device %x", device);
110
Eric Laurente552edb2014-03-10 17:42:56 -0700111 // register new device as available
Eric Laurent3a4311c2014-03-17 12:00:47 -0700112 index = mAvailableOutputDevices.add(devDesc);
113 if (index >= 0) {
François Gaffie53615e22015-03-19 09:24:12 +0100114 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurentcf817a22014-08-04 20:36:31 -0700115 if (module == 0) {
116 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
117 device);
118 mAvailableOutputDevices.remove(devDesc);
119 return INVALID_OPERATION;
120 }
Paul McLeane743a472015-01-28 11:07:31 -0800121 mAvailableOutputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700122 } else {
123 return NO_MEMORY;
Eric Laurente552edb2014-03-10 17:42:56 -0700124 }
125
François Gaffie44481e72016-04-20 07:49:57 +0200126 // Before checking outputs, broadcast connect event to allow HAL to retrieve dynamic
127 // parameters on newly connected devices (instead of opening the outputs...)
128 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
129
Eric Laurenta1d525f2015-01-29 13:36:45 -0800130 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700131 mAvailableOutputDevices.remove(devDesc);
François Gaffie44481e72016-04-20 07:49:57 +0200132
133 broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
134 devDesc->mAddress);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700135 return INVALID_OPERATION;
136 }
François Gaffie2110e042015-03-24 08:41:51 +0100137 // Propagate device availability to Engine
138 mEngine->setDeviceConnectionState(devDesc, state);
139
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700140 // outputs should never be empty here
141 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
142 "checkOutputsForDevice() returned no outputs but status OK");
143 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
144 outputs.size());
Eric Laurent3ae5f312015-02-03 17:12:08 -0800145
Eric Laurent3ae5f312015-02-03 17:12:08 -0800146 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700147 // handle output device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700148 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700149 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700150 ALOGW("setDeviceConnectionState() device not connected: %x", device);
151 return INVALID_OPERATION;
152 }
153
Paul McLean5c477aa2014-08-20 16:47:57 -0700154 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
155
Paul McLeane743a472015-01-28 11:07:31 -0800156 // Send Disconnect to HALs
François Gaffie44481e72016-04-20 07:49:57 +0200157 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700158
Eric Laurente552edb2014-03-10 17:42:56 -0700159 // remove device from available output devices
Eric Laurent3a4311c2014-03-17 12:00:47 -0700160 mAvailableOutputDevices.remove(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700161
Eric Laurenta1d525f2015-01-29 13:36:45 -0800162 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
François Gaffie2110e042015-03-24 08:41:51 +0100163
164 // Propagate device availability to Engine
165 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurente552edb2014-03-10 17:42:56 -0700166 } break;
167
168 default:
169 ALOGE("setDeviceConnectionState() invalid state: %x", state);
170 return BAD_VALUE;
171 }
172
Eric Laurent3a4311c2014-03-17 12:00:47 -0700173 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
174 // output is suspended before any tracks are moved to it
Eric Laurente552edb2014-03-10 17:42:56 -0700175 checkA2dpSuspend();
176 checkOutputForAllStrategies();
177 // outputs must be closed after checkOutputForAllStrategies() is executed
178 if (!outputs.isEmpty()) {
179 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700180 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700181 // close unused outputs after device disconnection or direct outputs that have been
182 // opened by checkOutputsForDevice() to query dynamic parameters
Eric Laurent3b73df72014-03-11 09:06:29 -0700183 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Eric Laurente552edb2014-03-10 17:42:56 -0700184 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
185 (desc->mDirectOpenCount == 0))) {
186 closeOutput(outputs[i]);
187 }
188 }
Eric Laurent3a4311c2014-03-17 12:00:47 -0700189 // check again after closing A2DP output to reset mA2dpSuspended if needed
190 checkA2dpSuspend();
Eric Laurente552edb2014-03-10 17:42:56 -0700191 }
192
193 updateDevicesAndOutputs();
Eric Laurent87ffa392015-05-22 10:32:38 -0700194 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700195 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
196 updateCallRouting(newDevice);
197 }
Eric Laurente552edb2014-03-10 17:42:56 -0700198 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700199 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
200 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
201 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700202 // do not force device change on duplicated output because if device is 0, it will
203 // also force a device 0 for the two outputs it is duplicated to which may override
204 // a valid device selection on those outputs.
Eric Laurentc75307b2015-03-17 15:29:32 -0700205 bool force = !desc->isDuplicated()
François Gaffie53615e22015-03-19 09:24:12 +0100206 && (!device_distinguishes_on_address(device)
Eric Laurentc2730ba2014-07-20 15:47:07 -0700207 // always force when disconnecting (a non-duplicated device)
208 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Eric Laurentc75307b2015-03-17 15:29:32 -0700209 setOutputDevice(desc, newDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700210 }
Eric Laurente552edb2014-03-10 17:42:56 -0700211 }
212
Eric Laurentd60560a2015-04-10 11:31:20 -0700213 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
214 cleanUpForDevice(devDesc);
215 }
216
Eric Laurent72aa32f2014-05-30 18:51:48 -0700217 mpClientInterface->onAudioPortListUpdate();
Eric Laurentb71e58b2014-05-29 16:08:11 -0700218 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700219 } // end if is output device
220
Eric Laurente552edb2014-03-10 17:42:56 -0700221 // handle input devices
222 if (audio_is_input_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700223 SortedVector <audio_io_handle_t> inputs;
224
Eric Laurent3a4311c2014-03-17 12:00:47 -0700225 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700226 switch (state)
227 {
228 // handle input device connection
Eric Laurent3b73df72014-03-11 09:06:29 -0700229 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700230 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700231 ALOGW("setDeviceConnectionState() device already connected: %d", device);
232 return INVALID_OPERATION;
233 }
François Gaffie53615e22015-03-19 09:24:12 +0100234 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurent6a94d692014-05-20 11:18:06 -0700235 if (module == NULL) {
236 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
237 device);
238 return INVALID_OPERATION;
239 }
François Gaffie44481e72016-04-20 07:49:57 +0200240
241 // Before checking intputs, broadcast connect event to allow HAL to retrieve dynamic
242 // parameters on newly connected devices (instead of opening the inputs...)
243 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
244
Paul McLean9080a4c2015-06-18 08:24:02 -0700245 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
François Gaffie44481e72016-04-20 07:49:57 +0200246 broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
247 devDesc->mAddress);
Eric Laurentd4692962014-05-05 18:13:44 -0700248 return INVALID_OPERATION;
249 }
250
Eric Laurent3a4311c2014-03-17 12:00:47 -0700251 index = mAvailableInputDevices.add(devDesc);
252 if (index >= 0) {
Paul McLeane743a472015-01-28 11:07:31 -0800253 mAvailableInputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700254 } else {
255 return NO_MEMORY;
256 }
Eric Laurent3ae5f312015-02-03 17:12:08 -0800257
François Gaffie2110e042015-03-24 08:41:51 +0100258 // Propagate device availability to Engine
259 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700260 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700261
262 // handle input device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700263 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700264 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700265 ALOGW("setDeviceConnectionState() device not connected: %d", device);
266 return INVALID_OPERATION;
267 }
Paul McLean5c477aa2014-08-20 16:47:57 -0700268
269 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
270
271 // Set Disconnect to HALs
François Gaffie44481e72016-04-20 07:49:57 +0200272 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700273
Paul McLean9080a4c2015-06-18 08:24:02 -0700274 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700275 mAvailableInputDevices.remove(devDesc);
Paul McLean5c477aa2014-08-20 16:47:57 -0700276
François Gaffie2110e042015-03-24 08:41:51 +0100277 // Propagate device availability to Engine
278 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700279 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700280
281 default:
282 ALOGE("setDeviceConnectionState() invalid state: %x", state);
283 return BAD_VALUE;
284 }
285
Eric Laurentd4692962014-05-05 18:13:44 -0700286 closeAllInputs();
Eric Laurent5f5fca52016-08-04 11:48:57 -0700287 // As the input device list can impact the output device selection, update
288 // getDeviceForStrategy() cache
289 updateDevicesAndOutputs();
Eric Laurente552edb2014-03-10 17:42:56 -0700290
Eric Laurent87ffa392015-05-22 10:32:38 -0700291 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700292 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
293 updateCallRouting(newDevice);
294 }
295
Eric Laurentd60560a2015-04-10 11:31:20 -0700296 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
297 cleanUpForDevice(devDesc);
298 }
299
Eric Laurentb52c1522014-05-20 11:27:36 -0700300 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700301 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700302 } // end if is input device
Eric Laurente552edb2014-03-10 17:42:56 -0700303
304 ALOGW("setDeviceConnectionState() invalid device: %x", device);
305 return BAD_VALUE;
306}
307
Eric Laurente0720872014-03-11 09:30:41 -0700308audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +0100309 const char *device_address)
Eric Laurente552edb2014-03-10 17:42:56 -0700310{
Eric Laurent634b7142016-04-20 13:48:02 -0700311 sp<DeviceDescriptor> devDesc =
312 mHwModules.getDeviceDescriptor(device, device_address, "",
313 (strlen(device_address) != 0)/*matchAddress*/);
314
315 if (devDesc == 0) {
316 ALOGW("getDeviceConnectionState() undeclared device, type %08x, address: %s",
317 device, device_address);
318 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
319 }
François Gaffie53615e22015-03-19 09:24:12 +0100320
Eric Laurent3a4311c2014-03-17 12:00:47 -0700321 DeviceVector *deviceVector;
322
Eric Laurente552edb2014-03-10 17:42:56 -0700323 if (audio_is_output_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700324 deviceVector = &mAvailableOutputDevices;
Eric Laurente552edb2014-03-10 17:42:56 -0700325 } else if (audio_is_input_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700326 deviceVector = &mAvailableInputDevices;
327 } else {
328 ALOGW("getDeviceConnectionState() invalid device type %08x", device);
329 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700330 }
Eric Laurent634b7142016-04-20 13:48:02 -0700331
332 return (deviceVector->getDevice(device, String8(device_address)) != 0) ?
333 AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurenta1d525f2015-01-29 13:36:45 -0800334}
335
Eric Laurentdc462862016-07-19 12:29:53 -0700336uint32_t AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, uint32_t delayMs)
Eric Laurentc2730ba2014-07-20 15:47:07 -0700337{
338 bool createTxPatch = false;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700339 status_t status;
340 audio_patch_handle_t afPatchHandle;
341 DeviceVector deviceList;
Eric Laurentdc462862016-07-19 12:29:53 -0700342 uint32_t muteWaitMs = 0;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700343
Eric Laurent87ffa392015-05-22 10:32:38 -0700344 if(!hasPrimaryOutput()) {
Eric Laurentdc462862016-07-19 12:29:53 -0700345 return muteWaitMs;
Eric Laurent87ffa392015-05-22 10:32:38 -0700346 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -0800347 audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700348 ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice);
349
350 // release existing RX patch if any
351 if (mCallRxPatch != 0) {
352 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
353 mCallRxPatch.clear();
354 }
355 // release TX patch if any
356 if (mCallTxPatch != 0) {
357 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
358 mCallTxPatch.clear();
359 }
360
361 // If the RX device is on the primary HW module, then use legacy routing method for voice calls
362 // via setOutputDevice() on primary output.
363 // Otherwise, create two audio patches for TX and RX path.
364 if (availablePrimaryOutputDevices() & rxDevice) {
Eric Laurentdc462862016-07-19 12:29:53 -0700365 muteWaitMs = setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700366 // If the TX device is also on the primary HW module, setOutputDevice() will take care
367 // of it due to legacy implementation. If not, create a patch.
368 if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN)
369 == AUDIO_DEVICE_NONE) {
370 createTxPatch = true;
371 }
Eric Laurent8ae73122016-04-12 10:13:29 -0700372 } else { // create RX path audio patch
373 struct audio_patch patch;
374
375 patch.num_sources = 1;
376 patch.num_sinks = 1;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700377 deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice);
378 ALOG_ASSERT(!deviceList.isEmpty(),
379 "updateCallRouting() selected device not in output device list");
380 sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0);
381 deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX);
382 ALOG_ASSERT(!deviceList.isEmpty(),
383 "updateCallRouting() no telephony RX device");
384 sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0);
385
386 rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
387 rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
388
389 // request to reuse existing output stream if one is already opened to reach the RX device
390 SortedVector<audio_io_handle_t> outputs =
391 getOutputsForDevice(rxDevice, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700392 audio_io_handle_t output = selectOutput(outputs,
393 AUDIO_OUTPUT_FLAG_NONE,
394 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700395 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700396 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700397 ALOG_ASSERT(!outputDesc->isDuplicated(),
398 "updateCallRouting() RX device output is duplicated");
399 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700400 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700401 patch.num_sources = 2;
402 }
403
404 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentdc462862016-07-19 12:29:53 -0700405 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700406 ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch",
407 status);
408 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100409 mCallRxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700410 mCallRxPatch->mAfPatchHandle = afPatchHandle;
411 mCallRxPatch->mUid = mUidCached;
412 }
413 createTxPatch = true;
414 }
Eric Laurent8ae73122016-04-12 10:13:29 -0700415 if (createTxPatch) { // create TX path audio patch
Eric Laurentc2730ba2014-07-20 15:47:07 -0700416 struct audio_patch patch;
Eric Laurent8ae73122016-04-12 10:13:29 -0700417
Eric Laurentc2730ba2014-07-20 15:47:07 -0700418 patch.num_sources = 1;
419 patch.num_sinks = 1;
420 deviceList = mAvailableInputDevices.getDevicesFromType(txDevice);
421 ALOG_ASSERT(!deviceList.isEmpty(),
422 "updateCallRouting() selected device not in input device list");
423 sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0);
424 txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
425 deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
426 ALOG_ASSERT(!deviceList.isEmpty(),
427 "updateCallRouting() no telephony TX device");
428 sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0);
429 txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
430
431 SortedVector<audio_io_handle_t> outputs =
432 getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700433 audio_io_handle_t output = selectOutput(outputs,
434 AUDIO_OUTPUT_FLAG_NONE,
435 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700436 // request to reuse existing output stream if one is already opened to reach the TX
437 // path output device
438 if (output != AUDIO_IO_HANDLE_NONE) {
439 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
440 ALOG_ASSERT(!outputDesc->isDuplicated(),
441 "updateCallRouting() RX device output is duplicated");
442 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700443 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700444 patch.num_sources = 2;
445 }
446
Eric Laurentc0a889f2015-10-14 14:36:34 -0700447 // terminate active capture if on the same HW module as the call TX source device
448 // FIXME: would be better to refine to only inputs whose profile connects to the
449 // call TX device but this information is not in the audio patch and logic here must be
450 // symmetric to the one in startInput()
Eric Laurent232f26f2016-02-17 18:06:27 -0800451 audio_io_handle_t activeInput = mInputs.getActiveInput();
452 if (activeInput != 0) {
453 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
454 if (activeDesc->getModuleHandle() == txSourceDeviceDesc->getModuleHandle()) {
455 //FIXME: consider all active sessions
456 AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions();
457 audio_session_t activeSession = activeSessions.keyAt(0);
458 stopInput(activeInput, activeSession);
459 releaseInput(activeInput, activeSession);
Eric Laurentc0a889f2015-10-14 14:36:34 -0700460 }
461 }
462
Eric Laurentc2730ba2014-07-20 15:47:07 -0700463 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentdc462862016-07-19 12:29:53 -0700464 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700465 ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch",
466 status);
467 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100468 mCallTxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700469 mCallTxPatch->mAfPatchHandle = afPatchHandle;
470 mCallTxPatch->mUid = mUidCached;
471 }
472 }
Eric Laurentdc462862016-07-19 12:29:53 -0700473
474 return muteWaitMs;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700475}
476
Eric Laurente0720872014-03-11 09:30:41 -0700477void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700478{
479 ALOGV("setPhoneState() state %d", state);
François Gaffie2110e042015-03-24 08:41:51 +0100480 // store previous phone state for management of sonification strategy below
481 int oldState = mEngine->getPhoneState();
482
483 if (mEngine->setPhoneState(state) != NO_ERROR) {
484 ALOGW("setPhoneState() invalid or same state %d", state);
Eric Laurente552edb2014-03-10 17:42:56 -0700485 return;
486 }
François Gaffie2110e042015-03-24 08:41:51 +0100487 /// Opens: can these line be executed after the switch of volume curves???
Eric Laurente552edb2014-03-10 17:42:56 -0700488 // if leaving call state, handle special case of active streams
489 // pertaining to sonification strategy see handleIncallSonification()
Eric Laurent63dea1d2015-07-02 17:10:28 -0700490 if (isStateInCall(oldState)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700491 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent794fde22016-03-11 09:50:45 -0800492 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700493 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700494 }
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800495
Eric Laurent63dea1d2015-07-02 17:10:28 -0700496 // force reevaluating accessibility routing when call stops
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800497 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700498 }
499
François Gaffie2110e042015-03-24 08:41:51 +0100500 /**
501 * Switching to or from incall state or switching between telephony and VoIP lead to force
502 * routing command.
503 */
504 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
505 || (is_state_in_call(state) && (state != oldState)));
Eric Laurente552edb2014-03-10 17:42:56 -0700506
507 // check for device and output changes triggered by new phone state
Eric Laurente552edb2014-03-10 17:42:56 -0700508 checkA2dpSuspend();
509 checkOutputForAllStrategies();
510 updateDevicesAndOutputs();
511
Eric Laurente552edb2014-03-10 17:42:56 -0700512 int delayMs = 0;
513 if (isStateInCall(state)) {
514 nsecs_t sysTime = systemTime();
515 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700516 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700517 // mute media and sonification strategies and delay device switch by the largest
518 // latency of any output where either strategy is active.
519 // This avoid sending the ring tone or music tail into the earpiece or headset.
François Gaffiead3183e2015-03-18 16:55:35 +0100520 if ((isStrategyActive(desc, STRATEGY_MEDIA,
521 SONIFICATION_HEADSET_MUSIC_DELAY,
522 sysTime) ||
523 isStrategyActive(desc, STRATEGY_SONIFICATION,
524 SONIFICATION_HEADSET_MUSIC_DELAY,
525 sysTime)) &&
Eric Laurentc75307b2015-03-17 15:29:32 -0700526 (delayMs < (int)desc->latency()*2)) {
527 delayMs = desc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -0700528 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700529 setStrategyMute(STRATEGY_MEDIA, true, desc);
530 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700531 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Eric Laurentc75307b2015-03-17 15:29:32 -0700532 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
533 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700534 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
535 }
536 }
537
Eric Laurent87ffa392015-05-22 10:32:38 -0700538 if (hasPrimaryOutput()) {
539 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
540 // the device returned is not necessarily reachable via this output
541 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
542 // force routing command to audio hardware when ending call
543 // even if no device change is needed
544 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
545 rxDevice = mPrimaryOutput->device();
546 }
Eric Laurente552edb2014-03-10 17:42:56 -0700547
Eric Laurent87ffa392015-05-22 10:32:38 -0700548 if (state == AUDIO_MODE_IN_CALL) {
549 updateCallRouting(rxDevice, delayMs);
550 } else if (oldState == AUDIO_MODE_IN_CALL) {
551 if (mCallRxPatch != 0) {
552 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
553 mCallRxPatch.clear();
554 }
555 if (mCallTxPatch != 0) {
556 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
557 mCallTxPatch.clear();
558 }
559 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
560 } else {
561 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700562 }
Eric Laurentc2730ba2014-07-20 15:47:07 -0700563 }
Eric Laurente552edb2014-03-10 17:42:56 -0700564 // if entering in call state, handle special case of active streams
565 // pertaining to sonification strategy see handleIncallSonification()
566 if (isStateInCall(state)) {
567 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent794fde22016-03-11 09:50:45 -0800568 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700569 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700570 }
Eric Laurent63dea1d2015-07-02 17:10:28 -0700571
572 // force reevaluating accessibility routing when call starts
573 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700574 }
575
576 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700577 if (state == AUDIO_MODE_RINGTONE &&
578 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700579 mLimitRingtoneVolume = true;
580 } else {
581 mLimitRingtoneVolume = false;
582 }
583}
584
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700585audio_mode_t AudioPolicyManager::getPhoneState() {
586 return mEngine->getPhoneState();
587}
588
Eric Laurente0720872014-03-11 09:30:41 -0700589void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700590 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700591{
François Gaffie2110e042015-03-24 08:41:51 +0100592 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -0700593
François Gaffie2110e042015-03-24 08:41:51 +0100594 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
595 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
596 return;
Eric Laurente552edb2014-03-10 17:42:56 -0700597 }
François Gaffie2110e042015-03-24 08:41:51 +0100598 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
599 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
600 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
Eric Laurente552edb2014-03-10 17:42:56 -0700601
602 // check for device and output changes triggered by new force usage
603 checkA2dpSuspend();
604 checkOutputForAllStrategies();
605 updateDevicesAndOutputs();
Phil Burk09bc4612016-02-24 15:58:15 -0800606
Eric Laurentdc462862016-07-19 12:29:53 -0700607 //FIXME: workaround for truncated touch sounds
608 // to be removed when the problem is handled by system UI
609 uint32_t delayMs = 0;
610 uint32_t waitMs = 0;
611 if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) {
612 delayMs = TOUCH_SOUND_FIXED_DELAY_MS;
613 }
Eric Laurent87ffa392015-05-22 10:32:38 -0700614 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700615 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
Eric Laurentdc462862016-07-19 12:29:53 -0700616 waitMs = updateCallRouting(newDevice, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700617 }
Eric Laurente552edb2014-03-10 17:42:56 -0700618 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700619 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
620 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
621 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
Eric Laurentdc462862016-07-19 12:29:53 -0700622 waitMs = setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE),
623 delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700624 }
Eric Laurente552edb2014-03-10 17:42:56 -0700625 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
Eric Laurentdc462862016-07-19 12:29:53 -0700626 applyStreamVolumes(outputDesc, newDevice, waitMs, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700627 }
628 }
629
Eric Laurent232f26f2016-02-17 18:06:27 -0800630 audio_io_handle_t activeInput = mInputs.getActiveInput();
631 if (activeInput != 0) {
632 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
633 audio_devices_t newDevice = getNewInputDevice(activeInput);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700634 // Force new input selection if the new device can not be reached via current input
Eric Laurent232f26f2016-02-17 18:06:27 -0800635 if (activeDesc->mProfile->getSupportedDevices().types() & (newDevice & ~AUDIO_DEVICE_BIT_IN)) {
636 setInputDevice(activeInput, newDevice);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700637 } else {
Eric Laurent232f26f2016-02-17 18:06:27 -0800638 closeInput(activeInput);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700639 }
Eric Laurente552edb2014-03-10 17:42:56 -0700640 }
Eric Laurente552edb2014-03-10 17:42:56 -0700641}
642
Eric Laurente0720872014-03-11 09:30:41 -0700643void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700644{
645 ALOGV("setSystemProperty() property %s, value %s", property, value);
646}
647
648// Find a direct output profile compatible with the parameters passed, even if the input flags do
649// not explicitly request a direct output
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800650sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700651 audio_devices_t device,
652 uint32_t samplingRate,
653 audio_format_t format,
654 audio_channel_mask_t channelMask,
655 audio_output_flags_t flags)
656{
Eric Laurent861a6282015-05-18 15:40:16 -0700657 // only retain flags that will drive the direct output profile selection
658 // if explicitly requested
659 static const uint32_t kRelevantFlags =
660 (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
661 flags =
662 (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT);
663
664 sp<IOProfile> profile;
665
Eric Laurente552edb2014-03-10 17:42:56 -0700666 for (size_t i = 0; i < mHwModules.size(); i++) {
667 if (mHwModules[i]->mHandle == 0) {
668 continue;
669 }
670 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent861a6282015-05-18 15:40:16 -0700671 sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j];
672 if (!curProfile->isCompatibleProfile(device, String8(""),
Andy Hungf129b032015-04-07 13:45:50 -0700673 samplingRate, NULL /*updatedSamplingRate*/,
674 format, NULL /*updatedFormat*/,
675 channelMask, NULL /*updatedChannelMask*/,
Eric Laurent861a6282015-05-18 15:40:16 -0700676 flags)) {
677 continue;
678 }
679 // reject profiles not corresponding to a device currently available
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100680 if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) {
Eric Laurent861a6282015-05-18 15:40:16 -0700681 continue;
682 }
683 // if several profiles are compatible, give priority to one with offload capability
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100684 if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) {
Eric Laurent861a6282015-05-18 15:40:16 -0700685 continue;
686 }
687 profile = curProfile;
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100688 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent861a6282015-05-18 15:40:16 -0700689 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700690 }
Eric Laurente552edb2014-03-10 17:42:56 -0700691 }
692 }
Eric Laurent861a6282015-05-18 15:40:16 -0700693 return profile;
Eric Laurente552edb2014-03-10 17:42:56 -0700694}
695
Eric Laurente0720872014-03-11 09:30:41 -0700696audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +0100697 uint32_t samplingRate,
698 audio_format_t format,
699 audio_channel_mask_t channelMask,
700 audio_output_flags_t flags,
701 const audio_offload_info_t *offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -0700702{
Eric Laurent3b73df72014-03-11 09:06:29 -0700703 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700704 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
705 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
706 device, stream, samplingRate, format, channelMask, flags);
707
Eric Laurente83b55d2014-11-14 10:06:21 -0800708 return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE,
709 stream, samplingRate,format, channelMask,
710 flags, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700711}
712
Eric Laurente83b55d2014-11-14 10:06:21 -0800713status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
714 audio_io_handle_t *output,
715 audio_session_t session,
716 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700717 uid_t uid,
Eric Laurente83b55d2014-11-14 10:06:21 -0800718 uint32_t samplingRate,
719 audio_format_t format,
720 audio_channel_mask_t channelMask,
721 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700722 audio_port_handle_t selectedDeviceId,
Eric Laurente83b55d2014-11-14 10:06:21 -0800723 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700724{
Eric Laurente83b55d2014-11-14 10:06:21 -0800725 audio_attributes_t attributes;
726 if (attr != NULL) {
727 if (!isValidAttributes(attr)) {
728 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
729 attr->usage, attr->content_type, attr->flags,
730 attr->tags);
731 return BAD_VALUE;
732 }
733 attributes = *attr;
734 } else {
735 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
736 ALOGE("getOutputForAttr(): invalid stream type");
737 return BAD_VALUE;
738 }
739 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700740 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700741 sp<SwAudioOutputDescriptor> desc;
Jean-Michel Trivie8deced2016-02-11 12:50:39 -0800742 if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) {
François Gaffie036e1e92015-03-19 10:16:24 +0100743 ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr");
Phil Burkfdb3c072016-02-09 10:47:02 -0800744 if (!audio_has_proportional_frames(format)) {
François Gaffie036e1e92015-03-19 10:16:24 +0100745 return BAD_VALUE;
Eric Laurent275e8e92014-11-30 15:14:47 -0800746 }
François Gaffie036e1e92015-03-19 10:16:24 +0100747 *stream = streamTypefromAttributesInt(&attributes);
748 *output = desc->mIoHandle;
749 ALOGV("getOutputForAttr() returns output %d", *output);
750 return NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -0800751 }
752 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
753 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
754 return BAD_VALUE;
755 }
756
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700757 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x"
758 " session %d selectedDeviceId %d",
759 attributes.usage, attributes.content_type, attributes.tags, attributes.flags,
760 session, selectedDeviceId);
761
762 *stream = streamTypefromAttributesInt(&attributes);
763
764 // Explicit routing?
765 sp<DeviceDescriptor> deviceDesc;
766 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
767 if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) {
768 deviceDesc = mAvailableOutputDevices[i];
769 break;
770 }
771 }
772 mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700773
Eric Laurente83b55d2014-11-14 10:06:21 -0800774 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700775 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700776
Eric Laurente83b55d2014-11-14 10:06:21 -0800777 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700778 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
779 }
780
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700781 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700782 device, samplingRate, format, channelMask, flags);
783
Eric Laurente83b55d2014-11-14 10:06:21 -0800784 *output = getOutputForDevice(device, session, *stream,
785 samplingRate, format, channelMask,
786 flags, offloadInfo);
787 if (*output == AUDIO_IO_HANDLE_NONE) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700788 mOutputRoutes.removeRoute(session);
Eric Laurente83b55d2014-11-14 10:06:21 -0800789 return INVALID_OPERATION;
790 }
Paul McLeanaa981192015-03-21 09:55:15 -0700791
Eric Laurente83b55d2014-11-14 10:06:21 -0800792 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700793}
794
795audio_io_handle_t AudioPolicyManager::getOutputForDevice(
796 audio_devices_t device,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800797 audio_session_t session __unused,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700798 audio_stream_type_t stream,
799 uint32_t samplingRate,
800 audio_format_t format,
801 audio_channel_mask_t channelMask,
802 audio_output_flags_t flags,
803 const audio_offload_info_t *offloadInfo)
804{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700805 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700806 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700807
Eric Laurente552edb2014-03-10 17:42:56 -0700808#ifdef AUDIO_POLICY_TEST
809 if (mCurOutput != 0) {
810 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
811 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
812
813 if (mTestOutputs[mCurOutput] == 0) {
814 ALOGV("getOutput() opening test output");
Eric Laurentc75307b2015-03-17 15:29:32 -0700815 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
816 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700817 outputDesc->mDevice = mTestDevice;
Eric Laurente552edb2014-03-10 17:42:56 -0700818 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700819 outputDesc->mFlags =
820 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700821 outputDesc->mRefCount[stream] = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700822 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
823 config.sample_rate = mTestSamplingRate;
824 config.channel_mask = mTestChannels;
825 config.format = mTestFormat;
Phil Burk77cce802014-08-04 16:18:15 -0700826 if (offloadInfo != NULL) {
827 config.offload_info = *offloadInfo;
828 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700829 status = mpClientInterface->openOutput(0,
830 &mTestOutputs[mCurOutput],
831 &config,
832 &outputDesc->mDevice,
833 String8(""),
834 &outputDesc->mLatency,
835 outputDesc->mFlags);
836 if (status == NO_ERROR) {
837 outputDesc->mSamplingRate = config.sample_rate;
838 outputDesc->mFormat = config.format;
839 outputDesc->mChannelMask = config.channel_mask;
Eric Laurente552edb2014-03-10 17:42:56 -0700840 AudioParameter outputCmd = AudioParameter();
841 outputCmd.addInt(String8("set_id"),mCurOutput);
842 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
843 addOutput(mTestOutputs[mCurOutput], outputDesc);
844 }
845 }
846 return mTestOutputs[mCurOutput];
847 }
848#endif //AUDIO_POLICY_TEST
849
850 // open a direct output if required by specified parameters
851 //force direct flag if offload flag is set: offloading implies a direct output stream
852 // and all common behaviors are driven by checking only the direct flag
853 // this should normally be set appropriately in the policy configuration file
854 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700855 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700856 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700857 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
858 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
859 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800860 // only allow deep buffering for music stream type
861 if (stream != AUDIO_STREAM_MUSIC) {
862 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Ravi Kumar Alamanda439e4ed2015-04-03 12:13:21 -0700863 } else if (/* stream == AUDIO_STREAM_MUSIC && */
864 flags == AUDIO_OUTPUT_FLAG_NONE &&
865 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
866 // use DEEP_BUFFER as default output for music stream type
867 flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Eric Laurente83b55d2014-11-14 10:06:21 -0800868 }
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700869 if (stream == AUDIO_STREAM_TTS) {
870 flags = AUDIO_OUTPUT_FLAG_TTS;
871 }
Eric Laurente552edb2014-03-10 17:42:56 -0700872
Eric Laurentb732cf52014-09-24 19:08:21 -0700873 sp<IOProfile> profile;
874
875 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700876 // and not explicitly requested
877 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
Glenn Kasten05ddca52016-02-11 08:17:12 -0800878 audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700879 audio_channel_count_from_out_mask(channelMask) <= 2) {
880 goto non_direct_output;
881 }
882
Andy Hung2ddee192015-12-18 17:34:44 -0800883 // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled.
884 // This prevents creating an offloaded track and tearing it down immediately after start
885 // when audioflinger detects there is an active non offloadable effect.
Eric Laurente552edb2014-03-10 17:42:56 -0700886 // FIXME: We should check the audio session here but we do not have it in this context.
887 // This may prevent offloading in rare situations where effects are left active by apps
888 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700889
Eric Laurente552edb2014-03-10 17:42:56 -0700890 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
Andy Hung2ddee192015-12-18 17:34:44 -0800891 !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700892 profile = getProfileForDirectOutput(device,
893 samplingRate,
894 format,
895 channelMask,
896 (audio_output_flags_t)flags);
897 }
898
Eric Laurent1c333e22014-05-20 10:48:17 -0700899 if (profile != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700900 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700901
902 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700903 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700904 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
905 outputDesc = desc;
906 // reuse direct output if currently open and configured with same parameters
907 if ((samplingRate == outputDesc->mSamplingRate) &&
Eric Laurente6930022016-02-11 10:20:40 -0800908 audio_formats_match(format, outputDesc->mFormat) &&
Eric Laurente552edb2014-03-10 17:42:56 -0700909 (channelMask == outputDesc->mChannelMask)) {
910 outputDesc->mDirectOpenCount++;
911 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
912 return mOutputs.keyAt(i);
913 }
914 }
915 }
916 // close direct output if currently open and configured with different parameters
917 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700918 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700919 }
Eric Laurent861a6282015-05-18 15:40:16 -0700920
921 // if the selected profile is offloaded and no offload info was specified,
922 // create a default one
923 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100924 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
Eric Laurent861a6282015-05-18 15:40:16 -0700925 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
926 defaultOffloadInfo.sample_rate = samplingRate;
927 defaultOffloadInfo.channel_mask = channelMask;
928 defaultOffloadInfo.format = format;
929 defaultOffloadInfo.stream_type = stream;
930 defaultOffloadInfo.bit_rate = 0;
931 defaultOffloadInfo.duration_us = -1;
932 defaultOffloadInfo.has_video = true; // conservative
933 defaultOffloadInfo.is_streaming = true; // likely
934 offloadInfo = &defaultOffloadInfo;
935 }
936
Eric Laurentc75307b2015-03-17 15:29:32 -0700937 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700938 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -0700939 outputDesc->mLatency = 0;
Eric Laurent861a6282015-05-18 15:40:16 -0700940 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700941 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
942 config.sample_rate = samplingRate;
943 config.channel_mask = channelMask;
944 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -0700945 if (offloadInfo != NULL) {
946 config.offload_info = *offloadInfo;
947 }
Eric Laurent322b4d22015-04-03 15:57:54 -0700948 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -0700949 &output,
950 &config,
951 &outputDesc->mDevice,
952 String8(""),
953 &outputDesc->mLatency,
954 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -0700955
956 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -0700957 if (status != NO_ERROR ||
958 (samplingRate != 0 && samplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -0800959 (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) ||
Eric Laurentcf2c0212014-07-25 16:20:43 -0700960 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700961 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
962 "format %d %d, channelMask %04x %04x", output, samplingRate,
963 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
964 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700965 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700966 mpClientInterface->closeOutput(output);
967 }
Eric Laurenta82797f2015-01-30 11:49:43 -0800968 // fall back to mixer output if possible when the direct output could not be open
Glenn Kasten05ddca52016-02-11 08:17:12 -0800969 if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) {
Eric Laurenta82797f2015-01-30 11:49:43 -0800970 goto non_direct_output;
971 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700972 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -0700973 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700974 outputDesc->mSamplingRate = config.sample_rate;
975 outputDesc->mChannelMask = config.channel_mask;
976 outputDesc->mFormat = config.format;
977 outputDesc->mRefCount[stream] = 0;
978 outputDesc->mStopTime[stream] = 0;
979 outputDesc->mDirectOpenCount = 1;
980
Eric Laurente552edb2014-03-10 17:42:56 -0700981 audio_io_handle_t srcOutput = getOutputForEffect();
982 addOutput(output, outputDesc);
983 audio_io_handle_t dstOutput = getOutputForEffect();
984 if (dstOutput == output) {
985 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
986 }
987 mPreviousOutputs = mOutputs;
988 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -0700989 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700990 return output;
991 }
992
Eric Laurentb732cf52014-09-24 19:08:21 -0700993non_direct_output:
Eric Laurent14cbfca2016-03-17 09:42:16 -0700994
995 // A request for HW A/V sync cannot fallback to a mixed output because time
996 // stamps are embedded in audio data
997 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
998 return AUDIO_IO_HANDLE_NONE;
999 }
1000
Eric Laurente552edb2014-03-10 17:42:56 -07001001 // ignoring channel mask due to downmix capability in mixer
1002
1003 // open a non direct output
1004
1005 // for non direct outputs, only PCM is supported
1006 if (audio_is_linear_pcm(format)) {
1007 // get which output is suitable for the specified stream. The actual
1008 // routing change will happen when startOutput() will be called
1009 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1010
Eric Laurent8838a382014-09-08 16:44:28 -07001011 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1012 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1013 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -07001014 }
1015 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1016 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1017
Paul McLeanaa981192015-03-21 09:55:15 -07001018 ALOGV(" getOutputForDevice() returns output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -07001019
1020 return output;
1021}
1022
Eric Laurente0720872014-03-11 09:30:41 -07001023audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -07001024 audio_output_flags_t flags,
1025 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -07001026{
1027 // select one output among several that provide a path to a particular device or set of
1028 // devices (the list was previously build by getOutputsForDevice()).
1029 // The priority is as follows:
1030 // 1: the output with the highest number of requested policy flags
Eric Laurente6930022016-02-11 10:20:40 -08001031 // 2: the output with the bit depth the closest to the requested one
1032 // 3: the primary output
1033 // 4: the first output in the list
Eric Laurente552edb2014-03-10 17:42:56 -07001034
1035 if (outputs.size() == 0) {
1036 return 0;
1037 }
1038 if (outputs.size() == 1) {
1039 return outputs[0];
1040 }
1041
1042 int maxCommonFlags = 0;
Eric Laurente6930022016-02-11 10:20:40 -08001043 audio_io_handle_t outputForFlags = 0;
1044 audio_io_handle_t outputForPrimary = 0;
1045 audio_io_handle_t outputForFormat = 0;
1046 audio_format_t bestFormat = AUDIO_FORMAT_INVALID;
1047 audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07001048
1049 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001050 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -07001051 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -07001052 // if a valid format is specified, skip output if not compatible
1053 if (format != AUDIO_FORMAT_INVALID) {
1054 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente6930022016-02-11 10:20:40 -08001055 if (!audio_formats_match(format, outputDesc->mFormat)) {
Eric Laurent8838a382014-09-08 16:44:28 -07001056 continue;
1057 }
1058 } else if (!audio_is_linear_pcm(format)) {
1059 continue;
1060 }
Eric Laurente6930022016-02-11 10:20:40 -08001061 if (AudioPort::isBetterFormatMatch(
1062 outputDesc->mFormat, bestFormat, format)) {
1063 outputForFormat = outputs[i];
1064 bestFormat = outputDesc->mFormat;
1065 }
Eric Laurent8838a382014-09-08 16:44:28 -07001066 }
1067
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001068 int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags);
Eric Laurente6930022016-02-11 10:20:40 -08001069 if (commonFlags >= maxCommonFlags) {
1070 if (commonFlags == maxCommonFlags) {
1071 if (AudioPort::isBetterFormatMatch(
1072 outputDesc->mFormat, bestFormatForFlags, format)) {
1073 outputForFlags = outputs[i];
1074 bestFormatForFlags = outputDesc->mFormat;
1075 }
1076 } else {
1077 outputForFlags = outputs[i];
1078 maxCommonFlags = commonFlags;
1079 bestFormatForFlags = outputDesc->mFormat;
1080 }
Eric Laurente552edb2014-03-10 17:42:56 -07001081 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
1082 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001083 if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurente6930022016-02-11 10:20:40 -08001084 outputForPrimary = outputs[i];
Eric Laurente552edb2014-03-10 17:42:56 -07001085 }
1086 }
1087 }
1088
Eric Laurente6930022016-02-11 10:20:40 -08001089 if (outputForFlags != 0) {
1090 return outputForFlags;
Eric Laurente552edb2014-03-10 17:42:56 -07001091 }
Eric Laurente6930022016-02-11 10:20:40 -08001092 if (outputForFormat != 0) {
1093 return outputForFormat;
1094 }
1095 if (outputForPrimary != 0) {
1096 return outputForPrimary;
Eric Laurente552edb2014-03-10 17:42:56 -07001097 }
1098
1099 return outputs[0];
1100}
1101
Eric Laurente0720872014-03-11 09:30:41 -07001102status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001103 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001104 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001105{
Paul McLeanaa981192015-03-21 09:55:15 -07001106 ALOGV("startOutput() output %d, stream %d, session %d",
1107 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -07001108 ssize_t index = mOutputs.indexOfKey(output);
1109 if (index < 0) {
1110 ALOGW("startOutput() unknown output %d", output);
1111 return BAD_VALUE;
1112 }
1113
Eric Laurentc75307b2015-03-17 15:29:32 -07001114 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
1115
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001116 // Routing?
1117 mOutputRoutes.incRouteActivity(session);
1118
Eric Laurentc75307b2015-03-17 15:29:32 -07001119 audio_devices_t newDevice;
Eric Laurentc40d9692016-04-13 19:14:13 -07001120 AudioMix *policyMix = NULL;
1121 const char *address = NULL;
Eric Laurentc75307b2015-03-17 15:29:32 -07001122 if (outputDesc->mPolicyMix != NULL) {
Eric Laurentc40d9692016-04-13 19:14:13 -07001123 policyMix = outputDesc->mPolicyMix;
1124 address = policyMix->mDeviceAddress.string();
1125 if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
1126 newDevice = policyMix->mDeviceType;
1127 } else {
1128 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
1129 }
Eric Laurent493404d2015-04-21 15:07:36 -07001130 } else if (mOutputRoutes.hasRouteChanged(session)) {
1131 newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001132 checkStrategyRoute(getStrategy(stream), output);
Eric Laurentc75307b2015-03-17 15:29:32 -07001133 } else {
1134 newDevice = AUDIO_DEVICE_NONE;
1135 }
1136
1137 uint32_t delayMs = 0;
1138
Eric Laurentc40d9692016-04-13 19:14:13 -07001139 status_t status = startSource(outputDesc, stream, newDevice, address, &delayMs);
Eric Laurentc75307b2015-03-17 15:29:32 -07001140
1141 if (status != NO_ERROR) {
1142 mOutputRoutes.decRouteActivity(session);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001143 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -07001144 }
1145 // Automatically enable the remote submix input when output is started on a re routing mix
1146 // of type MIX_TYPE_RECORDERS
Eric Laurentc40d9692016-04-13 19:14:13 -07001147 if (audio_is_remote_submix_device(newDevice) && policyMix != NULL &&
1148 policyMix->mMixType == MIX_TYPE_RECORDERS) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001149 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1150 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Eric Laurentc40d9692016-04-13 19:14:13 -07001151 address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001152 "remote-submix");
1153 }
1154
1155 if (delayMs != 0) {
1156 usleep(delayMs * 1000);
1157 }
1158
1159 return status;
1160}
1161
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001162status_t AudioPolicyManager::startSource(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurentc75307b2015-03-17 15:29:32 -07001163 audio_stream_type_t stream,
1164 audio_devices_t device,
Eric Laurentc40d9692016-04-13 19:14:13 -07001165 const char *address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001166 uint32_t *delayMs)
1167{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001168 // cannot start playback of STREAM_TTS if any other output is being used
1169 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001170
1171 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001172 if (stream == AUDIO_STREAM_TTS) {
1173 ALOGV("\t found BEACON stream");
Eric Laurent9459fb02015-08-12 18:36:32 -07001174 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001175 return INVALID_OPERATION;
1176 } else {
1177 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1178 }
1179 } else {
1180 // some playback other than beacon starts
1181 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1182 }
1183
Eric Laurent77305a62016-07-25 16:39:22 -07001184 // force device change if the output is inactive and no audio patch is already present.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001185 // check active before incrementing usage count
Eric Laurent77305a62016-07-25 16:39:22 -07001186 bool force = !outputDesc->isActive() &&
1187 (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001188
Eric Laurente552edb2014-03-10 17:42:56 -07001189 // increment usage count for this stream on the requested output:
1190 // NOTE that the usage count is the same for duplicated output and hardware output which is
1191 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1192 outputDesc->changeRefCount(stream, 1);
1193
Eric Laurent493404d2015-04-21 15:07:36 -07001194 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001195 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001196 if (device == AUDIO_DEVICE_NONE) {
1197 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001198 }
Eric Laurente552edb2014-03-10 17:42:56 -07001199 routing_strategy strategy = getStrategy(stream);
1200 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001201 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1202 (beaconMuteLatency > 0);
1203 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001204 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001205 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001206 if (desc != outputDesc) {
Eric Laurent77305a62016-07-25 16:39:22 -07001207 // force a device change if any other output is:
1208 // - managed by the same hw module
1209 // - has a current device selection that differs from selected device.
1210 // - supports currently selected device
1211 // - has an active audio patch
Eric Laurente552edb2014-03-10 17:42:56 -07001212 // In this case, the audio HAL must receive the new device selection so that it can
1213 // change the device currently selected by the other active output.
1214 if (outputDesc->sharesHwModuleWith(desc) &&
Eric Laurent77305a62016-07-25 16:39:22 -07001215 desc->device() != device &&
1216 desc->supportedDevices() & device &&
1217 desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07001218 force = true;
1219 }
1220 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001221 // a notification so that audio focus effect can propagate, or that a mute/unmute
1222 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001223 uint32_t latency = desc->latency();
1224 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1225 waitMs = latency;
1226 }
1227 }
1228 }
Eric Laurentdc462862016-07-19 12:29:53 -07001229 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force, 0, NULL, address);
Eric Laurente552edb2014-03-10 17:42:56 -07001230
1231 // handle special case for sonification while in call
1232 if (isInCall()) {
1233 handleIncallSonification(stream, true, false);
1234 }
1235
1236 // apply volume rules for current stream and device if necessary
1237 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01001238 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07001239 outputDesc,
1240 device);
Eric Laurente552edb2014-03-10 17:42:56 -07001241
1242 // update the outputs if starting an output with a stream that can affect notification
1243 // routing
1244 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001245
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001246 // force reevaluating accessibility routing when ringtone or alarm starts
1247 if (strategy == STRATEGY_SONIFICATION) {
1248 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1249 }
Eric Laurentdc462862016-07-19 12:29:53 -07001250
1251 if (waitMs > muteWaitMs) {
1252 *delayMs = waitMs - muteWaitMs;
1253 }
Eric Laurente552edb2014-03-10 17:42:56 -07001254 }
Eric Laurentdc462862016-07-19 12:29:53 -07001255
Eric Laurente552edb2014-03-10 17:42:56 -07001256 return NO_ERROR;
1257}
1258
1259
Eric Laurente0720872014-03-11 09:30:41 -07001260status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001261 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001262 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001263{
1264 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1265 ssize_t index = mOutputs.indexOfKey(output);
1266 if (index < 0) {
1267 ALOGW("stopOutput() unknown output %d", output);
1268 return BAD_VALUE;
1269 }
1270
Eric Laurentc75307b2015-03-17 15:29:32 -07001271 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001272
Eric Laurentc75307b2015-03-17 15:29:32 -07001273 if (outputDesc->mRefCount[stream] == 1) {
1274 // Automatically disable the remote submix input when output is stopped on a
1275 // re routing mix of type MIX_TYPE_RECORDERS
1276 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1277 outputDesc->mPolicyMix != NULL &&
1278 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1279 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1280 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001281 outputDesc->mPolicyMix->mDeviceAddress,
Eric Laurentc75307b2015-03-17 15:29:32 -07001282 "remote-submix");
1283 }
1284 }
1285
1286 // Routing?
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001287 bool forceDeviceUpdate = false;
Eric Laurentc75307b2015-03-17 15:29:32 -07001288 if (outputDesc->mRefCount[stream] > 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001289 int activityCount = mOutputRoutes.decRouteActivity(session);
1290 forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0));
1291
1292 if (forceDeviceUpdate) {
1293 checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE);
1294 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001295 }
1296
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001297 return stopSource(outputDesc, stream, forceDeviceUpdate);
Eric Laurentc75307b2015-03-17 15:29:32 -07001298}
1299
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001300status_t AudioPolicyManager::stopSource(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001301 audio_stream_type_t stream,
1302 bool forceDeviceUpdate)
Eric Laurentc75307b2015-03-17 15:29:32 -07001303{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001304 // always handle stream stop, check which stream type is stopping
1305 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1306
Eric Laurente552edb2014-03-10 17:42:56 -07001307 // handle special case for sonification while in call
1308 if (isInCall()) {
1309 handleIncallSonification(stream, false, false);
1310 }
1311
1312 if (outputDesc->mRefCount[stream] > 0) {
1313 // decrement usage count of this stream on the output
1314 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001315
Eric Laurente552edb2014-03-10 17:42:56 -07001316 // store time at which the stream was stopped - see isStreamActive()
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001317 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
Eric Laurente552edb2014-03-10 17:42:56 -07001318 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001319 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001320 // delay the device switch by twice the latency because stopOutput() is executed when
1321 // the track stop() command is received and at that time the audio track buffer can
1322 // still contain data that needs to be drained. The latency only covers the audio HAL
1323 // and kernel buffers. Also the latency does not always include additional delay in the
1324 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001325 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001326
1327 // force restoring the device selection on other active outputs if it differs from the
1328 // one being selected for this output
1329 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001330 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001331 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001332 desc->isActive() &&
1333 outputDesc->sharesHwModuleWith(desc) &&
1334 (newDevice != desc->device())) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001335 setOutputDevice(desc,
1336 getNewOutputDevice(desc, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -07001337 true,
Eric Laurentc75307b2015-03-17 15:29:32 -07001338 outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001339 }
1340 }
1341 // update the outputs if stopping one with a stream that can affect notification routing
1342 handleNotificationRoutingForStream(stream);
1343 }
1344 return NO_ERROR;
1345 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001346 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001347 return INVALID_OPERATION;
1348 }
1349}
1350
Eric Laurente83b55d2014-11-14 10:06:21 -08001351void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001352 audio_stream_type_t stream __unused,
1353 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001354{
1355 ALOGV("releaseOutput() %d", output);
1356 ssize_t index = mOutputs.indexOfKey(output);
1357 if (index < 0) {
1358 ALOGW("releaseOutput() releasing unknown output %d", output);
1359 return;
1360 }
1361
1362#ifdef AUDIO_POLICY_TEST
1363 int testIndex = testOutputIndex(output);
1364 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001365 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001366 if (outputDesc->isActive()) {
1367 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001368 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001369 mTestOutputs[testIndex] = 0;
1370 }
1371 return;
1372 }
1373#endif //AUDIO_POLICY_TEST
1374
Paul McLeanaa981192015-03-21 09:55:15 -07001375 // Routing
1376 mOutputRoutes.removeRoute(session);
1377
Eric Laurentc75307b2015-03-17 15:29:32 -07001378 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001379 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001380 if (desc->mDirectOpenCount <= 0) {
1381 ALOGW("releaseOutput() invalid open count %d for output %d",
1382 desc->mDirectOpenCount, output);
1383 return;
1384 }
1385 if (--desc->mDirectOpenCount == 0) {
1386 closeOutput(output);
1387 // If effects where present on the output, audioflinger moved them to the primary
1388 // output by default: move them back to the appropriate output.
1389 audio_io_handle_t dstOutput = getOutputForEffect();
Eric Laurent87ffa392015-05-22 10:32:38 -07001390 if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001391 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
1392 mPrimaryOutput->mIoHandle, dstOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07001393 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001394 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001395 }
1396 }
1397}
1398
1399
Eric Laurentcaf7f482014-11-25 17:50:47 -08001400status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1401 audio_io_handle_t *input,
1402 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001403 uid_t uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001404 uint32_t samplingRate,
1405 audio_format_t format,
1406 audio_channel_mask_t channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001407 audio_input_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -06001408 audio_port_handle_t selectedDeviceId,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001409 input_type_t *inputType)
Eric Laurente552edb2014-03-10 17:42:56 -07001410{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001411 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1412 "session %d, flags %#x",
1413 attr->source, samplingRate, format, channelMask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001414
Eric Laurentcaf7f482014-11-25 17:50:47 -08001415 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001416 *inputType = API_INPUT_INVALID;
Eric Laurent275e8e92014-11-30 15:14:47 -08001417 audio_devices_t device;
1418 // handle legacy remote submix case where the address was not always specified
1419 String8 address = String8("");
Eric Laurentc447ded2015-01-06 08:47:05 -08001420 audio_source_t inputSource = attr->source;
1421 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001422 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001423
Eric Laurentc447ded2015-01-06 08:47:05 -08001424 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1425 inputSource = AUDIO_SOURCE_MIC;
1426 }
1427 halInputSource = inputSource;
1428
Paul McLean466dc8e2015-04-17 13:15:36 -06001429 // Explicit routing?
1430 sp<DeviceDescriptor> deviceDesc;
1431 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1432 if (mAvailableInputDevices[i]->getId() == selectedDeviceId) {
1433 deviceDesc = mAvailableInputDevices[i];
1434 break;
1435 }
1436 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001437 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001438
Eric Laurentc447ded2015-01-06 08:47:05 -08001439 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001440 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001441 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
François Gaffie036e1e92015-03-19 10:16:24 +01001442 if (ret != NO_ERROR) {
1443 return ret;
1444 }
1445 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001446 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1447 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001448 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001449 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001450 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001451 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001452 return BAD_VALUE;
1453 }
Eric Laurentc722f302014-12-10 11:21:49 -08001454 if (policyMix != NULL) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001455 address = policyMix->mDeviceAddress;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001456 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1457 // there is an external policy, but this input is attached to a mix of recorders,
1458 // meaning it receives audio injected into the framework, so the recorder doesn't
1459 // know about it and is therefore considered "legacy"
1460 *inputType = API_INPUT_LEGACY;
1461 } else {
1462 // recording a mix of players defined by an external policy, we're rerouting for
1463 // an external policy
1464 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1465 }
Eric Laurentc722f302014-12-10 11:21:49 -08001466 } else if (audio_is_remote_submix_device(device)) {
1467 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001468 *inputType = API_INPUT_MIX_CAPTURE;
Eric Laurent82db2692015-08-07 13:59:42 -07001469 } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
1470 *inputType = API_INPUT_TELEPHONY_RX;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001471 } else {
1472 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001473 }
Ravi Kumar Alamandab367f5b2015-08-25 08:21:37 -07001474
Eric Laurent599c7582015-12-07 18:05:55 -08001475 }
1476
1477 *input = getInputForDevice(device, address, session, uid, inputSource,
1478 samplingRate, format, channelMask, flags,
1479 policyMix);
1480 if (*input == AUDIO_IO_HANDLE_NONE) {
1481 mInputRoutes.removeRoute(session);
1482 return INVALID_OPERATION;
1483 }
1484 ALOGV("getInputForAttr() returns input type = %d", *inputType);
1485 return NO_ERROR;
1486}
1487
1488
1489audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device,
1490 String8 address,
1491 audio_session_t session,
1492 uid_t uid,
1493 audio_source_t inputSource,
1494 uint32_t samplingRate,
1495 audio_format_t format,
1496 audio_channel_mask_t channelMask,
1497 audio_input_flags_t flags,
1498 AudioMix *policyMix)
1499{
1500 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
1501 audio_source_t halInputSource = inputSource;
1502 bool isSoundTrigger = false;
1503
1504 if (inputSource == AUDIO_SOURCE_HOTWORD) {
1505 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1506 if (index >= 0) {
1507 input = mSoundTriggerSessions.valueFor(session);
1508 isSoundTrigger = true;
1509 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1510 ALOGV("SoundTrigger capture on session %d input %d", session, input);
1511 } else {
1512 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001513 }
1514 }
1515
Andy Hungf129b032015-04-07 13:45:50 -07001516 // find a compatible input profile (not necessarily identical in parameters)
1517 sp<IOProfile> profile;
1518 // samplingRate and flags may be updated by getInputProfile
Glenn Kasten05ddca52016-02-11 08:17:12 -08001519 uint32_t profileSamplingRate = (samplingRate == 0) ? SAMPLE_RATE_HZ_DEFAULT : samplingRate;
Andy Hungf129b032015-04-07 13:45:50 -07001520 audio_format_t profileFormat = format;
1521 audio_channel_mask_t profileChannelMask = channelMask;
1522 audio_input_flags_t profileFlags = flags;
1523 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001524 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001525 profileSamplingRate, profileFormat, profileChannelMask,
1526 profileFlags);
1527 if (profile != 0) {
1528 break; // success
Eric Laurent05067782016-06-01 18:27:28 -07001529 } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) {
1530 profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry
Andy Hungf129b032015-04-07 13:45:50 -07001531 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1532 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1533 } else { // fail
Eric Laurent599c7582015-12-07 18:05:55 -08001534 ALOGW("getInputForDevice() could not find profile for device 0x%X,"
1535 "samplingRate %u, format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001536 device, samplingRate, format, channelMask, flags);
Eric Laurent599c7582015-12-07 18:05:55 -08001537 return input;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001538 }
Eric Laurente552edb2014-03-10 17:42:56 -07001539 }
Glenn Kasten05ddca52016-02-11 08:17:12 -08001540 // Pick input sampling rate if not specified by client
1541 if (samplingRate == 0) {
1542 samplingRate = profileSamplingRate;
1543 }
Eric Laurente552edb2014-03-10 17:42:56 -07001544
Eric Laurent322b4d22015-04-03 15:57:54 -07001545 if (profile->getModuleHandle() == 0) {
1546 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurent599c7582015-12-07 18:05:55 -08001547 return input;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001548 }
1549
Eric Laurent599c7582015-12-07 18:05:55 -08001550 sp<AudioSession> audioSession = new AudioSession(session,
1551 inputSource,
1552 format,
1553 samplingRate,
1554 channelMask,
1555 flags,
1556 uid,
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001557 isSoundTrigger,
1558 policyMix, mpClientInterface);
Eric Laurent599c7582015-12-07 18:05:55 -08001559
Eric Laurent232f26f2016-02-17 18:06:27 -08001560// TODO enable input reuse
1561#if 0
Eric Laurent599c7582015-12-07 18:05:55 -08001562 // reuse an open input if possible
1563 for (size_t i = 0; i < mInputs.size(); i++) {
1564 sp<AudioInputDescriptor> desc = mInputs.valueAt(i);
Eric Laurent232f26f2016-02-17 18:06:27 -08001565 // reuse input if it shares the same profile and same sound trigger attribute
1566 if (profile == desc->mProfile &&
1567 isSoundTrigger == desc->isSoundTrigger()) {
Eric Laurent599c7582015-12-07 18:05:55 -08001568
1569 sp<AudioSession> as = desc->getAudioSession(session);
1570 if (as != 0) {
1571 // do not allow unmatching properties on same session
1572 if (as->matches(audioSession)) {
1573 as->changeOpenCount(1);
1574 } else {
1575 ALOGW("getInputForDevice() record with different attributes"
1576 " exists for session %d", session);
Eric Laurent232f26f2016-02-17 18:06:27 -08001577 return input;
Eric Laurent599c7582015-12-07 18:05:55 -08001578 }
1579 } else {
1580 desc->addAudioSession(session, audioSession);
1581 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001582 ALOGV("getInputForDevice() reusing input %d", mInputs.keyAt(i));
1583 return mInputs.keyAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08001584 }
1585 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001586#endif
Eric Laurent599c7582015-12-07 18:05:55 -08001587
Eric Laurentcf2c0212014-07-25 16:20:43 -07001588 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001589 config.sample_rate = profileSamplingRate;
1590 config.channel_mask = profileChannelMask;
1591 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001592
Eric Laurent322b4d22015-04-03 15:57:54 -07001593 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurent599c7582015-12-07 18:05:55 -08001594 &input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001595 &config,
1596 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001597 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001598 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001599 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001600
1601 // only accept input with the exact requested set of parameters
Eric Laurent599c7582015-12-07 18:05:55 -08001602 if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001603 (profileSamplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -08001604 !audio_formats_match(profileFormat, config.format) ||
Andy Hungf129b032015-04-07 13:45:50 -07001605 (profileChannelMask != config.channel_mask)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001606 ALOGW("getInputForAttr() failed opening input: samplingRate %d"
1607 ", format %d, channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001608 samplingRate, format, channelMask);
Eric Laurent599c7582015-12-07 18:05:55 -08001609 if (input != AUDIO_IO_HANDLE_NONE) {
1610 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001611 }
Eric Laurent599c7582015-12-07 18:05:55 -08001612 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001613 }
1614
Eric Laurent1f2f2232014-06-02 12:01:23 -07001615 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Andy Hungf129b032015-04-07 13:45:50 -07001616 inputDesc->mSamplingRate = profileSamplingRate;
1617 inputDesc->mFormat = profileFormat;
1618 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001619 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001620 inputDesc->mPolicyMix = policyMix;
Eric Laurent599c7582015-12-07 18:05:55 -08001621 inputDesc->addAudioSession(session, audioSession);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001622
Eric Laurent599c7582015-12-07 18:05:55 -08001623 addInput(input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001624 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001625
Eric Laurent599c7582015-12-07 18:05:55 -08001626 return input;
Eric Laurente552edb2014-03-10 17:42:56 -07001627}
1628
Eric Laurent4dc68062014-07-28 17:26:49 -07001629status_t AudioPolicyManager::startInput(audio_io_handle_t input,
Eric Laurent232f26f2016-02-17 18:06:27 -08001630 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001631{
1632 ALOGV("startInput() input %d", input);
1633 ssize_t index = mInputs.indexOfKey(input);
1634 if (index < 0) {
1635 ALOGW("startInput() unknown input %d", input);
1636 return BAD_VALUE;
1637 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001638 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001639
Eric Laurent599c7582015-12-07 18:05:55 -08001640 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1641 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07001642 ALOGW("startInput() unknown session %d on input %d", session, input);
1643 return BAD_VALUE;
1644 }
1645
Eric Laurent232f26f2016-02-17 18:06:27 -08001646 // virtual input devices are compatible with other input devices
1647 if (!is_virtual_input_device(inputDesc->mDevice)) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001648
Eric Laurent232f26f2016-02-17 18:06:27 -08001649 // for a non-virtual input device, check if there is another (non-virtual) active input
1650 audio_io_handle_t activeInput = mInputs.getActiveInput();
1651 if (activeInput != 0 && activeInput != input) {
Eric Laurente552edb2014-03-10 17:42:56 -07001652
Eric Laurent232f26f2016-02-17 18:06:27 -08001653 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1654 // otherwise the active input continues and the new input cannot be started.
1655 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
1656 if ((activeDesc->inputSource() == AUDIO_SOURCE_HOTWORD) &&
1657 !activeDesc->hasPreemptedSession(session)) {
1658 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
1659 //FIXME: consider all active sessions
1660 AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions();
1661 audio_session_t activeSession = activeSessions.keyAt(0);
1662 SortedVector<audio_session_t> sessions =
1663 activeDesc->getPreemptedSessions();
1664 sessions.add(activeSession);
1665 inputDesc->setPreemptedSessions(sessions);
1666 stopInput(activeInput, activeSession);
1667 releaseInput(activeInput, activeSession);
1668 } else {
1669 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
1670 return INVALID_OPERATION;
1671 }
1672 }
1673
1674 // Do not allow capture if an active voice call is using a software patch and
1675 // the call TX source device is on the same HW module.
1676 // FIXME: would be better to refine to only inputs whose profile connects to the
1677 // call TX device but this information is not in the audio patch
1678 if (mCallTxPatch != 0 &&
1679 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1680 return INVALID_OPERATION;
1681 }
1682 }
Eric Laurent313d1e72016-01-29 09:56:57 -08001683
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001684 // Routing?
1685 mInputRoutes.incRouteActivity(session);
1686
Eric Laurent232f26f2016-02-17 18:06:27 -08001687 if (!inputDesc->isActive() || mInputRoutes.hasRouteChanged(session)) {
1688 // if input maps to a dynamic policy with an activity listener, notify of state change
1689 if ((inputDesc->mPolicyMix != NULL)
1690 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001691 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
Eric Laurent232f26f2016-02-17 18:06:27 -08001692 MIX_STATE_MIXING);
1693 }
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001694
Eric Laurent232f26f2016-02-17 18:06:27 -08001695 if (mInputs.activeInputsCount() == 0) {
1696 SoundTrigger::setCaptureState(true);
1697 }
1698 setInputDevice(input, getNewInputDevice(input), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001699
Eric Laurent232f26f2016-02-17 18:06:27 -08001700 // automatically enable the remote submix output when input is started if not
1701 // used by a policy mix of type MIX_TYPE_RECORDERS
1702 // For remote submix (a virtual device), we open only one input per capture request.
1703 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1704 String8 address = String8("");
1705 if (inputDesc->mPolicyMix == NULL) {
1706 address = String8("0");
1707 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001708 address = inputDesc->mPolicyMix->mDeviceAddress;
Eric Laurentc722f302014-12-10 11:21:49 -08001709 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001710 if (address != "") {
1711 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1712 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1713 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001714 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001715 }
Eric Laurente552edb2014-03-10 17:42:56 -07001716 }
1717
Eric Laurent599c7582015-12-07 18:05:55 -08001718 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Eric Laurente552edb2014-03-10 17:42:56 -07001719
Eric Laurent232f26f2016-02-17 18:06:27 -08001720 audioSession->changeActiveCount(1);
Eric Laurente552edb2014-03-10 17:42:56 -07001721 return NO_ERROR;
1722}
1723
Eric Laurent4dc68062014-07-28 17:26:49 -07001724status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1725 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001726{
1727 ALOGV("stopInput() input %d", input);
1728 ssize_t index = mInputs.indexOfKey(input);
1729 if (index < 0) {
1730 ALOGW("stopInput() unknown input %d", input);
1731 return BAD_VALUE;
1732 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001733 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001734
Eric Laurent599c7582015-12-07 18:05:55 -08001735 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001736 if (index < 0) {
1737 ALOGW("stopInput() unknown session %d on input %d", session, input);
1738 return BAD_VALUE;
1739 }
1740
Eric Laurent599c7582015-12-07 18:05:55 -08001741 if (audioSession->activeCount() == 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07001742 ALOGW("stopInput() input %d already stopped", input);
1743 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001744 }
1745
Eric Laurent599c7582015-12-07 18:05:55 -08001746 audioSession->changeActiveCount(-1);
Paul McLean466dc8e2015-04-17 13:15:36 -06001747
1748 // Routing?
1749 mInputRoutes.decRouteActivity(session);
1750
Eric Laurent232f26f2016-02-17 18:06:27 -08001751 if (!inputDesc->isActive()) {
1752 // if input maps to a dynamic policy with an activity listener, notify of state change
1753 if ((inputDesc->mPolicyMix != NULL)
1754 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001755 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
Eric Laurent232f26f2016-02-17 18:06:27 -08001756 MIX_STATE_IDLE);
Eric Laurent84332aa2016-01-28 22:19:18 +00001757 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001758
1759 // automatically disable the remote submix output when input is stopped if not
1760 // used by a policy mix of type MIX_TYPE_RECORDERS
1761 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1762 String8 address = String8("");
1763 if (inputDesc->mPolicyMix == NULL) {
1764 address = String8("0");
1765 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001766 address = inputDesc->mPolicyMix->mDeviceAddress;
Eric Laurent232f26f2016-02-17 18:06:27 -08001767 }
1768 if (address != "") {
1769 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1770 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1771 address, "remote-submix");
1772 }
1773 }
1774
1775 resetInputDevice(input);
1776
1777 if (mInputs.activeInputsCount() == 0) {
1778 SoundTrigger::setCaptureState(false);
1779 }
1780 inputDesc->clearPreemptedSessions();
Eric Laurente552edb2014-03-10 17:42:56 -07001781 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001782 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001783}
1784
Eric Laurent4dc68062014-07-28 17:26:49 -07001785void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1786 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001787{
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001788
Eric Laurente552edb2014-03-10 17:42:56 -07001789 ALOGV("releaseInput() %d", input);
1790 ssize_t index = mInputs.indexOfKey(input);
1791 if (index < 0) {
1792 ALOGW("releaseInput() releasing unknown input %d", input);
1793 return;
1794 }
Paul McLean466dc8e2015-04-17 13:15:36 -06001795
1796 // Routing
1797 mInputRoutes.removeRoute(session);
1798
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001799 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1800 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001801
Eric Laurent599c7582015-12-07 18:05:55 -08001802 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001803 if (index < 0) {
1804 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1805 return;
1806 }
Eric Laurent599c7582015-12-07 18:05:55 -08001807
1808 if (audioSession->openCount() == 0) {
1809 ALOGW("releaseInput() invalid open count %d on session %d",
1810 audioSession->openCount(), session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001811 return;
1812 }
Eric Laurent599c7582015-12-07 18:05:55 -08001813
1814 if (audioSession->changeOpenCount(-1) == 0) {
1815 inputDesc->removeAudioSession(session);
1816 }
1817
Jean-Michel Trivi65bfe912015-12-04 15:56:47 -08001818 if (inputDesc->getOpenRefCount() > 0) {
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001819 ALOGV("releaseInput() exit > 0");
1820 return;
1821 }
1822
Eric Laurent05b90f82014-08-27 15:32:29 -07001823 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001824 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001825 ALOGV("releaseInput() exit");
1826}
1827
Eric Laurentd4692962014-05-05 18:13:44 -07001828void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001829 bool patchRemoved = false;
1830
Eric Laurentd4692962014-05-05 18:13:44 -07001831 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001832 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001833 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07001834 if (patch_index >= 0) {
1835 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07001836 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07001837 mAudioPatches.removeItemsAt(patch_index);
1838 patchRemoved = true;
1839 }
Eric Laurentd4692962014-05-05 18:13:44 -07001840 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1841 }
1842 mInputs.clear();
Chris Thornton52785f32016-02-09 17:28:49 -08001843 SoundTrigger::setCaptureState(false);
Eric Laurent6a94d692014-05-20 11:18:06 -07001844 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001845
1846 if (patchRemoved) {
1847 mpClientInterface->onAudioPatchListUpdate();
1848 }
Eric Laurentd4692962014-05-05 18:13:44 -07001849}
1850
Eric Laurente0720872014-03-11 09:30:41 -07001851void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001852 int indexMin,
1853 int indexMax)
1854{
1855 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffied1ab2bd2015-12-02 18:20:06 +01001856 mVolumeCurves->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent28d09f02016-03-08 10:43:05 -08001857
1858 // initialize other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08001859 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1860 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001861 continue;
1862 }
1863 mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001864 }
Eric Laurente552edb2014-03-10 17:42:56 -07001865}
1866
Eric Laurente0720872014-03-11 09:30:41 -07001867status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01001868 int index,
1869 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07001870{
1871
François Gaffied1ab2bd2015-12-02 18:20:06 +01001872 if ((index < mVolumeCurves->getVolumeIndexMin(stream)) ||
1873 (index > mVolumeCurves->getVolumeIndexMax(stream))) {
Eric Laurente552edb2014-03-10 17:42:56 -07001874 return BAD_VALUE;
1875 }
1876 if (!audio_is_output_device(device)) {
1877 return BAD_VALUE;
1878 }
1879
1880 // Force max volume if stream cannot be muted
François Gaffied1ab2bd2015-12-02 18:20:06 +01001881 if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001882
Eric Laurent1fd372e2016-04-06 14:23:53 -07001883 ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d",
Eric Laurente552edb2014-03-10 17:42:56 -07001884 stream, device, index);
1885
Eric Laurent28d09f02016-03-08 10:43:05 -08001886 // update other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08001887 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1888 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001889 continue;
1890 }
1891 mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index);
1892 }
Eric Laurente552edb2014-03-10 17:42:56 -07001893
Eric Laurent1fd372e2016-04-06 14:23:53 -07001894 // update volume on all outputs and streams matching the following:
1895 // - The requested stream (or a stream matching for volume control) is active on the output
1896 // - The device (or devices) selected by the strategy corresponding to this stream includes
1897 // the requested device
1898 // - For non default requested device, currently selected device on the output is either the
1899 // requested device or one of the devices selected by the strategy
Eric Laurent5a2b6292016-04-14 18:05:57 -07001900 // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if
1901 // no specific device volume value exists for currently selected device.
Eric Laurente552edb2014-03-10 17:42:56 -07001902 status_t status = NO_ERROR;
1903 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001904 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1905 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent794fde22016-03-11 09:50:45 -08001906 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1907 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001908 continue;
Eric Laurente552edb2014-03-10 17:42:56 -07001909 }
Eric Laurentd3926fe2016-04-15 18:10:07 -07001910 if (!(desc->isStreamActive((audio_stream_type_t)curStream) ||
1911 (isInCall() && (curStream == AUDIO_STREAM_VOICE_CALL)))) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07001912 continue;
1913 }
Eric Laurent794fde22016-03-11 09:50:45 -08001914 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08001915 audio_devices_t curStreamDevice = getDeviceForStrategy(curStrategy, true /*fromCache*/);
Eric Laurent1fd372e2016-04-06 14:23:53 -07001916 if ((curStreamDevice & device) == 0) {
1917 continue;
1918 }
1919 bool applyDefault = false;
Eric Laurent5a2b6292016-04-14 18:05:57 -07001920 if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07001921 curStreamDevice |= device;
1922 } else if (!mVolumeCurves->hasVolumeIndexForDevice(
1923 stream, Volume::getDeviceForVolume(curStreamDevice))) {
1924 applyDefault = true;
1925 }
Eric Laurent28d09f02016-03-08 10:43:05 -08001926
Eric Laurent1fd372e2016-04-06 14:23:53 -07001927 if (applyDefault || ((curDevice & curStreamDevice) != 0)) {
Eric Laurentdc462862016-07-19 12:29:53 -07001928 //FIXME: workaround for truncated touch sounds
1929 // delayed volume change for system stream to be removed when the problem is
1930 // handled by system UI
Eric Laurent28d09f02016-03-08 10:43:05 -08001931 status_t volStatus =
Eric Laurentdc462862016-07-19 12:29:53 -07001932 checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice,
1933 (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0);
Eric Laurent28d09f02016-03-08 10:43:05 -08001934 if (volStatus != NO_ERROR) {
1935 status = volStatus;
1936 }
1937 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001938 }
Eric Laurente552edb2014-03-10 17:42:56 -07001939 }
1940 return status;
1941}
1942
Eric Laurente0720872014-03-11 09:30:41 -07001943status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001944 int *index,
1945 audio_devices_t device)
1946{
1947 if (index == NULL) {
1948 return BAD_VALUE;
1949 }
1950 if (!audio_is_output_device(device)) {
1951 return BAD_VALUE;
1952 }
Eric Laurent5a2b6292016-04-14 18:05:57 -07001953 // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device corresponding to
Eric Laurente552edb2014-03-10 17:42:56 -07001954 // the strategy the stream belongs to.
Eric Laurent5a2b6292016-04-14 18:05:57 -07001955 if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurente552edb2014-03-10 17:42:56 -07001956 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1957 }
François Gaffiedfd74092015-03-19 12:10:59 +01001958 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001959
François Gaffied1ab2bd2015-12-02 18:20:06 +01001960 *index = mVolumeCurves->getVolumeIndex(stream, device);
Eric Laurente552edb2014-03-10 17:42:56 -07001961 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1962 return NO_ERROR;
1963}
1964
Eric Laurente0720872014-03-11 09:30:41 -07001965audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001966 const SortedVector<audio_io_handle_t>& outputs)
1967{
1968 // select one output among several suitable for global effects.
1969 // The priority is as follows:
1970 // 1: An offloaded output. If the effect ends up not being offloadable,
1971 // AudioFlinger will invalidate the track and the offloaded output
1972 // will be closed causing the effect to be moved to a PCM output.
1973 // 2: A deep buffer output
1974 // 3: the first output in the list
1975
1976 if (outputs.size() == 0) {
1977 return 0;
1978 }
1979
1980 audio_io_handle_t outputOffloaded = 0;
1981 audio_io_handle_t outputDeepBuffer = 0;
1982
1983 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001984 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001985 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001986 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1987 outputOffloaded = outputs[i];
1988 }
1989 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1990 outputDeepBuffer = outputs[i];
1991 }
1992 }
1993
1994 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1995 outputOffloaded, outputDeepBuffer);
1996 if (outputOffloaded != 0) {
1997 return outputOffloaded;
1998 }
1999 if (outputDeepBuffer != 0) {
2000 return outputDeepBuffer;
2001 }
2002
2003 return outputs[0];
2004}
2005
Eric Laurente0720872014-03-11 09:30:41 -07002006audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07002007{
2008 // apply simple rule where global effects are attached to the same output as MUSIC streams
2009
Eric Laurent3b73df72014-03-11 09:06:29 -07002010 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07002011 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2012 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
2013
2014 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
2015 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
2016 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
2017
2018 return output;
2019}
2020
Eric Laurente0720872014-03-11 09:30:41 -07002021status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07002022 audio_io_handle_t io,
2023 uint32_t strategy,
2024 int session,
2025 int id)
2026{
2027 ssize_t index = mOutputs.indexOfKey(io);
2028 if (index < 0) {
2029 index = mInputs.indexOfKey(io);
2030 if (index < 0) {
2031 ALOGW("registerEffect() unknown io %d", io);
2032 return INVALID_OPERATION;
2033 }
2034 }
François Gaffie45ed3b02015-03-19 10:35:14 +01002035 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07002036}
2037
Eric Laurentc75307b2015-03-17 15:29:32 -07002038bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
2039{
Eric Laurent28d09f02016-03-08 10:43:05 -08002040 bool active = false;
Eric Laurent794fde22016-03-11 09:50:45 -08002041 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) {
2042 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002043 continue;
2044 }
2045 active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs);
2046 }
Eric Laurent28d09f02016-03-08 10:43:05 -08002047 return active;
Eric Laurentc75307b2015-03-17 15:29:32 -07002048}
2049
2050bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
2051{
2052 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
2053}
2054
Eric Laurente0720872014-03-11 09:30:41 -07002055bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07002056{
2057 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002058 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002059 if (inputDescriptor->isSourceActive(source)) {
Eric Laurente552edb2014-03-10 17:42:56 -07002060 return true;
2061 }
2062 }
2063 return false;
2064}
2065
Eric Laurent275e8e92014-11-30 15:14:47 -08002066// Register a list of custom mixes with their attributes and format.
2067// When a mix is registered, corresponding input and output profiles are
2068// added to the remote submix hw module. The profile contains only the
2069// parameters (sampling rate, format...) specified by the mix.
2070// The corresponding input remote submix device is also connected.
2071//
2072// When a remote submix device is connected, the address is checked to select the
2073// appropriate profile and the corresponding input or output stream is opened.
2074//
2075// When capture starts, getInputForAttr() will:
2076// - 1 look for a mix matching the address passed in attribtutes tags if any
2077// - 2 if none found, getDeviceForInputSource() will:
2078// - 2.1 look for a mix matching the attributes source
2079// - 2.2 if none found, default to device selection by policy rules
2080// At this time, the corresponding output remote submix device is also connected
2081// and active playback use cases can be transferred to this mix if needed when reconnecting
2082// after AudioTracks are invalidated
2083//
2084// When playback starts, getOutputForAttr() will:
2085// - 1 look for a mix matching the address passed in attribtutes tags if any
2086// - 2 if none found, look for a mix matching the attributes usage
2087// - 3 if none found, default to device and output selection by policy rules.
2088
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002089status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes)
Eric Laurent275e8e92014-11-30 15:14:47 -08002090{
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002091 ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size());
2092 status_t res = NO_ERROR;
2093
2094 sp<HwModule> rSubmixModule;
2095 // examine each mix's route type
2096 for (size_t i = 0; i < mixes.size(); i++) {
2097 // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination
2098 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) {
2099 res = INVALID_OPERATION;
Eric Laurent275e8e92014-11-30 15:14:47 -08002100 break;
2101 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002102 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
2103 // Loop back through "remote submix"
2104 if (rSubmixModule == 0) {
2105 for (size_t j = 0; i < mHwModules.size(); j++) {
2106 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2107 && mHwModules[j]->mHandle != 0) {
2108 rSubmixModule = mHwModules[j];
2109 break;
2110 }
2111 }
2112 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002113
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002114 ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size());
Eric Laurent275e8e92014-11-30 15:14:47 -08002115
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002116 if (rSubmixModule == 0) {
2117 ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", i);
2118 res = INVALID_OPERATION;
2119 break;
2120 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002121
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002122 String8 address = mixes[i].mDeviceAddress;
François Gaffie036e1e92015-03-19 10:16:24 +01002123
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002124 if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) {
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002125 ALOGE(" Error registering mix %zu for address %s", i, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002126 res = INVALID_OPERATION;
2127 break;
2128 }
2129 audio_config_t outputConfig = mixes[i].mFormat;
2130 audio_config_t inputConfig = mixes[i].mFormat;
2131 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
2132 // stereo and let audio flinger do the channel conversion if needed.
2133 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2134 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
2135 rSubmixModule->addOutputProfile(address, &outputConfig,
2136 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
2137 rSubmixModule->addInputProfile(address, &inputConfig,
2138 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01002139
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002140 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
2141 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2142 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2143 address.string(), "remote-submix");
2144 } else {
2145 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2146 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2147 address.string(), "remote-submix");
2148 }
2149 } else if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002150 String8 address = mixes[i].mDeviceAddress;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002151 audio_devices_t device = mixes[i].mDeviceType;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002152 ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s",
2153 i, mixes.size(), device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002154
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002155 bool foundOutput = false;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002156 for (size_t j = 0 ; j < mOutputs.size() ; j++) {
2157 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j);
2158 sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle());
2159 if ((patch != 0) && (patch->mPatch.num_sinks != 0)
2160 && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE)
2161 && (patch->mPatch.sinks[0].ext.device.type == device)
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002162 && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(),
2163 AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002164 if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) {
2165 res = INVALID_OPERATION;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002166 } else {
2167 foundOutput = true;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002168 }
2169 break;
2170 }
2171 }
2172
2173 if (res != NO_ERROR) {
2174 ALOGE(" Error registering mix %zu for device 0x%X addr %s",
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002175 i, device, address.string());
2176 res = INVALID_OPERATION;
2177 break;
2178 } else if (!foundOutput) {
2179 ALOGE(" Output not found for mix %zu for device 0x%X addr %s",
2180 i, device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002181 res = INVALID_OPERATION;
2182 break;
2183 }
Eric Laurentc722f302014-12-10 11:21:49 -08002184 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002185 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002186 if (res != NO_ERROR) {
2187 unregisterPolicyMixes(mixes);
2188 }
2189 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002190}
2191
2192status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
2193{
Eric Laurent7b279bb2015-12-14 10:18:23 -08002194 ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002195 status_t res = NO_ERROR;
2196 sp<HwModule> rSubmixModule;
2197 // examine each mix's route type
Eric Laurent275e8e92014-11-30 15:14:47 -08002198 for (size_t i = 0; i < mixes.size(); i++) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002199 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
François Gaffie036e1e92015-03-19 10:16:24 +01002200
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002201 if (rSubmixModule == 0) {
2202 for (size_t j = 0; i < mHwModules.size(); j++) {
2203 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2204 && mHwModules[j]->mHandle != 0) {
2205 rSubmixModule = mHwModules[j];
2206 break;
2207 }
2208 }
2209 }
2210 if (rSubmixModule == 0) {
2211 res = INVALID_OPERATION;
2212 continue;
2213 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002214
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002215 String8 address = mixes[i].mDeviceAddress;
Eric Laurent275e8e92014-11-30 15:14:47 -08002216
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002217 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
2218 res = INVALID_OPERATION;
2219 continue;
2220 }
2221
2222 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
2223 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2224 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2225 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2226 address.string(), "remote-submix");
2227 }
2228 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
2229 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2230 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2231 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2232 address.string(), "remote-submix");
2233 }
2234 rSubmixModule->removeOutputProfile(address);
2235 rSubmixModule->removeInputProfile(address);
2236
2237 } if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
2238 if (mPolicyMixes.unregisterMix(mixes[i].mDeviceAddress) != NO_ERROR) {
2239 res = INVALID_OPERATION;
2240 continue;
2241 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002242 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002243 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002244 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002245}
2246
Eric Laurente552edb2014-03-10 17:42:56 -07002247
Eric Laurente0720872014-03-11 09:30:41 -07002248status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07002249{
2250 const size_t SIZE = 256;
2251 char buffer[SIZE];
2252 String8 result;
2253
2254 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
2255 result.append(buffer);
2256
Eric Laurent87ffa392015-05-22 10:32:38 -07002257 snprintf(buffer, SIZE, " Primary Output: %d\n",
2258 hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE);
Eric Laurente552edb2014-03-10 17:42:56 -07002259 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002260 snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -07002261 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002262 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002263 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07002264 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002265 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07002266 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002267 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07002268 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002269 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07002270 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002271 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07002272 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002273 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002274 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002275 result.append(buffer);
Phil Burk09bc4612016-02-24 15:58:15 -08002276 snprintf(buffer, SIZE, " Force use for encoded surround output %d\n",
2277 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND));
2278 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002279 snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available");
2280 result.append(buffer);
Andy Hung2ddee192015-12-18 17:34:44 -08002281 snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off");
2282 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002283
François Gaffie2110e042015-03-24 08:41:51 +01002284 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07002285
François Gaffie112b0af2015-11-19 16:13:25 +01002286 mAvailableOutputDevices.dump(fd, String8("Available output"));
2287 mAvailableInputDevices.dump(fd, String8("Available input"));
François Gaffie53615e22015-03-19 09:24:12 +01002288 mHwModules.dump(fd);
2289 mOutputs.dump(fd);
2290 mInputs.dump(fd);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002291 mVolumeCurves->dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01002292 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002293 mAudioPatches.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07002294
2295 return NO_ERROR;
2296}
2297
2298// This function checks for the parameters which can be offloaded.
2299// This can be enhanced depending on the capability of the DSP and policy
2300// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002301bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002302{
2303 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002304 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002305 offloadInfo.sample_rate, offloadInfo.channel_mask,
2306 offloadInfo.format,
2307 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2308 offloadInfo.has_video);
2309
Andy Hung2ddee192015-12-18 17:34:44 -08002310 if (mMasterMono) {
2311 return false; // no offloading if mono is set.
2312 }
2313
Eric Laurente552edb2014-03-10 17:42:56 -07002314 // Check if offload has been disabled
2315 char propValue[PROPERTY_VALUE_MAX];
2316 if (property_get("audio.offload.disable", propValue, "0")) {
2317 if (atoi(propValue) != 0) {
2318 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2319 return false;
2320 }
2321 }
2322
2323 // Check if stream type is music, then only allow offload as of now.
2324 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2325 {
2326 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2327 return false;
2328 }
2329
2330 //TODO: enable audio offloading with video when ready
Andy Hung08945c42015-05-31 21:36:46 -07002331 const bool allowOffloadWithVideo =
2332 property_get_bool("audio.offload.video", false /* default_value */);
2333 if (offloadInfo.has_video && !allowOffloadWithVideo) {
Eric Laurente552edb2014-03-10 17:42:56 -07002334 ALOGV("isOffloadSupported: has_video == true, returning false");
2335 return false;
2336 }
2337
2338 //If duration is less than minimum value defined in property, return false
2339 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2340 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2341 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2342 return false;
2343 }
2344 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2345 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2346 return false;
2347 }
2348
2349 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2350 // creating an offloaded track and tearing it down immediately after start when audioflinger
2351 // detects there is an active non offloadable effect.
2352 // FIXME: We should check the audio session here but we do not have it in this context.
2353 // This may prevent offloading in rare situations where effects are left active by apps
2354 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002355 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002356 return false;
2357 }
2358
2359 // See if there is a profile to support this.
2360 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002361 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002362 offloadInfo.sample_rate,
2363 offloadInfo.format,
2364 offloadInfo.channel_mask,
2365 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002366 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2367 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002368}
2369
Eric Laurent6a94d692014-05-20 11:18:06 -07002370status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2371 audio_port_type_t type,
2372 unsigned int *num_ports,
2373 struct audio_port *ports,
2374 unsigned int *generation)
2375{
2376 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2377 generation == NULL) {
2378 return BAD_VALUE;
2379 }
2380 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2381 if (ports == NULL) {
2382 *num_ports = 0;
2383 }
2384
2385 size_t portsWritten = 0;
2386 size_t portsMax = *num_ports;
2387 *num_ports = 0;
2388 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002389 // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB
2390 // as they are used by stub HALs by convention
Eric Laurent6a94d692014-05-20 11:18:06 -07002391 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002392 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
2393 if (mAvailableOutputDevices[i]->type() == AUDIO_DEVICE_OUT_STUB) {
2394 continue;
2395 }
2396 if (portsWritten < portsMax) {
2397 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2398 }
2399 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002400 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002401 }
2402 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002403 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
2404 if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_STUB) {
2405 continue;
2406 }
2407 if (portsWritten < portsMax) {
2408 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2409 }
2410 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002411 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002412 }
2413 }
2414 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2415 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2416 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2417 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2418 }
2419 *num_ports += mInputs.size();
2420 }
2421 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002422 size_t numOutputs = 0;
2423 for (size_t i = 0; i < mOutputs.size(); i++) {
2424 if (!mOutputs[i]->isDuplicated()) {
2425 numOutputs++;
2426 if (portsWritten < portsMax) {
2427 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2428 }
2429 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002430 }
Eric Laurent84c70242014-06-23 08:46:27 -07002431 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002432 }
2433 }
2434 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002435 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002436 return NO_ERROR;
2437}
2438
2439status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2440{
2441 return NO_ERROR;
2442}
2443
Eric Laurent6a94d692014-05-20 11:18:06 -07002444status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2445 audio_patch_handle_t *handle,
2446 uid_t uid)
2447{
2448 ALOGV("createAudioPatch()");
2449
2450 if (handle == NULL || patch == NULL) {
2451 return BAD_VALUE;
2452 }
2453 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2454
Eric Laurent874c42872014-08-08 15:13:39 -07002455 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2456 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2457 return BAD_VALUE;
2458 }
2459 // only one source per audio patch supported for now
2460 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002461 return INVALID_OPERATION;
2462 }
Eric Laurent874c42872014-08-08 15:13:39 -07002463
2464 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002465 return INVALID_OPERATION;
2466 }
Eric Laurent874c42872014-08-08 15:13:39 -07002467 for (size_t i = 0; i < patch->num_sinks; i++) {
2468 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2469 return INVALID_OPERATION;
2470 }
2471 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002472
2473 sp<AudioPatch> patchDesc;
2474 ssize_t index = mAudioPatches.indexOfKey(*handle);
2475
Eric Laurent6a94d692014-05-20 11:18:06 -07002476 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2477 patch->sources[0].role,
2478 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002479#if LOG_NDEBUG == 0
2480 for (size_t i = 0; i < patch->num_sinks; i++) {
Eric Laurent7b279bb2015-12-14 10:18:23 -08002481 ALOGV("createAudioPatch sink %zu: id %d role %d type %d", i, patch->sinks[i].id,
Eric Laurent874c42872014-08-08 15:13:39 -07002482 patch->sinks[i].role,
2483 patch->sinks[i].type);
2484 }
2485#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002486
2487 if (index >= 0) {
2488 patchDesc = mAudioPatches.valueAt(index);
2489 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2490 mUidCached, patchDesc->mUid, uid);
2491 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2492 return INVALID_OPERATION;
2493 }
2494 } else {
Glenn Kastena13cde92016-03-28 15:26:02 -07002495 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07002496 }
2497
2498 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002499 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002500 if (outputDesc == NULL) {
2501 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2502 return BAD_VALUE;
2503 }
Eric Laurent84c70242014-06-23 08:46:27 -07002504 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2505 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002506 if (patchDesc != 0) {
2507 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2508 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2509 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2510 return BAD_VALUE;
2511 }
2512 }
Eric Laurent874c42872014-08-08 15:13:39 -07002513 DeviceVector devices;
2514 for (size_t i = 0; i < patch->num_sinks; i++) {
2515 // Only support mix to devices connection
2516 // TODO add support for mix to mix connection
2517 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2518 ALOGV("createAudioPatch() source mix but sink is not a device");
2519 return INVALID_OPERATION;
2520 }
2521 sp<DeviceDescriptor> devDesc =
2522 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2523 if (devDesc == 0) {
2524 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2525 return BAD_VALUE;
2526 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002527
François Gaffie53615e22015-03-19 09:24:12 +01002528 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002529 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002530 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002531 NULL, // updatedSamplingRate
2532 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002533 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002534 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002535 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002536 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002537 ALOGV("createAudioPatch() profile not supported for device %08x",
2538 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002539 return INVALID_OPERATION;
2540 }
2541 devices.add(devDesc);
2542 }
2543 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002544 return INVALID_OPERATION;
2545 }
Eric Laurent874c42872014-08-08 15:13:39 -07002546
Eric Laurent6a94d692014-05-20 11:18:06 -07002547 // TODO: reconfigure output format and channels here
2548 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002549 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002550 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002551 index = mAudioPatches.indexOfKey(*handle);
2552 if (index >= 0) {
2553 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2554 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2555 }
2556 patchDesc = mAudioPatches.valueAt(index);
2557 patchDesc->mUid = uid;
2558 ALOGV("createAudioPatch() success");
2559 } else {
2560 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2561 return INVALID_OPERATION;
2562 }
2563 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2564 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2565 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002566 // only one sink supported when connecting an input device to a mix
2567 if (patch->num_sinks > 1) {
2568 return INVALID_OPERATION;
2569 }
François Gaffie53615e22015-03-19 09:24:12 +01002570 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002571 if (inputDesc == NULL) {
2572 return BAD_VALUE;
2573 }
2574 if (patchDesc != 0) {
2575 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2576 return BAD_VALUE;
2577 }
2578 }
2579 sp<DeviceDescriptor> devDesc =
2580 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2581 if (devDesc == 0) {
2582 return BAD_VALUE;
2583 }
2584
François Gaffie53615e22015-03-19 09:24:12 +01002585 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002586 devDesc->mAddress,
2587 patch->sinks[0].sample_rate,
2588 NULL, /*updatedSampleRate*/
2589 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002590 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002591 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002592 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002593 // FIXME for the parameter type,
2594 // and the NONE
2595 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002596 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002597 return INVALID_OPERATION;
2598 }
2599 // TODO: reconfigure output format and channels here
2600 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002601 devDesc->type(), inputDesc->mIoHandle);
2602 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002603 index = mAudioPatches.indexOfKey(*handle);
2604 if (index >= 0) {
2605 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2606 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2607 }
2608 patchDesc = mAudioPatches.valueAt(index);
2609 patchDesc->mUid = uid;
2610 ALOGV("createAudioPatch() success");
2611 } else {
2612 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2613 return INVALID_OPERATION;
2614 }
2615 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2616 // device to device connection
2617 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002618 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002619 return BAD_VALUE;
2620 }
2621 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002622 sp<DeviceDescriptor> srcDeviceDesc =
2623 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002624 if (srcDeviceDesc == 0) {
2625 return BAD_VALUE;
2626 }
Eric Laurent874c42872014-08-08 15:13:39 -07002627
Eric Laurent6a94d692014-05-20 11:18:06 -07002628 //update source and sink with our own data as the data passed in the patch may
2629 // be incomplete.
2630 struct audio_patch newPatch = *patch;
2631 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002632
Eric Laurent874c42872014-08-08 15:13:39 -07002633 for (size_t i = 0; i < patch->num_sinks; i++) {
2634 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2635 ALOGV("createAudioPatch() source device but one sink is not a device");
2636 return INVALID_OPERATION;
2637 }
2638
2639 sp<DeviceDescriptor> sinkDeviceDesc =
2640 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2641 if (sinkDeviceDesc == 0) {
2642 return BAD_VALUE;
2643 }
2644 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2645
Eric Laurent3bcf8592015-04-03 12:13:24 -07002646 // create a software bridge in PatchPanel if:
2647 // - source and sink devices are on differnt HW modules OR
2648 // - audio HAL version is < 3.0
Eric Laurent232f26f2016-02-17 18:06:27 -08002649 if ((srcDeviceDesc->getModuleHandle() != sinkDeviceDesc->getModuleHandle()) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01002650 (srcDeviceDesc->mModule->getHalVersion() < AUDIO_DEVICE_API_VERSION_3_0)) {
Eric Laurent3bcf8592015-04-03 12:13:24 -07002651 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002652 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002653 return INVALID_OPERATION;
2654 }
Eric Laurent874c42872014-08-08 15:13:39 -07002655 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002656 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002657 // if the sink device is reachable via an opened output stream, request to go via
2658 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002659 audio_io_handle_t output = selectOutput(outputs,
2660 AUDIO_OUTPUT_FLAG_NONE,
2661 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002662 if (output != AUDIO_IO_HANDLE_NONE) {
2663 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2664 if (outputDesc->isDuplicated()) {
2665 return INVALID_OPERATION;
2666 }
2667 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002668 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002669 newPatch.num_sources = 2;
2670 }
Eric Laurent83b88082014-06-20 18:31:16 -07002671 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002672 }
2673 // TODO: check from routing capabilities in config file and other conflicting patches
2674
2675 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2676 if (index >= 0) {
2677 afPatchHandle = patchDesc->mAfPatchHandle;
2678 }
2679
2680 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2681 &afPatchHandle,
2682 0);
2683 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2684 status, afPatchHandle);
2685 if (status == NO_ERROR) {
2686 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002687 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002688 addAudioPatch(patchDesc->mHandle, patchDesc);
2689 } else {
2690 patchDesc->mPatch = newPatch;
2691 }
2692 patchDesc->mAfPatchHandle = afPatchHandle;
2693 *handle = patchDesc->mHandle;
2694 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002695 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002696 } else {
2697 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2698 status);
2699 return INVALID_OPERATION;
2700 }
2701 } else {
2702 return BAD_VALUE;
2703 }
2704 } else {
2705 return BAD_VALUE;
2706 }
2707 return NO_ERROR;
2708}
2709
2710status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2711 uid_t uid)
2712{
2713 ALOGV("releaseAudioPatch() patch %d", handle);
2714
2715 ssize_t index = mAudioPatches.indexOfKey(handle);
2716
2717 if (index < 0) {
2718 return BAD_VALUE;
2719 }
2720 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2721 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2722 mUidCached, patchDesc->mUid, uid);
2723 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2724 return INVALID_OPERATION;
2725 }
2726
2727 struct audio_patch *patch = &patchDesc->mPatch;
2728 patchDesc->mUid = mUidCached;
2729 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002730 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002731 if (outputDesc == NULL) {
2732 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2733 return BAD_VALUE;
2734 }
2735
Eric Laurentc75307b2015-03-17 15:29:32 -07002736 setOutputDevice(outputDesc,
2737 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002738 true,
2739 0,
2740 NULL);
2741 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2742 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002743 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002744 if (inputDesc == NULL) {
2745 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2746 return BAD_VALUE;
2747 }
2748 setInputDevice(inputDesc->mIoHandle,
Eric Laurent232f26f2016-02-17 18:06:27 -08002749 getNewInputDevice(inputDesc->mIoHandle),
Eric Laurent6a94d692014-05-20 11:18:06 -07002750 true,
2751 NULL);
2752 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002753 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2754 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2755 status, patchDesc->mAfPatchHandle);
2756 removeAudioPatch(patchDesc->mHandle);
2757 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002758 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002759 } else {
2760 return BAD_VALUE;
2761 }
2762 } else {
2763 return BAD_VALUE;
2764 }
2765 return NO_ERROR;
2766}
2767
2768status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2769 struct audio_patch *patches,
2770 unsigned int *generation)
2771{
François Gaffie53615e22015-03-19 09:24:12 +01002772 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002773 return BAD_VALUE;
2774 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002775 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01002776 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002777}
2778
Eric Laurente1715a42014-05-20 11:30:42 -07002779status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002780{
Eric Laurente1715a42014-05-20 11:30:42 -07002781 ALOGV("setAudioPortConfig()");
2782
2783 if (config == NULL) {
2784 return BAD_VALUE;
2785 }
2786 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2787 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002788 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2789 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002790 }
2791
Eric Laurenta121f902014-06-03 13:32:54 -07002792 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002793 if (config->type == AUDIO_PORT_TYPE_MIX) {
2794 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002795 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002796 if (outputDesc == NULL) {
2797 return BAD_VALUE;
2798 }
Eric Laurent84c70242014-06-23 08:46:27 -07002799 ALOG_ASSERT(!outputDesc->isDuplicated(),
2800 "setAudioPortConfig() called on duplicated output %d",
2801 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002802 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002803 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01002804 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002805 if (inputDesc == NULL) {
2806 return BAD_VALUE;
2807 }
Eric Laurenta121f902014-06-03 13:32:54 -07002808 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002809 } else {
2810 return BAD_VALUE;
2811 }
2812 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2813 sp<DeviceDescriptor> deviceDesc;
2814 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2815 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2816 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2817 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2818 } else {
2819 return BAD_VALUE;
2820 }
2821 if (deviceDesc == NULL) {
2822 return BAD_VALUE;
2823 }
Eric Laurenta121f902014-06-03 13:32:54 -07002824 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002825 } else {
2826 return BAD_VALUE;
2827 }
2828
Eric Laurenta121f902014-06-03 13:32:54 -07002829 struct audio_port_config backupConfig;
2830 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2831 if (status == NO_ERROR) {
2832 struct audio_port_config newConfig;
2833 audioPortConfig->toAudioPortConfig(&newConfig, config);
2834 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002835 }
Eric Laurenta121f902014-06-03 13:32:54 -07002836 if (status != NO_ERROR) {
2837 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002838 }
Eric Laurente1715a42014-05-20 11:30:42 -07002839
2840 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002841}
2842
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002843void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
2844{
Eric Laurentd60560a2015-04-10 11:31:20 -07002845 clearAudioSources(uid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002846 clearAudioPatches(uid);
2847 clearSessionRoutes(uid);
2848}
2849
Eric Laurent6a94d692014-05-20 11:18:06 -07002850void AudioPolicyManager::clearAudioPatches(uid_t uid)
2851{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002852 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002853 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2854 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002855 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002856 }
2857 }
2858}
2859
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002860void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
2861 audio_io_handle_t ouptutToSkip)
2862{
2863 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2864 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
2865 for (size_t j = 0; j < mOutputs.size(); j++) {
2866 if (mOutputs.keyAt(j) == ouptutToSkip) {
2867 continue;
2868 }
2869 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
2870 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
2871 continue;
2872 }
2873 // If the default device for this strategy is on another output mix,
2874 // invalidate all tracks in this strategy to force re connection.
2875 // Otherwise select new device on the output mix.
2876 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
Eric Laurent794fde22016-03-11 09:50:45 -08002877 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002878 if (getStrategy((audio_stream_type_t)stream) == strategy) {
2879 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
2880 }
2881 }
2882 } else {
2883 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
2884 setOutputDevice(outputDesc, newDevice, false);
2885 }
2886 }
2887}
2888
2889void AudioPolicyManager::clearSessionRoutes(uid_t uid)
2890{
2891 // remove output routes associated with this uid
2892 SortedVector<routing_strategy> affectedStrategies;
2893 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
2894 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
2895 if (route->mUid == uid) {
2896 mOutputRoutes.removeItemsAt(i);
2897 if (route->mDeviceDescriptor != 0) {
2898 affectedStrategies.add(getStrategy(route->mStreamType));
2899 }
2900 }
2901 }
2902 // reroute outputs if necessary
2903 for (size_t i = 0; i < affectedStrategies.size(); i++) {
2904 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
2905 }
2906
2907 // remove input routes associated with this uid
2908 SortedVector<audio_source_t> affectedSources;
2909 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
2910 sp<SessionRoute> route = mInputRoutes.valueAt(i);
2911 if (route->mUid == uid) {
2912 mInputRoutes.removeItemsAt(i);
2913 if (route->mDeviceDescriptor != 0) {
2914 affectedSources.add(route->mSource);
2915 }
2916 }
2917 }
2918 // reroute inputs if necessary
2919 SortedVector<audio_io_handle_t> inputsToClose;
2920 for (size_t i = 0; i < mInputs.size(); i++) {
2921 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002922 if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002923 inputsToClose.add(inputDesc->mIoHandle);
2924 }
2925 }
2926 for (size_t i = 0; i < inputsToClose.size(); i++) {
2927 closeInput(inputsToClose[i]);
2928 }
2929}
2930
Eric Laurentd60560a2015-04-10 11:31:20 -07002931void AudioPolicyManager::clearAudioSources(uid_t uid)
2932{
2933 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
2934 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
2935 if (sourceDesc->mUid == uid) {
2936 stopAudioSource(mAudioSources.keyAt(i));
2937 }
2938 }
2939}
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002940
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002941status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2942 audio_io_handle_t *ioHandle,
2943 audio_devices_t *device)
2944{
Glenn Kasteneeecb982016-02-26 10:44:04 -08002945 *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
2946 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002947 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002948
François Gaffiedf372692015-03-19 10:43:27 +01002949 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002950}
2951
Eric Laurentd60560a2015-04-10 11:31:20 -07002952status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source,
2953 const audio_attributes_t *attributes,
2954 audio_io_handle_t *handle,
2955 uid_t uid)
Eric Laurent554a2772015-04-10 11:29:24 -07002956{
Eric Laurentd60560a2015-04-10 11:31:20 -07002957 ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle);
2958 if (source == NULL || attributes == NULL || handle == NULL) {
2959 return BAD_VALUE;
2960 }
2961
2962 *handle = AUDIO_IO_HANDLE_NONE;
2963
2964 if (source->role != AUDIO_PORT_ROLE_SOURCE ||
2965 source->type != AUDIO_PORT_TYPE_DEVICE) {
2966 ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type);
2967 return INVALID_OPERATION;
2968 }
2969
2970 sp<DeviceDescriptor> srcDeviceDesc =
2971 mAvailableInputDevices.getDevice(source->ext.device.type,
2972 String8(source->ext.device.address));
2973 if (srcDeviceDesc == 0) {
2974 ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type);
2975 return BAD_VALUE;
2976 }
2977 sp<AudioSourceDescriptor> sourceDesc =
2978 new AudioSourceDescriptor(srcDeviceDesc, attributes, uid);
2979
2980 struct audio_patch dummyPatch;
2981 sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid);
2982 sourceDesc->mPatchDesc = patchDesc;
2983
2984 status_t status = connectAudioSource(sourceDesc);
2985 if (status == NO_ERROR) {
2986 mAudioSources.add(sourceDesc->getHandle(), sourceDesc);
2987 *handle = sourceDesc->getHandle();
2988 }
2989 return status;
2990}
2991
2992status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
2993{
2994 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
2995
2996 // make sure we only have one patch per source.
2997 disconnectAudioSource(sourceDesc);
2998
2999 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
Eric Laurent28d09f02016-03-08 10:43:05 -08003000 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003001 sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice;
3002
3003 audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true);
3004 sp<DeviceDescriptor> sinkDeviceDesc =
3005 mAvailableOutputDevices.getDevice(sinkDevice, String8(""));
3006
3007 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3008 struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch;
3009
3010 if (srcDeviceDesc->getAudioPort()->mModule->getHandle() ==
3011 sinkDeviceDesc->getAudioPort()->mModule->getHandle() &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003012 srcDeviceDesc->getAudioPort()->mModule->getHalVersion() >= AUDIO_DEVICE_API_VERSION_3_0 &&
Eric Laurentd60560a2015-04-10 11:31:20 -07003013 srcDeviceDesc->getAudioPort()->mGains.size() > 0) {
3014 ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__);
3015 // create patch between src device and output device
3016 // create Hwoutput and add to mHwOutputs
3017 } else {
3018 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs);
3019 audio_io_handle_t output =
3020 selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID);
3021 if (output == AUDIO_IO_HANDLE_NONE) {
3022 ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice);
3023 return INVALID_OPERATION;
3024 }
3025 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
3026 if (outputDesc->isDuplicated()) {
3027 ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice);
3028 return INVALID_OPERATION;
3029 }
3030 // create a special patch with no sink and two sources:
3031 // - the second source indicates to PatchPanel through which output mix this patch should
3032 // be connected as well as the stream type for volume control
3033 // - the sink is defined by whatever output device is currently selected for the output
3034 // though which this patch is routed.
3035 patch->num_sinks = 0;
3036 patch->num_sources = 2;
3037 srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL);
3038 outputDesc->toAudioPortConfig(&patch->sources[1], NULL);
3039 patch->sources[1].ext.mix.usecase.stream = stream;
3040 status_t status = mpClientInterface->createAudioPatch(patch,
3041 &afPatchHandle,
3042 0);
3043 ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__,
3044 status, afPatchHandle);
3045 if (status != NO_ERROR) {
3046 ALOGW("%s patch panel could not connect device patch, error %d",
3047 __FUNCTION__, status);
3048 return INVALID_OPERATION;
3049 }
3050 uint32_t delayMs = 0;
Eric Laurentc40d9692016-04-13 19:14:13 -07003051 status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs);
Eric Laurentd60560a2015-04-10 11:31:20 -07003052
3053 if (status != NO_ERROR) {
3054 mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0);
3055 return status;
3056 }
3057 sourceDesc->mSwOutput = outputDesc;
3058 if (delayMs != 0) {
3059 usleep(delayMs * 1000);
3060 }
3061 }
3062
3063 sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle;
3064 addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc);
3065
3066 return NO_ERROR;
Eric Laurent554a2772015-04-10 11:29:24 -07003067}
3068
Eric Laurent493404d2015-04-21 15:07:36 -07003069status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07003070{
Eric Laurentd60560a2015-04-10 11:31:20 -07003071 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle);
3072 ALOGV("%s handle %d", __FUNCTION__, handle);
3073 if (sourceDesc == 0) {
3074 ALOGW("%s unknown source for handle %d", __FUNCTION__, handle);
3075 return BAD_VALUE;
3076 }
3077 status_t status = disconnectAudioSource(sourceDesc);
3078
3079 mAudioSources.removeItem(handle);
3080 return status;
3081}
3082
Andy Hung2ddee192015-12-18 17:34:44 -08003083status_t AudioPolicyManager::setMasterMono(bool mono)
3084{
3085 if (mMasterMono == mono) {
3086 return NO_ERROR;
3087 }
3088 mMasterMono = mono;
3089 // if enabling mono we close all offloaded devices, which will invalidate the
3090 // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible
3091 // for recreating the new AudioTrack as non-offloaded PCM.
3092 //
3093 // If disabling mono, we leave all tracks as is: we don't know which clients
3094 // and tracks are able to be recreated as offloaded. The next "song" should
3095 // play back offloaded.
3096 if (mMasterMono) {
3097 Vector<audio_io_handle_t> offloaded;
3098 for (size_t i = 0; i < mOutputs.size(); ++i) {
3099 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
3100 if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
3101 offloaded.push(desc->mIoHandle);
3102 }
3103 }
3104 for (size_t i = 0; i < offloaded.size(); ++i) {
3105 closeOutput(offloaded[i]);
3106 }
3107 }
3108 // update master mono for all remaining outputs
3109 for (size_t i = 0; i < mOutputs.size(); ++i) {
3110 updateMono(mOutputs.keyAt(i));
3111 }
3112 return NO_ERROR;
3113}
3114
3115status_t AudioPolicyManager::getMasterMono(bool *mono)
3116{
3117 *mono = mMasterMono;
3118 return NO_ERROR;
3119}
3120
Eric Laurentd60560a2015-04-10 11:31:20 -07003121status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3122{
3123 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3124
3125 sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle);
3126 if (patchDesc == 0) {
3127 ALOGW("%s source has no patch with handle %d", __FUNCTION__,
3128 sourceDesc->mPatchDesc->mHandle);
3129 return BAD_VALUE;
3130 }
3131 removeAudioPatch(sourceDesc->mPatchDesc->mHandle);
3132
Eric Laurent28d09f02016-03-08 10:43:05 -08003133 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003134 sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote();
3135 if (swOutputDesc != 0) {
3136 stopSource(swOutputDesc, stream, false);
3137 mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3138 } else {
3139 sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote();
3140 if (hwOutputDesc != 0) {
3141 // release patch between src device and output device
3142 // close Hwoutput and remove from mHwOutputs
3143 } else {
3144 ALOGW("%s source has neither SW nor HW output", __FUNCTION__);
3145 }
3146 }
3147 return NO_ERROR;
3148}
3149
3150sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput(
3151 audio_io_handle_t output, routing_strategy strategy)
3152{
3153 sp<AudioSourceDescriptor> source;
3154 for (size_t i = 0; i < mAudioSources.size(); i++) {
3155 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3156 routing_strategy sourceStrategy =
3157 (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
3158 sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote();
3159 if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) {
3160 source = sourceDesc;
3161 break;
3162 }
3163 }
3164 return source;
Eric Laurent554a2772015-04-10 11:29:24 -07003165}
3166
Eric Laurente552edb2014-03-10 17:42:56 -07003167// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07003168// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07003169// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07003170uint32_t AudioPolicyManager::nextAudioPortGeneration()
3171{
3172 return android_atomic_inc(&mAudioPortGeneration);
3173}
3174
Eric Laurente0720872014-03-11 09:30:41 -07003175AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07003176 :
3177#ifdef AUDIO_POLICY_TEST
3178 Thread(false),
3179#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07003180 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07003181 mA2dpSuspended(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003182 mAudioPortGeneration(1),
3183 mBeaconMuteRefCount(0),
3184 mBeaconPlayingRefCount(0),
Eric Laurent9459fb02015-08-12 18:36:32 -07003185 mBeaconMuted(false),
Andy Hung2ddee192015-12-18 17:34:44 -08003186 mTtsOutputAvailable(false),
3187 mMasterMono(false)
Eric Laurente552edb2014-03-10 17:42:56 -07003188{
François Gaffied1ab2bd2015-12-02 18:20:06 +01003189 mUidCached = getuid();
3190 mpClientInterface = clientInterface;
3191
3192 // TODO: remove when legacy conf file is removed. true on devices that use DRC on the
3193 // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly.
3194 // Note: remove also speaker_drc_enabled from global configuration of XML config file.
3195 bool speakerDrcEnabled = false;
3196
3197#ifdef USE_XML_AUDIO_POLICY_CONF
3198 mVolumeCurves = new VolumeCurvesCollection();
3199 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3200 mDefaultOutputDevice, speakerDrcEnabled,
3201 static_cast<VolumeCurvesCollection *>(mVolumeCurves));
3202 PolicySerializer serializer;
3203 if (serializer.deserialize(AUDIO_POLICY_XML_CONFIG_FILE, config) != NO_ERROR) {
3204#else
3205 mVolumeCurves = new StreamDescriptorCollection();
3206 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3207 mDefaultOutputDevice, speakerDrcEnabled);
3208 if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) &&
3209 (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) {
3210#endif
3211 ALOGE("could not load audio policy configuration file, setting defaults");
3212 config.setDefault();
3213 }
3214 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
3215 mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled);
3216
3217 // Once policy config has been parsed, retrieve an instance of the engine and initialize it.
François Gaffie2110e042015-03-24 08:41:51 +01003218 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
3219 if (!engineInstance) {
3220 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
3221 return;
3222 }
3223 // Retrieve the Policy Manager Interface
3224 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
3225 if (mEngine == NULL) {
3226 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
3227 return;
3228 }
3229 mEngine->setObserver(this);
3230 status_t status = mEngine->initCheck();
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07003231 (void) status;
François Gaffie2110e042015-03-24 08:41:51 +01003232 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
3233
Eric Laurent3a4311c2014-03-17 12:00:47 -07003234 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07003235 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07003236 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
3237 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07003238 for (size_t i = 0; i < mHwModules.size(); i++) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003239 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003240 if (mHwModules[i]->mHandle == 0) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003241 ALOGW("could not open HW module %s", mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003242 continue;
3243 }
3244 // open all output streams needed to access attached devices
3245 // except for direct output streams that are only opened when they are actually
3246 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07003247 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07003248 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3249 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003250 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003251
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003252 if (!outProfile->hasSupportedDevices()) {
3253 ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003254 continue;
3255 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003256 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) {
Eric Laurent9459fb02015-08-12 18:36:32 -07003257 mTtsOutputAvailable = true;
3258 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003259
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003260 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
Eric Laurentd78f1532014-09-16 16:38:20 -07003261 continue;
3262 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003263 audio_devices_t profileType = outProfile->getSupportedDevicesType();
François Gaffie53615e22015-03-19 09:24:12 +01003264 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
3265 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07003266 } else {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003267 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003268 // outputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003269 profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes);
Eric Laurente552edb2014-03-10 17:42:56 -07003270 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003271 if ((profileType & outputDeviceTypes) == 0) {
3272 continue;
3273 }
Eric Laurentc75307b2015-03-17 15:29:32 -07003274 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
3275 mpClientInterface);
Eric Laurentc40d9692016-04-13 19:14:13 -07003276 const DeviceVector &supportedDevices = outProfile->getSupportedDevices();
3277 const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType);
3278 String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress
3279 : String8("");
Eric Laurentd78f1532014-09-16 16:38:20 -07003280
3281 outputDesc->mDevice = profileType;
3282 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3283 config.sample_rate = outputDesc->mSamplingRate;
3284 config.channel_mask = outputDesc->mChannelMask;
3285 config.format = outputDesc->mFormat;
3286 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003287 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003288 &output,
3289 &config,
3290 &outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003291 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003292 &outputDesc->mLatency,
3293 outputDesc->mFlags);
3294
3295 if (status != NO_ERROR) {
3296 ALOGW("Cannot open output stream for device %08x on hw module %s",
3297 outputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003298 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003299 } else {
3300 outputDesc->mSamplingRate = config.sample_rate;
3301 outputDesc->mChannelMask = config.channel_mask;
3302 outputDesc->mFormat = config.format;
3303
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003304 for (size_t k = 0; k < supportedDevices.size(); k++) {
3305 ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003306 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08003307 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
3308 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003309 }
3310 }
3311 if (mPrimaryOutput == 0 &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003312 outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003313 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07003314 }
3315 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003316 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07003317 outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003318 true,
3319 0,
3320 NULL,
3321 address.string());
Eric Laurentd78f1532014-09-16 16:38:20 -07003322 }
Eric Laurente552edb2014-03-10 17:42:56 -07003323 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003324 // open input streams needed to access attached devices to validate
3325 // mAvailableInputDevices list
3326 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
3327 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003328 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003329
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003330 if (!inProfile->hasSupportedDevices()) {
3331 ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003332 continue;
3333 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003334 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003335 // inputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003336 audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes);
3337
Eric Laurentd78f1532014-09-16 16:38:20 -07003338 if ((profileType & inputDeviceTypes) == 0) {
3339 continue;
3340 }
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08003341 sp<AudioInputDescriptor> inputDesc =
3342 new AudioInputDescriptor(inProfile);
Eric Laurentd78f1532014-09-16 16:38:20 -07003343
Eric Laurentd78f1532014-09-16 16:38:20 -07003344 inputDesc->mDevice = profileType;
3345
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003346 // find the address
3347 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
3348 // the inputs vector must be of size 1, but we don't want to crash here
3349 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
3350 : String8("");
3351 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
3352 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
3353
Eric Laurentd78f1532014-09-16 16:38:20 -07003354 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3355 config.sample_rate = inputDesc->mSamplingRate;
3356 config.channel_mask = inputDesc->mChannelMask;
3357 config.format = inputDesc->mFormat;
3358 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003359 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003360 &input,
3361 &config,
3362 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003363 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003364 AUDIO_SOURCE_MIC,
3365 AUDIO_INPUT_FLAG_NONE);
3366
3367 if (status == NO_ERROR) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003368 const DeviceVector &supportedDevices = inProfile->getSupportedDevices();
3369 for (size_t k = 0; k < supportedDevices.size(); k++) {
3370 ssize_t index = mAvailableInputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003371 // give a valid ID to an attached device once confirmed it is reachable
Eric Laurent45aabc32015-08-06 09:11:13 -07003372 if (index >= 0) {
3373 sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index];
3374 if (!devDesc->isAttached()) {
3375 devDesc->attach(mHwModules[i]);
3376 devDesc->importAudioPort(inProfile);
3377 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003378 }
3379 }
3380 mpClientInterface->closeInput(input);
3381 } else {
3382 ALOGW("Cannot open input stream for device %08x on hw module %s",
3383 inputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003384 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003385 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003386 }
3387 }
3388 // make sure all attached devices have been allocated a unique ID
3389 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003390 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003391 ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003392 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
3393 continue;
3394 }
François Gaffie2110e042015-03-24 08:41:51 +01003395 // The device is now validated and can be appended to the available devices of the engine
3396 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
3397 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003398 i++;
3399 }
3400 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003401 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01003402 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003403 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
3404 continue;
3405 }
François Gaffie2110e042015-03-24 08:41:51 +01003406 // The device is now validated and can be appended to the available devices of the engine
3407 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
3408 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003409 i++;
3410 }
3411 // make sure default device is reachable
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003412 if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01003413 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003414 }
Eric Laurente552edb2014-03-10 17:42:56 -07003415
3416 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
3417
3418 updateDevicesAndOutputs();
3419
3420#ifdef AUDIO_POLICY_TEST
3421 if (mPrimaryOutput != 0) {
3422 AudioParameter outputCmd = AudioParameter();
3423 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003424 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07003425
3426 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
3427 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07003428 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
3429 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003430 mTestLatencyMs = 0;
3431 mCurOutput = 0;
3432 mDirectOutput = false;
3433 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3434 mTestOutputs[i] = 0;
3435 }
3436
3437 const size_t SIZE = 256;
3438 char buffer[SIZE];
3439 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
3440 run(buffer, ANDROID_PRIORITY_AUDIO);
3441 }
3442#endif //AUDIO_POLICY_TEST
3443}
3444
Eric Laurente0720872014-03-11 09:30:41 -07003445AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07003446{
3447#ifdef AUDIO_POLICY_TEST
3448 exit();
3449#endif //AUDIO_POLICY_TEST
3450 for (size_t i = 0; i < mOutputs.size(); i++) {
3451 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003452 }
3453 for (size_t i = 0; i < mInputs.size(); i++) {
3454 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003455 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003456 mAvailableOutputDevices.clear();
3457 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003458 mOutputs.clear();
3459 mInputs.clear();
3460 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07003461}
3462
Eric Laurente0720872014-03-11 09:30:41 -07003463status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07003464{
Eric Laurent87ffa392015-05-22 10:32:38 -07003465 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003466}
3467
3468#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07003469bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07003470{
3471 ALOGV("entering threadLoop()");
3472 while (!exitPending())
3473 {
3474 String8 command;
3475 int valueInt;
3476 String8 value;
3477
3478 Mutex::Autolock _l(mLock);
3479 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
3480
3481 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
3482 AudioParameter param = AudioParameter(command);
3483
3484 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
3485 valueInt != 0) {
3486 ALOGV("Test command %s received", command.string());
3487 String8 target;
3488 if (param.get(String8("target"), target) != NO_ERROR) {
3489 target = "Manager";
3490 }
3491 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
3492 param.remove(String8("test_cmd_policy_output"));
3493 mCurOutput = valueInt;
3494 }
3495 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
3496 param.remove(String8("test_cmd_policy_direct"));
3497 if (value == "false") {
3498 mDirectOutput = false;
3499 } else if (value == "true") {
3500 mDirectOutput = true;
3501 }
3502 }
3503 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
3504 param.remove(String8("test_cmd_policy_input"));
3505 mTestInput = valueInt;
3506 }
3507
3508 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
3509 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07003510 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07003511 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003512 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003513 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003514 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003515 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003516 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07003517 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003518 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07003519 if (target == "Manager") {
3520 mTestFormat = format;
3521 } else if (mTestOutputs[mCurOutput] != 0) {
3522 AudioParameter outputParam = AudioParameter();
3523 outputParam.addInt(String8("format"), format);
3524 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3525 }
3526 }
3527 }
3528 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
3529 param.remove(String8("test_cmd_policy_channels"));
3530 int channels = 0;
3531
3532 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003533 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003534 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003535 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07003536 }
3537 if (channels != 0) {
3538 if (target == "Manager") {
3539 mTestChannels = channels;
3540 } else if (mTestOutputs[mCurOutput] != 0) {
3541 AudioParameter outputParam = AudioParameter();
3542 outputParam.addInt(String8("channels"), channels);
3543 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3544 }
3545 }
3546 }
3547 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
3548 param.remove(String8("test_cmd_policy_sampleRate"));
3549 if (valueInt >= 0 && valueInt <= 96000) {
3550 int samplingRate = valueInt;
3551 if (target == "Manager") {
3552 mTestSamplingRate = samplingRate;
3553 } else if (mTestOutputs[mCurOutput] != 0) {
3554 AudioParameter outputParam = AudioParameter();
3555 outputParam.addInt(String8("sampling_rate"), samplingRate);
3556 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3557 }
3558 }
3559 }
3560
3561 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
3562 param.remove(String8("test_cmd_policy_reopen"));
3563
Eric Laurentc75307b2015-03-17 15:29:32 -07003564 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07003565
Eric Laurentc75307b2015-03-17 15:29:32 -07003566 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07003567
Eric Laurentc75307b2015-03-17 15:29:32 -07003568 removeOutput(mPrimaryOutput->mIoHandle);
3569 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
3570 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003571 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003572 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3573 config.sample_rate = outputDesc->mSamplingRate;
3574 config.channel_mask = outputDesc->mChannelMask;
3575 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07003576 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003577 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07003578 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07003579 &config,
3580 &outputDesc->mDevice,
3581 String8(""),
3582 &outputDesc->mLatency,
3583 outputDesc->mFlags);
3584 if (status != NO_ERROR) {
3585 ALOGE("Failed to reopen hardware output stream, "
3586 "samplingRate: %d, format %d, channels %d",
3587 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003588 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003589 outputDesc->mSamplingRate = config.sample_rate;
3590 outputDesc->mChannelMask = config.channel_mask;
3591 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07003592 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07003593 AudioParameter outputCmd = AudioParameter();
3594 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003595 mpClientInterface->setParameters(handle, outputCmd.toString());
3596 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07003597 }
3598 }
3599
3600
3601 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3602 }
3603 }
3604 return false;
3605}
3606
Eric Laurente0720872014-03-11 09:30:41 -07003607void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003608{
3609 {
3610 AutoMutex _l(mLock);
3611 requestExit();
3612 mWaitWorkCV.signal();
3613 }
3614 requestExitAndWait();
3615}
3616
Eric Laurente0720872014-03-11 09:30:41 -07003617int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003618{
3619 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3620 if (output == mTestOutputs[i]) return i;
3621 }
3622 return 0;
3623}
3624#endif //AUDIO_POLICY_TEST
3625
3626// ---
3627
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07003628void AudioPolicyManager::addOutput(audio_io_handle_t output, const sp<SwAudioOutputDescriptor>& outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003629{
François Gaffie98cc1912015-03-18 17:52:40 +01003630 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003631 mOutputs.add(output, outputDesc);
Andy Hung2ddee192015-12-18 17:34:44 -08003632 updateMono(output); // update mono status when adding to output list
Eric Laurent6a94d692014-05-20 11:18:06 -07003633 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003634}
3635
François Gaffie53615e22015-03-19 09:24:12 +01003636void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3637{
3638 mOutputs.removeItem(output);
3639}
3640
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07003641void AudioPolicyManager::addInput(audio_io_handle_t input, const sp<AudioInputDescriptor>& inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003642{
François Gaffie98cc1912015-03-18 17:52:40 +01003643 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003644 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003645 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003646}
Eric Laurente552edb2014-03-10 17:42:56 -07003647
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07003648void AudioPolicyManager::findIoHandlesByAddress(const sp<SwAudioOutputDescriptor>& desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003649 const audio_devices_t device /*in*/,
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07003650 const String8& address /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003651 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003652 sp<DeviceDescriptor> devDesc =
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003653 desc->mProfile->getSupportedDeviceByAddress(device, address);
keunyoung3190e672014-12-30 13:00:52 -08003654 if (devDesc != 0) {
3655 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3656 desc->mIoHandle, address.string());
3657 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003658 }
3659}
3660
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07003661status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003662 audio_policy_dev_state_t state,
3663 SortedVector<audio_io_handle_t>& outputs,
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07003664 const String8& address)
Eric Laurente552edb2014-03-10 17:42:56 -07003665{
François Gaffie53615e22015-03-19 09:24:12 +01003666 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003667 sp<SwAudioOutputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003668
3669 if (audio_device_is_digital(device)) {
3670 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003671 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003672 }
Eric Laurente552edb2014-03-10 17:42:56 -07003673
Eric Laurent3b73df72014-03-11 09:06:29 -07003674 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003675 // first list already open outputs that can be routed to this device
3676 for (size_t i = 0; i < mOutputs.size(); i++) {
3677 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003678 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003679 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003680 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3681 outputs.add(mOutputs.keyAt(i));
3682 } else {
3683 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003684 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003685 }
Eric Laurente552edb2014-03-10 17:42:56 -07003686 }
3687 }
3688 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003689 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003690 for (size_t i = 0; i < mHwModules.size(); i++)
3691 {
3692 if (mHwModules[i]->mHandle == 0) {
3693 continue;
3694 }
3695 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3696 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003697 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003698 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003699 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003700 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003701 profiles.add(profile);
3702 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3703 }
Eric Laurente552edb2014-03-10 17:42:56 -07003704 }
3705 }
3706 }
3707
Eric Laurent7b279bb2015-12-14 10:18:23 -08003708 ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size());
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003709
Eric Laurente552edb2014-03-10 17:42:56 -07003710 if (profiles.isEmpty() && outputs.isEmpty()) {
3711 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3712 return BAD_VALUE;
3713 }
3714
3715 // open outputs for matching profiles if needed. Direct outputs are also opened to
3716 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3717 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003718 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003719
3720 // nothing to do if one output is already opened for this profile
3721 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003722 for (j = 0; j < outputs.size(); j++) {
3723 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003724 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003725 // matching profile: save the sample rates, format and channel masks supported
3726 // by the profile in our device descriptor
Paul McLean9080a4c2015-06-18 08:24:02 -07003727 if (audio_device_is_digital(device)) {
3728 devDesc->importAudioPort(profile);
3729 }
Eric Laurente552edb2014-03-10 17:42:56 -07003730 break;
3731 }
3732 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003733 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003734 continue;
3735 }
3736
Eric Laurent83b88082014-06-20 18:31:16 -07003737 ALOGV("opening output for device %08x with params %s profile %p",
3738 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07003739 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003740 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003741 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3742 config.sample_rate = desc->mSamplingRate;
3743 config.channel_mask = desc->mChannelMask;
3744 config.format = desc->mFormat;
3745 config.offload_info.sample_rate = desc->mSamplingRate;
3746 config.offload_info.channel_mask = desc->mChannelMask;
3747 config.offload_info.format = desc->mFormat;
3748 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003749 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003750 &output,
3751 &config,
3752 &desc->mDevice,
3753 address,
3754 &desc->mLatency,
3755 desc->mFlags);
3756 if (status == NO_ERROR) {
3757 desc->mSamplingRate = config.sample_rate;
3758 desc->mChannelMask = config.channel_mask;
3759 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003760
Eric Laurentd4692962014-05-05 18:13:44 -07003761 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003762 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003763 char *param = audio_device_address_to_parameter(device, address);
3764 mpClientInterface->setParameters(output, String8(param));
3765 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003766 }
Phil Burk00eeb322016-03-31 12:41:00 -07003767 updateAudioProfiles(device, output, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01003768 if (!profile->hasValidAudioProfile()) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003769 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003770 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003771 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003772 } else if (profile->hasDynamicAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07003773 mpClientInterface->closeOutput(output);
Phil Burk702b1052016-03-02 16:38:26 -08003774 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003775 profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003776 config.offload_info.sample_rate = config.sample_rate;
3777 config.offload_info.channel_mask = config.channel_mask;
3778 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07003779 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003780 &output,
3781 &config,
3782 &desc->mDevice,
3783 address,
3784 &desc->mLatency,
3785 desc->mFlags);
3786 if (status == NO_ERROR) {
3787 desc->mSamplingRate = config.sample_rate;
3788 desc->mChannelMask = config.channel_mask;
3789 desc->mFormat = config.format;
3790 } else {
3791 output = AUDIO_IO_HANDLE_NONE;
3792 }
Eric Laurentd4692962014-05-05 18:13:44 -07003793 }
3794
Eric Laurentcf2c0212014-07-25 16:20:43 -07003795 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003796 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003797 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003798 sp<AudioPolicyMix> policyMix;
3799 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003800 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3801 address.string());
3802 }
François Gaffie036e1e92015-03-19 10:16:24 +01003803 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07003804 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01003805
Eric Laurent87ffa392015-05-22 10:32:38 -07003806 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
3807 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08003808 // no duplicated output for direct outputs and
3809 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003810 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003811
Eric Laurentd4692962014-05-05 18:13:44 -07003812 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07003813 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003814
Eric Laurentd4692962014-05-05 18:13:44 -07003815 //TODO: configure audio effect output stage here
3816
3817 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07003818 duplicatedOutput =
3819 mpClientInterface->openDuplicateOutput(output,
3820 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003821 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003822 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07003823 sp<SwAudioOutputDescriptor> dupOutputDesc =
3824 new SwAudioOutputDescriptor(NULL, mpClientInterface);
3825 dupOutputDesc->mOutput1 = mPrimaryOutput;
3826 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003827 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3828 dupOutputDesc->mFormat = desc->mFormat;
3829 dupOutputDesc->mChannelMask = desc->mChannelMask;
3830 dupOutputDesc->mLatency = desc->mLatency;
3831 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003832 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07003833 } else {
3834 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07003835 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07003836 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003837 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003838 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003839 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003840 }
Eric Laurente552edb2014-03-10 17:42:56 -07003841 }
3842 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003843 } else {
3844 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003845 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003846 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003847 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003848 profiles.removeAt(profile_index);
3849 profile_index--;
3850 } else {
3851 outputs.add(output);
Paul McLean9080a4c2015-06-18 08:24:02 -07003852 // Load digital format info only for digital devices
3853 if (audio_device_is_digital(device)) {
3854 devDesc->importAudioPort(profile);
3855 }
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003856
François Gaffie53615e22015-03-19 09:24:12 +01003857 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003858 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3859 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003860 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003861 NULL/*patch handle*/, address.string());
3862 }
Eric Laurente552edb2014-03-10 17:42:56 -07003863 ALOGV("checkOutputsForDevice(): adding output %d", output);
3864 }
3865 }
3866
3867 if (profiles.isEmpty()) {
3868 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3869 return BAD_VALUE;
3870 }
Eric Laurentd4692962014-05-05 18:13:44 -07003871 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003872 // check if one opened output is not needed any more after disconnecting one device
3873 for (size_t i = 0; i < mOutputs.size(); i++) {
3874 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003875 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003876 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01003877 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07003878 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003879 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07003880 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003881 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3882 mOutputs.keyAt(i));
3883 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003884 }
Eric Laurente552edb2014-03-10 17:42:56 -07003885 }
3886 }
Eric Laurentd4692962014-05-05 18:13:44 -07003887 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003888 for (size_t i = 0; i < mHwModules.size(); i++)
3889 {
3890 if (mHwModules[i]->mHandle == 0) {
3891 continue;
3892 }
3893 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3894 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003895 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003896 if (profile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003897 ALOGV("checkOutputsForDevice(): "
3898 "clearing direct output profile %zu on module %zu", j, i);
François Gaffie112b0af2015-11-19 16:13:25 +01003899 profile->clearAudioProfiles();
Eric Laurente552edb2014-03-10 17:42:56 -07003900 }
3901 }
3902 }
3903 }
3904 return NO_ERROR;
3905}
3906
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07003907status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003908 audio_policy_dev_state_t state,
3909 SortedVector<audio_io_handle_t>& inputs,
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07003910 const String8& address)
Eric Laurentd4692962014-05-05 18:13:44 -07003911{
Paul McLean9080a4c2015-06-18 08:24:02 -07003912 audio_devices_t device = devDesc->type();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003913 sp<AudioInputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003914
3915 if (audio_device_is_digital(device)) {
3916 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003917 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003918 }
3919
Eric Laurentd4692962014-05-05 18:13:44 -07003920 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3921 // first list already open inputs that can be routed to this device
3922 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3923 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003924 if (desc->mProfile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003925 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3926 inputs.add(mInputs.keyAt(input_index));
3927 }
3928 }
3929
3930 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003931 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003932 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3933 {
3934 if (mHwModules[module_idx]->mHandle == 0) {
3935 continue;
3936 }
3937 for (size_t profile_index = 0;
3938 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3939 profile_index++)
3940 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003941 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3942
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003943 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003944 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003945 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003946 profiles.add(profile);
3947 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3948 profile_index, module_idx);
3949 }
Eric Laurentd4692962014-05-05 18:13:44 -07003950 }
3951 }
3952 }
3953
3954 if (profiles.isEmpty() && inputs.isEmpty()) {
3955 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3956 return BAD_VALUE;
3957 }
3958
3959 // open inputs for matching profiles if needed. Direct inputs are also opened to
3960 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3961 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3962
Eric Laurent1c333e22014-05-20 10:48:17 -07003963 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003964 // nothing to do if one input is already opened for this profile
3965 size_t input_index;
3966 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3967 desc = mInputs.valueAt(input_index);
3968 if (desc->mProfile == profile) {
Paul McLean9080a4c2015-06-18 08:24:02 -07003969 if (audio_device_is_digital(device)) {
3970 devDesc->importAudioPort(profile);
3971 }
Eric Laurentd4692962014-05-05 18:13:44 -07003972 break;
3973 }
3974 }
3975 if (input_index != mInputs.size()) {
3976 continue;
3977 }
3978
3979 ALOGV("opening input for device 0x%X with params %s", device, address.string());
3980 desc = new AudioInputDescriptor(profile);
3981 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003982 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3983 config.sample_rate = desc->mSamplingRate;
3984 config.channel_mask = desc->mChannelMask;
3985 config.format = desc->mFormat;
3986 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003987 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003988 &input,
3989 &config,
3990 &desc->mDevice,
3991 address,
3992 AUDIO_SOURCE_MIC,
3993 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07003994
Eric Laurentcf2c0212014-07-25 16:20:43 -07003995 if (status == NO_ERROR) {
3996 desc->mSamplingRate = config.sample_rate;
3997 desc->mChannelMask = config.channel_mask;
3998 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07003999
Eric Laurentd4692962014-05-05 18:13:44 -07004000 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07004001 char *param = audio_device_address_to_parameter(device, address);
4002 mpClientInterface->setParameters(input, String8(param));
4003 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07004004 }
Phil Burk00eeb322016-03-31 12:41:00 -07004005 updateAudioProfiles(device, input, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01004006 if (!profile->hasValidAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07004007 ALOGW("checkInputsForDevice() direct input missing param");
4008 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004009 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07004010 }
4011
4012 if (input != 0) {
4013 addInput(input, desc);
4014 }
4015 } // endif input != 0
4016
Eric Laurentcf2c0212014-07-25 16:20:43 -07004017 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07004018 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07004019 profiles.removeAt(profile_index);
4020 profile_index--;
4021 } else {
4022 inputs.add(input);
Paul McLean9080a4c2015-06-18 08:24:02 -07004023 if (audio_device_is_digital(device)) {
4024 devDesc->importAudioPort(profile);
4025 }
Eric Laurentd4692962014-05-05 18:13:44 -07004026 ALOGV("checkInputsForDevice(): adding input %d", input);
4027 }
4028 } // end scan profiles
4029
4030 if (profiles.isEmpty()) {
4031 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
4032 return BAD_VALUE;
4033 }
4034 } else {
4035 // Disconnect
4036 // check if one opened input is not needed any more after disconnecting one device
4037 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
4038 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004039 if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) {
Eric Laurentd4692962014-05-05 18:13:44 -07004040 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
4041 mInputs.keyAt(input_index));
4042 inputs.add(mInputs.keyAt(input_index));
4043 }
4044 }
4045 // Clear any profiles associated with the disconnected device.
4046 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
4047 if (mHwModules[module_index]->mHandle == 0) {
4048 continue;
4049 }
4050 for (size_t profile_index = 0;
4051 profile_index < mHwModules[module_index]->mInputProfiles.size();
4052 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004053 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004054 if (profile->supportDevice(device)) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07004055 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07004056 profile_index, module_index);
François Gaffie112b0af2015-11-19 16:13:25 +01004057 profile->clearAudioProfiles();
Eric Laurentd4692962014-05-05 18:13:44 -07004058 }
4059 }
4060 }
4061 } // end disconnect
4062
4063 return NO_ERROR;
4064}
4065
4066
Eric Laurente0720872014-03-11 09:30:41 -07004067void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07004068{
4069 ALOGV("closeOutput(%d)", output);
4070
Eric Laurentc75307b2015-03-17 15:29:32 -07004071 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004072 if (outputDesc == NULL) {
4073 ALOGW("closeOutput() unknown output %d", output);
4074 return;
4075 }
François Gaffie036e1e92015-03-19 10:16:24 +01004076 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08004077
Eric Laurente552edb2014-03-10 17:42:56 -07004078 // look for duplicated outputs connected to the output being removed.
4079 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004080 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07004081 if (dupOutputDesc->isDuplicated() &&
4082 (dupOutputDesc->mOutput1 == outputDesc ||
4083 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004084 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07004085 if (dupOutputDesc->mOutput1 == outputDesc) {
4086 outputDesc2 = dupOutputDesc->mOutput2;
4087 } else {
4088 outputDesc2 = dupOutputDesc->mOutput1;
4089 }
4090 // As all active tracks on duplicated output will be deleted,
4091 // and as they were also referenced on the other output, the reference
4092 // count for their stream type must be adjusted accordingly on
4093 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07004094 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07004095 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07004096 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07004097 }
4098 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
4099 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
4100
4101 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01004102 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07004103 }
4104 }
4105
Eric Laurent05b90f82014-08-27 15:32:29 -07004106 nextAudioPortGeneration();
4107
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004108 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004109 if (index >= 0) {
4110 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07004111 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07004112 mAudioPatches.removeItemsAt(index);
4113 mpClientInterface->onAudioPatchListUpdate();
4114 }
4115
Eric Laurente552edb2014-03-10 17:42:56 -07004116 AudioParameter param;
4117 param.add(String8("closing"), String8("true"));
4118 mpClientInterface->setParameters(output, param.toString());
4119
4120 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01004121 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004122 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07004123}
4124
4125void AudioPolicyManager::closeInput(audio_io_handle_t input)
4126{
4127 ALOGV("closeInput(%d)", input);
4128
4129 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
4130 if (inputDesc == NULL) {
4131 ALOGW("closeInput() unknown input %d", input);
4132 return;
4133 }
4134
Eric Laurent6a94d692014-05-20 11:18:06 -07004135 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07004136
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004137 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004138 if (index >= 0) {
4139 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07004140 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07004141 mAudioPatches.removeItemsAt(index);
4142 mpClientInterface->onAudioPatchListUpdate();
4143 }
4144
4145 mpClientInterface->closeInput(input);
4146 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07004147}
4148
Eric Laurentc75307b2015-03-17 15:29:32 -07004149SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
4150 audio_devices_t device,
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07004151 const SwAudioOutputCollection& openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07004152{
4153 SortedVector<audio_io_handle_t> outputs;
4154
4155 ALOGVV("getOutputsForDevice() device %04x", device);
4156 for (size_t i = 0; i < openOutputs.size(); i++) {
4157 ALOGVV("output %d isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004158 i, openOutputs.valueAt(i)->isDuplicated(),
4159 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004160 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
4161 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
4162 outputs.add(openOutputs.keyAt(i));
4163 }
4164 }
4165 return outputs;
4166}
4167
Eric Laurente0720872014-03-11 09:30:41 -07004168bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01004169 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07004170{
4171 if (outputs1.size() != outputs2.size()) {
4172 return false;
4173 }
4174 for (size_t i = 0; i < outputs1.size(); i++) {
4175 if (outputs1[i] != outputs2[i]) {
4176 return false;
4177 }
4178 }
4179 return true;
4180}
4181
Eric Laurente0720872014-03-11 09:30:41 -07004182void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07004183{
4184 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
4185 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
4186 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
4187 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
4188
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004189 // also take into account external policy-related changes: add all outputs which are
4190 // associated with policies in the "before" and "after" output vectors
4191 ALOGVV("checkOutputForStrategy(): policy related outputs");
4192 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004193 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004194 if (desc != 0 && desc->mPolicyMix != NULL) {
4195 srcOutputs.add(desc->mIoHandle);
4196 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
4197 }
4198 }
4199 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004200 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004201 if (desc != 0 && desc->mPolicyMix != NULL) {
4202 dstOutputs.add(desc->mIoHandle);
4203 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
4204 }
4205 }
4206
Eric Laurente552edb2014-03-10 17:42:56 -07004207 if (!vectorsEqual(srcOutputs,dstOutputs)) {
4208 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
4209 strategy, srcOutputs[0], dstOutputs[0]);
4210 // mute strategy while moving tracks from one output to another
4211 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004212 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01004213 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004214 setStrategyMute(strategy, true, desc);
4215 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004216 }
Eric Laurentd60560a2015-04-10 11:31:20 -07004217 sp<AudioSourceDescriptor> source =
4218 getSourceForStrategyOnOutput(srcOutputs[i], strategy);
4219 if (source != 0){
4220 connectAudioSource(source);
4221 }
Eric Laurente552edb2014-03-10 17:42:56 -07004222 }
4223
4224 // Move effects associated to this strategy from previous output to new output
4225 if (strategy == STRATEGY_MEDIA) {
4226 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
4227 SortedVector<audio_io_handle_t> moved;
4228 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004229 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
4230 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
4231 effectDesc->mIo != fxOutput) {
4232 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004233 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
4234 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004235 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07004236 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004237 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07004238 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07004239 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004240 }
4241 }
4242 }
4243 // Move tracks associated to this strategy from previous output to new output
Eric Laurent794fde22016-03-11 09:50:45 -08004244 for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004245 if (getStrategy((audio_stream_type_t)i) == strategy) {
4246 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07004247 }
4248 }
4249 }
4250}
4251
Eric Laurente0720872014-03-11 09:30:41 -07004252void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07004253{
François Gaffie2110e042015-03-24 08:41:51 +01004254 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004255 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004256 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01004257 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004258 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004259 checkOutputForStrategy(STRATEGY_SONIFICATION);
4260 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004261 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07004262 checkOutputForStrategy(STRATEGY_MEDIA);
4263 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004264 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07004265}
4266
Eric Laurente0720872014-03-11 09:30:41 -07004267void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07004268{
François Gaffie53615e22015-03-19 09:24:12 +01004269 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07004270 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004271 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07004272 return;
4273 }
4274
Eric Laurent3a4311c2014-03-17 12:00:47 -07004275 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08004276 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
4277 ~AUDIO_DEVICE_BIT_IN) != 0) ||
4278 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07004279 // suspend A2DP output if:
4280 // (NOT already suspended) &&
4281 // ((SCO device is connected &&
4282 // (forced usage for communication || for record is SCO))) ||
4283 // (phone state is ringing || in call)
4284 //
4285 // restore A2DP output if:
4286 // (Already suspended) &&
4287 // ((SCO device is NOT connected ||
4288 // (forced usage NOT for communication && NOT for record is SCO))) &&
4289 // (phone state is NOT ringing && NOT in call)
4290 //
4291 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004292 if ((!isScoConnected ||
François Gaffie2110e042015-03-24 08:41:51 +01004293 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) &&
4294 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) &&
4295 ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
4296 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004297
4298 mpClientInterface->restoreOutput(a2dpOutput);
4299 mA2dpSuspended = false;
4300 }
4301 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004302 if ((isScoConnected &&
François Gaffie2110e042015-03-24 08:41:51 +01004303 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) ||
4304 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) ||
4305 ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
4306 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004307
4308 mpClientInterface->suspendOutput(a2dpOutput);
4309 mA2dpSuspended = true;
4310 }
4311 }
4312}
4313
Eric Laurentc75307b2015-03-17 15:29:32 -07004314audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
4315 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004316{
4317 audio_devices_t device = AUDIO_DEVICE_NONE;
4318
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004319 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004320 if (index >= 0) {
4321 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4322 if (patchDesc->mUid != mUidCached) {
4323 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004324 outputDesc->device(), outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004325 return outputDesc->device();
4326 }
4327 }
4328
Eric Laurente552edb2014-03-10 17:42:56 -07004329 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05004330 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004331 // use device for strategy enforced audible
4332 // 2: we are in call or the strategy phone is active on the output:
4333 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05004334 // 3: the strategy for enforced audible is active but not enforced on the output:
4335 // use the device for strategy enforced audible
Eric Laurent28d09f02016-03-08 10:43:05 -08004336 // 4: the strategy sonification is active on the output:
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004337 // use device for strategy sonification
Eric Laurent28d09f02016-03-08 10:43:05 -08004338 // 5: the strategy accessibility is active on the output:
4339 // use device for strategy accessibility
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004340 // 6: the strategy "respectful" sonification is active on the output:
4341 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08004342 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004343 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08004344 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004345 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08004346 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004347 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01004348 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004349 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07004350 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
4351 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01004352 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004353 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004354 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05004355 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004356 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004357 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Eric Laurent28d09f02016-03-08 10:43:05 -08004358 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
4359 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004360 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004361 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004362 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004363 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004364 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004365 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004366 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004367 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004368 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004369 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07004370 }
4371
Eric Laurent1c333e22014-05-20 10:48:17 -07004372 ALOGV("getNewOutputDevice() selected device %x", device);
4373 return device;
4374}
4375
Eric Laurent232f26f2016-02-17 18:06:27 -08004376audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input)
Eric Laurent1c333e22014-05-20 10:48:17 -07004377{
Eric Laurent232f26f2016-02-17 18:06:27 -08004378 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004379
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004380 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004381 if (index >= 0) {
4382 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4383 if (patchDesc->mUid != mUidCached) {
4384 ALOGV("getNewInputDevice() device %08x forced by patch %d",
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004385 inputDesc->mDevice, inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004386 return inputDesc->mDevice;
4387 }
4388 }
4389
Eric Laurent232f26f2016-02-17 18:06:27 -08004390 audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->inputSource());
Eric Laurent1c333e22014-05-20 10:48:17 -07004391
Eric Laurente552edb2014-03-10 17:42:56 -07004392 return device;
4393}
4394
Eric Laurent794fde22016-03-11 09:50:45 -08004395bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1,
4396 audio_stream_type_t stream2) {
4397 return ((stream1 == stream2) ||
4398 ((stream1 == AUDIO_STREAM_ACCESSIBILITY) && (stream2 == AUDIO_STREAM_MUSIC)) ||
4399 ((stream1 == AUDIO_STREAM_MUSIC) && (stream2 == AUDIO_STREAM_ACCESSIBILITY)));
Eric Laurent28d09f02016-03-08 10:43:05 -08004400}
4401
Eric Laurente0720872014-03-11 09:30:41 -07004402uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004403 return (uint32_t)getStrategy(stream);
4404}
4405
Eric Laurente0720872014-03-11 09:30:41 -07004406audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004407 // By checking the range of stream before calling getStrategy, we avoid
4408 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
4409 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08004410 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004411 return AUDIO_DEVICE_NONE;
4412 }
Eric Laurent28d09f02016-03-08 10:43:05 -08004413 audio_devices_t devices = AUDIO_DEVICE_NONE;
Eric Laurent794fde22016-03-11 09:50:45 -08004414 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
4415 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004416 continue;
Eric Laurent6a94d692014-05-20 11:18:06 -07004417 }
Eric Laurent794fde22016-03-11 09:50:45 -08004418 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08004419 audio_devices_t curDevices =
Eric Laurent447a87b2016-07-07 17:32:10 -07004420 getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/);
Eric Laurent28d09f02016-03-08 10:43:05 -08004421 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs);
4422 for (size_t i = 0; i < outputs.size(); i++) {
4423 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurent794fde22016-03-11 09:50:45 -08004424 if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004425 curDevices |= outputDesc->device();
4426 }
4427 }
4428 devices |= curDevices;
Eric Laurente552edb2014-03-10 17:42:56 -07004429 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05004430
4431 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
4432 and doesn't really need to.*/
4433 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
4434 devices |= AUDIO_DEVICE_OUT_SPEAKER;
4435 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4436 }
Eric Laurente552edb2014-03-10 17:42:56 -07004437 return devices;
4438}
4439
François Gaffie2110e042015-03-24 08:41:51 +01004440routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
4441{
Eric Laurent223fd5c2014-11-11 13:43:36 -08004442 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01004443 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004444}
4445
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004446uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4447 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004448 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4449 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4450 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004451 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4452 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4453 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004454 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01004455 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004456}
4457
Eric Laurente0720872014-03-11 09:30:41 -07004458void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004459 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004460 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004461 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4462 updateDevicesAndOutputs();
4463 break;
4464 default:
4465 break;
4466 }
4467}
4468
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004469uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
Eric Laurent9459fb02015-08-12 18:36:32 -07004470
4471 // skip beacon mute management if a dedicated TTS output is available
4472 if (mTtsOutputAvailable) {
4473 return 0;
4474 }
4475
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004476 switch(event) {
4477 case STARTING_OUTPUT:
4478 mBeaconMuteRefCount++;
4479 break;
4480 case STOPPING_OUTPUT:
4481 if (mBeaconMuteRefCount > 0) {
4482 mBeaconMuteRefCount--;
4483 }
4484 break;
4485 case STARTING_BEACON:
4486 mBeaconPlayingRefCount++;
4487 break;
4488 case STOPPING_BEACON:
4489 if (mBeaconPlayingRefCount > 0) {
4490 mBeaconPlayingRefCount--;
4491 }
4492 break;
4493 }
4494
4495 if (mBeaconMuteRefCount > 0) {
4496 // any playback causes beacon to be muted
4497 return setBeaconMute(true);
4498 } else {
4499 // no other playback: unmute when beacon starts playing, mute when it stops
4500 return setBeaconMute(mBeaconPlayingRefCount == 0);
4501 }
4502}
4503
4504uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4505 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4506 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4507 // keep track of muted state to avoid repeating mute/unmute operations
4508 if (mBeaconMuted != mute) {
4509 // mute/unmute AUDIO_STREAM_TTS on all outputs
4510 ALOGV("\t muting %d", mute);
4511 uint32_t maxLatency = 0;
4512 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004513 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004514 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004515 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004516 0 /*delay*/, AUDIO_DEVICE_NONE);
4517 const uint32_t latency = desc->latency() * 2;
4518 if (latency > maxLatency) {
4519 maxLatency = latency;
4520 }
4521 }
4522 mBeaconMuted = mute;
4523 return maxLatency;
4524 }
4525 return 0;
4526}
4527
Eric Laurente0720872014-03-11 09:30:41 -07004528audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004529 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004530{
Paul McLeanaa981192015-03-21 09:55:15 -07004531 // Routing
4532 // see if we have an explicit route
4533 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4534 // (getStrategy(stream)).
4535 // if the strategy from the stream type in the RouteMap is the same as the argument above,
4536 // and activity count is non-zero
4537 // the device = the device from the descriptor in the RouteMap, and exit.
4538 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4539 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
Eric Laurent28d09f02016-03-08 10:43:05 -08004540 routing_strategy routeStrategy = getStrategy(route->mStreamType);
4541 if ((routeStrategy == strategy) && route->isActive()) {
Paul McLeanaa981192015-03-21 09:55:15 -07004542 return route->mDeviceDescriptor->type();
4543 }
4544 }
4545
Eric Laurente552edb2014-03-10 17:42:56 -07004546 if (fromCache) {
4547 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4548 strategy, mDeviceForStrategy[strategy]);
4549 return mDeviceForStrategy[strategy];
4550 }
François Gaffie2110e042015-03-24 08:41:51 +01004551 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004552}
4553
Eric Laurente0720872014-03-11 09:30:41 -07004554void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004555{
4556 for (int i = 0; i < NUM_STRATEGIES; i++) {
4557 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4558 }
4559 mPreviousOutputs = mOutputs;
4560}
4561
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07004562uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004563 audio_devices_t prevDevice,
4564 uint32_t delayMs)
4565{
4566 // mute/unmute strategies using an incompatible device combination
4567 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4568 // if unmuting, unmute only after the specified delay
4569 if (outputDesc->isDuplicated()) {
4570 return 0;
4571 }
4572
4573 uint32_t muteWaitMs = 0;
4574 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004575 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004576
4577 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4578 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004579 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004580 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4581 bool doMute = false;
4582
4583 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4584 doMute = true;
4585 outputDesc->mStrategyMutedByDevice[i] = true;
4586 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4587 doMute = true;
4588 outputDesc->mStrategyMutedByDevice[i] = false;
4589 }
Eric Laurent99401132014-05-07 19:48:15 -07004590 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004591 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004592 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004593 // skip output if it does not share any device with current output
4594 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4595 == AUDIO_DEVICE_NONE) {
4596 continue;
4597 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004598 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)",
4599 mute ? "muting" : "unmuting", i, curDevice);
4600 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004601 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004602 if (mute) {
4603 // FIXME: should not need to double latency if volume could be applied
4604 // immediately by the audioflinger mixer. We must account for the delay
4605 // between now and the next time the audioflinger thread for this output
4606 // will process a buffer (which corresponds to one buffer size,
4607 // usually 1/2 or 1/4 of the latency).
4608 if (muteWaitMs < desc->latency() * 2) {
4609 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004610 }
4611 }
4612 }
4613 }
4614 }
4615 }
4616
Eric Laurent99401132014-05-07 19:48:15 -07004617 // temporary mute output if device selection changes to avoid volume bursts due to
4618 // different per device volumes
4619 if (outputDesc->isActive() && (device != prevDevice)) {
Eric Laurentdc462862016-07-19 12:29:53 -07004620 uint32_t tempMuteWaitMs = outputDesc->latency() * 2;
4621 // temporary mute duration is conservatively set to 4 times the reported latency
4622 uint32_t tempMuteDurationMs = outputDesc->latency() * 4;
4623 if (muteWaitMs < tempMuteWaitMs) {
4624 muteWaitMs = tempMuteWaitMs;
Eric Laurent99401132014-05-07 19:48:15 -07004625 }
Eric Laurentdc462862016-07-19 12:29:53 -07004626
Eric Laurent99401132014-05-07 19:48:15 -07004627 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004628 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentdc462862016-07-19 12:29:53 -07004629 // make sure that we do not start the temporary mute period too early in case of
4630 // delayed device change
4631 setStrategyMute((routing_strategy)i, true, outputDesc, delayMs);
Eric Laurentc75307b2015-03-17 15:29:32 -07004632 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurentdc462862016-07-19 12:29:53 -07004633 delayMs + tempMuteDurationMs, device);
Eric Laurent99401132014-05-07 19:48:15 -07004634 }
4635 }
4636 }
4637
Eric Laurente552edb2014-03-10 17:42:56 -07004638 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4639 if (muteWaitMs > delayMs) {
4640 muteWaitMs -= delayMs;
4641 usleep(muteWaitMs * 1000);
4642 return muteWaitMs;
4643 }
4644 return 0;
4645}
4646
Eric Laurentc75307b2015-03-17 15:29:32 -07004647uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004648 audio_devices_t device,
4649 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004650 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004651 audio_patch_handle_t *patchHandle,
4652 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004653{
Eric Laurentc75307b2015-03-17 15:29:32 -07004654 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004655 AudioParameter param;
4656 uint32_t muteWaitMs;
4657
4658 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004659 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4660 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004661 return muteWaitMs;
4662 }
4663 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4664 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004665 if ((device != AUDIO_DEVICE_NONE) &&
4666 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004667 return 0;
4668 }
4669
4670 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004671 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004672
4673 audio_devices_t prevDevice = outputDesc->mDevice;
4674
Paul McLeanaa981192015-03-21 09:55:15 -07004675 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004676
4677 if (device != AUDIO_DEVICE_NONE) {
4678 outputDesc->mDevice = device;
4679 }
4680 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4681
4682 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004683 // the requested device is AUDIO_DEVICE_NONE
4684 // OR the requested device is the same as current device
4685 // AND force is not specified
4686 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004687 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004688 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4689 !force &&
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004690 outputDesc->getPatchHandle() != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004691 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004692 return muteWaitMs;
4693 }
4694
4695 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004696
Eric Laurente552edb2014-03-10 17:42:56 -07004697 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004698 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004699 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004700 } else {
Eric Laurentc40d9692016-04-13 19:14:13 -07004701 DeviceVector deviceList;
4702 if ((address == NULL) || (strlen(address) == 0)) {
4703 deviceList = mAvailableOutputDevices.getDevicesFromType(device);
4704 } else {
4705 deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
4706 }
4707
Eric Laurent1c333e22014-05-20 10:48:17 -07004708 if (!deviceList.isEmpty()) {
4709 struct audio_patch patch;
4710 outputDesc->toAudioPortConfig(&patch.sources[0]);
4711 patch.num_sources = 1;
4712 patch.num_sinks = 0;
4713 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4714 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004715 patch.num_sinks++;
4716 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004717 ssize_t index;
4718 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4719 index = mAudioPatches.indexOfKey(*patchHandle);
4720 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004721 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004722 }
4723 sp< AudioPatch> patchDesc;
4724 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4725 if (index >= 0) {
4726 patchDesc = mAudioPatches.valueAt(index);
4727 afPatchHandle = patchDesc->mAfPatchHandle;
4728 }
4729
Eric Laurent1c333e22014-05-20 10:48:17 -07004730 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004731 &afPatchHandle,
4732 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004733 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4734 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004735 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004736 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004737 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004738 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004739 addAudioPatch(patchDesc->mHandle, patchDesc);
4740 } else {
4741 patchDesc->mPatch = patch;
4742 }
4743 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004744 if (patchHandle) {
4745 *patchHandle = patchDesc->mHandle;
4746 }
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004747 outputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004748 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004749 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004750 }
4751 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004752
4753 // inform all input as well
4754 for (size_t i = 0; i < mInputs.size(); i++) {
4755 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004756 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004757 AudioParameter inputCmd = AudioParameter();
4758 ALOGV("%s: inform input %d of device:%d", __func__,
4759 inputDescriptor->mIoHandle, device);
4760 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4761 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4762 inputCmd.toString(),
4763 delayMs);
4764 }
4765 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004766 }
Eric Laurente552edb2014-03-10 17:42:56 -07004767
4768 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004769 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004770
4771 return muteWaitMs;
4772}
4773
Eric Laurentc75307b2015-03-17 15:29:32 -07004774status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004775 int delayMs,
4776 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004777{
Eric Laurent6a94d692014-05-20 11:18:06 -07004778 ssize_t index;
4779 if (patchHandle) {
4780 index = mAudioPatches.indexOfKey(*patchHandle);
4781 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004782 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004783 }
4784 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004785 return INVALID_OPERATION;
4786 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004787 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4788 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004789 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07004790 outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07004791 removeAudioPatch(patchDesc->mHandle);
4792 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004793 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004794 return status;
4795}
4796
4797status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4798 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004799 bool force,
4800 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004801{
4802 status_t status = NO_ERROR;
4803
Eric Laurent1f2f2232014-06-02 12:01:23 -07004804 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004805 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4806 inputDesc->mDevice = device;
4807
4808 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4809 if (!deviceList.isEmpty()) {
4810 struct audio_patch patch;
4811 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004812 // AUDIO_SOURCE_HOTWORD is for internal use only:
4813 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004814 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
Eric Laurent599c7582015-12-07 18:05:55 -08004815 !inputDesc->isSoundTrigger()) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004816 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4817 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004818 patch.num_sinks = 1;
4819 //only one input device for now
4820 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004821 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004822 ssize_t index;
4823 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4824 index = mAudioPatches.indexOfKey(*patchHandle);
4825 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004826 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004827 }
4828 sp< AudioPatch> patchDesc;
4829 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4830 if (index >= 0) {
4831 patchDesc = mAudioPatches.valueAt(index);
4832 afPatchHandle = patchDesc->mAfPatchHandle;
4833 }
4834
Eric Laurent1c333e22014-05-20 10:48:17 -07004835 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004836 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004837 0);
4838 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004839 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004840 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004841 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004842 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004843 addAudioPatch(patchDesc->mHandle, patchDesc);
4844 } else {
4845 patchDesc->mPatch = patch;
4846 }
4847 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004848 if (patchHandle) {
4849 *patchHandle = patchDesc->mHandle;
4850 }
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004851 inputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004852 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004853 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004854 }
4855 }
4856 }
4857 return status;
4858}
4859
Eric Laurent6a94d692014-05-20 11:18:06 -07004860status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4861 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004862{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004863 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004864 ssize_t index;
4865 if (patchHandle) {
4866 index = mAudioPatches.indexOfKey(*patchHandle);
4867 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004868 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004869 }
4870 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004871 return INVALID_OPERATION;
4872 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004873 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4874 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004875 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07004876 inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07004877 removeAudioPatch(patchDesc->mHandle);
4878 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004879 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004880 return status;
4881}
4882
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004883sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07004884 const String8& address,
François Gaffie53615e22015-03-19 09:24:12 +01004885 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07004886 audio_format_t& format,
4887 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01004888 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004889{
4890 // Choose an input profile based on the requested capture parameters: select the first available
4891 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07004892 //
4893 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
4894 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07004895
4896 for (size_t i = 0; i < mHwModules.size(); i++)
4897 {
4898 if (mHwModules[i]->mHandle == 0) {
4899 continue;
4900 }
4901 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4902 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004903 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004904 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004905 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07004906 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07004907 format,
4908 &format /*updatedFormat*/,
4909 channelMask,
4910 &channelMask /*updatedChannelMask*/,
4911 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004912
Eric Laurente552edb2014-03-10 17:42:56 -07004913 return profile;
4914 }
4915 }
4916 }
4917 return NULL;
4918}
4919
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004920
4921audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01004922 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07004923{
François Gaffie036e1e92015-03-19 10:16:24 +01004924 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
4925 audio_devices_t selectedDeviceFromMix =
4926 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08004927
François Gaffie036e1e92015-03-19 10:16:24 +01004928 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
4929 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08004930 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004931 return getDeviceForInputSource(inputSource);
4932}
4933
4934audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
4935{
Paul McLean466dc8e2015-04-17 13:15:36 -06004936 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
4937 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004938 if (inputSource == route->mSource && route->isActive()) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004939 return route->mDeviceDescriptor->type();
4940 }
4941 }
4942
4943 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07004944}
4945
Eric Laurente0720872014-03-11 09:30:41 -07004946float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004947 int index,
4948 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004949{
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004950 float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index);
Eric Laurente552edb2014-03-10 17:42:56 -07004951 // if a headset is connected, apply the following rules to ring tones and notifications
4952 // to avoid sound level bursts in user's ears:
Eric Laurent6af1c1d2016-04-14 11:20:44 -07004953 // - always attenuate notifications volume by 6dB
4954 // - attenuate ring tones volume by 6dB unless music is not playing and
4955 // speaker is part of the select devices
Eric Laurente552edb2014-03-10 17:42:56 -07004956 // - if music is playing, always limit the volume to current music volume,
4957 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004958 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004959 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4960 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4961 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4962 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4963 ((stream_strategy == STRATEGY_SONIFICATION)
4964 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004965 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004966 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004967 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
François Gaffied1ab2bd2015-12-02 18:20:06 +01004968 mVolumeCurves->canBeMuted(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004969 // when the phone is ringing we must consider that music could have been paused just before
4970 // by the music application and behave as if music was active if the last music track was
4971 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004972 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004973 mLimitRingtoneVolume) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004974 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004975 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004976 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004977 mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC,
4978 musicDevice),
4979 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004980 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
4981 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004982 if (volumeDB > minVolDB) {
4983 volumeDB = minVolDB;
Eric Laurentffbc80f2015-03-18 18:30:19 -07004984 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07004985 }
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004986 if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4987 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) {
4988 // on A2DP, also ensure notification volume is not too low compared to media when
4989 // intended to be played
4990 if ((volumeDB > -96.0f) &&
4991 (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) {
4992 ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f",
4993 stream, device,
4994 volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB);
4995 volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB;
4996 }
4997 }
Eric Laurent6af1c1d2016-04-14 11:20:44 -07004998 } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) ||
4999 stream_strategy != STRATEGY_SONIFICATION) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005000 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07005001 }
5002 }
5003
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005004 return volumeDB;
Eric Laurente552edb2014-03-10 17:42:56 -07005005}
5006
Eric Laurente0720872014-03-11 09:30:41 -07005007status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005008 int index,
5009 const sp<AudioOutputDescriptor>& outputDesc,
5010 audio_devices_t device,
5011 int delayMs,
5012 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005013{
Eric Laurente552edb2014-03-10 17:42:56 -07005014 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07005015 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07005016 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07005017 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07005018 return NO_ERROR;
5019 }
François Gaffie2110e042015-03-24 08:41:51 +01005020 audio_policy_forced_cfg_t forceUseForComm =
5021 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07005022 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01005023 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
5024 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005025 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01005026 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07005027 return INVALID_OPERATION;
5028 }
5029
Eric Laurentc75307b2015-03-17 15:29:32 -07005030 if (device == AUDIO_DEVICE_NONE) {
5031 device = outputDesc->device();
5032 }
Eric Laurent275e8e92014-11-30 15:14:47 -08005033
Eric Laurentffbc80f2015-03-18 18:30:19 -07005034 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07005035 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07005036 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08005037 }
Eric Laurentc75307b2015-03-17 15:29:32 -07005038
Eric Laurentffbc80f2015-03-18 18:30:19 -07005039 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07005040
Eric Laurent3b73df72014-03-11 09:06:29 -07005041 if (stream == AUDIO_STREAM_VOICE_CALL ||
5042 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07005043 float voiceVolume;
5044 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07005045 if (stream == AUDIO_STREAM_VOICE_CALL) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005046 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005047 } else {
5048 voiceVolume = 1.0;
5049 }
5050
Eric Laurent18fba842016-03-31 14:41:26 -07005051 if (voiceVolume != mLastVoiceVolume) {
Eric Laurente552edb2014-03-10 17:42:56 -07005052 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
5053 mLastVoiceVolume = voiceVolume;
5054 }
5055 }
5056
5057 return NO_ERROR;
5058}
5059
Eric Laurentc75307b2015-03-17 15:29:32 -07005060void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
5061 audio_devices_t device,
5062 int delayMs,
5063 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005064{
Eric Laurentc75307b2015-03-17 15:29:32 -07005065 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07005066
Eric Laurent794fde22016-03-11 09:50:45 -08005067 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005068 checkAndSetVolume((audio_stream_type_t)stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005069 mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005070 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005071 device,
5072 delayMs,
5073 force);
5074 }
5075}
5076
Eric Laurente0720872014-03-11 09:30:41 -07005077void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07005078 bool on,
5079 const sp<AudioOutputDescriptor>& outputDesc,
5080 int delayMs,
5081 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005082{
Eric Laurent72249d52015-06-08 11:12:15 -07005083 ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d",
5084 strategy, on, outputDesc->getId());
Eric Laurent794fde22016-03-11 09:50:45 -08005085 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005086 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005087 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07005088 }
5089 }
5090}
5091
Eric Laurente0720872014-03-11 09:30:41 -07005092void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005093 bool on,
5094 const sp<AudioOutputDescriptor>& outputDesc,
5095 int delayMs,
5096 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005097{
Eric Laurente552edb2014-03-10 17:42:56 -07005098 if (device == AUDIO_DEVICE_NONE) {
5099 device = outputDesc->device();
5100 }
5101
Eric Laurentc75307b2015-03-17 15:29:32 -07005102 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
5103 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07005104
5105 if (on) {
5106 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005107 if (mVolumeCurves->canBeMuted(stream) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07005108 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01005109 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005110 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005111 }
5112 }
5113 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
5114 outputDesc->mMuteCount[stream]++;
5115 } else {
5116 if (outputDesc->mMuteCount[stream] == 0) {
5117 ALOGV("setStreamMute() unmuting non muted stream!");
5118 return;
5119 }
5120 if (--outputDesc->mMuteCount[stream] == 0) {
5121 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005122 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005123 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005124 device,
5125 delayMs);
5126 }
5127 }
5128}
5129
Eric Laurente0720872014-03-11 09:30:41 -07005130void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07005131 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07005132{
Eric Laurent87ffa392015-05-22 10:32:38 -07005133 if(!hasPrimaryOutput()) {
5134 return;
5135 }
5136
Eric Laurente552edb2014-03-10 17:42:56 -07005137 // if the stream pertains to sonification strategy and we are in call we must
5138 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
5139 // in the device used for phone strategy and play the tone if the selected device does not
5140 // interfere with the device used for phone strategy
5141 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
5142 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07005143 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005144 if ((stream_strategy == STRATEGY_SONIFICATION) ||
5145 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005146 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07005147 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
5148 stream, starting, outputDesc->mDevice, stateChange);
5149 if (outputDesc->mRefCount[stream]) {
5150 int muteCount = 1;
5151 if (stateChange) {
5152 muteCount = outputDesc->mRefCount[stream];
5153 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005154 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005155 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
5156 for (int i = 0; i < muteCount; i++) {
5157 setStreamMute(stream, starting, mPrimaryOutput);
5158 }
5159 } else {
5160 ALOGV("handleIncallSonification() high visibility");
5161 if (outputDesc->device() &
5162 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
5163 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
5164 for (int i = 0; i < muteCount; i++) {
5165 setStreamMute(stream, starting, mPrimaryOutput);
5166 }
5167 }
5168 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005169 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
5170 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07005171 } else {
5172 mpClientInterface->stopTone();
5173 }
5174 }
5175 }
5176 }
5177}
5178
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005179audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
5180{
5181 // flags to stream type mapping
5182 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
5183 return AUDIO_STREAM_ENFORCED_AUDIBLE;
5184 }
5185 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
5186 return AUDIO_STREAM_BLUETOOTH_SCO;
5187 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08005188 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
5189 return AUDIO_STREAM_TTS;
5190 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005191
5192 // usage to stream type mapping
5193 switch (attr->usage) {
5194 case AUDIO_USAGE_MEDIA:
5195 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005196 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5197 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08005198 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5199 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005200 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5201 return AUDIO_STREAM_SYSTEM;
5202 case AUDIO_USAGE_VOICE_COMMUNICATION:
5203 return AUDIO_STREAM_VOICE_CALL;
5204
5205 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5206 return AUDIO_STREAM_DTMF;
5207
5208 case AUDIO_USAGE_ALARM:
5209 return AUDIO_STREAM_ALARM;
5210 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5211 return AUDIO_STREAM_RING;
5212
5213 case AUDIO_USAGE_NOTIFICATION:
5214 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5215 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5216 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5217 case AUDIO_USAGE_NOTIFICATION_EVENT:
5218 return AUDIO_STREAM_NOTIFICATION;
5219
5220 case AUDIO_USAGE_UNKNOWN:
5221 default:
5222 return AUDIO_STREAM_MUSIC;
5223 }
5224}
Eric Laurente83b55d2014-11-14 10:06:21 -08005225
François Gaffie53615e22015-03-19 09:24:12 +01005226bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
5227{
Eric Laurente83b55d2014-11-14 10:06:21 -08005228 // has flags that map to a strategy?
5229 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
5230 return true;
5231 }
5232
5233 // has known usage?
5234 switch (paa->usage) {
5235 case AUDIO_USAGE_UNKNOWN:
5236 case AUDIO_USAGE_MEDIA:
5237 case AUDIO_USAGE_VOICE_COMMUNICATION:
5238 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5239 case AUDIO_USAGE_ALARM:
5240 case AUDIO_USAGE_NOTIFICATION:
5241 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5242 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5243 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5244 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5245 case AUDIO_USAGE_NOTIFICATION_EVENT:
5246 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5247 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5248 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5249 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08005250 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08005251 break;
5252 default:
5253 return false;
5254 }
5255 return true;
5256}
5257
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07005258bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor>& outputDesc,
François Gaffiead3183e2015-03-18 16:55:35 +01005259 routing_strategy strategy, uint32_t inPastMs,
5260 nsecs_t sysTime) const
5261{
5262 if ((sysTime == 0) && (inPastMs != 0)) {
5263 sysTime = systemTime();
5264 }
Eric Laurent794fde22016-03-11 09:50:45 -08005265 for (int i = 0; i < (int)AUDIO_STREAM_FOR_POLICY_CNT; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01005266 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
5267 (NUM_STRATEGIES == strategy)) &&
5268 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
5269 return true;
5270 }
5271 }
5272 return false;
5273}
5274
François Gaffie2110e042015-03-24 08:41:51 +01005275audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
5276{
5277 return mEngine->getForceUse(usage);
5278}
5279
5280bool AudioPolicyManager::isInCall()
5281{
5282 return isStateInCall(mEngine->getPhoneState());
5283}
5284
5285bool AudioPolicyManager::isStateInCall(int state)
5286{
5287 return is_state_in_call(state);
5288}
5289
Eric Laurentd60560a2015-04-10 11:31:20 -07005290void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc)
5291{
5292 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
5293 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
5294 if (sourceDesc->mDevice->equals(deviceDesc)) {
5295 ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle());
5296 stopAudioSource(sourceDesc->getHandle());
5297 }
5298 }
5299
5300 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
5301 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
5302 bool release = false;
5303 for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) {
5304 const struct audio_port_config *source = &patchDesc->mPatch.sources[j];
5305 if (source->type == AUDIO_PORT_TYPE_DEVICE &&
5306 source->ext.device.type == deviceDesc->type()) {
5307 release = true;
5308 }
5309 }
5310 for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) {
5311 const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j];
5312 if (sink->type == AUDIO_PORT_TYPE_DEVICE &&
5313 sink->ext.device.type == deviceDesc->type()) {
5314 release = true;
5315 }
5316 }
5317 if (release) {
5318 ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle);
5319 releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid);
5320 }
5321 }
5322}
5323
Phil Burk09bc4612016-02-24 15:58:15 -08005324// Modify the list of surround sound formats supported.
Phil Burk0709b0a2016-03-31 12:54:57 -07005325void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) {
5326 FormatVector &formats = *formatsPtr;
Phil Burk07ac1142016-03-25 13:39:29 -07005327 // TODO Set this based on Config properties.
5328 const bool alwaysForceAC3 = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005329
5330 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5331 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
Phil Burk0709b0a2016-03-31 12:54:57 -07005332 ALOGD("%s: forced use = %d", __FUNCTION__, forceUse);
Phil Burk09bc4612016-02-24 15:58:15 -08005333
5334 // Analyze original support for various formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005335 bool supportsAC3 = false;
5336 bool supportsOtherSurround = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005337 bool supportsIEC61937 = false;
5338 for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) {
5339 audio_format_t format = formats[formatIndex];
Phil Burk09bc4612016-02-24 15:58:15 -08005340 switch (format) {
5341 case AUDIO_FORMAT_AC3:
Phil Burk07ac1142016-03-25 13:39:29 -07005342 supportsAC3 = true;
5343 break;
Phil Burk09bc4612016-02-24 15:58:15 -08005344 case AUDIO_FORMAT_E_AC3:
5345 case AUDIO_FORMAT_DTS:
5346 case AUDIO_FORMAT_DTS_HD:
Phil Burk07ac1142016-03-25 13:39:29 -07005347 supportsOtherSurround = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005348 break;
5349 case AUDIO_FORMAT_IEC61937:
5350 supportsIEC61937 = true;
5351 break;
5352 default:
5353 break;
5354 }
5355 }
Phil Burk09bc4612016-02-24 15:58:15 -08005356
5357 // Modify formats based on surround preferences.
5358 // If NEVER, remove support for surround formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005359 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5360 if (supportsAC3 || supportsOtherSurround || supportsIEC61937) {
5361 // Remove surround sound related formats.
5362 for (size_t formatIndex = 0; formatIndex < formats.size(); ) {
5363 audio_format_t format = formats[formatIndex];
5364 switch(format) {
5365 case AUDIO_FORMAT_AC3:
5366 case AUDIO_FORMAT_E_AC3:
5367 case AUDIO_FORMAT_DTS:
5368 case AUDIO_FORMAT_DTS_HD:
5369 case AUDIO_FORMAT_IEC61937:
Phil Burk07ac1142016-03-25 13:39:29 -07005370 formats.removeAt(formatIndex);
5371 break;
5372 default:
5373 formatIndex++; // keep it
5374 break;
5375 }
Phil Burk09bc4612016-02-24 15:58:15 -08005376 }
Phil Burk07ac1142016-03-25 13:39:29 -07005377 supportsAC3 = false;
5378 supportsOtherSurround = false;
5379 supportsIEC61937 = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005380 }
Phil Burk07ac1142016-03-25 13:39:29 -07005381 } else { // AUTO or ALWAYS
5382 // Most TVs support AC3 even if they do not report it in the EDID.
5383 if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS))
5384 && !supportsAC3) {
5385 formats.add(AUDIO_FORMAT_AC3);
5386 supportsAC3 = true;
5387 }
5388
5389 // If ALWAYS, add support for raw surround formats if all are missing.
5390 // This assumes that if any of these formats are reported by the HAL
5391 // then the report is valid and should not be modified.
5392 if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)
5393 && !supportsOtherSurround) {
5394 formats.add(AUDIO_FORMAT_E_AC3);
5395 formats.add(AUDIO_FORMAT_DTS);
5396 formats.add(AUDIO_FORMAT_DTS_HD);
5397 supportsOtherSurround = true;
5398 }
5399
5400 // Add support for IEC61937 if any raw surround supported.
5401 // The HAL could do this but add it here, just in case.
5402 if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) {
5403 formats.add(AUDIO_FORMAT_IEC61937);
5404 supportsIEC61937 = true;
5405 }
Phil Burk09bc4612016-02-24 15:58:15 -08005406 }
Phil Burk0709b0a2016-03-31 12:54:57 -07005407}
5408
5409// Modify the list of channel masks supported.
5410void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) {
5411 ChannelsVector &channelMasks = *channelMasksPtr;
5412 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5413 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
5414
5415 // If NEVER, then remove support for channelMasks > stereo.
5416 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5417 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) {
5418 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5419 if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) {
5420 ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask);
5421 channelMasks.removeAt(maskIndex);
5422 } else {
5423 maskIndex++;
5424 }
5425 }
5426 // If ALWAYS, then make sure we at least support 5.1
5427 } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) {
5428 bool supports5dot1 = false;
5429 // Are there any channel masks that can be considered "surround"?
5430 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); maskIndex++) {
5431 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5432 if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) {
5433 supports5dot1 = true;
5434 break;
5435 }
5436 }
5437 // If not then add 5.1 support.
5438 if (!supports5dot1) {
5439 channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1);
5440 ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__);
5441 }
Phil Burk09bc4612016-02-24 15:58:15 -08005442 }
5443}
5444
Phil Burk00eeb322016-03-31 12:41:00 -07005445void AudioPolicyManager::updateAudioProfiles(audio_devices_t device,
5446 audio_io_handle_t ioHandle,
François Gaffie112b0af2015-11-19 16:13:25 +01005447 AudioProfileVector &profiles)
5448{
5449 String8 reply;
Phil Burk0709b0a2016-03-31 12:54:57 -07005450
François Gaffie112b0af2015-11-19 16:13:25 +01005451 // Format MUST be checked first to update the list of AudioProfile
5452 if (profiles.hasDynamicFormat()) {
5453 reply = mpClientInterface->getParameters(ioHandle,
5454 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
Phil Burk0709b0a2016-03-31 12:54:57 -07005455 ALOGV("%s: supported formats %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005456 AudioParameter repliedParameters(reply);
5457 if (repliedParameters.get(
5458 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS), reply) != NO_ERROR) {
François Gaffie112b0af2015-11-19 16:13:25 +01005459 ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__);
5460 return;
5461 }
Phil Burk09bc4612016-02-24 15:58:15 -08005462 FormatVector formats = formatsFromString(reply.string());
Phil Burk00eeb322016-03-31 12:41:00 -07005463 if (device == AUDIO_DEVICE_OUT_HDMI) {
Phil Burk0709b0a2016-03-31 12:54:57 -07005464 filterSurroundFormats(&formats);
Phil Burk00eeb322016-03-31 12:41:00 -07005465 }
Phil Burk09bc4612016-02-24 15:58:15 -08005466 profiles.setFormats(formats);
François Gaffie112b0af2015-11-19 16:13:25 +01005467 }
5468 const FormatVector &supportedFormats = profiles.getSupportedFormats();
5469
Eric Laurent20eb3a42016-01-26 18:39:17 -08005470 for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) {
François Gaffie112b0af2015-11-19 16:13:25 +01005471 audio_format_t format = supportedFormats[formatIndex];
Eric Laurent20eb3a42016-01-26 18:39:17 -08005472 ChannelsVector channelMasks;
5473 SampleRateVector samplingRates;
François Gaffie112b0af2015-11-19 16:13:25 +01005474 AudioParameter requestedParameters;
5475 requestedParameters.addInt(String8(AUDIO_PARAMETER_STREAM_FORMAT), format);
5476
5477 if (profiles.hasDynamicRateFor(format)) {
5478 reply = mpClientInterface->getParameters(ioHandle,
5479 requestedParameters.toString() + ";" +
5480 AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES);
5481 ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005482 AudioParameter repliedParameters(reply);
5483 if (repliedParameters.get(
5484 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES), reply) == NO_ERROR) {
5485 samplingRates = samplingRatesFromString(reply.string());
François Gaffie112b0af2015-11-19 16:13:25 +01005486 }
5487 }
5488 if (profiles.hasDynamicChannelsFor(format)) {
5489 reply = mpClientInterface->getParameters(ioHandle,
5490 requestedParameters.toString() + ";" +
5491 AUDIO_PARAMETER_STREAM_SUP_CHANNELS);
5492 ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005493 AudioParameter repliedParameters(reply);
5494 if (repliedParameters.get(
5495 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS), reply) == NO_ERROR) {
5496 channelMasks = channelMasksFromString(reply.string());
Phil Burk0709b0a2016-03-31 12:54:57 -07005497 if (device == AUDIO_DEVICE_OUT_HDMI) {
5498 filterSurroundChannelMasks(&channelMasks);
5499 }
François Gaffie112b0af2015-11-19 16:13:25 +01005500 }
5501 }
Eric Laurent20eb3a42016-01-26 18:39:17 -08005502 profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates));
François Gaffie112b0af2015-11-19 16:13:25 +01005503 }
5504}
Eric Laurentd60560a2015-04-10 11:31:20 -07005505
Eric Laurente552edb2014-03-10 17:42:56 -07005506}; // namespace android