blob: 114df81e362be63c58bcee5d0be867aefb1cc2f1 [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
52// ----------------------------------------------------------------------------
53// AudioPolicyInterface implementation
54// ----------------------------------------------------------------------------
55
Eric Laurente0720872014-03-11 09:30:41 -070056status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
Paul McLeane743a472015-01-28 11:07:31 -080057 audio_policy_dev_state_t state,
58 const char *device_address,
59 const char *device_name)
Eric Laurente552edb2014-03-10 17:42:56 -070060{
Paul McLeane743a472015-01-28 11:07:31 -080061 return setDeviceConnectionStateInt(device, state, device_address, device_name);
Eric Laurentc73ca6e2014-12-12 14:34:22 -080062}
63
François Gaffie44481e72016-04-20 07:49:57 +020064void AudioPolicyManager::broadcastDeviceConnectionState(audio_devices_t device,
65 audio_policy_dev_state_t state,
66 const String8 &device_address)
67{
68 AudioParameter param(device_address);
69 const String8 key(state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE ?
70 AUDIO_PARAMETER_DEVICE_CONNECT : AUDIO_PARAMETER_DEVICE_DISCONNECT);
71 param.addInt(key, device);
72 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
73}
74
Eric Laurentc73ca6e2014-12-12 14:34:22 -080075status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device,
Eric Laurenta1d525f2015-01-29 13:36:45 -080076 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -080077 const char *device_address,
78 const char *device_name)
Eric Laurentc73ca6e2014-12-12 14:34:22 -080079{
Paul McLeane743a472015-01-28 11:07:31 -080080 ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
81- device, state, device_address, device_name);
Eric Laurente552edb2014-03-10 17:42:56 -070082
83 // connect/disconnect only 1 device at a time
84 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
85
François Gaffie53615e22015-03-19 09:24:12 +010086 sp<DeviceDescriptor> devDesc =
87 mHwModules.getDeviceDescriptor(device, device_address, device_name);
Paul McLeane743a472015-01-28 11:07:31 -080088
Eric Laurente552edb2014-03-10 17:42:56 -070089 // handle output devices
90 if (audio_is_output_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -070091 SortedVector <audio_io_handle_t> outputs;
92
Eric Laurent3a4311c2014-03-17 12:00:47 -070093 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
94
Eric Laurente552edb2014-03-10 17:42:56 -070095 // save a copy of the opened output descriptors before any output is opened or closed
96 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
97 mPreviousOutputs = mOutputs;
Eric Laurente552edb2014-03-10 17:42:56 -070098 switch (state)
99 {
100 // handle output device connection
Eric Laurent3ae5f312015-02-03 17:12:08 -0800101 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700102 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700103 ALOGW("setDeviceConnectionState() device already connected: %x", device);
104 return INVALID_OPERATION;
105 }
106 ALOGV("setDeviceConnectionState() connecting device %x", device);
107
Eric Laurente552edb2014-03-10 17:42:56 -0700108 // register new device as available
Eric Laurent3a4311c2014-03-17 12:00:47 -0700109 index = mAvailableOutputDevices.add(devDesc);
110 if (index >= 0) {
François Gaffie53615e22015-03-19 09:24:12 +0100111 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurentcf817a22014-08-04 20:36:31 -0700112 if (module == 0) {
113 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
114 device);
115 mAvailableOutputDevices.remove(devDesc);
116 return INVALID_OPERATION;
117 }
Paul McLeane743a472015-01-28 11:07:31 -0800118 mAvailableOutputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700119 } else {
120 return NO_MEMORY;
Eric Laurente552edb2014-03-10 17:42:56 -0700121 }
122
François Gaffie44481e72016-04-20 07:49:57 +0200123 // Before checking outputs, broadcast connect event to allow HAL to retrieve dynamic
124 // parameters on newly connected devices (instead of opening the outputs...)
125 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
126
Eric Laurenta1d525f2015-01-29 13:36:45 -0800127 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700128 mAvailableOutputDevices.remove(devDesc);
François Gaffie44481e72016-04-20 07:49:57 +0200129
130 broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
131 devDesc->mAddress);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700132 return INVALID_OPERATION;
133 }
François Gaffie2110e042015-03-24 08:41:51 +0100134 // Propagate device availability to Engine
135 mEngine->setDeviceConnectionState(devDesc, state);
136
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700137 // outputs should never be empty here
138 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
139 "checkOutputsForDevice() returned no outputs but status OK");
140 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
141 outputs.size());
Eric Laurent3ae5f312015-02-03 17:12:08 -0800142
Eric Laurent3ae5f312015-02-03 17:12:08 -0800143 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700144 // handle output device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700145 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700146 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700147 ALOGW("setDeviceConnectionState() device not connected: %x", device);
148 return INVALID_OPERATION;
149 }
150
Paul McLean5c477aa2014-08-20 16:47:57 -0700151 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
152
Paul McLeane743a472015-01-28 11:07:31 -0800153 // Send Disconnect to HALs
François Gaffie44481e72016-04-20 07:49:57 +0200154 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700155
Eric Laurente552edb2014-03-10 17:42:56 -0700156 // remove device from available output devices
Eric Laurent3a4311c2014-03-17 12:00:47 -0700157 mAvailableOutputDevices.remove(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700158
Eric Laurenta1d525f2015-01-29 13:36:45 -0800159 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
François Gaffie2110e042015-03-24 08:41:51 +0100160
161 // Propagate device availability to Engine
162 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurente552edb2014-03-10 17:42:56 -0700163 } break;
164
165 default:
166 ALOGE("setDeviceConnectionState() invalid state: %x", state);
167 return BAD_VALUE;
168 }
169
Eric Laurent3a4311c2014-03-17 12:00:47 -0700170 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
171 // output is suspended before any tracks are moved to it
Eric Laurente552edb2014-03-10 17:42:56 -0700172 checkA2dpSuspend();
173 checkOutputForAllStrategies();
174 // outputs must be closed after checkOutputForAllStrategies() is executed
175 if (!outputs.isEmpty()) {
176 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700177 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700178 // close unused outputs after device disconnection or direct outputs that have been
179 // opened by checkOutputsForDevice() to query dynamic parameters
Eric Laurent3b73df72014-03-11 09:06:29 -0700180 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Eric Laurente552edb2014-03-10 17:42:56 -0700181 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
182 (desc->mDirectOpenCount == 0))) {
183 closeOutput(outputs[i]);
184 }
185 }
Eric Laurent3a4311c2014-03-17 12:00:47 -0700186 // check again after closing A2DP output to reset mA2dpSuspended if needed
187 checkA2dpSuspend();
Eric Laurente552edb2014-03-10 17:42:56 -0700188 }
189
190 updateDevicesAndOutputs();
Eric Laurent87ffa392015-05-22 10:32:38 -0700191 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700192 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
193 updateCallRouting(newDevice);
194 }
Eric Laurente552edb2014-03-10 17:42:56 -0700195 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700196 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
197 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
198 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700199 // do not force device change on duplicated output because if device is 0, it will
200 // also force a device 0 for the two outputs it is duplicated to which may override
201 // a valid device selection on those outputs.
Eric Laurentc75307b2015-03-17 15:29:32 -0700202 bool force = !desc->isDuplicated()
François Gaffie53615e22015-03-19 09:24:12 +0100203 && (!device_distinguishes_on_address(device)
Eric Laurentc2730ba2014-07-20 15:47:07 -0700204 // always force when disconnecting (a non-duplicated device)
205 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Eric Laurentc75307b2015-03-17 15:29:32 -0700206 setOutputDevice(desc, newDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700207 }
Eric Laurente552edb2014-03-10 17:42:56 -0700208 }
209
Eric Laurentd60560a2015-04-10 11:31:20 -0700210 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
211 cleanUpForDevice(devDesc);
212 }
213
Eric Laurent72aa32f2014-05-30 18:51:48 -0700214 mpClientInterface->onAudioPortListUpdate();
Eric Laurentb71e58b2014-05-29 16:08:11 -0700215 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700216 } // end if is output device
217
Eric Laurente552edb2014-03-10 17:42:56 -0700218 // handle input devices
219 if (audio_is_input_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700220 SortedVector <audio_io_handle_t> inputs;
221
Eric Laurent3a4311c2014-03-17 12:00:47 -0700222 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700223 switch (state)
224 {
225 // handle input device connection
Eric Laurent3b73df72014-03-11 09:06:29 -0700226 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700227 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700228 ALOGW("setDeviceConnectionState() device already connected: %d", device);
229 return INVALID_OPERATION;
230 }
François Gaffie53615e22015-03-19 09:24:12 +0100231 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurent6a94d692014-05-20 11:18:06 -0700232 if (module == NULL) {
233 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
234 device);
235 return INVALID_OPERATION;
236 }
François Gaffie44481e72016-04-20 07:49:57 +0200237
238 // Before checking intputs, broadcast connect event to allow HAL to retrieve dynamic
239 // parameters on newly connected devices (instead of opening the inputs...)
240 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
241
Paul McLean9080a4c2015-06-18 08:24:02 -0700242 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
François Gaffie44481e72016-04-20 07:49:57 +0200243 broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
244 devDesc->mAddress);
Eric Laurentd4692962014-05-05 18:13:44 -0700245 return INVALID_OPERATION;
246 }
247
Eric Laurent3a4311c2014-03-17 12:00:47 -0700248 index = mAvailableInputDevices.add(devDesc);
249 if (index >= 0) {
Paul McLeane743a472015-01-28 11:07:31 -0800250 mAvailableInputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700251 } else {
252 return NO_MEMORY;
253 }
Eric Laurent3ae5f312015-02-03 17:12:08 -0800254
François Gaffie2110e042015-03-24 08:41:51 +0100255 // Propagate device availability to Engine
256 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700257 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700258
259 // handle input device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700260 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700261 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700262 ALOGW("setDeviceConnectionState() device not connected: %d", device);
263 return INVALID_OPERATION;
264 }
Paul McLean5c477aa2014-08-20 16:47:57 -0700265
266 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
267
268 // Set Disconnect to HALs
François Gaffie44481e72016-04-20 07:49:57 +0200269 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700270
Paul McLean9080a4c2015-06-18 08:24:02 -0700271 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700272 mAvailableInputDevices.remove(devDesc);
Paul McLean5c477aa2014-08-20 16:47:57 -0700273
François Gaffie2110e042015-03-24 08:41:51 +0100274 // Propagate device availability to Engine
275 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700276 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700277
278 default:
279 ALOGE("setDeviceConnectionState() invalid state: %x", state);
280 return BAD_VALUE;
281 }
282
Eric Laurentd4692962014-05-05 18:13:44 -0700283 closeAllInputs();
Eric Laurente552edb2014-03-10 17:42:56 -0700284
Eric Laurent87ffa392015-05-22 10:32:38 -0700285 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700286 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
287 updateCallRouting(newDevice);
288 }
289
Eric Laurentd60560a2015-04-10 11:31:20 -0700290 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
291 cleanUpForDevice(devDesc);
292 }
293
Eric Laurentb52c1522014-05-20 11:27:36 -0700294 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700295 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700296 } // end if is input device
Eric Laurente552edb2014-03-10 17:42:56 -0700297
298 ALOGW("setDeviceConnectionState() invalid device: %x", device);
299 return BAD_VALUE;
300}
301
Eric Laurente0720872014-03-11 09:30:41 -0700302audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +0100303 const char *device_address)
Eric Laurente552edb2014-03-10 17:42:56 -0700304{
Eric Laurent634b7142016-04-20 13:48:02 -0700305 sp<DeviceDescriptor> devDesc =
306 mHwModules.getDeviceDescriptor(device, device_address, "",
307 (strlen(device_address) != 0)/*matchAddress*/);
308
309 if (devDesc == 0) {
310 ALOGW("getDeviceConnectionState() undeclared device, type %08x, address: %s",
311 device, device_address);
312 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
313 }
François Gaffie53615e22015-03-19 09:24:12 +0100314
Eric Laurent3a4311c2014-03-17 12:00:47 -0700315 DeviceVector *deviceVector;
316
Eric Laurente552edb2014-03-10 17:42:56 -0700317 if (audio_is_output_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700318 deviceVector = &mAvailableOutputDevices;
Eric Laurente552edb2014-03-10 17:42:56 -0700319 } else if (audio_is_input_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700320 deviceVector = &mAvailableInputDevices;
321 } else {
322 ALOGW("getDeviceConnectionState() invalid device type %08x", device);
323 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700324 }
Eric Laurent634b7142016-04-20 13:48:02 -0700325
326 return (deviceVector->getDevice(device, String8(device_address)) != 0) ?
327 AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurenta1d525f2015-01-29 13:36:45 -0800328}
329
Eric Laurentc2730ba2014-07-20 15:47:07 -0700330void AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, int delayMs)
331{
332 bool createTxPatch = false;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700333 status_t status;
334 audio_patch_handle_t afPatchHandle;
335 DeviceVector deviceList;
336
Eric Laurent87ffa392015-05-22 10:32:38 -0700337 if(!hasPrimaryOutput()) {
338 return;
339 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -0800340 audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700341 ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice);
342
343 // release existing RX patch if any
344 if (mCallRxPatch != 0) {
345 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
346 mCallRxPatch.clear();
347 }
348 // release TX patch if any
349 if (mCallTxPatch != 0) {
350 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
351 mCallTxPatch.clear();
352 }
353
354 // If the RX device is on the primary HW module, then use legacy routing method for voice calls
355 // via setOutputDevice() on primary output.
356 // Otherwise, create two audio patches for TX and RX path.
357 if (availablePrimaryOutputDevices() & rxDevice) {
358 setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs);
359 // If the TX device is also on the primary HW module, setOutputDevice() will take care
360 // of it due to legacy implementation. If not, create a patch.
361 if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN)
362 == AUDIO_DEVICE_NONE) {
363 createTxPatch = true;
364 }
Eric Laurent8ae73122016-04-12 10:13:29 -0700365 } else { // create RX path audio patch
366 struct audio_patch patch;
367
368 patch.num_sources = 1;
369 patch.num_sinks = 1;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700370 deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice);
371 ALOG_ASSERT(!deviceList.isEmpty(),
372 "updateCallRouting() selected device not in output device list");
373 sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0);
374 deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX);
375 ALOG_ASSERT(!deviceList.isEmpty(),
376 "updateCallRouting() no telephony RX device");
377 sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0);
378
379 rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
380 rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
381
382 // request to reuse existing output stream if one is already opened to reach the RX device
383 SortedVector<audio_io_handle_t> outputs =
384 getOutputsForDevice(rxDevice, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700385 audio_io_handle_t output = selectOutput(outputs,
386 AUDIO_OUTPUT_FLAG_NONE,
387 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700388 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700389 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700390 ALOG_ASSERT(!outputDesc->isDuplicated(),
391 "updateCallRouting() RX device output is duplicated");
392 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700393 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700394 patch.num_sources = 2;
395 }
396
397 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
398 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
399 ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch",
400 status);
401 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100402 mCallRxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700403 mCallRxPatch->mAfPatchHandle = afPatchHandle;
404 mCallRxPatch->mUid = mUidCached;
405 }
406 createTxPatch = true;
407 }
Eric Laurent8ae73122016-04-12 10:13:29 -0700408 if (createTxPatch) { // create TX path audio patch
Eric Laurentc2730ba2014-07-20 15:47:07 -0700409 struct audio_patch patch;
Eric Laurent8ae73122016-04-12 10:13:29 -0700410
Eric Laurentc2730ba2014-07-20 15:47:07 -0700411 patch.num_sources = 1;
412 patch.num_sinks = 1;
413 deviceList = mAvailableInputDevices.getDevicesFromType(txDevice);
414 ALOG_ASSERT(!deviceList.isEmpty(),
415 "updateCallRouting() selected device not in input device list");
416 sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0);
417 txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
418 deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
419 ALOG_ASSERT(!deviceList.isEmpty(),
420 "updateCallRouting() no telephony TX device");
421 sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0);
422 txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
423
424 SortedVector<audio_io_handle_t> outputs =
425 getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700426 audio_io_handle_t output = selectOutput(outputs,
427 AUDIO_OUTPUT_FLAG_NONE,
428 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700429 // request to reuse existing output stream if one is already opened to reach the TX
430 // path output device
431 if (output != AUDIO_IO_HANDLE_NONE) {
432 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
433 ALOG_ASSERT(!outputDesc->isDuplicated(),
434 "updateCallRouting() RX device output is duplicated");
435 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700436 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700437 patch.num_sources = 2;
438 }
439
Eric Laurentc0a889f2015-10-14 14:36:34 -0700440 // terminate active capture if on the same HW module as the call TX source device
441 // FIXME: would be better to refine to only inputs whose profile connects to the
442 // call TX device but this information is not in the audio patch and logic here must be
443 // symmetric to the one in startInput()
Eric Laurentfb66dd92016-01-28 18:32:03 -0800444 Vector<sp <AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
445 for (size_t i = 0; i < activeInputs.size(); i++) {
446 sp<AudioInputDescriptor> activeDesc = activeInputs[i];
447 if (activeDesc->hasSameHwModuleAs(txSourceDeviceDesc)) {
448 AudioSessionCollection activeSessions =
449 activeDesc->getAudioSessions(true /*activeOnly*/);
450 for (size_t j = 0; j < activeSessions.size(); j++) {
451 audio_session_t activeSession = activeSessions.keyAt(j);
452 stopInput(activeDesc->mIoHandle, activeSession);
453 releaseInput(activeDesc->mIoHandle, activeSession);
454 }
Eric Laurentc0a889f2015-10-14 14:36:34 -0700455 }
456 }
457
Eric Laurentc2730ba2014-07-20 15:47:07 -0700458 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
459 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
460 ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch",
461 status);
462 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100463 mCallTxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700464 mCallTxPatch->mAfPatchHandle = afPatchHandle;
465 mCallTxPatch->mUid = mUidCached;
466 }
467 }
468}
469
Eric Laurente0720872014-03-11 09:30:41 -0700470void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700471{
472 ALOGV("setPhoneState() state %d", state);
François Gaffie2110e042015-03-24 08:41:51 +0100473 // store previous phone state for management of sonification strategy below
474 int oldState = mEngine->getPhoneState();
475
476 if (mEngine->setPhoneState(state) != NO_ERROR) {
477 ALOGW("setPhoneState() invalid or same state %d", state);
Eric Laurente552edb2014-03-10 17:42:56 -0700478 return;
479 }
François Gaffie2110e042015-03-24 08:41:51 +0100480 /// Opens: can these line be executed after the switch of volume curves???
Eric Laurente552edb2014-03-10 17:42:56 -0700481 // if leaving call state, handle special case of active streams
482 // pertaining to sonification strategy see handleIncallSonification()
Eric Laurent63dea1d2015-07-02 17:10:28 -0700483 if (isStateInCall(oldState)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700484 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent794fde22016-03-11 09:50:45 -0800485 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700486 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700487 }
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800488
Eric Laurent63dea1d2015-07-02 17:10:28 -0700489 // force reevaluating accessibility routing when call stops
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800490 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700491 }
492
François Gaffie2110e042015-03-24 08:41:51 +0100493 /**
494 * Switching to or from incall state or switching between telephony and VoIP lead to force
495 * routing command.
496 */
497 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
498 || (is_state_in_call(state) && (state != oldState)));
Eric Laurente552edb2014-03-10 17:42:56 -0700499
500 // check for device and output changes triggered by new phone state
Eric Laurente552edb2014-03-10 17:42:56 -0700501 checkA2dpSuspend();
502 checkOutputForAllStrategies();
503 updateDevicesAndOutputs();
504
Eric Laurente552edb2014-03-10 17:42:56 -0700505 int delayMs = 0;
506 if (isStateInCall(state)) {
507 nsecs_t sysTime = systemTime();
508 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700509 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700510 // mute media and sonification strategies and delay device switch by the largest
511 // latency of any output where either strategy is active.
512 // This avoid sending the ring tone or music tail into the earpiece or headset.
François Gaffiead3183e2015-03-18 16:55:35 +0100513 if ((isStrategyActive(desc, STRATEGY_MEDIA,
514 SONIFICATION_HEADSET_MUSIC_DELAY,
515 sysTime) ||
516 isStrategyActive(desc, STRATEGY_SONIFICATION,
517 SONIFICATION_HEADSET_MUSIC_DELAY,
518 sysTime)) &&
Eric Laurentc75307b2015-03-17 15:29:32 -0700519 (delayMs < (int)desc->latency()*2)) {
520 delayMs = desc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -0700521 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700522 setStrategyMute(STRATEGY_MEDIA, true, desc);
523 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700524 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Eric Laurentc75307b2015-03-17 15:29:32 -0700525 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
526 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700527 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
528 }
529 }
530
Eric Laurent87ffa392015-05-22 10:32:38 -0700531 if (hasPrimaryOutput()) {
532 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
533 // the device returned is not necessarily reachable via this output
534 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
535 // force routing command to audio hardware when ending call
536 // even if no device change is needed
537 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
538 rxDevice = mPrimaryOutput->device();
539 }
Eric Laurente552edb2014-03-10 17:42:56 -0700540
Eric Laurent87ffa392015-05-22 10:32:38 -0700541 if (state == AUDIO_MODE_IN_CALL) {
542 updateCallRouting(rxDevice, delayMs);
543 } else if (oldState == AUDIO_MODE_IN_CALL) {
544 if (mCallRxPatch != 0) {
545 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
546 mCallRxPatch.clear();
547 }
548 if (mCallTxPatch != 0) {
549 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
550 mCallTxPatch.clear();
551 }
552 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
553 } else {
554 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700555 }
Eric Laurentc2730ba2014-07-20 15:47:07 -0700556 }
Eric Laurente552edb2014-03-10 17:42:56 -0700557 // if entering in call state, handle special case of active streams
558 // pertaining to sonification strategy see handleIncallSonification()
559 if (isStateInCall(state)) {
560 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent794fde22016-03-11 09:50:45 -0800561 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700562 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700563 }
Eric Laurent63dea1d2015-07-02 17:10:28 -0700564
565 // force reevaluating accessibility routing when call starts
566 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700567 }
568
569 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700570 if (state == AUDIO_MODE_RINGTONE &&
571 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700572 mLimitRingtoneVolume = true;
573 } else {
574 mLimitRingtoneVolume = false;
575 }
576}
577
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700578audio_mode_t AudioPolicyManager::getPhoneState() {
579 return mEngine->getPhoneState();
580}
581
Eric Laurente0720872014-03-11 09:30:41 -0700582void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700583 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700584{
François Gaffie2110e042015-03-24 08:41:51 +0100585 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -0700586
François Gaffie2110e042015-03-24 08:41:51 +0100587 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
588 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
589 return;
Eric Laurente552edb2014-03-10 17:42:56 -0700590 }
François Gaffie2110e042015-03-24 08:41:51 +0100591 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
592 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
593 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
Eric Laurente552edb2014-03-10 17:42:56 -0700594
595 // check for device and output changes triggered by new force usage
596 checkA2dpSuspend();
597 checkOutputForAllStrategies();
598 updateDevicesAndOutputs();
Phil Burk09bc4612016-02-24 15:58:15 -0800599
Eric Laurent87ffa392015-05-22 10:32:38 -0700600 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700601 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
602 updateCallRouting(newDevice);
603 }
Eric Laurente552edb2014-03-10 17:42:56 -0700604 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700605 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
606 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
607 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
608 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
Eric Laurentc2730ba2014-07-20 15:47:07 -0700609 }
Eric Laurente552edb2014-03-10 17:42:56 -0700610 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700611 applyStreamVolumes(outputDesc, newDevice, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700612 }
613 }
614
Eric Laurentfb66dd92016-01-28 18:32:03 -0800615 Vector<sp <AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
616 for (size_t i = 0; i < activeInputs.size(); i++) {
617 sp<AudioInputDescriptor> activeDesc = activeInputs[i];
618 audio_devices_t newDevice = getNewInputDevice(activeDesc);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700619 // Force new input selection if the new device can not be reached via current input
Eric Laurentfb66dd92016-01-28 18:32:03 -0800620 if (activeDesc->mProfile->getSupportedDevices().types() &
621 (newDevice & ~AUDIO_DEVICE_BIT_IN)) {
622 setInputDevice(activeDesc->mIoHandle, newDevice);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700623 } else {
Eric Laurentfb66dd92016-01-28 18:32:03 -0800624 closeInput(activeDesc->mIoHandle);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700625 }
Eric Laurente552edb2014-03-10 17:42:56 -0700626 }
Eric Laurente552edb2014-03-10 17:42:56 -0700627}
628
Eric Laurente0720872014-03-11 09:30:41 -0700629void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700630{
631 ALOGV("setSystemProperty() property %s, value %s", property, value);
632}
633
634// Find a direct output profile compatible with the parameters passed, even if the input flags do
635// not explicitly request a direct output
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800636sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700637 audio_devices_t device,
638 uint32_t samplingRate,
639 audio_format_t format,
640 audio_channel_mask_t channelMask,
641 audio_output_flags_t flags)
642{
Eric Laurent861a6282015-05-18 15:40:16 -0700643 // only retain flags that will drive the direct output profile selection
644 // if explicitly requested
645 static const uint32_t kRelevantFlags =
646 (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
647 flags =
648 (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT);
649
650 sp<IOProfile> profile;
651
Eric Laurente552edb2014-03-10 17:42:56 -0700652 for (size_t i = 0; i < mHwModules.size(); i++) {
653 if (mHwModules[i]->mHandle == 0) {
654 continue;
655 }
656 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent861a6282015-05-18 15:40:16 -0700657 sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j];
658 if (!curProfile->isCompatibleProfile(device, String8(""),
Andy Hungf129b032015-04-07 13:45:50 -0700659 samplingRate, NULL /*updatedSamplingRate*/,
660 format, NULL /*updatedFormat*/,
661 channelMask, NULL /*updatedChannelMask*/,
Eric Laurent861a6282015-05-18 15:40:16 -0700662 flags)) {
663 continue;
664 }
665 // reject profiles not corresponding to a device currently available
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100666 if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) {
Eric Laurent861a6282015-05-18 15:40:16 -0700667 continue;
668 }
669 // if several profiles are compatible, give priority to one with offload capability
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100670 if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) {
Eric Laurent861a6282015-05-18 15:40:16 -0700671 continue;
672 }
673 profile = curProfile;
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100674 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent861a6282015-05-18 15:40:16 -0700675 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700676 }
Eric Laurente552edb2014-03-10 17:42:56 -0700677 }
678 }
Eric Laurent861a6282015-05-18 15:40:16 -0700679 return profile;
Eric Laurente552edb2014-03-10 17:42:56 -0700680}
681
Eric Laurente0720872014-03-11 09:30:41 -0700682audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +0100683 uint32_t samplingRate,
684 audio_format_t format,
685 audio_channel_mask_t channelMask,
686 audio_output_flags_t flags,
687 const audio_offload_info_t *offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -0700688{
Eric Laurent3b73df72014-03-11 09:06:29 -0700689 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700690 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
691 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
692 device, stream, samplingRate, format, channelMask, flags);
693
Eric Laurente83b55d2014-11-14 10:06:21 -0800694 return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE,
695 stream, samplingRate,format, channelMask,
696 flags, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700697}
698
Eric Laurente83b55d2014-11-14 10:06:21 -0800699status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
700 audio_io_handle_t *output,
701 audio_session_t session,
702 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700703 uid_t uid,
Eric Laurente83b55d2014-11-14 10:06:21 -0800704 uint32_t samplingRate,
705 audio_format_t format,
706 audio_channel_mask_t channelMask,
707 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700708 audio_port_handle_t selectedDeviceId,
Eric Laurente83b55d2014-11-14 10:06:21 -0800709 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700710{
Eric Laurente83b55d2014-11-14 10:06:21 -0800711 audio_attributes_t attributes;
712 if (attr != NULL) {
713 if (!isValidAttributes(attr)) {
714 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
715 attr->usage, attr->content_type, attr->flags,
716 attr->tags);
717 return BAD_VALUE;
718 }
719 attributes = *attr;
720 } else {
721 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
722 ALOGE("getOutputForAttr(): invalid stream type");
723 return BAD_VALUE;
724 }
725 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700726 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700727 sp<SwAudioOutputDescriptor> desc;
Jean-Michel Trivie8deced2016-02-11 12:50:39 -0800728 if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) {
François Gaffie036e1e92015-03-19 10:16:24 +0100729 ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr");
Phil Burkfdb3c072016-02-09 10:47:02 -0800730 if (!audio_has_proportional_frames(format)) {
François Gaffie036e1e92015-03-19 10:16:24 +0100731 return BAD_VALUE;
Eric Laurent275e8e92014-11-30 15:14:47 -0800732 }
François Gaffie036e1e92015-03-19 10:16:24 +0100733 *stream = streamTypefromAttributesInt(&attributes);
734 *output = desc->mIoHandle;
735 ALOGV("getOutputForAttr() returns output %d", *output);
736 return NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -0800737 }
738 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
739 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
740 return BAD_VALUE;
741 }
742
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700743 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x"
744 " session %d selectedDeviceId %d",
745 attributes.usage, attributes.content_type, attributes.tags, attributes.flags,
746 session, selectedDeviceId);
747
748 *stream = streamTypefromAttributesInt(&attributes);
749
750 // Explicit routing?
751 sp<DeviceDescriptor> deviceDesc;
752 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
753 if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) {
754 deviceDesc = mAvailableOutputDevices[i];
755 break;
756 }
757 }
758 mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700759
Eric Laurente83b55d2014-11-14 10:06:21 -0800760 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700761 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700762
Eric Laurente83b55d2014-11-14 10:06:21 -0800763 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700764 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
765 }
766
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700767 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700768 device, samplingRate, format, channelMask, flags);
769
Eric Laurente83b55d2014-11-14 10:06:21 -0800770 *output = getOutputForDevice(device, session, *stream,
771 samplingRate, format, channelMask,
772 flags, offloadInfo);
773 if (*output == AUDIO_IO_HANDLE_NONE) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700774 mOutputRoutes.removeRoute(session);
Eric Laurente83b55d2014-11-14 10:06:21 -0800775 return INVALID_OPERATION;
776 }
Paul McLeanaa981192015-03-21 09:55:15 -0700777
Eric Laurente83b55d2014-11-14 10:06:21 -0800778 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700779}
780
781audio_io_handle_t AudioPolicyManager::getOutputForDevice(
782 audio_devices_t device,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800783 audio_session_t session __unused,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700784 audio_stream_type_t stream,
785 uint32_t samplingRate,
786 audio_format_t format,
787 audio_channel_mask_t channelMask,
788 audio_output_flags_t flags,
789 const audio_offload_info_t *offloadInfo)
790{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700791 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700792 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700793
Eric Laurente552edb2014-03-10 17:42:56 -0700794#ifdef AUDIO_POLICY_TEST
795 if (mCurOutput != 0) {
796 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
797 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
798
799 if (mTestOutputs[mCurOutput] == 0) {
800 ALOGV("getOutput() opening test output");
Eric Laurentc75307b2015-03-17 15:29:32 -0700801 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
802 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700803 outputDesc->mDevice = mTestDevice;
Eric Laurente552edb2014-03-10 17:42:56 -0700804 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700805 outputDesc->mFlags =
806 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700807 outputDesc->mRefCount[stream] = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700808 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
809 config.sample_rate = mTestSamplingRate;
810 config.channel_mask = mTestChannels;
811 config.format = mTestFormat;
Phil Burk77cce802014-08-04 16:18:15 -0700812 if (offloadInfo != NULL) {
813 config.offload_info = *offloadInfo;
814 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700815 status = mpClientInterface->openOutput(0,
816 &mTestOutputs[mCurOutput],
817 &config,
818 &outputDesc->mDevice,
819 String8(""),
820 &outputDesc->mLatency,
821 outputDesc->mFlags);
822 if (status == NO_ERROR) {
823 outputDesc->mSamplingRate = config.sample_rate;
824 outputDesc->mFormat = config.format;
825 outputDesc->mChannelMask = config.channel_mask;
Eric Laurente552edb2014-03-10 17:42:56 -0700826 AudioParameter outputCmd = AudioParameter();
827 outputCmd.addInt(String8("set_id"),mCurOutput);
828 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
829 addOutput(mTestOutputs[mCurOutput], outputDesc);
830 }
831 }
832 return mTestOutputs[mCurOutput];
833 }
834#endif //AUDIO_POLICY_TEST
835
836 // open a direct output if required by specified parameters
837 //force direct flag if offload flag is set: offloading implies a direct output stream
838 // and all common behaviors are driven by checking only the direct flag
839 // this should normally be set appropriately in the policy configuration file
840 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700841 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700842 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700843 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
844 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
845 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800846 // only allow deep buffering for music stream type
847 if (stream != AUDIO_STREAM_MUSIC) {
848 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Ravi Kumar Alamanda439e4ed2015-04-03 12:13:21 -0700849 } else if (/* stream == AUDIO_STREAM_MUSIC && */
850 flags == AUDIO_OUTPUT_FLAG_NONE &&
851 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
852 // use DEEP_BUFFER as default output for music stream type
853 flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Eric Laurente83b55d2014-11-14 10:06:21 -0800854 }
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700855 if (stream == AUDIO_STREAM_TTS) {
856 flags = AUDIO_OUTPUT_FLAG_TTS;
857 }
Eric Laurente552edb2014-03-10 17:42:56 -0700858
Eric Laurentb732cf52014-09-24 19:08:21 -0700859 sp<IOProfile> profile;
860
861 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700862 // and not explicitly requested
863 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
Glenn Kasten05ddca52016-02-11 08:17:12 -0800864 audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700865 audio_channel_count_from_out_mask(channelMask) <= 2) {
866 goto non_direct_output;
867 }
868
Andy Hung2ddee192015-12-18 17:34:44 -0800869 // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled.
870 // This prevents creating an offloaded track and tearing it down immediately after start
871 // when audioflinger detects there is an active non offloadable effect.
Eric Laurente552edb2014-03-10 17:42:56 -0700872 // FIXME: We should check the audio session here but we do not have it in this context.
873 // This may prevent offloading in rare situations where effects are left active by apps
874 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700875
Eric Laurente552edb2014-03-10 17:42:56 -0700876 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
Andy Hung2ddee192015-12-18 17:34:44 -0800877 !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700878 profile = getProfileForDirectOutput(device,
879 samplingRate,
880 format,
881 channelMask,
882 (audio_output_flags_t)flags);
883 }
884
Eric Laurent1c333e22014-05-20 10:48:17 -0700885 if (profile != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700886 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700887
888 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700889 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700890 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
891 outputDesc = desc;
892 // reuse direct output if currently open and configured with same parameters
893 if ((samplingRate == outputDesc->mSamplingRate) &&
Eric Laurente6930022016-02-11 10:20:40 -0800894 audio_formats_match(format, outputDesc->mFormat) &&
Eric Laurente552edb2014-03-10 17:42:56 -0700895 (channelMask == outputDesc->mChannelMask)) {
896 outputDesc->mDirectOpenCount++;
897 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
898 return mOutputs.keyAt(i);
899 }
900 }
901 }
902 // close direct output if currently open and configured with different parameters
903 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700904 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700905 }
Eric Laurent861a6282015-05-18 15:40:16 -0700906
907 // if the selected profile is offloaded and no offload info was specified,
908 // create a default one
909 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100910 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
Eric Laurent861a6282015-05-18 15:40:16 -0700911 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
912 defaultOffloadInfo.sample_rate = samplingRate;
913 defaultOffloadInfo.channel_mask = channelMask;
914 defaultOffloadInfo.format = format;
915 defaultOffloadInfo.stream_type = stream;
916 defaultOffloadInfo.bit_rate = 0;
917 defaultOffloadInfo.duration_us = -1;
918 defaultOffloadInfo.has_video = true; // conservative
919 defaultOffloadInfo.is_streaming = true; // likely
920 offloadInfo = &defaultOffloadInfo;
921 }
922
Eric Laurentc75307b2015-03-17 15:29:32 -0700923 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700924 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -0700925 outputDesc->mLatency = 0;
Eric Laurent861a6282015-05-18 15:40:16 -0700926 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700927 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
928 config.sample_rate = samplingRate;
929 config.channel_mask = channelMask;
930 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -0700931 if (offloadInfo != NULL) {
932 config.offload_info = *offloadInfo;
933 }
Eric Laurent322b4d22015-04-03 15:57:54 -0700934 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -0700935 &output,
936 &config,
937 &outputDesc->mDevice,
938 String8(""),
939 &outputDesc->mLatency,
940 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -0700941
942 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -0700943 if (status != NO_ERROR ||
944 (samplingRate != 0 && samplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -0800945 (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) ||
Eric Laurentcf2c0212014-07-25 16:20:43 -0700946 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700947 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
948 "format %d %d, channelMask %04x %04x", output, samplingRate,
949 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
950 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700951 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700952 mpClientInterface->closeOutput(output);
953 }
Eric Laurenta82797f2015-01-30 11:49:43 -0800954 // fall back to mixer output if possible when the direct output could not be open
Glenn Kasten05ddca52016-02-11 08:17:12 -0800955 if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) {
Eric Laurenta82797f2015-01-30 11:49:43 -0800956 goto non_direct_output;
957 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700958 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -0700959 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700960 outputDesc->mSamplingRate = config.sample_rate;
961 outputDesc->mChannelMask = config.channel_mask;
962 outputDesc->mFormat = config.format;
963 outputDesc->mRefCount[stream] = 0;
964 outputDesc->mStopTime[stream] = 0;
965 outputDesc->mDirectOpenCount = 1;
966
Eric Laurente552edb2014-03-10 17:42:56 -0700967 audio_io_handle_t srcOutput = getOutputForEffect();
968 addOutput(output, outputDesc);
969 audio_io_handle_t dstOutput = getOutputForEffect();
970 if (dstOutput == output) {
971 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
972 }
973 mPreviousOutputs = mOutputs;
974 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -0700975 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700976 return output;
977 }
978
Eric Laurentb732cf52014-09-24 19:08:21 -0700979non_direct_output:
Eric Laurent14cbfca2016-03-17 09:42:16 -0700980
981 // A request for HW A/V sync cannot fallback to a mixed output because time
982 // stamps are embedded in audio data
983 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
984 return AUDIO_IO_HANDLE_NONE;
985 }
986
Eric Laurente552edb2014-03-10 17:42:56 -0700987 // ignoring channel mask due to downmix capability in mixer
988
989 // open a non direct output
990
991 // for non direct outputs, only PCM is supported
992 if (audio_is_linear_pcm(format)) {
993 // get which output is suitable for the specified stream. The actual
994 // routing change will happen when startOutput() will be called
995 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
996
Eric Laurent8838a382014-09-08 16:44:28 -0700997 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
998 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
999 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -07001000 }
1001 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1002 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1003
Paul McLeanaa981192015-03-21 09:55:15 -07001004 ALOGV(" getOutputForDevice() returns output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -07001005
1006 return output;
1007}
1008
Eric Laurente0720872014-03-11 09:30:41 -07001009audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -07001010 audio_output_flags_t flags,
1011 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -07001012{
1013 // select one output among several that provide a path to a particular device or set of
1014 // devices (the list was previously build by getOutputsForDevice()).
1015 // The priority is as follows:
1016 // 1: the output with the highest number of requested policy flags
Eric Laurente6930022016-02-11 10:20:40 -08001017 // 2: the output with the bit depth the closest to the requested one
1018 // 3: the primary output
1019 // 4: the first output in the list
Eric Laurente552edb2014-03-10 17:42:56 -07001020
1021 if (outputs.size() == 0) {
1022 return 0;
1023 }
1024 if (outputs.size() == 1) {
1025 return outputs[0];
1026 }
1027
1028 int maxCommonFlags = 0;
Eric Laurente6930022016-02-11 10:20:40 -08001029 audio_io_handle_t outputForFlags = 0;
1030 audio_io_handle_t outputForPrimary = 0;
1031 audio_io_handle_t outputForFormat = 0;
1032 audio_format_t bestFormat = AUDIO_FORMAT_INVALID;
1033 audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07001034
1035 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001036 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -07001037 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -07001038 // if a valid format is specified, skip output if not compatible
1039 if (format != AUDIO_FORMAT_INVALID) {
1040 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente6930022016-02-11 10:20:40 -08001041 if (!audio_formats_match(format, outputDesc->mFormat)) {
Eric Laurent8838a382014-09-08 16:44:28 -07001042 continue;
1043 }
1044 } else if (!audio_is_linear_pcm(format)) {
1045 continue;
1046 }
Eric Laurente6930022016-02-11 10:20:40 -08001047 if (AudioPort::isBetterFormatMatch(
1048 outputDesc->mFormat, bestFormat, format)) {
1049 outputForFormat = outputs[i];
1050 bestFormat = outputDesc->mFormat;
1051 }
Eric Laurent8838a382014-09-08 16:44:28 -07001052 }
1053
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001054 int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags);
Eric Laurente6930022016-02-11 10:20:40 -08001055 if (commonFlags >= maxCommonFlags) {
1056 if (commonFlags == maxCommonFlags) {
1057 if (AudioPort::isBetterFormatMatch(
1058 outputDesc->mFormat, bestFormatForFlags, format)) {
1059 outputForFlags = outputs[i];
1060 bestFormatForFlags = outputDesc->mFormat;
1061 }
1062 } else {
1063 outputForFlags = outputs[i];
1064 maxCommonFlags = commonFlags;
1065 bestFormatForFlags = outputDesc->mFormat;
1066 }
Eric Laurente552edb2014-03-10 17:42:56 -07001067 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
1068 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001069 if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurente6930022016-02-11 10:20:40 -08001070 outputForPrimary = outputs[i];
Eric Laurente552edb2014-03-10 17:42:56 -07001071 }
1072 }
1073 }
1074
Eric Laurente6930022016-02-11 10:20:40 -08001075 if (outputForFlags != 0) {
1076 return outputForFlags;
Eric Laurente552edb2014-03-10 17:42:56 -07001077 }
Eric Laurente6930022016-02-11 10:20:40 -08001078 if (outputForFormat != 0) {
1079 return outputForFormat;
1080 }
1081 if (outputForPrimary != 0) {
1082 return outputForPrimary;
Eric Laurente552edb2014-03-10 17:42:56 -07001083 }
1084
1085 return outputs[0];
1086}
1087
Eric Laurente0720872014-03-11 09:30:41 -07001088status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001089 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001090 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001091{
Paul McLeanaa981192015-03-21 09:55:15 -07001092 ALOGV("startOutput() output %d, stream %d, session %d",
1093 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -07001094 ssize_t index = mOutputs.indexOfKey(output);
1095 if (index < 0) {
1096 ALOGW("startOutput() unknown output %d", output);
1097 return BAD_VALUE;
1098 }
1099
Eric Laurentc75307b2015-03-17 15:29:32 -07001100 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
1101
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001102 // Routing?
1103 mOutputRoutes.incRouteActivity(session);
1104
Eric Laurentc75307b2015-03-17 15:29:32 -07001105 audio_devices_t newDevice;
Eric Laurentc40d9692016-04-13 19:14:13 -07001106 AudioMix *policyMix = NULL;
1107 const char *address = NULL;
Eric Laurentc75307b2015-03-17 15:29:32 -07001108 if (outputDesc->mPolicyMix != NULL) {
Eric Laurentc40d9692016-04-13 19:14:13 -07001109 policyMix = outputDesc->mPolicyMix;
1110 address = policyMix->mDeviceAddress.string();
1111 if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
1112 newDevice = policyMix->mDeviceType;
1113 } else {
1114 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
1115 }
Eric Laurent493404d2015-04-21 15:07:36 -07001116 } else if (mOutputRoutes.hasRouteChanged(session)) {
1117 newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001118 checkStrategyRoute(getStrategy(stream), output);
Eric Laurentc75307b2015-03-17 15:29:32 -07001119 } else {
1120 newDevice = AUDIO_DEVICE_NONE;
1121 }
1122
1123 uint32_t delayMs = 0;
1124
Eric Laurentc40d9692016-04-13 19:14:13 -07001125 status_t status = startSource(outputDesc, stream, newDevice, address, &delayMs);
Eric Laurentc75307b2015-03-17 15:29:32 -07001126
1127 if (status != NO_ERROR) {
1128 mOutputRoutes.decRouteActivity(session);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001129 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -07001130 }
1131 // Automatically enable the remote submix input when output is started on a re routing mix
1132 // of type MIX_TYPE_RECORDERS
Eric Laurentc40d9692016-04-13 19:14:13 -07001133 if (audio_is_remote_submix_device(newDevice) && policyMix != NULL &&
1134 policyMix->mMixType == MIX_TYPE_RECORDERS) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001135 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1136 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Eric Laurentc40d9692016-04-13 19:14:13 -07001137 address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001138 "remote-submix");
1139 }
1140
1141 if (delayMs != 0) {
1142 usleep(delayMs * 1000);
1143 }
1144
1145 return status;
1146}
1147
1148status_t AudioPolicyManager::startSource(sp<AudioOutputDescriptor> outputDesc,
1149 audio_stream_type_t stream,
1150 audio_devices_t device,
Eric Laurentc40d9692016-04-13 19:14:13 -07001151 const char *address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001152 uint32_t *delayMs)
1153{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001154 // cannot start playback of STREAM_TTS if any other output is being used
1155 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001156
1157 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001158 if (stream == AUDIO_STREAM_TTS) {
1159 ALOGV("\t found BEACON stream");
Eric Laurent9459fb02015-08-12 18:36:32 -07001160 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001161 return INVALID_OPERATION;
1162 } else {
1163 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1164 }
1165 } else {
1166 // some playback other than beacon starts
1167 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1168 }
1169
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001170 // check active before incrementing usage count
1171 bool force = !outputDesc->isActive();
1172
Eric Laurente552edb2014-03-10 17:42:56 -07001173 // increment usage count for this stream on the requested output:
1174 // NOTE that the usage count is the same for duplicated output and hardware output which is
1175 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1176 outputDesc->changeRefCount(stream, 1);
1177
Eric Laurent493404d2015-04-21 15:07:36 -07001178 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001179 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001180 if (device == AUDIO_DEVICE_NONE) {
1181 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001182 }
Eric Laurente552edb2014-03-10 17:42:56 -07001183 routing_strategy strategy = getStrategy(stream);
1184 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001185 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1186 (beaconMuteLatency > 0);
1187 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001188 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001189 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001190 if (desc != outputDesc) {
1191 // force a device change if any other output is managed by the same hw
1192 // module and has a current device selection that differs from selected device.
1193 // In this case, the audio HAL must receive the new device selection so that it can
1194 // change the device currently selected by the other active output.
1195 if (outputDesc->sharesHwModuleWith(desc) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07001196 desc->device() != device) {
Eric Laurente552edb2014-03-10 17:42:56 -07001197 force = true;
1198 }
1199 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001200 // a notification so that audio focus effect can propagate, or that a mute/unmute
1201 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001202 uint32_t latency = desc->latency();
1203 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1204 waitMs = latency;
1205 }
1206 }
1207 }
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07001208 (void) /*uint32_t muteWaitMs*/ setOutputDevice(outputDesc, device, force, 0, NULL, address);
Eric Laurente552edb2014-03-10 17:42:56 -07001209
1210 // handle special case for sonification while in call
1211 if (isInCall()) {
1212 handleIncallSonification(stream, true, false);
1213 }
1214
1215 // apply volume rules for current stream and device if necessary
1216 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01001217 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07001218 outputDesc,
1219 device);
Eric Laurente552edb2014-03-10 17:42:56 -07001220
1221 // update the outputs if starting an output with a stream that can affect notification
1222 // routing
1223 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001224
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001225 // force reevaluating accessibility routing when ringtone or alarm starts
1226 if (strategy == STRATEGY_SONIFICATION) {
1227 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1228 }
Eric Laurente552edb2014-03-10 17:42:56 -07001229 }
1230 return NO_ERROR;
1231}
1232
1233
Eric Laurente0720872014-03-11 09:30:41 -07001234status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001235 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001236 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001237{
1238 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1239 ssize_t index = mOutputs.indexOfKey(output);
1240 if (index < 0) {
1241 ALOGW("stopOutput() unknown output %d", output);
1242 return BAD_VALUE;
1243 }
1244
Eric Laurentc75307b2015-03-17 15:29:32 -07001245 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001246
Eric Laurentc75307b2015-03-17 15:29:32 -07001247 if (outputDesc->mRefCount[stream] == 1) {
1248 // Automatically disable the remote submix input when output is stopped on a
1249 // re routing mix of type MIX_TYPE_RECORDERS
1250 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1251 outputDesc->mPolicyMix != NULL &&
1252 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1253 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1254 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001255 outputDesc->mPolicyMix->mDeviceAddress,
Eric Laurentc75307b2015-03-17 15:29:32 -07001256 "remote-submix");
1257 }
1258 }
1259
1260 // Routing?
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001261 bool forceDeviceUpdate = false;
Eric Laurentc75307b2015-03-17 15:29:32 -07001262 if (outputDesc->mRefCount[stream] > 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001263 int activityCount = mOutputRoutes.decRouteActivity(session);
1264 forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0));
1265
1266 if (forceDeviceUpdate) {
1267 checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE);
1268 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001269 }
1270
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001271 return stopSource(outputDesc, stream, forceDeviceUpdate);
Eric Laurentc75307b2015-03-17 15:29:32 -07001272}
1273
1274status_t AudioPolicyManager::stopSource(sp<AudioOutputDescriptor> outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001275 audio_stream_type_t stream,
1276 bool forceDeviceUpdate)
Eric Laurentc75307b2015-03-17 15:29:32 -07001277{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001278 // always handle stream stop, check which stream type is stopping
1279 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1280
Eric Laurente552edb2014-03-10 17:42:56 -07001281 // handle special case for sonification while in call
1282 if (isInCall()) {
1283 handleIncallSonification(stream, false, false);
1284 }
1285
1286 if (outputDesc->mRefCount[stream] > 0) {
1287 // decrement usage count of this stream on the output
1288 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001289
Eric Laurente552edb2014-03-10 17:42:56 -07001290 // store time at which the stream was stopped - see isStreamActive()
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001291 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
Eric Laurente552edb2014-03-10 17:42:56 -07001292 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001293 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001294 // delay the device switch by twice the latency because stopOutput() is executed when
1295 // the track stop() command is received and at that time the audio track buffer can
1296 // still contain data that needs to be drained. The latency only covers the audio HAL
1297 // and kernel buffers. Also the latency does not always include additional delay in the
1298 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001299 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001300
1301 // force restoring the device selection on other active outputs if it differs from the
1302 // one being selected for this output
1303 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001304 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001305 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001306 desc->isActive() &&
1307 outputDesc->sharesHwModuleWith(desc) &&
1308 (newDevice != desc->device())) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001309 setOutputDevice(desc,
1310 getNewOutputDevice(desc, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -07001311 true,
Eric Laurentc75307b2015-03-17 15:29:32 -07001312 outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001313 }
1314 }
1315 // update the outputs if stopping one with a stream that can affect notification routing
1316 handleNotificationRoutingForStream(stream);
1317 }
1318 return NO_ERROR;
1319 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001320 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001321 return INVALID_OPERATION;
1322 }
1323}
1324
Eric Laurente83b55d2014-11-14 10:06:21 -08001325void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001326 audio_stream_type_t stream __unused,
1327 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001328{
1329 ALOGV("releaseOutput() %d", output);
1330 ssize_t index = mOutputs.indexOfKey(output);
1331 if (index < 0) {
1332 ALOGW("releaseOutput() releasing unknown output %d", output);
1333 return;
1334 }
1335
1336#ifdef AUDIO_POLICY_TEST
1337 int testIndex = testOutputIndex(output);
1338 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001339 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001340 if (outputDesc->isActive()) {
1341 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001342 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001343 mTestOutputs[testIndex] = 0;
1344 }
1345 return;
1346 }
1347#endif //AUDIO_POLICY_TEST
1348
Paul McLeanaa981192015-03-21 09:55:15 -07001349 // Routing
1350 mOutputRoutes.removeRoute(session);
1351
Eric Laurentc75307b2015-03-17 15:29:32 -07001352 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001353 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001354 if (desc->mDirectOpenCount <= 0) {
1355 ALOGW("releaseOutput() invalid open count %d for output %d",
1356 desc->mDirectOpenCount, output);
1357 return;
1358 }
1359 if (--desc->mDirectOpenCount == 0) {
1360 closeOutput(output);
1361 // If effects where present on the output, audioflinger moved them to the primary
1362 // output by default: move them back to the appropriate output.
1363 audio_io_handle_t dstOutput = getOutputForEffect();
Eric Laurent87ffa392015-05-22 10:32:38 -07001364 if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001365 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
1366 mPrimaryOutput->mIoHandle, dstOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07001367 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001368 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001369 }
1370 }
1371}
1372
1373
Eric Laurentcaf7f482014-11-25 17:50:47 -08001374status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1375 audio_io_handle_t *input,
1376 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001377 uid_t uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001378 uint32_t samplingRate,
1379 audio_format_t format,
1380 audio_channel_mask_t channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001381 audio_input_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -06001382 audio_port_handle_t selectedDeviceId,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001383 input_type_t *inputType)
Eric Laurente552edb2014-03-10 17:42:56 -07001384{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001385 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1386 "session %d, flags %#x",
1387 attr->source, samplingRate, format, channelMask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001388
Eric Laurentcaf7f482014-11-25 17:50:47 -08001389 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001390 *inputType = API_INPUT_INVALID;
Eric Laurentfb66dd92016-01-28 18:32:03 -08001391
Eric Laurent275e8e92014-11-30 15:14:47 -08001392 audio_devices_t device;
1393 // handle legacy remote submix case where the address was not always specified
1394 String8 address = String8("");
Eric Laurentc447ded2015-01-06 08:47:05 -08001395 audio_source_t inputSource = attr->source;
1396 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001397 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001398
Eric Laurentc447ded2015-01-06 08:47:05 -08001399 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1400 inputSource = AUDIO_SOURCE_MIC;
1401 }
1402 halInputSource = inputSource;
1403
Paul McLean466dc8e2015-04-17 13:15:36 -06001404 // Explicit routing?
1405 sp<DeviceDescriptor> deviceDesc;
1406 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1407 if (mAvailableInputDevices[i]->getId() == selectedDeviceId) {
1408 deviceDesc = mAvailableInputDevices[i];
1409 break;
1410 }
1411 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001412 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001413
Eric Laurentc447ded2015-01-06 08:47:05 -08001414 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001415 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001416 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
François Gaffie036e1e92015-03-19 10:16:24 +01001417 if (ret != NO_ERROR) {
1418 return ret;
1419 }
1420 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001421 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1422 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001423 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001424 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001425 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001426 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001427 return BAD_VALUE;
1428 }
Eric Laurentc722f302014-12-10 11:21:49 -08001429 if (policyMix != NULL) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001430 address = policyMix->mDeviceAddress;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001431 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1432 // there is an external policy, but this input is attached to a mix of recorders,
1433 // meaning it receives audio injected into the framework, so the recorder doesn't
1434 // know about it and is therefore considered "legacy"
1435 *inputType = API_INPUT_LEGACY;
1436 } else {
1437 // recording a mix of players defined by an external policy, we're rerouting for
1438 // an external policy
1439 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1440 }
Eric Laurentc722f302014-12-10 11:21:49 -08001441 } else if (audio_is_remote_submix_device(device)) {
1442 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001443 *inputType = API_INPUT_MIX_CAPTURE;
Eric Laurent82db2692015-08-07 13:59:42 -07001444 } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
1445 *inputType = API_INPUT_TELEPHONY_RX;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001446 } else {
1447 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001448 }
Ravi Kumar Alamandab367f5b2015-08-25 08:21:37 -07001449
Eric Laurent599c7582015-12-07 18:05:55 -08001450 }
1451
1452 *input = getInputForDevice(device, address, session, uid, inputSource,
1453 samplingRate, format, channelMask, flags,
1454 policyMix);
1455 if (*input == AUDIO_IO_HANDLE_NONE) {
1456 mInputRoutes.removeRoute(session);
1457 return INVALID_OPERATION;
1458 }
1459 ALOGV("getInputForAttr() returns input type = %d", *inputType);
1460 return NO_ERROR;
1461}
1462
1463
1464audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device,
1465 String8 address,
1466 audio_session_t session,
1467 uid_t uid,
1468 audio_source_t inputSource,
1469 uint32_t samplingRate,
1470 audio_format_t format,
1471 audio_channel_mask_t channelMask,
1472 audio_input_flags_t flags,
1473 AudioMix *policyMix)
1474{
1475 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
1476 audio_source_t halInputSource = inputSource;
1477 bool isSoundTrigger = false;
1478
1479 if (inputSource == AUDIO_SOURCE_HOTWORD) {
1480 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1481 if (index >= 0) {
1482 input = mSoundTriggerSessions.valueFor(session);
1483 isSoundTrigger = true;
1484 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1485 ALOGV("SoundTrigger capture on session %d input %d", session, input);
1486 } else {
1487 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001488 }
1489 }
1490
Andy Hungf129b032015-04-07 13:45:50 -07001491 // find a compatible input profile (not necessarily identical in parameters)
1492 sp<IOProfile> profile;
1493 // samplingRate and flags may be updated by getInputProfile
Glenn Kasten05ddca52016-02-11 08:17:12 -08001494 uint32_t profileSamplingRate = (samplingRate == 0) ? SAMPLE_RATE_HZ_DEFAULT : samplingRate;
Andy Hungf129b032015-04-07 13:45:50 -07001495 audio_format_t profileFormat = format;
1496 audio_channel_mask_t profileChannelMask = channelMask;
1497 audio_input_flags_t profileFlags = flags;
1498 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001499 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001500 profileSamplingRate, profileFormat, profileChannelMask,
1501 profileFlags);
1502 if (profile != 0) {
1503 break; // success
Eric Laurent05067782016-06-01 18:27:28 -07001504 } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) {
1505 profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry
Andy Hungf129b032015-04-07 13:45:50 -07001506 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1507 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1508 } else { // fail
Eric Laurent599c7582015-12-07 18:05:55 -08001509 ALOGW("getInputForDevice() could not find profile for device 0x%X,"
1510 "samplingRate %u, format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001511 device, samplingRate, format, channelMask, flags);
Eric Laurent599c7582015-12-07 18:05:55 -08001512 return input;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001513 }
Eric Laurente552edb2014-03-10 17:42:56 -07001514 }
Glenn Kasten05ddca52016-02-11 08:17:12 -08001515 // Pick input sampling rate if not specified by client
1516 if (samplingRate == 0) {
1517 samplingRate = profileSamplingRate;
1518 }
Eric Laurente552edb2014-03-10 17:42:56 -07001519
Eric Laurent322b4d22015-04-03 15:57:54 -07001520 if (profile->getModuleHandle() == 0) {
1521 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurent599c7582015-12-07 18:05:55 -08001522 return input;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001523 }
1524
Eric Laurent599c7582015-12-07 18:05:55 -08001525 sp<AudioSession> audioSession = new AudioSession(session,
1526 inputSource,
1527 format,
1528 samplingRate,
1529 channelMask,
1530 flags,
1531 uid,
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001532 isSoundTrigger,
1533 policyMix, mpClientInterface);
Eric Laurent599c7582015-12-07 18:05:55 -08001534
Eric Laurentfb66dd92016-01-28 18:32:03 -08001535
Eric Laurent599c7582015-12-07 18:05:55 -08001536 // reuse an open input if possible
1537 for (size_t i = 0; i < mInputs.size(); i++) {
1538 sp<AudioInputDescriptor> desc = mInputs.valueAt(i);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001539 // reuse input if:
1540 // - it shares the same profile
1541 // AND
1542 // - it is not a reroute submix input
1543 // AND
1544 // - it is: not used for sound trigger
1545 // OR
1546 // used for sound trigger and all clients use the same session ID
1547 //
1548 if ((profile == desc->mProfile) &&
1549 (isSoundTrigger == desc->isSoundTrigger()) &&
1550 !is_virtual_input_device(device)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001551
1552 sp<AudioSession> as = desc->getAudioSession(session);
1553 if (as != 0) {
1554 // do not allow unmatching properties on same session
1555 if (as->matches(audioSession)) {
1556 as->changeOpenCount(1);
1557 } else {
1558 ALOGW("getInputForDevice() record with different attributes"
1559 " exists for session %d", session);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001560 break;
Eric Laurent599c7582015-12-07 18:05:55 -08001561 }
Eric Laurentfb66dd92016-01-28 18:32:03 -08001562 } else if (isSoundTrigger) {
1563 break;
1564 }
1565 // force close input if current source is now the highest priority request on this input
1566 // and current input properties are not exactly as requested.
1567 if ((desc->mSamplingRate != samplingRate ||
1568 desc->mChannelMask != channelMask ||
Eric Laurente6930022016-02-11 10:20:40 -08001569 !audio_formats_match(desc->mFormat, format)) &&
Eric Laurentfb66dd92016-01-28 18:32:03 -08001570 (source_priority(desc->getHighestPrioritySource(false /*activeOnly*/)) <
1571 source_priority(inputSource))) {
1572 ALOGV("%s: ", __FUNCTION__);
1573 AudioSessionCollection sessions = desc->getAudioSessions(false /*activeOnly*/);
1574 for (size_t j = 0; j < sessions.size(); j++) {
1575 audio_session_t currentSession = sessions.keyAt(j);
1576 stopInput(desc->mIoHandle, currentSession);
1577 releaseInput(desc->mIoHandle, currentSession);
1578 }
1579 break;
Eric Laurent599c7582015-12-07 18:05:55 -08001580 } else {
1581 desc->addAudioSession(session, audioSession);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001582 ALOGV("%s: reusing input %d", __FUNCTION__, mInputs.keyAt(i));
1583 return mInputs.keyAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08001584 }
Eric Laurent599c7582015-12-07 18:05:55 -08001585 }
1586 }
Eric Laurent599c7582015-12-07 18:05:55 -08001587
Eric Laurentcf2c0212014-07-25 16:20:43 -07001588 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001589 config.sample_rate = profileSamplingRate;
1590 config.channel_mask = profileChannelMask;
1591 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001592
Eric Laurent322b4d22015-04-03 15:57:54 -07001593 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurent599c7582015-12-07 18:05:55 -08001594 &input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001595 &config,
1596 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001597 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001598 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001599 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001600
1601 // only accept input with the exact requested set of parameters
Eric Laurent599c7582015-12-07 18:05:55 -08001602 if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001603 (profileSamplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -08001604 !audio_formats_match(profileFormat, config.format) ||
Andy Hungf129b032015-04-07 13:45:50 -07001605 (profileChannelMask != config.channel_mask)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001606 ALOGW("getInputForAttr() failed opening input: samplingRate %d"
1607 ", format %d, channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001608 samplingRate, format, channelMask);
Eric Laurent599c7582015-12-07 18:05:55 -08001609 if (input != AUDIO_IO_HANDLE_NONE) {
1610 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001611 }
Eric Laurent599c7582015-12-07 18:05:55 -08001612 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001613 }
1614
Eric Laurent1f2f2232014-06-02 12:01:23 -07001615 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Andy Hungf129b032015-04-07 13:45:50 -07001616 inputDesc->mSamplingRate = profileSamplingRate;
1617 inputDesc->mFormat = profileFormat;
1618 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001619 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001620 inputDesc->mPolicyMix = policyMix;
Eric Laurent599c7582015-12-07 18:05:55 -08001621 inputDesc->addAudioSession(session, audioSession);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001622
Eric Laurent599c7582015-12-07 18:05:55 -08001623 addInput(input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001624 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001625
Eric Laurent599c7582015-12-07 18:05:55 -08001626 return input;
Eric Laurente552edb2014-03-10 17:42:56 -07001627}
1628
Eric Laurentfb66dd92016-01-28 18:32:03 -08001629bool AudioPolicyManager::isConcurentCaptureAllowed(const sp<AudioInputDescriptor>& inputDesc,
1630 const sp<AudioSession>& audioSession)
1631{
1632 // Do not allow capture if an active voice call is using a software patch and
1633 // the call TX source device is on the same HW module.
1634 // FIXME: would be better to refine to only inputs whose profile connects to the
1635 // call TX device but this information is not in the audio patch
1636 if (mCallTxPatch != 0 &&
1637 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1638 return false;
1639 }
1640
1641 // starting concurrent capture is enabled if:
1642 // 1) capturing for re-routing
1643 // 2) capturing for HOTWORD source
1644 // 3) capturing for FM TUNER source
1645 // 3) All other active captures are either for re-routing or HOTWORD
1646
1647 if (is_virtual_input_device(inputDesc->mDevice) ||
1648 audioSession->inputSource() == AUDIO_SOURCE_HOTWORD ||
1649 audioSession->inputSource() == AUDIO_SOURCE_FM_TUNER) {
1650 return true;
1651 }
1652
1653 Vector< sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
1654 for (size_t i = 0; i < activeInputs.size(); i++) {
1655 sp<AudioInputDescriptor> activeInput = activeInputs[i];
1656 if ((activeInput->inputSource() != AUDIO_SOURCE_HOTWORD) &&
1657 (activeInput->inputSource() != AUDIO_SOURCE_FM_TUNER) &&
1658 !is_virtual_input_device(activeInput->mDevice)) {
1659 return false;
1660 }
1661 }
1662
1663 return true;
1664}
1665
1666
Eric Laurent4dc68062014-07-28 17:26:49 -07001667status_t AudioPolicyManager::startInput(audio_io_handle_t input,
Eric Laurentfb66dd92016-01-28 18:32:03 -08001668 audio_session_t session,
1669 concurrency_type__mask_t *concurrency)
Eric Laurente552edb2014-03-10 17:42:56 -07001670{
1671 ALOGV("startInput() input %d", input);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001672 *concurrency = API_INPUT_CONCURRENCY_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001673 ssize_t index = mInputs.indexOfKey(input);
1674 if (index < 0) {
1675 ALOGW("startInput() unknown input %d", input);
1676 return BAD_VALUE;
1677 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001678 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001679
Eric Laurent599c7582015-12-07 18:05:55 -08001680 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1681 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07001682 ALOGW("startInput() unknown session %d on input %d", session, input);
1683 return BAD_VALUE;
1684 }
1685
Eric Laurentfb66dd92016-01-28 18:32:03 -08001686 if (!isConcurentCaptureAllowed(inputDesc, audioSession)) {
1687 ALOGW("startInput(%d) failed: other input already started", input);
1688 return INVALID_OPERATION;
1689 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001690
Eric Laurentfb66dd92016-01-28 18:32:03 -08001691 if (isInCall()) {
1692 *concurrency |= API_INPUT_CONCURRENCY_CALL;
1693 }
1694 if (mInputs.activeInputsCount() != 0) {
1695 *concurrency |= API_INPUT_CONCURRENCY_CAPTURE;
Eric Laurente552edb2014-03-10 17:42:56 -07001696 }
1697
Eric Laurent313d1e72016-01-29 09:56:57 -08001698 // increment activity count before calling getNewInputDevice() below as only active sessions
1699 // are considered for device selection
1700 audioSession->changeActiveCount(1);
1701
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001702 // Routing?
1703 mInputRoutes.incRouteActivity(session);
1704
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001705 if (audioSession->activeCount() == 1 || mInputRoutes.hasRouteChanged(session)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001706
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001707 setInputDevice(input, getNewInputDevice(inputDesc), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001708
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001709 if (inputDesc->getAudioSessionCount(true/*activeOnly*/) == 1) {
1710 // if input maps to a dynamic policy with an activity listener, notify of state change
1711 if ((inputDesc->mPolicyMix != NULL)
1712 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1713 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
1714 MIX_STATE_MIXING);
Eric Laurentc722f302014-12-10 11:21:49 -08001715 }
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001716
1717 if (mInputs.activeInputsCount() == 0) {
1718 SoundTrigger::setCaptureState(true);
1719 }
1720
1721 // automatically enable the remote submix output when input is started if not
1722 // used by a policy mix of type MIX_TYPE_RECORDERS
1723 // For remote submix (a virtual device), we open only one input per capture request.
1724 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1725 String8 address = String8("");
1726 if (inputDesc->mPolicyMix == NULL) {
1727 address = String8("0");
1728 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1729 address = inputDesc->mPolicyMix->mDeviceAddress;
1730 }
1731 if (address != "") {
1732 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1733 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1734 address, "remote-submix");
1735 }
Eric Laurentc722f302014-12-10 11:21:49 -08001736 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001737 }
Eric Laurente552edb2014-03-10 17:42:56 -07001738 }
1739
Eric Laurent599c7582015-12-07 18:05:55 -08001740 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Eric Laurente552edb2014-03-10 17:42:56 -07001741
Eric Laurente552edb2014-03-10 17:42:56 -07001742 return NO_ERROR;
1743}
1744
Eric Laurent4dc68062014-07-28 17:26:49 -07001745status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1746 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001747{
1748 ALOGV("stopInput() input %d", input);
1749 ssize_t index = mInputs.indexOfKey(input);
1750 if (index < 0) {
1751 ALOGW("stopInput() unknown input %d", input);
1752 return BAD_VALUE;
1753 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001754 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001755
Eric Laurent599c7582015-12-07 18:05:55 -08001756 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001757 if (index < 0) {
1758 ALOGW("stopInput() unknown session %d on input %d", session, input);
1759 return BAD_VALUE;
1760 }
1761
Eric Laurent599c7582015-12-07 18:05:55 -08001762 if (audioSession->activeCount() == 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07001763 ALOGW("stopInput() input %d already stopped", input);
1764 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001765 }
1766
Eric Laurent599c7582015-12-07 18:05:55 -08001767 audioSession->changeActiveCount(-1);
Paul McLean466dc8e2015-04-17 13:15:36 -06001768
1769 // Routing?
1770 mInputRoutes.decRouteActivity(session);
1771
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001772 if (audioSession->activeCount() == 0) {
Eric Laurent84332aa2016-01-28 22:19:18 +00001773
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001774 if (inputDesc->isActive()) {
1775 setInputDevice(input, getNewInputDevice(inputDesc), false /* force */);
1776 } else {
1777 // if input maps to a dynamic policy with an activity listener, notify of state change
1778 if ((inputDesc->mPolicyMix != NULL)
1779 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1780 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
1781 MIX_STATE_IDLE);
Eric Laurent84332aa2016-01-28 22:19:18 +00001782 }
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001783
1784 // automatically disable the remote submix output when input is stopped if not
1785 // used by a policy mix of type MIX_TYPE_RECORDERS
1786 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1787 String8 address = String8("");
1788 if (inputDesc->mPolicyMix == NULL) {
1789 address = String8("0");
1790 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1791 address = inputDesc->mPolicyMix->mDeviceAddress;
1792 }
1793 if (address != "") {
1794 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1795 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1796 address, "remote-submix");
1797 }
Eric Laurent84332aa2016-01-28 22:19:18 +00001798 }
Eric Laurent84332aa2016-01-28 22:19:18 +00001799
Eric Laurentfb66dd92016-01-28 18:32:03 -08001800 resetInputDevice(input);
Eric Laurent84332aa2016-01-28 22:19:18 +00001801
Eric Laurentfb66dd92016-01-28 18:32:03 -08001802 if (mInputs.activeInputsCount() == 0) {
1803 SoundTrigger::setCaptureState(false);
1804 }
1805 inputDesc->clearPreemptedSessions();
Eric Laurent84332aa2016-01-28 22:19:18 +00001806 }
Eric Laurente552edb2014-03-10 17:42:56 -07001807 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001808 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001809}
1810
Eric Laurent4dc68062014-07-28 17:26:49 -07001811void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1812 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001813{
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001814
Eric Laurente552edb2014-03-10 17:42:56 -07001815 ALOGV("releaseInput() %d", input);
1816 ssize_t index = mInputs.indexOfKey(input);
1817 if (index < 0) {
1818 ALOGW("releaseInput() releasing unknown input %d", input);
1819 return;
1820 }
Paul McLean466dc8e2015-04-17 13:15:36 -06001821
1822 // Routing
1823 mInputRoutes.removeRoute(session);
1824
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001825 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1826 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001827
Eric Laurent599c7582015-12-07 18:05:55 -08001828 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001829 if (index < 0) {
1830 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1831 return;
1832 }
Eric Laurent599c7582015-12-07 18:05:55 -08001833
1834 if (audioSession->openCount() == 0) {
1835 ALOGW("releaseInput() invalid open count %d on session %d",
1836 audioSession->openCount(), session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001837 return;
1838 }
Eric Laurent599c7582015-12-07 18:05:55 -08001839
1840 if (audioSession->changeOpenCount(-1) == 0) {
1841 inputDesc->removeAudioSession(session);
1842 }
1843
Jean-Michel Trivi65bfe912015-12-04 15:56:47 -08001844 if (inputDesc->getOpenRefCount() > 0) {
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001845 ALOGV("releaseInput() exit > 0");
1846 return;
1847 }
1848
Eric Laurent05b90f82014-08-27 15:32:29 -07001849 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001850 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001851 ALOGV("releaseInput() exit");
1852}
1853
Eric Laurentd4692962014-05-05 18:13:44 -07001854void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001855 bool patchRemoved = false;
1856
Eric Laurentd4692962014-05-05 18:13:44 -07001857 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001858 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001859 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07001860 if (patch_index >= 0) {
1861 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07001862 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07001863 mAudioPatches.removeItemsAt(patch_index);
1864 patchRemoved = true;
1865 }
Eric Laurentd4692962014-05-05 18:13:44 -07001866 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1867 }
1868 mInputs.clear();
Chris Thornton52785f32016-02-09 17:28:49 -08001869 SoundTrigger::setCaptureState(false);
Eric Laurent6a94d692014-05-20 11:18:06 -07001870 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001871
1872 if (patchRemoved) {
1873 mpClientInterface->onAudioPatchListUpdate();
1874 }
Eric Laurentd4692962014-05-05 18:13:44 -07001875}
1876
Eric Laurente0720872014-03-11 09:30:41 -07001877void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001878 int indexMin,
1879 int indexMax)
1880{
1881 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffied1ab2bd2015-12-02 18:20:06 +01001882 mVolumeCurves->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent28d09f02016-03-08 10:43:05 -08001883
1884 // initialize other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08001885 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1886 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001887 continue;
1888 }
1889 mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001890 }
Eric Laurente552edb2014-03-10 17:42:56 -07001891}
1892
Eric Laurente0720872014-03-11 09:30:41 -07001893status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01001894 int index,
1895 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07001896{
1897
François Gaffied1ab2bd2015-12-02 18:20:06 +01001898 if ((index < mVolumeCurves->getVolumeIndexMin(stream)) ||
1899 (index > mVolumeCurves->getVolumeIndexMax(stream))) {
Eric Laurente552edb2014-03-10 17:42:56 -07001900 return BAD_VALUE;
1901 }
1902 if (!audio_is_output_device(device)) {
1903 return BAD_VALUE;
1904 }
1905
1906 // Force max volume if stream cannot be muted
François Gaffied1ab2bd2015-12-02 18:20:06 +01001907 if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001908
Eric Laurent1fd372e2016-04-06 14:23:53 -07001909 ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d",
Eric Laurente552edb2014-03-10 17:42:56 -07001910 stream, device, index);
1911
Eric Laurent28d09f02016-03-08 10:43:05 -08001912 // update other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08001913 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1914 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001915 continue;
1916 }
1917 mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index);
1918 }
Eric Laurente552edb2014-03-10 17:42:56 -07001919
Eric Laurent1fd372e2016-04-06 14:23:53 -07001920 // update volume on all outputs and streams matching the following:
1921 // - The requested stream (or a stream matching for volume control) is active on the output
1922 // - The device (or devices) selected by the strategy corresponding to this stream includes
1923 // the requested device
1924 // - For non default requested device, currently selected device on the output is either the
1925 // requested device or one of the devices selected by the strategy
Eric Laurent5a2b6292016-04-14 18:05:57 -07001926 // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if
1927 // no specific device volume value exists for currently selected device.
Eric Laurente552edb2014-03-10 17:42:56 -07001928 status_t status = NO_ERROR;
1929 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001930 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1931 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent794fde22016-03-11 09:50:45 -08001932 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1933 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001934 continue;
Eric Laurente552edb2014-03-10 17:42:56 -07001935 }
Eric Laurentd3926fe2016-04-15 18:10:07 -07001936 if (!(desc->isStreamActive((audio_stream_type_t)curStream) ||
1937 (isInCall() && (curStream == AUDIO_STREAM_VOICE_CALL)))) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07001938 continue;
1939 }
Eric Laurent794fde22016-03-11 09:50:45 -08001940 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08001941 audio_devices_t curStreamDevice = getDeviceForStrategy(curStrategy, true /*fromCache*/);
Eric Laurent1fd372e2016-04-06 14:23:53 -07001942 if ((curStreamDevice & device) == 0) {
1943 continue;
1944 }
1945 bool applyDefault = false;
Eric Laurent5a2b6292016-04-14 18:05:57 -07001946 if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07001947 curStreamDevice |= device;
1948 } else if (!mVolumeCurves->hasVolumeIndexForDevice(
1949 stream, Volume::getDeviceForVolume(curStreamDevice))) {
1950 applyDefault = true;
1951 }
Eric Laurent28d09f02016-03-08 10:43:05 -08001952
Eric Laurent1fd372e2016-04-06 14:23:53 -07001953 if (applyDefault || ((curDevice & curStreamDevice) != 0)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001954 status_t volStatus =
1955 checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice);
1956 if (volStatus != NO_ERROR) {
1957 status = volStatus;
1958 }
1959 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001960 }
Eric Laurente552edb2014-03-10 17:42:56 -07001961 }
1962 return status;
1963}
1964
Eric Laurente0720872014-03-11 09:30:41 -07001965status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001966 int *index,
1967 audio_devices_t device)
1968{
1969 if (index == NULL) {
1970 return BAD_VALUE;
1971 }
1972 if (!audio_is_output_device(device)) {
1973 return BAD_VALUE;
1974 }
Eric Laurent5a2b6292016-04-14 18:05:57 -07001975 // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device corresponding to
Eric Laurente552edb2014-03-10 17:42:56 -07001976 // the strategy the stream belongs to.
Eric Laurent5a2b6292016-04-14 18:05:57 -07001977 if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurente552edb2014-03-10 17:42:56 -07001978 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1979 }
François Gaffiedfd74092015-03-19 12:10:59 +01001980 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001981
François Gaffied1ab2bd2015-12-02 18:20:06 +01001982 *index = mVolumeCurves->getVolumeIndex(stream, device);
Eric Laurente552edb2014-03-10 17:42:56 -07001983 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1984 return NO_ERROR;
1985}
1986
Eric Laurente0720872014-03-11 09:30:41 -07001987audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001988 const SortedVector<audio_io_handle_t>& outputs)
1989{
1990 // select one output among several suitable for global effects.
1991 // The priority is as follows:
1992 // 1: An offloaded output. If the effect ends up not being offloadable,
1993 // AudioFlinger will invalidate the track and the offloaded output
1994 // will be closed causing the effect to be moved to a PCM output.
1995 // 2: A deep buffer output
1996 // 3: the first output in the list
1997
1998 if (outputs.size() == 0) {
1999 return 0;
2000 }
2001
2002 audio_io_handle_t outputOffloaded = 0;
2003 audio_io_handle_t outputDeepBuffer = 0;
2004
2005 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002006 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07002007 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07002008 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
2009 outputOffloaded = outputs[i];
2010 }
2011 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
2012 outputDeepBuffer = outputs[i];
2013 }
2014 }
2015
2016 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
2017 outputOffloaded, outputDeepBuffer);
2018 if (outputOffloaded != 0) {
2019 return outputOffloaded;
2020 }
2021 if (outputDeepBuffer != 0) {
2022 return outputDeepBuffer;
2023 }
2024
2025 return outputs[0];
2026}
2027
Eric Laurente0720872014-03-11 09:30:41 -07002028audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07002029{
2030 // apply simple rule where global effects are attached to the same output as MUSIC streams
2031
Eric Laurent3b73df72014-03-11 09:06:29 -07002032 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07002033 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2034 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
2035
2036 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
2037 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
2038 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
2039
2040 return output;
2041}
2042
Eric Laurente0720872014-03-11 09:30:41 -07002043status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07002044 audio_io_handle_t io,
2045 uint32_t strategy,
2046 int session,
2047 int id)
2048{
2049 ssize_t index = mOutputs.indexOfKey(io);
2050 if (index < 0) {
2051 index = mInputs.indexOfKey(io);
2052 if (index < 0) {
2053 ALOGW("registerEffect() unknown io %d", io);
2054 return INVALID_OPERATION;
2055 }
2056 }
François Gaffie45ed3b02015-03-19 10:35:14 +01002057 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07002058}
2059
Eric Laurentc75307b2015-03-17 15:29:32 -07002060bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
2061{
Eric Laurent28d09f02016-03-08 10:43:05 -08002062 bool active = false;
Eric Laurent794fde22016-03-11 09:50:45 -08002063 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) {
2064 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002065 continue;
2066 }
2067 active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs);
2068 }
Eric Laurent28d09f02016-03-08 10:43:05 -08002069 return active;
Eric Laurentc75307b2015-03-17 15:29:32 -07002070}
2071
2072bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
2073{
2074 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
2075}
2076
Eric Laurente0720872014-03-11 09:30:41 -07002077bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07002078{
2079 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002080 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002081 if (inputDescriptor->isSourceActive(source)) {
Eric Laurente552edb2014-03-10 17:42:56 -07002082 return true;
2083 }
2084 }
2085 return false;
2086}
2087
Eric Laurent275e8e92014-11-30 15:14:47 -08002088// Register a list of custom mixes with their attributes and format.
2089// When a mix is registered, corresponding input and output profiles are
2090// added to the remote submix hw module. The profile contains only the
2091// parameters (sampling rate, format...) specified by the mix.
2092// The corresponding input remote submix device is also connected.
2093//
2094// When a remote submix device is connected, the address is checked to select the
2095// appropriate profile and the corresponding input or output stream is opened.
2096//
2097// When capture starts, getInputForAttr() will:
2098// - 1 look for a mix matching the address passed in attribtutes tags if any
2099// - 2 if none found, getDeviceForInputSource() will:
2100// - 2.1 look for a mix matching the attributes source
2101// - 2.2 if none found, default to device selection by policy rules
2102// At this time, the corresponding output remote submix device is also connected
2103// and active playback use cases can be transferred to this mix if needed when reconnecting
2104// after AudioTracks are invalidated
2105//
2106// When playback starts, getOutputForAttr() will:
2107// - 1 look for a mix matching the address passed in attribtutes tags if any
2108// - 2 if none found, look for a mix matching the attributes usage
2109// - 3 if none found, default to device and output selection by policy rules.
2110
2111status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes)
2112{
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002113 ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size());
2114 status_t res = NO_ERROR;
2115
2116 sp<HwModule> rSubmixModule;
2117 // examine each mix's route type
2118 for (size_t i = 0; i < mixes.size(); i++) {
2119 // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination
2120 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) {
2121 res = INVALID_OPERATION;
Eric Laurent275e8e92014-11-30 15:14:47 -08002122 break;
2123 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002124 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
2125 // Loop back through "remote submix"
2126 if (rSubmixModule == 0) {
2127 for (size_t j = 0; i < mHwModules.size(); j++) {
2128 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2129 && mHwModules[j]->mHandle != 0) {
2130 rSubmixModule = mHwModules[j];
2131 break;
2132 }
2133 }
2134 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002135
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002136 ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size());
Eric Laurent275e8e92014-11-30 15:14:47 -08002137
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002138 if (rSubmixModule == 0) {
2139 ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", i);
2140 res = INVALID_OPERATION;
2141 break;
2142 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002143
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002144 String8 address = mixes[i].mDeviceAddress;
François Gaffie036e1e92015-03-19 10:16:24 +01002145
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002146 if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) {
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002147 ALOGE(" Error registering mix %zu for address %s", i, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002148 res = INVALID_OPERATION;
2149 break;
2150 }
2151 audio_config_t outputConfig = mixes[i].mFormat;
2152 audio_config_t inputConfig = mixes[i].mFormat;
2153 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
2154 // stereo and let audio flinger do the channel conversion if needed.
2155 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2156 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
2157 rSubmixModule->addOutputProfile(address, &outputConfig,
2158 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
2159 rSubmixModule->addInputProfile(address, &inputConfig,
2160 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01002161
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002162 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
2163 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2164 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2165 address.string(), "remote-submix");
2166 } else {
2167 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2168 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2169 address.string(), "remote-submix");
2170 }
2171 } else if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002172 String8 address = mixes[i].mDeviceAddress;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002173 audio_devices_t device = mixes[i].mDeviceType;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002174 ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s",
2175 i, mixes.size(), device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002176
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002177 bool foundOutput = false;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002178 for (size_t j = 0 ; j < mOutputs.size() ; j++) {
2179 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j);
2180 sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle());
2181 if ((patch != 0) && (patch->mPatch.num_sinks != 0)
2182 && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE)
2183 && (patch->mPatch.sinks[0].ext.device.type == device)
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002184 && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(),
2185 AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002186 if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) {
2187 res = INVALID_OPERATION;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002188 } else {
2189 foundOutput = true;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002190 }
2191 break;
2192 }
2193 }
2194
2195 if (res != NO_ERROR) {
2196 ALOGE(" Error registering mix %zu for device 0x%X addr %s",
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002197 i, device, address.string());
2198 res = INVALID_OPERATION;
2199 break;
2200 } else if (!foundOutput) {
2201 ALOGE(" Output not found for mix %zu for device 0x%X addr %s",
2202 i, device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002203 res = INVALID_OPERATION;
2204 break;
2205 }
Eric Laurentc722f302014-12-10 11:21:49 -08002206 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002207 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002208 if (res != NO_ERROR) {
2209 unregisterPolicyMixes(mixes);
2210 }
2211 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002212}
2213
2214status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
2215{
Eric Laurent7b279bb2015-12-14 10:18:23 -08002216 ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002217 status_t res = NO_ERROR;
2218 sp<HwModule> rSubmixModule;
2219 // examine each mix's route type
Eric Laurent275e8e92014-11-30 15:14:47 -08002220 for (size_t i = 0; i < mixes.size(); i++) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002221 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
François Gaffie036e1e92015-03-19 10:16:24 +01002222
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002223 if (rSubmixModule == 0) {
2224 for (size_t j = 0; i < mHwModules.size(); j++) {
2225 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2226 && mHwModules[j]->mHandle != 0) {
2227 rSubmixModule = mHwModules[j];
2228 break;
2229 }
2230 }
2231 }
2232 if (rSubmixModule == 0) {
2233 res = INVALID_OPERATION;
2234 continue;
2235 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002236
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002237 String8 address = mixes[i].mDeviceAddress;
Eric Laurent275e8e92014-11-30 15:14:47 -08002238
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002239 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
2240 res = INVALID_OPERATION;
2241 continue;
2242 }
2243
2244 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
2245 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2246 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2247 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2248 address.string(), "remote-submix");
2249 }
2250 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
2251 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2252 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2253 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2254 address.string(), "remote-submix");
2255 }
2256 rSubmixModule->removeOutputProfile(address);
2257 rSubmixModule->removeInputProfile(address);
2258
2259 } if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
2260 if (mPolicyMixes.unregisterMix(mixes[i].mDeviceAddress) != NO_ERROR) {
2261 res = INVALID_OPERATION;
2262 continue;
2263 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002264 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002265 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002266 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002267}
2268
Eric Laurente552edb2014-03-10 17:42:56 -07002269
Eric Laurente0720872014-03-11 09:30:41 -07002270status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07002271{
2272 const size_t SIZE = 256;
2273 char buffer[SIZE];
2274 String8 result;
2275
2276 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
2277 result.append(buffer);
2278
Eric Laurent87ffa392015-05-22 10:32:38 -07002279 snprintf(buffer, SIZE, " Primary Output: %d\n",
2280 hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE);
Eric Laurente552edb2014-03-10 17:42:56 -07002281 result.append(buffer);
Mikhail Naganov0d6a0332016-04-19 17:12:38 -07002282 std::string stateLiteral;
2283 AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral);
2284 snprintf(buffer, SIZE, " Phone state: %s\n", stateLiteral.c_str());
Eric Laurente552edb2014-03-10 17:42:56 -07002285 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002286 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002287 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07002288 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002289 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07002290 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002291 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07002292 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002293 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07002294 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002295 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07002296 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002297 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002298 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002299 result.append(buffer);
Phil Burk09bc4612016-02-24 15:58:15 -08002300 snprintf(buffer, SIZE, " Force use for encoded surround output %d\n",
2301 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND));
2302 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002303 snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available");
2304 result.append(buffer);
Andy Hung2ddee192015-12-18 17:34:44 -08002305 snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off");
2306 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002307
François Gaffie2110e042015-03-24 08:41:51 +01002308 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07002309
François Gaffie112b0af2015-11-19 16:13:25 +01002310 mAvailableOutputDevices.dump(fd, String8("Available output"));
2311 mAvailableInputDevices.dump(fd, String8("Available input"));
François Gaffie53615e22015-03-19 09:24:12 +01002312 mHwModules.dump(fd);
2313 mOutputs.dump(fd);
2314 mInputs.dump(fd);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002315 mVolumeCurves->dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01002316 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002317 mAudioPatches.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07002318
2319 return NO_ERROR;
2320}
2321
2322// This function checks for the parameters which can be offloaded.
2323// This can be enhanced depending on the capability of the DSP and policy
2324// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002325bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002326{
2327 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002328 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002329 offloadInfo.sample_rate, offloadInfo.channel_mask,
2330 offloadInfo.format,
2331 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2332 offloadInfo.has_video);
2333
Andy Hung2ddee192015-12-18 17:34:44 -08002334 if (mMasterMono) {
2335 return false; // no offloading if mono is set.
2336 }
2337
Eric Laurente552edb2014-03-10 17:42:56 -07002338 // Check if offload has been disabled
2339 char propValue[PROPERTY_VALUE_MAX];
2340 if (property_get("audio.offload.disable", propValue, "0")) {
2341 if (atoi(propValue) != 0) {
2342 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2343 return false;
2344 }
2345 }
2346
2347 // Check if stream type is music, then only allow offload as of now.
2348 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2349 {
2350 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2351 return false;
2352 }
2353
2354 //TODO: enable audio offloading with video when ready
Andy Hung08945c42015-05-31 21:36:46 -07002355 const bool allowOffloadWithVideo =
2356 property_get_bool("audio.offload.video", false /* default_value */);
2357 if (offloadInfo.has_video && !allowOffloadWithVideo) {
Eric Laurente552edb2014-03-10 17:42:56 -07002358 ALOGV("isOffloadSupported: has_video == true, returning false");
2359 return false;
2360 }
2361
2362 //If duration is less than minimum value defined in property, return false
2363 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2364 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2365 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2366 return false;
2367 }
2368 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2369 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2370 return false;
2371 }
2372
2373 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2374 // creating an offloaded track and tearing it down immediately after start when audioflinger
2375 // detects there is an active non offloadable effect.
2376 // FIXME: We should check the audio session here but we do not have it in this context.
2377 // This may prevent offloading in rare situations where effects are left active by apps
2378 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002379 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002380 return false;
2381 }
2382
2383 // See if there is a profile to support this.
2384 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002385 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002386 offloadInfo.sample_rate,
2387 offloadInfo.format,
2388 offloadInfo.channel_mask,
2389 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002390 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2391 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002392}
2393
Eric Laurent6a94d692014-05-20 11:18:06 -07002394status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2395 audio_port_type_t type,
2396 unsigned int *num_ports,
2397 struct audio_port *ports,
2398 unsigned int *generation)
2399{
2400 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2401 generation == NULL) {
2402 return BAD_VALUE;
2403 }
2404 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2405 if (ports == NULL) {
2406 *num_ports = 0;
2407 }
2408
2409 size_t portsWritten = 0;
2410 size_t portsMax = *num_ports;
2411 *num_ports = 0;
2412 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002413 // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB
2414 // as they are used by stub HALs by convention
Eric Laurent6a94d692014-05-20 11:18:06 -07002415 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002416 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
2417 if (mAvailableOutputDevices[i]->type() == AUDIO_DEVICE_OUT_STUB) {
2418 continue;
2419 }
2420 if (portsWritten < portsMax) {
2421 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2422 }
2423 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002424 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002425 }
2426 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002427 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
2428 if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_STUB) {
2429 continue;
2430 }
2431 if (portsWritten < portsMax) {
2432 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2433 }
2434 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002435 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002436 }
2437 }
2438 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2439 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2440 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2441 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2442 }
2443 *num_ports += mInputs.size();
2444 }
2445 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002446 size_t numOutputs = 0;
2447 for (size_t i = 0; i < mOutputs.size(); i++) {
2448 if (!mOutputs[i]->isDuplicated()) {
2449 numOutputs++;
2450 if (portsWritten < portsMax) {
2451 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2452 }
2453 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002454 }
Eric Laurent84c70242014-06-23 08:46:27 -07002455 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002456 }
2457 }
2458 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002459 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002460 return NO_ERROR;
2461}
2462
2463status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2464{
2465 return NO_ERROR;
2466}
2467
Eric Laurent6a94d692014-05-20 11:18:06 -07002468status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2469 audio_patch_handle_t *handle,
2470 uid_t uid)
2471{
2472 ALOGV("createAudioPatch()");
2473
2474 if (handle == NULL || patch == NULL) {
2475 return BAD_VALUE;
2476 }
2477 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2478
Eric Laurent874c42872014-08-08 15:13:39 -07002479 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2480 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2481 return BAD_VALUE;
2482 }
2483 // only one source per audio patch supported for now
2484 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002485 return INVALID_OPERATION;
2486 }
Eric Laurent874c42872014-08-08 15:13:39 -07002487
2488 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002489 return INVALID_OPERATION;
2490 }
Eric Laurent874c42872014-08-08 15:13:39 -07002491 for (size_t i = 0; i < patch->num_sinks; i++) {
2492 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2493 return INVALID_OPERATION;
2494 }
2495 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002496
2497 sp<AudioPatch> patchDesc;
2498 ssize_t index = mAudioPatches.indexOfKey(*handle);
2499
Eric Laurent6a94d692014-05-20 11:18:06 -07002500 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2501 patch->sources[0].role,
2502 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002503#if LOG_NDEBUG == 0
2504 for (size_t i = 0; i < patch->num_sinks; i++) {
Eric Laurent7b279bb2015-12-14 10:18:23 -08002505 ALOGV("createAudioPatch sink %zu: id %d role %d type %d", i, patch->sinks[i].id,
Eric Laurent874c42872014-08-08 15:13:39 -07002506 patch->sinks[i].role,
2507 patch->sinks[i].type);
2508 }
2509#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002510
2511 if (index >= 0) {
2512 patchDesc = mAudioPatches.valueAt(index);
2513 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2514 mUidCached, patchDesc->mUid, uid);
2515 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2516 return INVALID_OPERATION;
2517 }
2518 } else {
Glenn Kastena13cde92016-03-28 15:26:02 -07002519 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07002520 }
2521
2522 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002523 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002524 if (outputDesc == NULL) {
2525 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2526 return BAD_VALUE;
2527 }
Eric Laurent84c70242014-06-23 08:46:27 -07002528 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2529 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002530 if (patchDesc != 0) {
2531 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2532 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2533 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2534 return BAD_VALUE;
2535 }
2536 }
Eric Laurent874c42872014-08-08 15:13:39 -07002537 DeviceVector devices;
2538 for (size_t i = 0; i < patch->num_sinks; i++) {
2539 // Only support mix to devices connection
2540 // TODO add support for mix to mix connection
2541 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2542 ALOGV("createAudioPatch() source mix but sink is not a device");
2543 return INVALID_OPERATION;
2544 }
2545 sp<DeviceDescriptor> devDesc =
2546 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2547 if (devDesc == 0) {
2548 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2549 return BAD_VALUE;
2550 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002551
François Gaffie53615e22015-03-19 09:24:12 +01002552 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002553 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002554 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002555 NULL, // updatedSamplingRate
2556 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002557 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002558 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002559 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002560 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002561 ALOGV("createAudioPatch() profile not supported for device %08x",
2562 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002563 return INVALID_OPERATION;
2564 }
2565 devices.add(devDesc);
2566 }
2567 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002568 return INVALID_OPERATION;
2569 }
Eric Laurent874c42872014-08-08 15:13:39 -07002570
Eric Laurent6a94d692014-05-20 11:18:06 -07002571 // TODO: reconfigure output format and channels here
2572 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002573 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002574 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002575 index = mAudioPatches.indexOfKey(*handle);
2576 if (index >= 0) {
2577 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2578 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2579 }
2580 patchDesc = mAudioPatches.valueAt(index);
2581 patchDesc->mUid = uid;
2582 ALOGV("createAudioPatch() success");
2583 } else {
2584 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2585 return INVALID_OPERATION;
2586 }
2587 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2588 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2589 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002590 // only one sink supported when connecting an input device to a mix
2591 if (patch->num_sinks > 1) {
2592 return INVALID_OPERATION;
2593 }
François Gaffie53615e22015-03-19 09:24:12 +01002594 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002595 if (inputDesc == NULL) {
2596 return BAD_VALUE;
2597 }
2598 if (patchDesc != 0) {
2599 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2600 return BAD_VALUE;
2601 }
2602 }
2603 sp<DeviceDescriptor> devDesc =
2604 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2605 if (devDesc == 0) {
2606 return BAD_VALUE;
2607 }
2608
François Gaffie53615e22015-03-19 09:24:12 +01002609 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002610 devDesc->mAddress,
2611 patch->sinks[0].sample_rate,
2612 NULL, /*updatedSampleRate*/
2613 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002614 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002615 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002616 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002617 // FIXME for the parameter type,
2618 // and the NONE
2619 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002620 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002621 return INVALID_OPERATION;
2622 }
2623 // TODO: reconfigure output format and channels here
2624 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002625 devDesc->type(), inputDesc->mIoHandle);
2626 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002627 index = mAudioPatches.indexOfKey(*handle);
2628 if (index >= 0) {
2629 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2630 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2631 }
2632 patchDesc = mAudioPatches.valueAt(index);
2633 patchDesc->mUid = uid;
2634 ALOGV("createAudioPatch() success");
2635 } else {
2636 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2637 return INVALID_OPERATION;
2638 }
2639 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2640 // device to device connection
2641 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002642 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002643 return BAD_VALUE;
2644 }
2645 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002646 sp<DeviceDescriptor> srcDeviceDesc =
2647 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002648 if (srcDeviceDesc == 0) {
2649 return BAD_VALUE;
2650 }
Eric Laurent874c42872014-08-08 15:13:39 -07002651
Eric Laurent6a94d692014-05-20 11:18:06 -07002652 //update source and sink with our own data as the data passed in the patch may
2653 // be incomplete.
2654 struct audio_patch newPatch = *patch;
2655 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002656
Eric Laurent874c42872014-08-08 15:13:39 -07002657 for (size_t i = 0; i < patch->num_sinks; i++) {
2658 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2659 ALOGV("createAudioPatch() source device but one sink is not a device");
2660 return INVALID_OPERATION;
2661 }
2662
2663 sp<DeviceDescriptor> sinkDeviceDesc =
2664 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2665 if (sinkDeviceDesc == 0) {
2666 return BAD_VALUE;
2667 }
2668 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2669
Eric Laurent3bcf8592015-04-03 12:13:24 -07002670 // create a software bridge in PatchPanel if:
2671 // - source and sink devices are on differnt HW modules OR
2672 // - audio HAL version is < 3.0
Eric Laurentfb66dd92016-01-28 18:32:03 -08002673 if (!srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01002674 (srcDeviceDesc->mModule->getHalVersion() < AUDIO_DEVICE_API_VERSION_3_0)) {
Eric Laurent3bcf8592015-04-03 12:13:24 -07002675 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002676 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002677 return INVALID_OPERATION;
2678 }
Eric Laurent874c42872014-08-08 15:13:39 -07002679 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002680 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002681 // if the sink device is reachable via an opened output stream, request to go via
2682 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002683 audio_io_handle_t output = selectOutput(outputs,
2684 AUDIO_OUTPUT_FLAG_NONE,
2685 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002686 if (output != AUDIO_IO_HANDLE_NONE) {
2687 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2688 if (outputDesc->isDuplicated()) {
2689 return INVALID_OPERATION;
2690 }
2691 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002692 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002693 newPatch.num_sources = 2;
2694 }
Eric Laurent83b88082014-06-20 18:31:16 -07002695 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002696 }
2697 // TODO: check from routing capabilities in config file and other conflicting patches
2698
2699 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2700 if (index >= 0) {
2701 afPatchHandle = patchDesc->mAfPatchHandle;
2702 }
2703
2704 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2705 &afPatchHandle,
2706 0);
2707 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2708 status, afPatchHandle);
2709 if (status == NO_ERROR) {
2710 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002711 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002712 addAudioPatch(patchDesc->mHandle, patchDesc);
2713 } else {
2714 patchDesc->mPatch = newPatch;
2715 }
2716 patchDesc->mAfPatchHandle = afPatchHandle;
2717 *handle = patchDesc->mHandle;
2718 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002719 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002720 } else {
2721 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2722 status);
2723 return INVALID_OPERATION;
2724 }
2725 } else {
2726 return BAD_VALUE;
2727 }
2728 } else {
2729 return BAD_VALUE;
2730 }
2731 return NO_ERROR;
2732}
2733
2734status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2735 uid_t uid)
2736{
2737 ALOGV("releaseAudioPatch() patch %d", handle);
2738
2739 ssize_t index = mAudioPatches.indexOfKey(handle);
2740
2741 if (index < 0) {
2742 return BAD_VALUE;
2743 }
2744 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2745 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2746 mUidCached, patchDesc->mUid, uid);
2747 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2748 return INVALID_OPERATION;
2749 }
2750
2751 struct audio_patch *patch = &patchDesc->mPatch;
2752 patchDesc->mUid = mUidCached;
2753 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002754 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002755 if (outputDesc == NULL) {
2756 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2757 return BAD_VALUE;
2758 }
2759
Eric Laurentc75307b2015-03-17 15:29:32 -07002760 setOutputDevice(outputDesc,
2761 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002762 true,
2763 0,
2764 NULL);
2765 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2766 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002767 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002768 if (inputDesc == NULL) {
2769 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2770 return BAD_VALUE;
2771 }
2772 setInputDevice(inputDesc->mIoHandle,
Eric Laurentfb66dd92016-01-28 18:32:03 -08002773 getNewInputDevice(inputDesc),
Eric Laurent6a94d692014-05-20 11:18:06 -07002774 true,
2775 NULL);
2776 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002777 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2778 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2779 status, patchDesc->mAfPatchHandle);
2780 removeAudioPatch(patchDesc->mHandle);
2781 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002782 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002783 } else {
2784 return BAD_VALUE;
2785 }
2786 } else {
2787 return BAD_VALUE;
2788 }
2789 return NO_ERROR;
2790}
2791
2792status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2793 struct audio_patch *patches,
2794 unsigned int *generation)
2795{
François Gaffie53615e22015-03-19 09:24:12 +01002796 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002797 return BAD_VALUE;
2798 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002799 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01002800 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002801}
2802
Eric Laurente1715a42014-05-20 11:30:42 -07002803status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002804{
Eric Laurente1715a42014-05-20 11:30:42 -07002805 ALOGV("setAudioPortConfig()");
2806
2807 if (config == NULL) {
2808 return BAD_VALUE;
2809 }
2810 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2811 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002812 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2813 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002814 }
2815
Eric Laurenta121f902014-06-03 13:32:54 -07002816 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002817 if (config->type == AUDIO_PORT_TYPE_MIX) {
2818 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002819 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002820 if (outputDesc == NULL) {
2821 return BAD_VALUE;
2822 }
Eric Laurent84c70242014-06-23 08:46:27 -07002823 ALOG_ASSERT(!outputDesc->isDuplicated(),
2824 "setAudioPortConfig() called on duplicated output %d",
2825 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002826 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002827 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01002828 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002829 if (inputDesc == NULL) {
2830 return BAD_VALUE;
2831 }
Eric Laurenta121f902014-06-03 13:32:54 -07002832 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002833 } else {
2834 return BAD_VALUE;
2835 }
2836 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2837 sp<DeviceDescriptor> deviceDesc;
2838 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2839 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2840 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2841 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2842 } else {
2843 return BAD_VALUE;
2844 }
2845 if (deviceDesc == NULL) {
2846 return BAD_VALUE;
2847 }
Eric Laurenta121f902014-06-03 13:32:54 -07002848 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002849 } else {
2850 return BAD_VALUE;
2851 }
2852
Eric Laurenta121f902014-06-03 13:32:54 -07002853 struct audio_port_config backupConfig;
2854 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2855 if (status == NO_ERROR) {
2856 struct audio_port_config newConfig;
2857 audioPortConfig->toAudioPortConfig(&newConfig, config);
2858 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002859 }
Eric Laurenta121f902014-06-03 13:32:54 -07002860 if (status != NO_ERROR) {
2861 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002862 }
Eric Laurente1715a42014-05-20 11:30:42 -07002863
2864 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002865}
2866
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002867void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
2868{
Eric Laurentd60560a2015-04-10 11:31:20 -07002869 clearAudioSources(uid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002870 clearAudioPatches(uid);
2871 clearSessionRoutes(uid);
2872}
2873
Eric Laurent6a94d692014-05-20 11:18:06 -07002874void AudioPolicyManager::clearAudioPatches(uid_t uid)
2875{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002876 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002877 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2878 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002879 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002880 }
2881 }
2882}
2883
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002884void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
2885 audio_io_handle_t ouptutToSkip)
2886{
2887 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2888 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
2889 for (size_t j = 0; j < mOutputs.size(); j++) {
2890 if (mOutputs.keyAt(j) == ouptutToSkip) {
2891 continue;
2892 }
2893 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
2894 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
2895 continue;
2896 }
2897 // If the default device for this strategy is on another output mix,
2898 // invalidate all tracks in this strategy to force re connection.
2899 // Otherwise select new device on the output mix.
2900 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
Eric Laurent794fde22016-03-11 09:50:45 -08002901 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002902 if (getStrategy((audio_stream_type_t)stream) == strategy) {
2903 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
2904 }
2905 }
2906 } else {
2907 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
2908 setOutputDevice(outputDesc, newDevice, false);
2909 }
2910 }
2911}
2912
2913void AudioPolicyManager::clearSessionRoutes(uid_t uid)
2914{
2915 // remove output routes associated with this uid
2916 SortedVector<routing_strategy> affectedStrategies;
2917 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
2918 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
2919 if (route->mUid == uid) {
2920 mOutputRoutes.removeItemsAt(i);
2921 if (route->mDeviceDescriptor != 0) {
2922 affectedStrategies.add(getStrategy(route->mStreamType));
2923 }
2924 }
2925 }
2926 // reroute outputs if necessary
2927 for (size_t i = 0; i < affectedStrategies.size(); i++) {
2928 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
2929 }
2930
2931 // remove input routes associated with this uid
2932 SortedVector<audio_source_t> affectedSources;
2933 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
2934 sp<SessionRoute> route = mInputRoutes.valueAt(i);
2935 if (route->mUid == uid) {
2936 mInputRoutes.removeItemsAt(i);
2937 if (route->mDeviceDescriptor != 0) {
2938 affectedSources.add(route->mSource);
2939 }
2940 }
2941 }
2942 // reroute inputs if necessary
2943 SortedVector<audio_io_handle_t> inputsToClose;
2944 for (size_t i = 0; i < mInputs.size(); i++) {
2945 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002946 if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002947 inputsToClose.add(inputDesc->mIoHandle);
2948 }
2949 }
2950 for (size_t i = 0; i < inputsToClose.size(); i++) {
2951 closeInput(inputsToClose[i]);
2952 }
2953}
2954
Eric Laurentd60560a2015-04-10 11:31:20 -07002955void AudioPolicyManager::clearAudioSources(uid_t uid)
2956{
2957 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
2958 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
2959 if (sourceDesc->mUid == uid) {
2960 stopAudioSource(mAudioSources.keyAt(i));
2961 }
2962 }
2963}
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002964
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002965status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2966 audio_io_handle_t *ioHandle,
2967 audio_devices_t *device)
2968{
Glenn Kastenf0c6d7d2016-02-26 10:44:04 -08002969 *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
2970 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002971 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002972
François Gaffiedf372692015-03-19 10:43:27 +01002973 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002974}
2975
Eric Laurentd60560a2015-04-10 11:31:20 -07002976status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source,
2977 const audio_attributes_t *attributes,
2978 audio_io_handle_t *handle,
2979 uid_t uid)
Eric Laurent554a2772015-04-10 11:29:24 -07002980{
Eric Laurentd60560a2015-04-10 11:31:20 -07002981 ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle);
2982 if (source == NULL || attributes == NULL || handle == NULL) {
2983 return BAD_VALUE;
2984 }
2985
2986 *handle = AUDIO_IO_HANDLE_NONE;
2987
2988 if (source->role != AUDIO_PORT_ROLE_SOURCE ||
2989 source->type != AUDIO_PORT_TYPE_DEVICE) {
2990 ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type);
2991 return INVALID_OPERATION;
2992 }
2993
2994 sp<DeviceDescriptor> srcDeviceDesc =
2995 mAvailableInputDevices.getDevice(source->ext.device.type,
2996 String8(source->ext.device.address));
2997 if (srcDeviceDesc == 0) {
2998 ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type);
2999 return BAD_VALUE;
3000 }
3001 sp<AudioSourceDescriptor> sourceDesc =
3002 new AudioSourceDescriptor(srcDeviceDesc, attributes, uid);
3003
3004 struct audio_patch dummyPatch;
3005 sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid);
3006 sourceDesc->mPatchDesc = patchDesc;
3007
3008 status_t status = connectAudioSource(sourceDesc);
3009 if (status == NO_ERROR) {
3010 mAudioSources.add(sourceDesc->getHandle(), sourceDesc);
3011 *handle = sourceDesc->getHandle();
3012 }
3013 return status;
3014}
3015
3016status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3017{
3018 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3019
3020 // make sure we only have one patch per source.
3021 disconnectAudioSource(sourceDesc);
3022
3023 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
Eric Laurent28d09f02016-03-08 10:43:05 -08003024 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003025 sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice;
3026
3027 audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true);
3028 sp<DeviceDescriptor> sinkDeviceDesc =
3029 mAvailableOutputDevices.getDevice(sinkDevice, String8(""));
3030
3031 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3032 struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch;
3033
3034 if (srcDeviceDesc->getAudioPort()->mModule->getHandle() ==
3035 sinkDeviceDesc->getAudioPort()->mModule->getHandle() &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003036 srcDeviceDesc->getAudioPort()->mModule->getHalVersion() >= AUDIO_DEVICE_API_VERSION_3_0 &&
Eric Laurentd60560a2015-04-10 11:31:20 -07003037 srcDeviceDesc->getAudioPort()->mGains.size() > 0) {
3038 ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__);
3039 // create patch between src device and output device
3040 // create Hwoutput and add to mHwOutputs
3041 } else {
3042 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs);
3043 audio_io_handle_t output =
3044 selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID);
3045 if (output == AUDIO_IO_HANDLE_NONE) {
3046 ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice);
3047 return INVALID_OPERATION;
3048 }
3049 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
3050 if (outputDesc->isDuplicated()) {
3051 ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice);
3052 return INVALID_OPERATION;
3053 }
3054 // create a special patch with no sink and two sources:
3055 // - the second source indicates to PatchPanel through which output mix this patch should
3056 // be connected as well as the stream type for volume control
3057 // - the sink is defined by whatever output device is currently selected for the output
3058 // though which this patch is routed.
3059 patch->num_sinks = 0;
3060 patch->num_sources = 2;
3061 srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL);
3062 outputDesc->toAudioPortConfig(&patch->sources[1], NULL);
3063 patch->sources[1].ext.mix.usecase.stream = stream;
3064 status_t status = mpClientInterface->createAudioPatch(patch,
3065 &afPatchHandle,
3066 0);
3067 ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__,
3068 status, afPatchHandle);
3069 if (status != NO_ERROR) {
3070 ALOGW("%s patch panel could not connect device patch, error %d",
3071 __FUNCTION__, status);
3072 return INVALID_OPERATION;
3073 }
3074 uint32_t delayMs = 0;
Eric Laurentc40d9692016-04-13 19:14:13 -07003075 status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs);
Eric Laurentd60560a2015-04-10 11:31:20 -07003076
3077 if (status != NO_ERROR) {
3078 mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0);
3079 return status;
3080 }
3081 sourceDesc->mSwOutput = outputDesc;
3082 if (delayMs != 0) {
3083 usleep(delayMs * 1000);
3084 }
3085 }
3086
3087 sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle;
3088 addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc);
3089
3090 return NO_ERROR;
Eric Laurent554a2772015-04-10 11:29:24 -07003091}
3092
Eric Laurent493404d2015-04-21 15:07:36 -07003093status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07003094{
Eric Laurentd60560a2015-04-10 11:31:20 -07003095 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle);
3096 ALOGV("%s handle %d", __FUNCTION__, handle);
3097 if (sourceDesc == 0) {
3098 ALOGW("%s unknown source for handle %d", __FUNCTION__, handle);
3099 return BAD_VALUE;
3100 }
3101 status_t status = disconnectAudioSource(sourceDesc);
3102
3103 mAudioSources.removeItem(handle);
3104 return status;
3105}
3106
Andy Hung2ddee192015-12-18 17:34:44 -08003107status_t AudioPolicyManager::setMasterMono(bool mono)
3108{
3109 if (mMasterMono == mono) {
3110 return NO_ERROR;
3111 }
3112 mMasterMono = mono;
3113 // if enabling mono we close all offloaded devices, which will invalidate the
3114 // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible
3115 // for recreating the new AudioTrack as non-offloaded PCM.
3116 //
3117 // If disabling mono, we leave all tracks as is: we don't know which clients
3118 // and tracks are able to be recreated as offloaded. The next "song" should
3119 // play back offloaded.
3120 if (mMasterMono) {
3121 Vector<audio_io_handle_t> offloaded;
3122 for (size_t i = 0; i < mOutputs.size(); ++i) {
3123 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
3124 if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
3125 offloaded.push(desc->mIoHandle);
3126 }
3127 }
3128 for (size_t i = 0; i < offloaded.size(); ++i) {
3129 closeOutput(offloaded[i]);
3130 }
3131 }
3132 // update master mono for all remaining outputs
3133 for (size_t i = 0; i < mOutputs.size(); ++i) {
3134 updateMono(mOutputs.keyAt(i));
3135 }
3136 return NO_ERROR;
3137}
3138
3139status_t AudioPolicyManager::getMasterMono(bool *mono)
3140{
3141 *mono = mMasterMono;
3142 return NO_ERROR;
3143}
3144
Eric Laurentd60560a2015-04-10 11:31:20 -07003145status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3146{
3147 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3148
3149 sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle);
3150 if (patchDesc == 0) {
3151 ALOGW("%s source has no patch with handle %d", __FUNCTION__,
3152 sourceDesc->mPatchDesc->mHandle);
3153 return BAD_VALUE;
3154 }
3155 removeAudioPatch(sourceDesc->mPatchDesc->mHandle);
3156
Eric Laurent28d09f02016-03-08 10:43:05 -08003157 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003158 sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote();
3159 if (swOutputDesc != 0) {
3160 stopSource(swOutputDesc, stream, false);
3161 mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3162 } else {
3163 sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote();
3164 if (hwOutputDesc != 0) {
3165 // release patch between src device and output device
3166 // close Hwoutput and remove from mHwOutputs
3167 } else {
3168 ALOGW("%s source has neither SW nor HW output", __FUNCTION__);
3169 }
3170 }
3171 return NO_ERROR;
3172}
3173
3174sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput(
3175 audio_io_handle_t output, routing_strategy strategy)
3176{
3177 sp<AudioSourceDescriptor> source;
3178 for (size_t i = 0; i < mAudioSources.size(); i++) {
3179 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3180 routing_strategy sourceStrategy =
3181 (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
3182 sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote();
3183 if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) {
3184 source = sourceDesc;
3185 break;
3186 }
3187 }
3188 return source;
Eric Laurent554a2772015-04-10 11:29:24 -07003189}
3190
Eric Laurente552edb2014-03-10 17:42:56 -07003191// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07003192// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07003193// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07003194uint32_t AudioPolicyManager::nextAudioPortGeneration()
3195{
3196 return android_atomic_inc(&mAudioPortGeneration);
3197}
3198
Eric Laurente0720872014-03-11 09:30:41 -07003199AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07003200 :
3201#ifdef AUDIO_POLICY_TEST
3202 Thread(false),
3203#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07003204 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07003205 mA2dpSuspended(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003206 mAudioPortGeneration(1),
3207 mBeaconMuteRefCount(0),
3208 mBeaconPlayingRefCount(0),
Eric Laurent9459fb02015-08-12 18:36:32 -07003209 mBeaconMuted(false),
Andy Hung2ddee192015-12-18 17:34:44 -08003210 mTtsOutputAvailable(false),
3211 mMasterMono(false)
Eric Laurente552edb2014-03-10 17:42:56 -07003212{
François Gaffied1ab2bd2015-12-02 18:20:06 +01003213 mUidCached = getuid();
3214 mpClientInterface = clientInterface;
3215
3216 // TODO: remove when legacy conf file is removed. true on devices that use DRC on the
3217 // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly.
3218 // Note: remove also speaker_drc_enabled from global configuration of XML config file.
3219 bool speakerDrcEnabled = false;
3220
3221#ifdef USE_XML_AUDIO_POLICY_CONF
3222 mVolumeCurves = new VolumeCurvesCollection();
3223 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3224 mDefaultOutputDevice, speakerDrcEnabled,
3225 static_cast<VolumeCurvesCollection *>(mVolumeCurves));
3226 PolicySerializer serializer;
3227 if (serializer.deserialize(AUDIO_POLICY_XML_CONFIG_FILE, config) != NO_ERROR) {
3228#else
3229 mVolumeCurves = new StreamDescriptorCollection();
3230 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3231 mDefaultOutputDevice, speakerDrcEnabled);
3232 if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) &&
3233 (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) {
3234#endif
3235 ALOGE("could not load audio policy configuration file, setting defaults");
3236 config.setDefault();
3237 }
3238 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
3239 mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled);
3240
3241 // Once policy config has been parsed, retrieve an instance of the engine and initialize it.
François Gaffie2110e042015-03-24 08:41:51 +01003242 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
3243 if (!engineInstance) {
3244 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
3245 return;
3246 }
3247 // Retrieve the Policy Manager Interface
3248 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
3249 if (mEngine == NULL) {
3250 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
3251 return;
3252 }
3253 mEngine->setObserver(this);
3254 status_t status = mEngine->initCheck();
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07003255 (void) status;
François Gaffie2110e042015-03-24 08:41:51 +01003256 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
3257
Eric Laurent3a4311c2014-03-17 12:00:47 -07003258 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07003259 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07003260 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
3261 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07003262 for (size_t i = 0; i < mHwModules.size(); i++) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003263 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003264 if (mHwModules[i]->mHandle == 0) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003265 ALOGW("could not open HW module %s", mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003266 continue;
3267 }
3268 // open all output streams needed to access attached devices
3269 // except for direct output streams that are only opened when they are actually
3270 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07003271 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07003272 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3273 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003274 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003275
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003276 if (!outProfile->hasSupportedDevices()) {
3277 ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003278 continue;
3279 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003280 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) {
Eric Laurent9459fb02015-08-12 18:36:32 -07003281 mTtsOutputAvailable = true;
3282 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003283
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003284 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
Eric Laurentd78f1532014-09-16 16:38:20 -07003285 continue;
3286 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003287 audio_devices_t profileType = outProfile->getSupportedDevicesType();
François Gaffie53615e22015-03-19 09:24:12 +01003288 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
3289 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07003290 } else {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003291 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003292 // outputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003293 profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes);
Eric Laurente552edb2014-03-10 17:42:56 -07003294 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003295 if ((profileType & outputDeviceTypes) == 0) {
3296 continue;
3297 }
Eric Laurentc75307b2015-03-17 15:29:32 -07003298 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
3299 mpClientInterface);
Eric Laurentc40d9692016-04-13 19:14:13 -07003300 const DeviceVector &supportedDevices = outProfile->getSupportedDevices();
3301 const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType);
3302 String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress
3303 : String8("");
Eric Laurentd78f1532014-09-16 16:38:20 -07003304
3305 outputDesc->mDevice = profileType;
3306 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3307 config.sample_rate = outputDesc->mSamplingRate;
3308 config.channel_mask = outputDesc->mChannelMask;
3309 config.format = outputDesc->mFormat;
3310 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003311 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003312 &output,
3313 &config,
3314 &outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003315 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003316 &outputDesc->mLatency,
3317 outputDesc->mFlags);
3318
3319 if (status != NO_ERROR) {
3320 ALOGW("Cannot open output stream for device %08x on hw module %s",
3321 outputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003322 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003323 } else {
3324 outputDesc->mSamplingRate = config.sample_rate;
3325 outputDesc->mChannelMask = config.channel_mask;
3326 outputDesc->mFormat = config.format;
3327
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003328 for (size_t k = 0; k < supportedDevices.size(); k++) {
3329 ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003330 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08003331 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
3332 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003333 }
3334 }
3335 if (mPrimaryOutput == 0 &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003336 outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003337 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07003338 }
3339 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003340 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07003341 outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003342 true,
3343 0,
3344 NULL,
3345 address.string());
Eric Laurentd78f1532014-09-16 16:38:20 -07003346 }
Eric Laurente552edb2014-03-10 17:42:56 -07003347 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003348 // open input streams needed to access attached devices to validate
3349 // mAvailableInputDevices list
3350 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
3351 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003352 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003353
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003354 if (!inProfile->hasSupportedDevices()) {
3355 ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003356 continue;
3357 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003358 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003359 // inputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003360 audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes);
3361
Eric Laurentd78f1532014-09-16 16:38:20 -07003362 if ((profileType & inputDeviceTypes) == 0) {
3363 continue;
3364 }
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08003365 sp<AudioInputDescriptor> inputDesc =
3366 new AudioInputDescriptor(inProfile);
Eric Laurentd78f1532014-09-16 16:38:20 -07003367
Eric Laurentd78f1532014-09-16 16:38:20 -07003368 inputDesc->mDevice = profileType;
3369
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003370 // find the address
3371 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
3372 // the inputs vector must be of size 1, but we don't want to crash here
3373 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
3374 : String8("");
3375 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
3376 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
3377
Eric Laurentd78f1532014-09-16 16:38:20 -07003378 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3379 config.sample_rate = inputDesc->mSamplingRate;
3380 config.channel_mask = inputDesc->mChannelMask;
3381 config.format = inputDesc->mFormat;
3382 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003383 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003384 &input,
3385 &config,
3386 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003387 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003388 AUDIO_SOURCE_MIC,
3389 AUDIO_INPUT_FLAG_NONE);
3390
3391 if (status == NO_ERROR) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003392 const DeviceVector &supportedDevices = inProfile->getSupportedDevices();
3393 for (size_t k = 0; k < supportedDevices.size(); k++) {
3394 ssize_t index = mAvailableInputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003395 // give a valid ID to an attached device once confirmed it is reachable
Eric Laurent45aabc32015-08-06 09:11:13 -07003396 if (index >= 0) {
3397 sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index];
3398 if (!devDesc->isAttached()) {
3399 devDesc->attach(mHwModules[i]);
3400 devDesc->importAudioPort(inProfile);
3401 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003402 }
3403 }
3404 mpClientInterface->closeInput(input);
3405 } else {
3406 ALOGW("Cannot open input stream for device %08x on hw module %s",
3407 inputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003408 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003409 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003410 }
3411 }
3412 // make sure all attached devices have been allocated a unique ID
3413 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003414 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003415 ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003416 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
3417 continue;
3418 }
François Gaffie2110e042015-03-24 08:41:51 +01003419 // The device is now validated and can be appended to the available devices of the engine
3420 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
3421 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003422 i++;
3423 }
3424 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003425 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01003426 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003427 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
3428 continue;
3429 }
François Gaffie2110e042015-03-24 08:41:51 +01003430 // The device is now validated and can be appended to the available devices of the engine
3431 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
3432 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003433 i++;
3434 }
3435 // make sure default device is reachable
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003436 if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01003437 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003438 }
Eric Laurente552edb2014-03-10 17:42:56 -07003439
3440 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
3441
3442 updateDevicesAndOutputs();
3443
3444#ifdef AUDIO_POLICY_TEST
3445 if (mPrimaryOutput != 0) {
3446 AudioParameter outputCmd = AudioParameter();
3447 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003448 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07003449
3450 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
3451 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07003452 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
3453 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003454 mTestLatencyMs = 0;
3455 mCurOutput = 0;
3456 mDirectOutput = false;
3457 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3458 mTestOutputs[i] = 0;
3459 }
3460
3461 const size_t SIZE = 256;
3462 char buffer[SIZE];
3463 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
3464 run(buffer, ANDROID_PRIORITY_AUDIO);
3465 }
3466#endif //AUDIO_POLICY_TEST
3467}
3468
Eric Laurente0720872014-03-11 09:30:41 -07003469AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07003470{
3471#ifdef AUDIO_POLICY_TEST
3472 exit();
3473#endif //AUDIO_POLICY_TEST
3474 for (size_t i = 0; i < mOutputs.size(); i++) {
3475 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003476 }
3477 for (size_t i = 0; i < mInputs.size(); i++) {
3478 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003479 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003480 mAvailableOutputDevices.clear();
3481 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003482 mOutputs.clear();
3483 mInputs.clear();
3484 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07003485}
3486
Eric Laurente0720872014-03-11 09:30:41 -07003487status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07003488{
Eric Laurent87ffa392015-05-22 10:32:38 -07003489 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003490}
3491
3492#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07003493bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07003494{
3495 ALOGV("entering threadLoop()");
3496 while (!exitPending())
3497 {
3498 String8 command;
3499 int valueInt;
3500 String8 value;
3501
3502 Mutex::Autolock _l(mLock);
3503 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
3504
3505 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
3506 AudioParameter param = AudioParameter(command);
3507
3508 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
3509 valueInt != 0) {
3510 ALOGV("Test command %s received", command.string());
3511 String8 target;
3512 if (param.get(String8("target"), target) != NO_ERROR) {
3513 target = "Manager";
3514 }
3515 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
3516 param.remove(String8("test_cmd_policy_output"));
3517 mCurOutput = valueInt;
3518 }
3519 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
3520 param.remove(String8("test_cmd_policy_direct"));
3521 if (value == "false") {
3522 mDirectOutput = false;
3523 } else if (value == "true") {
3524 mDirectOutput = true;
3525 }
3526 }
3527 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
3528 param.remove(String8("test_cmd_policy_input"));
3529 mTestInput = valueInt;
3530 }
3531
3532 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
3533 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07003534 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07003535 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003536 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003537 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003538 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003539 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003540 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07003541 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003542 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07003543 if (target == "Manager") {
3544 mTestFormat = format;
3545 } else if (mTestOutputs[mCurOutput] != 0) {
3546 AudioParameter outputParam = AudioParameter();
3547 outputParam.addInt(String8("format"), format);
3548 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3549 }
3550 }
3551 }
3552 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
3553 param.remove(String8("test_cmd_policy_channels"));
3554 int channels = 0;
3555
3556 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003557 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003558 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003559 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07003560 }
3561 if (channels != 0) {
3562 if (target == "Manager") {
3563 mTestChannels = channels;
3564 } else if (mTestOutputs[mCurOutput] != 0) {
3565 AudioParameter outputParam = AudioParameter();
3566 outputParam.addInt(String8("channels"), channels);
3567 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3568 }
3569 }
3570 }
3571 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
3572 param.remove(String8("test_cmd_policy_sampleRate"));
3573 if (valueInt >= 0 && valueInt <= 96000) {
3574 int samplingRate = valueInt;
3575 if (target == "Manager") {
3576 mTestSamplingRate = samplingRate;
3577 } else if (mTestOutputs[mCurOutput] != 0) {
3578 AudioParameter outputParam = AudioParameter();
3579 outputParam.addInt(String8("sampling_rate"), samplingRate);
3580 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3581 }
3582 }
3583 }
3584
3585 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
3586 param.remove(String8("test_cmd_policy_reopen"));
3587
Eric Laurentc75307b2015-03-17 15:29:32 -07003588 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07003589
Eric Laurentc75307b2015-03-17 15:29:32 -07003590 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07003591
Eric Laurentc75307b2015-03-17 15:29:32 -07003592 removeOutput(mPrimaryOutput->mIoHandle);
3593 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
3594 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003595 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003596 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3597 config.sample_rate = outputDesc->mSamplingRate;
3598 config.channel_mask = outputDesc->mChannelMask;
3599 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07003600 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003601 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07003602 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07003603 &config,
3604 &outputDesc->mDevice,
3605 String8(""),
3606 &outputDesc->mLatency,
3607 outputDesc->mFlags);
3608 if (status != NO_ERROR) {
3609 ALOGE("Failed to reopen hardware output stream, "
3610 "samplingRate: %d, format %d, channels %d",
3611 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003612 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003613 outputDesc->mSamplingRate = config.sample_rate;
3614 outputDesc->mChannelMask = config.channel_mask;
3615 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07003616 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07003617 AudioParameter outputCmd = AudioParameter();
3618 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003619 mpClientInterface->setParameters(handle, outputCmd.toString());
3620 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07003621 }
3622 }
3623
3624
3625 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3626 }
3627 }
3628 return false;
3629}
3630
Eric Laurente0720872014-03-11 09:30:41 -07003631void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003632{
3633 {
3634 AutoMutex _l(mLock);
3635 requestExit();
3636 mWaitWorkCV.signal();
3637 }
3638 requestExitAndWait();
3639}
3640
Eric Laurente0720872014-03-11 09:30:41 -07003641int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003642{
3643 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3644 if (output == mTestOutputs[i]) return i;
3645 }
3646 return 0;
3647}
3648#endif //AUDIO_POLICY_TEST
3649
3650// ---
3651
Eric Laurentc75307b2015-03-17 15:29:32 -07003652void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003653{
François Gaffie98cc1912015-03-18 17:52:40 +01003654 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003655 mOutputs.add(output, outputDesc);
Andy Hung2ddee192015-12-18 17:34:44 -08003656 updateMono(output); // update mono status when adding to output list
Eric Laurent6a94d692014-05-20 11:18:06 -07003657 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003658}
3659
François Gaffie53615e22015-03-19 09:24:12 +01003660void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3661{
3662 mOutputs.removeItem(output);
3663}
3664
Eric Laurent1f2f2232014-06-02 12:01:23 -07003665void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003666{
François Gaffie98cc1912015-03-18 17:52:40 +01003667 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003668 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003669 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003670}
Eric Laurente552edb2014-03-10 17:42:56 -07003671
Eric Laurentc75307b2015-03-17 15:29:32 -07003672void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003673 const audio_devices_t device /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003674 const String8 address /*in*/,
3675 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003676 sp<DeviceDescriptor> devDesc =
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003677 desc->mProfile->getSupportedDeviceByAddress(device, address);
keunyoung3190e672014-12-30 13:00:52 -08003678 if (devDesc != 0) {
3679 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3680 desc->mIoHandle, address.string());
3681 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003682 }
3683}
3684
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003685status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003686 audio_policy_dev_state_t state,
3687 SortedVector<audio_io_handle_t>& outputs,
3688 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07003689{
François Gaffie53615e22015-03-19 09:24:12 +01003690 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003691 sp<SwAudioOutputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003692
3693 if (audio_device_is_digital(device)) {
3694 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003695 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003696 }
Eric Laurente552edb2014-03-10 17:42:56 -07003697
Eric Laurent3b73df72014-03-11 09:06:29 -07003698 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003699 // first list already open outputs that can be routed to this device
3700 for (size_t i = 0; i < mOutputs.size(); i++) {
3701 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003702 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003703 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003704 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3705 outputs.add(mOutputs.keyAt(i));
3706 } else {
3707 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003708 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003709 }
Eric Laurente552edb2014-03-10 17:42:56 -07003710 }
3711 }
3712 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003713 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003714 for (size_t i = 0; i < mHwModules.size(); i++)
3715 {
3716 if (mHwModules[i]->mHandle == 0) {
3717 continue;
3718 }
3719 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3720 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003721 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003722 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003723 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003724 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003725 profiles.add(profile);
3726 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3727 }
Eric Laurente552edb2014-03-10 17:42:56 -07003728 }
3729 }
3730 }
3731
Eric Laurent7b279bb2015-12-14 10:18:23 -08003732 ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size());
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003733
Eric Laurente552edb2014-03-10 17:42:56 -07003734 if (profiles.isEmpty() && outputs.isEmpty()) {
3735 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3736 return BAD_VALUE;
3737 }
3738
3739 // open outputs for matching profiles if needed. Direct outputs are also opened to
3740 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3741 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003742 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003743
3744 // nothing to do if one output is already opened for this profile
3745 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003746 for (j = 0; j < outputs.size(); j++) {
3747 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003748 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003749 // matching profile: save the sample rates, format and channel masks supported
3750 // by the profile in our device descriptor
Paul McLean9080a4c2015-06-18 08:24:02 -07003751 if (audio_device_is_digital(device)) {
3752 devDesc->importAudioPort(profile);
3753 }
Eric Laurente552edb2014-03-10 17:42:56 -07003754 break;
3755 }
3756 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003757 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003758 continue;
3759 }
3760
Eric Laurent83b88082014-06-20 18:31:16 -07003761 ALOGV("opening output for device %08x with params %s profile %p",
3762 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07003763 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003764 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003765 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3766 config.sample_rate = desc->mSamplingRate;
3767 config.channel_mask = desc->mChannelMask;
3768 config.format = desc->mFormat;
3769 config.offload_info.sample_rate = desc->mSamplingRate;
3770 config.offload_info.channel_mask = desc->mChannelMask;
3771 config.offload_info.format = desc->mFormat;
3772 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003773 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003774 &output,
3775 &config,
3776 &desc->mDevice,
3777 address,
3778 &desc->mLatency,
3779 desc->mFlags);
3780 if (status == NO_ERROR) {
3781 desc->mSamplingRate = config.sample_rate;
3782 desc->mChannelMask = config.channel_mask;
3783 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003784
Eric Laurentd4692962014-05-05 18:13:44 -07003785 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003786 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003787 char *param = audio_device_address_to_parameter(device, address);
3788 mpClientInterface->setParameters(output, String8(param));
3789 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003790 }
Phil Burk00eeb322016-03-31 12:41:00 -07003791 updateAudioProfiles(device, output, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01003792 if (!profile->hasValidAudioProfile()) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003793 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003794 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003795 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003796 } else if (profile->hasDynamicAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07003797 mpClientInterface->closeOutput(output);
Phil Burk702b1052016-03-02 16:38:26 -08003798 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003799 profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003800 config.offload_info.sample_rate = config.sample_rate;
3801 config.offload_info.channel_mask = config.channel_mask;
3802 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07003803 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003804 &output,
3805 &config,
3806 &desc->mDevice,
3807 address,
3808 &desc->mLatency,
3809 desc->mFlags);
3810 if (status == NO_ERROR) {
3811 desc->mSamplingRate = config.sample_rate;
3812 desc->mChannelMask = config.channel_mask;
3813 desc->mFormat = config.format;
3814 } else {
3815 output = AUDIO_IO_HANDLE_NONE;
3816 }
Eric Laurentd4692962014-05-05 18:13:44 -07003817 }
3818
Eric Laurentcf2c0212014-07-25 16:20:43 -07003819 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003820 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003821 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003822 sp<AudioPolicyMix> policyMix;
3823 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003824 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3825 address.string());
3826 }
François Gaffie036e1e92015-03-19 10:16:24 +01003827 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07003828 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01003829
Eric Laurent87ffa392015-05-22 10:32:38 -07003830 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
3831 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08003832 // no duplicated output for direct outputs and
3833 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003834 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003835
Eric Laurentd4692962014-05-05 18:13:44 -07003836 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07003837 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003838
Eric Laurentd4692962014-05-05 18:13:44 -07003839 //TODO: configure audio effect output stage here
3840
3841 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07003842 duplicatedOutput =
3843 mpClientInterface->openDuplicateOutput(output,
3844 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003845 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003846 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07003847 sp<SwAudioOutputDescriptor> dupOutputDesc =
3848 new SwAudioOutputDescriptor(NULL, mpClientInterface);
3849 dupOutputDesc->mOutput1 = mPrimaryOutput;
3850 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003851 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3852 dupOutputDesc->mFormat = desc->mFormat;
3853 dupOutputDesc->mChannelMask = desc->mChannelMask;
3854 dupOutputDesc->mLatency = desc->mLatency;
3855 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003856 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07003857 } else {
3858 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07003859 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07003860 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003861 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003862 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003863 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003864 }
Eric Laurente552edb2014-03-10 17:42:56 -07003865 }
3866 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003867 } else {
3868 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003869 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003870 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003871 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003872 profiles.removeAt(profile_index);
3873 profile_index--;
3874 } else {
3875 outputs.add(output);
Paul McLean9080a4c2015-06-18 08:24:02 -07003876 // Load digital format info only for digital devices
3877 if (audio_device_is_digital(device)) {
3878 devDesc->importAudioPort(profile);
3879 }
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003880
François Gaffie53615e22015-03-19 09:24:12 +01003881 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003882 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3883 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003884 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003885 NULL/*patch handle*/, address.string());
3886 }
Eric Laurente552edb2014-03-10 17:42:56 -07003887 ALOGV("checkOutputsForDevice(): adding output %d", output);
3888 }
3889 }
3890
3891 if (profiles.isEmpty()) {
3892 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3893 return BAD_VALUE;
3894 }
Eric Laurentd4692962014-05-05 18:13:44 -07003895 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003896 // check if one opened output is not needed any more after disconnecting one device
3897 for (size_t i = 0; i < mOutputs.size(); i++) {
3898 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003899 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003900 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01003901 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07003902 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003903 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07003904 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003905 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3906 mOutputs.keyAt(i));
3907 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003908 }
Eric Laurente552edb2014-03-10 17:42:56 -07003909 }
3910 }
Eric Laurentd4692962014-05-05 18:13:44 -07003911 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003912 for (size_t i = 0; i < mHwModules.size(); i++)
3913 {
3914 if (mHwModules[i]->mHandle == 0) {
3915 continue;
3916 }
3917 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3918 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003919 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003920 if (profile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003921 ALOGV("checkOutputsForDevice(): "
3922 "clearing direct output profile %zu on module %zu", j, i);
François Gaffie112b0af2015-11-19 16:13:25 +01003923 profile->clearAudioProfiles();
Eric Laurente552edb2014-03-10 17:42:56 -07003924 }
3925 }
3926 }
3927 }
3928 return NO_ERROR;
3929}
3930
Paul McLean9080a4c2015-06-18 08:24:02 -07003931status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003932 audio_policy_dev_state_t state,
3933 SortedVector<audio_io_handle_t>& inputs,
3934 const String8 address)
Eric Laurentd4692962014-05-05 18:13:44 -07003935{
Paul McLean9080a4c2015-06-18 08:24:02 -07003936 audio_devices_t device = devDesc->type();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003937 sp<AudioInputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003938
3939 if (audio_device_is_digital(device)) {
3940 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003941 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003942 }
3943
Eric Laurentd4692962014-05-05 18:13:44 -07003944 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3945 // first list already open inputs that can be routed to this device
3946 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3947 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003948 if (desc->mProfile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003949 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3950 inputs.add(mInputs.keyAt(input_index));
3951 }
3952 }
3953
3954 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003955 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003956 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3957 {
3958 if (mHwModules[module_idx]->mHandle == 0) {
3959 continue;
3960 }
3961 for (size_t profile_index = 0;
3962 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3963 profile_index++)
3964 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003965 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3966
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003967 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003968 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003969 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003970 profiles.add(profile);
3971 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3972 profile_index, module_idx);
3973 }
Eric Laurentd4692962014-05-05 18:13:44 -07003974 }
3975 }
3976 }
3977
3978 if (profiles.isEmpty() && inputs.isEmpty()) {
3979 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3980 return BAD_VALUE;
3981 }
3982
3983 // open inputs for matching profiles if needed. Direct inputs are also opened to
3984 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3985 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3986
Eric Laurent1c333e22014-05-20 10:48:17 -07003987 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003988 // nothing to do if one input is already opened for this profile
3989 size_t input_index;
3990 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3991 desc = mInputs.valueAt(input_index);
3992 if (desc->mProfile == profile) {
Paul McLean9080a4c2015-06-18 08:24:02 -07003993 if (audio_device_is_digital(device)) {
3994 devDesc->importAudioPort(profile);
3995 }
Eric Laurentd4692962014-05-05 18:13:44 -07003996 break;
3997 }
3998 }
3999 if (input_index != mInputs.size()) {
4000 continue;
4001 }
4002
4003 ALOGV("opening input for device 0x%X with params %s", device, address.string());
4004 desc = new AudioInputDescriptor(profile);
4005 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07004006 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
4007 config.sample_rate = desc->mSamplingRate;
4008 config.channel_mask = desc->mChannelMask;
4009 config.format = desc->mFormat;
4010 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07004011 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07004012 &input,
4013 &config,
4014 &desc->mDevice,
4015 address,
4016 AUDIO_SOURCE_MIC,
4017 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07004018
Eric Laurentcf2c0212014-07-25 16:20:43 -07004019 if (status == NO_ERROR) {
4020 desc->mSamplingRate = config.sample_rate;
4021 desc->mChannelMask = config.channel_mask;
4022 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07004023
Eric Laurentd4692962014-05-05 18:13:44 -07004024 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07004025 char *param = audio_device_address_to_parameter(device, address);
4026 mpClientInterface->setParameters(input, String8(param));
4027 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07004028 }
Phil Burk00eeb322016-03-31 12:41:00 -07004029 updateAudioProfiles(device, input, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01004030 if (!profile->hasValidAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07004031 ALOGW("checkInputsForDevice() direct input missing param");
4032 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004033 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07004034 }
4035
4036 if (input != 0) {
4037 addInput(input, desc);
4038 }
4039 } // endif input != 0
4040
Eric Laurentcf2c0212014-07-25 16:20:43 -07004041 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07004042 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07004043 profiles.removeAt(profile_index);
4044 profile_index--;
4045 } else {
4046 inputs.add(input);
Paul McLean9080a4c2015-06-18 08:24:02 -07004047 if (audio_device_is_digital(device)) {
4048 devDesc->importAudioPort(profile);
4049 }
Eric Laurentd4692962014-05-05 18:13:44 -07004050 ALOGV("checkInputsForDevice(): adding input %d", input);
4051 }
4052 } // end scan profiles
4053
4054 if (profiles.isEmpty()) {
4055 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
4056 return BAD_VALUE;
4057 }
4058 } else {
4059 // Disconnect
4060 // check if one opened input is not needed any more after disconnecting one device
4061 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
4062 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004063 if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) {
Eric Laurentd4692962014-05-05 18:13:44 -07004064 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
4065 mInputs.keyAt(input_index));
4066 inputs.add(mInputs.keyAt(input_index));
4067 }
4068 }
4069 // Clear any profiles associated with the disconnected device.
4070 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
4071 if (mHwModules[module_index]->mHandle == 0) {
4072 continue;
4073 }
4074 for (size_t profile_index = 0;
4075 profile_index < mHwModules[module_index]->mInputProfiles.size();
4076 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004077 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004078 if (profile->supportDevice(device)) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07004079 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07004080 profile_index, module_index);
François Gaffie112b0af2015-11-19 16:13:25 +01004081 profile->clearAudioProfiles();
Eric Laurentd4692962014-05-05 18:13:44 -07004082 }
4083 }
4084 }
4085 } // end disconnect
4086
4087 return NO_ERROR;
4088}
4089
4090
Eric Laurente0720872014-03-11 09:30:41 -07004091void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07004092{
4093 ALOGV("closeOutput(%d)", output);
4094
Eric Laurentc75307b2015-03-17 15:29:32 -07004095 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004096 if (outputDesc == NULL) {
4097 ALOGW("closeOutput() unknown output %d", output);
4098 return;
4099 }
François Gaffie036e1e92015-03-19 10:16:24 +01004100 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08004101
Eric Laurente552edb2014-03-10 17:42:56 -07004102 // look for duplicated outputs connected to the output being removed.
4103 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004104 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07004105 if (dupOutputDesc->isDuplicated() &&
4106 (dupOutputDesc->mOutput1 == outputDesc ||
4107 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004108 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07004109 if (dupOutputDesc->mOutput1 == outputDesc) {
4110 outputDesc2 = dupOutputDesc->mOutput2;
4111 } else {
4112 outputDesc2 = dupOutputDesc->mOutput1;
4113 }
4114 // As all active tracks on duplicated output will be deleted,
4115 // and as they were also referenced on the other output, the reference
4116 // count for their stream type must be adjusted accordingly on
4117 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07004118 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07004119 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07004120 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07004121 }
4122 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
4123 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
4124
4125 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01004126 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07004127 }
4128 }
4129
Eric Laurent05b90f82014-08-27 15:32:29 -07004130 nextAudioPortGeneration();
4131
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004132 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004133 if (index >= 0) {
4134 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07004135 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07004136 mAudioPatches.removeItemsAt(index);
4137 mpClientInterface->onAudioPatchListUpdate();
4138 }
4139
Eric Laurente552edb2014-03-10 17:42:56 -07004140 AudioParameter param;
4141 param.add(String8("closing"), String8("true"));
4142 mpClientInterface->setParameters(output, param.toString());
4143
4144 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01004145 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004146 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07004147}
4148
4149void AudioPolicyManager::closeInput(audio_io_handle_t input)
4150{
4151 ALOGV("closeInput(%d)", input);
4152
4153 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
4154 if (inputDesc == NULL) {
4155 ALOGW("closeInput() unknown input %d", input);
4156 return;
4157 }
4158
Eric Laurent6a94d692014-05-20 11:18:06 -07004159 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07004160
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004161 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004162 if (index >= 0) {
4163 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07004164 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07004165 mAudioPatches.removeItemsAt(index);
4166 mpClientInterface->onAudioPatchListUpdate();
4167 }
4168
4169 mpClientInterface->closeInput(input);
4170 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07004171}
4172
Eric Laurentc75307b2015-03-17 15:29:32 -07004173SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
4174 audio_devices_t device,
4175 SwAudioOutputCollection openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07004176{
4177 SortedVector<audio_io_handle_t> outputs;
4178
4179 ALOGVV("getOutputsForDevice() device %04x", device);
4180 for (size_t i = 0; i < openOutputs.size(); i++) {
4181 ALOGVV("output %d isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004182 i, openOutputs.valueAt(i)->isDuplicated(),
4183 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004184 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
4185 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
4186 outputs.add(openOutputs.keyAt(i));
4187 }
4188 }
4189 return outputs;
4190}
4191
Eric Laurente0720872014-03-11 09:30:41 -07004192bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01004193 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07004194{
4195 if (outputs1.size() != outputs2.size()) {
4196 return false;
4197 }
4198 for (size_t i = 0; i < outputs1.size(); i++) {
4199 if (outputs1[i] != outputs2[i]) {
4200 return false;
4201 }
4202 }
4203 return true;
4204}
4205
Eric Laurente0720872014-03-11 09:30:41 -07004206void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07004207{
4208 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
4209 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
4210 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
4211 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
4212
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004213 // also take into account external policy-related changes: add all outputs which are
4214 // associated with policies in the "before" and "after" output vectors
4215 ALOGVV("checkOutputForStrategy(): policy related outputs");
4216 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004217 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004218 if (desc != 0 && desc->mPolicyMix != NULL) {
4219 srcOutputs.add(desc->mIoHandle);
4220 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
4221 }
4222 }
4223 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004224 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004225 if (desc != 0 && desc->mPolicyMix != NULL) {
4226 dstOutputs.add(desc->mIoHandle);
4227 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
4228 }
4229 }
4230
Eric Laurente552edb2014-03-10 17:42:56 -07004231 if (!vectorsEqual(srcOutputs,dstOutputs)) {
4232 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
4233 strategy, srcOutputs[0], dstOutputs[0]);
4234 // mute strategy while moving tracks from one output to another
4235 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004236 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01004237 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004238 setStrategyMute(strategy, true, desc);
4239 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004240 }
Eric Laurentd60560a2015-04-10 11:31:20 -07004241 sp<AudioSourceDescriptor> source =
4242 getSourceForStrategyOnOutput(srcOutputs[i], strategy);
4243 if (source != 0){
4244 connectAudioSource(source);
4245 }
Eric Laurente552edb2014-03-10 17:42:56 -07004246 }
4247
4248 // Move effects associated to this strategy from previous output to new output
4249 if (strategy == STRATEGY_MEDIA) {
4250 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
4251 SortedVector<audio_io_handle_t> moved;
4252 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004253 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
4254 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
4255 effectDesc->mIo != fxOutput) {
4256 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004257 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
4258 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004259 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07004260 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004261 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07004262 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07004263 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004264 }
4265 }
4266 }
4267 // Move tracks associated to this strategy from previous output to new output
Eric Laurent794fde22016-03-11 09:50:45 -08004268 for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004269 if (getStrategy((audio_stream_type_t)i) == strategy) {
4270 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07004271 }
4272 }
4273 }
4274}
4275
Eric Laurente0720872014-03-11 09:30:41 -07004276void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07004277{
François Gaffie2110e042015-03-24 08:41:51 +01004278 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004279 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004280 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01004281 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004282 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004283 checkOutputForStrategy(STRATEGY_SONIFICATION);
4284 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004285 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07004286 checkOutputForStrategy(STRATEGY_MEDIA);
4287 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004288 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07004289}
4290
Eric Laurente0720872014-03-11 09:30:41 -07004291void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07004292{
François Gaffie53615e22015-03-19 09:24:12 +01004293 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07004294 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004295 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07004296 return;
4297 }
4298
Eric Laurent3a4311c2014-03-17 12:00:47 -07004299 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08004300 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
4301 ~AUDIO_DEVICE_BIT_IN) != 0) ||
4302 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07004303 // suspend A2DP output if:
4304 // (NOT already suspended) &&
4305 // ((SCO device is connected &&
4306 // (forced usage for communication || for record is SCO))) ||
4307 // (phone state is ringing || in call)
4308 //
4309 // restore A2DP output if:
4310 // (Already suspended) &&
4311 // ((SCO device is NOT connected ||
4312 // (forced usage NOT for communication && NOT for record is SCO))) &&
4313 // (phone state is NOT ringing && NOT in call)
4314 //
4315 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004316 if ((!isScoConnected ||
François Gaffie2110e042015-03-24 08:41:51 +01004317 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) &&
4318 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) &&
4319 ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
4320 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004321
4322 mpClientInterface->restoreOutput(a2dpOutput);
4323 mA2dpSuspended = false;
4324 }
4325 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004326 if ((isScoConnected &&
François Gaffie2110e042015-03-24 08:41:51 +01004327 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) ||
4328 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) ||
4329 ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
4330 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004331
4332 mpClientInterface->suspendOutput(a2dpOutput);
4333 mA2dpSuspended = true;
4334 }
4335 }
4336}
4337
Eric Laurentc75307b2015-03-17 15:29:32 -07004338audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
4339 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004340{
4341 audio_devices_t device = AUDIO_DEVICE_NONE;
4342
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004343 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004344 if (index >= 0) {
4345 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4346 if (patchDesc->mUid != mUidCached) {
4347 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004348 outputDesc->device(), outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004349 return outputDesc->device();
4350 }
4351 }
4352
Eric Laurente552edb2014-03-10 17:42:56 -07004353 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05004354 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004355 // use device for strategy enforced audible
4356 // 2: we are in call or the strategy phone is active on the output:
4357 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05004358 // 3: the strategy for enforced audible is active but not enforced on the output:
4359 // use the device for strategy enforced audible
Eric Laurent28d09f02016-03-08 10:43:05 -08004360 // 4: the strategy sonification is active on the output:
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004361 // use device for strategy sonification
Eric Laurent28d09f02016-03-08 10:43:05 -08004362 // 5: the strategy accessibility is active on the output:
4363 // use device for strategy accessibility
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004364 // 6: the strategy "respectful" sonification is active on the output:
4365 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08004366 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004367 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08004368 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004369 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08004370 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004371 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01004372 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004373 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07004374 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
4375 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01004376 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004377 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004378 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05004379 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004380 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004381 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Eric Laurent28d09f02016-03-08 10:43:05 -08004382 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
4383 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004384 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004385 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004386 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004387 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004388 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004389 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004390 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004391 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004392 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004393 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07004394 }
4395
Eric Laurent1c333e22014-05-20 10:48:17 -07004396 ALOGV("getNewOutputDevice() selected device %x", device);
4397 return device;
4398}
4399
Eric Laurentfb66dd92016-01-28 18:32:03 -08004400audio_devices_t AudioPolicyManager::getNewInputDevice(const sp<AudioInputDescriptor>& inputDesc)
Eric Laurent1c333e22014-05-20 10:48:17 -07004401{
Eric Laurentfb66dd92016-01-28 18:32:03 -08004402 audio_devices_t device = AUDIO_DEVICE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07004403
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004404 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004405 if (index >= 0) {
4406 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4407 if (patchDesc->mUid != mUidCached) {
4408 ALOGV("getNewInputDevice() device %08x forced by patch %d",
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004409 inputDesc->mDevice, inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004410 return inputDesc->mDevice;
4411 }
4412 }
4413
Eric Laurentfb66dd92016-01-28 18:32:03 -08004414 audio_source_t source = inputDesc->getHighestPrioritySource(true /*activeOnly*/);
4415 if (isInCall()) {
4416 device = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
4417 } else if (source != AUDIO_SOURCE_DEFAULT) {
4418 device = getDeviceAndMixForInputSource(source);
4419 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004420
Eric Laurente552edb2014-03-10 17:42:56 -07004421 return device;
4422}
4423
Eric Laurent794fde22016-03-11 09:50:45 -08004424bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1,
4425 audio_stream_type_t stream2) {
4426 return ((stream1 == stream2) ||
4427 ((stream1 == AUDIO_STREAM_ACCESSIBILITY) && (stream2 == AUDIO_STREAM_MUSIC)) ||
4428 ((stream1 == AUDIO_STREAM_MUSIC) && (stream2 == AUDIO_STREAM_ACCESSIBILITY)));
Eric Laurent28d09f02016-03-08 10:43:05 -08004429}
4430
Eric Laurente0720872014-03-11 09:30:41 -07004431uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004432 return (uint32_t)getStrategy(stream);
4433}
4434
Eric Laurente0720872014-03-11 09:30:41 -07004435audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004436 // By checking the range of stream before calling getStrategy, we avoid
4437 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
4438 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08004439 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004440 return AUDIO_DEVICE_NONE;
4441 }
Eric Laurent28d09f02016-03-08 10:43:05 -08004442 audio_devices_t devices = AUDIO_DEVICE_NONE;
Eric Laurent794fde22016-03-11 09:50:45 -08004443 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
4444 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004445 continue;
Eric Laurent6a94d692014-05-20 11:18:06 -07004446 }
Eric Laurent794fde22016-03-11 09:50:45 -08004447 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08004448 audio_devices_t curDevices =
Eric Laurent447a87b2016-07-07 17:32:10 -07004449 getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/);
Eric Laurent28d09f02016-03-08 10:43:05 -08004450 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs);
4451 for (size_t i = 0; i < outputs.size(); i++) {
4452 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurent794fde22016-03-11 09:50:45 -08004453 if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004454 curDevices |= outputDesc->device();
4455 }
4456 }
4457 devices |= curDevices;
Eric Laurente552edb2014-03-10 17:42:56 -07004458 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05004459
4460 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
4461 and doesn't really need to.*/
4462 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
4463 devices |= AUDIO_DEVICE_OUT_SPEAKER;
4464 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4465 }
Eric Laurente552edb2014-03-10 17:42:56 -07004466 return devices;
4467}
4468
François Gaffie2110e042015-03-24 08:41:51 +01004469routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
4470{
Eric Laurent223fd5c2014-11-11 13:43:36 -08004471 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01004472 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004473}
4474
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004475uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4476 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004477 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4478 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4479 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004480 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4481 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4482 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004483 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01004484 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004485}
4486
Eric Laurente0720872014-03-11 09:30:41 -07004487void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004488 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004489 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004490 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4491 updateDevicesAndOutputs();
4492 break;
4493 default:
4494 break;
4495 }
4496}
4497
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004498uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
Eric Laurent9459fb02015-08-12 18:36:32 -07004499
4500 // skip beacon mute management if a dedicated TTS output is available
4501 if (mTtsOutputAvailable) {
4502 return 0;
4503 }
4504
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004505 switch(event) {
4506 case STARTING_OUTPUT:
4507 mBeaconMuteRefCount++;
4508 break;
4509 case STOPPING_OUTPUT:
4510 if (mBeaconMuteRefCount > 0) {
4511 mBeaconMuteRefCount--;
4512 }
4513 break;
4514 case STARTING_BEACON:
4515 mBeaconPlayingRefCount++;
4516 break;
4517 case STOPPING_BEACON:
4518 if (mBeaconPlayingRefCount > 0) {
4519 mBeaconPlayingRefCount--;
4520 }
4521 break;
4522 }
4523
4524 if (mBeaconMuteRefCount > 0) {
4525 // any playback causes beacon to be muted
4526 return setBeaconMute(true);
4527 } else {
4528 // no other playback: unmute when beacon starts playing, mute when it stops
4529 return setBeaconMute(mBeaconPlayingRefCount == 0);
4530 }
4531}
4532
4533uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4534 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4535 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4536 // keep track of muted state to avoid repeating mute/unmute operations
4537 if (mBeaconMuted != mute) {
4538 // mute/unmute AUDIO_STREAM_TTS on all outputs
4539 ALOGV("\t muting %d", mute);
4540 uint32_t maxLatency = 0;
4541 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004542 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004543 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004544 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004545 0 /*delay*/, AUDIO_DEVICE_NONE);
4546 const uint32_t latency = desc->latency() * 2;
4547 if (latency > maxLatency) {
4548 maxLatency = latency;
4549 }
4550 }
4551 mBeaconMuted = mute;
4552 return maxLatency;
4553 }
4554 return 0;
4555}
4556
Eric Laurente0720872014-03-11 09:30:41 -07004557audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004558 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004559{
Paul McLeanaa981192015-03-21 09:55:15 -07004560 // Routing
4561 // see if we have an explicit route
4562 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4563 // (getStrategy(stream)).
4564 // if the strategy from the stream type in the RouteMap is the same as the argument above,
4565 // and activity count is non-zero
4566 // the device = the device from the descriptor in the RouteMap, and exit.
4567 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4568 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
Eric Laurent28d09f02016-03-08 10:43:05 -08004569 routing_strategy routeStrategy = getStrategy(route->mStreamType);
4570 if ((routeStrategy == strategy) && route->isActive()) {
Paul McLeanaa981192015-03-21 09:55:15 -07004571 return route->mDeviceDescriptor->type();
4572 }
4573 }
4574
Eric Laurente552edb2014-03-10 17:42:56 -07004575 if (fromCache) {
4576 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4577 strategy, mDeviceForStrategy[strategy]);
4578 return mDeviceForStrategy[strategy];
4579 }
François Gaffie2110e042015-03-24 08:41:51 +01004580 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004581}
4582
Eric Laurente0720872014-03-11 09:30:41 -07004583void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004584{
4585 for (int i = 0; i < NUM_STRATEGIES; i++) {
4586 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4587 }
4588 mPreviousOutputs = mOutputs;
4589}
4590
Eric Laurent1f2f2232014-06-02 12:01:23 -07004591uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004592 audio_devices_t prevDevice,
4593 uint32_t delayMs)
4594{
4595 // mute/unmute strategies using an incompatible device combination
4596 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4597 // if unmuting, unmute only after the specified delay
4598 if (outputDesc->isDuplicated()) {
4599 return 0;
4600 }
4601
4602 uint32_t muteWaitMs = 0;
4603 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004604 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004605
4606 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4607 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004608 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004609 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4610 bool doMute = false;
4611
4612 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4613 doMute = true;
4614 outputDesc->mStrategyMutedByDevice[i] = true;
4615 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4616 doMute = true;
4617 outputDesc->mStrategyMutedByDevice[i] = false;
4618 }
Eric Laurent99401132014-05-07 19:48:15 -07004619 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004620 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004621 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004622 // skip output if it does not share any device with current output
4623 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4624 == AUDIO_DEVICE_NONE) {
4625 continue;
4626 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004627 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)",
4628 mute ? "muting" : "unmuting", i, curDevice);
4629 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004630 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004631 if (mute) {
4632 // FIXME: should not need to double latency if volume could be applied
4633 // immediately by the audioflinger mixer. We must account for the delay
4634 // between now and the next time the audioflinger thread for this output
4635 // will process a buffer (which corresponds to one buffer size,
4636 // usually 1/2 or 1/4 of the latency).
4637 if (muteWaitMs < desc->latency() * 2) {
4638 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004639 }
4640 }
4641 }
4642 }
4643 }
4644 }
4645
Eric Laurent99401132014-05-07 19:48:15 -07004646 // temporary mute output if device selection changes to avoid volume bursts due to
4647 // different per device volumes
4648 if (outputDesc->isActive() && (device != prevDevice)) {
4649 if (muteWaitMs < outputDesc->latency() * 2) {
4650 muteWaitMs = outputDesc->latency() * 2;
4651 }
4652 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004653 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004654 setStrategyMute((routing_strategy)i, true, outputDesc);
Eric Laurent99401132014-05-07 19:48:15 -07004655 // do tempMute unmute after twice the mute wait time
Eric Laurentc75307b2015-03-17 15:29:32 -07004656 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurent99401132014-05-07 19:48:15 -07004657 muteWaitMs *2, device);
4658 }
4659 }
4660 }
4661
Eric Laurente552edb2014-03-10 17:42:56 -07004662 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4663 if (muteWaitMs > delayMs) {
4664 muteWaitMs -= delayMs;
4665 usleep(muteWaitMs * 1000);
4666 return muteWaitMs;
4667 }
4668 return 0;
4669}
4670
Eric Laurentc75307b2015-03-17 15:29:32 -07004671uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004672 audio_devices_t device,
4673 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004674 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004675 audio_patch_handle_t *patchHandle,
4676 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004677{
Eric Laurentc75307b2015-03-17 15:29:32 -07004678 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004679 AudioParameter param;
4680 uint32_t muteWaitMs;
4681
4682 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004683 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4684 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004685 return muteWaitMs;
4686 }
4687 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4688 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004689 if ((device != AUDIO_DEVICE_NONE) &&
4690 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004691 return 0;
4692 }
4693
4694 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004695 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004696
4697 audio_devices_t prevDevice = outputDesc->mDevice;
4698
Paul McLeanaa981192015-03-21 09:55:15 -07004699 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004700
4701 if (device != AUDIO_DEVICE_NONE) {
4702 outputDesc->mDevice = device;
4703 }
4704 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4705
4706 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004707 // the requested device is AUDIO_DEVICE_NONE
4708 // OR the requested device is the same as current device
4709 // AND force is not specified
4710 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004711 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004712 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4713 !force &&
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004714 outputDesc->getPatchHandle() != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004715 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004716 return muteWaitMs;
4717 }
4718
4719 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004720
Eric Laurente552edb2014-03-10 17:42:56 -07004721 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004722 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004723 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004724 } else {
Eric Laurentc40d9692016-04-13 19:14:13 -07004725 DeviceVector deviceList;
4726 if ((address == NULL) || (strlen(address) == 0)) {
4727 deviceList = mAvailableOutputDevices.getDevicesFromType(device);
4728 } else {
4729 deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
4730 }
4731
Eric Laurent1c333e22014-05-20 10:48:17 -07004732 if (!deviceList.isEmpty()) {
4733 struct audio_patch patch;
4734 outputDesc->toAudioPortConfig(&patch.sources[0]);
4735 patch.num_sources = 1;
4736 patch.num_sinks = 0;
4737 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4738 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004739 patch.num_sinks++;
4740 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004741 ssize_t index;
4742 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4743 index = mAudioPatches.indexOfKey(*patchHandle);
4744 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004745 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004746 }
4747 sp< AudioPatch> patchDesc;
4748 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4749 if (index >= 0) {
4750 patchDesc = mAudioPatches.valueAt(index);
4751 afPatchHandle = patchDesc->mAfPatchHandle;
4752 }
4753
Eric Laurent1c333e22014-05-20 10:48:17 -07004754 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004755 &afPatchHandle,
4756 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004757 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4758 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004759 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004760 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004761 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004762 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004763 addAudioPatch(patchDesc->mHandle, patchDesc);
4764 } else {
4765 patchDesc->mPatch = patch;
4766 }
4767 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004768 if (patchHandle) {
4769 *patchHandle = patchDesc->mHandle;
4770 }
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004771 outputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004772 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004773 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004774 }
4775 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004776
4777 // inform all input as well
4778 for (size_t i = 0; i < mInputs.size(); i++) {
4779 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004780 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004781 AudioParameter inputCmd = AudioParameter();
4782 ALOGV("%s: inform input %d of device:%d", __func__,
4783 inputDescriptor->mIoHandle, device);
4784 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4785 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4786 inputCmd.toString(),
4787 delayMs);
4788 }
4789 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004790 }
Eric Laurente552edb2014-03-10 17:42:56 -07004791
4792 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004793 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004794
4795 return muteWaitMs;
4796}
4797
Eric Laurentc75307b2015-03-17 15:29:32 -07004798status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004799 int delayMs,
4800 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004801{
Eric Laurent6a94d692014-05-20 11:18:06 -07004802 ssize_t index;
4803 if (patchHandle) {
4804 index = mAudioPatches.indexOfKey(*patchHandle);
4805 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004806 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004807 }
4808 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004809 return INVALID_OPERATION;
4810 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004811 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4812 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004813 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07004814 outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07004815 removeAudioPatch(patchDesc->mHandle);
4816 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004817 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004818 return status;
4819}
4820
4821status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4822 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004823 bool force,
4824 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004825{
4826 status_t status = NO_ERROR;
4827
Eric Laurent1f2f2232014-06-02 12:01:23 -07004828 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004829 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4830 inputDesc->mDevice = device;
4831
4832 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4833 if (!deviceList.isEmpty()) {
4834 struct audio_patch patch;
4835 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004836 // AUDIO_SOURCE_HOTWORD is for internal use only:
4837 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004838 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
Eric Laurent599c7582015-12-07 18:05:55 -08004839 !inputDesc->isSoundTrigger()) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004840 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4841 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004842 patch.num_sinks = 1;
4843 //only one input device for now
4844 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004845 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004846 ssize_t index;
4847 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4848 index = mAudioPatches.indexOfKey(*patchHandle);
4849 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004850 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004851 }
4852 sp< AudioPatch> patchDesc;
4853 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4854 if (index >= 0) {
4855 patchDesc = mAudioPatches.valueAt(index);
4856 afPatchHandle = patchDesc->mAfPatchHandle;
4857 }
4858
Eric Laurent1c333e22014-05-20 10:48:17 -07004859 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004860 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004861 0);
4862 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004863 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004864 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004865 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004866 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004867 addAudioPatch(patchDesc->mHandle, patchDesc);
4868 } else {
4869 patchDesc->mPatch = patch;
4870 }
4871 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004872 if (patchHandle) {
4873 *patchHandle = patchDesc->mHandle;
4874 }
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004875 inputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004876 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004877 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004878 }
4879 }
4880 }
4881 return status;
4882}
4883
Eric Laurent6a94d692014-05-20 11:18:06 -07004884status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4885 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004886{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004887 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004888 ssize_t index;
4889 if (patchHandle) {
4890 index = mAudioPatches.indexOfKey(*patchHandle);
4891 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004892 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004893 }
4894 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004895 return INVALID_OPERATION;
4896 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004897 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4898 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004899 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07004900 inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07004901 removeAudioPatch(patchDesc->mHandle);
4902 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004903 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004904 return status;
4905}
4906
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004907sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01004908 String8 address,
4909 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07004910 audio_format_t& format,
4911 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01004912 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004913{
4914 // Choose an input profile based on the requested capture parameters: select the first available
4915 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07004916 //
4917 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
4918 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07004919
4920 for (size_t i = 0; i < mHwModules.size(); i++)
4921 {
4922 if (mHwModules[i]->mHandle == 0) {
4923 continue;
4924 }
4925 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4926 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004927 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004928 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004929 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07004930 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07004931 format,
4932 &format /*updatedFormat*/,
4933 channelMask,
4934 &channelMask /*updatedChannelMask*/,
4935 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004936
Eric Laurente552edb2014-03-10 17:42:56 -07004937 return profile;
4938 }
4939 }
4940 }
4941 return NULL;
4942}
4943
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004944
4945audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01004946 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07004947{
François Gaffie036e1e92015-03-19 10:16:24 +01004948 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
4949 audio_devices_t selectedDeviceFromMix =
4950 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08004951
François Gaffie036e1e92015-03-19 10:16:24 +01004952 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
4953 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08004954 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004955 return getDeviceForInputSource(inputSource);
4956}
4957
4958audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
4959{
Paul McLean466dc8e2015-04-17 13:15:36 -06004960 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
4961 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004962 if (inputSource == route->mSource && route->isActive()) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004963 return route->mDeviceDescriptor->type();
4964 }
4965 }
4966
4967 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07004968}
4969
Eric Laurente0720872014-03-11 09:30:41 -07004970float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004971 int index,
4972 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004973{
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004974 float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index);
Eric Laurente552edb2014-03-10 17:42:56 -07004975 // if a headset is connected, apply the following rules to ring tones and notifications
4976 // to avoid sound level bursts in user's ears:
Eric Laurent6af1c1d2016-04-14 11:20:44 -07004977 // - always attenuate notifications volume by 6dB
4978 // - attenuate ring tones volume by 6dB unless music is not playing and
4979 // speaker is part of the select devices
Eric Laurente552edb2014-03-10 17:42:56 -07004980 // - if music is playing, always limit the volume to current music volume,
4981 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004982 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004983 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4984 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4985 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4986 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4987 ((stream_strategy == STRATEGY_SONIFICATION)
4988 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004989 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004990 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004991 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
François Gaffied1ab2bd2015-12-02 18:20:06 +01004992 mVolumeCurves->canBeMuted(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004993 // when the phone is ringing we must consider that music could have been paused just before
4994 // by the music application and behave as if music was active if the last music track was
4995 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004996 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004997 mLimitRingtoneVolume) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004998 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004999 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07005000 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005001 mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC,
5002 musicDevice),
5003 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07005004 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
5005 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005006 if (volumeDB > minVolDB) {
5007 volumeDB = minVolDB;
Eric Laurentffbc80f2015-03-18 18:30:19 -07005008 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07005009 }
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005010 if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
5011 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) {
5012 // on A2DP, also ensure notification volume is not too low compared to media when
5013 // intended to be played
5014 if ((volumeDB > -96.0f) &&
5015 (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) {
5016 ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f",
5017 stream, device,
5018 volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB);
5019 volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB;
5020 }
5021 }
Eric Laurent6af1c1d2016-04-14 11:20:44 -07005022 } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) ||
5023 stream_strategy != STRATEGY_SONIFICATION) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005024 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07005025 }
5026 }
5027
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005028 return volumeDB;
Eric Laurente552edb2014-03-10 17:42:56 -07005029}
5030
Eric Laurente0720872014-03-11 09:30:41 -07005031status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005032 int index,
5033 const sp<AudioOutputDescriptor>& outputDesc,
5034 audio_devices_t device,
5035 int delayMs,
5036 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005037{
Eric Laurente552edb2014-03-10 17:42:56 -07005038 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07005039 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07005040 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07005041 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07005042 return NO_ERROR;
5043 }
François Gaffie2110e042015-03-24 08:41:51 +01005044 audio_policy_forced_cfg_t forceUseForComm =
5045 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07005046 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01005047 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
5048 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005049 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01005050 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07005051 return INVALID_OPERATION;
5052 }
5053
Eric Laurentc75307b2015-03-17 15:29:32 -07005054 if (device == AUDIO_DEVICE_NONE) {
5055 device = outputDesc->device();
5056 }
Eric Laurent275e8e92014-11-30 15:14:47 -08005057
Eric Laurentffbc80f2015-03-18 18:30:19 -07005058 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07005059 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07005060 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08005061 }
Eric Laurentc75307b2015-03-17 15:29:32 -07005062
Eric Laurentffbc80f2015-03-18 18:30:19 -07005063 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07005064
Eric Laurent3b73df72014-03-11 09:06:29 -07005065 if (stream == AUDIO_STREAM_VOICE_CALL ||
5066 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07005067 float voiceVolume;
5068 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07005069 if (stream == AUDIO_STREAM_VOICE_CALL) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005070 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005071 } else {
5072 voiceVolume = 1.0;
5073 }
5074
Eric Laurent18fba842016-03-31 14:41:26 -07005075 if (voiceVolume != mLastVoiceVolume) {
Eric Laurente552edb2014-03-10 17:42:56 -07005076 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
5077 mLastVoiceVolume = voiceVolume;
5078 }
5079 }
5080
5081 return NO_ERROR;
5082}
5083
Eric Laurentc75307b2015-03-17 15:29:32 -07005084void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
5085 audio_devices_t device,
5086 int delayMs,
5087 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005088{
Eric Laurentc75307b2015-03-17 15:29:32 -07005089 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07005090
Eric Laurent794fde22016-03-11 09:50:45 -08005091 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005092 checkAndSetVolume((audio_stream_type_t)stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005093 mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005094 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005095 device,
5096 delayMs,
5097 force);
5098 }
5099}
5100
Eric Laurente0720872014-03-11 09:30:41 -07005101void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07005102 bool on,
5103 const sp<AudioOutputDescriptor>& outputDesc,
5104 int delayMs,
5105 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005106{
Eric Laurent72249d52015-06-08 11:12:15 -07005107 ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d",
5108 strategy, on, outputDesc->getId());
Eric Laurent794fde22016-03-11 09:50:45 -08005109 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005110 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005111 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07005112 }
5113 }
5114}
5115
Eric Laurente0720872014-03-11 09:30:41 -07005116void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005117 bool on,
5118 const sp<AudioOutputDescriptor>& outputDesc,
5119 int delayMs,
5120 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005121{
Eric Laurente552edb2014-03-10 17:42:56 -07005122 if (device == AUDIO_DEVICE_NONE) {
5123 device = outputDesc->device();
5124 }
5125
Eric Laurentc75307b2015-03-17 15:29:32 -07005126 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
5127 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07005128
5129 if (on) {
5130 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005131 if (mVolumeCurves->canBeMuted(stream) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07005132 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01005133 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005134 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005135 }
5136 }
5137 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
5138 outputDesc->mMuteCount[stream]++;
5139 } else {
5140 if (outputDesc->mMuteCount[stream] == 0) {
5141 ALOGV("setStreamMute() unmuting non muted stream!");
5142 return;
5143 }
5144 if (--outputDesc->mMuteCount[stream] == 0) {
5145 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005146 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005147 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005148 device,
5149 delayMs);
5150 }
5151 }
5152}
5153
Eric Laurente0720872014-03-11 09:30:41 -07005154void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07005155 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07005156{
Eric Laurent87ffa392015-05-22 10:32:38 -07005157 if(!hasPrimaryOutput()) {
5158 return;
5159 }
5160
Eric Laurente552edb2014-03-10 17:42:56 -07005161 // if the stream pertains to sonification strategy and we are in call we must
5162 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
5163 // in the device used for phone strategy and play the tone if the selected device does not
5164 // interfere with the device used for phone strategy
5165 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
5166 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07005167 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005168 if ((stream_strategy == STRATEGY_SONIFICATION) ||
5169 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005170 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07005171 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
5172 stream, starting, outputDesc->mDevice, stateChange);
5173 if (outputDesc->mRefCount[stream]) {
5174 int muteCount = 1;
5175 if (stateChange) {
5176 muteCount = outputDesc->mRefCount[stream];
5177 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005178 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005179 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
5180 for (int i = 0; i < muteCount; i++) {
5181 setStreamMute(stream, starting, mPrimaryOutput);
5182 }
5183 } else {
5184 ALOGV("handleIncallSonification() high visibility");
5185 if (outputDesc->device() &
5186 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
5187 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
5188 for (int i = 0; i < muteCount; i++) {
5189 setStreamMute(stream, starting, mPrimaryOutput);
5190 }
5191 }
5192 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005193 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
5194 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07005195 } else {
5196 mpClientInterface->stopTone();
5197 }
5198 }
5199 }
5200 }
5201}
5202
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005203audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
5204{
5205 // flags to stream type mapping
5206 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
5207 return AUDIO_STREAM_ENFORCED_AUDIBLE;
5208 }
5209 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
5210 return AUDIO_STREAM_BLUETOOTH_SCO;
5211 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08005212 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
5213 return AUDIO_STREAM_TTS;
5214 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005215
5216 // usage to stream type mapping
5217 switch (attr->usage) {
5218 case AUDIO_USAGE_MEDIA:
5219 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005220 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5221 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08005222 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5223 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005224 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5225 return AUDIO_STREAM_SYSTEM;
5226 case AUDIO_USAGE_VOICE_COMMUNICATION:
5227 return AUDIO_STREAM_VOICE_CALL;
5228
5229 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5230 return AUDIO_STREAM_DTMF;
5231
5232 case AUDIO_USAGE_ALARM:
5233 return AUDIO_STREAM_ALARM;
5234 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5235 return AUDIO_STREAM_RING;
5236
5237 case AUDIO_USAGE_NOTIFICATION:
5238 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5239 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5240 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5241 case AUDIO_USAGE_NOTIFICATION_EVENT:
5242 return AUDIO_STREAM_NOTIFICATION;
5243
5244 case AUDIO_USAGE_UNKNOWN:
5245 default:
5246 return AUDIO_STREAM_MUSIC;
5247 }
5248}
Eric Laurente83b55d2014-11-14 10:06:21 -08005249
François Gaffie53615e22015-03-19 09:24:12 +01005250bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
5251{
Eric Laurente83b55d2014-11-14 10:06:21 -08005252 // has flags that map to a strategy?
5253 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
5254 return true;
5255 }
5256
5257 // has known usage?
5258 switch (paa->usage) {
5259 case AUDIO_USAGE_UNKNOWN:
5260 case AUDIO_USAGE_MEDIA:
5261 case AUDIO_USAGE_VOICE_COMMUNICATION:
5262 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5263 case AUDIO_USAGE_ALARM:
5264 case AUDIO_USAGE_NOTIFICATION:
5265 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5266 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5267 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5268 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5269 case AUDIO_USAGE_NOTIFICATION_EVENT:
5270 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5271 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5272 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5273 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08005274 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08005275 break;
5276 default:
5277 return false;
5278 }
5279 return true;
5280}
5281
François Gaffiead3183e2015-03-18 16:55:35 +01005282bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc,
5283 routing_strategy strategy, uint32_t inPastMs,
5284 nsecs_t sysTime) const
5285{
5286 if ((sysTime == 0) && (inPastMs != 0)) {
5287 sysTime = systemTime();
5288 }
Eric Laurent794fde22016-03-11 09:50:45 -08005289 for (int i = 0; i < (int)AUDIO_STREAM_FOR_POLICY_CNT; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01005290 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
5291 (NUM_STRATEGIES == strategy)) &&
5292 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
5293 return true;
5294 }
5295 }
5296 return false;
5297}
5298
François Gaffie2110e042015-03-24 08:41:51 +01005299audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
5300{
5301 return mEngine->getForceUse(usage);
5302}
5303
5304bool AudioPolicyManager::isInCall()
5305{
5306 return isStateInCall(mEngine->getPhoneState());
5307}
5308
5309bool AudioPolicyManager::isStateInCall(int state)
5310{
5311 return is_state_in_call(state);
5312}
5313
Eric Laurentd60560a2015-04-10 11:31:20 -07005314void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc)
5315{
5316 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
5317 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
5318 if (sourceDesc->mDevice->equals(deviceDesc)) {
5319 ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle());
5320 stopAudioSource(sourceDesc->getHandle());
5321 }
5322 }
5323
5324 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
5325 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
5326 bool release = false;
5327 for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) {
5328 const struct audio_port_config *source = &patchDesc->mPatch.sources[j];
5329 if (source->type == AUDIO_PORT_TYPE_DEVICE &&
5330 source->ext.device.type == deviceDesc->type()) {
5331 release = true;
5332 }
5333 }
5334 for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) {
5335 const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j];
5336 if (sink->type == AUDIO_PORT_TYPE_DEVICE &&
5337 sink->ext.device.type == deviceDesc->type()) {
5338 release = true;
5339 }
5340 }
5341 if (release) {
5342 ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle);
5343 releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid);
5344 }
5345 }
5346}
5347
Phil Burk09bc4612016-02-24 15:58:15 -08005348// Modify the list of surround sound formats supported.
Phil Burk0709b0a2016-03-31 12:54:57 -07005349void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) {
5350 FormatVector &formats = *formatsPtr;
Phil Burk07ac1142016-03-25 13:39:29 -07005351 // TODO Set this based on Config properties.
5352 const bool alwaysForceAC3 = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005353
5354 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5355 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
Phil Burk0709b0a2016-03-31 12:54:57 -07005356 ALOGD("%s: forced use = %d", __FUNCTION__, forceUse);
Phil Burk09bc4612016-02-24 15:58:15 -08005357
5358 // Analyze original support for various formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005359 bool supportsAC3 = false;
5360 bool supportsOtherSurround = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005361 bool supportsIEC61937 = false;
5362 for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) {
5363 audio_format_t format = formats[formatIndex];
Phil Burk09bc4612016-02-24 15:58:15 -08005364 switch (format) {
5365 case AUDIO_FORMAT_AC3:
Phil Burk07ac1142016-03-25 13:39:29 -07005366 supportsAC3 = true;
5367 break;
Phil Burk09bc4612016-02-24 15:58:15 -08005368 case AUDIO_FORMAT_E_AC3:
5369 case AUDIO_FORMAT_DTS:
5370 case AUDIO_FORMAT_DTS_HD:
Phil Burk07ac1142016-03-25 13:39:29 -07005371 supportsOtherSurround = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005372 break;
5373 case AUDIO_FORMAT_IEC61937:
5374 supportsIEC61937 = true;
5375 break;
5376 default:
5377 break;
5378 }
5379 }
Phil Burk09bc4612016-02-24 15:58:15 -08005380
5381 // Modify formats based on surround preferences.
5382 // If NEVER, remove support for surround formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005383 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5384 if (supportsAC3 || supportsOtherSurround || supportsIEC61937) {
5385 // Remove surround sound related formats.
5386 for (size_t formatIndex = 0; formatIndex < formats.size(); ) {
5387 audio_format_t format = formats[formatIndex];
5388 switch(format) {
5389 case AUDIO_FORMAT_AC3:
5390 case AUDIO_FORMAT_E_AC3:
5391 case AUDIO_FORMAT_DTS:
5392 case AUDIO_FORMAT_DTS_HD:
5393 case AUDIO_FORMAT_IEC61937:
Phil Burk07ac1142016-03-25 13:39:29 -07005394 formats.removeAt(formatIndex);
5395 break;
5396 default:
5397 formatIndex++; // keep it
5398 break;
5399 }
Phil Burk09bc4612016-02-24 15:58:15 -08005400 }
Phil Burk07ac1142016-03-25 13:39:29 -07005401 supportsAC3 = false;
5402 supportsOtherSurround = false;
5403 supportsIEC61937 = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005404 }
Phil Burk07ac1142016-03-25 13:39:29 -07005405 } else { // AUTO or ALWAYS
5406 // Most TVs support AC3 even if they do not report it in the EDID.
5407 if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS))
5408 && !supportsAC3) {
5409 formats.add(AUDIO_FORMAT_AC3);
5410 supportsAC3 = true;
5411 }
5412
5413 // If ALWAYS, add support for raw surround formats if all are missing.
5414 // This assumes that if any of these formats are reported by the HAL
5415 // then the report is valid and should not be modified.
5416 if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)
5417 && !supportsOtherSurround) {
5418 formats.add(AUDIO_FORMAT_E_AC3);
5419 formats.add(AUDIO_FORMAT_DTS);
5420 formats.add(AUDIO_FORMAT_DTS_HD);
5421 supportsOtherSurround = true;
5422 }
5423
5424 // Add support for IEC61937 if any raw surround supported.
5425 // The HAL could do this but add it here, just in case.
5426 if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) {
5427 formats.add(AUDIO_FORMAT_IEC61937);
5428 supportsIEC61937 = true;
5429 }
Phil Burk09bc4612016-02-24 15:58:15 -08005430 }
Phil Burk0709b0a2016-03-31 12:54:57 -07005431}
5432
5433// Modify the list of channel masks supported.
5434void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) {
5435 ChannelsVector &channelMasks = *channelMasksPtr;
5436 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5437 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
5438
5439 // If NEVER, then remove support for channelMasks > stereo.
5440 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5441 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) {
5442 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5443 if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) {
5444 ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask);
5445 channelMasks.removeAt(maskIndex);
5446 } else {
5447 maskIndex++;
5448 }
5449 }
5450 // If ALWAYS, then make sure we at least support 5.1
5451 } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) {
5452 bool supports5dot1 = false;
5453 // Are there any channel masks that can be considered "surround"?
5454 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); maskIndex++) {
5455 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5456 if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) {
5457 supports5dot1 = true;
5458 break;
5459 }
5460 }
5461 // If not then add 5.1 support.
5462 if (!supports5dot1) {
5463 channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1);
5464 ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__);
5465 }
Phil Burk09bc4612016-02-24 15:58:15 -08005466 }
5467}
5468
Phil Burk00eeb322016-03-31 12:41:00 -07005469void AudioPolicyManager::updateAudioProfiles(audio_devices_t device,
5470 audio_io_handle_t ioHandle,
François Gaffie112b0af2015-11-19 16:13:25 +01005471 AudioProfileVector &profiles)
5472{
5473 String8 reply;
Phil Burk0709b0a2016-03-31 12:54:57 -07005474
François Gaffie112b0af2015-11-19 16:13:25 +01005475 // Format MUST be checked first to update the list of AudioProfile
5476 if (profiles.hasDynamicFormat()) {
5477 reply = mpClientInterface->getParameters(ioHandle,
5478 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
Phil Burk0709b0a2016-03-31 12:54:57 -07005479 ALOGV("%s: supported formats %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005480 AudioParameter repliedParameters(reply);
5481 if (repliedParameters.get(
5482 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS), reply) != NO_ERROR) {
François Gaffie112b0af2015-11-19 16:13:25 +01005483 ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__);
5484 return;
5485 }
Phil Burk09bc4612016-02-24 15:58:15 -08005486 FormatVector formats = formatsFromString(reply.string());
Phil Burk00eeb322016-03-31 12:41:00 -07005487 if (device == AUDIO_DEVICE_OUT_HDMI) {
Phil Burk0709b0a2016-03-31 12:54:57 -07005488 filterSurroundFormats(&formats);
Phil Burk00eeb322016-03-31 12:41:00 -07005489 }
Phil Burk09bc4612016-02-24 15:58:15 -08005490 profiles.setFormats(formats);
François Gaffie112b0af2015-11-19 16:13:25 +01005491 }
5492 const FormatVector &supportedFormats = profiles.getSupportedFormats();
5493
Eric Laurent20eb3a42016-01-26 18:39:17 -08005494 for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) {
François Gaffie112b0af2015-11-19 16:13:25 +01005495 audio_format_t format = supportedFormats[formatIndex];
Eric Laurent20eb3a42016-01-26 18:39:17 -08005496 ChannelsVector channelMasks;
5497 SampleRateVector samplingRates;
François Gaffie112b0af2015-11-19 16:13:25 +01005498 AudioParameter requestedParameters;
5499 requestedParameters.addInt(String8(AUDIO_PARAMETER_STREAM_FORMAT), format);
5500
5501 if (profiles.hasDynamicRateFor(format)) {
5502 reply = mpClientInterface->getParameters(ioHandle,
5503 requestedParameters.toString() + ";" +
5504 AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES);
5505 ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005506 AudioParameter repliedParameters(reply);
5507 if (repliedParameters.get(
5508 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES), reply) == NO_ERROR) {
5509 samplingRates = samplingRatesFromString(reply.string());
François Gaffie112b0af2015-11-19 16:13:25 +01005510 }
5511 }
5512 if (profiles.hasDynamicChannelsFor(format)) {
5513 reply = mpClientInterface->getParameters(ioHandle,
5514 requestedParameters.toString() + ";" +
5515 AUDIO_PARAMETER_STREAM_SUP_CHANNELS);
5516 ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005517 AudioParameter repliedParameters(reply);
5518 if (repliedParameters.get(
5519 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS), reply) == NO_ERROR) {
5520 channelMasks = channelMasksFromString(reply.string());
Phil Burk0709b0a2016-03-31 12:54:57 -07005521 if (device == AUDIO_DEVICE_OUT_HDMI) {
5522 filterSurroundChannelMasks(&channelMasks);
5523 }
François Gaffie112b0af2015-11-19 16:13:25 +01005524 }
5525 }
Eric Laurent20eb3a42016-01-26 18:39:17 -08005526 profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates));
François Gaffie112b0af2015-11-19 16:13:25 +01005527 }
5528}
Eric Laurentd60560a2015-04-10 11:31:20 -07005529
Eric Laurente552edb2014-03-10 17:42:56 -07005530}; // namespace android