blob: a8bdf86100fceda19451c1b540b7af18a5e0e82f [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
Eric Laurent57de36c2016-09-28 16:59:11 -07001329 uint32_t delayMs = outputDesc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -07001330 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001331 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001332 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001333 desc->isActive() &&
1334 outputDesc->sharesHwModuleWith(desc) &&
1335 (newDevice != desc->device())) {
Haynes Mathew George11c499a2016-08-26 12:08:25 -07001336 audio_devices_t newDevice2 = getNewOutputDevice(desc, false /*fromCache*/);
1337 bool force = desc->device() != newDevice2;
Eric Laurentc75307b2015-03-17 15:29:32 -07001338 setOutputDevice(desc,
Haynes Mathew George11c499a2016-08-26 12:08:25 -07001339 newDevice2,
1340 force,
Eric Laurent57de36c2016-09-28 16:59:11 -07001341 delayMs);
1342 // re-apply device specific volume if not done by setOutputDevice()
1343 if (!force) {
1344 applyStreamVolumes(desc, newDevice2, delayMs);
1345 }
Eric Laurente552edb2014-03-10 17:42:56 -07001346 }
1347 }
1348 // update the outputs if stopping one with a stream that can affect notification routing
1349 handleNotificationRoutingForStream(stream);
1350 }
1351 return NO_ERROR;
1352 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001353 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001354 return INVALID_OPERATION;
1355 }
1356}
1357
Eric Laurente83b55d2014-11-14 10:06:21 -08001358void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001359 audio_stream_type_t stream __unused,
1360 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001361{
1362 ALOGV("releaseOutput() %d", output);
1363 ssize_t index = mOutputs.indexOfKey(output);
1364 if (index < 0) {
1365 ALOGW("releaseOutput() releasing unknown output %d", output);
1366 return;
1367 }
1368
1369#ifdef AUDIO_POLICY_TEST
1370 int testIndex = testOutputIndex(output);
1371 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001372 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001373 if (outputDesc->isActive()) {
1374 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001375 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001376 mTestOutputs[testIndex] = 0;
1377 }
1378 return;
1379 }
1380#endif //AUDIO_POLICY_TEST
1381
Paul McLeanaa981192015-03-21 09:55:15 -07001382 // Routing
1383 mOutputRoutes.removeRoute(session);
1384
Eric Laurentc75307b2015-03-17 15:29:32 -07001385 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001386 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001387 if (desc->mDirectOpenCount <= 0) {
1388 ALOGW("releaseOutput() invalid open count %d for output %d",
1389 desc->mDirectOpenCount, output);
1390 return;
1391 }
1392 if (--desc->mDirectOpenCount == 0) {
1393 closeOutput(output);
1394 // If effects where present on the output, audioflinger moved them to the primary
1395 // output by default: move them back to the appropriate output.
1396 audio_io_handle_t dstOutput = getOutputForEffect();
Eric Laurent87ffa392015-05-22 10:32:38 -07001397 if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001398 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
1399 mPrimaryOutput->mIoHandle, dstOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07001400 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001401 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001402 }
1403 }
1404}
1405
1406
Eric Laurentcaf7f482014-11-25 17:50:47 -08001407status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1408 audio_io_handle_t *input,
1409 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001410 uid_t uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001411 uint32_t samplingRate,
1412 audio_format_t format,
1413 audio_channel_mask_t channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001414 audio_input_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -06001415 audio_port_handle_t selectedDeviceId,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001416 input_type_t *inputType)
Eric Laurente552edb2014-03-10 17:42:56 -07001417{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001418 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1419 "session %d, flags %#x",
1420 attr->source, samplingRate, format, channelMask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001421
Eric Laurentcaf7f482014-11-25 17:50:47 -08001422 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001423 *inputType = API_INPUT_INVALID;
Eric Laurent275e8e92014-11-30 15:14:47 -08001424 audio_devices_t device;
1425 // handle legacy remote submix case where the address was not always specified
1426 String8 address = String8("");
Eric Laurentc447ded2015-01-06 08:47:05 -08001427 audio_source_t inputSource = attr->source;
1428 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001429 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001430
Eric Laurentc447ded2015-01-06 08:47:05 -08001431 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1432 inputSource = AUDIO_SOURCE_MIC;
1433 }
1434 halInputSource = inputSource;
1435
Paul McLean466dc8e2015-04-17 13:15:36 -06001436 // Explicit routing?
1437 sp<DeviceDescriptor> deviceDesc;
1438 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1439 if (mAvailableInputDevices[i]->getId() == selectedDeviceId) {
1440 deviceDesc = mAvailableInputDevices[i];
1441 break;
1442 }
1443 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001444 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001445
Eric Laurentc447ded2015-01-06 08:47:05 -08001446 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001447 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001448 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
François Gaffie036e1e92015-03-19 10:16:24 +01001449 if (ret != NO_ERROR) {
1450 return ret;
1451 }
1452 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001453 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1454 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001455 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001456 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001457 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001458 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001459 return BAD_VALUE;
1460 }
Eric Laurentc722f302014-12-10 11:21:49 -08001461 if (policyMix != NULL) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001462 address = policyMix->mDeviceAddress;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001463 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1464 // there is an external policy, but this input is attached to a mix of recorders,
1465 // meaning it receives audio injected into the framework, so the recorder doesn't
1466 // know about it and is therefore considered "legacy"
1467 *inputType = API_INPUT_LEGACY;
1468 } else {
1469 // recording a mix of players defined by an external policy, we're rerouting for
1470 // an external policy
1471 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1472 }
Eric Laurentc722f302014-12-10 11:21:49 -08001473 } else if (audio_is_remote_submix_device(device)) {
1474 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001475 *inputType = API_INPUT_MIX_CAPTURE;
Eric Laurent82db2692015-08-07 13:59:42 -07001476 } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
1477 *inputType = API_INPUT_TELEPHONY_RX;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001478 } else {
1479 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001480 }
Ravi Kumar Alamandab367f5b2015-08-25 08:21:37 -07001481
Eric Laurent599c7582015-12-07 18:05:55 -08001482 }
1483
1484 *input = getInputForDevice(device, address, session, uid, inputSource,
1485 samplingRate, format, channelMask, flags,
1486 policyMix);
1487 if (*input == AUDIO_IO_HANDLE_NONE) {
1488 mInputRoutes.removeRoute(session);
1489 return INVALID_OPERATION;
1490 }
1491 ALOGV("getInputForAttr() returns input type = %d", *inputType);
1492 return NO_ERROR;
1493}
1494
1495
1496audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device,
1497 String8 address,
1498 audio_session_t session,
1499 uid_t uid,
1500 audio_source_t inputSource,
1501 uint32_t samplingRate,
1502 audio_format_t format,
1503 audio_channel_mask_t channelMask,
1504 audio_input_flags_t flags,
1505 AudioMix *policyMix)
1506{
1507 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
1508 audio_source_t halInputSource = inputSource;
1509 bool isSoundTrigger = false;
1510
1511 if (inputSource == AUDIO_SOURCE_HOTWORD) {
1512 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1513 if (index >= 0) {
1514 input = mSoundTriggerSessions.valueFor(session);
1515 isSoundTrigger = true;
1516 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1517 ALOGV("SoundTrigger capture on session %d input %d", session, input);
1518 } else {
1519 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001520 }
1521 }
1522
Andy Hungf129b032015-04-07 13:45:50 -07001523 // find a compatible input profile (not necessarily identical in parameters)
1524 sp<IOProfile> profile;
1525 // samplingRate and flags may be updated by getInputProfile
Glenn Kasten05ddca52016-02-11 08:17:12 -08001526 uint32_t profileSamplingRate = (samplingRate == 0) ? SAMPLE_RATE_HZ_DEFAULT : samplingRate;
Andy Hungf129b032015-04-07 13:45:50 -07001527 audio_format_t profileFormat = format;
1528 audio_channel_mask_t profileChannelMask = channelMask;
1529 audio_input_flags_t profileFlags = flags;
1530 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001531 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001532 profileSamplingRate, profileFormat, profileChannelMask,
1533 profileFlags);
1534 if (profile != 0) {
1535 break; // success
Eric Laurent05067782016-06-01 18:27:28 -07001536 } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) {
1537 profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry
Andy Hungf129b032015-04-07 13:45:50 -07001538 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1539 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1540 } else { // fail
Eric Laurent599c7582015-12-07 18:05:55 -08001541 ALOGW("getInputForDevice() could not find profile for device 0x%X,"
1542 "samplingRate %u, format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001543 device, samplingRate, format, channelMask, flags);
Eric Laurent599c7582015-12-07 18:05:55 -08001544 return input;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001545 }
Eric Laurente552edb2014-03-10 17:42:56 -07001546 }
Glenn Kasten05ddca52016-02-11 08:17:12 -08001547 // Pick input sampling rate if not specified by client
1548 if (samplingRate == 0) {
1549 samplingRate = profileSamplingRate;
1550 }
Eric Laurente552edb2014-03-10 17:42:56 -07001551
Eric Laurent322b4d22015-04-03 15:57:54 -07001552 if (profile->getModuleHandle() == 0) {
1553 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurent599c7582015-12-07 18:05:55 -08001554 return input;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001555 }
1556
Eric Laurent599c7582015-12-07 18:05:55 -08001557 sp<AudioSession> audioSession = new AudioSession(session,
1558 inputSource,
1559 format,
1560 samplingRate,
1561 channelMask,
1562 flags,
1563 uid,
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001564 isSoundTrigger,
1565 policyMix, mpClientInterface);
Eric Laurent599c7582015-12-07 18:05:55 -08001566
Eric Laurent232f26f2016-02-17 18:06:27 -08001567// TODO enable input reuse
1568#if 0
Eric Laurent599c7582015-12-07 18:05:55 -08001569 // reuse an open input if possible
1570 for (size_t i = 0; i < mInputs.size(); i++) {
1571 sp<AudioInputDescriptor> desc = mInputs.valueAt(i);
Eric Laurent232f26f2016-02-17 18:06:27 -08001572 // reuse input if it shares the same profile and same sound trigger attribute
1573 if (profile == desc->mProfile &&
1574 isSoundTrigger == desc->isSoundTrigger()) {
Eric Laurent599c7582015-12-07 18:05:55 -08001575
1576 sp<AudioSession> as = desc->getAudioSession(session);
1577 if (as != 0) {
1578 // do not allow unmatching properties on same session
1579 if (as->matches(audioSession)) {
1580 as->changeOpenCount(1);
1581 } else {
1582 ALOGW("getInputForDevice() record with different attributes"
1583 " exists for session %d", session);
Eric Laurent232f26f2016-02-17 18:06:27 -08001584 return input;
Eric Laurent599c7582015-12-07 18:05:55 -08001585 }
1586 } else {
1587 desc->addAudioSession(session, audioSession);
1588 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001589 ALOGV("getInputForDevice() reusing input %d", mInputs.keyAt(i));
1590 return mInputs.keyAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08001591 }
1592 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001593#endif
Eric Laurent599c7582015-12-07 18:05:55 -08001594
Eric Laurentcf2c0212014-07-25 16:20:43 -07001595 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001596 config.sample_rate = profileSamplingRate;
1597 config.channel_mask = profileChannelMask;
1598 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001599
Eric Laurent322b4d22015-04-03 15:57:54 -07001600 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurent599c7582015-12-07 18:05:55 -08001601 &input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001602 &config,
1603 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001604 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001605 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001606 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001607
1608 // only accept input with the exact requested set of parameters
Eric Laurent599c7582015-12-07 18:05:55 -08001609 if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001610 (profileSamplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -08001611 !audio_formats_match(profileFormat, config.format) ||
Andy Hungf129b032015-04-07 13:45:50 -07001612 (profileChannelMask != config.channel_mask)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001613 ALOGW("getInputForAttr() failed opening input: samplingRate %d"
1614 ", format %d, channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001615 samplingRate, format, channelMask);
Eric Laurent599c7582015-12-07 18:05:55 -08001616 if (input != AUDIO_IO_HANDLE_NONE) {
1617 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001618 }
Eric Laurent599c7582015-12-07 18:05:55 -08001619 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001620 }
1621
Eric Laurent1f2f2232014-06-02 12:01:23 -07001622 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Andy Hungf129b032015-04-07 13:45:50 -07001623 inputDesc->mSamplingRate = profileSamplingRate;
1624 inputDesc->mFormat = profileFormat;
1625 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001626 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001627 inputDesc->mPolicyMix = policyMix;
Eric Laurent599c7582015-12-07 18:05:55 -08001628 inputDesc->addAudioSession(session, audioSession);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001629
Eric Laurent599c7582015-12-07 18:05:55 -08001630 addInput(input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001631 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001632
Eric Laurent599c7582015-12-07 18:05:55 -08001633 return input;
Eric Laurente552edb2014-03-10 17:42:56 -07001634}
1635
Eric Laurent4dc68062014-07-28 17:26:49 -07001636status_t AudioPolicyManager::startInput(audio_io_handle_t input,
Eric Laurent232f26f2016-02-17 18:06:27 -08001637 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001638{
1639 ALOGV("startInput() input %d", input);
1640 ssize_t index = mInputs.indexOfKey(input);
1641 if (index < 0) {
1642 ALOGW("startInput() unknown input %d", input);
1643 return BAD_VALUE;
1644 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001645 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001646
Eric Laurent599c7582015-12-07 18:05:55 -08001647 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1648 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07001649 ALOGW("startInput() unknown session %d on input %d", session, input);
1650 return BAD_VALUE;
1651 }
1652
Eric Laurent232f26f2016-02-17 18:06:27 -08001653 // virtual input devices are compatible with other input devices
1654 if (!is_virtual_input_device(inputDesc->mDevice)) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001655
Eric Laurent232f26f2016-02-17 18:06:27 -08001656 // for a non-virtual input device, check if there is another (non-virtual) active input
1657 audio_io_handle_t activeInput = mInputs.getActiveInput();
1658 if (activeInput != 0 && activeInput != input) {
Eric Laurente552edb2014-03-10 17:42:56 -07001659
Eric Laurent232f26f2016-02-17 18:06:27 -08001660 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1661 // otherwise the active input continues and the new input cannot be started.
1662 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
1663 if ((activeDesc->inputSource() == AUDIO_SOURCE_HOTWORD) &&
1664 !activeDesc->hasPreemptedSession(session)) {
1665 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
1666 //FIXME: consider all active sessions
1667 AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions();
1668 audio_session_t activeSession = activeSessions.keyAt(0);
1669 SortedVector<audio_session_t> sessions =
1670 activeDesc->getPreemptedSessions();
1671 sessions.add(activeSession);
1672 inputDesc->setPreemptedSessions(sessions);
1673 stopInput(activeInput, activeSession);
1674 releaseInput(activeInput, activeSession);
1675 } else {
1676 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
1677 return INVALID_OPERATION;
1678 }
1679 }
1680
1681 // Do not allow capture if an active voice call is using a software patch and
1682 // the call TX source device is on the same HW module.
1683 // FIXME: would be better to refine to only inputs whose profile connects to the
1684 // call TX device but this information is not in the audio patch
1685 if (mCallTxPatch != 0 &&
1686 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1687 return INVALID_OPERATION;
1688 }
1689 }
Eric Laurent313d1e72016-01-29 09:56:57 -08001690
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001691 // Routing?
1692 mInputRoutes.incRouteActivity(session);
1693
Eric Laurent232f26f2016-02-17 18:06:27 -08001694 if (!inputDesc->isActive() || mInputRoutes.hasRouteChanged(session)) {
1695 // if input maps to a dynamic policy with an activity listener, notify of state change
1696 if ((inputDesc->mPolicyMix != NULL)
1697 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001698 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
Eric Laurent232f26f2016-02-17 18:06:27 -08001699 MIX_STATE_MIXING);
1700 }
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001701
Eric Laurent271a93e2016-09-29 14:47:06 -07001702 // indicate active capture to sound trigger service if starting capture from a mic on
1703 // primary HW module
1704 audio_devices_t device = getNewInputDevice(input);
1705 audio_devices_t primaryInputDevices = availablePrimaryInputDevices();
1706 if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) &&
1707 mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) {
Eric Laurent232f26f2016-02-17 18:06:27 -08001708 SoundTrigger::setCaptureState(true);
1709 }
Eric Laurent271a93e2016-09-29 14:47:06 -07001710 setInputDevice(input, device, true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001711
Eric Laurent232f26f2016-02-17 18:06:27 -08001712 // automatically enable the remote submix output when input is started if not
1713 // used by a policy mix of type MIX_TYPE_RECORDERS
1714 // For remote submix (a virtual device), we open only one input per capture request.
1715 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1716 String8 address = String8("");
1717 if (inputDesc->mPolicyMix == NULL) {
1718 address = String8("0");
1719 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001720 address = inputDesc->mPolicyMix->mDeviceAddress;
Eric Laurentc722f302014-12-10 11:21:49 -08001721 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001722 if (address != "") {
1723 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1724 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1725 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001726 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001727 }
Eric Laurente552edb2014-03-10 17:42:56 -07001728 }
1729
Eric Laurent599c7582015-12-07 18:05:55 -08001730 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Eric Laurente552edb2014-03-10 17:42:56 -07001731
Eric Laurent232f26f2016-02-17 18:06:27 -08001732 audioSession->changeActiveCount(1);
Eric Laurente552edb2014-03-10 17:42:56 -07001733 return NO_ERROR;
1734}
1735
Eric Laurent4dc68062014-07-28 17:26:49 -07001736status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1737 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001738{
1739 ALOGV("stopInput() input %d", input);
1740 ssize_t index = mInputs.indexOfKey(input);
1741 if (index < 0) {
1742 ALOGW("stopInput() unknown input %d", input);
1743 return BAD_VALUE;
1744 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001745 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001746
Eric Laurent599c7582015-12-07 18:05:55 -08001747 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001748 if (index < 0) {
1749 ALOGW("stopInput() unknown session %d on input %d", session, input);
1750 return BAD_VALUE;
1751 }
1752
Eric Laurent599c7582015-12-07 18:05:55 -08001753 if (audioSession->activeCount() == 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07001754 ALOGW("stopInput() input %d already stopped", input);
1755 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001756 }
1757
Eric Laurent599c7582015-12-07 18:05:55 -08001758 audioSession->changeActiveCount(-1);
Paul McLean466dc8e2015-04-17 13:15:36 -06001759
1760 // Routing?
1761 mInputRoutes.decRouteActivity(session);
1762
Eric Laurent232f26f2016-02-17 18:06:27 -08001763 if (!inputDesc->isActive()) {
1764 // if input maps to a dynamic policy with an activity listener, notify of state change
1765 if ((inputDesc->mPolicyMix != NULL)
1766 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001767 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
Eric Laurent232f26f2016-02-17 18:06:27 -08001768 MIX_STATE_IDLE);
Eric Laurent84332aa2016-01-28 22:19:18 +00001769 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001770
1771 // automatically disable the remote submix output when input is stopped if not
1772 // used by a policy mix of type MIX_TYPE_RECORDERS
1773 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1774 String8 address = String8("");
1775 if (inputDesc->mPolicyMix == NULL) {
1776 address = String8("0");
1777 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001778 address = inputDesc->mPolicyMix->mDeviceAddress;
Eric Laurent232f26f2016-02-17 18:06:27 -08001779 }
1780 if (address != "") {
1781 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1782 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1783 address, "remote-submix");
1784 }
1785 }
1786
Eric Laurent271a93e2016-09-29 14:47:06 -07001787 audio_devices_t device = inputDesc->mDevice;
Eric Laurent232f26f2016-02-17 18:06:27 -08001788 resetInputDevice(input);
1789
Eric Laurent271a93e2016-09-29 14:47:06 -07001790 // indicate inactive capture to sound trigger service if stopping capture from a mic on
1791 // primary HW module
1792 audio_devices_t primaryInputDevices = availablePrimaryInputDevices();
1793 if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) &&
1794 mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) {
Eric Laurent232f26f2016-02-17 18:06:27 -08001795 SoundTrigger::setCaptureState(false);
1796 }
1797 inputDesc->clearPreemptedSessions();
Eric Laurente552edb2014-03-10 17:42:56 -07001798 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001799 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001800}
1801
Eric Laurent4dc68062014-07-28 17:26:49 -07001802void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1803 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001804{
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001805
Eric Laurente552edb2014-03-10 17:42:56 -07001806 ALOGV("releaseInput() %d", input);
1807 ssize_t index = mInputs.indexOfKey(input);
1808 if (index < 0) {
1809 ALOGW("releaseInput() releasing unknown input %d", input);
1810 return;
1811 }
Paul McLean466dc8e2015-04-17 13:15:36 -06001812
1813 // Routing
1814 mInputRoutes.removeRoute(session);
1815
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001816 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1817 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001818
Eric Laurent599c7582015-12-07 18:05:55 -08001819 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001820 if (index < 0) {
1821 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1822 return;
1823 }
Eric Laurent599c7582015-12-07 18:05:55 -08001824
1825 if (audioSession->openCount() == 0) {
1826 ALOGW("releaseInput() invalid open count %d on session %d",
1827 audioSession->openCount(), session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001828 return;
1829 }
Eric Laurent599c7582015-12-07 18:05:55 -08001830
1831 if (audioSession->changeOpenCount(-1) == 0) {
1832 inputDesc->removeAudioSession(session);
1833 }
1834
Jean-Michel Trivi65bfe912015-12-04 15:56:47 -08001835 if (inputDesc->getOpenRefCount() > 0) {
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001836 ALOGV("releaseInput() exit > 0");
1837 return;
1838 }
1839
Eric Laurent05b90f82014-08-27 15:32:29 -07001840 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001841 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001842 ALOGV("releaseInput() exit");
1843}
1844
Eric Laurentd4692962014-05-05 18:13:44 -07001845void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001846 bool patchRemoved = false;
1847
Eric Laurentd4692962014-05-05 18:13:44 -07001848 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001849 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001850 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07001851 if (patch_index >= 0) {
1852 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07001853 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07001854 mAudioPatches.removeItemsAt(patch_index);
1855 patchRemoved = true;
1856 }
Eric Laurentd4692962014-05-05 18:13:44 -07001857 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1858 }
1859 mInputs.clear();
Chris Thornton52785f32016-02-09 17:28:49 -08001860 SoundTrigger::setCaptureState(false);
Eric Laurent6a94d692014-05-20 11:18:06 -07001861 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001862
1863 if (patchRemoved) {
1864 mpClientInterface->onAudioPatchListUpdate();
1865 }
Eric Laurentd4692962014-05-05 18:13:44 -07001866}
1867
Eric Laurente0720872014-03-11 09:30:41 -07001868void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001869 int indexMin,
1870 int indexMax)
1871{
1872 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffied1ab2bd2015-12-02 18:20:06 +01001873 mVolumeCurves->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent28d09f02016-03-08 10:43:05 -08001874
1875 // initialize other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08001876 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1877 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001878 continue;
1879 }
1880 mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001881 }
Eric Laurente552edb2014-03-10 17:42:56 -07001882}
1883
Eric Laurente0720872014-03-11 09:30:41 -07001884status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01001885 int index,
1886 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07001887{
1888
François Gaffied1ab2bd2015-12-02 18:20:06 +01001889 if ((index < mVolumeCurves->getVolumeIndexMin(stream)) ||
1890 (index > mVolumeCurves->getVolumeIndexMax(stream))) {
Eric Laurente552edb2014-03-10 17:42:56 -07001891 return BAD_VALUE;
1892 }
1893 if (!audio_is_output_device(device)) {
1894 return BAD_VALUE;
1895 }
1896
1897 // Force max volume if stream cannot be muted
François Gaffied1ab2bd2015-12-02 18:20:06 +01001898 if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001899
Eric Laurent1fd372e2016-04-06 14:23:53 -07001900 ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d",
Eric Laurente552edb2014-03-10 17:42:56 -07001901 stream, device, index);
1902
Eric Laurent28d09f02016-03-08 10:43:05 -08001903 // update other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08001904 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1905 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001906 continue;
1907 }
1908 mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index);
1909 }
Eric Laurente552edb2014-03-10 17:42:56 -07001910
Eric Laurent1fd372e2016-04-06 14:23:53 -07001911 // update volume on all outputs and streams matching the following:
1912 // - The requested stream (or a stream matching for volume control) is active on the output
1913 // - The device (or devices) selected by the strategy corresponding to this stream includes
1914 // the requested device
1915 // - For non default requested device, currently selected device on the output is either the
1916 // requested device or one of the devices selected by the strategy
Eric Laurent5a2b6292016-04-14 18:05:57 -07001917 // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if
1918 // no specific device volume value exists for currently selected device.
Eric Laurente552edb2014-03-10 17:42:56 -07001919 status_t status = NO_ERROR;
1920 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001921 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1922 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent794fde22016-03-11 09:50:45 -08001923 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1924 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001925 continue;
Eric Laurente552edb2014-03-10 17:42:56 -07001926 }
Eric Laurentd3926fe2016-04-15 18:10:07 -07001927 if (!(desc->isStreamActive((audio_stream_type_t)curStream) ||
1928 (isInCall() && (curStream == AUDIO_STREAM_VOICE_CALL)))) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07001929 continue;
1930 }
Eric Laurent794fde22016-03-11 09:50:45 -08001931 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurente76f29c2016-09-14 17:17:53 -07001932 audio_devices_t curStreamDevice = getDeviceForStrategy(curStrategy, false /*fromCache*/);
1933 if ((device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) &&
1934 ((curStreamDevice & device) == 0)) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07001935 continue;
1936 }
Eric Laurente76f29c2016-09-14 17:17:53 -07001937 bool applyVolume;
Eric Laurent5a2b6292016-04-14 18:05:57 -07001938 if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07001939 curStreamDevice |= device;
Eric Laurente76f29c2016-09-14 17:17:53 -07001940 applyVolume = (curDevice & curStreamDevice) != 0;
1941 } else {
1942 applyVolume = !mVolumeCurves->hasVolumeIndexForDevice(
1943 stream, Volume::getDeviceForVolume(curStreamDevice));
Eric Laurent1fd372e2016-04-06 14:23:53 -07001944 }
Eric Laurent28d09f02016-03-08 10:43:05 -08001945
Eric Laurente76f29c2016-09-14 17:17:53 -07001946 if (applyVolume) {
Eric Laurentdc462862016-07-19 12:29:53 -07001947 //FIXME: workaround for truncated touch sounds
1948 // delayed volume change for system stream to be removed when the problem is
1949 // handled by system UI
Eric Laurent28d09f02016-03-08 10:43:05 -08001950 status_t volStatus =
Eric Laurentdc462862016-07-19 12:29:53 -07001951 checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice,
1952 (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0);
Eric Laurent28d09f02016-03-08 10:43:05 -08001953 if (volStatus != NO_ERROR) {
1954 status = volStatus;
1955 }
1956 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001957 }
Eric Laurente552edb2014-03-10 17:42:56 -07001958 }
1959 return status;
1960}
1961
Eric Laurente0720872014-03-11 09:30:41 -07001962status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001963 int *index,
1964 audio_devices_t device)
1965{
1966 if (index == NULL) {
1967 return BAD_VALUE;
1968 }
1969 if (!audio_is_output_device(device)) {
1970 return BAD_VALUE;
1971 }
Eric Laurent5a2b6292016-04-14 18:05:57 -07001972 // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device corresponding to
Eric Laurente552edb2014-03-10 17:42:56 -07001973 // the strategy the stream belongs to.
Eric Laurent5a2b6292016-04-14 18:05:57 -07001974 if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurente552edb2014-03-10 17:42:56 -07001975 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1976 }
François Gaffiedfd74092015-03-19 12:10:59 +01001977 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001978
François Gaffied1ab2bd2015-12-02 18:20:06 +01001979 *index = mVolumeCurves->getVolumeIndex(stream, device);
Eric Laurente552edb2014-03-10 17:42:56 -07001980 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1981 return NO_ERROR;
1982}
1983
Eric Laurente0720872014-03-11 09:30:41 -07001984audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001985 const SortedVector<audio_io_handle_t>& outputs)
1986{
1987 // select one output among several suitable for global effects.
1988 // The priority is as follows:
1989 // 1: An offloaded output. If the effect ends up not being offloadable,
1990 // AudioFlinger will invalidate the track and the offloaded output
1991 // will be closed causing the effect to be moved to a PCM output.
1992 // 2: A deep buffer output
1993 // 3: the first output in the list
1994
1995 if (outputs.size() == 0) {
1996 return 0;
1997 }
1998
1999 audio_io_handle_t outputOffloaded = 0;
2000 audio_io_handle_t outputDeepBuffer = 0;
2001
2002 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002003 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07002004 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07002005 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
2006 outputOffloaded = outputs[i];
2007 }
2008 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
2009 outputDeepBuffer = outputs[i];
2010 }
2011 }
2012
2013 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
2014 outputOffloaded, outputDeepBuffer);
2015 if (outputOffloaded != 0) {
2016 return outputOffloaded;
2017 }
2018 if (outputDeepBuffer != 0) {
2019 return outputDeepBuffer;
2020 }
2021
2022 return outputs[0];
2023}
2024
Eric Laurente0720872014-03-11 09:30:41 -07002025audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07002026{
2027 // apply simple rule where global effects are attached to the same output as MUSIC streams
2028
Eric Laurent3b73df72014-03-11 09:06:29 -07002029 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07002030 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2031 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
2032
2033 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
2034 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
2035 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
2036
2037 return output;
2038}
2039
Eric Laurente0720872014-03-11 09:30:41 -07002040status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07002041 audio_io_handle_t io,
2042 uint32_t strategy,
2043 int session,
2044 int id)
2045{
2046 ssize_t index = mOutputs.indexOfKey(io);
2047 if (index < 0) {
2048 index = mInputs.indexOfKey(io);
2049 if (index < 0) {
2050 ALOGW("registerEffect() unknown io %d", io);
2051 return INVALID_OPERATION;
2052 }
2053 }
François Gaffie45ed3b02015-03-19 10:35:14 +01002054 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07002055}
2056
Eric Laurentc75307b2015-03-17 15:29:32 -07002057bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
2058{
Eric Laurent28d09f02016-03-08 10:43:05 -08002059 bool active = false;
Eric Laurent794fde22016-03-11 09:50:45 -08002060 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) {
2061 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002062 continue;
2063 }
2064 active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs);
2065 }
Eric Laurent28d09f02016-03-08 10:43:05 -08002066 return active;
Eric Laurentc75307b2015-03-17 15:29:32 -07002067}
2068
2069bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
2070{
2071 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
2072}
2073
Eric Laurente0720872014-03-11 09:30:41 -07002074bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07002075{
2076 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002077 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002078 if (inputDescriptor->isSourceActive(source)) {
Eric Laurente552edb2014-03-10 17:42:56 -07002079 return true;
2080 }
2081 }
2082 return false;
2083}
2084
Eric Laurent275e8e92014-11-30 15:14:47 -08002085// Register a list of custom mixes with their attributes and format.
2086// When a mix is registered, corresponding input and output profiles are
2087// added to the remote submix hw module. The profile contains only the
2088// parameters (sampling rate, format...) specified by the mix.
2089// The corresponding input remote submix device is also connected.
2090//
2091// When a remote submix device is connected, the address is checked to select the
2092// appropriate profile and the corresponding input or output stream is opened.
2093//
2094// When capture starts, getInputForAttr() will:
2095// - 1 look for a mix matching the address passed in attribtutes tags if any
2096// - 2 if none found, getDeviceForInputSource() will:
2097// - 2.1 look for a mix matching the attributes source
2098// - 2.2 if none found, default to device selection by policy rules
2099// At this time, the corresponding output remote submix device is also connected
2100// and active playback use cases can be transferred to this mix if needed when reconnecting
2101// after AudioTracks are invalidated
2102//
2103// When playback starts, getOutputForAttr() will:
2104// - 1 look for a mix matching the address passed in attribtutes tags if any
2105// - 2 if none found, look for a mix matching the attributes usage
2106// - 3 if none found, default to device and output selection by policy rules.
2107
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002108status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes)
Eric Laurent275e8e92014-11-30 15:14:47 -08002109{
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002110 ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size());
2111 status_t res = NO_ERROR;
2112
2113 sp<HwModule> rSubmixModule;
2114 // examine each mix's route type
2115 for (size_t i = 0; i < mixes.size(); i++) {
2116 // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination
2117 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) {
2118 res = INVALID_OPERATION;
Eric Laurent275e8e92014-11-30 15:14:47 -08002119 break;
2120 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002121 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
2122 // Loop back through "remote submix"
2123 if (rSubmixModule == 0) {
2124 for (size_t j = 0; i < mHwModules.size(); j++) {
2125 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2126 && mHwModules[j]->mHandle != 0) {
2127 rSubmixModule = mHwModules[j];
2128 break;
2129 }
2130 }
2131 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002132
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002133 ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size());
Eric Laurent275e8e92014-11-30 15:14:47 -08002134
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002135 if (rSubmixModule == 0) {
2136 ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", i);
2137 res = INVALID_OPERATION;
2138 break;
2139 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002140
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002141 String8 address = mixes[i].mDeviceAddress;
François Gaffie036e1e92015-03-19 10:16:24 +01002142
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002143 if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) {
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002144 ALOGE(" Error registering mix %zu for address %s", i, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002145 res = INVALID_OPERATION;
2146 break;
2147 }
2148 audio_config_t outputConfig = mixes[i].mFormat;
2149 audio_config_t inputConfig = mixes[i].mFormat;
2150 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
2151 // stereo and let audio flinger do the channel conversion if needed.
2152 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2153 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
2154 rSubmixModule->addOutputProfile(address, &outputConfig,
2155 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
2156 rSubmixModule->addInputProfile(address, &inputConfig,
2157 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01002158
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002159 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
2160 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2161 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2162 address.string(), "remote-submix");
2163 } else {
2164 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2165 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2166 address.string(), "remote-submix");
2167 }
2168 } else if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002169 String8 address = mixes[i].mDeviceAddress;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002170 audio_devices_t device = mixes[i].mDeviceType;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002171 ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s",
2172 i, mixes.size(), device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002173
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002174 bool foundOutput = false;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002175 for (size_t j = 0 ; j < mOutputs.size() ; j++) {
2176 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j);
2177 sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle());
2178 if ((patch != 0) && (patch->mPatch.num_sinks != 0)
2179 && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE)
2180 && (patch->mPatch.sinks[0].ext.device.type == device)
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002181 && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(),
2182 AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002183 if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) {
2184 res = INVALID_OPERATION;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002185 } else {
2186 foundOutput = true;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002187 }
2188 break;
2189 }
2190 }
2191
2192 if (res != NO_ERROR) {
2193 ALOGE(" Error registering mix %zu for device 0x%X addr %s",
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002194 i, device, address.string());
2195 res = INVALID_OPERATION;
2196 break;
2197 } else if (!foundOutput) {
2198 ALOGE(" Output not found for mix %zu for device 0x%X addr %s",
2199 i, device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002200 res = INVALID_OPERATION;
2201 break;
2202 }
Eric Laurentc722f302014-12-10 11:21:49 -08002203 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002204 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002205 if (res != NO_ERROR) {
2206 unregisterPolicyMixes(mixes);
2207 }
2208 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002209}
2210
2211status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
2212{
Eric Laurent7b279bb2015-12-14 10:18:23 -08002213 ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002214 status_t res = NO_ERROR;
2215 sp<HwModule> rSubmixModule;
2216 // examine each mix's route type
Eric Laurent275e8e92014-11-30 15:14:47 -08002217 for (size_t i = 0; i < mixes.size(); i++) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002218 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
François Gaffie036e1e92015-03-19 10:16:24 +01002219
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002220 if (rSubmixModule == 0) {
2221 for (size_t j = 0; i < mHwModules.size(); j++) {
2222 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2223 && mHwModules[j]->mHandle != 0) {
2224 rSubmixModule = mHwModules[j];
2225 break;
2226 }
2227 }
2228 }
2229 if (rSubmixModule == 0) {
2230 res = INVALID_OPERATION;
2231 continue;
2232 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002233
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002234 String8 address = mixes[i].mDeviceAddress;
Eric Laurent275e8e92014-11-30 15:14:47 -08002235
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002236 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
2237 res = INVALID_OPERATION;
2238 continue;
2239 }
2240
2241 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
2242 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2243 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2244 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2245 address.string(), "remote-submix");
2246 }
2247 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
2248 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2249 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2250 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2251 address.string(), "remote-submix");
2252 }
2253 rSubmixModule->removeOutputProfile(address);
2254 rSubmixModule->removeInputProfile(address);
2255
2256 } if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
2257 if (mPolicyMixes.unregisterMix(mixes[i].mDeviceAddress) != NO_ERROR) {
2258 res = INVALID_OPERATION;
2259 continue;
2260 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002261 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002262 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002263 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002264}
2265
Eric Laurente552edb2014-03-10 17:42:56 -07002266
Eric Laurente0720872014-03-11 09:30:41 -07002267status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07002268{
2269 const size_t SIZE = 256;
2270 char buffer[SIZE];
2271 String8 result;
2272
2273 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
2274 result.append(buffer);
2275
Eric Laurent87ffa392015-05-22 10:32:38 -07002276 snprintf(buffer, SIZE, " Primary Output: %d\n",
2277 hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE);
Eric Laurente552edb2014-03-10 17:42:56 -07002278 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002279 snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -07002280 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002281 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002282 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07002283 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002284 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07002285 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002286 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07002287 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002288 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07002289 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002290 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07002291 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002292 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002293 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002294 result.append(buffer);
Phil Burk09bc4612016-02-24 15:58:15 -08002295 snprintf(buffer, SIZE, " Force use for encoded surround output %d\n",
2296 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND));
2297 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002298 snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available");
2299 result.append(buffer);
Andy Hung2ddee192015-12-18 17:34:44 -08002300 snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off");
2301 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002302
François Gaffie2110e042015-03-24 08:41:51 +01002303 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07002304
François Gaffie112b0af2015-11-19 16:13:25 +01002305 mAvailableOutputDevices.dump(fd, String8("Available output"));
2306 mAvailableInputDevices.dump(fd, String8("Available input"));
François Gaffie53615e22015-03-19 09:24:12 +01002307 mHwModules.dump(fd);
2308 mOutputs.dump(fd);
2309 mInputs.dump(fd);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002310 mVolumeCurves->dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01002311 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002312 mAudioPatches.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07002313
2314 return NO_ERROR;
2315}
2316
2317// This function checks for the parameters which can be offloaded.
2318// This can be enhanced depending on the capability of the DSP and policy
2319// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002320bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002321{
2322 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002323 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002324 offloadInfo.sample_rate, offloadInfo.channel_mask,
2325 offloadInfo.format,
2326 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2327 offloadInfo.has_video);
2328
Andy Hung2ddee192015-12-18 17:34:44 -08002329 if (mMasterMono) {
2330 return false; // no offloading if mono is set.
2331 }
2332
Eric Laurente552edb2014-03-10 17:42:56 -07002333 // Check if offload has been disabled
2334 char propValue[PROPERTY_VALUE_MAX];
2335 if (property_get("audio.offload.disable", propValue, "0")) {
2336 if (atoi(propValue) != 0) {
2337 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2338 return false;
2339 }
2340 }
2341
2342 // Check if stream type is music, then only allow offload as of now.
2343 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2344 {
2345 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2346 return false;
2347 }
2348
2349 //TODO: enable audio offloading with video when ready
Andy Hung08945c42015-05-31 21:36:46 -07002350 const bool allowOffloadWithVideo =
2351 property_get_bool("audio.offload.video", false /* default_value */);
2352 if (offloadInfo.has_video && !allowOffloadWithVideo) {
Eric Laurente552edb2014-03-10 17:42:56 -07002353 ALOGV("isOffloadSupported: has_video == true, returning false");
2354 return false;
2355 }
2356
2357 //If duration is less than minimum value defined in property, return false
2358 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2359 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2360 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2361 return false;
2362 }
2363 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2364 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2365 return false;
2366 }
2367
2368 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2369 // creating an offloaded track and tearing it down immediately after start when audioflinger
2370 // detects there is an active non offloadable effect.
2371 // FIXME: We should check the audio session here but we do not have it in this context.
2372 // This may prevent offloading in rare situations where effects are left active by apps
2373 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002374 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002375 return false;
2376 }
2377
2378 // See if there is a profile to support this.
2379 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002380 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002381 offloadInfo.sample_rate,
2382 offloadInfo.format,
2383 offloadInfo.channel_mask,
2384 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002385 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2386 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002387}
2388
Eric Laurent6a94d692014-05-20 11:18:06 -07002389status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2390 audio_port_type_t type,
2391 unsigned int *num_ports,
2392 struct audio_port *ports,
2393 unsigned int *generation)
2394{
2395 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2396 generation == NULL) {
2397 return BAD_VALUE;
2398 }
2399 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2400 if (ports == NULL) {
2401 *num_ports = 0;
2402 }
2403
2404 size_t portsWritten = 0;
2405 size_t portsMax = *num_ports;
2406 *num_ports = 0;
2407 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002408 // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB
2409 // as they are used by stub HALs by convention
Eric Laurent6a94d692014-05-20 11:18:06 -07002410 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002411 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
2412 if (mAvailableOutputDevices[i]->type() == AUDIO_DEVICE_OUT_STUB) {
2413 continue;
2414 }
2415 if (portsWritten < portsMax) {
2416 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2417 }
2418 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002419 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002420 }
2421 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002422 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
2423 if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_STUB) {
2424 continue;
2425 }
2426 if (portsWritten < portsMax) {
2427 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2428 }
2429 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002430 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002431 }
2432 }
2433 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2434 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2435 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2436 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2437 }
2438 *num_ports += mInputs.size();
2439 }
2440 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002441 size_t numOutputs = 0;
2442 for (size_t i = 0; i < mOutputs.size(); i++) {
2443 if (!mOutputs[i]->isDuplicated()) {
2444 numOutputs++;
2445 if (portsWritten < portsMax) {
2446 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2447 }
2448 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002449 }
Eric Laurent84c70242014-06-23 08:46:27 -07002450 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002451 }
2452 }
2453 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002454 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002455 return NO_ERROR;
2456}
2457
2458status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2459{
2460 return NO_ERROR;
2461}
2462
Eric Laurent6a94d692014-05-20 11:18:06 -07002463status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2464 audio_patch_handle_t *handle,
2465 uid_t uid)
2466{
2467 ALOGV("createAudioPatch()");
2468
2469 if (handle == NULL || patch == NULL) {
2470 return BAD_VALUE;
2471 }
2472 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2473
Eric Laurent874c42872014-08-08 15:13:39 -07002474 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2475 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2476 return BAD_VALUE;
2477 }
2478 // only one source per audio patch supported for now
2479 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002480 return INVALID_OPERATION;
2481 }
Eric Laurent874c42872014-08-08 15:13:39 -07002482
2483 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002484 return INVALID_OPERATION;
2485 }
Eric Laurent874c42872014-08-08 15:13:39 -07002486 for (size_t i = 0; i < patch->num_sinks; i++) {
2487 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2488 return INVALID_OPERATION;
2489 }
2490 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002491
2492 sp<AudioPatch> patchDesc;
2493 ssize_t index = mAudioPatches.indexOfKey(*handle);
2494
Eric Laurent6a94d692014-05-20 11:18:06 -07002495 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2496 patch->sources[0].role,
2497 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002498#if LOG_NDEBUG == 0
2499 for (size_t i = 0; i < patch->num_sinks; i++) {
Eric Laurent7b279bb2015-12-14 10:18:23 -08002500 ALOGV("createAudioPatch sink %zu: id %d role %d type %d", i, patch->sinks[i].id,
Eric Laurent874c42872014-08-08 15:13:39 -07002501 patch->sinks[i].role,
2502 patch->sinks[i].type);
2503 }
2504#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002505
2506 if (index >= 0) {
2507 patchDesc = mAudioPatches.valueAt(index);
2508 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2509 mUidCached, patchDesc->mUid, uid);
2510 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2511 return INVALID_OPERATION;
2512 }
2513 } else {
Glenn Kastena13cde92016-03-28 15:26:02 -07002514 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07002515 }
2516
2517 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002518 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002519 if (outputDesc == NULL) {
2520 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2521 return BAD_VALUE;
2522 }
Eric Laurent84c70242014-06-23 08:46:27 -07002523 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2524 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002525 if (patchDesc != 0) {
2526 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2527 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2528 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2529 return BAD_VALUE;
2530 }
2531 }
Eric Laurent874c42872014-08-08 15:13:39 -07002532 DeviceVector devices;
2533 for (size_t i = 0; i < patch->num_sinks; i++) {
2534 // Only support mix to devices connection
2535 // TODO add support for mix to mix connection
2536 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2537 ALOGV("createAudioPatch() source mix but sink is not a device");
2538 return INVALID_OPERATION;
2539 }
2540 sp<DeviceDescriptor> devDesc =
2541 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2542 if (devDesc == 0) {
2543 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2544 return BAD_VALUE;
2545 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002546
François Gaffie53615e22015-03-19 09:24:12 +01002547 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002548 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002549 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002550 NULL, // updatedSamplingRate
2551 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002552 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002553 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002554 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002555 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002556 ALOGV("createAudioPatch() profile not supported for device %08x",
2557 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002558 return INVALID_OPERATION;
2559 }
2560 devices.add(devDesc);
2561 }
2562 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002563 return INVALID_OPERATION;
2564 }
Eric Laurent874c42872014-08-08 15:13:39 -07002565
Eric Laurent6a94d692014-05-20 11:18:06 -07002566 // TODO: reconfigure output format and channels here
2567 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002568 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002569 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002570 index = mAudioPatches.indexOfKey(*handle);
2571 if (index >= 0) {
2572 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2573 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2574 }
2575 patchDesc = mAudioPatches.valueAt(index);
2576 patchDesc->mUid = uid;
2577 ALOGV("createAudioPatch() success");
2578 } else {
2579 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2580 return INVALID_OPERATION;
2581 }
2582 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2583 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2584 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002585 // only one sink supported when connecting an input device to a mix
2586 if (patch->num_sinks > 1) {
2587 return INVALID_OPERATION;
2588 }
François Gaffie53615e22015-03-19 09:24:12 +01002589 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002590 if (inputDesc == NULL) {
2591 return BAD_VALUE;
2592 }
2593 if (patchDesc != 0) {
2594 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2595 return BAD_VALUE;
2596 }
2597 }
2598 sp<DeviceDescriptor> devDesc =
2599 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2600 if (devDesc == 0) {
2601 return BAD_VALUE;
2602 }
2603
François Gaffie53615e22015-03-19 09:24:12 +01002604 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002605 devDesc->mAddress,
2606 patch->sinks[0].sample_rate,
2607 NULL, /*updatedSampleRate*/
2608 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002609 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002610 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002611 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002612 // FIXME for the parameter type,
2613 // and the NONE
2614 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002615 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002616 return INVALID_OPERATION;
2617 }
2618 // TODO: reconfigure output format and channels here
2619 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002620 devDesc->type(), inputDesc->mIoHandle);
2621 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002622 index = mAudioPatches.indexOfKey(*handle);
2623 if (index >= 0) {
2624 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2625 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2626 }
2627 patchDesc = mAudioPatches.valueAt(index);
2628 patchDesc->mUid = uid;
2629 ALOGV("createAudioPatch() success");
2630 } else {
2631 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2632 return INVALID_OPERATION;
2633 }
2634 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2635 // device to device connection
2636 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002637 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002638 return BAD_VALUE;
2639 }
2640 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002641 sp<DeviceDescriptor> srcDeviceDesc =
2642 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002643 if (srcDeviceDesc == 0) {
2644 return BAD_VALUE;
2645 }
Eric Laurent874c42872014-08-08 15:13:39 -07002646
Eric Laurent6a94d692014-05-20 11:18:06 -07002647 //update source and sink with our own data as the data passed in the patch may
2648 // be incomplete.
2649 struct audio_patch newPatch = *patch;
2650 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002651
Eric Laurent874c42872014-08-08 15:13:39 -07002652 for (size_t i = 0; i < patch->num_sinks; i++) {
2653 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2654 ALOGV("createAudioPatch() source device but one sink is not a device");
2655 return INVALID_OPERATION;
2656 }
2657
2658 sp<DeviceDescriptor> sinkDeviceDesc =
2659 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2660 if (sinkDeviceDesc == 0) {
2661 return BAD_VALUE;
2662 }
2663 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2664
Eric Laurent3bcf8592015-04-03 12:13:24 -07002665 // create a software bridge in PatchPanel if:
2666 // - source and sink devices are on differnt HW modules OR
2667 // - audio HAL version is < 3.0
Eric Laurent232f26f2016-02-17 18:06:27 -08002668 if ((srcDeviceDesc->getModuleHandle() != sinkDeviceDesc->getModuleHandle()) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01002669 (srcDeviceDesc->mModule->getHalVersion() < AUDIO_DEVICE_API_VERSION_3_0)) {
Eric Laurent3bcf8592015-04-03 12:13:24 -07002670 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002671 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002672 return INVALID_OPERATION;
2673 }
Eric Laurent874c42872014-08-08 15:13:39 -07002674 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002675 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002676 // if the sink device is reachable via an opened output stream, request to go via
2677 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002678 audio_io_handle_t output = selectOutput(outputs,
2679 AUDIO_OUTPUT_FLAG_NONE,
2680 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002681 if (output != AUDIO_IO_HANDLE_NONE) {
2682 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2683 if (outputDesc->isDuplicated()) {
2684 return INVALID_OPERATION;
2685 }
2686 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002687 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002688 newPatch.num_sources = 2;
2689 }
Eric Laurent83b88082014-06-20 18:31:16 -07002690 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002691 }
2692 // TODO: check from routing capabilities in config file and other conflicting patches
2693
2694 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2695 if (index >= 0) {
2696 afPatchHandle = patchDesc->mAfPatchHandle;
2697 }
2698
2699 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2700 &afPatchHandle,
2701 0);
2702 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2703 status, afPatchHandle);
2704 if (status == NO_ERROR) {
2705 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002706 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002707 addAudioPatch(patchDesc->mHandle, patchDesc);
2708 } else {
2709 patchDesc->mPatch = newPatch;
2710 }
2711 patchDesc->mAfPatchHandle = afPatchHandle;
2712 *handle = patchDesc->mHandle;
2713 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002714 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002715 } else {
2716 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2717 status);
2718 return INVALID_OPERATION;
2719 }
2720 } else {
2721 return BAD_VALUE;
2722 }
2723 } else {
2724 return BAD_VALUE;
2725 }
2726 return NO_ERROR;
2727}
2728
2729status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2730 uid_t uid)
2731{
2732 ALOGV("releaseAudioPatch() patch %d", handle);
2733
2734 ssize_t index = mAudioPatches.indexOfKey(handle);
2735
2736 if (index < 0) {
2737 return BAD_VALUE;
2738 }
2739 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2740 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2741 mUidCached, patchDesc->mUid, uid);
2742 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2743 return INVALID_OPERATION;
2744 }
2745
2746 struct audio_patch *patch = &patchDesc->mPatch;
2747 patchDesc->mUid = mUidCached;
2748 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002749 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002750 if (outputDesc == NULL) {
2751 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2752 return BAD_VALUE;
2753 }
2754
Eric Laurentc75307b2015-03-17 15:29:32 -07002755 setOutputDevice(outputDesc,
2756 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002757 true,
2758 0,
2759 NULL);
2760 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2761 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002762 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002763 if (inputDesc == NULL) {
2764 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2765 return BAD_VALUE;
2766 }
2767 setInputDevice(inputDesc->mIoHandle,
Eric Laurent232f26f2016-02-17 18:06:27 -08002768 getNewInputDevice(inputDesc->mIoHandle),
Eric Laurent6a94d692014-05-20 11:18:06 -07002769 true,
2770 NULL);
2771 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002772 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2773 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2774 status, patchDesc->mAfPatchHandle);
2775 removeAudioPatch(patchDesc->mHandle);
2776 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002777 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002778 } else {
2779 return BAD_VALUE;
2780 }
2781 } else {
2782 return BAD_VALUE;
2783 }
2784 return NO_ERROR;
2785}
2786
2787status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2788 struct audio_patch *patches,
2789 unsigned int *generation)
2790{
François Gaffie53615e22015-03-19 09:24:12 +01002791 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002792 return BAD_VALUE;
2793 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002794 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01002795 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002796}
2797
Eric Laurente1715a42014-05-20 11:30:42 -07002798status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002799{
Eric Laurente1715a42014-05-20 11:30:42 -07002800 ALOGV("setAudioPortConfig()");
2801
2802 if (config == NULL) {
2803 return BAD_VALUE;
2804 }
2805 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2806 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002807 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2808 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002809 }
2810
Eric Laurenta121f902014-06-03 13:32:54 -07002811 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002812 if (config->type == AUDIO_PORT_TYPE_MIX) {
2813 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002814 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002815 if (outputDesc == NULL) {
2816 return BAD_VALUE;
2817 }
Eric Laurent84c70242014-06-23 08:46:27 -07002818 ALOG_ASSERT(!outputDesc->isDuplicated(),
2819 "setAudioPortConfig() called on duplicated output %d",
2820 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002821 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002822 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01002823 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002824 if (inputDesc == NULL) {
2825 return BAD_VALUE;
2826 }
Eric Laurenta121f902014-06-03 13:32:54 -07002827 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002828 } else {
2829 return BAD_VALUE;
2830 }
2831 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2832 sp<DeviceDescriptor> deviceDesc;
2833 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2834 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2835 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2836 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2837 } else {
2838 return BAD_VALUE;
2839 }
2840 if (deviceDesc == NULL) {
2841 return BAD_VALUE;
2842 }
Eric Laurenta121f902014-06-03 13:32:54 -07002843 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002844 } else {
2845 return BAD_VALUE;
2846 }
2847
Eric Laurenta121f902014-06-03 13:32:54 -07002848 struct audio_port_config backupConfig;
2849 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2850 if (status == NO_ERROR) {
2851 struct audio_port_config newConfig;
2852 audioPortConfig->toAudioPortConfig(&newConfig, config);
2853 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002854 }
Eric Laurenta121f902014-06-03 13:32:54 -07002855 if (status != NO_ERROR) {
2856 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002857 }
Eric Laurente1715a42014-05-20 11:30:42 -07002858
2859 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002860}
2861
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002862void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
2863{
Eric Laurentd60560a2015-04-10 11:31:20 -07002864 clearAudioSources(uid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002865 clearAudioPatches(uid);
2866 clearSessionRoutes(uid);
2867}
2868
Eric Laurent6a94d692014-05-20 11:18:06 -07002869void AudioPolicyManager::clearAudioPatches(uid_t uid)
2870{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002871 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002872 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2873 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002874 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002875 }
2876 }
2877}
2878
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002879void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
2880 audio_io_handle_t ouptutToSkip)
2881{
2882 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2883 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
2884 for (size_t j = 0; j < mOutputs.size(); j++) {
2885 if (mOutputs.keyAt(j) == ouptutToSkip) {
2886 continue;
2887 }
2888 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
2889 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
2890 continue;
2891 }
2892 // If the default device for this strategy is on another output mix,
2893 // invalidate all tracks in this strategy to force re connection.
2894 // Otherwise select new device on the output mix.
2895 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
Eric Laurent794fde22016-03-11 09:50:45 -08002896 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002897 if (getStrategy((audio_stream_type_t)stream) == strategy) {
2898 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
2899 }
2900 }
2901 } else {
2902 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
2903 setOutputDevice(outputDesc, newDevice, false);
2904 }
2905 }
2906}
2907
2908void AudioPolicyManager::clearSessionRoutes(uid_t uid)
2909{
2910 // remove output routes associated with this uid
2911 SortedVector<routing_strategy> affectedStrategies;
2912 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
2913 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
2914 if (route->mUid == uid) {
2915 mOutputRoutes.removeItemsAt(i);
2916 if (route->mDeviceDescriptor != 0) {
2917 affectedStrategies.add(getStrategy(route->mStreamType));
2918 }
2919 }
2920 }
2921 // reroute outputs if necessary
2922 for (size_t i = 0; i < affectedStrategies.size(); i++) {
2923 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
2924 }
2925
2926 // remove input routes associated with this uid
2927 SortedVector<audio_source_t> affectedSources;
2928 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
2929 sp<SessionRoute> route = mInputRoutes.valueAt(i);
2930 if (route->mUid == uid) {
2931 mInputRoutes.removeItemsAt(i);
2932 if (route->mDeviceDescriptor != 0) {
2933 affectedSources.add(route->mSource);
2934 }
2935 }
2936 }
2937 // reroute inputs if necessary
2938 SortedVector<audio_io_handle_t> inputsToClose;
2939 for (size_t i = 0; i < mInputs.size(); i++) {
2940 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002941 if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002942 inputsToClose.add(inputDesc->mIoHandle);
2943 }
2944 }
2945 for (size_t i = 0; i < inputsToClose.size(); i++) {
2946 closeInput(inputsToClose[i]);
2947 }
2948}
2949
Eric Laurentd60560a2015-04-10 11:31:20 -07002950void AudioPolicyManager::clearAudioSources(uid_t uid)
2951{
2952 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
2953 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
2954 if (sourceDesc->mUid == uid) {
2955 stopAudioSource(mAudioSources.keyAt(i));
2956 }
2957 }
2958}
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002959
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002960status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2961 audio_io_handle_t *ioHandle,
2962 audio_devices_t *device)
2963{
Glenn Kasteneeecb982016-02-26 10:44:04 -08002964 *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
2965 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002966 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002967
François Gaffiedf372692015-03-19 10:43:27 +01002968 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002969}
2970
Eric Laurentd60560a2015-04-10 11:31:20 -07002971status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source,
2972 const audio_attributes_t *attributes,
2973 audio_io_handle_t *handle,
2974 uid_t uid)
Eric Laurent554a2772015-04-10 11:29:24 -07002975{
Eric Laurentd60560a2015-04-10 11:31:20 -07002976 ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle);
2977 if (source == NULL || attributes == NULL || handle == NULL) {
2978 return BAD_VALUE;
2979 }
2980
2981 *handle = AUDIO_IO_HANDLE_NONE;
2982
2983 if (source->role != AUDIO_PORT_ROLE_SOURCE ||
2984 source->type != AUDIO_PORT_TYPE_DEVICE) {
2985 ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type);
2986 return INVALID_OPERATION;
2987 }
2988
2989 sp<DeviceDescriptor> srcDeviceDesc =
2990 mAvailableInputDevices.getDevice(source->ext.device.type,
2991 String8(source->ext.device.address));
2992 if (srcDeviceDesc == 0) {
2993 ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type);
2994 return BAD_VALUE;
2995 }
2996 sp<AudioSourceDescriptor> sourceDesc =
2997 new AudioSourceDescriptor(srcDeviceDesc, attributes, uid);
2998
2999 struct audio_patch dummyPatch;
3000 sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid);
3001 sourceDesc->mPatchDesc = patchDesc;
3002
3003 status_t status = connectAudioSource(sourceDesc);
3004 if (status == NO_ERROR) {
3005 mAudioSources.add(sourceDesc->getHandle(), sourceDesc);
3006 *handle = sourceDesc->getHandle();
3007 }
3008 return status;
3009}
3010
3011status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3012{
3013 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3014
3015 // make sure we only have one patch per source.
3016 disconnectAudioSource(sourceDesc);
3017
3018 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
Eric Laurent28d09f02016-03-08 10:43:05 -08003019 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003020 sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice;
3021
3022 audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true);
3023 sp<DeviceDescriptor> sinkDeviceDesc =
3024 mAvailableOutputDevices.getDevice(sinkDevice, String8(""));
3025
3026 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3027 struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch;
3028
3029 if (srcDeviceDesc->getAudioPort()->mModule->getHandle() ==
3030 sinkDeviceDesc->getAudioPort()->mModule->getHandle() &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003031 srcDeviceDesc->getAudioPort()->mModule->getHalVersion() >= AUDIO_DEVICE_API_VERSION_3_0 &&
Eric Laurentd60560a2015-04-10 11:31:20 -07003032 srcDeviceDesc->getAudioPort()->mGains.size() > 0) {
3033 ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__);
3034 // create patch between src device and output device
3035 // create Hwoutput and add to mHwOutputs
3036 } else {
3037 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs);
3038 audio_io_handle_t output =
3039 selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID);
3040 if (output == AUDIO_IO_HANDLE_NONE) {
3041 ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice);
3042 return INVALID_OPERATION;
3043 }
3044 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
3045 if (outputDesc->isDuplicated()) {
3046 ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice);
3047 return INVALID_OPERATION;
3048 }
3049 // create a special patch with no sink and two sources:
3050 // - the second source indicates to PatchPanel through which output mix this patch should
3051 // be connected as well as the stream type for volume control
3052 // - the sink is defined by whatever output device is currently selected for the output
3053 // though which this patch is routed.
3054 patch->num_sinks = 0;
3055 patch->num_sources = 2;
3056 srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL);
3057 outputDesc->toAudioPortConfig(&patch->sources[1], NULL);
3058 patch->sources[1].ext.mix.usecase.stream = stream;
3059 status_t status = mpClientInterface->createAudioPatch(patch,
3060 &afPatchHandle,
3061 0);
3062 ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__,
3063 status, afPatchHandle);
3064 if (status != NO_ERROR) {
3065 ALOGW("%s patch panel could not connect device patch, error %d",
3066 __FUNCTION__, status);
3067 return INVALID_OPERATION;
3068 }
3069 uint32_t delayMs = 0;
Eric Laurentc40d9692016-04-13 19:14:13 -07003070 status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs);
Eric Laurentd60560a2015-04-10 11:31:20 -07003071
3072 if (status != NO_ERROR) {
3073 mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0);
3074 return status;
3075 }
3076 sourceDesc->mSwOutput = outputDesc;
3077 if (delayMs != 0) {
3078 usleep(delayMs * 1000);
3079 }
3080 }
3081
3082 sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle;
3083 addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc);
3084
3085 return NO_ERROR;
Eric Laurent554a2772015-04-10 11:29:24 -07003086}
3087
Eric Laurent493404d2015-04-21 15:07:36 -07003088status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07003089{
Eric Laurentd60560a2015-04-10 11:31:20 -07003090 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle);
3091 ALOGV("%s handle %d", __FUNCTION__, handle);
3092 if (sourceDesc == 0) {
3093 ALOGW("%s unknown source for handle %d", __FUNCTION__, handle);
3094 return BAD_VALUE;
3095 }
3096 status_t status = disconnectAudioSource(sourceDesc);
3097
3098 mAudioSources.removeItem(handle);
3099 return status;
3100}
3101
Andy Hung2ddee192015-12-18 17:34:44 -08003102status_t AudioPolicyManager::setMasterMono(bool mono)
3103{
3104 if (mMasterMono == mono) {
3105 return NO_ERROR;
3106 }
3107 mMasterMono = mono;
3108 // if enabling mono we close all offloaded devices, which will invalidate the
3109 // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible
3110 // for recreating the new AudioTrack as non-offloaded PCM.
3111 //
3112 // If disabling mono, we leave all tracks as is: we don't know which clients
3113 // and tracks are able to be recreated as offloaded. The next "song" should
3114 // play back offloaded.
3115 if (mMasterMono) {
3116 Vector<audio_io_handle_t> offloaded;
3117 for (size_t i = 0; i < mOutputs.size(); ++i) {
3118 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
3119 if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
3120 offloaded.push(desc->mIoHandle);
3121 }
3122 }
3123 for (size_t i = 0; i < offloaded.size(); ++i) {
3124 closeOutput(offloaded[i]);
3125 }
3126 }
3127 // update master mono for all remaining outputs
3128 for (size_t i = 0; i < mOutputs.size(); ++i) {
3129 updateMono(mOutputs.keyAt(i));
3130 }
3131 return NO_ERROR;
3132}
3133
3134status_t AudioPolicyManager::getMasterMono(bool *mono)
3135{
3136 *mono = mMasterMono;
3137 return NO_ERROR;
3138}
3139
Eric Laurentd60560a2015-04-10 11:31:20 -07003140status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3141{
3142 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3143
3144 sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle);
3145 if (patchDesc == 0) {
3146 ALOGW("%s source has no patch with handle %d", __FUNCTION__,
3147 sourceDesc->mPatchDesc->mHandle);
3148 return BAD_VALUE;
3149 }
3150 removeAudioPatch(sourceDesc->mPatchDesc->mHandle);
3151
Eric Laurent28d09f02016-03-08 10:43:05 -08003152 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003153 sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote();
3154 if (swOutputDesc != 0) {
3155 stopSource(swOutputDesc, stream, false);
3156 mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3157 } else {
3158 sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote();
3159 if (hwOutputDesc != 0) {
3160 // release patch between src device and output device
3161 // close Hwoutput and remove from mHwOutputs
3162 } else {
3163 ALOGW("%s source has neither SW nor HW output", __FUNCTION__);
3164 }
3165 }
3166 return NO_ERROR;
3167}
3168
3169sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput(
3170 audio_io_handle_t output, routing_strategy strategy)
3171{
3172 sp<AudioSourceDescriptor> source;
3173 for (size_t i = 0; i < mAudioSources.size(); i++) {
3174 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3175 routing_strategy sourceStrategy =
3176 (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
3177 sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote();
3178 if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) {
3179 source = sourceDesc;
3180 break;
3181 }
3182 }
3183 return source;
Eric Laurent554a2772015-04-10 11:29:24 -07003184}
3185
Eric Laurente552edb2014-03-10 17:42:56 -07003186// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07003187// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07003188// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07003189uint32_t AudioPolicyManager::nextAudioPortGeneration()
3190{
3191 return android_atomic_inc(&mAudioPortGeneration);
3192}
3193
Eric Laurente0720872014-03-11 09:30:41 -07003194AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07003195 :
3196#ifdef AUDIO_POLICY_TEST
3197 Thread(false),
3198#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07003199 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07003200 mA2dpSuspended(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003201 mAudioPortGeneration(1),
3202 mBeaconMuteRefCount(0),
3203 mBeaconPlayingRefCount(0),
Eric Laurent9459fb02015-08-12 18:36:32 -07003204 mBeaconMuted(false),
Andy Hung2ddee192015-12-18 17:34:44 -08003205 mTtsOutputAvailable(false),
3206 mMasterMono(false)
Eric Laurente552edb2014-03-10 17:42:56 -07003207{
François Gaffied1ab2bd2015-12-02 18:20:06 +01003208 mUidCached = getuid();
3209 mpClientInterface = clientInterface;
3210
3211 // TODO: remove when legacy conf file is removed. true on devices that use DRC on the
3212 // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly.
3213 // Note: remove also speaker_drc_enabled from global configuration of XML config file.
3214 bool speakerDrcEnabled = false;
3215
3216#ifdef USE_XML_AUDIO_POLICY_CONF
3217 mVolumeCurves = new VolumeCurvesCollection();
3218 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3219 mDefaultOutputDevice, speakerDrcEnabled,
3220 static_cast<VolumeCurvesCollection *>(mVolumeCurves));
3221 PolicySerializer serializer;
3222 if (serializer.deserialize(AUDIO_POLICY_XML_CONFIG_FILE, config) != NO_ERROR) {
3223#else
3224 mVolumeCurves = new StreamDescriptorCollection();
3225 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3226 mDefaultOutputDevice, speakerDrcEnabled);
3227 if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) &&
3228 (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) {
3229#endif
3230 ALOGE("could not load audio policy configuration file, setting defaults");
3231 config.setDefault();
3232 }
3233 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
3234 mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled);
3235
3236 // Once policy config has been parsed, retrieve an instance of the engine and initialize it.
François Gaffie2110e042015-03-24 08:41:51 +01003237 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
3238 if (!engineInstance) {
3239 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
3240 return;
3241 }
3242 // Retrieve the Policy Manager Interface
3243 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
3244 if (mEngine == NULL) {
3245 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
3246 return;
3247 }
3248 mEngine->setObserver(this);
3249 status_t status = mEngine->initCheck();
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07003250 (void) status;
François Gaffie2110e042015-03-24 08:41:51 +01003251 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
3252
Eric Laurent3a4311c2014-03-17 12:00:47 -07003253 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07003254 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07003255 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
3256 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07003257 for (size_t i = 0; i < mHwModules.size(); i++) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003258 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003259 if (mHwModules[i]->mHandle == 0) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003260 ALOGW("could not open HW module %s", mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003261 continue;
3262 }
3263 // open all output streams needed to access attached devices
3264 // except for direct output streams that are only opened when they are actually
3265 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07003266 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07003267 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3268 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003269 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003270
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003271 if (!outProfile->hasSupportedDevices()) {
3272 ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003273 continue;
3274 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003275 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) {
Eric Laurent9459fb02015-08-12 18:36:32 -07003276 mTtsOutputAvailable = true;
3277 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003278
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003279 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
Eric Laurentd78f1532014-09-16 16:38:20 -07003280 continue;
3281 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003282 audio_devices_t profileType = outProfile->getSupportedDevicesType();
François Gaffie53615e22015-03-19 09:24:12 +01003283 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
3284 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07003285 } else {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003286 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003287 // outputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003288 profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes);
Eric Laurente552edb2014-03-10 17:42:56 -07003289 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003290 if ((profileType & outputDeviceTypes) == 0) {
3291 continue;
3292 }
Eric Laurentc75307b2015-03-17 15:29:32 -07003293 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
3294 mpClientInterface);
Eric Laurentc40d9692016-04-13 19:14:13 -07003295 const DeviceVector &supportedDevices = outProfile->getSupportedDevices();
3296 const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType);
3297 String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress
3298 : String8("");
Eric Laurentd78f1532014-09-16 16:38:20 -07003299
3300 outputDesc->mDevice = profileType;
3301 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3302 config.sample_rate = outputDesc->mSamplingRate;
3303 config.channel_mask = outputDesc->mChannelMask;
3304 config.format = outputDesc->mFormat;
3305 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003306 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003307 &output,
3308 &config,
3309 &outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003310 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003311 &outputDesc->mLatency,
3312 outputDesc->mFlags);
3313
3314 if (status != NO_ERROR) {
3315 ALOGW("Cannot open output stream for device %08x on hw module %s",
3316 outputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003317 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003318 } else {
3319 outputDesc->mSamplingRate = config.sample_rate;
3320 outputDesc->mChannelMask = config.channel_mask;
3321 outputDesc->mFormat = config.format;
3322
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003323 for (size_t k = 0; k < supportedDevices.size(); k++) {
3324 ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003325 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08003326 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
3327 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003328 }
3329 }
3330 if (mPrimaryOutput == 0 &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003331 outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003332 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07003333 }
3334 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003335 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07003336 outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003337 true,
3338 0,
3339 NULL,
3340 address.string());
Eric Laurentd78f1532014-09-16 16:38:20 -07003341 }
Eric Laurente552edb2014-03-10 17:42:56 -07003342 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003343 // open input streams needed to access attached devices to validate
3344 // mAvailableInputDevices list
3345 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
3346 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003347 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003348
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003349 if (!inProfile->hasSupportedDevices()) {
3350 ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003351 continue;
3352 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003353 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003354 // inputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003355 audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes);
3356
Eric Laurentd78f1532014-09-16 16:38:20 -07003357 if ((profileType & inputDeviceTypes) == 0) {
3358 continue;
3359 }
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08003360 sp<AudioInputDescriptor> inputDesc =
3361 new AudioInputDescriptor(inProfile);
Eric Laurentd78f1532014-09-16 16:38:20 -07003362
Eric Laurentd78f1532014-09-16 16:38:20 -07003363 inputDesc->mDevice = profileType;
3364
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003365 // find the address
3366 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
3367 // the inputs vector must be of size 1, but we don't want to crash here
3368 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
3369 : String8("");
3370 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
3371 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
3372
Eric Laurentd78f1532014-09-16 16:38:20 -07003373 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3374 config.sample_rate = inputDesc->mSamplingRate;
3375 config.channel_mask = inputDesc->mChannelMask;
3376 config.format = inputDesc->mFormat;
3377 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003378 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003379 &input,
3380 &config,
3381 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003382 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003383 AUDIO_SOURCE_MIC,
3384 AUDIO_INPUT_FLAG_NONE);
3385
3386 if (status == NO_ERROR) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003387 const DeviceVector &supportedDevices = inProfile->getSupportedDevices();
3388 for (size_t k = 0; k < supportedDevices.size(); k++) {
3389 ssize_t index = mAvailableInputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003390 // give a valid ID to an attached device once confirmed it is reachable
Eric Laurent45aabc32015-08-06 09:11:13 -07003391 if (index >= 0) {
3392 sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index];
3393 if (!devDesc->isAttached()) {
3394 devDesc->attach(mHwModules[i]);
3395 devDesc->importAudioPort(inProfile);
3396 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003397 }
3398 }
3399 mpClientInterface->closeInput(input);
3400 } else {
3401 ALOGW("Cannot open input stream for device %08x on hw module %s",
3402 inputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003403 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003404 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003405 }
3406 }
3407 // make sure all attached devices have been allocated a unique ID
3408 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003409 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003410 ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003411 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
3412 continue;
3413 }
François Gaffie2110e042015-03-24 08:41:51 +01003414 // The device is now validated and can be appended to the available devices of the engine
3415 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
3416 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003417 i++;
3418 }
3419 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003420 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01003421 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003422 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
3423 continue;
3424 }
François Gaffie2110e042015-03-24 08:41:51 +01003425 // The device is now validated and can be appended to the available devices of the engine
3426 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
3427 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003428 i++;
3429 }
3430 // make sure default device is reachable
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003431 if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01003432 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003433 }
Eric Laurente552edb2014-03-10 17:42:56 -07003434
3435 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
3436
3437 updateDevicesAndOutputs();
3438
3439#ifdef AUDIO_POLICY_TEST
3440 if (mPrimaryOutput != 0) {
3441 AudioParameter outputCmd = AudioParameter();
3442 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003443 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07003444
3445 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
3446 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07003447 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
3448 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003449 mTestLatencyMs = 0;
3450 mCurOutput = 0;
3451 mDirectOutput = false;
3452 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3453 mTestOutputs[i] = 0;
3454 }
3455
3456 const size_t SIZE = 256;
3457 char buffer[SIZE];
3458 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
3459 run(buffer, ANDROID_PRIORITY_AUDIO);
3460 }
3461#endif //AUDIO_POLICY_TEST
3462}
3463
Eric Laurente0720872014-03-11 09:30:41 -07003464AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07003465{
3466#ifdef AUDIO_POLICY_TEST
3467 exit();
3468#endif //AUDIO_POLICY_TEST
3469 for (size_t i = 0; i < mOutputs.size(); i++) {
3470 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003471 }
3472 for (size_t i = 0; i < mInputs.size(); i++) {
3473 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003474 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003475 mAvailableOutputDevices.clear();
3476 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003477 mOutputs.clear();
3478 mInputs.clear();
3479 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07003480}
3481
Eric Laurente0720872014-03-11 09:30:41 -07003482status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07003483{
Eric Laurent87ffa392015-05-22 10:32:38 -07003484 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003485}
3486
3487#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07003488bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07003489{
3490 ALOGV("entering threadLoop()");
3491 while (!exitPending())
3492 {
3493 String8 command;
3494 int valueInt;
3495 String8 value;
3496
3497 Mutex::Autolock _l(mLock);
3498 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
3499
3500 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
3501 AudioParameter param = AudioParameter(command);
3502
3503 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
3504 valueInt != 0) {
3505 ALOGV("Test command %s received", command.string());
3506 String8 target;
3507 if (param.get(String8("target"), target) != NO_ERROR) {
3508 target = "Manager";
3509 }
3510 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
3511 param.remove(String8("test_cmd_policy_output"));
3512 mCurOutput = valueInt;
3513 }
3514 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
3515 param.remove(String8("test_cmd_policy_direct"));
3516 if (value == "false") {
3517 mDirectOutput = false;
3518 } else if (value == "true") {
3519 mDirectOutput = true;
3520 }
3521 }
3522 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
3523 param.remove(String8("test_cmd_policy_input"));
3524 mTestInput = valueInt;
3525 }
3526
3527 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
3528 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07003529 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07003530 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003531 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003532 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003533 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003534 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003535 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07003536 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003537 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07003538 if (target == "Manager") {
3539 mTestFormat = format;
3540 } else if (mTestOutputs[mCurOutput] != 0) {
3541 AudioParameter outputParam = AudioParameter();
3542 outputParam.addInt(String8("format"), format);
3543 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3544 }
3545 }
3546 }
3547 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
3548 param.remove(String8("test_cmd_policy_channels"));
3549 int channels = 0;
3550
3551 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003552 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003553 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003554 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07003555 }
3556 if (channels != 0) {
3557 if (target == "Manager") {
3558 mTestChannels = channels;
3559 } else if (mTestOutputs[mCurOutput] != 0) {
3560 AudioParameter outputParam = AudioParameter();
3561 outputParam.addInt(String8("channels"), channels);
3562 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3563 }
3564 }
3565 }
3566 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
3567 param.remove(String8("test_cmd_policy_sampleRate"));
3568 if (valueInt >= 0 && valueInt <= 96000) {
3569 int samplingRate = valueInt;
3570 if (target == "Manager") {
3571 mTestSamplingRate = samplingRate;
3572 } else if (mTestOutputs[mCurOutput] != 0) {
3573 AudioParameter outputParam = AudioParameter();
3574 outputParam.addInt(String8("sampling_rate"), samplingRate);
3575 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3576 }
3577 }
3578 }
3579
3580 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
3581 param.remove(String8("test_cmd_policy_reopen"));
3582
Eric Laurentc75307b2015-03-17 15:29:32 -07003583 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07003584
Eric Laurentc75307b2015-03-17 15:29:32 -07003585 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07003586
Eric Laurentc75307b2015-03-17 15:29:32 -07003587 removeOutput(mPrimaryOutput->mIoHandle);
3588 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
3589 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003590 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003591 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3592 config.sample_rate = outputDesc->mSamplingRate;
3593 config.channel_mask = outputDesc->mChannelMask;
3594 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07003595 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003596 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07003597 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07003598 &config,
3599 &outputDesc->mDevice,
3600 String8(""),
3601 &outputDesc->mLatency,
3602 outputDesc->mFlags);
3603 if (status != NO_ERROR) {
3604 ALOGE("Failed to reopen hardware output stream, "
3605 "samplingRate: %d, format %d, channels %d",
3606 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003607 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003608 outputDesc->mSamplingRate = config.sample_rate;
3609 outputDesc->mChannelMask = config.channel_mask;
3610 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07003611 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07003612 AudioParameter outputCmd = AudioParameter();
3613 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003614 mpClientInterface->setParameters(handle, outputCmd.toString());
3615 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07003616 }
3617 }
3618
3619
3620 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3621 }
3622 }
3623 return false;
3624}
3625
Eric Laurente0720872014-03-11 09:30:41 -07003626void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003627{
3628 {
3629 AutoMutex _l(mLock);
3630 requestExit();
3631 mWaitWorkCV.signal();
3632 }
3633 requestExitAndWait();
3634}
3635
Eric Laurente0720872014-03-11 09:30:41 -07003636int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003637{
3638 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3639 if (output == mTestOutputs[i]) return i;
3640 }
3641 return 0;
3642}
3643#endif //AUDIO_POLICY_TEST
3644
3645// ---
3646
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07003647void AudioPolicyManager::addOutput(audio_io_handle_t output, const sp<SwAudioOutputDescriptor>& outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003648{
François Gaffie98cc1912015-03-18 17:52:40 +01003649 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003650 mOutputs.add(output, outputDesc);
Andy Hung2ddee192015-12-18 17:34:44 -08003651 updateMono(output); // update mono status when adding to output list
Eric Laurent6a94d692014-05-20 11:18:06 -07003652 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003653}
3654
François Gaffie53615e22015-03-19 09:24:12 +01003655void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3656{
3657 mOutputs.removeItem(output);
3658}
3659
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07003660void AudioPolicyManager::addInput(audio_io_handle_t input, const sp<AudioInputDescriptor>& inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003661{
François Gaffie98cc1912015-03-18 17:52:40 +01003662 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003663 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003664 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003665}
Eric Laurente552edb2014-03-10 17:42:56 -07003666
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07003667void AudioPolicyManager::findIoHandlesByAddress(const sp<SwAudioOutputDescriptor>& desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003668 const audio_devices_t device /*in*/,
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07003669 const String8& address /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003670 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003671 sp<DeviceDescriptor> devDesc =
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003672 desc->mProfile->getSupportedDeviceByAddress(device, address);
keunyoung3190e672014-12-30 13:00:52 -08003673 if (devDesc != 0) {
3674 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3675 desc->mIoHandle, address.string());
3676 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003677 }
3678}
3679
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07003680status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003681 audio_policy_dev_state_t state,
3682 SortedVector<audio_io_handle_t>& outputs,
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07003683 const String8& address)
Eric Laurente552edb2014-03-10 17:42:56 -07003684{
François Gaffie53615e22015-03-19 09:24:12 +01003685 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003686 sp<SwAudioOutputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003687
3688 if (audio_device_is_digital(device)) {
3689 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003690 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003691 }
Eric Laurente552edb2014-03-10 17:42:56 -07003692
Eric Laurent3b73df72014-03-11 09:06:29 -07003693 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003694 // first list already open outputs that can be routed to this device
3695 for (size_t i = 0; i < mOutputs.size(); i++) {
3696 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003697 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003698 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003699 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3700 outputs.add(mOutputs.keyAt(i));
3701 } else {
3702 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003703 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003704 }
Eric Laurente552edb2014-03-10 17:42:56 -07003705 }
3706 }
3707 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003708 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003709 for (size_t i = 0; i < mHwModules.size(); i++)
3710 {
3711 if (mHwModules[i]->mHandle == 0) {
3712 continue;
3713 }
3714 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3715 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003716 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003717 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003718 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003719 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003720 profiles.add(profile);
3721 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3722 }
Eric Laurente552edb2014-03-10 17:42:56 -07003723 }
3724 }
3725 }
3726
Eric Laurent7b279bb2015-12-14 10:18:23 -08003727 ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size());
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003728
Eric Laurente552edb2014-03-10 17:42:56 -07003729 if (profiles.isEmpty() && outputs.isEmpty()) {
3730 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3731 return BAD_VALUE;
3732 }
3733
3734 // open outputs for matching profiles if needed. Direct outputs are also opened to
3735 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3736 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003737 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003738
3739 // nothing to do if one output is already opened for this profile
3740 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003741 for (j = 0; j < outputs.size(); j++) {
3742 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003743 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003744 // matching profile: save the sample rates, format and channel masks supported
3745 // by the profile in our device descriptor
Paul McLean9080a4c2015-06-18 08:24:02 -07003746 if (audio_device_is_digital(device)) {
3747 devDesc->importAudioPort(profile);
3748 }
Eric Laurente552edb2014-03-10 17:42:56 -07003749 break;
3750 }
3751 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003752 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003753 continue;
3754 }
3755
Eric Laurent83b88082014-06-20 18:31:16 -07003756 ALOGV("opening output for device %08x with params %s profile %p",
3757 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07003758 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003759 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003760 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3761 config.sample_rate = desc->mSamplingRate;
3762 config.channel_mask = desc->mChannelMask;
3763 config.format = desc->mFormat;
3764 config.offload_info.sample_rate = desc->mSamplingRate;
3765 config.offload_info.channel_mask = desc->mChannelMask;
3766 config.offload_info.format = desc->mFormat;
3767 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003768 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003769 &output,
3770 &config,
3771 &desc->mDevice,
3772 address,
3773 &desc->mLatency,
3774 desc->mFlags);
3775 if (status == NO_ERROR) {
3776 desc->mSamplingRate = config.sample_rate;
3777 desc->mChannelMask = config.channel_mask;
3778 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003779
Eric Laurentd4692962014-05-05 18:13:44 -07003780 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003781 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003782 char *param = audio_device_address_to_parameter(device, address);
3783 mpClientInterface->setParameters(output, String8(param));
3784 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003785 }
Phil Burk00eeb322016-03-31 12:41:00 -07003786 updateAudioProfiles(device, output, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01003787 if (!profile->hasValidAudioProfile()) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003788 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003789 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003790 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003791 } else if (profile->hasDynamicAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07003792 mpClientInterface->closeOutput(output);
Phil Burk702b1052016-03-02 16:38:26 -08003793 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003794 profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003795 config.offload_info.sample_rate = config.sample_rate;
3796 config.offload_info.channel_mask = config.channel_mask;
3797 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07003798 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003799 &output,
3800 &config,
3801 &desc->mDevice,
3802 address,
3803 &desc->mLatency,
3804 desc->mFlags);
3805 if (status == NO_ERROR) {
3806 desc->mSamplingRate = config.sample_rate;
3807 desc->mChannelMask = config.channel_mask;
3808 desc->mFormat = config.format;
3809 } else {
3810 output = AUDIO_IO_HANDLE_NONE;
3811 }
Eric Laurentd4692962014-05-05 18:13:44 -07003812 }
3813
Eric Laurentcf2c0212014-07-25 16:20:43 -07003814 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003815 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003816 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003817 sp<AudioPolicyMix> policyMix;
3818 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003819 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3820 address.string());
3821 }
François Gaffie036e1e92015-03-19 10:16:24 +01003822 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07003823 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01003824
Eric Laurent87ffa392015-05-22 10:32:38 -07003825 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
3826 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08003827 // no duplicated output for direct outputs and
3828 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003829 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003830
Eric Laurentd4692962014-05-05 18:13:44 -07003831 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07003832 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003833
Eric Laurentd4692962014-05-05 18:13:44 -07003834 //TODO: configure audio effect output stage here
3835
3836 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07003837 duplicatedOutput =
3838 mpClientInterface->openDuplicateOutput(output,
3839 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003840 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003841 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07003842 sp<SwAudioOutputDescriptor> dupOutputDesc =
3843 new SwAudioOutputDescriptor(NULL, mpClientInterface);
3844 dupOutputDesc->mOutput1 = mPrimaryOutput;
3845 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003846 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3847 dupOutputDesc->mFormat = desc->mFormat;
3848 dupOutputDesc->mChannelMask = desc->mChannelMask;
3849 dupOutputDesc->mLatency = desc->mLatency;
3850 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003851 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07003852 } else {
3853 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07003854 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07003855 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003856 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003857 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003858 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003859 }
Eric Laurente552edb2014-03-10 17:42:56 -07003860 }
3861 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003862 } else {
3863 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003864 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003865 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003866 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003867 profiles.removeAt(profile_index);
3868 profile_index--;
3869 } else {
3870 outputs.add(output);
Paul McLean9080a4c2015-06-18 08:24:02 -07003871 // Load digital format info only for digital devices
3872 if (audio_device_is_digital(device)) {
3873 devDesc->importAudioPort(profile);
3874 }
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003875
François Gaffie53615e22015-03-19 09:24:12 +01003876 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003877 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3878 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003879 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003880 NULL/*patch handle*/, address.string());
3881 }
Eric Laurente552edb2014-03-10 17:42:56 -07003882 ALOGV("checkOutputsForDevice(): adding output %d", output);
3883 }
3884 }
3885
3886 if (profiles.isEmpty()) {
3887 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3888 return BAD_VALUE;
3889 }
Eric Laurentd4692962014-05-05 18:13:44 -07003890 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003891 // check if one opened output is not needed any more after disconnecting one device
3892 for (size_t i = 0; i < mOutputs.size(); i++) {
3893 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003894 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003895 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01003896 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07003897 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003898 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07003899 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003900 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3901 mOutputs.keyAt(i));
3902 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003903 }
Eric Laurente552edb2014-03-10 17:42:56 -07003904 }
3905 }
Eric Laurentd4692962014-05-05 18:13:44 -07003906 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003907 for (size_t i = 0; i < mHwModules.size(); i++)
3908 {
3909 if (mHwModules[i]->mHandle == 0) {
3910 continue;
3911 }
3912 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3913 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003914 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003915 if (profile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003916 ALOGV("checkOutputsForDevice(): "
3917 "clearing direct output profile %zu on module %zu", j, i);
François Gaffie112b0af2015-11-19 16:13:25 +01003918 profile->clearAudioProfiles();
Eric Laurente552edb2014-03-10 17:42:56 -07003919 }
3920 }
3921 }
3922 }
3923 return NO_ERROR;
3924}
3925
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07003926status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003927 audio_policy_dev_state_t state,
3928 SortedVector<audio_io_handle_t>& inputs,
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07003929 const String8& address)
Eric Laurentd4692962014-05-05 18:13:44 -07003930{
Paul McLean9080a4c2015-06-18 08:24:02 -07003931 audio_devices_t device = devDesc->type();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003932 sp<AudioInputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003933
3934 if (audio_device_is_digital(device)) {
3935 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003936 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003937 }
3938
Eric Laurentd4692962014-05-05 18:13:44 -07003939 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3940 // first list already open inputs that can be routed to this device
3941 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3942 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003943 if (desc->mProfile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003944 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3945 inputs.add(mInputs.keyAt(input_index));
3946 }
3947 }
3948
3949 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003950 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003951 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3952 {
3953 if (mHwModules[module_idx]->mHandle == 0) {
3954 continue;
3955 }
3956 for (size_t profile_index = 0;
3957 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3958 profile_index++)
3959 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003960 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3961
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003962 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003963 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003964 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003965 profiles.add(profile);
3966 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3967 profile_index, module_idx);
3968 }
Eric Laurentd4692962014-05-05 18:13:44 -07003969 }
3970 }
3971 }
3972
3973 if (profiles.isEmpty() && inputs.isEmpty()) {
3974 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3975 return BAD_VALUE;
3976 }
3977
3978 // open inputs for matching profiles if needed. Direct inputs are also opened to
3979 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3980 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3981
Eric Laurent1c333e22014-05-20 10:48:17 -07003982 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003983 // nothing to do if one input is already opened for this profile
3984 size_t input_index;
3985 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3986 desc = mInputs.valueAt(input_index);
3987 if (desc->mProfile == profile) {
Paul McLean9080a4c2015-06-18 08:24:02 -07003988 if (audio_device_is_digital(device)) {
3989 devDesc->importAudioPort(profile);
3990 }
Eric Laurentd4692962014-05-05 18:13:44 -07003991 break;
3992 }
3993 }
3994 if (input_index != mInputs.size()) {
3995 continue;
3996 }
3997
3998 ALOGV("opening input for device 0x%X with params %s", device, address.string());
3999 desc = new AudioInputDescriptor(profile);
4000 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07004001 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
4002 config.sample_rate = desc->mSamplingRate;
4003 config.channel_mask = desc->mChannelMask;
4004 config.format = desc->mFormat;
4005 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07004006 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07004007 &input,
4008 &config,
4009 &desc->mDevice,
4010 address,
4011 AUDIO_SOURCE_MIC,
4012 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07004013
Eric Laurentcf2c0212014-07-25 16:20:43 -07004014 if (status == NO_ERROR) {
4015 desc->mSamplingRate = config.sample_rate;
4016 desc->mChannelMask = config.channel_mask;
4017 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07004018
Eric Laurentd4692962014-05-05 18:13:44 -07004019 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07004020 char *param = audio_device_address_to_parameter(device, address);
4021 mpClientInterface->setParameters(input, String8(param));
4022 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07004023 }
Phil Burk00eeb322016-03-31 12:41:00 -07004024 updateAudioProfiles(device, input, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01004025 if (!profile->hasValidAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07004026 ALOGW("checkInputsForDevice() direct input missing param");
4027 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004028 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07004029 }
4030
4031 if (input != 0) {
4032 addInput(input, desc);
4033 }
4034 } // endif input != 0
4035
Eric Laurentcf2c0212014-07-25 16:20:43 -07004036 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07004037 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07004038 profiles.removeAt(profile_index);
4039 profile_index--;
4040 } else {
4041 inputs.add(input);
Paul McLean9080a4c2015-06-18 08:24:02 -07004042 if (audio_device_is_digital(device)) {
4043 devDesc->importAudioPort(profile);
4044 }
Eric Laurentd4692962014-05-05 18:13:44 -07004045 ALOGV("checkInputsForDevice(): adding input %d", input);
4046 }
4047 } // end scan profiles
4048
4049 if (profiles.isEmpty()) {
4050 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
4051 return BAD_VALUE;
4052 }
4053 } else {
4054 // Disconnect
4055 // check if one opened input is not needed any more after disconnecting one device
4056 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
4057 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004058 if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) {
Eric Laurentd4692962014-05-05 18:13:44 -07004059 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
4060 mInputs.keyAt(input_index));
4061 inputs.add(mInputs.keyAt(input_index));
4062 }
4063 }
4064 // Clear any profiles associated with the disconnected device.
4065 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
4066 if (mHwModules[module_index]->mHandle == 0) {
4067 continue;
4068 }
4069 for (size_t profile_index = 0;
4070 profile_index < mHwModules[module_index]->mInputProfiles.size();
4071 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004072 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004073 if (profile->supportDevice(device)) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07004074 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07004075 profile_index, module_index);
François Gaffie112b0af2015-11-19 16:13:25 +01004076 profile->clearAudioProfiles();
Eric Laurentd4692962014-05-05 18:13:44 -07004077 }
4078 }
4079 }
4080 } // end disconnect
4081
4082 return NO_ERROR;
4083}
4084
4085
Eric Laurente0720872014-03-11 09:30:41 -07004086void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07004087{
4088 ALOGV("closeOutput(%d)", output);
4089
Eric Laurentc75307b2015-03-17 15:29:32 -07004090 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004091 if (outputDesc == NULL) {
4092 ALOGW("closeOutput() unknown output %d", output);
4093 return;
4094 }
François Gaffie036e1e92015-03-19 10:16:24 +01004095 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08004096
Eric Laurente552edb2014-03-10 17:42:56 -07004097 // look for duplicated outputs connected to the output being removed.
4098 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004099 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07004100 if (dupOutputDesc->isDuplicated() &&
4101 (dupOutputDesc->mOutput1 == outputDesc ||
4102 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004103 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07004104 if (dupOutputDesc->mOutput1 == outputDesc) {
4105 outputDesc2 = dupOutputDesc->mOutput2;
4106 } else {
4107 outputDesc2 = dupOutputDesc->mOutput1;
4108 }
4109 // As all active tracks on duplicated output will be deleted,
4110 // and as they were also referenced on the other output, the reference
4111 // count for their stream type must be adjusted accordingly on
4112 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07004113 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07004114 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07004115 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07004116 }
4117 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
4118 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
4119
4120 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01004121 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07004122 }
4123 }
4124
Eric Laurent05b90f82014-08-27 15:32:29 -07004125 nextAudioPortGeneration();
4126
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004127 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004128 if (index >= 0) {
4129 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07004130 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07004131 mAudioPatches.removeItemsAt(index);
4132 mpClientInterface->onAudioPatchListUpdate();
4133 }
4134
Eric Laurente552edb2014-03-10 17:42:56 -07004135 AudioParameter param;
4136 param.add(String8("closing"), String8("true"));
4137 mpClientInterface->setParameters(output, param.toString());
4138
4139 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01004140 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004141 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07004142}
4143
4144void AudioPolicyManager::closeInput(audio_io_handle_t input)
4145{
4146 ALOGV("closeInput(%d)", input);
4147
4148 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
4149 if (inputDesc == NULL) {
4150 ALOGW("closeInput() unknown input %d", input);
4151 return;
4152 }
4153
Eric Laurent6a94d692014-05-20 11:18:06 -07004154 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07004155
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004156 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004157 if (index >= 0) {
4158 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07004159 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07004160 mAudioPatches.removeItemsAt(index);
4161 mpClientInterface->onAudioPatchListUpdate();
4162 }
4163
4164 mpClientInterface->closeInput(input);
4165 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07004166}
4167
Eric Laurentc75307b2015-03-17 15:29:32 -07004168SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
4169 audio_devices_t device,
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07004170 const SwAudioOutputCollection& openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07004171{
4172 SortedVector<audio_io_handle_t> outputs;
4173
4174 ALOGVV("getOutputsForDevice() device %04x", device);
4175 for (size_t i = 0; i < openOutputs.size(); i++) {
4176 ALOGVV("output %d isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004177 i, openOutputs.valueAt(i)->isDuplicated(),
4178 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004179 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
4180 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
4181 outputs.add(openOutputs.keyAt(i));
4182 }
4183 }
4184 return outputs;
4185}
4186
Eric Laurente0720872014-03-11 09:30:41 -07004187bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01004188 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07004189{
4190 if (outputs1.size() != outputs2.size()) {
4191 return false;
4192 }
4193 for (size_t i = 0; i < outputs1.size(); i++) {
4194 if (outputs1[i] != outputs2[i]) {
4195 return false;
4196 }
4197 }
4198 return true;
4199}
4200
Eric Laurente0720872014-03-11 09:30:41 -07004201void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07004202{
4203 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
4204 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
4205 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
4206 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
4207
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004208 // also take into account external policy-related changes: add all outputs which are
4209 // associated with policies in the "before" and "after" output vectors
4210 ALOGVV("checkOutputForStrategy(): policy related outputs");
4211 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004212 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004213 if (desc != 0 && desc->mPolicyMix != NULL) {
4214 srcOutputs.add(desc->mIoHandle);
4215 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
4216 }
4217 }
4218 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004219 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004220 if (desc != 0 && desc->mPolicyMix != NULL) {
4221 dstOutputs.add(desc->mIoHandle);
4222 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
4223 }
4224 }
4225
Eric Laurente552edb2014-03-10 17:42:56 -07004226 if (!vectorsEqual(srcOutputs,dstOutputs)) {
4227 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
4228 strategy, srcOutputs[0], dstOutputs[0]);
4229 // mute strategy while moving tracks from one output to another
4230 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004231 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01004232 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004233 setStrategyMute(strategy, true, desc);
4234 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004235 }
Eric Laurentd60560a2015-04-10 11:31:20 -07004236 sp<AudioSourceDescriptor> source =
4237 getSourceForStrategyOnOutput(srcOutputs[i], strategy);
4238 if (source != 0){
4239 connectAudioSource(source);
4240 }
Eric Laurente552edb2014-03-10 17:42:56 -07004241 }
4242
4243 // Move effects associated to this strategy from previous output to new output
4244 if (strategy == STRATEGY_MEDIA) {
4245 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
4246 SortedVector<audio_io_handle_t> moved;
4247 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004248 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
4249 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
4250 effectDesc->mIo != fxOutput) {
4251 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004252 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
4253 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004254 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07004255 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004256 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07004257 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07004258 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004259 }
4260 }
4261 }
4262 // Move tracks associated to this strategy from previous output to new output
Eric Laurent794fde22016-03-11 09:50:45 -08004263 for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004264 if (getStrategy((audio_stream_type_t)i) == strategy) {
4265 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07004266 }
4267 }
4268 }
4269}
4270
Eric Laurente0720872014-03-11 09:30:41 -07004271void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07004272{
François Gaffie2110e042015-03-24 08:41:51 +01004273 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004274 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004275 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01004276 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004277 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004278 checkOutputForStrategy(STRATEGY_SONIFICATION);
4279 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004280 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07004281 checkOutputForStrategy(STRATEGY_MEDIA);
4282 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004283 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07004284}
4285
Eric Laurente0720872014-03-11 09:30:41 -07004286void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07004287{
François Gaffie53615e22015-03-19 09:24:12 +01004288 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07004289 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004290 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07004291 return;
4292 }
4293
Eric Laurent3a4311c2014-03-17 12:00:47 -07004294 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08004295 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
4296 ~AUDIO_DEVICE_BIT_IN) != 0) ||
4297 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07004298 // suspend A2DP output if:
4299 // (NOT already suspended) &&
4300 // ((SCO device is connected &&
4301 // (forced usage for communication || for record is SCO))) ||
4302 // (phone state is ringing || in call)
4303 //
4304 // restore A2DP output if:
4305 // (Already suspended) &&
4306 // ((SCO device is NOT connected ||
4307 // (forced usage NOT for communication && NOT for record is SCO))) &&
4308 // (phone state is NOT ringing && NOT in call)
4309 //
4310 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004311 if ((!isScoConnected ||
François Gaffie2110e042015-03-24 08:41:51 +01004312 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) &&
4313 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) &&
4314 ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
4315 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004316
4317 mpClientInterface->restoreOutput(a2dpOutput);
4318 mA2dpSuspended = false;
4319 }
4320 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004321 if ((isScoConnected &&
François Gaffie2110e042015-03-24 08:41:51 +01004322 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) ||
4323 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) ||
4324 ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
4325 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004326
4327 mpClientInterface->suspendOutput(a2dpOutput);
4328 mA2dpSuspended = true;
4329 }
4330 }
4331}
4332
Eric Laurentc75307b2015-03-17 15:29:32 -07004333audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
4334 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004335{
4336 audio_devices_t device = AUDIO_DEVICE_NONE;
4337
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004338 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004339 if (index >= 0) {
4340 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4341 if (patchDesc->mUid != mUidCached) {
4342 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004343 outputDesc->device(), outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004344 return outputDesc->device();
4345 }
4346 }
4347
Eric Laurente552edb2014-03-10 17:42:56 -07004348 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05004349 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004350 // use device for strategy enforced audible
4351 // 2: we are in call or the strategy phone is active on the output:
4352 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05004353 // 3: the strategy for enforced audible is active but not enforced on the output:
4354 // use the device for strategy enforced audible
Eric Laurent28d09f02016-03-08 10:43:05 -08004355 // 4: the strategy sonification is active on the output:
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004356 // use device for strategy sonification
Eric Laurent28d09f02016-03-08 10:43:05 -08004357 // 5: the strategy accessibility is active on the output:
4358 // use device for strategy accessibility
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004359 // 6: the strategy "respectful" sonification is active on the output:
4360 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08004361 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004362 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08004363 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004364 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08004365 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004366 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01004367 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004368 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07004369 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
4370 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01004371 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004372 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004373 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05004374 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004375 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004376 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Eric Laurent28d09f02016-03-08 10:43:05 -08004377 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
4378 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004379 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004380 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004381 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004382 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004383 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004384 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004385 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004386 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004387 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004388 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07004389 }
4390
Eric Laurent1c333e22014-05-20 10:48:17 -07004391 ALOGV("getNewOutputDevice() selected device %x", device);
4392 return device;
4393}
4394
Eric Laurent232f26f2016-02-17 18:06:27 -08004395audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input)
Eric Laurent1c333e22014-05-20 10:48:17 -07004396{
Eric Laurent232f26f2016-02-17 18:06:27 -08004397 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004398
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004399 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004400 if (index >= 0) {
4401 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4402 if (patchDesc->mUid != mUidCached) {
4403 ALOGV("getNewInputDevice() device %08x forced by patch %d",
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004404 inputDesc->mDevice, inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004405 return inputDesc->mDevice;
4406 }
4407 }
4408
Eric Laurent232f26f2016-02-17 18:06:27 -08004409 audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->inputSource());
Eric Laurent1c333e22014-05-20 10:48:17 -07004410
Eric Laurente552edb2014-03-10 17:42:56 -07004411 return device;
4412}
4413
Eric Laurent794fde22016-03-11 09:50:45 -08004414bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1,
4415 audio_stream_type_t stream2) {
4416 return ((stream1 == stream2) ||
4417 ((stream1 == AUDIO_STREAM_ACCESSIBILITY) && (stream2 == AUDIO_STREAM_MUSIC)) ||
4418 ((stream1 == AUDIO_STREAM_MUSIC) && (stream2 == AUDIO_STREAM_ACCESSIBILITY)));
Eric Laurent28d09f02016-03-08 10:43:05 -08004419}
4420
Eric Laurente0720872014-03-11 09:30:41 -07004421uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004422 return (uint32_t)getStrategy(stream);
4423}
4424
Eric Laurente0720872014-03-11 09:30:41 -07004425audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004426 // By checking the range of stream before calling getStrategy, we avoid
4427 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
4428 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08004429 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004430 return AUDIO_DEVICE_NONE;
4431 }
Eric Laurent28d09f02016-03-08 10:43:05 -08004432 audio_devices_t devices = AUDIO_DEVICE_NONE;
Eric Laurent794fde22016-03-11 09:50:45 -08004433 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
4434 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004435 continue;
Eric Laurent6a94d692014-05-20 11:18:06 -07004436 }
Eric Laurent794fde22016-03-11 09:50:45 -08004437 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08004438 audio_devices_t curDevices =
Eric Laurent447a87b2016-07-07 17:32:10 -07004439 getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/);
Eric Laurent28d09f02016-03-08 10:43:05 -08004440 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs);
4441 for (size_t i = 0; i < outputs.size(); i++) {
4442 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurent794fde22016-03-11 09:50:45 -08004443 if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004444 curDevices |= outputDesc->device();
4445 }
4446 }
4447 devices |= curDevices;
Eric Laurente552edb2014-03-10 17:42:56 -07004448 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05004449
4450 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
4451 and doesn't really need to.*/
4452 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
4453 devices |= AUDIO_DEVICE_OUT_SPEAKER;
4454 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4455 }
Eric Laurente552edb2014-03-10 17:42:56 -07004456 return devices;
4457}
4458
François Gaffie2110e042015-03-24 08:41:51 +01004459routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
4460{
Eric Laurent223fd5c2014-11-11 13:43:36 -08004461 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01004462 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004463}
4464
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004465uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4466 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004467 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4468 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4469 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004470 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4471 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4472 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004473 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01004474 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004475}
4476
Eric Laurente0720872014-03-11 09:30:41 -07004477void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004478 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004479 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004480 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4481 updateDevicesAndOutputs();
4482 break;
4483 default:
4484 break;
4485 }
4486}
4487
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004488uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
Eric Laurent9459fb02015-08-12 18:36:32 -07004489
4490 // skip beacon mute management if a dedicated TTS output is available
4491 if (mTtsOutputAvailable) {
4492 return 0;
4493 }
4494
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004495 switch(event) {
4496 case STARTING_OUTPUT:
4497 mBeaconMuteRefCount++;
4498 break;
4499 case STOPPING_OUTPUT:
4500 if (mBeaconMuteRefCount > 0) {
4501 mBeaconMuteRefCount--;
4502 }
4503 break;
4504 case STARTING_BEACON:
4505 mBeaconPlayingRefCount++;
4506 break;
4507 case STOPPING_BEACON:
4508 if (mBeaconPlayingRefCount > 0) {
4509 mBeaconPlayingRefCount--;
4510 }
4511 break;
4512 }
4513
4514 if (mBeaconMuteRefCount > 0) {
4515 // any playback causes beacon to be muted
4516 return setBeaconMute(true);
4517 } else {
4518 // no other playback: unmute when beacon starts playing, mute when it stops
4519 return setBeaconMute(mBeaconPlayingRefCount == 0);
4520 }
4521}
4522
4523uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4524 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4525 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4526 // keep track of muted state to avoid repeating mute/unmute operations
4527 if (mBeaconMuted != mute) {
4528 // mute/unmute AUDIO_STREAM_TTS on all outputs
4529 ALOGV("\t muting %d", mute);
4530 uint32_t maxLatency = 0;
4531 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004532 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004533 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004534 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004535 0 /*delay*/, AUDIO_DEVICE_NONE);
4536 const uint32_t latency = desc->latency() * 2;
4537 if (latency > maxLatency) {
4538 maxLatency = latency;
4539 }
4540 }
4541 mBeaconMuted = mute;
4542 return maxLatency;
4543 }
4544 return 0;
4545}
4546
Eric Laurente0720872014-03-11 09:30:41 -07004547audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004548 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004549{
Paul McLeanaa981192015-03-21 09:55:15 -07004550 // Routing
4551 // see if we have an explicit route
4552 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4553 // (getStrategy(stream)).
4554 // if the strategy from the stream type in the RouteMap is the same as the argument above,
4555 // and activity count is non-zero
4556 // the device = the device from the descriptor in the RouteMap, and exit.
4557 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4558 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
Eric Laurent28d09f02016-03-08 10:43:05 -08004559 routing_strategy routeStrategy = getStrategy(route->mStreamType);
4560 if ((routeStrategy == strategy) && route->isActive()) {
Paul McLeanaa981192015-03-21 09:55:15 -07004561 return route->mDeviceDescriptor->type();
4562 }
4563 }
4564
Eric Laurente552edb2014-03-10 17:42:56 -07004565 if (fromCache) {
4566 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4567 strategy, mDeviceForStrategy[strategy]);
4568 return mDeviceForStrategy[strategy];
4569 }
François Gaffie2110e042015-03-24 08:41:51 +01004570 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004571}
4572
Eric Laurente0720872014-03-11 09:30:41 -07004573void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004574{
4575 for (int i = 0; i < NUM_STRATEGIES; i++) {
4576 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4577 }
4578 mPreviousOutputs = mOutputs;
4579}
4580
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07004581uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004582 audio_devices_t prevDevice,
4583 uint32_t delayMs)
4584{
4585 // mute/unmute strategies using an incompatible device combination
4586 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4587 // if unmuting, unmute only after the specified delay
4588 if (outputDesc->isDuplicated()) {
4589 return 0;
4590 }
4591
4592 uint32_t muteWaitMs = 0;
4593 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004594 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004595
4596 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4597 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004598 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004599 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4600 bool doMute = false;
4601
4602 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4603 doMute = true;
4604 outputDesc->mStrategyMutedByDevice[i] = true;
4605 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4606 doMute = true;
4607 outputDesc->mStrategyMutedByDevice[i] = false;
4608 }
Eric Laurent99401132014-05-07 19:48:15 -07004609 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004610 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004611 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004612 // skip output if it does not share any device with current output
4613 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4614 == AUDIO_DEVICE_NONE) {
4615 continue;
4616 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004617 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)",
4618 mute ? "muting" : "unmuting", i, curDevice);
4619 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004620 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004621 if (mute) {
4622 // FIXME: should not need to double latency if volume could be applied
4623 // immediately by the audioflinger mixer. We must account for the delay
4624 // between now and the next time the audioflinger thread for this output
4625 // will process a buffer (which corresponds to one buffer size,
4626 // usually 1/2 or 1/4 of the latency).
4627 if (muteWaitMs < desc->latency() * 2) {
4628 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004629 }
4630 }
4631 }
4632 }
4633 }
4634 }
4635
Eric Laurent99401132014-05-07 19:48:15 -07004636 // temporary mute output if device selection changes to avoid volume bursts due to
4637 // different per device volumes
4638 if (outputDesc->isActive() && (device != prevDevice)) {
Eric Laurentdc462862016-07-19 12:29:53 -07004639 uint32_t tempMuteWaitMs = outputDesc->latency() * 2;
4640 // temporary mute duration is conservatively set to 4 times the reported latency
4641 uint32_t tempMuteDurationMs = outputDesc->latency() * 4;
4642 if (muteWaitMs < tempMuteWaitMs) {
4643 muteWaitMs = tempMuteWaitMs;
Eric Laurent99401132014-05-07 19:48:15 -07004644 }
Eric Laurentdc462862016-07-19 12:29:53 -07004645
Eric Laurent99401132014-05-07 19:48:15 -07004646 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004647 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentdc462862016-07-19 12:29:53 -07004648 // make sure that we do not start the temporary mute period too early in case of
4649 // delayed device change
4650 setStrategyMute((routing_strategy)i, true, outputDesc, delayMs);
Eric Laurentc75307b2015-03-17 15:29:32 -07004651 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurentdc462862016-07-19 12:29:53 -07004652 delayMs + tempMuteDurationMs, device);
Eric Laurent99401132014-05-07 19:48:15 -07004653 }
4654 }
4655 }
4656
Eric Laurente552edb2014-03-10 17:42:56 -07004657 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4658 if (muteWaitMs > delayMs) {
4659 muteWaitMs -= delayMs;
4660 usleep(muteWaitMs * 1000);
4661 return muteWaitMs;
4662 }
4663 return 0;
4664}
4665
Eric Laurentc75307b2015-03-17 15:29:32 -07004666uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004667 audio_devices_t device,
4668 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004669 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004670 audio_patch_handle_t *patchHandle,
4671 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004672{
Eric Laurentc75307b2015-03-17 15:29:32 -07004673 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004674 AudioParameter param;
4675 uint32_t muteWaitMs;
4676
4677 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004678 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4679 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004680 return muteWaitMs;
4681 }
4682 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4683 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004684 if ((device != AUDIO_DEVICE_NONE) &&
4685 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004686 return 0;
4687 }
4688
4689 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004690 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004691
4692 audio_devices_t prevDevice = outputDesc->mDevice;
4693
Paul McLeanaa981192015-03-21 09:55:15 -07004694 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004695
4696 if (device != AUDIO_DEVICE_NONE) {
4697 outputDesc->mDevice = device;
4698 }
4699 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4700
4701 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004702 // the requested device is AUDIO_DEVICE_NONE
4703 // OR the requested device is the same as current device
4704 // AND force is not specified
4705 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004706 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004707 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4708 !force &&
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004709 outputDesc->getPatchHandle() != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004710 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004711 return muteWaitMs;
4712 }
4713
4714 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004715
Eric Laurente552edb2014-03-10 17:42:56 -07004716 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004717 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004718 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004719 } else {
Eric Laurentc40d9692016-04-13 19:14:13 -07004720 DeviceVector deviceList;
4721 if ((address == NULL) || (strlen(address) == 0)) {
4722 deviceList = mAvailableOutputDevices.getDevicesFromType(device);
4723 } else {
4724 deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
4725 }
4726
Eric Laurent1c333e22014-05-20 10:48:17 -07004727 if (!deviceList.isEmpty()) {
4728 struct audio_patch patch;
4729 outputDesc->toAudioPortConfig(&patch.sources[0]);
4730 patch.num_sources = 1;
4731 patch.num_sinks = 0;
4732 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4733 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004734 patch.num_sinks++;
4735 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004736 ssize_t index;
4737 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4738 index = mAudioPatches.indexOfKey(*patchHandle);
4739 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004740 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004741 }
4742 sp< AudioPatch> patchDesc;
4743 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4744 if (index >= 0) {
4745 patchDesc = mAudioPatches.valueAt(index);
4746 afPatchHandle = patchDesc->mAfPatchHandle;
4747 }
4748
Eric Laurent1c333e22014-05-20 10:48:17 -07004749 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004750 &afPatchHandle,
4751 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004752 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4753 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004754 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004755 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004756 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004757 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004758 addAudioPatch(patchDesc->mHandle, patchDesc);
4759 } else {
4760 patchDesc->mPatch = patch;
4761 }
4762 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004763 if (patchHandle) {
4764 *patchHandle = patchDesc->mHandle;
4765 }
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004766 outputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004767 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004768 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004769 }
4770 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004771
4772 // inform all input as well
4773 for (size_t i = 0; i < mInputs.size(); i++) {
4774 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004775 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004776 AudioParameter inputCmd = AudioParameter();
4777 ALOGV("%s: inform input %d of device:%d", __func__,
4778 inputDescriptor->mIoHandle, device);
4779 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4780 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4781 inputCmd.toString(),
4782 delayMs);
4783 }
4784 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004785 }
Eric Laurente552edb2014-03-10 17:42:56 -07004786
4787 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004788 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004789
4790 return muteWaitMs;
4791}
4792
Eric Laurentc75307b2015-03-17 15:29:32 -07004793status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004794 int delayMs,
4795 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004796{
Eric Laurent6a94d692014-05-20 11:18:06 -07004797 ssize_t index;
4798 if (patchHandle) {
4799 index = mAudioPatches.indexOfKey(*patchHandle);
4800 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004801 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004802 }
4803 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004804 return INVALID_OPERATION;
4805 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004806 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4807 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004808 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07004809 outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07004810 removeAudioPatch(patchDesc->mHandle);
4811 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004812 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004813 return status;
4814}
4815
4816status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4817 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004818 bool force,
4819 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004820{
4821 status_t status = NO_ERROR;
4822
Eric Laurent1f2f2232014-06-02 12:01:23 -07004823 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004824 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4825 inputDesc->mDevice = device;
4826
4827 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4828 if (!deviceList.isEmpty()) {
4829 struct audio_patch patch;
4830 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004831 // AUDIO_SOURCE_HOTWORD is for internal use only:
4832 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004833 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
Eric Laurent599c7582015-12-07 18:05:55 -08004834 !inputDesc->isSoundTrigger()) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004835 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4836 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004837 patch.num_sinks = 1;
4838 //only one input device for now
4839 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004840 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004841 ssize_t index;
4842 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4843 index = mAudioPatches.indexOfKey(*patchHandle);
4844 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004845 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004846 }
4847 sp< AudioPatch> patchDesc;
4848 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4849 if (index >= 0) {
4850 patchDesc = mAudioPatches.valueAt(index);
4851 afPatchHandle = patchDesc->mAfPatchHandle;
4852 }
4853
Eric Laurent1c333e22014-05-20 10:48:17 -07004854 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004855 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004856 0);
4857 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004858 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004859 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004860 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004861 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004862 addAudioPatch(patchDesc->mHandle, patchDesc);
4863 } else {
4864 patchDesc->mPatch = patch;
4865 }
4866 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004867 if (patchHandle) {
4868 *patchHandle = patchDesc->mHandle;
4869 }
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004870 inputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004871 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004872 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004873 }
4874 }
4875 }
4876 return status;
4877}
4878
Eric Laurent6a94d692014-05-20 11:18:06 -07004879status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4880 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004881{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004882 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004883 ssize_t index;
4884 if (patchHandle) {
4885 index = mAudioPatches.indexOfKey(*patchHandle);
4886 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004887 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004888 }
4889 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004890 return INVALID_OPERATION;
4891 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004892 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4893 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004894 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07004895 inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07004896 removeAudioPatch(patchDesc->mHandle);
4897 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004898 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004899 return status;
4900}
4901
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004902sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07004903 const String8& address,
François Gaffie53615e22015-03-19 09:24:12 +01004904 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07004905 audio_format_t& format,
4906 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01004907 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004908{
4909 // Choose an input profile based on the requested capture parameters: select the first available
4910 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07004911 //
4912 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
4913 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07004914
4915 for (size_t i = 0; i < mHwModules.size(); i++)
4916 {
4917 if (mHwModules[i]->mHandle == 0) {
4918 continue;
4919 }
4920 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4921 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004922 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004923 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004924 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07004925 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07004926 format,
4927 &format /*updatedFormat*/,
4928 channelMask,
4929 &channelMask /*updatedChannelMask*/,
4930 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004931
Eric Laurente552edb2014-03-10 17:42:56 -07004932 return profile;
4933 }
4934 }
4935 }
4936 return NULL;
4937}
4938
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004939
4940audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01004941 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07004942{
François Gaffie036e1e92015-03-19 10:16:24 +01004943 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
4944 audio_devices_t selectedDeviceFromMix =
4945 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08004946
François Gaffie036e1e92015-03-19 10:16:24 +01004947 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
4948 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08004949 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004950 return getDeviceForInputSource(inputSource);
4951}
4952
4953audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
4954{
Paul McLean466dc8e2015-04-17 13:15:36 -06004955 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
4956 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004957 if (inputSource == route->mSource && route->isActive()) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004958 return route->mDeviceDescriptor->type();
4959 }
4960 }
4961
4962 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07004963}
4964
Eric Laurente0720872014-03-11 09:30:41 -07004965float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004966 int index,
4967 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004968{
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004969 float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index);
Jean-Michel Trivi3d8b4a42016-09-14 18:37:46 -07004970
4971 // handle the case of accessibility active while a ringtone is playing: if the ringtone is much
4972 // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch
4973 // exploration of the dialer UI. In this situation, bring the accessibility volume closer to
4974 // the ringtone volume
4975 if ((stream == AUDIO_STREAM_ACCESSIBILITY)
4976 && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState())
4977 && isStreamActive(AUDIO_STREAM_RING, 0)) {
4978 const float ringVolumeDB = computeVolume(AUDIO_STREAM_RING, index, device);
4979 return ringVolumeDB - 4 > volumeDB ? ringVolumeDB - 4 : volumeDB;
4980 }
4981
Eric Laurente552edb2014-03-10 17:42:56 -07004982 // if a headset is connected, apply the following rules to ring tones and notifications
4983 // to avoid sound level bursts in user's ears:
Eric Laurent6af1c1d2016-04-14 11:20:44 -07004984 // - always attenuate notifications volume by 6dB
4985 // - attenuate ring tones volume by 6dB unless music is not playing and
4986 // speaker is part of the select devices
Eric Laurente552edb2014-03-10 17:42:56 -07004987 // - if music is playing, always limit the volume to current music volume,
4988 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004989 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004990 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4991 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4992 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4993 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4994 ((stream_strategy == STRATEGY_SONIFICATION)
4995 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004996 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004997 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004998 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
François Gaffied1ab2bd2015-12-02 18:20:06 +01004999 mVolumeCurves->canBeMuted(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005000 // when the phone is ringing we must consider that music could have been paused just before
5001 // by the music application and behave as if music was active if the last music track was
5002 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07005003 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07005004 mLimitRingtoneVolume) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005005 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07005006 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07005007 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005008 mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC,
5009 musicDevice),
5010 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07005011 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
5012 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005013 if (volumeDB > minVolDB) {
5014 volumeDB = minVolDB;
Eric Laurentffbc80f2015-03-18 18:30:19 -07005015 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07005016 }
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005017 if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
5018 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) {
5019 // on A2DP, also ensure notification volume is not too low compared to media when
5020 // intended to be played
5021 if ((volumeDB > -96.0f) &&
5022 (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) {
5023 ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f",
5024 stream, device,
5025 volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB);
5026 volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB;
5027 }
5028 }
Eric Laurent6af1c1d2016-04-14 11:20:44 -07005029 } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) ||
5030 stream_strategy != STRATEGY_SONIFICATION) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005031 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07005032 }
5033 }
5034
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005035 return volumeDB;
Eric Laurente552edb2014-03-10 17:42:56 -07005036}
5037
Eric Laurente0720872014-03-11 09:30:41 -07005038status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005039 int index,
5040 const sp<AudioOutputDescriptor>& outputDesc,
5041 audio_devices_t device,
5042 int delayMs,
5043 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005044{
Eric Laurente552edb2014-03-10 17:42:56 -07005045 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07005046 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07005047 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07005048 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07005049 return NO_ERROR;
5050 }
François Gaffie2110e042015-03-24 08:41:51 +01005051 audio_policy_forced_cfg_t forceUseForComm =
5052 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07005053 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01005054 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
5055 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005056 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01005057 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07005058 return INVALID_OPERATION;
5059 }
5060
Eric Laurentc75307b2015-03-17 15:29:32 -07005061 if (device == AUDIO_DEVICE_NONE) {
5062 device = outputDesc->device();
5063 }
Eric Laurent275e8e92014-11-30 15:14:47 -08005064
Eric Laurentffbc80f2015-03-18 18:30:19 -07005065 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07005066 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07005067 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08005068 }
Eric Laurentc75307b2015-03-17 15:29:32 -07005069
Eric Laurentffbc80f2015-03-18 18:30:19 -07005070 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07005071
Eric Laurent3b73df72014-03-11 09:06:29 -07005072 if (stream == AUDIO_STREAM_VOICE_CALL ||
5073 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07005074 float voiceVolume;
5075 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07005076 if (stream == AUDIO_STREAM_VOICE_CALL) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005077 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005078 } else {
5079 voiceVolume = 1.0;
5080 }
5081
Eric Laurent18fba842016-03-31 14:41:26 -07005082 if (voiceVolume != mLastVoiceVolume) {
Eric Laurente552edb2014-03-10 17:42:56 -07005083 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
5084 mLastVoiceVolume = voiceVolume;
5085 }
5086 }
5087
5088 return NO_ERROR;
5089}
5090
Eric Laurentc75307b2015-03-17 15:29:32 -07005091void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
5092 audio_devices_t device,
5093 int delayMs,
5094 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005095{
Eric Laurentc75307b2015-03-17 15:29:32 -07005096 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07005097
Eric Laurent794fde22016-03-11 09:50:45 -08005098 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005099 checkAndSetVolume((audio_stream_type_t)stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005100 mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005101 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005102 device,
5103 delayMs,
5104 force);
5105 }
5106}
5107
Eric Laurente0720872014-03-11 09:30:41 -07005108void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07005109 bool on,
5110 const sp<AudioOutputDescriptor>& outputDesc,
5111 int delayMs,
5112 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005113{
Eric Laurent72249d52015-06-08 11:12:15 -07005114 ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d",
5115 strategy, on, outputDesc->getId());
Eric Laurent794fde22016-03-11 09:50:45 -08005116 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005117 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005118 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07005119 }
5120 }
5121}
5122
Eric Laurente0720872014-03-11 09:30:41 -07005123void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005124 bool on,
5125 const sp<AudioOutputDescriptor>& outputDesc,
5126 int delayMs,
5127 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005128{
Eric Laurente552edb2014-03-10 17:42:56 -07005129 if (device == AUDIO_DEVICE_NONE) {
5130 device = outputDesc->device();
5131 }
5132
Eric Laurentc75307b2015-03-17 15:29:32 -07005133 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
5134 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07005135
5136 if (on) {
5137 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005138 if (mVolumeCurves->canBeMuted(stream) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07005139 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01005140 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005141 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005142 }
5143 }
5144 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
5145 outputDesc->mMuteCount[stream]++;
5146 } else {
5147 if (outputDesc->mMuteCount[stream] == 0) {
5148 ALOGV("setStreamMute() unmuting non muted stream!");
5149 return;
5150 }
5151 if (--outputDesc->mMuteCount[stream] == 0) {
5152 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005153 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005154 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005155 device,
5156 delayMs);
5157 }
5158 }
5159}
5160
Eric Laurente0720872014-03-11 09:30:41 -07005161void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07005162 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07005163{
Eric Laurent87ffa392015-05-22 10:32:38 -07005164 if(!hasPrimaryOutput()) {
5165 return;
5166 }
5167
Eric Laurente552edb2014-03-10 17:42:56 -07005168 // if the stream pertains to sonification strategy and we are in call we must
5169 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
5170 // in the device used for phone strategy and play the tone if the selected device does not
5171 // interfere with the device used for phone strategy
5172 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
5173 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07005174 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005175 if ((stream_strategy == STRATEGY_SONIFICATION) ||
5176 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005177 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07005178 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
5179 stream, starting, outputDesc->mDevice, stateChange);
5180 if (outputDesc->mRefCount[stream]) {
5181 int muteCount = 1;
5182 if (stateChange) {
5183 muteCount = outputDesc->mRefCount[stream];
5184 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005185 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005186 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
5187 for (int i = 0; i < muteCount; i++) {
5188 setStreamMute(stream, starting, mPrimaryOutput);
5189 }
5190 } else {
5191 ALOGV("handleIncallSonification() high visibility");
5192 if (outputDesc->device() &
5193 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
5194 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
5195 for (int i = 0; i < muteCount; i++) {
5196 setStreamMute(stream, starting, mPrimaryOutput);
5197 }
5198 }
5199 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005200 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
5201 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07005202 } else {
5203 mpClientInterface->stopTone();
5204 }
5205 }
5206 }
5207 }
5208}
5209
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005210audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
5211{
5212 // flags to stream type mapping
5213 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
5214 return AUDIO_STREAM_ENFORCED_AUDIBLE;
5215 }
5216 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
5217 return AUDIO_STREAM_BLUETOOTH_SCO;
5218 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08005219 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
5220 return AUDIO_STREAM_TTS;
5221 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005222
5223 // usage to stream type mapping
5224 switch (attr->usage) {
5225 case AUDIO_USAGE_MEDIA:
5226 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005227 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5228 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08005229 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5230 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005231 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5232 return AUDIO_STREAM_SYSTEM;
5233 case AUDIO_USAGE_VOICE_COMMUNICATION:
5234 return AUDIO_STREAM_VOICE_CALL;
5235
5236 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5237 return AUDIO_STREAM_DTMF;
5238
5239 case AUDIO_USAGE_ALARM:
5240 return AUDIO_STREAM_ALARM;
5241 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5242 return AUDIO_STREAM_RING;
5243
5244 case AUDIO_USAGE_NOTIFICATION:
5245 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5246 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5247 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5248 case AUDIO_USAGE_NOTIFICATION_EVENT:
5249 return AUDIO_STREAM_NOTIFICATION;
5250
5251 case AUDIO_USAGE_UNKNOWN:
5252 default:
5253 return AUDIO_STREAM_MUSIC;
5254 }
5255}
Eric Laurente83b55d2014-11-14 10:06:21 -08005256
François Gaffie53615e22015-03-19 09:24:12 +01005257bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
5258{
Eric Laurente83b55d2014-11-14 10:06:21 -08005259 // has flags that map to a strategy?
5260 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
5261 return true;
5262 }
5263
5264 // has known usage?
5265 switch (paa->usage) {
5266 case AUDIO_USAGE_UNKNOWN:
5267 case AUDIO_USAGE_MEDIA:
5268 case AUDIO_USAGE_VOICE_COMMUNICATION:
5269 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5270 case AUDIO_USAGE_ALARM:
5271 case AUDIO_USAGE_NOTIFICATION:
5272 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5273 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5274 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5275 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5276 case AUDIO_USAGE_NOTIFICATION_EVENT:
5277 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5278 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5279 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5280 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08005281 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08005282 break;
5283 default:
5284 return false;
5285 }
5286 return true;
5287}
5288
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07005289bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor>& outputDesc,
François Gaffiead3183e2015-03-18 16:55:35 +01005290 routing_strategy strategy, uint32_t inPastMs,
5291 nsecs_t sysTime) const
5292{
5293 if ((sysTime == 0) && (inPastMs != 0)) {
5294 sysTime = systemTime();
5295 }
Eric Laurent794fde22016-03-11 09:50:45 -08005296 for (int i = 0; i < (int)AUDIO_STREAM_FOR_POLICY_CNT; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01005297 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
5298 (NUM_STRATEGIES == strategy)) &&
5299 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
5300 return true;
5301 }
5302 }
5303 return false;
5304}
5305
François Gaffie2110e042015-03-24 08:41:51 +01005306audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
5307{
5308 return mEngine->getForceUse(usage);
5309}
5310
5311bool AudioPolicyManager::isInCall()
5312{
5313 return isStateInCall(mEngine->getPhoneState());
5314}
5315
5316bool AudioPolicyManager::isStateInCall(int state)
5317{
5318 return is_state_in_call(state);
5319}
5320
Eric Laurentd60560a2015-04-10 11:31:20 -07005321void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc)
5322{
5323 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
5324 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
5325 if (sourceDesc->mDevice->equals(deviceDesc)) {
5326 ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle());
5327 stopAudioSource(sourceDesc->getHandle());
5328 }
5329 }
5330
5331 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
5332 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
5333 bool release = false;
5334 for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) {
5335 const struct audio_port_config *source = &patchDesc->mPatch.sources[j];
5336 if (source->type == AUDIO_PORT_TYPE_DEVICE &&
5337 source->ext.device.type == deviceDesc->type()) {
5338 release = true;
5339 }
5340 }
5341 for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) {
5342 const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j];
5343 if (sink->type == AUDIO_PORT_TYPE_DEVICE &&
5344 sink->ext.device.type == deviceDesc->type()) {
5345 release = true;
5346 }
5347 }
5348 if (release) {
5349 ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle);
5350 releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid);
5351 }
5352 }
5353}
5354
Phil Burk09bc4612016-02-24 15:58:15 -08005355// Modify the list of surround sound formats supported.
Phil Burk0709b0a2016-03-31 12:54:57 -07005356void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) {
5357 FormatVector &formats = *formatsPtr;
Phil Burk07ac1142016-03-25 13:39:29 -07005358 // TODO Set this based on Config properties.
5359 const bool alwaysForceAC3 = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005360
5361 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5362 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
Phil Burk0709b0a2016-03-31 12:54:57 -07005363 ALOGD("%s: forced use = %d", __FUNCTION__, forceUse);
Phil Burk09bc4612016-02-24 15:58:15 -08005364
5365 // Analyze original support for various formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005366 bool supportsAC3 = false;
5367 bool supportsOtherSurround = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005368 bool supportsIEC61937 = false;
5369 for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) {
5370 audio_format_t format = formats[formatIndex];
Phil Burk09bc4612016-02-24 15:58:15 -08005371 switch (format) {
5372 case AUDIO_FORMAT_AC3:
Phil Burk07ac1142016-03-25 13:39:29 -07005373 supportsAC3 = true;
5374 break;
Phil Burk09bc4612016-02-24 15:58:15 -08005375 case AUDIO_FORMAT_E_AC3:
5376 case AUDIO_FORMAT_DTS:
5377 case AUDIO_FORMAT_DTS_HD:
Phil Burk07ac1142016-03-25 13:39:29 -07005378 supportsOtherSurround = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005379 break;
5380 case AUDIO_FORMAT_IEC61937:
5381 supportsIEC61937 = true;
5382 break;
5383 default:
5384 break;
5385 }
5386 }
Phil Burk09bc4612016-02-24 15:58:15 -08005387
5388 // Modify formats based on surround preferences.
5389 // If NEVER, remove support for surround formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005390 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5391 if (supportsAC3 || supportsOtherSurround || supportsIEC61937) {
5392 // Remove surround sound related formats.
5393 for (size_t formatIndex = 0; formatIndex < formats.size(); ) {
5394 audio_format_t format = formats[formatIndex];
5395 switch(format) {
5396 case AUDIO_FORMAT_AC3:
5397 case AUDIO_FORMAT_E_AC3:
5398 case AUDIO_FORMAT_DTS:
5399 case AUDIO_FORMAT_DTS_HD:
5400 case AUDIO_FORMAT_IEC61937:
Phil Burk07ac1142016-03-25 13:39:29 -07005401 formats.removeAt(formatIndex);
5402 break;
5403 default:
5404 formatIndex++; // keep it
5405 break;
5406 }
Phil Burk09bc4612016-02-24 15:58:15 -08005407 }
Phil Burk07ac1142016-03-25 13:39:29 -07005408 supportsAC3 = false;
5409 supportsOtherSurround = false;
5410 supportsIEC61937 = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005411 }
Phil Burk07ac1142016-03-25 13:39:29 -07005412 } else { // AUTO or ALWAYS
5413 // Most TVs support AC3 even if they do not report it in the EDID.
5414 if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS))
5415 && !supportsAC3) {
5416 formats.add(AUDIO_FORMAT_AC3);
5417 supportsAC3 = true;
5418 }
5419
5420 // If ALWAYS, add support for raw surround formats if all are missing.
5421 // This assumes that if any of these formats are reported by the HAL
5422 // then the report is valid and should not be modified.
5423 if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)
5424 && !supportsOtherSurround) {
5425 formats.add(AUDIO_FORMAT_E_AC3);
5426 formats.add(AUDIO_FORMAT_DTS);
5427 formats.add(AUDIO_FORMAT_DTS_HD);
5428 supportsOtherSurround = true;
5429 }
5430
5431 // Add support for IEC61937 if any raw surround supported.
5432 // The HAL could do this but add it here, just in case.
5433 if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) {
5434 formats.add(AUDIO_FORMAT_IEC61937);
5435 supportsIEC61937 = true;
5436 }
Phil Burk09bc4612016-02-24 15:58:15 -08005437 }
Phil Burk0709b0a2016-03-31 12:54:57 -07005438}
5439
5440// Modify the list of channel masks supported.
5441void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) {
5442 ChannelsVector &channelMasks = *channelMasksPtr;
5443 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5444 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
5445
5446 // If NEVER, then remove support for channelMasks > stereo.
5447 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5448 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) {
5449 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5450 if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) {
5451 ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask);
5452 channelMasks.removeAt(maskIndex);
5453 } else {
5454 maskIndex++;
5455 }
5456 }
5457 // If ALWAYS, then make sure we at least support 5.1
5458 } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) {
5459 bool supports5dot1 = false;
5460 // Are there any channel masks that can be considered "surround"?
5461 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); maskIndex++) {
5462 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5463 if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) {
5464 supports5dot1 = true;
5465 break;
5466 }
5467 }
5468 // If not then add 5.1 support.
5469 if (!supports5dot1) {
5470 channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1);
5471 ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__);
5472 }
Phil Burk09bc4612016-02-24 15:58:15 -08005473 }
5474}
5475
Phil Burk00eeb322016-03-31 12:41:00 -07005476void AudioPolicyManager::updateAudioProfiles(audio_devices_t device,
5477 audio_io_handle_t ioHandle,
François Gaffie112b0af2015-11-19 16:13:25 +01005478 AudioProfileVector &profiles)
5479{
5480 String8 reply;
Phil Burk0709b0a2016-03-31 12:54:57 -07005481
François Gaffie112b0af2015-11-19 16:13:25 +01005482 // Format MUST be checked first to update the list of AudioProfile
5483 if (profiles.hasDynamicFormat()) {
5484 reply = mpClientInterface->getParameters(ioHandle,
5485 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
Phil Burk0709b0a2016-03-31 12:54:57 -07005486 ALOGV("%s: supported formats %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005487 AudioParameter repliedParameters(reply);
5488 if (repliedParameters.get(
5489 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS), reply) != NO_ERROR) {
François Gaffie112b0af2015-11-19 16:13:25 +01005490 ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__);
5491 return;
5492 }
Phil Burk09bc4612016-02-24 15:58:15 -08005493 FormatVector formats = formatsFromString(reply.string());
Phil Burk00eeb322016-03-31 12:41:00 -07005494 if (device == AUDIO_DEVICE_OUT_HDMI) {
Phil Burk0709b0a2016-03-31 12:54:57 -07005495 filterSurroundFormats(&formats);
Phil Burk00eeb322016-03-31 12:41:00 -07005496 }
Phil Burk09bc4612016-02-24 15:58:15 -08005497 profiles.setFormats(formats);
François Gaffie112b0af2015-11-19 16:13:25 +01005498 }
5499 const FormatVector &supportedFormats = profiles.getSupportedFormats();
5500
Eric Laurent20eb3a42016-01-26 18:39:17 -08005501 for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) {
François Gaffie112b0af2015-11-19 16:13:25 +01005502 audio_format_t format = supportedFormats[formatIndex];
Eric Laurent20eb3a42016-01-26 18:39:17 -08005503 ChannelsVector channelMasks;
5504 SampleRateVector samplingRates;
François Gaffie112b0af2015-11-19 16:13:25 +01005505 AudioParameter requestedParameters;
5506 requestedParameters.addInt(String8(AUDIO_PARAMETER_STREAM_FORMAT), format);
5507
5508 if (profiles.hasDynamicRateFor(format)) {
5509 reply = mpClientInterface->getParameters(ioHandle,
5510 requestedParameters.toString() + ";" +
5511 AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES);
5512 ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005513 AudioParameter repliedParameters(reply);
5514 if (repliedParameters.get(
5515 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES), reply) == NO_ERROR) {
5516 samplingRates = samplingRatesFromString(reply.string());
François Gaffie112b0af2015-11-19 16:13:25 +01005517 }
5518 }
5519 if (profiles.hasDynamicChannelsFor(format)) {
5520 reply = mpClientInterface->getParameters(ioHandle,
5521 requestedParameters.toString() + ";" +
5522 AUDIO_PARAMETER_STREAM_SUP_CHANNELS);
5523 ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005524 AudioParameter repliedParameters(reply);
5525 if (repliedParameters.get(
5526 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS), reply) == NO_ERROR) {
5527 channelMasks = channelMasksFromString(reply.string());
Phil Burk0709b0a2016-03-31 12:54:57 -07005528 if (device == AUDIO_DEVICE_OUT_HDMI) {
5529 filterSurroundChannelMasks(&channelMasks);
5530 }
François Gaffie112b0af2015-11-19 16:13:25 +01005531 }
5532 }
Eric Laurent20eb3a42016-01-26 18:39:17 -08005533 profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates));
François Gaffie112b0af2015-11-19 16:13:25 +01005534 }
5535}
Eric Laurentd60560a2015-04-10 11:31:20 -07005536
Eric Laurente552edb2014-03-10 17:42:56 -07005537}; // namespace android